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