Code

family name field on svgfonts dialog now properly saves attribute. Should do the...
[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  * Authors:
6  *   Bryce W. Harrington <bryce@bryceharrington.org>
7  *   buliabyak@gmail.com
8  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9  *   Gustav Broberg <broberg@kth.se>
10  *
11  * Copyright (C) 2004--2007 Authors
12  *
13  * Released under GNU GPL.  Read the file 'COPYING' for more information.
14  */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
20 #include <gtkmm/stock.h>
21 #include <gtk/gtk.h>
22 #include <gdk/gdkkeysyms.h>
24 #include "application/application.h"
25 #include "application/editor.h"
26 #include "inkscape.h"
27 #include "event-context.h"
28 #include "desktop.h"
29 #include "desktop-handles.h"
30 #include "dialog-manager.h"
31 #include "modifier-fns.h"
32 #include "shortcuts.h"
33 #include "prefs-utils.h"
34 #include "interface.h"
35 #include "verbs.h"
37 #define MIN_ONSCREEN_DISTANCE 50
40 namespace Inkscape {
41 namespace UI {
42 namespace Dialog {
44 void
45 sp_retransientize(Inkscape::Application */*inkscape*/, SPDesktop *desktop, gpointer dlgPtr)
46 {
47     Dialog *dlg = (Dialog *)dlgPtr;
48     dlg->onDesktopActivated (desktop);
49 }
51 gboolean
52 sp_retransientize_again(gpointer dlgPtr)
53 {
54     Dialog *dlg = (Dialog *)dlgPtr;
55     dlg->retransientize_suppress = false;
56     return FALSE; // so that it is only called once
57 }
59 void
60 sp_dialog_shutdown(GtkObject */*object*/, gpointer dlgPtr)
61 {
62     Dialog *dlg = (Dialog *)dlgPtr;
63     dlg->onShutdown();
64 }
67 void hideCallback(GtkObject */*object*/, gpointer dlgPtr)
68 {
69     g_return_if_fail( dlgPtr != NULL );
71     Dialog *dlg = (Dialog *)dlgPtr;
72     dlg->onHideF12();
73 }
75 void unhideCallback(GtkObject */*object*/, gpointer dlgPtr)
76 {
77     g_return_if_fail( dlgPtr != NULL );
79     Dialog *dlg = (Dialog *)dlgPtr;
80     dlg->onShowF12();
81 }
84 //=====================================================================
86 /**
87  * UI::Dialog::Dialog is a base class for all dialogs in Inkscape.  The
88  * purpose of this class is to provide a unified place for ensuring
89  * style and behavior.  Specifically, this class provides functionality
90  * for saving and restoring the size and position of dialogs (through
91  * the user's preferences file).
92  *
93  * It also provides some general purpose signal handlers for things like
94  * showing and hiding all dialogs.
95  */
97 Dialog::Dialog(Behavior::BehaviorFactory behavior_factory, const char *prefs_path, int verb_num,
98                Glib::ustring const &apply_label)
99     : _hiddenF12 (false),
100       _prefs_path (prefs_path),
101       _verb_num(verb_num),
102       _apply_label (apply_label)
104     gchar title[500];
106     if (verb_num)
107         sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
109     _title = title;
111     _behavior = behavior_factory(*this);
113     if (Inkscape::NSApplication::Application::getNewGui()) {
114         _desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*this, &Dialog::onDesktopActivated));
115         _dialogs_hidden_connection = Inkscape::NSApplication::Editor::connectDialogsHidden (sigc::mem_fun (*this, &Dialog::onHideF12));
116         _dialogs_unhidden_connection = Inkscape::NSApplication::Editor::connectDialogsUnhidden (sigc::mem_fun (*this, &Dialog::onShowF12));
117         _shutdown_connection = Inkscape::NSApplication::Editor::connectShutdown (sigc::mem_fun (*this, &Dialog::onShutdown));
118     } else {
119         g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop", G_CALLBACK(sp_retransientize), (void *)this);
120         g_signal_connect(G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this);
121         g_signal_connect(G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this);
122         g_signal_connect(G_OBJECT(INKSCAPE), "shut_down", G_CALLBACK(sp_dialog_shutdown), (void *)this);
123     }
125     Glib::wrap(gobj())->signal_event().connect(sigc::mem_fun(*this, &Dialog::_onEvent));
126     Glib::wrap(gobj())->signal_key_press_event().connect(sigc::mem_fun(*this, &Dialog::_onKeyPress));
128     read_geometry();
131 Dialog::~Dialog()
133     if (Inkscape::NSApplication::Application::getNewGui())
134     {
135         _desktop_activated_connection.disconnect();
136         _dialogs_hidden_connection.disconnect();
137         _dialogs_unhidden_connection.disconnect();
138         _shutdown_connection.disconnect();
139     }
141     save_geometry();
142     delete _behavior;
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     int x = prefs_get_int_attribute (_prefs_path, "x", -1000);
210     int y = prefs_get_int_attribute (_prefs_path, "y", -1000);
211     int w = prefs_get_int_attribute (_prefs_path, "w", 0);
212     int h = prefs_get_int_attribute (_prefs_path, "h", 0);
214     // g_print ("read %d %d %d %d\n", x, y, w, h);
216     // If there are stored height and width values for the dialog,
217     // resize the window to match; otherwise we leave it at its default
218     if (w != 0 && h != 0) {
219         resize(w, h);
220     }
222     // If there are stored values for where the dialog should be
223     // located, then restore the dialog to that position.
224     // also check if (x,y) is actually onscreen with the current screen dimensions
225     if ( (x >= 0) && (y >= 0) && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)) ) {
226         move(x, y);
227     } else {
228         // ...otherwise just put it in the middle of the screen
229         set_position(Gtk::WIN_POS_CENTER);
230     }
235 void
236 Dialog::save_geometry()
238     int y, x, w, h;
240     get_position(x, y);
241     get_size(w, h);
243     // g_print ("write %d %d %d %d\n", x, y, w, h);
245     if (x<0) x=0;
246     if (y<0) y=0;
248     prefs_set_int_attribute (_prefs_path, "x", x);
249     prefs_set_int_attribute (_prefs_path, "y", y);
250     prefs_set_int_attribute (_prefs_path, "w", w);
251     prefs_set_int_attribute (_prefs_path, "h", h);
255 void
256 Dialog::_handleResponse(int response_id)
258     switch (response_id) {
259         case Gtk::RESPONSE_CLOSE: {
260             _close();
261             break;
262         }
263     }
266 bool
267 Dialog::_onDeleteEvent(GdkEventAny */*event*/)
269     save_geometry();
270     _user_hidden = true;
272     return false;
275 bool
276 Dialog::_onEvent(GdkEvent *event)
278     bool ret = false;
280     switch (event->type) {
281         case GDK_KEY_PRESS: {
282             switch (get_group0_keyval (&event->key)) {
283                 case GDK_Escape: {
284                     _defocus();
285                     ret = true;
286                     break;
287                 }
288                 case GDK_F4:
289                 case GDK_w:
290                 case GDK_W: {
291                     if (mod_ctrl_only(event->key.state)) {
292                         _close();
293                         ret = true;
294                     }
295                     break;
296                 }
297                 default: { // pass keypress to the canvas
298                     break;
299                 }
300             }
301         }
302         default:
303             ;
304     }
306     return ret;
309 bool
310 Dialog::_onKeyPress(GdkEventKey *event)
312     unsigned int shortcut;
313     shortcut = get_group0_keyval(event) |
314         ( event->state & GDK_SHIFT_MASK ?
315           SP_SHORTCUT_SHIFT_MASK : 0 ) |
316         ( event->state & GDK_CONTROL_MASK ?
317           SP_SHORTCUT_CONTROL_MASK : 0 ) |
318         ( event->state & GDK_MOD1_MASK ?
319           SP_SHORTCUT_ALT_MASK : 0 );
320     return sp_shortcut_invoke(shortcut, SP_ACTIVE_DESKTOP);
323 void
324 Dialog::_apply()
326     g_warning("Apply button clicked for dialog [Dialog::_apply()]");
329 void
330 Dialog::_close()
332     GtkWidget *dlg = GTK_WIDGET(_behavior->gobj());
334     /* this code sends a delete_event to the dialog,
335      * instead of just destroying it, so that the
336      * dialog can do some housekeeping, such as remember
337      * its position.
338      */
340     GdkEventAny event;
341     event.type = GDK_DELETE;
342     event.window = dlg->window;
343     event.send_event = TRUE;
345     if (event.window)
346         g_object_ref(G_OBJECT(event.window));
348     gtk_main_do_event ((GdkEvent*)&event);
350     if (event.window)
351         g_object_unref(G_OBJECT(event.window));
354 void
355 Dialog::_defocus()
357     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
359     if (desktop) {
360         Gtk::Widget *canvas = Glib::wrap(GTK_WIDGET(desktop->canvas));
362         // make sure the canvas window is present before giving it focus
363         Gtk::Window *toplevel_window = dynamic_cast<Gtk::Window *>(canvas->get_toplevel());
364         if (toplevel_window)
365             toplevel_window->present();
367         canvas->grab_focus();
368     }
371 Inkscape::Selection*
372 Dialog::_getSelection()
374     return sp_desktop_selection(SP_ACTIVE_DESKTOP);
377 } // namespace Dialog
378 } // namespace UI
379 } // namespace Inkscape
381 /*
382   Local Variables:
383   mode:c++
384   c-file-style:"stroustrup"
385   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
386   indent-tabs-mode:nil
387   fill-column:99
388   End:
389 */
390 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :