2 * @file GenericMainMenu.cpp
3 * Implements the GenericMainMenu class.
8 * Copyright 2012 Joel Baxter
10 * This file is part of MeshTex.
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.
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.
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/>.
26 #include "GenericMainMenu.h"
27 #include "GenericPluginUI.h"
28 #include "GenericPluginUIMessages.h"
29 #include "PluginProperties.h"
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.
37 GenericMainMenu::GenericMainMenu()
44 GenericMainMenu::~GenericMainMenu()
49 * Invoke the handler for the given command token.
51 * @param command The command token.
54 GenericMainMenu::Dispatch(const char *command)
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.
62 ASSERT_MESSAGE(_dispatchMap.find(commandString) != _dispatchMap.end(),
63 "dispatched plugin command unknown");
65 _dispatchMap[commandString](commandString);
69 * Handle a command that summons a dialog window.
71 * @param commandString The command token.
74 GenericMainMenu::CommandDialogShow(const std::string& commandString)
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
81 GenericDialog *dialog;
82 if (dialogMapIter == _dialogMap.end() ||
83 (dialog = dialogMapIter->second) == NULL)
85 std::string message(commandString + ": " + DIALOG_INTERNAL_ERROR);
86 GenericPluginUI::ErrorReportDialog(DIALOG_ERROR_TITLE, message.c_str());
89 // If we have the dialog though, let's summon it.
90 dialog->Show(commandString);
94 * Get the command list for the plugin menu, as a semicolon-separated string
95 * of tokens representing each command.
97 * @return The command list string.
100 GenericMainMenu::GetCommandList() const
102 return _menuCommands;
106 * Get the command label list for the plugin menu, as a semicolon-separated
107 * string of labels to appear in the menu.
109 * @return The command label list string.
112 GenericMainMenu::GetCommandLabelList() const
114 return _menuCommandLabels;
118 * Invoked before beginning construction of the command list (by subsequent
119 * Add* invocations). In this base class, BeginEntries does nothing.
122 GenericMainMenu::BeginEntries()
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.)
132 GenericMainMenu::AddSeparator()
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;
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.
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
150 * @param commandCallback The command callback function.
152 * @return The globally-unique command token: plugin name + "." + command.
155 GenericMainMenu::AddEntry(const char *commandLabel,
157 const CommandCallback& commandCallback)
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())
167 commandString = commandLabelString;
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;
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.
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.
191 GenericMainMenu::AddDialogShowEntry(const char *commandLabel,
193 const SmartPointer<GenericDialog>& dialog)
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.
201 std::make_pair(AddEntry(commandLabel, command, commandDialogShow),
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.
211 GenericMainMenu::EndEntries()
213 if (_menuCommandLabels.length() > 0)
215 _menuCommandLabels.resize(_menuCommandLabels.length() - 1);
216 _menuCommands.resize(_menuCommands.length() - 1);