25f8e8af0422712a8df8a0da9bc884b766e8e5ec
1 /** \file
2 *
3 * Document properties dialog, Gtkmm-style
4 *
5 * Authors:
6 * bulia byak <buliabyak@users.sf.net>
7 * Bryce W. Harrington <bryce@bryceharrington.org>
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * Jon Phillips <jon@rejon.org>
10 * Ralf Stephan <ralf@ark.in-berlin.de> (Gtkmm)
11 *
12 * Copyright (C) 2000 - 2005 Authors
13 *
14 * Released under GNU GPL. Read the file 'COPYING' for more information
15 */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
23 #include <gtkmm.h>
24 #include "ui/widget/color-picker.h"
25 #include "ui/widget/scalar-unit.h"
26 #include "ui/widget/button.h"
28 #include "xml/node-event-vector.h"
29 #include "helper/units.h"
31 #include "inkscape.h"
32 #include "verbs.h"
33 #include "document.h"
34 #include "desktop-handles.h"
35 #include "desktop.h"
36 #include "sp-namedview.h"
37 #include "helper/action.h"
39 #include "document-properties.h"
41 using std::pair;
43 namespace Inkscape {
44 namespace UI {
45 namespace Dialog {
47 #define SPACE_SIZE_X 15
48 #define SPACE_SIZE_Y 10
50 //===================================================
52 //---------------------------------------------------
54 static DocumentProperties *_instance = 0;
56 static void on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer);
57 static void on_doc_replaced (SPDesktop* dt, SPDocument* doc);
58 static void on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*);
59 static void on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*);
61 static void fire_fit_canvas_to_selection_or_drawing();
63 static Inkscape::XML::NodeEventVector const _repr_events = {
64 NULL, /* child_added */
65 NULL, /* child_removed */
66 on_repr_attr_changed,
67 NULL, /* content_changed */
68 NULL /* order_changed */
69 };
72 DocumentProperties*
73 DocumentProperties::create()
74 {
75 if (_instance) return _instance;
76 _instance = new DocumentProperties;
77 _instance->init();
78 return _instance;
79 }
81 void
82 DocumentProperties::destroy()
83 {
84 if (_instance)
85 {
86 delete _instance;
87 _instance = 0;
88 }
89 }
91 DocumentProperties::DocumentProperties()
92 : Dialog ("dialogs.documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
93 _page_page(1, 1), _page_grid(1, 1), _page_guides(1, 1),
94 _page_snap(1, 1),
95 _prefs_path("dialogs.documentoptions")
96 {
97 hide();
98 set_resizable (false);
99 _tt.enable();
100 get_vbox()->set_spacing (4);
101 get_vbox()->pack_start (_notebook, true, true);
103 _notebook.append_page(_page_page, _("Page"));
104 _notebook.append_page(_page_grid, _("Grid/Guides"));
105 _notebook.append_page(_page_snap, _("Snap"));
107 build_page();
108 build_grid();
109 build_snap();
110 }
112 void
113 DocumentProperties::init()
114 {
115 update();
117 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
118 repr->addListener (&_repr_events, this);
120 _doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
122 g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
123 G_CALLBACK(on_activate_desktop), 0);
125 g_signal_connect(G_OBJECT(INKSCAPE), "deactivate_desktop",
126 G_CALLBACK(on_deactivate_desktop), 0);
128 show_all_children();
129 present();
130 }
132 DocumentProperties::~DocumentProperties()
133 {
134 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
135 repr->removeListenerByData (this);
136 _doc_replaced_connection.disconnect();
137 }
139 //========================================================================
141 /**
142 * Helper function that attachs widgets in a 3xn table. The widgets come in an
143 * array that has two entries per table row. The two entries code for four
144 * possible cases: (0,0) means insert space in first column; (0, non-0) means
145 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
146 * (non-0, non-0) means two widgets in columns 2 and 3.
147 **/
148 inline void
149 attach_all (Gtk::Table &table, const Gtk::Widget *arr[], unsigned size, int start = 0)
150 {
151 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2)
152 {
153 if (arr[i] && arr[i+1])
154 {
155 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
156 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
157 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
158 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
159 }
160 else
161 {
162 if (arr[i+1])
163 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
164 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
165 else if (arr[i])
166 {
167 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
168 label.set_alignment (0.0);
169 table.attach (label, 0, 3, r, r+1,
170 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
171 }
172 else
173 {
174 Gtk::HBox *space = manage (new Gtk::HBox);
175 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
176 table.attach (*space, 0, 1, r, r+1,
177 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
178 }
179 }
180 ++r;
181 }
182 }
184 void
185 DocumentProperties::build_page()
186 {
187 _page_page.show();
189 _rcp_bg.init (_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"),
190 "pagecolor", "inkscape:pageopacity", _wr);
191 _rcb_canb.init (_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false);
192 _rcb_bord.init (_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false);
193 _rcp_bord.init (_("Border _color:"), _("Page border color"),
194 _("Color of the page border"),
195 "bordercolor", "borderopacity", _wr);
196 _rcb_shad.init (_("_Show border shadow"), _("If set, page border shows a shadow on its right and lower side"), "inkscape:showpageshadow", _wr, false);
197 _rum_deflt.init (_("Default _units:"), "inkscape:document-units", _wr);
199 Inkscape::UI::Widget::Button* fit_canv = manage(new Inkscape::UI::Widget::Button(_("Fit canvas to selection"),
200 _("Resize the canvas to fit the current selection, or the entire drawing if there is no selection")));
201 fit_canv->signal_clicked().connect(sigc::ptr_fun(fire_fit_canvas_to_selection_or_drawing));
203 // prevent fit_canv from expanding
204 Gtk::Alignment *fit_canv_cont = manage(new Gtk::Alignment(1.0,0.5,0.0,0.0));
205 fit_canv_cont->add(*fit_canv);
207 Gtk::Label* label_gen = manage (new Gtk::Label);
208 label_gen->set_markup (_("<b>General</b>"));
209 Gtk::Label* label_bor = manage (new Gtk::Label);
210 label_bor->set_markup (_("<b>Border</b>"));
211 Gtk::Label *label_for = manage (new Gtk::Label);
212 label_for->set_markup (_("<b>Format</b>"));
213 _page_sizer.init (_wr);
215 const Gtk::Widget* widget_array[] =
216 {
217 label_gen, 0,
218 _rum_deflt._label, _rum_deflt._sel,
219 _rcp_bg._label, _rcp_bg._cp,
220 0, 0,
221 label_for, 0,
222 0, &_page_sizer,
223 0, fit_canv_cont,
224 0, 0,
225 label_bor, 0,
226 0, _rcb_canb._button,
227 0, _rcb_bord._button,
228 0, _rcb_shad._button,
229 _rcp_bord._label, _rcp_bord._cp,
230 };
232 attach_all (_page_page.table(), widget_array, sizeof(widget_array));
233 }
235 void
236 DocumentProperties::build_grid()
237 {
238 _page_grid.show();
240 /// \todo FIXME: gray out snapping when grid is off.
241 /// Dissenting view: you want snapping without grid.
243 _rcbgrid.init (_("_Show grid"), _("Show or hide grid"), "showgrid", _wr);
244 _rumg.init (_("Grid _units:"), "grid_units", _wr);
245 _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
246 "gridoriginx", _rumg, _wr);
247 _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
248 "gridoriginy", _rumg, _wr);
249 _rsu_sx.init (_("Spacing _X:"), _("Distance of vertical grid lines"),
250 "gridspacingx", _rumg, _wr);
251 _rsu_sy.init (_("Spacing _Y:"), _("Distance of horizontal grid lines"),
252 "gridspacingy", _rumg, _wr);
253 _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
254 _("Color of grid lines"), "gridcolor", "gridopacity", _wr);
255 _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
256 _("Color of the major (highlighted) grid lines"),
257 "gridempcolor", "gridempopacity", _wr);
258 _rsi.init (_("_Major grid line every:"), _("lines"), "gridempspacing", _wr);
259 _rcb_sgui.init (_("Show _guides"), _("Show or hide guides"), "showguides", _wr);
260 _rcp_gui.init (_("Guide co_lor:"), _("Guideline color"),
261 _("Color of guidelines"), "guidecolor", "guideopacity", _wr);
262 _rcp_hgui.init (_("_Highlight color:"), _("Highlighted guideline color"),
263 _("Color of a guideline when it is under mouse"),
264 "guidehicolor", "guidehiopacity", _wr);
265 Gtk::Label *label_grid = manage (new Gtk::Label);
266 label_grid->set_markup (_("<b>Grid</b>"));
267 Gtk::Label *label_gui = manage (new Gtk::Label);
268 label_gui->set_markup (_("<b>Guides</b>"));
270 const Gtk::Widget* widget_array[] =
271 {
272 label_grid, 0,
273 0, _rcbgrid._button,
274 _rumg._label, _rumg._sel,
275 0, _rsu_ox.getSU(),
276 0, _rsu_oy.getSU(),
277 0, _rsu_sx.getSU(),
278 0, _rsu_sy.getSU(),
279 _rcp_gcol._label, _rcp_gcol._cp,
280 0, 0,
281 _rcp_gmcol._label, _rcp_gmcol._cp,
282 _rsi._label, &_rsi._hbox,
283 0, 0,
284 label_gui, 0,
285 0, _rcb_sgui._button,
286 _rcp_gui._label, _rcp_gui._cp,
287 _rcp_hgui._label, _rcp_hgui._cp,
288 };
290 attach_all (_page_grid.table(), widget_array, sizeof(widget_array));
291 }
293 void
294 DocumentProperties::build_snap()
295 {
296 _page_snap.show();
298 _rcbsnbo.init (_("_Snap bounding boxes to objects"),
299 _("Snap the edges of the object bounding boxes to other objects"),
300 "inkscape:object-bbox", _wr);
301 _rcbsnnob.init (_("Snap nodes _to objects"),
302 _("Snap the nodes of objects to other objects"),
303 "inkscape:object-points", _wr);
304 _rcbsnop.init (_("Snap to object _paths"),
305 _("Snap to other object paths"),
306 "inkscape:object-paths", _wr);
307 _rcbsnon.init (_("Snap to object _nodes"),
308 _("Snap to other object nodes"),
309 "inkscape:object-nodes", _wr);
310 _rsu_sno.init (_("Snap s_ensitivity:"), _("Always snap"),
311 _("Controls max. snapping distance from object"),
312 _("If set, objects snap to the nearest object when moved, regardless of distance"),
313 "objecttolerance", _wr);
314 _rcbsnbb.init (_("Snap _bounding boxes to grid"),
315 _("Snap the edges of the object bounding boxes"),
316 "inkscape:grid-bbox", _wr);
317 _rcbsnnod.init (_("Snap nodes to _grid"),
318 _("Snap path nodes, text baselines, ellipse centers, etc."),
319 "inkscape:grid-points", _wr);
320 _rsu_sn.init (_("Snap sens_itivity:"), _("Always snap"),
321 _("Controls max. snapping distance from grid"),
322 _("If set, objects snap to the nearest grid line when moved, regardless of distance"),
323 "gridtolerance", _wr);
324 _rcb_snpgui.init (_("Snap bounding boxes to g_uides"),
325 _("Snap the edges of the object bounding boxes"),
326 "inkscape:guide-bbox", _wr);
327 _rcb_snbgui.init (_("Snap p_oints to guides"),
328 _("Snap path nodes, text baselines, ellipse centers, etc."),
329 "inkscape:guide-points", _wr);
330 _rsu_gusn.init (_("Snap sensiti_vity:"), _("Always snap"),
331 _("Controls max. snapping distance from guides"),
332 _("If set, objects snap to the nearest guide when moved, regardless of distance"),
333 "guidetolerance", _wr);
334 // _rrb_pix.init (_("Sensitivity:"), _("S_creen pixels"), _("p_x units"),
335 // _("Sensitivity is always the same, regardless of zoom."),
336 // _("Sensitivity changes with zoom; zooming in will enlarge max. snapping distance."),
337 // _("inkscape:has_abs_tolerance"), _wr);
338 Gtk::Label *label_o = manage (new Gtk::Label);
339 label_o->set_markup (_("<b>Object Snapping</b>"));
340 Gtk::Label *label_gr = manage (new Gtk::Label);
341 label_gr->set_markup (_("<b>Grid Snapping</b>"));
342 Gtk::Label *label_gu = manage (new Gtk::Label);
343 label_gu->set_markup (_("<b>Guide Snapping</b>"));
345 const Gtk::Widget* array[] =
346 {
347 label_o, 0,
348 0, _rcbsnbo._button,
349 0, _rcbsnnob._button,
350 0, _rcbsnop._button,
351 0, _rcbsnon._button,
352 0, _rsu_sno._vbox,
353 0, 0,
354 label_gr, 0,
355 0, _rcbsnbb._button,
356 0, _rcbsnnod._button,
357 0, _rsu_sn._vbox,
358 0, 0,
359 label_gu, 0,
360 0, _rcb_snpgui._button,
361 0, _rcb_snbgui._button,
362 0, _rsu_gusn._vbox,
363 // 0, 0,
364 // 0, _rrb_pix._hbox,
365 };
367 attach_all (_page_snap.table(), array, sizeof(array));
368 }
370 /**
371 * Update dialog widgets from desktop.
372 */
373 void
374 DocumentProperties::update()
375 {
376 if (_wr.isUpdating()) return;
378 SPDesktop *dt = SP_ACTIVE_DESKTOP;
379 SPNamedView *nv = sp_desktop_namedview(dt);
380 _wr.setUpdating (true);
381 set_sensitive (true);
383 //-----------------------------------------------------------page page
384 _rcp_bg.setRgba32 (nv->pagecolor);
385 _rcb_canb.setActive (nv->showborder);
386 _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
387 _rcp_bord.setRgba32 (nv->bordercolor);
388 _rcb_shad.setActive (nv->showpageshadow);
390 if (nv->doc_units)
391 _rum_deflt.setUnit (nv->doc_units);
393 double const doc_w_px = sp_document_width(sp_desktop_document(dt));
394 double const doc_h_px = sp_document_height(sp_desktop_document(dt));
395 _page_sizer.setDim (doc_w_px, doc_h_px);
397 //-----------------------------------------------------------grid page
398 _rcbgrid.setActive (nv->showgrid);
399 _rumg.setUnit (nv->gridunit);
401 gdouble val;
402 val = nv->gridorigin[NR::X];
403 val = sp_pixels_get_units (val, *(nv->gridunit));
404 _rsu_ox.setValue (val);
405 val = nv->gridorigin[NR::Y];
406 val = sp_pixels_get_units (val, *(nv->gridunit));
407 _rsu_oy.setValue (val);
408 val = nv->gridspacing[NR::X];
409 double gridx = sp_pixels_get_units (val, *(nv->gridunit));
410 _rsu_sx.setValue (gridx);
411 val = nv->gridspacing[NR::Y];
412 double gridy = sp_pixels_get_units (val, *(nv->gridunit));
413 _rsu_sy.setValue (gridy);
415 _rcp_gcol.setRgba32 (nv->gridcolor);
416 _rcp_gmcol.setRgba32 (nv->gridempcolor);
417 _rsi.setValue (nv->gridempspacing);
419 //-----------------------------------------------------------guide
420 _rcb_sgui.setActive (nv->showguides);
421 _rcp_gui.setRgba32 (nv->guidecolor);
422 _rcp_hgui.setRgba32 (nv->guidehicolor);
424 //-----------------------------------------------------------snap
425 _rcbsnbo.setActive (nv->snap_object_bbox);
426 _rcbsnnob.setActive (nv->snap_object_point);
427 _rcbsnop.setActive (nv->snap_object_paths);
428 _rcbsnop.setActive (nv->snap_object_nodes);
429 _rsu_sno.setValue (nv->objecttolerance, nv->has_abs_tolerance);
431 _rcbsnbb.setActive (nv->snap_grid_bbox);
432 _rcbsnnod.setActive (nv->snap_grid_point);
433 _rsu_sn.setValue (nv->gridtolerance, nv->has_abs_tolerance);
435 _rcb_snpgui.setActive (nv->snap_guide_bbox);
436 _rcb_snbgui.setActive (nv->snap_guide_point);
437 _rsu_gusn.setValue (nv->guidetolerance, nv->has_abs_tolerance);
438 // _rrb_pix.setValue (true);
440 _wr.setUpdating (false);
441 }
443 //--------------------------------------------------------------------
445 void
446 DocumentProperties::on_response (int id)
447 {
448 if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
449 {
450 _rcp_bg.closeWindow();
451 _rcp_bord.closeWindow();
452 _rcp_gcol.closeWindow();
453 _rcp_gmcol.closeWindow();
454 _rcp_gui.closeWindow();
455 _rcp_hgui.closeWindow();
456 }
458 if (id == Gtk::RESPONSE_CLOSE)
459 hide();
460 }
462 /**
463 * Called when XML node attribute changed; updates dialog widgets.
464 */
465 static void
466 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer)
467 {
468 if (!_instance)
469 return;
471 _instance->update();
472 }
474 static void
475 on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
476 {
477 if (!_instance)
478 return;
480 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
481 repr->addListener (&_repr_events, _instance);
482 _instance->_doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
483 _instance->update();
484 }
486 static void
487 on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
488 {
489 if (!_instance)
490 return;
492 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
493 repr->removeListenerByData (_instance);
494 _instance->_doc_replaced_connection.disconnect();
495 }
497 static void
498 on_doc_replaced (SPDesktop* dt, SPDocument* doc)
499 {
500 if (!_instance)
501 return;
503 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
504 repr->addListener (&_repr_events, _instance);
505 _instance->update();
506 }
508 static void
509 fire_fit_canvas_to_selection_or_drawing() {
510 SPDesktop *dt = SP_ACTIVE_DESKTOP;
511 if (!dt) return;
512 Verb *verb = Verb::get( SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING );
513 if (verb) {
514 SPAction *action = verb->get_action(dt);
515 if (action) {
516 sp_action_perform(action, NULL);
517 }
518 }
519 }
522 } // namespace Dialog
523 } // namespace UI
524 } // namespace Inkscape
526 /*
527 Local Variables:
528 mode:c++
529 c-file-style:"stroustrup"
530 c-file-offsets:((innamespace . 0)(inline-open . 0))
531 indent-tabs-mode:nil
532 fill-column:99
533 End:
534 */
535 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :