0d22a90f7647ce3df824decbcf551ad40ffad750
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 *
11 * Copyright (C) 2001-2005 authors
12 * Copyright (C) 2001 Ximian, Inc.
13 * Copyright (C) 2004 John Cliff
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #define noSP_SS_VERBOSE
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
26 #include <glib/gmem.h>
27 #include <gtk/gtk.h>
29 #include <glibmm/i18n.h>
30 #include "helper/unit-menu.h"
31 #include "helper/units.h"
32 #include "svg/css-ostringstream.h"
33 #include "widgets/sp-widget.h"
34 #include "widgets/spw-utilities.h"
35 #include "sp-linear-gradient.h"
36 #include "sp-radial-gradient.h"
37 #include "marker.h"
38 #include "sp-pattern.h"
39 #include "widgets/paint-selector.h"
40 #include "widgets/dash-selector.h"
41 #include "style.h"
42 #include "gradient-chemistry.h"
43 #include "sp-namedview.h"
44 #include "desktop-handles.h"
45 #include "desktop-style.h"
46 #include "selection.h"
47 #include "inkscape.h"
48 #include "inkscape-stock.h"
49 #include "dialogs/dialog-events.h"
50 #include "sp-text.h"
51 #include "sp-rect.h"
52 #include "document-private.h"
53 #include "display/nr-arena.h"
54 #include "display/nr-arena-item.h"
55 #include "path-prefix.h"
56 #include "widgets/icon.h"
57 #include "helper/stock-items.h"
58 #include "io/sys.h"
59 #include "ui/cache/svg_preview_cache.h"
61 #include "dialogs/stroke-style.h"
64 /* Paint */
66 static void sp_stroke_style_paint_construct(SPWidget *spw, SPPaintSelector *psel);
67 static void sp_stroke_style_paint_selection_modified (SPWidget *spw, Inkscape::Selection *selection, guint flags, SPPaintSelector *psel);
68 static void sp_stroke_style_paint_selection_changed (SPWidget *spw, Inkscape::Selection *selection, SPPaintSelector *psel);
69 static void sp_stroke_style_paint_update(SPWidget *spw);
71 static void sp_stroke_style_paint_mode_changed(SPPaintSelector *psel, SPPaintSelectorMode mode, SPWidget *spw);
72 static void sp_stroke_style_paint_dragged(SPPaintSelector *psel, SPWidget *spw);
73 static void sp_stroke_style_paint_changed(SPPaintSelector *psel, SPWidget *spw);
75 static void sp_stroke_style_widget_change_subselection ( Inkscape::Application *inkscape, SPDesktop *desktop, SPWidget *spw );
76 static void sp_stroke_style_widget_transientize_callback(Inkscape::Application *inkscape,
77 SPDesktop *desktop,
78 SPWidget *spw );
80 /** Marker selection option menus */
81 static GtkWidget * marker_start_menu = NULL;
82 static GtkWidget * marker_mid_menu = NULL;
83 static GtkWidget * marker_end_menu = NULL;
85 static SPObject *ink_extract_marker_name(gchar const *n);
86 static void ink_markers_menu_update();
88 static Inkscape::UI::Cache::SvgPreview svg_preview_cache;
90 /**
91 * Create the stroke style widget, and hook up all the signals.
92 */
93 GtkWidget *
94 sp_stroke_style_paint_widget_new(void)
95 {
96 GtkWidget *spw, *psel;
98 spw = sp_widget_new_global(INKSCAPE);
100 psel = sp_paint_selector_new(false); // without fillrule selector
101 gtk_widget_show(psel);
102 gtk_container_add(GTK_CONTAINER(spw), psel);
103 gtk_object_set_data(GTK_OBJECT(spw), "paint-selector", psel);
105 gtk_signal_connect(GTK_OBJECT(spw), "construct",
106 GTK_SIGNAL_FUNC(sp_stroke_style_paint_construct),
107 psel);
108 gtk_signal_connect(GTK_OBJECT(spw), "modify_selection",
109 GTK_SIGNAL_FUNC(sp_stroke_style_paint_selection_modified),
110 psel);
111 gtk_signal_connect(GTK_OBJECT(spw), "change_selection",
112 GTK_SIGNAL_FUNC(sp_stroke_style_paint_selection_changed),
113 psel);
115 g_signal_connect (INKSCAPE, "change_subselection", G_CALLBACK (sp_stroke_style_widget_change_subselection), spw);
117 g_signal_connect (G_OBJECT(INKSCAPE), "activate_desktop", G_CALLBACK (sp_stroke_style_widget_transientize_callback), spw );
119 gtk_signal_connect(GTK_OBJECT(psel), "mode_changed",
120 GTK_SIGNAL_FUNC(sp_stroke_style_paint_mode_changed),
121 spw);
122 gtk_signal_connect(GTK_OBJECT(psel), "dragged",
123 GTK_SIGNAL_FUNC(sp_stroke_style_paint_dragged),
124 spw);
125 gtk_signal_connect(GTK_OBJECT(psel), "changed",
126 GTK_SIGNAL_FUNC(sp_stroke_style_paint_changed),
127 spw);
129 sp_stroke_style_paint_update (SP_WIDGET(spw));
130 return spw;
131 }
133 /**
134 * On construction, simply does an update of the stroke style paint object.
135 */
136 static void
137 sp_stroke_style_paint_construct(SPWidget *spw, SPPaintSelector *psel)
138 {
139 #ifdef SP_SS_VERBOSE
140 g_print( "Stroke style widget constructed: inkscape %p repr %p\n",
141 spw->inkscape, spw->repr );
142 #endif
143 if (spw->inkscape) {
144 sp_stroke_style_paint_update (spw);
145 }
146 }
148 /**
149 * On signal modified, invokes an update of the stroke style paint object.
150 */
151 static void
152 sp_stroke_style_paint_selection_modified ( SPWidget *spw,
153 Inkscape::Selection *selection,
154 guint flags,
155 SPPaintSelector *psel)
156 {
157 if (flags & ( SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG |
158 SP_OBJECT_STYLE_MODIFIED_FLAG) ) {
159 sp_stroke_style_paint_update(spw);
160 }
161 }
164 /**
165 * On signal selection changed, invokes an update of the stroke style paint object.
166 */
167 static void
168 sp_stroke_style_paint_selection_changed ( SPWidget *spw,
169 Inkscape::Selection *selection,
170 SPPaintSelector *psel )
171 {
172 sp_stroke_style_paint_update (spw);
173 }
176 /**
177 * On signal change subselection, invoke an update of the stroke style widget.
178 */
179 static void
180 sp_stroke_style_widget_change_subselection ( Inkscape::Application *inkscape,
181 SPDesktop *desktop,
182 SPWidget *spw )
183 {
184 sp_stroke_style_paint_update (spw);
185 }
187 static void
188 sp_stroke_style_widget_transientize_callback(Inkscape::Application *inkscape,
189 SPDesktop *desktop,
190 SPWidget *spw )
191 {
192 ink_markers_menu_update();
193 }
195 /**
196 * Gets the active stroke style property, then sets the appropriate color, alpha, gradient,
197 * pattern, etc. for the paint-selector.
198 */
199 static void
200 sp_stroke_style_paint_update (SPWidget *spw)
201 {
202 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
203 return;
204 }
206 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
208 SPPaintSelector *psel = SP_PAINT_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "paint-selector"));
210 // create temporary style
211 SPStyle *query = sp_style_new ();
212 // query into it
213 int result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKE);
215 switch (result) {
216 case QUERY_STYLE_NOTHING:
217 {
218 /* No paint at all */
219 sp_paint_selector_set_mode (psel, SP_PAINT_SELECTOR_MODE_EMPTY);
220 break;
221 }
223 case QUERY_STYLE_SINGLE:
224 case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently, e.g. display "averaged" somewhere in paint selector
225 case QUERY_STYLE_MULTIPLE_SAME:
226 {
227 SPPaintSelectorMode pselmode = sp_style_determine_paint_selector_mode (query, false);
228 sp_paint_selector_set_mode (psel, pselmode);
230 if (query->stroke.set && query->stroke.type == SP_PAINT_TYPE_COLOR) {
231 gfloat d[3];
232 sp_color_get_rgb_floatv (&query->stroke.value.color, d);
233 SPColor color;
234 sp_color_set_rgb_float (&color, d[0], d[1], d[2]);
235 sp_paint_selector_set_color_alpha (psel, &color, SP_SCALE24_TO_FLOAT (query->stroke_opacity.value));
237 } else if (query->stroke.set && query->stroke.type == SP_PAINT_TYPE_PAINTSERVER) {
239 SPPaintServer *server = SP_STYLE_STROKE_SERVER (query);
241 if (SP_IS_LINEARGRADIENT (server)) {
242 SPGradient *vector = sp_gradient_get_vector (SP_GRADIENT (server), FALSE);
243 sp_paint_selector_set_gradient_linear (psel, vector);
245 SPLinearGradient *lg = SP_LINEARGRADIENT (server);
246 sp_paint_selector_set_gradient_properties (psel,
247 SP_GRADIENT_UNITS (lg),
248 SP_GRADIENT_SPREAD (lg));
249 } else if (SP_IS_RADIALGRADIENT (server)) {
250 SPGradient *vector = sp_gradient_get_vector (SP_GRADIENT (server), FALSE);
251 sp_paint_selector_set_gradient_radial (psel, vector);
253 SPRadialGradient *rg = SP_RADIALGRADIENT (server);
254 sp_paint_selector_set_gradient_properties (psel,
255 SP_GRADIENT_UNITS (rg),
256 SP_GRADIENT_SPREAD (rg));
257 } else if (SP_IS_PATTERN (server)) {
258 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
259 sp_update_pattern_list (psel, pat);
260 }
261 }
262 break;
263 }
265 case QUERY_STYLE_MULTIPLE_DIFFERENT:
266 {
267 sp_paint_selector_set_mode (psel, SP_PAINT_SELECTOR_MODE_MULTIPLE);
268 break;
269 }
270 }
272 g_free (query);
274 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
275 }
277 /**
278 * When the mode is changed, invoke a regular changed handler.
279 */
280 static void
281 sp_stroke_style_paint_mode_changed( SPPaintSelector *psel,
282 SPPaintSelectorMode mode,
283 SPWidget *spw )
284 {
285 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
286 return;
287 }
289 /* TODO: Does this work?
290 * Not really, here we have to get old color back from object
291 * Instead of relying on paint widget having meaningful colors set
292 */
293 sp_stroke_style_paint_changed(psel, spw);
294 }
296 static gchar *undo_label_1 = "stroke:flatcolor:1";
297 static gchar *undo_label_2 = "stroke:flatcolor:2";
298 static gchar *undo_label = undo_label_1;
300 /**
301 * When a drag callback occurs on a paint selector object, if it is a RGB or CMYK
302 * color mode, then set the stroke opacity to psel's flat color.
303 */
304 static void
305 sp_stroke_style_paint_dragged(SPPaintSelector *psel, SPWidget *spw)
306 {
307 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
308 return;
309 }
311 switch (psel->mode) {
312 case SP_PAINT_SELECTOR_MODE_COLOR_RGB:
313 case SP_PAINT_SELECTOR_MODE_COLOR_CMYK:
314 {
315 sp_paint_selector_set_flat_color (psel, SP_ACTIVE_DESKTOP, "stroke", "stroke-opacity");
316 sp_document_maybe_done (sp_desktop_document(SP_ACTIVE_DESKTOP), undo_label, SP_VERB_DIALOG_FILL_STROKE,
317 _("Set stroke color"));
318 break;
319 }
321 default:
322 g_warning( "file %s: line %d: Paint %d should not emit 'dragged'",
323 __FILE__, __LINE__, psel->mode);
324 break;
325 }
326 }
328 /**
329 * When the stroke style's paint settings change, this handler updates the
330 * repr's stroke css style and applies the style to relevant drawing items.
331 */
332 static void
333 sp_stroke_style_paint_changed(SPPaintSelector *psel, SPWidget *spw)
334 {
335 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
336 return;
337 }
338 g_object_set_data (G_OBJECT (spw), "update", GINT_TO_POINTER (TRUE));
340 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
341 SPDocument *document = sp_desktop_document (desktop);
342 Inkscape::Selection *selection = sp_desktop_selection (desktop);
344 GSList const *items = selection->itemList();
346 switch (psel->mode) {
347 case SP_PAINT_SELECTOR_MODE_EMPTY:
348 // This should not happen.
349 g_warning ( "file %s: line %d: Paint %d should not emit 'changed'",
350 __FILE__, __LINE__, psel->mode);
351 break;
352 case SP_PAINT_SELECTOR_MODE_MULTIPLE:
353 // This happens when you switch multiple objects with different gradients to flat color;
354 // nothing to do here.
355 break;
357 case SP_PAINT_SELECTOR_MODE_NONE:
358 {
359 SPCSSAttr *css = sp_repr_css_attr_new();
360 sp_repr_css_set_property(css, "stroke", "none");
362 sp_desktop_set_style (desktop, css);
364 sp_repr_css_attr_unref(css);
366 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
367 _("Remove stroke"));
368 break;
369 }
371 case SP_PAINT_SELECTOR_MODE_COLOR_RGB:
372 case SP_PAINT_SELECTOR_MODE_COLOR_CMYK:
373 {
374 sp_paint_selector_set_flat_color (psel, desktop, "stroke", "stroke-opacity");
375 sp_document_maybe_done (sp_desktop_document(desktop), undo_label, SP_VERB_DIALOG_FILL_STROKE,
376 _("Set stroke color"));
378 // on release, toggle undo_label so that the next drag will not be lumped with this one
379 if (undo_label == undo_label_1)
380 undo_label = undo_label_2;
381 else
382 undo_label = undo_label_1;
384 break;
385 }
387 case SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR:
388 case SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL:
389 if (items) {
390 SPGradientType const gradient_type = ( psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR
391 ? SP_GRADIENT_TYPE_LINEAR
392 : SP_GRADIENT_TYPE_RADIAL );
393 SPGradient *vector = sp_paint_selector_get_gradient_vector(psel);
394 if (!vector) {
395 /* No vector in paint selector should mean that we just changed mode */
397 SPStyle *query = sp_style_new ();
398 int result = objects_query_fillstroke ((GSList *) items, query, false);
399 guint32 common_rgb = 0;
400 if (result == QUERY_STYLE_MULTIPLE_SAME) {
401 if (query->fill.type != SP_PAINT_TYPE_COLOR) {
402 common_rgb = sp_desktop_get_color(desktop, false);
403 } else {
404 common_rgb = sp_color_get_rgba32_ualpha(&query->stroke.value.color, 0xff);
405 }
406 vector = sp_document_default_gradient_vector(document, common_rgb);
407 }
408 g_free (query);
410 for (GSList const *i = items; i != NULL; i = i->next) {
411 if (!vector) {
412 sp_item_set_gradient(SP_ITEM(i->data),
413 sp_gradient_vector_for_object(document, desktop, SP_OBJECT(i->data), false),
414 gradient_type, false);
415 } else {
416 sp_item_set_gradient(SP_ITEM(i->data), vector, gradient_type, false);
417 }
418 }
419 } else {
420 vector = sp_gradient_ensure_vector_normalized(vector);
421 for (GSList const *i = items; i != NULL; i = i->next) {
422 SPGradient *gr = sp_item_set_gradient(SP_ITEM(i->data), vector, gradient_type, false);
423 sp_gradient_selector_attrs_to_gradient(gr, psel);
424 }
425 }
427 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
428 _("Set gradient on stroke"));
429 }
430 break;
432 case SP_PAINT_SELECTOR_MODE_PATTERN:
434 if (items) {
436 SPPattern *pattern = sp_paint_selector_get_pattern (psel);
437 if (!pattern) {
439 /* No Pattern in paint selector should mean that we just
440 * changed mode - dont do jack.
441 */
443 } else {
444 Inkscape::XML::Node *patrepr = SP_OBJECT_REPR(pattern);
445 SPCSSAttr *css = sp_repr_css_attr_new ();
446 gchar *urltext = g_strdup_printf ("url(#%s)", patrepr->attribute("id"));
447 sp_repr_css_set_property (css, "stroke", urltext);
449 for (GSList const *i = items; i != NULL; i = i->next) {
450 Inkscape::XML::Node *selrepr = SP_OBJECT_REPR (i->data);
451 SPObject *selobj = SP_OBJECT (i->data);
452 if (!selrepr)
453 continue;
455 SPStyle *style = SP_OBJECT_STYLE (selobj);
456 if (style && style->stroke.type == SP_PAINT_TYPE_PAINTSERVER) {
457 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (selobj);
458 if (SP_IS_PATTERN (server) && pattern_getroot (SP_PATTERN(server)) == pattern)
459 // only if this object's pattern is not rooted in our selected pattern, apply
460 continue;
461 }
463 sp_repr_css_change_recursive (selrepr, css, "style");
464 }
466 sp_repr_css_attr_unref (css);
467 g_free (urltext);
469 } // end if
471 sp_document_done (document, SP_VERB_DIALOG_FILL_STROKE,
472 _("Set pattern on stroke"));
473 } // end if
475 break;
477 case SP_PAINT_SELECTOR_MODE_UNSET:
478 if (items) {
479 SPCSSAttr *css = sp_repr_css_attr_new ();
480 sp_repr_css_unset_property (css, "stroke");
482 sp_desktop_set_style (desktop, css);
483 sp_repr_css_attr_unref (css);
485 sp_document_done (document, SP_VERB_DIALOG_FILL_STROKE,
486 _("Unset stroke"));
487 }
488 break;
490 default:
491 g_warning( "file %s: line %d: Paint selector should not be in "
492 "mode %d",
493 __FILE__, __LINE__,
494 psel->mode );
495 break;
496 }
498 g_object_set_data (G_OBJECT (spw), "update", GINT_TO_POINTER (FALSE));
499 }
505 /* Line */
507 static void sp_stroke_style_line_construct(SPWidget *spw, gpointer data);
508 static void sp_stroke_style_line_selection_modified (SPWidget *spw,
509 Inkscape::Selection *selection,
510 guint flags,
511 gpointer data);
513 static void sp_stroke_style_line_selection_changed (SPWidget *spw,
514 Inkscape::Selection *selection,
515 gpointer data );
517 static void sp_stroke_style_line_update(SPWidget *spw, Inkscape::Selection *sel);
519 static void sp_stroke_style_set_join_buttons(SPWidget *spw,
520 GtkWidget *active);
522 static void sp_stroke_style_set_cap_buttons(SPWidget *spw,
523 GtkWidget *active);
525 static void sp_stroke_style_width_changed(GtkAdjustment *adj, SPWidget *spw);
526 static void sp_stroke_style_miterlimit_changed(GtkAdjustment *adj, SPWidget *spw);
527 static void sp_stroke_style_any_toggled(GtkToggleButton *tb, SPWidget *spw);
528 static void sp_stroke_style_line_dash_changed(SPDashSelector *dsel,
529 SPWidget *spw);
531 static void sp_stroke_style_update_marker_menus(SPWidget *spw, GSList const *objects);
534 /**
535 * Helper function for creating radio buttons. This should probably be re-thought out
536 * when reimplementing this with Gtkmm.
537 */
538 static GtkWidget *
539 sp_stroke_radio_button(GtkWidget *tb, char const *icon,
540 GtkWidget *hb, GtkWidget *spw,
541 gchar const *key, gchar const *data)
542 {
543 g_assert(icon != NULL);
544 g_assert(hb != NULL);
545 g_assert(spw != NULL);
547 if (tb == NULL) {
548 tb = gtk_radio_button_new(NULL);
549 } else {
550 tb = gtk_radio_button_new(gtk_radio_button_group(GTK_RADIO_BUTTON(tb)) );
551 }
553 gtk_widget_show(tb);
554 gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(tb), FALSE);
555 gtk_box_pack_start(GTK_BOX(hb), tb, FALSE, FALSE, 0);
556 gtk_object_set_data(GTK_OBJECT(spw), icon, tb);
557 gtk_object_set_data(GTK_OBJECT(tb), key, (gpointer*)data);
558 gtk_signal_connect(GTK_OBJECT(tb), "toggled",
559 GTK_SIGNAL_FUNC(sp_stroke_style_any_toggled),
560 spw);
561 GtkWidget *px = sp_icon_new(Inkscape::ICON_SIZE_LARGE_TOOLBAR, icon);
562 g_assert(px != NULL);
563 gtk_widget_show(px);
564 gtk_container_add(GTK_CONTAINER(tb), px);
566 return tb;
568 }
570 /**
571 * Creates a copy of the marker named mname, determines its visible and renderable
572 * area in menu_id's bounding box, and then renders it. This allows us to fill in
573 * preview images of each marker in the marker menu.
574 */
575 static GtkWidget *
576 sp_marker_prev_new(unsigned psize, gchar const *mname,
577 SPDocument *source, SPDocument *sandbox,
578 gchar *menu_id, NRArena const *arena, unsigned visionkey, NRArenaItem *root)
579 {
580 // Retrieve the marker named 'mname' from the source SVG document
581 SPObject const *marker = source->getObjectById(mname);
582 if (marker == NULL)
583 return NULL;
585 // Create a copy repr of the marker with id="sample"
586 Inkscape::XML::Node *mrepr = SP_OBJECT_REPR (marker)->duplicate();
587 mrepr->setAttribute("id", "sample");
589 // Replace the old sample in the sandbox by the new one
590 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (sandbox->getObjectById("defs"));
591 SPObject *oldmarker = sandbox->getObjectById("sample");
592 if (oldmarker)
593 oldmarker->deleteObject(false);
594 defsrepr->appendChild(mrepr);
595 Inkscape::GC::release(mrepr);
597 // Uncomment this to get the sandbox documents saved (useful for debugging)
598 //FILE *fp = fopen (g_strconcat(mname, ".svg", NULL), "w");
599 //sp_repr_save_stream (sp_document_repr_doc (sandbox), fp);
600 //fclose (fp);
602 // object to render; note that the id is the same as that of the menu we're building
603 SPObject *object = sandbox->getObjectById(menu_id);
604 sp_document_root (sandbox)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
605 sp_document_ensure_up_to_date(sandbox);
607 if (object == NULL || !SP_IS_ITEM(object))
608 return NULL; // sandbox broken?
610 // Find object's bbox in document
611 NR::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
612 NR::Rect const dbox = SP_ITEM(object)->invokeBbox(i2doc);
614 if (dbox.isEmpty()) {
615 return NULL;
616 }
618 /* Update to renderable state */
619 double sf = 0.8;
620 GdkPixbuf* pixbuf = NULL;
622 Glib::ustring key = svg_preview_cache.cache_key(source->uri, mname, psize);
623 pixbuf = svg_preview_cache.get_preview_from_cache(key);
625 if (pixbuf == NULL) {
626 pixbuf = render_pixbuf(root, sf, dbox, psize);
627 svg_preview_cache.set_preview_in_cache(key, pixbuf);
628 }
630 // Create widget
631 GtkWidget *pb = gtk_image_new_from_pixbuf(pixbuf);
633 return pb;
634 }
637 /**
638 * Returns a list of markers in the defs of the given source document as a GSList object
639 * Returns NULL if there are no markers in the document.
640 */
641 GSList *
642 ink_marker_list_get (SPDocument *source)
643 {
644 if (source == NULL)
645 return NULL;
647 GSList *ml = NULL;
648 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS (source);
649 for ( SPObject *child = sp_object_first_child(SP_OBJECT(defs));
650 child != NULL;
651 child = SP_OBJECT_NEXT (child) )
652 {
653 if (SP_IS_MARKER(child)) {
654 ml = g_slist_prepend (ml, child);
655 }
656 }
657 return ml;
658 }
660 #define MARKER_ITEM_MARGIN 0
662 /**
663 * Adds previews of markers in marker_list to the given menu widget
664 */
665 static void
666 sp_marker_menu_build (GtkWidget *m, GSList *marker_list, SPDocument *source, SPDocument *sandbox, gchar *menu_id)
667 {
668 // Do this here, outside of loop, to speed up preview generation:
669 NRArena const *arena = NRArena::create();
670 unsigned const visionkey = sp_item_display_key_new(1);
671 NRArenaItem *root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT (sandbox)), (NRArena *) arena, visionkey, SP_ITEM_SHOW_DISPLAY );
673 for (; marker_list != NULL; marker_list = marker_list->next) {
674 Inkscape::XML::Node *repr = SP_OBJECT_REPR((SPItem *) marker_list->data);
675 GtkWidget *i = gtk_menu_item_new();
676 gtk_widget_show(i);
678 if (repr->attribute("inkscape:stockid"))
679 g_object_set_data (G_OBJECT(i), "stockid", (void *) "true");
680 else
681 g_object_set_data (G_OBJECT(i), "stockid", (void *) "false");
683 gchar const *markid = repr->attribute("id");
684 g_object_set_data (G_OBJECT(i), "marker", (void *) markid);
686 GtkWidget *hb = gtk_hbox_new(FALSE, MARKER_ITEM_MARGIN);
687 gtk_widget_show(hb);
689 // generate preview
691 GtkWidget *prv = sp_marker_prev_new (22, markid, source, sandbox, menu_id, arena, visionkey, root);
692 gtk_widget_show(prv);
693 gtk_box_pack_start(GTK_BOX(hb), prv, FALSE, FALSE, 6);
695 // create label
696 GtkWidget *l = gtk_label_new(repr->attribute("id"));
697 gtk_widget_show(l);
698 gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
700 gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
702 gtk_widget_show(hb);
703 gtk_container_add(GTK_CONTAINER(i), hb);
705 gtk_menu_append(GTK_MENU(m), i);
706 }
707 }
709 /**
710 * sp_marker_list_from_doc()
711 *
712 * \brief Pick up all markers from source, except those that are in
713 * current_doc (if non-NULL), and add items to the m menu
714 *
715 */
716 static void
717 sp_marker_list_from_doc (GtkWidget *m, SPDocument *current_doc, SPDocument *source, SPDocument *markers_doc, SPDocument *sandbox, gchar *menu_id)
718 {
719 GSList *ml = ink_marker_list_get(source);
720 GSList *clean_ml = NULL;
722 // Do this here, outside of loop, to speed up preview generation:
723 /* Create new arena */
724 NRArena const *arena = NRArena::create();
725 /* Create ArenaItem and set transform */
726 unsigned const visionkey = sp_item_display_key_new(1);
727 /*
728 NRArenaItem *root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT (sandbox)), (NRArena *) arena, visionkey, SP_ITEM_SHOW_DISPLAY );
729 */
731 for (; ml != NULL; ml = ml->next) {
732 if (!SP_IS_MARKER(ml->data))
733 continue;
735 // Add to the list of markers we really do wish to show
736 clean_ml = g_slist_prepend (clean_ml, ml->data);
737 }
738 sp_marker_menu_build (m, clean_ml, source, sandbox, menu_id);
740 g_slist_free (ml);
741 g_slist_free (clean_ml);
742 }
745 /**
746 * Returns a new document containing default start, mid, and end markers.
747 */
748 SPDocument *
749 ink_markers_preview_doc ()
750 {
751 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\">"
752 " <defs id=\"defs\" />"
754 " <g id=\"marker-start\">"
755 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:url(#sample);marker-mid:none;marker-end:none\""
756 " d=\"M 12.5,13 L 25,13\" id=\"path1\" />"
757 " <rect style=\"fill:none;stroke:none\" id=\"rect2\""
758 " width=\"25\" height=\"25\" x=\"0\" y=\"0\" />"
759 " </g>"
761 " <g id=\"marker-mid\">"
762 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:url(#sample);marker-end:none\""
763 " d=\"M 0,113 L 12.5,113 L 25,113\" id=\"path11\" />"
764 " <rect style=\"fill:none;stroke:none\" id=\"rect22\""
765 " width=\"25\" height=\"25\" x=\"0\" y=\"100\" />"
766 " </g>"
768 " <g id=\"marker-end\">"
769 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:none;marker-end:url(#sample)\""
770 " d=\"M 0,213 L 12.5,213\" id=\"path111\" />"
771 " <rect style=\"fill:none;stroke:none\" id=\"rect222\""
772 " width=\"25\" height=\"25\" x=\"0\" y=\"200\" />"
773 " </g>"
775 "</svg>";
777 return sp_document_new_from_mem (buffer, strlen(buffer), FALSE);
778 }
780 static void
781 ink_marker_menu_create_menu(GtkWidget *m, gchar *menu_id, SPDocument *doc, SPDocument *sandbox)
782 {
783 static SPDocument *markers_doc = NULL;
785 // add "None"
786 GtkWidget *i = gtk_menu_item_new();
787 gtk_widget_show(i);
789 // g_object_set_data(G_OBJECT(i), "marker", (void *) "none");
791 GtkWidget *hb = gtk_hbox_new(FALSE, MARKER_ITEM_MARGIN);
792 gtk_widget_show(hb);
794 GtkWidget *l = gtk_label_new( _("None") );
795 gtk_widget_show(l);
796 gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
798 gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
800 gtk_widget_show(hb);
801 gtk_container_add(GTK_CONTAINER(i), hb);
802 gtk_menu_append(GTK_MENU(m), i);
804 // find and load markers.svg
805 if (markers_doc == NULL) {
806 char *markers_source = g_build_filename(INKSCAPE_MARKERSDIR, "markers.svg", NULL);
807 if (Inkscape::IO::file_test(markers_source, G_FILE_TEST_IS_REGULAR)) {
808 markers_doc = sp_document_new(markers_source, FALSE);
809 }
810 g_free(markers_source);
811 }
813 // suck in from current doc
814 sp_marker_list_from_doc ( m, NULL, doc, markers_doc, sandbox, menu_id );
816 // add separator
817 {
818 GtkWidget *i = gtk_separator_menu_item_new();
819 gtk_widget_show(i);
820 gtk_menu_append(GTK_MENU(m), i);
821 }
823 // suck in from markers.svg
824 if (markers_doc) {
825 sp_document_ensure_up_to_date(doc);
826 sp_marker_list_from_doc ( m, doc, markers_doc, NULL, sandbox, menu_id );
827 }
828 }
831 /**
832 * Creates a menu widget to display markers from markers.svg
833 */
834 static GtkWidget *
835 ink_marker_menu( GtkWidget *tbl, gchar *menu_id, SPDocument *sandbox)
836 {
837 SPDesktop *desktop = inkscape_active_desktop();
838 SPDocument *doc = sp_desktop_document(desktop);
839 GtkWidget *mnu = gtk_option_menu_new();
841 /* Create new menu widget */
842 GtkWidget *m = gtk_menu_new();
843 gtk_widget_show(m);
845 g_object_set_data(G_OBJECT(mnu), "updating", (gpointer) FALSE);
847 if (!doc) {
848 GtkWidget *i = gtk_menu_item_new_with_label(_("No document selected"));
849 gtk_widget_show(i);
850 gtk_menu_append(GTK_MENU(m), i);
851 gtk_widget_set_sensitive(mnu, FALSE);
853 } else {
854 ink_marker_menu_create_menu(m, menu_id, doc, sandbox);
856 gtk_widget_set_sensitive(mnu, TRUE);
857 }
859 gtk_object_set_data(GTK_OBJECT(mnu), "menu_id", menu_id);
860 gtk_option_menu_set_menu(GTK_OPTION_MENU(mnu), m);
862 /* Set history */
863 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
865 return mnu;
866 }
869 /**
870 * Handles when user selects one of the markers from the marker menu.
871 * Defines a uri string to refer to it, then applies it to all selected
872 * items in the current desktop.
873 */
874 static void
875 sp_marker_select(GtkOptionMenu *mnu, GtkWidget *spw)
876 {
877 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
878 return;
879 }
881 SPDesktop *desktop = inkscape_active_desktop();
882 SPDocument *document = sp_desktop_document(desktop);
883 if (!document) {
884 return;
885 }
887 /* Get Marker */
888 if (!g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
889 "marker"))
890 {
891 return;
892 }
893 gchar *markid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
894 "marker");
895 gchar *marker = "";
896 if (strcmp(markid, "none")){
897 gchar *stockid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
898 "stockid");
900 gchar *markurn = markid;
901 if (!strcmp(stockid,"true")) markurn = g_strconcat("urn:inkscape:marker:",markid,NULL);
902 SPObject *mark = get_stock_item(markurn);
903 if (mark) {
904 Inkscape::XML::Node *repr = SP_OBJECT_REPR(mark);
905 marker = g_strconcat("url(#", repr->attribute("id"), ")", NULL);
906 }
907 } else {
908 marker = markid;
909 }
911 SPCSSAttr *css = sp_repr_css_attr_new();
912 gchar *menu_id = (gchar *) g_object_get_data(G_OBJECT(mnu), "menu_id");
913 sp_repr_css_set_property(css, menu_id, marker);
915 Inkscape::Selection *selection = sp_desktop_selection(desktop);
916 GSList const *items = selection->itemList();
917 for (; items != NULL; items = items->next) {
918 SPItem *item = (SPItem *) items->data;
919 if (!SP_IS_SHAPE(item) || SP_IS_RECT(item)) // can't set marker to rect, until it's converted to using <path>
920 continue;
921 Inkscape::XML::Node *selrepr = SP_OBJECT_REPR((SPItem *) items->data);
922 if (selrepr) {
923 sp_repr_css_change_recursive(selrepr, css, "style");
924 }
925 SP_OBJECT(items->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
926 SP_OBJECT(items->data)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
927 }
929 sp_repr_css_attr_unref(css);
931 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
932 _("Set markers"));
934 // Lastly, also update the marker dropdown menus, so the document's markers
935 // show up at the top of the menu
936 ink_markers_menu_update();
937 };
939 static void
940 ink_markers_menu_update() {
941 SPDesktop *desktop = inkscape_active_desktop();
942 SPDocument *document = sp_desktop_document(desktop);
943 SPDocument *sandbox = ink_markers_preview_doc ();
944 GtkWidget *m;
946 m = gtk_menu_new();
947 gtk_widget_show(m);
948 ink_marker_menu_create_menu(m, "marker-start", document, sandbox);
949 gtk_option_menu_remove_menu(GTK_OPTION_MENU(marker_start_menu));
950 gtk_option_menu_set_menu(GTK_OPTION_MENU(marker_start_menu), m);
952 m = gtk_menu_new();
953 gtk_widget_show(m);
954 ink_marker_menu_create_menu(m, "marker-mid", document, sandbox);
955 gtk_option_menu_remove_menu(GTK_OPTION_MENU(marker_mid_menu));
956 gtk_option_menu_set_menu(GTK_OPTION_MENU(marker_mid_menu), m);
958 m = gtk_menu_new();
959 gtk_widget_show(m);
960 ink_marker_menu_create_menu(m, "marker-end", document, sandbox);
961 gtk_option_menu_remove_menu(GTK_OPTION_MENU(marker_end_menu));
962 gtk_option_menu_set_menu(GTK_OPTION_MENU(marker_end_menu), m);
963 }
965 /**
966 * Sets the stroke width units for all selected items.
967 * Also handles absolute and dimensionless units.
968 */
969 static gboolean stroke_width_set_unit(SPUnitSelector *,
970 SPUnit const *old,
971 SPUnit const *new_units,
972 GObject *spw)
973 {
974 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
976 if (!desktop) {
977 return FALSE;
978 }
980 Inkscape::Selection *selection = sp_desktop_selection (desktop);
982 if (selection->isEmpty())
983 return FALSE;
985 GSList const *objects = selection->itemList();
987 if ((old->base == SP_UNIT_ABSOLUTE || old->base == SP_UNIT_DEVICE) &&
988 (new_units->base == SP_UNIT_DIMENSIONLESS)) {
990 /* Absolute to percentage */
991 g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
993 GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
994 float w = sp_units_get_pixels (a->value, *old);
996 gdouble average = stroke_average_width (objects);
998 if (average == NR_HUGE || average == 0)
999 return FALSE;
1001 gtk_adjustment_set_value (a, 100.0 * w / average);
1003 g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
1004 return TRUE;
1006 } else if ((old->base == SP_UNIT_DIMENSIONLESS) &&
1007 (new_units->base == SP_UNIT_ABSOLUTE || new_units->base == SP_UNIT_DEVICE)) {
1009 /* Percentage to absolute */
1010 g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
1012 GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
1014 gdouble average = stroke_average_width (objects);
1016 gtk_adjustment_set_value (a, sp_pixels_get_units (0.01 * a->value * average, *new_units));
1018 g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
1019 return TRUE;
1020 }
1022 return FALSE;
1023 }
1026 /**
1027 * \brief Creates a new widget for the line stroke style.
1028 *
1029 */
1030 GtkWidget *
1031 sp_stroke_style_line_widget_new(void)
1032 {
1033 GtkWidget *spw, *f, *t, *hb, *sb, *us, *tb, *ds;
1034 GtkObject *a;
1036 GtkTooltips *tt = gtk_tooltips_new();
1038 spw = sp_widget_new_global(INKSCAPE);
1040 f = gtk_hbox_new (FALSE, 0);
1041 gtk_widget_show(f);
1042 gtk_container_add(GTK_CONTAINER(spw), f);
1044 t = gtk_table_new(3, 6, FALSE);
1045 gtk_widget_show(t);
1046 gtk_container_set_border_width(GTK_CONTAINER(t), 4);
1047 gtk_table_set_row_spacings(GTK_TABLE(t), 4);
1048 gtk_container_add(GTK_CONTAINER(f), t);
1049 gtk_object_set_data(GTK_OBJECT(spw), "stroke", t);
1051 gint i = 0;
1053 /* Stroke width */
1054 spw_label(t, _("Width:"), 0, i);
1056 hb = spw_hbox(t, 3, 1, i);
1058 // TODO: when this is gtkmmified, use an Inkscape::UI::Widget::ScalarUnit instead of the separate
1059 // spinbutton and unit selector for stroke width. In sp_stroke_style_line_update, use
1060 // setHundredPercent to remember the aeraged width corresponding to 100%. Then the
1061 // stroke_width_set_unit will be removed (because ScalarUnit takes care of conversions itself), and
1062 // with it, the two remaining calls of stroke_average_width, allowing us to get rid of that
1063 // function in desktop-style.
1065 a = gtk_adjustment_new(1.0, 0.0, 1000.0, 0.1, 10.0, 10.0);
1066 gtk_object_set_data(GTK_OBJECT(spw), "width", a);
1067 sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 3);
1068 gtk_tooltips_set_tip(tt, sb, _("Stroke width"), NULL);
1069 gtk_widget_show(sb);
1071 sp_dialog_defocus_on_enter(sb);
1073 gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1074 us = sp_unit_selector_new(SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE);
1075 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1076 if (desktop)
1077 sp_unit_selector_set_unit (SP_UNIT_SELECTOR(us), sp_desktop_namedview(desktop)->doc_units);
1078 sp_unit_selector_add_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT), 0);
1079 g_signal_connect ( G_OBJECT (us), "set_unit", G_CALLBACK (stroke_width_set_unit), spw );
1080 gtk_widget_show(us);
1081 sp_unit_selector_add_adjustment( SP_UNIT_SELECTOR(us), GTK_ADJUSTMENT(a) );
1082 gtk_box_pack_start(GTK_BOX(hb), us, FALSE, FALSE, 0);
1083 gtk_object_set_data(GTK_OBJECT(spw), "units", us);
1085 gtk_signal_connect( GTK_OBJECT(a), "value_changed", GTK_SIGNAL_FUNC(sp_stroke_style_width_changed), spw );
1086 i++;
1088 /* Join type */
1089 // TRANSLATORS: The line join style specifies the shape to be used at the
1090 // corners of paths. It can be "miter", "round" or "bevel".
1091 spw_label(t, _("Join:"), 0, i);
1093 hb = spw_hbox(t, 3, 1, i);
1095 tb = NULL;
1097 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_MITER,
1098 hb, spw, "join", "miter");
1100 // TRANSLATORS: Miter join: joining lines with a sharp (pointed) corner.
1101 // For an example, draw a triangle with a large stroke width and modify the
1102 // "Join" option (in the Fill and Stroke dialog).
1103 gtk_tooltips_set_tip(tt, tb, _("Miter join"), NULL);
1105 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_ROUND,
1106 hb, spw, "join", "round");
1108 // TRANSLATORS: Round join: joining lines with a rounded corner.
1109 // For an example, draw a triangle with a large stroke width and modify the
1110 // "Join" option (in the Fill and Stroke dialog).
1111 gtk_tooltips_set_tip(tt, tb, _("Round join"), NULL);
1113 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_BEVEL,
1114 hb, spw, "join", "bevel");
1116 // TRANSLATORS: Bevel join: joining lines with a blunted (flattened) corner.
1117 // For an example, draw a triangle with a large stroke width and modify the
1118 // "Join" option (in the Fill and Stroke dialog).
1119 gtk_tooltips_set_tip(tt, tb, _("Bevel join"), NULL);
1121 i++;
1123 /* Miterlimit */
1124 // TRANSLATORS: Miter limit: only for "miter join", this limits the length
1125 // of the sharp "spike" when the lines connect at too sharp an angle.
1126 // When two line segments meet at a sharp angle, a miter join results in a
1127 // spike that extends well beyond the connection point. The purpose of the
1128 // miter limit is to cut off such spikes (i.e. convert them into bevels)
1129 // when they become too long.
1130 spw_label(t, _("Miter limit:"), 0, i);
1132 hb = spw_hbox(t, 3, 1, i);
1134 a = gtk_adjustment_new(4.0, 0.0, 100.0, 0.1, 10.0, 10.0);
1135 gtk_object_set_data(GTK_OBJECT(spw), "miterlimit", a);
1137 sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 2);
1138 gtk_tooltips_set_tip(tt, sb, _("Maximum length of the miter (in units of stroke width)"), NULL);
1139 gtk_widget_show(sb);
1140 gtk_object_set_data(GTK_OBJECT(spw), "miterlimit_sb", sb);
1141 sp_dialog_defocus_on_enter(sb);
1143 gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1145 gtk_signal_connect( GTK_OBJECT(a), "value_changed",
1146 GTK_SIGNAL_FUNC(sp_stroke_style_miterlimit_changed), spw );
1147 i++;
1149 /* Cap type */
1150 // TRANSLATORS: cap type specifies the shape for the ends of lines
1151 spw_label(t, _("Cap:"), 0, i);
1153 hb = spw_hbox(t, 3, 1, i);
1155 tb = NULL;
1157 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_BUTT,
1158 hb, spw, "cap", "butt");
1160 // TRANSLATORS: Butt cap: the line shape does not extend beyond the end point
1161 // of the line; the ends of the line are square
1162 gtk_tooltips_set_tip(tt, tb, _("Butt cap"), NULL);
1164 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_ROUND,
1165 hb, spw, "cap", "round");
1167 // TRANSLATORS: Round cap: the line shape extends beyond the end point of the
1168 // line; the ends of the line are rounded
1169 gtk_tooltips_set_tip(tt, tb, _("Round cap"), NULL);
1171 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_SQUARE,
1172 hb, spw, "cap", "square");
1174 // TRANSLATORS: Square cap: the line shape extends beyond the end point of the
1175 // line; the ends of the line are square
1176 gtk_tooltips_set_tip(tt, tb, _("Square cap"), NULL);
1178 i++;
1181 /* Dash */
1182 spw_label(t, _("Dashes:"), 0, i);
1183 ds = sp_dash_selector_new( inkscape_get_repr( INKSCAPE,
1184 "palette.dashes") );
1186 gtk_widget_show(ds);
1187 gtk_table_attach( GTK_TABLE(t), ds, 1, 4, i, i+1,
1188 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1189 (GtkAttachOptions)0, 0, 0 );
1190 gtk_object_set_data(GTK_OBJECT(spw), "dash", ds);
1191 gtk_signal_connect( GTK_OBJECT(ds), "changed",
1192 GTK_SIGNAL_FUNC(sp_stroke_style_line_dash_changed),
1193 spw );
1194 i++;
1196 /* Drop down marker selectors*/
1198 // doing this here once, instead of for each preview, to speed things up
1199 SPDocument *sandbox = ink_markers_preview_doc ();
1201 // TRANSLATORS: Path markers are an SVG feature that allows you to attach arbitrary shapes
1202 // (arrowheads, bullets, faces, whatever) to the start, end, or middle nodes of a path.
1203 spw_label(t, _("Start Markers:"), 0, i);
1204 marker_start_menu = ink_marker_menu( spw ,"marker-start", sandbox);
1205 gtk_signal_connect( GTK_OBJECT(marker_start_menu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1206 gtk_widget_show(marker_start_menu);
1207 gtk_table_attach( GTK_TABLE(t), marker_start_menu, 1, 4, i, i+1,
1208 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1209 (GtkAttachOptions)0, 0, 0 );
1210 gtk_object_set_data(GTK_OBJECT(spw), "start_mark_menu", marker_start_menu);
1212 i++;
1213 spw_label(t, _("Mid Markers:"), 0, i);
1214 marker_mid_menu = ink_marker_menu( spw ,"marker-mid", sandbox);
1215 gtk_signal_connect( GTK_OBJECT(marker_mid_menu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1216 gtk_widget_show(marker_mid_menu);
1217 gtk_table_attach( GTK_TABLE(t), marker_mid_menu, 1, 4, i, i+1,
1218 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1219 (GtkAttachOptions)0, 0, 0 );
1220 gtk_object_set_data(GTK_OBJECT(spw), "mid_mark_menu", marker_mid_menu);
1222 i++;
1223 spw_label(t, _("End Markers:"), 0, i);
1224 marker_end_menu = ink_marker_menu( spw ,"marker-end", sandbox);
1225 gtk_signal_connect( GTK_OBJECT(marker_end_menu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1226 gtk_widget_show(marker_end_menu);
1227 gtk_table_attach( GTK_TABLE(t), marker_end_menu, 1, 4, i, i+1,
1228 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1229 (GtkAttachOptions)0, 0, 0 );
1230 gtk_object_set_data(GTK_OBJECT(spw), "end_mark_menu", marker_end_menu);
1232 i++;
1234 gtk_signal_connect( GTK_OBJECT(spw), "construct",
1235 GTK_SIGNAL_FUNC(sp_stroke_style_line_construct),
1236 NULL );
1237 gtk_signal_connect( GTK_OBJECT(spw), "modify_selection",
1238 GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_modified),
1239 NULL );
1240 gtk_signal_connect( GTK_OBJECT(spw), "change_selection",
1241 GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_changed),
1242 NULL );
1244 sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
1246 return spw;
1247 }
1250 /**
1251 * Callback for when the stroke style widget is called. It causes
1252 * the stroke line style to be updated.
1253 */
1254 static void
1255 sp_stroke_style_line_construct(SPWidget *spw, gpointer data)
1256 {
1258 #ifdef SP_SS_VERBOSE
1259 g_print( "Stroke style widget constructed: inkscape %p repr %p\n",
1260 spw->inkscape, spw->repr );
1261 #endif
1262 if (spw->inkscape) {
1263 sp_stroke_style_line_update(spw,
1264 ( SP_ACTIVE_DESKTOP
1265 ? sp_desktop_selection(SP_ACTIVE_DESKTOP)
1266 : NULL ));
1267 }
1268 }
1270 /**
1271 * Callback for when stroke style widget is modified.
1272 * Triggers update action.
1273 */
1274 static void
1275 sp_stroke_style_line_selection_modified ( SPWidget *spw,
1276 Inkscape::Selection *selection,
1277 guint flags,
1278 gpointer data )
1279 {
1280 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1281 sp_stroke_style_line_update (spw, selection);
1282 }
1284 }
1286 /**
1287 * Callback for when stroke style widget is changed.
1288 * Triggers update action.
1289 */
1290 static void
1291 sp_stroke_style_line_selection_changed ( SPWidget *spw,
1292 Inkscape::Selection *selection,
1293 gpointer data )
1294 {
1295 sp_stroke_style_line_update (spw, selection);
1296 }
1299 /**
1300 * Sets selector widgets' dash style from an SPStyle object.
1301 */
1302 static void
1303 sp_dash_selector_set_from_style (GtkWidget *dsel, SPStyle *style)
1304 {
1305 if (style->stroke_dash.n_dash > 0) {
1306 double d[64];
1307 int len = MIN(style->stroke_dash.n_dash, 64);
1308 for (int i = 0; i < len; i++) {
1309 if (style->stroke_width.computed != 0)
1310 d[i] = style->stroke_dash.dash[i] / style->stroke_width.computed;
1311 else
1312 d[i] = style->stroke_dash.dash[i]; // is there a better thing to do for stroke_width==0?
1313 }
1314 sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), len, d,
1315 style->stroke_width.computed != 0?
1316 style->stroke_dash.offset / style->stroke_width.computed :
1317 style->stroke_dash.offset);
1318 } else {
1319 sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), 0, NULL, 0.0);
1320 }
1321 }
1323 /**
1324 * Sets the join type for a line, and updates the stroke style widget's buttons
1325 */
1326 static void
1327 sp_jointype_set (SPWidget *spw, unsigned const jointype)
1328 {
1329 GtkWidget *tb = NULL;
1330 switch (jointype) {
1331 case SP_STROKE_LINEJOIN_MITER:
1332 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_MITER));
1333 break;
1334 case SP_STROKE_LINEJOIN_ROUND:
1335 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_ROUND));
1336 break;
1337 case SP_STROKE_LINEJOIN_BEVEL:
1338 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_BEVEL));
1339 break;
1340 default:
1341 break;
1342 }
1343 sp_stroke_style_set_join_buttons (spw, tb);
1344 }
1346 /**
1347 * Sets the cap type for a line, and updates the stroke style widget's buttons
1348 */
1349 static void
1350 sp_captype_set (SPWidget *spw, unsigned const captype)
1351 {
1352 GtkWidget *tb = NULL;
1353 switch (captype) {
1354 case SP_STROKE_LINECAP_BUTT:
1355 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_BUTT));
1356 break;
1357 case SP_STROKE_LINECAP_ROUND:
1358 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_ROUND));
1359 break;
1360 case SP_STROKE_LINECAP_SQUARE:
1361 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_SQUARE));
1362 break;
1363 default:
1364 break;
1365 }
1366 sp_stroke_style_set_cap_buttons (spw, tb);
1367 }
1369 /**
1370 * Callback for when stroke style widget is updated, including markers, cap type,
1371 * join type, etc.
1372 */
1373 static void
1374 sp_stroke_style_line_update(SPWidget *spw, Inkscape::Selection *sel)
1375 {
1376 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1377 return;
1378 }
1380 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1382 GtkWidget *sset = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "stroke"));
1383 GtkObject *width = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1384 GtkObject *ml = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1385 GtkWidget *us = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1386 GtkWidget *dsel = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1388 // create temporary style
1389 SPStyle *query = sp_style_new ();
1390 // query into it
1391 int result_sw = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1392 int result_ml = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1393 int result_cap = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKECAP);
1394 int result_join = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1396 if (result_sw == QUERY_STYLE_NOTHING) {
1397 /* No objects stroked, set insensitive */
1398 gtk_widget_set_sensitive(sset, FALSE);
1400 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1401 return;
1402 } else {
1403 gtk_widget_set_sensitive(sset, TRUE);
1405 SPUnit const *unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1407 if (result_sw == QUERY_STYLE_MULTIPLE_AVERAGED) {
1408 sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT));
1409 } else {
1410 // same width, or only one object; no sense to keep percent, switch to absolute
1411 if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1412 sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), sp_desktop_namedview(SP_ACTIVE_DESKTOP)->doc_units);
1413 }
1414 }
1416 unit = sp_unit_selector_get_unit (SP_UNIT_SELECTOR (us));
1418 if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1419 double avgwidth = sp_pixels_get_units (query->stroke_width.computed, *unit);
1420 gtk_adjustment_set_value(GTK_ADJUSTMENT(width), avgwidth);
1421 } else {
1422 gtk_adjustment_set_value(GTK_ADJUSTMENT(width), 100);
1423 }
1424 }
1426 if (result_ml != QUERY_STYLE_NOTHING)
1427 gtk_adjustment_set_value(GTK_ADJUSTMENT(ml), query->stroke_miterlimit.value); // TODO: reflect averagedness?
1429 if (result_join != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1430 sp_jointype_set (spw, query->stroke_linejoin.value);
1431 } else {
1432 sp_stroke_style_set_join_buttons(spw, NULL);
1433 }
1435 if (result_cap != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1436 sp_captype_set (spw, query->stroke_linecap.value);
1437 } else {
1438 sp_stroke_style_set_cap_buttons(spw, NULL);
1439 }
1441 g_free (query);
1443 if (!sel || sel->isEmpty())
1444 return;
1446 GSList const *objects = sel->itemList();
1447 SPObject * const object = SP_OBJECT(objects->data);
1448 SPStyle * const style = SP_OBJECT_STYLE(object);
1450 /* Markers */
1451 sp_stroke_style_update_marker_menus(spw, objects); // FIXME: make this desktop query too
1453 /* Dash */
1454 sp_dash_selector_set_from_style (dsel, style); // FIXME: make this desktop query too
1456 gtk_widget_set_sensitive(sset, TRUE);
1458 gtk_object_set_data(GTK_OBJECT(spw), "update",
1459 GINT_TO_POINTER(FALSE));
1460 }
1462 /**
1463 * Sets a line's dash properties in a CSS style object.
1464 */
1465 static void
1466 sp_stroke_style_set_scaled_dash(SPCSSAttr *css,
1467 int ndash, double *dash, double offset,
1468 double scale)
1469 {
1470 if (ndash > 0) {
1471 Inkscape::CSSOStringStream osarray;
1472 for (int i = 0; i < ndash; i++) {
1473 osarray << dash[i] * scale;
1474 if (i < (ndash - 1)) {
1475 osarray << ",";
1476 }
1477 }
1478 sp_repr_css_set_property(css, "stroke-dasharray", osarray.str().c_str());
1480 Inkscape::CSSOStringStream osoffset;
1481 osoffset << offset * scale;
1482 sp_repr_css_set_property(css, "stroke-dashoffset", osoffset.str().c_str());
1483 } else {
1484 sp_repr_css_set_property(css, "stroke-dasharray", "none");
1485 sp_repr_css_set_property(css, "stroke-dashoffset", NULL);
1486 }
1487 }
1489 /**
1490 * Sets line properties like width, dashes, markers, etc. on all currently selected items.
1491 */
1492 static void
1493 sp_stroke_style_scale_line(SPWidget *spw)
1494 {
1495 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1496 return;
1497 }
1499 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1501 GtkAdjustment *wadj = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1502 SPUnitSelector *us = SP_UNIT_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1503 SPDashSelector *dsel = SP_DASH_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1504 GtkAdjustment *ml = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1506 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1507 SPDocument *document = sp_desktop_document (desktop);
1508 Inkscape::Selection *selection = sp_desktop_selection (desktop);
1510 GSList const *items = selection->itemList();
1512 /* TODO: Create some standardized method */
1513 SPCSSAttr *css = sp_repr_css_attr_new();
1515 if (items) {
1517 double width_typed = wadj->value;
1518 double const miterlimit = ml->value;
1520 SPUnit const *const unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1522 double *dash, offset;
1523 int ndash;
1524 sp_dash_selector_get_dash(dsel, &ndash, &dash, &offset);
1526 for (GSList const *i = items; i != NULL; i = i->next) {
1527 /* Set stroke width */
1528 double width;
1529 if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1530 width = sp_units_get_pixels (width_typed, *unit);
1531 } else { // percentage
1532 gdouble old_w = SP_OBJECT_STYLE (i->data)->stroke_width.computed;
1533 width = old_w * width_typed / 100;
1534 }
1536 {
1537 Inkscape::CSSOStringStream os_width;
1538 os_width << width;
1539 sp_repr_css_set_property(css, "stroke-width", os_width.str().c_str());
1540 }
1542 {
1543 Inkscape::CSSOStringStream os_ml;
1544 os_ml << miterlimit;
1545 sp_repr_css_set_property(css, "stroke-miterlimit", os_ml.str().c_str());
1546 }
1548 /* Set dash */
1549 sp_stroke_style_set_scaled_dash(css, ndash, dash, offset, width);
1551 sp_desktop_apply_css_recursive (SP_OBJECT(i->data), css, true);
1552 }
1554 g_free(dash);
1556 if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1557 // reset to 100 percent
1558 gtk_adjustment_set_value (wadj, 100.0);
1559 }
1561 }
1563 // we have already changed the items, so set style without changing selection
1564 // FIXME: move the above stroke-setting stuff, including percentages, to desktop-style
1565 sp_desktop_set_style (desktop, css, false);
1567 sp_repr_css_attr_unref(css);
1569 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
1570 _("Set stroke style"));
1572 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1573 }
1576 /**
1577 * Callback for when the stroke style's width changes.
1578 * Causes all line styles to be applied to all selected items.
1579 */
1580 static void
1581 sp_stroke_style_width_changed(GtkAdjustment *adj, SPWidget *spw)
1582 {
1583 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1584 return;
1585 }
1587 sp_stroke_style_scale_line(spw);
1588 }
1590 /**
1591 * Callback for when the stroke style's miterlimit changes.
1592 * Causes all line styles to be applied to all selected items.
1593 */
1594 static void
1595 sp_stroke_style_miterlimit_changed(GtkAdjustment *adj, SPWidget *spw)
1596 {
1597 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1598 return;
1599 }
1601 sp_stroke_style_scale_line(spw);
1602 }
1604 /**
1605 * Callback for when the stroke style's dash changes.
1606 * Causes all line styles to be applied to all selected items.
1607 */
1608 static void
1609 sp_stroke_style_line_dash_changed(SPDashSelector *dsel, SPWidget *spw)
1610 {
1611 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1612 return;
1613 }
1615 sp_stroke_style_scale_line(spw);
1616 }
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(GtkToggleButton *tb, SPWidget *spw)
1629 {
1630 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1631 return;
1632 }
1634 if (gtk_toggle_button_get_active(tb)) {
1636 gchar const *join
1637 = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "join"));
1638 gchar const *cap
1639 = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "cap"));
1641 if (join) {
1642 GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1643 gtk_widget_set_sensitive (ml, !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, GTK_WIDGET(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, GTK_WIDGET(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 }
1673 /**
1674 * Updates the join style toggle buttons
1675 */
1676 static void
1677 sp_stroke_style_set_join_buttons(SPWidget *spw, GtkWidget *active)
1678 {
1679 GtkWidget *tb;
1681 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1682 INKSCAPE_STOCK_JOIN_MITER) );
1683 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1685 GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1686 gtk_widget_set_sensitive(ml, (active == tb));
1688 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1689 INKSCAPE_STOCK_JOIN_ROUND) );
1690 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1691 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1692 INKSCAPE_STOCK_JOIN_BEVEL) );
1693 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1694 }
1698 /**
1699 * Updates the cap style toggle buttons
1700 */
1701 static void
1702 sp_stroke_style_set_cap_buttons(SPWidget *spw, GtkWidget *active)
1703 {
1704 GtkWidget *tb;
1706 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1707 INKSCAPE_STOCK_CAP_BUTT));
1708 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1709 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1710 INKSCAPE_STOCK_CAP_ROUND) );
1711 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1712 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1713 INKSCAPE_STOCK_CAP_SQUARE) );
1714 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1715 }
1717 /**
1718 * Sets the current marker in the marker menu.
1719 */
1720 static void
1721 ink_marker_menu_set_current(SPObject *marker, GtkOptionMenu *mnu)
1722 {
1723 gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(TRUE));
1725 GtkMenu *m = GTK_MENU(gtk_option_menu_get_menu(mnu));
1726 if (marker != NULL) {
1727 bool mark_is_stock = false;
1728 if (SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"))
1729 mark_is_stock = true;
1731 gchar *markname;
1732 if (mark_is_stock)
1733 markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"));
1734 else
1735 markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("id"));
1737 int markpos = 0;
1738 GList *kids = GTK_MENU_SHELL(m)->children;
1739 int i = 0;
1740 for (; kids != NULL; kids = kids->next) {
1741 gchar *mark = (gchar *) g_object_get_data(G_OBJECT(kids->data), "marker");
1742 if ( mark && strcmp(mark, markname) == 0 ) {
1743 if ( mark_is_stock && !strcmp((gchar *) g_object_get_data(G_OBJECT(kids->data), "stockid"), "true"))
1744 markpos = i;
1745 if ( !mark_is_stock && !strcmp((gchar *) g_object_get_data(G_OBJECT(kids->data), "stockid"), "false"))
1746 markpos = i;
1747 }
1748 i++;
1749 }
1750 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), markpos);
1752 g_free (markname);
1753 }
1754 else {
1755 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1756 }
1757 gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(FALSE));
1758 }
1760 /**
1761 * Updates the marker menus to highlight the appropriate marker and scroll to
1762 * that marker.
1763 */
1764 static void
1765 sp_stroke_style_update_marker_menus( SPWidget *spw,
1766 GSList const *objects)
1767 {
1768 struct { char const *key; int loc; } const keyloc[] = {
1769 { "start_mark_menu", SP_MARKER_LOC_START },
1770 { "mid_mark_menu", SP_MARKER_LOC_MID },
1771 { "end_mark_menu", SP_MARKER_LOC_END }
1772 };
1774 bool all_texts = true;
1775 for (GSList *i = (GSList *) objects; i != NULL; i = i->next) {
1776 if (!SP_IS_TEXT (i->data)) {
1777 all_texts = false;
1778 }
1779 }
1781 for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1782 GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1783 if (all_texts) {
1784 // Per SVG spec, text objects cannot have markers; disable menus if only texts are selected
1785 gtk_widget_set_sensitive (GTK_WIDGET(mnu), FALSE);
1786 } else {
1787 gtk_widget_set_sensitive (GTK_WIDGET(mnu), TRUE);
1788 }
1789 }
1791 // We show markers of the first object in the list only
1792 // FIXME: use the first in the list that has the marker of each type, if any
1793 SPObject *object = SP_OBJECT(objects->data);
1795 for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1796 // For all three marker types,
1798 // find the corresponding menu
1799 GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1801 // Quit if we're in update state
1802 if (gtk_object_get_data(GTK_OBJECT(mnu), "update")) {
1803 return;
1804 }
1806 if (object->style->marker[keyloc[i].loc].value != NULL && !all_texts) {
1807 // If the object has this type of markers,
1809 // Extract the name of the marker that the object uses
1810 SPObject *marker = ink_extract_marker_name(object->style->marker[keyloc[i].loc].value);
1811 // Scroll the menu to that marker
1812 ink_marker_menu_set_current (marker, mnu);
1814 } else {
1815 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1816 }
1817 }
1818 }
1821 /**
1822 * Extract the actual name of the link
1823 * e.g. get mTriangle from url(#mTriangle).
1824 * \return Buffer containing the actual name, allocated from GLib;
1825 * the caller should free the buffer when they no longer need it.
1826 */
1827 static SPObject*
1828 ink_extract_marker_name(gchar const *n)
1829 {
1830 gchar const *p = n;
1831 while (*p != '\0' && *p != '#') {
1832 p++;
1833 }
1835 if (*p == '\0' || p[1] == '\0') {
1836 return NULL;
1837 }
1839 p++;
1840 int c = 0;
1841 while (p[c] != '\0' && p[c] != ')') {
1842 c++;
1843 }
1845 if (p[c] == '\0') {
1846 return NULL;
1847 }
1849 gchar* b = g_strdup(p);
1850 b[c] = '\0';
1853 SPDesktop *desktop = inkscape_active_desktop();
1854 SPDocument *doc = sp_desktop_document(desktop);
1855 SPObject *marker = doc->getObjectById(b);
1856 return marker;
1857 }
1860 /*
1861 Local Variables:
1862 mode:c++
1863 c-file-style:"stroustrup"
1864 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1865 indent-tabs-mode:nil
1866 fill-column:99
1867 End:
1868 */
1869 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :