Code

All snap options which can be accessed through the snapping toolbar are now removed...
[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_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       _grids_label_crea("", Gtk::ALIGN_LEFT),
109       //TRANSLATORS: In Grid|_New translate only the word _New. It ref to grid
110       _grids_button_new(Q_("Grid|_New"), _("Create new grid.")),
111       _grids_button_remove(_("_Remove"), _("Remove selected grid.")),
112       _grids_label_def("", Gtk::ALIGN_LEFT)
113     //---------------------------------------------------------------
115     _tt.enable();
116     _getContents()->set_spacing (4);
117     _getContents()->pack_start(_notebook, true, true);
119     _notebook.append_page(_page_page,      _("Page"));
120     _notebook.append_page(_page_guides,    _("Guides"));
121     _notebook.append_page(_grids_vbox,     _("Grids"));
122     _notebook.append_page(_page_snap,      _("Snap"));
123     _notebook.append_page(_page_cms, _("Color Management"));
124     _notebook.append_page(_page_scripting, _("Scripting"));
126     build_page();
127     build_guides();
128     build_gridspage();
129     build_snap();
130 #if ENABLE_LCMS
131     build_cms();
132 #endif // ENABLE_LCMS
133     build_scripting();
135     _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
136     _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
138     signalDocumentReplaced().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDocumentReplaced));
139     signalActivateDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleActivateDesktop));
140     signalDeactiveDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDeactivateDesktop));
143 void
144 DocumentProperties::init()
146     update();
148     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
149     repr->addListener (&_repr_events, this);
150     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
151     root->addListener (&_repr_events, this);
153     show_all_children();
154     _grids_button_remove.hide();
157 DocumentProperties::~DocumentProperties()
159     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
160     repr->removeListenerByData (this);
161     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
162     root->removeListenerByData (this);
165 //========================================================================
167 /**
168  * Helper function that attaches widgets in a 3xn table. The widgets come in an
169  * array that has two entries per table row. The two entries code for four
170  * possible cases: (0,0) means insert space in first column; (0, non-0) means
171  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
172  * (non-0, non-0) means two widgets in columns 2 and 3.
173 **/
174 inline void
175 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
177     for (unsigned i = 0, r = start; i < n; i += 2)
178     {
179         if (arr[i] && arr[i+1])
180         {
181             table.attach(*arr[i],   1, 2, r, r+1,
182                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
183             table.attach(*arr[i+1], 2, 3, r, r+1,
184                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
185         }
186         else
187         {
188             if (arr[i+1]) {
189                 Gtk::AttachOptions yoptions = (Gtk::AttachOptions)0;
190                 if (dynamic_cast<Inkscape::UI::Widget::PageSizer*>(arr[i+1])) {
191                     // only the PageSizer in Document Properties|Page should be stretched vertically
192                     yoptions = Gtk::FILL|Gtk::EXPAND;
193                 }
194                 table.attach(*arr[i+1], 1, 3, r, r+1,
195                       Gtk::FILL|Gtk::EXPAND, yoptions, 0,0);
196             }
197             else if (arr[i])
198             {
199                 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
200                 label.set_alignment (0.0);
201                 table.attach (label, 0, 3, r, r+1,
202                       Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
203             }
204             else
205             {
206                 Gtk::HBox *space = manage (new Gtk::HBox);
207                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
208                 table.attach (*space, 0, 1, r, r+1,
209                       (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
210             }
211         }
212         ++r;
213     }
216 void
217 DocumentProperties::build_page()
219     _page_page.show();
221     Gtk::Label* label_gen = manage (new Gtk::Label);
222     label_gen->set_markup (_("<b>General</b>"));
223     Gtk::Label* label_bor = manage (new Gtk::Label);
224     label_bor->set_markup (_("<b>Border</b>"));
225     Gtk::Label *label_for = manage (new Gtk::Label);
226     label_for->set_markup (_("<b>Format</b>"));
227     _page_sizer.init();
229     Gtk::Widget *const widget_array[] =
230     {
231         label_gen,         0,
232         0,                 &_rum_deflt,
233         _rcp_bg._label,    &_rcp_bg,
234         0,                 0,
235         label_for,         0,
236         0,                 &_page_sizer,
237         0,                 0,
238         label_bor,         0,
239         0,                 &_rcb_canb,
240         0,                 &_rcb_bord,
241         0,                 &_rcb_shad,
242         _rcp_bord._label,  &_rcp_bord,
243     };
245     attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
248 void
249 DocumentProperties::build_guides()
251     _page_guides.show();
253     Gtk::Label *label_gui = manage (new Gtk::Label);
254     label_gui->set_markup (_("<b>Guides</b>"));
256     Gtk::Widget *const widget_array[] =
257     {
258         label_gui,        0,
259         0,                &_rcb_sgui,
260         _rcp_gui._label,  &_rcp_gui,
261         _rcp_hgui._label, &_rcp_hgui,
262         0,                &_rcbsng,
263     };
265     attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
268 void
269 DocumentProperties::build_snap()
271     _page_snap.show();
273     _rsu_sno.init (_("Snap _distance"), _("Snap only when _closer than:"), _("Always snap"),
274                   _("Snapping distance, in screen pixels, for snapping to objects"), _("Always snap to objects, regardless of their distance"),
275                   _("If set, objects only snap to another object when it's within the range specified below"),
276                   "objecttolerance", _wr);
278     //Options for snapping to grids
279     _rsu_sn.init (_("Snap d_istance"), _("Snap only when c_loser than:"), _("Always snap"),
280                   _("Snapping distance, in screen pixels, for snapping to grid"), _("Always snap to grids, regardless of the distance"),
281                   _("If set, objects only snap to a grid line when it's within the range specified below"),
282                   "gridtolerance", _wr);
284     //Options for snapping to guides
285     _rsu_gusn.init (_("Snap dist_ance"), _("Snap only when close_r than:"), _("Always snap"),
286                 _("Snapping distance, in screen pixels, for snapping to guides"), _("Always snap to guides, regardless of the distance"),
287                 _("If set, objects only snap to a guide when it's within the range specified below"),
288                 "guidetolerance", _wr);
290     Gtk::Label *label_o = manage (new Gtk::Label);
291     label_o->set_markup (_("<b>Snap to objects</b>"));
292     Gtk::Label *label_gr = manage (new Gtk::Label);
293     label_gr->set_markup (_("<b>Snap to grids</b>"));
294     Gtk::Label *label_gu = manage (new Gtk::Label);
295     label_gu->set_markup (_("<b>Snap to guides</b>"));
297     Gtk::Widget *const array[] =
298     {
299         label_o,            0,
300         0,                  _rsu_sno._vbox,
301         0,                  0,
302         label_gr,           0,
303         0,                  _rsu_sn._vbox,
304         0,                  0,
305         label_gu,           0,
306         0,                  _rsu_gusn._vbox
307     };
309     attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
310  }
312 #if ENABLE_LCMS
313 static void
314 lcms_profile_get_name (cmsHPROFILE   profile, const gchar **name)
316   if (profile)
317     {
318       *name = cmsTakeProductDesc (profile);
320       if (! *name)
321         *name = cmsTakeProductName (profile);
323       if (*name && ! g_utf8_validate (*name, -1, NULL))
324         *name = _("(invalid UTF-8 string)");
325     }
326   else
327     {
328       *name = _("None");
329     }
332 void
333 DocumentProperties::populate_available_profiles(){
334     Glib::ListHandle<Gtk::Widget*> children = _menu.get_children();
335     for ( Glib::ListHandle<Gtk::Widget*>::iterator it2 = children.begin(); it2 != children.end(); ++it2 ) {
336         _menu.remove(**it2);
337         delete(*it2);
338     }
340     std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
342     // Use this loop to iterate through a list of possible document locations.
343     for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
344         if ( Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_EXISTS )
345              && Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_IS_DIR )) {
346             GError *err = 0;
347             GDir *directory = g_dir_open(it->c_str(), 0, &err);
348             if (!directory) {
349                 gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str());
350                 g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
351                 g_free(safeDir);
352             } else {
353                 gchar *filename = 0;
354                 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
355                     gchar* full = g_build_filename(it->c_str(), filename, NULL);
356                     if ( !Inkscape::IO::file_test( full, G_FILE_TEST_IS_DIR ) ) {
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 void
386 DocumentProperties::linkSelectedProfile()
388 //store this profile in the SVG document (create <color-profile> element in the XML)
389     // TODO remove use of 'active' desktop
390     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
391     if (!desktop){
392         g_warning("No active desktop");
393     } else {
394         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
395         Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
396         cprofRepr->setAttribute("name", (gchar*) _menu.get_active()->get_data("name"));
397         cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
399         // Checks whether there is a defs element. Creates it when needed
400         Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
401         if (!defsRepr){
402             defsRepr = xml_doc->createElement("svg:defs");
403             xml_doc->root()->addChild(defsRepr, NULL);
404         }
406         g_assert(SP_ROOT(desktop->doc()->root)->defs);
407         defsRepr->addChild(cprofRepr, NULL);
409         // TODO check if this next line was sometimes needed. It being there caused an assertion.
410         //Inkscape::GC::release(defsRepr);
412         // inform the document, so we can undo
413         sp_document_done(desktop->doc(), SP_VERB_EDIT_LINK_COLOR_PROFILE, _("Link Color Profile"));
415         populate_linked_profiles_box();
416     }
419 void
420 DocumentProperties::populate_linked_profiles_box()
422     _LinkedProfilesListStore->clear();
423     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
424     if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
425     while ( current ) {
426         SPObject* obj = SP_OBJECT(current->data);
427         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
428         Gtk::TreeModel::Row row = *(_LinkedProfilesListStore->append());
429         row[_LinkedProfilesListColumns.nameColumn] = prof->name;
430 //        row[_LinkedProfilesListColumns.previewColumn] = "Color Preview";
431         current = g_slist_next(current);
432     }
435 void DocumentProperties::external_scripts_list_button_release(GdkEventButton* event)
437     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
438         _ExternalScriptsContextMenu.popup(event->button, event->time);
439     }
442 void DocumentProperties::linked_profiles_list_button_release(GdkEventButton* event)
444     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
445         _EmbProfContextMenu.popup(event->button, event->time);
446     }
449 void DocumentProperties::cms_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
451     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
452     _EmbProfContextMenu.append(*mi);
453     mi->signal_activate().connect(rem);
454     mi->show();
455     _EmbProfContextMenu.accelerate(parent);
459 void DocumentProperties::scripting_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
461     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
462     _ExternalScriptsContextMenu.append(*mi);
463     mi->signal_activate().connect(rem);
464     mi->show();
465     _ExternalScriptsContextMenu.accelerate(parent);
468 void DocumentProperties::removeSelectedProfile(){
469     Glib::ustring name;
470     if(_LinkedProfilesList.get_selection()) {
471         Gtk::TreeModel::iterator i = _LinkedProfilesList.get_selection()->get_selected();
473         if(i){
474             name = (*i)[_LinkedProfilesListColumns.nameColumn];
475         } else {
476             return;
477         }
478     }
480     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
481     while ( current ) {
482         SPObject* obj = SP_OBJECT(current->data);
483         Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
484         if (!name.compare(prof->name)){
485             sp_repr_unparent(obj->repr);
486             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove linked color profile"));
487         }
488         current = g_slist_next(current);
489     }
491     populate_linked_profiles_box();
494 void
495 DocumentProperties::build_cms()
497     _page_cms.show();
499     Gtk::Label *label_link= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
500     label_link->set_markup (_("<b>Linked Color Profiles:</b>"));
501     Gtk::Label *label_avail = manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
502     label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
504     _link_btn.set_label(_("Link Profile"));
506     _page_cms.set_spacing(4);
507     gint row = 0;
509     label_link->set_alignment(0.0);
510     _page_cms.table().attach(*label_link, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
511     row++;
512     _page_cms.table().attach(_LinkedProfilesListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
513     row++;
515     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
516     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
517     _page_cms.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
518     row++;
520     label_avail->set_alignment(0.0);
521     _page_cms.table().attach(*label_avail, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
522     row++;
523     _page_cms.table().attach(_combo_avail, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
524     _page_cms.table().attach(_link_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
526     populate_available_profiles();
528     _combo_avail.set_menu(_menu);
529     _combo_avail.set_history(0);
530     _combo_avail.show_all();
532     //# Set up the Linked Profiles combo box
533     _LinkedProfilesListStore = Gtk::ListStore::create(_LinkedProfilesListColumns);
534     _LinkedProfilesList.set_model(_LinkedProfilesListStore);
535     _LinkedProfilesList.append_column(_("Profile Name"), _LinkedProfilesListColumns.nameColumn);
536 //    _LinkedProfilesList.append_column(_("Color Preview"), _LinkedProfilesListColumns.previewColumn);
537     _LinkedProfilesList.set_headers_visible(false);
538 // TODO restore?    _LinkedProfilesList.set_fixed_height_mode(true);
540     populate_linked_profiles_box();
542     _LinkedProfilesListScroller.add(_LinkedProfilesList);
543     _LinkedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
544     _LinkedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
545     _LinkedProfilesListScroller.set_size_request(-1, 90);
547     _link_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::linkSelectedProfile));
549     _LinkedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::linked_profiles_list_button_release));
550     cms_create_popup_menu(_LinkedProfilesList, sigc::mem_fun(*this, &DocumentProperties::removeSelectedProfile));
552     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "defs" );
553     if (current) {
554         _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
555     }
556     _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_linked_profiles_box));
558 #endif // ENABLE_LCMS
560 void
561 DocumentProperties::build_scripting()
563     _page_scripting.show();
565     Gtk::Label *label_script= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
566     label_script->set_markup (_("<b>External script files:</b>"));
568     _add_btn.set_label(_("Add"));
570     _page_scripting.set_spacing(4);
571     gint row = 0;
573     label_script->set_alignment(0.0);
574     _page_scripting.table().attach(*label_script, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
575     row++;
576     _page_scripting.table().attach(_ExternalScriptsListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
577     row++;
579     Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
580     spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
581     _page_scripting.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
582     row++;
584     _page_scripting.table().attach(_script_entry, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
585     _page_scripting.table().attach(_add_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
586     row++;
588     //# Set up the External Scripts box
589     _ExternalScriptsListStore = Gtk::ListStore::create(_ExternalScriptsListColumns);
590     _ExternalScriptsList.set_model(_ExternalScriptsListStore);
591     _ExternalScriptsList.append_column(_("Filename"), _ExternalScriptsListColumns.filenameColumn);
592     _ExternalScriptsList.set_headers_visible(true);
593 // TODO restore?    _ExternalScriptsList.set_fixed_height_mode(true);
595     populate_external_scripts_box();
597     _ExternalScriptsListScroller.add(_ExternalScriptsList);
598     _ExternalScriptsListScroller.set_shadow_type(Gtk::SHADOW_IN);
599     _ExternalScriptsListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
600     _ExternalScriptsListScroller.set_size_request(-1, 90);
602     _add_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::addExternalScript));
604 #if ENABLE_LCMS
605     _ExternalScriptsList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::external_scripts_list_button_release));
606     scripting_create_popup_menu(_ExternalScriptsList, sigc::mem_fun(*this, &DocumentProperties::removeExternalScript));
607 #endif // ENABLE_LCMS
609 //TODO: review this observers code:
610     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
611     if (current) {
612         _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
613     }
614     _ext_scripts_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_external_scripts_box));
618 void DocumentProperties::addExternalScript(){
619     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
620     if (!desktop){
621         g_warning("No active desktop");
622     } else {
623         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
624         Inkscape::XML::Node *scriptRepr = xml_doc->createElement("svg:script");
625         scriptRepr->setAttribute("xlink:href", (gchar*) _script_entry.get_text().c_str());
626         _script_entry.set_text("");
628         xml_doc->root()->addChild(scriptRepr, NULL);
630         // inform the document, so we can undo
631         sp_document_done(desktop->doc(), SP_VERB_EDIT_ADD_EXTERNAL_SCRIPT, _("Add external script..."));
633         populate_external_scripts_box();
634     }
637 void DocumentProperties::removeExternalScript(){
638     Glib::ustring name;
639     if(_ExternalScriptsList.get_selection()) {
640         Gtk::TreeModel::iterator i = _ExternalScriptsList.get_selection()->get_selected();
642         if(i){
643             name = (*i)[_ExternalScriptsListColumns.filenameColumn];
644         } else {
645             return;
646         }
647     }
649     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
650     while ( current ) {
651         SPObject* obj = SP_OBJECT(current->data);
652         SPScript* script = (SPScript*) obj;
653         if (!name.compare(script->xlinkhref)){
654             sp_repr_unparent(obj->repr);
655             sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_EXTERNAL_SCRIPT, _("Remove external script"));
656         }
657         current = g_slist_next(current);
658     }
660     populate_external_scripts_box();
664 void DocumentProperties::populate_external_scripts_box(){
665     _ExternalScriptsListStore->clear();
666     const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
667     if (current) _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
668     while ( current ) {
669         SPObject* obj = SP_OBJECT(current->data);
670         SPScript* script = (SPScript*) obj;
671         Gtk::TreeModel::Row row = *(_ExternalScriptsListStore->append());
672         row[_ExternalScriptsListColumns.filenameColumn] = script->xlinkhref;
673         current = g_slist_next(current);
674     }
677 /**
678 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
679 */
680 void
681 DocumentProperties::update_gridspage()
683     SPDesktop *dt = getDesktop();
684     SPNamedView *nv = sp_desktop_namedview(dt);
686     //remove all tabs
687     while (_grids_notebook.get_n_pages() != 0) {
688         _grids_notebook.remove_page(-1); // this also deletes the page.
689     }
691     //add tabs
692     bool grids_present = false;
693     for (GSList const * l = nv->grids; l != NULL; l = l->next) {
694         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
695         if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
696         Glib::ustring name(grid->repr->attribute("id"));
697         const char *icon = NULL;
698         switch (grid->getGridType()) {
699             case GRID_RECTANGULAR:
700                 icon = INKSCAPE_ICON_GRID_XY;
701                 break;
702             case GRID_AXONOMETRIC:
703                 icon = INKSCAPE_ICON_GRID_AXONOM;
704                 break;
705             default:
706                 break;
707         }
708         _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
709         grids_present = true;
710     }
711     _grids_notebook.show_all();
713     if (grids_present)
714         _grids_button_remove.set_sensitive(true);
715     else
716         _grids_button_remove.set_sensitive(false);
719 /**
720  * Build grid page of dialog.
721  */
722 void
723 DocumentProperties::build_gridspage()
725     /// \todo FIXME: gray out snapping when grid is off.
726     /// Dissenting view: you want snapping without grid.
728     SPDesktop *dt = getDesktop();
729     SPNamedView *nv = sp_desktop_namedview(dt);
730     (void)nv;
732     _grids_label_crea.set_markup(_("<b>Creation</b>"));
733     _grids_label_def.set_markup(_("<b>Defined grids</b>"));
734     _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
735     _grids_hbox_crea.pack_start(_grids_button_new, true, true);
737     for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
738         _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
739     }
740     _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
742     _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
744     _grids_vbox.set_spacing(4);
745     _grids_vbox.pack_start(_grids_label_crea, false, false);
746     _grids_vbox.pack_start(_grids_hbox_crea, false, false);
747     _grids_vbox.pack_start(_grids_space, false, false);
748     _grids_vbox.pack_start(_grids_label_def, false, false);
749     _grids_vbox.pack_start(_grids_notebook, false, false);
750     _grids_vbox.pack_start(_grids_button_remove, false, false);
752     update_gridspage();
757 /**
758  * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
759  */
760 void
761 DocumentProperties::update()
763     if (_wr.isUpdating()) return;
765     SPDesktop *dt = getDesktop();
766     SPNamedView *nv = sp_desktop_namedview(dt);
768     _wr.setUpdating (true);
769     set_sensitive (true);
771     //-----------------------------------------------------------page page
772     _rcp_bg.setRgba32 (nv->pagecolor);
773     _rcb_canb.setActive (nv->showborder);
774     _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
775     _rcp_bord.setRgba32 (nv->bordercolor);
776     _rcb_shad.setActive (nv->showpageshadow);
778     if (nv->doc_units)
779         _rum_deflt.setUnit (nv->doc_units);
781     double const doc_w_px = sp_document_width(sp_desktop_document(dt));
782     double const doc_h_px = sp_document_height(sp_desktop_document(dt));
783     _page_sizer.setDim (doc_w_px, doc_h_px);
785     //-----------------------------------------------------------guide page
787     _rcb_sgui.setActive (nv->showguides);
788     _rcp_gui.setRgba32 (nv->guidecolor);
789     _rcp_hgui.setRgba32 (nv->guidehicolor);
791     //-----------------------------------------------------------snap page
793     _rsu_sno.setValue (nv->objecttolerance);
794     _rsu_sn.setValue (nv->gridtolerance);
795     _rsu_gusn.setValue (nv->guidetolerance);
798     //-----------------------------------------------------------grids page
800     update_gridspage();
802     //------------------------------------------------Color Management page
804 #if ENABLE_LCMS
805     populate_linked_profiles_box();
806     populate_available_profiles();
807 #endif // ENABLE_LCMS
809     _wr.setUpdating (false);
812 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
813 Gtk::HBox&
814 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
816     Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
817     _tab_label_box->set_spacing(4);
818     _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
819                                                        label_image)));
821     Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
822     _tab_label_box->pack_start(*_tab_label);
823     _tab_label_box->show_all();
825     return *_tab_label_box;
828 //--------------------------------------------------------------------
830 void
831 DocumentProperties::on_response (int id)
833     if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
834     {
835         _rcp_bg.closeWindow();
836         _rcp_bord.closeWindow();
837         _rcp_gui.closeWindow();
838         _rcp_hgui.closeWindow();
839     }
841     if (id == Gtk::RESPONSE_CLOSE)
842         hide();
845 void
846 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
848     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
849     repr->addListener(&_repr_events, this);
850     Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
851     root->addListener(&_repr_events, this);
852     update();
855 void
856 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
858     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
859     repr->addListener(&_repr_events, this);
860     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
861     root->addListener(&_repr_events, this);
862     update();
865 void
866 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
868     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
869     repr->removeListenerByData(this);
870     Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
871     root->removeListenerByData(this);
874 static void
875 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
877     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
878         dialog->update_gridspage();
881 static void
882 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
884     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
885         dialog->update_gridspage();
890 /**
891  * Called when XML node attribute changed; updates dialog widgets.
892  */
893 static void
894 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
896     if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
897         dialog->update();
901 /*########################################################################
902 # BUTTON CLICK HANDLERS    (callbacks)
903 ########################################################################*/
905 void
906 DocumentProperties::onNewGrid()
908     SPDesktop *dt = getDesktop();
909     Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
910     SPDocument *doc = sp_desktop_document(dt);
912     Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
913     CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
915     // toggle grid showing to ON:
916     dt->showGrids(true);
920 void
921 DocumentProperties::onRemoveGrid()
923     gint pagenum = _grids_notebook.get_current_page();
924     if (pagenum == -1) // no pages
925       return;
927     SPDesktop *dt = getDesktop();
928     SPNamedView *nv = sp_desktop_namedview(dt);
929     Inkscape::CanvasGrid * found_grid = NULL;
930     int i = 0;
931     for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) {  // not a very nice fix, but works.
932         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
933         if (pagenum == i) {
934             found_grid = grid;
935             break; // break out of for-loop
936         }
937     }
938     if (found_grid) {
939         // delete the grid that corresponds with the selected tab
940         // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
941         found_grid->repr->parent()->removeChild(found_grid->repr);
942         sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
943     }
947 } // namespace Dialog
948 } // namespace UI
949 } // namespace Inkscape
951 /*
952   Local Variables:
953   mode:c++
954   c-file-style:"stroustrup"
955   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
956   indent-tabs-mode:nil
957   fill-column:99
958   End:
959 */
960 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :