1 /** @file
2 * @brief Document properties dialog, Gtkmm-style
3 */
4 /* Authors:
5 * bulia byak <buliabyak@users.sf.net>
6 * Bryce W. Harrington <bryce@bryceharrington.org>
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Jon Phillips <jon@rejon.org>
9 * Ralf Stephan <ralf@ark.in-berlin.de> (Gtkmm)
10 * Diederik van Lierop <mail@diedenrezi.nl>
11 *
12 * Copyright (C) 2006-2008 Johan Engelen <johan@shouraizou.nl>
13 * Copyright (C) 2000 - 2008 Authors
14 *
15 * Released under GNU GPL. Read the file 'COPYING' for more information
16 */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include "display/canvas-grid.h"
23 #include "document-properties.h"
24 #include "document.h"
25 #include "desktop-handles.h"
26 #include "desktop.h"
27 #include <gtkmm.h>
28 #include "helper/units.h"
29 #include "inkscape.h"
30 #include "io/sys.h"
31 #include "preferences.h"
32 #include "sp-namedview.h"
33 #include "sp-object-repr.h"
34 #include "sp-root.h"
35 #include "sp-script.h"
36 #include "ui/widget/color-picker.h"
37 #include "ui/widget/scalar-unit.h"
38 #include "verbs.h"
39 #include "widgets/icon.h"
40 #include "xml/node-event-vector.h"
41 #include "xml/repr.h"
43 #if ENABLE_LCMS
44 #include <lcms.h>
45 //#include "color-profile-fns.h"
46 #include "color-profile.h"
47 #endif // ENABLE_LCMS
49 using std::pair;
51 namespace Inkscape {
52 namespace UI {
53 namespace Dialog {
55 #define SPACE_SIZE_X 15
56 #define SPACE_SIZE_Y 10
59 //===================================================
61 //---------------------------------------------------
63 static void on_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
64 static void on_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void * data);
65 static void on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer);
67 static Inkscape::XML::NodeEventVector const _repr_events = {
68 on_child_added, // child_added
69 on_child_removed, // child_removed
70 on_repr_attr_changed,
71 NULL, // content_changed
72 NULL // order_changed
73 };
76 DocumentProperties &
77 DocumentProperties::getInstance()
78 {
79 DocumentProperties &instance = *new DocumentProperties();
80 instance.init();
82 return instance;
83 }
85 DocumentProperties::DocumentProperties()
86 : UI::Widget::Panel ("", "/dialogs/documentoptions", SP_VERB_DIALOG_NAMEDVIEW),
87 _page_page(1, 1, true, true), _page_guides(1, 1),
88 _page_snap(1, 1), _page_cms(1, 1), _page_scripting(1, 1),
89 //---------------------------------------------------------------
90 _rcb_canb(_("Show page _border"), _("If set, rectangular page border is shown"), "showborder", _wr, false),
91 _rcb_bord(_("Border on _top of drawing"), _("If set, border is always on top of the drawing"), "borderlayer", _wr, false),
92 _rcb_shad(_("_Show border shadow"), _("If set, page border shows a shadow on its right and lower side"), "inkscape:showpageshadow", _wr, false),
93 _rcp_bg(_("Back_ground:"), _("Background color"), _("Color and transparency of the page background (also used for bitmap export)"), "pagecolor", "inkscape:pageopacity", _wr),
94 _rcp_bord(_("Border _color:"), _("Page border color"), _("Color of the page border"), "bordercolor", "borderopacity", _wr),
95 _rum_deflt(_("Default _units:"), "inkscape:document-units", _wr),
96 _page_sizer(_wr),
97 //---------------------------------------------------------------
98 //General snap options
99 _rcb_sgui(_("Show _guides"), _("Show or hide guides"), "showguides", _wr),
100 _rcbsng(_("_Snap guides while dragging"), _("While dragging a guide, snap to object nodes or bounding box corners ('Snap to nodes' or 'snap to bounding box corners' must be enabled; only a small part of the guide near the cursor will snap)"),
101 "inkscape:snap-from-guide", _wr),
102 _rcp_gui(_("Guide co_lor:"), _("Guideline color"), _("Color of guidelines"), "guidecolor", "guideopacity", _wr),
103 _rcp_hgui(_("_Highlight color:"), _("Highlighted guideline color"), _("Color of a guideline when it is under mouse"), "guidehicolor", "guidehiopacity", _wr),
104 //---------------------------------------------------------------
105 _grids_label_crea("", Gtk::ALIGN_LEFT),
106 //TRANSLATORS: only translate "string" in "context|string".
107 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
108 // "New" refers to grid
109 _grids_button_new(Q_("Grid|_New"), _("Create new grid.")),
110 _grids_button_remove(_("_Remove"), _("Remove selected grid.")),
111 _grids_label_def("", Gtk::ALIGN_LEFT)
112 //---------------------------------------------------------------
113 {
114 _tt.enable();
115 _getContents()->set_spacing (4);
116 _getContents()->pack_start(_notebook, true, true);
118 _notebook.append_page(_page_page, _("Page"));
119 _notebook.append_page(_page_guides, _("Guides"));
120 _notebook.append_page(_grids_vbox, _("Grids"));
121 _notebook.append_page(_page_snap, _("Snap"));
122 _notebook.append_page(_page_cms, _("Color Management"));
123 _notebook.append_page(_page_scripting, _("Scripting"));
125 build_page();
126 build_guides();
127 build_gridspage();
128 build_snap();
129 #if ENABLE_LCMS
130 build_cms();
131 #endif // ENABLE_LCMS
132 build_scripting();
134 _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
135 _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
137 signalDocumentReplaced().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDocumentReplaced));
138 signalActivateDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleActivateDesktop));
139 signalDeactiveDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDeactivateDesktop));
140 }
142 void
143 DocumentProperties::init()
144 {
145 update();
147 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
148 repr->addListener (&_repr_events, this);
149 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
150 root->addListener (&_repr_events, this);
152 show_all_children();
153 _grids_button_remove.hide();
154 }
156 DocumentProperties::~DocumentProperties()
157 {
158 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
159 repr->removeListenerByData (this);
160 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
161 root->removeListenerByData (this);
162 }
164 //========================================================================
166 /**
167 * Helper function that attaches widgets in a 3xn table. The widgets come in an
168 * array that has two entries per table row. The two entries code for four
169 * possible cases: (0,0) means insert space in first column; (0, non-0) means
170 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
171 * (non-0, non-0) means two widgets in columns 2 and 3.
172 **/
173 inline void
174 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
175 {
176 for (unsigned i = 0, r = start; i < n; i += 2)
177 {
178 if (arr[i] && arr[i+1])
179 {
180 table.attach(*arr[i], 1, 2, r, r+1,
181 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
182 table.attach(*arr[i+1], 2, 3, r, r+1,
183 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
184 }
185 else
186 {
187 if (arr[i+1]) {
188 Gtk::AttachOptions yoptions = (Gtk::AttachOptions)0;
189 if (dynamic_cast<Inkscape::UI::Widget::PageSizer*>(arr[i+1])) {
190 // only the PageSizer in Document Properties|Page should be stretched vertically
191 yoptions = Gtk::FILL|Gtk::EXPAND;
192 }
193 table.attach(*arr[i+1], 1, 3, r, r+1,
194 Gtk::FILL|Gtk::EXPAND, yoptions, 0,0);
195 }
196 else if (arr[i])
197 {
198 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
199 label.set_alignment (0.0);
200 table.attach (label, 0, 3, r, r+1,
201 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
202 }
203 else
204 {
205 Gtk::HBox *space = manage (new Gtk::HBox);
206 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
207 table.attach (*space, 0, 1, r, r+1,
208 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
209 }
210 }
211 ++r;
212 }
213 }
215 void
216 DocumentProperties::build_page()
217 {
218 _page_page.show();
220 Gtk::Label* label_gen = manage (new Gtk::Label);
221 label_gen->set_markup (_("<b>General</b>"));
222 Gtk::Label* label_bor = manage (new Gtk::Label);
223 label_bor->set_markup (_("<b>Border</b>"));
224 Gtk::Label *label_for = manage (new Gtk::Label);
225 label_for->set_markup (_("<b>Format</b>"));
226 _page_sizer.init();
228 Gtk::Widget *const widget_array[] =
229 {
230 label_gen, 0,
231 0, &_rum_deflt,
232 _rcp_bg._label, &_rcp_bg,
233 0, 0,
234 label_for, 0,
235 0, &_page_sizer,
236 0, 0,
237 label_bor, 0,
238 0, &_rcb_canb,
239 0, &_rcb_bord,
240 0, &_rcb_shad,
241 _rcp_bord._label, &_rcp_bord,
242 };
244 attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
245 }
247 void
248 DocumentProperties::build_guides()
249 {
250 _page_guides.show();
252 Gtk::Label *label_gui = manage (new Gtk::Label);
253 label_gui->set_markup (_("<b>Guides</b>"));
255 Gtk::Widget *const widget_array[] =
256 {
257 label_gui, 0,
258 0, &_rcb_sgui,
259 _rcp_gui._label, &_rcp_gui,
260 _rcp_hgui._label, &_rcp_hgui,
261 0, &_rcbsng,
262 };
264 attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
265 }
267 void
268 DocumentProperties::build_snap()
269 {
270 _page_snap.show();
272 _rsu_sno.init (_("Snap _distance"), _("Snap only when _closer than:"), _("Always snap"),
273 _("Snapping distance, in screen pixels, for snapping to objects"), _("Always snap to objects, regardless of their distance"),
274 _("If set, objects only snap to another object when it's within the range specified below"),
275 "objecttolerance", _wr);
277 //Options for snapping to grids
278 _rsu_sn.init (_("Snap d_istance"), _("Snap only when c_loser than:"), _("Always snap"),
279 _("Snapping distance, in screen pixels, for snapping to grid"), _("Always snap to grids, regardless of the distance"),
280 _("If set, objects only snap to a grid line when it's within the range specified below"),
281 "gridtolerance", _wr);
283 //Options for snapping to guides
284 _rsu_gusn.init (_("Snap dist_ance"), _("Snap only when close_r than:"), _("Always snap"),
285 _("Snapping distance, in screen pixels, for snapping to guides"), _("Always snap to guides, regardless of the distance"),
286 _("If set, objects only snap to a guide when it's within the range specified below"),
287 "guidetolerance", _wr);
289 Gtk::Label *label_o = manage (new Gtk::Label);
290 label_o->set_markup (_("<b>Snap to objects</b>"));
291 Gtk::Label *label_gr = manage (new Gtk::Label);
292 label_gr->set_markup (_("<b>Snap to grids</b>"));
293 Gtk::Label *label_gu = manage (new Gtk::Label);
294 label_gu->set_markup (_("<b>Snap to guides</b>"));
296 Gtk::Widget *const array[] =
297 {
298 label_o, 0,
299 0, _rsu_sno._vbox,
300 0, 0,
301 label_gr, 0,
302 0, _rsu_sn._vbox,
303 0, 0,
304 label_gu, 0,
305 0, _rsu_gusn._vbox
306 };
308 attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
309 }
311 #if ENABLE_LCMS
312 static void
313 lcms_profile_get_name (cmsHPROFILE profile, const gchar **name)
314 {
315 if (profile)
316 {
317 *name = cmsTakeProductDesc (profile);
319 if (! *name)
320 *name = cmsTakeProductName (profile);
322 if (*name && ! g_utf8_validate (*name, -1, NULL))
323 *name = _("(invalid UTF-8 string)");
324 }
325 else
326 {
327 *name = _("None");
328 }
329 }
331 void
332 DocumentProperties::populate_available_profiles(){
333 Glib::ListHandle<Gtk::Widget*> children = _menu.get_children();
334 for ( Glib::ListHandle<Gtk::Widget*>::iterator it2 = children.begin(); it2 != children.end(); ++it2 ) {
335 _menu.remove(**it2);
336 delete(*it2);
337 }
339 std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
341 // Use this loop to iterate through a list of possible document locations.
342 for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
343 if ( Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_EXISTS )
344 && Inkscape::IO::file_test( it->c_str(), G_FILE_TEST_IS_DIR )) {
345 GError *err = 0;
346 GDir *directory = g_dir_open(it->c_str(), 0, &err);
347 if (!directory) {
348 gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str());
349 g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
350 g_free(safeDir);
351 } else {
352 gchar *filename = 0;
353 while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
354 gchar* full = g_build_filename(it->c_str(), filename, NULL);
355 if ( !Inkscape::IO::file_test( full, G_FILE_TEST_IS_DIR ) ) {
356 cmsHPROFILE hProfile = cmsOpenProfileFromFile(full, "r");
357 if (hProfile != NULL){
358 const gchar* name;
359 lcms_profile_get_name(hProfile, &name);
360 Gtk::MenuItem* mi = manage(new Gtk::MenuItem());
361 mi->set_data("filepath", g_strdup(full));
362 mi->set_data("name", g_strdup(name));
363 Gtk::HBox *hbox = manage(new Gtk::HBox());
364 hbox->show();
365 Gtk::Label* lbl = manage(new Gtk::Label(name));
366 lbl->show();
367 hbox->pack_start(*lbl, true, true, 0);
368 mi->add(*hbox);
369 mi->show_all();
370 _menu.append(*mi);
371 // g_free((void*)name);
372 cmsCloseProfile(hProfile);
373 }
374 }
375 g_free(full);
376 }
377 g_dir_close(directory);
378 }
379 }
380 }
381 _menu.show_all();
382 }
384 void
385 DocumentProperties::linkSelectedProfile()
386 {
387 //store this profile in the SVG document (create <color-profile> element in the XML)
388 // TODO remove use of 'active' desktop
389 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
390 if (!desktop){
391 g_warning("No active desktop");
392 } else {
393 if (!_menu.get_active()){
394 g_warning("No color profile available.");
395 return;
396 }
397 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
398 Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
399 cprofRepr->setAttribute("name", (gchar*) _menu.get_active()->get_data("name"));
400 cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
402 // Checks whether there is a defs element. Creates it when needed
403 Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
404 if (!defsRepr){
405 defsRepr = xml_doc->createElement("svg:defs");
406 xml_doc->root()->addChild(defsRepr, NULL);
407 }
409 g_assert(SP_ROOT(desktop->doc()->root)->defs);
410 defsRepr->addChild(cprofRepr, NULL);
412 // TODO check if this next line was sometimes needed. It being there caused an assertion.
413 //Inkscape::GC::release(defsRepr);
415 // inform the document, so we can undo
416 sp_document_done(desktop->doc(), SP_VERB_EDIT_LINK_COLOR_PROFILE, _("Link Color Profile"));
418 populate_linked_profiles_box();
419 }
420 }
422 void
423 DocumentProperties::populate_linked_profiles_box()
424 {
425 _LinkedProfilesListStore->clear();
426 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
427 if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
428 while ( current ) {
429 SPObject* obj = SP_OBJECT(current->data);
430 Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
431 Gtk::TreeModel::Row row = *(_LinkedProfilesListStore->append());
432 row[_LinkedProfilesListColumns.nameColumn] = prof->name;
433 // row[_LinkedProfilesListColumns.previewColumn] = "Color Preview";
434 current = g_slist_next(current);
435 }
436 }
438 void DocumentProperties::external_scripts_list_button_release(GdkEventButton* event)
439 {
440 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
441 _ExternalScriptsContextMenu.popup(event->button, event->time);
442 }
443 }
445 void DocumentProperties::linked_profiles_list_button_release(GdkEventButton* event)
446 {
447 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
448 _EmbProfContextMenu.popup(event->button, event->time);
449 }
450 }
452 void DocumentProperties::cms_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
453 {
454 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
455 _EmbProfContextMenu.append(*mi);
456 mi->signal_activate().connect(rem);
457 mi->show();
458 _EmbProfContextMenu.accelerate(parent);
459 }
462 void DocumentProperties::scripting_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
463 {
464 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
465 _ExternalScriptsContextMenu.append(*mi);
466 mi->signal_activate().connect(rem);
467 mi->show();
468 _ExternalScriptsContextMenu.accelerate(parent);
469 }
471 void DocumentProperties::removeSelectedProfile(){
472 Glib::ustring name;
473 if(_LinkedProfilesList.get_selection()) {
474 Gtk::TreeModel::iterator i = _LinkedProfilesList.get_selection()->get_selected();
476 if(i){
477 name = (*i)[_LinkedProfilesListColumns.nameColumn];
478 } else {
479 return;
480 }
481 }
483 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
484 while ( current ) {
485 SPObject* obj = SP_OBJECT(current->data);
486 Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
487 if (!name.compare(prof->name)){
488 sp_repr_unparent(obj->repr);
489 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove linked color profile"));
490 }
491 current = g_slist_next(current);
492 }
494 populate_linked_profiles_box();
495 }
497 void
498 DocumentProperties::build_cms()
499 {
500 _page_cms.show();
502 Gtk::Label *label_link= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
503 label_link->set_markup (_("<b>Linked Color Profiles:</b>"));
504 Gtk::Label *label_avail = manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
505 label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
507 _link_btn.set_label(_("Link Profile"));
509 _page_cms.set_spacing(4);
510 gint row = 0;
512 label_link->set_alignment(0.0);
513 _page_cms.table().attach(*label_link, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
514 row++;
515 _page_cms.table().attach(_LinkedProfilesListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
516 row++;
518 Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
519 spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
520 _page_cms.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
521 row++;
523 label_avail->set_alignment(0.0);
524 _page_cms.table().attach(*label_avail, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
525 row++;
526 _page_cms.table().attach(_combo_avail, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
527 _page_cms.table().attach(_link_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
529 populate_available_profiles();
531 _combo_avail.set_menu(_menu);
532 _combo_avail.set_history(0);
533 _combo_avail.show_all();
535 //# Set up the Linked Profiles combo box
536 _LinkedProfilesListStore = Gtk::ListStore::create(_LinkedProfilesListColumns);
537 _LinkedProfilesList.set_model(_LinkedProfilesListStore);
538 _LinkedProfilesList.append_column(_("Profile Name"), _LinkedProfilesListColumns.nameColumn);
539 // _LinkedProfilesList.append_column(_("Color Preview"), _LinkedProfilesListColumns.previewColumn);
540 _LinkedProfilesList.set_headers_visible(false);
541 // TODO restore? _LinkedProfilesList.set_fixed_height_mode(true);
543 populate_linked_profiles_box();
545 _LinkedProfilesListScroller.add(_LinkedProfilesList);
546 _LinkedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
547 _LinkedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
548 _LinkedProfilesListScroller.set_size_request(-1, 90);
550 _link_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::linkSelectedProfile));
552 _LinkedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::linked_profiles_list_button_release));
553 cms_create_popup_menu(_LinkedProfilesList, sigc::mem_fun(*this, &DocumentProperties::removeSelectedProfile));
555 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "defs" );
556 if (current) {
557 _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
558 }
559 _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_linked_profiles_box));
560 }
561 #endif // ENABLE_LCMS
563 void
564 DocumentProperties::build_scripting()
565 {
566 _page_scripting.show();
568 Gtk::Label *label_script= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
569 label_script->set_markup (_("<b>External script files:</b>"));
571 _add_btn.set_label(_("Add"));
573 _page_scripting.set_spacing(4);
574 gint row = 0;
576 label_script->set_alignment(0.0);
577 _page_scripting.table().attach(*label_script, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
578 row++;
579 _page_scripting.table().attach(_ExternalScriptsListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
580 row++;
582 Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
583 spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
584 _page_scripting.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
585 row++;
587 _page_scripting.table().attach(_script_entry, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
588 _page_scripting.table().attach(_add_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
589 row++;
591 //# Set up the External Scripts box
592 _ExternalScriptsListStore = Gtk::ListStore::create(_ExternalScriptsListColumns);
593 _ExternalScriptsList.set_model(_ExternalScriptsListStore);
594 _ExternalScriptsList.append_column(_("Filename"), _ExternalScriptsListColumns.filenameColumn);
595 _ExternalScriptsList.set_headers_visible(true);
596 // TODO restore? _ExternalScriptsList.set_fixed_height_mode(true);
598 populate_external_scripts_box();
600 _ExternalScriptsListScroller.add(_ExternalScriptsList);
601 _ExternalScriptsListScroller.set_shadow_type(Gtk::SHADOW_IN);
602 _ExternalScriptsListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
603 _ExternalScriptsListScroller.set_size_request(-1, 90);
605 _add_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::addExternalScript));
607 #if ENABLE_LCMS
608 _ExternalScriptsList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::external_scripts_list_button_release));
609 scripting_create_popup_menu(_ExternalScriptsList, sigc::mem_fun(*this, &DocumentProperties::removeExternalScript));
610 #endif // ENABLE_LCMS
612 //TODO: review this observers code:
613 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
614 if (current) {
615 _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
616 }
617 _ext_scripts_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_external_scripts_box));
618 }
621 void DocumentProperties::addExternalScript(){
622 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
623 if (!desktop){
624 g_warning("No active desktop");
625 } else {
626 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
627 Inkscape::XML::Node *scriptRepr = xml_doc->createElement("svg:script");
628 scriptRepr->setAttribute("xlink:href", (gchar*) _script_entry.get_text().c_str());
629 _script_entry.set_text("");
631 xml_doc->root()->addChild(scriptRepr, NULL);
633 // inform the document, so we can undo
634 sp_document_done(desktop->doc(), SP_VERB_EDIT_ADD_EXTERNAL_SCRIPT, _("Add external script..."));
636 populate_external_scripts_box();
637 }
638 }
640 void DocumentProperties::removeExternalScript(){
641 Glib::ustring name;
642 if(_ExternalScriptsList.get_selection()) {
643 Gtk::TreeModel::iterator i = _ExternalScriptsList.get_selection()->get_selected();
645 if(i){
646 name = (*i)[_ExternalScriptsListColumns.filenameColumn];
647 } else {
648 return;
649 }
650 }
652 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
653 while ( current ) {
654 SPObject* obj = SP_OBJECT(current->data);
655 SPScript* script = (SPScript*) obj;
656 if (name == script->xlinkhref){
657 sp_repr_unparent(obj->repr);
658 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_EXTERNAL_SCRIPT, _("Remove external script"));
659 }
660 current = g_slist_next(current);
661 }
663 populate_external_scripts_box();
665 }
667 void DocumentProperties::populate_external_scripts_box(){
668 _ExternalScriptsListStore->clear();
669 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
670 if (current) _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
671 while ( current ) {
672 SPObject* obj = SP_OBJECT(current->data);
673 SPScript* script = (SPScript*) obj;
674 if (script->xlinkhref)
675 {
676 Gtk::TreeModel::Row row = *(_ExternalScriptsListStore->append());
677 row[_ExternalScriptsListColumns.filenameColumn] = script->xlinkhref;
678 }
680 current = g_slist_next(current);
681 }
682 }
684 /**
685 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
686 */
687 void
688 DocumentProperties::update_gridspage()
689 {
690 SPDesktop *dt = getDesktop();
691 SPNamedView *nv = sp_desktop_namedview(dt);
693 //remove all tabs
694 while (_grids_notebook.get_n_pages() != 0) {
695 _grids_notebook.remove_page(-1); // this also deletes the page.
696 }
698 //add tabs
699 bool grids_present = false;
700 for (GSList const * l = nv->grids; l != NULL; l = l->next) {
701 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
702 if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
703 Glib::ustring name(grid->repr->attribute("id"));
704 const char *icon = NULL;
705 switch (grid->getGridType()) {
706 case GRID_RECTANGULAR:
707 icon = "grid-rectangular";
708 break;
709 case GRID_AXONOMETRIC:
710 icon = "grid-axonometric";
711 break;
712 default:
713 break;
714 }
715 _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
716 grids_present = true;
717 }
718 _grids_notebook.show_all();
720 if (grids_present)
721 _grids_button_remove.set_sensitive(true);
722 else
723 _grids_button_remove.set_sensitive(false);
724 }
726 /**
727 * Build grid page of dialog.
728 */
729 void
730 DocumentProperties::build_gridspage()
731 {
732 /// \todo FIXME: gray out snapping when grid is off.
733 /// Dissenting view: you want snapping without grid.
735 SPDesktop *dt = getDesktop();
736 SPNamedView *nv = sp_desktop_namedview(dt);
737 (void)nv;
739 _grids_label_crea.set_markup(_("<b>Creation</b>"));
740 _grids_label_def.set_markup(_("<b>Defined grids</b>"));
741 _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
742 _grids_hbox_crea.pack_start(_grids_button_new, true, true);
744 for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
745 _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
746 }
747 _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
749 _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
751 _grids_vbox.set_spacing(4);
752 _grids_vbox.pack_start(_grids_label_crea, false, false);
753 _grids_vbox.pack_start(_grids_hbox_crea, false, false);
754 _grids_vbox.pack_start(_grids_space, false, false);
755 _grids_vbox.pack_start(_grids_label_def, false, false);
756 _grids_vbox.pack_start(_grids_notebook, false, false);
757 _grids_vbox.pack_start(_grids_button_remove, false, false);
759 update_gridspage();
760 }
764 /**
765 * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
766 */
767 void
768 DocumentProperties::update()
769 {
770 if (_wr.isUpdating()) return;
772 SPDesktop *dt = getDesktop();
773 SPNamedView *nv = sp_desktop_namedview(dt);
775 _wr.setUpdating (true);
776 set_sensitive (true);
778 //-----------------------------------------------------------page page
779 _rcp_bg.setRgba32 (nv->pagecolor);
780 _rcb_canb.setActive (nv->showborder);
781 _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
782 _rcp_bord.setRgba32 (nv->bordercolor);
783 _rcb_shad.setActive (nv->showpageshadow);
785 if (nv->doc_units)
786 _rum_deflt.setUnit (nv->doc_units);
788 double const doc_w_px = sp_document_width(sp_desktop_document(dt));
789 double const doc_h_px = sp_document_height(sp_desktop_document(dt));
790 _page_sizer.setDim (doc_w_px, doc_h_px);
792 //-----------------------------------------------------------guide page
794 _rcb_sgui.setActive (nv->showguides);
795 _rcp_gui.setRgba32 (nv->guidecolor);
796 _rcp_hgui.setRgba32 (nv->guidehicolor);
797 _rcbsng.setActive(nv->snap_manager.snapprefs.getSnapModeGuide());
799 //-----------------------------------------------------------snap page
801 _rsu_sno.setValue (nv->snap_manager.snapprefs.getObjectTolerance());
802 _rsu_sn.setValue (nv->snap_manager.snapprefs.getGridTolerance());
803 _rsu_gusn.setValue (nv->snap_manager.snapprefs.getGuideTolerance());
806 //-----------------------------------------------------------grids page
808 update_gridspage();
810 //------------------------------------------------Color Management page
812 #if ENABLE_LCMS
813 populate_linked_profiles_box();
814 populate_available_profiles();
815 #endif // ENABLE_LCMS
817 _wr.setUpdating (false);
818 }
820 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
821 Gtk::HBox&
822 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
823 {
824 Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
825 _tab_label_box->set_spacing(4);
826 _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
827 label_image)));
829 Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
830 _tab_label_box->pack_start(*_tab_label);
831 _tab_label_box->show_all();
833 return *_tab_label_box;
834 }
836 //--------------------------------------------------------------------
838 void
839 DocumentProperties::on_response (int id)
840 {
841 if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
842 {
843 _rcp_bg.closeWindow();
844 _rcp_bord.closeWindow();
845 _rcp_gui.closeWindow();
846 _rcp_hgui.closeWindow();
847 }
849 if (id == Gtk::RESPONSE_CLOSE)
850 hide();
851 }
853 void
854 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
855 {
856 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
857 repr->addListener(&_repr_events, this);
858 Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
859 root->addListener(&_repr_events, this);
860 update();
861 }
863 void
864 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
865 {
866 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
867 repr->addListener(&_repr_events, this);
868 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
869 root->addListener(&_repr_events, this);
870 update();
871 }
873 void
874 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
875 {
876 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
877 repr->removeListenerByData(this);
878 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
879 root->removeListenerByData(this);
880 }
882 static void
883 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
884 {
885 if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
886 dialog->update_gridspage();
887 }
889 static void
890 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
891 {
892 if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
893 dialog->update_gridspage();
894 }
898 /**
899 * Called when XML node attribute changed; updates dialog widgets.
900 */
901 static void
902 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
903 {
904 if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
905 dialog->update();
906 }
909 /*########################################################################
910 # BUTTON CLICK HANDLERS (callbacks)
911 ########################################################################*/
913 void
914 DocumentProperties::onNewGrid()
915 {
916 SPDesktop *dt = getDesktop();
917 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
918 SPDocument *doc = sp_desktop_document(dt);
920 Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
921 CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
923 // toggle grid showing to ON:
924 dt->showGrids(true);
925 }
928 void
929 DocumentProperties::onRemoveGrid()
930 {
931 gint pagenum = _grids_notebook.get_current_page();
932 if (pagenum == -1) // no pages
933 return;
935 SPDesktop *dt = getDesktop();
936 SPNamedView *nv = sp_desktop_namedview(dt);
937 Inkscape::CanvasGrid * found_grid = NULL;
938 int i = 0;
939 for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) { // not a very nice fix, but works.
940 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
941 if (pagenum == i) {
942 found_grid = grid;
943 break; // break out of for-loop
944 }
945 }
946 if (found_grid) {
947 // delete the grid that corresponds with the selected tab
948 // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
949 found_grid->repr->parent()->removeChild(found_grid->repr);
950 sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
951 }
952 }
955 } // namespace Dialog
956 } // namespace UI
957 } // namespace Inkscape
959 /*
960 Local Variables:
961 mode:c++
962 c-file-style:"stroustrup"
963 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
964 indent-tabs-mode:nil
965 fill-column:99
966 End:
967 */
968 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :