Code

bulk whitespace removal patch #1198588 by gigaclon
[inkscape.git] / src / widgets / dash-selector.cpp
1 #define __SP_DASH_SELECTOR_C__
3 /*
4  * Optionmenu for selecting dash patterns
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2002 Lauris Kaplinski
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #define DASH_PREVIEW_WIDTH 2
16 #define DASH_PREVIEW_LENGTH 80
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include <libnr/nr-macros.h>
22 #include <gtk/gtk.h>
24 #include <glibmm/i18n.h>
25 #include "../style.h"
26 #include "../dialogs/dialog-events.h"
28 #include "dash-selector.h"
30 enum {CHANGED, LAST_SIGNAL};
32 struct SPDashSelector {
33         GtkHBox hbox;
35         GtkWidget *dash;
36         GtkObject *offset;
37 };
39 struct SPDashSelectorClass {
40         GtkHBoxClass parent_class;
42         void (* changed) (SPDashSelector *dsel);
43 };
45 double dash_0[] = {-1.0};
46 double dash_1_1[] = {1.0, 1.0, -1.0};
47 double dash_2_1[] = {2.0, 1.0, -1.0};
48 double dash_4_1[] = {4.0, 1.0, -1.0};
49 double dash_1_2[] = {1.0, 2.0, -1.0};
50 double dash_1_4[] = {1.0, 4.0, -1.0};
52 double *builtin_dashes[] = {dash_0, dash_1_1, dash_2_1, dash_4_1, dash_1_2, dash_1_4, NULL};
54 static double **dashes = NULL;
56 static void sp_dash_selector_class_init (SPDashSelectorClass *klass);
57 static void sp_dash_selector_init (SPDashSelector *dsel);
58 static GtkWidget *sp_dash_selector_menu_item_new (SPDashSelector *dsel, double *pattern);
59 static void sp_dash_selector_dash_activate (GtkObject *object, SPDashSelector *dsel);
60 static void sp_dash_selector_offset_value_changed (GtkAdjustment *adj, SPDashSelector *dsel);
62 static GtkHBoxClass *parent_class;
63 static guint signals[LAST_SIGNAL] = {0};
65 GtkType
66 sp_dash_selector_get_type (void)
67 {
68         static GtkType type = 0;
69         if (!type) {
70                 GtkTypeInfo info = {
71                         "SPDashSelector",
72                         sizeof (SPDashSelector),
73                         sizeof (SPDashSelectorClass),
74                         (GtkClassInitFunc) sp_dash_selector_class_init,
75                         (GtkObjectInitFunc) sp_dash_selector_init,
76                         NULL, NULL, NULL
77                 };
78                 type = gtk_type_unique (GTK_TYPE_HBOX, &info);
79         }
80         return type;
81 }
83 static void
84 sp_dash_selector_class_init (SPDashSelectorClass *klass)
85 {
86         parent_class = (GtkHBoxClass*)gtk_type_class (GTK_TYPE_HBOX);
88         signals[CHANGED] = gtk_signal_new ("changed",
89                                            (GtkSignalRunType)(GTK_RUN_FIRST | GTK_RUN_NO_RECURSE),
90                                            G_TYPE_FROM_CLASS (klass),
91                                            GTK_SIGNAL_OFFSET (SPDashSelectorClass, changed),
92                                            gtk_marshal_NONE__NONE,
93                                            GTK_TYPE_NONE, 0);
94 }
96 static void
97 sp_dash_selector_init (SPDashSelector *dsel)
98 {
99         GtkTooltips *tt = gtk_tooltips_new();
101         dsel->dash = gtk_option_menu_new ();
102         gtk_tooltips_set_tip (tt, dsel->dash, _("Dash pattern"), NULL);
103         gtk_widget_show (dsel->dash);
104         gtk_box_pack_start (GTK_BOX (dsel), dsel->dash, FALSE, FALSE, 0);
106         GtkWidget *m = gtk_menu_new ();
107         gtk_widget_show (m);
108         for (int i = 0; dashes[i]; i++) {
109                 GtkWidget *mi = sp_dash_selector_menu_item_new (dsel, dashes[i]);
110                 gtk_widget_show (mi);
111                 gtk_menu_append (GTK_MENU (m), mi);
112         }
113         gtk_option_menu_set_menu (GTK_OPTION_MENU (dsel->dash), m);
115         dsel->offset = gtk_adjustment_new (0.0, 0.0, 10.0, 0.1, 1.0, 1.0);
116         GtkWidget *sb = gtk_spin_button_new (GTK_ADJUSTMENT (dsel->offset), 0.1, 2);
117         gtk_tooltips_set_tip (tt, sb, _("Pattern offset"), NULL);
119         sp_dialog_defocus_on_enter (sb);
120         gtk_widget_show (sb);
121         gtk_box_pack_start (GTK_BOX (dsel), sb, FALSE, FALSE, 0);
122         gtk_signal_connect (dsel->offset, "value_changed", GTK_SIGNAL_FUNC (sp_dash_selector_offset_value_changed), dsel);
124         gtk_object_set_data (GTK_OBJECT (dsel), "pattern", dashes[0]);
127 GtkWidget *
128 sp_dash_selector_new (Inkscape::XML::Node *drepr)
130         if (!dashes) {
131                 int ndashes = 0;
132                 if (drepr) {
133                         for (Inkscape::XML::Node *dr = drepr->firstChild(); dr; dr = dr->next()) {
134                                 if (!strcmp (dr->name(), "dash"))
135                                         ndashes += 1;
136                         }
137                 }
139                 if (ndashes > 0) {
140                         int pos = 0;
141                         SPStyle *style = sp_style_new ();
142                         dashes = g_new (double *, ndashes + 1);
143                         for (Inkscape::XML::Node *dr = drepr->firstChild(); dr; dr = dr->next()) {
144                                 if (!strcmp (dr->name(), "dash")) {
145                                         sp_style_read_from_repr (style, dr);
146                                         if (style->stroke_dash.n_dash > 0) {
147                                                 dashes[pos] = g_new (double, style->stroke_dash.n_dash + 1);
148                                                 double *d = dashes[pos];
149                                                 int i = 0;
150                                                 for (; i < style->stroke_dash.n_dash; i++) {
151                                                         d[i] = style->stroke_dash.dash[i];
152                                                 }
153                                                 d[i] = -1;
154                                         } else {
155                                                 dashes[pos] = dash_0;
156                                         }
157                                         pos += 1;
158                                 }
159                         }
160                         sp_style_unref (style);
161                         dashes[pos] = NULL;
162                 } else {
163                         dashes = builtin_dashes;
164                 }
165         }
167         GtkWidget *dsel = (GtkWidget*)gtk_type_new (SP_TYPE_DASH_SELECTOR);
169         return dsel;
172 void
173 sp_dash_selector_set_dash (SPDashSelector *dsel, int ndash, double *dash, double offset)
175         int pos = 0;
176         if (ndash > 0) {
177                 double delta = 0.0;
178                 for (int i = 0; i < ndash; i++)
179                         delta += dash[i];
180                 delta /= 1000.0;
182                 for (int i = 0; dashes[i]; i++) {
183                         double *pattern = dashes[i];
184                         int np = 0;
185                         while (pattern[np] >= 0.0)
186                                 np += 1;
187                         if (np == ndash) {
188                                 int j;
189                                 for (j = 0; j < ndash; j++) {
190                                         if (!NR_DF_TEST_CLOSE (dash[j], pattern[j], delta))
191                                                 break;
192                                 }
193                                 if (j == ndash) {
194                                         pos = i;
195                                         break;
196                                 }
197                         }
198                 }
199         }
201         gtk_object_set_data (GTK_OBJECT (dsel), "pattern", dashes[pos]);
202         gtk_option_menu_set_history (GTK_OPTION_MENU (dsel->dash), pos);
203         gtk_adjustment_set_value (GTK_ADJUSTMENT (dsel->offset), offset);
206 void
207 sp_dash_selector_get_dash (SPDashSelector *dsel, int *ndash, double **dash, double *offset)
209         double *pattern = (double*)gtk_object_get_data (GTK_OBJECT (dsel), "pattern");
211         int nd = 0;
212         while (pattern[nd] >= 0.0)
213                 nd += 1;
215         if (nd > 0) {
216                 if (ndash)
217                         *ndash = nd;
218                 if (dash) {
219                         *dash = g_new (double, nd);
220                         memcpy (*dash, pattern, nd * sizeof (double));
221                 }
222                 if (offset)
223                         *offset = GTK_ADJUSTMENT (dsel->offset)->value;
224         } else {
225                 if (ndash)
226                         *ndash = 0;
227                 if (dash)
228                         *dash = NULL;
229                 if (offset)
230                         *offset = 0.0;
231         }
234 bool
235 all_even_are_zero (double *pattern, int n)
237         for (int i = 0; i < n; i += 2) {
238                 if (pattern[i] != 0)
239                         return false;
240         }
241         return true;
244 bool
245 all_odd_are_zero (double *pattern, int n)
247         for (int i = 1; i < n; i += 2) {
248                 if (pattern[i] != 0)
249                         return false;
250         }
251         return true;
254 static GtkWidget *
255 sp_dash_selector_menu_item_new (SPDashSelector *dsel, double *pattern)
257         GtkWidget *mi = gtk_menu_item_new ();
259         GdkPixmap *pixmap = gdk_pixmap_new (GTK_WIDGET (dsel)->window, DASH_PREVIEW_LENGTH + 4, 16, gdk_visual_get_best_depth ());
260         GdkGC *gc = gdk_gc_new (pixmap);
262         gdk_rgb_gc_set_foreground (gc, 0xffffffff);
263         gdk_draw_rectangle (pixmap, gc, TRUE, 0, 0, DASH_PREVIEW_LENGTH + 4, 16);
265         // FIXME: all of the below twibblering is due to the limitations of gdk_gc_set_dashes (only integers, no zeroes).
266         // Perhaps would make sense to rework this with manually drawn dashes.
268         // Fill in the integer array of pixel-lengths, for display
269         gint8 pixels_i[64];
270         gdouble pixels_d[64];
271         int n_source_dashes = 0;
272         int n_pixel_dashes = 0;
274         signed int i_s, i_p;
275         for (i_s = 0, i_p = 0; pattern[i_s] >= 0.0; i_s ++, i_p ++) {
276                 pixels_d[i_p] = 0.0;
277         }
279         n_source_dashes = i_s;
281         for (i_s = 0, i_p = 0; i_s < n_source_dashes; i_s ++, i_p ++) {
283                 // calculate the pixel length corresponding to the current dash
284                 gdouble pixels = DASH_PREVIEW_WIDTH * pattern[i_s];
286                 if (pixels > 0.0)
287                         pixels_d [i_p] += pixels;
288                 else {
289                         if (i_p >= 1) {
290                                 // dash is zero, skip this element in the array, and set pointer backwards so the next dash is added to the previous
291                                 i_p -= 2;
292                         } else {
293                                 // the first dash is zero; bad luck, gdk cannot start pattern with non-stroke, so we put a 1-pixel stub here
294                                 // (it may turn out not shown, though, see special cases below)
295                                 pixels_d [i_p] = 1.0;
296                         }
297                 }
298         }
300         n_pixel_dashes = i_p;
302         gdouble longest_dash = 0.0;
304         // after summation, convert double dash lengths to ints
305         for (i_p = 0; i_p < n_pixel_dashes; i_p ++) {
306                 pixels_i [i_p] = (gint8) (pixels_d [i_p] + 0.5);
307                 // zero-length dashes are already eliminated, so the <1 dash is short but not zero;
308                 // we approximate it with a one-pixel mark
309                 if (pixels_i [i_p] < 1)
310                         pixels_i [i_p] = 1;
311                 if (i_p % 2 == 0) { // it's a dash
312                         if (pixels_d [i_p] > longest_dash)
313                                 longest_dash = pixels_d [i_p];
314                 }
315         }
317         if (longest_dash > 1e-18 && longest_dash < 0.5) {
318                 // fake "shortening" of one-pixel marks by painting them lighter-than-black
319                 gint rgb = 255 - (gint) (255 * longest_dash / 0.5);
320                 gdk_rgb_gc_set_foreground (gc, SP_RGBA32_U_COMPOSE (rgb, rgb, rgb, rgb));
321         } else {
322                 gdk_rgb_gc_set_foreground (gc, 0x00000000);
323         }
325         if (n_source_dashes > 0) {
326                 // special cases:
327                 if (all_even_are_zero (pattern, n_source_dashes)) {
328                         ; // do not draw anything, only gaps are non-zero
329                 } else if (all_odd_are_zero (pattern, n_source_dashes)) {
330                         // draw solid line, only dashes are non-zero
331                         gdk_gc_set_line_attributes (gc, DASH_PREVIEW_WIDTH,
332                                                                                 GDK_LINE_SOLID, GDK_CAP_BUTT,
333                                                                                 GDK_JOIN_MITER);
334                         gdk_draw_line (pixmap, gc, 4, 8, DASH_PREVIEW_LENGTH, 8);
335                 } else {
336                         // regular pattern with both gaps and dashes non-zero
337                         gdk_gc_set_line_attributes (gc, DASH_PREVIEW_WIDTH,
338                                                                                 GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT,
339                                                                                 GDK_JOIN_MITER);
340                         gdk_gc_set_dashes (gc, 0, pixels_i, n_pixel_dashes);
341                         gdk_draw_line (pixmap, gc, 4, 8, DASH_PREVIEW_LENGTH, 8);
342                 }
343         } else {
344                 // no pattern, draw solid line
345                 gdk_gc_set_line_attributes (gc, DASH_PREVIEW_WIDTH,
346                                                                         GDK_LINE_SOLID, GDK_CAP_BUTT,
347                                                                         GDK_JOIN_MITER);
348                 gdk_draw_line (pixmap, gc, 4, 8, DASH_PREVIEW_LENGTH, 8);
349         }
351         gdk_gc_unref (gc);
353         GtkWidget *px = gtk_pixmap_new (pixmap, NULL);
355         gdk_pixmap_unref (pixmap);
357         gtk_widget_show (px);
358         gtk_container_add (GTK_CONTAINER (mi), px);
360         gtk_object_set_data (GTK_OBJECT (mi), "pattern", pattern);
361         gtk_signal_connect (GTK_OBJECT (mi), "activate", G_CALLBACK (sp_dash_selector_dash_activate), dsel);
363         return mi;
366 static void
367 sp_dash_selector_dash_activate (GtkObject *object, SPDashSelector *dsel)
369         double *pattern = (double*)gtk_object_get_data (object, "pattern");
370         gtk_object_set_data (GTK_OBJECT (dsel), "pattern", pattern);
372         gtk_signal_emit (GTK_OBJECT (dsel), signals[CHANGED]);
375 static void
376 sp_dash_selector_offset_value_changed (GtkAdjustment *adj, SPDashSelector *dsel)
378         gtk_signal_emit (GTK_OBJECT (dsel), signals[CHANGED]);