Code

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