8fb04a91f8ab317f2548d3b956922b658a0d9b31
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 *
9 * Copyright (C) 2004, 2005 Authors
10 *
11 * Released under GNU GPL. Read the file 'COPYING' for more information.
12 */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #include <gtkmm/stock.h>
19 #include <gtk/gtk.h>
21 #include "application/application.h"
22 #include "application/editor.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 "dialogs/dialog-events.h"
29 #include "shortcuts.h"
30 #include "prefs-utils.h"
31 #include "interface.h"
32 #include "verbs.h"
35 #define MIN_ONSCREEN_DISTANCE 50
39 namespace Inkscape {
40 namespace UI {
41 namespace Dialog {
43 #ifndef WIN32
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 }
51 #endif
53 static void
54 sp_retransientize (Inkscape::Application *inkscape, SPDesktop *desktop, gpointer dlgPtr)
55 {
56 Dialog *dlg = (Dialog *)dlgPtr;
57 dlg->onDesktopActivated (desktop);
58 }
60 static void
61 sp_dialog_shutdown (GtkObject *object, gpointer dlgPtr)
62 {
63 Dialog *dlg = (Dialog *)dlgPtr;
64 dlg->onShutdown();
65 }
67 void
68 Dialog::present()
69 {
70 Gtk::Dialog::present();
71 }
73 void
74 Dialog::save_geometry()
75 {
76 int y, x, w, h;
78 get_position(x, y);
79 get_size(w, h);
81 // g_print ("write %d %d %d %d\n", x, y, w, h);
83 if (x<0) x=0;
84 if (y<0) y=0;
86 prefs_set_int_attribute (_prefs_path, "x", x);
87 prefs_set_int_attribute (_prefs_path, "y", y);
88 prefs_set_int_attribute (_prefs_path, "w", w);
89 prefs_set_int_attribute (_prefs_path, "h", h);
90 }
92 void
93 Dialog::read_geometry()
94 {
95 _user_hidden = false;
97 int x = prefs_get_int_attribute (_prefs_path, "x", -1000);
98 int y = prefs_get_int_attribute (_prefs_path, "y", -1000);
99 int w = prefs_get_int_attribute (_prefs_path, "w", 0);
100 int h = prefs_get_int_attribute (_prefs_path, "h", 0);
102 // g_print ("read %d %d %d %d\n", x, y, w, h);
104 // If there are stored height and width values for the dialog,
105 // resize the window to match; otherwise we leave it at its default
106 if (w != 0 && h != 0) {
107 resize (w, h);
108 }
110 // If there are stored values for where the dialog should be
111 // located, then restore the dialog to that position.
112 // also check if (x,y) is actually onscreen with the current screen dimensions
113 if ( (x >= 0) && (y >= 0) && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)) ) {
114 move(x, y);
115 } else {
116 // ...otherwise just put it in the middle of the screen
117 set_position(Gtk::WIN_POS_CENTER);
118 }
119 }
121 void hideCallback(GtkObject *object, gpointer dlgPtr)
122 {
123 g_return_if_fail( dlgPtr != NULL );
125 Dialog *dlg = (Dialog *)dlgPtr;
126 dlg->onHideF12();
127 }
129 void unhideCallback(GtkObject *object, gpointer dlgPtr)
130 {
131 g_return_if_fail( dlgPtr != NULL );
133 Dialog *dlg = (Dialog *)dlgPtr;
134 dlg->onShowF12();
135 }
137 //=====================================================================
139 /**
140 * UI::Dialog::Dialog is a base class for all dialogs in Inkscape. The
141 * purpose of this class is to provide a unified place for ensuring
142 * style and behavior. Specifically, this class provides functionality
143 * for saving and restoring the size and position of dialogs (through
144 * the user's preferences file).
145 *
146 * It also provides some general purpose signal handlers for things like
147 * showing and hiding all dialogs.
148 */
149 Dialog::Dialog(const char *prefs_path, int verb_num, const char *apply_label)
150 {
151 hide();
152 set_has_separator(false);
154 _prefs_path = prefs_path;
156 if (prefs_get_int_attribute ("dialogs", "showclose", 0) || apply_label) {
157 // TODO: make the order of buttons obey the global preference
158 if (apply_label) {
159 add_button(Glib::ustring(apply_label), Gtk::RESPONSE_APPLY);
160 set_default_response(Gtk::RESPONSE_APPLY);
161 }
162 add_button(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
163 }
165 GtkWidget *dlg = GTK_WIDGET(gobj());
167 if (verb_num)
168 {
169 gchar title[500];
170 sp_ui_dialog_title_string (Inkscape::Verb::get(verb_num), title);
171 set_title(title);
172 }
174 sp_transientize(dlg);
175 retransientize_suppress = false;
177 gtk_signal_connect( GTK_OBJECT (dlg), "event", GTK_SIGNAL_FUNC(sp_dialog_event_handler), dlg );
179 _hiddenF12 = false;
180 if (Inkscape::NSApplication::Application::getNewGui())
181 {
182 _desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*this, &Dialog::onDesktopActivated));
183 _dialogs_hidden_connection = Inkscape::NSApplication::Editor::connectDialogsHidden (sigc::mem_fun (*this, &Dialog::onHideF12));
184 _dialogs_unhidden_connection = Inkscape::NSApplication::Editor::connectDialogsUnhidden (sigc::mem_fun (*this, &Dialog::onShowF12));
185 _shutdown_connection = Inkscape::NSApplication::Editor::connectShutdown (sigc::mem_fun (*this, &Dialog::onShutdown));
186 }
187 else
188 {
189 g_signal_connect (G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_retransientize), (void *)this);
190 g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_hide", G_CALLBACK(hideCallback), (void *)this );
191 g_signal_connect( G_OBJECT(INKSCAPE), "dialogs_unhide", G_CALLBACK(unhideCallback), (void *)this );
192 g_signal_connect (G_OBJECT (INKSCAPE), "shut_down", G_CALLBACK (sp_dialog_shutdown), (void *)this);
193 }
195 g_signal_connect_after( gobj(), "key_press_event", (GCallback)windowKeyPress, NULL );
197 read_geometry();
198 present();
199 }
201 Dialog::Dialog(BaseObjectType *gobj)
202 : Gtk::Dialog(gobj)
203 {
204 }
206 Dialog::~Dialog()
207 {
208 if (Inkscape::NSApplication::Application::getNewGui())
209 {
210 _desktop_activated_connection.disconnect();
211 _dialogs_hidden_connection.disconnect();
212 _dialogs_unhidden_connection.disconnect();
213 _shutdown_connection.disconnect();
214 }
216 save_geometry();
217 }
220 bool Dialog::windowKeyPress( GtkWidget *widget, GdkEventKey *event )
221 {
222 unsigned int shortcut = 0;
223 shortcut = get_group0_keyval (event) |
224 ( event->state & GDK_SHIFT_MASK ?
225 SP_SHORTCUT_SHIFT_MASK : 0 ) |
226 ( event->state & GDK_CONTROL_MASK ?
227 SP_SHORTCUT_CONTROL_MASK : 0 ) |
228 ( event->state & GDK_MOD1_MASK ?
229 SP_SHORTCUT_ALT_MASK : 0 );
230 return sp_shortcut_invoke( shortcut, SP_ACTIVE_DESKTOP );
231 }
233 //---------------------------------------------------------------------
235 void
236 Dialog::on_response(int response_id)
237 {
238 switch (response_id) {
239 case Gtk::RESPONSE_APPLY: {
240 _apply();
241 break;
242 }
243 case Gtk::RESPONSE_CLOSE: {
244 _close();
245 break;
246 }
247 }
248 }
250 bool
251 Dialog::on_delete_event (GdkEventAny *event)
252 {
253 save_geometry();
254 _user_hidden = true;
256 return false;
257 }
259 void
260 Dialog::onHideF12()
261 {
262 _hiddenF12 = true;
263 save_geometry();
264 hide();
265 }
267 void
268 Dialog::onShowF12()
269 {
270 if (_user_hidden)
271 return;
273 if (_hiddenF12) {
274 show();
275 read_geometry();
276 }
278 _hiddenF12 = false;
279 }
281 void
282 Dialog::onShutdown()
283 {
284 save_geometry();
285 _user_hidden = true;
286 }
288 void
289 Dialog::onDesktopActivated (SPDesktop *desktop)
290 {
291 gint transient_policy = prefs_get_int_attribute_limited ( "options.transientpolicy", "value", 1, 0, 2);
293 if (!transient_policy)
294 return;
296 #ifndef WIN32
297 GtkWindow *dialog_win = GTK_WINDOW(gobj());
299 if (retransientize_suppress) {
300 /* if retransientizing of this dialog is still forbidden after
301 * previous call warning turned off because it was confusingly fired
302 * when loading many files from command line
303 */
305 // g_warning("Retranzientize aborted! You're switching windows too fast!");
306 return;
307 }
309 if (dialog_win)
310 {
311 retransientize_suppress = true; // disallow other attempts to retranzientize this dialog
313 desktop->setWindowTransient (dialog_win);
315 /*
316 * This enables "aggressive" transientization,
317 * i.e. dialogs always emerging on top when you switch documents. Note
318 * however that this breaks "click to raise" policy of a window
319 * manager because the switched-to document will be raised at once
320 * (so that its transients also could raise)
321 */
322 if (transient_policy == 2 && !_hiddenF12 && !_user_hidden) {
323 // without this, a transient window not always emerges on top
324 gtk_window_present (dialog_win);
325 }
326 }
328 // we're done, allow next retransientizing not sooner than after 120 msec
329 gtk_timeout_add (120, (GtkFunction) sp_retransientize_again, (gpointer) this);
330 #endif
331 }
334 Inkscape::Selection*
335 Dialog::_getSelection()
336 {
337 return sp_desktop_selection(SP_ACTIVE_DESKTOP);
338 }
340 void
341 Dialog::_apply()
342 {
343 g_warning("Apply button clicked for dialog [Dialog::_apply()]");
344 }
346 void
347 Dialog::_close()
348 {
349 GtkWidget *dlg = GTK_WIDGET(gobj());
351 /* this code sends a delete_event to the dialog,
352 * instead of just destroying it, so that the
353 * dialog can do some housekeeping, such as remember
354 * its position.
355 */
356 GdkEventAny event;
357 event.type = GDK_DELETE;
358 event.window = dlg->window;
359 event.send_event = TRUE;
360 g_object_ref (G_OBJECT (event.window));
361 gtk_main_do_event ((GdkEvent*)&event);
362 g_object_unref (G_OBJECT (event.window));
363 }
365 } // namespace Dialog
366 } // namespace UI
367 } // namespace Inkscape
369 /*
370 Local Variables:
371 mode:c++
372 c-file-style:"stroustrup"
373 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
374 indent-tabs-mode:nil
375 fill-column:99
376 End:
377 */
378 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :