Code

refactoring, add background removal
[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     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_desktop_namedview(SP_ACTIVE_DESKTOP));
113     repr->addListener (&_repr_events, this);
114     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
115     root->addListener (&_repr_events, this);
117     _doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
119     g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
120                      G_CALLBACK(on_activate_desktop), 0);
121     
122     g_signal_connect(G_OBJECT(INKSCAPE), "deactivate_desktop",
123                      G_CALLBACK(on_deactivate_desktop), 0);
124     
125     show_all_children();
126     present();
129 DocumentProperties::~DocumentProperties() 
131     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
132     repr->removeListenerByData (this);
133     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
134     root->removeListenerByData (this);
135     _doc_replaced_connection.disconnect();
138 //========================================================================
140 /**
141  * Helper function that attachs widgets in a 3xn table. The widgets come in an
142  * array that has two entries per table row. The two entries code for four
143  * possible cases: (0,0) means insert space in first column; (0, non-0) means
144  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
145  * (non-0, non-0) means two widgets in columns 2 and 3.
146 **/
147 inline void
148 attach_all (Gtk::Table &table, const Gtk::Widget *arr[], unsigned size, int start = 0)
150     for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2)
151     {
152         if (arr[i] && arr[i+1])
153         {
154             table.attach (const_cast<Gtk::Widget&>(*arr[i]),   1, 2, r, r+1, 
155                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
156             table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1, 
157                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
158         }
159         else
160         {
161             if (arr[i+1])
162                 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1, 
163                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
164             else if (arr[i])
165             {
166                 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
167                 label.set_alignment (0.0);
168                 table.attach (label, 0, 3, r, r+1, 
169                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
170             }
171             else
172             {
173                 Gtk::HBox *space = manage (new Gtk::HBox);
174                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
175                 table.attach (*space, 0, 1, r, r+1, 
176                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
177             }
178         }
179         ++r;
180     }
183 void
184 DocumentProperties::build_page()
186     _page_page.show();
188     _rcp_bg.init (_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"),
189                    "pagecolor", "inkscape:pageopacity", _wr);
190     _rcb_canb.init (_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false);
191     _rcb_bord.init (_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false);
192     _rcp_bord.init (_("Border _color:"), _("Page border color"),
193                     _("Color of the page border"),
194                     "bordercolor", "borderopacity", _wr);
195     _rcb_shad.init (_("_Show border shadow"), _("If set, page border shows a shadow on its right and lower side"), "inkscape:showpageshadow", _wr, false);
196     _rum_deflt.init (_("Default _units:"), "inkscape:document-units", _wr);
198     Gtk::Label* label_gen = manage (new Gtk::Label);
199     label_gen->set_markup (_("<b>General</b>"));
200     Gtk::Label* label_bor = manage (new Gtk::Label);
201     label_bor->set_markup (_("<b>Border</b>"));
202     Gtk::Label *label_for = manage (new Gtk::Label);
203     label_for->set_markup (_("<b>Format</b>"));
204     _page_sizer.init (_wr);
206     const Gtk::Widget* widget_array[] = 
207     {
208         label_gen,         0,
209         _rum_deflt._label, _rum_deflt._sel,
210         _rcp_bg._label,    _rcp_bg._cp,
211         0,                 0,
212         label_for,         0,
213         0,                 &_page_sizer,
214         0,                 0,
215         label_bor,         0,
216         0,                 _rcb_canb._button,
217         0,                 _rcb_bord._button,
218         0,                 _rcb_shad._button,
219         _rcp_bord._label,  _rcp_bord._cp,
220     };
221     
222     attach_all (_page_page.table(), widget_array, sizeof(widget_array));
225 void
226 DocumentProperties::build_grid()
228     _page_grid.show();
230     /// \todo FIXME: gray out snapping when grid is off.
231     /// Dissenting view: you want snapping without grid.
232     
233     _rcbgrid.init (_("_Show grid"), _("Show or hide grid"), "showgrid", _wr);
234     _rumg.init (_("Grid _units:"), "grid_units", _wr);
235     _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"), 
236                   "gridoriginx", _rumg, _wr);
237     _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"), 
238                   "gridoriginy", _rumg, _wr);
239     _rsu_sx.init (_("Spacing _X:"), _("Distance of vertical grid lines"), 
240                   "gridspacingx", _rumg, _wr);
241     _rsu_sy.init (_("Spacing _Y:"), _("Distance of horizontal grid lines"), 
242                   "gridspacingy", _rumg, _wr);
243     _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"), 
244                     _("Color of grid lines"), "gridcolor", "gridopacity", _wr);
245     _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"), 
246                      _("Color of the major (highlighted) grid lines"), 
247                      "gridempcolor", "gridempopacity", _wr);
248     _rsi.init (_("_Major grid line every:"), _("lines"), "gridempspacing", _wr);
249     _rcb_sgui.init (_("Show _guides"), _("Show or hide guides"), "showguides", _wr);
250     _rcp_gui.init (_("Guide co_lor:"), _("Guideline color"), 
251                    _("Color of guidelines"), "guidecolor", "guideopacity", _wr);
252     _rcp_hgui.init (_("_Highlight color:"), _("Highlighted guideline color"), 
253                     _("Color of a guideline when it is under mouse"),
254                     "guidehicolor", "guidehiopacity", _wr);
255     Gtk::Label *label_grid = manage (new Gtk::Label);
256     label_grid->set_markup (_("<b>Grid</b>"));
257     Gtk::Label *label_gui = manage (new Gtk::Label);
258     label_gui->set_markup (_("<b>Guides</b>"));
260     const Gtk::Widget* widget_array[] = 
261     {
262         label_grid,         0,
263         0,                  _rcbgrid._button,
264         _rumg._label,       _rumg._sel,
265         0,                  _rsu_ox.getSU(),
266         0,                  _rsu_oy.getSU(),
267         0,                  _rsu_sx.getSU(),
268         0,                  _rsu_sy.getSU(),
269         _rcp_gcol._label,   _rcp_gcol._cp, 
270         0,                  0,
271         _rcp_gmcol._label,  _rcp_gmcol._cp,
272         _rsi._label,        &_rsi._hbox,
273         0, 0,
274         label_gui,       0,
275         0,               _rcb_sgui._button,
276         _rcp_gui._label, _rcp_gui._cp,
277         _rcp_hgui._label, _rcp_hgui._cp,
278     };
280     attach_all (_page_grid.table(), widget_array, sizeof(widget_array));
283 void
284 DocumentProperties::build_snap()
286     _page_snap.show();
288     _rcbsnbo.init (_("_Snap bounding boxes to objects"), 
289                 _("Snap the edges of the object bounding boxes to other objects"), 
290                 "inkscape:object-bbox", _wr);
291     _rcbsnnob.init (_("Snap nodes _to objects"), 
292                 _("Snap the nodes of objects to other objects"), 
293                 "inkscape:object-points", _wr);
294     _rcbsnop.init (_("Snap to object _paths"), 
295                 _("Snap to other object paths"), 
296                 "inkscape:object-paths", _wr);
297     _rcbsnon.init (_("Snap to object _nodes"), 
298                 _("Snap to other object nodes"), 
299                 "inkscape:object-nodes", _wr);
300     _rsu_sno.init (_("Snap s_ensitivity:"), _("Always snap"),
301                   _("Controls max. snapping distance from object"),
302                   _("If set, objects snap to the nearest object when moved, regardless of distance"),
303                   "objecttolerance", _wr);
304     _rcbsnbb.init (_("Snap _bounding boxes to grid"), 
305                 _("Snap the edges of the object bounding boxes"), 
306                 "inkscape:grid-bbox", _wr);
307     _rcbsnnod.init (_("Snap nodes to _grid"), 
308                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
309                 "inkscape:grid-points", _wr);
310     _rsu_sn.init (_("Snap sens_itivity:"), _("Always snap"),
311                   _("Controls max. snapping distance from grid"),
312                   _("If set, objects snap to the nearest grid line when moved, regardless of distance"),
313                   "gridtolerance", _wr);
314     _rcb_snpgui.init (_("Snap bounding boxes to g_uides"),  
315                      _("Snap the edges of the object bounding boxes"), 
316                      "inkscape:guide-bbox", _wr);
317     _rcb_snbgui.init (_("Snap p_oints to guides"), 
318                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
319                 "inkscape:guide-points", _wr);
320     _rsu_gusn.init (_("Snap sensiti_vity:"), _("Always snap"),
321                 _("Controls max. snapping distance from guides"), 
322                 _("If set, objects snap to the nearest guide when moved, regardless of distance"),
323                 "guidetolerance", _wr);
324 //    _rrb_pix.init (_("Sensitivity:"), _("S_creen pixels"), _("p_x units"),
325 //                _("Sensitivity is always the same, regardless of zoom."),
326 //                _("Sensitivity changes with zoom; zooming in will enlarge max. snapping distance."),
327 //                _("inkscape:has_abs_tolerance"), _wr);
328     Gtk::Label *label_o = manage (new Gtk::Label);
329     label_o->set_markup (_("<b>Object Snapping</b>"));
330     Gtk::Label *label_gr = manage (new Gtk::Label);
331     label_gr->set_markup (_("<b>Grid Snapping</b>"));
332     Gtk::Label *label_gu = manage (new Gtk::Label);
333     label_gu->set_markup (_("<b>Guide Snapping</b>"));
334      
335     const Gtk::Widget* array[] = 
336     {
337         label_o,            0,
338         0,                  _rcbsnbo._button,
339         0,                  _rcbsnnob._button,
340         0,                  _rcbsnop._button,
341         0,                  _rcbsnon._button,
342         0,                  _rsu_sno._vbox,
343         0, 0,
344         label_gr,           0,
345         0,                  _rcbsnbb._button,
346         0,                  _rcbsnnod._button,
347         0,                  _rsu_sn._vbox,
348         0, 0,
349         label_gu,         0,
350         0,                _rcb_snpgui._button,
351         0,                _rcb_snbgui._button,
352         0,                _rsu_gusn._vbox,
353 //        0, 0,
354 //        0,                _rrb_pix._hbox,
355     };
357     attach_all (_page_snap.table(), array, sizeof(array));
358  }
360 /**
361  * Update dialog widgets from desktop.
362  */
363 void
364 DocumentProperties::update()
366     if (_wr.isUpdating()) return;
367     
368     SPDesktop *dt = SP_ACTIVE_DESKTOP;
369     SPNamedView *nv = sp_desktop_namedview(dt);
370     _wr.setUpdating (true);
371     set_sensitive (true);
373     //-----------------------------------------------------------page page
374     _rcp_bg.setRgba32 (nv->pagecolor);
375     _rcb_canb.setActive (nv->showborder);
376     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
377     _rcp_bord.setRgba32 (nv->bordercolor);
378     _rcb_shad.setActive (nv->showpageshadow);
379     
380     if (nv->doc_units) 
381         _rum_deflt.setUnit (nv->doc_units);
383     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
384     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
385     _page_sizer.setDim (doc_w_px, doc_h_px);
387     //-----------------------------------------------------------grid page
388     _rcbgrid.setActive (nv->showgrid);
389     _rumg.setUnit (nv->gridunit);
390     
391     gdouble val;
392     val = nv->gridorigin[NR::X];
393     val = sp_pixels_get_units (val, *(nv->gridunit));
394     _rsu_ox.setValue (val);
395     val = nv->gridorigin[NR::Y];
396     val = sp_pixels_get_units (val, *(nv->gridunit));
397     _rsu_oy.setValue (val);
398     val = nv->gridspacing[NR::X];
399     double gridx = sp_pixels_get_units (val, *(nv->gridunit));
400     _rsu_sx.setValue (gridx);
401     val = nv->gridspacing[NR::Y];
402     double gridy = sp_pixels_get_units (val, *(nv->gridunit));
403     _rsu_sy.setValue (gridy);
405     _rcp_gcol.setRgba32 (nv->gridcolor);
406     _rcp_gmcol.setRgba32 (nv->gridempcolor);
407     _rsi.setValue (nv->gridempspacing);
409     //-----------------------------------------------------------guide
410     _rcb_sgui.setActive (nv->showguides);
411     _rcp_gui.setRgba32 (nv->guidecolor);
412     _rcp_hgui.setRgba32 (nv->guidehicolor);
414     //-----------------------------------------------------------snap
415     _rcbsnbo.setActive (nv->snap_manager.object.getSnapTo(Inkscape::Snapper::BBOX_POINT));
416     _rcbsnnob.setActive (nv->snap_manager.object.getSnapTo(Inkscape::Snapper::SNAP_POINT));
417     _rcbsnop.setActive (nv->snap_manager.object.getSnapToPaths());
418     _rcbsnop.setActive (nv->snap_manager.object.getSnapToNodes());
419     _rsu_sno.setValue (nv->objecttolerance, nv->has_abs_tolerance);
420      
421     _rcbsnbb.setActive (nv->snap_manager.grid.getSnapTo(Inkscape::Snapper::BBOX_POINT));
422     _rcbsnnod.setActive (nv->snap_manager.grid.getSnapTo(Inkscape::Snapper::SNAP_POINT));
423     _rsu_sn.setValue (nv->gridtolerance, nv->has_abs_tolerance);
424     
425     _rcb_snpgui.setActive (nv->snap_manager.guide.getSnapTo(Inkscape::Snapper::BBOX_POINT));
426     _rcb_snbgui.setActive (nv->snap_manager.guide.getSnapTo(Inkscape::Snapper::SNAP_POINT));
427     _rsu_gusn.setValue (nv->guidetolerance, nv->has_abs_tolerance);
428 //    _rrb_pix.setValue (true);
430     _wr.setUpdating (false);
433 //--------------------------------------------------------------------
435 void
436 DocumentProperties::on_response (int id)
438     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
439     {
440         _rcp_bg.closeWindow();
441         _rcp_bord.closeWindow();
442         _rcp_gcol.closeWindow();
443         _rcp_gmcol.closeWindow();
444         _rcp_gui.closeWindow();
445         _rcp_hgui.closeWindow();
446     } 
447     
448     if (id == Gtk::RESPONSE_CLOSE)
449         hide();
452 /**
453  * Called when XML node attribute changed; updates dialog widgets.
454  */
455 static void
456 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer)
458     if (!_instance)
459         return;
461     _instance->update();
464 static void 
465 on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
467     if (!_instance)
468         return;
470     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
471     repr->addListener (&_repr_events, _instance);
472     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
473     root->addListener (&_repr_events, _instance);
474     _instance->_doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
475     _instance->update();
478 static void 
479 on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
481     if (!_instance)
482         return;
484     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
485     repr->removeListenerByData (_instance);
486     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(SP_ACTIVE_DESKTOP)->root);
487     root->removeListenerByData (_instance);
488     _instance->_doc_replaced_connection.disconnect();
491 static void 
492 on_doc_replaced (SPDesktop* dt, SPDocument* doc)
494     if (!_instance)
495         return;
497     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
498     repr->addListener (&_repr_events, _instance);
499     Inkscape::XML::Node *root = SP_OBJECT_REPR(doc->root);
500     root->addListener (&_repr_events, _instance);
501     _instance->update();
505 } // namespace Dialog
506 } // namespace UI
507 } // namespace Inkscape
509 /*
510   Local Variables:
511   mode:c++
512   c-file-style:"stroustrup"
513   c-file-offsets:((innamespace . 0)(inline-open . 0))
514   indent-tabs-mode:nil
515   fill-column:99
516   End:
517 */
518 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :