-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericDialog.cpp
268 lines (250 loc) · 7.31 KB
/
GenericDialog.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
/**
* @file GenericDialog.cpp
* Implements the GenericDialog 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 "GenericDialog.h"
#include "GenericPluginUI.h"
/**
* Constructor. Create the GTK+ widget for the dialog window (not visible
* yet). Initialize callback IDs to zero (invalid). Note that as this is a
* protected method, GenericDialog objects cannot be created directly; only
* subclasses of GenericDialog can be created.
*
* @param key Unique key to identify this dialog widget.
*/
GenericDialog::GenericDialog(const std::string& key) :
_dialog(gtk_window_new(GTK_WINDOW_TOPLEVEL)),
_window(NULL),
_key(key),
_okCallbackID(0),
_applyCallbackID(0),
_cancelCallbackID(0)
{
// XXX Should we go ahead invoke CreateWindowCloseCallback here (and make it
// private) rather than leaving that to the subclass constructors? Depends on
// whether it's plausible that a dialog would ever NOT want the usual
// handling for the close event.
}
/**
* Virtual destructor. Destroy the GTK+ dialog widget (and therefore its
* contained widgets) if necessary.
*/
GenericDialog::~GenericDialog()
{
if (_dialog != NULL)
{
gtk_widget_destroy(_dialog);
}
}
/**
* Get the unique key that identifies this dialog widget.
*
* @return The key.
*/
const std::string&
GenericDialog::GetKey() const
{
return _key;
}
/**
* Mark this window widget as a modal dialog for a parent window.
*
* @param window The parent window.
*/
void
GenericDialog::SetWindow(GtkWidget *window)
{
// Remember the parent window.
_window = window;
// Mark this widget as a modal dialog for it.
if (_dialog != NULL)
{
gtk_window_set_transient_for(GTK_WINDOW(_dialog), GTK_WINDOW(_window));
}
}
/**
* Raise this dialog window to the top of the window stack.
*/
void
GenericDialog::Raise()
{
// Don't bother if not visible.
if (GTK_WIDGET_VISIBLE(_dialog))
{
gdk_window_raise(_dialog->window);
}
}
/**
* Make this dialog window visible and foreground.
*
* @param triggerCommand The command token that summoned the dialog.
*/
void
GenericDialog::Show(const std::string& triggerCommand)
{
// Remember the command token that summoned the dialog; subclasses can make
// use of this information.
_triggerCommand = triggerCommand;
// Show the window if it is currently hidden.
if (!GTK_WIDGET_VISIBLE(_dialog))
{
gtk_widget_show(_dialog);
}
// Raise the window to the top of the stack.
Raise();
}
/**
* Hide this dialog window.
*/
void
GenericDialog::Hide()
{
// Bail out if the window is already invisible.
if (!GTK_WIDGET_VISIBLE(_dialog))
{
return;
}
// Hide the window.
gtk_widget_hide(_dialog);
// If there's a parent window, raise it to the top of the stack.
if (_window == NULL)
{
return;
}
gdk_window_raise(_window->window);
}
/**
* Default handler for Apply logic. This method should be overriden by
* subclass implementations that need to execute some logic when OK or Apply
* is clicked. The return value should be the success of that logic. A
* successful OK will cause the window to be hidden.
*
* @return true if the apply logic executed; always the case in this skeleton
* implementation.
*/
bool
GenericDialog::Apply()
{
// Default logic does nothing.
return true;
}
/**
* Callback for window-close event.
*
* @param widget This dialog window widget.
* @param event The event that instigated the callback.
* @param callbackID Unique numerical ID for the callback.
*
* @return TRUE as defined by glib.
*/
gint
GenericDialog::CloseEventCallback(GtkWidget *widget,
GdkEvent* event,
gpointer callbackID)
{
// All we need to do is hide the window.
Hide();
return TRUE;
}
/**
* Callback for clicking on OK/Apply/Cancel button.
*
* @param widget This dialog window widget.
* @param callbackID Unique numerical ID for the callback.
*/
void
GenericDialog::FinalizeCallback(GtkWidget *widget,
gpointer callbackID)
{
// Assume success until we have to do something.
bool success = true;
// If this is not a Cancel callback, run the Apply logic.
if (callbackID != _cancelCallbackID)
{
success = Apply();
}
// Hide the window if this is a cancel or a successful OK callback.
if (success && callbackID != _applyCallbackID)
{
Hide();
}
}
/**
* Register the callback for the close-window event.
*/
void
GenericDialog::CreateWindowCloseCallback()
{
// The close-window event will trigger the CloseEventCallback method.
const GenericPluginUI::DialogEventCallbackMethod
<GenericDialog, &GenericDialog::CloseEventCallback> closeCallback(*this);
UIInstance().RegisterDialogEventCallback(_dialog, "delete_event", closeCallback);
}
/**
* Register the callback for the OK button.
*
* @param button The OK button widget.
*/
void
GenericDialog::CreateOkButtonCallback(GtkWidget *button)
{
// Clicking the OK button will trigger the FinalizeCallback method. Since
// FinalizeCallback can be used for multiple buttons, we'll save the specific
// callback ID associated with the OK button.
const GenericPluginUI::DialogSignalCallbackMethod
<GenericDialog, &GenericDialog::FinalizeCallback> finalizeCallback(*this);
_okCallbackID =
UIInstance().RegisterDialogSignalCallback(button, "clicked", finalizeCallback);
}
/**
* Register the callback for the Apply button.
*
* @param button The Apply button widget.
*/
void
GenericDialog::CreateApplyButtonCallback(GtkWidget *button)
{
// Clicking the Apply button will trigger the FinalizeCallback method. Since
// FinalizeCallback can be used for multiple buttons, we'll save the specific
// callback ID associated with the Apply button.
const GenericPluginUI::DialogSignalCallbackMethod
<GenericDialog, &GenericDialog::FinalizeCallback> finalizeCallback(*this);
_applyCallbackID =
UIInstance().RegisterDialogSignalCallback(button, "clicked", finalizeCallback);
}
/**
* Register the callback for the Cancel button.
*
* @param button The Cancel button widget.
*/
void
GenericDialog::CreateCancelButtonCallback(GtkWidget *button)
{
// Clicking the Cancel button will trigger the FinalizeCallback method. Since
// FinalizeCallback can be used for multiple buttons, we'll save the specific
// callback ID associated with the Cancel button.
const GenericPluginUI::DialogSignalCallbackMethod
<GenericDialog, &GenericDialog::FinalizeCallback> finalizeCallback(*this);
_cancelCallbackID =
UIInstance().RegisterDialogSignalCallback(button, "clicked", finalizeCallback);
}