Code

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