Code

Remove redundant dialog present(), make sure user_hidden is set for
[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();
232 Dialog::~Dialog()
234     if (Inkscape::NSApplication::Application::getNewGui())
235     {
236         _desktop_activated_connection.disconnect();
237         _dialogs_hidden_connection.disconnect();
238         _dialogs_unhidden_connection.disconnect();
239         _shutdown_connection.disconnect();
240     }
241     
242     save_geometry();
243     delete _behavior;
247 bool Dialog::windowKeyPress(GdkEventKey *event)
249     unsigned int shortcut = 0;
250     shortcut = get_group0_keyval (event) |
251         ( event->state & GDK_SHIFT_MASK ?
252           SP_SHORTCUT_SHIFT_MASK : 0 ) |
253         ( event->state & GDK_CONTROL_MASK ?
254           SP_SHORTCUT_CONTROL_MASK : 0 ) |
255         ( event->state & GDK_MOD1_MASK ?
256           SP_SHORTCUT_ALT_MASK : 0 );
258     return sp_shortcut_invoke( shortcut, SP_ACTIVE_DESKTOP );
261 //---------------------------------------------------------------------
263 void
264 Dialog::on_response(int response_id)
266     switch (response_id) {
267         case Gtk::RESPONSE_APPLY: {
268             _apply();
269             break;
270         }
271         case Gtk::RESPONSE_CLOSE: {
272             _close();
273             break;
274         }
275     }
278 bool
279 Dialog::on_delete_event (GdkEventAny *event)
281     save_geometry();
282     _user_hidden = true;
284     return false;
287 void
288 Dialog::onHideF12()
290     _hiddenF12 = true;
291     _behavior->onHideF12();
294 void
295 Dialog::onShowF12()
297     if (_user_hidden)
298         return;
300     if (_hiddenF12) {
301         _behavior->onShowF12();
302     }
304     _hiddenF12 = false;
307 void 
308 Dialog::onShutdown()
310     save_geometry();
311     _user_hidden = true;
312     _behavior->onShutdown();
315 void
316 Dialog::onDesktopActivated(SPDesktop *desktop)
318     _behavior->onDesktopActivated(desktop);
321 Inkscape::Selection*
322 Dialog::_getSelection()
324     return sp_desktop_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(_behavior->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      */
344     GdkEventAny event;
345     event.type = GDK_DELETE;
346     event.window = dlg->window;
347     event.send_event = TRUE;
349     if (event.window) 
350         g_object_ref (G_OBJECT (event.window));
352     gtk_main_do_event ((GdkEvent*)&event);
354     if (event.window) 
355         g_object_unref (G_OBJECT (event.window));
358 } // namespace Dialog
359 } // namespace UI
360 } // namespace Inkscape
362 /*
363   Local Variables:
364   mode:c++
365   c-file-style:"stroustrup"
366   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
367   indent-tabs-mode:nil
368   fill-column:99
369   End:
370 */
371 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :