Code

adding "remove" context menu in the embedded color profiles box
[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       _rcbsigg(_("_Grid with guides"), _("Snap to grid-guide intersections"), "inkscape:snap-intersection-grid-guide", _wr),
123       _rcbsils(_("_Paths"), _("Snap to intersections of paths ('snap to paths' must be enabled, see the previous tab)"),
124                 "inkscape:snap-intersection-paths", _wr),
125     //---------------------------------------------------------------
126       _grids_label_crea("", Gtk::ALIGN_LEFT),
127       //TRANSLATORS: In Grid|_New translate only the word _New. It ref to grid
128       _grids_button_new(Q_("Grid|_New"), _("Create new grid.")),
129       _grids_button_remove(_("_Remove"), _("Remove selected grid.")),
130       _grids_label_def("", Gtk::ALIGN_LEFT)
131     //---------------------------------------------------------------
133     _tt.enable();
134     _getContents()->set_spacing (4);
135     _getContents()->pack_start(_notebook, true, true);
137     _notebook.append_page(_page_page,      _("Page"));
138     _notebook.append_page(_page_guides,    _("Guides"));
139     _notebook.append_page(_grids_vbox,     _("Grids"));
140     _notebook.append_page(_page_snap,      _("Snap"));
141     _notebook.append_page(_page_snap_dtls, _("Snap points"));
142     _notebook.append_page(_page_cms, _("Color Management"));
144     build_page();
145     build_guides();
146     build_gridspage();
147     build_snap();
148     build_snap_dtls();
149 #if ENABLE_LCMS
150     build_cms();
151 #endif // ENABLE_LCMS
153     _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
154     _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
156     signalDocumentReplaced().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDocumentReplaced));
157     signalActivateDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleActivateDesktop));
158     signalDeactiveDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDeactivateDesktop));
161 void
162 DocumentProperties::init()
164     update();
166     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
167     repr->addListener (&_repr_events, this);
168     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
169     root->addListener (&_repr_events, this);
171     show_all_children();
172     _grids_button_remove.hide();
175 DocumentProperties::~DocumentProperties()
177     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
178     repr->removeListenerByData (this);
179     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
180     root->removeListenerByData (this);
183 //========================================================================
185 /**
186  * Helper function that attaches widgets in a 3xn table. The widgets come in an
187  * array that has two entries per table row. The two entries code for four
188  * possible cases: (0,0) means insert space in first column; (0, non-0) means
189  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
190  * (non-0, non-0) means two widgets in columns 2 and 3.
191 **/
192 inline void
193 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
195     for (unsigned i = 0, r = start; i < n; i += 2)
196     {
197         if (arr[i] && arr[i+1])
198         {
199             table.attach(*arr[i],   1, 2, r, r+1,
200                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
201             table.attach(*arr[i+1], 2, 3, r, r+1,
202                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
203         }
204         else
205         {
206             if (arr[i+1]) {
207                 Gtk::AttachOptions yoptions = (Gtk::AttachOptions)0;
208                 if (dynamic_cast<Inkscape::UI::Widget::PageSizer*>(arr[i+1])) {
209                     // only the PageSizer in Document Properties|Page should be stretched vertically
210                     yoptions = Gtk::FILL|Gtk::EXPAND;
211                 }
212                 table.attach(*arr[i+1], 1, 3, r, r+1,
213                       Gtk::FILL|Gtk::EXPAND, yoptions, 0,0);
214             }
215             else if (arr[i])
216             {
217                 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
218                 label.set_alignment (0.0);
219                 table.attach (label, 0, 3, r, r+1,
220                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
221             }
222             else
223             {
224                 Gtk::HBox *space = manage (new Gtk::HBox);
225                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
226                 table.attach (*space, 0, 1, r, r+1,
227                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
228             }
229         }
230         ++r;
231     }
234 void
235 DocumentProperties::build_page()
237     _page_page.show();
239     Gtk::Label* label_gen = manage (new Gtk::Label);
240     label_gen->set_markup (_("<b>General</b>"));
241     Gtk::Label* label_bor = manage (new Gtk::Label);
242     label_bor->set_markup (_("<b>Border</b>"));
243     Gtk::Label *label_for = manage (new Gtk::Label);
244     label_for->set_markup (_("<b>Format</b>"));
245     _page_sizer.init();
247     Gtk::Widget *const widget_array[] =
248     {
249         label_gen,         0,
250         0,                 &_rum_deflt,
251         _rcp_bg._label,    &_rcp_bg,
252         0,                 0,
253         label_for,         0,
254         0,                 &_page_sizer,
255         0,                 0,
256         label_bor,         0,
257         0,                 &_rcb_canb,
258         0,                 &_rcb_bord,
259         0,                 &_rcb_shad,
260         _rcp_bord._label,  &_rcp_bord,
261     };
263     attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
266 void
267 DocumentProperties::build_guides()
269     _page_guides.show();
271     Gtk::Label *label_gui = manage (new Gtk::Label);
272     label_gui->set_markup (_("<b>Guides</b>"));
274     Gtk::Widget *const widget_array[] =
275     {
276         label_gui,        0,
277         0,                &_rcb_sgui,
278         _rcp_gui._label,  &_rcp_gui,
279         _rcp_hgui._label, &_rcp_hgui,
280         0,                &_rcbsng,
281     };
283     attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
286 void
287 DocumentProperties::build_snap()
289     _page_snap.show();
291     _rsu_sno.init (_("Snap _distance"), _("Snap only when _closer than:"), _("Always snap"),
292                   _("Snapping distance, in screen pixels, for snapping to objects"), _("Always snap to objects, regardless of their distance"),
293                   _("If set, objects only snap to another object when it's within the range specified below"),
294                   "objecttolerance", _wr);
296     //Options for snapping to grids
297     _rsu_sn.init (_("Snap d_istance"), _("Snap only when c_loser than:"), _("Always snap"),
298                   _("Snapping distance, in screen pixels, for snapping to grid"), _("Always snap to grids, regardless of the distance"),
299                   _("If set, objects only snap to a grid line when it's within the range specified below"),
300                   "gridtolerance", _wr);
302     //Options for snapping to guides
303     _rsu_gusn.init (_("Snap dist_ance"), _("Snap only when close_r than:"), _("Always snap"),
304                 _("Snapping distance, in screen pixels, for snapping to guides"), _("Always snap to guides, regardless of the distance"),
305                 _("If set, objects only snap to a guide when it's within the range specified below"),
306                 "guidetolerance", _wr);
308     //Other options to locate here: e.g. visual snapping indicators on/off
310     std::list<Gtk::Widget*> slaves;
311     slaves.push_back(&_rcbsnop);
312     slaves.push_back(&_rcbsnon);
313     _rcbsnn.setSlaveWidgets(slaves);
315     slaves.clear();
316     slaves.push_back(&_rcbsnbbp);
317     slaves.push_back(&_rcbsnbbn);
318     _rcbsnbb.setSlaveWidgets(slaves);
320     slaves.clear();
321     slaves.push_back(&_rcbsnn);
322     slaves.push_back(&_rcbsnbb);
323     _rcbs.setSlaveWidgets(slaves);
325     Gtk::Label *label_g = manage (new Gtk::Label);
326     label_g->set_markup (_("<b>Snapping</b>"));
327     Gtk::Label *label_w = manage (new Gtk::Label);
328     label_w->set_markup (_("<b>What snaps</b>"));
329     Gtk::Label *label_o = manage (new Gtk::Label);
330     label_o->set_markup (_("<b>Snap to objects</b>"));
331     Gtk::Label *label_gr = manage (new Gtk::Label);
332     label_gr->set_markup (_("<b>Snap to grids</b>"));
333     Gtk::Label *label_gu = manage (new Gtk::Label);
334     label_gu->set_markup (_("<b>Snap to guides</b>"));
336     Gtk::Widget *const array[] =
337     {
338         label_g,            0,
339         0,                  &_rcbs,
340         0,                  0,
341         label_w,            0,
342         0,                  &_rcbsnn,
343         0,                  &_rcbsnbb,
344         0,                  0,
345         label_o,            0,
346         0,                  &_rcbsnop,
347         0,                  &_rcbsnon,
348         0,                  &_rcbsnbbp,
349         0,                  &_rcbsnbbn,
350         0,                  &_rcbsnpb,
351         0,                  _rsu_sno._vbox,
352         0,                  0,
353         label_gr,           0,
354         0,                  _rsu_sn._vbox,
355         0,                  0,
356         label_gu,           0,
357         0,                  _rsu_gusn._vbox
358     };
360     attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
361  }
363 void
364 DocumentProperties::build_snap_dtls()
366     _page_snap_dtls.show();
368     //Other options to locate here: e.g. visual snapping indicators on/off
370     Gtk::Label *label_i= manage (new Gtk::Label);
371     label_i->set_markup (_("<b>Snapping to intersections of</b>"));
372     Gtk::Label *label_m = manage (new Gtk::Label);
373     label_m->set_markup (_("<b>Special points to consider</b>"));
375     Gtk::Widget *const array[] =
376     {
377         label_i,            0,
378         0,                  &_rcbsigg,
379         0,                  &_rcbsils,
380         0,                  0,
381         label_m,            0,
382         0,                  &_rcbic,
383         0,                  &_rcbsm
384     };
386     attach_all(_page_snap_dtls.table(), array, G_N_ELEMENTS(array));
389 // Very simple observer that just emits a signal if anything happens to a node
390 DocumentProperties::SignalObserver::SignalObserver()
391     : _oldsel(0)
392 {}
394 // Add this observer to the SPObject and remove it from any previous object
395 void
396 DocumentProperties::SignalObserver::set(SPObject* o)
398     if(_oldsel && _oldsel->repr)
399         _oldsel->repr->removeObserver(*this);
400     if(o && o->repr)
401         o->repr->addObserver(*this);
402     _oldsel = o;
405 void DocumentProperties::SignalObserver::notifyChildAdded(XML::Node&, XML::Node&, XML::Node*)
406 { signal_changed()(); }
408 void DocumentProperties::SignalObserver::notifyChildRemoved(XML::Node&, XML::Node&, XML::Node*)
409 { signal_changed()(); }
411 void DocumentProperties::SignalObserver::notifyChildOrderChanged(XML::Node&, XML::Node&, XML::Node*, XML::Node*)
412 { signal_changed()(); }
414 void DocumentProperties::SignalObserver::notifyContentChanged(XML::Node&, Util::ptr_shared<char>, Util::ptr_shared<char>)
415 {}
417 void DocumentProperties::SignalObserver::notifyAttributeChanged(XML::Node&, GQuark, Util::ptr_shared<char>, Util::ptr_shared<char>)
418 { signal_changed()(); }
420 sigc::signal<void>& DocumentProperties::SignalObserver::signal_changed()
422     return _signal_changed;
425 #if ENABLE_LCMS
426 static void
427 lcms_profile_get_name (cmsHPROFILE   profile, const gchar **name)
429   if (profile)
430     {
431       *name = cmsTakeProductDesc (profile);
433       if (! *name)
434         *name = cmsTakeProductName (profile);
436       if (*name && ! g_utf8_validate (*name, -1, NULL))
437         *name = _("(invalid UTF-8 string)");
438     }
439   else
440     {
441       *name = _("None");
442     }
445 void
446 DocumentProperties::populate_available_profiles(){
448     std::list<gchar *> sources;
449     sources.push_back( profile_path("color/icc") );
450     //sources.push_back( g_strdup(INKSCAPE_COLORPROFILESDIR) );
452     int index = 1;
454     // Use this loop to iterate through a list of possible document locations.
455     while (!sources.empty()) {
456         gchar *dirname = sources.front();
458         if ( Inkscape::IO::file_test( dirname, G_FILE_TEST_EXISTS )
459             && Inkscape::IO::file_test( dirname, G_FILE_TEST_IS_DIR )) {
460             GError *err = 0;
461             GDir *directory = g_dir_open(dirname, 0, &err);
462             if (!directory) {
463                 gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
464                 g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
465                 g_free(safeDir);
466             } else {
467                 gchar *filename = 0;
468                 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
469                     gchar* lower = g_ascii_strdown( filename, -1 );
470                     gchar* full = g_build_filename(dirname, 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 = new Gtk::MenuItem();
477                             mi->set_data("filepath", g_strdup(full));
478                             mi->set_data("name", g_strdup(name));
479                             Gtk::HBox *hbox = 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                         }
489                         cmsCloseProfile(hProfile);
490                         index++;
491                     }
492                     g_free(full);
493                     g_free(lower);
494                 }
495                 g_dir_close(directory);
496             }
497         }
499         // toss the dirname
500         g_free(dirname);
501         sources.pop_front();
502     }
503     _menu.show_all();
506 void
507 DocumentProperties::onEmbedProfile()
509 //store this profile in the SVG document (create <color-profile> element in the XML)
510     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
511     if (!desktop){
512         g_warning("No active desktop");
513         return;
514     }
515     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
516     Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
517     cprofRepr->setAttribute("name", (gchar*) _menu.get_active()->get_data("name"));
518     cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
520     /* Checks whether there is a defs element. Creates it when needed */
521     Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
522     if (!defsRepr){
523         defsRepr = xml_doc->createElement("svg:defs");
524         xml_doc->root()->addChild(defsRepr, NULL);
525     }
527     g_assert(SP_ROOT(desktop->doc()->root)->defs);
528     defsRepr->addChild(cprofRepr, NULL);
530     Inkscape::GC::release(defsRepr);
532     // inform the document, so we can undo
533     sp_document_done(desktop->doc(), SP_VERB_EDIT_EMBED_COLOR_PROFILE, _("Embed Color Profile"));
535     populate_embedded_profiles_box();
538 void
539 DocumentProperties::populate_embedded_profiles_box()
541     _EmbeddedProfilesListStore->clear();
542     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
543     if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
544     while ( current ) {
545         SPObject* obj = SP_OBJECT(current->data);
546         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
547         Gtk::TreeModel::Row row = *(_EmbeddedProfilesListStore->append());
548         row[_EmbeddedProfilesListColumns.nameColumn] = prof->name;
549 //        row[_EmbeddedProfilesListColumns.previewColumn] = "Color Preview";
550         current = g_slist_next(current);
551     }
554 void DocumentProperties::embedded_profiles_list_button_release(GdkEventButton* event)
556     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
557         _EmbProfContextMenu.popup(event->button, event->time);
558     }
561 void DocumentProperties::create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
563     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
564     _EmbProfContextMenu.append(*mi);
565     mi->signal_activate().connect(rem);
566     mi->show();
567     _EmbProfContextMenu.accelerate(parent);
570 void DocumentProperties::remove_profile(){
571     Glib::ustring name;
572     if(_EmbeddedProfilesList.get_selection()) {
573         Gtk::TreeModel::iterator i = _EmbeddedProfilesList.get_selection()->get_selected();
575         if(i){
576             name = (*i)[_EmbeddedProfilesListColumns.nameColumn];
577         } else {
578             return;
579         }
580     }
582     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
583     while ( current ) {
584         SPObject* obj = SP_OBJECT(current->data);
585         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
586         if (!name.compare(prof->name)){
587             sp_repr_unparent(obj->repr);
588             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove embedded color profile"));
589         }
590         current = g_slist_next(current);
591     }
593     populate_embedded_profiles_box();
596 void
597 DocumentProperties::build_cms()
599     _page_cms.show();
601     Gtk::Label *label_embed= manage (new Gtk::Label);
602     label_embed->set_markup (_("<b>Embedded Color Profiles:</b>"));
603     Gtk::Label *label_avail = manage (new Gtk::Label);
604     label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
606     _embed_btn.set_label("Embed Profile");
608     Gtk::Widget *const array[] =
609     {
610         label_embed,          0,
611         &_EmbeddedProfilesListScroller,      0,
612         label_avail,        0,
613         &_combo_avail,        &_embed_btn,
614     };
616     attach_all(_page_cms.table(), array, G_N_ELEMENTS(array));
618     populate_available_profiles();
620     _combo_avail.set_menu(_menu);
621     _combo_avail.set_history(0);
622     _combo_avail.show_all();
624     //# Set up the Embedded Profiles combo box
625     _EmbeddedProfilesListStore = Gtk::ListStore::create(_EmbeddedProfilesListColumns);
626     _EmbeddedProfilesList.set_model(_EmbeddedProfilesListStore);
627     _EmbeddedProfilesList.append_column(_("Profile Name"), _EmbeddedProfilesListColumns.nameColumn);
628 //    _EmbeddedProfilesList.append_column(_("Color Preview"), _EmbeddedProfilesListColumns.previewColumn);
629     _EmbeddedProfilesList.set_headers_visible(false);
630     _EmbeddedProfilesList.set_fixed_height_mode(true);
632     populate_embedded_profiles_box();
634     _EmbeddedProfilesListScroller.add(_EmbeddedProfilesList);
635     _EmbeddedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
636     _EmbeddedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
637     _EmbeddedProfilesListScroller.set_size_request(-1, 90);
639     _embed_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onEmbedProfile));
641     _EmbeddedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::embedded_profiles_list_button_release));
642     create_popup_menu(_EmbeddedProfilesList, sigc::mem_fun(*this, &DocumentProperties::remove_profile));
644     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "defs" );
645     if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
646     _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_embedded_profiles_box));
648 #endif // ENABLE_LCMS
650 /**
651 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
652 */
653 void
654 DocumentProperties::update_gridspage()
656     SPDesktop *dt = getDesktop();
657     SPNamedView *nv = sp_desktop_namedview(dt);
659     //remove all tabs
660     while (_grids_notebook.get_n_pages() != 0) {
661         _grids_notebook.remove_page(-1); // this also deletes the page.
662     }
664     //add tabs
665     bool grids_present = false;
666     for (GSList const * l = nv->grids; l != NULL; l = l->next) {
667         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
668         if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
669         Glib::ustring name(grid->repr->attribute("id"));
670         const char *icon = NULL;
671         switch (grid->getGridType()) {
672             case GRID_RECTANGULAR:
673                 icon = INKSCAPE_ICON_GRID_XY;
674                 break;
675             case GRID_AXONOMETRIC:
676                 icon = INKSCAPE_ICON_GRID_AXONOM;
677                 break;
678             default:
679                 break;
680         }
681         _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
682         grids_present = true;
683     }
684     _grids_notebook.show_all();
686     if (grids_present)
687         _grids_button_remove.set_sensitive(true);
688     else
689         _grids_button_remove.set_sensitive(false);
692 /**
693  * Build grid page of dialog.
694  */
695 void
696 DocumentProperties::build_gridspage()
698     /// \todo FIXME: gray out snapping when grid is off.
699     /// Dissenting view: you want snapping without grid.
701     SPDesktop *dt = getDesktop();
702     SPNamedView *nv = sp_desktop_namedview(dt);
703     (void)nv;
705     _grids_label_crea.set_markup(_("<b>Creation</b>"));
706     _grids_label_def.set_markup(_("<b>Defined grids</b>"));
707     _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
708     _grids_hbox_crea.pack_start(_grids_button_new, true, true);
710     for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
711         _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
712     }
713     _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
715     _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
717     _grids_vbox.set_spacing(4);
718     _grids_vbox.pack_start(_grids_label_crea, false, false);
719     _grids_vbox.pack_start(_grids_hbox_crea, false, false);
720     _grids_vbox.pack_start(_grids_space, false, false);
721     _grids_vbox.pack_start(_grids_label_def, false, false);
722     _grids_vbox.pack_start(_grids_notebook, false, false);
723     _grids_vbox.pack_start(_grids_button_remove, false, false);
725     update_gridspage();
730 /**
731  * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
732  */
733 void
734 DocumentProperties::update()
736     if (_wr.isUpdating()) return;
738     SPDesktop *dt = getDesktop();
739     SPNamedView *nv = sp_desktop_namedview(dt);
741     _wr.setUpdating (true);
742     set_sensitive (true);
744     //-----------------------------------------------------------page page
745     _rcp_bg.setRgba32 (nv->pagecolor);
746     _rcb_canb.setActive (nv->showborder);
747     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
748     _rcp_bord.setRgba32 (nv->bordercolor);
749     _rcb_shad.setActive (nv->showpageshadow);
751     if (nv->doc_units)
752         _rum_deflt.setUnit (nv->doc_units);
754     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
755     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
756     _page_sizer.setDim (doc_w_px, doc_h_px);
758     //-----------------------------------------------------------guide page
760     _rcb_sgui.setActive (nv->showguides);
761     _rcp_gui.setRgba32 (nv->guidecolor);
762     _rcp_hgui.setRgba32 (nv->guidehicolor);
764     //-----------------------------------------------------------snap page
766     _rcbsnbb.setActive (nv->snap_manager.snapprefs.getSnapModeBBox());
767     _rcbsnn.setActive (nv->snap_manager.snapprefs.getSnapModeNode());
768     _rcbsng.setActive (nv->snap_manager.snapprefs.getSnapModeGuide());
769     _rcbic.setActive (nv->snap_manager.snapprefs.getIncludeItemCenter());
770     _rcbsm.setActive (nv->snap_manager.snapprefs.getSnapSmoothNodes());
771     _rcbsigg.setActive (nv->snap_manager.snapprefs.getSnapIntersectionGG());
772     _rcbsils.setActive (nv->snap_manager.snapprefs.getSnapIntersectionCS());
773     _rcbsnop.setActive(nv->snap_manager.object.getSnapToItemPath());
774     _rcbsnon.setActive(nv->snap_manager.object.getSnapToItemNode());
775     _rcbsnbbp.setActive(nv->snap_manager.object.getSnapToBBoxPath());
776     _rcbsnbbn.setActive(nv->snap_manager.object.getSnapToBBoxNode());
777     _rcbsnpb.setActive(nv->snap_manager.object.getSnapToPageBorder());
778     _rsu_sno.setValue (nv->objecttolerance);
780     _rsu_sn.setValue (nv->gridtolerance);
782     _rsu_gusn.setValue (nv->guidetolerance);
784     _rcbs.setActive (nv->snap_manager.snapprefs.getSnapEnabledGlobally());
786     //-----------------------------------------------------------grids page
788     update_gridspage();
790     //------------------------------------------------Color Management page
792     populate_embedded_profiles_box();
793     populate_available_profiles();
795     _wr.setUpdating (false);
798 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
799 Gtk::HBox&
800 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
802     Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
803     _tab_label_box->set_spacing(4);
804     _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
805                                                        label_image)));
807     Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
808     _tab_label_box->pack_start(*_tab_label);
809     _tab_label_box->show_all();
811     return *_tab_label_box;
814 //--------------------------------------------------------------------
816 void
817 DocumentProperties::on_response (int id)
819     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
820     {
821         _rcp_bg.closeWindow();
822         _rcp_bord.closeWindow();
823         _rcp_gui.closeWindow();
824         _rcp_hgui.closeWindow();
825     }
827     if (id == Gtk::RESPONSE_CLOSE)
828         hide();
831 void
832 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
834     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
835     repr->addListener(&_repr_events, this);
836     Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
837     root->addListener(&_repr_events, this);
838     update();
841 void
842 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
844     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
845     repr->addListener(&_repr_events, this);
846     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
847     root->addListener(&_repr_events, this);
848     update();
851 void
852 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
854     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
855     repr->removeListenerByData(this);
856     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
857     root->removeListenerByData(this);
860 static void
861 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
863     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
864         dialog->update_gridspage();
867 static void
868 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
870     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
871         dialog->update_gridspage();
876 /**
877  * Called when XML node attribute changed; updates dialog widgets.
878  */
879 static void
880 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
882     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
883         dialog->update();
887 /*########################################################################
888 # BUTTON CLICK HANDLERS    (callbacks)
889 ########################################################################*/
891 void
892 DocumentProperties::onNewGrid()
894     SPDesktop *dt = getDesktop();
895     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
896     SPDocument *doc = sp_desktop_document(dt);
898     Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
899     CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
901     // toggle grid showing to ON:
902     dt->showGrids(true);
906 void
907 DocumentProperties::onRemoveGrid()
909     gint pagenum = _grids_notebook.get_current_page();
910     if (pagenum == -1) // no pages
911       return;
913     SPDesktop *dt = getDesktop();
914     SPNamedView *nv = sp_desktop_namedview(dt);
915     Inkscape::CanvasGrid * found_grid = NULL;
916     int i = 0;
917     for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) {  // not a very nice fix, but works.
918         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
919         if (pagenum == i) {
920             found_grid = grid;
921             break; // break out of for-loop
922         }
923     }
924     if (found_grid) {
925         // delete the grid that corresponds with the selected tab
926         // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
927         found_grid->repr->parent()->removeChild(found_grid->repr);
928         sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
929     }
933 } // namespace Dialog
934 } // namespace UI
935 } // namespace Inkscape
937 /*
938   Local Variables:
939   mode:c++
940   c-file-style:"stroustrup"
941   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
942   indent-tabs-mode:nil
943   fill-column:99
944   End:
945 */
946 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :