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 _grids_button_new(C_("Grid", "_New"), _("Create new grid.")),
107 _grids_button_remove(C_("Grid", "_Remove"), _("Remove selected grid.")),
108 _grids_label_def("", Gtk::ALIGN_LEFT)
109 //---------------------------------------------------------------
110 {
111 _tt.enable();
112 _getContents()->set_spacing (4);
113 _getContents()->pack_start(_notebook, true, true);
115 _notebook.append_page(_page_page, _("Page"));
116 _notebook.append_page(_page_guides, _("Guides"));
117 _notebook.append_page(_grids_vbox, _("Grids"));
118 _notebook.append_page(_page_snap, _("Snap"));
119 _notebook.append_page(_page_cms, _("Color Management"));
120 _notebook.append_page(_page_scripting, _("Scripting"));
122 build_page();
123 build_guides();
124 build_gridspage();
125 build_snap();
126 #if ENABLE_LCMS
127 build_cms();
128 #endif // ENABLE_LCMS
129 build_scripting();
131 _grids_button_new.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onNewGrid));
132 _grids_button_remove.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::onRemoveGrid));
134 signalDocumentReplaced().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDocumentReplaced));
135 signalActivateDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleActivateDesktop));
136 signalDeactiveDesktop().connect(sigc::mem_fun(*this, &DocumentProperties::_handleDeactivateDesktop));
137 }
139 void
140 DocumentProperties::init()
141 {
142 update();
144 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
145 repr->addListener (&_repr_events, this);
146 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
147 root->addListener (&_repr_events, this);
149 show_all_children();
150 _grids_button_remove.hide();
151 }
153 DocumentProperties::~DocumentProperties()
154 {
155 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(getDesktop()));
156 repr->removeListenerByData (this);
157 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(getDesktop())->root);
158 root->removeListenerByData (this);
159 }
161 //========================================================================
163 /**
164 * Helper function that attaches widgets in a 3xn table. The widgets come in an
165 * array that has two entries per table row. The two entries code for four
166 * possible cases: (0,0) means insert space in first column; (0, non-0) means
167 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
168 * (non-0, non-0) means two widgets in columns 2 and 3.
169 **/
170 inline void
171 attach_all(Gtk::Table &table, Gtk::Widget *const arr[], unsigned const n, int start = 0)
172 {
173 for (unsigned i = 0, r = start; i < n; i += 2)
174 {
175 if (arr[i] && arr[i+1])
176 {
177 table.attach(*arr[i], 1, 2, r, r+1,
178 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
179 table.attach(*arr[i+1], 2, 3, r, r+1,
180 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
181 }
182 else
183 {
184 if (arr[i+1]) {
185 Gtk::AttachOptions yoptions = (Gtk::AttachOptions)0;
186 if (dynamic_cast<Inkscape::UI::Widget::PageSizer*>(arr[i+1])) {
187 // only the PageSizer in Document Properties|Page should be stretched vertically
188 yoptions = Gtk::FILL|Gtk::EXPAND;
189 }
190 table.attach(*arr[i+1], 1, 3, r, r+1,
191 Gtk::FILL|Gtk::EXPAND, yoptions, 0,0);
192 }
193 else if (arr[i])
194 {
195 Gtk::Label& label = reinterpret_cast<Gtk::Label&>(*arr[i]);
196 label.set_alignment (0.0);
197 table.attach (label, 0, 3, r, r+1,
198 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
199 }
200 else
201 {
202 Gtk::HBox *space = manage (new Gtk::HBox);
203 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
204 table.attach (*space, 0, 1, r, r+1,
205 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
206 }
207 }
208 ++r;
209 }
210 }
212 void
213 DocumentProperties::build_page()
214 {
215 _page_page.show();
217 Gtk::Label* label_gen = manage (new Gtk::Label);
218 label_gen->set_markup (_("<b>General</b>"));
219 Gtk::Label* label_bor = manage (new Gtk::Label);
220 label_bor->set_markup (_("<b>Border</b>"));
221 Gtk::Label *label_for = manage (new Gtk::Label);
222 label_for->set_markup (_("<b>Page Size</b>"));
223 _page_sizer.init();
225 Gtk::Widget *const widget_array[] =
226 {
227 label_gen, 0,
228 0, &_rum_deflt,
229 _rcp_bg._label, &_rcp_bg,
230 0, 0,
231 label_for, 0,
232 0, &_page_sizer,
233 0, 0,
234 label_bor, 0,
235 0, &_rcb_canb,
236 0, &_rcb_bord,
237 0, &_rcb_shad,
238 _rcp_bord._label, &_rcp_bord,
239 };
241 attach_all(_page_page.table(), widget_array, G_N_ELEMENTS(widget_array));
242 }
244 void
245 DocumentProperties::build_guides()
246 {
247 _page_guides.show();
249 Gtk::Label *label_gui = manage (new Gtk::Label);
250 label_gui->set_markup (_("<b>Guides</b>"));
252 Gtk::Widget *const widget_array[] =
253 {
254 label_gui, 0,
255 0, &_rcb_sgui,
256 _rcp_gui._label, &_rcp_gui,
257 _rcp_hgui._label, &_rcp_hgui,
258 0, &_rcbsng,
259 };
261 attach_all(_page_guides.table(), widget_array, G_N_ELEMENTS(widget_array));
262 }
264 void
265 DocumentProperties::build_snap()
266 {
267 _page_snap.show();
269 _rsu_sno.init (_("Snap _distance"), _("Snap only when _closer than:"), _("Always snap"),
270 _("Snapping distance, in screen pixels, for snapping to objects"), _("Always snap to objects, regardless of their distance"),
271 _("If set, objects only snap to another object when it's within the range specified below"),
272 "objecttolerance", _wr);
274 //Options for snapping to grids
275 _rsu_sn.init (_("Snap d_istance"), _("Snap only when c_loser than:"), _("Always snap"),
276 _("Snapping distance, in screen pixels, for snapping to grid"), _("Always snap to grids, regardless of the distance"),
277 _("If set, objects only snap to a grid line when it's within the range specified below"),
278 "gridtolerance", _wr);
280 //Options for snapping to guides
281 _rsu_gusn.init (_("Snap dist_ance"), _("Snap only when close_r than:"), _("Always snap"),
282 _("Snapping distance, in screen pixels, for snapping to guides"), _("Always snap to guides, regardless of the distance"),
283 _("If set, objects only snap to a guide when it's within the range specified below"),
284 "guidetolerance", _wr);
286 Gtk::Label *label_o = manage (new Gtk::Label);
287 label_o->set_markup (_("<b>Snap to objects</b>"));
288 Gtk::Label *label_gr = manage (new Gtk::Label);
289 label_gr->set_markup (_("<b>Snap to grids</b>"));
290 Gtk::Label *label_gu = manage (new Gtk::Label);
291 label_gu->set_markup (_("<b>Snap to guides</b>"));
293 Gtk::Widget *const array[] =
294 {
295 label_o, 0,
296 0, _rsu_sno._vbox,
297 0, 0,
298 label_gr, 0,
299 0, _rsu_sn._vbox,
300 0, 0,
301 label_gu, 0,
302 0, _rsu_gusn._vbox
303 };
305 attach_all(_page_snap.table(), array, G_N_ELEMENTS(array));
306 }
308 #if ENABLE_LCMS
309 static void
310 lcms_profile_get_name (cmsHPROFILE profile, const gchar **name)
311 {
312 if (profile)
313 {
314 *name = cmsTakeProductDesc (profile);
316 if (! *name)
317 *name = cmsTakeProductName (profile);
319 if (*name && ! g_utf8_validate (*name, -1, NULL))
320 *name = _("(invalid UTF-8 string)");
321 }
322 else
323 {
324 *name = _("None");
325 }
326 }
328 void
329 DocumentProperties::populate_available_profiles(){
330 Glib::ListHandle<Gtk::Widget*> children = _menu.get_children();
331 for ( Glib::ListHandle<Gtk::Widget*>::iterator it2 = children.begin(); it2 != children.end(); ++it2 ) {
332 _menu.remove(**it2);
333 delete(*it2);
334 }
336 std::list<Glib::ustring> files = ColorProfile::getProfileFiles();
337 for ( std::list<Glib::ustring>::const_iterator it = files.begin(); it != files.end(); ++it ) {
338 cmsHPROFILE hProfile = cmsOpenProfileFromFile(it->c_str(), "r");
339 if ( hProfile ){
340 const gchar* name = 0;
341 lcms_profile_get_name(hProfile, &name);
342 Gtk::MenuItem* mi = manage(new Gtk::MenuItem());
343 mi->set_data("filepath", g_strdup(it->c_str()));
344 mi->set_data("name", g_strdup(name));
345 Gtk::HBox *hbox = manage(new Gtk::HBox());
346 hbox->show();
347 Gtk::Label* lbl = manage(new Gtk::Label(name));
348 lbl->show();
349 hbox->pack_start(*lbl, true, true, 0);
350 mi->add(*hbox);
351 mi->show_all();
352 _menu.append(*mi);
353 // g_free((void*)name);
354 cmsCloseProfile(hProfile);
355 }
356 }
358 _menu.show_all();
359 }
361 /**
362 * Cleans up name to remove disallowed characters.
363 * Some discussion at http://markmail.org/message/bhfvdfptt25kgtmj
364 * Allowed ASCII first characters: ':', 'A'-'Z', '_', 'a'-'z'
365 * Allowed ASCII remaining chars add: '-', '.', '0'-'9',
366 *
367 * @param str the string to clean up.
368 */
369 static void sanitizeName( Glib::ustring& str )
370 {
371 if (str.size() > 1) {
372 char val = str.at(0);
373 if (((val < 'A') || (val > 'Z'))
374 && ((val < 'a') || (val > 'z'))
375 && (val != '_')
376 && (val != ':')) {
377 str.replace(0, 1, "-");
378 }
379 for (Glib::ustring::size_type i = 1; i < str.size(); i++) {
380 char val = str.at(i);
381 if (((val < 'A') || (val > 'Z'))
382 && ((val < 'a') || (val > 'z'))
383 && ((val < '0') || (val > '9'))
384 && (val != '_')
385 && (val != ':')
386 && (val != '-')
387 && (val != '.')) {
388 str.replace(i, 1, "-");
389 }
390 }
391 }
392 }
394 void
395 DocumentProperties::linkSelectedProfile()
396 {
397 //store this profile in the SVG document (create <color-profile> element in the XML)
398 // TODO remove use of 'active' desktop
399 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
400 if (!desktop){
401 g_warning("No active desktop");
402 } else {
403 if (!_menu.get_active()){
404 g_warning("No color profile available.");
405 return;
406 }
407 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
408 Inkscape::XML::Node *cprofRepr = xml_doc->createElement("svg:color-profile");
409 gchar* tmp = static_cast<gchar*>(_menu.get_active()->get_data("name"));
410 Glib::ustring nameStr = tmp ? tmp : "profile"; // TODO add some auto-numbering to avoid collisions
411 sanitizeName(nameStr);
412 cprofRepr->setAttribute("name", nameStr.c_str());
413 cprofRepr->setAttribute("xlink:href", (gchar*) _menu.get_active()->get_data("filepath"));
415 // Checks whether there is a defs element. Creates it when needed
416 Inkscape::XML::Node *defsRepr = sp_repr_lookup_name(xml_doc, "svg:defs");
417 if (!defsRepr){
418 defsRepr = xml_doc->createElement("svg:defs");
419 xml_doc->root()->addChild(defsRepr, NULL);
420 }
422 g_assert(SP_ROOT(desktop->doc()->root)->defs);
423 defsRepr->addChild(cprofRepr, NULL);
425 // TODO check if this next line was sometimes needed. It being there caused an assertion.
426 //Inkscape::GC::release(defsRepr);
428 // inform the document, so we can undo
429 sp_document_done(desktop->doc(), SP_VERB_EDIT_LINK_COLOR_PROFILE, _("Link Color Profile"));
431 populate_linked_profiles_box();
432 }
433 }
435 void
436 DocumentProperties::populate_linked_profiles_box()
437 {
438 _LinkedProfilesListStore->clear();
439 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
440 if (current) _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
441 while ( current ) {
442 SPObject* obj = SP_OBJECT(current->data);
443 Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
444 Gtk::TreeModel::Row row = *(_LinkedProfilesListStore->append());
445 row[_LinkedProfilesListColumns.nameColumn] = prof->name;
446 // row[_LinkedProfilesListColumns.previewColumn] = "Color Preview";
447 current = g_slist_next(current);
448 }
449 }
451 void DocumentProperties::external_scripts_list_button_release(GdkEventButton* event)
452 {
453 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
454 _ExternalScriptsContextMenu.popup(event->button, event->time);
455 }
456 }
458 void DocumentProperties::linked_profiles_list_button_release(GdkEventButton* event)
459 {
460 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
461 _EmbProfContextMenu.popup(event->button, event->time);
462 }
463 }
465 void DocumentProperties::cms_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
466 {
467 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
468 _EmbProfContextMenu.append(*mi);
469 mi->signal_activate().connect(rem);
470 mi->show();
471 _EmbProfContextMenu.accelerate(parent);
472 }
475 void DocumentProperties::scripting_create_popup_menu(Gtk::Widget& parent, sigc::slot<void> rem)
476 {
477 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
478 _ExternalScriptsContextMenu.append(*mi);
479 mi->signal_activate().connect(rem);
480 mi->show();
481 _ExternalScriptsContextMenu.accelerate(parent);
482 }
484 void DocumentProperties::removeSelectedProfile(){
485 Glib::ustring name;
486 if(_LinkedProfilesList.get_selection()) {
487 Gtk::TreeModel::iterator i = _LinkedProfilesList.get_selection()->get_selected();
489 if(i){
490 name = (*i)[_LinkedProfilesListColumns.nameColumn];
491 } else {
492 return;
493 }
494 }
496 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "iccprofile" );
497 while ( current ) {
498 SPObject* obj = SP_OBJECT(current->data);
499 Inkscape::ColorProfile* prof = reinterpret_cast<Inkscape::ColorProfile*>(obj);
500 if (!name.compare(prof->name)){
501 sp_repr_unparent(obj->repr);
502 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_COLOR_PROFILE, _("Remove linked color profile"));
503 }
504 current = g_slist_next(current);
505 }
507 populate_linked_profiles_box();
508 }
510 void
511 DocumentProperties::build_cms()
512 {
513 _page_cms.show();
515 Gtk::Label *label_link= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
516 label_link->set_markup (_("<b>Linked Color Profiles:</b>"));
517 Gtk::Label *label_avail = manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
518 label_avail->set_markup (_("<b>Available Color Profiles:</b>"));
520 _link_btn.set_label(_("Link Profile"));
522 _page_cms.set_spacing(4);
523 gint row = 0;
525 label_link->set_alignment(0.0);
526 _page_cms.table().attach(*label_link, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
527 row++;
528 _page_cms.table().attach(_LinkedProfilesListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
529 row++;
531 Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
532 spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
533 _page_cms.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
534 row++;
536 label_avail->set_alignment(0.0);
537 _page_cms.table().attach(*label_avail, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
538 row++;
539 _page_cms.table().attach(_combo_avail, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
540 _page_cms.table().attach(_link_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
542 populate_available_profiles();
544 _combo_avail.set_menu(_menu);
545 _combo_avail.set_history(0);
546 _combo_avail.show_all();
548 //# Set up the Linked Profiles combo box
549 _LinkedProfilesListStore = Gtk::ListStore::create(_LinkedProfilesListColumns);
550 _LinkedProfilesList.set_model(_LinkedProfilesListStore);
551 _LinkedProfilesList.append_column(_("Profile Name"), _LinkedProfilesListColumns.nameColumn);
552 // _LinkedProfilesList.append_column(_("Color Preview"), _LinkedProfilesListColumns.previewColumn);
553 _LinkedProfilesList.set_headers_visible(false);
554 // TODO restore? _LinkedProfilesList.set_fixed_height_mode(true);
556 populate_linked_profiles_box();
558 _LinkedProfilesListScroller.add(_LinkedProfilesList);
559 _LinkedProfilesListScroller.set_shadow_type(Gtk::SHADOW_IN);
560 _LinkedProfilesListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
561 _LinkedProfilesListScroller.set_size_request(-1, 90);
563 _link_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::linkSelectedProfile));
565 _LinkedProfilesList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::linked_profiles_list_button_release));
566 cms_create_popup_menu(_LinkedProfilesList, sigc::mem_fun(*this, &DocumentProperties::removeSelectedProfile));
568 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "defs" );
569 if (current) {
570 _emb_profiles_observer.set(SP_OBJECT(current->data)->parent);
571 }
572 _emb_profiles_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_linked_profiles_box));
573 }
574 #endif // ENABLE_LCMS
576 void
577 DocumentProperties::build_scripting()
578 {
579 _page_scripting.show();
581 Gtk::Label *label_script= manage (new Gtk::Label("", Gtk::ALIGN_LEFT));
582 label_script->set_markup (_("<b>External script files:</b>"));
584 _add_btn.set_label(_("Add"));
586 _page_scripting.set_spacing(4);
587 gint row = 0;
589 label_script->set_alignment(0.0);
590 _page_scripting.table().attach(*label_script, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
591 row++;
592 _page_scripting.table().attach(_ExternalScriptsListScroller, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
593 row++;
595 Gtk::HBox* spacer = Gtk::manage(new Gtk::HBox());
596 spacer->set_size_request(SPACE_SIZE_X, SPACE_SIZE_Y);
597 _page_scripting.table().attach(*spacer, 0, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
598 row++;
600 _page_scripting.table().attach(_script_entry, 0, 2, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
601 _page_scripting.table().attach(_add_btn, 2, 3, row, row + 1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0, 0, 0);
602 row++;
604 //# Set up the External Scripts box
605 _ExternalScriptsListStore = Gtk::ListStore::create(_ExternalScriptsListColumns);
606 _ExternalScriptsList.set_model(_ExternalScriptsListStore);
607 _ExternalScriptsList.append_column(_("Filename"), _ExternalScriptsListColumns.filenameColumn);
608 _ExternalScriptsList.set_headers_visible(true);
609 // TODO restore? _ExternalScriptsList.set_fixed_height_mode(true);
611 populate_external_scripts_box();
613 _ExternalScriptsListScroller.add(_ExternalScriptsList);
614 _ExternalScriptsListScroller.set_shadow_type(Gtk::SHADOW_IN);
615 _ExternalScriptsListScroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
616 _ExternalScriptsListScroller.set_size_request(-1, 90);
618 _add_btn.signal_clicked().connect(sigc::mem_fun(*this, &DocumentProperties::addExternalScript));
620 #if ENABLE_LCMS
621 _ExternalScriptsList.signal_button_release_event().connect_notify(sigc::mem_fun(*this, &DocumentProperties::external_scripts_list_button_release));
622 scripting_create_popup_menu(_ExternalScriptsList, sigc::mem_fun(*this, &DocumentProperties::removeExternalScript));
623 #endif // ENABLE_LCMS
625 //TODO: review this observers code:
626 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
627 if (current) {
628 _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
629 }
630 _ext_scripts_observer.signal_changed().connect(sigc::mem_fun(*this, &DocumentProperties::populate_external_scripts_box));
631 }
634 void DocumentProperties::addExternalScript(){
635 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
636 if (!desktop){
637 g_warning("No active desktop");
638 } else {
639 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
640 Inkscape::XML::Node *scriptRepr = xml_doc->createElement("svg:script");
641 scriptRepr->setAttribute("xlink:href", (gchar*) _script_entry.get_text().c_str());
642 _script_entry.set_text("");
644 xml_doc->root()->addChild(scriptRepr, NULL);
646 // inform the document, so we can undo
647 sp_document_done(desktop->doc(), SP_VERB_EDIT_ADD_EXTERNAL_SCRIPT, _("Add external script..."));
649 populate_external_scripts_box();
650 }
651 }
653 void DocumentProperties::removeExternalScript(){
654 Glib::ustring name;
655 if(_ExternalScriptsList.get_selection()) {
656 Gtk::TreeModel::iterator i = _ExternalScriptsList.get_selection()->get_selected();
658 if(i){
659 name = (*i)[_ExternalScriptsListColumns.filenameColumn];
660 } else {
661 return;
662 }
663 }
665 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
666 while ( current ) {
667 SPObject* obj = SP_OBJECT(current->data);
668 SPScript* script = (SPScript*) obj;
669 if (name == script->xlinkhref){
670 sp_repr_unparent(obj->repr);
671 sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_REMOVE_EXTERNAL_SCRIPT, _("Remove external script"));
672 }
673 current = g_slist_next(current);
674 }
676 populate_external_scripts_box();
678 }
680 void DocumentProperties::populate_external_scripts_box(){
681 _ExternalScriptsListStore->clear();
682 const GSList *current = sp_document_get_resource_list( SP_ACTIVE_DOCUMENT, "script" );
683 if (current) _ext_scripts_observer.set(SP_OBJECT(current->data)->parent);
684 while ( current ) {
685 SPObject* obj = SP_OBJECT(current->data);
686 SPScript* script = (SPScript*) obj;
687 if (script->xlinkhref)
688 {
689 Gtk::TreeModel::Row row = *(_ExternalScriptsListStore->append());
690 row[_ExternalScriptsListColumns.filenameColumn] = script->xlinkhref;
691 }
693 current = g_slist_next(current);
694 }
695 }
697 /**
698 * Called for _updating_ the dialog (e.g. when a new grid was manually added in XML)
699 */
700 void
701 DocumentProperties::update_gridspage()
702 {
703 SPDesktop *dt = getDesktop();
704 SPNamedView *nv = sp_desktop_namedview(dt);
706 //remove all tabs
707 while (_grids_notebook.get_n_pages() != 0) {
708 _grids_notebook.remove_page(-1); // this also deletes the page.
709 }
711 //add tabs
712 bool grids_present = false;
713 for (GSList const * l = nv->grids; l != NULL; l = l->next) {
714 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
715 if (!grid->repr->attribute("id")) continue; // update_gridspage is called again when "id" is added
716 Glib::ustring name(grid->repr->attribute("id"));
717 const char *icon = NULL;
718 switch (grid->getGridType()) {
719 case GRID_RECTANGULAR:
720 icon = "grid-rectangular";
721 break;
722 case GRID_AXONOMETRIC:
723 icon = "grid-axonometric";
724 break;
725 default:
726 break;
727 }
728 _grids_notebook.append_page(*grid->newWidget(), _createPageTabLabel(name, icon));
729 grids_present = true;
730 }
731 _grids_notebook.show_all();
733 if (grids_present)
734 _grids_button_remove.set_sensitive(true);
735 else
736 _grids_button_remove.set_sensitive(false);
737 }
739 /**
740 * Build grid page of dialog.
741 */
742 void
743 DocumentProperties::build_gridspage()
744 {
745 /// \todo FIXME: gray out snapping when grid is off.
746 /// Dissenting view: you want snapping without grid.
748 SPDesktop *dt = getDesktop();
749 SPNamedView *nv = sp_desktop_namedview(dt);
750 (void)nv;
752 _grids_label_crea.set_markup(_("<b>Creation</b>"));
753 _grids_label_def.set_markup(_("<b>Defined grids</b>"));
754 _grids_hbox_crea.pack_start(_grids_combo_gridtype, true, true);
755 _grids_hbox_crea.pack_start(_grids_button_new, true, true);
757 for (gint t = 0; t <= GRID_MAXTYPENR; t++) {
758 _grids_combo_gridtype.append_text( CanvasGrid::getName( (GridType) t ) );
759 }
760 _grids_combo_gridtype.set_active_text( CanvasGrid::getName(GRID_RECTANGULAR) );
762 _grids_space.set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
764 _grids_vbox.set_spacing(4);
765 _grids_vbox.pack_start(_grids_label_crea, false, false);
766 _grids_vbox.pack_start(_grids_hbox_crea, false, false);
767 _grids_vbox.pack_start(_grids_space, false, false);
768 _grids_vbox.pack_start(_grids_label_def, false, false);
769 _grids_vbox.pack_start(_grids_notebook, false, false);
770 _grids_vbox.pack_start(_grids_button_remove, false, false);
772 update_gridspage();
773 }
777 /**
778 * Update dialog widgets from desktop. Also call updateWidget routines of the grids.
779 */
780 void
781 DocumentProperties::update()
782 {
783 if (_wr.isUpdating()) return;
785 SPDesktop *dt = getDesktop();
786 SPNamedView *nv = sp_desktop_namedview(dt);
788 _wr.setUpdating (true);
789 set_sensitive (true);
791 //-----------------------------------------------------------page page
792 _rcp_bg.setRgba32 (nv->pagecolor);
793 _rcb_canb.setActive (nv->showborder);
794 _rcb_bord.setActive (nv->borderlayer == SP_BORDER_LAYER_TOP);
795 _rcp_bord.setRgba32 (nv->bordercolor);
796 _rcb_shad.setActive (nv->showpageshadow);
798 if (nv->doc_units)
799 _rum_deflt.setUnit (nv->doc_units);
801 double const doc_w_px = sp_document_width(sp_desktop_document(dt));
802 double const doc_h_px = sp_document_height(sp_desktop_document(dt));
803 _page_sizer.setDim (doc_w_px, doc_h_px);
804 _page_sizer.updateFitMarginsUI(SP_OBJECT_REPR(nv));
806 //-----------------------------------------------------------guide page
808 _rcb_sgui.setActive (nv->showguides);
809 _rcp_gui.setRgba32 (nv->guidecolor);
810 _rcp_hgui.setRgba32 (nv->guidehicolor);
811 _rcbsng.setActive(nv->snap_manager.snapprefs.getSnapModeGuide());
813 //-----------------------------------------------------------snap page
815 _rsu_sno.setValue (nv->snap_manager.snapprefs.getObjectTolerance());
816 _rsu_sn.setValue (nv->snap_manager.snapprefs.getGridTolerance());
817 _rsu_gusn.setValue (nv->snap_manager.snapprefs.getGuideTolerance());
820 //-----------------------------------------------------------grids page
822 update_gridspage();
824 //------------------------------------------------Color Management page
826 #if ENABLE_LCMS
827 populate_linked_profiles_box();
828 populate_available_profiles();
829 #endif // ENABLE_LCMS
831 _wr.setUpdating (false);
832 }
834 // TODO: copied from fill-and-stroke.cpp factor out into new ui/widget file?
835 Gtk::HBox&
836 DocumentProperties::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
837 {
838 Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
839 _tab_label_box->set_spacing(4);
840 _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
841 label_image)));
843 Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
844 _tab_label_box->pack_start(*_tab_label);
845 _tab_label_box->show_all();
847 return *_tab_label_box;
848 }
850 //--------------------------------------------------------------------
852 void
853 DocumentProperties::on_response (int id)
854 {
855 if (id == Gtk::RESPONSE_DELETE_EVENT || id == Gtk::RESPONSE_CLOSE)
856 {
857 _rcp_bg.closeWindow();
858 _rcp_bord.closeWindow();
859 _rcp_gui.closeWindow();
860 _rcp_hgui.closeWindow();
861 }
863 if (id == Gtk::RESPONSE_CLOSE)
864 hide();
865 }
867 void
868 DocumentProperties::_handleDocumentReplaced(SPDesktop* desktop, SPDocument *document)
869 {
870 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
871 repr->addListener(&_repr_events, this);
872 Inkscape::XML::Node *root = SP_OBJECT_REPR(document->root);
873 root->addListener(&_repr_events, this);
874 update();
875 }
877 void
878 DocumentProperties::_handleActivateDesktop(Inkscape::Application *, SPDesktop *desktop)
879 {
880 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
881 repr->addListener(&_repr_events, this);
882 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
883 root->addListener(&_repr_events, this);
884 update();
885 }
887 void
888 DocumentProperties::_handleDeactivateDesktop(Inkscape::Application *, SPDesktop *desktop)
889 {
890 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(desktop));
891 repr->removeListenerByData(this);
892 Inkscape::XML::Node *root = SP_OBJECT_REPR(sp_desktop_document(desktop)->root);
893 root->removeListenerByData(this);
894 }
896 static void
897 on_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
898 {
899 if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
900 dialog->update_gridspage();
901 }
903 static void
904 on_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node */*child*/, Inkscape::XML::Node */*ref*/, void *data)
905 {
906 if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
907 dialog->update_gridspage();
908 }
912 /**
913 * Called when XML node attribute changed; updates dialog widgets.
914 */
915 static void
916 on_repr_attr_changed (Inkscape::XML::Node *, gchar const *, gchar const *, gchar const *, bool, gpointer data)
917 {
918 if (DocumentProperties *dialog = static_cast<DocumentProperties *>(data))
919 dialog->update();
920 }
923 /*########################################################################
924 # BUTTON CLICK HANDLERS (callbacks)
925 ########################################################################*/
927 void
928 DocumentProperties::onNewGrid()
929 {
930 SPDesktop *dt = getDesktop();
931 Inkscape::XML::Node *repr = SP_OBJECT_REPR(sp_desktop_namedview(dt));
932 SPDocument *doc = sp_desktop_document(dt);
934 Glib::ustring typestring = _grids_combo_gridtype.get_active_text();
935 CanvasGrid::writeNewGridToRepr(repr, doc, CanvasGrid::getGridTypeFromName(typestring.c_str()));
937 // toggle grid showing to ON:
938 dt->showGrids(true);
939 }
942 void
943 DocumentProperties::onRemoveGrid()
944 {
945 gint pagenum = _grids_notebook.get_current_page();
946 if (pagenum == -1) // no pages
947 return;
949 SPDesktop *dt = getDesktop();
950 SPNamedView *nv = sp_desktop_namedview(dt);
951 Inkscape::CanvasGrid * found_grid = NULL;
952 int i = 0;
953 for (GSList const * l = nv->grids; l != NULL; l = l->next, i++) { // not a very nice fix, but works.
954 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
955 if (pagenum == i) {
956 found_grid = grid;
957 break; // break out of for-loop
958 }
959 }
960 if (found_grid) {
961 // delete the grid that corresponds with the selected tab
962 // when the grid is deleted from SVG, the SPNamedview handler automatically deletes the object, so found_grid becomes an invalid pointer!
963 found_grid->repr->parent()->removeChild(found_grid->repr);
964 sp_document_done(sp_desktop_document(dt), SP_VERB_DIALOG_NAMEDVIEW, _("Remove grid"));
965 }
966 }
969 } // namespace Dialog
970 } // namespace UI
971 } // namespace Inkscape
973 /*
974 Local Variables:
975 mode:c++
976 c-file-style:"stroustrup"
977 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
978 indent-tabs-mode:nil
979 fill-column:99
980 End:
981 */
982 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :