-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericPluginUI.cpp
374 lines (353 loc) · 11.8 KB
/
GenericPluginUI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
* @file GenericPluginUI.cpp
* Implements the GenericPluginUI class.
* @ingroup generic-ui
*/
/*
* Copyright 2012 Joel Baxter
*
* This file is part of MeshTex.
*
* MeshTex is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* MeshTex is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MeshTex. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtk/gtk.h>
#include "GenericPluginUI.h"
/**
* Default constructor. Initialize object state; the initial callback ID to
* use is 1 because 0 is reserved for "invalid". Note that as this is a
* protected method, GenericPluginUI objects cannot be created directly; only
* subclasses of GenericPluginUI can be created.
*/
GenericPluginUI::GenericPluginUI() :
_window(NULL),
_mainMenu(NULL),
_callbackID(1),
_widgetControlCallback(*this)
{
}
/**
* Virtual destructor. Remove references to UI elements (which should
* trigger garbage collection).
*/
GenericPluginUI::~GenericPluginUI()
{
// Remove any reference to the menu object.
if (_mainMenu != NULL)
{
delete _mainMenu;
}
// The _dialogMap will also be deleted by the normal destructor operation,
// which will clear any references we hold on dialog windows.
}
/**
* Register the command menu.
*
* @param mainMenu The command menu.
*/
void
GenericPluginUI::RegisterMainMenu(SmartPointer<GenericMainMenu>& mainMenu)
{
// Remember the menu object, and hold a reference on it so it won't be
// garbage-collected while this object exists.
_mainMenu = new SmartPointer<GenericMainMenu>(mainMenu);
}
/**
* Register a dialog window.
*
* @param dialog The dialog.
*/
void
GenericPluginUI::RegisterDialog(SmartPointer<GenericDialog>& dialog)
{
// Remember the association between key and dialog, and hold a reference
// on it so it won't be garbage-collected while this object exists.
_dialogMap.insert(std::make_pair(dialog->GetKey(), dialog));
}
/**
* Specify the main application window.
*
* @param window The main window.
*/
void
GenericPluginUI::SetWindow(GtkWidget *window)
{
// Remember it.
_window = window;
// Set it as the parent for every dialog window.
DialogMap::const_iterator dialogMapIter = _dialogMap.begin();
for (; dialogMapIter != _dialogMap.end(); ++dialogMapIter)
{
if (dialogMapIter->second.get() != NULL)
{
dialogMapIter->second->SetWindow(window);
}
}
}
/**
* Get the command menu.
*
* @return The command menu, or NULL if none has been registered.
*/
GenericMainMenu *
GenericPluginUI::MainMenu()
{
if (_mainMenu == NULL)
{
return NULL;
}
return _mainMenu->get();
}
/**
* Get the dialog identified by the specified key.
*
* @param key The key.
*
* @return The dialog, or NULL if none found for that key.
*/
GenericDialog *
GenericPluginUI::Dialog(const std::string& key)
{
DialogMap::const_iterator dialogMapIter = _dialogMap.find(key);
if (dialogMapIter == _dialogMap.end())
{
return NULL;
}
return dialogMapIter->second;
}
/**
* Generic event callback used to invoke the specific callback functions
* registered with this manager. Those specific callbacks are not themselves
* registered directly with GTK+ because they may be methods that must be
* invoked on objects. (Unlike this function, which is a static method.)
*
* @param widget The widget generating the event.
* @param event The event.
* @param data ID of the specific callback registered with this manager.
*
* @return The return value from the specific callback.
*/
gint
GenericPluginUI::DialogEventCallbackDispatch(GtkWidget *widget,
GdkEvent* event,
gpointer data)
{
// Look up the callback ID in our registration map.
DialogEventCallbackMap::iterator dialogEventCallbackMapIter =
UIInstance()._dialogEventCallbackMap.find(data);
if (dialogEventCallbackMapIter == UIInstance()._dialogEventCallbackMap.end())
{
// If we didn't find it, nothing to do.
return TRUE;
}
// Otherwise invoke that callback.
return dialogEventCallbackMapIter->second(widget, event, data);
}
/**
* Generic signal callback used to invoke the specific callback functions
* registered with this manager. Those specific callbacks are not themselves
* registered directly with GTK+ because they may be methods that must be
* invoked on objects. (Unlike this function, which is a static method.)
*
* @param widget The widget generating the signal.
* @param data ID of the specific callback registered with this manager.
*/
void
GenericPluginUI::DialogSignalCallbackDispatch(GtkWidget *widget,
gpointer data)
{
// Look up the callback ID in our registration map.
DialogSignalCallbackMap::iterator dialogSignalCallbackMapIter =
UIInstance()._dialogSignalCallbackMap.find(data);
if (dialogSignalCallbackMapIter == UIInstance()._dialogSignalCallbackMap.end())
{
// If we didn't find it, nothing to do.
return;
}
// Otherwise invoke that callback.
dialogSignalCallbackMapIter->second(widget, data);
}
/**
* Register a function to be invoked when a widget generates an event.
*
* @param widget The widget generating the event.
* @param name The name of the event.
* @param callback The callback function.
*
* @return The unique ID for the registered callback function.
*/
gpointer
GenericPluginUI::RegisterDialogEventCallback(GtkWidget *widget,
const gchar *name,
const DialogEventCallback& callback)
{
// Get the next callback ID to use.
gpointer callbackID = GUINT_TO_POINTER(_callbackID++);
// Make that event on that dialog widget trigger our event dispatch.
g_signal_connect(G_OBJECT(widget), name,
G_CALLBACK(DialogEventCallbackDispatch), callbackID);
// Save the association between callback ID and function.
_dialogEventCallbackMap.insert(std::make_pair(callbackID, callback));
// Return the generated unique callback ID.
return callbackID;
}
/**
* Register a function to be invoked when a widget generates a signal.
*
* @param widget The widget generating the signal.
* @param name The name of the signal.
* @param callback The callback function.
*
* @return The unique ID for the registered callback function.
*/
gpointer
GenericPluginUI::RegisterDialogSignalCallback(GtkWidget *widget,
const gchar *name,
const DialogSignalCallback& callback)
{
// Get the next callback ID to use.
gpointer callbackID = GUINT_TO_POINTER(_callbackID++);
// Make that signal on that dialog widget trigger our signal dispatch.
g_signal_connect(G_OBJECT(widget), name,
G_CALLBACK(DialogSignalCallbackDispatch), callbackID);
// Save the association between callback ID and function.
_dialogSignalCallbackMap.insert(std::make_pair(callbackID, callback));
// Return the generated unique callback ID.
return callbackID;
}
/**
* Declare that the controllee widget should be inactive when the
* controller widget is inactive. The controllee will be active only
* when all of its controllers allow it to be so.
*
* @param controller The controller widget.
* @param controllee The controllee widget.
*/
void
GenericPluginUI::RegisterWidgetDependence(GtkWidget *controller,
GtkWidget *controllee)
{
// Make sure we get a callback when the controller is toggled.
if (_widgetControlMap.find(controller) == _widgetControlMap.end())
{
RegisterDialogSignalCallback(controller, "clicked", _widgetControlCallback);
}
// Save the association.
_widgetControlMap[controller].push_back(controllee);
_widgetControlledByMap[controllee].push_back(controller);
}
/**
* Declare that the controllee widget should be inactive when the
* controller widget is active. The controllee will be active only
* when all of its controllers allow it to be so.
*
* @param controller The controller widget.
* @param controllee The controllee widget.
*/
void
GenericPluginUI::RegisterWidgetAntiDependence(GtkWidget *controller,
GtkWidget *controllee)
{
// Make sure we get a callback when the controller is toggled.
if (_widgetControlMap.find(controller) == _widgetControlMap.end())
{
RegisterDialogSignalCallback(controller, "clicked", _widgetControlCallback);
}
// Save the association.
_widgetControlMap[controller].push_back(controllee);
_widgetAntiControlledByMap[controllee].push_back(controller);
}
/**
* Manage the state of controllee widgets when a controller widget is clicked.
*
* @param widget The controller widget.
* @param callbackID Unique numerical ID for the callback.
*/
void
GenericPluginUI::WidgetControlCallback(GtkWidget *widget,
gpointer callbackID)
{
// Iterate over all controllees registered for this widget.
std::vector<GtkWidget *>::iterator controlleeIter =
_widgetControlMap[widget].begin();
for (; controlleeIter != _widgetControlMap[widget].end(); ++controlleeIter)
{
GtkWidget *controllee = *controlleeIter;
std::vector<GtkWidget *>::iterator controllerIter;
// Start with an assumption that the controllee widget will be active.
bool sensitive = true;
// Look for a dependence on any widget.
controllerIter = _widgetControlledByMap[controllee].begin();
for (; controllerIter != _widgetControlledByMap[controllee].end(); ++controllerIter)
{
// Dependence found; honor it.
if (!(GTK_TOGGLE_BUTTON(*controllerIter)->active))
{
sensitive = false;
break;
}
}
// Look for an anti-dependence on any widget.
controllerIter = _widgetAntiControlledByMap[controllee].begin();
for (; controllerIter != _widgetAntiControlledByMap[controllee].end(); ++controllerIter)
{
// Anti-dependence found; honor it.
if (GTK_TOGGLE_BUTTON(*controllerIter)->active)
{
sensitive = false;
break;
}
}
// Set the active state of the controllee appropriately.
gtk_widget_set_sensitive(controllee, sensitive);
}
}
/**
* Generate an error dialog.
*
* @param title The dialog title.
* @param message The error message.
*/
void
GenericPluginUI::ErrorReportDialog(const char *title,
const char *message)
{
// Pass this operation to Radiant.
GlobalRadiant().m_pfnMessageBox(UIInstance()._window, message, title, eMB_OK, eMB_ICONERROR);
}
/**
* Generate a warning dialog.
*
* @param title The dialog title.
* @param message The warning message.
*/
void
GenericPluginUI::WarningReportDialog(const char *title,
const char *message)
{
// Pass this operation to Radiant.
GlobalRadiant().m_pfnMessageBox(UIInstance()._window, message, title, eMB_OK, eMB_ICONWARNING);
}
/**
* Generate an info dialog.
*
* @param title The dialog title.
* @param message The info message.
*/
void
GenericPluginUI::InfoReportDialog(const char *title,
const char *message)
{
// Pass this operation to Radiant.
GlobalRadiant().m_pfnMessageBox(UIInstance()._window, message, title, eMB_OK, eMB_ICONDEFAULT);
}