Code

copyedit
[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>
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 "dialogs/dialog-events.h"
31 #include "shortcuts.h"
32 #include "prefs-utils.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 }
65 void
66 Dialog::save_geometry()
67 {
68     int y, x, w, h;
70     get_position(x, y);
71     get_size(w, h);
73     // g_print ("write %d %d %d %d\n", x, y, w, h);
75     if (x<0) x=0;
76     if (y<0) y=0;
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);
83 }
85 void hideCallback(GtkObject *object, gpointer dlgPtr)
86 {
87     g_return_if_fail( dlgPtr != NULL );
89     Dialog *dlg = (Dialog *)dlgPtr;
90     dlg->onHideF12();
91 }
93 void unhideCallback(GtkObject *object, gpointer dlgPtr)
94 {
95     g_return_if_fail( dlgPtr != NULL );
97     Dialog *dlg = (Dialog *)dlgPtr;
98     dlg->onShowF12();
99 }
103 void
104 Dialog::read_geometry()
106     _user_hidden = false;
108     int x = prefs_get_int_attribute (_prefs_path, "x", -1000);
109     int y = prefs_get_int_attribute (_prefs_path, "y", -1000);
110     int w = prefs_get_int_attribute (_prefs_path, "w", 0);
111     int h = prefs_get_int_attribute (_prefs_path, "h", 0);
113     // g_print ("read %d %d %d %d\n", x, y, w, h);
115     // If there are stored height and width values for the dialog,
116     // resize the window to match; otherwise we leave it at its default
117     if (w != 0 && h != 0) {
118         resize(w, h);
119     }
120     
121     // If there are stored values for where the dialog should be
122     // located, then restore the dialog to that position.
123     // also check if (x,y) is actually onscreen with the current screen dimensions
124     if ( (x >= 0) && (y >= 0) && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)) ) {
125         move(x, y);
126     } else {
127         // ...otherwise just put it in the middle of the screen
128         set_position(Gtk::WIN_POS_CENTER);
129     }
133 inline Dialog::operator Gtk::Widget&()                           { return *_behavior; }
134 inline GtkWidget *Dialog::gobj()                                 { return _behavior->gobj(); }
135 inline void Dialog::present()                                    { _behavior->present(); }
136 inline Gtk::VBox *Dialog::get_vbox()                             {  return _behavior->get_vbox(); }
137 inline void Dialog::show_all_children()                          { _behavior->show_all_children(); }
138 inline void Dialog::hide()                                       { _behavior->hide(); }
139 inline void Dialog::show()                                       { _behavior->show(); }
140 inline void Dialog::set_size_request(int width, int height)      { _behavior->set_size_request(width, height); }
141 inline void Dialog::size_request(Gtk::Requisition& requisition)  { _behavior->size_request(requisition); }
142 inline void Dialog::get_position(int& x, int& y)                 { _behavior->get_position(x, y); }
143 inline void Dialog::get_size(int& width, int& height)            { _behavior->get_size(width, height); }
144 inline void Dialog::resize(int width, int height)                { _behavior->resize(width, height); }
145 inline void Dialog::move(int x, int y)                           { _behavior->move(x, y); }
146 inline void Dialog::set_position(Gtk::WindowPosition position)   { _behavior->set_position(position); }
147 inline void Dialog::set_title(Glib::ustring title)               { _behavior->set_title(title); }
148 inline void Dialog::set_sensitive(bool sensitive)                { _behavior->set_sensitive(sensitive); }
150 inline void Dialog::set_response_sensitive(int response_id, bool setting)
151 { _behavior->set_response_sensitive(response_id, setting); }
153 void Dialog::set_resizable(bool) { }
154 void Dialog::set_default(Gtk::Widget&) { }
156 inline void Dialog::set_default_response(int response_id) { _behavior->set_default_response(response_id); }
158 Glib::SignalProxy0<void> Dialog::signal_show () { return _behavior->signal_show(); }
159 Glib::SignalProxy0<void> Dialog::signal_hide () { return _behavior->signal_hide(); }
160 Glib::SignalProxy1<void, int> Dialog::signal_response () { return _behavior->signal_response(); }
162 Gtk::Button* Dialog::add_button (const Glib::ustring& button_text, int response_id) 
163 { return _behavior->add_button(button_text, response_id); }
165 Gtk::Button* Dialog::add_button (const Gtk::StockID& stock_id, int response_id)
166 { return _behavior->add_button(stock_id, response_id); }
168 Dialog::Dialog(const char *prefs_path, int verb_num, const char *apply_label)
173 //=====================================================================
175 /**
176  * UI::Dialog::Dialog is a base class for all dialogs in Inkscape.  The
177  * purpose of this class is to provide a unified place for ensuring
178  * style and behavior.  Specifically, this class provides functionality
179  * for saving and restoring the size and position of dialogs (through
180  * the user's preferences file).
181  *
182  * It also provides some general purpose signal handlers for things like
183  * showing and hiding all dialogs.
184  */
185 Dialog::Dialog(Behavior::BehaviorFactory behavior_factory, const char *prefs_path, int verb_num,
186                const char *apply_label) 
187     : _hiddenF12 (false),
188       _prefs_path (prefs_path),
189       _verb_num(verb_num),
190       _apply_label (apply_label)
192     gchar title[500];
194     if (verb_num)
195         sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
197     _title = title;
199     _behavior = behavior_factory(*this);
201     gtk_signal_connect( GTK_OBJECT (gobj()), "event", GTK_SIGNAL_FUNC(sp_dialog_event_handler), gobj() );
203     if (Inkscape::NSApplication::Application::getNewGui())
204     {
205         _desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*this, &Dialog::onDesktopActivated));
206         _dialogs_hidden_connection = Inkscape::NSApplication::Editor::connectDialogsHidden (sigc::mem_fun (*this, &Dialog::onHideF12));
207         _dialogs_unhidden_connection = Inkscape::NSApplication::Editor::connectDialogsUnhidden (sigc::mem_fun (*this, &Dialog::onShowF12));
208         _shutdown_connection = Inkscape::NSApplication::Editor::connectShutdown (sigc::mem_fun (*this, &Dialog::onShutdown));
209     }
210     else
211     {
212         g_signal_connect (G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_retransientize), (void *)this);
213         g_signal_connect ( G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this );
214         g_signal_connect ( G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this );
215         g_signal_connect (G_OBJECT (INKSCAPE), "shut_down", G_CALLBACK (sp_dialog_shutdown), (void *)this);
216     }
218     Glib::wrap(gobj())->signal_key_press_event().connect(sigc::ptr_fun(&windowKeyPress));
220     if (prefs_get_int_attribute ("dialogs", "showclose", 0) || apply_label) {
221         // TODO: make the order of buttons obey the global preference
222         if (apply_label) {
223             add_button(Glib::ustring(apply_label), Gtk::RESPONSE_APPLY);
224             set_default_response(Gtk::RESPONSE_APPLY);
225         }
226         add_button(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
227     }
229     read_geometry();
230     present();
233 Dialog::~Dialog()
235     if (Inkscape::NSApplication::Application::getNewGui())
236     {
237         _desktop_activated_connection.disconnect();
238         _dialogs_hidden_connection.disconnect();
239         _dialogs_unhidden_connection.disconnect();
240         _shutdown_connection.disconnect();
241     }
242     
243     save_geometry();
244     delete _behavior;
248 bool Dialog::windowKeyPress(GdkEventKey *event)
250     unsigned int shortcut = 0;
251     shortcut = get_group0_keyval (event) |
252         ( event->state & GDK_SHIFT_MASK ?
253           SP_SHORTCUT_SHIFT_MASK : 0 ) |
254         ( event->state & GDK_CONTROL_MASK ?
255           SP_SHORTCUT_CONTROL_MASK : 0 ) |
256         ( event->state & GDK_MOD1_MASK ?
257           SP_SHORTCUT_ALT_MASK : 0 );
259     return sp_shortcut_invoke( shortcut, SP_ACTIVE_DESKTOP );
262 //---------------------------------------------------------------------
264 void
265 Dialog::on_response(int response_id)
267     switch (response_id) {
268         case Gtk::RESPONSE_APPLY: {
269             _apply();
270             break;
271         }
272         case Gtk::RESPONSE_CLOSE: {
273             _close();
274             break;
275         }
276     }
279 bool
280 Dialog::on_delete_event (GdkEventAny *event)
282     save_geometry();
283     _user_hidden = true;
285     return false;
288 void
289 Dialog::onHideF12()
291     _hiddenF12 = true;
292     _behavior->onHideF12();
295 void
296 Dialog::onShowF12()
298     if (_user_hidden)
299         return;
301     if (_hiddenF12) {
302         _behavior->onShowF12();
303     }
305     _hiddenF12 = false;
308 void 
309 Dialog::onShutdown()
311     save_geometry();
312     _user_hidden = true;
313     _behavior->onShutdown();
316 void
317 Dialog::onDesktopActivated(SPDesktop *desktop)
319     _behavior->onDesktopActivated(desktop);
322 Inkscape::Selection*
323 Dialog::_getSelection()
325     return sp_desktop_selection(SP_ACTIVE_DESKTOP);
328 void
329 Dialog::_apply()
331     g_warning("Apply button clicked for dialog [Dialog::_apply()]");
334 void
335 Dialog::_close()
337     GtkWidget *dlg = GTK_WIDGET(_behavior->gobj());
339     /* this code sends a delete_event to the dialog,
340      * instead of just destroying it, so that the
341      * dialog can do some housekeeping, such as remember
342      * its position.
343      */
345     GdkEventAny event;
346     event.type = GDK_DELETE;
347     event.window = dlg->window;
348     event.send_event = TRUE;
350     if (event.window) 
351         g_object_ref (G_OBJECT (event.window));
353     gtk_main_do_event ((GdkEvent*)&event);
355     if (event.window) 
356         g_object_unref (G_OBJECT (event.window));
359 } // namespace Dialog
360 } // namespace UI
361 } // namespace Inkscape
363 /*
364   Local Variables:
365   mode:c++
366   c-file-style:"stroustrup"
367   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
368   indent-tabs-mode:nil
369   fill-column:99
370   End:
371 */
372 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :