Code

e147791634b28769cd18b432bcf7915683d896df
[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       _grids_button_new(C_("Grid", "_New"), _("Create new grid.")),
107       _grids_button_remove(C_("Grid", "_Remove"), _("Remove selected grid.")),
108       _grids_label_def("", Gtk::ALIGN_LEFT)
109     //---------------------------------------------------------------
111     _tt.enable();
112     _getContents()->set_spacing (4);
113     _getContents()->pack_start(_notebook, true, true);
115     _notebook.append_page(_page_page,      _("Page"));
116     _notebook.append_page(_page_guides,    _("Guides"));
117     _notebook.append_page(_grids_vbox,     _("Grids"));
118     _notebook.append_page(_page_snap,      _("Snap"));
119     _notebook.append_page(_page_cms, _("Color Management"));
120     _notebook.append_page(_page_scripting, _("Scripting"));
122     build_page();
123     build_guides();
124     build_gridspage();
125     build_snap();
126 #if ENABLE_LCMS
127     build_cms();
128 #endif // ENABLE_LCMS
129     build_scripting();
131     _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
132     _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
134     signalDocumentReplaced().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDocumentReplaced));
135     signalActivateDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleActivateDesktop));
136     signalDeactiveDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDeactivateDesktop));
139 void
140 DocumentProperties::init()
142     update();
144     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
145     repr->addListener (&_repr_events, this);
146     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
147     root->addListener (&_repr_events, this);
149     show_all_children();
150     _grids_button_remove.hide();
153 DocumentProperties::~DocumentProperties()
155     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
156     repr->removeListenerByData (this);
157     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
158     root->removeListenerByData (this);
161 //========================================================================
163 /**
164  * Helper function that attaches widgets in a 3xn table. The widgets come in an
165  * array that has two entries per table row. The two entries code for four
166  * possible cases: (0,0) means insert space in first column; (0, non-0) means
167  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
168  * (non-0, non-0) means two widgets in columns 2 and 3.
169 **/
170 inline void
171 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
173     for (unsigned i = 0, r = start; i < n; i += 2)
174     {
175         if (arr[i] && arr[i+1])
176         {
177             table.attach(*arr[i],   1, 2, r, r+1,
178                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
179             table.attach(*arr[i+1], 2, 3, r, r+1,
180                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
181         }
182         else
183         {
184             if (arr[i+1]) {
185                 Gtk::AttachOptions yoptions = (Gtk::AttachOptions)0;
186                 if (dynamic_cast<Inkscape::UI::Widget::PageSizer*>(arr[i+1])) {
187                     // only the PageSizer in Document Properties|Page should be stretched vertically
188                     yoptions = Gtk::FILL|Gtk::EXPAND;
189                 }
190                 table.attach(*arr[i+1], 1, 3, r, r+1,
191                       Gtk::FILL|Gtk::EXPAND, yoptions, 0,0);
192             }
193             else if (arr[i])
194             {
195                 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
196                 label.set_alignment (0.0);
197                 table.attach (label, 0, 3, r, r+1,
198                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
199             }
200             else
201             {
202                 Gtk::HBox *space = manage (new Gtk::HBox);
203                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
204                 table.attach (*space, 0, 1, r, r+1,
205                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
206             }
207         }
208         ++r;
209     }
212 void
213 DocumentProperties::build_page()
215     _page_page.show();
217     Gtk::Label* label_gen = manage (new Gtk::Label);
218     label_gen->set_markup (_("<b>General</b>"));
219     Gtk::Label* label_bor = manage (new Gtk::Label);
220     label_bor->set_markup (_("<b>Border</b>"));
221     Gtk::Label *label_for = manage (new Gtk::Label);
222     label_for->set_markup (_("<b>Page Size</b>"));
223     _page_sizer.init();
225     Gtk::Widget *const widget_array[] =
226     {
227         label_gen,         0,
228         0,                 &_rum_deflt,
229         _rcp_bg._label,    &_rcp_bg,
230         0,                 0,
231         label_for,         0,
232         0,                 &_page_sizer,
233         0,                 0,
234         label_bor,         0,
235         0,                 &_rcb_canb,
236         0,                 &_rcb_bord,
237         0,                 &_rcb_shad,
238         _rcp_bord._label,  &_rcp_bord,
239     };
241     attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
244 void
245 DocumentProperties::build_guides()
247     _page_guides.show();
249     Gtk::Label *label_gui = manage (new Gtk::Label);
250     label_gui->set_markup (_("<b>Guides</b>"));
252     Gtk::Widget *const widget_array[] =
253     {
254         label_gui,        0,
255         0,                &_rcb_sgui,
256         _rcp_gui._label,  &_rcp_gui,
257         _rcp_hgui._label, &_rcp_hgui,
258         0,                &_rcbsng,
259     };
261     attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
264 void
265 DocumentProperties::build_snap()
267     _page_snap.show();
269     _rsu_sno.init (_("Snap _distance"), _("Snap only when _closer than:"), _("Always snap"),
270                   _("Snapping distance, in screen pixels, for snapping to objects"), _("Always snap to objects, regardless of their distance"),
271                   _("If set, objects only snap to another object when it's within the range specified below"),
272                   "objecttolerance", _wr);
274     //Options for snapping to grids
275     _rsu_sn.init (_("Snap d_istance"), _("Snap only when c_loser than:"), _("Always snap"),
276                   _("Snapping distance, in screen pixels, for snapping to grid"), _("Always snap to grids, regardless of the distance"),
277                   _("If set, objects only snap to a grid line when it's within the range specified below"),
278                   "gridtolerance", _wr);
280     //Options for snapping to guides
281     _rsu_gusn.init (_("Snap dist_ance"), _("Snap only when close_r than:"), _("Always snap"),
282                 _("Snapping distance, in screen pixels, for snapping to guides"), _("Always snap to guides, regardless of the distance"),
283                 _("If set, objects only snap to a guide when it's within the range specified below"),
284                 "guidetolerance", _wr);
286     Gtk::Label *label_o = manage (new Gtk::Label);
287     label_o->set_markup (_("<b>Snap to objects</b>"));
288     Gtk::Label *label_gr = manage (new Gtk::Label);
289     label_gr->set_markup (_("<b>Snap to grids</b>"));
290     Gtk::Label *label_gu = manage (new Gtk::Label);
291     label_gu->set_markup (_("<b>Snap to guides</b>"));
293     Gtk::Widget *const array[] =
294     {
295         label_o,            0,
296         0,                  _rsu_sno._vbox,
297         0,                  0,
298         label_gr,           0,
299         0,                  _rsu_sn._vbox,
300         0,                  0,
301         label_gu,           0,
302         0,                  _rsu_gusn._vbox
303     };
305     attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
306  }
308 #if ENABLE_LCMS
309 static void
310 lcms_profile_get_name (cmsHPROFILE   profile, const gchar **name)
312   if (profile)
313     {
314       *name = cmsTakeProductDesc (profile);
316       if (! *name)
317         *name = cmsTakeProductName (profile);
319       if (*name && ! g_utf8_validate (*name, -1, NULL))
320         *name = _("(invalid UTF-8 string)");
321     }
322   else
323     {
324       *name = _("None");
325     }
328 void
329 DocumentProperties::populate_available_profiles(){
330     Glib::ListHandle<Gtk::Widget*> children = _menu.get_children();
331     for ( Glib::ListHandle<Gtk::Widget*>::iterator it2 = children.begin(); it2 != children.end(); ++it2 ) {
332         _menu.remove(**it2);
333         delete(*it2);
334     }
336     std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
338     // Use this loop to iterate through a list of possible document locations.
339     for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
340         if ( Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_EXISTS )
341              && Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_IS_DIR )) {
342             GError *err = 0;
343             GDir *directory = g_dir_open(it->c_str(), 0, &err);
344             if (!directory) {
345                 gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str());
346                 g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
347                 g_free(safeDir);
348             } else {
349                 gchar *filename = 0;
350                 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
351                     gchar* full = g_build_filename(it->c_str(), filename, NULL);
352                     if ( !Inkscape::IO::file_test( full, G_FILE_TEST_IS_DIR ) ) {
353                         cmsErrorAction( LCMS_ERROR_SHOW );
354                         cmsHPROFILE hProfile = cmsOpenProfileFromFile(full, "r");
355                         if (hProfile != NULL){
356                             const gchar* name;
357                             lcms_profile_get_name(hProfile, &name);
358                             Gtk::MenuItem* mi = manage(new Gtk::MenuItem());
359                             mi->set_data("filepath", g_strdup(full));
360                             mi->set_data("name", g_strdup(name));
361                             Gtk::HBox *hbox = manage(new Gtk::HBox());
362                             hbox->show();
363                             Gtk::Label* lbl = manage(new Gtk::Label(name));
364                             lbl->show();
365                             hbox->pack_start(*lbl, true, true, 0);
366                             mi->add(*hbox);
367                             mi->show_all();
368                             _menu.append(*mi);
369         //                    g_free((void*)name);
370                             cmsCloseProfile(hProfile);
371                         }
372                     }
373                     g_free(full);
374                 }
375                 g_dir_close(directory);
376             }
377         }
378     }
379     _menu.show_all();
382 /**
383  * Cleans up name to remove disallowed characters.
384  * Some discussion at http://markmail.org/message/bhfvdfptt25kgtmj
385  * Allowed ASCII first characters:  ':', 'A'-'Z', '_', 'a'-'z'
386  * Allowed ASCII remaining chars add: '-', '.', '0'-'9', 
387  *
388  * @param str the string to clean up.
389  */
390 static void sanitizeName( Glib::ustring& str )
392     if (str.size() > 1) {
393         char val = str.at(0);
394         if (((val < 'A') || (val > 'Z'))
395             && ((val < 'a') || (val > 'z'))
396             && (val != '_')
397             && (val != ':')) {
398             str.replace(0, 1, "-");
399         }
400         for (Glib::ustring::size_type i = 1; i < str.size(); i++) {
401             char val = str.at(i);
402             if (((val < 'A') || (val > 'Z'))
403                 && ((val < 'a') || (val > 'z'))
404                 && ((val < '0') || (val > '9'))
405                 && (val != '_')
406                 && (val != ':')
407                 && (val != '-')
408                 && (val != '.')) {
409                 str.replace(i, 1, "-");
410             }
411         }
412     }
415 void
416 DocumentProperties::linkSelectedProfile()
418 //store this profile in the SVG document (create <color-profile> element in the XML)
419     // TODO remove use of 'active' desktop
420     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
421     if (!desktop){
422         g_warning("No active desktop");
423     } else {
424         if (!_menu.get_active()){
425             g_warning("No color profile available.");
426             return;
427         }
428         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
429         Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
430         gchar* tmp = static_cast<gchar*>(_menu.get_active()->get_data("name"));
431         Glib::ustring nameStr = tmp ? tmp : "profile"; // TODO add some auto-numbering to avoid collisions
432         sanitizeName(nameStr);
433         cprofRepr->setAttribute("name", nameStr.c_str());
434         cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
436         // Checks whether there is a defs element. Creates it when needed
437         Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
438         if (!defsRepr){
439             defsRepr = xml_doc->createElement("svg:defs");
440             xml_doc->root()->addChild(defsRepr, NULL);
441         }
443         g_assert(SP_ROOT(desktop->doc()->root)->defs);
444         defsRepr->addChild(cprofRepr, NULL);
446         // TODO check if this next line was sometimes needed. It being there caused an assertion.
447         //Inkscape::GC::release(defsRepr);
449         // inform the document, so we can undo
450         sp_document_done(desktop->doc(), SP_VERB_EDIT_LINK_COLOR_PROFILE, _("Link Color Profile"));
452         populate_linked_profiles_box();
453     }
456 void
457 DocumentProperties::populate_linked_profiles_box()
459     _LinkedProfilesListStore->clear();
460     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
461     if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
462     while ( current ) {
463         SPObject* obj = SP_OBJECT(current->data);
464         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
465         Gtk::TreeModel::Row row = *(_LinkedProfilesListStore->append());
466         row[_LinkedProfilesListColumns.nameColumn] = prof->name;
467 //        row[_LinkedProfilesListColumns.previewColumn] = "Color Preview";
468         current = g_slist_next(current);
469     }
472 void DocumentProperties::external_scripts_list_button_release(GdkEventButton* event)
474     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
475         _ExternalScriptsContextMenu.popup(event->button, event->time);
476     }
479 void DocumentProperties::linked_profiles_list_button_release(GdkEventButton* event)
481     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
482         _EmbProfContextMenu.popup(event->button, event->time);
483     }
486 void DocumentProperties::cms_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
488     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
489     _EmbProfContextMenu.append(*mi);
490     mi->signal_activate().connect(rem);
491     mi->show();
492     _EmbProfContextMenu.accelerate(parent);
496 void DocumentProperties::scripting_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
498     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
499     _ExternalScriptsContextMenu.append(*mi);
500     mi->signal_activate().connect(rem);
501     mi->show();
502     _ExternalScriptsContextMenu.accelerate(parent);
505 void DocumentProperties::removeSelectedProfile(){
506     Glib::ustring name;
507     if(_LinkedProfilesList.get_selection()) {
508         Gtk::TreeModel::iterator i = _LinkedProfilesList.get_selection()->get_selected();
510         if(i){
511             name = (*i)[_LinkedProfilesListColumns.nameColumn];
512         } else {
513             return;
514         }
515     }
517     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
518     while ( current ) {
519         SPObject* obj = SP_OBJECT(current->data);
520         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
521         if (!name.compare(prof->name)){
522             sp_repr_unparent(obj->repr);
523             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove linked color profile"));
524         }
525         current = g_slist_next(current);
526     }
528     populate_linked_profiles_box();
531 void
532 DocumentProperties::build_cms()
534     _page_cms.show();
536     Gtk::Label *label_link= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
537     label_link->set_markup (_("<b>Linked Color Profiles:</b>"));
538     Gtk::Label *label_avail = manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
539     label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
541     _link_btn.set_label(_("Link Profile"));
543     _page_cms.set_spacing(4);
544     gint row = 0;
546     label_link->set_alignment(0.0);
547     _page_cms.table().attach(*label_link, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
548     row++;
549     _page_cms.table().attach(_LinkedProfilesListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
550     row++;
552     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
553     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
554     _page_cms.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
555     row++;
557     label_avail->set_alignment(0.0);
558     _page_cms.table().attach(*label_avail, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
559     row++;
560     _page_cms.table().attach(_combo_avail, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
561     _page_cms.table().attach(_link_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
563     populate_available_profiles();
565     _combo_avail.set_menu(_menu);
566     _combo_avail.set_history(0);
567     _combo_avail.show_all();
569     //# Set up the Linked Profiles combo box
570     _LinkedProfilesListStore = Gtk::ListStore::create(_LinkedProfilesListColumns);
571     _LinkedProfilesList.set_model(_LinkedProfilesListStore);
572     _LinkedProfilesList.append_column(_("Profile Name"), _LinkedProfilesListColumns.nameColumn);
573 //    _LinkedProfilesList.append_column(_("Color Preview"), _LinkedProfilesListColumns.previewColumn);
574     _LinkedProfilesList.set_headers_visible(false);
575 // TODO restore?    _LinkedProfilesList.set_fixed_height_mode(true);
577     populate_linked_profiles_box();
579     _LinkedProfilesListScroller.add(_LinkedProfilesList);
580     _LinkedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
581     _LinkedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
582     _LinkedProfilesListScroller.set_size_request(-1, 90);
584     _link_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::linkSelectedProfile));
586     _LinkedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::linked_profiles_list_button_release));
587     cms_create_popup_menu(_LinkedProfilesList, sigc::mem_fun(*this, &DocumentProperties::removeSelectedProfile));
589     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "defs" );
590     if (current) {
591         _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
592     }
593     _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_linked_profiles_box));
595 #endif // ENABLE_LCMS
597 void
598 DocumentProperties::build_scripting()
600     _page_scripting.show();
602     Gtk::Label *label_script= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
603     label_script->set_markup (_("<b>External script files:</b>"));
605     _add_btn.set_label(_("Add"));
607     _page_scripting.set_spacing(4);
608     gint row = 0;
610     label_script->set_alignment(0.0);
611     _page_scripting.table().attach(*label_script, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
612     row++;
613     _page_scripting.table().attach(_ExternalScriptsListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
614     row++;
616     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
617     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
618     _page_scripting.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
619     row++;
621     _page_scripting.table().attach(_script_entry, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
622     _page_scripting.table().attach(_add_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
623     row++;
625     //# Set up the External Scripts box
626     _ExternalScriptsListStore = Gtk::ListStore::create(_ExternalScriptsListColumns);
627     _ExternalScriptsList.set_model(_ExternalScriptsListStore);
628     _ExternalScriptsList.append_column(_("Filename"), _ExternalScriptsListColumns.filenameColumn);
629     _ExternalScriptsList.set_headers_visible(true);
630 // TODO restore?    _ExternalScriptsList.set_fixed_height_mode(true);
632     populate_external_scripts_box();
634     _ExternalScriptsListScroller.add(_ExternalScriptsList);
635     _ExternalScriptsListScroller.set_shadow_type(Gtk::SHADOW_IN);
636     _ExternalScriptsListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
637     _ExternalScriptsListScroller.set_size_request(-1, 90);
639     _add_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::addExternalScript));
641 #if ENABLE_LCMS
642     _ExternalScriptsList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::external_scripts_list_button_release));
643     scripting_create_popup_menu(_ExternalScriptsList, sigc::mem_fun(*this, &DocumentProperties::removeExternalScript));
644 #endif // ENABLE_LCMS
646 //TODO: review this observers code:
647     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
648     if (current) {
649         _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
650     }
651     _ext_scripts_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_external_scripts_box));
655 void DocumentProperties::addExternalScript(){
656     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
657     if (!desktop){
658         g_warning("No active desktop");
659     } else {
660         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
661         Inkscape::XML::Node *scriptRepr = xml_doc->createElement("svg:script");
662         scriptRepr->setAttribute("xlink:href", (gchar*) _script_entry.get_text().c_str());
663         _script_entry.set_text("");
665         xml_doc->root()->addChild(scriptRepr, NULL);
667         // inform the document, so we can undo
668         sp_document_done(desktop->doc(), SP_VERB_EDIT_ADD_EXTERNAL_SCRIPT, _("Add external script..."));
670         populate_external_scripts_box();
671     }
674 void DocumentProperties::removeExternalScript(){
675     Glib::ustring name;
676     if(_ExternalScriptsList.get_selection()) {
677         Gtk::TreeModel::iterator i = _ExternalScriptsList.get_selection()->get_selected();
679         if(i){
680             name = (*i)[_ExternalScriptsListColumns.filenameColumn];
681         } else {
682             return;
683         }
684     }
686     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
687     while ( current ) {
688         SPObject* obj = SP_OBJECT(current->data);
689         SPScript* script = (SPScript*) obj;
690         if (name == script->xlinkhref){
691             sp_repr_unparent(obj->repr);
692             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_EXTERNAL_SCRIPT, _("Remove external script"));
693         }
694         current = g_slist_next(current);
695     }
697     populate_external_scripts_box();
701 void DocumentProperties::populate_external_scripts_box(){
702     _ExternalScriptsListStore->clear();
703     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
704     if (current) _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
705     while ( current ) {
706         SPObject* obj = SP_OBJECT(current->data);
707         SPScript* script = (SPScript*) obj;
708         if (script->xlinkhref)
709         {
710             Gtk::TreeModel::Row row = *(_ExternalScriptsListStore->append());
711             row[_ExternalScriptsListColumns.filenameColumn] = script->xlinkhref;
712         }
714         current = g_slist_next(current);
715     }
718 /**
719 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
720 */
721 void
722 DocumentProperties::update_gridspage()
724     SPDesktop *dt = getDesktop();
725     SPNamedView *nv = sp_desktop_namedview(dt);
727     //remove all tabs
728     while (_grids_notebook.get_n_pages() != 0) {
729         _grids_notebook.remove_page(-1); // this also deletes the page.
730     }
732     //add tabs
733     bool grids_present = false;
734     for (GSList const * l = nv->grids; l != NULL; l = l->next) {
735         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
736         if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
737         Glib::ustring name(grid->repr->attribute("id"));
738         const char *icon = NULL;
739         switch (grid->getGridType()) {
740             case GRID_RECTANGULAR:
741                 icon = "grid-rectangular";
742                 break;
743             case GRID_AXONOMETRIC:
744                 icon = "grid-axonometric";
745                 break;
746             default:
747                 break;
748         }
749         _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
750         grids_present = true;
751     }
752     _grids_notebook.show_all();
754     if (grids_present)
755         _grids_button_remove.set_sensitive(true);
756     else
757         _grids_button_remove.set_sensitive(false);
760 /**
761  * Build grid page of dialog.
762  */
763 void
764 DocumentProperties::build_gridspage()
766     /// \todo FIXME: gray out snapping when grid is off.
767     /// Dissenting view: you want snapping without grid.
769     SPDesktop *dt = getDesktop();
770     SPNamedView *nv = sp_desktop_namedview(dt);
771     (void)nv;
773     _grids_label_crea.set_markup(_("<b>Creation</b>"));
774     _grids_label_def.set_markup(_("<b>Defined grids</b>"));
775     _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
776     _grids_hbox_crea.pack_start(_grids_button_new, true, true);
778     for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
779         _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
780     }
781     _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
783     _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
785     _grids_vbox.set_spacing(4);
786     _grids_vbox.pack_start(_grids_label_crea, false, false);
787     _grids_vbox.pack_start(_grids_hbox_crea, false, false);
788     _grids_vbox.pack_start(_grids_space, false, false);
789     _grids_vbox.pack_start(_grids_label_def, false, false);
790     _grids_vbox.pack_start(_grids_notebook, false, false);
791     _grids_vbox.pack_start(_grids_button_remove, false, false);
793     update_gridspage();
798 /**
799  * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
800  */
801 void
802 DocumentProperties::update()
804     if (_wr.isUpdating()) return;
806     SPDesktop *dt = getDesktop();
807     SPNamedView *nv = sp_desktop_namedview(dt);
809     _wr.setUpdating (true);
810     set_sensitive (true);
812     //-----------------------------------------------------------page page
813     _rcp_bg.setRgba32 (nv->pagecolor);
814     _rcb_canb.setActive (nv->showborder);
815     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
816     _rcp_bord.setRgba32 (nv->bordercolor);
817     _rcb_shad.setActive (nv->showpageshadow);
819     if (nv->doc_units)
820         _rum_deflt.setUnit (nv->doc_units);
822     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
823     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
824     _page_sizer.setDim (doc_w_px, doc_h_px);
825     _page_sizer.updateFitMarginsUI(SP_OBJECT_REPR(nv));
827     //-----------------------------------------------------------guide page
829     _rcb_sgui.setActive (nv->showguides);
830     _rcp_gui.setRgba32 (nv->guidecolor);
831     _rcp_hgui.setRgba32 (nv->guidehicolor);
832     _rcbsng.setActive(nv->snap_manager.snapprefs.getSnapModeGuide());
834     //-----------------------------------------------------------snap page
836     _rsu_sno.setValue (nv->snap_manager.snapprefs.getObjectTolerance());
837     _rsu_sn.setValue (nv->snap_manager.snapprefs.getGridTolerance());
838     _rsu_gusn.setValue (nv->snap_manager.snapprefs.getGuideTolerance());
841     //-----------------------------------------------------------grids page
843     update_gridspage();
845     //------------------------------------------------Color Management page
847 #if ENABLE_LCMS
848     populate_linked_profiles_box();
849     populate_available_profiles();
850 #endif // ENABLE_LCMS
852     _wr.setUpdating (false);
855 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
856 Gtk::HBox&
857 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
859     Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
860     _tab_label_box->set_spacing(4);
861     _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
862                                                        label_image)));
864     Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
865     _tab_label_box->pack_start(*_tab_label);
866     _tab_label_box->show_all();
868     return *_tab_label_box;
871 //--------------------------------------------------------------------
873 void
874 DocumentProperties::on_response (int id)
876     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
877     {
878         _rcp_bg.closeWindow();
879         _rcp_bord.closeWindow();
880         _rcp_gui.closeWindow();
881         _rcp_hgui.closeWindow();
882     }
884     if (id == Gtk::RESPONSE_CLOSE)
885         hide();
888 void
889 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
891     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
892     repr->addListener(&_repr_events, this);
893     Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
894     root->addListener(&_repr_events, this);
895     update();
898 void
899 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
901     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
902     repr->addListener(&_repr_events, this);
903     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
904     root->addListener(&_repr_events, this);
905     update();
908 void
909 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
911     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
912     repr->removeListenerByData(this);
913     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
914     root->removeListenerByData(this);
917 static void
918 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
920     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
921         dialog->update_gridspage();
924 static void
925 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
927     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
928         dialog->update_gridspage();
933 /**
934  * Called when XML node attribute changed; updates dialog widgets.
935  */
936 static void
937 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
939     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
940         dialog->update();
944 /*########################################################################
945 # BUTTON CLICK HANDLERS    (callbacks)
946 ########################################################################*/
948 void
949 DocumentProperties::onNewGrid()
951     SPDesktop *dt = getDesktop();
952     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
953     SPDocument *doc = sp_desktop_document(dt);
955     Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
956     CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
958     // toggle grid showing to ON:
959     dt->showGrids(true);
963 void
964 DocumentProperties::onRemoveGrid()
966     gint pagenum = _grids_notebook.get_current_page();
967     if (pagenum == -1) // no pages
968       return;
970     SPDesktop *dt = getDesktop();
971     SPNamedView *nv = sp_desktop_namedview(dt);
972     Inkscape::CanvasGrid * found_grid = NULL;
973     int i = 0;
974     for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) {  // not a very nice fix, but works.
975         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
976         if (pagenum == i) {
977             found_grid = grid;
978             break; // break out of for-loop
979         }
980     }
981     if (found_grid) {
982         // delete the grid that corresponds with the selected tab
983         // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
984         found_grid->repr->parent()->removeChild(found_grid->repr);
985         sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
986     }
990 } // namespace Dialog
991 } // namespace UI
992 } // namespace Inkscape
994 /*
995   Local Variables:
996   mode:c++
997   c-file-style:"stroustrup"
998   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
999   indent-tabs-mode:nil
1000   fill-column:99
1001   End:
1002 */
1003 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :