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(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
690 GtkWidget *prv = sp_marker_prev_new (22, markid, source, sandbox, menu_id, arena, visionkey, root);
691 gtk_widget_show(prv);
692 gtk_box_pack_start(GTK_BOX(hb), prv, FALSE, FALSE, 6);
694 // create label
695 GtkWidget *l = gtk_label_new(repr->attribute("id"));
696 gtk_widget_show(l);
697 gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
699 gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
701 gtk_widget_show(hb);
702 gtk_container_add(GTK_CONTAINER(i), hb);
704 gtk_menu_append(GTK_MENU(m), i);
705 }
706 }
708 /**
709 * sp_marker_list_from_doc()
710 *
711 * \brief Pick up all markers from source, except those that are in
712 * current_doc (if non-NULL), and add items to the m menu
713 *
714 */
715 static void
716 sp_marker_list_from_doc (GtkWidget *m, SPDocument *current_doc, SPDocument *source, SPDocument *markers_doc, SPDocument *sandbox, gchar *menu_id)
717 {
718 GSList *ml = ink_marker_list_get(source);
719 GSList *clean_ml = NULL;
721 // Do this here, outside of loop, to speed up preview generation:
722 /* Create new arena */
723 NRArena const *arena = NRArena::create();
724 /* Create ArenaItem and set transform */
725 unsigned const visionkey = sp_item_display_key_new(1);
726 /*
727 NRArenaItem *root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT (sandbox)), (NRArena *) arena, visionkey, SP_ITEM_SHOW_DISPLAY );
728 */
730 for (; ml != NULL; ml = ml->next) {
731 if (!SP_IS_MARKER(ml->data))
732 continue;
734 // Add to the list of markers we really do wish to show
735 clean_ml = g_slist_prepend (clean_ml, ml->data);
736 }
737 sp_marker_menu_build (m, clean_ml, source, sandbox, menu_id);
739 g_slist_free (ml);
740 g_slist_free (clean_ml);
741 }
744 /**
745 * Returns a new document containing default start, mid, and end markers.
746 */
747 SPDocument *
748 ink_markers_preview_doc ()
749 {
750 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\">"
751 " <defs id=\"defs\" />"
753 " <g id=\"marker-start\">"
754 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:url(#sample);marker-mid:none;marker-end:none\""
755 " d=\"M 12.5,13 L 25,13\" id=\"path1\" />"
756 " <rect style=\"fill:none;stroke:none\" id=\"rect2\""
757 " width=\"25\" height=\"25\" x=\"0\" y=\"0\" />"
758 " </g>"
760 " <g id=\"marker-mid\">"
761 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:url(#sample);marker-end:none\""
762 " d=\"M 0,113 L 12.5,113 L 25,113\" id=\"path11\" />"
763 " <rect style=\"fill:none;stroke:none\" id=\"rect22\""
764 " width=\"25\" height=\"25\" x=\"0\" y=\"100\" />"
765 " </g>"
767 " <g id=\"marker-end\">"
768 " <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:none;marker-end:url(#sample)\""
769 " d=\"M 0,213 L 12.5,213\" id=\"path111\" />"
770 " <rect style=\"fill:none;stroke:none\" id=\"rect222\""
771 " width=\"25\" height=\"25\" x=\"0\" y=\"200\" />"
772 " </g>"
774 "</svg>";
776 return sp_document_new_from_mem (buffer, strlen(buffer), FALSE);
777 }
779 static void
780 ink_marker_menu_create_menu(GtkWidget *m, gchar *menu_id, SPDocument *doc, SPDocument *sandbox)
781 {
782 static SPDocument *markers_doc = NULL;
784 // add "None"
785 GtkWidget *i = gtk_menu_item_new();
786 gtk_widget_show(i);
788 // g_object_set_data(G_OBJECT(i), "marker", (void *) "none");
790 GtkWidget *hb = gtk_hbox_new(FALSE, MARKER_ITEM_MARGIN);
791 gtk_widget_show(hb);
793 GtkWidget *l = gtk_label_new( _("None") );
794 gtk_widget_show(l);
795 gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
797 gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
799 gtk_widget_show(hb);
800 gtk_container_add(GTK_CONTAINER(i), hb);
801 gtk_menu_append(GTK_MENU(m), i);
803 // find and load markers.svg
804 if (markers_doc == NULL) {
805 char *markers_source = g_build_filename(INKSCAPE_MARKERSDIR, "markers.svg", NULL);
806 if (Inkscape::IO::file_test(markers_source, G_FILE_TEST_IS_REGULAR)) {
807 markers_doc = sp_document_new(markers_source, FALSE);
808 }
809 g_free(markers_source);
810 }
812 // suck in from current doc
813 sp_marker_list_from_doc ( m, NULL, doc, markers_doc, sandbox, menu_id );
815 // add separator
816 {
817 GtkWidget *i = gtk_separator_menu_item_new();
818 gtk_widget_show(i);
819 gtk_menu_append(GTK_MENU(m), i);
820 }
822 // suck in from markers.svg
823 if (markers_doc) {
824 sp_document_ensure_up_to_date(doc);
825 sp_marker_list_from_doc ( m, doc, markers_doc, NULL, sandbox, menu_id );
826 }
827 }
830 /**
831 * Creates a menu widget to display markers from markers.svg
832 */
833 static GtkWidget *
834 ink_marker_menu( GtkWidget *tbl, gchar *menu_id, SPDocument *sandbox)
835 {
836 SPDesktop *desktop = inkscape_active_desktop();
837 SPDocument *doc = sp_desktop_document(desktop);
838 GtkWidget *mnu = gtk_option_menu_new();
840 /* Create new menu widget */
841 GtkWidget *m = gtk_menu_new();
842 gtk_widget_show(m);
844 g_object_set_data(G_OBJECT(mnu), "updating", (gpointer) FALSE);
846 if (!doc) {
847 GtkWidget *i = gtk_menu_item_new_with_label(_("No document selected"));
848 gtk_widget_show(i);
849 gtk_menu_append(GTK_MENU(m), i);
850 gtk_widget_set_sensitive(mnu, FALSE);
852 } else {
853 ink_marker_menu_create_menu(m, menu_id, doc, sandbox);
855 gtk_widget_set_sensitive(mnu, TRUE);
856 }
858 gtk_object_set_data(GTK_OBJECT(mnu), "menu_id", menu_id);
859 gtk_option_menu_set_menu(GTK_OPTION_MENU(mnu), m);
861 /* Set history */
862 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
864 return mnu;
865 }
868 /**
869 * Handles when user selects one of the markers from the marker menu.
870 * Defines a uri string to refer to it, then applies it to all selected
871 * items in the current desktop.
872 */
873 static void
874 sp_marker_select(GtkOptionMenu *mnu, GtkWidget *spw)
875 {
876 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
877 return;
878 }
880 SPDesktop *desktop = inkscape_active_desktop();
881 SPDocument *document = sp_desktop_document(desktop);
882 if (!document) {
883 return;
884 }
886 /* Get Marker */
887 if (!g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
888 "marker"))
889 {
890 return;
891 }
892 gchar *markid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
893 "marker");
894 gchar *marker = "";
895 if (strcmp(markid, "none")){
896 gchar *stockid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
897 "stockid");
899 gchar *markurn = markid;
900 if (!strcmp(stockid,"true")) markurn = g_strconcat("urn:inkscape:marker:",markid,NULL);
901 SPObject *mark = get_stock_item(markurn);
902 if (mark) {
903 Inkscape::XML::Node *repr = SP_OBJECT_REPR(mark);
904 marker = g_strconcat("url(#", repr->attribute("id"), ")", NULL);
905 }
906 } else {
907 marker = markid;
908 }
910 SPCSSAttr *css = sp_repr_css_attr_new();
911 gchar *menu_id = (gchar *) g_object_get_data(G_OBJECT(mnu), "menu_id");
912 sp_repr_css_set_property(css, menu_id, marker);
914 Inkscape::Selection *selection = sp_desktop_selection(desktop);
915 GSList const *items = selection->itemList();
916 for (; items != NULL; items = items->next) {
917 SPItem *item = (SPItem *) items->data;
918 if (!SP_IS_SHAPE(item) || SP_IS_RECT(item)) // can't set marker to rect, until it's converted to using <path>
919 continue;
920 Inkscape::XML::Node *selrepr = SP_OBJECT_REPR((SPItem *) items->data);
921 if (selrepr) {
922 sp_repr_css_change_recursive(selrepr, css, "style");
923 }
924 SP_OBJECT(items->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
925 SP_OBJECT(items->data)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
926 }
928 sp_repr_css_attr_unref(css);
930 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
931 _("Set markers"));
933 // Lastly, also update the marker dropdown menus, so the document's markers
934 // show up at the top of the menu
935 ink_markers_menu_update();
936 };
938 static void
939 ink_markers_menu_update() {
940 SPDesktop *desktop = inkscape_active_desktop();
941 SPDocument *document = sp_desktop_document(desktop);
942 SPDocument *sandbox = ink_markers_preview_doc ();
943 GtkWidget *m;
945 m = gtk_menu_new();
946 gtk_widget_show(m);
947 ink_marker_menu_create_menu(m, "marker-start", document, sandbox);
948 gtk_option_menu_remove_menu(GTK_OPTION_MENU(marker_start_menu));
949 gtk_option_menu_set_menu(GTK_OPTION_MENU(marker_start_menu), m);
951 m = gtk_menu_new();
952 gtk_widget_show(m);
953 ink_marker_menu_create_menu(m, "marker-mid", document, sandbox);
954 gtk_option_menu_remove_menu(GTK_OPTION_MENU(marker_mid_menu));
955 gtk_option_menu_set_menu(GTK_OPTION_MENU(marker_mid_menu), m);
957 m = gtk_menu_new();
958 gtk_widget_show(m);
959 ink_marker_menu_create_menu(m, "marker-end", document, sandbox);
960 gtk_option_menu_remove_menu(GTK_OPTION_MENU(marker_end_menu));
961 gtk_option_menu_set_menu(GTK_OPTION_MENU(marker_end_menu), m);
962 }
964 /**
965 * Sets the stroke width units for all selected items.
966 * Also handles absolute and dimensionless units.
967 */
968 static gboolean stroke_width_set_unit(SPUnitSelector *,
969 SPUnit const *old,
970 SPUnit const *new_units,
971 GObject *spw)
972 {
973 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
975 if (!desktop) {
976 return FALSE;
977 }
979 Inkscape::Selection *selection = sp_desktop_selection (desktop);
981 if (selection->isEmpty())
982 return FALSE;
984 GSList const *objects = selection->itemList();
986 if ((old->base == SP_UNIT_ABSOLUTE || old->base == SP_UNIT_DEVICE) &&
987 (new_units->base == SP_UNIT_DIMENSIONLESS)) {
989 /* Absolute to percentage */
990 g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
992 GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
993 float w = sp_units_get_pixels (a->value, *old);
995 gdouble average = stroke_average_width (objects);
997 if (average == NR_HUGE || average == 0)
998 return FALSE;
1000 gtk_adjustment_set_value (a, 100.0 * w / average);
1002 g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
1003 return TRUE;
1005 } else if ((old->base == SP_UNIT_DIMENSIONLESS) &&
1006 (new_units->base == SP_UNIT_ABSOLUTE || new_units->base == SP_UNIT_DEVICE)) {
1008 /* Percentage to absolute */
1009 g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
1011 GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
1013 gdouble average = stroke_average_width (objects);
1015 gtk_adjustment_set_value (a, sp_pixels_get_units (0.01 * a->value * average, *new_units));
1017 g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
1018 return TRUE;
1019 }
1021 return FALSE;
1022 }
1025 /**
1026 * \brief Creates a new widget for the line stroke style.
1027 *
1028 */
1029 GtkWidget *
1030 sp_stroke_style_line_widget_new(void)
1031 {
1032 GtkWidget *spw, *f, *t, *hb, *sb, *us, *tb, *ds;
1033 GtkObject *a;
1035 GtkTooltips *tt = gtk_tooltips_new();
1037 spw = sp_widget_new_global(INKSCAPE);
1039 f = gtk_hbox_new (FALSE, 0);
1040 gtk_widget_show(f);
1041 gtk_container_add(GTK_CONTAINER(spw), f);
1043 t = gtk_table_new(3, 6, FALSE);
1044 gtk_widget_show(t);
1045 gtk_container_set_border_width(GTK_CONTAINER(t), 4);
1046 gtk_table_set_row_spacings(GTK_TABLE(t), 4);
1047 gtk_container_add(GTK_CONTAINER(f), t);
1048 gtk_object_set_data(GTK_OBJECT(spw), "stroke", t);
1050 gint i = 0;
1052 /* Stroke width */
1053 spw_label(t, _("Width:"), 0, i);
1055 hb = spw_hbox(t, 3, 1, i);
1057 // TODO: when this is gtkmmified, use an Inkscape::UI::Widget::ScalarUnit instead of the separate
1058 // spinbutton and unit selector for stroke width. In sp_stroke_style_line_update, use
1059 // setHundredPercent to remember the aeraged width corresponding to 100%. Then the
1060 // stroke_width_set_unit will be removed (because ScalarUnit takes care of conversions itself), and
1061 // with it, the two remaining calls of stroke_average_width, allowing us to get rid of that
1062 // function in desktop-style.
1064 a = gtk_adjustment_new(1.0, 0.0, 1000.0, 0.1, 10.0, 10.0);
1065 gtk_object_set_data(GTK_OBJECT(spw), "width", a);
1066 sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 3);
1067 gtk_tooltips_set_tip(tt, sb, _("Stroke width"), NULL);
1068 gtk_widget_show(sb);
1070 sp_dialog_defocus_on_enter(sb);
1072 gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1073 us = sp_unit_selector_new(SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE);
1074 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1075 if (desktop)
1076 sp_unit_selector_set_unit (SP_UNIT_SELECTOR(us), sp_desktop_namedview(desktop)->doc_units);
1077 sp_unit_selector_add_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT), 0);
1078 g_signal_connect ( G_OBJECT (us), "set_unit", G_CALLBACK (stroke_width_set_unit), spw );
1079 gtk_widget_show(us);
1080 sp_unit_selector_add_adjustment( SP_UNIT_SELECTOR(us), GTK_ADJUSTMENT(a) );
1081 gtk_box_pack_start(GTK_BOX(hb), us, FALSE, FALSE, 0);
1082 gtk_object_set_data(GTK_OBJECT(spw), "units", us);
1084 gtk_signal_connect( GTK_OBJECT(a), "value_changed", GTK_SIGNAL_FUNC(sp_stroke_style_width_changed), spw );
1085 i++;
1087 /* Join type */
1088 // TRANSLATORS: The line join style specifies the shape to be used at the
1089 // corners of paths. It can be "miter", "round" or "bevel".
1090 spw_label(t, _("Join:"), 0, i);
1092 hb = spw_hbox(t, 3, 1, i);
1094 tb = NULL;
1096 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_MITER,
1097 hb, spw, "join", "miter");
1099 // TRANSLATORS: Miter join: joining lines with a sharp (pointed) corner.
1100 // For an example, draw a triangle with a large stroke width and modify the
1101 // "Join" option (in the Fill and Stroke dialog).
1102 gtk_tooltips_set_tip(tt, tb, _("Miter join"), NULL);
1104 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_ROUND,
1105 hb, spw, "join", "round");
1107 // TRANSLATORS: Round join: joining lines with a rounded corner.
1108 // For an example, draw a triangle with a large stroke width and modify the
1109 // "Join" option (in the Fill and Stroke dialog).
1110 gtk_tooltips_set_tip(tt, tb, _("Round join"), NULL);
1112 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_BEVEL,
1113 hb, spw, "join", "bevel");
1115 // TRANSLATORS: Bevel join: joining lines with a blunted (flattened) corner.
1116 // For an example, draw a triangle with a large stroke width and modify the
1117 // "Join" option (in the Fill and Stroke dialog).
1118 gtk_tooltips_set_tip(tt, tb, _("Bevel join"), NULL);
1120 i++;
1122 /* Miterlimit */
1123 // TRANSLATORS: Miter limit: only for "miter join", this limits the length
1124 // of the sharp "spike" when the lines connect at too sharp an angle.
1125 // When two line segments meet at a sharp angle, a miter join results in a
1126 // spike that extends well beyond the connection point. The purpose of the
1127 // miter limit is to cut off such spikes (i.e. convert them into bevels)
1128 // when they become too long.
1129 spw_label(t, _("Miter limit:"), 0, i);
1131 hb = spw_hbox(t, 3, 1, i);
1133 a = gtk_adjustment_new(4.0, 0.0, 100.0, 0.1, 10.0, 10.0);
1134 gtk_object_set_data(GTK_OBJECT(spw), "miterlimit", a);
1136 sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 2);
1137 gtk_tooltips_set_tip(tt, sb, _("Maximum length of the miter (in units of stroke width)"), NULL);
1138 gtk_widget_show(sb);
1139 gtk_object_set_data(GTK_OBJECT(spw), "miterlimit_sb", sb);
1140 sp_dialog_defocus_on_enter(sb);
1142 gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1144 gtk_signal_connect( GTK_OBJECT(a), "value_changed",
1145 GTK_SIGNAL_FUNC(sp_stroke_style_miterlimit_changed), spw );
1146 i++;
1148 /* Cap type */
1149 // TRANSLATORS: cap type specifies the shape for the ends of lines
1150 spw_label(t, _("Cap:"), 0, i);
1152 hb = spw_hbox(t, 3, 1, i);
1154 tb = NULL;
1156 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_BUTT,
1157 hb, spw, "cap", "butt");
1159 // TRANSLATORS: Butt cap: the line shape does not extend beyond the end point
1160 // of the line; the ends of the line are square
1161 gtk_tooltips_set_tip(tt, tb, _("Butt cap"), NULL);
1163 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_ROUND,
1164 hb, spw, "cap", "round");
1166 // TRANSLATORS: Round cap: the line shape extends beyond the end point of the
1167 // line; the ends of the line are rounded
1168 gtk_tooltips_set_tip(tt, tb, _("Round cap"), NULL);
1170 tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_SQUARE,
1171 hb, spw, "cap", "square");
1173 // TRANSLATORS: Square cap: the line shape extends beyond the end point of the
1174 // line; the ends of the line are square
1175 gtk_tooltips_set_tip(tt, tb, _("Square cap"), NULL);
1177 i++;
1180 /* Dash */
1181 spw_label(t, _("Dashes:"), 0, i);
1182 ds = sp_dash_selector_new( inkscape_get_repr( INKSCAPE,
1183 "palette.dashes") );
1185 gtk_widget_show(ds);
1186 gtk_table_attach( GTK_TABLE(t), ds, 1, 4, i, i+1,
1187 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1188 (GtkAttachOptions)0, 0, 0 );
1189 gtk_object_set_data(GTK_OBJECT(spw), "dash", ds);
1190 gtk_signal_connect( GTK_OBJECT(ds), "changed",
1191 GTK_SIGNAL_FUNC(sp_stroke_style_line_dash_changed),
1192 spw );
1193 i++;
1195 /* Drop down marker selectors*/
1197 // doing this here once, instead of for each preview, to speed things up
1198 SPDocument *sandbox = ink_markers_preview_doc ();
1200 // TRANSLATORS: Path markers are an SVG feature that allows you to attach arbitrary shapes
1201 // (arrowheads, bullets, faces, whatever) to the start, end, or middle nodes of a path.
1202 spw_label(t, _("Start Markers:"), 0, i);
1203 marker_start_menu = ink_marker_menu( spw ,"marker-start", sandbox);
1204 gtk_signal_connect( GTK_OBJECT(marker_start_menu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1205 gtk_widget_show(marker_start_menu);
1206 gtk_table_attach( GTK_TABLE(t), marker_start_menu, 1, 4, i, i+1,
1207 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1208 (GtkAttachOptions)0, 0, 0 );
1209 gtk_object_set_data(GTK_OBJECT(spw), "start_mark_menu", marker_start_menu);
1211 i++;
1212 spw_label(t, _("Mid Markers:"), 0, i);
1213 marker_mid_menu = ink_marker_menu( spw ,"marker-mid", sandbox);
1214 gtk_signal_connect( GTK_OBJECT(marker_mid_menu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1215 gtk_widget_show(marker_mid_menu);
1216 gtk_table_attach( GTK_TABLE(t), marker_mid_menu, 1, 4, i, i+1,
1217 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1218 (GtkAttachOptions)0, 0, 0 );
1219 gtk_object_set_data(GTK_OBJECT(spw), "mid_mark_menu", marker_mid_menu);
1221 i++;
1222 spw_label(t, _("End Markers:"), 0, i);
1223 marker_end_menu = ink_marker_menu( spw ,"marker-end", sandbox);
1224 gtk_signal_connect( GTK_OBJECT(marker_end_menu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1225 gtk_widget_show(marker_end_menu);
1226 gtk_table_attach( GTK_TABLE(t), marker_end_menu, 1, 4, i, i+1,
1227 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1228 (GtkAttachOptions)0, 0, 0 );
1229 gtk_object_set_data(GTK_OBJECT(spw), "end_mark_menu", marker_end_menu);
1231 i++;
1233 gtk_signal_connect( GTK_OBJECT(spw), "construct",
1234 GTK_SIGNAL_FUNC(sp_stroke_style_line_construct),
1235 NULL );
1236 gtk_signal_connect( GTK_OBJECT(spw), "modify_selection",
1237 GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_modified),
1238 NULL );
1239 gtk_signal_connect( GTK_OBJECT(spw), "change_selection",
1240 GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_changed),
1241 NULL );
1243 sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
1245 return spw;
1246 }
1249 /**
1250 * Callback for when the stroke style widget is called. It causes
1251 * the stroke line style to be updated.
1252 */
1253 static void
1254 sp_stroke_style_line_construct(SPWidget *spw, gpointer data)
1255 {
1257 #ifdef SP_SS_VERBOSE
1258 g_print( "Stroke style widget constructed: inkscape %p repr %p\n",
1259 spw->inkscape, spw->repr );
1260 #endif
1261 if (spw->inkscape) {
1262 sp_stroke_style_line_update(spw,
1263 ( SP_ACTIVE_DESKTOP
1264 ? sp_desktop_selection(SP_ACTIVE_DESKTOP)
1265 : NULL ));
1266 }
1267 }
1269 /**
1270 * Callback for when stroke style widget is modified.
1271 * Triggers update action.
1272 */
1273 static void
1274 sp_stroke_style_line_selection_modified ( SPWidget *spw,
1275 Inkscape::Selection *selection,
1276 guint flags,
1277 gpointer data )
1278 {
1279 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1280 sp_stroke_style_line_update (spw, selection);
1281 }
1283 }
1285 /**
1286 * Callback for when stroke style widget is changed.
1287 * Triggers update action.
1288 */
1289 static void
1290 sp_stroke_style_line_selection_changed ( SPWidget *spw,
1291 Inkscape::Selection *selection,
1292 gpointer data )
1293 {
1294 sp_stroke_style_line_update (spw, selection);
1295 }
1298 /**
1299 * Sets selector widgets' dash style from an SPStyle object.
1300 */
1301 static void
1302 sp_dash_selector_set_from_style (GtkWidget *dsel, SPStyle *style)
1303 {
1304 if (style->stroke_dash.n_dash > 0) {
1305 double d[64];
1306 int len = MIN(style->stroke_dash.n_dash, 64);
1307 for (int i = 0; i < len; i++) {
1308 if (style->stroke_width.computed != 0)
1309 d[i] = style->stroke_dash.dash[i] / style->stroke_width.computed;
1310 else
1311 d[i] = style->stroke_dash.dash[i]; // is there a better thing to do for stroke_width==0?
1312 }
1313 sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), len, d,
1314 style->stroke_width.computed != 0?
1315 style->stroke_dash.offset / style->stroke_width.computed :
1316 style->stroke_dash.offset);
1317 } else {
1318 sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), 0, NULL, 0.0);
1319 }
1320 }
1322 /**
1323 * Sets the join type for a line, and updates the stroke style widget's buttons
1324 */
1325 static void
1326 sp_jointype_set (SPWidget *spw, unsigned const jointype)
1327 {
1328 GtkWidget *tb = NULL;
1329 switch (jointype) {
1330 case SP_STROKE_LINEJOIN_MITER:
1331 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_MITER));
1332 break;
1333 case SP_STROKE_LINEJOIN_ROUND:
1334 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_ROUND));
1335 break;
1336 case SP_STROKE_LINEJOIN_BEVEL:
1337 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_BEVEL));
1338 break;
1339 default:
1340 break;
1341 }
1342 sp_stroke_style_set_join_buttons (spw, tb);
1343 }
1345 /**
1346 * Sets the cap type for a line, and updates the stroke style widget's buttons
1347 */
1348 static void
1349 sp_captype_set (SPWidget *spw, unsigned const captype)
1350 {
1351 GtkWidget *tb = NULL;
1352 switch (captype) {
1353 case SP_STROKE_LINECAP_BUTT:
1354 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_BUTT));
1355 break;
1356 case SP_STROKE_LINECAP_ROUND:
1357 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_ROUND));
1358 break;
1359 case SP_STROKE_LINECAP_SQUARE:
1360 tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_SQUARE));
1361 break;
1362 default:
1363 break;
1364 }
1365 sp_stroke_style_set_cap_buttons (spw, tb);
1366 }
1368 /**
1369 * Callback for when stroke style widget is updated, including markers, cap type,
1370 * join type, etc.
1371 */
1372 static void
1373 sp_stroke_style_line_update(SPWidget *spw, Inkscape::Selection *sel)
1374 {
1375 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1376 return;
1377 }
1379 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1381 GtkWidget *sset = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "stroke"));
1382 GtkObject *width = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1383 GtkObject *ml = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1384 GtkWidget *us = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1385 GtkWidget *dsel = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1387 // create temporary style
1388 SPStyle *query = sp_style_new ();
1389 // query into it
1390 int result_sw = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1391 int result_ml = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1392 int result_cap = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKECAP);
1393 int result_join = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1395 if (result_sw == QUERY_STYLE_NOTHING) {
1396 /* No objects stroked, set insensitive */
1397 gtk_widget_set_sensitive(sset, FALSE);
1399 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1400 return;
1401 } else {
1402 gtk_widget_set_sensitive(sset, TRUE);
1404 SPUnit const *unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1406 if (result_sw == QUERY_STYLE_MULTIPLE_AVERAGED) {
1407 sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT));
1408 } else {
1409 // same width, or only one object; no sense to keep percent, switch to absolute
1410 if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1411 sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), sp_desktop_namedview(SP_ACTIVE_DESKTOP)->doc_units);
1412 }
1413 }
1415 unit = sp_unit_selector_get_unit (SP_UNIT_SELECTOR (us));
1417 if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1418 double avgwidth = sp_pixels_get_units (query->stroke_width.computed, *unit);
1419 gtk_adjustment_set_value(GTK_ADJUSTMENT(width), avgwidth);
1420 } else {
1421 gtk_adjustment_set_value(GTK_ADJUSTMENT(width), 100);
1422 }
1423 }
1425 if (result_ml != QUERY_STYLE_NOTHING)
1426 gtk_adjustment_set_value(GTK_ADJUSTMENT(ml), query->stroke_miterlimit.value); // TODO: reflect averagedness?
1428 if (result_join != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1429 sp_jointype_set (spw, query->stroke_linejoin.value);
1430 } else {
1431 sp_stroke_style_set_join_buttons(spw, NULL);
1432 }
1434 if (result_cap != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1435 sp_captype_set (spw, query->stroke_linecap.value);
1436 } else {
1437 sp_stroke_style_set_cap_buttons(spw, NULL);
1438 }
1440 g_free (query);
1442 if (!sel || sel->isEmpty())
1443 return;
1445 GSList const *objects = sel->itemList();
1446 SPObject * const object = SP_OBJECT(objects->data);
1447 SPStyle * const style = SP_OBJECT_STYLE(object);
1449 /* Markers */
1450 sp_stroke_style_update_marker_menus(spw, objects); // FIXME: make this desktop query too
1452 /* Dash */
1453 sp_dash_selector_set_from_style (dsel, style); // FIXME: make this desktop query too
1455 gtk_widget_set_sensitive(sset, TRUE);
1457 gtk_object_set_data(GTK_OBJECT(spw), "update",
1458 GINT_TO_POINTER(FALSE));
1459 }
1461 /**
1462 * Sets a line's dash properties in a CSS style object.
1463 */
1464 static void
1465 sp_stroke_style_set_scaled_dash(SPCSSAttr *css,
1466 int ndash, double *dash, double offset,
1467 double scale)
1468 {
1469 if (ndash > 0) {
1470 Inkscape::CSSOStringStream osarray;
1471 for (int i = 0; i < ndash; i++) {
1472 osarray << dash[i] * scale;
1473 if (i < (ndash - 1)) {
1474 osarray << ",";
1475 }
1476 }
1477 sp_repr_css_set_property(css, "stroke-dasharray", osarray.str().c_str());
1479 Inkscape::CSSOStringStream osoffset;
1480 osoffset << offset * scale;
1481 sp_repr_css_set_property(css, "stroke-dashoffset", osoffset.str().c_str());
1482 } else {
1483 sp_repr_css_set_property(css, "stroke-dasharray", "none");
1484 sp_repr_css_set_property(css, "stroke-dashoffset", NULL);
1485 }
1486 }
1488 /**
1489 * Sets line properties like width, dashes, markers, etc. on all currently selected items.
1490 */
1491 static void
1492 sp_stroke_style_scale_line(SPWidget *spw)
1493 {
1494 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1495 return;
1496 }
1498 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1500 GtkAdjustment *wadj = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1501 SPUnitSelector *us = SP_UNIT_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1502 SPDashSelector *dsel = SP_DASH_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1503 GtkAdjustment *ml = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1505 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1506 SPDocument *document = sp_desktop_document (desktop);
1507 Inkscape::Selection *selection = sp_desktop_selection (desktop);
1509 GSList const *items = selection->itemList();
1511 /* TODO: Create some standardized method */
1512 SPCSSAttr *css = sp_repr_css_attr_new();
1514 if (items) {
1516 double width_typed = wadj->value;
1517 double const miterlimit = ml->value;
1519 SPUnit const *const unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1521 double *dash, offset;
1522 int ndash;
1523 sp_dash_selector_get_dash(dsel, &ndash, &dash, &offset);
1525 for (GSList const *i = items; i != NULL; i = i->next) {
1526 /* Set stroke width */
1527 double width;
1528 if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1529 width = sp_units_get_pixels (width_typed, *unit);
1530 } else { // percentage
1531 gdouble old_w = SP_OBJECT_STYLE (i->data)->stroke_width.computed;
1532 width = old_w * width_typed / 100;
1533 }
1535 {
1536 Inkscape::CSSOStringStream os_width;
1537 os_width << width;
1538 sp_repr_css_set_property(css, "stroke-width", os_width.str().c_str());
1539 }
1541 {
1542 Inkscape::CSSOStringStream os_ml;
1543 os_ml << miterlimit;
1544 sp_repr_css_set_property(css, "stroke-miterlimit", os_ml.str().c_str());
1545 }
1547 /* Set dash */
1548 sp_stroke_style_set_scaled_dash(css, ndash, dash, offset, width);
1550 sp_desktop_apply_css_recursive (SP_OBJECT(i->data), css, true);
1551 }
1553 g_free(dash);
1555 if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1556 // reset to 100 percent
1557 gtk_adjustment_set_value (wadj, 100.0);
1558 }
1560 }
1562 // we have already changed the items, so set style without changing selection
1563 // FIXME: move the above stroke-setting stuff, including percentages, to desktop-style
1564 sp_desktop_set_style (desktop, css, false);
1566 sp_repr_css_attr_unref(css);
1568 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
1569 _("Set stroke style"));
1571 gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1572 }
1575 /**
1576 * Callback for when the stroke style's width changes.
1577 * Causes all line styles to be applied to all selected items.
1578 */
1579 static void
1580 sp_stroke_style_width_changed(GtkAdjustment *adj, SPWidget *spw)
1581 {
1582 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1583 return;
1584 }
1586 sp_stroke_style_scale_line(spw);
1587 }
1589 /**
1590 * Callback for when the stroke style's miterlimit changes.
1591 * Causes all line styles to be applied to all selected items.
1592 */
1593 static void
1594 sp_stroke_style_miterlimit_changed(GtkAdjustment *adj, SPWidget *spw)
1595 {
1596 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1597 return;
1598 }
1600 sp_stroke_style_scale_line(spw);
1601 }
1603 /**
1604 * Callback for when the stroke style's dash changes.
1605 * Causes all line styles to be applied to all selected items.
1606 */
1607 static void
1608 sp_stroke_style_line_dash_changed(SPDashSelector *dsel, SPWidget *spw)
1609 {
1610 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1611 return;
1612 }
1614 sp_stroke_style_scale_line(spw);
1615 }
1619 /**
1620 * \brief This routine handles toggle events for buttons in the stroke style
1621 * dialog.
1622 * When activated, this routine gets the data for the various widgets, and then
1623 * calls the respective routines to update css properties, etc.
1624 *
1625 */
1626 static void
1627 sp_stroke_style_any_toggled(GtkToggleButton *tb, SPWidget *spw)
1628 {
1629 if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1630 return;
1631 }
1633 if (gtk_toggle_button_get_active(tb)) {
1635 gchar const *join
1636 = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "join"));
1637 gchar const *cap
1638 = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "cap"));
1640 if (join) {
1641 GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1642 gtk_widget_set_sensitive (ml, !strcmp(join, "miter"));
1643 }
1645 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1647 /* TODO: Create some standardized method */
1648 SPCSSAttr *css = sp_repr_css_attr_new();
1650 if (join) {
1651 sp_repr_css_set_property(css, "stroke-linejoin", join);
1653 sp_desktop_set_style (desktop, css);
1655 sp_stroke_style_set_join_buttons(spw, GTK_WIDGET(tb));
1656 } else if (cap) {
1657 sp_repr_css_set_property(css, "stroke-linecap", cap);
1659 sp_desktop_set_style (desktop, css);
1661 sp_stroke_style_set_cap_buttons(spw, GTK_WIDGET(tb));
1662 }
1664 sp_repr_css_attr_unref(css);
1666 sp_document_done(sp_desktop_document(desktop), SP_VERB_DIALOG_FILL_STROKE,
1667 _("Set stroke style"));
1668 }
1669 }
1672 /**
1673 * Updates the join style toggle buttons
1674 */
1675 static void
1676 sp_stroke_style_set_join_buttons(SPWidget *spw, GtkWidget *active)
1677 {
1678 GtkWidget *tb;
1680 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1681 INKSCAPE_STOCK_JOIN_MITER) );
1682 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1684 GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1685 gtk_widget_set_sensitive(ml, (active == tb));
1687 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1688 INKSCAPE_STOCK_JOIN_ROUND) );
1689 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1690 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1691 INKSCAPE_STOCK_JOIN_BEVEL) );
1692 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1693 }
1697 /**
1698 * Updates the cap style toggle buttons
1699 */
1700 static void
1701 sp_stroke_style_set_cap_buttons(SPWidget *spw, GtkWidget *active)
1702 {
1703 GtkWidget *tb;
1705 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1706 INKSCAPE_STOCK_CAP_BUTT));
1707 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1708 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1709 INKSCAPE_STOCK_CAP_ROUND) );
1710 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1711 tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1712 INKSCAPE_STOCK_CAP_SQUARE) );
1713 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1714 }
1716 /**
1717 * Sets the current marker in the marker menu.
1718 */
1719 static void
1720 ink_marker_menu_set_current(SPObject *marker, GtkOptionMenu *mnu)
1721 {
1722 gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(TRUE));
1724 GtkMenu *m = GTK_MENU(gtk_option_menu_get_menu(mnu));
1725 if (marker != NULL) {
1726 bool mark_is_stock = false;
1727 if (SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"))
1728 mark_is_stock = true;
1730 gchar *markname;
1731 if (mark_is_stock)
1732 markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"));
1733 else
1734 markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("id"));
1736 int markpos = 0;
1737 GList *kids = GTK_MENU_SHELL(m)->children;
1738 int i = 0;
1739 for (; kids != NULL; kids = kids->next) {
1740 gchar *mark = (gchar *) g_object_get_data(G_OBJECT(kids->data), "marker");
1741 if ( mark && strcmp(mark, markname) == 0 ) {
1742 if ( mark_is_stock && !strcmp((gchar *) g_object_get_data(G_OBJECT(kids->data), "stockid"), "true"))
1743 markpos = i;
1744 if ( !mark_is_stock && !strcmp((gchar *) g_object_get_data(G_OBJECT(kids->data), "stockid"), "false"))
1745 markpos = i;
1746 }
1747 i++;
1748 }
1749 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), markpos);
1751 g_free (markname);
1752 }
1753 else {
1754 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1755 }
1756 gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(FALSE));
1757 }
1759 /**
1760 * Updates the marker menus to highlight the appropriate marker and scroll to
1761 * that marker.
1762 */
1763 static void
1764 sp_stroke_style_update_marker_menus( SPWidget *spw,
1765 GSList const *objects)
1766 {
1767 struct { char const *key; int loc; } const keyloc[] = {
1768 { "start_mark_menu", SP_MARKER_LOC_START },
1769 { "mid_mark_menu", SP_MARKER_LOC_MID },
1770 { "end_mark_menu", SP_MARKER_LOC_END }
1771 };
1773 bool all_texts = true;
1774 for (GSList *i = (GSList *) objects; i != NULL; i = i->next) {
1775 if (!SP_IS_TEXT (i->data)) {
1776 all_texts = false;
1777 }
1778 }
1780 for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1781 GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1782 if (all_texts) {
1783 // Per SVG spec, text objects cannot have markers; disable menus if only texts are selected
1784 gtk_widget_set_sensitive (GTK_WIDGET(mnu), FALSE);
1785 } else {
1786 gtk_widget_set_sensitive (GTK_WIDGET(mnu), TRUE);
1787 }
1788 }
1790 // We show markers of the first object in the list only
1791 // FIXME: use the first in the list that has the marker of each type, if any
1792 SPObject *object = SP_OBJECT(objects->data);
1794 for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1795 // For all three marker types,
1797 // find the corresponding menu
1798 GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1800 // Quit if we're in update state
1801 if (gtk_object_get_data(GTK_OBJECT(mnu), "update")) {
1802 return;
1803 }
1805 if (object->style->marker[keyloc[i].loc].value != NULL && !all_texts) {
1806 // If the object has this type of markers,
1808 // Extract the name of the marker that the object uses
1809 SPObject *marker = ink_extract_marker_name(object->style->marker[keyloc[i].loc].value);
1810 // Scroll the menu to that marker
1811 ink_marker_menu_set_current (marker, mnu);
1813 } else {
1814 gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1815 }
1816 }
1817 }
1820 /**
1821 * Extract the actual name of the link
1822 * e.g. get mTriangle from url(#mTriangle).
1823 * \return Buffer containing the actual name, allocated from GLib;
1824 * the caller should free the buffer when they no longer need it.
1825 */
1826 static SPObject*
1827 ink_extract_marker_name(gchar const *n)
1828 {
1829 gchar const *p = n;
1830 while (*p != '\0' && *p != '#') {
1831 p++;
1832 }
1834 if (*p == '\0' || p[1] == '\0') {
1835 return NULL;
1836 }
1838 p++;
1839 int c = 0;
1840 while (p[c] != '\0' && p[c] != ')') {
1841 c++;
1842 }
1844 if (p[c] == '\0') {
1845 return NULL;
1846 }
1848 gchar* b = g_strdup(p);
1849 b[c] = '\0';
1852 SPDesktop *desktop = inkscape_active_desktop();
1853 SPDocument *doc = sp_desktop_document(desktop);
1854 SPObject *marker = doc->getObjectById(b);
1855 return marker;
1856 }
1859 /*
1860 Local Variables:
1861 mode:c++
1862 c-file-style:"stroustrup"
1863 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1864 indent-tabs-mode:nil
1865 fill-column:99
1866 End:
1867 */
1868 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :