Code

non-lcms build fixes
[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                const char *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);
112     
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     if (prefs_get_int_attribute ("dialogs", "showclose", 0) || apply_label) {
129         // TODO: make the order of buttons obey the global preference
130         if (apply_label) {
131             add_button(Glib::ustring(apply_label), Gtk::RESPONSE_APPLY);
132             set_default_response(Gtk::RESPONSE_APPLY);
133         }
134         add_button(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
135     }
137     read_geometry();
140 Dialog::~Dialog()
142     if (Inkscape::NSApplication::Application::getNewGui())
143     {
144         _desktop_activated_connection.disconnect();
145         _dialogs_hidden_connection.disconnect();
146         _dialogs_unhidden_connection.disconnect();
147         _shutdown_connection.disconnect();
148     }
149     
150     save_geometry();
151     delete _behavior;
155 //---------------------------------------------------------------------
158 void
159 Dialog::onDesktopActivated(SPDesktop *desktop)
161     _behavior->onDesktopActivated(desktop);
164 void 
165 Dialog::onShutdown()
167     save_geometry();
168     _user_hidden = true;
169     _behavior->onShutdown();
172 void
173 Dialog::onHideF12()
175     _hiddenF12 = true;
176     _behavior->onHideF12();
179 void
180 Dialog::onShowF12()
182     if (_user_hidden)
183         return;
185     if (_hiddenF12) {
186         _behavior->onShowF12();
187     }
189     _hiddenF12 = false;
193 inline Dialog::operator Gtk::Widget&()                           { return *_behavior; }
194 inline GtkWidget *Dialog::gobj()                                 { return _behavior->gobj(); }
195 inline void Dialog::present()                                    { _behavior->present(); }
196 inline Gtk::VBox *Dialog::get_vbox()                             {  return _behavior->get_vbox(); }
197 inline void Dialog::hide()                                       { _behavior->hide(); }
198 inline void Dialog::show()                                       { _behavior->show(); }
199 inline void Dialog::show_all_children()                          { _behavior->show_all_children(); }
200 inline void Dialog::set_size_request(int width, int height)      { _behavior->set_size_request(width, height); }
201 inline void Dialog::size_request(Gtk::Requisition& requisition)  { _behavior->size_request(requisition); }
202 inline void Dialog::get_position(int& x, int& y)                 { _behavior->get_position(x, y); }
203 inline void Dialog::get_size(int& width, int& height)            { _behavior->get_size(width, height); }
204 inline void Dialog::resize(int width, int height)                { _behavior->resize(width, height); }
205 inline void Dialog::move(int x, int y)                           { _behavior->move(x, y); }
206 inline void Dialog::set_position(Gtk::WindowPosition position)   { _behavior->set_position(position); }
207 inline void Dialog::set_title(Glib::ustring title)               { _behavior->set_title(title); }
208 inline void Dialog::set_sensitive(bool sensitive)                { _behavior->set_sensitive(sensitive); }
210 inline void Dialog::set_response_sensitive(int response_id, bool setting)
211 { _behavior->set_response_sensitive(response_id, setting); }
213 void Dialog::set_resizable(bool) { }
214 void Dialog::set_default(Gtk::Widget&) { }
216 inline void Dialog::set_default_response(int response_id) { _behavior->set_default_response(response_id); }
218 Glib::SignalProxy0<void> Dialog::signal_show () { return _behavior->signal_show(); }
219 Glib::SignalProxy0<void> Dialog::signal_hide () { return _behavior->signal_hide(); }
220 Glib::SignalProxy1<void, int> Dialog::signal_response () { return _behavior->signal_response(); }
222 Gtk::Button* Dialog::add_button (const Glib::ustring& button_text, int response_id) 
223 { return _behavior->add_button(button_text, response_id); }
225 Gtk::Button* Dialog::add_button (const Gtk::StockID& stock_id, int response_id)
226 { return _behavior->add_button(stock_id, response_id); }
229 void
230 Dialog::read_geometry()
232     _user_hidden = false;
234     int x = prefs_get_int_attribute (_prefs_path, "x", -1000);
235     int y = prefs_get_int_attribute (_prefs_path, "y", -1000);
236     int w = prefs_get_int_attribute (_prefs_path, "w", 0);
237     int h = prefs_get_int_attribute (_prefs_path, "h", 0);
239     // g_print ("read %d %d %d %d\n", x, y, w, h);
241     // If there are stored height and width values for the dialog,
242     // resize the window to match; otherwise we leave it at its default
243     if (w != 0 && h != 0) {
244         resize(w, h);
245     }
246     
247     // If there are stored values for where the dialog should be
248     // located, then restore the dialog to that position.
249     // also check if (x,y) is actually onscreen with the current screen dimensions
250     if ( (x >= 0) && (y >= 0) && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)) ) {
251         move(x, y);
252     } else {
253         // ...otherwise just put it in the middle of the screen
254         set_position(Gtk::WIN_POS_CENTER);
255     }
260 void
261 Dialog::save_geometry()
263     int y, x, w, h;
265     get_position(x, y);
266     get_size(w, h);
268     // g_print ("write %d %d %d %d\n", x, y, w, h);
270     if (x<0) x=0;
271     if (y<0) y=0;
273     prefs_set_int_attribute (_prefs_path, "x", x);
274     prefs_set_int_attribute (_prefs_path, "y", y);
275     prefs_set_int_attribute (_prefs_path, "w", w);
276     prefs_set_int_attribute (_prefs_path, "h", h);
280 void
281 Dialog::_onResponse(int response_id)
283     switch (response_id) {
284         case Gtk::RESPONSE_APPLY: {
285             _apply();
286             break;
287         }
288         case Gtk::RESPONSE_CLOSE: {
289             _close();
290             break;
291         }
292     }
295 bool
296 Dialog::_onDeleteEvent(GdkEventAny *event)
298     save_geometry();
299     _user_hidden = true;
301     return false;
304 bool
305 Dialog::_onEvent(GdkEvent *event)
307     bool ret = false;
309     switch (event->type) {
310         case GDK_KEY_PRESS: {
311             switch (get_group0_keyval (&event->key)) {
312                 case GDK_Escape: {
313                     _defocus();
314                     ret = true;
315                     break;
316                 }
317                 case GDK_F4:
318                 case GDK_w:
319                 case GDK_W: {
320                     if (mod_ctrl_only(event->key.state)) {
321                         _close();
322                         ret = true;
323                     }
324                     break;
325                 }
326                 default: { // pass keypress to the canvas
327                     break;
328                 }
329             }
330         }
331         default:
332             ;
333     }
335     return ret;
338 bool
339 Dialog::_onKeyPress(GdkEventKey *event)
341     unsigned int shortcut;
342     shortcut = get_group0_keyval(event) |
343         ( event->state & GDK_SHIFT_MASK ?
344           SP_SHORTCUT_SHIFT_MASK : 0 ) |
345         ( event->state & GDK_CONTROL_MASK ?
346           SP_SHORTCUT_CONTROL_MASK : 0 ) |
347         ( event->state & GDK_MOD1_MASK ?
348           SP_SHORTCUT_ALT_MASK : 0 );
349     return sp_shortcut_invoke(shortcut, SP_ACTIVE_DESKTOP);
352 void
353 Dialog::_apply()
355     g_warning("Apply button clicked for dialog [Dialog::_apply()]");
358 void
359 Dialog::_close()
361     GtkWidget *dlg = GTK_WIDGET(_behavior->gobj());
363     /* this code sends a delete_event to the dialog,
364      * instead of just destroying it, so that the
365      * dialog can do some housekeeping, such as remember
366      * its position.
367      */
369     GdkEventAny event;
370     event.type = GDK_DELETE;
371     event.window = dlg->window;
372     event.send_event = TRUE;
374     if (event.window) 
375         g_object_ref(G_OBJECT(event.window));
377     gtk_main_do_event ((GdkEvent*)&event);
379     if (event.window) 
380         g_object_unref(G_OBJECT(event.window));
383 void
384 Dialog::_defocus()
386     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
388     if (desktop) {
389         Gtk::Widget *canvas = Glib::wrap(GTK_WIDGET(desktop->canvas));
391         // make sure the canvas window is present before giving it focus
392         Gtk::Window *toplevel_window = dynamic_cast<Gtk::Window *>(canvas->get_toplevel());
393         if (toplevel_window)
394             toplevel_window->present();
396         canvas->grab_focus();
397     }
400 Inkscape::Selection*
401 Dialog::_getSelection()
403     return sp_desktop_selection(SP_ACTIVE_DESKTOP);
406 } // namespace Dialog
407 } // namespace UI
408 } // namespace Inkscape
410 /*
411   Local Variables:
412   mode:c++
413   c-file-style:"stroustrup"
414   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
415   indent-tabs-mode:nil
416   fill-column:99
417   End:
418 */
419 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :