]> git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/meshtex/GenericMainMenu.cpp
* added MeshTex plugin src to project (can't compile)
[xonotic/netradiant.git] / contrib / meshtex / GenericMainMenu.cpp
1 /**
2  * @file GenericMainMenu.cpp
3  * Implements 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 #include "GenericMainMenu.h"
27 #include "GenericPluginUI.h"
28 #include "GenericPluginUIMessages.h"
29 #include "PluginProperties.h"
30
31
32 /**
33  * Default constructor. Note that as this is a protected method,
34  * GenericMainMenu objects cannot be created directly; only subclasses of
35  * GenericMainMenu can be created.
36  */
37 GenericMainMenu::GenericMainMenu()
38 {
39 }
40
41 /**
42  * Virtual destructor.
43  */
44 GenericMainMenu::~GenericMainMenu()
45 {
46 }
47
48 /**
49  * Invoke the handler for the given command token.
50  *
51  * @param command The command token.
52  */
53 void
54 GenericMainMenu::Dispatch(const char *command)
55 {
56    // Convert token to string.
57    std::string commandString(command);
58    // Invoke the function from the dispatch map that corresponds to the command.
59    // The command key should always be in the map, since the set of commands
60    // advertised to Radiant is the same as the set used to make the map.
61 #if defined(_DEBUG)
62    ASSERT_MESSAGE(_dispatchMap.find(commandString) != _dispatchMap.end(),
63                   "dispatched plugin command unknown");
64 #endif
65    _dispatchMap[commandString](commandString);
66 }
67
68 /**
69  * Handle a command that summons a dialog window.
70  *
71  * @param commandString The command token.
72  */
73 void
74 GenericMainMenu::CommandDialogShow(const std::string& commandString)
75 {
76    // Look up the dialog window for this command.
77    DialogMap::const_iterator dialogMapIter = _dialogMap.find(commandString);
78    // Seems somewhat more plausible that we could hit an error condition here
79    // where there is no associated dialog, so handle that more gracefully
80    // than an assert.
81    GenericDialog *dialog;
82    if (dialogMapIter == _dialogMap.end() ||
83        (dialog = dialogMapIter->second) == NULL)
84    {
85       std::string message(commandString + ": " + DIALOG_INTERNAL_ERROR);
86       GenericPluginUI::ErrorReportDialog(DIALOG_ERROR_TITLE, message.c_str());
87       return;
88    }
89    // If we have the dialog though, let's summon it.
90    dialog->Show(commandString);
91 }
92
93 /**
94  * Get the command list for the plugin menu, as a semicolon-separated string
95  * of tokens representing each command.
96  *
97  * @return The command list string.
98  */
99 const std::string&
100 GenericMainMenu::GetCommandList() const
101 {
102    return _menuCommands;
103 }
104
105 /**
106  * Get the command label list for the plugin menu, as a semicolon-separated
107  * string of labels to appear in the menu.
108  *
109  * @return The command label list string.
110  */
111 const std::string&
112 GenericMainMenu::GetCommandLabelList() const
113 {
114    return _menuCommandLabels;
115 }
116
117 /**
118  * Invoked before beginning construction of the command list (by subsequent
119  * Add* invocations). In this base class, BeginEntries does nothing.
120  */
121 void
122 GenericMainMenu::BeginEntries()
123 {
124 }
125
126 /**
127  * Append a command-group separator to the command list. This should be done
128  * between an invocation of BeginEntries and EndEntries. (And presumably in
129  * the neighborhood of invocations of AddEntry and/or AddDialogShowEntry.)
130  */
131 void
132 GenericMainMenu::AddSeparator()
133 {
134    // Our internal command and command-label strings actually grow backwards,
135    // so prepend the separator to them.
136    static std::string separatorString(MENU_SEPARATOR);
137    _menuCommandLabels = separatorString + ";" + _menuCommandLabels;
138    _menuCommands = separatorString + ";" + _menuCommands;
139 }
140
141 /**
142  * Append a command to the command list that will trigger a callback function
143  * when the menu entry is selected. Invoking AddEntry should be done between
144  * an invocation of BeginEntries and EndEntries.
145  *
146  * @param commandLabel    The command label.
147  * @param command         The command token, unique for this plugin.
148  *                        Emptystring is interpreted as: re-use the label
149  *                        for the token.
150  * @param commandCallback The command callback function.
151  *
152  * @return The globally-unique command token: plugin name + "." + command.
153  */
154 std::string
155 GenericMainMenu::AddEntry(const char *commandLabel,
156                           const char *command,
157                           const CommandCallback& commandCallback)
158 {
159    // Our internal command-label string actually grows backwards, so prepend the
160    // command label to it.
161    std::string commandLabelString(commandLabel);
162    _menuCommandLabels = commandLabelString + ";" + _menuCommandLabels;
163    // Use the label for the token if no token specified.
164    std::string commandString(command);
165    if (commandString.empty())
166    {
167       commandString = commandLabelString;
168    }
169    // Add the plugin name in front of the command token to globally uniquify it.
170    commandString = std::string(PLUGIN_NAME) + "." + commandString;
171    // Our internal command string actually grows backwards, so prepend the
172    // command token to it.
173    _menuCommands = commandString + ";" + _menuCommands;
174    // Save the association between the globally-unique token and callback.
175    _dispatchMap[commandString] = commandCallback;
176    // Return the globally-unique command token.
177    return commandString;
178 }
179
180 /**
181  * Append a command to the command list that will summon a dialog when the
182  * menu entry is selected. Invoking AddDialogShowEntry should be done between
183  * an invocation of BeginEntries and EndEntries.
184  *
185  * @param commandLabel The command label.
186  * @param command      The command token, unique for this plugin. Emptystring
187  *                     is interpreted as: re-use the label for the token.
188  * @param dialog       The dialog this command should summon.
189  */
190 void
191 GenericMainMenu::AddDialogShowEntry(const char *commandLabel,
192                                     const char *command,
193                                     const SmartPointer<GenericDialog>& dialog)
194 {
195    // Create a new command callback specifically for summoning that dialog.
196    const CommandCallbackMethod
197       <GenericMainMenu, &GenericMainMenu::CommandDialogShow> commandDialogShow(*this);
198    // Register the command and its callback via AddEntry, and save the
199    // association between the globally-unique token and dialog.
200    _dialogMap.insert(
201       std::make_pair(AddEntry(commandLabel, command, commandDialogShow),
202                      dialog));
203 }
204
205 /**
206  * Invoked after ending construction of the command list. In this base class,
207  * EndEntries only removes spurious semicolons left behind by the way we
208  * constructed the lists.
209  */
210 void
211 GenericMainMenu::EndEntries()
212 {
213    if (_menuCommandLabels.length() > 0)
214    {
215       _menuCommandLabels.resize(_menuCommandLabels.length() - 1);
216       _menuCommands.resize(_menuCommands.length() - 1);
217    }
218 }