Code

9a9deac9f5f7d0fac880757285c774bccdae7465
[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"
25 #include "ui/widget/button.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"
36 #include "helper/action.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 void fire_fit_canvas_to_selection_or_drawing();
62 static Inkscape::XML::NodeEventVector const _repr_events = {
63     NULL, /* child_added */
64     NULL, /* child_removed */
65     on_repr_attr_changed,
66     NULL, /* content_changed */
67     NULL  /* order_changed */
68 };
71 DocumentProperties*
72 DocumentProperties::create()
73 {
74     if (_instance) return _instance;
75     _instance = new DocumentProperties;
76     _instance->init();
77     return _instance;
78 }
80 void
81 DocumentProperties::destroy()
82 {
83     if (_instance)
84     {
85         delete _instance;
86         _instance = 0;
87     }
88 }
90 DocumentProperties::DocumentProperties() 
91     : Dialog ("dialogs.documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
92       _page_page(1, 1), _page_grid(1, 1), _page_guides(1, 1),
93       _page_snap(1, 1), 
94       _prefs_path("dialogs.documentoptions")
95 {
96     hide();
97     set_resizable (false);
98     _tt.enable();
99     get_vbox()->set_spacing (4);
100     get_vbox()->pack_start (_notebook, true, true);
102     _notebook.append_page(_page_page,      _("Page"));
103     _notebook.append_page(_page_grid,      _("Grid/Guides"));
104     _notebook.append_page(_page_snap,      _("Snap"));
106     build_page();
107     build_grid();
108     build_snap();
111 void
112 DocumentProperties::init()
114     update();
116     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
117     repr->addListener (&_repr_events, this);
119     _doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
121     g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
122                      G_CALLBACK(on_activate_desktop), 0);
123     
124     g_signal_connect(G_OBJECT(INKSCAPE), "deactivate_desktop",
125                      G_CALLBACK(on_deactivate_desktop), 0);
126     
127     show_all_children();
128     present();
131 DocumentProperties::~DocumentProperties() 
133     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
134     repr->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);
197     Inkscape::UI::Widget::Button* fit_canv = manage(new Inkscape::UI::Widget::Button(_("Fit canvas to selection"),
198                     _("Fit the canvas to the current selection or fit to the drawing when there is no selection.")));
199     fit_canv->signal_clicked().connect(sigc::ptr_fun(fire_fit_canvas_to_selection_or_drawing));
200     Gtk::Label* label_gen = manage (new Gtk::Label);
201     label_gen->set_markup (_("<b>General</b>"));
202     Gtk::Label* label_bor = manage (new Gtk::Label);
203     label_bor->set_markup (_("<b>Border</b>"));
204     Gtk::Label *label_for = manage (new Gtk::Label);
205     label_for->set_markup (_("<b>Format</b>"));
206     _page_sizer.init (_wr);
208     const Gtk::Widget* widget_array[] = 
209     {
210         label_gen,         0,
211         _rum_deflt._label, _rum_deflt._sel,
212         _rcp_bg._label,    _rcp_bg._cp,
213         0,                 0,
214         label_for,         0,
215         0,                 &_page_sizer,
216         0,                 fit_canv,
217         0,                 0,
218         label_bor,         0,
219         0,                 _rcb_canb._button,
220         0,                 _rcb_bord._button,
221         0,                 _rcb_shad._button,
222         _rcp_bord._label,  _rcp_bord._cp,
223     };
224     
225     attach_all (_page_page.table(), widget_array, sizeof(widget_array));
228 void
229 DocumentProperties::build_grid()
231     _page_grid.show();
233     /// \todo FIXME: gray out snapping when grid is off.
234     /// Dissenting view: you want snapping without grid.
235     
236     _rcbgrid.init (_("_Show grid"), _("Show or hide grid"), "showgrid", _wr);
237     _rumg.init (_("Grid _units:"), "grid_units", _wr);
238     _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"), 
239                   "gridoriginx", _rumg, _wr);
240     _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"), 
241                   "gridoriginy", _rumg, _wr);
242     _rsu_sx.init (_("Spacing _X:"), _("Distance of vertical grid lines"), 
243                   "gridspacingx", _rumg, _wr);
244     _rsu_sy.init (_("Spacing _Y:"), _("Distance of horizontal grid lines"), 
245                   "gridspacingy", _rumg, _wr);
246     _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"), 
247                     _("Color of grid lines"), "gridcolor", "gridopacity", _wr);
248     _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"), 
249                      _("Color of the major (highlighted) grid lines"), 
250                      "gridempcolor", "gridempopacity", _wr);
251     _rsi.init (_("_Major grid line every:"), _("lines"), "gridempspacing", _wr);
252     _rcb_sgui.init (_("Show _guides"), _("Show or hide guides"), "showguides", _wr);
253     _rcp_gui.init (_("Guide co_lor:"), _("Guideline color"), 
254                    _("Color of guidelines"), "guidecolor", "guideopacity", _wr);
255     _rcp_hgui.init (_("_Highlight color:"), _("Highlighted guideline color"), 
256                     _("Color of a guideline when it is under mouse"),
257                     "guidehicolor", "guidehiopacity", _wr);
258     Gtk::Label *label_grid = manage (new Gtk::Label);
259     label_grid->set_markup (_("<b>Grid</b>"));
260     Gtk::Label *label_gui = manage (new Gtk::Label);
261     label_gui->set_markup (_("<b>Guides</b>"));
263     const Gtk::Widget* widget_array[] = 
264     {
265         label_grid,         0,
266         0,                  _rcbgrid._button,
267         _rumg._label,       _rumg._sel,
268         0,                  _rsu_ox.getSU(),
269         0,                  _rsu_oy.getSU(),
270         0,                  _rsu_sx.getSU(),
271         0,                  _rsu_sy.getSU(),
272         _rcp_gcol._label,   _rcp_gcol._cp, 
273         0,                  0,
274         _rcp_gmcol._label,  _rcp_gmcol._cp,
275         _rsi._label,        &_rsi._hbox,
276         0, 0,
277         label_gui,       0,
278         0,               _rcb_sgui._button,
279         _rcp_gui._label, _rcp_gui._cp,
280         _rcp_hgui._label, _rcp_hgui._cp,
281     };
283     attach_all (_page_grid.table(), widget_array, sizeof(widget_array));
286 void
287 DocumentProperties::build_snap()
289     _page_snap.show();
291     _rcbsnbo.init (_("_Snap bounding boxes to objects"), 
292                 _("Snap the edges of the object bounding boxes to other objects"), 
293                 "inkscape:object-bbox", _wr);
294     _rcbsnnob.init (_("Snap nodes _to objects"), 
295                 _("Snap the nodes of objects to other objects"), 
296                 "inkscape:object-points", _wr);
297     _rcbsnop.init (_("Snap to object _paths"), 
298                 _("Snap to other object paths"), 
299                 "inkscape:object-paths", _wr);
300     _rcbsnon.init (_("Snap to object _nodes"), 
301                 _("Snap to other object nodes"), 
302                 "inkscape:object-nodes", _wr);
303     _rsu_sno.init (_("Snap s_ensitivity:"), _("Always snap"),
304                   _("Controls max. snapping distance from object"),
305                   _("If set, objects snap to the nearest object when moved, regardless of distance"),
306                   "objecttolerance", _wr);
307     _rcbsnbb.init (_("Snap _bounding boxes to grid"), 
308                 _("Snap the edges of the object bounding boxes"), 
309                 "inkscape:grid-bbox", _wr);
310     _rcbsnnod.init (_("Snap nodes to _grid"), 
311                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
312                 "inkscape:grid-points", _wr);
313     _rsu_sn.init (_("Snap sens_itivity:"), _("Always snap"),
314                   _("Controls max. snapping distance from grid"),
315                   _("If set, objects snap to the nearest grid line when moved, regardless of distance"),
316                   "gridtolerance", _wr);
317     _rcb_snpgui.init (_("Snap bounding boxes to g_uides"),  
318                      _("Snap the edges of the object bounding boxes"), 
319                      "inkscape:guide-bbox", _wr);
320     _rcb_snbgui.init (_("Snap p_oints to guides"), 
321                 _("Snap path nodes, text baselines, ellipse centers, etc."), 
322                 "inkscape:guide-points", _wr);
323     _rsu_gusn.init (_("Snap sensiti_vity:"), _("Always snap"),
324                 _("Controls max. snapping distance from guides"), 
325                 _("If set, objects snap to the nearest guide when moved, regardless of distance"),
326                 "guidetolerance", _wr);
327 //    _rrb_pix.init (_("Sensitivity:"), _("S_creen pixels"), _("p_x units"),
328 //                _("Sensitivity is always the same, regardless of zoom."),
329 //                _("Sensitivity changes with zoom; zooming in will enlarge max. snapping distance."),
330 //                _("inkscape:has_abs_tolerance"), _wr);
331     Gtk::Label *label_o = manage (new Gtk::Label);
332     label_o->set_markup (_("<b>Object Snapping</b>"));
333     Gtk::Label *label_gr = manage (new Gtk::Label);
334     label_gr->set_markup (_("<b>Grid Snapping</b>"));
335     Gtk::Label *label_gu = manage (new Gtk::Label);
336     label_gu->set_markup (_("<b>Guide Snapping</b>"));
337      
338     const Gtk::Widget* array[] = 
339     {
340         label_o,            0,
341         0,                  _rcbsnbo._button,
342         0,                  _rcbsnnob._button,
343         0,                  _rcbsnop._button,
344         0,                  _rcbsnon._button,
345         0,                  _rsu_sno._vbox,
346         0, 0,
347         label_gr,           0,
348         0,                  _rcbsnbb._button,
349         0,                  _rcbsnnod._button,
350         0,                  _rsu_sn._vbox,
351         0, 0,
352         label_gu,         0,
353         0,                _rcb_snpgui._button,
354         0,                _rcb_snbgui._button,
355         0,                _rsu_gusn._vbox,
356 //        0, 0,
357 //        0,                _rrb_pix._hbox,
358     };
360     attach_all (_page_snap.table(), array, sizeof(array));
361  }
363 /**
364  * Update dialog widgets from desktop.
365  */
366 void
367 DocumentProperties::update()
369     if (_wr.isUpdating()) return;
370     
371     SPDesktop *dt = SP_ACTIVE_DESKTOP;
372     SPNamedView *nv = sp_desktop_namedview(dt);
373     _wr.setUpdating (true);
374     set_sensitive (true);
376     //-----------------------------------------------------------page page
377     _rcp_bg.setRgba32 (nv->pagecolor);
378     _rcb_canb.setActive (nv->showborder);
379     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
380     _rcp_bord.setRgba32 (nv->bordercolor);
381     _rcb_shad.setActive (nv->showpageshadow);
382     
383     if (nv->doc_units) 
384         _rum_deflt.setUnit (nv->doc_units);
386     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
387     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
388     _page_sizer.setDim (doc_w_px, doc_h_px);
390     //-----------------------------------------------------------grid page
391     _rcbgrid.setActive (nv->showgrid);
392     _rumg.setUnit (nv->gridunit);
393     
394     gdouble val;
395     val = nv->gridorigin[NR::X];
396     val = sp_pixels_get_units (val, *(nv->gridunit));
397     _rsu_ox.setValue (val);
398     val = nv->gridorigin[NR::Y];
399     val = sp_pixels_get_units (val, *(nv->gridunit));
400     _rsu_oy.setValue (val);
401     val = nv->gridspacing[NR::X];
402     double gridx = sp_pixels_get_units (val, *(nv->gridunit));
403     _rsu_sx.setValue (gridx);
404     val = nv->gridspacing[NR::Y];
405     double gridy = sp_pixels_get_units (val, *(nv->gridunit));
406     _rsu_sy.setValue (gridy);
408     _rcp_gcol.setRgba32 (nv->gridcolor);
409     _rcp_gmcol.setRgba32 (nv->gridempcolor);
410     _rsi.setValue (nv->gridempspacing);
412     //-----------------------------------------------------------guide
413     _rcb_sgui.setActive (nv->showguides);
414     _rcp_gui.setRgba32 (nv->guidecolor);
415     _rcp_hgui.setRgba32 (nv->guidehicolor);
417     //-----------------------------------------------------------snap
418     _rcbsnbo.setActive (nv->snap_object_bbox);
419     _rcbsnnob.setActive (nv->snap_object_point);
420     _rcbsnop.setActive (nv->snap_object_paths);
421     _rcbsnop.setActive (nv->snap_object_nodes);
422     _rsu_sno.setValue (nv->objecttolerance, nv->has_abs_tolerance);
423      
424     _rcbsnbb.setActive (nv->snap_grid_bbox);
425     _rcbsnnod.setActive (nv->snap_grid_point);
426     _rsu_sn.setValue (nv->gridtolerance, nv->has_abs_tolerance);
427     
428      _rcb_snpgui.setActive (nv->snap_guide_bbox);
429     _rcb_snbgui.setActive (nv->snap_guide_point);
430     _rsu_gusn.setValue (nv->guidetolerance, nv->has_abs_tolerance);
431 //    _rrb_pix.setValue (true);
433     _wr.setUpdating (false);
436 //--------------------------------------------------------------------
438 void
439 DocumentProperties::on_response (int id)
441     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
442     {
443         _rcp_bg.closeWindow();
444         _rcp_bord.closeWindow();
445         _rcp_gcol.closeWindow();
446         _rcp_gmcol.closeWindow();
447         _rcp_gui.closeWindow();
448         _rcp_hgui.closeWindow();
449     } 
450     
451     if (id == Gtk::RESPONSE_CLOSE)
452         hide();
455 /**
456  * Called when XML node attribute changed; updates dialog widgets.
457  */
458 static void
459 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer)
461     if (!_instance)
462         return;
464     _instance->update();
467 static void 
468 on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
470     if (!_instance)
471         return;
473     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
474     repr->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     _instance->_doc_replaced_connection.disconnect();
490 static void 
491 on_doc_replaced (SPDesktop* dt, SPDocument* doc)
493     if (!_instance)
494         return;
496     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
497     repr->addListener (&_repr_events, _instance);
498     _instance->update();
501 static void
502 fire_fit_canvas_to_selection_or_drawing() {
503     SPDesktop *dt = SP_ACTIVE_DESKTOP;
504     if (!dt) return;
505     Verb *verb = Verb::get( SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING );
506     if (verb) {
507         SPAction *action = verb->get_action(dt);
508         if (action) {
509             sp_action_perform(action, NULL);        
510         }
511     }
515 } // namespace Dialog
516 } // namespace UI
517 } // namespace Inkscape
519 /*
520   Local Variables:
521   mode:c++
522   c-file-style:"stroustrup"
523   c-file-offsets:((innamespace . 0)(inline-open . 0))
524   indent-tabs-mode:nil
525   fill-column:99
526   End:
527 */
528 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :