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