Code

remove screen pixel toggle for now, add always-snap widget for all
[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::save_geometry()
64 {
65     int y, x, w, h;
67     get_position(x, y);
68     get_size(w, h);
70 //    g_print ("write %d %d %d %d\n", x, y, w, h);
72     prefs_set_int_attribute (_prefs_path, "x", x);
73     prefs_set_int_attribute (_prefs_path, "y", y);
74     prefs_set_int_attribute (_prefs_path, "w", w);
75     prefs_set_int_attribute (_prefs_path, "h", h);
76 }
78 void
79 Dialog::read_geometry()
80 {
81     _user_hidden = false;
83     int x = prefs_get_int_attribute (_prefs_path, "x", -1000);
84     int y = prefs_get_int_attribute (_prefs_path, "y", -1000);
85     int w = prefs_get_int_attribute (_prefs_path, "w", 0);
86     int h = prefs_get_int_attribute (_prefs_path, "h", 0);
88 //    g_print ("read %d %d %d %d\n", x, y, w, h);
90     // If there are stored height and width values for the dialog,
91     // resize the window to match; otherwise we leave it at its default
92     if (w != 0 && h != 0) {
93         resize (w, h);
94     }
96     // If there are stored values for where the dialog should be
97     // located, then restore the dialog to that position.
98     if (x != -1000 && y != -1000) {
99         move(x, y);
100     } else {
101         // ...otherwise just put it in the middle of the screen
102         set_position(Gtk::WIN_POS_CENTER);
103     }
106 void hideCallback(GtkObject *object, gpointer dlgPtr)
108     Dialog *dlg = (Dialog *)dlgPtr;
109     dlg->onHideF12();
112 void unhideCallback(GtkObject *object, gpointer dlgPtr)
114     Dialog *dlg = (Dialog *)dlgPtr;
115     dlg->onShowF12();
118 //=====================================================================
120 /**
121  * UI::Dialog::Dialog is a base class for all dialogs in Inkscape.  The
122  * purpose of this class is to provide a unified place for ensuring
123  * style and behavior.  Specifically, this class provides functionality
124  * for saving and restoring the size and position of dialogs (through
125  * the user's preferences file).
126  *
127  * It also provides some general purpose signal handlers for things like
128  * showing and hiding all dialogs.
129  */
130 Dialog::Dialog(const char *prefs_path, int verb_num, const char *apply_label)
132     hide();
133     set_has_separator(false);
135     _prefs_path = prefs_path;
137     if (prefs_get_int_attribute ("dialogs", "showclose", 0) || apply_label) {
138         // TODO: make the order of buttons obey the global preference
139         if (apply_label) {
140             add_button(Glib::ustring(apply_label), Gtk::RESPONSE_APPLY);
141             set_default_response(Gtk::RESPONSE_APPLY);
142         }
143        add_button(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
144     }
146     GtkWidget *dlg = GTK_WIDGET(gobj());
148     if (verb_num)
149     {
150         gchar title[500];
151         sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
152         set_title(title);
153     }
155     sp_transientize(dlg);
156     retransientize_suppress = false;
158     gtk_signal_connect( GTK_OBJECT (dlg), "event", GTK_SIGNAL_FUNC(sp_dialog_event_handler), dlg );
160     _hiddenF12 = false;
161     if (Inkscape::NSApplication::Application::getNewGui())
162     {
163         _desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*this, &Dialog::onDesktopActivated));
164         _dialogs_hidden_connection = Inkscape::NSApplication::Editor::connectDialogsHidden (sigc::mem_fun (*this, &Dialog::onHideF12));
165         _dialogs_unhidden_connection = Inkscape::NSApplication::Editor::connectDialogsUnhidden (sigc::mem_fun (*this, &Dialog::onShowF12));
166         _shutdown_connection = Inkscape::NSApplication::Editor::connectShutdown (sigc::mem_fun (*this, &Dialog::onShutdown));
167     }
168     else
169     {
170         g_signal_connect   (G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_retransientize), (void *)this);
171         g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this );
172         g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this );
173         g_signal_connect   (G_OBJECT (INKSCAPE), "shut_down", G_CALLBACK (sp_dialog_shutdown), (void *)this);
174     }
176     g_signal_connect_after( gobj(), "key_press_event", (GCallback)windowKeyPress, NULL );
178     read_geometry();
179     present();
182 Dialog::Dialog(BaseObjectType *gobj)
183     : Gtk::Dialog(gobj)
187 Dialog::~Dialog()
189     if (Inkscape::NSApplication::Application::getNewGui())
190     {
191         _desktop_activated_connection.disconnect();
192         _dialogs_hidden_connection.disconnect();
193         _dialogs_unhidden_connection.disconnect();
194         _shutdown_connection.disconnect();
195     }
196     
197     save_geometry();
201 bool Dialog::windowKeyPress( GtkWidget *widget, GdkEventKey *event )
203     unsigned int shortcut = 0;
204     shortcut = get_group0_keyval (event) |
205         ( event->state & GDK_SHIFT_MASK ?
206           SP_SHORTCUT_SHIFT_MASK : 0 ) |
207         ( event->state & GDK_CONTROL_MASK ?
208           SP_SHORTCUT_CONTROL_MASK : 0 ) |
209         ( event->state & GDK_MOD1_MASK ?
210           SP_SHORTCUT_ALT_MASK : 0 );
211     return sp_shortcut_invoke( shortcut, SP_ACTIVE_DESKTOP );
214 //---------------------------------------------------------------------
216 void
217 Dialog::on_response(int response_id)
219     switch (response_id) {
220         case Gtk::RESPONSE_APPLY: {
221             _apply();
222             break;
223         }
224         case Gtk::RESPONSE_CLOSE: {
225             _close();
226             break;
227         }
228     }
231 bool
232 Dialog::on_delete_event (GdkEventAny *event)
234     save_geometry();
235     _user_hidden = true;
237     return false;
240 void
241 Dialog::onHideF12()
243     _hiddenF12 = true;
244     save_geometry();
245     hide();
248 void
249 Dialog::onShowF12()
251     if (_user_hidden)
252         return;
254     if (_hiddenF12) {
255         show();
256         read_geometry();
257     }
259     _hiddenF12 = false;
262 void 
263 Dialog::onShutdown()
265     save_geometry();
266     _user_hidden = true;
269 void
270 Dialog::onDesktopActivated (SPDesktop *desktop)
272     gint transient_policy = prefs_get_int_attribute_limited ( "options.transientpolicy", "value", 1, 0, 2);
274     if (!transient_policy)
275         return;
277 #ifndef WIN32
278     GtkWindow *dialog_win = GTK_WINDOW(gobj());
280     if (retransientize_suppress) {
281          /* if retransientizing of this dialog is still forbidden after
282           * previous call warning turned off because it was confusingly fired
283           * when loading many files from command line
284           */
286          // g_warning("Retranzientize aborted! You're switching windows too fast!");
287         return;
288     }
290     if (dialog_win)
291     {
292         retransientize_suppress = true; // disallow other attempts to retranzientize this dialog
294         desktop->setWindowTransient (dialog_win);
296         /*
297          * This enables "aggressive" transientization,
298          * i.e. dialogs always emerging on top when you switch documents. Note
299          * however that this breaks "click to raise" policy of a window
300          * manager because the switched-to document will be raised at once
301          * (so that its transients also could raise)
302          */
303         if (transient_policy == 2 && !_hiddenF12 && !_user_hidden) {
304             // without this, a transient window not always emerges on top
305             gtk_window_present (dialog_win);
306         }
307     }
309     // we're done, allow next retransientizing not sooner than after 120 msec
310     gtk_timeout_add (120, (GtkFunction) sp_retransientize_again, (gpointer) this);
311 #endif
315 Inkscape::Selection*
316 Dialog::_getSelection()
318     return SP_DT_SELECTION(SP_ACTIVE_DESKTOP);
321 void
322 Dialog::_apply()
324     g_warning("Apply button clicked for dialog [Dialog::_apply()]");
327 void
328 Dialog::_close()
330     GtkWidget *dlg = GTK_WIDGET(gobj());
332                         /* this code sends a delete_event to the dialog,
333                          * instead of just destroying it, so that the
334                          * dialog can do some housekeeping, such as remember
335                          * its position.
336                          */
337                         GdkEventAny event;
338                         event.type = GDK_DELETE;
339                         event.window = dlg->window;
340                         event.send_event = TRUE;
341                         g_object_ref (G_OBJECT (event.window));
342                         gtk_main_do_event ((GdkEvent*)&event);
343                         g_object_unref (G_OBJECT (event.window));
346 } // namespace Dialog
347 } // namespace UI
348 } // namespace Inkscape
350 /*
351   Local Variables:
352   mode:c++
353   c-file-style:"stroustrup"
354   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
355   indent-tabs-mode:nil
356   fill-column:99
357   End:
358 */
359 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :