Code

GSoC C++-ificiation merge and cleanup.
[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  *   Jon A. Cruz <jon@joncruz.org>
12  *   Abhishek Sharma
13  *
14  * Copyright (C) 2006-2008 Johan Engelen  <johan@shouraizou.nl>
15  * Copyright (C) 2000 - 2008 Authors
16  *
17  * Released under GNU GPL.  Read the file 'COPYING' for more information
18  */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include "display/canvas-grid.h"
25 #include "document-properties.h"
26 #include "document.h"
27 #include "desktop-handles.h"
28 #include "desktop.h"
29 #include <gtkmm.h>
30 #include "helper/units.h"
31 #include "inkscape.h"
32 #include "io/sys.h"
33 #include "preferences.h"
34 #include "sp-namedview.h"
35 #include "sp-object-repr.h"
36 #include "sp-root.h"
37 #include "sp-script.h"
38 #include "ui/widget/color-picker.h"
39 #include "ui/widget/scalar-unit.h"
40 #include "verbs.h"
41 #include "widgets/icon.h"
42 #include "xml/node-event-vector.h"
43 #include "xml/repr.h"
45 #if ENABLE_LCMS
46 #include <lcms.h>
47 //#include "color-profile-fns.h"
48 #include "color-profile.h"
49 #endif // ENABLE_LCMS
51 using std::pair;
53 namespace Inkscape {
54 namespace UI {
55 namespace Dialog {
57 #define SPACE_SIZE_X 15
58 #define SPACE_SIZE_Y 10
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_cms(1, 1), _page_scripting(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; only a small part of the guide near the cursor will snap)"),
103                   "inkscape:snap-from-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       _grids_label_crea("", Gtk::ALIGN_LEFT),
108       _grids_button_new(C_("Grid", "_New"), _("Create new grid.")),
109       _grids_button_remove(C_("Grid", "_Remove"), _("Remove selected grid.")),
110       _grids_label_def("", Gtk::ALIGN_LEFT)
111     //---------------------------------------------------------------
113     _tt.enable();
114     _getContents()->set_spacing (4);
115     _getContents()->pack_start(_notebook, true, true);
117     _notebook.append_page(_page_page,      _("Page"));
118     _notebook.append_page(_page_guides,    _("Guides"));
119     _notebook.append_page(_grids_vbox,     _("Grids"));
120     _notebook.append_page(_page_snap,      _("Snap"));
121     _notebook.append_page(_page_cms, _("Color Management"));
122     _notebook.append_page(_page_scripting, _("Scripting"));
124     build_page();
125     build_guides();
126     build_gridspage();
127     build_snap();
128 #if ENABLE_LCMS
129     build_cms();
130 #endif // ENABLE_LCMS
131     build_scripting();
133     _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
134     _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
136     signalDocumentReplaced().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDocumentReplaced));
137     signalActivateDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleActivateDesktop));
138     signalDeactiveDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDeactivateDesktop));
141 void
142 DocumentProperties::init()
144     update();
146     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
147     repr->addListener (&_repr_events, this);
148     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
149     root->addListener (&_repr_events, this);
151     show_all_children();
152     _grids_button_remove.hide();
155 DocumentProperties::~DocumentProperties()
157     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
158     repr->removeListenerByData (this);
159     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
160     root->removeListenerByData (this);
163 //========================================================================
165 /**
166  * Helper function that attaches widgets in a 3xn table. The widgets come in an
167  * array that has two entries per table row. The two entries code for four
168  * possible cases: (0,0) means insert space in first column; (0, non-0) means
169  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
170  * (non-0, non-0) means two widgets in columns 2 and 3.
171 **/
172 inline void
173 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
175     for (unsigned i = 0, r = start; i < n; i += 2)
176     {
177         if (arr[i] && arr[i+1])
178         {
179             table.attach(*arr[i],   1, 2, r, r+1,
180                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
181             table.attach(*arr[i+1], 2, 3, r, r+1,
182                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
183         }
184         else
185         {
186             if (arr[i+1]) {
187                 Gtk::AttachOptions yoptions = (Gtk::AttachOptions)0;
188                 if (dynamic_cast<Inkscape::UI::Widget::PageSizer*>(arr[i+1])) {
189                     // only the PageSizer in Document Properties|Page should be stretched vertically
190                     yoptions = Gtk::FILL|Gtk::EXPAND;
191                 }
192                 table.attach(*arr[i+1], 1, 3, r, r+1,
193                       Gtk::FILL|Gtk::EXPAND, yoptions, 0,0);
194             }
195             else if (arr[i])
196             {
197                 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
198                 label.set_alignment (0.0);
199                 table.attach (label, 0, 3, r, r+1,
200                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
201             }
202             else
203             {
204                 Gtk::HBox *space = manage (new Gtk::HBox);
205                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
206                 table.attach (*space, 0, 1, r, r+1,
207                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
208             }
209         }
210         ++r;
211     }
214 void
215 DocumentProperties::build_page()
217     _page_page.show();
219     Gtk::Label* label_gen = manage (new Gtk::Label);
220     label_gen->set_markup (_("<b>General</b>"));
221     Gtk::Label* label_bor = manage (new Gtk::Label);
222     label_bor->set_markup (_("<b>Border</b>"));
223     Gtk::Label *label_for = manage (new Gtk::Label);
224     label_for->set_markup (_("<b>Page Size</b>"));
225     _page_sizer.init();
227     Gtk::Widget *const widget_array[] =
228     {
229         label_gen,         0,
230         0,                 &_rum_deflt,
231         _rcp_bg._label,    &_rcp_bg,
232         0,                 0,
233         label_for,         0,
234         0,                 &_page_sizer,
235         0,                 0,
236         label_bor,         0,
237         0,                 &_rcb_canb,
238         0,                 &_rcb_bord,
239         0,                 &_rcb_shad,
240         _rcp_bord._label,  &_rcp_bord,
241     };
243     attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
246 void
247 DocumentProperties::build_guides()
249     _page_guides.show();
251     Gtk::Label *label_gui = manage (new Gtk::Label);
252     label_gui->set_markup (_("<b>Guides</b>"));
254     Gtk::Widget *const widget_array[] =
255     {
256         label_gui,        0,
257         0,                &_rcb_sgui,
258         _rcp_gui._label,  &_rcp_gui,
259         _rcp_hgui._label, &_rcp_hgui,
260         0,                &_rcbsng,
261     };
263     attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
266 void
267 DocumentProperties::build_snap()
269     _page_snap.show();
271     _rsu_sno.init (_("Snap _distance"), _("Snap only when _closer than:"), _("Always snap"),
272                   _("Snapping distance, in screen pixels, for snapping to objects"), _("Always snap to objects, regardless of their distance"),
273                   _("If set, objects only snap to another object when it's within the range specified below"),
274                   "objecttolerance", _wr);
276     //Options for snapping to grids
277     _rsu_sn.init (_("Snap d_istance"), _("Snap only when c_loser than:"), _("Always snap"),
278                   _("Snapping distance, in screen pixels, for snapping to grid"), _("Always snap to grids, regardless of the distance"),
279                   _("If set, objects only snap to a grid line when it's within the range specified below"),
280                   "gridtolerance", _wr);
282     //Options for snapping to guides
283     _rsu_gusn.init (_("Snap dist_ance"), _("Snap only when close_r than:"), _("Always snap"),
284                 _("Snapping distance, in screen pixels, for snapping to guides"), _("Always snap to guides, regardless of the distance"),
285                 _("If set, objects only snap to a guide when it's within the range specified below"),
286                 "guidetolerance", _wr);
288     Gtk::Label *label_o = manage (new Gtk::Label);
289     label_o->set_markup (_("<b>Snap to objects</b>"));
290     Gtk::Label *label_gr = manage (new Gtk::Label);
291     label_gr->set_markup (_("<b>Snap to grids</b>"));
292     Gtk::Label *label_gu = manage (new Gtk::Label);
293     label_gu->set_markup (_("<b>Snap to guides</b>"));
295     Gtk::Widget *const array[] =
296     {
297         label_o,            0,
298         0,                  _rsu_sno._vbox,
299         0,                  0,
300         label_gr,           0,
301         0,                  _rsu_sn._vbox,
302         0,                  0,
303         label_gu,           0,
304         0,                  _rsu_gusn._vbox
305     };
307     attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
308  }
310 #if ENABLE_LCMS
311 static void
312 lcms_profile_get_name (cmsHPROFILE   profile, const gchar **name)
314   if (profile)
315     {
316       *name = cmsTakeProductDesc (profile);
318       if (! *name)
319         *name = cmsTakeProductName (profile);
321       if (*name && ! g_utf8_validate (*name, -1, NULL))
322         *name = _("(invalid UTF-8 string)");
323     }
324   else
325     {
326       *name = _("None");
327     }
330 void
331 DocumentProperties::populate_available_profiles(){
332     Glib::ListHandle<Gtk::Widget*> children = _menu.get_children();
333     for ( Glib::ListHandle<Gtk::Widget*>::iterator it2 = children.begin(); it2 != children.end(); ++it2 ) {
334         _menu.remove(**it2);
335         delete(*it2);
336     }
338     std::list<Glib::ustring> files = ColorProfile::getProfileFiles();
339     for ( std::list<Glib::ustring>::const_iterator it = files.begin(); it != files.end(); ++it ) {
340         cmsHPROFILE hProfile = cmsOpenProfileFromFile(it->c_str(), "r");
341         if ( hProfile ){
342             const gchar* name = 0;
343             lcms_profile_get_name(hProfile, &name);
344             Gtk::MenuItem* mi = manage(new Gtk::MenuItem());
345             mi->set_data("filepath", g_strdup(it->c_str()));
346             mi->set_data("name", g_strdup(name));
347             Gtk::HBox *hbox = manage(new Gtk::HBox());
348             hbox->show();
349             Gtk::Label* lbl = manage(new Gtk::Label(name));
350             lbl->show();
351             hbox->pack_start(*lbl, true, true, 0);
352             mi->add(*hbox);
353             mi->show_all();
354             _menu.append(*mi);
355 //            g_free((void*)name);
356             cmsCloseProfile(hProfile);
357         }
358     }
360     _menu.show_all();
363 /**
364  * Cleans up name to remove disallowed characters.
365  * Some discussion at http://markmail.org/message/bhfvdfptt25kgtmj
366  * Allowed ASCII first characters:  ':', 'A'-'Z', '_', 'a'-'z'
367  * Allowed ASCII remaining chars add: '-', '.', '0'-'9', 
368  *
369  * @param str the string to clean up.
370  */
371 static void sanitizeName( Glib::ustring& str )
373     if (str.size() > 1) {
374         char val = str.at(0);
375         if (((val < 'A') || (val > 'Z'))
376             && ((val < 'a') || (val > 'z'))
377             && (val != '_')
378             && (val != ':')) {
379             str.replace(0, 1, "-");
380         }
381         for (Glib::ustring::size_type i = 1; i < str.size(); i++) {
382             char val = str.at(i);
383             if (((val < 'A') || (val > 'Z'))
384                 && ((val < 'a') || (val > 'z'))
385                 && ((val < '0') || (val > '9'))
386                 && (val != '_')
387                 && (val != ':')
388                 && (val != '-')
389                 && (val != '.')) {
390                 str.replace(i, 1, "-");
391             }
392         }
393     }
396 void
397 DocumentProperties::linkSelectedProfile()
399 //store this profile in the SVG document (create <color-profile> element in the XML)
400     // TODO remove use of 'active' desktop
401     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
402     if (!desktop){
403         g_warning("No active desktop");
404     } else {
405         if (!_menu.get_active()){
406             g_warning("No color profile available.");
407             return;
408         }
409         Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
410         Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
411         gchar* tmp = static_cast<gchar*>(_menu.get_active()->get_data("name"));
412         Glib::ustring nameStr = tmp ? tmp : "profile"; // TODO add some auto-numbering to avoid collisions
413         sanitizeName(nameStr);
414         cprofRepr->setAttribute("name", nameStr.c_str());
415         cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
417         // Checks whether there is a defs element. Creates it when needed
418         Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
419         if (!defsRepr){
420             defsRepr = xml_doc->createElement("svg:defs");
421             xml_doc->root()->addChild(defsRepr, NULL);
422         }
424         g_assert(SP_ROOT(desktop->doc()->root)->defs);
425         defsRepr->addChild(cprofRepr, NULL);
427         // TODO check if this next line was sometimes needed. It being there caused an assertion.
428         //Inkscape::GC::release(defsRepr);
430         // inform the document, so we can undo
431         DocumentUndo::done(desktop->doc(), SP_VERB_EDIT_LINK_COLOR_PROFILE, _("Link Color Profile"));
433         populate_linked_profiles_box();
434     }
437 void
438 DocumentProperties::populate_linked_profiles_box()
440     _LinkedProfilesListStore->clear();
441     const GSList *current = SP_ACTIVE_DOCUMENT->getResourceList( "iccprofile" );
442     if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
443     while ( current ) {
444         SPObject* obj = SP_OBJECT(current->data);
445         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
446         Gtk::TreeModel::Row row = *(_LinkedProfilesListStore->append());
447         row[_LinkedProfilesListColumns.nameColumn] = prof->name;
448 //        row[_LinkedProfilesListColumns.previewColumn] = "Color Preview";
449         current = g_slist_next(current);
450     }
453 void DocumentProperties::external_scripts_list_button_release(GdkEventButton* event)
455     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
456         _ExternalScriptsContextMenu.popup(event->button, event->time);
457     }
460 void DocumentProperties::linked_profiles_list_button_release(GdkEventButton* event)
462     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
463         _EmbProfContextMenu.popup(event->button, event->time);
464     }
467 void DocumentProperties::cms_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
469     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
470     _EmbProfContextMenu.append(*mi);
471     mi->signal_activate().connect(rem);
472     mi->show();
473     _EmbProfContextMenu.accelerate(parent);
477 void DocumentProperties::scripting_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
479     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
480     _ExternalScriptsContextMenu.append(*mi);
481     mi->signal_activate().connect(rem);
482     mi->show();
483     _ExternalScriptsContextMenu.accelerate(parent);
486 void DocumentProperties::removeSelectedProfile(){
487     Glib::ustring name;
488     if(_LinkedProfilesList.get_selection()) {
489         Gtk::TreeModel::iterator i = _LinkedProfilesList.get_selection()->get_selected();
491         if(i){
492             name = (*i)[_LinkedProfilesListColumns.nameColumn];
493         } else {
494             return;
495         }
496     }
498     const GSList *current = SP_ACTIVE_DOCUMENT->getResourceList( "iccprofile" );
499     while ( current ) {
500         SPObject* obj = SP_OBJECT(current->data);
501         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
502         if (!name.compare(prof->name)){
504             //XML Tree being used directly here while it shouldn't be.
505             sp_repr_unparent(obj->getRepr());
506             DocumentUndo::done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove linked color profile"));
507         }
508         current = g_slist_next(current);
509     }
511     populate_linked_profiles_box();
514 void
515 DocumentProperties::build_cms()
517     _page_cms.show();
519     Gtk::Label *label_link= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
520     label_link->set_markup (_("<b>Linked Color Profiles:</b>"));
521     Gtk::Label *label_avail = manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
522     label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
524     _link_btn.set_label(_("Link Profile"));
526     _page_cms.set_spacing(4);
527     gint row = 0;
529     label_link->set_alignment(0.0);
530     _page_cms.table().attach(*label_link, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
531     row++;
532     _page_cms.table().attach(_LinkedProfilesListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
533     row++;
535     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
536     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
537     _page_cms.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
538     row++;
540     label_avail->set_alignment(0.0);
541     _page_cms.table().attach(*label_avail, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
542     row++;
543     _page_cms.table().attach(_combo_avail, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
544     _page_cms.table().attach(_link_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
546     populate_available_profiles();
548     _combo_avail.set_menu(_menu);
549     _combo_avail.set_history(0);
550     _combo_avail.show_all();
552     //# Set up the Linked Profiles combo box
553     _LinkedProfilesListStore = Gtk::ListStore::create(_LinkedProfilesListColumns);
554     _LinkedProfilesList.set_model(_LinkedProfilesListStore);
555     _LinkedProfilesList.append_column(_("Profile Name"), _LinkedProfilesListColumns.nameColumn);
556 //    _LinkedProfilesList.append_column(_("Color Preview"), _LinkedProfilesListColumns.previewColumn);
557     _LinkedProfilesList.set_headers_visible(false);
558 // TODO restore?    _LinkedProfilesList.set_fixed_height_mode(true);
560     populate_linked_profiles_box();
562     _LinkedProfilesListScroller.add(_LinkedProfilesList);
563     _LinkedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
564     _LinkedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
565     _LinkedProfilesListScroller.set_size_request(-1, 90);
567     _link_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::linkSelectedProfile));
569     _LinkedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::linked_profiles_list_button_release));
570     cms_create_popup_menu(_LinkedProfilesList, sigc::mem_fun(*this, &DocumentProperties::removeSelectedProfile));
572     const GSList *current = SP_ACTIVE_DOCUMENT->getResourceList( "defs" );
573     if (current) {
574         _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
575     }
576     _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_linked_profiles_box));
578 #endif // ENABLE_LCMS
580 void
581 DocumentProperties::build_scripting()
583     _page_scripting.show();
585     Gtk::Label *label_script= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
586     label_script->set_markup (_("<b>External script files:</b>"));
588     _add_btn.set_label(_("Add"));
590     _page_scripting.set_spacing(4);
591     gint row = 0;
593     label_script->set_alignment(0.0);
594     _page_scripting.table().attach(*label_script, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
595     row++;
596     _page_scripting.table().attach(_ExternalScriptsListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
597     row++;
599     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
600     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
601     _page_scripting.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
602     row++;
604     _page_scripting.table().attach(_script_entry, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
605     _page_scripting.table().attach(_add_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
606     row++;
608     //# Set up the External Scripts box
609     _ExternalScriptsListStore = Gtk::ListStore::create(_ExternalScriptsListColumns);
610     _ExternalScriptsList.set_model(_ExternalScriptsListStore);
611     _ExternalScriptsList.append_column(_("Filename"), _ExternalScriptsListColumns.filenameColumn);
612     _ExternalScriptsList.set_headers_visible(true);
613 // TODO restore?    _ExternalScriptsList.set_fixed_height_mode(true);
615     populate_external_scripts_box();
617     _ExternalScriptsListScroller.add(_ExternalScriptsList);
618     _ExternalScriptsListScroller.set_shadow_type(Gtk::SHADOW_IN);
619     _ExternalScriptsListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
620     _ExternalScriptsListScroller.set_size_request(-1, 90);
622     _add_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::addExternalScript));
624 #if ENABLE_LCMS
625     _ExternalScriptsList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::external_scripts_list_button_release));
626     scripting_create_popup_menu(_ExternalScriptsList, sigc::mem_fun(*this, &DocumentProperties::removeExternalScript));
627 #endif // ENABLE_LCMS
629 //TODO: review this observers code:
630     const GSList *current = SP_ACTIVE_DOCUMENT->getResourceList( "script" );
631     if (current) {
632         _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
633     }
634     _ext_scripts_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_external_scripts_box));
638 void DocumentProperties::addExternalScript(){
639     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
640     if (!desktop){
641         g_warning("No active desktop");
642     } else {
643         Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
644         Inkscape::XML::Node *scriptRepr = xml_doc->createElement("svg:script");
645         scriptRepr->setAttribute("xlink:href", (gchar*) _script_entry.get_text().c_str());
646         _script_entry.set_text("");
648         xml_doc->root()->addChild(scriptRepr, NULL);
650         // inform the document, so we can undo
651         DocumentUndo::done(desktop->doc(), SP_VERB_EDIT_ADD_EXTERNAL_SCRIPT, _("Add external script..."));
653         populate_external_scripts_box();
654     }
657 void DocumentProperties::removeExternalScript(){
658     Glib::ustring name;
659     if(_ExternalScriptsList.get_selection()) {
660         Gtk::TreeModel::iterator i = _ExternalScriptsList.get_selection()->get_selected();
662         if(i){
663             name = (*i)[_ExternalScriptsListColumns.filenameColumn];
664         } else {
665             return;
666         }
667     }
669     const GSList *current = SP_ACTIVE_DOCUMENT->getResourceList( "script" );
670     while ( current ) {
671         SPObject* obj = SP_OBJECT(current->data);
672         SPScript* script = (SPScript*) obj;
673         if (name == script->xlinkhref){
675             //XML Tree being used directly here while it shouldn't be.
676             sp_repr_unparent(obj->getRepr());
677             DocumentUndo::done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_EXTERNAL_SCRIPT, _("Remove external script"));
678         }
679         current = g_slist_next(current);
680     }
682     populate_external_scripts_box();
686 void DocumentProperties::populate_external_scripts_box(){
687     _ExternalScriptsListStore->clear();
688     const GSList *current = SP_ACTIVE_DOCUMENT->getResourceList( "script" );
689     if (current) _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
690     while ( current ) {
691         SPObject* obj = SP_OBJECT(current->data);
692         SPScript* script = (SPScript*) obj;
693         if (script->xlinkhref)
694         {
695             Gtk::TreeModel::Row row = *(_ExternalScriptsListStore->append());
696             row[_ExternalScriptsListColumns.filenameColumn] = script->xlinkhref;
697         }
699         current = g_slist_next(current);
700     }
703 /**
704 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
705 */
706 void
707 DocumentProperties::update_gridspage()
709     SPDesktop *dt = getDesktop();
710     SPNamedView *nv = sp_desktop_namedview(dt);
712     //remove all tabs
713     while (_grids_notebook.get_n_pages() != 0) {
714         _grids_notebook.remove_page(-1); // this also deletes the page.
715     }
717     //add tabs
718     bool grids_present = false;
719     for (GSList const * l = nv->grids; l != NULL; l = l->next) {
720         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
721         if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
722         Glib::ustring name(grid->repr->attribute("id"));
723         const char *icon = NULL;
724         switch (grid->getGridType()) {
725             case GRID_RECTANGULAR:
726                 icon = "grid-rectangular";
727                 break;
728             case GRID_AXONOMETRIC:
729                 icon = "grid-axonometric";
730                 break;
731             default:
732                 break;
733         }
734         _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
735         grids_present = true;
736     }
737     _grids_notebook.show_all();
739     if (grids_present)
740         _grids_button_remove.set_sensitive(true);
741     else
742         _grids_button_remove.set_sensitive(false);
745 /**
746  * Build grid page of dialog.
747  */
748 void
749 DocumentProperties::build_gridspage()
751     /// \todo FIXME: gray out snapping when grid is off.
752     /// Dissenting view: you want snapping without grid.
754     SPDesktop *dt = getDesktop();
755     SPNamedView *nv = sp_desktop_namedview(dt);
756     (void)nv;
758     _grids_label_crea.set_markup(_("<b>Creation</b>"));
759     _grids_label_def.set_markup(_("<b>Defined grids</b>"));
760     _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
761     _grids_hbox_crea.pack_start(_grids_button_new, true, true);
763     for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
764         _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
765     }
766     _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
768     _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
770     _grids_vbox.set_spacing(4);
771     _grids_vbox.pack_start(_grids_label_crea, false, false);
772     _grids_vbox.pack_start(_grids_hbox_crea, false, false);
773     _grids_vbox.pack_start(_grids_space, false, false);
774     _grids_vbox.pack_start(_grids_label_def, false, false);
775     _grids_vbox.pack_start(_grids_notebook, false, false);
776     _grids_vbox.pack_start(_grids_button_remove, false, false);
778     update_gridspage();
783 /**
784  * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
785  */
786 void
787 DocumentProperties::update()
789     if (_wr.isUpdating()) return;
791     SPDesktop *dt = getDesktop();
792     SPNamedView *nv = sp_desktop_namedview(dt);
794     _wr.setUpdating (true);
795     set_sensitive (true);
797     //-----------------------------------------------------------page page
798     _rcp_bg.setRgba32 (nv->pagecolor);
799     _rcb_canb.setActive (nv->showborder);
800     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
801     _rcp_bord.setRgba32 (nv->bordercolor);
802     _rcb_shad.setActive (nv->showpageshadow);
804     if (nv->doc_units)
805         _rum_deflt.setUnit (nv->doc_units);
807     double const doc_w_px = sp_desktop_document(dt)->getWidth();
808     double const doc_h_px = sp_desktop_document(dt)->getHeight();
809     _page_sizer.setDim (doc_w_px, doc_h_px);
810     _page_sizer.updateFitMarginsUI(SP_OBJECT_REPR(nv));
812     //-----------------------------------------------------------guide page
814     _rcb_sgui.setActive (nv->showguides);
815     _rcp_gui.setRgba32 (nv->guidecolor);
816     _rcp_hgui.setRgba32 (nv->guidehicolor);
817     _rcbsng.setActive(nv->snap_manager.snapprefs.getSnapModeGuide());
819     //-----------------------------------------------------------snap page
821     _rsu_sno.setValue (nv->snap_manager.snapprefs.getObjectTolerance());
822     _rsu_sn.setValue (nv->snap_manager.snapprefs.getGridTolerance());
823     _rsu_gusn.setValue (nv->snap_manager.snapprefs.getGuideTolerance());
826     //-----------------------------------------------------------grids page
828     update_gridspage();
830     //------------------------------------------------Color Management page
832 #if ENABLE_LCMS
833     populate_linked_profiles_box();
834     populate_available_profiles();
835 #endif // ENABLE_LCMS
837     _wr.setUpdating (false);
840 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
841 Gtk::HBox&
842 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
844     Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
845     _tab_label_box->set_spacing(4);
846     _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
847                                                        label_image)));
849     Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
850     _tab_label_box->pack_start(*_tab_label);
851     _tab_label_box->show_all();
853     return *_tab_label_box;
856 //--------------------------------------------------------------------
858 void
859 DocumentProperties::on_response (int id)
861     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
862     {
863         _rcp_bg.closeWindow();
864         _rcp_bord.closeWindow();
865         _rcp_gui.closeWindow();
866         _rcp_hgui.closeWindow();
867     }
869     if (id == Gtk::RESPONSE_CLOSE)
870         hide();
873 void
874 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
876     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
877     repr->addListener(&_repr_events, this);
878     Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
879     root->addListener(&_repr_events, this);
880     update();
883 void
884 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
886     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
887     repr->addListener(&_repr_events, this);
888     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
889     root->addListener(&_repr_events, this);
890     update();
893 void
894 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
896     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
897     repr->removeListenerByData(this);
898     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
899     root->removeListenerByData(this);
902 static void
903 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
905     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
906         dialog->update_gridspage();
909 static void
910 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
912     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
913         dialog->update_gridspage();
918 /**
919  * Called when XML node attribute changed; updates dialog widgets.
920  */
921 static void
922 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
924     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
925         dialog->update();
929 /*########################################################################
930 # BUTTON CLICK HANDLERS    (callbacks)
931 ########################################################################*/
933 void
934 DocumentProperties::onNewGrid()
936     SPDesktop *dt = getDesktop();
937     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
938     SPDocument *doc = sp_desktop_document(dt);
940     Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
941     CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
943     // toggle grid showing to ON:
944     dt->showGrids(true);
948 void
949 DocumentProperties::onRemoveGrid()
951     gint pagenum = _grids_notebook.get_current_page();
952     if (pagenum == -1) // no pages
953       return;
955     SPDesktop *dt = getDesktop();
956     SPNamedView *nv = sp_desktop_namedview(dt);
957     Inkscape::CanvasGrid * found_grid = NULL;
958     int i = 0;
959     for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) {  // not a very nice fix, but works.
960         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
961         if (pagenum == i) {
962             found_grid = grid;
963             break; // break out of for-loop
964         }
965     }
966     if (found_grid) {
967         // delete the grid that corresponds with the selected tab
968         // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
969         found_grid->repr->parent()->removeChild(found_grid->repr);
970         DocumentUndo::done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
971     }
975 } // namespace Dialog
976 } // namespace UI
977 } // namespace Inkscape
979 /*
980   Local Variables:
981   mode:c++
982   c-file-style:"stroustrup"
983   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
984   indent-tabs-mode:nil
985   fill-column:99
986   End:
987 */
988 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :