43863625f9ba50525c55d31861d54b4a6d1a1f28
1 /** @file
2 * @brief Base class for dialogs in Inkscape - implementation
3 */
4 /* Authors:
5 * Bryce W. Harrington <bryce@bryceharrington.org>
6 * buliabyak@gmail.com
7 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
8 * Gustav Broberg <broberg@kth.se>
9 *
10 * Copyright (C) 2004--2007 Authors
11 *
12 * Released under GNU GPL. Read the file 'COPYING' for more information.
13 */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #include <gtkmm/stock.h>
20 #include <gtk/gtk.h>
21 #include <gdk/gdkkeysyms.h>
23 #include "inkscape.h"
24 #include "event-context.h"
25 #include "desktop.h"
26 #include "desktop-handles.h"
27 #include "dialog-manager.h"
28 #include "modifier-fns.h"
29 #include "shortcuts.h"
30 #include "preferences.h"
31 #include "interface.h"
32 #include "verbs.h"
34 #define MIN_ONSCREEN_DISTANCE 50
37 namespace Inkscape {
38 namespace UI {
39 namespace Dialog {
41 void
42 sp_retransientize(Inkscape::Application */*inkscape*/, SPDesktop *desktop, gpointer dlgPtr)
43 {
44 Dialog *dlg = (Dialog *)dlgPtr;
45 dlg->onDesktopActivated (desktop);
46 }
48 gboolean
49 sp_retransientize_again(gpointer dlgPtr)
50 {
51 Dialog *dlg = (Dialog *)dlgPtr;
52 dlg->retransientize_suppress = false;
53 return FALSE; // so that it is only called once
54 }
56 void
57 sp_dialog_shutdown(GtkObject */*object*/, gpointer dlgPtr)
58 {
59 Dialog *dlg = (Dialog *)dlgPtr;
60 dlg->onShutdown();
61 }
64 void hideCallback(GtkObject */*object*/, gpointer dlgPtr)
65 {
66 g_return_if_fail( dlgPtr != NULL );
68 Dialog *dlg = (Dialog *)dlgPtr;
69 dlg->onHideF12();
70 }
72 void unhideCallback(GtkObject */*object*/, gpointer dlgPtr)
73 {
74 g_return_if_fail( dlgPtr != NULL );
76 Dialog *dlg = (Dialog *)dlgPtr;
77 dlg->onShowF12();
78 }
81 //=====================================================================
83 /**
84 * UI::Dialog::Dialog is a base class for all dialogs in Inkscape. The
85 * purpose of this class is to provide a unified place for ensuring
86 * style and behavior. Specifically, this class provides functionality
87 * for saving and restoring the size and position of dialogs (through
88 * the user's preferences file).
89 *
90 * It also provides some general purpose signal handlers for things like
91 * showing and hiding all dialogs.
92 */
94 Dialog::Dialog(Behavior::BehaviorFactory behavior_factory, const char *prefs_path, int verb_num,
95 Glib::ustring const &apply_label)
96 : _hiddenF12 (false),
97 _prefs_path (prefs_path),
98 _verb_num(verb_num),
99 _apply_label (apply_label)
100 {
101 gchar title[500];
103 if (verb_num)
104 sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
106 _title = title;
108 _behavior = behavior_factory(*this);
110 g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop", G_CALLBACK(sp_retransientize), (void *)this);
111 g_signal_connect(G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this);
112 g_signal_connect(G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this);
113 g_signal_connect(G_OBJECT(INKSCAPE), "shut_down", G_CALLBACK(sp_dialog_shutdown), (void *)this);
115 Glib::wrap(gobj())->signal_event().connect(sigc::mem_fun(*this, &Dialog::_onEvent));
116 Glib::wrap(gobj())->signal_key_press_event().connect(sigc::mem_fun(*this, &Dialog::_onKeyPress));
118 read_geometry();
119 }
121 Dialog::~Dialog()
122 {
123 save_geometry();
124 delete _behavior;
125 _behavior = 0;
126 }
129 //---------------------------------------------------------------------
132 void
133 Dialog::onDesktopActivated(SPDesktop *desktop)
134 {
135 _behavior->onDesktopActivated(desktop);
136 }
138 void
139 Dialog::onShutdown()
140 {
141 save_geometry();
142 _user_hidden = true;
143 _behavior->onShutdown();
144 }
146 void
147 Dialog::onHideF12()
148 {
149 _hiddenF12 = true;
150 _behavior->onHideF12();
151 }
153 void
154 Dialog::onShowF12()
155 {
156 if (_user_hidden)
157 return;
159 if (_hiddenF12) {
160 _behavior->onShowF12();
161 }
163 _hiddenF12 = false;
164 }
167 inline Dialog::operator Gtk::Widget &() { return *_behavior; }
168 inline GtkWidget *Dialog::gobj() { return _behavior->gobj(); }
169 inline void Dialog::present() { _behavior->present(); }
170 inline Gtk::VBox *Dialog::get_vbox() { return _behavior->get_vbox(); }
171 inline void Dialog::hide() { _behavior->hide(); }
172 inline void Dialog::show() { _behavior->show(); }
173 inline void Dialog::show_all_children() { _behavior->show_all_children(); }
174 inline void Dialog::set_size_request(int width, int height) { _behavior->set_size_request(width, height); }
175 inline void Dialog::size_request(Gtk::Requisition &requisition) { _behavior->size_request(requisition); }
176 inline void Dialog::get_position(int &x, int &y) { _behavior->get_position(x, y); }
177 inline void Dialog::get_size(int &width, int &height) { _behavior->get_size(width, height); }
178 inline void Dialog::resize(int width, int height) { _behavior->resize(width, height); }
179 inline void Dialog::move(int x, int y) { _behavior->move(x, y); }
180 inline void Dialog::set_position(Gtk::WindowPosition position) { _behavior->set_position(position); }
181 inline void Dialog::set_title(Glib::ustring title) { _behavior->set_title(title); }
182 inline void Dialog::set_sensitive(bool sensitive) { _behavior->set_sensitive(sensitive); }
184 Glib::SignalProxy0<void> Dialog::signal_show() { return _behavior->signal_show(); }
185 Glib::SignalProxy0<void> Dialog::signal_hide() { return _behavior->signal_hide(); }
187 void
188 Dialog::read_geometry()
189 {
190 _user_hidden = false;
192 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
193 int x = prefs->getInt(_prefs_path + "/x", -1000);
194 int y = prefs->getInt(_prefs_path + "/y", -1000);
195 int w = prefs->getInt(_prefs_path + "/w", 0);
196 int h = prefs->getInt(_prefs_path + "/h", 0);
198 // g_print ("read %d %d %d %d\n", x, y, w, h);
200 // If there are stored height and width values for the dialog,
201 // resize the window to match; otherwise we leave it at its default
202 if (w != 0 && h != 0) {
203 resize(w, h);
204 }
206 // If there are stored values for where the dialog should be
207 // located, then restore the dialog to that position.
208 // also check if (x,y) is actually onscreen with the current screen dimensions
209 if ( (x >= 0) && (y >= 0) && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)) ) {
210 move(x, y);
211 } else {
212 // ...otherwise just put it in the middle of the screen
213 set_position(Gtk::WIN_POS_CENTER);
214 }
216 }
219 void
220 Dialog::save_geometry()
221 {
222 int y, x, w, h;
224 get_position(x, y);
225 get_size(w, h);
227 // g_print ("write %d %d %d %d\n", x, y, w, h);
229 if (x<0) x=0;
230 if (y<0) y=0;
232 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
233 prefs->setInt(_prefs_path + "/x", x);
234 prefs->setInt(_prefs_path + "/y", y);
235 prefs->setInt(_prefs_path + "/w", w);
236 prefs->setInt(_prefs_path + "/h", h);
238 }
240 void
241 Dialog::_handleResponse(int response_id)
242 {
243 switch (response_id) {
244 case Gtk::RESPONSE_CLOSE: {
245 _close();
246 break;
247 }
248 }
249 }
251 bool
252 Dialog::_onDeleteEvent(GdkEventAny */*event*/)
253 {
254 save_geometry();
255 _user_hidden = true;
257 return false;
258 }
260 bool
261 Dialog::_onEvent(GdkEvent *event)
262 {
263 bool ret = false;
265 switch (event->type) {
266 case GDK_KEY_PRESS: {
267 switch (get_group0_keyval (&event->key)) {
268 case GDK_Escape: {
269 _defocus();
270 ret = true;
271 break;
272 }
273 case GDK_F4:
274 case GDK_w:
275 case GDK_W: {
276 if (mod_ctrl_only(event->key.state)) {
277 _close();
278 ret = true;
279 }
280 break;
281 }
282 default: { // pass keypress to the canvas
283 break;
284 }
285 }
286 }
287 default:
288 ;
289 }
291 return ret;
292 }
294 bool
295 Dialog::_onKeyPress(GdkEventKey *event)
296 {
297 unsigned int shortcut;
298 shortcut = get_group0_keyval(event) |
299 ( event->state & GDK_SHIFT_MASK ?
300 SP_SHORTCUT_SHIFT_MASK : 0 ) |
301 ( event->state & GDK_CONTROL_MASK ?
302 SP_SHORTCUT_CONTROL_MASK : 0 ) |
303 ( event->state & GDK_MOD1_MASK ?
304 SP_SHORTCUT_ALT_MASK : 0 );
305 return sp_shortcut_invoke(shortcut, SP_ACTIVE_DESKTOP);
306 }
308 void
309 Dialog::_apply()
310 {
311 g_warning("Apply button clicked for dialog [Dialog::_apply()]");
312 }
314 void
315 Dialog::_close()
316 {
317 GtkWidget *dlg = GTK_WIDGET(_behavior->gobj());
319 /* this code sends a delete_event to the dialog,
320 * instead of just destroying it, so that the
321 * dialog can do some housekeeping, such as remember
322 * its position.
323 */
325 GdkEventAny event;
326 event.type = GDK_DELETE;
327 event.window = dlg->window;
328 event.send_event = TRUE;
330 if (event.window)
331 g_object_ref(G_OBJECT(event.window));
333 gtk_main_do_event ((GdkEvent*)&event);
335 if (event.window)
336 g_object_unref(G_OBJECT(event.window));
337 }
339 void
340 Dialog::_defocus()
341 {
342 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
344 if (desktop) {
345 Gtk::Widget *canvas = Glib::wrap(GTK_WIDGET(desktop->canvas));
347 // make sure the canvas window is present before giving it focus
348 Gtk::Window *toplevel_window = dynamic_cast<Gtk::Window *>(canvas->get_toplevel());
349 if (toplevel_window)
350 toplevel_window->present();
352 canvas->grab_focus();
353 }
354 }
356 Inkscape::Selection*
357 Dialog::_getSelection()
358 {
359 return sp_desktop_selection(SP_ACTIVE_DESKTOP);
360 }
362 } // namespace Dialog
363 } // namespace UI
364 } // namespace Inkscape
366 /*
367 Local Variables:
368 mode:c++
369 c-file-style:"stroustrup"
370 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
371 indent-tabs-mode:nil
372 fill-column:99
373 End:
374 */
375 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :