Code

43f66134804a3fdfb369c453f190ef87d71dfe28
[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  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9  *
10  * Copyright (C) 2004-2007 Authors
11  *
12  * Released under GNU GPL.  Read the file 'COPYING' for more information.
13  */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #include <gtkmm/stock.h>
20 #include <gtk/gtk.h>
22 #include "application/application.h"
23 #include "application/editor.h"
24 #include "inkscape.h"
25 #include "event-context.h"
26 #include "desktop.h"
27 #include "desktop-handles.h"
28 #include "dialog-manager.h"
29 #include "dialogs/dialog-events.h"
30 #include "shortcuts.h"
31 #include "prefs-utils.h"
32 #include "interface.h"
33 #include "verbs.h"
36 #define MIN_ONSCREEN_DISTANCE 50
40 namespace Inkscape {
41 namespace UI {
42 namespace Dialog {
44 //#ifndef WIN32
45 static gboolean
46 sp_retransientize_again (gpointer dlgPtr)
47 {
48     Dialog *dlg = (Dialog *)dlgPtr;
49     dlg->retransientize_suppress = false;
50     return FALSE; // so that it is only called once
51 }
52 //#endif
54 static void
55 sp_retransientize (Inkscape::Application *inkscape, SPDesktop *desktop, gpointer dlgPtr)
56 {
57     Dialog *dlg = (Dialog *)dlgPtr;
58     dlg->onDesktopActivated (desktop);
59 }
61 static void
62 sp_dialog_shutdown (GtkObject *object, gpointer dlgPtr)
63 {
64     Dialog *dlg = (Dialog *)dlgPtr;
65     dlg->onShutdown();
66 }
68 void 
69 Dialog::present()
70 {
71     Gtk::Dialog::present();
72 }
74 void
75 Dialog::save_geometry()
76 {
77     int y, x, w, h;
79     get_position(x, y);
80     get_size(w, h);
82 //    g_print ("write %d %d %d %d\n", x, y, w, h);
84     if (x<0) x=0;
85     if (y<0) y=0;
87     prefs_set_int_attribute (_prefs_path, "x", x);
88     prefs_set_int_attribute (_prefs_path, "y", y);
89     prefs_set_int_attribute (_prefs_path, "w", w);
90     prefs_set_int_attribute (_prefs_path, "h", h);
91 }
93 void
94 Dialog::read_geometry()
95 {
96     _user_hidden = false;
98     int x = prefs_get_int_attribute (_prefs_path, "x", -1000);
99     int y = prefs_get_int_attribute (_prefs_path, "y", -1000);
100     int w = prefs_get_int_attribute (_prefs_path, "w", 0);
101     int h = prefs_get_int_attribute (_prefs_path, "h", 0);
103 //    g_print ("read %d %d %d %d\n", x, y, w, h);
105     // If there are stored height and width values for the dialog,
106     // resize the window to match; otherwise we leave it at its default
107     if (w != 0 && h != 0) {
108         resize (w, h);
109     }
110     
111     // If there are stored values for where the dialog should be
112     // located, then restore the dialog to that position.
113     // also check if (x,y) is actually onscreen with the current screen dimensions
114     if ( (x >= 0) && (y >= 0) && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)) ) {
115         move(x, y);
116     } else {
117         // ...otherwise just put it in the middle of the screen
118         set_position(Gtk::WIN_POS_CENTER);
119     }
122 void hideCallback(GtkObject *object, gpointer dlgPtr)
124     g_return_if_fail( dlgPtr != NULL );
126     Dialog *dlg = (Dialog *)dlgPtr;
127     dlg->onHideF12();
130 void unhideCallback(GtkObject *object, gpointer dlgPtr)
132     g_return_if_fail( dlgPtr != NULL );
134     Dialog *dlg = (Dialog *)dlgPtr;
135     dlg->onShowF12();
138 //=====================================================================
140 /**
141  * UI::Dialog::Dialog is a base class for all dialogs in Inkscape.  The
142  * purpose of this class is to provide a unified place for ensuring
143  * style and behavior.  Specifically, this class provides functionality
144  * for saving and restoring the size and position of dialogs (through
145  * the user's preferences file).
146  *
147  * It also provides some general purpose signal handlers for things like
148  * showing and hiding all dialogs.
149  */
150 Dialog::Dialog(const char *prefs_path, int verb_num, const char *apply_label)
152     hide();
153     set_has_separator(false);
155     _prefs_path = prefs_path;
157     if (prefs_get_int_attribute ("dialogs", "showclose", 0) || apply_label) {
158         // TODO: make the order of buttons obey the global preference
159         if (apply_label) {
160             add_button(Glib::ustring(apply_label), Gtk::RESPONSE_APPLY);
161             set_default_response(Gtk::RESPONSE_APPLY);
162         }
163        add_button(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
164     }
166     GtkWidget *dlg = GTK_WIDGET(gobj());
168     if (verb_num)
169     {
170         gchar title[500];
171         sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
172         set_title(title);
173     }
175     sp_transientize(dlg);
176     retransientize_suppress = false;
178     gtk_signal_connect( GTK_OBJECT (dlg), "event", GTK_SIGNAL_FUNC(sp_dialog_event_handler), dlg );
180     _hiddenF12 = false;
181     if (Inkscape::NSApplication::Application::getNewGui())
182     {
183         _desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*this, &Dialog::onDesktopActivated));
184         _dialogs_hidden_connection = Inkscape::NSApplication::Editor::connectDialogsHidden (sigc::mem_fun (*this, &Dialog::onHideF12));
185         _dialogs_unhidden_connection = Inkscape::NSApplication::Editor::connectDialogsUnhidden (sigc::mem_fun (*this, &Dialog::onShowF12));
186         _shutdown_connection = Inkscape::NSApplication::Editor::connectShutdown (sigc::mem_fun (*this, &Dialog::onShutdown));
187     }
188     else
189     {
190         g_signal_connect   (G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_retransientize), (void *)this);
191         g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this );
192         g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this );
193         g_signal_connect   (G_OBJECT (INKSCAPE), "shut_down", G_CALLBACK (sp_dialog_shutdown), (void *)this);
194     }
196     g_signal_connect_after( gobj(), "key_press_event", (GCallback)windowKeyPress, NULL );
198     read_geometry();
199     present();
202 Dialog::Dialog(BaseObjectType *gobj)
203     : Gtk::Dialog(gobj)
207 Dialog::~Dialog()
209     if (Inkscape::NSApplication::Application::getNewGui())
210     {
211         _desktop_activated_connection.disconnect();
212         _dialogs_hidden_connection.disconnect();
213         _dialogs_unhidden_connection.disconnect();
214         _shutdown_connection.disconnect();
215     }
216     
217     save_geometry();
221 bool Dialog::windowKeyPress( GtkWidget *widget, GdkEventKey *event )
223     unsigned int shortcut = 0;
224     shortcut = get_group0_keyval (event) |
225         ( event->state & GDK_SHIFT_MASK ?
226           SP_SHORTCUT_SHIFT_MASK : 0 ) |
227         ( event->state & GDK_CONTROL_MASK ?
228           SP_SHORTCUT_CONTROL_MASK : 0 ) |
229         ( event->state & GDK_MOD1_MASK ?
230           SP_SHORTCUT_ALT_MASK : 0 );
231     return sp_shortcut_invoke( shortcut, SP_ACTIVE_DESKTOP );
234 //---------------------------------------------------------------------
236 void
237 Dialog::on_response(int response_id)
239     switch (response_id) {
240         case Gtk::RESPONSE_APPLY: {
241             _apply();
242             break;
243         }
244         case Gtk::RESPONSE_CLOSE: {
245             _close();
246             break;
247         }
248     }
251 bool
252 Dialog::on_delete_event (GdkEventAny *event)
254     save_geometry();
255     _user_hidden = true;
257     return false;
260 void
261 Dialog::onHideF12()
263     _hiddenF12 = true;
264     save_geometry();
265     hide();
268 void
269 Dialog::onShowF12()
271     if (_user_hidden)
272         return;
274     if (_hiddenF12) {
275         show();
276         read_geometry();
277     }
279     _hiddenF12 = false;
282 void 
283 Dialog::onShutdown()
285     save_geometry();
286     _user_hidden = true;
289 void
290 Dialog::onDesktopActivated (SPDesktop *desktop)
292     gint transient_policy = prefs_get_int_attribute_limited ( "options.transientpolicy", "value", 1, 0, 2);
294 #ifdef WIN32 // FIXME: Temporary Win32 special code to enable transient dialogs
295     if (prefs_get_int_attribute ( "options.dialogsontopwin32", "value", 0))
296         transient_policy = 2;
297     else    
298         return;
299 #endif        
301     if (!transient_policy) 
302         return;
306     GtkWindow *dialog_win = GTK_WINDOW(gobj());
308     if (retransientize_suppress) {
309          /* if retransientizing of this dialog is still forbidden after
310           * previous call warning turned off because it was confusingly fired
311           * when loading many files from command line
312           */
314          // g_warning("Retranzientize aborted! You're switching windows too fast!");
315         return;
316     }
318     if (dialog_win)
319     {
320         retransientize_suppress = true; // disallow other attempts to retranzientize this dialog
322         desktop->setWindowTransient (dialog_win);
324         /*
325          * This enables "aggressive" transientization,
326          * i.e. dialogs always emerging on top when you switch documents. Note
327          * however that this breaks "click to raise" policy of a window
328          * manager because the switched-to document will be raised at once
329          * (so that its transients also could raise)
330          */
331         if (transient_policy == 2 && !_hiddenF12 && !_user_hidden) {
332             // without this, a transient window not always emerges on top
333             gtk_window_present (dialog_win);
334         }
335     }
337     // we're done, allow next retransientizing not sooner than after 120 msec
338     gtk_timeout_add (120, (GtkFunction) sp_retransientize_again, (gpointer) this);
342 Inkscape::Selection*
343 Dialog::_getSelection()
345     return sp_desktop_selection(SP_ACTIVE_DESKTOP);
348 void
349 Dialog::_apply()
351     g_warning("Apply button clicked for dialog [Dialog::_apply()]");
354 void
355 Dialog::_close()
357     GtkWidget *dlg = GTK_WIDGET(gobj());
359                         /* this code sends a delete_event to the dialog,
360                          * instead of just destroying it, so that the
361                          * dialog can do some housekeeping, such as remember
362                          * its position.
363                          */
364                         GdkEventAny event;
365                         event.type = GDK_DELETE;
366                         event.window = dlg->window;
367                         event.send_event = TRUE;
368                         g_object_ref (G_OBJECT (event.window));
369                         gtk_main_do_event ((GdkEvent*)&event);
370                         g_object_unref (G_OBJECT (event.window));
373 } // namespace Dialog
374 } // namespace UI
375 } // namespace Inkscape
377 /*
378   Local Variables:
379   mode:c++
380   c-file-style:"stroustrup"
381   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
382   indent-tabs-mode:nil
383   fill-column:99
384   End:
385 */
386 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :