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; only a small part of the guide near the cursor will snap)"),
104 "inkscape:snap-from-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 //---------------------------------------------------------------
114 {
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));
141 }
143 void
144 DocumentProperties::init()
145 {
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();
155 }
157 DocumentProperties::~DocumentProperties()
158 {
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);
163 }
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)
176 {
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 }
214 }
216 void
217 DocumentProperties::build_page()
218 {
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));
246 }
248 void
249 DocumentProperties::build_guides()
250 {
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));
266 }
268 void
269 DocumentProperties::build_snap()
270 {
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)
315 {
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 }
330 }
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();
383 }
385 void
386 DocumentProperties::linkSelectedProfile()
387 {
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 if (!_menu.get_active()){
395 g_warning("No color profile available.");
396 return;
397 }
398 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
399 Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
400 cprofRepr->setAttribute("name", (gchar*) _menu.get_active()->get_data("name"));
401 cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
403 // Checks whether there is a defs element. Creates it when needed
404 Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
405 if (!defsRepr){
406 defsRepr = xml_doc->createElement("svg:defs");
407 xml_doc->root()->addChild(defsRepr, NULL);
408 }
410 g_assert(SP_ROOT(desktop->doc()->root)->defs);
411 defsRepr->addChild(cprofRepr, NULL);
413 // TODO check if this next line was sometimes needed. It being there caused an assertion.
414 //Inkscape::GC::release(defsRepr);
416 // inform the document, so we can undo
417 sp_document_done(desktop->doc(), SP_VERB_EDIT_LINK_COLOR_PROFILE, _("Link Color Profile"));
419 populate_linked_profiles_box();
420 }
421 }
423 void
424 DocumentProperties::populate_linked_profiles_box()
425 {
426 _LinkedProfilesListStore->clear();
427 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
428 if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
429 while ( current ) {
430 SPObject* obj = SP_OBJECT(current->data);
431 Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
432 Gtk::TreeModel::Row row = *(_LinkedProfilesListStore->append());
433 row[_LinkedProfilesListColumns.nameColumn] = prof->name;
434 // row[_LinkedProfilesListColumns.previewColumn] = "Color Preview";
435 current = g_slist_next(current);
436 }
437 }
439 void DocumentProperties::external_scripts_list_button_release(GdkEventButton* event)
440 {
441 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
442 _ExternalScriptsContextMenu.popup(event->button, event->time);
443 }
444 }
446 void DocumentProperties::linked_profiles_list_button_release(GdkEventButton* event)
447 {
448 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
449 _EmbProfContextMenu.popup(event->button, event->time);
450 }
451 }
453 void DocumentProperties::cms_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
454 {
455 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
456 _EmbProfContextMenu.append(*mi);
457 mi->signal_activate().connect(rem);
458 mi->show();
459 _EmbProfContextMenu.accelerate(parent);
460 }
463 void DocumentProperties::scripting_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
464 {
465 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
466 _ExternalScriptsContextMenu.append(*mi);
467 mi->signal_activate().connect(rem);
468 mi->show();
469 _ExternalScriptsContextMenu.accelerate(parent);
470 }
472 void DocumentProperties::removeSelectedProfile(){
473 Glib::ustring name;
474 if(_LinkedProfilesList.get_selection()) {
475 Gtk::TreeModel::iterator i = _LinkedProfilesList.get_selection()->get_selected();
477 if(i){
478 name = (*i)[_LinkedProfilesListColumns.nameColumn];
479 } else {
480 return;
481 }
482 }
484 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
485 while ( current ) {
486 SPObject* obj = SP_OBJECT(current->data);
487 Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
488 if (!name.compare(prof->name)){
489 sp_repr_unparent(obj->repr);
490 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove linked color profile"));
491 }
492 current = g_slist_next(current);
493 }
495 populate_linked_profiles_box();
496 }
498 void
499 DocumentProperties::build_cms()
500 {
501 _page_cms.show();
503 Gtk::Label *label_link= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
504 label_link->set_markup (_("<b>Linked Color Profiles:</b>"));
505 Gtk::Label *label_avail = manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
506 label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
508 _link_btn.set_label(_("Link Profile"));
510 _page_cms.set_spacing(4);
511 gint row = 0;
513 label_link->set_alignment(0.0);
514 _page_cms.table().attach(*label_link, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
515 row++;
516 _page_cms.table().attach(_LinkedProfilesListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
517 row++;
519 Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
520 spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
521 _page_cms.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
522 row++;
524 label_avail->set_alignment(0.0);
525 _page_cms.table().attach(*label_avail, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
526 row++;
527 _page_cms.table().attach(_combo_avail, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
528 _page_cms.table().attach(_link_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
530 populate_available_profiles();
532 _combo_avail.set_menu(_menu);
533 _combo_avail.set_history(0);
534 _combo_avail.show_all();
536 //# Set up the Linked Profiles combo box
537 _LinkedProfilesListStore = Gtk::ListStore::create(_LinkedProfilesListColumns);
538 _LinkedProfilesList.set_model(_LinkedProfilesListStore);
539 _LinkedProfilesList.append_column(_("Profile Name"), _LinkedProfilesListColumns.nameColumn);
540 // _LinkedProfilesList.append_column(_("Color Preview"), _LinkedProfilesListColumns.previewColumn);
541 _LinkedProfilesList.set_headers_visible(false);
542 // TODO restore? _LinkedProfilesList.set_fixed_height_mode(true);
544 populate_linked_profiles_box();
546 _LinkedProfilesListScroller.add(_LinkedProfilesList);
547 _LinkedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
548 _LinkedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
549 _LinkedProfilesListScroller.set_size_request(-1, 90);
551 _link_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::linkSelectedProfile));
553 _LinkedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::linked_profiles_list_button_release));
554 cms_create_popup_menu(_LinkedProfilesList, sigc::mem_fun(*this, &DocumentProperties::removeSelectedProfile));
556 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "defs" );
557 if (current) {
558 _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
559 }
560 _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_linked_profiles_box));
561 }
562 #endif // ENABLE_LCMS
564 void
565 DocumentProperties::build_scripting()
566 {
567 _page_scripting.show();
569 Gtk::Label *label_script= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
570 label_script->set_markup (_("<b>External script files:</b>"));
572 _add_btn.set_label(_("Add"));
574 _page_scripting.set_spacing(4);
575 gint row = 0;
577 label_script->set_alignment(0.0);
578 _page_scripting.table().attach(*label_script, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
579 row++;
580 _page_scripting.table().attach(_ExternalScriptsListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
581 row++;
583 Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
584 spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
585 _page_scripting.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
586 row++;
588 _page_scripting.table().attach(_script_entry, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
589 _page_scripting.table().attach(_add_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
590 row++;
592 //# Set up the External Scripts box
593 _ExternalScriptsListStore = Gtk::ListStore::create(_ExternalScriptsListColumns);
594 _ExternalScriptsList.set_model(_ExternalScriptsListStore);
595 _ExternalScriptsList.append_column(_("Filename"), _ExternalScriptsListColumns.filenameColumn);
596 _ExternalScriptsList.set_headers_visible(true);
597 // TODO restore? _ExternalScriptsList.set_fixed_height_mode(true);
599 populate_external_scripts_box();
601 _ExternalScriptsListScroller.add(_ExternalScriptsList);
602 _ExternalScriptsListScroller.set_shadow_type(Gtk::SHADOW_IN);
603 _ExternalScriptsListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
604 _ExternalScriptsListScroller.set_size_request(-1, 90);
606 _add_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::addExternalScript));
608 #if ENABLE_LCMS
609 _ExternalScriptsList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::external_scripts_list_button_release));
610 scripting_create_popup_menu(_ExternalScriptsList, sigc::mem_fun(*this, &DocumentProperties::removeExternalScript));
611 #endif // ENABLE_LCMS
613 //TODO: review this observers code:
614 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
615 if (current) {
616 _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
617 }
618 _ext_scripts_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_external_scripts_box));
619 }
622 void DocumentProperties::addExternalScript(){
623 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
624 if (!desktop){
625 g_warning("No active desktop");
626 } else {
627 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
628 Inkscape::XML::Node *scriptRepr = xml_doc->createElement("svg:script");
629 scriptRepr->setAttribute("xlink:href", (gchar*) _script_entry.get_text().c_str());
630 _script_entry.set_text("");
632 xml_doc->root()->addChild(scriptRepr, NULL);
634 // inform the document, so we can undo
635 sp_document_done(desktop->doc(), SP_VERB_EDIT_ADD_EXTERNAL_SCRIPT, _("Add external script..."));
637 populate_external_scripts_box();
638 }
639 }
641 void DocumentProperties::removeExternalScript(){
642 Glib::ustring name;
643 if(_ExternalScriptsList.get_selection()) {
644 Gtk::TreeModel::iterator i = _ExternalScriptsList.get_selection()->get_selected();
646 if(i){
647 name = (*i)[_ExternalScriptsListColumns.filenameColumn];
648 } else {
649 return;
650 }
651 }
653 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
654 while ( current ) {
655 SPObject* obj = SP_OBJECT(current->data);
656 SPScript* script = (SPScript*) obj;
657 if (!name.compare(script->xlinkhref)){
658 sp_repr_unparent(obj->repr);
659 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_EXTERNAL_SCRIPT, _("Remove external script"));
660 }
661 current = g_slist_next(current);
662 }
664 populate_external_scripts_box();
666 }
668 void DocumentProperties::populate_external_scripts_box(){
669 _ExternalScriptsListStore->clear();
670 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
671 if (current) _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
672 while ( current ) {
673 SPObject* obj = SP_OBJECT(current->data);
674 SPScript* script = (SPScript*) obj;
675 Gtk::TreeModel::Row row = *(_ExternalScriptsListStore->append());
676 row[_ExternalScriptsListColumns.filenameColumn] = script->xlinkhref;
677 current = g_slist_next(current);
678 }
679 }
681 /**
682 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
683 */
684 void
685 DocumentProperties::update_gridspage()
686 {
687 SPDesktop *dt = getDesktop();
688 SPNamedView *nv = sp_desktop_namedview(dt);
690 //remove all tabs
691 while (_grids_notebook.get_n_pages() != 0) {
692 _grids_notebook.remove_page(-1); // this also deletes the page.
693 }
695 //add tabs
696 bool grids_present = false;
697 for (GSList const * l = nv->grids; l != NULL; l = l->next) {
698 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
699 if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
700 Glib::ustring name(grid->repr->attribute("id"));
701 const char *icon = NULL;
702 switch (grid->getGridType()) {
703 case GRID_RECTANGULAR:
704 icon = INKSCAPE_ICON_GRID_XY;
705 break;
706 case GRID_AXONOMETRIC:
707 icon = INKSCAPE_ICON_GRID_AXONOM;
708 break;
709 default:
710 break;
711 }
712 _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
713 grids_present = true;
714 }
715 _grids_notebook.show_all();
717 if (grids_present)
718 _grids_button_remove.set_sensitive(true);
719 else
720 _grids_button_remove.set_sensitive(false);
721 }
723 /**
724 * Build grid page of dialog.
725 */
726 void
727 DocumentProperties::build_gridspage()
728 {
729 /// \todo FIXME: gray out snapping when grid is off.
730 /// Dissenting view: you want snapping without grid.
732 SPDesktop *dt = getDesktop();
733 SPNamedView *nv = sp_desktop_namedview(dt);
734 (void)nv;
736 _grids_label_crea.set_markup(_("<b>Creation</b>"));
737 _grids_label_def.set_markup(_("<b>Defined grids</b>"));
738 _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
739 _grids_hbox_crea.pack_start(_grids_button_new, true, true);
741 for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
742 _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
743 }
744 _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
746 _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
748 _grids_vbox.set_spacing(4);
749 _grids_vbox.pack_start(_grids_label_crea, false, false);
750 _grids_vbox.pack_start(_grids_hbox_crea, false, false);
751 _grids_vbox.pack_start(_grids_space, false, false);
752 _grids_vbox.pack_start(_grids_label_def, false, false);
753 _grids_vbox.pack_start(_grids_notebook, false, false);
754 _grids_vbox.pack_start(_grids_button_remove, false, false);
756 update_gridspage();
757 }
761 /**
762 * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
763 */
764 void
765 DocumentProperties::update()
766 {
767 if (_wr.isUpdating()) return;
769 SPDesktop *dt = getDesktop();
770 SPNamedView *nv = sp_desktop_namedview(dt);
772 _wr.setUpdating (true);
773 set_sensitive (true);
775 //-----------------------------------------------------------page page
776 _rcp_bg.setRgba32 (nv->pagecolor);
777 _rcb_canb.setActive (nv->showborder);
778 _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
779 _rcp_bord.setRgba32 (nv->bordercolor);
780 _rcb_shad.setActive (nv->showpageshadow);
782 if (nv->doc_units)
783 _rum_deflt.setUnit (nv->doc_units);
785 double const doc_w_px = sp_document_width(sp_desktop_document(dt));
786 double const doc_h_px = sp_document_height(sp_desktop_document(dt));
787 _page_sizer.setDim (doc_w_px, doc_h_px);
789 //-----------------------------------------------------------guide page
791 _rcb_sgui.setActive (nv->showguides);
792 _rcp_gui.setRgba32 (nv->guidecolor);
793 _rcp_hgui.setRgba32 (nv->guidehicolor);
794 _rcbsng.setActive(nv->snap_manager.snapprefs.getSnapModeGuide());
796 //-----------------------------------------------------------snap page
798 _rsu_sno.setValue (nv->snap_manager.snapprefs.getObjectTolerance());
799 _rsu_sn.setValue (nv->snap_manager.snapprefs.getGridTolerance());
800 _rsu_gusn.setValue (nv->snap_manager.snapprefs.getGuideTolerance());
803 //-----------------------------------------------------------grids page
805 update_gridspage();
807 //------------------------------------------------Color Management page
809 #if ENABLE_LCMS
810 populate_linked_profiles_box();
811 populate_available_profiles();
812 #endif // ENABLE_LCMS
814 _wr.setUpdating (false);
815 }
817 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
818 Gtk::HBox&
819 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
820 {
821 Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
822 _tab_label_box->set_spacing(4);
823 _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
824 label_image)));
826 Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
827 _tab_label_box->pack_start(*_tab_label);
828 _tab_label_box->show_all();
830 return *_tab_label_box;
831 }
833 //--------------------------------------------------------------------
835 void
836 DocumentProperties::on_response (int id)
837 {
838 if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
839 {
840 _rcp_bg.closeWindow();
841 _rcp_bord.closeWindow();
842 _rcp_gui.closeWindow();
843 _rcp_hgui.closeWindow();
844 }
846 if (id == Gtk::RESPONSE_CLOSE)
847 hide();
848 }
850 void
851 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
852 {
853 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
854 repr->addListener(&_repr_events, this);
855 Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
856 root->addListener(&_repr_events, this);
857 update();
858 }
860 void
861 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
862 {
863 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
864 repr->addListener(&_repr_events, this);
865 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
866 root->addListener(&_repr_events, this);
867 update();
868 }
870 void
871 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
872 {
873 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
874 repr->removeListenerByData(this);
875 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
876 root->removeListenerByData(this);
877 }
879 static void
880 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
881 {
882 if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
883 dialog->update_gridspage();
884 }
886 static void
887 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
888 {
889 if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
890 dialog->update_gridspage();
891 }
895 /**
896 * Called when XML node attribute changed; updates dialog widgets.
897 */
898 static void
899 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
900 {
901 if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
902 dialog->update();
903 }
906 /*########################################################################
907 # BUTTON CLICK HANDLERS (callbacks)
908 ########################################################################*/
910 void
911 DocumentProperties::onNewGrid()
912 {
913 SPDesktop *dt = getDesktop();
914 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
915 SPDocument *doc = sp_desktop_document(dt);
917 Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
918 CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
920 // toggle grid showing to ON:
921 dt->showGrids(true);
922 }
925 void
926 DocumentProperties::onRemoveGrid()
927 {
928 gint pagenum = _grids_notebook.get_current_page();
929 if (pagenum == -1) // no pages
930 return;
932 SPDesktop *dt = getDesktop();
933 SPNamedView *nv = sp_desktop_namedview(dt);
934 Inkscape::CanvasGrid * found_grid = NULL;
935 int i = 0;
936 for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) { // not a very nice fix, but works.
937 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
938 if (pagenum == i) {
939 found_grid = grid;
940 break; // break out of for-loop
941 }
942 }
943 if (found_grid) {
944 // delete the grid that corresponds with the selected tab
945 // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
946 found_grid->repr->parent()->removeChild(found_grid->repr);
947 sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
948 }
949 }
952 } // namespace Dialog
953 } // namespace UI
954 } // namespace Inkscape
956 /*
957 Local Variables:
958 mode:c++
959 c-file-style:"stroustrup"
960 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
961 indent-tabs-mode:nil
962 fill-column:99
963 End:
964 */
965 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :