2 * @file GenericDialog.cpp
3 * Implements the GenericDialog 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/>.
28 #include "GenericDialog.h"
29 #include "GenericPluginUI.h"
33 * Constructor. Create the GTK+ widget for the dialog window (not visible
34 * yet). Initialize callback IDs to zero (invalid). Note that as this is a
35 * protected method, GenericDialog objects cannot be created directly; only
36 * subclasses of GenericDialog can be created.
38 * @param key Unique key to identify this dialog widget.
40 GenericDialog::GenericDialog(const std::string& key) :
41 _dialog(gtk_window_new(GTK_WINDOW_TOPLEVEL)),
48 // XXX Should we go ahead invoke CreateWindowCloseCallback here (and make it
49 // private) rather than leaving that to the subclass constructors? Depends on
50 // whether it's plausible that a dialog would ever NOT want the usual
51 // handling for the close event.
55 * Virtual destructor. Destroy the GTK+ dialog widget (and therefore its
56 * contained widgets) if necessary.
58 GenericDialog::~GenericDialog()
62 gtk_widget_destroy(_dialog);
67 * Get the unique key that identifies this dialog widget.
72 GenericDialog::GetKey() const
78 * Mark this window widget as a modal dialog for a parent window.
80 * @param window The parent window.
83 GenericDialog::SetWindow(GtkWidget *window)
85 // Remember the parent window.
87 // Mark this widget as a modal dialog for it.
90 gtk_window_set_transient_for(GTK_WINDOW(_dialog), GTK_WINDOW(_window));
95 * Raise this dialog window to the top of the window stack.
98 GenericDialog::Raise()
100 // Don't bother if not visible.
101 if (GTK_WIDGET_VISIBLE(_dialog))
103 gdk_window_raise(_dialog->window);
108 * Make this dialog window visible and foreground.
110 * @param triggerCommand The command token that summoned the dialog.
113 GenericDialog::Show(const std::string& triggerCommand)
115 // Remember the command token that summoned the dialog; subclasses can make
116 // use of this information.
117 _triggerCommand = triggerCommand;
118 // Show the window if it is currently hidden.
119 if (!GTK_WIDGET_VISIBLE(_dialog))
121 gtk_widget_show(_dialog);
123 // Raise the window to the top of the stack.
128 * Hide this dialog window.
131 GenericDialog::Hide()
133 // Bail out if the window is already invisible.
134 if (!GTK_WIDGET_VISIBLE(_dialog))
139 gtk_widget_hide(_dialog);
140 // If there's a parent window, raise it to the top of the stack.
145 gdk_window_raise(_window->window);
149 * Default handler for Apply logic. This method should be overriden by
150 * subclass implementations that need to execute some logic when OK or Apply
151 * is clicked. The return value should be the success of that logic. A
152 * successful OK will cause the window to be hidden.
154 * @return true if the apply logic executed; always the case in this skeleton
158 GenericDialog::Apply()
160 // Default logic does nothing.
165 * Callback for window-close event.
167 * @param widget This dialog window widget.
168 * @param event The event that instigated the callback.
169 * @param callbackID Unique numerical ID for the callback.
171 * @return TRUE as defined by glib.
174 GenericDialog::CloseEventCallback(GtkWidget *widget,
178 // All we need to do is hide the window.
184 * Callback for clicking on OK/Apply/Cancel button.
186 * @param widget This dialog window widget.
187 * @param callbackID Unique numerical ID for the callback.
190 GenericDialog::FinalizeCallback(GtkWidget *widget,
193 // Assume success until we have to do something.
195 // If this is not a Cancel callback, run the Apply logic.
196 if (callbackID != _cancelCallbackID)
200 // Hide the window if this is a cancel or a successful OK callback.
201 if (success && callbackID != _applyCallbackID)
208 * Register the callback for the close-window event.
211 GenericDialog::CreateWindowCloseCallback()
213 // The close-window event will trigger the CloseEventCallback method.
214 const GenericPluginUI::DialogEventCallbackMethod
215 <GenericDialog, &GenericDialog::CloseEventCallback> closeCallback(*this);
216 UIInstance().RegisterDialogEventCallback(_dialog, "delete_event", closeCallback);
220 * Register the callback for the OK button.
222 * @param button The OK button widget.
225 GenericDialog::CreateOkButtonCallback(GtkWidget *button)
227 // Clicking the OK button will trigger the FinalizeCallback method. Since
228 // FinalizeCallback can be used for multiple buttons, we'll save the specific
229 // callback ID associated with the OK button.
230 const GenericPluginUI::DialogSignalCallbackMethod
231 <GenericDialog, &GenericDialog::FinalizeCallback> finalizeCallback(*this);
233 UIInstance().RegisterDialogSignalCallback(button, "clicked", finalizeCallback);
237 * Register the callback for the Apply button.
239 * @param button The Apply button widget.
242 GenericDialog::CreateApplyButtonCallback(GtkWidget *button)
244 // Clicking the Apply button will trigger the FinalizeCallback method. Since
245 // FinalizeCallback can be used for multiple buttons, we'll save the specific
246 // callback ID associated with the Apply button.
247 const GenericPluginUI::DialogSignalCallbackMethod
248 <GenericDialog, &GenericDialog::FinalizeCallback> finalizeCallback(*this);
250 UIInstance().RegisterDialogSignalCallback(button, "clicked", finalizeCallback);
254 * Register the callback for the Cancel button.
256 * @param button The Cancel button widget.
259 GenericDialog::CreateCancelButtonCallback(GtkWidget *button)
261 // Clicking the Cancel button will trigger the FinalizeCallback method. Since
262 // FinalizeCallback can be used for multiple buttons, we'll save the specific
263 // callback ID associated with the Cancel button.
264 const GenericPluginUI::DialogSignalCallbackMethod
265 <GenericDialog, &GenericDialog::FinalizeCallback> finalizeCallback(*this);
267 UIInstance().RegisterDialogSignalCallback(button, "clicked", finalizeCallback);