Code

A simple layout document as to what, why and how is cppification.
[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     sandbox->ensure_up_to_date();
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(object)->i2doc_affine());
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(defs)->first_child();
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 = SPItem::display_key_new(1);
246     NRArenaItem *root =  SP_ITEM(SP_DOCUMENT_ROOT (sandbox))->invoke_show((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(sp_document_root(sandbox))->invoke_hide(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 SPDocument::createNewDocFromMem (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 = SPDocument::createNewDoc(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         doc->ensure_up_to_date();
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     SPDocumentUndo::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     //TRANSLATORS: only translate "string" in "context|string".
675     // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
676     /* Stroke width */
677     spw_label(t, Q_("StrokeWidth|Width:"), 0, i);
679     hb = spw_hbox(t, 3, 1, i);
681 // TODO: when this is gtkmmified, use an Inkscape::UI::Widget::ScalarUnit instead of the separate
682 // spinbutton and unit selector for stroke width. In sp_stroke_style_line_update, use
683 // setHundredPercent to remember the aeraged width corresponding to 100%. Then the
684 // stroke_width_set_unit will be removed (because ScalarUnit takes care of conversions itself), and
685 // with it, the two remaining calls of stroke_average_width, allowing us to get rid of that
686 // function in desktop-style.
688     a = new Gtk::Adjustment(1.0, 0.0, 1000.0, 0.1, 10.0, 0.0);
689     spw->set_data("width", a);
690     sb = new Gtk::SpinButton(*a, 0.1, 3);
691     tt->set_tip(*sb, _("Stroke width"));
692     sb->show();
694     sp_dialog_defocus_on_enter_cpp(sb);
696     hb->pack_start(*sb, false, false, 0);
697     us_old = sp_unit_selector_new(SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE);
698     us = manage(Glib::wrap(us_old));
699     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
700     if (desktop)
701         sp_unit_selector_set_unit (SP_UNIT_SELECTOR(us_old), sp_desktop_namedview(desktop)->doc_units);
702     sp_unit_selector_add_unit(SP_UNIT_SELECTOR(us_old), &sp_unit_get_by_id(SP_UNIT_PERCENT), 0);
703     g_signal_connect ( G_OBJECT (us_old), "set_unit", G_CALLBACK (stroke_width_set_unit), spw );
704     us->show();
705     sp_unit_selector_add_adjustment( SP_UNIT_SELECTOR(us_old), GTK_ADJUSTMENT(a->gobj()) );
706     hb->pack_start(*us, FALSE, FALSE, 0);
707     spw->set_data("units", us_old);
709     a->signal_value_changed().connect(sigc::bind(sigc::ptr_fun(&sp_stroke_style_width_changed), spw));
710     i++;
712     /* Join type */
713     // TRANSLATORS: The line join style specifies the shape to be used at the
714     //  corners of paths. It can be "miter", "round" or "bevel".
715     spw_label(t, _("Join:"), 0, i);
717     hb = spw_hbox(t, 3, 1, i);
719     tb = NULL;
721     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_JOIN_MITER,
722                                 hb, spw, "join", "miter");
724     // TRANSLATORS: Miter join: joining lines with a sharp (pointed) corner.
725     //  For an example, draw a triangle with a large stroke width and modify the
726     //  "Join" option (in the Fill and Stroke dialog).
727     tt->set_tip(*tb, _("Miter join"));
728     spw->set_data("miter join", tb);
730     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_JOIN_ROUND,
731                                 hb, spw, "join", "round");
734     // TRANSLATORS: Round join: joining lines with a rounded corner.
735     //  For an example, draw a triangle with a large stroke width and modify the
736     //  "Join" option (in the Fill and Stroke dialog).
737     tt->set_tip(*tb, _("Round join"));
738     spw->set_data("round join", tb);
740     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_JOIN_BEVEL,
741                                 hb, spw, "join", "bevel");
744     // TRANSLATORS: Bevel join: joining lines with a blunted (flattened) corner.
745     //  For an example, draw a triangle with a large stroke width and modify the
746     //  "Join" option (in the Fill and Stroke dialog).
747     tt->set_tip(*tb, _("Bevel join"));
748     spw->set_data("bevel join", tb);
750     i++;
752     /* Miterlimit  */
753     // TRANSLATORS: Miter limit: only for "miter join", this limits the length
754     //  of the sharp "spike" when the lines connect at too sharp an angle.
755     // When two line segments meet at a sharp angle, a miter join results in a
756     //  spike that extends well beyond the connection point. The purpose of the
757     //  miter limit is to cut off such spikes (i.e. convert them into bevels)
758     //  when they become too long.
759     spw_label(t, _("Miter limit:"), 0, i);
761     hb = spw_hbox(t, 3, 1, i);
763     a = new Gtk::Adjustment(4.0, 0.0, 100.0, 0.1, 10.0, 0.0);
764     spw->set_data("miterlimit", a);
766     sb = new Gtk::SpinButton(*a, 0.1, 2);
767     tt->set_tip(*sb, _("Maximum length of the miter (in units of stroke width)"));
768     sb->show();
769     spw->set_data("miterlimit_sb", sb);
770     sp_dialog_defocus_on_enter_cpp(sb);
772     hb->pack_start(*sb, false, false, 0);
774     a->signal_value_changed().connect(sigc::bind(sigc::ptr_fun(&sp_stroke_style_miterlimit_changed), spw));
775     i++;
777     /* Cap type */
778     // TRANSLATORS: cap type specifies the shape for the ends of lines
779     spw_label(t, _("Cap:"), 0, i);
781     hb = spw_hbox(t, 3, 1, i);
783     tb = NULL;
785     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_CAP_BUTT,
786                                 hb, spw, "cap", "butt");
787     spw->set_data("cap butt", tb);
789     // TRANSLATORS: Butt cap: the line shape does not extend beyond the end point
790     //  of the line; the ends of the line are square
791     tt->set_tip(*tb, _("Butt cap"));
793     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_CAP_ROUND,
794                                 hb, spw, "cap", "round");
795     spw->set_data("cap round", tb);
797     // TRANSLATORS: Round cap: the line shape extends beyond the end point of the
798     //  line; the ends of the line are rounded
799     tt->set_tip(*tb, _("Round cap"));
801     tb = sp_stroke_radio_button(tb, INKSCAPE_ICON_STROKE_CAP_SQUARE,
802                                 hb, spw, "cap", "square");
803     spw->set_data("cap square", tb);
805     // TRANSLATORS: Square cap: the line shape extends beyond the end point of the
806     //  line; the ends of the line are square
807     tt->set_tip(*tb, _("Square cap"));
809     i++;
812     /* Dash */
813     spw_label(t, _("Dashes:"), 0, i);
814     ds = manage(new SPDashSelector);
816     ds->show();
817     t->attach(*ds, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
818     spw->set_data("dash", ds);
819     ds->changed_signal.connect(sigc::bind(sigc::ptr_fun(&sp_stroke_style_line_dash_changed), spw));
820     i++;
822     /* Drop down marker selectors*/
823     // TODO: this code can be shortened by iterating over the possible menus!
825     // doing this here once, instead of for each preview, to speed things up
826     SPDocument *sandbox = ink_markers_preview_doc ();
828     // TRANSLATORS: Path markers are an SVG feature that allows you to attach arbitrary shapes
829     // (arrowheads, bullets, faces, whatever) to the start, end, or middle nodes of a path.
830     spw_label(t, _("Start Markers:"), 0, i);
831     marker_start_menu = ink_marker_menu(spw ,"marker-start", sandbox);
832     tt->set_tip(*marker_start_menu, _("Start Markers are drawn on the first node of a path or shape"));
833     marker_start_menu_connection = marker_start_menu->signal_changed().connect(
834         sigc::bind<Gtk::OptionMenu *, Gtk::Container *, SPMarkerLoc>(
835             sigc::ptr_fun(&sp_marker_select), marker_start_menu, spw, SP_MARKER_LOC_START));
836     marker_start_menu->show();
837     t->attach(*marker_start_menu, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
838     spw->set_data("start_mark_menu", marker_start_menu);
840     i++;
841     spw_label(t, _("Mid Markers:"), 0, i);
842     marker_mid_menu = ink_marker_menu(spw ,"marker-mid", sandbox);
843     tt->set_tip(*marker_mid_menu, _("Mid Markers are drawn on every node of a path or shape except the first and last nodes"));
844     marker_mid_menu_connection = marker_mid_menu->signal_changed().connect(
845         sigc::bind<Gtk::OptionMenu *, Gtk::Container *, SPMarkerLoc>(
846             sigc::ptr_fun(&sp_marker_select), marker_mid_menu,spw, SP_MARKER_LOC_MID));
847     marker_mid_menu->show();
848     t->attach(*marker_mid_menu, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
849     spw->set_data("mid_mark_menu", marker_mid_menu);
851     i++;
852     spw_label(t, _("End Markers:"), 0, i);
853     marker_end_menu = ink_marker_menu(spw ,"marker-end", sandbox);
854     tt->set_tip(*marker_end_menu, _("End Markers are drawn on the last node of a path or shape"));
855     marker_end_menu_connection = marker_end_menu->signal_changed().connect(
856         sigc::bind<Gtk::OptionMenu *, Gtk::Container *, SPMarkerLoc>(
857             sigc::ptr_fun(&sp_marker_select), marker_end_menu, spw, SP_MARKER_LOC_END));
858     marker_end_menu->show();
859     t->attach(*marker_end_menu, 1, 4, i, i+1, (Gtk::EXPAND | Gtk::FILL), static_cast<Gtk::AttachOptions>(0), 0, 0);
860     spw->set_data("end_mark_menu", marker_end_menu);
862     i++;
864     // FIXME: we cheat and still use gtk+ signals
866     g_signal_connect(G_OBJECT(spw_old), "modify_selection",
867                      G_CALLBACK(sp_stroke_style_line_selection_modified),
868                      spw);
869     g_signal_connect(G_OBJECT(spw_old), "change_selection",
870                      G_CALLBACK(sp_stroke_style_line_selection_changed),
871                      spw);
873     sp_stroke_style_line_update(spw, desktop ? sp_desktop_selection(desktop) : NULL);
875     return spw;
878 /**
879  * Callback for when stroke style widget is modified.
880  * Triggers update action.
881  */
882 static void
883 sp_stroke_style_line_selection_modified(SPWidget *,
884                                         Inkscape::Selection *selection,
885                                         guint flags,
886                                         gpointer data)
888     Gtk::Container *spw = static_cast<Gtk::Container *>(data);
889     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
890         sp_stroke_style_line_update(spw, selection);
891     }
895 /**
896  * Callback for when stroke style widget is changed.
897  * Triggers update action.
898  */
899 static void
900 sp_stroke_style_line_selection_changed(SPWidget *,
901                                        Inkscape::Selection *selection,
902                                        gpointer data)
904     Gtk::Container *spw = static_cast<Gtk::Container *>(data);
905     sp_stroke_style_line_update(spw, selection);
908 /**
909  * Sets selector widgets' dash style from an SPStyle object.
910  */
911 static void
912 sp_dash_selector_set_from_style(SPDashSelector *dsel, SPStyle *style)
914     if (style->stroke_dash.n_dash > 0) {
915         double d[64];
916         int len = MIN(style->stroke_dash.n_dash, 64);
917         for (int i = 0; i < len; i++) {
918             if (style->stroke_width.computed != 0)
919                 d[i] = style->stroke_dash.dash[i] / style->stroke_width.computed;
920             else
921                 d[i] = style->stroke_dash.dash[i]; // is there a better thing to do for stroke_width==0?
922         }
923         dsel->set_dash(len, d, style->stroke_width.computed != 0 ?
924                        style->stroke_dash.offset / style->stroke_width.computed  :
925                        style->stroke_dash.offset);
926     } else {
927         dsel->set_dash(0, NULL, 0.0);
928     }
931 /**
932  * Sets the join type for a line, and updates the stroke style widget's buttons
933  */
934 static void
935 sp_jointype_set (Gtk::Container *spw, unsigned const jointype)
937     Gtk::RadioButton *tb = NULL;
938     switch (jointype) {
939         case SP_STROKE_LINEJOIN_MITER:
940             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_MITER));
941             break;
942         case SP_STROKE_LINEJOIN_ROUND:
943             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_ROUND));
944             break;
945         case SP_STROKE_LINEJOIN_BEVEL:
946             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_BEVEL));
947             break;
948         default:
949             break;
950     }
951     sp_stroke_style_set_join_buttons(spw, tb);
954 /**
955  * Sets the cap type for a line, and updates the stroke style widget's buttons
956  */
957 static void
958 sp_captype_set (Gtk::Container *spw, unsigned const captype)
960     Gtk::RadioButton *tb = NULL;
961     switch (captype) {
962         case SP_STROKE_LINECAP_BUTT:
963             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_BUTT));
964             break;
965         case SP_STROKE_LINECAP_ROUND:
966             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_ROUND));
967             break;
968         case SP_STROKE_LINECAP_SQUARE:
969             tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_SQUARE));
970             break;
971         default:
972             break;
973     }
974     sp_stroke_style_set_cap_buttons(spw, tb);
977 /**
978  * Callback for when stroke style widget is updated, including markers, cap type,
979  * join type, etc.
980  */
981 static void
982 sp_stroke_style_line_update(Gtk::Container *spw, Inkscape::Selection *sel)
984     if (spw->get_data("update")) {
985         return;
986     }
988     spw->set_data("update", GINT_TO_POINTER(TRUE));
990     FillOrStroke kind = GPOINTER_TO_INT(spw->get_data("kind")) ? FILL : STROKE;
992     Gtk::Table *sset = static_cast<Gtk::Table *>(spw->get_data("stroke"));
993     Gtk::Adjustment *width = static_cast<Gtk::Adjustment *>(spw->get_data("width"));
994     Gtk::Adjustment *ml = static_cast<Gtk::Adjustment *>(spw->get_data("miterlimit"));
995     SPUnitSelector *us = SP_UNIT_SELECTOR(spw->get_data("units"));
996     SPDashSelector *dsel = static_cast<SPDashSelector *>(spw->get_data("dash"));
998     // create temporary style
999     SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
1000     // query into it
1001     int result_sw = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEWIDTH);
1002     int result_ml = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
1003     int result_cap = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKECAP);
1004     int result_join = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_STROKEJOIN);
1005     SPIPaint &targPaint = (kind == FILL) ? query->fill : query->stroke;
1007     if (!sel || sel->isEmpty()) {
1008         // Nothing selected, grey-out all controls in the stroke-style dialog
1009         sset->set_sensitive(false);
1011         spw->set_data("update", GINT_TO_POINTER(FALSE));
1013         return;
1014     } else {
1015         sset->set_sensitive(true);
1017         SPUnit const *unit = sp_unit_selector_get_unit(us);
1019         if (result_sw == QUERY_STYLE_MULTIPLE_AVERAGED) {
1020             sp_unit_selector_set_unit(us, &sp_unit_get_by_id(SP_UNIT_PERCENT));
1021         } else {
1022             // same width, or only one object; no sense to keep percent, switch to absolute
1023             if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1024                 sp_unit_selector_set_unit(us, sp_desktop_namedview(SP_ACTIVE_DESKTOP)->doc_units);
1025             }
1026         }
1028         unit = sp_unit_selector_get_unit(us);
1030         if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1031             double avgwidth = sp_pixels_get_units (query->stroke_width.computed, *unit);
1032             width->set_value(avgwidth);
1033         } else {
1034             width->set_value(100);
1035         }
1037         // if none of the selected objects has a stroke, than quite some controls should be disabled
1038         // The markers might still be shown though, so these will not be disabled
1039         bool enabled = (result_sw != QUERY_STYLE_NOTHING) && !targPaint.isNoneSet();
1040         /* No objects stroked, set insensitive */
1041         Gtk::RadioButton *tb = NULL;
1042         tb = static_cast<Gtk::RadioButton *>(spw->get_data("miter join"));
1043         tb->set_sensitive(enabled);
1044         tb = static_cast<Gtk::RadioButton *>(spw->get_data("round join"));
1045         tb->set_sensitive(enabled);
1046         tb = static_cast<Gtk::RadioButton *>(spw->get_data("bevel join"));
1047         tb->set_sensitive(enabled);
1049         Gtk::SpinButton* sb = NULL;
1050         sb = static_cast<Gtk::SpinButton *>(spw->get_data("miterlimit_sb"));
1051         sb->set_sensitive(enabled);
1053         tb = static_cast<Gtk::RadioButton *>(spw->get_data("cap butt"));
1054         tb->set_sensitive(enabled);
1055         tb = static_cast<Gtk::RadioButton *>(spw->get_data("cap round"));
1056         tb->set_sensitive(enabled);
1057         tb = static_cast<Gtk::RadioButton *>(spw->get_data("cap square"));
1058         tb->set_sensitive(enabled);
1060         dsel->set_sensitive(enabled);
1061     }
1063     if (result_ml != QUERY_STYLE_NOTHING)
1064         ml->set_value(query->stroke_miterlimit.value); // TODO: reflect averagedness?
1066     if (result_join != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1067         sp_jointype_set(spw, query->stroke_linejoin.value);
1068     } else {
1069         sp_stroke_style_set_join_buttons(spw, NULL);
1070     }
1072     if (result_cap != QUERY_STYLE_MULTIPLE_DIFFERENT) {
1073         sp_captype_set (spw, query->stroke_linecap.value);
1074     } else {
1075         sp_stroke_style_set_cap_buttons(spw, NULL);
1076     }
1078     sp_style_unref(query);
1080     if (!sel || sel->isEmpty())
1081         return;
1083     GSList const *objects = sel->itemList();
1084     SPObject * const object = SP_OBJECT(objects->data);
1085     SPStyle * const style = SP_OBJECT_STYLE(object);
1087     /* Markers */
1088     sp_stroke_style_update_marker_menus(spw, objects); // FIXME: make this desktop query too
1090     /* Dash */
1091     sp_dash_selector_set_from_style(dsel, style); // FIXME: make this desktop query too
1093     sset->set_sensitive(true);
1095     spw->set_data("update", GINT_TO_POINTER(FALSE));
1098 /**
1099  * Sets a line's dash properties in a CSS style object.
1100  */
1101 static void
1102 sp_stroke_style_set_scaled_dash(SPCSSAttr *css,
1103                                 int ndash, double *dash, double offset,
1104                                 double scale)
1106     if (ndash > 0) {
1107         Inkscape::CSSOStringStream osarray;
1108         for (int i = 0; i < ndash; i++) {
1109             osarray << dash[i] * scale;
1110             if (i < (ndash - 1)) {
1111                 osarray << ",";
1112             }
1113         }
1114         sp_repr_css_set_property(css, "stroke-dasharray", osarray.str().c_str());
1116         Inkscape::CSSOStringStream osoffset;
1117         osoffset << offset * scale;
1118         sp_repr_css_set_property(css, "stroke-dashoffset", osoffset.str().c_str());
1119     } else {
1120         sp_repr_css_set_property(css, "stroke-dasharray", "none");
1121         sp_repr_css_set_property(css, "stroke-dashoffset", NULL);
1122     }
1125 /**
1126  * Sets line properties like width, dashes, markers, etc. on all currently selected items.
1127  */
1128 static void
1129 sp_stroke_style_scale_line(Gtk::Container *spw)
1131     if (spw->get_data("update")) {
1132         return;
1133     }
1135     spw->set_data("update", GINT_TO_POINTER(TRUE));
1137     Gtk::Adjustment *wadj = static_cast<Gtk::Adjustment *>(spw->get_data("width"));
1138     SPUnitSelector *us = SP_UNIT_SELECTOR(spw->get_data("units"));
1139     SPDashSelector *dsel = static_cast<SPDashSelector *>(spw->get_data("dash"));
1140     Gtk::Adjustment *ml = static_cast<Gtk::Adjustment *>(spw->get_data("miterlimit"));
1142     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1143     SPDocument *document = sp_desktop_document (desktop);
1144     Inkscape::Selection *selection = sp_desktop_selection (desktop);
1146     GSList const *items = selection->itemList();
1148     /* TODO: Create some standardized method */
1149     SPCSSAttr *css = sp_repr_css_attr_new();
1151     if (items) {
1153         double width_typed = wadj->get_value();
1154         double const miterlimit = ml->get_value();
1156         SPUnit const *const unit = sp_unit_selector_get_unit(SP_UNIT_SELECTOR(us));
1158         double *dash, offset;
1159         int ndash;
1160         dsel->get_dash(&ndash, &dash, &offset);
1162         for (GSList const *i = items; i != NULL; i = i->next) {
1163             /* Set stroke width */
1164             double width;
1165             if (unit->base == SP_UNIT_ABSOLUTE || unit->base == SP_UNIT_DEVICE) {
1166                 width = sp_units_get_pixels (width_typed, *unit);
1167             } else { // percentage
1168                 gdouble old_w = SP_OBJECT_STYLE (i->data)->stroke_width.computed;
1169                 width = old_w * width_typed / 100;
1170             }
1172             {
1173                 Inkscape::CSSOStringStream os_width;
1174                 os_width << width;
1175                 sp_repr_css_set_property(css, "stroke-width", os_width.str().c_str());
1176             }
1178             {
1179                 Inkscape::CSSOStringStream os_ml;
1180                 os_ml << miterlimit;
1181                 sp_repr_css_set_property(css, "stroke-miterlimit", os_ml.str().c_str());
1182             }
1184             /* Set dash */
1185             sp_stroke_style_set_scaled_dash(css, ndash, dash, offset, width);
1187             sp_desktop_apply_css_recursive (SP_OBJECT(i->data), css, true);
1188         }
1190         g_free(dash);
1192         if (unit->base != SP_UNIT_ABSOLUTE && unit->base != SP_UNIT_DEVICE) {
1193             // reset to 100 percent
1194             wadj->set_value(100.0);
1195         }
1197     }
1199     // we have already changed the items, so set style without changing selection
1200     // FIXME: move the above stroke-setting stuff, including percentages, to desktop-style
1201     sp_desktop_set_style (desktop, css, false);
1203     sp_repr_css_attr_unref(css);
1204     css = 0;
1206     SPDocumentUndo::done(document, SP_VERB_DIALOG_FILL_STROKE,
1207                      _("Set stroke style"));
1209     spw->set_data("update", GINT_TO_POINTER(FALSE));
1212 /**
1213  * Callback for when the stroke style's width changes.
1214  * Causes all line styles to be applied to all selected items.
1215  */
1216 static void
1217 sp_stroke_style_width_changed(Gtk::Container *spw)
1219     if (spw->get_data("update")) {
1220         return;
1221     }
1223     sp_stroke_style_scale_line(spw);
1226 /**
1227  * Callback for when the stroke style's miterlimit changes.
1228  * Causes all line styles to be applied to all selected items.
1229  */
1230 static void
1231 sp_stroke_style_miterlimit_changed(Gtk::Container *spw)
1233     if (spw->get_data("update")) {
1234         return;
1235     }
1237     sp_stroke_style_scale_line(spw);
1240 /**
1241  * Callback for when the stroke style's dash changes.
1242  * Causes all line styles to be applied to all selected items.
1243  */
1245 static void
1246 sp_stroke_style_line_dash_changed(Gtk::Container *spw)
1248     if (spw->get_data("update")) {
1249         return;
1250     }
1252     sp_stroke_style_scale_line(spw);
1255 /**
1256  * \brief  This routine handles toggle events for buttons in the stroke style
1257  *         dialog.
1258  * When activated, this routine gets the data for the various widgets, and then
1259  * calls the respective routines to update css properties, etc.
1260  *
1261  */
1262 static void
1263 sp_stroke_style_any_toggled(Gtk::ToggleButton *tb, Gtk::Container *spw)
1265     if (spw->get_data("update")) {
1266         return;
1267     }
1269     if (tb->get_active()) {
1271         gchar const *join
1272             = static_cast<gchar const *>(tb->get_data("join"));
1273         gchar const *cap
1274             = static_cast<gchar const *>(tb->get_data("cap"));
1276         if (join) {
1277             Gtk::SpinButton *ml = static_cast<Gtk::SpinButton *>(spw->get_data("miterlimit_sb"));
1278             ml->set_sensitive(!strcmp(join, "miter"));
1279         }
1281         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1283         /* TODO: Create some standardized method */
1284         SPCSSAttr *css = sp_repr_css_attr_new();
1286         if (join) {
1287             sp_repr_css_set_property(css, "stroke-linejoin", join);
1289             sp_desktop_set_style (desktop, css);
1291             sp_stroke_style_set_join_buttons(spw, tb);
1292         } else if (cap) {
1293             sp_repr_css_set_property(css, "stroke-linecap", cap);
1295             sp_desktop_set_style (desktop, css);
1297             sp_stroke_style_set_cap_buttons(spw, tb);
1298         }
1300         sp_repr_css_attr_unref(css);
1301         css = 0;
1303         SPDocumentUndo::done(sp_desktop_document(desktop), SP_VERB_DIALOG_FILL_STROKE,
1304                          _("Set stroke style"));
1305     }
1308 /**
1309  * Updates the join style toggle buttons
1310  */
1311 static void
1312 sp_stroke_style_set_join_buttons(Gtk::Container *spw, Gtk::ToggleButton *active)
1314     Gtk::RadioButton *tb;
1316     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_MITER));
1317     tb->set_active(active == tb);
1319     Gtk::SpinButton *ml = static_cast<Gtk::SpinButton *>(spw->get_data("miterlimit_sb"));
1320     ml->set_sensitive(active == tb);
1322     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_ROUND));
1323     tb->set_active(active == tb);
1325     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_JOIN_BEVEL));
1326     tb->set_active(active == tb);
1329 /**
1330  * Updates the cap style toggle buttons
1331  */
1332 static void
1333 sp_stroke_style_set_cap_buttons(Gtk::Container *spw, Gtk::ToggleButton *active)
1335     Gtk::RadioButton *tb;
1337     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_BUTT));
1338     tb->set_active(active == tb);
1339     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_ROUND));
1340     tb->set_active(active == tb);
1341     tb = static_cast<Gtk::RadioButton *>(spw->get_data(INKSCAPE_ICON_STROKE_CAP_SQUARE));
1342     tb->set_active(active == tb);
1345 /**
1346  * Sets the current marker in the marker menu.
1347  */
1348 static void
1349 ink_marker_menu_set_current(SPObject *marker, Gtk::OptionMenu *mnu)
1351     mnu->set_data("update", GINT_TO_POINTER(TRUE));
1353     Gtk::Menu *m = mnu->get_menu();
1354     if (marker != NULL) {
1355         bool mark_is_stock = false;
1356         if (SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"))
1357             mark_is_stock = true;
1359         gchar *markname;
1360         if (mark_is_stock)
1361             markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("inkscape:stockid"));
1362         else
1363             markname = g_strdup(SP_OBJECT_REPR(marker)->attribute("id"));
1365         int markpos = ink_marker_menu_get_pos(m, markname);
1366         mnu->set_history(markpos);
1368         g_free (markname);
1369     }
1370     else {
1371         mnu->set_history(0);
1372     }
1373     mnu->set_data("update", GINT_TO_POINTER(FALSE));
1376 /**
1377  * Updates the marker menus to highlight the appropriate marker and scroll to
1378  * that marker.
1379  */
1380 static void
1381 sp_stroke_style_update_marker_menus(Gtk::Container *spw, GSList const *objects)
1383     struct { char const *key; int loc; } const keyloc[] = {
1384         { "start_mark_menu", SP_MARKER_LOC_START },
1385         { "mid_mark_menu", SP_MARKER_LOC_MID },
1386         { "end_mark_menu", SP_MARKER_LOC_END }
1387     };
1389     bool all_texts = true;
1390     for (GSList *i = (GSList *) objects; i != NULL; i = i->next) {
1391         if (!SP_IS_TEXT (i->data)) {
1392             all_texts = false;
1393         }
1394     }
1396     for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1397         Gtk::OptionMenu *mnu = static_cast<Gtk::OptionMenu *>(spw->get_data(keyloc[i].key));
1398         // Per SVG spec, text objects cannot have markers; disable menus if only texts are selected
1399         mnu->set_sensitive(!all_texts);
1400     }
1402     // We show markers of the first object in the list only
1403     // FIXME: use the first in the list that has the marker of each type, if any
1404     SPObject *object = SP_OBJECT(objects->data);
1406     for (unsigned i = 0; i < G_N_ELEMENTS(keyloc); ++i) {
1407         // For all three marker types,
1409         // find the corresponding menu
1410         Gtk::OptionMenu *mnu = static_cast<Gtk::OptionMenu *>(spw->get_data(keyloc[i].key));
1412         // Quit if we're in update state
1413         if (mnu->get_data("update")) {
1414             return;
1415         }
1417         if (object->style->marker[keyloc[i].loc].value != NULL && !all_texts) {
1418             // If the object has this type of markers,
1420             // Extract the name of the marker that the object uses
1421             SPObject *marker = ink_extract_marker_name(object->style->marker[keyloc[i].loc].value, SP_OBJECT_DOCUMENT(object));
1422             // Scroll the menu to that marker
1423             ink_marker_menu_set_current(marker, mnu);
1425         } else {
1426             mnu->set_history(0);
1427         }
1428     }
1432 /**
1433  * Extract the actual name of the link
1434  * e.g. get mTriangle from url(#mTriangle).
1435  * \return Buffer containing the actual name, allocated from GLib;
1436  * the caller should free the buffer when they no longer need it.
1437  */
1438 static SPObject*
1439 ink_extract_marker_name(gchar const *n, SPDocument *doc)
1441     gchar const *p = n;
1442     while (*p != '\0' && *p != '#') {
1443         p++;
1444     }
1446     if (*p == '\0' || p[1] == '\0') {
1447         return NULL;
1448     }
1450     p++;
1451     int c = 0;
1452     while (p[c] != '\0' && p[c] != ')') {
1453         c++;
1454     }
1456     if (p[c] == '\0') {
1457         return NULL;
1458     }
1460     gchar* b = g_strdup(p);
1461     b[c] = '\0';
1463     // FIXME: get the document from the object and let the caller pass it in
1464     SPObject *marker = doc->getObjectById(b);
1465     return marker;
1468 /*
1469   Local Variables:
1470   mode:c++
1471   c-file-style:"stroustrup"
1472   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1473   indent-tabs-mode:nil
1474   fill-column:99
1475   End:
1476 */
1477 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :