4d7b8abc7aaf3315dff19724e674d55d7a7ed573
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"
26 #include "xml/node-event-vector.h"
27 #include "helper/units.h"
29 #include "inkscape.h"
30 #include "verbs.h"
31 #include "document.h"
32 #include "desktop-handles.h"
33 #include "desktop.h"
34 #include "sp-namedview.h"
35 #include "helper/action.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 void fire_fit_canvas_to_selection_or_drawing();
61 static Inkscape::XML::NodeEventVector const _repr_events = {
62 NULL, /* child_added */
63 NULL, /* child_removed */
64 on_repr_attr_changed,
65 NULL, /* content_changed */
66 NULL /* order_changed */
67 };
70 DocumentProperties*
71 DocumentProperties::create()
72 {
73 if (_instance) return _instance;
74 _instance = new DocumentProperties;
75 _instance->init();
76 return _instance;
77 }
79 void
80 DocumentProperties::destroy()
81 {
82 if (_instance)
83 {
84 delete _instance;
85 _instance = 0;
86 }
87 }
89 DocumentProperties::DocumentProperties()
90 : Dialog ("dialogs.documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
91 _page_page(1, 1), _page_grid(1, 1), _page_guides(1, 1),
92 _page_snap(1, 1),
93 _prefs_path("dialogs.documentoptions")
94 {
95 hide();
96 set_resizable (false);
97 _tt.enable();
98 get_vbox()->set_spacing (4);
99 get_vbox()->pack_start (_notebook, true, true);
101 _notebook.append_page(_page_page, _("Page"));
102 _notebook.append_page(_page_grid, _("Grid/Guides"));
103 _notebook.append_page(_page_snap, _("Snap"));
105 build_page();
106 build_grid();
107 build_snap();
108 }
110 void
111 DocumentProperties::init()
112 {
113 update();
115 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
116 repr->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);
123 g_signal_connect(G_OBJECT(INKSCAPE), "deactivate_desktop",
124 G_CALLBACK(on_deactivate_desktop), 0);
126 show_all_children();
127 present();
128 }
130 DocumentProperties::~DocumentProperties()
131 {
132 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
133 repr->removeListenerByData (this);
134 _doc_replaced_connection.disconnect();
135 }
137 //========================================================================
139 /**
140 * Helper function that attachs widgets in a 3xn table. The widgets come in an
141 * array that has two entries per table row. The two entries code for four
142 * possible cases: (0,0) means insert space in first column; (0, non-0) means
143 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
144 * (non-0, non-0) means two widgets in columns 2 and 3.
145 **/
146 inline void
147 attach_all (Gtk::Table &table, const Gtk::Widget *arr[], unsigned size, int start = 0)
148 {
149 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2)
150 {
151 if (arr[i] && arr[i+1])
152 {
153 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
154 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
155 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
156 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
157 }
158 else
159 {
160 if (arr[i+1])
161 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
162 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
163 else if (arr[i])
164 {
165 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
166 label.set_alignment (0.0);
167 table.attach (label, 0, 3, r, r+1,
168 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
169 }
170 else
171 {
172 Gtk::HBox *space = manage (new Gtk::HBox);
173 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
174 table.attach (*space, 0, 1, r, r+1,
175 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
176 }
177 }
178 ++r;
179 }
180 }
182 void
183 DocumentProperties::build_page()
184 {
185 _page_page.show();
187 _rcp_bg.init (_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"),
188 "pagecolor", "inkscape:pageopacity", _wr);
189 _rcb_canb.init (_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false);
190 _rcb_bord.init (_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false);
191 _rcp_bord.init (_("Border _color:"), _("Page border color"),
192 _("Color of the page border"),
193 "bordercolor", "borderopacity", _wr);
194 _rcb_shad.init (_("_Show border shadow"), _("If set, page border shows a shadow on its right and lower side"), "inkscape:showpageshadow", _wr, false);
195 _rum_deflt.init (_("Default _units:"), "inkscape:document-units", _wr);
196 Gtk::Button* fit_canv = manage(new Gtk::Button(_("Fit canvas to current selection")));
197 fit_canv->signal_clicked().connect(sigc::ptr_fun(fire_fit_canvas_to_selection_or_drawing));
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, fit_canv,
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 };
223 attach_all (_page_page.table(), widget_array, sizeof(widget_array));
224 }
226 void
227 DocumentProperties::build_grid()
228 {
229 _page_grid.show();
231 /// \todo FIXME: gray out snapping when grid is off.
232 /// Dissenting view: you want snapping without grid.
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));
282 }
284 void
285 DocumentProperties::build_snap()
286 {
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>"));
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()
366 {
367 if (_wr.isUpdating()) return;
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);
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);
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_object_bbox);
417 _rcbsnnob.setActive (nv->snap_object_point);
418 _rcbsnop.setActive (nv->snap_object_paths);
419 _rcbsnop.setActive (nv->snap_object_nodes);
420 _rsu_sno.setValue (nv->objecttolerance, nv->has_abs_tolerance);
422 _rcbsnbb.setActive (nv->snap_grid_bbox);
423 _rcbsnnod.setActive (nv->snap_grid_point);
424 _rsu_sn.setValue (nv->gridtolerance, nv->has_abs_tolerance);
426 _rcb_snpgui.setActive (nv->snap_guide_bbox);
427 _rcb_snbgui.setActive (nv->snap_guide_point);
428 _rsu_gusn.setValue (nv->guidetolerance, nv->has_abs_tolerance);
429 // _rrb_pix.setValue (true);
431 _wr.setUpdating (false);
432 }
434 //--------------------------------------------------------------------
436 void
437 DocumentProperties::on_response (int id)
438 {
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 }
449 if (id == Gtk::RESPONSE_CLOSE)
450 hide();
451 }
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)
458 {
459 if (!_instance)
460 return;
462 _instance->update();
463 }
465 static void
466 on_activate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
467 {
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 _instance->_doc_replaced_connection = SP_ACTIVE_DESKTOP->connectDocumentReplaced (sigc::ptr_fun (on_doc_replaced));
474 _instance->update();
475 }
477 static void
478 on_deactivate_desktop (Inkscape::Application *, SPDesktop* dt, void*)
479 {
480 if (!_instance)
481 return;
483 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(SP_ACTIVE_DESKTOP));
484 repr->removeListenerByData (_instance);
485 _instance->_doc_replaced_connection.disconnect();
486 }
488 static void
489 on_doc_replaced (SPDesktop* dt, SPDocument* doc)
490 {
491 if (!_instance)
492 return;
494 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
495 repr->addListener (&_repr_events, _instance);
496 _instance->update();
497 }
499 static void
500 fire_fit_canvas_to_selection_or_drawing() {
501 SPDesktop *dt = SP_ACTIVE_DESKTOP;
502 if (!dt) return;
503 Verb *verb = Verb::get( SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING );
504 if (verb) {
505 SPAction *action = verb->get_action(dt);
506 if (action) {
507 sp_action_perform(action, NULL);
508 }
509 }
510 }
513 } // namespace Dialog
514 } // namespace UI
515 } // namespace Inkscape
517 /*
518 Local Variables:
519 mode:c++
520 c-file-style:"stroustrup"
521 c-file-offsets:((innamespace . 0)(inline-open . 0))
522 indent-tabs-mode:nil
523 fill-column:99
524 End:
525 */
526 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :