e2dc2c0b9156ad6fb403c462d699bff94b10ca26
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) 2006-2007 Johan Engelen <johan@shouraizou.nl>
13 * Copyright (C) 2000 - 2005 Authors
14 *
15 * Released under GNU GPL. Read the file 'COPYING' for more information
16 */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
24 #include <gtkmm.h>
25 #include "ui/widget/color-picker.h"
26 #include "ui/widget/scalar-unit.h"
28 #include "xml/node-event-vector.h"
29 #include "helper/units.h"
30 #include "prefs-utils.h"
32 #include "inkscape.h"
33 #include "verbs.h"
34 #include "document.h"
35 #include "desktop-handles.h"
36 #include "desktop.h"
37 #include "sp-namedview.h"
39 #include "document-properties.h"
41 #include "display/canvas-grid.h"
43 using std::pair;
45 namespace Inkscape {
46 namespace UI {
47 namespace Dialog {
49 #define SPACE_SIZE_X 15
50 #define SPACE_SIZE_Y 10
52 //===================================================
54 //---------------------------------------------------
56 static DocumentProperties *_instance = 0;
58 static void on_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
59 static void on_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
60 static void on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer);
61 static void on_doc_replaced (SPDesktop* dt, SPDocument* doc);
62 static void on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*);
63 static void on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*);
65 static Inkscape::XML::NodeEventVector const _repr_events = {
66 on_child_added, /* child_added */
67 on_child_removed, /* child_removed */
68 on_repr_attr_changed,
69 NULL, /* content_changed */
70 NULL /* order_changed */
71 };
74 DocumentProperties*
75 DocumentProperties::create(Behavior::BehaviorFactory behavior_factory)
76 {
77 if (_instance) return _instance;
78 _instance = new DocumentProperties(behavior_factory);
79 _instance->init();
80 return _instance;
81 }
83 void
84 DocumentProperties::destroy()
85 {
86 if (_instance)
87 {
88 delete _instance;
89 _instance = 0;
90 }
91 }
93 DocumentProperties::DocumentProperties(Behavior::BehaviorFactory behavior_factory)
94 : Dialog (behavior_factory, "dialogs.documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
95 _page_page(1, 1), _page_guides(1, 1),
96 _page_snap(1, 1), _page_grids(1, 1),
97 _grids_button_new(_("_New"), _("Create new grid.")),
98 _grids_button_remove(_("_Remove"), _("Remove selected grid.")),
99 _prefs_path("dialogs.documentoptions")
100 {
101 set_resizable (false);
102 _tt.enable();
103 get_vbox()->set_spacing (4);
104 get_vbox()->pack_start (_notebook, true, true);
106 _notebook.append_page(_page_page, _("Page"));
107 _notebook.append_page(_page_guides, _("Guides"));
108 _notebook.append_page(_page_grids, _("Grids"));
109 _notebook.append_page(_page_snap, _("Snapping"));
111 build_page();
112 build_guides();
113 build_gridspage();
114 build_snap();
116 _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
117 _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
118 }
120 void
121 DocumentProperties::init()
122 {
123 update();
125 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
126 repr->addListener (&_repr_events, this);
127 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
128 root->addListener (&_repr_events, this);
130 _doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
132 g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
133 G_CALLBACK(on_activate_desktop), 0);
135 g_signal_connect(G_OBJECT(INKSCAPE), "deactivate_desktop",
136 G_CALLBACK(on_deactivate_desktop), 0);
138 show_all_children();
140 present();
141 }
143 DocumentProperties::~DocumentProperties()
144 {
145 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
146 repr->removeListenerByData (this);
147 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
148 root->removeListenerByData (this);
149 _doc_replaced_connection.disconnect();
150 }
152 //========================================================================
154 /**
155 * Helper function that attaches widgets in a 3xn table. The widgets come in an
156 * array that has two entries per table row. The two entries code for four
157 * possible cases: (0,0) means insert space in first column; (0, non-0) means
158 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
159 * (non-0, non-0) means two widgets in columns 2 and 3.
160 **/
161 inline void
162 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
163 {
164 for (unsigned i = 0, r = start; i < n; i += 2)
165 {
166 if (arr[i] && arr[i+1])
167 {
168 table.attach(*arr[i], 1, 2, r, r+1,
169 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
170 table.attach(*arr[i+1], 2, 3, r, r+1,
171 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
172 }
173 else
174 {
175 if (arr[i+1])
176 table.attach(*arr[i+1], 1, 3, r, r+1,
177 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
178 else if (arr[i])
179 {
180 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
181 label.set_alignment (0.0);
182 table.attach (label, 0, 3, r, r+1,
183 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
184 }
185 else
186 {
187 Gtk::HBox *space = manage (new Gtk::HBox);
188 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
189 table.attach (*space, 0, 1, r, r+1,
190 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
191 }
192 }
193 ++r;
194 }
195 }
197 void
198 DocumentProperties::build_page()
199 {
200 _page_page.show();
202 _rcp_bg.init (_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"),
203 "pagecolor", "inkscape:pageopacity", _wr);
204 _rcb_canb.init (_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false);
205 _rcb_bord.init (_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false);
206 _rcp_bord.init (_("Border _color:"), _("Page border color"),
207 _("Color of the page border"),
208 "bordercolor", "borderopacity", _wr);
209 _rcb_shad.init (_("_Show border shadow"), _("If set, page border shows a shadow on its right and lower side"), "inkscape:showpageshadow", _wr, false);
210 _rum_deflt.init (_("Default _units:"), "inkscape:document-units", _wr);
212 Gtk::Label* label_gen = manage (new Gtk::Label);
213 label_gen->set_markup (_("<b>General</b>"));
214 Gtk::Label* label_bor = manage (new Gtk::Label);
215 label_bor->set_markup (_("<b>Border</b>"));
216 Gtk::Label *label_for = manage (new Gtk::Label);
217 label_for->set_markup (_("<b>Format</b>"));
218 _page_sizer.init (_wr);
220 Gtk::Widget *const widget_array[] =
221 {
222 label_gen, 0,
223 _rum_deflt._label, _rum_deflt._sel,
224 _rcp_bg._label, _rcp_bg._cp,
225 0, 0,
226 label_for, 0,
227 0, &_page_sizer,
228 0, 0,
229 label_bor, 0,
230 0, _rcb_canb._button,
231 0, _rcb_bord._button,
232 0, _rcb_shad._button,
233 _rcp_bord._label, _rcp_bord._cp,
234 };
236 attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
237 }
239 void
240 DocumentProperties::build_guides()
241 {
242 _page_guides.show();
244 _rcb_sgui.init (_("Show _guides"), _("Show or hide guides"), "showguides", _wr);
245 _rcp_gui.init (_("Guide co_lor:"), _("Guideline color"),
246 _("Color of guidelines"), "guidecolor", "guideopacity", _wr);
247 _rcp_hgui.init (_("_Highlight color:"), _("Highlighted guideline color"),
248 _("Color of a guideline when it is under mouse"),
249 "guidehicolor", "guidehiopacity", _wr);
250 Gtk::Label *label_gui = manage (new Gtk::Label);
251 label_gui->set_markup (_("<b>Guides</b>"));
253 Gtk::Widget *const widget_array[] =
254 {
255 label_gui, 0,
256 0, _rcb_sgui._button,
257 _rcp_gui._label, _rcp_gui._cp,
258 _rcp_hgui._label, _rcp_hgui._cp,
259 };
261 attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
262 }
264 void
265 DocumentProperties::build_snap()
266 {
267 _page_snap.show();
268 //General options
269 _rcbsnbb.init (_("Bounding _box corners"),
270 _("Snap bounding box corners to grid lines, to guides, and to other bounding boxes (only applicable to the selector tool)"),
271 "inkscape:snap-bbox", _wr);
272 _rcbsnn.init (_("_Nodes"),
273 _("Snap nodes to grid lines, to guides, to paths, and to other nodes"),
274 "inkscape:snap-nodes", _wr);
275 _rcbsng.init (_("_Guides"),
276 _("While dragging a guide, snap to object nodes. (In 'Snapping to objects', 'Snap to nodes' must also be enabled)"),
277 "inkscape:snap-guide", _wr);
279 //Options for snapping to objects
280 _rcbsnop.init (_("Snap to p_aths"),
281 _("Snap nodes to object paths"),
282 "inkscape:object-paths", _wr);
283 _rcbsnon.init (_("Snap to n_odes"),
284 _("Snap nodes or guides to object nodes"),
285 "inkscape:object-nodes", _wr);
286 _rsu_sno.init (_("Snap _distance"), _("Snap at any dist_ance"),
287 _("Snapping distance, in screen pixels, for snapping to objects"),
288 _("If set, objects snap to the nearest object, regardless of distance"),
289 "objecttolerance", _wr);
291 //Options for snapping to grids
292 _rsu_sn.init (_("Snap d_istance"), _("Snap at any distan_ce"),
293 _("Snapping distance, in screen pixels, for snapping to grid"),
294 _("If set, objects snap to the nearest grid line, regardless of distance"),
295 "gridtolerance", _wr);
297 //Options for snapping to guides
298 _rsu_gusn.init (_("Snap di_stance"), _("Snap at any distanc_e"),
299 _("Snapping distance, in screen pixels, for snapping to guides"),
300 _("If set, objects snap to the nearest guide, regardless of distance"),
301 "guidetolerance", _wr);
303 //Some other options
304 _rcbic.init (_("_Include the object's rotation center"),
305 _("Also snap the rotation center of an object when snapping nodes or guides"),
306 "inkscape:snap-center", _wr);
307 //Applies to both nodes and guides, but not to bboxes, that's why its located here
308 //Other options to locate here: e.g. visual snapping indicators on/off
310 Gtk::Label *label_g = manage (new Gtk::Label);
311 label_g->set_markup (_("<b>Snapping of</b>"));
312 Gtk::Label *label_o = manage (new Gtk::Label);
313 label_o->set_markup (_("<b>Snapping to objects</b>"));
314 Gtk::Label *label_gr = manage (new Gtk::Label);
315 label_gr->set_markup (_("<b>Snapping to grids</b>"));
316 Gtk::Label *label_gu = manage (new Gtk::Label);
317 label_gu->set_markup (_("<b>Snapping to guides</b>"));
318 Gtk::Label *label_m = manage (new Gtk::Label);
319 label_m->set_markup (_("<b>Miscellaneous</b>"));
321 Gtk::Widget *const array[] =
322 {
323 label_g, 0,
324 0, _rcbsnn._button,
325 //0, 0, //_rcbic._button will be inserted here
326 0, _rcbsnbb._button,
327 0, _rcbsng._button,
328 0, 0,
329 0, 0,
330 0, 0,
331 label_o, 0,
332 0, _rcbsnop._button,
333 0, _rcbsnon._button,
334 0, _rsu_sno._vbox,
335 0, 0,
336 label_gr, 0,
337 0, _rsu_sn._vbox,
338 0, 0,
339 label_gu, 0,
340 0, _rsu_gusn._vbox,
341 0, 0,
342 0, 0,
343 0, 0,
344 label_m, 0,
345 0, _rcbic._button
346 };
348 attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
349 }
351 /**
352 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
353 */
354 void
355 DocumentProperties::update_gridspage()
356 {
357 SPDesktop *dt = SP_ACTIVE_DESKTOP;
358 SPNamedView *nv = sp_desktop_namedview(dt);
360 //remove all tabs
361 while (_grids_notebook.get_current_page() != -1) {
362 _grids_notebook.remove_page(-1);
363 }
365 //add tabs
366 for (GSList const * l = nv->grids; l != NULL; l = l->next) {
367 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
368 _grids_notebook.append_page(grid->getWidget(), grid->repr->attribute("id"));
370 }
371 _grids_notebook.show_all();
373 _page_grids.table().resize_children();
374 }
376 /**
377 * Build grid page of dialog.
378 */
379 void
380 DocumentProperties::build_gridspage()
381 {
382 _page_grids.show();
384 /// \todo FIXME: gray out snapping when grid is off.
385 /// Dissenting view: you want snapping without grid.
387 SPDesktop *dt = SP_ACTIVE_DESKTOP;
388 SPNamedView *nv = sp_desktop_namedview(dt);
390 Gtk::Label* label_crea = manage (new Gtk::Label);
391 label_crea->set_markup (_("<b>Creation</b>"));
392 Gtk::Label* label_crea_type = manage (new Gtk::Label);
393 label_crea_type->set_markup (_("Gridtype"));
395 for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
396 _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
397 }
398 _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
400 Gtk::Label* label_def = manage (new Gtk::Label);
401 label_def->set_markup (_("<b>Defined grids</b>"));
403 for (GSList const * l = nv->grids; l != NULL; l = l->next) {
404 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
405 _grids_notebook.append_page(grid->getWidget(), grid->repr->attribute("id"));
406 }
408 Gtk::Widget *const widget_array[] =
409 {
410 label_crea, 0,
411 label_crea_type, (Gtk::Widget*) &_grids_combo_gridtype,
412 (Gtk::Widget*) &_grids_button_new, (Gtk::Widget*) &_grids_button_remove,
413 label_def, 0
414 };
415 attach_all(_page_grids.table(), widget_array, G_N_ELEMENTS(widget_array));
416 _page_grids.table().attach(_grids_notebook, 0, 3, 4, 5,
417 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
418 }
422 /**
423 * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
424 */
425 void
426 DocumentProperties::update()
427 {
428 if (_wr.isUpdating()) return;
430 SPDesktop *dt = SP_ACTIVE_DESKTOP;
431 SPNamedView *nv = sp_desktop_namedview(dt);
433 _wr.setUpdating (true);
434 set_sensitive (true);
436 //-----------------------------------------------------------page page
437 _rcp_bg.setRgba32 (nv->pagecolor);
438 _rcb_canb.setActive (nv->showborder);
439 _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
440 _rcp_bord.setRgba32 (nv->bordercolor);
441 _rcb_shad.setActive (nv->showpageshadow);
443 if (nv->doc_units)
444 _rum_deflt.setUnit (nv->doc_units);
446 double const doc_w_px = sp_document_width(sp_desktop_document(dt));
447 double const doc_h_px = sp_document_height(sp_desktop_document(dt));
448 _page_sizer.setDim (doc_w_px, doc_h_px);
450 //-----------------------------------------------------------guide
451 _rcb_sgui.setActive (nv->showguides);
452 _rcp_gui.setRgba32 (nv->guidecolor);
453 _rcp_hgui.setRgba32 (nv->guidehicolor);
455 //-----------------------------------------------------------snap
457 _rcbsnbb.setActive (nv->snap_manager.getSnapModeBBox());
458 _rcbsnn.setActive (nv->snap_manager.getSnapModeNode());
459 _rcbsng.setActive (nv->snap_manager.getSnapModeGuide());
460 _rcbic.setActive (nv->snap_manager.getIncludeItemCenter());
461 _rcbsnop.setActive(nv->snap_manager.object.getSnapToItemPath());
462 _rcbsnon.setActive(nv->snap_manager.object.getSnapToItemNode());
463 _rsu_sno.setValue (nv->objecttolerance);
465 _rsu_sn.setValue (nv->gridtolerance);
467 _rsu_gusn.setValue (nv->guidetolerance);
469 //-----------------------------------------------------------grids page
471 update_gridspage();
473 _wr.setUpdating (false);
474 }
476 //--------------------------------------------------------------------
478 void
479 DocumentProperties::on_response (int id)
480 {
481 if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
482 {
483 _rcp_bg.closeWindow();
484 _rcp_bord.closeWindow();
485 _rcp_gui.closeWindow();
486 _rcp_hgui.closeWindow();
487 }
489 if (id == Gtk::RESPONSE_CLOSE)
490 hide();
491 }
495 static void
496 on_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data)
497 {
498 if (!_instance)
499 return;
501 _instance->update_gridspage();
502 }
504 static void
505 on_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data)
506 {
507 if (!_instance)
508 return;
510 _instance->update_gridspage();
511 }
515 /**
516 * Called when XML node attribute changed; updates dialog widgets.
517 */
518 static void
519 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer)
520 {
521 if (!_instance)
522 return;
524 _instance->update();
525 }
527 static void
528 on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
529 {
530 if (!_instance)
531 return;
533 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
534 repr->addListener (&_repr_events, _instance);
535 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
536 root->addListener (&_repr_events, _instance);
537 _instance->_doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
538 _instance->update();
539 }
541 static void
542 on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
543 {
544 if (!_instance)
545 return;
547 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
548 repr->removeListenerByData (_instance);
549 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
550 root->removeListenerByData (_instance);
551 _instance->_doc_replaced_connection.disconnect();
552 }
554 static void
555 on_doc_replaced (SPDesktop* dt, SPDocument* doc)
556 {
557 if (!_instance)
558 return;
560 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
561 repr->addListener (&_repr_events, _instance);
562 Inkscape::XML::Node *root = SP_OBJECT_REPR(doc->root);
563 root->addListener (&_repr_events, _instance);
564 _instance->update();
565 }
570 /*########################################################################
571 # BUTTON CLICK HANDLERS (callbacks)
572 ########################################################################*/
574 void
575 DocumentProperties::onNewGrid()
576 {
577 SPDesktop *dt = SP_ACTIVE_DESKTOP;
578 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
579 SPDocument *doc = sp_desktop_document(dt);
581 Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
582 CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
583 }
586 void
587 DocumentProperties::onRemoveGrid()
588 {
589 gint pagenum = _grids_notebook.get_current_page();
590 if (pagenum == -1) // no pages
591 return;
593 Gtk::Widget *page = _grids_notebook.get_nth_page(pagenum);
594 if (!page) return;
596 Glib::ustring tabtext = _grids_notebook.get_tab_label_text(*page);
598 // find the grid with name tabtext (it's id) and delete that one.
599 SPDesktop *dt = SP_ACTIVE_DESKTOP;
600 SPNamedView *nv = sp_desktop_namedview(dt);
601 Inkscape::CanvasGrid * found_grid = NULL;
602 for (GSList const * l = nv->grids; l != NULL; l = l->next) {
603 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
604 gchar const *idtext = grid->repr->attribute("id");
605 if ( !strcmp(tabtext.c_str(), idtext) ) {
606 found_grid = grid;
607 break; // break out of for-loop
608 }
609 }
610 if (found_grid) {
611 // delete the grid that corresponds with the selected tab
612 // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
613 found_grid->repr->parent()->removeChild(found_grid->repr);
614 sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
615 }
616 }
619 } // namespace Dialog
620 } // namespace UI
621 } // namespace Inkscape
623 /*
624 Local Variables:
625 mode:c++
626 c-file-style:"stroustrup"
627 c-file-offsets:((innamespace . 0)(inline-open . 0))
628 indent-tabs-mode:nilu
629 fill-column:99
630 End:
631 */
632 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :