ed1082e26ef0d3b365fd082247bdf48cbf6661d0
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(SPWidget* spw);
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 /**
188 * Gets the active stroke style property, then sets the appropriate color, alpha, gradient,
189 * pattern, etc. for the paint-selector.
190 */
191 static void
192 sp_stroke_style_paint_update (SPWidget *spw)
193 {
194 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
195 return;
196 }
198 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
200 SPPaintSelector *psel = SP_PAINT_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "paint-selector"));
202 // create temporary style
203 SPStyle *query = sp_style_new ();
204 // query into it
205 int result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKE);
207 switch (result) {
208 case QUERY_STYLE_NOTHING:
209 {
210 /* No paint at all */
211 sp_paint_selector_set_mode (psel, SP_PAINT_SELECTOR_MODE_EMPTY);
212 break;
213 }
215 case QUERY_STYLE_SINGLE:
216 case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently, e.g. display "averaged" somewhere in paint selector
217 case QUERY_STYLE_MULTIPLE_SAME:
218 {
219 SPPaintSelectorMode pselmode = sp_style_determine_paint_selector_mode (query, false);
220 sp_paint_selector_set_mode (psel, pselmode);
222 if (query->stroke.set && query->stroke.type == SP_PAINT_TYPE_COLOR) {
223 gfloat d[3];
224 sp_color_get_rgb_floatv (&query->stroke.value.color, d);
225 SPColor color;
226 sp_color_set_rgb_float (&color, d[0], d[1], d[2]);
227 sp_paint_selector_set_color_alpha (psel, &color, SP_SCALE24_TO_FLOAT (query->stroke_opacity.value));
229 } else if (query->stroke.set && query->stroke.type == SP_PAINT_TYPE_PAINTSERVER) {
231 SPPaintServer *server = SP_STYLE_STROKE_SERVER (query);
233 if (SP_IS_LINEARGRADIENT (server)) {
234 SPGradient *vector = sp_gradient_get_vector (SP_GRADIENT (server), FALSE);
235 sp_paint_selector_set_gradient_linear (psel, vector);
237 SPLinearGradient *lg = SP_LINEARGRADIENT (server);
238 sp_paint_selector_set_gradient_properties (psel,
239 SP_GRADIENT_UNITS (lg),
240 SP_GRADIENT_SPREAD (lg));
241 } else if (SP_IS_RADIALGRADIENT (server)) {
242 SPGradient *vector = sp_gradient_get_vector (SP_GRADIENT (server), FALSE);
243 sp_paint_selector_set_gradient_radial (psel, vector);
245 SPRadialGradient *rg = SP_RADIALGRADIENT (server);
246 sp_paint_selector_set_gradient_properties (psel,
247 SP_GRADIENT_UNITS (rg),
248 SP_GRADIENT_SPREAD (rg));
249 } else if (SP_IS_PATTERN (server)) {
250 SPPattern *pat = pattern_getroot (SP_PATTERN (server));
251 sp_update_pattern_list (psel, pat);
252 }
253 }
254 break;
255 }
257 case QUERY_STYLE_MULTIPLE_DIFFERENT:
258 {
259 sp_paint_selector_set_mode (psel, SP_PAINT_SELECTOR_MODE_MULTIPLE);
260 break;
261 }
262 }
264 sp_style_unref(query);
266 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
267 }
269 /**
270 * When the mode is changed, invoke a regular changed handler.
271 */
272 static void
273 sp_stroke_style_paint_mode_changed( SPPaintSelector *psel,
274 SPPaintSelectorMode mode,
275 SPWidget *spw )
276 {
277 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
278 return;
279 }
281 /* TODO: Does this work?
282 * Not really, here we have to get old color back from object
283 * Instead of relying on paint widget having meaningful colors set
284 */
285 sp_stroke_style_paint_changed(psel, spw);
286 }
288 static gchar *undo_label_1 = "stroke:flatcolor:1";
289 static gchar *undo_label_2 = "stroke:flatcolor:2";
290 static gchar *undo_label = undo_label_1;
292 /**
293 * When a drag callback occurs on a paint selector object, if it is a RGB or CMYK
294 * color mode, then set the stroke opacity to psel's flat color.
295 */
296 static void
297 sp_stroke_style_paint_dragged(SPPaintSelector *psel, SPWidget *spw)
298 {
299 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
300 return;
301 }
303 switch (psel->mode) {
304 case SP_PAINT_SELECTOR_MODE_COLOR_RGB:
305 case SP_PAINT_SELECTOR_MODE_COLOR_CMYK:
306 {
307 sp_paint_selector_set_flat_color (psel, SP_ACTIVE_DESKTOP, "stroke", "stroke-opacity");
308 sp_document_maybe_done (sp_desktop_document(SP_ACTIVE_DESKTOP), undo_label, SP_VERB_DIALOG_FILL_STROKE,
309 _("Set stroke color"));
310 break;
311 }
313 default:
314 g_warning( "file %s: line %d: Paint %d should not emit 'dragged'",
315 __FILE__, __LINE__, psel->mode);
316 break;
317 }
318 }
320 /**
321 * When the stroke style's paint settings change, this handler updates the
322 * repr's stroke css style and applies the style to relevant drawing items.
323 */
324 static void
325 sp_stroke_style_paint_changed(SPPaintSelector *psel, SPWidget *spw)
326 {
327 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
328 return;
329 }
330 g_object_set_data (G_OBJECT (spw), "update", GINT_TO_POINTER (TRUE));
332 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
333 SPDocument *document = sp_desktop_document (desktop);
334 Inkscape::Selection *selection = sp_desktop_selection (desktop);
336 GSList const *items = selection->itemList();
338 switch (psel->mode) {
339 case SP_PAINT_SELECTOR_MODE_EMPTY:
340 // This should not happen.
341 g_warning ( "file %s: line %d: Paint %d should not emit 'changed'",
342 __FILE__, __LINE__, psel->mode);
343 break;
344 case SP_PAINT_SELECTOR_MODE_MULTIPLE:
345 // This happens when you switch multiple objects with different gradients to flat color;
346 // nothing to do here.
347 break;
349 case SP_PAINT_SELECTOR_MODE_NONE:
350 {
351 SPCSSAttr *css = sp_repr_css_attr_new();
352 sp_repr_css_set_property(css, "stroke", "none");
354 sp_desktop_set_style (desktop, css);
356 sp_repr_css_attr_unref(css);
358 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
359 _("Remove stroke"));
360 break;
361 }
363 case SP_PAINT_SELECTOR_MODE_COLOR_RGB:
364 case SP_PAINT_SELECTOR_MODE_COLOR_CMYK:
365 {
366 sp_paint_selector_set_flat_color (psel, desktop, "stroke", "stroke-opacity");
367 sp_document_maybe_done (sp_desktop_document(desktop), undo_label, SP_VERB_DIALOG_FILL_STROKE,
368 _("Set stroke color"));
370 // on release, toggle undo_label so that the next drag will not be lumped with this one
371 if (undo_label == undo_label_1)
372 undo_label = undo_label_2;
373 else
374 undo_label = undo_label_1;
376 break;
377 }
379 case SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR:
380 case SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL:
381 if (items) {
382 SPGradientType const gradient_type = ( psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR
383 ? SP_GRADIENT_TYPE_LINEAR
384 : SP_GRADIENT_TYPE_RADIAL );
385 SPGradient *vector = sp_paint_selector_get_gradient_vector(psel);
386 if (!vector) {
387 /* No vector in paint selector should mean that we just changed mode */
389 SPStyle *query = sp_style_new ();
390 int result = objects_query_fillstroke ((GSList *) items, query, false);
391 guint32 common_rgb = 0;
392 if (result == QUERY_STYLE_MULTIPLE_SAME) {
393 if (query->fill.type != SP_PAINT_TYPE_COLOR) {
394 common_rgb = sp_desktop_get_color(desktop, false);
395 } else {
396 common_rgb = sp_color_get_rgba32_ualpha(&query->stroke.value.color, 0xff);
397 }
398 vector = sp_document_default_gradient_vector(document, common_rgb);
399 }
400 sp_style_unref(query);
402 for (GSList const *i = items; i != NULL; i = i->next) {
403 if (!vector) {
404 sp_item_set_gradient(SP_ITEM(i->data),
405 sp_gradient_vector_for_object(document, desktop, SP_OBJECT(i->data), false),
406 gradient_type, false);
407 } else {
408 sp_item_set_gradient(SP_ITEM(i->data), vector, gradient_type, false);
409 }
410 }
411 } else {
412 vector = sp_gradient_ensure_vector_normalized(vector);
413 for (GSList const *i = items; i != NULL; i = i->next) {
414 SPGradient *gr = sp_item_set_gradient(SP_ITEM(i->data), vector, gradient_type, false);
415 sp_gradient_selector_attrs_to_gradient(gr, psel);
416 }
417 }
419 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
420 _("Set gradient on stroke"));
421 }
422 break;
424 case SP_PAINT_SELECTOR_MODE_PATTERN:
426 if (items) {
428 SPPattern *pattern = sp_paint_selector_get_pattern (psel);
429 if (!pattern) {
431 /* No Pattern in paint selector should mean that we just
432 * changed mode - dont do jack.
433 */
435 } else {
436 Inkscape::XML::Node *patrepr = SP_OBJECT_REPR(pattern);
437 SPCSSAttr *css = sp_repr_css_attr_new ();
438 gchar *urltext = g_strdup_printf ("url(#%s)", patrepr->attribute("id"));
439 sp_repr_css_set_property (css, "stroke", urltext);
441 for (GSList const *i = items; i != NULL; i = i->next) {
442 Inkscape::XML::Node *selrepr = SP_OBJECT_REPR (i->data);
443 SPObject *selobj = SP_OBJECT (i->data);
444 if (!selrepr)
445 continue;
447 SPStyle *style = SP_OBJECT_STYLE (selobj);
448 if (style && style->stroke.type == SP_PAINT_TYPE_PAINTSERVER) {
449 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (selobj);
450 if (SP_IS_PATTERN (server) && pattern_getroot (SP_PATTERN(server)) == pattern)
451 // only if this object's pattern is not rooted in our selected pattern, apply
452 continue;
453 }
455 sp_repr_css_change_recursive (selrepr, css, "style");
456 }
458 sp_repr_css_attr_unref (css);
459 g_free (urltext);
461 } // end if
463 sp_document_done (document, SP_VERB_DIALOG_FILL_STROKE,
464 _("Set pattern on stroke"));
465 } // end if
467 break;
469 case SP_PAINT_SELECTOR_MODE_UNSET:
470 if (items) {
471 SPCSSAttr *css = sp_repr_css_attr_new ();
472 sp_repr_css_unset_property (css, "stroke");
474 sp_desktop_set_style (desktop, css);
475 sp_repr_css_attr_unref (css);
477 sp_document_done (document, SP_VERB_DIALOG_FILL_STROKE,
478 _("Unset stroke"));
479 }
480 break;
482 default:
483 g_warning( "file %s: line %d: Paint selector should not be in "
484 "mode %d",
485 __FILE__, __LINE__,
486 psel->mode );
487 break;
488 }
490 g_object_set_data (G_OBJECT (spw), "update", GINT_TO_POINTER (FALSE));
491 }
497 /* Line */
499 static void sp_stroke_style_line_construct(SPWidget *spw, gpointer data);
500 static void sp_stroke_style_line_selection_modified (SPWidget *spw,
501 Inkscape::Selection *selection,
502 guint flags,
503 gpointer data);
505 static void sp_stroke_style_line_selection_changed (SPWidget *spw,
506 Inkscape::Selection *selection,
507 gpointer data );
509 static void sp_stroke_style_line_update(SPWidget *spw, Inkscape::Selection *sel);
511 static void sp_stroke_style_set_join_buttons(SPWidget *spw,
512 GtkWidget *active);
514 static void sp_stroke_style_set_cap_buttons(SPWidget *spw,
515 GtkWidget *active);
517 static void sp_stroke_style_width_changed(GtkAdjustment *adj, SPWidget *spw);
518 static void sp_stroke_style_miterlimit_changed(GtkAdjustment *adj, SPWidget *spw);
519 static void sp_stroke_style_any_toggled(GtkToggleButton *tb, SPWidget *spw);
520 static void sp_stroke_style_line_dash_changed(SPDashSelector *dsel,
521 SPWidget *spw);
523 static void sp_stroke_style_update_marker_menus(SPWidget *spw, GSList const *objects);
526 /**
527 * Helper function for creating radio buttons. This should probably be re-thought out
528 * when reimplementing this with Gtkmm.
529 */
530 static GtkWidget *
531 sp_stroke_radio_button(GtkWidget *tb, char const *icon,
532 GtkWidget *hb, GtkWidget *spw,
533 gchar const *key, gchar const *data)
534 {
535 g_assert(icon != NULL);
536 g_assert(hb != NULL);
537 g_assert(spw != NULL);
539 if (tb == NULL) {
540 tb = gtk_radio_button_new(NULL);
541 } else {
542 tb = gtk_radio_button_new(gtk_radio_button_group(GTK_RADIO_BUTTON(tb)) );
543 }
545 gtk_widget_show(tb);
546 gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(tb), FALSE);
547 gtk_box_pack_start(GTK_BOX(hb), tb, FALSE, FALSE, 0);
548 gtk_object_set_data(GTK_OBJECT(spw), icon, tb);
549 gtk_object_set_data(GTK_OBJECT(tb), key, (gpointer*)data);
550 gtk_signal_connect(GTK_OBJECT(tb), "toggled",
551 GTK_SIGNAL_FUNC(sp_stroke_style_any_toggled),
552 spw);
553 GtkWidget *px = sp_icon_new(Inkscape::ICON_SIZE_LARGE_TOOLBAR, icon);
554 g_assert(px != NULL);
555 gtk_widget_show(px);
556 gtk_container_add(GTK_CONTAINER(tb), px);
558 return tb;
560 }
562 static void
563 sp_stroke_style_widget_transientize_callback(Inkscape::Application *inkscape,
564 SPDesktop *desktop,
565 SPWidget *spw )
566 {
567 // TODO: Either of these will cause crashes sometimes
568 // sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
569 // ink_markers_menu_update(spw);
570 }
572 /**
573 * Creates a copy of the marker named mname, determines its visible and renderable
574 * area in menu_id's bounding box, and then renders it. This allows us to fill in
575 * preview images of each marker in the marker menu.
576 */
577 static GtkWidget *
578 sp_marker_prev_new(unsigned psize, gchar const *mname,
579 SPDocument *source, SPDocument *sandbox,
580 gchar *menu_id, NRArena const *arena, unsigned visionkey, NRArenaItem *root)
581 {
582 // Retrieve the marker named 'mname' from the source SVG document
583 SPObject const *marker = source->getObjectById(mname);
584 if (marker == NULL)
585 return NULL;
587 // Create a copy repr of the marker with id="sample"
588 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(sandbox);
589 Inkscape::XML::Node *mrepr = SP_OBJECT_REPR (marker)->duplicate(xml_doc);
590 mrepr->setAttribute("id", "sample");
592 // Replace the old sample in the sandbox by the new one
593 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (sandbox->getObjectById("defs"));
594 SPObject *oldmarker = sandbox->getObjectById("sample");
595 if (oldmarker)
596 oldmarker->deleteObject(false);
597 defsrepr->appendChild(mrepr);
598 Inkscape::GC::release(mrepr);
600 // Uncomment this to get the sandbox documents saved (useful for debugging)
601 //FILE *fp = fopen (g_strconcat(mname, ".svg", NULL), "w");
602 //sp_repr_save_stream (sp_document_repr_doc (sandbox), fp);
603 //fclose (fp);
605 // object to render; note that the id is the same as that of the menu we're building
606 SPObject *object = sandbox->getObjectById(menu_id);
607 sp_document_root (sandbox)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
608 sp_document_ensure_up_to_date(sandbox);
610 if (object == NULL || !SP_IS_ITEM(object))
611 return NULL; // sandbox broken?
613 // Find object's bbox in document
614 NR::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
615 NR::Maybe<NR::Rect> dbox = SP_ITEM(object)->getBounds(i2doc);
617 if (!dbox) {
618 return NULL;
619 }
621 /* Update to renderable state */
622 double sf = 0.8;
623 GdkPixbuf* pixbuf = NULL;
625 Glib::ustring key = svg_preview_cache.cache_key(source->uri, mname, psize);
626 pixbuf = svg_preview_cache.get_preview_from_cache(key);
628 if (pixbuf == NULL) {
629 pixbuf = render_pixbuf(root, sf, *dbox, psize);
630 svg_preview_cache.set_preview_in_cache(key, pixbuf);
631 }
633 // Create widget
634 GtkWidget *pb = gtk_image_new_from_pixbuf(pixbuf);
636 return pb;
637 }
640 /**
641 * Returns a list of markers in the defs of the given source document as a GSList object
642 * Returns NULL if there are no markers in the document.
643 */
644 GSList *
645 ink_marker_list_get (SPDocument *source)
646 {
647 if (source == NULL)
648 return NULL;
650 GSList *ml = NULL;
651 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS (source);
652 for ( SPObject *child = sp_object_first_child(SP_OBJECT(defs));
653 child != NULL;
654 child = SP_OBJECT_NEXT (child) )
655 {
656 if (SP_IS_MARKER(child)) {
657 ml = g_slist_prepend (ml, child);
658 }
659 }
660 return ml;
661 }
663 #define MARKER_ITEM_MARGIN 0
665 /**
666 * Adds previews of markers in marker_list to the given menu widget
667 */
668 static void
669 sp_marker_menu_build (GtkWidget *m, GSList *marker_list, SPDocument *source, SPDocument *sandbox, gchar *menu_id)
670 {
671 // Do this here, outside of loop, to speed up preview generation:
672 NRArena const *arena = NRArena::create();
673 unsigned const visionkey = sp_item_display_key_new(1);
674 NRArenaItem *root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT (sandbox)), (NRArena *) arena, visionkey, SP_ITEM_SHOW_DISPLAY );
676 for (; marker_list != NULL; marker_list = marker_list->next) {
677 Inkscape::XML::Node *repr = SP_OBJECT_REPR((SPItem *) marker_list->data);
678 GtkWidget *i = gtk_menu_item_new();
679 gtk_widget_show(i);
681 if (repr->attribute("inkscape:stockid"))
682 g_object_set_data (G_OBJECT(i), "stockid", (void *) "true");
683 else
684 g_object_set_data (G_OBJECT(i), "stockid", (void *) "false");
686 gchar const *markid = repr->attribute("id");
687 g_object_set_data (G_OBJECT(i), "marker", (void *) markid);
689 GtkWidget *hb = gtk_hbox_new(FALSE, MARKER_ITEM_MARGIN);
690 gtk_widget_show(hb);
692 // generate preview
694 GtkWidget *prv = sp_marker_prev_new (22, markid, source, sandbox, menu_id, arena, visionkey, root);
695 gtk_widget_show(prv);
696 gtk_box_pack_start(GTK_BOX(hb), prv, FALSE, FALSE, 6);
698 // create label
699 GtkWidget *l = gtk_label_new(repr->attribute("id"));
700 gtk_widget_show(l);
701 gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
703 gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
705 gtk_widget_show(hb);
706 gtk_container_add(GTK_CONTAINER(i), hb);
708 gtk_menu_append(GTK_MENU(m), i);
709 }
710 }
712 /**
713 * sp_marker_list_from_doc()
714 *
715 * \brief Pick up all markers from source, except those that are in
716 * current_doc (if non-NULL), and add items to the m menu
717 *
718 */
719 static void
720 sp_marker_list_from_doc (GtkWidget *m, SPDocument *current_doc, SPDocument *source, SPDocument *markers_doc, SPDocument *sandbox, gchar *menu_id)
721 {
722 GSList *ml = ink_marker_list_get(source);
723 GSList *clean_ml = NULL;
725 for (; ml != NULL; ml = ml->next) {
726 if (!SP_IS_MARKER(ml->data))
727 continue;
729 // Add to the list of markers we really do wish to show
730 clean_ml = g_slist_prepend (clean_ml, ml->data);
731 }
732 sp_marker_menu_build (m, clean_ml, source, sandbox, menu_id);
734 g_slist_free (ml);
735 g_slist_free (clean_ml);
736 }
739 /**
740 * Returns a new document containing default start, mid, and end markers.
741 */
742 SPDocument *
743 ink_markers_preview_doc ()
744 {
745 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\">"
746 " <defs id=\"defs\" />"
748 " <g id=\"marker-start\">"
749 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:url(#sample);marker-mid:none;marker-end:none\""
750 " d=\"M 12.5,13 L 25,13\" id=\"path1\" />"
751 " <rect style=\"fill:none;stroke:none\" id=\"rect2\""
752 " width=\"25\" height=\"25\" x=\"0\" y=\"0\" />"
753 " </g>"
755 " <g id=\"marker-mid\">"
756 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:url(#sample);marker-end:none\""
757 " d=\"M 0,113 L 12.5,113 L 25,113\" id=\"path11\" />"
758 " <rect style=\"fill:none;stroke:none\" id=\"rect22\""
759 " width=\"25\" height=\"25\" x=\"0\" y=\"100\" />"
760 " </g>"
762 " <g id=\"marker-end\">"
763 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:none;marker-end:url(#sample)\""
764 " d=\"M 0,213 L 12.5,213\" id=\"path111\" />"
765 " <rect style=\"fill:none;stroke:none\" id=\"rect222\""
766 " width=\"25\" height=\"25\" x=\"0\" y=\"200\" />"
767 " </g>"
769 "</svg>";
771 return sp_document_new_from_mem (buffer, strlen(buffer), FALSE);
772 }
774 static void
775 ink_marker_menu_create_menu(GtkWidget *m, gchar *menu_id, SPDocument *doc, SPDocument *sandbox)
776 {
777 static SPDocument *markers_doc = NULL;
779 // add "None"
780 GtkWidget *i = gtk_menu_item_new();
781 gtk_widget_show(i);
783 g_object_set_data(G_OBJECT(i), "marker", (void *) "none");
785 GtkWidget *hb = gtk_hbox_new(FALSE, MARKER_ITEM_MARGIN);
786 gtk_widget_show(hb);
788 GtkWidget *l = gtk_label_new( _("None") );
789 gtk_widget_show(l);
790 gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
792 gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
794 gtk_widget_show(hb);
795 gtk_container_add(GTK_CONTAINER(i), hb);
796 gtk_menu_append(GTK_MENU(m), i);
798 // find and load markers.svg
799 if (markers_doc == NULL) {
800 char *markers_source = g_build_filename(INKSCAPE_MARKERSDIR, "markers.svg", NULL);
801 if (Inkscape::IO::file_test(markers_source, G_FILE_TEST_IS_REGULAR)) {
802 markers_doc = sp_document_new(markers_source, FALSE);
803 }
804 g_free(markers_source);
805 }
807 // suck in from current doc
808 sp_marker_list_from_doc ( m, NULL, doc, markers_doc, sandbox, menu_id );
810 // add separator
811 {
812 GtkWidget *i = gtk_separator_menu_item_new();
813 gtk_widget_show(i);
814 gtk_menu_append(GTK_MENU(m), i);
815 }
817 // suck in from markers.svg
818 if (markers_doc) {
819 sp_document_ensure_up_to_date(doc);
820 sp_marker_list_from_doc ( m, doc, markers_doc, NULL, sandbox, menu_id );
821 }
823 }
826 /**
827 * Creates a menu widget to display markers from markers.svg
828 */
829 static GtkWidget *
830 ink_marker_menu( GtkWidget *tbl, gchar *menu_id, SPDocument *sandbox)
831 {
832 SPDesktop *desktop = inkscape_active_desktop();
833 SPDocument *doc = sp_desktop_document(desktop);
834 GtkWidget *mnu = gtk_option_menu_new();
836 /* Create new menu widget */
837 GtkWidget *m = gtk_menu_new();
838 gtk_widget_show(m);
840 g_object_set_data(G_OBJECT(mnu), "updating", (gpointer) FALSE);
842 if (!doc) {
843 GtkWidget *i = gtk_menu_item_new_with_label(_("No document selected"));
844 gtk_widget_show(i);
845 gtk_menu_append(GTK_MENU(m), i);
846 gtk_widget_set_sensitive(mnu, FALSE);
848 } else {
849 ink_marker_menu_create_menu(m, menu_id, doc, sandbox);
851 gtk_widget_set_sensitive(mnu, TRUE);
852 }
854 gtk_object_set_data(GTK_OBJECT(mnu), "menu_id", menu_id);
855 gtk_option_menu_set_menu(GTK_OPTION_MENU(mnu), m);
857 /* Set history */
858 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
860 return mnu;
861 }
864 /**
865 * Handles when user selects one of the markers from the marker menu.
866 * Defines a uri string to refer to it, then applies it to all selected
867 * items in the current desktop.
868 */
869 static void
870 sp_marker_select(GtkOptionMenu *mnu, GtkWidget *spw)
871 {
872 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
873 return;
874 }
876 SPDesktop *desktop = inkscape_active_desktop();
877 SPDocument *document = sp_desktop_document(desktop);
878 if (!document) {
879 return;
880 }
882 /* Get Marker */
883 if (!g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
884 "marker"))
885 {
886 return;
887 }
888 gchar *markid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
889 "marker");
890 gchar *marker = "";
891 if (strcmp(markid, "none")){
892 gchar *stockid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
893 "stockid");
895 gchar *markurn = markid;
896 if (!strcmp(stockid,"true")) markurn = g_strconcat("urn:inkscape:marker:",markid,NULL);
897 SPObject *mark = get_stock_item(markurn);
898 if (mark) {
899 Inkscape::XML::Node *repr = SP_OBJECT_REPR(mark);
900 marker = g_strconcat("url(#", repr->attribute("id"), ")", NULL);
901 }
902 } else {
903 marker = markid;
904 }
906 SPCSSAttr *css = sp_repr_css_attr_new();
907 gchar *menu_id = (gchar *) g_object_get_data(G_OBJECT(mnu), "menu_id");
908 sp_repr_css_set_property(css, menu_id, marker);
910 // Also update the marker dropdown menus, so the document's markers
911 // show up at the top of the menu
912 // sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
913 ink_markers_menu_update(SP_WIDGET(spw));
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 };
936 static int
937 ink_marker_menu_get_pos(GtkMenu* mnu, gchar* markname) {
939 if (markname == NULL)
940 markname = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(mnu)), "marker");
942 if (markname == NULL)
943 return 0;
945 GList *kids = GTK_MENU_SHELL(mnu)->children;
946 int i = 0;
947 for (; kids != NULL; kids = kids->next) {
948 gchar *mark = (gchar *) g_object_get_data(G_OBJECT(kids->data), "marker");
949 if ( mark && strcmp(mark, markname) == 0 ) {
950 break;
951 }
952 i++;
953 }
954 return i;
955 }
957 static void
958 ink_markers_menu_update(SPWidget* spw) {
959 SPDesktop *desktop = inkscape_active_desktop();
960 SPDocument *document = sp_desktop_document(desktop);
961 SPDocument *sandbox = ink_markers_preview_doc ();
962 GtkWidget *m;
963 int pos;
965 gtk_signal_handler_block_by_func( GTK_OBJECT(marker_start_menu), GTK_SIGNAL_FUNC(sp_marker_select), spw);
966 pos = ink_marker_menu_get_pos(GTK_MENU(gtk_option_menu_get_menu(GTK_OPTION_MENU(marker_start_menu))), NULL);
967 m = gtk_menu_new();
968 gtk_widget_show(m);
969 ink_marker_menu_create_menu(m, "marker-start", document, sandbox);
970 gtk_option_menu_remove_menu(GTK_OPTION_MENU(marker_start_menu));
971 gtk_option_menu_set_menu(GTK_OPTION_MENU(marker_start_menu), m);
972 gtk_option_menu_set_history(GTK_OPTION_MENU(marker_start_menu), pos);
973 gtk_signal_handler_unblock_by_func( GTK_OBJECT(marker_start_menu), GTK_SIGNAL_FUNC(sp_marker_select), spw);
975 gtk_signal_handler_block_by_func( GTK_OBJECT(marker_mid_menu), GTK_SIGNAL_FUNC(sp_marker_select), spw);
976 pos = ink_marker_menu_get_pos(GTK_MENU(gtk_option_menu_get_menu(GTK_OPTION_MENU(marker_mid_menu))), NULL);
977 m = gtk_menu_new();
978 gtk_widget_show(m);
979 ink_marker_menu_create_menu(m, "marker-mid", document, sandbox);
980 gtk_option_menu_remove_menu(GTK_OPTION_MENU(marker_mid_menu));
981 gtk_option_menu_set_menu(GTK_OPTION_MENU(marker_mid_menu), m);
982 gtk_option_menu_set_history(GTK_OPTION_MENU(marker_mid_menu), pos);
983 gtk_signal_handler_unblock_by_func( GTK_OBJECT(marker_mid_menu), GTK_SIGNAL_FUNC(sp_marker_select), spw);
985 gtk_signal_handler_block_by_func( GTK_OBJECT(marker_end_menu), GTK_SIGNAL_FUNC(sp_marker_select), spw);
986 pos = ink_marker_menu_get_pos(GTK_MENU(gtk_option_menu_get_menu(GTK_OPTION_MENU(marker_end_menu))), NULL);
987 m = gtk_menu_new();
988 gtk_widget_show(m);
989 ink_marker_menu_create_menu(m, "marker-end", document, sandbox);
990 gtk_option_menu_remove_menu(GTK_OPTION_MENU(marker_end_menu));
991 gtk_option_menu_set_menu(GTK_OPTION_MENU(marker_end_menu), m);
992 gtk_option_menu_set_history(GTK_OPTION_MENU(marker_end_menu), pos);
993 gtk_signal_handler_unblock_by_func( GTK_OBJECT(marker_end_menu), GTK_SIGNAL_FUNC(sp_marker_select), spw);
994 }
996 /**
997 * Sets the stroke width units for all selected items.
998 * Also handles absolute and dimensionless units.
999 */
1000 static gboolean stroke_width_set_unit(SPUnitSelector *,
1001 SPUnit const *old,
1002 SPUnit const *new_units,
1003 GObject *spw)
1004 {
1005 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1007 if (!desktop) {
1008 return FALSE;
1009 }
1011 Inkscape::Selection *selection = sp_desktop_selection (desktop);
1013 if (selection->isEmpty())
1014 return FALSE;
1016 GSList const *objects = selection->itemList();
1018 if ((old->base == SP_UNIT_ABSOLUTE || old->base == SP_UNIT_DEVICE) &&
1019 (new_units->base == SP_UNIT_DIMENSIONLESS)) {
1021 /* Absolute to percentage */
1022 g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
1024 GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
1025 float w = sp_units_get_pixels (a->value, *old);
1027 gdouble average = stroke_average_width (objects);
1029 if (average == NR_HUGE || average == 0)
1030 return FALSE;
1032 gtk_adjustment_set_value (a, 100.0 * w / average);
1034 g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
1035 return TRUE;
1037 } else if ((old->base == SP_UNIT_DIMENSIONLESS) &&
1038 (new_units->base == SP_UNIT_ABSOLUTE || new_units->base == SP_UNIT_DEVICE)) {
1040 /* Percentage to absolute */
1041 g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
1043 GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
1045 gdouble average = stroke_average_width (objects);
1047 gtk_adjustment_set_value (a, sp_pixels_get_units (0.01 * a->value * average, *new_units));
1049 g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
1050 return TRUE;
1051 }
1053 return FALSE;
1054 }
1057 /**
1058 * \brief Creates a new widget for the line stroke style.
1059 *
1060 */
1061 GtkWidget *
1062 sp_stroke_style_line_widget_new(void)
1063 {
1064 GtkWidget *spw, *f, *t, *hb, *sb, *us, *tb, *ds;
1065 GtkObject *a;
1067 GtkTooltips *tt = gtk_tooltips_new();
1069 spw = sp_widget_new_global(INKSCAPE);
1071 f = gtk_hbox_new (FALSE, 0);
1072 gtk_widget_show(f);
1073 gtk_container_add(GTK_CONTAINER(spw), f);
1075 t = gtk_table_new(3, 6, FALSE);
1076 gtk_widget_show(t);
1077 gtk_container_set_border_width(GTK_CONTAINER(t), 4);
1078 gtk_table_set_row_spacings(GTK_TABLE(t), 4);
1079 gtk_container_add(GTK_CONTAINER(f), t);
1080 gtk_object_set_data(GTK_OBJECT(spw), "stroke", t);
1082 gint i = 0;
1084 /* Stroke width */
1085 spw_label(t, _("Width:"), 0, i);
1087 hb = spw_hbox(t, 3, 1, i);
1089 // TODO: when this is gtkmmified, use an Inkscape::UI::Widget::ScalarUnit instead of the separate
1090 // spinbutton and unit selector for stroke width. In sp_stroke_style_line_update, use
1091 // setHundredPercent to remember the aeraged width corresponding to 100%. Then the
1092 // stroke_width_set_unit will be removed (because ScalarUnit takes care of conversions itself), and
1093 // with it, the two remaining calls of stroke_average_width, allowing us to get rid of that
1094 // function in desktop-style.
1096 a = gtk_adjustment_new(1.0, 0.0, 1000.0, 0.1, 10.0, 10.0);
1097 gtk_object_set_data(GTK_OBJECT(spw), "width", a);
1098 sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 3);
1099 gtk_tooltips_set_tip(tt, sb, _("Stroke width"), NULL);
1100 gtk_widget_show(sb);
1102 sp_dialog_defocus_on_enter(sb);
1104 gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1105 us = sp_unit_selector_new(SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE);
1106 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1107 if (desktop)
1108 sp_unit_selector_set_unit (SP_UNIT_SELECTOR(us), sp_desktop_namedview(desktop)->doc_units);
1109 sp_unit_selector_add_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT), 0);
1110 g_signal_connect ( G_OBJECT (us), "set_unit", G_CALLBACK (stroke_width_set_unit), spw );
1111 gtk_widget_show(us);
1112 sp_unit_selector_add_adjustment( SP_UNIT_SELECTOR(us), GTK_ADJUSTMENT(a) );
1113 gtk_box_pack_start(GTK_BOX(hb), us, FALSE, FALSE, 0);
1114 gtk_object_set_data(GTK_OBJECT(spw), "units", us);
1116 gtk_signal_connect( GTK_OBJECT(a), "value_changed", GTK_SIGNAL_FUNC(sp_stroke_style_width_changed), spw );
1117 i++;
1119 /* Join type */
1120 // TRANSLATORS: The line join style specifies the shape to be used at the
1121 // corners of paths. It can be "miter", "round" or "bevel".
1122 spw_label(t, _("Join:"), 0, i);
1124 hb = spw_hbox(t, 3, 1, i);
1126 tb = NULL;
1128 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_MITER,
1129 hb, spw, "join", "miter");
1131 // TRANSLATORS: Miter join: joining lines with a sharp (pointed) corner.
1132 // For an example, draw a triangle with a large stroke width and modify the
1133 // "Join" option (in the Fill and Stroke dialog).
1134 gtk_tooltips_set_tip(tt, tb, _("Miter join"), NULL);
1136 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_ROUND,
1137 hb, spw, "join", "round");
1139 // TRANSLATORS: Round join: joining lines with a rounded corner.
1140 // For an example, draw a triangle with a large stroke width and modify the
1141 // "Join" option (in the Fill and Stroke dialog).
1142 gtk_tooltips_set_tip(tt, tb, _("Round join"), NULL);
1144 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_BEVEL,
1145 hb, spw, "join", "bevel");
1147 // TRANSLATORS: Bevel join: joining lines with a blunted (flattened) corner.
1148 // For an example, draw a triangle with a large stroke width and modify the
1149 // "Join" option (in the Fill and Stroke dialog).
1150 gtk_tooltips_set_tip(tt, tb, _("Bevel join"), NULL);
1152 i++;
1154 /* Miterlimit */
1155 // TRANSLATORS: Miter limit: only for "miter join", this limits the length
1156 // of the sharp "spike" when the lines connect at too sharp an angle.
1157 // When two line segments meet at a sharp angle, a miter join results in a
1158 // spike that extends well beyond the connection point. The purpose of the
1159 // miter limit is to cut off such spikes (i.e. convert them into bevels)
1160 // when they become too long.
1161 spw_label(t, _("Miter limit:"), 0, i);
1163 hb = spw_hbox(t, 3, 1, i);
1165 a = gtk_adjustment_new(4.0, 0.0, 100.0, 0.1, 10.0, 10.0);
1166 gtk_object_set_data(GTK_OBJECT(spw), "miterlimit", a);
1168 sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 2);
1169 gtk_tooltips_set_tip(tt, sb, _("Maximum length of the miter (in units of stroke width)"), NULL);
1170 gtk_widget_show(sb);
1171 gtk_object_set_data(GTK_OBJECT(spw), "miterlimit_sb", sb);
1172 sp_dialog_defocus_on_enter(sb);
1174 gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1176 gtk_signal_connect( GTK_OBJECT(a), "value_changed",
1177 GTK_SIGNAL_FUNC(sp_stroke_style_miterlimit_changed), spw );
1178 i++;
1180 /* Cap type */
1181 // TRANSLATORS: cap type specifies the shape for the ends of lines
1182 spw_label(t, _("Cap:"), 0, i);
1184 hb = spw_hbox(t, 3, 1, i);
1186 tb = NULL;
1188 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_BUTT,
1189 hb, spw, "cap", "butt");
1191 // TRANSLATORS: Butt cap: the line shape does not extend beyond the end point
1192 // of the line; the ends of the line are square
1193 gtk_tooltips_set_tip(tt, tb, _("Butt cap"), NULL);
1195 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_ROUND,
1196 hb, spw, "cap", "round");
1198 // TRANSLATORS: Round cap: the line shape extends beyond the end point of the
1199 // line; the ends of the line are rounded
1200 gtk_tooltips_set_tip(tt, tb, _("Round cap"), NULL);
1202 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_SQUARE,
1203 hb, spw, "cap", "square");
1205 // TRANSLATORS: Square cap: the line shape extends beyond the end point of the
1206 // line; the ends of the line are square
1207 gtk_tooltips_set_tip(tt, tb, _("Square cap"), NULL);
1209 i++;
1212 /* Dash */
1213 spw_label(t, _("Dashes:"), 0, i);
1214 ds = sp_dash_selector_new( inkscape_get_repr( INKSCAPE,
1215 "palette.dashes") );
1217 gtk_widget_show(ds);
1218 gtk_table_attach( GTK_TABLE(t), ds, 1, 4, i, i+1,
1219 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1220 (GtkAttachOptions)0, 0, 0 );
1221 gtk_object_set_data(GTK_OBJECT(spw), "dash", ds);
1222 gtk_signal_connect( GTK_OBJECT(ds), "changed",
1223 GTK_SIGNAL_FUNC(sp_stroke_style_line_dash_changed),
1224 spw );
1225 i++;
1227 /* Drop down marker selectors*/
1229 // doing this here once, instead of for each preview, to speed things up
1230 SPDocument *sandbox = ink_markers_preview_doc ();
1232 // TRANSLATORS: Path markers are an SVG feature that allows you to attach arbitrary shapes
1233 // (arrowheads, bullets, faces, whatever) to the start, end, or middle nodes of a path.
1234 spw_label(t, _("Start Markers:"), 0, i);
1235 marker_start_menu = ink_marker_menu( spw ,"marker-start", sandbox);
1236 gtk_signal_connect( GTK_OBJECT(marker_start_menu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1237 gtk_widget_show(marker_start_menu);
1238 gtk_table_attach( GTK_TABLE(t), marker_start_menu, 1, 4, i, i+1,
1239 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1240 (GtkAttachOptions)0, 0, 0 );
1241 gtk_object_set_data(GTK_OBJECT(spw), "start_mark_menu", marker_start_menu);
1243 i++;
1244 spw_label(t, _("Mid Markers:"), 0, i);
1245 marker_mid_menu = ink_marker_menu( spw ,"marker-mid", sandbox);
1246 gtk_signal_connect( GTK_OBJECT(marker_mid_menu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1247 gtk_widget_show(marker_mid_menu);
1248 gtk_table_attach( GTK_TABLE(t), marker_mid_menu, 1, 4, i, i+1,
1249 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1250 (GtkAttachOptions)0, 0, 0 );
1251 gtk_object_set_data(GTK_OBJECT(spw), "mid_mark_menu", marker_mid_menu);
1253 i++;
1254 spw_label(t, _("End Markers:"), 0, i);
1255 marker_end_menu = ink_marker_menu( spw ,"marker-end", sandbox);
1256 gtk_signal_connect( GTK_OBJECT(marker_end_menu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1257 gtk_widget_show(marker_end_menu);
1258 gtk_table_attach( GTK_TABLE(t), marker_end_menu, 1, 4, i, i+1,
1259 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1260 (GtkAttachOptions)0, 0, 0 );
1261 gtk_object_set_data(GTK_OBJECT(spw), "end_mark_menu", marker_end_menu);
1263 i++;
1265 gtk_signal_connect( GTK_OBJECT(spw), "construct",
1266 GTK_SIGNAL_FUNC(sp_stroke_style_line_construct),
1267 NULL );
1268 gtk_signal_connect( GTK_OBJECT(spw), "modify_selection",
1269 GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_modified),
1270 NULL );
1271 gtk_signal_connect( GTK_OBJECT(spw), "change_selection",
1272 GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_changed),
1273 NULL );
1275 sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
1277 return spw;
1278 }
1281 /**
1282 * Callback for when the stroke style widget is called. It causes
1283 * the stroke line style to be updated.
1284 */
1285 static void
1286 sp_stroke_style_line_construct(SPWidget *spw, gpointer data)
1287 {
1289 #ifdef SP_SS_VERBOSE
1290 g_print( "Stroke style widget constructed: inkscape %p repr %p\n",
1291 spw->inkscape, spw->repr );
1292 #endif
1293 if (spw->inkscape) {
1294 sp_stroke_style_line_update(spw,
1295 ( SP_ACTIVE_DESKTOP
1296 ? sp_desktop_selection(SP_ACTIVE_DESKTOP)
1297 : NULL ));
1298 }
1299 }
1301 /**
1302 * Callback for when stroke style widget is modified.
1303 * Triggers update action.
1304 */
1305 static void
1306 sp_stroke_style_line_selection_modified ( SPWidget *spw,
1307 Inkscape::Selection *selection,
1308 guint flags,
1309 gpointer data )
1310 {
1311 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1312 sp_stroke_style_line_update (spw, selection);
1313 }
1315 }
1317 /**
1318 * Callback for when stroke style widget is changed.
1319 * Triggers update action.
1320 */
1321 static void
1322 sp_stroke_style_line_selection_changed ( SPWidget *spw,
1323 Inkscape::Selection *selection,
1324 gpointer data )
1325 {
1326 sp_stroke_style_line_update (spw, selection);
1327 }
1330 /**
1331 * Sets selector widgets' dash style from an SPStyle object.
1332 */
1333 static void
1334 sp_dash_selector_set_from_style (GtkWidget *dsel, SPStyle *style)
1335 {
1336 if (style->stroke_dash.n_dash > 0) {
1337 double d[64];
1338 int len = MIN(style->stroke_dash.n_dash, 64);
1339 for (int i = 0; i < len; i++) {
1340 if (style->stroke_width.computed != 0)
1341 d[i] = style->stroke_dash.dash[i] / style->stroke_width.computed;
1342 else
1343 d[i] = style->stroke_dash.dash[i]; // is there a better thing to do for stroke_width==0?
1344 }
1345 sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), len, d,
1346 style->stroke_width.computed != 0?
1347 style->stroke_dash.offset / style->stroke_width.computed :
1348 style->stroke_dash.offset);
1349 } else {
1350 sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), 0, NULL, 0.0);
1351 }
1352 }
1354 /**
1355 * Sets the join type for a line, and updates the stroke style widget's buttons
1356 */
1357 static void
1358 sp_jointype_set (SPWidget *spw, unsigned const jointype)
1359 {
1360 GtkWidget *tb = NULL;
1361 switch (jointype) {
1362 case SP_STROKE_LINEJOIN_MITER:
1363 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_MITER));
1364 break;
1365 case SP_STROKE_LINEJOIN_ROUND:
1366 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_ROUND));
1367 break;
1368 case SP_STROKE_LINEJOIN_BEVEL:
1369 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_BEVEL));
1370 break;
1371 default:
1372 break;
1373 }
1374 sp_stroke_style_set_join_buttons (spw, tb);
1375 }
1377 /**
1378 * Sets the cap type for a line, and updates the stroke style widget's buttons
1379 */
1380 static void
1381 sp_captype_set (SPWidget *spw, unsigned const captype)
1382 {
1383 GtkWidget *tb = NULL;
1384 switch (captype) {
1385 case SP_STROKE_LINECAP_BUTT:
1386 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_BUTT));
1387 break;
1388 case SP_STROKE_LINECAP_ROUND:
1389 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_ROUND));
1390 break;
1391 case SP_STROKE_LINECAP_SQUARE:
1392 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_SQUARE));
1393 break;
1394 default:
1395 break;
1396 }
1397 sp_stroke_style_set_cap_buttons (spw, tb);
1398 }
1400 /**
1401 * Callback for when stroke style widget is updated, including markers, cap type,
1402 * join type, etc.
1403 */
1404 static void
1405 sp_stroke_style_line_update(SPWidget *spw, Inkscape::Selection *sel)
1406 {
1407 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1408 return;
1409 }
1411 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1413 GtkWidget *sset = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "stroke"));
1414 GtkObject *width = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1415 GtkObject *ml = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1416 GtkWidget *us = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1417 GtkWidget *dsel = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1419 // create temporary style
1420 SPStyle *query = sp_style_new ();
1421 // query into it
1422 int result_sw = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1423 int result_ml = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1424 int result_cap = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKECAP);
1425 int result_join = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1427 if (result_sw == QUERY_STYLE_NOTHING) {
1428 /* No objects stroked, set insensitive */
1429 gtk_widget_set_sensitive(sset, FALSE);
1431 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1432 return;
1433 } else {
1434 gtk_widget_set_sensitive(sset, TRUE);
1436 SPUnit const *unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1438 if (result_sw == QUERY_STYLE_MULTIPLE_AVERAGED) {
1439 sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT));
1440 } else {
1441 // same width, or only one object; no sense to keep percent, switch to absolute
1442 if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1443 sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), sp_desktop_namedview(SP_ACTIVE_DESKTOP)->doc_units);
1444 }
1445 }
1447 unit = sp_unit_selector_get_unit (SP_UNIT_SELECTOR (us));
1449 if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1450 double avgwidth = sp_pixels_get_units (query->stroke_width.computed, *unit);
1451 gtk_adjustment_set_value(GTK_ADJUSTMENT(width), avgwidth);
1452 } else {
1453 gtk_adjustment_set_value(GTK_ADJUSTMENT(width), 100);
1454 }
1455 }
1457 if (result_ml != QUERY_STYLE_NOTHING)
1458 gtk_adjustment_set_value(GTK_ADJUSTMENT(ml), query->stroke_miterlimit.value); // TODO: reflect averagedness?
1460 if (result_join != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1461 sp_jointype_set (spw, query->stroke_linejoin.value);
1462 } else {
1463 sp_stroke_style_set_join_buttons(spw, NULL);
1464 }
1466 if (result_cap != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1467 sp_captype_set (spw, query->stroke_linecap.value);
1468 } else {
1469 sp_stroke_style_set_cap_buttons(spw, NULL);
1470 }
1472 sp_style_unref(query);
1474 if (!sel || sel->isEmpty())
1475 return;
1477 GSList const *objects = sel->itemList();
1478 SPObject * const object = SP_OBJECT(objects->data);
1479 SPStyle * const style = SP_OBJECT_STYLE(object);
1481 /* Markers */
1482 sp_stroke_style_update_marker_menus(spw, objects); // FIXME: make this desktop query too
1484 /* Dash */
1485 sp_dash_selector_set_from_style (dsel, style); // FIXME: make this desktop query too
1487 gtk_widget_set_sensitive(sset, TRUE);
1489 gtk_object_set_data(GTK_OBJECT(spw), "update",
1490 GINT_TO_POINTER(FALSE));
1491 }
1493 /**
1494 * Sets a line's dash properties in a CSS style object.
1495 */
1496 static void
1497 sp_stroke_style_set_scaled_dash(SPCSSAttr *css,
1498 int ndash, double *dash, double offset,
1499 double scale)
1500 {
1501 if (ndash > 0) {
1502 Inkscape::CSSOStringStream osarray;
1503 for (int i = 0; i < ndash; i++) {
1504 osarray << dash[i] * scale;
1505 if (i < (ndash - 1)) {
1506 osarray << ",";
1507 }
1508 }
1509 sp_repr_css_set_property(css, "stroke-dasharray", osarray.str().c_str());
1511 Inkscape::CSSOStringStream osoffset;
1512 osoffset << offset * scale;
1513 sp_repr_css_set_property(css, "stroke-dashoffset", osoffset.str().c_str());
1514 } else {
1515 sp_repr_css_set_property(css, "stroke-dasharray", "none");
1516 sp_repr_css_set_property(css, "stroke-dashoffset", NULL);
1517 }
1518 }
1520 /**
1521 * Sets line properties like width, dashes, markers, etc. on all currently selected items.
1522 */
1523 static void
1524 sp_stroke_style_scale_line(SPWidget *spw)
1525 {
1526 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1527 return;
1528 }
1530 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1532 GtkAdjustment *wadj = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1533 SPUnitSelector *us = SP_UNIT_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1534 SPDashSelector *dsel = SP_DASH_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1535 GtkAdjustment *ml = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1537 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1538 SPDocument *document = sp_desktop_document (desktop);
1539 Inkscape::Selection *selection = sp_desktop_selection (desktop);
1541 GSList const *items = selection->itemList();
1543 /* TODO: Create some standardized method */
1544 SPCSSAttr *css = sp_repr_css_attr_new();
1546 if (items) {
1548 double width_typed = wadj->value;
1549 double const miterlimit = ml->value;
1551 SPUnit const *const unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1553 double *dash, offset;
1554 int ndash;
1555 sp_dash_selector_get_dash(dsel, &ndash, &dash, &offset);
1557 for (GSList const *i = items; i != NULL; i = i->next) {
1558 /* Set stroke width */
1559 double width;
1560 if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1561 width = sp_units_get_pixels (width_typed, *unit);
1562 } else { // percentage
1563 gdouble old_w = SP_OBJECT_STYLE (i->data)->stroke_width.computed;
1564 width = old_w * width_typed / 100;
1565 }
1567 {
1568 Inkscape::CSSOStringStream os_width;
1569 os_width << width;
1570 sp_repr_css_set_property(css, "stroke-width", os_width.str().c_str());
1571 }
1573 {
1574 Inkscape::CSSOStringStream os_ml;
1575 os_ml << miterlimit;
1576 sp_repr_css_set_property(css, "stroke-miterlimit", os_ml.str().c_str());
1577 }
1579 /* Set dash */
1580 sp_stroke_style_set_scaled_dash(css, ndash, dash, offset, width);
1582 sp_desktop_apply_css_recursive (SP_OBJECT(i->data), css, true);
1583 }
1585 g_free(dash);
1587 if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1588 // reset to 100 percent
1589 gtk_adjustment_set_value (wadj, 100.0);
1590 }
1592 }
1594 // we have already changed the items, so set style without changing selection
1595 // FIXME: move the above stroke-setting stuff, including percentages, to desktop-style
1596 sp_desktop_set_style (desktop, css, false);
1598 sp_repr_css_attr_unref(css);
1600 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
1601 _("Set stroke style"));
1603 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1604 }
1607 /**
1608 * Callback for when the stroke style's width changes.
1609 * Causes all line styles to be applied to all selected items.
1610 */
1611 static void
1612 sp_stroke_style_width_changed(GtkAdjustment *adj, SPWidget *spw)
1613 {
1614 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1615 return;
1616 }
1618 sp_stroke_style_scale_line(spw);
1619 }
1621 /**
1622 * Callback for when the stroke style's miterlimit changes.
1623 * Causes all line styles to be applied to all selected items.
1624 */
1625 static void
1626 sp_stroke_style_miterlimit_changed(GtkAdjustment *adj, SPWidget *spw)
1627 {
1628 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1629 return;
1630 }
1632 sp_stroke_style_scale_line(spw);
1633 }
1635 /**
1636 * Callback for when the stroke style's dash changes.
1637 * Causes all line styles to be applied to all selected items.
1638 */
1639 static void
1640 sp_stroke_style_line_dash_changed(SPDashSelector *dsel, SPWidget *spw)
1641 {
1642 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1643 return;
1644 }
1646 sp_stroke_style_scale_line(spw);
1647 }
1651 /**
1652 * \brief This routine handles toggle events for buttons in the stroke style
1653 * dialog.
1654 * When activated, this routine gets the data for the various widgets, and then
1655 * calls the respective routines to update css properties, etc.
1656 *
1657 */
1658 static void
1659 sp_stroke_style_any_toggled(GtkToggleButton *tb, SPWidget *spw)
1660 {
1661 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1662 return;
1663 }
1665 if (gtk_toggle_button_get_active(tb)) {
1667 gchar const *join
1668 = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "join"));
1669 gchar const *cap
1670 = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "cap"));
1672 if (join) {
1673 GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1674 gtk_widget_set_sensitive (ml, !strcmp(join, "miter"));
1675 }
1677 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1679 /* TODO: Create some standardized method */
1680 SPCSSAttr *css = sp_repr_css_attr_new();
1682 if (join) {
1683 sp_repr_css_set_property(css, "stroke-linejoin", join);
1685 sp_desktop_set_style (desktop, css);
1687 sp_stroke_style_set_join_buttons(spw, GTK_WIDGET(tb));
1688 } else if (cap) {
1689 sp_repr_css_set_property(css, "stroke-linecap", cap);
1691 sp_desktop_set_style (desktop, css);
1693 sp_stroke_style_set_cap_buttons(spw, GTK_WIDGET(tb));
1694 }
1696 sp_repr_css_attr_unref(css);
1698 sp_document_done(sp_desktop_document(desktop), SP_VERB_DIALOG_FILL_STROKE,
1699 _("Set stroke style"));
1700 }
1701 }
1704 /**
1705 * Updates the join style toggle buttons
1706 */
1707 static void
1708 sp_stroke_style_set_join_buttons(SPWidget *spw, GtkWidget *active)
1709 {
1710 GtkWidget *tb;
1712 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1713 INKSCAPE_STOCK_JOIN_MITER) );
1714 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1716 GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1717 gtk_widget_set_sensitive(ml, (active == tb));
1719 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1720 INKSCAPE_STOCK_JOIN_ROUND) );
1721 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1722 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1723 INKSCAPE_STOCK_JOIN_BEVEL) );
1724 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1725 }
1729 /**
1730 * Updates the cap style toggle buttons
1731 */
1732 static void
1733 sp_stroke_style_set_cap_buttons(SPWidget *spw, GtkWidget *active)
1734 {
1735 GtkWidget *tb;
1737 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1738 INKSCAPE_STOCK_CAP_BUTT));
1739 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1740 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1741 INKSCAPE_STOCK_CAP_ROUND) );
1742 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1743 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1744 INKSCAPE_STOCK_CAP_SQUARE) );
1745 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1746 }
1748 /**
1749 * Sets the current marker in the marker menu.
1750 */
1751 static void
1752 ink_marker_menu_set_current(SPObject *marker, GtkOptionMenu *mnu)
1753 {
1754 gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(TRUE));
1756 GtkMenu *m = GTK_MENU(gtk_option_menu_get_menu(mnu));
1757 if (marker != NULL) {
1758 bool mark_is_stock = false;
1759 if (SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"))
1760 mark_is_stock = true;
1762 gchar *markname;
1763 if (mark_is_stock)
1764 markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"));
1765 else
1766 markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("id"));
1768 int markpos = ink_marker_menu_get_pos(m, markname);
1769 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), markpos);
1771 g_free (markname);
1772 }
1773 else {
1774 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1775 }
1776 gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(FALSE));
1777 }
1779 /**
1780 * Updates the marker menus to highlight the appropriate marker and scroll to
1781 * that marker.
1782 */
1783 static void
1784 sp_stroke_style_update_marker_menus( SPWidget *spw,
1785 GSList const *objects)
1786 {
1787 struct { char const *key; int loc; } const keyloc[] = {
1788 { "start_mark_menu", SP_MARKER_LOC_START },
1789 { "mid_mark_menu", SP_MARKER_LOC_MID },
1790 { "end_mark_menu", SP_MARKER_LOC_END }
1791 };
1793 bool all_texts = true;
1794 for (GSList *i = (GSList *) objects; i != NULL; i = i->next) {
1795 if (!SP_IS_TEXT (i->data)) {
1796 all_texts = false;
1797 }
1798 }
1800 for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1801 GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1802 if (all_texts) {
1803 // Per SVG spec, text objects cannot have markers; disable menus if only texts are selected
1804 gtk_widget_set_sensitive (GTK_WIDGET(mnu), FALSE);
1805 } else {
1806 gtk_widget_set_sensitive (GTK_WIDGET(mnu), TRUE);
1807 }
1808 }
1810 // We show markers of the first object in the list only
1811 // FIXME: use the first in the list that has the marker of each type, if any
1812 SPObject *object = SP_OBJECT(objects->data);
1814 for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1815 // For all three marker types,
1817 // find the corresponding menu
1818 GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1820 // Quit if we're in update state
1821 if (gtk_object_get_data(GTK_OBJECT(mnu), "update")) {
1822 return;
1823 }
1825 if (object->style->marker[keyloc[i].loc].value != NULL && !all_texts) {
1826 // If the object has this type of markers,
1828 // Extract the name of the marker that the object uses
1829 SPObject *marker = ink_extract_marker_name(object->style->marker[keyloc[i].loc].value);
1830 // Scroll the menu to that marker
1831 ink_marker_menu_set_current (marker, mnu);
1833 } else {
1834 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1835 }
1836 }
1837 }
1840 /**
1841 * Extract the actual name of the link
1842 * e.g. get mTriangle from url(#mTriangle).
1843 * \return Buffer containing the actual name, allocated from GLib;
1844 * the caller should free the buffer when they no longer need it.
1845 */
1846 static SPObject*
1847 ink_extract_marker_name(gchar const *n)
1848 {
1849 gchar const *p = n;
1850 while (*p != '\0' && *p != '#') {
1851 p++;
1852 }
1854 if (*p == '\0' || p[1] == '\0') {
1855 return NULL;
1856 }
1858 p++;
1859 int c = 0;
1860 while (p[c] != '\0' && p[c] != ')') {
1861 c++;
1862 }
1864 if (p[c] == '\0') {
1865 return NULL;
1866 }
1868 gchar* b = g_strdup(p);
1869 b[c] = '\0';
1871 SPDesktop *desktop = inkscape_active_desktop();
1872 SPDocument *doc = sp_desktop_document(desktop);
1873 SPObject *marker = doc->getObjectById(b);
1874 return marker;
1875 }
1878 /*
1879 Local Variables:
1880 mode:c++
1881 c-file-style:"stroustrup"
1882 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1883 indent-tabs-mode:nil
1884 fill-column:99
1885 End:
1886 */
1887 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :