Code

f9a280b8604dcd9aee25bc9a869ba849a504859a
[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) 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 "ui/widget/color-picker.h"
24 #include "ui/widget/scalar-unit.h"
26 #include "xml/node-event-vector.h"
27 #include "helper/units.h"
29 #include "inkscape.h"
30 #include "verbs.h"
31 #include "document.h"
32 #include "desktop-handles.h"
33 #include "desktop.h"
34 #include "sp-namedview.h"
36 #include "document-properties.h"
38 using std::pair;
40 namespace Inkscape {
41 namespace UI {
42 namespace Dialog {
44 #define SPACE_SIZE_X 15
45 #define SPACE_SIZE_Y 10
47 //===================================================
49 //---------------------------------------------------
51 static DocumentProperties *_instance = 0;
53 static void on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer);
54 static void on_doc_replaced (SPDesktop* dt, SPDocument* doc);
55 static void on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*);
56 static void on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*);
58 static Inkscape::XML::NodeEventVector const _repr_events = {
59     NULL, /* child_added */
60     NULL, /* child_removed */
61     on_repr_attr_changed,
62     NULL, /* content_changed */
63     NULL  /* order_changed */
64 };
67 DocumentProperties*
68 DocumentProperties::create()
69 {
70     if (_instance) return _instance;
71     _instance = new DocumentProperties;
72     _instance->init();
73     return _instance;
74 }
76 void
77 DocumentProperties::destroy()
78 {
79     if (_instance)
80     {
81         delete _instance;
82         _instance = 0;
83     }
84 }
86 DocumentProperties::DocumentProperties() 
87     : Dialog ("dialogs.documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
88       _page_page(1, 1), _page_grid(1, 1), _page_guides(1, 1),
89       _page_snap(1, 1), 
90       _prefs_path("dialogs.documentoptions")
91 {
92     hide();
93     set_resizable (false);
94     _tt.enable();
95     get_vbox()->set_spacing (4);
96     get_vbox()->pack_start (_notebook, true, true);
98     _notebook.append_page(_page_page,      _("Page"));
99     _notebook.append_page(_page_grid,      _("Grid/Guides"));
100     _notebook.append_page(_page_snap,      _("Snap"));
102     build_page();
103     build_grid();
104     build_snap();
107 void
108 DocumentProperties::init()
110     update();
112     Inkscape::XML::Node *repr = SP_OBJECT_REPR(SP_DT_NAMEDVIEW(SP_ACTIVE_DESKTOP));
113     repr->addListener (&_repr_events, this);
115     _doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
117     g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
118                      G_CALLBACK(on_activate_desktop), 0);
119     
120     g_signal_connect(G_OBJECT(INKSCAPE), "deactivate_desktop",
121                      G_CALLBACK(on_deactivate_desktop), 0);
122     
123     show_all_children();
124     present();
127 DocumentProperties::~DocumentProperties() 
129     Inkscape::XML::Node *repr = SP_OBJECT_REPR(SP_DT_NAMEDVIEW(SP_ACTIVE_DESKTOP));
130     repr->removeListenerByData (this);
131     _doc_replaced_connection.disconnect();
134 //========================================================================
136 /**
137  * Helper function that attachs widgets in a 3xn table. The widgets come in an
138  * array that has two entries per table row. The two entries code for four
139  * possible cases: (0,0) means insert space in first column; (0, non-0) means
140  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
141  * (non-0, non-0) means two widgets in columns 2 and 3.
142 **/
143 inline void
144 attach_all (Gtk::Table &table, const Gtk::Widget *arr[], unsigned size, int start = 0)
146     for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2)
147     {
148         if (arr[i] && arr[i+1])
149         {
150             table.attach (const_cast<Gtk::Widget&>(*arr[i]),   1, 2, r, r+1, 
151                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
152             table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1, 
153                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
154         }
155         else
156         {
157             if (arr[i+1])
158                 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1, 
159                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
160             else if (arr[i])
161             {
162                 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
163                 label.set_alignment (0.0);
164                 table.attach (label, 0, 3, r, r+1, 
165                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
166             }
167             else
168             {
169                 Gtk::HBox *space = manage (new Gtk::HBox);
170                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
171                 table.attach (*space, 0, 1, r, r+1, 
172                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
173             }
174         }
175         ++r;
176     }
179 void
180 DocumentProperties::build_page()
182     _page_page.show();
184     _rcp_bg.init (_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"),
185                    "pagecolor", "inkscape:pageopacity", _wr);
186     _rcb_canb.init (_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false);
187     _rcb_bord.init (_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false);
188     _rcp_bord.init (_("Border _color:"), _("Page border color"),
189                     _("Color of the page border"),
190                     "bordercolor", "borderopacity", _wr);
191     _rcb_shad.init (_("_Show border shadow"), "If set, page border shows a shadow on its right and lower side", "inkscape:showpageshadow", _wr, false);
192     _rum_deflt.init (_("Default _units:"), "inkscape:document-units", _wr);
193     Gtk::Label* label_gen = manage (new Gtk::Label);
194     label_gen->set_markup (_("<b>General</b>"));
195     Gtk::Label* label_bor = manage (new Gtk::Label);
196     label_bor->set_markup (_("<b>Border</b>"));
197     Gtk::Label *label_for = manage (new Gtk::Label);
198     label_for->set_markup (_("<b>Format</b>"));
199     _page_sizer.init (_wr);
201     const Gtk::Widget* widget_array[] = 
202     {
203         label_gen,         0,
204         _rum_deflt._label, _rum_deflt._sel,
205         _rcp_bg._label,    _rcp_bg._cp,
206         0, 0,
207         label_for,         0,
208         0,                 &_page_sizer,
209         0, 0,
210         label_bor,         0,
211         0,                 _rcb_canb._button,
212         0,                 _rcb_bord._button,
213         0,                 _rcb_shad._button,
214         _rcp_bord._label,  _rcp_bord._cp,
215     };
216     
217     attach_all (_page_page.table(), widget_array, sizeof(widget_array));
220 void
221 DocumentProperties::build_grid()
223     _page_grid.show();
225     /// \todo FIXME: gray out snapping when grid is off.
226     /// Dissenting view: you want snapping without grid.
227     
228     _rcbgrid.init (_("_Show grid"), _("Show or hide grid"), "showgrid", _wr);
229     _rumg.init (_("Grid _units:"), "grid_units", _wr);
230     _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"), 
231                   "gridoriginx", _rumg, _wr);
232     _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"), 
233                   "gridoriginy", _rumg, _wr);
234     _rsu_sx.init (_("Spacing _X:"), _("Distance of vertical grid lines"), 
235                   "gridspacingx", _rumg, _wr);
236     _rsu_sy.init (_("Spacing _Y:"), _("Distance of horizontal grid lines"), 
237                   "gridspacingy", _rumg, _wr);
238     _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"), 
239                     _("Color of grid lines"), "gridcolor", "gridopacity", _wr);
240     _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"), 
241                      _("Color of the major (highlighted) grid lines"), 
242                      "gridempcolor", "gridempopacity", _wr);
243     _rsi.init (_("_Major grid line every:"), _("lines"), "gridempspacing", _wr);
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_grid = manage (new Gtk::Label);
251     label_grid->set_markup (_("<b>Grid</b>"));
252     Gtk::Label *label_gui = manage (new Gtk::Label);
253     label_gui->set_markup (_("<b>Guides</b>"));
255     const Gtk::Widget* widget_array[] = 
256     {
257         label_grid,         0,
258         0,                  _rcbgrid._button,
259         _rumg._label,       _rumg._sel,
260         0,                  _rsu_ox.getSU(),
261         0,                  _rsu_oy.getSU(),
262         0,                  _rsu_sx.getSU(),
263         0,                  _rsu_sy.getSU(),
264         _rcp_gcol._label,   _rcp_gcol._cp, 
265         0,                  0,
266         _rcp_gmcol._label,  _rcp_gmcol._cp,
267         _rsi._label,        &_rsi._hbox,
268         0, 0,
269         label_gui,       0,
270         0,               _rcb_sgui._button,
271         _rcp_gui._label, _rcp_gui._cp,
272         _rcp_hgui._label, _rcp_hgui._cp,
273     };
275     attach_all (_page_grid.table(), widget_array, sizeof(widget_array));
278 void
279 DocumentProperties::build_snap()
281     _page_snap.show();
283     _rcbsnbo.init (_("_Snap bounding boxes to objects"), 
284                 _("Snap the edges of the object bounding boxes to other objects"), 
285                 "inkscape:object-bbox", _wr);
286     _rcbsnnob.init (_("Snap nodes _to objects"), 
287                 _("Snap the nodes of objects to other objects"), 
288                 "inkscape:object-points", _wr);
289     _rcbsnop.init (_("Snap to object _paths"), 
290                 _("Snap to other object paths"), 
291                 "inkscape:object-paths", _wr);
292     _rcbsnon.init (_("Snap to object _nodes"), 
293                 _("Snap to other object nodes"), 
294                 "inkscape:object-nodes", _wr);
295     _rsu_sno.init (_("Snap s_ensitivity:"), 
296                   _("Controls max. snapping distance from object"),
297                   "objecttolerance", _wr);
298     _rcbsnbb.init (_("Snap _bounding boxes to grid"), 
299                 _("Snap the edges of the object bounding boxes"), 
300                 "inkscape:grid-bbox", _wr);
301     _rcbsnnod.init (_("Snap nodes to _grid"), 
302                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
303                 "inkscape:grid-points", _wr);
304     _rsu_sn.init (_("Snap sens_itivity:"), 
305                   _("Controls max. snapping distance from grid"),
306                   "gridtolerance", _wr);
307     _rcb_snpgui.init (_("Snap bounding boxes to g_uides"),  
308                      _("Snap the edges of the object bounding boxes"), 
309                      "inkscape:guide-bbox", _wr);
310     _rcb_snbgui.init (_("Snap p_oints to guides"), 
311                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
312                 "inkscape:guide-points", _wr);
313     _rsu_gusn.init (_("Snap sensiti_vity:"), 
314                 _("Controls max. snapping distance from guides"), "guidetolerance", _wr);
315     _rrb_pix.init (_("Sensitivity:"), _("S_creen pixels"), _("p_x units"),
316                 _("Sensitivity is always the same, regardless of zoom."),
317                 _("Sensitivity changes with zoom; zooming in will enlarge max. snapping distance."),
318                 _("inkscape:has_abs_tolerance"), _wr);
319     Gtk::Label *label_o = manage (new Gtk::Label);
320     label_o->set_markup (_("<b>Object Snapping</b>"));
321     Gtk::Label *label_gr = manage (new Gtk::Label);
322     label_gr->set_markup (_("<b>Grid Snapping</b>"));
323     Gtk::Label *label_gu = manage (new Gtk::Label);
324     label_gu->set_markup (_("<b>Guide Snapping</b>"));
325      
326     const Gtk::Widget* array[] = 
327     {
328         label_o,            0,
329         0,                  _rcbsnbo._button,
330         0,                  _rcbsnnob._button,
331         0,                  _rcbsnop._button,
332         0,                  _rcbsnon._button,
333         0,                  _rsu_sno._hbox,
334         0, 0,
335         label_gr,           0,
336         0,                  _rcbsnbb._button,
337         0,                  _rcbsnnod._button,
338         0,                  _rsu_sn._hbox,
339         0, 0,
340         label_gu,         0,
341         0,                _rcb_snpgui._button,
342         0,                _rcb_snbgui._button,
343         0,                _rsu_gusn._hbox,
344         0, 0,
345         0,                _rrb_pix._hbox,
346     };
348     attach_all (_page_snap.table(), array, sizeof(array));
349  }
351 /**
352  * Update dialog widgets from desktop.
353  */
354 void
355 DocumentProperties::update()
357     if (_wr.isUpdating()) return;
358     
359     SPDesktop *dt = SP_ACTIVE_DESKTOP;
360     SPNamedView *nv = SP_DT_NAMEDVIEW(dt);
361     _wr.setUpdating (true);
362     set_sensitive (true);
364     //-----------------------------------------------------------page page
365     _rcp_bg.setRgba32 (nv->pagecolor);
366     _rcb_canb.setActive (nv->showborder);
367     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
368     _rcp_bord.setRgba32 (nv->bordercolor);
369     _rcb_shad.setActive (nv->showpageshadow);
370     
371     if (nv->doc_units) 
372         _rum_deflt.setUnit (nv->doc_units);
374     double const doc_w_px = sp_document_width(SP_DT_DOCUMENT(dt));
375     double const doc_h_px = sp_document_height(SP_DT_DOCUMENT(dt));
376     _page_sizer.setDim (doc_w_px, doc_h_px);
378     //-----------------------------------------------------------grid page
379     _rcbgrid.setActive (nv->showgrid);
380     _rumg.setUnit (nv->gridunit);
381     
382     gdouble val;
383     val = nv->gridorigin[NR::X];
384     val = sp_pixels_get_units (val, *(nv->gridunit));
385     _rsu_ox.setValue (val);
386     val = nv->gridorigin[NR::Y];
387     val = sp_pixels_get_units (val, *(nv->gridunit));
388     _rsu_oy.setValue (val);
389     val = nv->gridspacing[NR::X];
390     double gridx = sp_pixels_get_units (val, *(nv->gridunit));
391     _rsu_sx.setValue (gridx);
392     val = nv->gridspacing[NR::Y];
393     double gridy = sp_pixels_get_units (val, *(nv->gridunit));
394     _rsu_sy.setValue (gridy);
396     _rcp_gcol.setRgba32 (nv->gridcolor);
397     _rcp_gmcol.setRgba32 (nv->gridempcolor);
398     _rsi.setValue (nv->gridempspacing);
400     //-----------------------------------------------------------guide
401     _rcb_sgui.setActive (nv->showguides);
402     _rcp_gui.setRgba32 (nv->guidecolor);
403     _rcp_hgui.setRgba32 (nv->guidehicolor);
405     //-----------------------------------------------------------snap
406     _rcbsnbo.setActive (nv->snap_object_bbox);
407     _rcbsnnob.setActive (nv->snap_object_point);
408     _rcbsnop.setActive (nv->snap_object_paths);
409     _rcbsnop.setActive (nv->snap_object_nodes);
410     _rsu_sno.setValue (nv->objecttolerance, nv->has_abs_tolerance);
411      
412     _rcbsnbb.setActive (nv->snap_grid_bbox);
413     _rcbsnnod.setActive (nv->snap_grid_point);
414     _rsu_sn.setValue (nv->gridtolerance, nv->has_abs_tolerance);
415     
416      _rcb_snpgui.setActive (nv->snap_guide_bbox);
417     _rcb_snbgui.setActive (nv->snap_guide_point);
418     _rsu_gusn.setValue (nv->guidetolerance, nv->has_abs_tolerance);
419     _rrb_pix.setValue (true);
421     _wr.setUpdating (false);
424 //--------------------------------------------------------------------
426 void
427 DocumentProperties::on_response (int id)
429     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
430     {
431         _rcp_bg.closeWindow();
432         _rcp_bord.closeWindow();
433         _rcp_gcol.closeWindow();
434         _rcp_gmcol.closeWindow();
435         _rcp_gui.closeWindow();
436         _rcp_hgui.closeWindow();
437     } 
438     
439     if (id == Gtk::RESPONSE_CLOSE)
440         hide();
443 /**
444  * Called when XML node attribute changed; updates dialog widgets.
445  */
446 static void
447 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer)
449     if (!_instance)
450         return;
452     _instance->update();
455 static void 
456 on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
458     if (!_instance)
459         return;
461     Inkscape::XML::Node *repr = SP_OBJECT_REPR(SP_DT_NAMEDVIEW(SP_ACTIVE_DESKTOP));
462     repr->addListener (&_repr_events, _instance);
463     _instance->_doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
464     _instance->update();
467 static void 
468 on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
470     if (!_instance)
471         return;
473     Inkscape::XML::Node *repr = SP_OBJECT_REPR(SP_DT_NAMEDVIEW(SP_ACTIVE_DESKTOP));
474     repr->removeListenerByData (_instance);
475     _instance->_doc_replaced_connection.disconnect();
478 static void 
479 on_doc_replaced (SPDesktop* dt, SPDocument* doc)
481     if (!_instance)
482         return;
484     Inkscape::XML::Node *repr = SP_OBJECT_REPR(SP_DT_NAMEDVIEW(dt));
485     repr->addListener (&_repr_events, _instance);
486     _instance->update();
490 } // namespace Dialog
491 } // namespace UI
492 } // namespace Inkscape
494 /*
495   Local Variables:
496   mode:c++
497   c-file-style:"stroustrup"
498   c-file-offsets:((innamespace . 0)(inline-open . 0))
499   indent-tabs-mode:nil
500   fill-column:99
501   End:
502 */
503 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :