Code

Marker menus now display custom markers in a document that are present
[inkscape.git] / src / dialogs / stroke-style.cpp
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"
60 #include "dialogs/stroke-style.h"
62 /* Paint */
64 static void sp_stroke_style_paint_construct(SPWidget *spw, SPPaintSelector *psel);
65 static void sp_stroke_style_paint_selection_modified (SPWidget *spw, Inkscape::Selection *selection, guint flags, SPPaintSelector *psel);
66 static void sp_stroke_style_paint_selection_changed (SPWidget *spw, Inkscape::Selection *selection, SPPaintSelector *psel);
67 static void sp_stroke_style_paint_update(SPWidget *spw);
69 static void sp_stroke_style_paint_mode_changed(SPPaintSelector *psel, SPPaintSelectorMode mode, SPWidget *spw);
70 static void sp_stroke_style_paint_dragged(SPPaintSelector *psel, SPWidget *spw);
71 static void sp_stroke_style_paint_changed(SPPaintSelector *psel, SPWidget *spw);
73 static void sp_stroke_style_widget_change_subselection ( Inkscape::Application *inkscape, SPDesktop *desktop, SPWidget *spw );
75 /**
76  * Create the stroke style widget, and hook up all the signals.
77  */
78 GtkWidget *
79 sp_stroke_style_paint_widget_new(void)
80 {
81     GtkWidget *spw, *psel;
83     spw = sp_widget_new_global(INKSCAPE);
85     psel = sp_paint_selector_new(false); // without fillrule selector
86     gtk_widget_show(psel);
87     gtk_container_add(GTK_CONTAINER(spw), psel);
88     gtk_object_set_data(GTK_OBJECT(spw), "paint-selector", psel);
90     gtk_signal_connect(GTK_OBJECT(spw), "construct",
91                        GTK_SIGNAL_FUNC(sp_stroke_style_paint_construct),
92                        psel);
93     gtk_signal_connect(GTK_OBJECT(spw), "modify_selection",
94                        GTK_SIGNAL_FUNC(sp_stroke_style_paint_selection_modified),
95                        psel);
96     gtk_signal_connect(GTK_OBJECT(spw), "change_selection",
97                        GTK_SIGNAL_FUNC(sp_stroke_style_paint_selection_changed),
98                        psel);
100     g_signal_connect (INKSCAPE, "change_subselection", G_CALLBACK (sp_stroke_style_widget_change_subselection), spw);
102     gtk_signal_connect(GTK_OBJECT(psel), "mode_changed",
103                        GTK_SIGNAL_FUNC(sp_stroke_style_paint_mode_changed),
104                        spw);
105     gtk_signal_connect(GTK_OBJECT(psel), "dragged",
106                        GTK_SIGNAL_FUNC(sp_stroke_style_paint_dragged),
107                        spw);
108     gtk_signal_connect(GTK_OBJECT(psel), "changed",
109                        GTK_SIGNAL_FUNC(sp_stroke_style_paint_changed),
110                        spw);
112     sp_stroke_style_paint_update (SP_WIDGET(spw));
113     return spw;
116 /**
117  * On construction, simply does an update of the stroke style paint object.
118  */
119 static void
120 sp_stroke_style_paint_construct(SPWidget *spw, SPPaintSelector *psel)
122 #ifdef SP_SS_VERBOSE
123     g_print( "Stroke style widget constructed: inkscape %p repr %p\n",
124              spw->inkscape, spw->repr );
125 #endif
126     if (spw->inkscape) {
127         sp_stroke_style_paint_update (spw);
128     } 
131 /**
132  * On signal modified, invokes an update of the stroke style paint object.
133  */
134 static void
135 sp_stroke_style_paint_selection_modified ( SPWidget *spw,
136                                         Inkscape::Selection *selection,
137                                         guint flags,
138                                         SPPaintSelector *psel)
140     if (flags & ( SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG |
141                   SP_OBJECT_STYLE_MODIFIED_FLAG) ) {
142         sp_stroke_style_paint_update(spw);
143     }
147 /**
148  * On signal selection changed, invokes an update of the stroke style paint object.
149  */
150 static void
151 sp_stroke_style_paint_selection_changed ( SPWidget *spw,
152                                         Inkscape::Selection *selection,
153                                         SPPaintSelector *psel )
155     sp_stroke_style_paint_update (spw);
159 /**
160  * On signal change subselection, invoke an update of the stroke style widget.
161  */
162 static void
163 sp_stroke_style_widget_change_subselection ( Inkscape::Application *inkscape, 
164                                         SPDesktop *desktop,
165                                         SPWidget *spw )
167     sp_stroke_style_paint_update (spw);
170 /**
171  * Gets the active stroke style property, then sets the appropriate color, alpha, gradient,
172  * pattern, etc. for the paint-selector.
173  */
174 static void
175 sp_stroke_style_paint_update (SPWidget *spw)
177     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
178         return;
179     }
181     gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
183     SPPaintSelector *psel = SP_PAINT_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "paint-selector"));
185     // create temporary style
186     SPStyle *query = sp_style_new ();
187     // query into it
188     int result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKE); 
190     switch (result) {
191         case QUERY_STYLE_NOTHING:
192         {
193             /* No paint at all */
194             sp_paint_selector_set_mode (psel, SP_PAINT_SELECTOR_MODE_EMPTY);
195             break;
196         }
198         case QUERY_STYLE_SINGLE:
199         case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently, e.g. display "averaged" somewhere in paint selector
200         case QUERY_STYLE_MULTIPLE_SAME:
201         {
202             SPPaintSelectorMode pselmode = sp_style_determine_paint_selector_mode (query, false);
203             sp_paint_selector_set_mode (psel, pselmode);
205             if (query->stroke.set && query->stroke.type == SP_PAINT_TYPE_COLOR) {
206                 gfloat d[3];
207                 sp_color_get_rgb_floatv (&query->stroke.value.color, d);
208                 SPColor color;
209                 sp_color_set_rgb_float (&color, d[0], d[1], d[2]);
210                 sp_paint_selector_set_color_alpha (psel, &color, SP_SCALE24_TO_FLOAT (query->stroke_opacity.value));
212             } else if (query->stroke.set && query->stroke.type == SP_PAINT_TYPE_PAINTSERVER) {
214                 SPPaintServer *server = SP_STYLE_STROKE_SERVER (query);
216                 if (SP_IS_LINEARGRADIENT (server)) {
217                     SPGradient *vector = sp_gradient_get_vector (SP_GRADIENT (server), FALSE);
218                     sp_paint_selector_set_gradient_linear (psel, vector);
220                     SPLinearGradient *lg = SP_LINEARGRADIENT (server);
221                     sp_paint_selector_set_gradient_properties (psel,
222                                                        SP_GRADIENT_UNITS (lg),
223                                                        SP_GRADIENT_SPREAD (lg));
224                 } else if (SP_IS_RADIALGRADIENT (server)) {
225                     SPGradient *vector = sp_gradient_get_vector (SP_GRADIENT (server), FALSE);
226                     sp_paint_selector_set_gradient_radial (psel, vector);
228                     SPRadialGradient *rg = SP_RADIALGRADIENT (server);
229                     sp_paint_selector_set_gradient_properties (psel,
230                                                        SP_GRADIENT_UNITS (rg),
231                                                        SP_GRADIENT_SPREAD (rg));
232                 } else if (SP_IS_PATTERN (server)) {
233                     SPPattern *pat = pattern_getroot (SP_PATTERN (server));
234                     sp_update_pattern_list (psel, pat);
235                 }
236             }
237             break;
238         }
240         case QUERY_STYLE_MULTIPLE_DIFFERENT:
241         {
242             sp_paint_selector_set_mode (psel, SP_PAINT_SELECTOR_MODE_MULTIPLE);
243             break;
244         }
245     }
247     g_free (query);
249     gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
252 /**
253  * When the mode is changed, invoke a regular changed handler.
254  */
255 static void
256 sp_stroke_style_paint_mode_changed( SPPaintSelector *psel,
257                                     SPPaintSelectorMode mode,
258                                     SPWidget *spw )
260     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
261         return;
262     }
264     /* TODO: Does this work?
265      * Not really, here we have to get old color back from object
266      * Instead of relying on paint widget having meaningful colors set
267      */
268     sp_stroke_style_paint_changed(psel, spw);
271 static gchar *undo_label_1 = "stroke:flatcolor:1";
272 static gchar *undo_label_2 = "stroke:flatcolor:2";
273 static gchar *undo_label = undo_label_1;
275 /**
276  * When a drag callback occurs on a paint selector object, if it is a RGB or CMYK 
277  * color mode, then set the stroke opacity to psel's flat color.
278  */
279 static void
280 sp_stroke_style_paint_dragged(SPPaintSelector *psel, SPWidget *spw)
282     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
283         return;
284     }
286     switch (psel->mode) {
287         case SP_PAINT_SELECTOR_MODE_COLOR_RGB:
288         case SP_PAINT_SELECTOR_MODE_COLOR_CMYK:
289         {
290             sp_paint_selector_set_flat_color (psel, SP_ACTIVE_DESKTOP, "stroke", "stroke-opacity");
291             sp_document_maybe_done (sp_desktop_document(SP_ACTIVE_DESKTOP), undo_label, SP_VERB_DIALOG_FILL_STROKE, 
292                                     _("Set stroke color"));
293             break;
294         }
296         default:
297             g_warning( "file %s: line %d: Paint %d should not emit 'dragged'",
298                        __FILE__, __LINE__, psel->mode);
299             break;
300     }
303 /**
304  * When the stroke style's paint settings change, this handler updates the
305  * repr's stroke css style and applies the style to relevant drawing items.
306  */
307 static void
308 sp_stroke_style_paint_changed(SPPaintSelector *psel, SPWidget *spw)
310     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
311         return;
312     }
313     g_object_set_data (G_OBJECT (spw), "update", GINT_TO_POINTER (TRUE));
315     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
316     SPDocument *document = sp_desktop_document (desktop);
317     Inkscape::Selection *selection = sp_desktop_selection (desktop);
319     GSList const *items = selection->itemList();
321     switch (psel->mode) {
322         case SP_PAINT_SELECTOR_MODE_EMPTY:
323             // This should not happen.
324             g_warning ( "file %s: line %d: Paint %d should not emit 'changed'",
325                         __FILE__, __LINE__, psel->mode);
326             break;
327         case SP_PAINT_SELECTOR_MODE_MULTIPLE:
328             // This happens when you switch multiple objects with different gradients to flat color;
329             // nothing to do here.
330             break;
332         case SP_PAINT_SELECTOR_MODE_NONE:
333         {
334             SPCSSAttr *css = sp_repr_css_attr_new();
335             sp_repr_css_set_property(css, "stroke", "none");
337             sp_desktop_set_style (desktop, css);
339             sp_repr_css_attr_unref(css);
341             sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE, 
342                              _("Remove stroke"));
343             break;
344         }
346         case SP_PAINT_SELECTOR_MODE_COLOR_RGB:
347         case SP_PAINT_SELECTOR_MODE_COLOR_CMYK:
348         {
349             sp_paint_selector_set_flat_color (psel, desktop, "stroke", "stroke-opacity");
350             sp_document_maybe_done (sp_desktop_document(desktop), undo_label, SP_VERB_DIALOG_FILL_STROKE, 
351                                     _("Set stroke color"));
353             // on release, toggle undo_label so that the next drag will not be lumped with this one
354             if (undo_label == undo_label_1)
355                 undo_label = undo_label_2;
356             else 
357                 undo_label = undo_label_1;
359             break;
360         }
362         case SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR:
363         case SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL:
364             if (items) {
365                 SPGradientType const gradient_type = ( psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR
366                                                        ? SP_GRADIENT_TYPE_LINEAR
367                                                        : SP_GRADIENT_TYPE_RADIAL );
368                 SPGradient *vector = sp_paint_selector_get_gradient_vector(psel);
369                 if (!vector) {
370                     /* No vector in paint selector should mean that we just changed mode */
372                     SPStyle *query = sp_style_new ();
373                     int result = objects_query_fillstroke ((GSList *) items, query, false); 
374                     guint32 common_rgb = 0;
375                     if (result == QUERY_STYLE_MULTIPLE_SAME) {
376                         if (query->fill.type != SP_PAINT_TYPE_COLOR) {
377                             common_rgb = sp_desktop_get_color(desktop, false);
378                         } else {
379                             common_rgb = sp_color_get_rgba32_ualpha(&query->stroke.value.color, 0xff);
380                         }
381                         vector = sp_document_default_gradient_vector(document, common_rgb);
382                     }
383                     g_free (query);
385                     for (GSList const *i = items; i != NULL; i = i->next) {
386                         if (!vector) {
387                             sp_item_set_gradient(SP_ITEM(i->data), 
388                                                  sp_gradient_vector_for_object(document, desktop, SP_OBJECT(i->data), false),
389                                                  gradient_type, false);
390                         } else {
391                             sp_item_set_gradient(SP_ITEM(i->data), vector, gradient_type, false);
392                         }
393                     }
394                 } else {
395                     vector = sp_gradient_ensure_vector_normalized(vector);
396                     for (GSList const *i = items; i != NULL; i = i->next) {
397                         SPGradient *gr = sp_item_set_gradient(SP_ITEM(i->data), vector, gradient_type, false);
398                         sp_gradient_selector_attrs_to_gradient(gr, psel);
399                     }
400                 }
402                 sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE, 
403                                  _("Set gradient on stroke"));
404             }
405             break;
407         case SP_PAINT_SELECTOR_MODE_PATTERN:
409             if (items) {
411                 SPPattern *pattern = sp_paint_selector_get_pattern (psel);
412                 if (!pattern) {
414                     /* No Pattern in paint selector should mean that we just
415                      * changed mode - dont do jack.
416                      */
418                 } else {
419                     Inkscape::XML::Node *patrepr = SP_OBJECT_REPR(pattern);
420                     SPCSSAttr *css = sp_repr_css_attr_new ();
421                     gchar *urltext = g_strdup_printf ("url(#%s)", patrepr->attribute("id"));
422                     sp_repr_css_set_property (css, "stroke", urltext);
424                     for (GSList const *i = items; i != NULL; i = i->next) {
425                          Inkscape::XML::Node *selrepr = SP_OBJECT_REPR (i->data);
426                          SPObject *selobj = SP_OBJECT (i->data);
427                          if (!selrepr)
428                              continue;
430                          SPStyle *style = SP_OBJECT_STYLE (selobj);
431                          if (style && style->stroke.type == SP_PAINT_TYPE_PAINTSERVER) {
432                              SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (selobj);
433                              if (SP_IS_PATTERN (server) && pattern_getroot (SP_PATTERN(server)) == pattern)
434                                  // only if this object's pattern is not rooted in our selected pattern, apply
435                                  continue;
436                          }
438                          sp_repr_css_change_recursive (selrepr, css, "style");
439                      }
441                     sp_repr_css_attr_unref (css);
442                     g_free (urltext);
444                 } // end if
446                 sp_document_done (document, SP_VERB_DIALOG_FILL_STROKE, 
447                                   _("Set pattern on stroke"));
448             } // end if
450             break;
452         case SP_PAINT_SELECTOR_MODE_UNSET:
453             if (items) {
454                     SPCSSAttr *css = sp_repr_css_attr_new ();
455                     sp_repr_css_unset_property (css, "stroke");
457                     sp_desktop_set_style (desktop, css);
458                     sp_repr_css_attr_unref (css);
460                     sp_document_done (document, SP_VERB_DIALOG_FILL_STROKE, 
461                                       _("Unset stroke"));
462             }
463             break;
465         default:
466             g_warning( "file %s: line %d: Paint selector should not be in "
467                        "mode %d",
468                        __FILE__, __LINE__,
469                        psel->mode );
470             break;
471     }
473     g_object_set_data (G_OBJECT (spw), "update", GINT_TO_POINTER (FALSE));
480 /* Line */
482 static void sp_stroke_style_line_construct(SPWidget *spw, gpointer data);
483 static void sp_stroke_style_line_selection_modified (SPWidget *spw,
484                                                   Inkscape::Selection *selection,
485                                                   guint flags,
486                                                   gpointer data);
488 static void sp_stroke_style_line_selection_changed (SPWidget *spw,
489                                                    Inkscape::Selection *selection,
490                                                    gpointer data );
492 static void sp_stroke_style_line_update(SPWidget *spw, Inkscape::Selection *sel);
494 static void sp_stroke_style_set_join_buttons(SPWidget *spw,
495                                              GtkWidget *active);
497 static void sp_stroke_style_set_cap_buttons(SPWidget *spw,
498                                             GtkWidget *active);
500 static void sp_stroke_style_width_changed(GtkAdjustment *adj, SPWidget *spw);
501 static void sp_stroke_style_miterlimit_changed(GtkAdjustment *adj, SPWidget *spw);
502 static void sp_stroke_style_any_toggled(GtkToggleButton *tb, SPWidget *spw);
503 static void sp_stroke_style_line_dash_changed(SPDashSelector *dsel,
504                                               SPWidget *spw);
506 static void sp_stroke_style_update_marker_menus(SPWidget *spw, GSList const *objects);
508 static SPObject *ink_extract_marker_name(gchar const *n);
511 /**
512  * Helper function for creating radio buttons.  This should probably be re-thought out
513  * when reimplementing this with Gtkmm.
514  */
515 static GtkWidget *
516 sp_stroke_radio_button(GtkWidget *tb, char const *icon,
517                        GtkWidget *hb, GtkWidget *spw,
518                        gchar const *key, gchar const *data)
520     g_assert(icon != NULL);
521     g_assert(hb  != NULL);
522     g_assert(spw != NULL);
524     if (tb == NULL) {
525         tb = gtk_radio_button_new(NULL);
526     } else {
527         tb = gtk_radio_button_new(gtk_radio_button_group(GTK_RADIO_BUTTON(tb)) );
528     }
530     gtk_widget_show(tb);
531     gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(tb), FALSE);
532     gtk_box_pack_start(GTK_BOX(hb), tb, FALSE, FALSE, 0);
533     gtk_object_set_data(GTK_OBJECT(spw), icon, tb);
534     gtk_object_set_data(GTK_OBJECT(tb), key, (gpointer*)data);
535     gtk_signal_connect(GTK_OBJECT(tb), "toggled",
536                        GTK_SIGNAL_FUNC(sp_stroke_style_any_toggled),
537                        spw);
538     GtkWidget *px = sp_icon_new(Inkscape::ICON_SIZE_LARGE_TOOLBAR, icon);
539     g_assert(px != NULL);
540     gtk_widget_show(px);
541     gtk_container_add(GTK_CONTAINER(tb), px);
543     return tb;
547 /**
548  * Creates a copy of the marker named mname, determines its visible and renderable
549  * area in menu_id's bounding box, and then renders it.  This allows us to fill in
550  * preview images of each marker in the marker menu.
551  */
552 static GtkWidget *
553 sp_marker_prev_new(unsigned size, gchar const *mname,
554                    SPDocument *source, SPDocument *sandbox,
555                    gchar *menu_id, NRArena const *arena, unsigned visionkey, NRArenaItem *root)
557     // Retrieve the marker named 'mname' from the source SVG document
558     SPObject const *marker = source->getObjectById(mname);
559     if (marker == NULL)
560         return NULL;
562     // Create a copy repr of the marker with id="sample"
563     Inkscape::XML::Node *mrepr = SP_OBJECT_REPR (marker)->duplicate();
564     mrepr->setAttribute("id", "sample");
566     // Replace the old sample in the sandbox by the new one
567     Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (sandbox->getObjectById("defs"));
568     SPObject *oldmarker = sandbox->getObjectById("sample");
569     if (oldmarker)
570         oldmarker->deleteObject(false);
571     defsrepr->appendChild(mrepr);
572     Inkscape::GC::release(mrepr);
574 // Uncomment this to get the sandbox documents saved (useful for debugging)
575     //FILE *fp = fopen (g_strconcat(mname, ".svg", NULL), "w");
576     //sp_repr_save_stream (sp_document_repr_doc (sandbox), fp);
577     //fclose (fp);
579     // object to render; note that the id is the same as that of the menu we're building
580     SPObject *object = sandbox->getObjectById(menu_id);
581     sp_document_root (sandbox)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
582     sp_document_ensure_up_to_date(sandbox);
584     if (object == NULL || !SP_IS_ITEM(object))
585         return NULL; // sandbox broken?
587     // Find object's bbox in document
588     NR::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
590     NR::Rect const dbox = SP_ITEM(object)->invokeBbox(i2doc);
592     if (dbox.isEmpty()) {
593         return NULL;
594     }
596     /* Update to renderable state */
597     NRMatrix t;
598     double sf = 0.8;
599     nr_matrix_set_scale(&t, sf, sf);
600     nr_arena_item_set_transform(root, &t);
601     NRGC gc(NULL);
602     nr_matrix_set_identity(&gc.transform);
603     nr_arena_item_invoke_update( root, NULL, &gc,
604                                  NR_ARENA_ITEM_STATE_ALL,
605                                  NR_ARENA_ITEM_STATE_NONE );
607     /* Item integer bbox in points */
608     NRRectL ibox;
609     ibox.x0 = (int) floor(sf * dbox.min()[NR::X] + 0.5);
610     ibox.y0 = (int) floor(sf * dbox.min()[NR::Y] + 0.5);
611     ibox.x1 = (int) floor(sf * dbox.max()[NR::X] + 0.5);
612     ibox.y1 = (int) floor(sf * dbox.max()[NR::Y] + 0.5);
614     /* Find visible area */
615     int width = ibox.x1 - ibox.x0;
616     int height = ibox.y1 - ibox.y0;
617     int dx = size;
618     int dy = size;
619     dx=(dx - width)/2; // watch out for size, since 'unsigned'-'signed' can cause problems if the result is negative
620     dy=(dy - height)/2;
622     NRRectL area;
623     area.x0 = ibox.x0 - dx;
624     area.y0 = ibox.y0 - dy;
625     area.x1 = area.x0 + size;
626     area.y1 = area.y0 + size;
628     /* Actual renderable area */
629     NRRectL ua;
630     ua.x0 = MAX(ibox.x0, area.x0);
631     ua.y0 = MAX(ibox.y0, area.y0);
632     ua.x1 = MIN(ibox.x1, area.x1);
633     ua.y1 = MIN(ibox.y1, area.y1);
635     /* Set up pixblock */
636     guchar *px = g_new(guchar, 4 * size * size);
637     memset(px, 0x00, 4 * size * size);
639     /* Render */
640     NRPixBlock B;
641     nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
642                               ua.x0, ua.y0, ua.x1, ua.y1,
643                               px + 4 * size * (ua.y0 - area.y0) +
644                               4 * (ua.x0 - area.x0),
645                               4 * size, FALSE, FALSE );
646     nr_arena_item_invoke_render( root, &ua, &B,
647                                  NR_ARENA_ITEM_RENDER_NO_CACHE );
648     nr_pixblock_release(&B);
650     // Create widget
651     GtkWidget *pb = gtk_image_new_from_pixbuf(gdk_pixbuf_new_from_data(px,
652                               GDK_COLORSPACE_RGB,
653                               TRUE,
654                               8, size, size, size * 4,
655                               (GdkPixbufDestroyNotify)g_free,
656                               NULL));
657     return pb;
661 /**
662  *  Returns a list of markers in the defs of the given source document as a GSList object
663  *  Returns NULL if there are no markers in the document.
664  */
665 GSList *
666 ink_marker_list_get (SPDocument *source)
668     if (source == NULL)
669         return NULL;
671     GSList *ml   = NULL;
672     SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS (source);
673     for ( SPObject *child = sp_object_first_child(SP_OBJECT(defs));
674           child != NULL;
675           child = SP_OBJECT_NEXT (child) )
676     {
677         if (SP_IS_MARKER(child)) {
678             ml = g_slist_prepend (ml, child);
679         }
680     }
681     return ml;
684 #define MARKER_ITEM_MARGIN 0
686 /**
687  * Adds previews of markers in marker_list to the given menu widget
688  */
689 static void
690 sp_marker_menu_build (GtkWidget *m, GSList *marker_list, SPDocument *source, SPDocument *sandbox, gchar *menu_id)
692     // Do this here, outside of loop, to speed up preview generation:
693     NRArena const *arena = NRArena::create();
694     unsigned const visionkey = sp_item_display_key_new(1);
695     NRArenaItem *root =  sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT (sandbox)), (NRArena *) arena, visionkey, SP_ITEM_SHOW_DISPLAY );
697     for (; marker_list != NULL; marker_list = marker_list->next) {
698         Inkscape::XML::Node *repr = SP_OBJECT_REPR((SPItem *) marker_list->data);
699         GtkWidget *i = gtk_menu_item_new();
700         gtk_widget_show(i);
702         if (repr->attribute("inkscape:stockid"))
703             g_object_set_data (G_OBJECT(i), "stockid", (void *) "true");
704         else
705             g_object_set_data (G_OBJECT(i), "stockid", (void *) "false");
707         gchar const *markid = repr->attribute("id");
708         g_object_set_data (G_OBJECT(i), "marker", (void *) markid);
710         GtkWidget *hb = gtk_hbox_new(FALSE, MARKER_ITEM_MARGIN);
711         gtk_widget_show(hb);
713         // generate preview
714         GtkWidget *prv = sp_marker_prev_new (22, markid, source, sandbox, menu_id, arena, visionkey, root);
715         gtk_widget_show(prv);
716         gtk_box_pack_start(GTK_BOX(hb), prv, FALSE, FALSE, 6);
718         // create label
719         GtkWidget *l = gtk_label_new(repr->attribute("id"));
720         gtk_widget_show(l);
721         gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
723         gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
725         gtk_widget_show(hb);
726         gtk_container_add(GTK_CONTAINER(i), hb);
728         gtk_menu_append(GTK_MENU(m), i);
729     }
732 /**
733  * sp_marker_list_from_doc()
734  *
735  * \brief Pick up all markers from source, except those that are in
736  * current_doc (if non-NULL), and add items to the m menu
737  *
738  */
739 static void
740 sp_marker_list_from_doc (GtkWidget *m, SPDocument *current_doc, SPDocument *source, SPDocument *markers_doc, SPDocument *sandbox, gchar *menu_id)
742     GSList *ml = ink_marker_list_get(source);
743     GSList *clean_ml = NULL;
745     // Do this here, outside of loop, to speed up preview generation:
746     /* Create new arena */
747     NRArena const *arena = NRArena::create();
748     /* Create ArenaItem and set transform */
749     unsigned const visionkey = sp_item_display_key_new(1);
750     NRArenaItem *root =  sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT (sandbox)), (NRArena *) arena, visionkey, SP_ITEM_SHOW_DISPLAY );
752     for (; ml != NULL; ml = ml->next) {
753         if (!SP_IS_MARKER(ml->data))
754             continue;
756         Inkscape::XML::Node *repr = SP_OBJECT_REPR((SPItem *) ml->data);
757         bool stock_dupe = false;
759         GSList * markers_doc_ml = ink_marker_list_get(markers_doc);
760         for (; markers_doc_ml != NULL; markers_doc_ml = markers_doc_ml->next) {
761             const gchar* stockid = SP_OBJECT_REPR(markers_doc_ml->data)->attribute("inkscape:stockid");
762             if (stockid && !strcmp(repr->attribute("inkscape:stockid"), stockid))
763                 stock_dupe = true;
764         }
766         if (stock_dupe) // stock item, dont add to list from current doc
767             continue;
769         // Add to the list of markers we really do wish to show
770         clean_ml = g_slist_prepend (clean_ml, ml->data);
771     }
772     sp_marker_menu_build (m, clean_ml, source, sandbox, menu_id);
774     g_slist_free (ml);
775     g_slist_free (clean_ml);
779 /**
780  * Returns a new document containing default start, mid, and end markers.
781  */
782 SPDocument *
783 ink_markers_preview_doc ()
785 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\">"
786 "  <defs id=\"defs\" />"
788 "  <g id=\"marker-start\">"
789 "    <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:url(#sample);marker-mid:none;marker-end:none\""
790 "       d=\"M 12.5,13 L 25,13\" id=\"path1\" />"
791 "    <rect style=\"fill:none;stroke:none\" id=\"rect2\""
792 "       width=\"25\" height=\"25\" x=\"0\" y=\"0\" />"
793 "  </g>"
795 "  <g id=\"marker-mid\">"
796 "    <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:url(#sample);marker-end:none\""
797 "       d=\"M 0,113 L 12.5,113 L 25,113\" id=\"path11\" />"
798 "    <rect style=\"fill:none;stroke:none\" id=\"rect22\""
799 "       width=\"25\" height=\"25\" x=\"0\" y=\"100\" />"
800 "  </g>"
802 "  <g id=\"marker-end\">"
803 "    <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:none;marker-end:url(#sample)\""
804 "       d=\"M 0,213 L 12.5,213\" id=\"path111\" />"
805 "    <rect style=\"fill:none;stroke:none\" id=\"rect222\""
806 "       width=\"25\" height=\"25\" x=\"0\" y=\"200\" />"
807 "  </g>"
809 "</svg>";
811     return sp_document_new_from_mem (buffer, strlen(buffer), FALSE);
815 /**
816  * Creates a menu widget to display markers from markers.svg
817  */
818 static GtkWidget *
819 ink_marker_menu( GtkWidget *tbl, gchar *menu_id, SPDocument *sandbox)
821     SPDesktop *desktop = inkscape_active_desktop();
822     SPDocument *doc = sp_desktop_document(desktop);
823     GtkWidget *mnu = gtk_option_menu_new();
825     /* Create new menu widget */
826     GtkWidget *m = gtk_menu_new();
827     gtk_widget_show(m);
829     g_object_set_data(G_OBJECT(mnu), "updating", (gpointer) FALSE);
831     if (!doc) {
832         GtkWidget *i = gtk_menu_item_new_with_label(_("No document selected"));
833         gtk_widget_show(i);
834         gtk_menu_append(GTK_MENU(m), i);
835         gtk_widget_set_sensitive(mnu, FALSE);
837     } else {
839         // add "None"
840         {
841             GtkWidget *i = gtk_menu_item_new();
842             gtk_widget_show(i);
844             g_object_set_data(G_OBJECT(i), "marker", (void *) "none");
846             GtkWidget *hb = gtk_hbox_new(FALSE,  MARKER_ITEM_MARGIN);
847             gtk_widget_show(hb);
849             GtkWidget *l = gtk_label_new( _("None") );
850             gtk_widget_show(l);
851             gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
853             gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
855             gtk_widget_show(hb);
856             gtk_container_add(GTK_CONTAINER(i), hb);
857             gtk_menu_append(GTK_MENU(m), i);
858         }
860         // find and load  markers.svg
861         static SPDocument *markers_doc = NULL;
862         char *markers_source = g_build_filename(INKSCAPE_MARKERSDIR, "markers.svg", NULL);
863         if (Inkscape::IO::file_test(markers_source, G_FILE_TEST_IS_REGULAR)) {
864             markers_doc = sp_document_new(markers_source, FALSE);
865         }
866         g_free(markers_source);
868         // suck in from current doc
869         sp_marker_list_from_doc ( m, NULL, doc, markers_doc, sandbox, menu_id );
871         // add separator
872         {
873             GtkWidget *i = gtk_separator_menu_item_new();
874             gtk_widget_show(i);
875             gtk_menu_append(GTK_MENU(m), i);
876         }
878         // suck in from markers.svg
879         if (markers_doc) {
880             sp_document_ensure_up_to_date(doc);
881             sp_marker_list_from_doc ( m, doc, markers_doc, NULL, sandbox, menu_id );
882         }
884         gtk_widget_set_sensitive(mnu, TRUE);
885     }
887     gtk_object_set_data(GTK_OBJECT(mnu), "menu_id", menu_id);
888     gtk_option_menu_set_menu(GTK_OPTION_MENU(mnu), m);
890     /* Set history */
891     gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
893     return mnu;
897 /**
898  * Handles when user selects one of the markers from the marker menu.
899  * Defines a uri string to refer to it, then applies it to all selected
900  * items in the current desktop.
901  */
902 static void
903 sp_marker_select(GtkOptionMenu *mnu, GtkWidget *spw)
905     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
906         return;
907     }
909     SPDesktop *desktop = inkscape_active_desktop();
910     SPDocument *document = sp_desktop_document(desktop);
911     if (!document) {
912         return;
913     }
915     /* Get Marker */
916     if (!g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
917                            "marker"))
918     {
919         return;
920     }
921     gchar *markid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
922                                                 "marker");
923     gchar *marker = "";
924     if (strcmp(markid, "none")){
925        gchar *stockid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
926                                                 "stockid");
928        gchar *markurn = markid;
929        if (!strcmp(stockid,"true")) markurn = g_strconcat("urn:inkscape:marker:",markid,NULL);
930        SPObject *mark = get_stock_item(markurn);
931        if (mark) {
932             Inkscape::XML::Node *repr = SP_OBJECT_REPR(mark);
933             marker = g_strconcat("url(#", repr->attribute("id"), ")", NULL);
934         }
935     } else {
936         marker = markid;
937     }
939     SPCSSAttr *css = sp_repr_css_attr_new();
940     gchar *menu_id = (gchar *) g_object_get_data(G_OBJECT(mnu), "menu_id");
941     sp_repr_css_set_property(css, menu_id, marker);
943      Inkscape::Selection *selection = sp_desktop_selection(desktop);
944      GSList const *items = selection->itemList();
945      for (; items != NULL; items = items->next) {
946          SPItem *item = (SPItem *) items->data;
947          if (!SP_IS_SHAPE(item) || SP_IS_RECT(item)) // can't set marker to rect, until it's converted to using <path>
948              continue;
949          Inkscape::XML::Node *selrepr = SP_OBJECT_REPR((SPItem *) items->data);
950          if (selrepr) {
951              sp_repr_css_change_recursive(selrepr, css, "style");
952          }
953          SP_OBJECT(items->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
954          SP_OBJECT(items->data)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
955      }
957     sp_repr_css_attr_unref(css);
959     sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE, 
960                      _("Set markers"));
963 /**
964  * Sets the stroke width units for all selected items.
965  * Also handles absolute and dimensionless units.
966  */
967 static gboolean stroke_width_set_unit(SPUnitSelector *,
968                                                  SPUnit const *old,
969                                                  SPUnit const *new_units,
970                                                  GObject *spw)
972     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
974     if (!desktop) {
975         return FALSE;
976     }
978     Inkscape::Selection *selection = sp_desktop_selection (desktop);
980     if (selection->isEmpty())
981         return FALSE;
983     GSList const *objects = selection->itemList();
985     if ((old->base == SP_UNIT_ABSOLUTE || old->base == SP_UNIT_DEVICE) &&
986        (new_units->base == SP_UNIT_DIMENSIONLESS)) {
988         /* Absolute to percentage */
989         g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
991         GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
992         float w = sp_units_get_pixels (a->value, *old);
994         gdouble average = stroke_average_width (objects);
996         if (average == NR_HUGE || average == 0)
997             return FALSE;
999         gtk_adjustment_set_value (a, 100.0 * w / average);
1001         g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
1002         return TRUE;
1004     } else if ((old->base == SP_UNIT_DIMENSIONLESS) &&
1005               (new_units->base == SP_UNIT_ABSOLUTE || new_units->base == SP_UNIT_DEVICE)) {
1007         /* Percentage to absolute */
1008         g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
1010         GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
1012         gdouble average = stroke_average_width (objects);
1014         gtk_adjustment_set_value (a, sp_pixels_get_units (0.01 * a->value * average, *new_units));
1016         g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
1017         return TRUE;
1018     }
1020     return FALSE;
1024 /**
1025  * \brief  Creates a new widget for the line stroke style.
1026  *
1027  */
1028 GtkWidget *
1029 sp_stroke_style_line_widget_new(void)
1031     GtkWidget *spw, *f, *t, *hb, *sb, *us, *tb, *ds;
1032     GtkObject *a;
1034     GtkTooltips *tt = gtk_tooltips_new();
1036     spw = sp_widget_new_global(INKSCAPE);
1038     f = gtk_hbox_new (FALSE, 0);
1039     gtk_widget_show(f);
1040     gtk_container_add(GTK_CONTAINER(spw), f);
1042     t = gtk_table_new(3, 6, FALSE);
1043     gtk_widget_show(t);
1044     gtk_container_set_border_width(GTK_CONTAINER(t), 4);
1045     gtk_table_set_row_spacings(GTK_TABLE(t), 4);
1046     gtk_container_add(GTK_CONTAINER(f), t);
1047     gtk_object_set_data(GTK_OBJECT(spw), "stroke", t);
1049     gint i = 0;
1051     /* Stroke width */
1052     spw_label(t, _("Width:"), 0, i);
1054     hb = spw_hbox(t, 3, 1, i);
1056 // TODO: when this is gtkmmified, use an Inkscape::UI::Widget::ScalarUnit instead of the separate
1057 // spinbutton and unit selector for stroke width. In sp_stroke_style_line_update, use
1058 // setHundredPercent to remember the aeraged width corresponding to 100%. Then the
1059 // stroke_width_set_unit will be removed (because ScalarUnit takes care of conversions itself), and
1060 // with it, the two remaining calls of stroke_average_width, allowing us to get rid of that
1061 // function in desktop-style.
1063     a = gtk_adjustment_new(1.0, 0.0, 1000.0, 0.1, 10.0, 10.0);
1064     gtk_object_set_data(GTK_OBJECT(spw), "width", a);
1065     sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 3);
1066     gtk_tooltips_set_tip(tt, sb, _("Stroke width"), NULL);
1067     gtk_widget_show(sb);
1069     sp_dialog_defocus_on_enter(sb);
1071     gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1072     us = sp_unit_selector_new(SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE);
1073     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1074     if (desktop)
1075         sp_unit_selector_set_unit (SP_UNIT_SELECTOR(us), sp_desktop_namedview(desktop)->doc_units);
1076     sp_unit_selector_add_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT), 0);
1077     g_signal_connect ( G_OBJECT (us), "set_unit", G_CALLBACK (stroke_width_set_unit), spw );
1078     gtk_widget_show(us);
1079     sp_unit_selector_add_adjustment( SP_UNIT_SELECTOR(us), GTK_ADJUSTMENT(a) );
1080     gtk_box_pack_start(GTK_BOX(hb), us, FALSE, FALSE, 0);
1081     gtk_object_set_data(GTK_OBJECT(spw), "units", us);
1083     gtk_signal_connect( GTK_OBJECT(a), "value_changed", GTK_SIGNAL_FUNC(sp_stroke_style_width_changed), spw );
1084     i++;
1086     /* Join type */
1087     // TRANSLATORS: The line join style specifies the shape to be used at the
1088     //  corners of paths. It can be "miter", "round" or "bevel".
1089     spw_label(t, _("Join:"), 0, i);
1091     hb = spw_hbox(t, 3, 1, i);
1093     tb = NULL;
1095     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_MITER,
1096                                 hb, spw, "join", "miter");
1098     // TRANSLATORS: Miter join: joining lines with a sharp (pointed) corner.
1099     //  For an example, draw a triangle with a large stroke width and modify the
1100     //  "Join" option (in the Fill and Stroke dialog).
1101     gtk_tooltips_set_tip(tt, tb, _("Miter join"), NULL);
1103     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_ROUND,
1104                                 hb, spw, "join", "round");
1106     // TRANSLATORS: Round join: joining lines with a rounded corner.
1107     //  For an example, draw a triangle with a large stroke width and modify the
1108     //  "Join" option (in the Fill and Stroke dialog).
1109     gtk_tooltips_set_tip(tt, tb, _("Round join"), NULL);
1111     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_BEVEL,
1112                                 hb, spw, "join", "bevel");
1114     // TRANSLATORS: Bevel join: joining lines with a blunted (flattened) corner.
1115     //  For an example, draw a triangle with a large stroke width and modify the
1116     //  "Join" option (in the Fill and Stroke dialog).
1117     gtk_tooltips_set_tip(tt, tb, _("Bevel join"), NULL);
1119     i++;
1121     /* Miterlimit  */
1122     // TRANSLATORS: Miter limit: only for "miter join", this limits the length
1123     //  of the sharp "spike" when the lines connect at too sharp an angle.
1124     // When two line segments meet at a sharp angle, a miter join results in a
1125     //  spike that extends well beyond the connection point. The purpose of the
1126     //  miter limit is to cut off such spikes (i.e. convert them into bevels)
1127     //  when they become too long.
1128     spw_label(t, _("Miter limit:"), 0, i);
1130     hb = spw_hbox(t, 3, 1, i);
1132     a = gtk_adjustment_new(4.0, 0.0, 100.0, 0.1, 10.0, 10.0);
1133     gtk_object_set_data(GTK_OBJECT(spw), "miterlimit", a);
1135     sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 2);
1136     gtk_tooltips_set_tip(tt, sb, _("Maximum length of the miter (in units of stroke width)"), NULL);
1137     gtk_widget_show(sb);
1138     gtk_object_set_data(GTK_OBJECT(spw), "miterlimit_sb", sb);
1139     sp_dialog_defocus_on_enter(sb);
1141     gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1143     gtk_signal_connect( GTK_OBJECT(a), "value_changed",
1144                         GTK_SIGNAL_FUNC(sp_stroke_style_miterlimit_changed), spw );
1145     i++;
1147     /* Cap type */
1148     // TRANSLATORS: cap type specifies the shape for the ends of lines
1149     spw_label(t, _("Cap:"), 0, i);
1151     hb = spw_hbox(t, 3, 1, i);
1153     tb = NULL;
1155     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_BUTT,
1156                                 hb, spw, "cap", "butt");
1158     // TRANSLATORS: Butt cap: the line shape does not extend beyond the end point
1159     //  of the line; the ends of the line are square
1160     gtk_tooltips_set_tip(tt, tb, _("Butt cap"), NULL);
1162     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_ROUND,
1163                                 hb, spw, "cap", "round");
1165     // TRANSLATORS: Round cap: the line shape extends beyond the end point of the
1166     //  line; the ends of the line are rounded
1167     gtk_tooltips_set_tip(tt, tb, _("Round cap"), NULL);
1169     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_SQUARE,
1170                                 hb, spw, "cap", "square");
1172     // TRANSLATORS: Square cap: the line shape extends beyond the end point of the
1173     //  line; the ends of the line are square
1174     gtk_tooltips_set_tip(tt, tb, _("Square cap"), NULL);
1176     i++;
1179     /* Dash */
1180     spw_label(t, _("Dashes:"), 0, i);
1181     ds = sp_dash_selector_new( inkscape_get_repr( INKSCAPE,
1182                                                   "palette.dashes") );
1184     gtk_widget_show(ds);
1185     gtk_table_attach( GTK_TABLE(t), ds, 1, 4, i, i+1,
1186                       (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1187                       (GtkAttachOptions)0, 0, 0 );
1188     gtk_object_set_data(GTK_OBJECT(spw), "dash", ds);
1189     gtk_signal_connect( GTK_OBJECT(ds), "changed",
1190                         GTK_SIGNAL_FUNC(sp_stroke_style_line_dash_changed),
1191                         spw );
1192     i++;
1194     /* Drop down marker selectors*/
1196     // doing this here once, instead of for each preview, to speed things up
1197     SPDocument *sandbox = ink_markers_preview_doc ();
1199     // TRANSLATORS: Path markers are an SVG feature that allows you to attach arbitrary shapes
1200     // (arrowheads, bullets, faces, whatever) to the start, end, or middle nodes of a path.
1201     spw_label(t, _("Start Markers:"), 0, i);
1202     GtkWidget *mnu  = ink_marker_menu( spw ,"marker-start", sandbox);
1203     gtk_signal_connect( GTK_OBJECT(mnu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1204     gtk_widget_show(mnu);
1205     gtk_table_attach( GTK_TABLE(t), mnu, 1, 4, i, i+1,
1206                       (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1207                       (GtkAttachOptions)0, 0, 0 );
1208     gtk_object_set_data(GTK_OBJECT(spw), "start_mark_menu", mnu);
1210     i++;
1211     spw_label(t, _("Mid Markers:"), 0, i);
1212     mnu = NULL;
1213     mnu  = ink_marker_menu( spw ,"marker-mid", sandbox);
1214     gtk_signal_connect( GTK_OBJECT(mnu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1215     gtk_widget_show(mnu);
1216     gtk_table_attach( GTK_TABLE(t), mnu, 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", mnu);
1221     i++;
1222     spw_label(t, _("End Markers:"), 0, i);
1223     mnu = NULL;
1224     mnu  = ink_marker_menu( spw ,"marker-end", sandbox);
1225     gtk_signal_connect( GTK_OBJECT(mnu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1226     gtk_widget_show(mnu);
1227     gtk_table_attach( GTK_TABLE(t), mnu, 1, 4, i, i+1,
1228                       (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1229                       (GtkAttachOptions)0, 0, 0 );
1230     gtk_object_set_data(GTK_OBJECT(spw), "end_mark_menu", mnu);
1232     i++;
1234     gtk_signal_connect( GTK_OBJECT(spw), "construct",
1235                         GTK_SIGNAL_FUNC(sp_stroke_style_line_construct),
1236                         NULL );
1237     gtk_signal_connect( GTK_OBJECT(spw), "modify_selection",
1238                         GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_modified),
1239                         NULL );
1240     gtk_signal_connect( GTK_OBJECT(spw), "change_selection",
1241                         GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_changed),
1242                         NULL );
1244     sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
1246     return spw;
1250 /**
1251  * Callback for when the stroke style widget is called.  It causes
1252  * the stroke line style to be updated.
1253  */
1254 static void
1255 sp_stroke_style_line_construct(SPWidget *spw, gpointer data)
1258 #ifdef SP_SS_VERBOSE
1259     g_print( "Stroke style widget constructed: inkscape %p repr %p\n",
1260              spw->inkscape, spw->repr );
1261 #endif
1262     if (spw->inkscape) {
1263         sp_stroke_style_line_update(spw,
1264                                     ( SP_ACTIVE_DESKTOP
1265                                       ? sp_desktop_selection(SP_ACTIVE_DESKTOP)
1266                                       : NULL ));
1267     } 
1270 /**
1271  * Callback for when stroke style widget is modified.  
1272  * Triggers update action.
1273  */
1274 static void
1275 sp_stroke_style_line_selection_modified ( SPWidget *spw,
1276                                        Inkscape::Selection *selection,
1277                                        guint flags,
1278                                        gpointer data )
1280     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1281         sp_stroke_style_line_update (spw, selection);
1282     }
1286 /**
1287  * Callback for when stroke style widget is changed.
1288  * Triggers update action.
1289  */
1290 static void
1291 sp_stroke_style_line_selection_changed ( SPWidget *spw,
1292                                        Inkscape::Selection *selection,
1293                                        gpointer data )
1295     sp_stroke_style_line_update (spw, selection);
1299 /**
1300  * Sets selector widgets' dash style from an SPStyle object.
1301  */
1302 static void
1303 sp_dash_selector_set_from_style (GtkWidget *dsel, SPStyle *style)
1305     if (style->stroke_dash.n_dash > 0) {
1306         double d[64];
1307         int len = MIN(style->stroke_dash.n_dash, 64);
1308         for (int i = 0; i < len; i++) {
1309             if (style->stroke_width.computed != 0)
1310                 d[i] = style->stroke_dash.dash[i] / style->stroke_width.computed;
1311             else
1312                 d[i] = style->stroke_dash.dash[i]; // is there a better thing to do for stroke_width==0?
1313         }
1314         sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), len, d,
1315                style->stroke_width.computed != 0?
1316                     style->stroke_dash.offset / style->stroke_width.computed  :
1317                     style->stroke_dash.offset);
1318     } else {
1319         sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), 0, NULL, 0.0);
1320     }
1323 /**
1324  * Sets the join type for a line, and updates the stroke style widget's buttons
1325  */
1326 static void
1327 sp_jointype_set (SPWidget *spw, unsigned const jointype)
1329     GtkWidget *tb = NULL;
1330     switch (jointype) {
1331         case SP_STROKE_LINEJOIN_MITER:
1332             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_MITER));
1333             break;
1334         case SP_STROKE_LINEJOIN_ROUND:
1335             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_ROUND));
1336             break;
1337         case SP_STROKE_LINEJOIN_BEVEL:
1338             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_BEVEL));
1339             break;
1340         default:
1341             break;
1342     }
1343     sp_stroke_style_set_join_buttons (spw, tb);
1346 /**
1347  * Sets the cap type for a line, and updates the stroke style widget's buttons
1348  */
1349 static void
1350 sp_captype_set (SPWidget *spw, unsigned const captype)
1352     GtkWidget *tb = NULL;
1353     switch (captype) {
1354         case SP_STROKE_LINECAP_BUTT:
1355             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_BUTT));
1356             break;
1357         case SP_STROKE_LINECAP_ROUND:
1358             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_ROUND));
1359             break;
1360         case SP_STROKE_LINECAP_SQUARE:
1361             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_SQUARE));
1362             break;
1363         default:
1364             break;
1365     }
1366     sp_stroke_style_set_cap_buttons (spw, tb);
1369 /**
1370  * Callback for when stroke style widget is updated, including markers, cap type,
1371  * join type, etc.
1372  */
1373 static void
1374 sp_stroke_style_line_update(SPWidget *spw, Inkscape::Selection *sel)
1376     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1377         return;
1378     }
1380     gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1382     GtkWidget *sset = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "stroke"));
1383     GtkObject *width = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1384     GtkObject *ml = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1385     GtkWidget *us = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1386     GtkWidget *dsel = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1388     // create temporary style
1389     SPStyle *query = sp_style_new ();
1390     // query into it
1391     int result_sw = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEWIDTH); 
1392     int result_ml = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT); 
1393     int result_cap = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKECAP); 
1394     int result_join = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEJOIN); 
1396     if (result_sw == QUERY_STYLE_NOTHING) {
1397         /* No objects stroked, set insensitive */
1398         gtk_widget_set_sensitive(sset, FALSE);
1400         gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1401         return;
1402     } else {
1403         gtk_widget_set_sensitive(sset, TRUE);
1405         SPUnit const *unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1407         if (result_sw == QUERY_STYLE_MULTIPLE_AVERAGED) {
1408             sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT));
1409         } else {
1410             // same width, or only one object; no sense to keep percent, switch to absolute
1411             if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1412                 sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), sp_desktop_namedview(SP_ACTIVE_DESKTOP)->doc_units);
1413             }
1414         }
1416         unit = sp_unit_selector_get_unit (SP_UNIT_SELECTOR (us));
1418         if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1419             double avgwidth = sp_pixels_get_units (query->stroke_width.computed, *unit);
1420             gtk_adjustment_set_value(GTK_ADJUSTMENT(width), avgwidth);
1421         } else {
1422             gtk_adjustment_set_value(GTK_ADJUSTMENT(width), 100);
1423         }
1424     }
1426     if (result_ml != QUERY_STYLE_NOTHING)
1427         gtk_adjustment_set_value(GTK_ADJUSTMENT(ml), query->stroke_miterlimit.value); // TODO: reflect averagedness?
1429     if (result_join != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1430         sp_jointype_set (spw, query->stroke_linejoin.value);
1431     } else {
1432         sp_stroke_style_set_join_buttons(spw, NULL);
1433     }
1435     if (result_cap != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1436         sp_captype_set (spw, query->stroke_linecap.value);
1437     } else {
1438         sp_stroke_style_set_cap_buttons(spw, NULL);
1439     }
1441     g_free (query);
1443     GSList const *objects = sel->itemList();
1444     SPObject * const object = SP_OBJECT(objects->data);
1445     SPStyle * const style = SP_OBJECT_STYLE(object);
1447     /* Markers */
1448     sp_stroke_style_update_marker_menus(spw, objects); // FIXME: make this desktop query too
1450     /* Dash */
1451     sp_dash_selector_set_from_style (dsel, style); // FIXME: make this desktop query too
1453     gtk_widget_set_sensitive(sset, TRUE);
1455     gtk_object_set_data(GTK_OBJECT(spw), "update",
1456                         GINT_TO_POINTER(FALSE));
1459 /**
1460  * Sets a line's dash properties in a CSS style object.
1461  */
1462 static void
1463 sp_stroke_style_set_scaled_dash(SPCSSAttr *css,
1464                                 int ndash, double *dash, double offset,
1465                                 double scale)
1467     if (ndash > 0) {
1468         Inkscape::CSSOStringStream osarray;
1469         for (int i = 0; i < ndash; i++) {
1470             osarray << dash[i] * scale;
1471             if (i < (ndash - 1)) {
1472                 osarray << ",";
1473             }
1474         }
1475         sp_repr_css_set_property(css, "stroke-dasharray", osarray.str().c_str());
1477         Inkscape::CSSOStringStream osoffset;
1478         osoffset << offset * scale;
1479         sp_repr_css_set_property(css, "stroke-dashoffset", osoffset.str().c_str());
1480     } else {
1481         sp_repr_css_set_property(css, "stroke-dasharray", "none");
1482         sp_repr_css_set_property(css, "stroke-dashoffset", NULL);
1483     }
1486 /**
1487  * Sets line properties like width, dashes, markers, etc. on all currently selected items.
1488  */
1489 static void
1490 sp_stroke_style_scale_line(SPWidget *spw)
1492     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1493         return;
1494     }
1496     gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1498     GtkAdjustment *wadj = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1499     SPUnitSelector *us = SP_UNIT_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1500     SPDashSelector *dsel = SP_DASH_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1501     GtkAdjustment *ml = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1503     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1504     SPDocument *document = sp_desktop_document (desktop);
1505     Inkscape::Selection *selection = sp_desktop_selection (desktop);
1507     GSList const *items = selection->itemList();
1509     /* TODO: Create some standardized method */
1510     SPCSSAttr *css = sp_repr_css_attr_new();
1512     if (items) {
1514         double width_typed = wadj->value;
1515         double const miterlimit = ml->value;
1517         SPUnit const *const unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1519         double *dash, offset;
1520         int ndash;
1521         sp_dash_selector_get_dash(dsel, &ndash, &dash, &offset);
1523         for (GSList const *i = items; i != NULL; i = i->next) {
1524             /* Set stroke width */
1525             double width;
1526             if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1527                 width = sp_units_get_pixels (width_typed, *unit);
1528             } else { // percentage
1529                 gdouble old_w = SP_OBJECT_STYLE (i->data)->stroke_width.computed;
1530                 width = old_w * width_typed / 100;
1531             }
1533             {
1534                 Inkscape::CSSOStringStream os_width;
1535                 os_width << width;
1536                 sp_repr_css_set_property(css, "stroke-width", os_width.str().c_str());
1537             }
1539             {
1540                 Inkscape::CSSOStringStream os_ml;
1541                 os_ml << miterlimit;
1542                 sp_repr_css_set_property(css, "stroke-miterlimit", os_ml.str().c_str());
1543             }
1545             /* Set dash */
1546             sp_stroke_style_set_scaled_dash(css, ndash, dash, offset, width);
1548             sp_desktop_apply_css_recursive (SP_OBJECT(i->data), css, true);
1549         }
1551         g_free(dash);
1553         if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1554             // reset to 100 percent
1555             gtk_adjustment_set_value (wadj, 100.0);
1556         }
1558     }
1560     // we have already changed the items, so set style without changing selection
1561     // FIXME: move the above stroke-setting stuff, including percentages, to desktop-style
1562     sp_desktop_set_style (desktop, css, false);
1564     sp_repr_css_attr_unref(css);
1566     sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE, 
1567                      _("Set stroke style"));
1569     gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1573 /**
1574  * Callback for when the stroke style's width changes.  
1575  * Causes all line styles to be applied to all selected items.
1576  */
1577 static void
1578 sp_stroke_style_width_changed(GtkAdjustment *adj, SPWidget *spw)
1580     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1581         return;
1582     }
1584     sp_stroke_style_scale_line(spw);
1587 /**
1588  * Callback for when the stroke style's miterlimit changes.  
1589  * Causes all line styles to be applied to all selected items.
1590  */
1591 static void
1592 sp_stroke_style_miterlimit_changed(GtkAdjustment *adj, SPWidget *spw)
1594     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1595         return;
1596     }
1598     sp_stroke_style_scale_line(spw);
1601 /**
1602  * Callback for when the stroke style's dash changes.  
1603  * Causes all line styles to be applied to all selected items.
1604  */
1605 static void
1606 sp_stroke_style_line_dash_changed(SPDashSelector *dsel, SPWidget *spw)
1608     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1609         return;
1610     }
1612     sp_stroke_style_scale_line(spw);
1617 /**
1618  * \brief  This routine handles toggle events for buttons in the stroke style
1619  *         dialog.
1620  * When activated, this routine gets the data for the various widgets, and then
1621  * calls the respective routines to update css properties, etc.
1622  *
1623  */
1624 static void
1625 sp_stroke_style_any_toggled(GtkToggleButton *tb, SPWidget *spw)
1627     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1628         return;
1629     }
1631     if (gtk_toggle_button_get_active(tb)) {
1633         gchar const *join
1634             = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "join"));
1635         gchar const *cap
1636             = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "cap"));
1638         if (join) {
1639             GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1640             gtk_widget_set_sensitive (ml, !strcmp(join, "miter"));
1641         }
1643         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1645         /* TODO: Create some standardized method */
1646         SPCSSAttr *css = sp_repr_css_attr_new();
1648         if (join) {
1649             sp_repr_css_set_property(css, "stroke-linejoin", join);
1651             sp_desktop_set_style (desktop, css);
1653             sp_stroke_style_set_join_buttons(spw, GTK_WIDGET(tb));
1654         } else if (cap) {
1655             sp_repr_css_set_property(css, "stroke-linecap", cap);
1657             sp_desktop_set_style (desktop, css);
1659             sp_stroke_style_set_cap_buttons(spw, GTK_WIDGET(tb));
1660         }
1662         sp_repr_css_attr_unref(css);
1664         sp_document_done(sp_desktop_document(desktop), SP_VERB_DIALOG_FILL_STROKE,
1665                          _("Set stroke style"));
1666     }
1670 /**
1671  * Updates the join style toggle buttons
1672  */
1673 static void
1674 sp_stroke_style_set_join_buttons(SPWidget *spw, GtkWidget *active)
1676     GtkWidget *tb;
1678     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1679                                          INKSCAPE_STOCK_JOIN_MITER) );
1680     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1682     GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1683     gtk_widget_set_sensitive(ml, (active == tb));
1685     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1686                                          INKSCAPE_STOCK_JOIN_ROUND) );
1687     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1688     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1689                                          INKSCAPE_STOCK_JOIN_BEVEL) );
1690     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1695 /**
1696  * Updates the cap style toggle buttons
1697  */
1698 static void
1699 sp_stroke_style_set_cap_buttons(SPWidget *spw, GtkWidget *active)
1701     GtkWidget *tb;
1703     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1704                                          INKSCAPE_STOCK_CAP_BUTT));
1705     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1706     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1707                                          INKSCAPE_STOCK_CAP_ROUND) );
1708     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1709     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1710                                          INKSCAPE_STOCK_CAP_SQUARE) );
1711     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1714 /**
1715  * Sets the current marker in the marker menu.
1716  */
1717 static void
1718 ink_marker_menu_set_current(SPObject *marker, GtkOptionMenu *mnu)
1720     gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(TRUE));
1722     GtkMenu *m = GTK_MENU(gtk_option_menu_get_menu(mnu));
1723     if (marker != NULL) {
1724         bool mark_is_stock = false;
1725         if (SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"))
1726             mark_is_stock = true;
1728         gchar *markname;
1729         if (mark_is_stock)
1730             markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"));
1731         else
1732             markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("id"));
1734         int markpos = 0;
1735         GList *kids = GTK_MENU_SHELL(m)->children;
1736         int i = 0;
1737         for (; kids != NULL; kids = kids->next) {
1738             gchar *mark = (gchar *) g_object_get_data(G_OBJECT(kids->data), "marker");
1739             if ( mark && strcmp(mark, markname) == 0 ) {
1740                 if ( mark_is_stock && !strcmp((gchar *) g_object_get_data(G_OBJECT(kids->data), "stockid"), "true"))
1741                     markpos = i;
1742                 if ( !mark_is_stock && !strcmp((gchar *) g_object_get_data(G_OBJECT(kids->data), "stockid"), "false"))
1743                     markpos = i;
1744             }
1745             i++;
1746         }
1747         gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), markpos);
1749         g_free (markname);
1750     }
1751     else {
1752         gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1753     }
1754     gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(FALSE));
1757 /**
1758  * Updates the marker menus to highlight the appropriate marker and scroll to 
1759  * that marker.
1760  */
1761 static void
1762 sp_stroke_style_update_marker_menus( SPWidget *spw,
1763                                      GSList const *objects)
1765     struct { char const *key; int loc; } const keyloc[] = {
1766         { "start_mark_menu", SP_MARKER_LOC_START },
1767         { "mid_mark_menu", SP_MARKER_LOC_MID },
1768         { "end_mark_menu", SP_MARKER_LOC_END }
1769     };
1771     bool all_texts = true;
1772     for (GSList *i = (GSList *) objects; i != NULL; i = i->next) {
1773         if (!SP_IS_TEXT (i->data)) {
1774             all_texts = false;
1775         }
1776     }
1778     for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1779         GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1780         if (all_texts) {
1781             // Per SVG spec, text objects cannot have markers; disable menus if only texts are selected
1782             gtk_widget_set_sensitive (GTK_WIDGET(mnu), FALSE);
1783         } else {
1784             gtk_widget_set_sensitive (GTK_WIDGET(mnu), TRUE);
1785         }
1786     }
1788     // We show markers of the first object in the list only
1789     // FIXME: use the first in the list that has the marker of each type, if any
1790     SPObject *object = SP_OBJECT(objects->data);
1792     for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1793         // For all three marker types,
1795         // find the corresponding menu
1796         GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1798         // Quit if we're in update state
1799         if (gtk_object_get_data(GTK_OBJECT(mnu), "update")) {
1800             return;
1801         }
1803         if (object->style->marker[keyloc[i].loc].value != NULL && !all_texts) {
1804             // If the object has this type of markers,
1806             // Extract the name of the marker that the object uses
1807             SPObject *marker = ink_extract_marker_name(object->style->marker[keyloc[i].loc].value);
1808             // Scroll the menu to that marker
1809             ink_marker_menu_set_current (marker, mnu);
1811         } else {
1812             gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1813         }
1814     }
1818 /**
1819  * Extract the actual name of the link
1820  * e.g. get mTriangle from url(#mTriangle).
1821  * \return Buffer containing the actual name, allocated from GLib;
1822  * the caller should free the buffer when they no longer need it.
1823  */
1824 static SPObject*
1825 ink_extract_marker_name(gchar const *n)
1827     gchar const *p = n;
1828     while (*p != '\0' && *p != '#') {
1829         p++;
1830     }
1832     if (*p == '\0' || p[1] == '\0') {
1833         return NULL;
1834     }
1836     p++;
1837     int c = 0;
1838     while (p[c] != '\0' && p[c] != ')') {
1839         c++;
1840     }
1842     if (p[c] == '\0') {
1843         return NULL;
1844     }
1846     gchar* b = g_strdup(p);
1847     b[c] = '\0';
1850     SPDesktop *desktop = inkscape_active_desktop();
1851     SPDocument *doc = sp_desktop_document(desktop);
1852     SPObject *marker = doc->getObjectById(b);
1853     return marker;
1857 /*
1858   Local Variables:
1859   mode:c++
1860   c-file-style:"stroustrup"
1861   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1862   indent-tabs-mode:nil
1863   fill-column:99
1864   End:
1865 */
1866 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :