Code

Moved EventLog from SPDocument to SPDesktop to prevent it from being
[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"
31 #include "inkscape.h"
32 #include "verbs.h"
33 #include "document.h"
34 #include "desktop-handles.h"
35 #include "desktop.h"
36 #include "sp-namedview.h"
38 #include "document-properties.h"
40 using std::pair;
42 namespace Inkscape {
43 namespace UI {
44 namespace Dialog {
46 #define SPACE_SIZE_X 15
47 #define SPACE_SIZE_Y 10
49 //===================================================
51 //---------------------------------------------------
53 static DocumentProperties *_instance = 0;
55 static void on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer);
56 static void on_doc_replaced (SPDesktop* dt, SPDocument* doc);
57 static void on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*);
58 static void on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*);
60 static Inkscape::XML::NodeEventVector const _repr_events = {
61     NULL, /* child_added */
62     NULL, /* child_removed */
63     on_repr_attr_changed,
64     NULL, /* content_changed */
65     NULL  /* order_changed */
66 };
69 DocumentProperties*
70 DocumentProperties::create()
71 {
72     if (_instance) return _instance;
73     _instance = new DocumentProperties;
74     _instance->init();
75     return _instance;
76 }
78 void
79 DocumentProperties::destroy()
80 {
81     if (_instance)
82     {
83         delete _instance;
84         _instance = 0;
85     }
86 }
88 DocumentProperties::DocumentProperties() 
89     : Dialog ("dialogs.documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
90       _page_page(1, 1), _page_grid(1, 1), _page_guides(1, 1),
91       _page_snap(1, 1), 
92       _prefs_path("dialogs.documentoptions")
93 {
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     _rrb_gridtype.init (_("Grid type:"), _("Normal (2D)"), _("Axonometric (3D)"),
236                 _("The normal grid with vertical and horizontal lines."),
237                 _("A grid with vertical lines and two diagonal line groups, each representing the projection of a primary axis."),
238                 _("gridtype"), _wr);
239     
240     _rumg.init (_("Grid _units:"), "grid_units", _wr);
241     _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"), 
242                   "gridoriginx", _rumg, _wr);
243     _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"), 
244                   "gridoriginy", _rumg, _wr);
245     _rsu_sx.init (_("Spacing _X:"), _("Distance of vertical grid lines"), 
246                   "gridspacingx", _rumg, _wr);
247     _rsu_sy.init (_("Spacing _Y:"), _("Distance of horizontal grid lines"), 
248                   "gridspacingy", _rumg, _wr);
249     _rsu_ax.init (_("Angle X:"), _("Angle of x-axis of axonometric grid"), 
250                   "gridanglex", _rumg, _wr);
251     _rsu_az.init (_("Angle Z:"), _("Angle of z-axis of axonometric grid"), 
252                   "gridanglez", _rumg, _wr);
253     _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"), 
254                     _("Color of grid lines"), "gridcolor", "gridopacity", _wr);
255     _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"), 
256                      _("Color of the major (highlighted) grid lines"), 
257                      "gridempcolor", "gridempopacity", _wr);
258     _rsi.init (_("_Major grid line every:"), _("lines"), "gridempspacing", _wr);
259     _rcb_sgui.init (_("Show _guides"), _("Show or hide guides"), "showguides", _wr);
260     _rcp_gui.init (_("Guide co_lor:"), _("Guideline color"), 
261                    _("Color of guidelines"), "guidecolor", "guideopacity", _wr);
262     _rcp_hgui.init (_("_Highlight color:"), _("Highlighted guideline color"), 
263                     _("Color of a guideline when it is under mouse"),
264                     "guidehicolor", "guidehiopacity", _wr);
265     Gtk::Label *label_grid = manage (new Gtk::Label);
266     label_grid->set_markup (_("<b>Grid</b>"));
267     Gtk::Label *label_gui = manage (new Gtk::Label);
268     label_gui->set_markup (_("<b>Guides</b>"));
270     const Gtk::Widget* widget_array[] = 
271     {
272         label_grid,         0,
273         0,                  _rcbgrid._button,
274         0,                  _rrb_gridtype._hbox, 
275         _rumg._label,       _rumg._sel,
276         0,                  _rsu_ox.getSU(),
277         0,                  _rsu_oy.getSU(),
278         0,                  _rsu_sx.getSU(),
279         0,                  _rsu_sy.getSU(),
280         0,                  _rsu_ax.getSU(),
281         0,                  _rsu_az.getSU(),
282         _rcp_gcol._label,   _rcp_gcol._cp, 
283         0,                  0,
284         _rcp_gmcol._label,  _rcp_gmcol._cp,
285         _rsi._label,        &_rsi._hbox,
286         0, 0,
287         label_gui,       0,
288         0,               _rcb_sgui._button,
289         _rcp_gui._label, _rcp_gui._cp,
290         _rcp_hgui._label, _rcp_hgui._cp,
291     };
293     attach_all (_page_grid.table(), widget_array, sizeof(widget_array));
296 void
297 DocumentProperties::build_snap()
299     _page_snap.show();
301     _rcbsnbo.init (_("_Snap bounding boxes to objects"), 
302                 _("Snap the edges of the object bounding boxes to other objects"), 
303                 "inkscape:object-bbox", _wr);
304     _rcbsnnob.init (_("Snap nodes _to objects"), 
305                 _("Snap the nodes of objects to other objects"), 
306                 "inkscape:object-points", _wr);
307     _rcbsnop.init (_("Snap to object _paths"), 
308                 _("Snap to other object paths"), 
309                 "inkscape:object-paths", _wr);
310     _rcbsnon.init (_("Snap to object _nodes"), 
311                 _("Snap to other object nodes"), 
312                 "inkscape:object-nodes", _wr);
313     _rsu_sno.init (_("Snap s_ensitivity:"), _("Always snap"),
314                   _("Controls max. snapping distance from object"),
315                   _("If set, objects snap to the nearest object when moved, regardless of distance"),
316                   "objecttolerance", _wr);
317     _rcbsnbb.init (_("Snap _bounding boxes to grid"), 
318                 _("Snap the edges of the object bounding boxes"), 
319                 "inkscape:grid-bbox", _wr);
320     _rcbsnnod.init (_("Snap nodes to _grid"), 
321                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
322                 "inkscape:grid-points", _wr);
323     _rsu_sn.init (_("Snap sens_itivity:"), _("Always snap"),
324                   _("Controls max. snapping distance from grid"),
325                   _("If set, objects snap to the nearest grid line when moved, regardless of distance"),
326                   "gridtolerance", _wr);
327     _rcb_snpgui.init (_("Snap bounding boxes to g_uides"),  
328                      _("Snap the edges of the object bounding boxes"), 
329                      "inkscape:guide-bbox", _wr);
330     _rcb_snbgui.init (_("Snap p_oints to guides"), 
331                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
332                 "inkscape:guide-points", _wr);
333     _rsu_gusn.init (_("Snap sensiti_vity:"), _("Always snap"),
334                 _("Controls max. snapping distance from guides"), 
335                 _("If set, objects snap to the nearest guide when moved, regardless of distance"),
336                 "guidetolerance", _wr);
337 //    _rrb_pix.init (_("Sensitivity:"), _("S_creen pixels"), _("p_x units"),
338 //                _("Sensitivity is always the same, regardless of zoom."),
339 //                _("Sensitivity changes with zoom; zooming in will enlarge max. snapping distance."),
340 //                _("inkscape:has_abs_tolerance"), _wr);
341     Gtk::Label *label_o = manage (new Gtk::Label);
342     label_o->set_markup (_("<b>Object Snapping</b>"));
343     Gtk::Label *label_gr = manage (new Gtk::Label);
344     label_gr->set_markup (_("<b>Grid Snapping</b>"));
345     Gtk::Label *label_gu = manage (new Gtk::Label);
346     label_gu->set_markup (_("<b>Guide Snapping</b>"));
347      
348     const Gtk::Widget* array[] = 
349     {
350         label_o,            0,
351         0,                  _rcbsnbo._button,
352         0,                  _rcbsnnob._button,
353         0,                  _rcbsnop._button,
354         0,                  _rcbsnon._button,
355         0,                  _rsu_sno._vbox,
356         0, 0,
357         label_gr,           0,
358         0,                  _rcbsnbb._button,
359         0,                  _rcbsnnod._button,
360         0,                  _rsu_sn._vbox,
361         0, 0,
362         label_gu,         0,
363         0,                _rcb_snpgui._button,
364         0,                _rcb_snbgui._button,
365         0,                _rsu_gusn._vbox,
366 //        0, 0,
367 //        0,                _rrb_pix._hbox,
368     };
370     attach_all (_page_snap.table(), array, sizeof(array));
371  }
373 /**
374  * Update dialog widgets from desktop.
375  */
376 void
377 DocumentProperties::update()
379     if (_wr.isUpdating()) return;
380     
381     SPDesktop *dt = SP_ACTIVE_DESKTOP;
382     SPNamedView *nv = sp_desktop_namedview(dt);
383     _wr.setUpdating (true);
384     set_sensitive (true);
386     //-----------------------------------------------------------page page
387     _rcp_bg.setRgba32 (nv->pagecolor);
388     _rcb_canb.setActive (nv->showborder);
389     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
390     _rcp_bord.setRgba32 (nv->bordercolor);
391     _rcb_shad.setActive (nv->showpageshadow);
392     
393     if (nv->doc_units) 
394         _rum_deflt.setUnit (nv->doc_units);
396     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
397     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
398     _page_sizer.setDim (doc_w_px, doc_h_px);
400     //-----------------------------------------------------------grid page
401     _rcbgrid.setActive (nv->showgrid);
402     _rrb_gridtype.setValue (nv->gridtype);
403     _rumg.setUnit (nv->gridunit);
404     
405     gdouble val;
406     val = nv->gridorigin[NR::X];
407     val = sp_pixels_get_units (val, *(nv->gridunit));
408     _rsu_ox.setValue (val);
409     val = nv->gridorigin[NR::Y];
410     val = sp_pixels_get_units (val, *(nv->gridunit));
411     _rsu_oy.setValue (val);
412     val = nv->gridspacing[NR::X];
413     double gridx = sp_pixels_get_units (val, *(nv->gridunit));
414     _rsu_sx.setValue (gridx);
415     val = nv->gridspacing[NR::Y];
416     double gridy = sp_pixels_get_units (val, *(nv->gridunit));
417     _rsu_sy.setValue (gridy);
419     val = nv->gridangle[0];
420     _rsu_ax.setValue (val);
421     val = nv->gridangle[1];
422     _rsu_az.setValue (val);
424     _rcp_gcol.setRgba32 (nv->gridcolor);
425     _rcp_gmcol.setRgba32 (nv->gridempcolor);
426     _rsi.setValue (nv->gridempspacing);
428     //-----------------------------------------------------------guide
429     _rcb_sgui.setActive (nv->showguides);
430     _rcp_gui.setRgba32 (nv->guidecolor);
431     _rcp_hgui.setRgba32 (nv->guidehicolor);
433     //-----------------------------------------------------------snap
434     _rcbsnbo.setActive (nv->snap_manager.object.getSnapTo(Inkscape::Snapper::BBOX_POINT));
435     _rcbsnnob.setActive (nv->snap_manager.object.getSnapTo(Inkscape::Snapper::SNAP_POINT));
436     _rcbsnop.setActive (nv->snap_manager.object.getSnapToPaths());
437     _rcbsnop.setActive (nv->snap_manager.object.getSnapToNodes());
438     _rsu_sno.setValue (nv->objecttolerance, nv->has_abs_tolerance);
439      
440     _rcbsnbb.setActive (nv->snap_manager.grid.getSnapTo(Inkscape::Snapper::BBOX_POINT));
441     _rcbsnnod.setActive (nv->snap_manager.grid.getSnapTo(Inkscape::Snapper::SNAP_POINT));
442     _rsu_sn.setValue (nv->gridtolerance, nv->has_abs_tolerance);
443     
444     _rcb_snpgui.setActive (nv->snap_manager.guide.getSnapTo(Inkscape::Snapper::BBOX_POINT));
445     _rcb_snbgui.setActive (nv->snap_manager.guide.getSnapTo(Inkscape::Snapper::SNAP_POINT));
446     _rsu_gusn.setValue (nv->guidetolerance, nv->has_abs_tolerance);
447 //    _rrb_pix.setValue (true);
449     _wr.setUpdating (false);
452 //--------------------------------------------------------------------
454 void
455 DocumentProperties::on_response (int id)
457     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
458     {
459         _rcp_bg.closeWindow();
460         _rcp_bord.closeWindow();
461         _rcp_gcol.closeWindow();
462         _rcp_gmcol.closeWindow();
463         _rcp_gui.closeWindow();
464         _rcp_hgui.closeWindow();
465     } 
466     
467     if (id == Gtk::RESPONSE_CLOSE)
468         hide();
471 /**
472  * Called when XML node attribute changed; updates dialog widgets.
473  */
474 static void
475 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer)
477     if (!_instance)
478         return;
480     _instance->update();
483 static void 
484 on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
486     if (!_instance)
487         return;
489     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
490     repr->addListener (&_repr_events, _instance);
491     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
492     root->addListener (&_repr_events, _instance);
493     _instance->_doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
494     _instance->update();
497 static void 
498 on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
500     if (!_instance)
501         return;
503     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
504     repr->removeListenerByData (_instance);
505     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
506     root->removeListenerByData (_instance);
507     _instance->_doc_replaced_connection.disconnect();
510 static void 
511 on_doc_replaced (SPDesktop* dt, SPDocument* doc)
513     if (!_instance)
514         return;
516     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
517     repr->addListener (&_repr_events, _instance);
518     Inkscape::XML::Node *root = SP_OBJECT_REPR(doc->root);
519     root->addListener (&_repr_events, _instance);
520     _instance->update();
524 } // namespace Dialog
525 } // namespace UI
526 } // namespace Inkscape
528 /*
529   Local Variables:
530   mode:c++
531   c-file-style:"stroustrup"
532   c-file-offsets:((innamespace . 0)(inline-open . 0))
533   indent-tabs-mode:nil
534   fill-column:99
535   End:
536 */
537 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :