Code

string fixes from bug 1516157
[inkscape.git] / src / ui / dialog / document-properties.cpp
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 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 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 Inkscape::XML::NodeEventVector const _repr_events = {
62     NULL, /* child_added */
63     NULL, /* child_removed */
64     on_repr_attr_changed,
65     NULL, /* content_changed */
66     NULL  /* order_changed */
67 };
70 DocumentProperties*
71 DocumentProperties::create()
72 {
73     if (_instance) return _instance;
74     _instance = new DocumentProperties;
75     _instance->init();
76     return _instance;
77 }
79 void
80 DocumentProperties::destroy()
81 {
82     if (_instance)
83     {
84         delete _instance;
85         _instance = 0;
86     }
87 }
89 DocumentProperties::DocumentProperties() 
90     : Dialog ("dialogs.documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
91       _page_page(1, 1), _page_grid(1, 1), _page_guides(1, 1),
92       _page_snap(1, 1), 
93       _prefs_path("dialogs.documentoptions")
94 {
95     set_resizable (false);
96     _tt.enable();
97     get_vbox()->set_spacing (4);
98     get_vbox()->pack_start (_notebook, true, true);
100     _notebook.append_page(_page_page,      _("Page"));
101     _notebook.append_page(_page_grid,      _("Grid/Guides"));
102     _notebook.append_page(_page_snap,      _("Snap"));
104     build_page();
105     build_grid();
106     build_snap();
109 void
110 DocumentProperties::init()
112     update();
114     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
115     repr->addListener (&_repr_events, this);
116     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
117     root->addListener (&_repr_events, this);
119     _doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
121     g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
122                      G_CALLBACK(on_activate_desktop), 0);
123     
124     g_signal_connect(G_OBJECT(INKSCAPE), "deactivate_desktop",
125                      G_CALLBACK(on_deactivate_desktop), 0);
126     
127     show_all_children();
128     if (prefs_get_int_attribute("dialogs.documentoptions", "axonomgrid_enabled", 0) != 1) {
129         _rrb_gridtype._hbox->hide();
130         _rsu_ax.getSU()->hide();
131         _rsu_az.getSU()->hide();
132     }
134     present();
137 DocumentProperties::~DocumentProperties() 
139     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
140     repr->removeListenerByData (this);
141     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
142     root->removeListenerByData (this);
143     _doc_replaced_connection.disconnect();
146 //========================================================================
148 /**
149  * Helper function that attachs widgets in a 3xn table. The widgets come in an
150  * array that has two entries per table row. The two entries code for four
151  * possible cases: (0,0) means insert space in first column; (0, non-0) means
152  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
153  * (non-0, non-0) means two widgets in columns 2 and 3.
154 **/
155 inline void
156 attach_all (Gtk::Table &table, const Gtk::Widget *arr[], unsigned size, int start = 0)
158     for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2)
159     {
160         if (arr[i] && arr[i+1])
161         {
162             table.attach (const_cast<Gtk::Widget&>(*arr[i]),   1, 2, r, r+1, 
163                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
164             table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1, 
165                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
166         }
167         else
168         {
169             if (arr[i+1])
170                 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1, 
171                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
172             else if (arr[i])
173             {
174                 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
175                 label.set_alignment (0.0);
176                 table.attach (label, 0, 3, r, r+1, 
177                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
178             }
179             else
180             {
181                 Gtk::HBox *space = manage (new Gtk::HBox);
182                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
183                 table.attach (*space, 0, 1, r, r+1, 
184                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
185             }
186         }
187         ++r;
188     }
191 void
192 DocumentProperties::build_page()
194     _page_page.show();
196     _rcp_bg.init (_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"),
197                    "pagecolor", "inkscape:pageopacity", _wr);
198     _rcb_canb.init (_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false);
199     _rcb_bord.init (_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false);
200     _rcp_bord.init (_("Border _color:"), _("Page border color"),
201                     _("Color of the page border"),
202                     "bordercolor", "borderopacity", _wr);
203     _rcb_shad.init (_("_Show border shadow"), _("If set, page border shows a shadow on its right and lower side"), "inkscape:showpageshadow", _wr, false);
204     _rum_deflt.init (_("Default _units:"), "inkscape:document-units", _wr);
206     Gtk::Label* label_gen = manage (new Gtk::Label);
207     label_gen->set_markup (_("<b>General</b>"));
208     Gtk::Label* label_bor = manage (new Gtk::Label);
209     label_bor->set_markup (_("<b>Border</b>"));
210     Gtk::Label *label_for = manage (new Gtk::Label);
211     label_for->set_markup (_("<b>Format</b>"));
212     _page_sizer.init (_wr);
214     const Gtk::Widget* widget_array[] = 
215     {
216         label_gen,         0,
217         _rum_deflt._label, _rum_deflt._sel,
218         _rcp_bg._label,    _rcp_bg._cp,
219         0,                 0,
220         label_for,         0,
221         0,                 &_page_sizer,
222         0,                 0,
223         label_bor,         0,
224         0,                 _rcb_canb._button,
225         0,                 _rcb_bord._button,
226         0,                 _rcb_shad._button,
227         _rcp_bord._label,  _rcp_bord._cp,
228     };
229     
230     attach_all (_page_page.table(), widget_array, sizeof(widget_array));
233 void
234 DocumentProperties::build_grid()
236     _page_grid.show();
238     /// \todo FIXME: gray out snapping when grid is off.
239     /// Dissenting view: you want snapping without grid.
240     
241     _rcbgrid.init (_("_Show grid"), _("Show or hide grid"), "showgrid", _wr);
242     _rrb_gridtype.init (_("Grid type:"), _("Normal (2D)"), _("Axonometric (3D)"),
243                 _("The normal grid with vertical and horizontal lines."),
244                 _("A grid with vertical lines and two diagonal line groups, each representing the projection of a primary axis."),
245                 "gridtype", _wr);
246     
247     _rumg.init (_("Grid _units:"), "grid_units", _wr);
248     _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"), 
249                   "gridoriginx", _rumg, _wr);
250     _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"), 
251                   "gridoriginy", _rumg, _wr);
252     _rsu_sx.init (_("Spacing _X:"), _("Distance between vertical grid lines"), 
253                   "gridspacingx", _rumg, _wr);
254     _rsu_sy.init (_("Spacing _Y:"), _("Distance between horizontal grid lines"), 
255                   "gridspacingy", _rumg, _wr);
256     _rsu_ax.init (_("Angle X:"), _("Angle of x-axis of axonometric grid"), 
257                   "gridanglex", _rumg, _wr);
258     _rsu_az.init (_("Angle Z:"), _("Angle of z-axis of axonometric grid"), 
259                   "gridanglez", _rumg, _wr);
260     _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"), 
261                     _("Color of grid lines"), "gridcolor", "gridopacity", _wr);
262     _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"), 
263                      _("Color of the major (highlighted) grid lines"), 
264                      "gridempcolor", "gridempopacity", _wr);
265     _rsi.init (_("_Major grid line every:"), _("lines"), "gridempspacing", _wr);
266     _rcb_sgui.init (_("Show _guides"), _("Show or hide guides"), "showguides", _wr);
267     _rcp_gui.init (_("Guide co_lor:"), _("Guideline color"), 
268                    _("Color of guidelines"), "guidecolor", "guideopacity", _wr);
269     _rcp_hgui.init (_("_Highlight color:"), _("Highlighted guideline color"), 
270                     _("Color of a guideline when it is under mouse"),
271                     "guidehicolor", "guidehiopacity", _wr);
272     Gtk::Label *label_grid = manage (new Gtk::Label);
273     label_grid->set_markup (_("<b>Grid</b>"));
274     Gtk::Label *label_gui = manage (new Gtk::Label);
275     label_gui->set_markup (_("<b>Guides</b>"));
277     const Gtk::Widget* widget_array[] = 
278     {
279         label_grid,         0,
280         0,                  _rcbgrid._button,
281         0,                  _rrb_gridtype._hbox, 
282         _rumg._label,       _rumg._sel,
283         0,                  _rsu_ox.getSU(),
284         0,                  _rsu_oy.getSU(),
285         0,                  _rsu_sx.getSU(),
286         0,                  _rsu_sy.getSU(),
287         0,                  _rsu_ax.getSU(),
288         0,                  _rsu_az.getSU(),
289         _rcp_gcol._label,   _rcp_gcol._cp, 
290         0,                  0,
291         _rcp_gmcol._label,  _rcp_gmcol._cp,
292         _rsi._label,        &_rsi._hbox,
293         0, 0,
294         label_gui,       0,
295         0,               _rcb_sgui._button,
296         _rcp_gui._label, _rcp_gui._cp,
297         _rcp_hgui._label, _rcp_hgui._cp,
298     };
300     attach_all (_page_grid.table(), widget_array, sizeof(widget_array));
303 void
304 DocumentProperties::build_snap()
306     _page_snap.show();
308     _rcbsnbo.init (_("_Snap bounding boxes to objects"), 
309                 _("Snap the edges of the object bounding boxes to other objects"), 
310                 "inkscape:object-bbox", _wr);
311     _rcbsnnob.init (_("Snap nodes _to objects"), 
312                 _("Snap the nodes of objects to other objects"), 
313                 "inkscape:object-points", _wr);
314     _rcbsnop.init (_("Snap to object _paths"), 
315                 _("Snap to other object paths"), 
316                 "inkscape:object-paths", _wr);
317     _rcbsnon.init (_("Snap to object _nodes"), 
318                 _("Snap to other object nodes"), 
319                 "inkscape:object-nodes", _wr);
320     _rsu_sno.init (_("Snap s_ensitivity:"), _("Always snap"),
321                   _("Controls max. snapping distance from object"),
322                   _("If set, objects snap to the nearest object when moved, regardless of distance"),
323                   "objecttolerance", _wr);
324     _rcbsnbb.init (_("Snap _bounding boxes to grid"), 
325                 _("Snap the edges of the object bounding boxes"), 
326                 "inkscape:grid-bbox", _wr);
327     _rcbsnnod.init (_("Snap nodes to _grid"), 
328                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
329                 "inkscape:grid-points", _wr);
330     _rsu_sn.init (_("Snap sens_itivity:"), _("Always snap"),
331                   _("Controls max. snapping distance from grid"),
332                   _("If set, objects snap to the nearest grid line when moved, regardless of distance"),
333                   "gridtolerance", _wr);
334     _rcb_snpgui.init (_("Snap bounding boxes to g_uides"),  
335                      _("Snap the edges of the object bounding boxes"), 
336                      "inkscape:guide-bbox", _wr);
337     _rcb_snbgui.init (_("Snap p_oints to guides"), 
338                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
339                 "inkscape:guide-points", _wr);
340     _rsu_gusn.init (_("Snap sensiti_vity:"), _("Always snap"),
341                 _("Controls max. snapping distance from guides"), 
342                 _("If set, objects snap to the nearest guide when moved, regardless of distance"),
343                 "guidetolerance", _wr);
344 //    _rrb_pix.init (_("Sensitivity:"), _("S_creen pixels"), _("p_x units"),
345 //                _("Sensitivity is always the same, regardless of zoom."),
346 //                _("Sensitivity changes with zoom; zooming in will enlarge max. snapping distance."),
347 //                _("inkscape:has_abs_tolerance"), _wr);
348     Gtk::Label *label_o = manage (new Gtk::Label);
349     label_o->set_markup (_("<b>Object Snapping</b>"));
350     Gtk::Label *label_gr = manage (new Gtk::Label);
351     label_gr->set_markup (_("<b>Grid Snapping</b>"));
352     Gtk::Label *label_gu = manage (new Gtk::Label);
353     label_gu->set_markup (_("<b>Guide Snapping</b>"));
354      
355     const Gtk::Widget* array[] = 
356     {
357         label_o,            0,
358         0,                  _rcbsnbo._button,
359         0,                  _rcbsnnob._button,
360         0,                  _rcbsnop._button,
361         0,                  _rcbsnon._button,
362         0,                  _rsu_sno._vbox,
363         0, 0,
364         label_gr,           0,
365         0,                  _rcbsnbb._button,
366         0,                  _rcbsnnod._button,
367         0,                  _rsu_sn._vbox,
368         0, 0,
369         label_gu,         0,
370         0,                _rcb_snpgui._button,
371         0,                _rcb_snbgui._button,
372         0,                _rsu_gusn._vbox,
373 //        0, 0,
374 //        0,                _rrb_pix._hbox,
375     };
377     attach_all (_page_snap.table(), array, sizeof(array));
378  }
380 /**
381  * Update dialog widgets from desktop.
382  */
383 void
384 DocumentProperties::update()
386     if (_wr.isUpdating()) return;
387     
388     SPDesktop *dt = SP_ACTIVE_DESKTOP;
389     SPNamedView *nv = sp_desktop_namedview(dt);
390     _wr.setUpdating (true);
391     set_sensitive (true);
393     //-----------------------------------------------------------page page
394     _rcp_bg.setRgba32 (nv->pagecolor);
395     _rcb_canb.setActive (nv->showborder);
396     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
397     _rcp_bord.setRgba32 (nv->bordercolor);
398     _rcb_shad.setActive (nv->showpageshadow);
399     
400     if (nv->doc_units) 
401         _rum_deflt.setUnit (nv->doc_units);
403     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
404     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
405     _page_sizer.setDim (doc_w_px, doc_h_px);
407     //-----------------------------------------------------------grid page
408     _rcbgrid.setActive (nv->showgrid);
409     _rrb_gridtype.setValue (nv->gridtype);
410     _rumg.setUnit (nv->gridunit);
411     
412     gdouble val;
413     val = nv->gridorigin[NR::X];
414     val = sp_pixels_get_units (val, *(nv->gridunit));
415     _rsu_ox.setValue (val);
416     val = nv->gridorigin[NR::Y];
417     val = sp_pixels_get_units (val, *(nv->gridunit));
418     _rsu_oy.setValue (val);
419     val = nv->gridspacing[NR::X];
420     double gridx = sp_pixels_get_units (val, *(nv->gridunit));
421     _rsu_sx.setValue (gridx);
422     val = nv->gridspacing[NR::Y];
423     double gridy = sp_pixels_get_units (val, *(nv->gridunit));
424     _rsu_sy.setValue (gridy);
426     val = nv->gridangle[0];
427     _rsu_ax.setValue (val);
428     val = nv->gridangle[1];
429     _rsu_az.setValue (val);
431     _rcp_gcol.setRgba32 (nv->gridcolor);
432     _rcp_gmcol.setRgba32 (nv->gridempcolor);
433     _rsi.setValue (nv->gridempspacing);
435     //-----------------------------------------------------------guide
436     _rcb_sgui.setActive (nv->showguides);
437     _rcp_gui.setRgba32 (nv->guidecolor);
438     _rcp_hgui.setRgba32 (nv->guidehicolor);
440     //-----------------------------------------------------------snap
441     _rcbsnbo.setActive (nv->snap_manager.object.getSnapTo(Inkscape::Snapper::BBOX_POINT));
442     _rcbsnnob.setActive (nv->snap_manager.object.getSnapTo(Inkscape::Snapper::SNAP_POINT));
443     _rcbsnop.setActive (nv->snap_manager.object.getSnapToPaths());
444     _rcbsnop.setActive (nv->snap_manager.object.getSnapToNodes());
445     _rsu_sno.setValue (nv->objecttolerance, nv->has_abs_tolerance);
446      
447     _rcbsnbb.setActive (nv->snap_manager.grid.getSnapTo(Inkscape::Snapper::BBOX_POINT));
448     _rcbsnnod.setActive (nv->snap_manager.grid.getSnapTo(Inkscape::Snapper::SNAP_POINT));
449     _rsu_sn.setValue (nv->gridtolerance, nv->has_abs_tolerance);
450     
451     _rcb_snpgui.setActive (nv->snap_manager.guide.getSnapTo(Inkscape::Snapper::BBOX_POINT));
452     _rcb_snbgui.setActive (nv->snap_manager.guide.getSnapTo(Inkscape::Snapper::SNAP_POINT));
453     _rsu_gusn.setValue (nv->guidetolerance, nv->has_abs_tolerance);
454 //    _rrb_pix.setValue (true);
456     _wr.setUpdating (false);
459 //--------------------------------------------------------------------
461 void
462 DocumentProperties::on_response (int id)
464     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
465     {
466         _rcp_bg.closeWindow();
467         _rcp_bord.closeWindow();
468         _rcp_gcol.closeWindow();
469         _rcp_gmcol.closeWindow();
470         _rcp_gui.closeWindow();
471         _rcp_hgui.closeWindow();
472     } 
473     
474     if (id == Gtk::RESPONSE_CLOSE)
475         hide();
478 /**
479  * Called when XML node attribute changed; updates dialog widgets.
480  */
481 static void
482 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer)
484     if (!_instance)
485         return;
487     _instance->update();
490 static void 
491 on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
493     if (!_instance)
494         return;
496     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
497     repr->addListener (&_repr_events, _instance);
498     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
499     root->addListener (&_repr_events, _instance);
500     _instance->_doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
501     _instance->update();
504 static void 
505 on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
507     if (!_instance)
508         return;
510     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
511     repr->removeListenerByData (_instance);
512     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
513     root->removeListenerByData (_instance);
514     _instance->_doc_replaced_connection.disconnect();
517 static void 
518 on_doc_replaced (SPDesktop* dt, SPDocument* doc)
520     if (!_instance)
521         return;
523     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
524     repr->addListener (&_repr_events, _instance);
525     Inkscape::XML::Node *root = SP_OBJECT_REPR(doc->root);
526     root->addListener (&_repr_events, _instance);
527     _instance->update();
531 } // namespace Dialog
532 } // namespace UI
533 } // namespace Inkscape
535 /*
536   Local Variables:
537   mode:c++
538   c-file-style:"stroustrup"
539   c-file-offsets:((innamespace . 0)(inline-open . 0))
540   indent-tabs-mode:nil
541   fill-column:99
542   End:
543 */
544 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :