]> git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/meshtex/GenericMainMenu.h
* added MeshTex plugin src to project (can't compile)
[xonotic/netradiant.git] / contrib / meshtex / GenericMainMenu.h
1 /**
2  * @file GenericMainMenu.h
3  * Declares the GenericMainMenu class.
4  * @ingroup generic-ui
5  */
6
7 /*
8  * Copyright 2012 Joel Baxter
9  *
10  * This file is part of MeshTex.
11  *
12  * MeshTex is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * MeshTex is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with MeshTex.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26 #if !defined(INCLUDED_GENERICMAINMENU_H)
27 #define INCLUDED_GENERICMAINMENU_H
28
29 #include <string>
30 #include <map>
31
32 #include "RefCounted.h"
33 #include "GenericDialog.h"
34
35 #include "generic/callback.h"
36 #include "generic/referencecounted.h"
37
38 /**
39  * String used in lists of command tokens or command labels to separate groups
40  * of commands.
41  */
42 #define MENU_SEPARATOR "-"
43
44 /**
45  * Framework for a Radiant plugin's main menu. This object handles the menu
46  * logic: what commands exist and how to execute them. The actual menu
47  * display is handled by Radiant.
48  * 
49  * A subclass should handle creating the command list.
50  *
51  * @ingroup generic-ui
52  */
53 class GenericMainMenu : public RefCounted
54 {
55 protected: // protected methods
56
57    /// @name Lifecycle
58    //@{
59    GenericMainMenu();
60    virtual ~GenericMainMenu();
61    //@}
62
63 private: // private methods
64
65    /// @name Unimplemented to prevent copy/assignment
66    //@{
67    GenericMainMenu(const GenericMainMenu&);
68    const GenericMainMenu& operator=(const GenericMainMenu&);
69    //@}
70
71 public: // public methods
72
73    /// @name Service the plugin interface
74    //@{
75    virtual void Dispatch(const char *command);
76    virtual void CommandDialogShow(const std::string& commandString);
77    const std::string& GetCommandList() const;
78    const std::string& GetCommandLabelList() const;
79    //@}
80
81 protected: // protected types
82    /**
83     * Function signature for a menu command callback. The callback takes a
84     * string argument (the command token); it has no return value.
85     */
86    typedef Callback1<const std::string&, void> CommandCallback;
87
88    /**
89     * An instance of this class can be used as a
90     * GenericMainMenu::CommandCallback, in situations where the callback is a
91     * method to be invoked on a target object. When invoking this constructor,
92     * the target object is the constructor argument, and the target object class
93     * and method are template parameters. The target object's method must have
94     * an appropriate signature for CommandCallback: one string argument, void
95     * return.
96     */
97    template<typename ObjectClass, void (ObjectClass::*member)(const std::string&)>
98    class CommandCallbackMethod :
99       public MemberCaller1<ObjectClass, const std::string&, member>
100    {
101    public:
102       /**
103        * Constructor.
104        *
105        * @param object The object on which to invoke the callback method.
106        */
107       CommandCallbackMethod(ObjectClass& object) :
108          MemberCaller1<ObjectClass, const std::string&, member>(object) {}
109    };
110
111 protected: // protected methods
112
113    /// @name Command list construction
114    //@{
115    virtual void BeginEntries();
116    virtual void AddSeparator();
117    virtual std::string AddEntry(const char *commandLabel,
118                                 const char *command,
119                                 const CommandCallback& commandCallback);
120    virtual void AddDialogShowEntry(const char *commandLabel,
121                                    const char *command,
122                                    const SmartPointer<GenericDialog>& dialog);
123    virtual void EndEntries();
124    //@}
125
126 private: // private types
127
128    /**
129     * Type for a map between string and reference-counted dialog window.
130     */
131    typedef std::map<std::string, SmartPointer<GenericDialog> > DialogMap;
132
133 private: // private member vars
134
135    /**
136     * Semicolon-separated string of command tokens.
137     */
138    std::string _menuCommands;
139
140    /**
141     * Semicolon-separated string of command labels.
142     */
143    std::string _menuCommandLabels;
144
145    /**
146     * Associations between command tokens and callbacks.
147     */
148    std::map<std::string, CommandCallback> _dispatchMap;
149
150    /**
151     * Associations between command tokens and dialog windows.
152     */
153    DialogMap _dialogMap;
154 };
155
156 #endif // #if !defined(INCLUDED_GENERICMAINMENU_H)