Code

713acc23e049592beb554f3a00abddac79a21eab
[inkscape.git] / src / ui / dialog / document-properties.cpp
1 /** @file
2  * @brief Document properties dialog, Gtkmm-style
3  */
4 /* Authors:
5  *   bulia byak <buliabyak@users.sf.net>
6  *   Bryce W. Harrington <bryce@bryceharrington.org>
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Jon Phillips <jon@rejon.org>
9  *   Ralf Stephan <ralf@ark.in-berlin.de> (Gtkmm)
10  *   Diederik van Lierop <mail@diedenrezi.nl>
11  *
12  * Copyright (C) 2006-2008 Johan Engelen  <johan@shouraizou.nl>
13  * Copyright (C) 2000 - 2008 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
22 #include "display/canvas-grid.h"
23 #include "document-properties.h"
24 #include "document.h"
25 #include "desktop-handles.h"
26 #include "desktop.h"
27 #include <gtkmm.h>
28 #include "helper/units.h"
29 #include "inkscape.h"
30 #include "io/sys.h"
31 #include "preferences.h"
32 #include "sp-namedview.h"
33 #include "sp-object-repr.h"
34 #include "sp-root.h"
35 #include "ui/widget/color-picker.h"
36 #include "ui/widget/scalar-unit.h"
37 #include "verbs.h"
38 #include "widgets/icon.h"
39 #include "xml/node-event-vector.h"
40 #include "xml/repr.h"
42 #if ENABLE_LCMS
43 #include <lcms.h>
44 //#include "color-profile-fns.h"
45 #include "color-profile.h"
46 #endif // ENABLE_LCMS
48 using std::pair;
50 namespace Inkscape {
51 namespace UI {
52 namespace Dialog {
54 #define SPACE_SIZE_X 15
55 #define SPACE_SIZE_Y 10
57 #define INKSCAPE_ICON_GRID_XY     "grid_xy"
58 #define INKSCAPE_ICON_GRID_AXONOM "grid_axonom"
61 //===================================================
63 //---------------------------------------------------
65 static void on_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
66 static void on_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
67 static void on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer);
69 static Inkscape::XML::NodeEventVector const _repr_events = {
70     on_child_added, // child_added
71     on_child_removed, // child_removed
72     on_repr_attr_changed,
73     NULL, // content_changed
74     NULL  // order_changed
75 };
78 DocumentProperties &
79 DocumentProperties::getInstance()
80 {
81     DocumentProperties &instance = *new DocumentProperties();
82     instance.init();
84     return instance;
85 }
87 DocumentProperties::DocumentProperties()
88     : UI::Widget::Panel ("", "/dialogs/documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
89       _page_page(1, 1, true, true), _page_guides(1, 1),
90       _page_snap(1, 1), _page_snap_dtls(1, 1), _page_cms(1, 1),
91     //---------------------------------------------------------------
92       _rcb_canb(_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false),
93       _rcb_bord(_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false),
94       _rcb_shad(_("_Show border shadow"), _("If set, page border shows a shadow on its right and lower side"), "inkscape:showpageshadow", _wr, false),
95       _rcp_bg(_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"), "pagecolor", "inkscape:pageopacity", _wr),
96       _rcp_bord(_("Border _color:"), _("Page border color"), _("Color of the page border"), "bordercolor", "borderopacity", _wr),
97       _rum_deflt(_("Default _units:"), "inkscape:document-units", _wr),
98       _page_sizer(_wr),
99     //---------------------------------------------------------------
100       //General snap options
101       _rcb_sgui(_("Show _guides"), _("Show or hide guides"), "showguides", _wr),
102       _rcbsng(_("_Snap guides while dragging"), _("While dragging a guide, snap to object nodes or bounding box corners ('Snap to nodes' or 'snap to bounding box corners' must be enabled in the 'Snap' tab; only a small part of the guide near the cursor will snap)"),
103                   "inkscape:snap-guide", _wr),
104       _rcp_gui(_("Guide co_lor:"), _("Guideline color"), _("Color of guidelines"), "guidecolor", "guideopacity", _wr),
105       _rcp_hgui(_("_Highlight color:"), _("Highlighted guideline color"), _("Color of a guideline when it is under mouse"), "guidehicolor", "guidehiopacity", _wr),
106     //---------------------------------------------------------------
107       _rcbs(_("_Enable snapping"), _("Toggle snapping on or off"), "inkscape:snap-global", _wr),
108       _rcbsnbb(_("_Bounding box corners"), _("Only available in the selector tool: snap bounding box corners to guides, to grids, and to other bounding boxes (but not to nodes or paths)"),
109                   "inkscape:snap-bbox", _wr),
110       _rcbsnn(_("_Nodes"), _("Snap nodes (e.g. path nodes, special points in shapes, gradient handles, text base points, transformation origins, etc.) to guides, to grids, to paths and to other nodes"),
111                 "inkscape:snap-nodes", _wr),
112       //Options for snapping to objects
113       _rcbsnop(_("Snap to path_s"), _("Snap nodes to object paths"), "inkscape:object-paths", _wr),
114       _rcbsnon(_("Snap to n_odes"), _("Snap nodes and guides to object nodes"), "inkscape:object-nodes", _wr),
115       _rcbsnbbp(_("Snap to bounding bo_x edges"), _("Snap bounding box corners and guides to bounding box edges"), "inkscape:bbox-paths", _wr),
116       _rcbsnbbn(_("Snap to bounding box co_rners"), _("Snap bounding box corners to other bounding box corners"), "inkscape:bbox-nodes", _wr),
117       _rcbsnpb(_("Snap to page border"), _("Snap bounding box corners and nodes to the page border"), "inkscape:snap-page", _wr),
118     //---------------------------------------------------------------
119        //Applies to both nodes and guides, but not to bboxes, that's why its located here
120       _rcbic( _("Rotation _center"), _("Consider the rotation center of an object when snapping"), "inkscape:snap-center", _wr),
121       _rcbsm( _("_Smooth nodes"), _("Snap to smooth nodes too, instead of only snapping to cusp nodes"), "inkscape:snap-smooth-nodes", _wr),
122       _rcbmp( _("_Midpoints"), _("Snap midpoints of straight line segments"), "inkscape:snap-midpoints", _wr),
123       _rcbsigg(_("_Grid with guides"), _("Snap to grid-guide intersections"), "inkscape:snap-intersection-grid-guide", _wr),
124       _rcbsils(_("_Paths"), _("Snap to intersections of paths ('snap to paths' must be enabled, see the previous tab)"),
125                 "inkscape:snap-intersection-paths", _wr),
126     //---------------------------------------------------------------
127       _grids_label_crea("", Gtk::ALIGN_LEFT),
128       //TRANSLATORS: In Grid|_New translate only the word _New. It ref to grid
129       _grids_button_new(Q_("Grid|_New"), _("Create new grid.")),
130       _grids_button_remove(_("_Remove"), _("Remove selected grid.")),
131       _grids_label_def("", Gtk::ALIGN_LEFT)
132     //---------------------------------------------------------------
134     _tt.enable();
135     _getContents()->set_spacing (4);
136     _getContents()->pack_start(_notebook, true, true);
138     _notebook.append_page(_page_page,      _("Page"));
139     _notebook.append_page(_page_guides,    _("Guides"));
140     _notebook.append_page(_grids_vbox,     _("Grids"));
141     _notebook.append_page(_page_snap,      _("Snap"));
142     _notebook.append_page(_page_snap_dtls, _("Snap points"));
143     _notebook.append_page(_page_cms, _("Color Management"));
145     build_page();
146     build_guides();
147     build_gridspage();
148     build_snap();
149     build_snap_dtls();
150 #if ENABLE_LCMS
151     build_cms();
152 #endif // ENABLE_LCMS
154     _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
155     _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
157     signalDocumentReplaced().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDocumentReplaced));
158     signalActivateDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleActivateDesktop));
159     signalDeactiveDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDeactivateDesktop));
162 void
163 DocumentProperties::init()
165     update();
167     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
168     repr->addListener (&_repr_events, this);
169     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
170     root->addListener (&_repr_events, this);
172     show_all_children();
173     _grids_button_remove.hide();
176 DocumentProperties::~DocumentProperties()
178     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
179     repr->removeListenerByData (this);
180     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
181     root->removeListenerByData (this);
184 //========================================================================
186 /**
187  * Helper function that attaches widgets in a 3xn table. The widgets come in an
188  * array that has two entries per table row. The two entries code for four
189  * possible cases: (0,0) means insert space in first column; (0, non-0) means
190  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
191  * (non-0, non-0) means two widgets in columns 2 and 3.
192 **/
193 inline void
194 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
196     for (unsigned i = 0, r = start; i < n; i += 2)
197     {
198         if (arr[i] && arr[i+1])
199         {
200             table.attach(*arr[i],   1, 2, r, r+1,
201                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
202             table.attach(*arr[i+1], 2, 3, r, r+1,
203                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
204         }
205         else
206         {
207             if (arr[i+1]) {
208                 Gtk::AttachOptions yoptions = (Gtk::AttachOptions)0;
209                 if (dynamic_cast<Inkscape::UI::Widget::PageSizer*>(arr[i+1])) {
210                     // only the PageSizer in Document Properties|Page should be stretched vertically
211                     yoptions = Gtk::FILL|Gtk::EXPAND;
212                 }
213                 table.attach(*arr[i+1], 1, 3, r, r+1,
214                       Gtk::FILL|Gtk::EXPAND, yoptions, 0,0);
215             }
216             else if (arr[i])
217             {
218                 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
219                 label.set_alignment (0.0);
220                 table.attach (label, 0, 3, r, r+1,
221                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
222             }
223             else
224             {
225                 Gtk::HBox *space = manage (new Gtk::HBox);
226                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
227                 table.attach (*space, 0, 1, r, r+1,
228                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
229             }
230         }
231         ++r;
232     }
235 void
236 DocumentProperties::build_page()
238     _page_page.show();
240     Gtk::Label* label_gen = manage (new Gtk::Label);
241     label_gen->set_markup (_("<b>General</b>"));
242     Gtk::Label* label_bor = manage (new Gtk::Label);
243     label_bor->set_markup (_("<b>Border</b>"));
244     Gtk::Label *label_for = manage (new Gtk::Label);
245     label_for->set_markup (_("<b>Format</b>"));
246     _page_sizer.init();
248     Gtk::Widget *const widget_array[] =
249     {
250         label_gen,         0,
251         0,                 &_rum_deflt,
252         _rcp_bg._label,    &_rcp_bg,
253         0,                 0,
254         label_for,         0,
255         0,                 &_page_sizer,
256         0,                 0,
257         label_bor,         0,
258         0,                 &_rcb_canb,
259         0,                 &_rcb_bord,
260         0,                 &_rcb_shad,
261         _rcp_bord._label,  &_rcp_bord,
262     };
264     attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
267 void
268 DocumentProperties::build_guides()
270     _page_guides.show();
272     Gtk::Label *label_gui = manage (new Gtk::Label);
273     label_gui->set_markup (_("<b>Guides</b>"));
275     Gtk::Widget *const widget_array[] =
276     {
277         label_gui,        0,
278         0,                &_rcb_sgui,
279         _rcp_gui._label,  &_rcp_gui,
280         _rcp_hgui._label, &_rcp_hgui,
281         0,                &_rcbsng,
282     };
284     attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
287 void
288 DocumentProperties::build_snap()
290     _page_snap.show();
292     _rsu_sno.init (_("Snap _distance"), _("Snap only when _closer than:"), _("Always snap"),
293                   _("Snapping distance, in screen pixels, for snapping to objects"), _("Always snap to objects, regardless of their distance"),
294                   _("If set, objects only snap to another object when it's within the range specified below"),
295                   "objecttolerance", _wr);
297     //Options for snapping to grids
298     _rsu_sn.init (_("Snap d_istance"), _("Snap only when c_loser than:"), _("Always snap"),
299                   _("Snapping distance, in screen pixels, for snapping to grid"), _("Always snap to grids, regardless of the distance"),
300                   _("If set, objects only snap to a grid line when it's within the range specified below"),
301                   "gridtolerance", _wr);
303     //Options for snapping to guides
304     _rsu_gusn.init (_("Snap dist_ance"), _("Snap only when close_r than:"), _("Always snap"),
305                 _("Snapping distance, in screen pixels, for snapping to guides"), _("Always snap to guides, regardless of the distance"),
306                 _("If set, objects only snap to a guide when it's within the range specified below"),
307                 "guidetolerance", _wr);
309     //Other options to locate here: e.g. visual snapping indicators on/off
311     std::list<Gtk::Widget*> slaves;
312     slaves.push_back(&_rcbsnop);
313     slaves.push_back(&_rcbsnon);
314     _rcbsnn.setSlaveWidgets(slaves);
316     slaves.clear();
317     slaves.push_back(&_rcbsnbbp);
318     slaves.push_back(&_rcbsnbbn);
319     _rcbsnbb.setSlaveWidgets(slaves);
321     slaves.clear();
322     slaves.push_back(&_rcbsnn);
323     slaves.push_back(&_rcbsnbb);
324     _rcbs.setSlaveWidgets(slaves);
326     Gtk::Label *label_g = manage (new Gtk::Label);
327     label_g->set_markup (_("<b>Snapping</b>"));
328     Gtk::Label *label_w = manage (new Gtk::Label);
329     label_w->set_markup (_("<b>What snaps</b>"));
330     Gtk::Label *label_o = manage (new Gtk::Label);
331     label_o->set_markup (_("<b>Snap to objects</b>"));
332     Gtk::Label *label_gr = manage (new Gtk::Label);
333     label_gr->set_markup (_("<b>Snap to grids</b>"));
334     Gtk::Label *label_gu = manage (new Gtk::Label);
335     label_gu->set_markup (_("<b>Snap to guides</b>"));
337     Gtk::Widget *const array[] =
338     {
339         label_g,            0,
340         0,                  &_rcbs,
341         0,                  0,
342         label_w,            0,
343         0,                  &_rcbsnn,
344         0,                  &_rcbsnbb,
345         0,                  0,
346         label_o,            0,
347         0,                  &_rcbsnop,
348         0,                  &_rcbsnon,
349         0,                  &_rcbsnbbp,
350         0,                  &_rcbsnbbn,
351         0,                  &_rcbsnpb,
352         0,                  _rsu_sno._vbox,
353         0,                  0,
354         label_gr,           0,
355         0,                  _rsu_sn._vbox,
356         0,                  0,
357         label_gu,           0,
358         0,                  _rsu_gusn._vbox
359     };
361     attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
362  }
364 void
365 DocumentProperties::build_snap_dtls()
367     _page_snap_dtls.show();
369     //Other options to locate here: e.g. visual snapping indicators on/off
371     Gtk::Label *label_i= manage (new Gtk::Label);
372     label_i->set_markup (_("<b>Snapping intersections of</b>"));
373     Gtk::Label *label_m = manage (new Gtk::Label);
374     label_m->set_markup (_("<b>Special points to consider</b>"));
376     Gtk::Widget *const array[] =
377     {
378         label_i,            0,
379         0,                  &_rcbsigg,
380         0,                  &_rcbsils,
381         0,                  0,
382         label_m,            0,
383         0,                  &_rcbic,
384         0,                  &_rcbsm,
385         0,                  &_rcbmp
386     };
388     attach_all(_page_snap_dtls.table(), array, G_N_ELEMENTS(array));
391 // Very simple observer that just emits a signal if anything happens to a node
392 DocumentProperties::SignalObserver::SignalObserver()
393     : _oldsel(0)
394 {}
396 // Add this observer to the SPObject and remove it from any previous object
397 void
398 DocumentProperties::SignalObserver::set(SPObject* o)
400     if(_oldsel && _oldsel->repr)
401         _oldsel->repr->removeObserver(*this);
402     if(o && o->repr)
403         o->repr->addObserver(*this);
404     _oldsel = o;
407 void DocumentProperties::SignalObserver::notifyChildAdded(XML::Node&, XML::Node&, XML::Node*)
408 { signal_changed()(); }
410 void DocumentProperties::SignalObserver::notifyChildRemoved(XML::Node&, XML::Node&, XML::Node*)
411 { signal_changed()(); }
413 void DocumentProperties::SignalObserver::notifyChildOrderChanged(XML::Node&, XML::Node&, XML::Node*, XML::Node*)
414 { signal_changed()(); }
416 void DocumentProperties::SignalObserver::notifyContentChanged(XML::Node&, Util::ptr_shared<char>, Util::ptr_shared<char>)
417 {}
419 void DocumentProperties::SignalObserver::notifyAttributeChanged(XML::Node&, GQuark, Util::ptr_shared<char>, Util::ptr_shared<char>)
420 { signal_changed()(); }
422 sigc::signal<void>& DocumentProperties::SignalObserver::signal_changed()
424     return _signal_changed;
427 #if ENABLE_LCMS
428 static void
429 lcms_profile_get_name (cmsHPROFILE   profile, const gchar **name)
431   if (profile)
432     {
433       *name = cmsTakeProductDesc (profile);
435       if (! *name)
436         *name = cmsTakeProductName (profile);
438       if (*name && ! g_utf8_validate (*name, -1, NULL))
439         *name = _("(invalid UTF-8 string)");
440     }
441   else
442     {
443       *name = _("None");
444     }
447 void
448 DocumentProperties::populate_available_profiles(){
449     Glib::ListHandle<Gtk::Widget*> children = _menu.get_children();
450     for ( Glib::ListHandle<Gtk::Widget*>::iterator it2 = children.begin(); it2 != children.end(); ++it2 ) {
451         _menu.remove(**it2);
452         delete(*it2);
453     }
455     std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
457     // Use this loop to iterate through a list of possible document locations.
458     for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
459         if ( Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_EXISTS )
460              && Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_IS_DIR )) {
461             GError *err = 0;
462             GDir *directory = g_dir_open(it->c_str(), 0, &err);
463             if (!directory) {
464                 gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str());
465                 g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
466                 g_free(safeDir);
467             } else {
468                 gchar *filename = 0;
469                 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
470                     gchar* full = g_build_filename(it->c_str(), filename, NULL);
471                     if ( !Inkscape::IO::file_test( full, G_FILE_TEST_IS_DIR ) ) {
472                         cmsHPROFILE hProfile = cmsOpenProfileFromFile(full, "r");
473                         if (hProfile != NULL){
474                             const gchar* name;
475                             lcms_profile_get_name(hProfile, &name);
476                             Gtk::MenuItem* mi = manage(new Gtk::MenuItem());
477                             mi->set_data("filepath", g_strdup(full));
478                             mi->set_data("name", g_strdup(name));
479                             Gtk::HBox *hbox = manage(new Gtk::HBox());
480                             hbox->show();
481                             Gtk::Label* lbl = manage(new Gtk::Label(name));
482                             lbl->show();
483                             hbox->pack_start(*lbl, true, true, 0);
484                             mi->add(*hbox);
485                             mi->show_all();
486                             _menu.append(*mi);
487         //                    g_free((void*)name);
488                             cmsCloseProfile(hProfile);
489                         }
490                     }
491                     g_free(full);
492                 }
493                 g_dir_close(directory);
494             }
495         }
496     }
497     _menu.show_all();
500 void
501 DocumentProperties::linkSelectedProfile()
503 //store this profile in the SVG document (create <color-profile> element in the XML)
504     // TODO remove use of 'active' desktop
505     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
506     if (!desktop){
507         g_warning("No active desktop");
508     } else {
509         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
510         Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
511         cprofRepr->setAttribute("name", (gchar*) _menu.get_active()->get_data("name"));
512         cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
514         // Checks whether there is a defs element. Creates it when needed
515         Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
516         if (!defsRepr){
517             defsRepr = xml_doc->createElement("svg:defs");
518             xml_doc->root()->addChild(defsRepr, NULL);
519         }
521         g_assert(SP_ROOT(desktop->doc()->root)->defs);
522         defsRepr->addChild(cprofRepr, NULL);
524         // TODO check if this next line was sometimes needed. It being there caused an assertion.
525         //Inkscape::GC::release(defsRepr);
527         // inform the document, so we can undo
528         sp_document_done(desktop->doc(), SP_VERB_EDIT_LINK_COLOR_PROFILE, _("Link Color Profile"));
530         populate_linked_profiles_box();
531     }
534 void
535 DocumentProperties::populate_linked_profiles_box()
537     _LinkedProfilesListStore->clear();
538     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
539     if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
540     while ( current ) {
541         SPObject* obj = SP_OBJECT(current->data);
542         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
543         Gtk::TreeModel::Row row = *(_LinkedProfilesListStore->append());
544         row[_LinkedProfilesListColumns.nameColumn] = prof->name;
545 //        row[_LinkedProfilesListColumns.previewColumn] = "Color Preview";
546         current = g_slist_next(current);
547     }
550 void DocumentProperties::linked_profiles_list_button_release(GdkEventButton* event)
552     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
553         _EmbProfContextMenu.popup(event->button, event->time);
554     }
557 void DocumentProperties::create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
559     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
560     _EmbProfContextMenu.append(*mi);
561     mi->signal_activate().connect(rem);
562     mi->show();
563     _EmbProfContextMenu.accelerate(parent);
566 void DocumentProperties::removeSelectedProfile(){
567     Glib::ustring name;
568     if(_LinkedProfilesList.get_selection()) {
569         Gtk::TreeModel::iterator i = _LinkedProfilesList.get_selection()->get_selected();
571         if(i){
572             name = (*i)[_LinkedProfilesListColumns.nameColumn];
573         } else {
574             return;
575         }
576     }
578     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
579     while ( current ) {
580         SPObject* obj = SP_OBJECT(current->data);
581         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
582         if (!name.compare(prof->name)){
583             sp_repr_unparent(obj->repr);
584             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove linked color profile"));
585         }
586         current = g_slist_next(current);
587     }
589     populate_linked_profiles_box();
592 void
593 DocumentProperties::build_cms()
595     _page_cms.show();
597     Gtk::Label *label_link= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
598     label_link->set_markup (_("<b>Linked Color Profiles:</b>"));
599     Gtk::Label *label_avail = manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
600     label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
602     _link_btn.set_label(_("Link Profile"));
604     _page_cms.set_spacing(4);
605     gint row = 0;
607     label_link->set_alignment(0.0);
608     _page_cms.table().attach(*label_link, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
609     row++;
610     _page_cms.table().attach(_LinkedProfilesListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
611     row++;
613     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
614     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
615     _page_cms.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
616     row++;
618     label_avail->set_alignment(0.0);
619     _page_cms.table().attach(*label_avail, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
620     row++;
621     _page_cms.table().attach(_combo_avail, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
622     _page_cms.table().attach(_link_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
624     populate_available_profiles();
626     _combo_avail.set_menu(_menu);
627     _combo_avail.set_history(0);
628     _combo_avail.show_all();
630     //# Set up the Linked Profiles combo box
631     _LinkedProfilesListStore = Gtk::ListStore::create(_LinkedProfilesListColumns);
632     _LinkedProfilesList.set_model(_LinkedProfilesListStore);
633     _LinkedProfilesList.append_column(_("Profile Name"), _LinkedProfilesListColumns.nameColumn);
634 //    _LinkedProfilesList.append_column(_("Color Preview"), _LinkedProfilesListColumns.previewColumn);
635     _LinkedProfilesList.set_headers_visible(false);
636 // TODO restore?    _LinkedProfilesList.set_fixed_height_mode(true);
638     populate_linked_profiles_box();
640     _LinkedProfilesListScroller.add(_LinkedProfilesList);
641     _LinkedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
642     _LinkedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
643     _LinkedProfilesListScroller.set_size_request(-1, 90);
645     _link_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::linkSelectedProfile));
647     _LinkedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::linked_profiles_list_button_release));
648     create_popup_menu(_LinkedProfilesList, sigc::mem_fun(*this, &DocumentProperties::removeSelectedProfile));
650     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "defs" );
651     if (current) {
652         _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
653     }
654     _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_linked_profiles_box));
656 #endif // ENABLE_LCMS
658 /**
659 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
660 */
661 void
662 DocumentProperties::update_gridspage()
664     SPDesktop *dt = getDesktop();
665     SPNamedView *nv = sp_desktop_namedview(dt);
667     //remove all tabs
668     while (_grids_notebook.get_n_pages() != 0) {
669         _grids_notebook.remove_page(-1); // this also deletes the page.
670     }
672     //add tabs
673     bool grids_present = false;
674     for (GSList const * l = nv->grids; l != NULL; l = l->next) {
675         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
676         if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
677         Glib::ustring name(grid->repr->attribute("id"));
678         const char *icon = NULL;
679         switch (grid->getGridType()) {
680             case GRID_RECTANGULAR:
681                 icon = INKSCAPE_ICON_GRID_XY;
682                 break;
683             case GRID_AXONOMETRIC:
684                 icon = INKSCAPE_ICON_GRID_AXONOM;
685                 break;
686             default:
687                 break;
688         }
689         _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
690         grids_present = true;
691     }
692     _grids_notebook.show_all();
694     if (grids_present)
695         _grids_button_remove.set_sensitive(true);
696     else
697         _grids_button_remove.set_sensitive(false);
700 /**
701  * Build grid page of dialog.
702  */
703 void
704 DocumentProperties::build_gridspage()
706     /// \todo FIXME: gray out snapping when grid is off.
707     /// Dissenting view: you want snapping without grid.
709     SPDesktop *dt = getDesktop();
710     SPNamedView *nv = sp_desktop_namedview(dt);
711     (void)nv;
713     _grids_label_crea.set_markup(_("<b>Creation</b>"));
714     _grids_label_def.set_markup(_("<b>Defined grids</b>"));
715     _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
716     _grids_hbox_crea.pack_start(_grids_button_new, true, true);
718     for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
719         _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
720     }
721     _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
723     _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
725     _grids_vbox.set_spacing(4);
726     _grids_vbox.pack_start(_grids_label_crea, false, false);
727     _grids_vbox.pack_start(_grids_hbox_crea, false, false);
728     _grids_vbox.pack_start(_grids_space, false, false);
729     _grids_vbox.pack_start(_grids_label_def, false, false);
730     _grids_vbox.pack_start(_grids_notebook, false, false);
731     _grids_vbox.pack_start(_grids_button_remove, false, false);
733     update_gridspage();
738 /**
739  * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
740  */
741 void
742 DocumentProperties::update()
744     if (_wr.isUpdating()) return;
746     SPDesktop *dt = getDesktop();
747     SPNamedView *nv = sp_desktop_namedview(dt);
749     _wr.setUpdating (true);
750     set_sensitive (true);
752     //-----------------------------------------------------------page page
753     _rcp_bg.setRgba32 (nv->pagecolor);
754     _rcb_canb.setActive (nv->showborder);
755     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
756     _rcp_bord.setRgba32 (nv->bordercolor);
757     _rcb_shad.setActive (nv->showpageshadow);
759     if (nv->doc_units)
760         _rum_deflt.setUnit (nv->doc_units);
762     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
763     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
764     _page_sizer.setDim (doc_w_px, doc_h_px);
766     //-----------------------------------------------------------guide page
768     _rcb_sgui.setActive (nv->showguides);
769     _rcp_gui.setRgba32 (nv->guidecolor);
770     _rcp_hgui.setRgba32 (nv->guidehicolor);
772     //-----------------------------------------------------------snap page
774     _rcbsnbb.setActive (nv->snap_manager.snapprefs.getSnapModeBBox());
775     _rcbsnn.setActive (nv->snap_manager.snapprefs.getSnapModeNode());
776     _rcbsng.setActive (nv->snap_manager.snapprefs.getSnapModeGuide());
777     _rcbic.setActive (nv->snap_manager.snapprefs.getIncludeItemCenter());
778     _rcbsm.setActive (nv->snap_manager.snapprefs.getSnapSmoothNodes());
779     _rcbmp.setActive (nv->snap_manager.snapprefs.getSnapMidpoints());
780     _rcbsigg.setActive (nv->snap_manager.snapprefs.getSnapIntersectionGG());
781     _rcbsils.setActive (nv->snap_manager.snapprefs.getSnapIntersectionCS());
782     _rcbsnop.setActive(nv->snap_manager.object.getSnapToItemPath());
783     _rcbsnon.setActive(nv->snap_manager.object.getSnapToItemNode());
784     _rcbsnbbp.setActive(nv->snap_manager.object.getSnapToBBoxPath());
785     _rcbsnbbn.setActive(nv->snap_manager.object.getSnapToBBoxNode());
786     _rcbsnpb.setActive(nv->snap_manager.object.getSnapToPageBorder());
787     _rsu_sno.setValue (nv->objecttolerance);
789     _rsu_sn.setValue (nv->gridtolerance);
791     _rsu_gusn.setValue (nv->guidetolerance);
793     _rcbs.setActive (nv->snap_manager.snapprefs.getSnapEnabledGlobally());
795     //-----------------------------------------------------------grids page
797     update_gridspage();
799     //------------------------------------------------Color Management page
801 #if ENABLE_LCMS
802     populate_linked_profiles_box();
803     populate_available_profiles();
804 #endif // ENABLE_LCMS
806     _wr.setUpdating (false);
809 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
810 Gtk::HBox&
811 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
813     Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
814     _tab_label_box->set_spacing(4);
815     _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
816                                                        label_image)));
818     Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
819     _tab_label_box->pack_start(*_tab_label);
820     _tab_label_box->show_all();
822     return *_tab_label_box;
825 //--------------------------------------------------------------------
827 void
828 DocumentProperties::on_response (int id)
830     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
831     {
832         _rcp_bg.closeWindow();
833         _rcp_bord.closeWindow();
834         _rcp_gui.closeWindow();
835         _rcp_hgui.closeWindow();
836     }
838     if (id == Gtk::RESPONSE_CLOSE)
839         hide();
842 void
843 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
845     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
846     repr->addListener(&_repr_events, this);
847     Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
848     root->addListener(&_repr_events, this);
849     update();
852 void
853 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
855     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
856     repr->addListener(&_repr_events, this);
857     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
858     root->addListener(&_repr_events, this);
859     update();
862 void
863 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
865     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
866     repr->removeListenerByData(this);
867     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
868     root->removeListenerByData(this);
871 static void
872 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
874     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
875         dialog->update_gridspage();
878 static void
879 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
881     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
882         dialog->update_gridspage();
887 /**
888  * Called when XML node attribute changed; updates dialog widgets.
889  */
890 static void
891 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
893     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
894         dialog->update();
898 /*########################################################################
899 # BUTTON CLICK HANDLERS    (callbacks)
900 ########################################################################*/
902 void
903 DocumentProperties::onNewGrid()
905     SPDesktop *dt = getDesktop();
906     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
907     SPDocument *doc = sp_desktop_document(dt);
909     Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
910     CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
912     // toggle grid showing to ON:
913     dt->showGrids(true);
917 void
918 DocumentProperties::onRemoveGrid()
920     gint pagenum = _grids_notebook.get_current_page();
921     if (pagenum == -1) // no pages
922       return;
924     SPDesktop *dt = getDesktop();
925     SPNamedView *nv = sp_desktop_namedview(dt);
926     Inkscape::CanvasGrid * found_grid = NULL;
927     int i = 0;
928     for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) {  // not a very nice fix, but works.
929         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
930         if (pagenum == i) {
931             found_grid = grid;
932             break; // break out of for-loop
933         }
934     }
935     if (found_grid) {
936         // delete the grid that corresponds with the selected tab
937         // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
938         found_grid->repr->parent()->removeChild(found_grid->repr);
939         sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
940     }
944 } // namespace Dialog
945 } // namespace UI
946 } // namespace Inkscape
948 /*
949   Local Variables:
950   mode:c++
951   c-file-style:"stroustrup"
952   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
953   indent-tabs-mode:nil
954   fill-column:99
955   End:
956 */
957 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :