Code

Extensions. Shebangs branch merge.
[inkscape.git] / src / ui / dialog / dialog.cpp
1 /** @file
2  * @brief Base class for dialogs in Inkscape - implementation
3  */
4 /* Authors:
5  *   Bryce W. Harrington <bryce@bryceharrington.org>
6  *   buliabyak@gmail.com
7  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
8  *   Gustav Broberg <broberg@kth.se>
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>
21 #include <gdk/gdkkeysyms.h>
23 #include "application/application.h"
24 #include "application/editor.h"
25 #include "inkscape.h"
26 #include "event-context.h"
27 #include "desktop.h"
28 #include "desktop-handles.h"
29 #include "dialog-manager.h"
30 #include "modifier-fns.h"
31 #include "shortcuts.h"
32 #include "preferences.h"
33 #include "interface.h"
34 #include "verbs.h"
36 #define MIN_ONSCREEN_DISTANCE 50
39 namespace Inkscape {
40 namespace UI {
41 namespace Dialog {
43 void
44 sp_retransientize(Inkscape::Application */*inkscape*/, SPDesktop *desktop, gpointer dlgPtr)
45 {
46     Dialog *dlg = (Dialog *)dlgPtr;
47     dlg->onDesktopActivated (desktop);
48 }
50 gboolean
51 sp_retransientize_again(gpointer dlgPtr)
52 {
53     Dialog *dlg = (Dialog *)dlgPtr;
54     dlg->retransientize_suppress = false;
55     return FALSE; // so that it is only called once
56 }
58 void
59 sp_dialog_shutdown(GtkObject */*object*/, gpointer dlgPtr)
60 {
61     Dialog *dlg = (Dialog *)dlgPtr;
62     dlg->onShutdown();
63 }
66 void hideCallback(GtkObject */*object*/, gpointer dlgPtr)
67 {
68     g_return_if_fail( dlgPtr != NULL );
70     Dialog *dlg = (Dialog *)dlgPtr;
71     dlg->onHideF12();
72 }
74 void unhideCallback(GtkObject */*object*/, gpointer dlgPtr)
75 {
76     g_return_if_fail( dlgPtr != NULL );
78     Dialog *dlg = (Dialog *)dlgPtr;
79     dlg->onShowF12();
80 }
83 //=====================================================================
85 /**
86  * UI::Dialog::Dialog is a base class for all dialogs in Inkscape.  The
87  * purpose of this class is to provide a unified place for ensuring
88  * style and behavior.  Specifically, this class provides functionality
89  * for saving and restoring the size and position of dialogs (through
90  * the user's preferences file).
91  *
92  * It also provides some general purpose signal handlers for things like
93  * showing and hiding all dialogs.
94  */
96 Dialog::Dialog(Behavior::BehaviorFactory behavior_factory, const char *prefs_path, int verb_num,
97                Glib::ustring const &apply_label)
98     : _hiddenF12 (false),
99       _prefs_path (prefs_path),
100       _verb_num(verb_num),
101       _apply_label (apply_label)
103     gchar title[500];
105     if (verb_num)
106         sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
108     _title = title;
110     _behavior = behavior_factory(*this);
112     if (Inkscape::NSApplication::Application::getNewGui()) {
113         _desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*this, &Dialog::onDesktopActivated));
114         _dialogs_hidden_connection = Inkscape::NSApplication::Editor::connectDialogsHidden (sigc::mem_fun (*this, &Dialog::onHideF12));
115         _dialogs_unhidden_connection = Inkscape::NSApplication::Editor::connectDialogsUnhidden (sigc::mem_fun (*this, &Dialog::onShowF12));
116         _shutdown_connection = Inkscape::NSApplication::Editor::connectShutdown (sigc::mem_fun (*this, &Dialog::onShutdown));
117     } else {
118         g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop", G_CALLBACK(sp_retransientize), (void *)this);
119         g_signal_connect(G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this);
120         g_signal_connect(G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this);
121         g_signal_connect(G_OBJECT(INKSCAPE), "shut_down", G_CALLBACK(sp_dialog_shutdown), (void *)this);
122     }
124     Glib::wrap(gobj())->signal_event().connect(sigc::mem_fun(*this, &Dialog::_onEvent));
125     Glib::wrap(gobj())->signal_key_press_event().connect(sigc::mem_fun(*this, &Dialog::_onKeyPress));
127     read_geometry();
130 Dialog::~Dialog()
132     if (Inkscape::NSApplication::Application::getNewGui())
133     {
134         _desktop_activated_connection.disconnect();
135         _dialogs_hidden_connection.disconnect();
136         _dialogs_unhidden_connection.disconnect();
137         _shutdown_connection.disconnect();
138     }
140     save_geometry();
141     delete _behavior;
142     _behavior = 0;
146 //---------------------------------------------------------------------
149 void
150 Dialog::onDesktopActivated(SPDesktop *desktop)
152     _behavior->onDesktopActivated(desktop);
155 void
156 Dialog::onShutdown()
158     save_geometry();
159     _user_hidden = true;
160     _behavior->onShutdown();
163 void
164 Dialog::onHideF12()
166     _hiddenF12 = true;
167     _behavior->onHideF12();
170 void
171 Dialog::onShowF12()
173     if (_user_hidden)
174         return;
176     if (_hiddenF12) {
177         _behavior->onShowF12();
178     }
180     _hiddenF12 = false;
184 inline Dialog::operator Gtk::Widget &()                          { return *_behavior; }
185 inline GtkWidget *Dialog::gobj()                                 { return _behavior->gobj(); }
186 inline void Dialog::present()                                    { _behavior->present(); }
187 inline Gtk::VBox *Dialog::get_vbox()                             {  return _behavior->get_vbox(); }
188 inline void Dialog::hide()                                       { _behavior->hide(); }
189 inline void Dialog::show()                                       { _behavior->show(); }
190 inline void Dialog::show_all_children()                          { _behavior->show_all_children(); }
191 inline void Dialog::set_size_request(int width, int height)      { _behavior->set_size_request(width, height); }
192 inline void Dialog::size_request(Gtk::Requisition &requisition)  { _behavior->size_request(requisition); }
193 inline void Dialog::get_position(int &x, int &y)                 { _behavior->get_position(x, y); }
194 inline void Dialog::get_size(int &width, int &height)            { _behavior->get_size(width, height); }
195 inline void Dialog::resize(int width, int height)                { _behavior->resize(width, height); }
196 inline void Dialog::move(int x, int y)                           { _behavior->move(x, y); }
197 inline void Dialog::set_position(Gtk::WindowPosition position)   { _behavior->set_position(position); }
198 inline void Dialog::set_title(Glib::ustring title)               { _behavior->set_title(title); }
199 inline void Dialog::set_sensitive(bool sensitive)                { _behavior->set_sensitive(sensitive); }
201 Glib::SignalProxy0<void> Dialog::signal_show() { return _behavior->signal_show(); }
202 Glib::SignalProxy0<void> Dialog::signal_hide() { return _behavior->signal_hide(); }
204 void
205 Dialog::read_geometry()
207     _user_hidden = false;
209     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
210     int x = prefs->getInt(_prefs_path + "/x", -1000);
211     int y = prefs->getInt(_prefs_path + "/y", -1000);
212     int w = prefs->getInt(_prefs_path + "/w", 0);
213     int h = prefs->getInt(_prefs_path + "/h", 0);
215     // g_print ("read %d %d %d %d\n", x, y, w, h);
217     // If there are stored height and width values for the dialog,
218     // resize the window to match; otherwise we leave it at its default
219     if (w != 0 && h != 0) {
220         resize(w, h);
221     }
223     // If there are stored values for where the dialog should be
224     // located, then restore the dialog to that position.
225     // also check if (x,y) is actually onscreen with the current screen dimensions
226     if ( (x >= 0) && (y >= 0) && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)) ) {
227         move(x, y);
228     } else {
229         // ...otherwise just put it in the middle of the screen
230         set_position(Gtk::WIN_POS_CENTER);
231     }
236 void
237 Dialog::save_geometry()
239     int y, x, w, h;
241     get_position(x, y);
242     get_size(w, h);
244     // g_print ("write %d %d %d %d\n", x, y, w, h);
246     if (x<0) x=0;
247     if (y<0) y=0;
249     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
250     prefs->setInt(_prefs_path + "/x", x);
251     prefs->setInt(_prefs_path + "/y", y);
252     prefs->setInt(_prefs_path + "/w", w);
253     prefs->setInt(_prefs_path + "/h", h);
257 void
258 Dialog::_handleResponse(int response_id)
260     switch (response_id) {
261         case Gtk::RESPONSE_CLOSE: {
262             _close();
263             break;
264         }
265     }
268 bool
269 Dialog::_onDeleteEvent(GdkEventAny */*event*/)
271     save_geometry();
272     _user_hidden = true;
274     return false;
277 bool
278 Dialog::_onEvent(GdkEvent *event)
280     bool ret = false;
282     switch (event->type) {
283         case GDK_KEY_PRESS: {
284             switch (get_group0_keyval (&event->key)) {
285                 case GDK_Escape: {
286                     _defocus();
287                     ret = true;
288                     break;
289                 }
290                 case GDK_F4:
291                 case GDK_w:
292                 case GDK_W: {
293                     if (mod_ctrl_only(event->key.state)) {
294                         _close();
295                         ret = true;
296                     }
297                     break;
298                 }
299                 default: { // pass keypress to the canvas
300                     break;
301                 }
302             }
303         }
304         default:
305             ;
306     }
308     return ret;
311 bool
312 Dialog::_onKeyPress(GdkEventKey *event)
314     unsigned int shortcut;
315     shortcut = get_group0_keyval(event) |
316         ( event->state & GDK_SHIFT_MASK ?
317           SP_SHORTCUT_SHIFT_MASK : 0 ) |
318         ( event->state & GDK_CONTROL_MASK ?
319           SP_SHORTCUT_CONTROL_MASK : 0 ) |
320         ( event->state & GDK_MOD1_MASK ?
321           SP_SHORTCUT_ALT_MASK : 0 );
322     return sp_shortcut_invoke(shortcut, SP_ACTIVE_DESKTOP);
325 void
326 Dialog::_apply()
328     g_warning("Apply button clicked for dialog [Dialog::_apply()]");
331 void
332 Dialog::_close()
334     GtkWidget *dlg = GTK_WIDGET(_behavior->gobj());
336     /* this code sends a delete_event to the dialog,
337      * instead of just destroying it, so that the
338      * dialog can do some housekeeping, such as remember
339      * its position.
340      */
342     GdkEventAny event;
343     event.type = GDK_DELETE;
344     event.window = dlg->window;
345     event.send_event = TRUE;
347     if (event.window)
348         g_object_ref(G_OBJECT(event.window));
350     gtk_main_do_event ((GdkEvent*)&event);
352     if (event.window)
353         g_object_unref(G_OBJECT(event.window));
356 void
357 Dialog::_defocus()
359     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
361     if (desktop) {
362         Gtk::Widget *canvas = Glib::wrap(GTK_WIDGET(desktop->canvas));
364         // make sure the canvas window is present before giving it focus
365         Gtk::Window *toplevel_window = dynamic_cast<Gtk::Window *>(canvas->get_toplevel());
366         if (toplevel_window)
367             toplevel_window->present();
369         canvas->grab_focus();
370     }
373 Inkscape::Selection*
374 Dialog::_getSelection()
376     return sp_desktop_selection(SP_ACTIVE_DESKTOP);
379 } // namespace Dialog
380 } // namespace UI
381 } // namespace Inkscape
383 /*
384   Local Variables:
385   mode:c++
386   c-file-style:"stroustrup"
387   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
388   indent-tabs-mode:nil
389   fill-column:99
390   End:
391 */
392 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :