1 #define __SP_STROKE_STYLE_C__
3 /**
4 * \brief Stroke style dialog
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Bryce Harrington <brycehar@bryceharrington.org>
9 * bulia byak <buliabyak@users.sf.net>
10 * Maximilian Albert <maximilian.albert@gmail.com>
11 *
12 * Copyright (C) 2001-2005 authors
13 * Copyright (C) 2001 Ximian, Inc.
14 * Copyright (C) 2004 John Cliff
15 * Copyright (C) 2008 Maximilian Albert (gtkmm-ification)
16 *
17 * Released under GNU GPL, read the file 'COPYING' for more information
18 */
20 #define noSP_SS_VERBOSE
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
28 #include <glib/gmem.h>
29 #include <gtk/gtk.h>
31 #include <glibmm/i18n.h>
32 #include "helper/unit-menu.h"
33 #include "helper/units.h"
34 #include "svg/css-ostringstream.h"
35 #include "widgets/sp-widget.h"
36 #include "widgets/spw-utilities.h"
37 #include "sp-linear-gradient.h"
38 #include "sp-radial-gradient.h"
39 #include "marker.h"
40 #include "sp-pattern.h"
41 #include "widgets/paint-selector.h"
42 #include "widgets/dash-selector.h"
43 #include "style.h"
44 #include "gradient-chemistry.h"
45 #include "sp-namedview.h"
46 #include "desktop-handles.h"
47 #include "desktop-style.h"
48 #include "selection.h"
49 #include "inkscape.h"
50 #include "inkscape-stock.h"
51 #include "dialogs/dialog-events.h"
52 #include "sp-text.h"
53 #include "sp-rect.h"
54 #include "document-private.h"
55 #include "display/nr-arena.h"
56 #include "display/nr-arena-item.h"
57 #include "path-prefix.h"
58 #include "widgets/icon.h"
59 #include "helper/stock-items.h"
60 #include "io/sys.h"
61 #include "ui/cache/svg_preview_cache.h"
63 #include "dialogs/stroke-style.h"
66 /* Paint */
68 static void sp_stroke_style_paint_selection_modified (SPWidget *spw, Inkscape::Selection *selection, guint flags, SPPaintSelector *psel);
69 static void sp_stroke_style_paint_selection_changed (SPWidget *spw, Inkscape::Selection *selection, SPPaintSelector *psel);
70 static void sp_stroke_style_paint_update(SPWidget *spw);
72 static void sp_stroke_style_paint_mode_changed(SPPaintSelector *psel, SPPaintSelectorMode mode, SPWidget *spw);
73 static void sp_stroke_style_paint_dragged(SPPaintSelector *psel, SPWidget *spw);
74 static void sp_stroke_style_paint_changed(SPPaintSelector *psel, SPWidget *spw);
76 static void sp_stroke_style_widget_change_subselection ( Inkscape::Application *inkscape, SPDesktop *desktop, SPWidget *spw );
77 static void sp_stroke_style_widget_transientize_callback(Inkscape::Application *inkscape,
78 SPDesktop *desktop,
79 SPWidget *spw );
81 /** Marker selection option menus */
82 static Gtk::OptionMenu * marker_start_menu = NULL;
83 static Gtk::OptionMenu * marker_mid_menu = NULL;
84 static Gtk::OptionMenu * marker_end_menu = NULL;
86 sigc::connection marker_start_menu_connection;
87 sigc::connection marker_mid_menu_connection;
88 sigc::connection marker_end_menu_connection;
90 static SPObject *ink_extract_marker_name(gchar const *n, SPDocument *doc);
91 static void ink_markers_menu_update(Gtk::Container* spw, SPMarkerLoc const which);
93 static Inkscape::UI::Cache::SvgPreview svg_preview_cache;
95 /**
96 * Create the stroke style widget, and hook up all the signals.
97 */
98 GtkWidget *
99 sp_stroke_style_paint_widget_new(void)
100 {
101 GtkWidget *spw, *psel;
103 spw = sp_widget_new_global(INKSCAPE);
105 psel = sp_paint_selector_new(false); // without fillrule selector
106 gtk_widget_show(psel);
107 gtk_container_add(GTK_CONTAINER(spw), psel);
108 gtk_object_set_data(GTK_OBJECT(spw), "paint-selector", psel);
110 gtk_signal_connect(GTK_OBJECT(spw), "modify_selection",
111 GTK_SIGNAL_FUNC(sp_stroke_style_paint_selection_modified),
112 psel);
113 gtk_signal_connect(GTK_OBJECT(spw), "change_selection",
114 GTK_SIGNAL_FUNC(sp_stroke_style_paint_selection_changed),
115 psel);
117 g_signal_connect (INKSCAPE, "change_subselection", G_CALLBACK (sp_stroke_style_widget_change_subselection), spw);
119 g_signal_connect (G_OBJECT(INKSCAPE), "activate_desktop", G_CALLBACK (sp_stroke_style_widget_transientize_callback), spw );
121 gtk_signal_connect(GTK_OBJECT(psel), "mode_changed",
122 GTK_SIGNAL_FUNC(sp_stroke_style_paint_mode_changed),
123 spw);
124 gtk_signal_connect(GTK_OBJECT(psel), "dragged",
125 GTK_SIGNAL_FUNC(sp_stroke_style_paint_dragged),
126 spw);
127 gtk_signal_connect(GTK_OBJECT(psel), "changed",
128 GTK_SIGNAL_FUNC(sp_stroke_style_paint_changed),
129 spw);
131 sp_stroke_style_paint_update (SP_WIDGET(spw));
132 return spw;
133 }
135 /**
136 * On signal modified, invokes an update of the stroke style paint object.
137 */
138 static void
139 sp_stroke_style_paint_selection_modified( SPWidget *spw,
140 Inkscape::Selection */*selection*/,
141 guint flags,
142 SPPaintSelector */*psel*/ )
143 {
144 if (flags & ( SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG |
145 SP_OBJECT_STYLE_MODIFIED_FLAG) ) {
146 sp_stroke_style_paint_update(spw);
147 }
148 }
151 /**
152 * On signal selection changed, invokes an update of the stroke style paint object.
153 */
154 static void
155 sp_stroke_style_paint_selection_changed( SPWidget *spw,
156 Inkscape::Selection */*selection*/,
157 SPPaintSelector */*psel*/ )
158 {
159 sp_stroke_style_paint_update (spw);
160 }
163 /**
164 * On signal change subselection, invoke an update of the stroke style widget.
165 */
166 static void
167 sp_stroke_style_widget_change_subselection( Inkscape::Application */*inkscape*/,
168 SPDesktop */*desktop*/,
169 SPWidget *spw )
170 {
171 sp_stroke_style_paint_update (spw);
172 }
174 /**
175 * Gets the active stroke style property, then sets the appropriate color, alpha, gradient,
176 * pattern, etc. for the paint-selector.
177 */
178 static void
179 sp_stroke_style_paint_update (SPWidget *spw)
180 {
181 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
182 return;
183 }
185 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
187 SPPaintSelector *psel = SP_PAINT_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "paint-selector"));
189 // create temporary style
190 SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
191 // query into it
192 int result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKE);
194 switch (result) {
195 case QUERY_STYLE_NOTHING:
196 {
197 /* No paint at all */
198 sp_paint_selector_set_mode (psel, SP_PAINT_SELECTOR_MODE_EMPTY);
199 break;
200 }
202 case QUERY_STYLE_SINGLE:
203 case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently, e.g. display "averaged" somewhere in paint selector
204 case QUERY_STYLE_MULTIPLE_SAME:
205 {
206 SPPaintSelectorMode pselmode = sp_style_determine_paint_selector_mode (query, false);
207 sp_paint_selector_set_mode (psel, pselmode);
209 if (query->stroke.set && query->stroke.isPaintserver()) {
211 SPPaintServer *server = SP_STYLE_STROKE_SERVER (query);
213 if (SP_IS_LINEARGRADIENT (server)) {
214 SPGradient *vector = sp_gradient_get_vector (SP_GRADIENT (server), FALSE);
215 sp_paint_selector_set_gradient_linear (psel, vector);
217 SPLinearGradient *lg = SP_LINEARGRADIENT (server);
218 sp_paint_selector_set_gradient_properties (psel,
219 SP_GRADIENT_UNITS (lg),
220 SP_GRADIENT_SPREAD (lg));
221 } else if (SP_IS_RADIALGRADIENT (server)) {
222 SPGradient *vector = sp_gradient_get_vector (SP_GRADIENT (server), FALSE);
223 sp_paint_selector_set_gradient_radial (psel, vector);
225 SPRadialGradient *rg = SP_RADIALGRADIENT (server);
226 sp_paint_selector_set_gradient_properties (psel,
227 SP_GRADIENT_UNITS (rg),
228 SP_GRADIENT_SPREAD (rg));
229 } else if (SP_IS_PATTERN (server)) {
230 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
231 sp_update_pattern_list (psel, pat);
232 }
233 } else if (query->stroke.set && query->stroke.isColor()) {
234 sp_paint_selector_set_color_alpha (psel, &query->stroke.value.color, SP_SCALE24_TO_FLOAT (query->stroke_opacity.value));
236 }
237 break;
238 }
240 case QUERY_STYLE_MULTIPLE_DIFFERENT:
241 {
242 sp_paint_selector_set_mode (psel, SP_PAINT_SELECTOR_MODE_MULTIPLE);
243 break;
244 }
245 }
247 sp_style_unref(query);
249 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
250 }
252 /**
253 * When the mode is changed, invoke a regular changed handler.
254 */
255 static void
256 sp_stroke_style_paint_mode_changed( SPPaintSelector *psel,
257 SPPaintSelectorMode /*mode*/,
258 SPWidget *spw )
259 {
260 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
261 return;
262 }
264 /* TODO: Does this work?
265 * Not really, here we have to get old color back from object
266 * Instead of relying on paint widget having meaningful colors set
267 */
268 sp_stroke_style_paint_changed(psel, spw);
269 }
271 static gchar const *const undo_label_1 = "stroke:flatcolor:1";
272 static gchar const *const undo_label_2 = "stroke:flatcolor:2";
273 static gchar const *undo_label = undo_label_1;
275 /**
276 * When a drag callback occurs on a paint selector object, if it is a RGB or CMYK
277 * color mode, then set the stroke opacity to psel's flat color.
278 */
279 static void
280 sp_stroke_style_paint_dragged(SPPaintSelector *psel, SPWidget *spw)
281 {
282 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
283 return;
284 }
286 switch (psel->mode) {
287 case SP_PAINT_SELECTOR_MODE_COLOR_RGB:
288 case SP_PAINT_SELECTOR_MODE_COLOR_CMYK:
289 {
290 sp_paint_selector_set_flat_color (psel, SP_ACTIVE_DESKTOP, "stroke", "stroke-opacity");
291 sp_document_maybe_done (sp_desktop_document(SP_ACTIVE_DESKTOP), undo_label, SP_VERB_DIALOG_FILL_STROKE,
292 _("Set stroke color"));
293 break;
294 }
296 default:
297 g_warning( "file %s: line %d: Paint %d should not emit 'dragged'",
298 __FILE__, __LINE__, psel->mode);
299 break;
300 }
301 }
303 /**
304 * When the stroke style's paint settings change, this handler updates the
305 * repr's stroke css style and applies the style to relevant drawing items.
306 */
307 static void
308 sp_stroke_style_paint_changed(SPPaintSelector *psel, SPWidget *spw)
309 {
310 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
311 return;
312 }
313 g_object_set_data (G_OBJECT (spw), "update", GINT_TO_POINTER (TRUE));
315 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
316 SPDocument *document = sp_desktop_document (desktop);
317 Inkscape::Selection *selection = sp_desktop_selection (desktop);
319 GSList const *items = selection->itemList();
321 switch (psel->mode) {
322 case SP_PAINT_SELECTOR_MODE_EMPTY:
323 // This should not happen.
324 g_warning ( "file %s: line %d: Paint %d should not emit 'changed'",
325 __FILE__, __LINE__, psel->mode);
326 break;
327 case SP_PAINT_SELECTOR_MODE_MULTIPLE:
328 // This happens when you switch multiple objects with different gradients to flat color;
329 // nothing to do here.
330 break;
332 case SP_PAINT_SELECTOR_MODE_NONE:
333 {
334 SPCSSAttr *css = sp_repr_css_attr_new();
335 sp_repr_css_set_property(css, "stroke", "none");
337 sp_desktop_set_style (desktop, css);
339 sp_repr_css_attr_unref(css);
341 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
342 _("Remove stroke"));
343 break;
344 }
346 case SP_PAINT_SELECTOR_MODE_COLOR_RGB:
347 case SP_PAINT_SELECTOR_MODE_COLOR_CMYK:
348 {
349 sp_paint_selector_set_flat_color (psel, desktop, "stroke", "stroke-opacity");
350 sp_document_maybe_done (sp_desktop_document(desktop), undo_label, SP_VERB_DIALOG_FILL_STROKE,
351 _("Set stroke color"));
353 // on release, toggle undo_label so that the next drag will not be lumped with this one
354 if (undo_label == undo_label_1)
355 undo_label = undo_label_2;
356 else
357 undo_label = undo_label_1;
359 break;
360 }
362 case SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR:
363 case SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL:
364 if (items) {
365 SPGradientType const gradient_type = ( psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR
366 ? SP_GRADIENT_TYPE_LINEAR
367 : SP_GRADIENT_TYPE_RADIAL );
368 SPGradient *vector = sp_paint_selector_get_gradient_vector(psel);
369 if (!vector) {
370 /* No vector in paint selector should mean that we just changed mode */
372 SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
373 int result = objects_query_fillstroke ((GSList *) items, query, false);
374 guint32 common_rgb = 0;
375 if (result == QUERY_STYLE_MULTIPLE_SAME) {
376 if (!query->fill.isColor()) {
377 common_rgb = sp_desktop_get_color(desktop, false);
378 } else {
379 common_rgb = query->stroke.value.color.toRGBA32( 0xff );
380 }
381 vector = sp_document_default_gradient_vector(document, common_rgb);
382 }
383 sp_style_unref(query);
385 for (GSList const *i = items; i != NULL; i = i->next) {
386 if (!vector) {
387 sp_item_set_gradient(SP_ITEM(i->data),
388 sp_gradient_vector_for_object(document, desktop, SP_OBJECT(i->data), false),
389 gradient_type, false);
390 } else {
391 sp_item_set_gradient(SP_ITEM(i->data), vector, gradient_type, false);
392 }
393 }
394 } else {
395 vector = sp_gradient_ensure_vector_normalized(vector);
396 for (GSList const *i = items; i != NULL; i = i->next) {
397 SPGradient *gr = sp_item_set_gradient(SP_ITEM(i->data), vector, gradient_type, false);
398 sp_gradient_selector_attrs_to_gradient(gr, psel);
399 }
400 }
402 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
403 _("Set gradient on stroke"));
404 }
405 break;
407 case SP_PAINT_SELECTOR_MODE_PATTERN:
409 if (items) {
411 SPPattern *pattern = sp_paint_selector_get_pattern (psel);
412 if (!pattern) {
414 /* No Pattern in paint selector should mean that we just
415 * changed mode - dont do jack.
416 */
418 } else {
419 Inkscape::XML::Node *patrepr = SP_OBJECT_REPR(pattern);
420 SPCSSAttr *css = sp_repr_css_attr_new ();
421 gchar *urltext = g_strdup_printf ("url(#%s)", patrepr->attribute("id"));
422 sp_repr_css_set_property (css, "stroke", urltext);
424 for (GSList const *i = items; i != NULL; i = i->next) {
425 Inkscape::XML::Node *selrepr = SP_OBJECT_REPR (i->data);
426 SPObject *selobj = SP_OBJECT (i->data);
427 if (!selrepr)
428 continue;
430 SPStyle *style = SP_OBJECT_STYLE (selobj);
431 if (style && style->stroke.isPaintserver()) {
432 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (selobj);
433 if (SP_IS_PATTERN (server) && pattern_getroot (SP_PATTERN(server)) == pattern)
434 // only if this object's pattern is not rooted in our selected pattern, apply
435 continue;
436 }
438 sp_repr_css_change_recursive (selrepr, css, "style");
439 }
441 sp_repr_css_attr_unref (css);
442 g_free (urltext);
444 } // end if
446 sp_document_done (document, SP_VERB_DIALOG_FILL_STROKE,
447 _("Set pattern on stroke"));
448 } // end if
450 break;
452 case SP_PAINT_SELECTOR_MODE_UNSET:
453 if (items) {
454 SPCSSAttr *css = sp_repr_css_attr_new ();
455 sp_repr_css_unset_property (css, "stroke");
456 sp_repr_css_unset_property (css, "stroke-opacity");
457 sp_repr_css_unset_property (css, "stroke-width");
458 sp_repr_css_unset_property (css, "stroke-miterlimit");
459 sp_repr_css_unset_property (css, "stroke-linejoin");
460 sp_repr_css_unset_property (css, "stroke-linecap");
461 sp_repr_css_unset_property (css, "stroke-dashoffset");
462 sp_repr_css_unset_property (css, "stroke-dasharray");
464 sp_desktop_set_style (desktop, css);
465 sp_repr_css_attr_unref (css);
467 sp_document_done (document, SP_VERB_DIALOG_FILL_STROKE,
468 _("Unset stroke"));
469 }
470 break;
472 default:
473 g_warning( "file %s: line %d: Paint selector should not be in "
474 "mode %d",
475 __FILE__, __LINE__,
476 psel->mode );
477 break;
478 }
480 g_object_set_data (G_OBJECT (spw), "update", GINT_TO_POINTER (FALSE));
481 }
487 /* Line */
489 static void sp_stroke_style_line_selection_modified(SPWidget *spw, Inkscape::Selection *selection, guint flags, gpointer data);
490 static void sp_stroke_style_line_selection_changed(SPWidget *spw, Inkscape::Selection *selection, gpointer data);
492 static void sp_stroke_style_line_update(Gtk::Container *spw, Inkscape::Selection *sel);
494 static void sp_stroke_style_set_join_buttons(Gtk::Container *spw, Gtk::ToggleButton *active);
496 static void sp_stroke_style_set_cap_buttons(Gtk::Container *spw, Gtk::ToggleButton *active);
498 static void sp_stroke_style_width_changed(Gtk::Container *spw);
499 static void sp_stroke_style_miterlimit_changed(Gtk::Container *spw);
500 static void sp_stroke_style_any_toggled(Gtk::ToggleButton *tb, Gtk::Container *spw);
501 static void sp_stroke_style_line_dash_changed(Gtk::Container *spw);
503 static void sp_stroke_style_update_marker_menus(Gtk::Container *spw, GSList const *objects);
506 /**
507 * Helper function for creating radio buttons. This should probably be re-thought out
508 * when reimplementing this with Gtkmm.
509 */
510 static Gtk::RadioButton *
511 sp_stroke_radio_button(Gtk::RadioButton *tb, char const *icon,
512 Gtk::HBox *hb, Gtk::Container *spw,
513 gchar const *key, gchar const *data)
514 {
515 g_assert(icon != NULL);
516 g_assert(hb != NULL);
517 g_assert(spw != NULL);
519 if (tb == NULL) {
520 tb = new Gtk::RadioButton();
521 } else {
522 Gtk::RadioButtonGroup grp = tb->get_group();
523 tb = new Gtk::RadioButton(grp);
524 }
526 tb->show();
527 tb->set_mode(false);
528 hb->pack_start(*tb, false, false, 0);
529 spw->set_data(icon, tb);
530 tb->set_data(key, (gpointer*)data);
531 tb->signal_toggled().connect(sigc::bind<Gtk::RadioButton *, Gtk::Container *>(
532 sigc::ptr_fun(&sp_stroke_style_any_toggled), tb, spw));
533 Gtk::Widget *px = manage(Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_LARGE_TOOLBAR, icon)));
534 g_assert(px != NULL);
535 px->show();
536 tb->add(*px);
538 return tb;
540 }
542 static void
543 sp_stroke_style_widget_transientize_callback(Inkscape::Application */*inkscape*/,
544 SPDesktop */*desktop*/,
545 SPWidget */*spw*/ )
546 {
547 // TODO: Either of these will cause crashes sometimes
548 // sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
549 // ink_markers_menu_update(spw);
550 }
552 /**
553 * Creates a copy of the marker named mname, determines its visible and renderable
554 * area in menu_id's bounding box, and then renders it. This allows us to fill in
555 * preview images of each marker in the marker menu.
556 */
557 static Gtk::Image *
558 sp_marker_prev_new(unsigned psize, gchar const *mname,
559 SPDocument *source, SPDocument *sandbox,
560 gchar const *menu_id, NRArena const */*arena*/, unsigned /*visionkey*/, NRArenaItem *root)
561 {
562 // Retrieve the marker named 'mname' from the source SVG document
563 SPObject const *marker = source->getObjectById(mname);
564 if (marker == NULL)
565 return NULL;
567 // Create a copy repr of the marker with id="sample"
568 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(sandbox);
569 Inkscape::XML::Node *mrepr = SP_OBJECT_REPR (marker)->duplicate(xml_doc);
570 mrepr->setAttribute("id", "sample");
572 // Replace the old sample in the sandbox by the new one
573 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (sandbox->getObjectById("defs"));
574 SPObject *oldmarker = sandbox->getObjectById("sample");
575 if (oldmarker)
576 oldmarker->deleteObject(false);
577 defsrepr->appendChild(mrepr);
578 Inkscape::GC::release(mrepr);
580 // Uncomment this to get the sandbox documents saved (useful for debugging)
581 //FILE *fp = fopen (g_strconcat(menu_id, mname, ".svg", NULL), "w");
582 //sp_repr_save_stream (sp_document_repr_doc (sandbox), fp);
583 //fclose (fp);
585 // object to render; note that the id is the same as that of the menu we're building
586 SPObject *object = sandbox->getObjectById(menu_id);
587 sp_document_root (sandbox)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
588 sp_document_ensure_up_to_date(sandbox);
590 if (object == NULL || !SP_IS_ITEM(object))
591 return NULL; // sandbox broken?
593 // Find object's bbox in document
594 Geom::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
595 boost::optional<Geom::Rect> dbox = SP_ITEM(object)->getBounds(i2doc);
597 if (!dbox) {
598 return NULL;
599 }
601 /* Update to renderable state */
602 double sf = 0.8;
604 gchar *cache_name = g_strconcat(menu_id, mname, NULL);
605 Glib::ustring key = svg_preview_cache.cache_key(source->uri, cache_name, psize);
606 g_free (cache_name);
607 // TODO: is this correct?
608 Glib::RefPtr<Gdk::Pixbuf> pixbuf = Glib::wrap(svg_preview_cache.get_preview_from_cache(key));
610 if (!pixbuf) {
611 pixbuf = Glib::wrap(render_pixbuf(root, sf, *dbox, psize));
612 svg_preview_cache.set_preview_in_cache(key, pixbuf->gobj());
613 }
615 // Create widget
616 Gtk::Image *pb = new Gtk::Image(pixbuf);
618 return pb;
619 }
621 /**
622 * Returns a list of markers in the defs of the given source document as a GSList object
623 * Returns NULL if there are no markers in the document.
624 */
625 GSList *
626 ink_marker_list_get (SPDocument *source)
627 {
628 if (source == NULL)
629 return NULL;
631 GSList *ml = NULL;
632 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS (source);
633 for ( SPObject *child = sp_object_first_child(SP_OBJECT(defs));
634 child != NULL;
635 child = SP_OBJECT_NEXT (child) )
636 {
637 if (SP_IS_MARKER(child)) {
638 ml = g_slist_prepend (ml, child);
639 }
640 }
641 return ml;
642 }
644 #define MARKER_ITEM_MARGIN 0
646 /**
647 * Adds previews of markers in marker_list to the given menu widget
648 */
649 static void
650 sp_marker_menu_build (Gtk::Menu *m, GSList *marker_list, SPDocument *source, SPDocument *sandbox, gchar const *menu_id)
651 {
652 // Do this here, outside of loop, to speed up preview generation:
653 NRArena const *arena = NRArena::create();
654 unsigned const visionkey = sp_item_display_key_new(1);
655 NRArenaItem *root = sp_item_invoke_show(SP_ITEM(SP_DOCUMENT_ROOT (sandbox)), (NRArena *) arena, visionkey, SP_ITEM_SHOW_DISPLAY);
657 for (; marker_list != NULL; marker_list = marker_list->next) {
658 Inkscape::XML::Node *repr = SP_OBJECT_REPR((SPItem *) marker_list->data);
659 Gtk::MenuItem *i = new Gtk::MenuItem();
660 i->show();
662 if (repr->attribute("inkscape:stockid"))
663 i->set_data("stockid", (void *) "true");
664 else
665 i->set_data("stockid", (void *) "false");
667 gchar const *markid = repr->attribute("id");
668 i->set_data("marker", (void *) markid);
670 Gtk::HBox *hb = new Gtk::HBox(false, MARKER_ITEM_MARGIN);
671 hb->show();
673 // generate preview
675 Gtk::Image *prv = sp_marker_prev_new (22, markid, source, sandbox, menu_id, arena, visionkey, root);
676 prv->show();
677 hb->pack_start(*prv, false, false, 6);
679 // create label
680 Gtk::Label *l = new Gtk::Label(repr->attribute("id"));
681 l->show();
682 l->set_alignment(0.0, 0.5);
684 hb->pack_start(*l, true, true, 0);
686 hb->show();
687 i->add(*hb);
689 m->append(*i);
690 }
692 sp_item_invoke_hide(SP_ITEM(sp_document_root(sandbox)), visionkey);
693 nr_object_unref((NRObject *) arena);
694 }
696 /**
697 * sp_marker_list_from_doc()
698 *
699 * \brief Pick up all markers from source, except those that are in
700 * current_doc (if non-NULL), and add items to the m menu
701 *
702 */
703 static void
704 sp_marker_list_from_doc (Gtk::Menu *m, SPDocument */*current_doc*/, SPDocument *source, SPDocument */*markers_doc*/, SPDocument *sandbox, gchar const *menu_id)
705 {
706 GSList *ml = ink_marker_list_get(source);
707 GSList *clean_ml = NULL;
709 for (; ml != NULL; ml = ml->next) {
710 if (!SP_IS_MARKER(ml->data))
711 continue;
713 // Add to the list of markers we really do wish to show
714 clean_ml = g_slist_prepend (clean_ml, ml->data);
715 }
716 sp_marker_menu_build(m, clean_ml, source, sandbox, menu_id);
718 g_slist_free (ml);
719 g_slist_free (clean_ml);
720 }
722 /**
723 * Returns a new document containing default start, mid, and end markers.
724 */
725 SPDocument *
726 ink_markers_preview_doc ()
727 {
728 gchar const *buffer = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">"
729 " <defs id=\"defs\" />"
731 " <g id=\"marker-start\">"
732 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:url(#sample);marker-mid:none;marker-end:none\""
733 " d=\"M 12.5,13 L 25,13\" id=\"path1\" />"
734 " <rect style=\"fill:none;stroke:none\" id=\"rect2\""
735 " width=\"25\" height=\"25\" x=\"0\" y=\"0\" />"
736 " </g>"
738 " <g id=\"marker-mid\">"
739 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:url(#sample);marker-end:none\""
740 " d=\"M 0,113 L 12.5,113 L 25,113\" id=\"path11\" />"
741 " <rect style=\"fill:none;stroke:none\" id=\"rect22\""
742 " width=\"25\" height=\"25\" x=\"0\" y=\"100\" />"
743 " </g>"
745 " <g id=\"marker-end\">"
746 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:none;marker-end:url(#sample)\""
747 " d=\"M 0,213 L 12.5,213\" id=\"path111\" />"
748 " <rect style=\"fill:none;stroke:none\" id=\"rect222\""
749 " width=\"25\" height=\"25\" x=\"0\" y=\"200\" />"
750 " </g>"
752 "</svg>";
754 return sp_document_new_from_mem (buffer, strlen(buffer), FALSE);
755 }
757 static void
758 ink_marker_menu_create_menu(Gtk::Menu *m, gchar const *menu_id, SPDocument *doc, SPDocument *sandbox)
759 {
760 static SPDocument *markers_doc = NULL;
762 // add "None"
763 Gtk::MenuItem *i = new Gtk::MenuItem();
764 i->show();
766 i->set_data("marker", (void *) "none");
768 Gtk::HBox *hb = new Gtk::HBox(false, MARKER_ITEM_MARGIN);
769 hb->show();
771 Gtk::Label *l = new Gtk::Label( _("None") );
772 l->show();
773 l->set_alignment(0.0, 0.5);
775 hb->pack_start(*l, true, true, 0);
777 hb->show();
778 i->add(*hb);
779 m->append(*i);
781 // find and load markers.svg
782 if (markers_doc == NULL) {
783 char *markers_source = g_build_filename(INKSCAPE_MARKERSDIR, "markers.svg", NULL);
784 if (Inkscape::IO::file_test(markers_source, G_FILE_TEST_IS_REGULAR)) {
785 markers_doc = sp_document_new(markers_source, FALSE);
786 }
787 g_free(markers_source);
788 }
790 // suck in from current doc
791 sp_marker_list_from_doc(m, NULL, doc, markers_doc, sandbox, menu_id);
793 // add separator
794 {
795 //Gtk::Separator *i = gtk_separator_menu_item_new();
796 Gtk::SeparatorMenuItem *i = new Gtk::SeparatorMenuItem();
797 i->show();
798 m->append(*i);
799 }
801 // suck in from markers.svg
802 if (markers_doc) {
803 sp_document_ensure_up_to_date(doc);
804 sp_marker_list_from_doc(m, doc, markers_doc, NULL, sandbox, menu_id);
805 }
807 }
809 /**
810 * Creates a menu widget to display markers from markers.svg
811 */
812 static Gtk::OptionMenu *
813 ink_marker_menu(Gtk::Widget */*tbl*/, gchar const *menu_id, SPDocument *sandbox)
814 {
815 SPDesktop *desktop = inkscape_active_desktop();
816 SPDocument *doc = sp_desktop_document(desktop);
817 Gtk::OptionMenu *mnu = new Gtk::OptionMenu();
819 /* Create new menu widget */
820 Gtk::Menu *m = new Gtk::Menu();
821 m->show();
823 mnu->set_data("updating", (gpointer) FALSE);
825 if (!doc) {
826 Gtk::MenuItem *i = new Gtk::MenuItem(_("No document selected"));
827 i->show();
828 m->append(*i);
829 mnu->set_sensitive(false);
831 } else {
832 ink_marker_menu_create_menu(m, menu_id, doc, sandbox);
834 mnu->set_sensitive(true);
835 }
837 mnu->set_data("menu_id", const_cast<gchar *>(menu_id));
838 mnu->set_menu(*m);
840 /* Set history */
841 mnu->set_history(0);
843 return mnu;
844 }
846 /**
847 * Handles when user selects one of the markers from the marker menu.
848 * Defines a uri string to refer to it, then applies it to all selected
849 * items in the current desktop.
850 */
851 static void
852 sp_marker_select(Gtk::OptionMenu *mnu, Gtk::Container *spw, SPMarkerLoc const which)
853 {
854 if (spw->get_data("update")) {
855 return;
856 }
858 SPDesktop *desktop = inkscape_active_desktop();
859 SPDocument *document = sp_desktop_document(desktop);
860 if (!document) {
861 return;
862 }
864 /* Get Marker */
865 if (!mnu->get_menu()->get_active()->get_data("marker"))
866 {
867 return;
868 }
869 gchar *markid = static_cast<gchar *>(mnu->get_menu()->get_active()->get_data("marker"));
870 gchar const *marker = "";
871 if (strcmp(markid, "none")) {
872 gchar *stockid = static_cast<gchar *>(mnu->get_menu()->get_active()->get_data("stockid"));
874 gchar *markurn = markid;
875 if (!strcmp(stockid,"true")) markurn = g_strconcat("urn:inkscape:marker:",markid,NULL);
876 SPObject *mark = get_stock_item(markurn);
877 if (mark) {
878 Inkscape::XML::Node *repr = SP_OBJECT_REPR(mark);
879 marker = g_strconcat("url(#", repr->attribute("id"), ")", NULL);
880 }
881 } else {
882 marker = markid;
883 }
884 SPCSSAttr *css = sp_repr_css_attr_new();
885 gchar const *menu_id = static_cast<gchar const *>(mnu->get_data("menu_id"));
886 sp_repr_css_set_property(css, menu_id, marker);
888 // Also update the marker dropdown menus, so the document's markers
889 // show up at the top of the menu
890 // sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
891 ink_markers_menu_update(spw, which);
893 Inkscape::Selection *selection = sp_desktop_selection(desktop);
894 GSList const *items = selection->itemList();
895 for (; items != NULL; items = items->next) {
896 SPItem *item = (SPItem *) items->data;
897 if (!SP_IS_SHAPE(item) || SP_IS_RECT(item)) // can't set marker to rect, until it's converted to using <path>
898 continue;
899 Inkscape::XML::Node *selrepr = SP_OBJECT_REPR((SPItem *) items->data);
900 if (selrepr) {
901 sp_repr_css_change_recursive(selrepr, css, "style");
902 }
903 SP_OBJECT(items->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
904 SP_OBJECT(items->data)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
905 }
907 sp_repr_css_attr_unref(css);
909 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
910 _("Set markers"));
912 };
914 static unsigned int
915 ink_marker_menu_get_pos(Gtk::Menu *mnu, gchar const *markname)
916 {
917 if (markname == NULL)
918 markname = static_cast<gchar const *>(mnu->get_active()->get_data("marker"));
920 if (markname == NULL)
921 return 0;
923 std::vector<Gtk::Widget *> kids = mnu->get_children();
924 unsigned int i = 0;
925 for (; i < kids.size();) {
926 gchar const *mark = static_cast<gchar const *>(kids[i]->get_data("marker"));
927 if (mark && strcmp(mark, markname) == 0) {
928 break;
929 }
930 ++i;
931 }
933 return i;
934 }
936 static void
937 ink_markers_menu_update(Gtk::Container* /*spw*/, SPMarkerLoc const which) {
938 SPDesktop *desktop = inkscape_active_desktop();
939 SPDocument *document = sp_desktop_document(desktop);
940 SPDocument *sandbox = ink_markers_preview_doc ();
941 Gtk::Menu *m;
942 int pos;
944 // TODO: this code can be shortened by abstracting out marker_(start|mid|end)_...
945 switch (which) {
946 case SP_MARKER_LOC_START:
947 marker_start_menu_connection.block();
948 pos = ink_marker_menu_get_pos(marker_start_menu->get_menu(), NULL);
949 m = new Gtk::Menu();
950 m->show();
951 ink_marker_menu_create_menu(m, "marker-start", document, sandbox);
952 marker_start_menu->remove_menu();
953 marker_start_menu->set_menu(*m);
954 marker_start_menu->set_history(pos);
955 marker_start_menu_connection.unblock();
956 break;
958 case SP_MARKER_LOC_MID:
959 marker_mid_menu_connection.block();
960 pos = ink_marker_menu_get_pos(marker_mid_menu->get_menu(), NULL);
961 m = new Gtk::Menu();
962 m->show();
963 ink_marker_menu_create_menu(m, "marker-mid", document, sandbox);
964 marker_mid_menu->remove_menu();
965 marker_mid_menu->set_menu(*m);
966 marker_mid_menu->set_history(pos);
967 marker_mid_menu_connection.unblock();
968 break;
970 case SP_MARKER_LOC_END:
971 marker_end_menu_connection.block();
972 pos = ink_marker_menu_get_pos(marker_end_menu->get_menu(), NULL);
973 m = new Gtk::Menu();
974 m->show();
975 ink_marker_menu_create_menu(m, "marker-end", document, sandbox);
976 marker_end_menu->remove_menu();
977 marker_end_menu->set_menu(*m);
978 marker_end_menu->set_history(pos);
979 marker_end_menu_connection.unblock();
980 break;
981 default:
982 g_assert_not_reached();
983 }
984 }
986 /**
987 * Sets the stroke width units for all selected items.
988 * Also handles absolute and dimensionless units.
989 */
990 static gboolean stroke_width_set_unit(SPUnitSelector *,
991 SPUnit const *old,
992 SPUnit const *new_units,
993 Gtk::Container *spw)
994 {
995 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
997 if (!desktop) {
998 return FALSE;
999 }
1001 Inkscape::Selection *selection = sp_desktop_selection (desktop);
1003 if (selection->isEmpty())
1004 return FALSE;
1006 GSList const *objects = selection->itemList();
1008 if ((old->base == SP_UNIT_ABSOLUTE || old->base == SP_UNIT_DEVICE) &&
1009 (new_units->base == SP_UNIT_DIMENSIONLESS)) {
1011 /* Absolute to percentage */
1012 spw->set_data ("update", GUINT_TO_POINTER (TRUE));
1014 Gtk::Adjustment *a = static_cast<Gtk::Adjustment *>(spw->get_data("width"));
1015 float w = sp_units_get_pixels (a->get_value(), *old);
1017 gdouble average = stroke_average_width (objects);
1019 if (average == NR_HUGE || average == 0)
1020 return FALSE;
1022 a->set_value (100.0 * w / average);
1024 spw->set_data ("update", GUINT_TO_POINTER (FALSE));
1025 return TRUE;
1027 } else if ((old->base == SP_UNIT_DIMENSIONLESS) &&
1028 (new_units->base == SP_UNIT_ABSOLUTE || new_units->base == SP_UNIT_DEVICE)) {
1030 /* Percentage to absolute */
1031 spw->set_data ("update", GUINT_TO_POINTER (TRUE));
1033 Gtk::Adjustment *a = static_cast<Gtk::Adjustment *>(spw->get_data ("width"));
1035 gdouble average = stroke_average_width (objects);
1037 a->set_value (sp_pixels_get_units (0.01 * a->get_value() * average, *new_units));
1039 spw->set_data ("update", GUINT_TO_POINTER (FALSE));
1040 return TRUE;
1041 }
1043 return FALSE;
1044 }
1047 /**
1048 * \brief Creates a new widget for the line stroke style.
1049 *
1050 */
1051 Gtk::Container *
1052 sp_stroke_style_line_widget_new(void)
1053 {
1054 Gtk::Widget *us;
1055 SPDashSelector *ds;
1056 GtkWidget *us_old, *spw_old;
1057 Gtk::Container *spw;
1058 Gtk::Table *t;
1059 Gtk::Adjustment *a;
1060 Gtk::SpinButton *sb;
1061 Gtk::RadioButton *tb;
1062 Gtk::HBox *f, *hb;
1064 Gtk::Tooltips *tt = new Gtk::Tooltips();
1066 spw_old = sp_widget_new_global(INKSCAPE);
1067 spw = dynamic_cast<Gtk::Container *>(manage(Glib::wrap(spw_old)));
1069 f = new Gtk::HBox(false, 0);
1070 f->show();
1071 spw->add(*f);
1073 t = new Gtk::Table(3, 6, false);
1074 t->show();
1075 t->set_border_width(4);
1076 t->set_row_spacings(4);
1077 f->add(*t);
1078 spw->set_data("stroke", t);
1080 gint i = 0;
1082 /* Stroke width */
1083 spw_label(t, _("Width:"), 0, i);
1085 hb = spw_hbox(t, 3, 1, i);
1087 // TODO: when this is gtkmmified, use an Inkscape::UI::Widget::ScalarUnit instead of the separate
1088 // spinbutton and unit selector for stroke width. In sp_stroke_style_line_update, use
1089 // setHundredPercent to remember the aeraged width corresponding to 100%. Then the
1090 // stroke_width_set_unit will be removed (because ScalarUnit takes care of conversions itself), and
1091 // with it, the two remaining calls of stroke_average_width, allowing us to get rid of that
1092 // function in desktop-style.
1094 a = new Gtk::Adjustment(1.0, 0.0, 1000.0, 0.1, 10.0, 10.0);
1095 spw->set_data("width", a);
1096 sb = new Gtk::SpinButton(*a, 0.1, 3);
1097 tt->set_tip(*sb, _("Stroke width"));
1098 sb->show();
1100 sp_dialog_defocus_on_enter_cpp(sb);
1102 hb->pack_start(*sb, false, false, 0);
1103 us_old = sp_unit_selector_new(SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE);
1104 us = manage(Glib::wrap(us_old));
1105 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1106 if (desktop)
1107 sp_unit_selector_set_unit (SP_UNIT_SELECTOR(us_old), sp_desktop_namedview(desktop)->doc_units);
1108 sp_unit_selector_add_unit(SP_UNIT_SELECTOR(us_old), &sp_unit_get_by_id(SP_UNIT_PERCENT), 0);
1109 g_signal_connect ( G_OBJECT (us_old), "set_unit", G_CALLBACK (stroke_width_set_unit), spw );
1110 us->show();
1111 sp_unit_selector_add_adjustment( SP_UNIT_SELECTOR(us_old), GTK_ADJUSTMENT(a->gobj()) );
1112 hb->pack_start(*us, FALSE, FALSE, 0);
1113 spw->set_data("units", us_old);
1115 a->signal_value_changed().connect(sigc::bind(sigc::ptr_fun(&sp_stroke_style_width_changed), spw));
1116 i++;
1118 /* Join type */
1119 // TRANSLATORS: The line join style specifies the shape to be used at the
1120 // corners of paths. It can be "miter", "round" or "bevel".
1121 spw_label(t, _("Join:"), 0, i);
1123 hb = spw_hbox(t, 3, 1, i);
1125 tb = NULL;
1127 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_MITER,
1128 hb, spw, "join", "miter");
1130 // TRANSLATORS: Miter join: joining lines with a sharp (pointed) corner.
1131 // For an example, draw a triangle with a large stroke width and modify the
1132 // "Join" option (in the Fill and Stroke dialog).
1133 tt->set_tip(*tb, _("Miter join"));
1135 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_ROUND,
1136 hb, spw, "join", "round");
1138 // TRANSLATORS: Round join: joining lines with a rounded corner.
1139 // For an example, draw a triangle with a large stroke width and modify the
1140 // "Join" option (in the Fill and Stroke dialog).
1141 tt->set_tip(*tb, _("Round join"));
1143 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_BEVEL,
1144 hb, spw, "join", "bevel");
1146 // TRANSLATORS: Bevel join: joining lines with a blunted (flattened) corner.
1147 // For an example, draw a triangle with a large stroke width and modify the
1148 // "Join" option (in the Fill and Stroke dialog).
1149 tt->set_tip(*tb, _("Bevel join"));
1151 i++;
1153 /* Miterlimit */
1154 // TRANSLATORS: Miter limit: only for "miter join", this limits the length
1155 // of the sharp "spike" when the lines connect at too sharp an angle.
1156 // When two line segments meet at a sharp angle, a miter join results in a
1157 // spike that extends well beyond the connection point. The purpose of the
1158 // miter limit is to cut off such spikes (i.e. convert them into bevels)
1159 // when they become too long.
1160 spw_label(t, _("Miter limit:"), 0, i);
1162 hb = spw_hbox(t, 3, 1, i);
1164 a = new Gtk::Adjustment(4.0, 0.0, 100.0, 0.1, 10.0, 10.0);
1165 spw->set_data("miterlimit", a);
1167 sb = new Gtk::SpinButton(*a, 0.1, 2);
1168 tt->set_tip(*sb, _("Maximum length of the miter (in units of stroke width)"));
1169 sb->show();
1170 spw->set_data("miterlimit_sb", sb);
1171 sp_dialog_defocus_on_enter_cpp(sb);
1173 hb->pack_start(*sb, false, false, 0);
1175 a->signal_value_changed().connect(sigc::bind(sigc::ptr_fun(&sp_stroke_style_miterlimit_changed), spw));
1176 i++;
1178 /* Cap type */
1179 // TRANSLATORS: cap type specifies the shape for the ends of lines
1180 spw_label(t, _("Cap:"), 0, i);
1182 hb = spw_hbox(t, 3, 1, i);
1184 tb = NULL;
1186 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_BUTT,
1187 hb, spw, "cap", "butt");
1189 // TRANSLATORS: Butt cap: the line shape does not extend beyond the end point
1190 // of the line; the ends of the line are square
1191 tt->set_tip(*tb, _("Butt cap"));
1193 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_ROUND,
1194 hb, spw, "cap", "round");
1196 // TRANSLATORS: Round cap: the line shape extends beyond the end point of the
1197 // line; the ends of the line are rounded
1198 tt->set_tip(*tb, _("Round cap"));
1200 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_SQUARE,
1201 hb, spw, "cap", "square");
1203 // TRANSLATORS: Square cap: the line shape extends beyond the end point of the
1204 // line; the ends of the line are square
1205 tt->set_tip(*tb, _("Square cap"));
1207 i++;
1210 /* Dash */
1211 spw_label(t, _("Dashes:"), 0, i);
1212 ds = manage(new SPDashSelector(inkscape_get_repr(INKSCAPE, "palette.dashes")));
1214 ds->show();
1215 t->attach(*ds, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
1216 spw->set_data("dash", ds);
1217 ds->changed_signal.connect(sigc::bind(sigc::ptr_fun(&sp_stroke_style_line_dash_changed), spw));
1218 i++;
1220 /* Drop down marker selectors*/
1221 // TODO: this code can be shortened by iterating over the possible menus!
1223 // doing this here once, instead of for each preview, to speed things up
1224 SPDocument *sandbox = ink_markers_preview_doc ();
1226 // TRANSLATORS: Path markers are an SVG feature that allows you to attach arbitrary shapes
1227 // (arrowheads, bullets, faces, whatever) to the start, end, or middle nodes of a path.
1228 spw_label(t, _("Start Markers:"), 0, i);
1229 marker_start_menu = ink_marker_menu(spw ,"marker-start", sandbox);
1230 marker_start_menu_connection = marker_start_menu->signal_changed().connect(
1231 sigc::bind<Gtk::OptionMenu *, Gtk::Container *, SPMarkerLoc>(
1232 sigc::ptr_fun(&sp_marker_select), marker_start_menu, spw, SP_MARKER_LOC_START));
1233 marker_start_menu->show();
1234 t->attach(*marker_start_menu, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
1235 spw->set_data("start_mark_menu", marker_start_menu);
1237 i++;
1238 spw_label(t, _("Mid Markers:"), 0, i);
1239 marker_mid_menu = ink_marker_menu(spw ,"marker-mid", sandbox);
1240 marker_mid_menu_connection = marker_mid_menu->signal_changed().connect(
1241 sigc::bind<Gtk::OptionMenu *, Gtk::Container *, SPMarkerLoc>(
1242 sigc::ptr_fun(&sp_marker_select), marker_mid_menu,spw, SP_MARKER_LOC_MID));
1243 marker_mid_menu->show();
1244 t->attach(*marker_mid_menu, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
1245 spw->set_data("mid_mark_menu", marker_mid_menu);
1247 i++;
1248 spw_label(t, _("End Markers:"), 0, i);
1249 marker_end_menu = ink_marker_menu(spw ,"marker-end", sandbox);
1250 marker_end_menu_connection = marker_end_menu->signal_changed().connect(
1251 sigc::bind<Gtk::OptionMenu *, Gtk::Container *, SPMarkerLoc>(
1252 sigc::ptr_fun(&sp_marker_select), marker_end_menu, spw, SP_MARKER_LOC_END));
1253 marker_end_menu->show();
1254 t->attach(*marker_end_menu, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
1255 spw->set_data("end_mark_menu", marker_end_menu);
1257 i++;
1259 // FIXME: we cheat and still use gtk+ signals
1261 gtk_signal_connect(GTK_OBJECT(spw_old), "modify_selection",
1262 GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_modified),
1263 spw);
1264 gtk_signal_connect(GTK_OBJECT(spw_old), "change_selection",
1265 GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_changed),
1266 spw);
1268 sp_stroke_style_line_update(spw, desktop ? sp_desktop_selection(desktop) : NULL);
1270 return spw;
1271 }
1273 /**
1274 * Callback for when stroke style widget is modified.
1275 * Triggers update action.
1276 */
1277 static void
1278 sp_stroke_style_line_selection_modified(SPWidget *,
1279 Inkscape::Selection *selection,
1280 guint flags,
1281 gpointer data)
1282 {
1283 Gtk::Container *spw = static_cast<Gtk::Container *>(data);
1284 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1285 sp_stroke_style_line_update(spw, selection);
1286 }
1288 }
1290 /**
1291 * Callback for when stroke style widget is changed.
1292 * Triggers update action.
1293 */
1294 static void
1295 sp_stroke_style_line_selection_changed(SPWidget *,
1296 Inkscape::Selection *selection,
1297 gpointer data)
1298 {
1299 Gtk::Container *spw = static_cast<Gtk::Container *>(data);
1300 sp_stroke_style_line_update(spw, selection);
1301 }
1303 /**
1304 * Sets selector widgets' dash style from an SPStyle object.
1305 */
1306 static void
1307 sp_dash_selector_set_from_style(SPDashSelector *dsel, SPStyle *style)
1308 {
1309 if (style->stroke_dash.n_dash > 0) {
1310 double d[64];
1311 int len = MIN(style->stroke_dash.n_dash, 64);
1312 for (int i = 0; i < len; i++) {
1313 if (style->stroke_width.computed != 0)
1314 d[i] = style->stroke_dash.dash[i] / style->stroke_width.computed;
1315 else
1316 d[i] = style->stroke_dash.dash[i]; // is there a better thing to do for stroke_width==0?
1317 }
1318 dsel->set_dash(len, d, style->stroke_width.computed != 0 ?
1319 style->stroke_dash.offset / style->stroke_width.computed :
1320 style->stroke_dash.offset);
1321 } else {
1322 dsel->set_dash(0, NULL, 0.0);
1323 }
1324 }
1326 /**
1327 * Sets the join type for a line, and updates the stroke style widget's buttons
1328 */
1329 static void
1330 sp_jointype_set (Gtk::Container *spw, unsigned const jointype)
1331 {
1332 Gtk::RadioButton *tb = NULL;
1333 switch (jointype) {
1334 case SP_STROKE_LINEJOIN_MITER:
1335 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_JOIN_MITER));
1336 break;
1337 case SP_STROKE_LINEJOIN_ROUND:
1338 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_JOIN_ROUND));
1339 break;
1340 case SP_STROKE_LINEJOIN_BEVEL:
1341 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_JOIN_BEVEL));
1342 break;
1343 default:
1344 break;
1345 }
1346 sp_stroke_style_set_join_buttons(spw, tb);
1347 }
1349 /**
1350 * Sets the cap type for a line, and updates the stroke style widget's buttons
1351 */
1352 static void
1353 sp_captype_set (Gtk::Container *spw, unsigned const captype)
1354 {
1355 Gtk::RadioButton *tb = NULL;
1356 switch (captype) {
1357 case SP_STROKE_LINECAP_BUTT:
1358 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_CAP_BUTT));
1359 break;
1360 case SP_STROKE_LINECAP_ROUND:
1361 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_CAP_ROUND));
1362 break;
1363 case SP_STROKE_LINECAP_SQUARE:
1364 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_CAP_SQUARE));
1365 break;
1366 default:
1367 break;
1368 }
1369 sp_stroke_style_set_cap_buttons(spw, tb);
1370 }
1372 /**
1373 * Callback for when stroke style widget is updated, including markers, cap type,
1374 * join type, etc.
1375 */
1376 static void
1377 sp_stroke_style_line_update(Gtk::Container *spw, Inkscape::Selection *sel)
1378 {
1379 if (spw->get_data("update")) {
1380 return;
1381 }
1383 spw->set_data("update", GINT_TO_POINTER(TRUE));
1385 Gtk::Table *sset = static_cast<Gtk::Table *>(spw->get_data("stroke"));
1386 Gtk::Adjustment *width = static_cast<Gtk::Adjustment *>(spw->get_data("width"));
1387 Gtk::Adjustment *ml = static_cast<Gtk::Adjustment *>(spw->get_data("miterlimit"));
1388 SPUnitSelector *us = SP_UNIT_SELECTOR(spw->get_data("units"));
1389 SPDashSelector *dsel = static_cast<SPDashSelector *>(spw->get_data("dash"));
1391 // create temporary style
1392 SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
1393 // query into it
1394 int result_sw = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1395 int result_ml = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1396 int result_cap = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKECAP);
1397 int result_join = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1399 if (result_sw == QUERY_STYLE_NOTHING) {
1400 /* No objects stroked, set insensitive */
1401 sset->set_sensitive(false);
1403 spw->set_data("update", GINT_TO_POINTER(FALSE));
1404 return;
1405 } else {
1406 sset->set_sensitive(true);
1408 SPUnit const *unit = sp_unit_selector_get_unit(us);
1410 if (result_sw == QUERY_STYLE_MULTIPLE_AVERAGED) {
1411 sp_unit_selector_set_unit(us, &sp_unit_get_by_id(SP_UNIT_PERCENT));
1412 } else {
1413 // same width, or only one object; no sense to keep percent, switch to absolute
1414 if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1415 sp_unit_selector_set_unit(us, sp_desktop_namedview(SP_ACTIVE_DESKTOP)->doc_units);
1416 }
1417 }
1419 unit = sp_unit_selector_get_unit(us);
1421 if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1422 double avgwidth = sp_pixels_get_units (query->stroke_width.computed, *unit);
1423 width->set_value(avgwidth);
1424 } else {
1425 width->set_value(100);
1426 }
1427 }
1429 if (result_ml != QUERY_STYLE_NOTHING)
1430 ml->set_value(query->stroke_miterlimit.value); // TODO: reflect averagedness?
1432 if (result_join != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1433 sp_jointype_set(spw, query->stroke_linejoin.value);
1434 } else {
1435 sp_stroke_style_set_join_buttons(spw, NULL);
1436 }
1438 if (result_cap != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1439 sp_captype_set (spw, query->stroke_linecap.value);
1440 } else {
1441 sp_stroke_style_set_cap_buttons(spw, NULL);
1442 }
1444 sp_style_unref(query);
1446 if (!sel || sel->isEmpty())
1447 return;
1449 GSList const *objects = sel->itemList();
1450 SPObject * const object = SP_OBJECT(objects->data);
1451 SPStyle * const style = SP_OBJECT_STYLE(object);
1453 /* Markers */
1454 sp_stroke_style_update_marker_menus(spw, objects); // FIXME: make this desktop query too
1456 /* Dash */
1457 sp_dash_selector_set_from_style(dsel, style); // FIXME: make this desktop query too
1459 sset->set_sensitive(true);
1461 spw->set_data("update", GINT_TO_POINTER(FALSE));
1462 }
1464 /**
1465 * Sets a line's dash properties in a CSS style object.
1466 */
1467 static void
1468 sp_stroke_style_set_scaled_dash(SPCSSAttr *css,
1469 int ndash, double *dash, double offset,
1470 double scale)
1471 {
1472 if (ndash > 0) {
1473 Inkscape::CSSOStringStream osarray;
1474 for (int i = 0; i < ndash; i++) {
1475 osarray << dash[i] * scale;
1476 if (i < (ndash - 1)) {
1477 osarray << ",";
1478 }
1479 }
1480 sp_repr_css_set_property(css, "stroke-dasharray", osarray.str().c_str());
1482 Inkscape::CSSOStringStream osoffset;
1483 osoffset << offset * scale;
1484 sp_repr_css_set_property(css, "stroke-dashoffset", osoffset.str().c_str());
1485 } else {
1486 sp_repr_css_set_property(css, "stroke-dasharray", "none");
1487 sp_repr_css_set_property(css, "stroke-dashoffset", NULL);
1488 }
1489 }
1491 /**
1492 * Sets line properties like width, dashes, markers, etc. on all currently selected items.
1493 */
1494 static void
1495 sp_stroke_style_scale_line(Gtk::Container *spw)
1496 {
1497 if (spw->get_data("update")) {
1498 return;
1499 }
1501 spw->set_data("update", GINT_TO_POINTER(TRUE));
1503 Gtk::Adjustment *wadj = static_cast<Gtk::Adjustment *>(spw->get_data("width"));
1504 SPUnitSelector *us = SP_UNIT_SELECTOR(spw->get_data("units"));
1505 SPDashSelector *dsel = static_cast<SPDashSelector *>(spw->get_data("dash"));
1506 Gtk::Adjustment *ml = static_cast<Gtk::Adjustment *>(spw->get_data("miterlimit"));
1508 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1509 SPDocument *document = sp_desktop_document (desktop);
1510 Inkscape::Selection *selection = sp_desktop_selection (desktop);
1512 GSList const *items = selection->itemList();
1514 /* TODO: Create some standardized method */
1515 SPCSSAttr *css = sp_repr_css_attr_new();
1517 if (items) {
1519 double width_typed = wadj->get_value();
1520 double const miterlimit = ml->get_value();
1522 SPUnit const *const unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1524 double *dash, offset;
1525 int ndash;
1526 dsel->get_dash(&ndash, &dash, &offset);
1528 for (GSList const *i = items; i != NULL; i = i->next) {
1529 /* Set stroke width */
1530 double width;
1531 if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1532 width = sp_units_get_pixels (width_typed, *unit);
1533 } else { // percentage
1534 gdouble old_w = SP_OBJECT_STYLE (i->data)->stroke_width.computed;
1535 width = old_w * width_typed / 100;
1536 }
1538 {
1539 Inkscape::CSSOStringStream os_width;
1540 os_width << width;
1541 sp_repr_css_set_property(css, "stroke-width", os_width.str().c_str());
1542 }
1544 {
1545 Inkscape::CSSOStringStream os_ml;
1546 os_ml << miterlimit;
1547 sp_repr_css_set_property(css, "stroke-miterlimit", os_ml.str().c_str());
1548 }
1550 /* Set dash */
1551 sp_stroke_style_set_scaled_dash(css, ndash, dash, offset, width);
1553 sp_desktop_apply_css_recursive (SP_OBJECT(i->data), css, true);
1554 }
1556 g_free(dash);
1558 if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1559 // reset to 100 percent
1560 wadj->set_value(100.0);
1561 }
1563 }
1565 // we have already changed the items, so set style without changing selection
1566 // FIXME: move the above stroke-setting stuff, including percentages, to desktop-style
1567 sp_desktop_set_style (desktop, css, false);
1569 sp_repr_css_attr_unref(css);
1571 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
1572 _("Set stroke style"));
1574 spw->set_data("update", GINT_TO_POINTER(FALSE));
1575 }
1577 /**
1578 * Callback for when the stroke style's width changes.
1579 * Causes all line styles to be applied to all selected items.
1580 */
1581 static void
1582 sp_stroke_style_width_changed(Gtk::Container *spw)
1583 {
1584 if (spw->get_data("update")) {
1585 return;
1586 }
1588 sp_stroke_style_scale_line(spw);
1589 }
1591 /**
1592 * Callback for when the stroke style's miterlimit changes.
1593 * Causes all line styles to be applied to all selected items.
1594 */
1595 static void
1596 sp_stroke_style_miterlimit_changed(Gtk::Container *spw)
1597 {
1598 if (spw->get_data("update")) {
1599 return;
1600 }
1602 sp_stroke_style_scale_line(spw);
1603 }
1605 /**
1606 * Callback for when the stroke style's dash changes.
1607 * Causes all line styles to be applied to all selected items.
1608 */
1610 static void
1611 sp_stroke_style_line_dash_changed(Gtk::Container *spw)
1612 {
1613 if (spw->get_data("update")) {
1614 return;
1615 }
1617 sp_stroke_style_scale_line(spw);
1618 }
1620 /**
1621 * \brief This routine handles toggle events for buttons in the stroke style
1622 * dialog.
1623 * When activated, this routine gets the data for the various widgets, and then
1624 * calls the respective routines to update css properties, etc.
1625 *
1626 */
1627 static void
1628 sp_stroke_style_any_toggled(Gtk::ToggleButton *tb, Gtk::Container *spw)
1629 {
1630 if (spw->get_data("update")) {
1631 return;
1632 }
1634 if (tb->get_active()) {
1636 gchar const *join
1637 = static_cast<gchar const *>(tb->get_data("join"));
1638 gchar const *cap
1639 = static_cast<gchar const *>(tb->get_data("cap"));
1641 if (join) {
1642 Gtk::SpinButton *ml = static_cast<Gtk::SpinButton *>(spw->get_data("miterlimit_sb"));
1643 ml->set_sensitive(!strcmp(join, "miter"));
1644 }
1646 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1648 /* TODO: Create some standardized method */
1649 SPCSSAttr *css = sp_repr_css_attr_new();
1651 if (join) {
1652 sp_repr_css_set_property(css, "stroke-linejoin", join);
1654 sp_desktop_set_style (desktop, css);
1656 sp_stroke_style_set_join_buttons(spw, tb);
1657 } else if (cap) {
1658 sp_repr_css_set_property(css, "stroke-linecap", cap);
1660 sp_desktop_set_style (desktop, css);
1662 sp_stroke_style_set_cap_buttons(spw, tb);
1663 }
1665 sp_repr_css_attr_unref(css);
1667 sp_document_done(sp_desktop_document(desktop), SP_VERB_DIALOG_FILL_STROKE,
1668 _("Set stroke style"));
1669 }
1670 }
1672 /**
1673 * Updates the join style toggle buttons
1674 */
1675 static void
1676 sp_stroke_style_set_join_buttons(Gtk::Container *spw, Gtk::ToggleButton *active)
1677 {
1678 Gtk::RadioButton *tb;
1680 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_JOIN_MITER));
1681 tb->set_active(active == tb);
1683 Gtk::SpinButton *ml = static_cast<Gtk::SpinButton *>(spw->get_data("miterlimit_sb"));
1684 ml->set_sensitive(active == tb);
1686 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_JOIN_ROUND));
1687 tb->set_active(active == tb);
1689 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_JOIN_BEVEL));
1690 tb->set_active(active == tb);
1691 }
1693 /**
1694 * Updates the cap style toggle buttons
1695 */
1696 static void
1697 sp_stroke_style_set_cap_buttons(Gtk::Container *spw, Gtk::ToggleButton *active)
1698 {
1699 Gtk::RadioButton *tb;
1701 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_CAP_BUTT));
1702 tb->set_active(active == tb);
1703 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_CAP_ROUND));
1704 tb->set_active(active == tb);
1705 tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_STOCK_CAP_SQUARE));
1706 tb->set_active(active == tb);
1707 }
1709 /**
1710 * Sets the current marker in the marker menu.
1711 */
1712 static void
1713 ink_marker_menu_set_current(SPObject *marker, Gtk::OptionMenu *mnu)
1714 {
1715 mnu->set_data("update", GINT_TO_POINTER(TRUE));
1717 Gtk::Menu *m = mnu->get_menu();
1718 if (marker != NULL) {
1719 bool mark_is_stock = false;
1720 if (SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"))
1721 mark_is_stock = true;
1723 gchar *markname;
1724 if (mark_is_stock)
1725 markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"));
1726 else
1727 markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("id"));
1729 int markpos = ink_marker_menu_get_pos(m, markname);
1730 mnu->set_history(markpos);
1732 g_free (markname);
1733 }
1734 else {
1735 mnu->set_history(0);
1736 }
1737 mnu->set_data("update", GINT_TO_POINTER(FALSE));
1738 }
1740 /**
1741 * Updates the marker menus to highlight the appropriate marker and scroll to
1742 * that marker.
1743 */
1744 static void
1745 sp_stroke_style_update_marker_menus(Gtk::Container *spw, GSList const *objects)
1746 {
1747 struct { char const *key; int loc; } const keyloc[] = {
1748 { "start_mark_menu", SP_MARKER_LOC_START },
1749 { "mid_mark_menu", SP_MARKER_LOC_MID },
1750 { "end_mark_menu", SP_MARKER_LOC_END }
1751 };
1753 bool all_texts = true;
1754 for (GSList *i = (GSList *) objects; i != NULL; i = i->next) {
1755 if (!SP_IS_TEXT (i->data)) {
1756 all_texts = false;
1757 }
1758 }
1760 for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1761 Gtk::OptionMenu *mnu = static_cast<Gtk::OptionMenu *>(spw->get_data(keyloc[i].key));
1762 // Per SVG spec, text objects cannot have markers; disable menus if only texts are selected
1763 mnu->set_sensitive(!all_texts);
1764 }
1766 // We show markers of the first object in the list only
1767 // FIXME: use the first in the list that has the marker of each type, if any
1768 SPObject *object = SP_OBJECT(objects->data);
1770 for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1771 // For all three marker types,
1773 // find the corresponding menu
1774 Gtk::OptionMenu *mnu = static_cast<Gtk::OptionMenu *>(spw->get_data(keyloc[i].key));
1776 // Quit if we're in update state
1777 if (mnu->get_data("update")) {
1778 return;
1779 }
1781 if (object->style->marker[keyloc[i].loc].value != NULL && !all_texts) {
1782 // If the object has this type of markers,
1784 // Extract the name of the marker that the object uses
1785 SPObject *marker = ink_extract_marker_name(object->style->marker[keyloc[i].loc].value, SP_OBJECT_DOCUMENT(object));
1786 // Scroll the menu to that marker
1787 ink_marker_menu_set_current(marker, mnu);
1789 } else {
1790 mnu->set_history(0);
1791 }
1792 }
1793 }
1796 /**
1797 * Extract the actual name of the link
1798 * e.g. get mTriangle from url(#mTriangle).
1799 * \return Buffer containing the actual name, allocated from GLib;
1800 * the caller should free the buffer when they no longer need it.
1801 */
1802 static SPObject*
1803 ink_extract_marker_name(gchar const *n, SPDocument *doc)
1804 {
1805 gchar const *p = n;
1806 while (*p != '\0' && *p != '#') {
1807 p++;
1808 }
1810 if (*p == '\0' || p[1] == '\0') {
1811 return NULL;
1812 }
1814 p++;
1815 int c = 0;
1816 while (p[c] != '\0' && p[c] != ')') {
1817 c++;
1818 }
1820 if (p[c] == '\0') {
1821 return NULL;
1822 }
1824 gchar* b = g_strdup(p);
1825 b[c] = '\0';
1827 // FIXME: get the document from the object and let the caller pass it in
1828 SPObject *marker = doc->getObjectById(b);
1829 return marker;
1830 }
1833 /*
1834 Local Variables:
1835 mode:c++
1836 c-file-style:"stroustrup"
1837 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1838 indent-tabs-mode:nil
1839 fill-column:99
1840 End:
1841 */
1842 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :