Code

Renaming sp-marker.* to marker.*
[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.com>
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.
550  */
551 static GtkWidget *
552 sp_marker_prev_new(unsigned size, gchar const *mname,
553                    SPDocument *source, SPDocument *sandbox,
554                    gchar *menu_id, NRArena const *arena, unsigned visionkey, NRArenaItem *root)
556     // Retrieve the marker named 'mname' from the source SVG document
557     SPObject const *marker = source->getObjectById(mname);
558     if (marker == NULL)
559         return NULL;
561     // Create a copy repr of the marker with id="sample"
562     Inkscape::XML::Node *mrepr = SP_OBJECT_REPR (marker)->duplicate();
563     mrepr->setAttribute("id", "sample");
565     // Replace the old sample in the sandbox by the new one
566     Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (sandbox->getObjectById("defs"));
567     SPObject *oldmarker = sandbox->getObjectById("sample");
568     if (oldmarker)
569         oldmarker->deleteObject(false);
570     defsrepr->appendChild(mrepr);
571     Inkscape::GC::release(mrepr);
573 // Uncomment this to get the sandbox documents saved (useful for debugging)
574     //FILE *fp = fopen (g_strconcat(mname, ".svg", NULL), "w");
575     //sp_repr_save_stream (sp_document_repr_doc (sandbox), fp);
576     //fclose (fp);
578     // object to render; note that the id is the same as that of the menu we're building
579     SPObject *object = sandbox->getObjectById(menu_id);
580     sp_document_root (sandbox)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
581     sp_document_ensure_up_to_date(sandbox);
583     if (object == NULL || !SP_IS_ITEM(object))
584         return NULL; // sandbox broken?
586     // Find object's bbox in document
587     NR::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
589     NR::Rect const dbox = SP_ITEM(object)->invokeBbox(i2doc);
591     if (dbox.isEmpty()) {
592         return NULL;
593     }
595     /* Update to renderable state */
596     NRMatrix t;
597     double sf = 0.8;
598     nr_matrix_set_scale(&t, sf, sf);
599     nr_arena_item_set_transform(root, &t);
600     NRGC gc(NULL);
601     nr_matrix_set_identity(&gc.transform);
602     nr_arena_item_invoke_update( root, NULL, &gc,
603                                  NR_ARENA_ITEM_STATE_ALL,
604                                  NR_ARENA_ITEM_STATE_NONE );
606     /* Item integer bbox in points */
607     NRRectL ibox;
608     ibox.x0 = (int) floor(sf * dbox.min()[NR::X] + 0.5);
609     ibox.y0 = (int) floor(sf * dbox.min()[NR::Y] + 0.5);
610     ibox.x1 = (int) floor(sf * dbox.max()[NR::X] + 0.5);
611     ibox.y1 = (int) floor(sf * dbox.max()[NR::Y] + 0.5);
613     /* Find visible area */
614     int width = ibox.x1 - ibox.x0;
615     int height = ibox.y1 - ibox.y0;
616     int dx = size;
617     int dy = size;
618     dx=(dx - width)/2; // watch out for size, since 'unsigned'-'signed' can cause problems if the result is negative
619     dy=(dy - height)/2;
621     NRRectL area;
622     area.x0 = ibox.x0 - dx;
623     area.y0 = ibox.y0 - dy;
624     area.x1 = area.x0 + size;
625     area.y1 = area.y0 + size;
627     /* Actual renderable area */
628     NRRectL ua;
629     ua.x0 = MAX(ibox.x0, area.x0);
630     ua.y0 = MAX(ibox.y0, area.y0);
631     ua.x1 = MIN(ibox.x1, area.x1);
632     ua.y1 = MIN(ibox.y1, area.y1);
634     /* Set up pixblock */
635     guchar *px = g_new(guchar, 4 * size * size);
636     memset(px, 0x00, 4 * size * size);
638     /* Render */
639     NRPixBlock B;
640     nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
641                               ua.x0, ua.y0, ua.x1, ua.y1,
642                               px + 4 * size * (ua.y0 - area.y0) +
643                               4 * (ua.x0 - area.x0),
644                               4 * size, FALSE, FALSE );
645     nr_arena_item_invoke_render( root, &ua, &B,
646                                  NR_ARENA_ITEM_RENDER_NO_CACHE );
647     nr_pixblock_release(&B);
649     // Create widget
650     GtkWidget *pb = gtk_image_new_from_pixbuf(gdk_pixbuf_new_from_data(px,
651                               GDK_COLORSPACE_RGB,
652                               TRUE,
653                               8, size, size, size * 4,
654                               (GdkPixbufDestroyNotify)g_free,
655                               NULL));
656     return pb;
660 #define MARKER_ITEM_MARGIN 0
663 /**
664  * sp_marker_list_from_doc()
665  *
666  * \brief Pick up all markers from source, except those that are in
667  * current_doc (if non-NULL), and add items to the m menu
668  *
669  */
670 static void
671 sp_marker_list_from_doc (GtkWidget *m, SPDocument *current_doc, SPDocument *source, SPDocument *markers_doc, SPDocument *sandbox, gchar *menu_id)
674     // search through defs
675     GSList *ml = NULL;
676     SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS (source);
677     for ( SPObject *ochild = sp_object_first_child(SP_OBJECT(defs)) ; ochild != NULL ; ochild = SP_OBJECT_NEXT (ochild) ) {
678         if (SP_IS_MARKER(ochild)) {
679             ml = g_slist_prepend (ml, ochild);
680         }
681     }
683     // Do this here, outside of loop, to speed up preview generation:
684     /* Create new arena */
685     NRArena const *arena = NRArena::create();
686     /* Create ArenaItem and set transform */
687     unsigned const visionkey = sp_item_display_key_new(1);
688     NRArenaItem *root =  sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT (sandbox)), (NRArena *) arena, visionkey, SP_ITEM_SHOW_DISPLAY );
690     for (; ml != NULL; ml = ml->next) {
692         if (!SP_IS_MARKER(ml->data))
693             continue;
695         Inkscape::XML::Node *repr = SP_OBJECT_REPR((SPItem *) ml->data);
697         bool stock_dupe = false;
699         if (markers_doc && repr->attribute("inkscape:stockid")) {
700             // find out if markers_doc has a marker with the same stockid, and if so, skip this
701             for (SPObject *child = sp_object_first_child(SP_OBJECT(SP_DOCUMENT_DEFS(markers_doc))) ;
702                  child != NULL;
703                  child = SP_OBJECT_NEXT(child) )
704             {
705                 if (SP_IS_MARKER(child) &&
706                     SP_OBJECT_REPR(child)->attribute("inkscape:stockid") &&
707                     !strcmp(repr->attribute("inkscape:stockid"), SP_OBJECT_REPR(child)->attribute("inkscape:stockid"))) {
708                     stock_dupe = true; 
709                 }
710             }
711         }
713         if (stock_dupe) // stock item, dont add to list from current doc
714             continue;
716         GtkWidget *i = gtk_menu_item_new();
717         gtk_widget_show(i);
719         if (repr->attribute("inkscape:stockid"))
720             g_object_set_data (G_OBJECT(i), "stockid", (void *) "true");
721         else
722             g_object_set_data (G_OBJECT(i), "stockid", (void *) "false");
724         gchar const *markid = repr->attribute("id");
725         g_object_set_data (G_OBJECT(i), "marker", (void *) markid);
727         GtkWidget *hb = gtk_hbox_new(FALSE, MARKER_ITEM_MARGIN);
728         gtk_widget_show(hb);
730         // generate preview
731         GtkWidget *prv = sp_marker_prev_new (22, markid, source, sandbox, menu_id, arena, visionkey, root);
732         gtk_widget_show(prv);
733         gtk_box_pack_start(GTK_BOX(hb), prv, FALSE, FALSE, 6);
735         // create label
736         GtkWidget *l = gtk_label_new(repr->attribute("id"));
737         gtk_widget_show(l);
738         gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
740         gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
742         gtk_widget_show(hb);
743         gtk_container_add(GTK_CONTAINER(i), hb);
745         gtk_menu_append(GTK_MENU(m), i);
746     }
748     g_slist_free (ml);
752 /**
753  * Returns a new document containing default start, mid, and end markers.
754  */
755 SPDocument *
756 ink_markers_preview_doc ()
758 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\">"
759 "  <defs id=\"defs\" />"
761 "  <g id=\"marker-start\">"
762 "    <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:url(#sample);marker-mid:none;marker-end:none\""
763 "       d=\"M 12.5,13 L 25,13\" id=\"path1\" />"
764 "    <rect style=\"fill:none;stroke:none\" id=\"rect2\""
765 "       width=\"25\" height=\"25\" x=\"0\" y=\"0\" />"
766 "  </g>"
768 "  <g id=\"marker-mid\">"
769 "    <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:url(#sample);marker-end:none\""
770 "       d=\"M 0,113 L 12.5,113 L 25,113\" id=\"path11\" />"
771 "    <rect style=\"fill:none;stroke:none\" id=\"rect22\""
772 "       width=\"25\" height=\"25\" x=\"0\" y=\"100\" />"
773 "  </g>"
775 "  <g id=\"marker-end\">"
776 "    <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:none;marker-end:url(#sample)\""
777 "       d=\"M 0,213 L 12.5,213\" id=\"path111\" />"
778 "    <rect style=\"fill:none;stroke:none\" id=\"rect222\""
779 "       width=\"25\" height=\"25\" x=\"0\" y=\"200\" />"
780 "  </g>"
782 "</svg>";
784     return sp_document_new_from_mem (buffer, strlen(buffer), FALSE);
788 /**
789  * Creates a menu widget to display markers from markers.svg
790  */
791 static GtkWidget *
792 ink_marker_menu( GtkWidget *tbl, gchar *menu_id, SPDocument *sandbox)
794     SPDesktop *desktop = inkscape_active_desktop();
795     SPDocument *doc = sp_desktop_document(desktop);
796     GtkWidget *mnu = gtk_option_menu_new();
798     /* Create new menu widget */
799     GtkWidget *m = gtk_menu_new();
800     gtk_widget_show(m);
802     g_object_set_data(G_OBJECT(mnu), "updating", (gpointer) FALSE);
804     if (!doc) {
805         GtkWidget *i = gtk_menu_item_new_with_label(_("No document selected"));
806         gtk_widget_show(i);
807         gtk_menu_append(GTK_MENU(m), i);
808         gtk_widget_set_sensitive(mnu, FALSE);
810     } else {
812         // add "None"
813         {
814             GtkWidget *i = gtk_menu_item_new();
815             gtk_widget_show(i);
817             g_object_set_data(G_OBJECT(i), "marker", (void *) "none");
819             GtkWidget *hb = gtk_hbox_new(FALSE,  MARKER_ITEM_MARGIN);
820             gtk_widget_show(hb);
822             GtkWidget *l = gtk_label_new( _("None") );
823             gtk_widget_show(l);
824             gtk_misc_set_alignment(GTK_MISC(l), 0.0, 0.5);
826             gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
828             gtk_widget_show(hb);
829             gtk_container_add(GTK_CONTAINER(i), hb);
830             gtk_menu_append(GTK_MENU(m), i);
831         }
833         // find and load  markers.svg
834         static SPDocument *markers_doc = NULL;
835         char *markers_source = g_build_filename(INKSCAPE_MARKERSDIR, "markers.svg", NULL);
836         if (Inkscape::IO::file_test(markers_source, G_FILE_TEST_IS_REGULAR)) {
837             markers_doc = sp_document_new(markers_source, FALSE);
838         }
839         g_free(markers_source);
841         // suck in from current doc
842         sp_marker_list_from_doc ( m, NULL, doc, markers_doc, sandbox, menu_id );
844         // add separator
845         {
846             GtkWidget *i = gtk_separator_menu_item_new();
847             gtk_widget_show(i);
848             gtk_menu_append(GTK_MENU(m), i);
849         }
851         // suck in from markers.svg
852         if (markers_doc) {
853             sp_document_ensure_up_to_date(doc);
854             sp_marker_list_from_doc ( m, doc, markers_doc, NULL, sandbox, menu_id );
855         }
857         gtk_widget_set_sensitive(mnu, TRUE);
858     }
860     gtk_object_set_data(GTK_OBJECT(mnu), "menu_id", menu_id);
861     gtk_option_menu_set_menu(GTK_OPTION_MENU(mnu), m);
863     /* Set history */
864     gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
866     return mnu;
870 /**
871  * Handles when user selects one of the markers from the marker menu.
872  * Defines a uri string to refer to it, then applies it to all selected
873  * items in the current desktop.
874  */
875 static void
876 sp_marker_select(GtkOptionMenu *mnu, GtkWidget *spw)
878     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
879         return;
880     }
882     SPDesktop *desktop = inkscape_active_desktop();
883     SPDocument *document = sp_desktop_document(desktop);
884     if (!document) {
885         return;
886     }
888     /* Get Marker */
889     if (!g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
890                            "marker"))
891     {
892         return;
893     }
894     gchar *markid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
895                                                 "marker");
896     gchar *marker = "";
897     if (strcmp(markid, "none")){
898        gchar *stockid = (gchar *) g_object_get_data(G_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(mnu)))),
899                                                 "stockid");
901        gchar *markurn = markid;
902        if (!strcmp(stockid,"true")) markurn = g_strconcat("urn:inkscape:marker:",markid,NULL);
903        SPObject *mark = get_stock_item(markurn);
904        if (mark) {
905             Inkscape::XML::Node *repr = SP_OBJECT_REPR(mark);
906             marker = g_strconcat("url(#", repr->attribute("id"), ")", NULL);
907         }
908     } else {
909         marker = markid;
910     }
912     SPCSSAttr *css = sp_repr_css_attr_new();
913     gchar *menu_id = (gchar *) g_object_get_data(G_OBJECT(mnu), "menu_id");
914     sp_repr_css_set_property(css, menu_id, marker);
916      Inkscape::Selection *selection = sp_desktop_selection(desktop);
917      GSList const *items = selection->itemList();
918      for (; items != NULL; items = items->next) {
919          SPItem *item = (SPItem *) items->data;
920          if (!SP_IS_SHAPE(item) || SP_IS_RECT(item)) // can't set marker to rect, until it's converted to using <path>
921              continue;
922          Inkscape::XML::Node *selrepr = SP_OBJECT_REPR((SPItem *) items->data);
923          if (selrepr) {
924              sp_repr_css_change_recursive(selrepr, css, "style");
925          }
926          SP_OBJECT(items->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
927          SP_OBJECT(items->data)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
928      }
930     sp_repr_css_attr_unref(css);
932     sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE, 
933                      _("Set markers"));
936 /**
937  * Sets the stroke width units for all selected items.
938  * Also handles absolute and dimensionless units.
939  */
940 static gboolean stroke_width_set_unit(SPUnitSelector *,
941                                                  SPUnit const *old,
942                                                  SPUnit const *new_units,
943                                                  GObject *spw)
945     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
947     if (!desktop) {
948         return FALSE;
949     }
951     Inkscape::Selection *selection = sp_desktop_selection (desktop);
953     if (selection->isEmpty())
954         return FALSE;
956     GSList const *objects = selection->itemList();
958     if ((old->base == SP_UNIT_ABSOLUTE || old->base == SP_UNIT_DEVICE) &&
959        (new_units->base == SP_UNIT_DIMENSIONLESS)) {
961         /* Absolute to percentage */
962         g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
964         GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
965         float w = sp_units_get_pixels (a->value, *old);
967         gdouble average = stroke_average_width (objects);
969         if (average == NR_HUGE || average == 0)
970             return FALSE;
972         gtk_adjustment_set_value (a, 100.0 * w / average);
974         g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
975         return TRUE;
977     } else if ((old->base == SP_UNIT_DIMENSIONLESS) &&
978               (new_units->base == SP_UNIT_ABSOLUTE || new_units->base == SP_UNIT_DEVICE)) {
980         /* Percentage to absolute */
981         g_object_set_data (spw, "update", GUINT_TO_POINTER (TRUE));
983         GtkAdjustment *a = GTK_ADJUSTMENT(g_object_get_data (spw, "width"));
985         gdouble average = stroke_average_width (objects);
987         gtk_adjustment_set_value (a, sp_pixels_get_units (0.01 * a->value * average, *new_units));
989         g_object_set_data (spw, "update", GUINT_TO_POINTER (FALSE));
990         return TRUE;
991     }
993     return FALSE;
997 /**
998  * \brief  Creates a new widget for the line stroke style.
999  *
1000  */
1001 GtkWidget *
1002 sp_stroke_style_line_widget_new(void)
1004     GtkWidget *spw, *f, *t, *hb, *sb, *us, *tb, *ds;
1005     GtkObject *a;
1007     GtkTooltips *tt = gtk_tooltips_new();
1009     spw = sp_widget_new_global(INKSCAPE);
1011     f = gtk_hbox_new (FALSE, 0);
1012     gtk_widget_show(f);
1013     gtk_container_add(GTK_CONTAINER(spw), f);
1015     t = gtk_table_new(3, 6, FALSE);
1016     gtk_widget_show(t);
1017     gtk_container_set_border_width(GTK_CONTAINER(t), 4);
1018     gtk_table_set_row_spacings(GTK_TABLE(t), 4);
1019     gtk_container_add(GTK_CONTAINER(f), t);
1020     gtk_object_set_data(GTK_OBJECT(spw), "stroke", t);
1022     gint i = 0;
1024     /* Stroke width */
1025     spw_label(t, _("Width:"), 0, i);
1027     hb = spw_hbox(t, 3, 1, i);
1029 // TODO: when this is gtkmmified, use an Inkscape::UI::Widget::ScalarUnit instead of the separate
1030 // spinbutton and unit selector for stroke width. In sp_stroke_style_line_update, use
1031 // setHundredPercent to remember the aeraged width corresponding to 100%. Then the
1032 // stroke_width_set_unit will be removed (because ScalarUnit takes care of conversions itself), and
1033 // with it, the two remaining calls of stroke_average_width, allowing us to get rid of that
1034 // function in desktop-style.
1036     a = gtk_adjustment_new(1.0, 0.0, 1000.0, 0.1, 10.0, 10.0);
1037     gtk_object_set_data(GTK_OBJECT(spw), "width", a);
1038     sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 3);
1039     gtk_tooltips_set_tip(tt, sb, _("Stroke width"), NULL);
1040     gtk_widget_show(sb);
1042     sp_dialog_defocus_on_enter(sb);
1044     gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1045     us = sp_unit_selector_new(SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE);
1046     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1047     if (desktop)
1048         sp_unit_selector_set_unit (SP_UNIT_SELECTOR(us), sp_desktop_namedview(desktop)->doc_units);
1049     sp_unit_selector_add_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT), 0);
1050     g_signal_connect ( G_OBJECT (us), "set_unit", G_CALLBACK (stroke_width_set_unit), spw );
1051     gtk_widget_show(us);
1052     sp_unit_selector_add_adjustment( SP_UNIT_SELECTOR(us), GTK_ADJUSTMENT(a) );
1053     gtk_box_pack_start(GTK_BOX(hb), us, FALSE, FALSE, 0);
1054     gtk_object_set_data(GTK_OBJECT(spw), "units", us);
1056     gtk_signal_connect( GTK_OBJECT(a), "value_changed", GTK_SIGNAL_FUNC(sp_stroke_style_width_changed), spw );
1057     i++;
1059     /* Join type */
1060     // TRANSLATORS: The line join style specifies the shape to be used at the
1061     //  corners of paths. It can be "miter", "round" or "bevel".
1062     spw_label(t, _("Join:"), 0, i);
1064     hb = spw_hbox(t, 3, 1, i);
1066     tb = NULL;
1068     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_MITER,
1069                                 hb, spw, "join", "miter");
1071     // TRANSLATORS: Miter join: joining lines with a sharp (pointed) corner.
1072     //  For an example, draw a triangle with a large stroke width and modify the
1073     //  "Join" option (in the Fill and Stroke dialog).
1074     gtk_tooltips_set_tip(tt, tb, _("Miter join"), NULL);
1076     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_ROUND,
1077                                 hb, spw, "join", "round");
1079     // TRANSLATORS: Round join: joining lines with a rounded corner.
1080     //  For an example, draw a triangle with a large stroke width and modify the
1081     //  "Join" option (in the Fill and Stroke dialog).
1082     gtk_tooltips_set_tip(tt, tb, _("Round join"), NULL);
1084     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_JOIN_BEVEL,
1085                                 hb, spw, "join", "bevel");
1087     // TRANSLATORS: Bevel join: joining lines with a blunted (flattened) corner.
1088     //  For an example, draw a triangle with a large stroke width and modify the
1089     //  "Join" option (in the Fill and Stroke dialog).
1090     gtk_tooltips_set_tip(tt, tb, _("Bevel join"), NULL);
1092     i++;
1094     /* Miterlimit  */
1095     // TRANSLATORS: Miter limit: only for "miter join", this limits the length
1096     //  of the sharp "spike" when the lines connect at too sharp an angle.
1097     // When two line segments meet at a sharp angle, a miter join results in a
1098     //  spike that extends well beyond the connection point. The purpose of the
1099     //  miter limit is to cut off such spikes (i.e. convert them into bevels)
1100     //  when they become too long.
1101     spw_label(t, _("Miter limit:"), 0, i);
1103     hb = spw_hbox(t, 3, 1, i);
1105     a = gtk_adjustment_new(4.0, 0.0, 100.0, 0.1, 10.0, 10.0);
1106     gtk_object_set_data(GTK_OBJECT(spw), "miterlimit", a);
1108     sb = gtk_spin_button_new(GTK_ADJUSTMENT(a), 0.1, 2);
1109     gtk_tooltips_set_tip(tt, sb, _("Maximum length of the miter (in units of stroke width)"), NULL);
1110     gtk_widget_show(sb);
1111     gtk_object_set_data(GTK_OBJECT(spw), "miterlimit_sb", sb);
1112     sp_dialog_defocus_on_enter(sb);
1114     gtk_box_pack_start(GTK_BOX(hb), sb, FALSE, FALSE, 0);
1116     gtk_signal_connect( GTK_OBJECT(a), "value_changed",
1117                         GTK_SIGNAL_FUNC(sp_stroke_style_miterlimit_changed), spw );
1118     i++;
1120     /* Cap type */
1121     // TRANSLATORS: cap type specifies the shape for the ends of lines
1122     spw_label(t, _("Cap:"), 0, i);
1124     hb = spw_hbox(t, 3, 1, i);
1126     tb = NULL;
1128     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_BUTT,
1129                                 hb, spw, "cap", "butt");
1131     // TRANSLATORS: Butt cap: the line shape does not extend beyond the end point
1132     //  of the line; the ends of the line are square
1133     gtk_tooltips_set_tip(tt, tb, _("Butt cap"), NULL);
1135     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_ROUND,
1136                                 hb, spw, "cap", "round");
1138     // TRANSLATORS: Round cap: the line shape extends beyond the end point of the
1139     //  line; the ends of the line are rounded
1140     gtk_tooltips_set_tip(tt, tb, _("Round cap"), NULL);
1142     tb = sp_stroke_radio_button(tb, INKSCAPE_STOCK_CAP_SQUARE,
1143                                 hb, spw, "cap", "square");
1145     // TRANSLATORS: Square cap: the line shape extends beyond the end point of the
1146     //  line; the ends of the line are square
1147     gtk_tooltips_set_tip(tt, tb, _("Square cap"), NULL);
1149     i++;
1152     /* Dash */
1153     spw_label(t, _("Dashes:"), 0, i);
1154     ds = sp_dash_selector_new( inkscape_get_repr( INKSCAPE,
1155                                                   "palette.dashes") );
1157     gtk_widget_show(ds);
1158     gtk_table_attach( GTK_TABLE(t), ds, 1, 4, i, i+1,
1159                       (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1160                       (GtkAttachOptions)0, 0, 0 );
1161     gtk_object_set_data(GTK_OBJECT(spw), "dash", ds);
1162     gtk_signal_connect( GTK_OBJECT(ds), "changed",
1163                         GTK_SIGNAL_FUNC(sp_stroke_style_line_dash_changed),
1164                         spw );
1165     i++;
1167     /* Drop down marker selectors*/
1169     // doing this here once, instead of for each preview, to speed things up
1170     SPDocument *sandbox = ink_markers_preview_doc ();
1172     // TRANSLATORS: Path markers are an SVG feature that allows you to attach arbitrary shapes
1173     // (arrowheads, bullets, faces, whatever) to the start, end, or middle nodes of a path.
1174     spw_label(t, _("Start Markers:"), 0, i);
1175     GtkWidget *mnu  = ink_marker_menu( spw ,"marker-start", sandbox);
1176     gtk_signal_connect( GTK_OBJECT(mnu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1177     gtk_widget_show(mnu);
1178     gtk_table_attach( GTK_TABLE(t), mnu, 1, 4, i, i+1,
1179                       (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1180                       (GtkAttachOptions)0, 0, 0 );
1181     gtk_object_set_data(GTK_OBJECT(spw), "start_mark_menu", mnu);
1183     i++;
1184     spw_label(t, _("Mid Markers:"), 0, i);
1185     mnu = NULL;
1186     mnu  = ink_marker_menu( spw ,"marker-mid", sandbox);
1187     gtk_signal_connect( GTK_OBJECT(mnu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1188     gtk_widget_show(mnu);
1189     gtk_table_attach( GTK_TABLE(t), mnu, 1, 4, i, i+1,
1190                       (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1191                       (GtkAttachOptions)0, 0, 0 );
1192     gtk_object_set_data(GTK_OBJECT(spw), "mid_mark_menu", mnu);
1194     i++;
1195     spw_label(t, _("End Markers:"), 0, i);
1196     mnu = NULL;
1197     mnu  = ink_marker_menu( spw ,"marker-end", sandbox);
1198     gtk_signal_connect( GTK_OBJECT(mnu), "changed", GTK_SIGNAL_FUNC(sp_marker_select), spw );
1199     gtk_widget_show(mnu);
1200     gtk_table_attach( GTK_TABLE(t), mnu, 1, 4, i, i+1,
1201                       (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1202                       (GtkAttachOptions)0, 0, 0 );
1203     gtk_object_set_data(GTK_OBJECT(spw), "end_mark_menu", mnu);
1205     i++;
1207     gtk_signal_connect( GTK_OBJECT(spw), "construct",
1208                         GTK_SIGNAL_FUNC(sp_stroke_style_line_construct),
1209                         NULL );
1210     gtk_signal_connect( GTK_OBJECT(spw), "modify_selection",
1211                         GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_modified),
1212                         NULL );
1213     gtk_signal_connect( GTK_OBJECT(spw), "change_selection",
1214                         GTK_SIGNAL_FUNC(sp_stroke_style_line_selection_changed),
1215                         NULL );
1217     sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
1219     return spw;
1223 /**
1224  * Callback for when the stroke style widget is called.  It causes
1225  * the stroke line style to be updated.
1226  */
1227 static void
1228 sp_stroke_style_line_construct(SPWidget *spw, gpointer data)
1231 #ifdef SP_SS_VERBOSE
1232     g_print( "Stroke style widget constructed: inkscape %p repr %p\n",
1233              spw->inkscape, spw->repr );
1234 #endif
1235     if (spw->inkscape) {
1236         sp_stroke_style_line_update(spw,
1237                                     ( SP_ACTIVE_DESKTOP
1238                                       ? sp_desktop_selection(SP_ACTIVE_DESKTOP)
1239                                       : NULL ));
1240     } 
1243 /**
1244  * Callback for when stroke style widget is modified.  
1245  * Triggers update action.
1246  */
1247 static void
1248 sp_stroke_style_line_selection_modified ( SPWidget *spw,
1249                                        Inkscape::Selection *selection,
1250                                        guint flags,
1251                                        gpointer data )
1253     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1254         sp_stroke_style_line_update (spw, selection);
1255     }
1259 /**
1260  * Callback for when stroke style widget is changed.
1261  * Triggers update action.
1262  */
1263 static void
1264 sp_stroke_style_line_selection_changed ( SPWidget *spw,
1265                                        Inkscape::Selection *selection,
1266                                        gpointer data )
1268     sp_stroke_style_line_update (spw, selection);
1272 /**
1273  * Sets selector widgets' dash style from an SPStyle object.
1274  */
1275 static void
1276 sp_dash_selector_set_from_style (GtkWidget *dsel, SPStyle *style)
1278     if (style->stroke_dash.n_dash > 0) {
1279         double d[64];
1280         int len = MIN(style->stroke_dash.n_dash, 64);
1281         for (int i = 0; i < len; i++) {
1282             if (style->stroke_width.computed != 0)
1283                 d[i] = style->stroke_dash.dash[i] / style->stroke_width.computed;
1284             else
1285                 d[i] = style->stroke_dash.dash[i]; // is there a better thing to do for stroke_width==0?
1286         }
1287         sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), len, d,
1288                style->stroke_width.computed != 0?
1289                     style->stroke_dash.offset / style->stroke_width.computed  :
1290                     style->stroke_dash.offset);
1291     } else {
1292         sp_dash_selector_set_dash(SP_DASH_SELECTOR(dsel), 0, NULL, 0.0);
1293     }
1296 /**
1297  * Sets the join type for a line, and updates the stroke style widget's buttons
1298  */
1299 static void
1300 sp_jointype_set (SPWidget *spw, unsigned const jointype)
1302     GtkWidget *tb = NULL;
1303     switch (jointype) {
1304         case SP_STROKE_LINEJOIN_MITER:
1305             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_MITER));
1306             break;
1307         case SP_STROKE_LINEJOIN_ROUND:
1308             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_ROUND));
1309             break;
1310         case SP_STROKE_LINEJOIN_BEVEL:
1311             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_JOIN_BEVEL));
1312             break;
1313         default:
1314             break;
1315     }
1316     sp_stroke_style_set_join_buttons (spw, tb);
1319 /**
1320  * Sets the cap type for a line, and updates the stroke style widget's buttons
1321  */
1322 static void
1323 sp_captype_set (SPWidget *spw, unsigned const captype)
1325     GtkWidget *tb = NULL;
1326     switch (captype) {
1327         case SP_STROKE_LINECAP_BUTT:
1328             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_BUTT));
1329             break;
1330         case SP_STROKE_LINECAP_ROUND:
1331             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_ROUND));
1332             break;
1333         case SP_STROKE_LINECAP_SQUARE:
1334             tb = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), INKSCAPE_STOCK_CAP_SQUARE));
1335             break;
1336         default:
1337             break;
1338     }
1339     sp_stroke_style_set_cap_buttons (spw, tb);
1342 /**
1343  * Callback for when stroke style widget is updated, including markers, cap type,
1344  * join type, etc.
1345  */
1346 static void
1347 sp_stroke_style_line_update(SPWidget *spw, Inkscape::Selection *sel)
1349     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1350         return;
1351     }
1353     gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1355     GtkWidget *sset = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "stroke"));
1356     GtkObject *width = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1357     GtkObject *ml = GTK_OBJECT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1358     GtkWidget *us = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1359     GtkWidget *dsel = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1361     // create temporary style
1362     SPStyle *query = sp_style_new ();
1363     // query into it
1364     int result_sw = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEWIDTH); 
1365     int result_ml = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT); 
1366     int result_cap = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKECAP); 
1367     int result_join = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEJOIN); 
1369     if (result_sw == QUERY_STYLE_NOTHING) {
1370         /* No objects stroked, set insensitive */
1371         gtk_widget_set_sensitive(sset, FALSE);
1373         gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1374         return;
1375     } else {
1376         gtk_widget_set_sensitive(sset, TRUE);
1378         SPUnit const *unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1380         if (result_sw == QUERY_STYLE_MULTIPLE_AVERAGED) {
1381             sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), &sp_unit_get_by_id(SP_UNIT_PERCENT));
1382         } else {
1383             // same width, or only one object; no sense to keep percent, switch to absolute
1384             if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1385                 sp_unit_selector_set_unit(SP_UNIT_SELECTOR(us), sp_desktop_namedview(SP_ACTIVE_DESKTOP)->doc_units);
1386             }
1387         }
1389         unit = sp_unit_selector_get_unit (SP_UNIT_SELECTOR (us));
1391         if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1392             double avgwidth = sp_pixels_get_units (query->stroke_width.computed, *unit);
1393             gtk_adjustment_set_value(GTK_ADJUSTMENT(width), avgwidth);
1394         } else {
1395             gtk_adjustment_set_value(GTK_ADJUSTMENT(width), 100);
1396         }
1397     }
1399     if (result_ml != QUERY_STYLE_NOTHING)
1400         gtk_adjustment_set_value(GTK_ADJUSTMENT(ml), query->stroke_miterlimit.value); // TODO: reflect averagedness?
1402     if (result_join != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1403         sp_jointype_set (spw, query->stroke_linejoin.value);
1404     } else {
1405         sp_stroke_style_set_join_buttons(spw, NULL);
1406     }
1408     if (result_cap != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1409         sp_captype_set (spw, query->stroke_linecap.value);
1410     } else {
1411         sp_stroke_style_set_cap_buttons(spw, NULL);
1412     }
1414     g_free (query);
1416     GSList const *objects = sel->itemList();
1417     SPObject * const object = SP_OBJECT(objects->data);
1418     SPStyle * const style = SP_OBJECT_STYLE(object);
1420     /* Markers */
1421     sp_stroke_style_update_marker_menus(spw, objects); // FIXME: make this desktop query too
1423     /* Dash */
1424     sp_dash_selector_set_from_style (dsel, style); // FIXME: make this desktop query too
1426     gtk_widget_set_sensitive(sset, TRUE);
1428     gtk_object_set_data(GTK_OBJECT(spw), "update",
1429                         GINT_TO_POINTER(FALSE));
1432 /**
1433  * Sets a line's dash properties in a CSS style object.
1434  */
1435 static void
1436 sp_stroke_style_set_scaled_dash(SPCSSAttr *css,
1437                                 int ndash, double *dash, double offset,
1438                                 double scale)
1440     if (ndash > 0) {
1441         Inkscape::CSSOStringStream osarray;
1442         for (int i = 0; i < ndash; i++) {
1443             osarray << dash[i] * scale;
1444             if (i < (ndash - 1)) {
1445                 osarray << ",";
1446             }
1447         }
1448         sp_repr_css_set_property(css, "stroke-dasharray", osarray.str().c_str());
1450         Inkscape::CSSOStringStream osoffset;
1451         osoffset << offset * scale;
1452         sp_repr_css_set_property(css, "stroke-dashoffset", osoffset.str().c_str());
1453     } else {
1454         sp_repr_css_set_property(css, "stroke-dasharray", "none");
1455         sp_repr_css_set_property(css, "stroke-dashoffset", NULL);
1456     }
1459 /**
1460  * Sets line properties like width, dashes, markers, etc. on all currently selected items.
1461  */
1462 static void
1463 sp_stroke_style_scale_line(SPWidget *spw)
1465     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1466         return;
1467     }
1469     gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(TRUE));
1471     GtkAdjustment *wadj = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "width"));
1472     SPUnitSelector *us = SP_UNIT_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "units"));
1473     SPDashSelector *dsel = SP_DASH_SELECTOR(gtk_object_get_data(GTK_OBJECT(spw), "dash"));
1474     GtkAdjustment *ml = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(spw), "miterlimit"));
1476     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1477     SPDocument *document = sp_desktop_document (desktop);
1478     Inkscape::Selection *selection = sp_desktop_selection (desktop);
1480     GSList const *items = selection->itemList();
1482     /* TODO: Create some standardized method */
1483     SPCSSAttr *css = sp_repr_css_attr_new();
1485     if (items) {
1487         double width_typed = wadj->value;
1488         double const miterlimit = ml->value;
1490         SPUnit const *const unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1492         double *dash, offset;
1493         int ndash;
1494         sp_dash_selector_get_dash(dsel, &ndash, &dash, &offset);
1496         for (GSList const *i = items; i != NULL; i = i->next) {
1497             /* Set stroke width */
1498             double width;
1499             if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1500                 width = sp_units_get_pixels (width_typed, *unit);
1501             } else { // percentage
1502                 gdouble old_w = SP_OBJECT_STYLE (i->data)->stroke_width.computed;
1503                 width = old_w * width_typed / 100;
1504             }
1506             {
1507                 Inkscape::CSSOStringStream os_width;
1508                 os_width << width;
1509                 sp_repr_css_set_property(css, "stroke-width", os_width.str().c_str());
1510             }
1512             {
1513                 Inkscape::CSSOStringStream os_ml;
1514                 os_ml << miterlimit;
1515                 sp_repr_css_set_property(css, "stroke-miterlimit", os_ml.str().c_str());
1516             }
1518             /* Set dash */
1519             sp_stroke_style_set_scaled_dash(css, ndash, dash, offset, width);
1521             sp_desktop_apply_css_recursive (SP_OBJECT(i->data), css, true);
1522         }
1524         g_free(dash);
1526         if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1527             // reset to 100 percent
1528             gtk_adjustment_set_value (wadj, 100.0);
1529         }
1531     }
1533     // we have already changed the items, so set style without changing selection
1534     // FIXME: move the above stroke-setting stuff, including percentages, to desktop-style
1535     sp_desktop_set_style (desktop, css, false);
1537     sp_repr_css_attr_unref(css);
1539     sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE, 
1540                      _("Set stroke style"));
1542     gtk_object_set_data(GTK_OBJECT(spw), "update", GINT_TO_POINTER(FALSE));
1546 /**
1547  * Callback for when the stroke style's width changes.  
1548  * Causes all line styles to be applied to all selected items.
1549  */
1550 static void
1551 sp_stroke_style_width_changed(GtkAdjustment *adj, SPWidget *spw)
1553     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1554         return;
1555     }
1557     sp_stroke_style_scale_line(spw);
1560 /**
1561  * Callback for when the stroke style's miterlimit changes.  
1562  * Causes all line styles to be applied to all selected items.
1563  */
1564 static void
1565 sp_stroke_style_miterlimit_changed(GtkAdjustment *adj, SPWidget *spw)
1567     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1568         return;
1569     }
1571     sp_stroke_style_scale_line(spw);
1574 /**
1575  * Callback for when the stroke style's dash changes.  
1576  * Causes all line styles to be applied to all selected items.
1577  */
1578 static void
1579 sp_stroke_style_line_dash_changed(SPDashSelector *dsel, SPWidget *spw)
1581     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1582         return;
1583     }
1585     sp_stroke_style_scale_line(spw);
1590 /**
1591  * \brief  This routine handles toggle events for buttons in the stroke style
1592  *         dialog.
1593  * When activated, this routine gets the data for the various widgets, and then
1594  * calls the respective routines to update css properties, etc.
1595  *
1596  */
1597 static void
1598 sp_stroke_style_any_toggled(GtkToggleButton *tb, SPWidget *spw)
1600     if (gtk_object_get_data(GTK_OBJECT(spw), "update")) {
1601         return;
1602     }
1604     if (gtk_toggle_button_get_active(tb)) {
1606         gchar const *join
1607             = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "join"));
1608         gchar const *cap
1609             = static_cast<gchar const *>(gtk_object_get_data(GTK_OBJECT(tb), "cap"));
1611         if (join) {
1612             GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1613             gtk_widget_set_sensitive (ml, !strcmp(join, "miter"));
1614         }
1616         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1618         /* TODO: Create some standardized method */
1619         SPCSSAttr *css = sp_repr_css_attr_new();
1621         if (join) {
1622             sp_repr_css_set_property(css, "stroke-linejoin", join);
1624             sp_desktop_set_style (desktop, css);
1626             sp_stroke_style_set_join_buttons(spw, GTK_WIDGET(tb));
1627         } else if (cap) {
1628             sp_repr_css_set_property(css, "stroke-linecap", cap);
1630             sp_desktop_set_style (desktop, css);
1632             sp_stroke_style_set_cap_buttons(spw, GTK_WIDGET(tb));
1633         }
1635         sp_repr_css_attr_unref(css);
1637         sp_document_done(sp_desktop_document(desktop), SP_VERB_DIALOG_FILL_STROKE,
1638                          _("Set stroke style"));
1639     }
1643 /**
1644  * Updates the join style toggle buttons
1645  */
1646 static void
1647 sp_stroke_style_set_join_buttons(SPWidget *spw, GtkWidget *active)
1649     GtkWidget *tb;
1651     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1652                                          INKSCAPE_STOCK_JOIN_MITER) );
1653     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1655     GtkWidget *ml = GTK_WIDGET(g_object_get_data(G_OBJECT(spw), "miterlimit_sb"));
1656     gtk_widget_set_sensitive(ml, (active == tb));
1658     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1659                                          INKSCAPE_STOCK_JOIN_ROUND) );
1660     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1661     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1662                                          INKSCAPE_STOCK_JOIN_BEVEL) );
1663     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1668 /**
1669  * Updates the cap style toggle buttons
1670  */
1671 static void
1672 sp_stroke_style_set_cap_buttons(SPWidget *spw, GtkWidget *active)
1674     GtkWidget *tb;
1676     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1677                                          INKSCAPE_STOCK_CAP_BUTT));
1678     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1679     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1680                                          INKSCAPE_STOCK_CAP_ROUND) );
1681     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1682     tb = GTK_WIDGET(gtk_object_get_data( GTK_OBJECT(spw),
1683                                          INKSCAPE_STOCK_CAP_SQUARE) );
1684     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tb), (active == tb));
1687 /**
1688  * Sets the current marker in the marker menu.
1689  */
1690 static void
1691 ink_marker_menu_set_current(SPObject *marker, GtkOptionMenu *mnu)
1693     gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(TRUE));
1695     GtkMenu *m = GTK_MENU(gtk_option_menu_get_menu(mnu));
1696     if (marker != NULL) {
1697         bool mark_is_stock = false;
1698         if (SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"))
1699             mark_is_stock = true;
1701         gchar *markname;
1702         if (mark_is_stock)
1703             markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"));
1704         else
1705             markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("id"));
1707         int markpos = 0;
1708         GList *kids = GTK_MENU_SHELL(m)->children;
1709         int i = 0;
1710         for (; kids != NULL; kids = kids->next) {
1711             gchar *mark = (gchar *) g_object_get_data(G_OBJECT(kids->data), "marker");
1712             if ( mark && strcmp(mark, markname) == 0 ) {
1713                 if ( mark_is_stock && !strcmp((gchar *) g_object_get_data(G_OBJECT(kids->data), "stockid"), "true"))
1714                     markpos = i;
1715                 if ( !mark_is_stock && !strcmp((gchar *) g_object_get_data(G_OBJECT(kids->data), "stockid"), "false"))
1716                     markpos = i;
1717             }
1718             i++;
1719         }
1720         gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), markpos);
1722         g_free (markname);
1723     }
1724     else {
1725         gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1726     }
1727     gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(FALSE));
1730 /**
1731  * Updates the marker menus to highlight the appropriate marker and scroll to 
1732  * that marker.
1733  */
1734 static void
1735 sp_stroke_style_update_marker_menus( SPWidget *spw,
1736                                      GSList const *objects)
1738     struct { char const *key; int loc; } const keyloc[] = {
1739         { "start_mark_menu", SP_MARKER_LOC_START },
1740         { "mid_mark_menu", SP_MARKER_LOC_MID },
1741         { "end_mark_menu", SP_MARKER_LOC_END }
1742     };
1744     bool all_texts = true;
1745     for (GSList *i = (GSList *) objects; i != NULL; i = i->next) {
1746         if (!SP_IS_TEXT (i->data)) {
1747             all_texts = false;
1748         }
1749     }
1751     for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1752         GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1753         if (all_texts) {
1754             // Per SVG spec, text objects cannot have markers; disable menus if only texts are selected
1755             gtk_widget_set_sensitive (GTK_WIDGET(mnu), FALSE);
1756         } else {
1757             gtk_widget_set_sensitive (GTK_WIDGET(mnu), TRUE);
1758         }
1759     }
1761     // We show markers of the first object in the list only
1762     // FIXME: use the first in the list that has the marker of each type, if any
1763     SPObject *object = SP_OBJECT(objects->data);
1765     for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1766         // For all three marker types,
1768         // find the corresponding menu
1769         GtkOptionMenu *mnu = (GtkOptionMenu *) g_object_get_data(G_OBJECT(spw), keyloc[i].key);
1771         // Quit if we're in update state
1772         if (gtk_object_get_data(GTK_OBJECT(mnu), "update")) {
1773             return;
1774         }
1776         if (object->style->marker[keyloc[i].loc].value != NULL && !all_texts) {
1777             // If the object has this type of markers,
1779             // Extract the name of the marker that the object uses
1780             SPObject *marker = ink_extract_marker_name(object->style->marker[keyloc[i].loc].value);
1781             // Scroll the menu to that marker
1782             ink_marker_menu_set_current (marker, mnu);
1784         } else {
1785             gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
1786         }
1787     }
1791 /**
1792  * Extract the actual name of the link
1793  * e.g. get mTriangle from url(#mTriangle).
1794  * \return Buffer containing the actual name, allocated from GLib;
1795  * the caller should free the buffer when they no longer need it.
1796  */
1797 static SPObject*
1798 ink_extract_marker_name(gchar const *n)
1800     gchar const *p = n;
1801     while (*p != '\0' && *p != '#') {
1802         p++;
1803     }
1805     if (*p == '\0' || p[1] == '\0') {
1806         return NULL;
1807     }
1809     p++;
1810     int c = 0;
1811     while (p[c] != '\0' && p[c] != ')') {
1812         c++;
1813     }
1815     if (p[c] == '\0') {
1816         return NULL;
1817     }
1819     gchar* b = g_strdup(p);
1820     b[c] = '\0';
1823     SPDesktop *desktop = inkscape_active_desktop();
1824     SPDocument *doc = sp_desktop_document(desktop);
1825     SPObject *marker = doc->getObjectById(b);
1826     return marker;
1830 /*
1831   Local Variables:
1832   mode:c++
1833   c-file-style:"stroustrup"
1834   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1835   indent-tabs-mode:nil
1836   fill-column:99
1837   End:
1838 */
1839 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :