Code

Dockable dialogs patch applied
[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 static void
44 sp_retransientize (Inkscape::Application *inkscape, SPDesktop *desktop, gpointer dlgPtr)
45 {
46     Dialog *dlg = (Dialog *)dlgPtr;
47     dlg->onDesktopActivated (desktop);
48 }
50 static void
51 sp_dialog_shutdown (GtkObject *object, gpointer dlgPtr)
52 {
53     Dialog *dlg = (Dialog *)dlgPtr;
54     dlg->onShutdown();
55 }
57 void
58 Dialog::save_geometry()
59 {
60     int y, x, w, h;
62     get_position(x, y);
63     get_size(w, h);
65     // g_print ("write %d %d %d %d\n", x, y, w, h);
67     if (x<0) x=0;
68     if (y<0) y=0;
70     prefs_set_int_attribute (_prefs_path, "x", x);
71     prefs_set_int_attribute (_prefs_path, "y", y);
72     prefs_set_int_attribute (_prefs_path, "w", w);
73     prefs_set_int_attribute (_prefs_path, "h", h);
75 }
77 void hideCallback(GtkObject *object, gpointer dlgPtr)
78 {
79     g_return_if_fail( dlgPtr != NULL );
81     Dialog *dlg = (Dialog *)dlgPtr;
82     dlg->onHideF12();
83 }
85 void unhideCallback(GtkObject *object, gpointer dlgPtr)
86 {
87     g_return_if_fail( dlgPtr != NULL );
89     Dialog *dlg = (Dialog *)dlgPtr;
90     dlg->onShowF12();
91 }
95 void
96 Dialog::read_geometry()
97 {
98     _user_hidden = false;
100     int x = prefs_get_int_attribute (_prefs_path, "x", -1000);
101     int y = prefs_get_int_attribute (_prefs_path, "y", -1000);
102     int w = prefs_get_int_attribute (_prefs_path, "w", 0);
103     int h = prefs_get_int_attribute (_prefs_path, "h", 0);
105     // g_print ("read %d %d %d %d\n", x, y, w, h);
107     // If there are stored height and width values for the dialog,
108     // resize the window to match; otherwise we leave it at its default
109     if (w != 0 && h != 0) {
110         resize(w, h);
111     }
112     
113     // If there are stored values for where the dialog should be
114     // located, then restore the dialog to that position.
115     // also check if (x,y) is actually onscreen with the current screen dimensions
116     if ( (x >= 0) && (y >= 0) && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)) ) {
117         move(x, y);
118     } else {
119         // ...otherwise just put it in the middle of the screen
120         set_position(Gtk::WIN_POS_CENTER);
121     }
125 inline Dialog::operator Gtk::Widget&()                           { return *_behavior; }
126 inline GtkWidget *Dialog::gobj()                                 { return _behavior->gobj(); }
127 inline void Dialog::present()                                    { _behavior->present(); }
128 inline Gtk::VBox *Dialog::get_vbox()                             {  return _behavior->get_vbox(); }
129 inline void Dialog::show_all_children()                          { _behavior->show_all_children(); }
130 inline void Dialog::hide()                                       { _behavior->hide(); }
131 inline void Dialog::show()                                       { _behavior->show(); }
132 inline void Dialog::set_size_request(int width, int height)      { _behavior->set_size_request(width, height); }
133 inline void Dialog::size_request(Gtk::Requisition& requisition)  { _behavior->size_request(requisition); }
134 inline void Dialog::get_position(int& x, int& y)                 { _behavior->get_position(x, y); }
135 inline void Dialog::get_size(int& width, int& height)            { _behavior->get_size(width, height); }
136 inline void Dialog::resize(int width, int height)                { _behavior->resize(width, height); }
137 inline void Dialog::move(int x, int y)                           { _behavior->move(x, y); }
138 inline void Dialog::set_position(Gtk::WindowPosition position)   { _behavior->set_position(position); }
139 inline void Dialog::set_title(Glib::ustring title)               { _behavior->set_title(title); }
140 inline void Dialog::set_sensitive(bool sensitive)                { _behavior->set_sensitive(sensitive); }
142 inline void Dialog::set_response_sensitive(int response_id, bool setting)
143 { _behavior->set_response_sensitive(response_id, setting); }
145 void Dialog::set_resizable(bool) { }
146 void Dialog::set_default(Gtk::Widget&) { }
148 inline void Dialog::set_default_response(int response_id) { _behavior->set_default_response(response_id); }
150 Glib::SignalProxy0<void> Dialog::signal_show () { return _behavior->signal_show(); }
151 Glib::SignalProxy0<void> Dialog::signal_hide () { return _behavior->signal_hide(); }
152 Glib::SignalProxy1<void, int> Dialog::signal_response () { return _behavior->signal_response(); }
154 Gtk::Button* Dialog::add_button (const Glib::ustring& button_text, int response_id) 
155 { return _behavior->add_button(button_text, response_id); }
157 Gtk::Button* Dialog::add_button (const Gtk::StockID& stock_id, int response_id)
158 { return _behavior->add_button(stock_id, response_id); }
160 Dialog::Dialog(const char *prefs_path, int verb_num, const char *apply_label)
165 //=====================================================================
167 /**
168  * UI::Dialog::Dialog is a base class for all dialogs in Inkscape.  The
169  * purpose of this class is to provide a unified place for ensuring
170  * style and behavior.  Specifically, this class provides functionality
171  * for saving and restoring the size and position of dialogs (through
172  * the user's preferences file).
173  *
174  * It also provides some general purpose signal handlers for things like
175  * showing and hiding all dialogs.
176  */
177 Dialog::Dialog(Behavior::BehaviorFactory behavior_factory, const char *prefs_path, int verb_num,
178                const char *apply_label) 
179     : _hiddenF12 (false),
180       _prefs_path (prefs_path),
181       _verb_num(verb_num),
182       _apply_label (apply_label)
184     gchar title[500];
186     if (verb_num)
187         sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
189     _title = title;
191     _behavior = behavior_factory(*this);
193     gtk_signal_connect( GTK_OBJECT (gobj()), "event", GTK_SIGNAL_FUNC(sp_dialog_event_handler), gobj() );
195     if (Inkscape::NSApplication::Application::getNewGui())
196     {
197         _desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*this, &Dialog::onDesktopActivated));
198         _dialogs_hidden_connection = Inkscape::NSApplication::Editor::connectDialogsHidden (sigc::mem_fun (*this, &Dialog::onHideF12));
199         _dialogs_unhidden_connection = Inkscape::NSApplication::Editor::connectDialogsUnhidden (sigc::mem_fun (*this, &Dialog::onShowF12));
200         _shutdown_connection = Inkscape::NSApplication::Editor::connectShutdown (sigc::mem_fun (*this, &Dialog::onShutdown));
201     }
202     else
203     {
204         g_signal_connect (G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_retransientize), (void *)this);
205         g_signal_connect ( G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this );
206         g_signal_connect ( G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this );
207         g_signal_connect (G_OBJECT (INKSCAPE), "shut_down", G_CALLBACK (sp_dialog_shutdown), (void *)this);
208     }
210     Glib::wrap(gobj())->signal_key_press_event().connect(sigc::ptr_fun(&windowKeyPress));
212     if (prefs_get_int_attribute ("dialogs", "showclose", 0) || apply_label) {
213         // TODO: make the order of buttons obey the global preference
214         if (apply_label) {
215             add_button(Glib::ustring(apply_label), Gtk::RESPONSE_APPLY);
216             set_default_response(Gtk::RESPONSE_APPLY);
217         }
218         add_button(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
219     }
221     read_geometry();
222     present();
225 Dialog::~Dialog()
227     if (Inkscape::NSApplication::Application::getNewGui())
228     {
229         _desktop_activated_connection.disconnect();
230         _dialogs_hidden_connection.disconnect();
231         _dialogs_unhidden_connection.disconnect();
232         _shutdown_connection.disconnect();
233     }
234     
235     save_geometry();
236     delete _behavior;
240 bool Dialog::windowKeyPress(GdkEventKey *event)
242     unsigned int shortcut = 0;
243     shortcut = get_group0_keyval (event) |
244         ( event->state & GDK_SHIFT_MASK ?
245           SP_SHORTCUT_SHIFT_MASK : 0 ) |
246         ( event->state & GDK_CONTROL_MASK ?
247           SP_SHORTCUT_CONTROL_MASK : 0 ) |
248         ( event->state & GDK_MOD1_MASK ?
249           SP_SHORTCUT_ALT_MASK : 0 );
251     return sp_shortcut_invoke( shortcut, SP_ACTIVE_DESKTOP );
254 //---------------------------------------------------------------------
256 void
257 Dialog::on_response(int response_id)
259     switch (response_id) {
260         case Gtk::RESPONSE_APPLY: {
261             _apply();
262             break;
263         }
264         case Gtk::RESPONSE_CLOSE: {
265             _close();
266             break;
267         }
268     }
271 bool
272 Dialog::on_delete_event (GdkEventAny *event)
274     save_geometry();
275     _user_hidden = true;
277     return false;
280 void
281 Dialog::onHideF12()
283     _hiddenF12 = true;
284     _behavior->onHideF12();
287 void
288 Dialog::onShowF12()
290     if (_user_hidden)
291         return;
293     if (_hiddenF12) {
294         _behavior->onShowF12();
295     }
297     _hiddenF12 = false;
300 void 
301 Dialog::onShutdown()
303     save_geometry();
304     _user_hidden = true;
305     _behavior->onShutdown();
308 void
309 Dialog::onDesktopActivated(SPDesktop *desktop)
311     _behavior->onDesktopActivated(desktop);
314 Inkscape::Selection*
315 Dialog::_getSelection()
317     return sp_desktop_selection(SP_ACTIVE_DESKTOP);
320 void
321 Dialog::_apply()
323     g_warning("Apply button clicked for dialog [Dialog::_apply()]");
326 void
327 Dialog::_close()
329     GtkWidget *dlg = GTK_WIDGET(_behavior->gobj());
331     /* this code sends a delete_event to the dialog,
332      * instead of just destroying it, so that the
333      * dialog can do some housekeeping, such as remember
334      * its position.
335      */
337     GdkEventAny event;
338     event.type = GDK_DELETE;
339     event.window = dlg->window;
340     event.send_event = TRUE;
342     if (event.window) 
343         g_object_ref (G_OBJECT (event.window));
345     gtk_main_do_event ((GdkEvent*)&event);
347     if (event.window) 
348         g_object_unref (G_OBJECT (event.window));
351 } // namespace Dialog
352 } // namespace UI
353 } // namespace Inkscape
355 /*
356   Local Variables:
357   mode:c++
358   c-file-style:"stroustrup"
359   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
360   indent-tabs-mode:nil
361   fill-column:99
362   End:
363 */
364 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :