64109de3fa2f6c24d04048805b54646a487b8f3f
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 * Author:
6 * Bryce W. Harrington <bryce@bryceharrington.org>
7 * buliabyak@gmail.com
8 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
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>
22 #include "application/application.h"
23 #include "application/editor.h"
24 #include "inkscape.h"
25 #include "event-context.h"
26 #include "desktop.h"
27 #include "desktop-handles.h"
28 #include "dialog-manager.h"
29 #include "dialogs/dialog-events.h"
30 #include "shortcuts.h"
31 #include "prefs-utils.h"
32 #include "interface.h"
33 #include "verbs.h"
36 #define MIN_ONSCREEN_DISTANCE 50
40 namespace Inkscape {
41 namespace UI {
42 namespace Dialog {
44 static gboolean
45 sp_retransientize_again (gpointer dlgPtr)
46 {
47 Dialog *dlg = (Dialog *)dlgPtr;
48 dlg->retransientize_suppress = false;
49 return FALSE; // so that it is only called once
50 }
52 static void
53 sp_retransientize (Inkscape::Application *inkscape, SPDesktop *desktop, gpointer dlgPtr)
54 {
55 Dialog *dlg = (Dialog *)dlgPtr;
56 dlg->onDesktopActivated (desktop);
57 }
59 static void
60 sp_dialog_shutdown (GtkObject *object, gpointer dlgPtr)
61 {
62 Dialog *dlg = (Dialog *)dlgPtr;
63 dlg->onShutdown();
64 }
66 void
67 Dialog::present()
68 {
69 Gtk::Dialog::present();
70 }
72 void
73 Dialog::save_geometry()
74 {
75 int y, x, w, h;
77 get_position(x, y);
78 get_size(w, h);
80 // g_print ("write %d %d %d %d\n", x, y, w, h);
82 if (x<0) x=0;
83 if (y<0) y=0;
85 prefs_set_int_attribute (_prefs_path, "x", x);
86 prefs_set_int_attribute (_prefs_path, "y", y);
87 prefs_set_int_attribute (_prefs_path, "w", w);
88 prefs_set_int_attribute (_prefs_path, "h", h);
89 }
91 void
92 Dialog::read_geometry()
93 {
94 _user_hidden = false;
96 int x = prefs_get_int_attribute (_prefs_path, "x", -1000);
97 int y = prefs_get_int_attribute (_prefs_path, "y", -1000);
98 int w = prefs_get_int_attribute (_prefs_path, "w", 0);
99 int h = prefs_get_int_attribute (_prefs_path, "h", 0);
101 // g_print ("read %d %d %d %d\n", x, y, w, h);
103 // If there are stored height and width values for the dialog,
104 // resize the window to match; otherwise we leave it at its default
105 if (w != 0 && h != 0) {
106 resize (w, h);
107 }
109 // If there are stored values for where the dialog should be
110 // located, then restore the dialog to that position.
111 // also check if (x,y) is actually onscreen with the current screen dimensions
112 if ( (x >= 0) && (y >= 0) && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)) ) {
113 move(x, y);
114 } else {
115 // ...otherwise just put it in the middle of the screen
116 set_position(Gtk::WIN_POS_CENTER);
117 }
118 }
120 void hideCallback(GtkObject *object, gpointer dlgPtr)
121 {
122 g_return_if_fail( dlgPtr != NULL );
124 Dialog *dlg = (Dialog *)dlgPtr;
125 dlg->onHideF12();
126 }
128 void unhideCallback(GtkObject *object, gpointer dlgPtr)
129 {
130 g_return_if_fail( dlgPtr != NULL );
132 Dialog *dlg = (Dialog *)dlgPtr;
133 dlg->onShowF12();
134 }
136 //=====================================================================
138 /**
139 * UI::Dialog::Dialog is a base class for all dialogs in Inkscape. The
140 * purpose of this class is to provide a unified place for ensuring
141 * style and behavior. Specifically, this class provides functionality
142 * for saving and restoring the size and position of dialogs (through
143 * the user's preferences file).
144 *
145 * It also provides some general purpose signal handlers for things like
146 * showing and hiding all dialogs.
147 */
148 Dialog::Dialog(const char *prefs_path, int verb_num, const char *apply_label)
149 {
150 hide();
151 set_has_separator(false);
153 _prefs_path = prefs_path;
155 if (prefs_get_int_attribute ("dialogs", "showclose", 0) || apply_label) {
156 // TODO: make the order of buttons obey the global preference
157 if (apply_label) {
158 add_button(Glib::ustring(apply_label), Gtk::RESPONSE_APPLY);
159 set_default_response(Gtk::RESPONSE_APPLY);
160 }
161 add_button(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
162 }
164 GtkWidget *dlg = GTK_WIDGET(gobj());
166 if (verb_num)
167 {
168 gchar title[500];
169 sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
170 set_title(title);
171 }
173 gtk_signal_connect( GTK_OBJECT (dlg), "event", GTK_SIGNAL_FUNC(sp_dialog_event_handler), dlg );
175 _hiddenF12 = false;
176 if (Inkscape::NSApplication::Application::getNewGui())
177 {
178 _desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*this, &Dialog::onDesktopActivated));
179 _dialogs_hidden_connection = Inkscape::NSApplication::Editor::connectDialogsHidden (sigc::mem_fun (*this, &Dialog::onHideF12));
180 _dialogs_unhidden_connection = Inkscape::NSApplication::Editor::connectDialogsUnhidden (sigc::mem_fun (*this, &Dialog::onShowF12));
181 _shutdown_connection = Inkscape::NSApplication::Editor::connectShutdown (sigc::mem_fun (*this, &Dialog::onShutdown));
182 }
183 else
184 {
185 g_signal_connect (G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_retransientize), (void *)this);
186 g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this );
187 g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this );
188 g_signal_connect (G_OBJECT (INKSCAPE), "shut_down", G_CALLBACK (sp_dialog_shutdown), (void *)this);
189 }
191 g_signal_connect_after( gobj(), "key_press_event", (GCallback)windowKeyPress, NULL );
193 read_geometry();
194 present();
195 sp_transientize(dlg);
196 retransientize_suppress = false;
197 }
199 Dialog::Dialog(BaseObjectType *gobj)
200 : Gtk::Dialog(gobj)
201 {
202 }
204 Dialog::~Dialog()
205 {
206 if (Inkscape::NSApplication::Application::getNewGui())
207 {
208 _desktop_activated_connection.disconnect();
209 _dialogs_hidden_connection.disconnect();
210 _dialogs_unhidden_connection.disconnect();
211 _shutdown_connection.disconnect();
212 }
214 save_geometry();
215 }
218 bool Dialog::windowKeyPress( GtkWidget *widget, GdkEventKey *event )
219 {
220 unsigned int shortcut = 0;
221 shortcut = get_group0_keyval (event) |
222 ( event->state & GDK_SHIFT_MASK ?
223 SP_SHORTCUT_SHIFT_MASK : 0 ) |
224 ( event->state & GDK_CONTROL_MASK ?
225 SP_SHORTCUT_CONTROL_MASK : 0 ) |
226 ( event->state & GDK_MOD1_MASK ?
227 SP_SHORTCUT_ALT_MASK : 0 );
228 return sp_shortcut_invoke( shortcut, SP_ACTIVE_DESKTOP );
229 }
231 //---------------------------------------------------------------------
233 void
234 Dialog::on_response(int response_id)
235 {
236 switch (response_id) {
237 case Gtk::RESPONSE_APPLY: {
238 _apply();
239 break;
240 }
241 case Gtk::RESPONSE_CLOSE: {
242 _close();
243 break;
244 }
245 }
246 }
248 bool
249 Dialog::on_delete_event (GdkEventAny *event)
250 {
251 save_geometry();
252 _user_hidden = true;
254 return false;
255 }
257 void
258 Dialog::onHideF12()
259 {
260 _hiddenF12 = true;
261 save_geometry();
262 hide();
263 }
265 void
266 Dialog::onShowF12()
267 {
268 if (_user_hidden)
269 return;
271 if (_hiddenF12) {
272 show();
273 read_geometry();
274 }
276 _hiddenF12 = false;
277 }
279 void
280 Dialog::onShutdown()
281 {
282 save_geometry();
283 _user_hidden = true;
284 }
286 void
287 Dialog::onDesktopActivated (SPDesktop *desktop)
288 {
289 gint transient_policy = prefs_get_int_attribute_limited ( "options.transientpolicy", "value", 1, 0, 2);
291 #ifdef WIN32 // FIXME: Temporary Win32 special code to enable transient dialogs
292 if (prefs_get_int_attribute ( "options.dialogsontopwin32", "value", 0))
293 transient_policy = 2;
294 else
295 return;
296 #endif
298 if (!transient_policy)
299 return;
303 GtkWindow *dialog_win = GTK_WINDOW(gobj());
305 if (retransientize_suppress) {
306 /* if retransientizing of this dialog is still forbidden after
307 * previous call warning turned off because it was confusingly fired
308 * when loading many files from command line
309 */
311 // g_warning("Retranzientize aborted! You're switching windows too fast!");
312 return;
313 }
315 if (dialog_win)
316 {
317 retransientize_suppress = true; // disallow other attempts to retranzientize this dialog
319 desktop->setWindowTransient (dialog_win);
321 /*
322 * This enables "aggressive" transientization,
323 * i.e. dialogs always emerging on top when you switch documents. Note
324 * however that this breaks "click to raise" policy of a window
325 * manager because the switched-to document will be raised at once
326 * (so that its transients also could raise)
327 */
328 if (transient_policy == 2 && !_hiddenF12 && !_user_hidden) {
329 // without this, a transient window not always emerges on top
330 gtk_window_present (dialog_win);
331 }
332 }
334 // we're done, allow next retransientizing not sooner than after 120 msec
335 gtk_timeout_add (120, (GtkFunction) sp_retransientize_again, (gpointer) this);
336 }
339 Inkscape::Selection*
340 Dialog::_getSelection()
341 {
342 return sp_desktop_selection(SP_ACTIVE_DESKTOP);
343 }
345 void
346 Dialog::_apply()
347 {
348 g_warning("Apply button clicked for dialog [Dialog::_apply()]");
349 }
351 void
352 Dialog::_close()
353 {
354 GtkWidget *dlg = GTK_WIDGET(gobj());
356 /* this code sends a delete_event to the dialog,
357 * instead of just destroying it, so that the
358 * dialog can do some housekeeping, such as remember
359 * its position.
360 */
361 GdkEventAny event;
362 event.type = GDK_DELETE;
363 event.window = dlg->window;
364 event.send_event = TRUE;
365 g_object_ref (G_OBJECT (event.window));
366 gtk_main_do_event ((GdkEvent*)&event);
367 g_object_unref (G_OBJECT (event.window));
368 }
370 } // namespace Dialog
371 } // namespace UI
372 } // namespace Inkscape
374 /*
375 Local Variables:
376 mode:c++
377 c-file-style:"stroustrup"
378 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
379 indent-tabs-mode:nil
380 fill-column:99
381 End:
382 */
383 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :