Code

Pot and Dutch translation update
[inkscape.git] / src / widgets / stroke-style.cpp
1 /** @file
2  * @brief  Stroke style dialog
3  */
4 /* Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Bryce Harrington <brycehar@bryceharrington.org>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Maximilian Albert <maximilian.albert@gmail.com>
9  *   Josh Andler <scislac@users.sf.net>
10  *
11  * Copyright (C) 2001-2005 authors
12  * Copyright (C) 2001 Ximian, Inc.
13  * Copyright (C) 2004 John Cliff
14  * Copyright (C) 2008 Maximilian Albert (gtkmm-ification)
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 #define noSP_SS_VERBOSE
21 #include <glib/gmem.h>
22 #include <gtk/gtk.h>
23 #include <glibmm/i18n.h>
25 #include "desktop-handles.h"
26 #include "desktop-style.h"
27 #include "dialogs/dialog-events.h"
28 #include "display/canvas-bpath.h" // for SP_STROKE_LINEJOIN_*
29 #include "display/nr-arena.h"
30 #include "display/nr-arena-item.h"
31 #include "document-private.h"
32 #include "gradient-chemistry.h"
33 #include "helper/stock-items.h"
34 #include "helper/unit-menu.h"
35 #include "helper/units.h"
36 #include "inkscape.h"
37 #include "io/sys.h"
38 #include "marker.h"
39 #include "path-prefix.h"
40 #include "selection.h"
41 #include "sp-linear-gradient.h"
42 #include "sp-namedview.h"
43 #include "sp-pattern.h"
44 #include "sp-radial-gradient.h"
45 #include "sp-rect.h"
46 #include "sp-text.h"
47 #include "style.h"
48 #include "svg/css-ostringstream.h"
49 #include "ui/cache/svg_preview_cache.h"
50 #include "ui/icon-names.h"
51 #include "widgets/dash-selector.h"
52 #include "widgets/icon.h"
53 #include "widgets/paint-selector.h"
54 #include "widgets/sp-widget.h"
55 #include "widgets/spw-utilities.h"
56 #include "xml/repr.h"
58 #include "stroke-style.h"
59 #include "fill-style.h" // to get sp_fill_style_widget_set_desktop
60 #include "fill-n-stroke-factory.h"
62 /** Marker selection option menus */
63 static Gtk::OptionMenu * marker_start_menu = NULL;
64 static Gtk::OptionMenu * marker_mid_menu = NULL;
65 static Gtk::OptionMenu * marker_end_menu = NULL;
67 sigc::connection marker_start_menu_connection;
68 sigc::connection marker_mid_menu_connection;
69 sigc::connection marker_end_menu_connection;
71 static SPObject *ink_extract_marker_name(gchar const *n, SPDocument *doc);
72 static void      ink_markers_menu_update(Gtk::Container* spw, SPMarkerLoc const which);
74 static Inkscape::UI::Cache::SvgPreview svg_preview_cache;
76 Gtk::Widget *sp_stroke_style_paint_widget_new(void)
77 {
78     return Inkscape::Widgets::createStyleWidget( STROKE );
79 }
81 void sp_stroke_style_widget_set_desktop(Gtk::Widget *widget, SPDesktop *desktop)
82 {
83     sp_fill_style_widget_set_desktop(widget, desktop);
84 }
88 /* Line */
90 static void sp_stroke_style_line_selection_modified(SPWidget *spw, Inkscape::Selection *selection, guint flags, gpointer data);
91 static void sp_stroke_style_line_selection_changed(SPWidget *spw, Inkscape::Selection *selection, gpointer data);
93 static void sp_stroke_style_line_update(Gtk::Container *spw, Inkscape::Selection *sel);
95 static void sp_stroke_style_set_join_buttons(Gtk::Container *spw, Gtk::ToggleButton *active);
97 static void sp_stroke_style_set_cap_buttons(Gtk::Container *spw, Gtk::ToggleButton *active);
99 static void sp_stroke_style_width_changed(Gtk::Container *spw);
100 static void sp_stroke_style_miterlimit_changed(Gtk::Container *spw);
101 static void sp_stroke_style_any_toggled(Gtk::ToggleButton *tb, Gtk::Container *spw);
102 static void sp_stroke_style_line_dash_changed(Gtk::Container *spw);
104 static void sp_stroke_style_update_marker_menus(Gtk::Container *spw, GSList const *objects);
107 /**
108  * Helper function for creating radio buttons.  This should probably be re-thought out
109  * when reimplementing this with Gtkmm.
110  */
111 static Gtk::RadioButton *
112 sp_stroke_radio_button(Gtk::RadioButton *tb, char const *icon,
113                        Gtk::HBox *hb, Gtk::Container *spw,
114                        gchar const *key, gchar const *data)
116     g_assert(icon != NULL);
117     g_assert(hb  != NULL);
118     g_assert(spw != NULL);
120     if (tb == NULL) {
121         tb = new Gtk::RadioButton();
122     } else {
123         Gtk::RadioButtonGroup grp = tb->get_group();
124         tb = new Gtk::RadioButton(grp);
125     }
127     tb->show();
128     tb->set_mode(false);
129     hb->pack_start(*tb, false, false, 0);
130     spw->set_data(icon, tb);
131     tb->set_data(key, (gpointer*)data);
132     tb->signal_toggled().connect(sigc::bind<Gtk::RadioButton *, Gtk::Container *>(
133                                      sigc::ptr_fun(&sp_stroke_style_any_toggled), tb, spw));
134     Gtk::Widget *px = manage(Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_LARGE_TOOLBAR, icon)));
135     g_assert(px != NULL);
136     px->show();
137     tb->add(*px);
139     return tb;
143 /**
144  * Create sa copy of the marker named mname, determines its visible and renderable
145  * area in menu_id's bounding box, and then renders it.  This allows us to fill in
146  * preview images of each marker in the marker menu.
147  */
148 static Gtk::Image *
149 sp_marker_prev_new(unsigned psize, gchar const *mname,
150                    SPDocument *source, SPDocument *sandbox,
151                    gchar const *menu_id, NRArena const * /*arena*/, unsigned /*visionkey*/, NRArenaItem *root)
153     // Retrieve the marker named 'mname' from the source SVG document
154     SPObject const *marker = source->getObjectById(mname);
155     if (marker == NULL)
156         return NULL;
158     // Create a copy repr of the marker with id="sample"
159     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(sandbox);
160     Inkscape::XML::Node *mrepr = SP_OBJECT_REPR (marker)->duplicate(xml_doc);
161     mrepr->setAttribute("id", "sample");
163     // Replace the old sample in the sandbox by the new one
164     Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (sandbox->getObjectById("defs"));
165     SPObject *oldmarker = sandbox->getObjectById("sample");
166     if (oldmarker)
167         oldmarker->deleteObject(false);
168     defsrepr->appendChild(mrepr);
169     Inkscape::GC::release(mrepr);
171 // Uncomment this to get the sandbox documents saved (useful for debugging)
172     //FILE *fp = fopen (g_strconcat(menu_id, mname, ".svg", NULL), "w");
173     //sp_repr_save_stream (sp_document_repr_doc (sandbox), fp);
174     //fclose (fp);
176     // object to render; note that the id is the same as that of the menu we're building
177     SPObject *object = sandbox->getObjectById(menu_id);
178     sp_document_root (sandbox)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
179     sp_document_ensure_up_to_date(sandbox);
181     if (object == NULL || !SP_IS_ITEM(object))
182         return NULL; // sandbox broken?
184     // Find object's bbox in document
185     Geom::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
186     Geom::OptRect dbox = SP_ITEM(object)->getBounds(i2doc);
188     if (!dbox) {
189         return NULL;
190     }
192     /* Update to renderable state */
193     double sf = 0.8;
195     gchar *cache_name = g_strconcat(menu_id, mname, NULL);
196     Glib::ustring key = svg_preview_cache.cache_key(source->uri, cache_name, psize);
197     g_free (cache_name);
198     // TODO: is this correct?
199     Glib::RefPtr<Gdk::Pixbuf> pixbuf = Glib::wrap(svg_preview_cache.get_preview_from_cache(key));
201     if (!pixbuf) {
202         pixbuf = Glib::wrap(render_pixbuf(root, sf, *dbox, psize));
203         svg_preview_cache.set_preview_in_cache(key, pixbuf->gobj());
204     }
206     // Create widget
207     Gtk::Image *pb = new Gtk::Image(pixbuf);
209     return pb;
212 /**
213  *  Returns a list of markers in the defs of the given source document as a GSList object
214  *  Returns NULL if there are no markers in the document.
215  */
216 GSList *
217 ink_marker_list_get (SPDocument *source)
219     if (source == NULL)
220         return NULL;
222     GSList *ml   = NULL;
223     SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS (source);
224     for ( SPObject *child = sp_object_first_child(SP_OBJECT(defs));
225           child != NULL;
226           child = SP_OBJECT_NEXT (child) )
227     {
228         if (SP_IS_MARKER(child)) {
229             ml = g_slist_prepend (ml, child);
230         }
231     }
232     return ml;
235 #define MARKER_ITEM_MARGIN 0
237 /**
238  * Adds previews of markers in marker_list to the given menu widget
239  */
240 static void
241 sp_marker_menu_build (Gtk::Menu *m, GSList *marker_list, SPDocument *source, SPDocument *sandbox, gchar const *menu_id)
243     // Do this here, outside of loop, to speed up preview generation:
244     NRArena const *arena = NRArena::create();
245     unsigned const visionkey = sp_item_display_key_new(1);
246     NRArenaItem *root =  sp_item_invoke_show(SP_ITEM(SP_DOCUMENT_ROOT (sandbox)), (NRArena *) arena, visionkey, SP_ITEM_SHOW_DISPLAY);
248     for (; marker_list != NULL; marker_list = marker_list->next) {
249         Inkscape::XML::Node *repr = SP_OBJECT_REPR((SPItem *) marker_list->data);
250         Gtk::MenuItem *i = new Gtk::MenuItem();
251         i->show();
253         if (repr->attribute("inkscape:stockid"))
254             i->set_data("stockid", (void *) "true");
255         else
256             i->set_data("stockid", (void *) "false");
258         gchar const *markid = repr->attribute("id");
259         i->set_data("marker", (void *) markid);
261         Gtk::HBox *hb = new Gtk::HBox(false, MARKER_ITEM_MARGIN);
262         hb->show();
264         // generate preview
266         Gtk::Image *prv = sp_marker_prev_new (22, markid, source, sandbox, menu_id, arena, visionkey, root);
267         prv->show();
268         hb->pack_start(*prv, false, false, 6);
270         // create label
271         Gtk::Label *l = new Gtk::Label(repr->attribute("id"));
272         l->show();
273         l->set_alignment(0.0, 0.5);
275         hb->pack_start(*l, true, true, 0);
277         hb->show();
278         i->add(*hb);
280         m->append(*i);
281     }
283     sp_item_invoke_hide(SP_ITEM(sp_document_root(sandbox)), visionkey);
284     nr_object_unref((NRObject *) arena);
287 /**
288  * sp_marker_list_from_doc()
289  *
290  * \brief Pick up all markers from source, except those that are in
291  * current_doc (if non-NULL), and add items to the m menu
292  *
293  */
294 static void
295 sp_marker_list_from_doc (Gtk::Menu *m, SPDocument * /*current_doc*/, SPDocument *source, SPDocument * /*markers_doc*/, SPDocument *sandbox, gchar const *menu_id)
297     GSList *ml = ink_marker_list_get(source);
298     GSList *clean_ml = NULL;
300     for (; ml != NULL; ml = ml->next) {
301         if (!SP_IS_MARKER(ml->data))
302             continue;
304         // Add to the list of markers we really do wish to show
305         clean_ml = g_slist_prepend (clean_ml, ml->data);
306     }
307     sp_marker_menu_build(m, clean_ml, source, sandbox, menu_id);
309     g_slist_free (ml);
310     g_slist_free (clean_ml);
313 /**
314  * Returns a new document containing default start, mid, and end markers.
315  */
316 SPDocument *
317 ink_markers_preview_doc ()
319 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\">"
320 "  <defs id=\"defs\" />"
322 "  <g id=\"marker-start\">"
323 "    <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:url(#sample);marker-mid:none;marker-end:none\""
324 "       d=\"M 12.5,13 L 25,13\" id=\"path1\" />"
325 "    <rect style=\"fill:none;stroke:none\" id=\"rect2\""
326 "       width=\"25\" height=\"25\" x=\"0\" y=\"0\" />"
327 "  </g>"
329 "  <g id=\"marker-mid\">"
330 "    <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:url(#sample);marker-end:none\""
331 "       d=\"M 0,113 L 12.5,113 L 25,113\" id=\"path11\" />"
332 "    <rect style=\"fill:none;stroke:none\" id=\"rect22\""
333 "       width=\"25\" height=\"25\" x=\"0\" y=\"100\" />"
334 "  </g>"
336 "  <g id=\"marker-end\">"
337 "    <path style=\"fill:none;stroke:black;stroke-width:1.7;marker-start:none;marker-mid:none;marker-end:url(#sample)\""
338 "       d=\"M 0,213 L 12.5,213\" id=\"path111\" />"
339 "    <rect style=\"fill:none;stroke:none\" id=\"rect222\""
340 "       width=\"25\" height=\"25\" x=\"0\" y=\"200\" />"
341 "  </g>"
343 "</svg>";
345     return sp_document_new_from_mem (buffer, strlen(buffer), FALSE);
348 static void
349 ink_marker_menu_create_menu(Gtk::Menu *m, gchar const *menu_id, SPDocument *doc, SPDocument *sandbox)
351     static SPDocument *markers_doc = NULL;
353     // add "None"
354     Gtk::MenuItem *i = new Gtk::MenuItem();
355     i->show();
357     i->set_data("marker", (void *) "none");
359     Gtk::HBox *hb = new Gtk::HBox(false,  MARKER_ITEM_MARGIN);
360     hb->show();
362     Gtk::Label *l = new Gtk::Label( _("None") );
363     l->show();
364     l->set_alignment(0.0, 0.5);
366     hb->pack_start(*l, true, true, 0);
368     hb->show();
369     i->add(*hb);
370     m->append(*i);
372     // find and load markers.svg
373     if (markers_doc == NULL) {
374         char *markers_source = g_build_filename(INKSCAPE_MARKERSDIR, "markers.svg", NULL);
375         if (Inkscape::IO::file_test(markers_source, G_FILE_TEST_IS_REGULAR)) {
376             markers_doc = sp_document_new(markers_source, FALSE);
377         }
378         g_free(markers_source);
379     }
381     // suck in from current doc
382     sp_marker_list_from_doc(m, NULL, doc, markers_doc, sandbox, menu_id);
384     // add separator
385     {
386         //Gtk::Separator *i = gtk_separator_menu_item_new();
387         Gtk::SeparatorMenuItem *i = new Gtk::SeparatorMenuItem();
388         i->show();
389         m->append(*i);
390     }
392     // suck in from markers.svg
393     if (markers_doc) {
394         sp_document_ensure_up_to_date(doc);
395         sp_marker_list_from_doc(m, doc, markers_doc, NULL, sandbox, menu_id);
396     }
400 /**
401  * Creates a menu widget to display markers from markers.svg
402  */
403 static Gtk::OptionMenu *
404 ink_marker_menu(Gtk::Widget * /*tbl*/, gchar const *menu_id, SPDocument *sandbox)
406     SPDesktop *desktop = inkscape_active_desktop();
407     SPDocument *doc = sp_desktop_document(desktop);
408     Gtk::OptionMenu *mnu = new Gtk::OptionMenu();
410     /* Create new menu widget */
411     Gtk::Menu *m = new Gtk::Menu();
412     m->show();
414     mnu->set_data("updating", (gpointer) FALSE);
416     if (!doc) {
417         Gtk::MenuItem *i = new Gtk::MenuItem(_("No document selected"));
418         i->show();
419         m->append(*i);
420         mnu->set_sensitive(false);
422     } else {
423         ink_marker_menu_create_menu(m, menu_id, doc, sandbox);
425         mnu->set_sensitive(true);
426     }
428     mnu->set_data("menu_id", const_cast<gchar *>(menu_id));
429     mnu->set_menu(*m);
431     /* Set history */
432     mnu->set_history(0);
434     return mnu;
437 /**
438  * Handles when user selects one of the markers from the marker menu.
439  * Defines a uri string to refer to it, then applies it to all selected
440  * items in the current desktop.
441  */
442 static void
443 sp_marker_select(Gtk::OptionMenu *mnu, Gtk::Container *spw, SPMarkerLoc const which)
445     if (spw->get_data("update")) {
446         return;
447     }
449     SPDesktop *desktop = inkscape_active_desktop();
450     SPDocument *document = sp_desktop_document(desktop);
451     if (!document) {
452         return;
453     }
455     /* Get Marker */
456     if (!mnu->get_menu()->get_active()->get_data("marker"))
457     {
458         return;
459     }
460     gchar *markid = static_cast<gchar *>(mnu->get_menu()->get_active()->get_data("marker"));
461     gchar const *marker = "";
462     if (strcmp(markid, "none")) {
463        gchar *stockid = static_cast<gchar *>(mnu->get_menu()->get_active()->get_data("stockid"));
465        gchar *markurn = markid;
466        if (!strcmp(stockid,"true")) markurn = g_strconcat("urn:inkscape:marker:",markid,NULL);
467        SPObject *mark = get_stock_item(markurn);
468        if (mark) {
469             Inkscape::XML::Node *repr = SP_OBJECT_REPR(mark);
470             marker = g_strconcat("url(#", repr->attribute("id"), ")", NULL);
471         }
472     } else {
473         marker = markid;
474     }
475     SPCSSAttr *css = sp_repr_css_attr_new();
476     gchar const *menu_id = static_cast<gchar const *>(mnu->get_data("menu_id"));
477     sp_repr_css_set_property(css, menu_id, marker);
479     // Also update the marker dropdown menus, so the document's markers
480     // show up at the top of the menu
481 //    sp_stroke_style_line_update( SP_WIDGET(spw), desktop ? sp_desktop_selection(desktop) : NULL);
482     ink_markers_menu_update(spw, which);
484     Inkscape::Selection *selection = sp_desktop_selection(desktop);
485     GSList const *items = selection->itemList();
486     for (; items != NULL; items = items->next) {
487          SPItem *item = (SPItem *) items->data;
488          if (!SP_IS_SHAPE(item) || SP_IS_RECT(item)) // can't set marker to rect, until it's converted to using <path>
489              continue;
490          Inkscape::XML::Node *selrepr = SP_OBJECT_REPR((SPItem *) items->data);
491          if (selrepr) {
492              sp_repr_css_change_recursive(selrepr, css, "style");
493          }
494          SP_OBJECT(items->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
495          SP_OBJECT(items->data)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
496      }
498     sp_repr_css_attr_unref(css);
499     css = 0;
501     sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
502                      _("Set markers"));
504 };
506 static unsigned int
507 ink_marker_menu_get_pos(Gtk::Menu *mnu, gchar const *markname)
509     if (markname == NULL)
510         markname = static_cast<gchar const *>(mnu->get_active()->get_data("marker"));
512     if (markname == NULL)
513         return 0;
515     std::vector<Gtk::Widget *> kids = mnu->get_children();
516     unsigned int i = 0;
517     for (; i < kids.size();) {
518         gchar const *mark = static_cast<gchar const *>(kids[i]->get_data("marker"));
519         if (mark && strcmp(mark, markname) == 0) {
520             break;
521         }
522         ++i;
523     }
525     return i;
528 static void
529 ink_markers_menu_update(Gtk::Container* /*spw*/, SPMarkerLoc const which) {
530     SPDesktop  *desktop = inkscape_active_desktop();
531     SPDocument *document = sp_desktop_document(desktop);
532     SPDocument *sandbox = ink_markers_preview_doc ();
533     Gtk::Menu  *m;
534     int        pos;
536     // TODO: this code can be shortened by abstracting out marker_(start|mid|end)_...
537     switch (which) {
538         case SP_MARKER_LOC_START:
539             marker_start_menu_connection.block();
540             pos = ink_marker_menu_get_pos(marker_start_menu->get_menu(), NULL);
541             m = new Gtk::Menu();
542             m->show();
543             ink_marker_menu_create_menu(m, "marker-start", document, sandbox);
544             marker_start_menu->remove_menu();
545             marker_start_menu->set_menu(*m);
546             marker_start_menu->set_history(pos);
547             marker_start_menu_connection.unblock();
548             break;
550         case SP_MARKER_LOC_MID:
551             marker_mid_menu_connection.block();
552             pos = ink_marker_menu_get_pos(marker_mid_menu->get_menu(), NULL);
553             m = new Gtk::Menu();
554             m->show();
555             ink_marker_menu_create_menu(m, "marker-mid", document, sandbox);
556             marker_mid_menu->remove_menu();
557             marker_mid_menu->set_menu(*m);
558             marker_mid_menu->set_history(pos);
559             marker_mid_menu_connection.unblock();
560             break;
562         case SP_MARKER_LOC_END:
563             marker_end_menu_connection.block();
564             pos = ink_marker_menu_get_pos(marker_end_menu->get_menu(), NULL);
565             m = new Gtk::Menu();
566             m->show();
567             ink_marker_menu_create_menu(m, "marker-end", document, sandbox);
568             marker_end_menu->remove_menu();
569             marker_end_menu->set_menu(*m);
570             marker_end_menu->set_history(pos);
571             marker_end_menu_connection.unblock();
572             break;
573         default:
574             g_assert_not_reached();
575     }
578 /**
579  * Sets the stroke width units for all selected items.
580  * Also handles absolute and dimensionless units.
581  */
582 static gboolean stroke_width_set_unit(SPUnitSelector *,
583                                       SPUnit const *old,
584                                       SPUnit const *new_units,
585                                       Gtk::Container *spw)
587     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
589     if (!desktop) {
590         return FALSE;
591     }
593     Inkscape::Selection *selection = sp_desktop_selection (desktop);
595     if (selection->isEmpty())
596         return FALSE;
598     GSList const *objects = selection->itemList();
600     if ((old->base == SP_UNIT_ABSOLUTE || old->base == SP_UNIT_DEVICE) &&
601        (new_units->base == SP_UNIT_DIMENSIONLESS)) {
603         /* Absolute to percentage */
604         spw->set_data ("update", GUINT_TO_POINTER (TRUE));
606         Gtk::Adjustment *a = static_cast<Gtk::Adjustment *>(spw->get_data("width"));
607         float w = sp_units_get_pixels (a->get_value(), *old);
609         gdouble average = stroke_average_width (objects);
611         if (average == NR_HUGE || average == 0)
612             return FALSE;
614         a->set_value (100.0 * w / average);
616         spw->set_data ("update", GUINT_TO_POINTER (FALSE));
617         return TRUE;
619     } else if ((old->base == SP_UNIT_DIMENSIONLESS) &&
620               (new_units->base == SP_UNIT_ABSOLUTE || new_units->base == SP_UNIT_DEVICE)) {
622         /* Percentage to absolute */
623         spw->set_data ("update", GUINT_TO_POINTER (TRUE));
625         Gtk::Adjustment *a = static_cast<Gtk::Adjustment *>(spw->get_data ("width"));
627         gdouble average = stroke_average_width (objects);
629         a->set_value (sp_pixels_get_units (0.01 * a->get_value() * average, *new_units));
631         spw->set_data ("update", GUINT_TO_POINTER (FALSE));
632         return TRUE;
633     }
635     return FALSE;
639 /**
640  * \brief  Creates a new widget for the line stroke style.
641  *
642  */
643 Gtk::Container *
644 sp_stroke_style_line_widget_new(void)
646     Gtk::Widget *us;
647     SPDashSelector *ds;
648     GtkWidget *us_old, *spw_old;
649     Gtk::Container *spw;
650     Gtk::Table *t;
651     Gtk::Adjustment *a;
652     Gtk::SpinButton *sb;
653     Gtk::RadioButton *tb;
654     Gtk::HBox *f, *hb;
656     Gtk::Tooltips *tt = new Gtk::Tooltips();
658     spw_old = sp_widget_new_global(INKSCAPE);
659     spw = dynamic_cast<Gtk::Container *>(manage(Glib::wrap(spw_old)));
661     f = new Gtk::HBox(false, 0);
662     f->show();
663     spw->add(*f);
665     t = new Gtk::Table(3, 6, false);
666     t->show();
667     t->set_border_width(4);
668     t->set_row_spacings(4);
669     f->add(*t);
670     spw->set_data("stroke", t);
672     gint i = 0;
674     spw_label(t, C_("Stroke width", "Width:"), 0, i);
676     hb = spw_hbox(t, 3, 1, i);
678 // TODO: when this is gtkmmified, use an Inkscape::UI::Widget::ScalarUnit instead of the separate
679 // spinbutton and unit selector for stroke width. In sp_stroke_style_line_update, use
680 // setHundredPercent to remember the aeraged width corresponding to 100%. Then the
681 // stroke_width_set_unit will be removed (because ScalarUnit takes care of conversions itself), and
682 // with it, the two remaining calls of stroke_average_width, allowing us to get rid of that
683 // function in desktop-style.
685     a = new Gtk::Adjustment(1.0, 0.0, 1000.0, 0.1, 10.0, 0.0);
686     spw->set_data("width", a);
687     sb = new Gtk::SpinButton(*a, 0.1, 3);
688     tt->set_tip(*sb, _("Stroke width"));
689     sb->show();
691     sp_dialog_defocus_on_enter_cpp(sb);
693     hb->pack_start(*sb, false, false, 0);
694     us_old = sp_unit_selector_new(SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE);
695     us = manage(Glib::wrap(us_old));
696     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
697     if (desktop)
698         sp_unit_selector_set_unit (SP_UNIT_SELECTOR(us_old), sp_desktop_namedview(desktop)->doc_units);
699     sp_unit_selector_add_unit(SP_UNIT_SELECTOR(us_old), &sp_unit_get_by_id(SP_UNIT_PERCENT), 0);
700     g_signal_connect ( G_OBJECT (us_old), "set_unit", G_CALLBACK (stroke_width_set_unit), spw );
701     us->show();
702     sp_unit_selector_add_adjustment( SP_UNIT_SELECTOR(us_old), GTK_ADJUSTMENT(a->gobj()) );
703     hb->pack_start(*us, FALSE, FALSE, 0);
704     spw->set_data("units", us_old);
706     a->signal_value_changed().connect(sigc::bind(sigc::ptr_fun(&sp_stroke_style_width_changed), spw));
707     i++;
709     /* Join type */
710     // TRANSLATORS: The line join style specifies the shape to be used at the
711     //  corners of paths. It can be "miter", "round" or "bevel".
712     spw_label(t, _("Join:"), 0, i);
714     hb = spw_hbox(t, 3, 1, i);
716     tb = NULL;
718     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_JOIN_MITER,
719                                 hb, spw, "join", "miter");
721     // TRANSLATORS: Miter join: joining lines with a sharp (pointed) corner.
722     //  For an example, draw a triangle with a large stroke width and modify the
723     //  "Join" option (in the Fill and Stroke dialog).
724     tt->set_tip(*tb, _("Miter join"));
725     spw->set_data("miter join", tb);
727     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_JOIN_ROUND,
728                                 hb, spw, "join", "round");
731     // TRANSLATORS: Round join: joining lines with a rounded corner.
732     //  For an example, draw a triangle with a large stroke width and modify the
733     //  "Join" option (in the Fill and Stroke dialog).
734     tt->set_tip(*tb, _("Round join"));
735     spw->set_data("round join", tb);
737     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_JOIN_BEVEL,
738                                 hb, spw, "join", "bevel");
741     // TRANSLATORS: Bevel join: joining lines with a blunted (flattened) corner.
742     //  For an example, draw a triangle with a large stroke width and modify the
743     //  "Join" option (in the Fill and Stroke dialog).
744     tt->set_tip(*tb, _("Bevel join"));
745     spw->set_data("bevel join", tb);
747     i++;
749     /* Miterlimit  */
750     // TRANSLATORS: Miter limit: only for "miter join", this limits the length
751     //  of the sharp "spike" when the lines connect at too sharp an angle.
752     // When two line segments meet at a sharp angle, a miter join results in a
753     //  spike that extends well beyond the connection point. The purpose of the
754     //  miter limit is to cut off such spikes (i.e. convert them into bevels)
755     //  when they become too long.
756     spw_label(t, _("Miter limit:"), 0, i);
758     hb = spw_hbox(t, 3, 1, i);
760     a = new Gtk::Adjustment(4.0, 0.0, 100.0, 0.1, 10.0, 0.0);
761     spw->set_data("miterlimit", a);
763     sb = new Gtk::SpinButton(*a, 0.1, 2);
764     tt->set_tip(*sb, _("Maximum length of the miter (in units of stroke width)"));
765     sb->show();
766     spw->set_data("miterlimit_sb", sb);
767     sp_dialog_defocus_on_enter_cpp(sb);
769     hb->pack_start(*sb, false, false, 0);
771     a->signal_value_changed().connect(sigc::bind(sigc::ptr_fun(&sp_stroke_style_miterlimit_changed), spw));
772     i++;
774     /* Cap type */
775     // TRANSLATORS: cap type specifies the shape for the ends of lines
776     spw_label(t, _("Cap:"), 0, i);
778     hb = spw_hbox(t, 3, 1, i);
780     tb = NULL;
782     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_CAP_BUTT,
783                                 hb, spw, "cap", "butt");
784     spw->set_data("cap butt", tb);
786     // TRANSLATORS: Butt cap: the line shape does not extend beyond the end point
787     //  of the line; the ends of the line are square
788     tt->set_tip(*tb, _("Butt cap"));
790     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_CAP_ROUND,
791                                 hb, spw, "cap", "round");
792     spw->set_data("cap round", tb);
794     // TRANSLATORS: Round cap: the line shape extends beyond the end point of the
795     //  line; the ends of the line are rounded
796     tt->set_tip(*tb, _("Round cap"));
798     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_CAP_SQUARE,
799                                 hb, spw, "cap", "square");
800     spw->set_data("cap square", tb);
802     // TRANSLATORS: Square cap: the line shape extends beyond the end point of the
803     //  line; the ends of the line are square
804     tt->set_tip(*tb, _("Square cap"));
806     i++;
809     /* Dash */
810     spw_label(t, _("Dashes:"), 0, i);
811     ds = manage(new SPDashSelector);
813     ds->show();
814     t->attach(*ds, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
815     spw->set_data("dash", ds);
816     ds->changed_signal.connect(sigc::bind(sigc::ptr_fun(&sp_stroke_style_line_dash_changed), spw));
817     i++;
819     /* Drop down marker selectors*/
820     // TODO: this code can be shortened by iterating over the possible menus!
822     // doing this here once, instead of for each preview, to speed things up
823     SPDocument *sandbox = ink_markers_preview_doc ();
825     // TRANSLATORS: Path markers are an SVG feature that allows you to attach arbitrary shapes
826     // (arrowheads, bullets, faces, whatever) to the start, end, or middle nodes of a path.
827     spw_label(t, _("Start Markers:"), 0, i);
828     marker_start_menu = ink_marker_menu(spw ,"marker-start", sandbox);
829     tt->set_tip(*marker_start_menu, _("Start Markers are drawn on the first node of a path or shape"));
830     marker_start_menu_connection = marker_start_menu->signal_changed().connect(
831         sigc::bind<Gtk::OptionMenu *, Gtk::Container *, SPMarkerLoc>(
832             sigc::ptr_fun(&sp_marker_select), marker_start_menu, spw, SP_MARKER_LOC_START));
833     marker_start_menu->show();
834     t->attach(*marker_start_menu, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
835     spw->set_data("start_mark_menu", marker_start_menu);
837     i++;
838     spw_label(t, _("Mid Markers:"), 0, i);
839     marker_mid_menu = ink_marker_menu(spw ,"marker-mid", sandbox);
840     tt->set_tip(*marker_mid_menu, _("Mid Markers are drawn on every node of a path or shape except the first and last nodes"));
841     marker_mid_menu_connection = marker_mid_menu->signal_changed().connect(
842         sigc::bind<Gtk::OptionMenu *, Gtk::Container *, SPMarkerLoc>(
843             sigc::ptr_fun(&sp_marker_select), marker_mid_menu,spw, SP_MARKER_LOC_MID));
844     marker_mid_menu->show();
845     t->attach(*marker_mid_menu, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
846     spw->set_data("mid_mark_menu", marker_mid_menu);
848     i++;
849     spw_label(t, _("End Markers:"), 0, i);
850     marker_end_menu = ink_marker_menu(spw ,"marker-end", sandbox);
851     tt->set_tip(*marker_end_menu, _("End Markers are drawn on the last node of a path or shape"));
852     marker_end_menu_connection = marker_end_menu->signal_changed().connect(
853         sigc::bind<Gtk::OptionMenu *, Gtk::Container *, SPMarkerLoc>(
854             sigc::ptr_fun(&sp_marker_select), marker_end_menu, spw, SP_MARKER_LOC_END));
855     marker_end_menu->show();
856     t->attach(*marker_end_menu, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
857     spw->set_data("end_mark_menu", marker_end_menu);
859     i++;
861     // FIXME: we cheat and still use gtk+ signals
863     g_signal_connect(G_OBJECT(spw_old), "modify_selection",
864                      G_CALLBACK(sp_stroke_style_line_selection_modified),
865                      spw);
866     g_signal_connect(G_OBJECT(spw_old), "change_selection",
867                      G_CALLBACK(sp_stroke_style_line_selection_changed),
868                      spw);
870     sp_stroke_style_line_update(spw, desktop ? sp_desktop_selection(desktop) : NULL);
872     return spw;
875 /**
876  * Callback for when stroke style widget is modified.
877  * Triggers update action.
878  */
879 static void
880 sp_stroke_style_line_selection_modified(SPWidget *,
881                                         Inkscape::Selection *selection,
882                                         guint flags,
883                                         gpointer data)
885     Gtk::Container *spw = static_cast<Gtk::Container *>(data);
886     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
887         sp_stroke_style_line_update(spw, selection);
888     }
892 /**
893  * Callback for when stroke style widget is changed.
894  * Triggers update action.
895  */
896 static void
897 sp_stroke_style_line_selection_changed(SPWidget *,
898                                        Inkscape::Selection *selection,
899                                        gpointer data)
901     Gtk::Container *spw = static_cast<Gtk::Container *>(data);
902     sp_stroke_style_line_update(spw, selection);
905 /**
906  * Sets selector widgets' dash style from an SPStyle object.
907  */
908 static void
909 sp_dash_selector_set_from_style(SPDashSelector *dsel, SPStyle *style)
911     if (style->stroke_dash.n_dash > 0) {
912         double d[64];
913         int len = MIN(style->stroke_dash.n_dash, 64);
914         for (int i = 0; i < len; i++) {
915             if (style->stroke_width.computed != 0)
916                 d[i] = style->stroke_dash.dash[i] / style->stroke_width.computed;
917             else
918                 d[i] = style->stroke_dash.dash[i]; // is there a better thing to do for stroke_width==0?
919         }
920         dsel->set_dash(len, d, style->stroke_width.computed != 0 ?
921                        style->stroke_dash.offset / style->stroke_width.computed  :
922                        style->stroke_dash.offset);
923     } else {
924         dsel->set_dash(0, NULL, 0.0);
925     }
928 /**
929  * Sets the join type for a line, and updates the stroke style widget's buttons
930  */
931 static void
932 sp_jointype_set (Gtk::Container *spw, unsigned const jointype)
934     Gtk::RadioButton *tb = NULL;
935     switch (jointype) {
936         case SP_STROKE_LINEJOIN_MITER:
937             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_MITER));
938             break;
939         case SP_STROKE_LINEJOIN_ROUND:
940             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_ROUND));
941             break;
942         case SP_STROKE_LINEJOIN_BEVEL:
943             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_BEVEL));
944             break;
945         default:
946             break;
947     }
948     sp_stroke_style_set_join_buttons(spw, tb);
951 /**
952  * Sets the cap type for a line, and updates the stroke style widget's buttons
953  */
954 static void
955 sp_captype_set (Gtk::Container *spw, unsigned const captype)
957     Gtk::RadioButton *tb = NULL;
958     switch (captype) {
959         case SP_STROKE_LINECAP_BUTT:
960             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_BUTT));
961             break;
962         case SP_STROKE_LINECAP_ROUND:
963             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_ROUND));
964             break;
965         case SP_STROKE_LINECAP_SQUARE:
966             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_SQUARE));
967             break;
968         default:
969             break;
970     }
971     sp_stroke_style_set_cap_buttons(spw, tb);
974 /**
975  * Callback for when stroke style widget is updated, including markers, cap type,
976  * join type, etc.
977  */
978 static void
979 sp_stroke_style_line_update(Gtk::Container *spw, Inkscape::Selection *sel)
981     if (spw->get_data("update")) {
982         return;
983     }
985     spw->set_data("update", GINT_TO_POINTER(TRUE));
987     FillOrStroke kind = GPOINTER_TO_INT(spw->get_data("kind")) ? FILL : STROKE;
989     Gtk::Table *sset = static_cast<Gtk::Table *>(spw->get_data("stroke"));
990     Gtk::Adjustment *width = static_cast<Gtk::Adjustment *>(spw->get_data("width"));
991     Gtk::Adjustment *ml = static_cast<Gtk::Adjustment *>(spw->get_data("miterlimit"));
992     SPUnitSelector *us = SP_UNIT_SELECTOR(spw->get_data("units"));
993     SPDashSelector *dsel = static_cast<SPDashSelector *>(spw->get_data("dash"));
995     // create temporary style
996     SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
997     // query into it
998     int result_sw = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
999     int result_ml = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1000     int result_cap = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKECAP);
1001     int result_join = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1002     SPIPaint &targPaint = (kind == FILL) ? query->fill : query->stroke;
1004     if (!sel || sel->isEmpty()) {
1005         // Nothing selected, grey-out all controls in the stroke-style dialog
1006         sset->set_sensitive(false);
1008         spw->set_data("update", GINT_TO_POINTER(FALSE));
1010         return;
1011     } else {
1012         sset->set_sensitive(true);
1014         SPUnit const *unit = sp_unit_selector_get_unit(us);
1016         if (result_sw == QUERY_STYLE_MULTIPLE_AVERAGED) {
1017             sp_unit_selector_set_unit(us, &sp_unit_get_by_id(SP_UNIT_PERCENT));
1018         } else {
1019             // same width, or only one object; no sense to keep percent, switch to absolute
1020             if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1021                 sp_unit_selector_set_unit(us, sp_desktop_namedview(SP_ACTIVE_DESKTOP)->doc_units);
1022             }
1023         }
1025         unit = sp_unit_selector_get_unit(us);
1027         if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1028             double avgwidth = sp_pixels_get_units (query->stroke_width.computed, *unit);
1029             width->set_value(avgwidth);
1030         } else {
1031             width->set_value(100);
1032         }
1034         // if none of the selected objects has a stroke, than quite some controls should be disabled
1035         // The markers might still be shown though, so these will not be disabled
1036         bool enabled = (result_sw != QUERY_STYLE_NOTHING) && !targPaint.isNoneSet();
1037         /* No objects stroked, set insensitive */
1038         Gtk::RadioButton *tb = NULL;
1039         tb = static_cast<Gtk::RadioButton *>(spw->get_data("miter join"));
1040         tb->set_sensitive(enabled);
1041         tb = static_cast<Gtk::RadioButton *>(spw->get_data("round join"));
1042         tb->set_sensitive(enabled);
1043         tb = static_cast<Gtk::RadioButton *>(spw->get_data("bevel join"));
1044         tb->set_sensitive(enabled);
1046         Gtk::SpinButton* sb = NULL;
1047         sb = static_cast<Gtk::SpinButton *>(spw->get_data("miterlimit_sb"));
1048         sb->set_sensitive(enabled);
1050         tb = static_cast<Gtk::RadioButton *>(spw->get_data("cap butt"));
1051         tb->set_sensitive(enabled);
1052         tb = static_cast<Gtk::RadioButton *>(spw->get_data("cap round"));
1053         tb->set_sensitive(enabled);
1054         tb = static_cast<Gtk::RadioButton *>(spw->get_data("cap square"));
1055         tb->set_sensitive(enabled);
1057         dsel->set_sensitive(enabled);
1058     }
1060     if (result_ml != QUERY_STYLE_NOTHING)
1061         ml->set_value(query->stroke_miterlimit.value); // TODO: reflect averagedness?
1063     if (result_join != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1064         sp_jointype_set(spw, query->stroke_linejoin.value);
1065     } else {
1066         sp_stroke_style_set_join_buttons(spw, NULL);
1067     }
1069     if (result_cap != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1070         sp_captype_set (spw, query->stroke_linecap.value);
1071     } else {
1072         sp_stroke_style_set_cap_buttons(spw, NULL);
1073     }
1075     sp_style_unref(query);
1077     if (!sel || sel->isEmpty())
1078         return;
1080     GSList const *objects = sel->itemList();
1081     SPObject * const object = SP_OBJECT(objects->data);
1082     SPStyle * const style = SP_OBJECT_STYLE(object);
1084     /* Markers */
1085     sp_stroke_style_update_marker_menus(spw, objects); // FIXME: make this desktop query too
1087     /* Dash */
1088     sp_dash_selector_set_from_style(dsel, style); // FIXME: make this desktop query too
1090     sset->set_sensitive(true);
1092     spw->set_data("update", GINT_TO_POINTER(FALSE));
1095 /**
1096  * Sets a line's dash properties in a CSS style object.
1097  */
1098 static void
1099 sp_stroke_style_set_scaled_dash(SPCSSAttr *css,
1100                                 int ndash, double *dash, double offset,
1101                                 double scale)
1103     if (ndash > 0) {
1104         Inkscape::CSSOStringStream osarray;
1105         for (int i = 0; i < ndash; i++) {
1106             osarray << dash[i] * scale;
1107             if (i < (ndash - 1)) {
1108                 osarray << ",";
1109             }
1110         }
1111         sp_repr_css_set_property(css, "stroke-dasharray", osarray.str().c_str());
1113         Inkscape::CSSOStringStream osoffset;
1114         osoffset << offset * scale;
1115         sp_repr_css_set_property(css, "stroke-dashoffset", osoffset.str().c_str());
1116     } else {
1117         sp_repr_css_set_property(css, "stroke-dasharray", "none");
1118         sp_repr_css_set_property(css, "stroke-dashoffset", NULL);
1119     }
1122 /**
1123  * Sets line properties like width, dashes, markers, etc. on all currently selected items.
1124  */
1125 static void
1126 sp_stroke_style_scale_line(Gtk::Container *spw)
1128     if (spw->get_data("update")) {
1129         return;
1130     }
1132     spw->set_data("update", GINT_TO_POINTER(TRUE));
1134     Gtk::Adjustment *wadj = static_cast<Gtk::Adjustment *>(spw->get_data("width"));
1135     SPUnitSelector *us = SP_UNIT_SELECTOR(spw->get_data("units"));
1136     SPDashSelector *dsel = static_cast<SPDashSelector *>(spw->get_data("dash"));
1137     Gtk::Adjustment *ml = static_cast<Gtk::Adjustment *>(spw->get_data("miterlimit"));
1139     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1140     SPDocument *document = sp_desktop_document (desktop);
1141     Inkscape::Selection *selection = sp_desktop_selection (desktop);
1143     GSList const *items = selection->itemList();
1145     /* TODO: Create some standardized method */
1146     SPCSSAttr *css = sp_repr_css_attr_new();
1148     if (items) {
1150         double width_typed = wadj->get_value();
1151         double const miterlimit = ml->get_value();
1153         SPUnit const *const unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1155         double *dash, offset;
1156         int ndash;
1157         dsel->get_dash(&ndash, &dash, &offset);
1159         for (GSList const *i = items; i != NULL; i = i->next) {
1160             /* Set stroke width */
1161             double width;
1162             if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1163                 width = sp_units_get_pixels (width_typed, *unit);
1164             } else { // percentage
1165                 gdouble old_w = SP_OBJECT_STYLE (i->data)->stroke_width.computed;
1166                 width = old_w * width_typed / 100;
1167             }
1169             {
1170                 Inkscape::CSSOStringStream os_width;
1171                 os_width << width;
1172                 sp_repr_css_set_property(css, "stroke-width", os_width.str().c_str());
1173             }
1175             {
1176                 Inkscape::CSSOStringStream os_ml;
1177                 os_ml << miterlimit;
1178                 sp_repr_css_set_property(css, "stroke-miterlimit", os_ml.str().c_str());
1179             }
1181             /* Set dash */
1182             sp_stroke_style_set_scaled_dash(css, ndash, dash, offset, width);
1184             sp_desktop_apply_css_recursive (SP_OBJECT(i->data), css, true);
1185         }
1187         g_free(dash);
1189         if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1190             // reset to 100 percent
1191             wadj->set_value(100.0);
1192         }
1194     }
1196     // we have already changed the items, so set style without changing selection
1197     // FIXME: move the above stroke-setting stuff, including percentages, to desktop-style
1198     sp_desktop_set_style (desktop, css, false);
1200     sp_repr_css_attr_unref(css);
1201     css = 0;
1203     sp_document_done(document, SP_VERB_DIALOG_FILL_STROKE,
1204                      _("Set stroke style"));
1206     spw->set_data("update", GINT_TO_POINTER(FALSE));
1209 /**
1210  * Callback for when the stroke style's width changes.
1211  * Causes all line styles to be applied to all selected items.
1212  */
1213 static void
1214 sp_stroke_style_width_changed(Gtk::Container *spw)
1216     if (spw->get_data("update")) {
1217         return;
1218     }
1220     sp_stroke_style_scale_line(spw);
1223 /**
1224  * Callback for when the stroke style's miterlimit changes.
1225  * Causes all line styles to be applied to all selected items.
1226  */
1227 static void
1228 sp_stroke_style_miterlimit_changed(Gtk::Container *spw)
1230     if (spw->get_data("update")) {
1231         return;
1232     }
1234     sp_stroke_style_scale_line(spw);
1237 /**
1238  * Callback for when the stroke style's dash changes.
1239  * Causes all line styles to be applied to all selected items.
1240  */
1242 static void
1243 sp_stroke_style_line_dash_changed(Gtk::Container *spw)
1245     if (spw->get_data("update")) {
1246         return;
1247     }
1249     sp_stroke_style_scale_line(spw);
1252 /**
1253  * \brief  This routine handles toggle events for buttons in the stroke style
1254  *         dialog.
1255  * When activated, this routine gets the data for the various widgets, and then
1256  * calls the respective routines to update css properties, etc.
1257  *
1258  */
1259 static void
1260 sp_stroke_style_any_toggled(Gtk::ToggleButton *tb, Gtk::Container *spw)
1262     if (spw->get_data("update")) {
1263         return;
1264     }
1266     if (tb->get_active()) {
1268         gchar const *join
1269             = static_cast<gchar const *>(tb->get_data("join"));
1270         gchar const *cap
1271             = static_cast<gchar const *>(tb->get_data("cap"));
1273         if (join) {
1274             Gtk::SpinButton *ml = static_cast<Gtk::SpinButton *>(spw->get_data("miterlimit_sb"));
1275             ml->set_sensitive(!strcmp(join, "miter"));
1276         }
1278         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1280         /* TODO: Create some standardized method */
1281         SPCSSAttr *css = sp_repr_css_attr_new();
1283         if (join) {
1284             sp_repr_css_set_property(css, "stroke-linejoin", join);
1286             sp_desktop_set_style (desktop, css);
1288             sp_stroke_style_set_join_buttons(spw, tb);
1289         } else if (cap) {
1290             sp_repr_css_set_property(css, "stroke-linecap", cap);
1292             sp_desktop_set_style (desktop, css);
1294             sp_stroke_style_set_cap_buttons(spw, tb);
1295         }
1297         sp_repr_css_attr_unref(css);
1298         css = 0;
1300         sp_document_done(sp_desktop_document(desktop), SP_VERB_DIALOG_FILL_STROKE,
1301                          _("Set stroke style"));
1302     }
1305 /**
1306  * Updates the join style toggle buttons
1307  */
1308 static void
1309 sp_stroke_style_set_join_buttons(Gtk::Container *spw, Gtk::ToggleButton *active)
1311     Gtk::RadioButton *tb;
1313     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_MITER));
1314     tb->set_active(active == tb);
1316     Gtk::SpinButton *ml = static_cast<Gtk::SpinButton *>(spw->get_data("miterlimit_sb"));
1317     ml->set_sensitive(active == tb);
1319     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_ROUND));
1320     tb->set_active(active == tb);
1322     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_BEVEL));
1323     tb->set_active(active == tb);
1326 /**
1327  * Updates the cap style toggle buttons
1328  */
1329 static void
1330 sp_stroke_style_set_cap_buttons(Gtk::Container *spw, Gtk::ToggleButton *active)
1332     Gtk::RadioButton *tb;
1334     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_BUTT));
1335     tb->set_active(active == tb);
1336     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_ROUND));
1337     tb->set_active(active == tb);
1338     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_SQUARE));
1339     tb->set_active(active == tb);
1342 /**
1343  * Sets the current marker in the marker menu.
1344  */
1345 static void
1346 ink_marker_menu_set_current(SPObject *marker, Gtk::OptionMenu *mnu)
1348     mnu->set_data("update", GINT_TO_POINTER(TRUE));
1350     Gtk::Menu *m = mnu->get_menu();
1351     if (marker != NULL) {
1352         bool mark_is_stock = false;
1353         if (SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"))
1354             mark_is_stock = true;
1356         gchar *markname;
1357         if (mark_is_stock)
1358             markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"));
1359         else
1360             markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("id"));
1362         int markpos = ink_marker_menu_get_pos(m, markname);
1363         mnu->set_history(markpos);
1365         g_free (markname);
1366     }
1367     else {
1368         mnu->set_history(0);
1369     }
1370     mnu->set_data("update", GINT_TO_POINTER(FALSE));
1373 /**
1374  * Updates the marker menus to highlight the appropriate marker and scroll to
1375  * that marker.
1376  */
1377 static void
1378 sp_stroke_style_update_marker_menus(Gtk::Container *spw, GSList const *objects)
1380     struct { char const *key; int loc; } const keyloc[] = {
1381         { "start_mark_menu", SP_MARKER_LOC_START },
1382         { "mid_mark_menu", SP_MARKER_LOC_MID },
1383         { "end_mark_menu", SP_MARKER_LOC_END }
1384     };
1386     bool all_texts = true;
1387     for (GSList *i = (GSList *) objects; i != NULL; i = i->next) {
1388         if (!SP_IS_TEXT (i->data)) {
1389             all_texts = false;
1390         }
1391     }
1393     for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1394         Gtk::OptionMenu *mnu = static_cast<Gtk::OptionMenu *>(spw->get_data(keyloc[i].key));
1395         // Per SVG spec, text objects cannot have markers; disable menus if only texts are selected
1396         mnu->set_sensitive(!all_texts);
1397     }
1399     // We show markers of the first object in the list only
1400     // FIXME: use the first in the list that has the marker of each type, if any
1401     SPObject *object = SP_OBJECT(objects->data);
1403     for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1404         // For all three marker types,
1406         // find the corresponding menu
1407         Gtk::OptionMenu *mnu = static_cast<Gtk::OptionMenu *>(spw->get_data(keyloc[i].key));
1409         // Quit if we're in update state
1410         if (mnu->get_data("update")) {
1411             return;
1412         }
1414         if (object->style->marker[keyloc[i].loc].value != NULL && !all_texts) {
1415             // If the object has this type of markers,
1417             // Extract the name of the marker that the object uses
1418             SPObject *marker = ink_extract_marker_name(object->style->marker[keyloc[i].loc].value, SP_OBJECT_DOCUMENT(object));
1419             // Scroll the menu to that marker
1420             ink_marker_menu_set_current(marker, mnu);
1422         } else {
1423             mnu->set_history(0);
1424         }
1425     }
1429 /**
1430  * Extract the actual name of the link
1431  * e.g. get mTriangle from url(#mTriangle).
1432  * \return Buffer containing the actual name, allocated from GLib;
1433  * the caller should free the buffer when they no longer need it.
1434  */
1435 static SPObject*
1436 ink_extract_marker_name(gchar const *n, SPDocument *doc)
1438     gchar const *p = n;
1439     while (*p != '\0' && *p != '#') {
1440         p++;
1441     }
1443     if (*p == '\0' || p[1] == '\0') {
1444         return NULL;
1445     }
1447     p++;
1448     int c = 0;
1449     while (p[c] != '\0' && p[c] != ')') {
1450         c++;
1451     }
1453     if (p[c] == '\0') {
1454         return NULL;
1455     }
1457     gchar* b = g_strdup(p);
1458     b[c] = '\0';
1460     // FIXME: get the document from the object and let the caller pass it in
1461     SPObject *marker = doc->getObjectById(b);
1462     return marker;
1465 /*
1466   Local Variables:
1467   mode:c++
1468   c-file-style:"stroustrup"
1469   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1470   indent-tabs-mode:nil
1471   fill-column:99
1472   End:
1473 */
1474 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :