Code

C++fied SPDocument added
[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 "sp-script.h"
36 #include "ui/widget/color-picker.h"
37 #include "ui/widget/scalar-unit.h"
38 #include "verbs.h"
39 #include "widgets/icon.h"
40 #include "xml/node-event-vector.h"
41 #include "xml/repr.h"
43 #if ENABLE_LCMS
44 #include <lcms.h>
45 //#include "color-profile-fns.h"
46 #include "color-profile.h"
47 #endif // ENABLE_LCMS
49 using std::pair;
51 namespace Inkscape {
52 namespace UI {
53 namespace Dialog {
55 #define SPACE_SIZE_X 15
56 #define SPACE_SIZE_Y 10
59 //===================================================
61 //---------------------------------------------------
63 static void on_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
64 static void on_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
65 static void on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer);
67 static Inkscape::XML::NodeEventVector const _repr_events = {
68     on_child_added, // child_added
69     on_child_removed, // child_removed
70     on_repr_attr_changed,
71     NULL, // content_changed
72     NULL  // order_changed
73 };
76 DocumentProperties &
77 DocumentProperties::getInstance()
78 {
79     DocumentProperties &instance = *new DocumentProperties();
80     instance.init();
82     return instance;
83 }
85 DocumentProperties::DocumentProperties()
86     : UI::Widget::Panel ("", "/dialogs/documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
87       _page_page(1, 1, true, true), _page_guides(1, 1),
88       _page_snap(1, 1), _page_cms(1, 1), _page_scripting(1, 1),
89     //---------------------------------------------------------------
90       _rcb_canb(_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false),
91       _rcb_bord(_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false),
92       _rcb_shad(_("_Show border shadow"), _("If set, page border shows a shadow on its right and lower side"), "inkscape:showpageshadow", _wr, false),
93       _rcp_bg(_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"), "pagecolor", "inkscape:pageopacity", _wr),
94       _rcp_bord(_("Border _color:"), _("Page border color"), _("Color of the page border"), "bordercolor", "borderopacity", _wr),
95       _rum_deflt(_("Default _units:"), "inkscape:document-units", _wr),
96       _page_sizer(_wr),
97     //---------------------------------------------------------------
98       //General snap options
99       _rcb_sgui(_("Show _guides"), _("Show or hide guides"), "showguides", _wr),
100       _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)"),
101                   "inkscape:snap-from-guide", _wr),
102       _rcp_gui(_("Guide co_lor:"), _("Guideline color"), _("Color of guidelines"), "guidecolor", "guideopacity", _wr),
103       _rcp_hgui(_("_Highlight color:"), _("Highlighted guideline color"), _("Color of a guideline when it is under mouse"), "guidehicolor", "guidehiopacity", _wr),
104     //---------------------------------------------------------------
105       _grids_label_crea("", Gtk::ALIGN_LEFT),
106       //TRANSLATORS: only translate "string" in "context|string".
107       // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
108       // "New" refers to grid
109       _grids_button_new(Q_("Grid|_New"), _("Create new grid.")),
110       _grids_button_remove(_("_Remove"), _("Remove selected grid.")),
111       _grids_label_def("", Gtk::ALIGN_LEFT)
112     //---------------------------------------------------------------
114     _tt.enable();
115     _getContents()->set_spacing (4);
116     _getContents()->pack_start(_notebook, true, true);
118     _notebook.append_page(_page_page,      _("Page"));
119     _notebook.append_page(_page_guides,    _("Guides"));
120     _notebook.append_page(_grids_vbox,     _("Grids"));
121     _notebook.append_page(_page_snap,      _("Snap"));
122     _notebook.append_page(_page_cms, _("Color Management"));
123     _notebook.append_page(_page_scripting, _("Scripting"));
125     build_page();
126     build_guides();
127     build_gridspage();
128     build_snap();
129 #if ENABLE_LCMS
130     build_cms();
131 #endif // ENABLE_LCMS
132     build_scripting();
134     _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
135     _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
137     signalDocumentReplaced().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDocumentReplaced));
138     signalActivateDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleActivateDesktop));
139     signalDeactiveDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDeactivateDesktop));
142 void
143 DocumentProperties::init()
145     update();
147     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
148     repr->addListener (&_repr_events, this);
149     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
150     root->addListener (&_repr_events, this);
152     show_all_children();
153     _grids_button_remove.hide();
156 DocumentProperties::~DocumentProperties()
158     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
159     repr->removeListenerByData (this);
160     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
161     root->removeListenerByData (this);
164 //========================================================================
166 /**
167  * Helper function that attaches widgets in a 3xn table. The widgets come in an
168  * array that has two entries per table row. The two entries code for four
169  * possible cases: (0,0) means insert space in first column; (0, non-0) means
170  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
171  * (non-0, non-0) means two widgets in columns 2 and 3.
172 **/
173 inline void
174 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
176     for (unsigned i = 0, r = start; i < n; i += 2)
177     {
178         if (arr[i] && arr[i+1])
179         {
180             table.attach(*arr[i],   1, 2, r, r+1,
181                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
182             table.attach(*arr[i+1], 2, 3, r, r+1,
183                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
184         }
185         else
186         {
187             if (arr[i+1]) {
188                 Gtk::AttachOptions yoptions = (Gtk::AttachOptions)0;
189                 if (dynamic_cast<Inkscape::UI::Widget::PageSizer*>(arr[i+1])) {
190                     // only the PageSizer in Document Properties|Page should be stretched vertically
191                     yoptions = Gtk::FILL|Gtk::EXPAND;
192                 }
193                 table.attach(*arr[i+1], 1, 3, r, r+1,
194                       Gtk::FILL|Gtk::EXPAND, yoptions, 0,0);
195             }
196             else if (arr[i])
197             {
198                 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
199                 label.set_alignment (0.0);
200                 table.attach (label, 0, 3, r, r+1,
201                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
202             }
203             else
204             {
205                 Gtk::HBox *space = manage (new Gtk::HBox);
206                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
207                 table.attach (*space, 0, 1, r, r+1,
208                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
209             }
210         }
211         ++r;
212     }
215 void
216 DocumentProperties::build_page()
218     _page_page.show();
220     Gtk::Label* label_gen = manage (new Gtk::Label);
221     label_gen->set_markup (_("<b>General</b>"));
222     Gtk::Label* label_bor = manage (new Gtk::Label);
223     label_bor->set_markup (_("<b>Border</b>"));
224     Gtk::Label *label_for = manage (new Gtk::Label);
225     label_for->set_markup (_("<b>Page Size</b>"));
226     _page_sizer.init();
228     Gtk::Widget *const widget_array[] =
229     {
230         label_gen,         0,
231         0,                 &_rum_deflt,
232         _rcp_bg._label,    &_rcp_bg,
233         0,                 0,
234         label_for,         0,
235         0,                 &_page_sizer,
236         0,                 0,
237         label_bor,         0,
238         0,                 &_rcb_canb,
239         0,                 &_rcb_bord,
240         0,                 &_rcb_shad,
241         _rcp_bord._label,  &_rcp_bord,
242     };
244     attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
247 void
248 DocumentProperties::build_guides()
250     _page_guides.show();
252     Gtk::Label *label_gui = manage (new Gtk::Label);
253     label_gui->set_markup (_("<b>Guides</b>"));
255     Gtk::Widget *const widget_array[] =
256     {
257         label_gui,        0,
258         0,                &_rcb_sgui,
259         _rcp_gui._label,  &_rcp_gui,
260         _rcp_hgui._label, &_rcp_hgui,
261         0,                &_rcbsng,
262     };
264     attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
267 void
268 DocumentProperties::build_snap()
270     _page_snap.show();
272     _rsu_sno.init (_("Snap _distance"), _("Snap only when _closer than:"), _("Always snap"),
273                   _("Snapping distance, in screen pixels, for snapping to objects"), _("Always snap to objects, regardless of their distance"),
274                   _("If set, objects only snap to another object when it's within the range specified below"),
275                   "objecttolerance", _wr);
277     //Options for snapping to grids
278     _rsu_sn.init (_("Snap d_istance"), _("Snap only when c_loser than:"), _("Always snap"),
279                   _("Snapping distance, in screen pixels, for snapping to grid"), _("Always snap to grids, regardless of the distance"),
280                   _("If set, objects only snap to a grid line when it's within the range specified below"),
281                   "gridtolerance", _wr);
283     //Options for snapping to guides
284     _rsu_gusn.init (_("Snap dist_ance"), _("Snap only when close_r than:"), _("Always snap"),
285                 _("Snapping distance, in screen pixels, for snapping to guides"), _("Always snap to guides, regardless of the distance"),
286                 _("If set, objects only snap to a guide when it's within the range specified below"),
287                 "guidetolerance", _wr);
289     Gtk::Label *label_o = manage (new Gtk::Label);
290     label_o->set_markup (_("<b>Snap to objects</b>"));
291     Gtk::Label *label_gr = manage (new Gtk::Label);
292     label_gr->set_markup (_("<b>Snap to grids</b>"));
293     Gtk::Label *label_gu = manage (new Gtk::Label);
294     label_gu->set_markup (_("<b>Snap to guides</b>"));
296     Gtk::Widget *const array[] =
297     {
298         label_o,            0,
299         0,                  _rsu_sno._vbox,
300         0,                  0,
301         label_gr,           0,
302         0,                  _rsu_sn._vbox,
303         0,                  0,
304         label_gu,           0,
305         0,                  _rsu_gusn._vbox
306     };
308     attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
309  }
311 #if ENABLE_LCMS
312 static void
313 lcms_profile_get_name (cmsHPROFILE   profile, const gchar **name)
315   if (profile)
316     {
317       *name = cmsTakeProductDesc (profile);
319       if (! *name)
320         *name = cmsTakeProductName (profile);
322       if (*name && ! g_utf8_validate (*name, -1, NULL))
323         *name = _("(invalid UTF-8 string)");
324     }
325   else
326     {
327       *name = _("None");
328     }
331 void
332 DocumentProperties::populate_available_profiles(){
333     Glib::ListHandle<Gtk::Widget*> children = _menu.get_children();
334     for ( Glib::ListHandle<Gtk::Widget*>::iterator it2 = children.begin(); it2 != children.end(); ++it2 ) {
335         _menu.remove(**it2);
336         delete(*it2);
337     }
339     std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
341     // Use this loop to iterate through a list of possible document locations.
342     for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
343         if ( Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_EXISTS )
344              && Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_IS_DIR )) {
345             GError *err = 0;
346             GDir *directory = g_dir_open(it->c_str(), 0, &err);
347             if (!directory) {
348                 gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str());
349                 g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
350                 g_free(safeDir);
351             } else {
352                 gchar *filename = 0;
353                 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
354                     gchar* full = g_build_filename(it->c_str(), filename, NULL);
355                     if ( !Inkscape::IO::file_test( full, G_FILE_TEST_IS_DIR ) ) {
356                         cmsErrorAction( LCMS_ERROR_SHOW );
357                         cmsHPROFILE hProfile = cmsOpenProfileFromFile(full, "r");
358                         if (hProfile != NULL){
359                             const gchar* name;
360                             lcms_profile_get_name(hProfile, &name);
361                             Gtk::MenuItem* mi = manage(new Gtk::MenuItem());
362                             mi->set_data("filepath", g_strdup(full));
363                             mi->set_data("name", g_strdup(name));
364                             Gtk::HBox *hbox = manage(new Gtk::HBox());
365                             hbox->show();
366                             Gtk::Label* lbl = manage(new Gtk::Label(name));
367                             lbl->show();
368                             hbox->pack_start(*lbl, true, true, 0);
369                             mi->add(*hbox);
370                             mi->show_all();
371                             _menu.append(*mi);
372         //                    g_free((void*)name);
373                             cmsCloseProfile(hProfile);
374                         }
375                     }
376                     g_free(full);
377                 }
378                 g_dir_close(directory);
379             }
380         }
381     }
382     _menu.show_all();
385 /**
386  * Cleans up name to remove disallowed characters.
387  * Some discussion at http://markmail.org/message/bhfvdfptt25kgtmj
388  * Allowed ASCII first characters:  ':', 'A'-'Z', '_', 'a'-'z'
389  * Allowed ASCII remaining chars add: '-', '.', '0'-'9', 
390  *
391  * @param str the string to clean up.
392  */
393 static void sanitizeName( Glib::ustring& str )
395     if (str.size() > 1) {
396         char val = str.at(0);
397         if (((val < 'A') || (val > 'Z'))
398             && ((val < 'a') || (val > 'z'))
399             && (val != '_')
400             && (val != ':')) {
401             str.replace(0, 1, "-");
402         }
403         for (Glib::ustring::size_type i = 1; i < str.size(); i++) {
404             char val = str.at(i);
405             if (((val < 'A') || (val > 'Z'))
406                 && ((val < 'a') || (val > 'z'))
407                 && ((val < '0') || (val > '9'))
408                 && (val != '_')
409                 && (val != ':')
410                 && (val != '-')
411                 && (val != '.')) {
412                 str.replace(i, 1, "-");
413             }
414         }
415     }
418 void
419 DocumentProperties::linkSelectedProfile()
421 //store this profile in the SVG document (create <color-profile> element in the XML)
422     // TODO remove use of 'active' desktop
423     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
424     if (!desktop){
425         g_warning("No active desktop");
426     } else {
427         if (!_menu.get_active()){
428             g_warning("No color profile available.");
429             return;
430         }
431         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
432         Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
433         gchar* tmp = static_cast<gchar*>(_menu.get_active()->get_data("name"));
434         Glib::ustring nameStr = tmp ? tmp : "profile"; // TODO add some auto-numbering to avoid collisions
435         sanitizeName(nameStr);
436         cprofRepr->setAttribute("name", nameStr.c_str());
437         cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
439         // Checks whether there is a defs element. Creates it when needed
440         Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
441         if (!defsRepr){
442             defsRepr = xml_doc->createElement("svg:defs");
443             xml_doc->root()->addChild(defsRepr, NULL);
444         }
446         g_assert(SP_ROOT(desktop->doc()->root)->defs);
447         defsRepr->addChild(cprofRepr, NULL);
449         // TODO check if this next line was sometimes needed. It being there caused an assertion.
450         //Inkscape::GC::release(defsRepr);
452         // inform the document, so we can undo
453         sp_document_done(desktop->doc(), SP_VERB_EDIT_LINK_COLOR_PROFILE, _("Link Color Profile"));
455         populate_linked_profiles_box();
456     }
459 void
460 DocumentProperties::populate_linked_profiles_box()
462     _LinkedProfilesListStore->clear();
463     const GSList *current = SP_ACTIVE_DOCUMENT->get_resource_list( "iccprofile" );
464     if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
465     while ( current ) {
466         SPObject* obj = SP_OBJECT(current->data);
467         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
468         Gtk::TreeModel::Row row = *(_LinkedProfilesListStore->append());
469         row[_LinkedProfilesListColumns.nameColumn] = prof->name;
470 //        row[_LinkedProfilesListColumns.previewColumn] = "Color Preview";
471         current = g_slist_next(current);
472     }
475 void DocumentProperties::external_scripts_list_button_release(GdkEventButton* event)
477     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
478         _ExternalScriptsContextMenu.popup(event->button, event->time);
479     }
482 void DocumentProperties::linked_profiles_list_button_release(GdkEventButton* event)
484     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
485         _EmbProfContextMenu.popup(event->button, event->time);
486     }
489 void DocumentProperties::cms_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
491     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
492     _EmbProfContextMenu.append(*mi);
493     mi->signal_activate().connect(rem);
494     mi->show();
495     _EmbProfContextMenu.accelerate(parent);
499 void DocumentProperties::scripting_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
501     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
502     _ExternalScriptsContextMenu.append(*mi);
503     mi->signal_activate().connect(rem);
504     mi->show();
505     _ExternalScriptsContextMenu.accelerate(parent);
508 void DocumentProperties::removeSelectedProfile(){
509     Glib::ustring name;
510     if(_LinkedProfilesList.get_selection()) {
511         Gtk::TreeModel::iterator i = _LinkedProfilesList.get_selection()->get_selected();
513         if(i){
514             name = (*i)[_LinkedProfilesListColumns.nameColumn];
515         } else {
516             return;
517         }
518     }
520     const GSList *current = SP_ACTIVE_DOCUMENT->get_resource_list( "iccprofile" );
521     while ( current ) {
522         SPObject* obj = SP_OBJECT(current->data);
523         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
524         if (!name.compare(prof->name)){
525             sp_repr_unparent(obj->repr);
526             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove linked color profile"));
527         }
528         current = g_slist_next(current);
529     }
531     populate_linked_profiles_box();
534 void
535 DocumentProperties::build_cms()
537     _page_cms.show();
539     Gtk::Label *label_link= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
540     label_link->set_markup (_("<b>Linked Color Profiles:</b>"));
541     Gtk::Label *label_avail = manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
542     label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
544     _link_btn.set_label(_("Link Profile"));
546     _page_cms.set_spacing(4);
547     gint row = 0;
549     label_link->set_alignment(0.0);
550     _page_cms.table().attach(*label_link, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
551     row++;
552     _page_cms.table().attach(_LinkedProfilesListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
553     row++;
555     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
556     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
557     _page_cms.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
558     row++;
560     label_avail->set_alignment(0.0);
561     _page_cms.table().attach(*label_avail, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
562     row++;
563     _page_cms.table().attach(_combo_avail, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
564     _page_cms.table().attach(_link_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
566     populate_available_profiles();
568     _combo_avail.set_menu(_menu);
569     _combo_avail.set_history(0);
570     _combo_avail.show_all();
572     //# Set up the Linked Profiles combo box
573     _LinkedProfilesListStore = Gtk::ListStore::create(_LinkedProfilesListColumns);
574     _LinkedProfilesList.set_model(_LinkedProfilesListStore);
575     _LinkedProfilesList.append_column(_("Profile Name"), _LinkedProfilesListColumns.nameColumn);
576 //    _LinkedProfilesList.append_column(_("Color Preview"), _LinkedProfilesListColumns.previewColumn);
577     _LinkedProfilesList.set_headers_visible(false);
578 // TODO restore?    _LinkedProfilesList.set_fixed_height_mode(true);
580     populate_linked_profiles_box();
582     _LinkedProfilesListScroller.add(_LinkedProfilesList);
583     _LinkedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
584     _LinkedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
585     _LinkedProfilesListScroller.set_size_request(-1, 90);
587     _link_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::linkSelectedProfile));
589     _LinkedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::linked_profiles_list_button_release));
590     cms_create_popup_menu(_LinkedProfilesList, sigc::mem_fun(*this, &DocumentProperties::removeSelectedProfile));
592     const GSList *current = SP_ACTIVE_DOCUMENT->get_resource_list( "defs" );
593     if (current) {
594         _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
595     }
596     _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_linked_profiles_box));
598 #endif // ENABLE_LCMS
600 void
601 DocumentProperties::build_scripting()
603     _page_scripting.show();
605     Gtk::Label *label_script= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
606     label_script->set_markup (_("<b>External script files:</b>"));
608     _add_btn.set_label(_("Add"));
610     _page_scripting.set_spacing(4);
611     gint row = 0;
613     label_script->set_alignment(0.0);
614     _page_scripting.table().attach(*label_script, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
615     row++;
616     _page_scripting.table().attach(_ExternalScriptsListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
617     row++;
619     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
620     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
621     _page_scripting.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
622     row++;
624     _page_scripting.table().attach(_script_entry, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
625     _page_scripting.table().attach(_add_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
626     row++;
628     //# Set up the External Scripts box
629     _ExternalScriptsListStore = Gtk::ListStore::create(_ExternalScriptsListColumns);
630     _ExternalScriptsList.set_model(_ExternalScriptsListStore);
631     _ExternalScriptsList.append_column(_("Filename"), _ExternalScriptsListColumns.filenameColumn);
632     _ExternalScriptsList.set_headers_visible(true);
633 // TODO restore?    _ExternalScriptsList.set_fixed_height_mode(true);
635     populate_external_scripts_box();
637     _ExternalScriptsListScroller.add(_ExternalScriptsList);
638     _ExternalScriptsListScroller.set_shadow_type(Gtk::SHADOW_IN);
639     _ExternalScriptsListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
640     _ExternalScriptsListScroller.set_size_request(-1, 90);
642     _add_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::addExternalScript));
644 #if ENABLE_LCMS
645     _ExternalScriptsList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::external_scripts_list_button_release));
646     scripting_create_popup_menu(_ExternalScriptsList, sigc::mem_fun(*this, &DocumentProperties::removeExternalScript));
647 #endif // ENABLE_LCMS
649 //TODO: review this observers code:
650     const GSList *current = SP_ACTIVE_DOCUMENT->get_resource_list( "script" );
651     if (current) {
652         _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
653     }
654     _ext_scripts_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_external_scripts_box));
658 void DocumentProperties::addExternalScript(){
659     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
660     if (!desktop){
661         g_warning("No active desktop");
662     } else {
663         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
664         Inkscape::XML::Node *scriptRepr = xml_doc->createElement("svg:script");
665         scriptRepr->setAttribute("xlink:href", (gchar*) _script_entry.get_text().c_str());
666         _script_entry.set_text("");
668         xml_doc->root()->addChild(scriptRepr, NULL);
670         // inform the document, so we can undo
671         sp_document_done(desktop->doc(), SP_VERB_EDIT_ADD_EXTERNAL_SCRIPT, _("Add external script..."));
673         populate_external_scripts_box();
674     }
677 void DocumentProperties::removeExternalScript(){
678     Glib::ustring name;
679     if(_ExternalScriptsList.get_selection()) {
680         Gtk::TreeModel::iterator i = _ExternalScriptsList.get_selection()->get_selected();
682         if(i){
683             name = (*i)[_ExternalScriptsListColumns.filenameColumn];
684         } else {
685             return;
686         }
687     }
689     const GSList *current = SP_ACTIVE_DOCUMENT->get_resource_list( "script" );
690     while ( current ) {
691         SPObject* obj = SP_OBJECT(current->data);
692         SPScript* script = (SPScript*) obj;
693         if (name == script->xlinkhref){
694             sp_repr_unparent(obj->repr);
695             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_EXTERNAL_SCRIPT, _("Remove external script"));
696         }
697         current = g_slist_next(current);
698     }
700     populate_external_scripts_box();
704 void DocumentProperties::populate_external_scripts_box(){
705     _ExternalScriptsListStore->clear();
706     const GSList *current = SP_ACTIVE_DOCUMENT->get_resource_list( "script" );
707     if (current) _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
708     while ( current ) {
709         SPObject* obj = SP_OBJECT(current->data);
710         SPScript* script = (SPScript*) obj;
711         if (script->xlinkhref)
712         {
713             Gtk::TreeModel::Row row = *(_ExternalScriptsListStore->append());
714             row[_ExternalScriptsListColumns.filenameColumn] = script->xlinkhref;
715         }
717         current = g_slist_next(current);
718     }
721 /**
722 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
723 */
724 void
725 DocumentProperties::update_gridspage()
727     SPDesktop *dt = getDesktop();
728     SPNamedView *nv = sp_desktop_namedview(dt);
730     //remove all tabs
731     while (_grids_notebook.get_n_pages() != 0) {
732         _grids_notebook.remove_page(-1); // this also deletes the page.
733     }
735     //add tabs
736     bool grids_present = false;
737     for (GSList const * l = nv->grids; l != NULL; l = l->next) {
738         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
739         if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
740         Glib::ustring name(grid->repr->attribute("id"));
741         const char *icon = NULL;
742         switch (grid->getGridType()) {
743             case GRID_RECTANGULAR:
744                 icon = "grid-rectangular";
745                 break;
746             case GRID_AXONOMETRIC:
747                 icon = "grid-axonometric";
748                 break;
749             default:
750                 break;
751         }
752         _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
753         grids_present = true;
754     }
755     _grids_notebook.show_all();
757     if (grids_present)
758         _grids_button_remove.set_sensitive(true);
759     else
760         _grids_button_remove.set_sensitive(false);
763 /**
764  * Build grid page of dialog.
765  */
766 void
767 DocumentProperties::build_gridspage()
769     /// \todo FIXME: gray out snapping when grid is off.
770     /// Dissenting view: you want snapping without grid.
772     SPDesktop *dt = getDesktop();
773     SPNamedView *nv = sp_desktop_namedview(dt);
774     (void)nv;
776     _grids_label_crea.set_markup(_("<b>Creation</b>"));
777     _grids_label_def.set_markup(_("<b>Defined grids</b>"));
778     _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
779     _grids_hbox_crea.pack_start(_grids_button_new, true, true);
781     for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
782         _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
783     }
784     _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
786     _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
788     _grids_vbox.set_spacing(4);
789     _grids_vbox.pack_start(_grids_label_crea, false, false);
790     _grids_vbox.pack_start(_grids_hbox_crea, false, false);
791     _grids_vbox.pack_start(_grids_space, false, false);
792     _grids_vbox.pack_start(_grids_label_def, false, false);
793     _grids_vbox.pack_start(_grids_notebook, false, false);
794     _grids_vbox.pack_start(_grids_button_remove, false, false);
796     update_gridspage();
801 /**
802  * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
803  */
804 void
805 DocumentProperties::update()
807     if (_wr.isUpdating()) return;
809     SPDesktop *dt = getDesktop();
810     SPNamedView *nv = sp_desktop_namedview(dt);
812     _wr.setUpdating (true);
813     set_sensitive (true);
815     //-----------------------------------------------------------page page
816     _rcp_bg.setRgba32 (nv->pagecolor);
817     _rcb_canb.setActive (nv->showborder);
818     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
819     _rcp_bord.setRgba32 (nv->bordercolor);
820     _rcb_shad.setActive (nv->showpageshadow);
822     if (nv->doc_units)
823         _rum_deflt.setUnit (nv->doc_units);
825     double const doc_w_px = sp_desktop_document(dt)->getWidth();
826     double const doc_h_px = sp_desktop_document(dt)->getHeight();
827     _page_sizer.setDim (doc_w_px, doc_h_px);
828     _page_sizer.updateFitMarginsUI(SP_OBJECT_REPR(nv));
830     //-----------------------------------------------------------guide page
832     _rcb_sgui.setActive (nv->showguides);
833     _rcp_gui.setRgba32 (nv->guidecolor);
834     _rcp_hgui.setRgba32 (nv->guidehicolor);
835     _rcbsng.setActive(nv->snap_manager.snapprefs.getSnapModeGuide());
837     //-----------------------------------------------------------snap page
839     _rsu_sno.setValue (nv->snap_manager.snapprefs.getObjectTolerance());
840     _rsu_sn.setValue (nv->snap_manager.snapprefs.getGridTolerance());
841     _rsu_gusn.setValue (nv->snap_manager.snapprefs.getGuideTolerance());
844     //-----------------------------------------------------------grids page
846     update_gridspage();
848     //------------------------------------------------Color Management page
850 #if ENABLE_LCMS
851     populate_linked_profiles_box();
852     populate_available_profiles();
853 #endif // ENABLE_LCMS
855     _wr.setUpdating (false);
858 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
859 Gtk::HBox&
860 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
862     Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
863     _tab_label_box->set_spacing(4);
864     _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
865                                                        label_image)));
867     Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
868     _tab_label_box->pack_start(*_tab_label);
869     _tab_label_box->show_all();
871     return *_tab_label_box;
874 //--------------------------------------------------------------------
876 void
877 DocumentProperties::on_response (int id)
879     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
880     {
881         _rcp_bg.closeWindow();
882         _rcp_bord.closeWindow();
883         _rcp_gui.closeWindow();
884         _rcp_hgui.closeWindow();
885     }
887     if (id == Gtk::RESPONSE_CLOSE)
888         hide();
891 void
892 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
894     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
895     repr->addListener(&_repr_events, this);
896     Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
897     root->addListener(&_repr_events, this);
898     update();
901 void
902 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
904     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
905     repr->addListener(&_repr_events, this);
906     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
907     root->addListener(&_repr_events, this);
908     update();
911 void
912 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
914     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
915     repr->removeListenerByData(this);
916     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
917     root->removeListenerByData(this);
920 static void
921 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
923     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
924         dialog->update_gridspage();
927 static void
928 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
930     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
931         dialog->update_gridspage();
936 /**
937  * Called when XML node attribute changed; updates dialog widgets.
938  */
939 static void
940 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
942     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
943         dialog->update();
947 /*########################################################################
948 # BUTTON CLICK HANDLERS    (callbacks)
949 ########################################################################*/
951 void
952 DocumentProperties::onNewGrid()
954     SPDesktop *dt = getDesktop();
955     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
956     SPDocument *doc = sp_desktop_document(dt);
958     Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
959     CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
961     // toggle grid showing to ON:
962     dt->showGrids(true);
966 void
967 DocumentProperties::onRemoveGrid()
969     gint pagenum = _grids_notebook.get_current_page();
970     if (pagenum == -1) // no pages
971       return;
973     SPDesktop *dt = getDesktop();
974     SPNamedView *nv = sp_desktop_namedview(dt);
975     Inkscape::CanvasGrid * found_grid = NULL;
976     int i = 0;
977     for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) {  // not a very nice fix, but works.
978         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
979         if (pagenum == i) {
980             found_grid = grid;
981             break; // break out of for-loop
982         }
983     }
984     if (found_grid) {
985         // delete the grid that corresponds with the selected tab
986         // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
987         found_grid->repr->parent()->removeChild(found_grid->repr);
988         sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
989     }
993 } // namespace Dialog
994 } // namespace UI
995 } // namespace Inkscape
997 /*
998   Local Variables:
999   mode:c++
1000   c-file-style:"stroustrup"
1001   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1002   indent-tabs-mode:nil
1003   fill-column:99
1004   End:
1005 */
1006 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :