Code

peeled back the gboolean code as it hit on some complexity theory principles...
[inkscape.git] / src / ui / dialog / dialog.cpp
1 /**
2  * \brief Base class for dialogs in Inkscape.  This class provides certain
3  *        common behaviors and styles wanted of all dialogs in the application.
4  *
5  * Author:
6  *   Bryce W. Harrington <bryce@bryceharrington.org>
7  *   buliabyak@gmail.com
8  *
9  * Copyright (C) 2004, 2005 Authors
10  *
11  * Released under GNU GPL.  Read the file 'COPYING' for more information.
12  */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #include <gtkmm/stock.h>
19 #include <gtk/gtk.h>
21 #include "application/application.h"
22 #include "application/editor.h"
23 #include "inkscape.h"
24 #include "event-context.h"
25 #include "desktop.h"
26 #include "desktop-handles.h"
27 #include "dialog-manager.h"
28 #include "dialogs/dialog-events.h"
29 #include "shortcuts.h"
30 #include "prefs-utils.h"
31 #include "interface.h"
32 #include "verbs.h"
34 namespace Inkscape {
35 namespace UI {
36 namespace Dialog {
38 #ifndef WIN32
39 static gboolean
40 sp_retransientize_again (gpointer dlgPtr)
41 {
42     Dialog *dlg = (Dialog *)dlgPtr;
43     dlg->retransientize_suppress = false;
44     return FALSE; // so that it is only called once
45 }
46 #endif
48 static void
49 sp_retransientize (Inkscape::Application *inkscape, SPDesktop *desktop, gpointer dlgPtr)
50 {
51     Dialog *dlg = (Dialog *)dlgPtr;
52     dlg->onDesktopActivated (desktop);
53 }
55 static void
56 sp_dialog_shutdown (GtkObject *object, gpointer dlgPtr)
57 {
58     Dialog *dlg = (Dialog *)dlgPtr;
59     dlg->onShutdown();
60 }
62 void 
63 Dialog::present()
64 {
65     Gtk::Dialog::present();
66 }
68 void
69 Dialog::save_geometry()
70 {
71     int y, x, w, h;
73     get_position(x, y);
74     get_size(w, h);
76 //    g_print ("write %d %d %d %d\n", x, y, w, h);
78     if (x<0) x=0;
79     if (y<0) y=0;
81     prefs_set_int_attribute (_prefs_path, "x", x);
82     prefs_set_int_attribute (_prefs_path, "y", y);
83     prefs_set_int_attribute (_prefs_path, "w", w);
84     prefs_set_int_attribute (_prefs_path, "h", h);
85 }
87 void
88 Dialog::read_geometry()
89 {
90     _user_hidden = false;
92     int x = prefs_get_int_attribute (_prefs_path, "x", -1000);
93     int y = prefs_get_int_attribute (_prefs_path, "y", -1000);
94     int w = prefs_get_int_attribute (_prefs_path, "w", 0);
95     int h = prefs_get_int_attribute (_prefs_path, "h", 0);
97 //    g_print ("read %d %d %d %d\n", x, y, w, h);
99         if (x<0) x=0;
100         if (y<0) y=0;
102     // If there are stored height and width values for the dialog,
103     // resize the window to match; otherwise we leave it at its default
104     if (w != 0 && h != 0) {
105         resize (w, h);
106     }
108     // If there are stored values for where the dialog should be
109     // located, then restore the dialog to that position.
110     if (x != -1000 && y != -1000) {
111         move(x, y);
112     } else {
113         // ...otherwise just put it in the middle of the screen
114         set_position(Gtk::WIN_POS_CENTER);
115     }
118 void hideCallback(GtkObject *object, gpointer dlgPtr)
120     g_return_if_fail( dlgPtr != NULL );
122     Dialog *dlg = (Dialog *)dlgPtr;
123     dlg->onHideF12();
126 void unhideCallback(GtkObject *object, gpointer dlgPtr)
128     g_return_if_fail( dlgPtr != NULL );
130     Dialog *dlg = (Dialog *)dlgPtr;
131     dlg->onShowF12();
134 //=====================================================================
136 /**
137  * UI::Dialog::Dialog is a base class for all dialogs in Inkscape.  The
138  * purpose of this class is to provide a unified place for ensuring
139  * style and behavior.  Specifically, this class provides functionality
140  * for saving and restoring the size and position of dialogs (through
141  * the user's preferences file).
142  *
143  * It also provides some general purpose signal handlers for things like
144  * showing and hiding all dialogs.
145  */
146 Dialog::Dialog(const char *prefs_path, int verb_num, const char *apply_label)
148     hide();
149     set_has_separator(false);
151     _prefs_path = prefs_path;
153     if (prefs_get_int_attribute ("dialogs", "showclose", 0) || apply_label) {
154         // TODO: make the order of buttons obey the global preference
155         if (apply_label) {
156             add_button(Glib::ustring(apply_label), Gtk::RESPONSE_APPLY);
157             set_default_response(Gtk::RESPONSE_APPLY);
158         }
159        add_button(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
160     }
162     GtkWidget *dlg = GTK_WIDGET(gobj());
164     if (verb_num)
165     {
166         gchar title[500];
167         sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
168         set_title(title);
169     }
171     sp_transientize(dlg);
172     retransientize_suppress = false;
174     gtk_signal_connect( GTK_OBJECT (dlg), "event", GTK_SIGNAL_FUNC(sp_dialog_event_handler), dlg );
176     _hiddenF12 = false;
177     if (Inkscape::NSApplication::Application::getNewGui())
178     {
179         _desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*this, &Dialog::onDesktopActivated));
180         _dialogs_hidden_connection = Inkscape::NSApplication::Editor::connectDialogsHidden (sigc::mem_fun (*this, &Dialog::onHideF12));
181         _dialogs_unhidden_connection = Inkscape::NSApplication::Editor::connectDialogsUnhidden (sigc::mem_fun (*this, &Dialog::onShowF12));
182         _shutdown_connection = Inkscape::NSApplication::Editor::connectShutdown (sigc::mem_fun (*this, &Dialog::onShutdown));
183     }
184     else
185     {
186         g_signal_connect   (G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_retransientize), (void *)this);
187         g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this );
188         g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this );
189         g_signal_connect   (G_OBJECT (INKSCAPE), "shut_down", G_CALLBACK (sp_dialog_shutdown), (void *)this);
190     }
192     g_signal_connect_after( gobj(), "key_press_event", (GCallback)windowKeyPress, NULL );
194     read_geometry();
195     present();
198 Dialog::Dialog(BaseObjectType *gobj)
199     : Gtk::Dialog(gobj)
203 Dialog::~Dialog()
205     if (Inkscape::NSApplication::Application::getNewGui())
206     {
207         _desktop_activated_connection.disconnect();
208         _dialogs_hidden_connection.disconnect();
209         _dialogs_unhidden_connection.disconnect();
210         _shutdown_connection.disconnect();
211     }
212     
213     save_geometry();
217 bool Dialog::windowKeyPress( GtkWidget *widget, GdkEventKey *event )
219     unsigned int shortcut = 0;
220     shortcut = get_group0_keyval (event) |
221         ( event->state & GDK_SHIFT_MASK ?
222           SP_SHORTCUT_SHIFT_MASK : 0 ) |
223         ( event->state & GDK_CONTROL_MASK ?
224           SP_SHORTCUT_CONTROL_MASK : 0 ) |
225         ( event->state & GDK_MOD1_MASK ?
226           SP_SHORTCUT_ALT_MASK : 0 );
227     return sp_shortcut_invoke( shortcut, SP_ACTIVE_DESKTOP );
230 //---------------------------------------------------------------------
232 void
233 Dialog::on_response(int response_id)
235     switch (response_id) {
236         case Gtk::RESPONSE_APPLY: {
237             _apply();
238             break;
239         }
240         case Gtk::RESPONSE_CLOSE: {
241             _close();
242             break;
243         }
244     }
247 bool
248 Dialog::on_delete_event (GdkEventAny *event)
250     save_geometry();
251     _user_hidden = true;
253     return false;
256 void
257 Dialog::onHideF12()
259     _hiddenF12 = true;
260     save_geometry();
261     hide();
264 void
265 Dialog::onShowF12()
267     if (_user_hidden)
268         return;
270     if (_hiddenF12) {
271         show();
272         read_geometry();
273     }
275     _hiddenF12 = false;
278 void 
279 Dialog::onShutdown()
281     save_geometry();
282     _user_hidden = true;
285 void
286 Dialog::onDesktopActivated (SPDesktop *desktop)
288     gint transient_policy = prefs_get_int_attribute_limited ( "options.transientpolicy", "value", 1, 0, 2);
290     if (!transient_policy)
291         return;
293 #ifndef WIN32
294     GtkWindow *dialog_win = GTK_WINDOW(gobj());
296     if (retransientize_suppress) {
297          /* if retransientizing of this dialog is still forbidden after
298           * previous call warning turned off because it was confusingly fired
299           * when loading many files from command line
300           */
302          // g_warning("Retranzientize aborted! You're switching windows too fast!");
303         return;
304     }
306     if (dialog_win)
307     {
308         retransientize_suppress = true; // disallow other attempts to retranzientize this dialog
310         desktop->setWindowTransient (dialog_win);
312         /*
313          * This enables "aggressive" transientization,
314          * i.e. dialogs always emerging on top when you switch documents. Note
315          * however that this breaks "click to raise" policy of a window
316          * manager because the switched-to document will be raised at once
317          * (so that its transients also could raise)
318          */
319         if (transient_policy == 2 && !_hiddenF12 && !_user_hidden) {
320             // without this, a transient window not always emerges on top
321             gtk_window_present (dialog_win);
322         }
323     }
325     // we're done, allow next retransientizing not sooner than after 120 msec
326     gtk_timeout_add (120, (GtkFunction) sp_retransientize_again, (gpointer) this);
327 #endif
331 Inkscape::Selection*
332 Dialog::_getSelection()
334     return sp_desktop_selection(SP_ACTIVE_DESKTOP);
337 void
338 Dialog::_apply()
340     g_warning("Apply button clicked for dialog [Dialog::_apply()]");
343 void
344 Dialog::_close()
346     GtkWidget *dlg = GTK_WIDGET(gobj());
348                         /* this code sends a delete_event to the dialog,
349                          * instead of just destroying it, so that the
350                          * dialog can do some housekeeping, such as remember
351                          * its position.
352                          */
353                         GdkEventAny event;
354                         event.type = GDK_DELETE;
355                         event.window = dlg->window;
356                         event.send_event = TRUE;
357                         g_object_ref (G_OBJECT (event.window));
358                         gtk_main_do_event ((GdkEvent*)&event);
359                         g_object_unref (G_OBJECT (event.window));
362 } // namespace Dialog
363 } // namespace UI
364 } // namespace Inkscape
366 /*
367   Local Variables:
368   mode:c++
369   c-file-style:"stroustrup"
370   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
371   indent-tabs-mode:nil
372   fill-column:99
373   End:
374 */
375 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :