Code

Give SPNamedView a SnapManager instance and use it for all management of snapping...
[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);
115     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
116     root->addListener (&_repr_events, this);
118     _doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
120     g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
121                      G_CALLBACK(on_activate_desktop), 0);
122     
123     g_signal_connect(G_OBJECT(INKSCAPE), "deactivate_desktop",
124                      G_CALLBACK(on_deactivate_desktop), 0);
125     
126     show_all_children();
127     present();
130 DocumentProperties::~DocumentProperties() 
132     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
133     repr->removeListenerByData (this);
134     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
135     root->removeListenerByData (this);
136     _doc_replaced_connection.disconnect();
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)
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     }
184 void
185 DocumentProperties::build_page()
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     Gtk::Label* label_gen = manage (new Gtk::Label);
200     label_gen->set_markup (_("<b>General</b>"));
201     Gtk::Label* label_bor = manage (new Gtk::Label);
202     label_bor->set_markup (_("<b>Border</b>"));
203     Gtk::Label *label_for = manage (new Gtk::Label);
204     label_for->set_markup (_("<b>Format</b>"));
205     _page_sizer.init (_wr);
207     const Gtk::Widget* widget_array[] = 
208     {
209         label_gen,         0,
210         _rum_deflt._label, _rum_deflt._sel,
211         _rcp_bg._label,    _rcp_bg._cp,
212         0,                 0,
213         label_for,         0,
214         0,                 &_page_sizer,
215         0,                 0,
216         label_bor,         0,
217         0,                 _rcb_canb._button,
218         0,                 _rcb_bord._button,
219         0,                 _rcb_shad._button,
220         _rcp_bord._label,  _rcp_bord._cp,
221     };
222     
223     attach_all (_page_page.table(), widget_array, sizeof(widget_array));
226 void
227 DocumentProperties::build_grid()
229     _page_grid.show();
231     /// \todo FIXME: gray out snapping when grid is off.
232     /// Dissenting view: you want snapping without grid.
233     
234     _rcbgrid.init (_("_Show grid"), _("Show or hide grid"), "showgrid", _wr);
235     _rumg.init (_("Grid _units:"), "grid_units", _wr);
236     _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"), 
237                   "gridoriginx", _rumg, _wr);
238     _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"), 
239                   "gridoriginy", _rumg, _wr);
240     _rsu_sx.init (_("Spacing _X:"), _("Distance of vertical grid lines"), 
241                   "gridspacingx", _rumg, _wr);
242     _rsu_sy.init (_("Spacing _Y:"), _("Distance of horizontal grid lines"), 
243                   "gridspacingy", _rumg, _wr);
244     _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"), 
245                     _("Color of grid lines"), "gridcolor", "gridopacity", _wr);
246     _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"), 
247                      _("Color of the major (highlighted) grid lines"), 
248                      "gridempcolor", "gridempopacity", _wr);
249     _rsi.init (_("_Major grid line every:"), _("lines"), "gridempspacing", _wr);
250     _rcb_sgui.init (_("Show _guides"), _("Show or hide guides"), "showguides", _wr);
251     _rcp_gui.init (_("Guide co_lor:"), _("Guideline color"), 
252                    _("Color of guidelines"), "guidecolor", "guideopacity", _wr);
253     _rcp_hgui.init (_("_Highlight color:"), _("Highlighted guideline color"), 
254                     _("Color of a guideline when it is under mouse"),
255                     "guidehicolor", "guidehiopacity", _wr);
256     Gtk::Label *label_grid = manage (new Gtk::Label);
257     label_grid->set_markup (_("<b>Grid</b>"));
258     Gtk::Label *label_gui = manage (new Gtk::Label);
259     label_gui->set_markup (_("<b>Guides</b>"));
261     const Gtk::Widget* widget_array[] = 
262     {
263         label_grid,         0,
264         0,                  _rcbgrid._button,
265         _rumg._label,       _rumg._sel,
266         0,                  _rsu_ox.getSU(),
267         0,                  _rsu_oy.getSU(),
268         0,                  _rsu_sx.getSU(),
269         0,                  _rsu_sy.getSU(),
270         _rcp_gcol._label,   _rcp_gcol._cp, 
271         0,                  0,
272         _rcp_gmcol._label,  _rcp_gmcol._cp,
273         _rsi._label,        &_rsi._hbox,
274         0, 0,
275         label_gui,       0,
276         0,               _rcb_sgui._button,
277         _rcp_gui._label, _rcp_gui._cp,
278         _rcp_hgui._label, _rcp_hgui._cp,
279     };
281     attach_all (_page_grid.table(), widget_array, sizeof(widget_array));
284 void
285 DocumentProperties::build_snap()
287     _page_snap.show();
289     _rcbsnbo.init (_("_Snap bounding boxes to objects"), 
290                 _("Snap the edges of the object bounding boxes to other objects"), 
291                 "inkscape:object-bbox", _wr);
292     _rcbsnnob.init (_("Snap nodes _to objects"), 
293                 _("Snap the nodes of objects to other objects"), 
294                 "inkscape:object-points", _wr);
295     _rcbsnop.init (_("Snap to object _paths"), 
296                 _("Snap to other object paths"), 
297                 "inkscape:object-paths", _wr);
298     _rcbsnon.init (_("Snap to object _nodes"), 
299                 _("Snap to other object nodes"), 
300                 "inkscape:object-nodes", _wr);
301     _rsu_sno.init (_("Snap s_ensitivity:"), _("Always snap"),
302                   _("Controls max. snapping distance from object"),
303                   _("If set, objects snap to the nearest object when moved, regardless of distance"),
304                   "objecttolerance", _wr);
305     _rcbsnbb.init (_("Snap _bounding boxes to grid"), 
306                 _("Snap the edges of the object bounding boxes"), 
307                 "inkscape:grid-bbox", _wr);
308     _rcbsnnod.init (_("Snap nodes to _grid"), 
309                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
310                 "inkscape:grid-points", _wr);
311     _rsu_sn.init (_("Snap sens_itivity:"), _("Always snap"),
312                   _("Controls max. snapping distance from grid"),
313                   _("If set, objects snap to the nearest grid line when moved, regardless of distance"),
314                   "gridtolerance", _wr);
315     _rcb_snpgui.init (_("Snap bounding boxes to g_uides"),  
316                      _("Snap the edges of the object bounding boxes"), 
317                      "inkscape:guide-bbox", _wr);
318     _rcb_snbgui.init (_("Snap p_oints to guides"), 
319                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
320                 "inkscape:guide-points", _wr);
321     _rsu_gusn.init (_("Snap sensiti_vity:"), _("Always snap"),
322                 _("Controls max. snapping distance from guides"), 
323                 _("If set, objects snap to the nearest guide when moved, regardless of distance"),
324                 "guidetolerance", _wr);
325 //    _rrb_pix.init (_("Sensitivity:"), _("S_creen pixels"), _("p_x units"),
326 //                _("Sensitivity is always the same, regardless of zoom."),
327 //                _("Sensitivity changes with zoom; zooming in will enlarge max. snapping distance."),
328 //                _("inkscape:has_abs_tolerance"), _wr);
329     Gtk::Label *label_o = manage (new Gtk::Label);
330     label_o->set_markup (_("<b>Object Snapping</b>"));
331     Gtk::Label *label_gr = manage (new Gtk::Label);
332     label_gr->set_markup (_("<b>Grid Snapping</b>"));
333     Gtk::Label *label_gu = manage (new Gtk::Label);
334     label_gu->set_markup (_("<b>Guide Snapping</b>"));
335      
336     const Gtk::Widget* array[] = 
337     {
338         label_o,            0,
339         0,                  _rcbsnbo._button,
340         0,                  _rcbsnnob._button,
341         0,                  _rcbsnop._button,
342         0,                  _rcbsnon._button,
343         0,                  _rsu_sno._vbox,
344         0, 0,
345         label_gr,           0,
346         0,                  _rcbsnbb._button,
347         0,                  _rcbsnnod._button,
348         0,                  _rsu_sn._vbox,
349         0, 0,
350         label_gu,         0,
351         0,                _rcb_snpgui._button,
352         0,                _rcb_snbgui._button,
353         0,                _rsu_gusn._vbox,
354 //        0, 0,
355 //        0,                _rrb_pix._hbox,
356     };
358     attach_all (_page_snap.table(), array, sizeof(array));
359  }
361 /**
362  * Update dialog widgets from desktop.
363  */
364 void
365 DocumentProperties::update()
367     if (_wr.isUpdating()) return;
368     
369     SPDesktop *dt = SP_ACTIVE_DESKTOP;
370     SPNamedView *nv = sp_desktop_namedview(dt);
371     _wr.setUpdating (true);
372     set_sensitive (true);
374     //-----------------------------------------------------------page page
375     _rcp_bg.setRgba32 (nv->pagecolor);
376     _rcb_canb.setActive (nv->showborder);
377     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
378     _rcp_bord.setRgba32 (nv->bordercolor);
379     _rcb_shad.setActive (nv->showpageshadow);
380     
381     if (nv->doc_units) 
382         _rum_deflt.setUnit (nv->doc_units);
384     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
385     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
386     _page_sizer.setDim (doc_w_px, doc_h_px);
388     //-----------------------------------------------------------grid page
389     _rcbgrid.setActive (nv->showgrid);
390     _rumg.setUnit (nv->gridunit);
391     
392     gdouble val;
393     val = nv->gridorigin[NR::X];
394     val = sp_pixels_get_units (val, *(nv->gridunit));
395     _rsu_ox.setValue (val);
396     val = nv->gridorigin[NR::Y];
397     val = sp_pixels_get_units (val, *(nv->gridunit));
398     _rsu_oy.setValue (val);
399     val = nv->gridspacing[NR::X];
400     double gridx = sp_pixels_get_units (val, *(nv->gridunit));
401     _rsu_sx.setValue (gridx);
402     val = nv->gridspacing[NR::Y];
403     double gridy = sp_pixels_get_units (val, *(nv->gridunit));
404     _rsu_sy.setValue (gridy);
406     _rcp_gcol.setRgba32 (nv->gridcolor);
407     _rcp_gmcol.setRgba32 (nv->gridempcolor);
408     _rsi.setValue (nv->gridempspacing);
410     //-----------------------------------------------------------guide
411     _rcb_sgui.setActive (nv->showguides);
412     _rcp_gui.setRgba32 (nv->guidecolor);
413     _rcp_hgui.setRgba32 (nv->guidehicolor);
415     //-----------------------------------------------------------snap
416     _rcbsnbo.setActive (nv->snap_manager.object.getSnapTo(Inkscape::Snapper::BBOX_POINT));
417     _rcbsnnob.setActive (nv->snap_manager.object.getSnapTo(Inkscape::Snapper::SNAP_POINT));
418     _rcbsnop.setActive (nv->snap_manager.object.getSnapToPaths());
419     _rcbsnop.setActive (nv->snap_manager.object.getSnapToNodes());
420     _rsu_sno.setValue (nv->objecttolerance, nv->has_abs_tolerance);
421      
422     _rcbsnbb.setActive (nv->snap_manager.grid.getSnapTo(Inkscape::Snapper::BBOX_POINT));
423     _rcbsnnod.setActive (nv->snap_manager.grid.getSnapTo(Inkscape::Snapper::SNAP_POINT));
424     _rsu_sn.setValue (nv->gridtolerance, nv->has_abs_tolerance);
425     
426     _rcb_snpgui.setActive (nv->snap_manager.guide.getSnapTo(Inkscape::Snapper::BBOX_POINT));
427     _rcb_snbgui.setActive (nv->snap_manager.guide.getSnapTo(Inkscape::Snapper::SNAP_POINT));
428     _rsu_gusn.setValue (nv->guidetolerance, nv->has_abs_tolerance);
429 //    _rrb_pix.setValue (true);
431     _wr.setUpdating (false);
434 //--------------------------------------------------------------------
436 void
437 DocumentProperties::on_response (int id)
439     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
440     {
441         _rcp_bg.closeWindow();
442         _rcp_bord.closeWindow();
443         _rcp_gcol.closeWindow();
444         _rcp_gmcol.closeWindow();
445         _rcp_gui.closeWindow();
446         _rcp_hgui.closeWindow();
447     } 
448     
449     if (id == Gtk::RESPONSE_CLOSE)
450         hide();
453 /**
454  * Called when XML node attribute changed; updates dialog widgets.
455  */
456 static void
457 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer)
459     if (!_instance)
460         return;
462     _instance->update();
465 static void 
466 on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
468     if (!_instance)
469         return;
471     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
472     repr->addListener (&_repr_events, _instance);
473     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
474     root->addListener (&_repr_events, _instance);
475     _instance->_doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
476     _instance->update();
479 static void 
480 on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
482     if (!_instance)
483         return;
485     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
486     repr->removeListenerByData (_instance);
487     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
488     root->removeListenerByData (_instance);
489     _instance->_doc_replaced_connection.disconnect();
492 static void 
493 on_doc_replaced (SPDesktop* dt, SPDocument* doc)
495     if (!_instance)
496         return;
498     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
499     repr->addListener (&_repr_events, _instance);
500     Inkscape::XML::Node *root = SP_OBJECT_REPR(doc->root);
501     root->addListener (&_repr_events, _instance);
502     _instance->update();
506 } // namespace Dialog
507 } // namespace UI
508 } // namespace Inkscape
510 /*
511   Local Variables:
512   mode:c++
513   c-file-style:"stroustrup"
514   c-file-offsets:((innamespace . 0)(inline-open . 0))
515   indent-tabs-mode:nil
516   fill-column:99
517   End:
518 */
519 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :