Code

adding a "scripting" tab to document properties dialog that allow the user to manage...
[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
58 #define INKSCAPE_ICON_GRID_XY     "grid_xy"
59 #define INKSCAPE_ICON_GRID_AXONOM "grid_axonom"
62 //===================================================
64 //---------------------------------------------------
66 static void on_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
67 static void on_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
68 static void on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer);
70 static Inkscape::XML::NodeEventVector const _repr_events = {
71     on_child_added, // child_added
72     on_child_removed, // child_removed
73     on_repr_attr_changed,
74     NULL, // content_changed
75     NULL  // order_changed
76 };
79 DocumentProperties &
80 DocumentProperties::getInstance()
81 {
82     DocumentProperties &instance = *new DocumentProperties();
83     instance.init();
85     return instance;
86 }
88 DocumentProperties::DocumentProperties()
89     : UI::Widget::Panel ("", "/dialogs/documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
90       _page_page(1, 1, true, true), _page_guides(1, 1),
91       _page_snap(1, 1), _page_snap_dtls(1, 1), _page_cms(1, 1), _page_scripting(1, 1),
92     //---------------------------------------------------------------
93       _rcb_canb(_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false),
94       _rcb_bord(_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false),
95       _rcb_shad(_("_Show border shadow"), _("If set, page border shows a shadow on its right and lower side"), "inkscape:showpageshadow", _wr, false),
96       _rcp_bg(_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"), "pagecolor", "inkscape:pageopacity", _wr),
97       _rcp_bord(_("Border _color:"), _("Page border color"), _("Color of the page border"), "bordercolor", "borderopacity", _wr),
98       _rum_deflt(_("Default _units:"), "inkscape:document-units", _wr),
99       _page_sizer(_wr),
100     //---------------------------------------------------------------
101       //General snap options
102       _rcb_sgui(_("Show _guides"), _("Show or hide guides"), "showguides", _wr),
103       _rcbsng(_("_Snap guides while dragging"), _("While dragging a guide, snap to object nodes or bounding box corners ('Snap to nodes' or 'snap to bounding box corners' must be enabled in the 'Snap' tab; only a small part of the guide near the cursor will snap)"),
104                   "inkscape:snap-guide", _wr),
105       _rcp_gui(_("Guide co_lor:"), _("Guideline color"), _("Color of guidelines"), "guidecolor", "guideopacity", _wr),
106       _rcp_hgui(_("_Highlight color:"), _("Highlighted guideline color"), _("Color of a guideline when it is under mouse"), "guidehicolor", "guidehiopacity", _wr),
107     //---------------------------------------------------------------
108       _rcbs(_("_Enable snapping"), _("Toggle snapping on or off"), "inkscape:snap-global", _wr),
109       _rcbsnbb(_("_Bounding box corners"), _("Only available in the selector tool: snap bounding box corners to guides, to grids, and to other bounding boxes (but not to nodes or paths)"),
110                   "inkscape:snap-bbox", _wr),
111       _rcbsnn(_("_Nodes"), _("Snap nodes (e.g. path nodes, special points in shapes, gradient handles, text base points, transformation origins, etc.) to guides, to grids, to paths and to other nodes"),
112                 "inkscape:snap-nodes", _wr),
113       //Options for snapping to objects
114       _rcbsnop(_("Snap to path_s"), _("Snap nodes to object paths"), "inkscape:object-paths", _wr),
115       _rcbsnon(_("Snap to n_odes"), _("Snap nodes and guides to object nodes"), "inkscape:object-nodes", _wr),
116       _rcbsnbbp(_("Snap to bounding bo_x edges"), _("Snap bounding box corners and guides to bounding box edges"), "inkscape:bbox-paths", _wr),
117       _rcbsnbbn(_("Snap to bounding box co_rners"), _("Snap bounding box corners to other bounding box corners"), "inkscape:bbox-nodes", _wr),
118       _rcbsnpb(_("Snap to page border"), _("Snap bounding box corners and nodes to the page border"), "inkscape:snap-page", _wr),
119     //---------------------------------------------------------------
120        //Applies to both nodes and guides, but not to bboxes, that's why its located here
121       _rcbic( _("Rotation _center"), _("Consider the rotation center of an object when snapping"), "inkscape:snap-center", _wr),
122       _rcbsm( _("_Smooth nodes"), _("Snap to smooth nodes too, instead of only snapping to cusp nodes"), "inkscape:snap-smooth-nodes", _wr),
123       _rcbmp( _("_Midpoints"), _("Snap midpoints of straight line segments"), "inkscape:snap-midpoints", _wr),
124       _rcbsigg(_("_Grid with guides"), _("Snap to grid-guide intersections"), "inkscape:snap-intersection-grid-guide", _wr),
125       _rcbsils(_("_Paths"), _("Snap to intersections of paths ('snap to paths' must be enabled, see the previous tab)"),
126                 "inkscape:snap-intersection-paths", _wr),
127     //---------------------------------------------------------------
128       _grids_label_crea("", Gtk::ALIGN_LEFT),
129       //TRANSLATORS: In Grid|_New translate only the word _New. It ref to grid
130       _grids_button_new(Q_("Grid|_New"), _("Create new grid.")),
131       _grids_button_remove(_("_Remove"), _("Remove selected grid.")),
132       _grids_label_def("", Gtk::ALIGN_LEFT)
133     //---------------------------------------------------------------
135     _tt.enable();
136     _getContents()->set_spacing (4);
137     _getContents()->pack_start(_notebook, true, true);
139     _notebook.append_page(_page_page,      _("Page"));
140     _notebook.append_page(_page_guides,    _("Guides"));
141     _notebook.append_page(_grids_vbox,     _("Grids"));
142     _notebook.append_page(_page_snap,      _("Snap"));
143     _notebook.append_page(_page_snap_dtls, _("Snap points"));
144     _notebook.append_page(_page_cms, _("Color Management"));
145     _notebook.append_page(_page_scripting, _("Scripting"));
147     build_page();
148     build_guides();
149     build_gridspage();
150     build_snap();
151     build_snap_dtls();
152 #if ENABLE_LCMS
153     build_cms();
154 #endif // ENABLE_LCMS
155     build_scripting();
157     _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
158     _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
160     signalDocumentReplaced().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDocumentReplaced));
161     signalActivateDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleActivateDesktop));
162     signalDeactiveDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDeactivateDesktop));
165 void
166 DocumentProperties::init()
168     update();
170     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
171     repr->addListener (&_repr_events, this);
172     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
173     root->addListener (&_repr_events, this);
175     show_all_children();
176     _grids_button_remove.hide();
179 DocumentProperties::~DocumentProperties()
181     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
182     repr->removeListenerByData (this);
183     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
184     root->removeListenerByData (this);
187 //========================================================================
189 /**
190  * Helper function that attaches widgets in a 3xn table. The widgets come in an
191  * array that has two entries per table row. The two entries code for four
192  * possible cases: (0,0) means insert space in first column; (0, non-0) means
193  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
194  * (non-0, non-0) means two widgets in columns 2 and 3.
195 **/
196 inline void
197 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
199     for (unsigned i = 0, r = start; i < n; i += 2)
200     {
201         if (arr[i] && arr[i+1])
202         {
203             table.attach(*arr[i],   1, 2, r, r+1,
204                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
205             table.attach(*arr[i+1], 2, 3, r, r+1,
206                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
207         }
208         else
209         {
210             if (arr[i+1]) {
211                 Gtk::AttachOptions yoptions = (Gtk::AttachOptions)0;
212                 if (dynamic_cast<Inkscape::UI::Widget::PageSizer*>(arr[i+1])) {
213                     // only the PageSizer in Document Properties|Page should be stretched vertically
214                     yoptions = Gtk::FILL|Gtk::EXPAND;
215                 }
216                 table.attach(*arr[i+1], 1, 3, r, r+1,
217                       Gtk::FILL|Gtk::EXPAND, yoptions, 0,0);
218             }
219             else if (arr[i])
220             {
221                 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
222                 label.set_alignment (0.0);
223                 table.attach (label, 0, 3, r, r+1,
224                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
225             }
226             else
227             {
228                 Gtk::HBox *space = manage (new Gtk::HBox);
229                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
230                 table.attach (*space, 0, 1, r, r+1,
231                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
232             }
233         }
234         ++r;
235     }
238 void
239 DocumentProperties::build_page()
241     _page_page.show();
243     Gtk::Label* label_gen = manage (new Gtk::Label);
244     label_gen->set_markup (_("<b>General</b>"));
245     Gtk::Label* label_bor = manage (new Gtk::Label);
246     label_bor->set_markup (_("<b>Border</b>"));
247     Gtk::Label *label_for = manage (new Gtk::Label);
248     label_for->set_markup (_("<b>Format</b>"));
249     _page_sizer.init();
251     Gtk::Widget *const widget_array[] =
252     {
253         label_gen,         0,
254         0,                 &_rum_deflt,
255         _rcp_bg._label,    &_rcp_bg,
256         0,                 0,
257         label_for,         0,
258         0,                 &_page_sizer,
259         0,                 0,
260         label_bor,         0,
261         0,                 &_rcb_canb,
262         0,                 &_rcb_bord,
263         0,                 &_rcb_shad,
264         _rcp_bord._label,  &_rcp_bord,
265     };
267     attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
270 void
271 DocumentProperties::build_guides()
273     _page_guides.show();
275     Gtk::Label *label_gui = manage (new Gtk::Label);
276     label_gui->set_markup (_("<b>Guides</b>"));
278     Gtk::Widget *const widget_array[] =
279     {
280         label_gui,        0,
281         0,                &_rcb_sgui,
282         _rcp_gui._label,  &_rcp_gui,
283         _rcp_hgui._label, &_rcp_hgui,
284         0,                &_rcbsng,
285     };
287     attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
290 void
291 DocumentProperties::build_snap()
293     _page_snap.show();
295     _rsu_sno.init (_("Snap _distance"), _("Snap only when _closer than:"), _("Always snap"),
296                   _("Snapping distance, in screen pixels, for snapping to objects"), _("Always snap to objects, regardless of their distance"),
297                   _("If set, objects only snap to another object when it's within the range specified below"),
298                   "objecttolerance", _wr);
300     //Options for snapping to grids
301     _rsu_sn.init (_("Snap d_istance"), _("Snap only when c_loser than:"), _("Always snap"),
302                   _("Snapping distance, in screen pixels, for snapping to grid"), _("Always snap to grids, regardless of the distance"),
303                   _("If set, objects only snap to a grid line when it's within the range specified below"),
304                   "gridtolerance", _wr);
306     //Options for snapping to guides
307     _rsu_gusn.init (_("Snap dist_ance"), _("Snap only when close_r than:"), _("Always snap"),
308                 _("Snapping distance, in screen pixels, for snapping to guides"), _("Always snap to guides, regardless of the distance"),
309                 _("If set, objects only snap to a guide when it's within the range specified below"),
310                 "guidetolerance", _wr);
312     //Other options to locate here: e.g. visual snapping indicators on/off
314     std::list<Gtk::Widget*> slaves;
315     slaves.push_back(&_rcbsnop);
316     slaves.push_back(&_rcbsnon);
317     _rcbsnn.setSlaveWidgets(slaves);
319     slaves.clear();
320     slaves.push_back(&_rcbsnbbp);
321     slaves.push_back(&_rcbsnbbn);
322     _rcbsnbb.setSlaveWidgets(slaves);
324     slaves.clear();
325     slaves.push_back(&_rcbsnn);
326     slaves.push_back(&_rcbsnbb);
327     _rcbs.setSlaveWidgets(slaves);
329     Gtk::Label *label_g = manage (new Gtk::Label);
330     label_g->set_markup (_("<b>Snapping</b>"));
331     Gtk::Label *label_w = manage (new Gtk::Label);
332     label_w->set_markup (_("<b>What snaps</b>"));
333     Gtk::Label *label_o = manage (new Gtk::Label);
334     label_o->set_markup (_("<b>Snap to objects</b>"));
335     Gtk::Label *label_gr = manage (new Gtk::Label);
336     label_gr->set_markup (_("<b>Snap to grids</b>"));
337     Gtk::Label *label_gu = manage (new Gtk::Label);
338     label_gu->set_markup (_("<b>Snap to guides</b>"));
340     Gtk::Widget *const array[] =
341     {
342         label_g,            0,
343         0,                  &_rcbs,
344         0,                  0,
345         label_w,            0,
346         0,                  &_rcbsnn,
347         0,                  &_rcbsnbb,
348         0,                  0,
349         label_o,            0,
350         0,                  &_rcbsnop,
351         0,                  &_rcbsnon,
352         0,                  &_rcbsnbbp,
353         0,                  &_rcbsnbbn,
354         0,                  &_rcbsnpb,
355         0,                  _rsu_sno._vbox,
356         0,                  0,
357         label_gr,           0,
358         0,                  _rsu_sn._vbox,
359         0,                  0,
360         label_gu,           0,
361         0,                  _rsu_gusn._vbox
362     };
364     attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
365  }
367 void
368 DocumentProperties::build_snap_dtls()
370     _page_snap_dtls.show();
372     //Other options to locate here: e.g. visual snapping indicators on/off
374     Gtk::Label *label_i= manage (new Gtk::Label);
375     label_i->set_markup (_("<b>Snapping intersections of</b>"));
376     Gtk::Label *label_m = manage (new Gtk::Label);
377     label_m->set_markup (_("<b>Special points to consider</b>"));
379     Gtk::Widget *const array[] =
380     {
381         label_i,            0,
382         0,                  &_rcbsigg,
383         0,                  &_rcbsils,
384         0,                  0,
385         label_m,            0,
386         0,                  &_rcbic,
387         0,                  &_rcbsm,
388         0,                  &_rcbmp
389     };
391     attach_all(_page_snap_dtls.table(), array, G_N_ELEMENTS(array));
394 #if ENABLE_LCMS
395 static void
396 lcms_profile_get_name (cmsHPROFILE   profile, const gchar **name)
398   if (profile)
399     {
400       *name = cmsTakeProductDesc (profile);
402       if (! *name)
403         *name = cmsTakeProductName (profile);
405       if (*name && ! g_utf8_validate (*name, -1, NULL))
406         *name = _("(invalid UTF-8 string)");
407     }
408   else
409     {
410       *name = _("None");
411     }
414 void
415 DocumentProperties::populate_available_profiles(){
416     Glib::ListHandle<Gtk::Widget*> children = _menu.get_children();
417     for ( Glib::ListHandle<Gtk::Widget*>::iterator it2 = children.begin(); it2 != children.end(); ++it2 ) {
418         _menu.remove(**it2);
419         delete(*it2);
420     }
422     std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
424     // Use this loop to iterate through a list of possible document locations.
425     for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
426         if ( Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_EXISTS )
427              && Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_IS_DIR )) {
428             GError *err = 0;
429             GDir *directory = g_dir_open(it->c_str(), 0, &err);
430             if (!directory) {
431                 gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str());
432                 g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
433                 g_free(safeDir);
434             } else {
435                 gchar *filename = 0;
436                 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
437                     gchar* full = g_build_filename(it->c_str(), filename, NULL);
438                     if ( !Inkscape::IO::file_test( full, G_FILE_TEST_IS_DIR ) ) {
439                         cmsHPROFILE hProfile = cmsOpenProfileFromFile(full, "r");
440                         if (hProfile != NULL){
441                             const gchar* name;
442                             lcms_profile_get_name(hProfile, &name);
443                             Gtk::MenuItem* mi = manage(new Gtk::MenuItem());
444                             mi->set_data("filepath", g_strdup(full));
445                             mi->set_data("name", g_strdup(name));
446                             Gtk::HBox *hbox = manage(new Gtk::HBox());
447                             hbox->show();
448                             Gtk::Label* lbl = manage(new Gtk::Label(name));
449                             lbl->show();
450                             hbox->pack_start(*lbl, true, true, 0);
451                             mi->add(*hbox);
452                             mi->show_all();
453                             _menu.append(*mi);
454         //                    g_free((void*)name);
455                             cmsCloseProfile(hProfile);
456                         }
457                     }
458                     g_free(full);
459                 }
460                 g_dir_close(directory);
461             }
462         }
463     }
464     _menu.show_all();
467 void
468 DocumentProperties::linkSelectedProfile()
470 //store this profile in the SVG document (create <color-profile> element in the XML)
471     // TODO remove use of 'active' desktop
472     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
473     if (!desktop){
474         g_warning("No active desktop");
475     } else {
476         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
477         Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
478         cprofRepr->setAttribute("name", (gchar*) _menu.get_active()->get_data("name"));
479         cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
481         // Checks whether there is a defs element. Creates it when needed
482         Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
483         if (!defsRepr){
484             defsRepr = xml_doc->createElement("svg:defs");
485             xml_doc->root()->addChild(defsRepr, NULL);
486         }
488         g_assert(SP_ROOT(desktop->doc()->root)->defs);
489         defsRepr->addChild(cprofRepr, NULL);
491         // TODO check if this next line was sometimes needed. It being there caused an assertion.
492         //Inkscape::GC::release(defsRepr);
494         // inform the document, so we can undo
495         sp_document_done(desktop->doc(), SP_VERB_EDIT_LINK_COLOR_PROFILE, _("Link Color Profile"));
497         populate_linked_profiles_box();
498     }
501 void
502 DocumentProperties::populate_linked_profiles_box()
504     _LinkedProfilesListStore->clear();
505     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
506     if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
507     while ( current ) {
508         SPObject* obj = SP_OBJECT(current->data);
509         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
510         Gtk::TreeModel::Row row = *(_LinkedProfilesListStore->append());
511         row[_LinkedProfilesListColumns.nameColumn] = prof->name;
512 //        row[_LinkedProfilesListColumns.previewColumn] = "Color Preview";
513         current = g_slist_next(current);
514     }
517 void DocumentProperties::external_scripts_list_button_release(GdkEventButton* event)
519     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
520         _ExternalScriptsContextMenu.popup(event->button, event->time);
521     }
524 void DocumentProperties::linked_profiles_list_button_release(GdkEventButton* event)
526     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
527         _EmbProfContextMenu.popup(event->button, event->time);
528     }
531 void DocumentProperties::cms_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
533     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
534     _EmbProfContextMenu.append(*mi);
535     mi->signal_activate().connect(rem);
536     mi->show();
537     _EmbProfContextMenu.accelerate(parent);
541 void DocumentProperties::scripting_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
543     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
544     _ExternalScriptsContextMenu.append(*mi);
545     mi->signal_activate().connect(rem);
546     mi->show();
547     _ExternalScriptsContextMenu.accelerate(parent);
550 void DocumentProperties::removeSelectedProfile(){
551     Glib::ustring name;
552     if(_LinkedProfilesList.get_selection()) {
553         Gtk::TreeModel::iterator i = _LinkedProfilesList.get_selection()->get_selected();
555         if(i){
556             name = (*i)[_LinkedProfilesListColumns.nameColumn];
557         } else {
558             return;
559         }
560     }
562     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
563     while ( current ) {
564         SPObject* obj = SP_OBJECT(current->data);
565         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
566         if (!name.compare(prof->name)){
567             sp_repr_unparent(obj->repr);
568             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove linked color profile"));
569         }
570         current = g_slist_next(current);
571     }
573     populate_linked_profiles_box();
576 void
577 DocumentProperties::build_cms()
579     _page_cms.show();
581     Gtk::Label *label_link= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
582     label_link->set_markup (_("<b>Linked Color Profiles:</b>"));
583     Gtk::Label *label_avail = manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
584     label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
586     _link_btn.set_label(_("Link Profile"));
588     _page_cms.set_spacing(4);
589     gint row = 0;
591     label_link->set_alignment(0.0);
592     _page_cms.table().attach(*label_link, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
593     row++;
594     _page_cms.table().attach(_LinkedProfilesListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
595     row++;
597     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
598     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
599     _page_cms.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
600     row++;
602     label_avail->set_alignment(0.0);
603     _page_cms.table().attach(*label_avail, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
604     row++;
605     _page_cms.table().attach(_combo_avail, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
606     _page_cms.table().attach(_link_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
608     populate_available_profiles();
610     _combo_avail.set_menu(_menu);
611     _combo_avail.set_history(0);
612     _combo_avail.show_all();
614     //# Set up the Linked Profiles combo box
615     _LinkedProfilesListStore = Gtk::ListStore::create(_LinkedProfilesListColumns);
616     _LinkedProfilesList.set_model(_LinkedProfilesListStore);
617     _LinkedProfilesList.append_column(_("Profile Name"), _LinkedProfilesListColumns.nameColumn);
618 //    _LinkedProfilesList.append_column(_("Color Preview"), _LinkedProfilesListColumns.previewColumn);
619     _LinkedProfilesList.set_headers_visible(false);
620 // TODO restore?    _LinkedProfilesList.set_fixed_height_mode(true);
622     populate_linked_profiles_box();
624     _LinkedProfilesListScroller.add(_LinkedProfilesList);
625     _LinkedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
626     _LinkedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
627     _LinkedProfilesListScroller.set_size_request(-1, 90);
629     _link_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::linkSelectedProfile));
631     _LinkedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::linked_profiles_list_button_release));
632     cms_create_popup_menu(_LinkedProfilesList, sigc::mem_fun(*this, &DocumentProperties::removeSelectedProfile));
634     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "defs" );
635     if (current) {
636         _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
637     }
638     _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_linked_profiles_box));
640 #endif // ENABLE_LCMS
642 void
643 DocumentProperties::build_scripting()
645     _page_scripting.show();
647     Gtk::Label *label_script= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
648     label_script->set_markup (_("<b>External script files:</b>"));
650     _add_btn.set_label(_("Add"));
652     _page_scripting.set_spacing(4);
653     gint row = 0;
655     label_script->set_alignment(0.0);
656     _page_scripting.table().attach(*label_script, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
657     row++;
658     _page_scripting.table().attach(_ExternalScriptsListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
659     row++;
661     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
662     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
663     _page_scripting.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
664     row++;
666     _page_scripting.table().attach(_script_entry, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
667     _page_scripting.table().attach(_add_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
668     row++;
670     //# Set up the External Scripts box
671     _ExternalScriptsListStore = Gtk::ListStore::create(_ExternalScriptsListColumns);
672     _ExternalScriptsList.set_model(_ExternalScriptsListStore);
673     _ExternalScriptsList.append_column(_("Filename"), _ExternalScriptsListColumns.filenameColumn);
674     _ExternalScriptsList.set_headers_visible(true);
675 // TODO restore?    _ExternalScriptsList.set_fixed_height_mode(true);
677     populate_external_scripts_box();
679     _ExternalScriptsListScroller.add(_ExternalScriptsList);
680     _ExternalScriptsListScroller.set_shadow_type(Gtk::SHADOW_IN);
681     _ExternalScriptsListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
682     _ExternalScriptsListScroller.set_size_request(-1, 90);
684     _add_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::addExternalScript));
686     _ExternalScriptsList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::external_scripts_list_button_release));
687     scripting_create_popup_menu(_ExternalScriptsList, sigc::mem_fun(*this, &DocumentProperties::removeExternalScript));
689 //TODO: review this observers code:
690     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
691     if (current) {
692         _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
693     }
694     _ext_scripts_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_external_scripts_box));
698 void DocumentProperties::addExternalScript(){
699     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
700     if (!desktop){
701         g_warning("No active desktop");
702     } else {
703         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
704         Inkscape::XML::Node *scriptRepr = xml_doc->createElement("svg:script");
705         scriptRepr->setAttribute("xlink:href", (gchar*) _script_entry.get_text().c_str());
706         _script_entry.set_text("");
708         xml_doc->root()->addChild(scriptRepr, NULL);
710         // inform the document, so we can undo
711         sp_document_done(desktop->doc(), SP_VERB_EDIT_ADD_EXTERNAL_SCRIPT, _("Add external script..."));
713         populate_external_scripts_box();
714     }
717 void DocumentProperties::removeExternalScript(){
718     Glib::ustring name;
719     if(_ExternalScriptsList.get_selection()) {
720         Gtk::TreeModel::iterator i = _ExternalScriptsList.get_selection()->get_selected();
722         if(i){
723             name = (*i)[_ExternalScriptsListColumns.filenameColumn];
724         } else {
725             return;
726         }
727     }
729     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
730     while ( current ) {
731         SPObject* obj = SP_OBJECT(current->data);
732         SPScript* script = (SPScript*) obj;
733         if (!name.compare(script->xlinkhref)){
734             sp_repr_unparent(obj->repr);
735             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_EXTERNAL_SCRIPT, _("Remove external script"));
736         }
737         current = g_slist_next(current);
738     }
740     populate_external_scripts_box();
744 void DocumentProperties::populate_external_scripts_box(){
745     _ExternalScriptsListStore->clear();
746     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
747     if (current) _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
748     while ( current ) {
749         SPObject* obj = SP_OBJECT(current->data);
750         SPScript* script = (SPScript*) obj;
751         Gtk::TreeModel::Row row = *(_ExternalScriptsListStore->append());
752         row[_ExternalScriptsListColumns.filenameColumn] = script->xlinkhref;
753         current = g_slist_next(current);
754     }
757 /**
758 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
759 */
760 void
761 DocumentProperties::update_gridspage()
763     SPDesktop *dt = getDesktop();
764     SPNamedView *nv = sp_desktop_namedview(dt);
766     //remove all tabs
767     while (_grids_notebook.get_n_pages() != 0) {
768         _grids_notebook.remove_page(-1); // this also deletes the page.
769     }
771     //add tabs
772     bool grids_present = false;
773     for (GSList const * l = nv->grids; l != NULL; l = l->next) {
774         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
775         if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
776         Glib::ustring name(grid->repr->attribute("id"));
777         const char *icon = NULL;
778         switch (grid->getGridType()) {
779             case GRID_RECTANGULAR:
780                 icon = INKSCAPE_ICON_GRID_XY;
781                 break;
782             case GRID_AXONOMETRIC:
783                 icon = INKSCAPE_ICON_GRID_AXONOM;
784                 break;
785             default:
786                 break;
787         }
788         _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
789         grids_present = true;
790     }
791     _grids_notebook.show_all();
793     if (grids_present)
794         _grids_button_remove.set_sensitive(true);
795     else
796         _grids_button_remove.set_sensitive(false);
799 /**
800  * Build grid page of dialog.
801  */
802 void
803 DocumentProperties::build_gridspage()
805     /// \todo FIXME: gray out snapping when grid is off.
806     /// Dissenting view: you want snapping without grid.
808     SPDesktop *dt = getDesktop();
809     SPNamedView *nv = sp_desktop_namedview(dt);
810     (void)nv;
812     _grids_label_crea.set_markup(_("<b>Creation</b>"));
813     _grids_label_def.set_markup(_("<b>Defined grids</b>"));
814     _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
815     _grids_hbox_crea.pack_start(_grids_button_new, true, true);
817     for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
818         _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
819     }
820     _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
822     _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
824     _grids_vbox.set_spacing(4);
825     _grids_vbox.pack_start(_grids_label_crea, false, false);
826     _grids_vbox.pack_start(_grids_hbox_crea, false, false);
827     _grids_vbox.pack_start(_grids_space, false, false);
828     _grids_vbox.pack_start(_grids_label_def, false, false);
829     _grids_vbox.pack_start(_grids_notebook, false, false);
830     _grids_vbox.pack_start(_grids_button_remove, false, false);
832     update_gridspage();
837 /**
838  * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
839  */
840 void
841 DocumentProperties::update()
843     if (_wr.isUpdating()) return;
845     SPDesktop *dt = getDesktop();
846     SPNamedView *nv = sp_desktop_namedview(dt);
848     _wr.setUpdating (true);
849     set_sensitive (true);
851     //-----------------------------------------------------------page page
852     _rcp_bg.setRgba32 (nv->pagecolor);
853     _rcb_canb.setActive (nv->showborder);
854     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
855     _rcp_bord.setRgba32 (nv->bordercolor);
856     _rcb_shad.setActive (nv->showpageshadow);
858     if (nv->doc_units)
859         _rum_deflt.setUnit (nv->doc_units);
861     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
862     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
863     _page_sizer.setDim (doc_w_px, doc_h_px);
865     //-----------------------------------------------------------guide page
867     _rcb_sgui.setActive (nv->showguides);
868     _rcp_gui.setRgba32 (nv->guidecolor);
869     _rcp_hgui.setRgba32 (nv->guidehicolor);
871     //-----------------------------------------------------------snap page
873     _rcbsnbb.setActive (nv->snap_manager.snapprefs.getSnapModeBBox());
874     _rcbsnn.setActive (nv->snap_manager.snapprefs.getSnapModeNode());
875     _rcbsng.setActive (nv->snap_manager.snapprefs.getSnapModeGuide());
876     _rcbic.setActive (nv->snap_manager.snapprefs.getIncludeItemCenter());
877     _rcbsm.setActive (nv->snap_manager.snapprefs.getSnapSmoothNodes());
878     _rcbmp.setActive (nv->snap_manager.snapprefs.getSnapMidpoints());
879     _rcbsigg.setActive (nv->snap_manager.snapprefs.getSnapIntersectionGG());
880     _rcbsils.setActive (nv->snap_manager.snapprefs.getSnapIntersectionCS());
881     _rcbsnop.setActive(nv->snap_manager.object.getSnapToItemPath());
882     _rcbsnon.setActive(nv->snap_manager.object.getSnapToItemNode());
883     _rcbsnbbp.setActive(nv->snap_manager.object.getSnapToBBoxPath());
884     _rcbsnbbn.setActive(nv->snap_manager.object.getSnapToBBoxNode());
885     _rcbsnpb.setActive(nv->snap_manager.object.getSnapToPageBorder());
886     _rsu_sno.setValue (nv->objecttolerance);
888     _rsu_sn.setValue (nv->gridtolerance);
890     _rsu_gusn.setValue (nv->guidetolerance);
892     _rcbs.setActive (nv->snap_manager.snapprefs.getSnapEnabledGlobally());
894     //-----------------------------------------------------------grids page
896     update_gridspage();
898     //------------------------------------------------Color Management page
900 #if ENABLE_LCMS
901     populate_linked_profiles_box();
902     populate_available_profiles();
903 #endif // ENABLE_LCMS
905     _wr.setUpdating (false);
908 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
909 Gtk::HBox&
910 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
912     Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
913     _tab_label_box->set_spacing(4);
914     _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
915                                                        label_image)));
917     Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
918     _tab_label_box->pack_start(*_tab_label);
919     _tab_label_box->show_all();
921     return *_tab_label_box;
924 //--------------------------------------------------------------------
926 void
927 DocumentProperties::on_response (int id)
929     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
930     {
931         _rcp_bg.closeWindow();
932         _rcp_bord.closeWindow();
933         _rcp_gui.closeWindow();
934         _rcp_hgui.closeWindow();
935     }
937     if (id == Gtk::RESPONSE_CLOSE)
938         hide();
941 void
942 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
944     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
945     repr->addListener(&_repr_events, this);
946     Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
947     root->addListener(&_repr_events, this);
948     update();
951 void
952 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
954     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
955     repr->addListener(&_repr_events, this);
956     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
957     root->addListener(&_repr_events, this);
958     update();
961 void
962 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
964     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
965     repr->removeListenerByData(this);
966     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
967     root->removeListenerByData(this);
970 static void
971 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
973     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
974         dialog->update_gridspage();
977 static void
978 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
980     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
981         dialog->update_gridspage();
986 /**
987  * Called when XML node attribute changed; updates dialog widgets.
988  */
989 static void
990 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
992     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
993         dialog->update();
997 /*########################################################################
998 # BUTTON CLICK HANDLERS    (callbacks)
999 ########################################################################*/
1001 void
1002 DocumentProperties::onNewGrid()
1004     SPDesktop *dt = getDesktop();
1005     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
1006     SPDocument *doc = sp_desktop_document(dt);
1008     Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
1009     CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
1011     // toggle grid showing to ON:
1012     dt->showGrids(true);
1016 void
1017 DocumentProperties::onRemoveGrid()
1019     gint pagenum = _grids_notebook.get_current_page();
1020     if (pagenum == -1) // no pages
1021       return;
1023     SPDesktop *dt = getDesktop();
1024     SPNamedView *nv = sp_desktop_namedview(dt);
1025     Inkscape::CanvasGrid * found_grid = NULL;
1026     int i = 0;
1027     for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) {  // not a very nice fix, but works.
1028         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
1029         if (pagenum == i) {
1030             found_grid = grid;
1031             break; // break out of for-loop
1032         }
1033     }
1034     if (found_grid) {
1035         // delete the grid that corresponds with the selected tab
1036         // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
1037         found_grid->repr->parent()->removeChild(found_grid->repr);
1038         sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
1039     }
1043 } // namespace Dialog
1044 } // namespace UI
1045 } // namespace Inkscape
1047 /*
1048   Local Variables:
1049   mode:c++
1050   c-file-style:"stroustrup"
1051   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1052   indent-tabs-mode:nil
1053   fill-column:99
1054   End:
1055 */
1056 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :