Code

Merge from fe-moved
[inkscape.git] / src / widgets / dash-selector.cpp
1 #define __SP_DASH_SELECTOR_NEW_C__
3 /** @file
4  * @brief Option menu for selecting dash patterns - implementation
5  */
6 /* Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *   Maximilian Albert <maximilian.albert@gmail.com>
10  *
11  * Copyright (C) 2002 Lauris Kaplinski
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #define DASH_PREVIEW_WIDTH 2
17 #define DASH_PREVIEW_LENGTH 80
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include <cstring>
24 #include <string>
25 #include <libnr/nr-macros.h>
26 #include <gtk/gtk.h>
27 #include <glibmm/i18n.h>
29 #include "style.h"
30 #include "dialogs/dialog-events.h"
31 #include "preferences.h"
33 #include <gtkmm/optionmenu.h>
34 #include <gtkmm/adjustment.h>
35 #include <gtkmm/spinbutton.h>
38 #include "dash-selector.h"
40 gchar const *const SPDashSelector::_prefs_path = "/palette/dashes";
42 static double dash_0[] = {-1.0};
43 static double dash_1_1[] = {1.0, 1.0, -1.0};
44 static double dash_2_1[] = {2.0, 1.0, -1.0};
45 static double dash_4_1[] = {4.0, 1.0, -1.0};
46 static double dash_1_2[] = {1.0, 2.0, -1.0};
47 static double dash_1_4[] = {1.0, 4.0, -1.0};
49 static double *builtin_dashes[] = {dash_0, dash_1_1, dash_2_1, dash_4_1, dash_1_2, dash_1_4, NULL};
51 static double **dashes = NULL;
53 static void sp_dash_selector_menu_item_image_realize(Gtk::Image *px, double *pattern);
55 SPDashSelector::SPDashSelector() {
56     // TODO: find something more sensible here!!
57     init_dashes();
59     Gtk::Tooltips *tt = new Gtk::Tooltips();
61     dash = new Gtk::OptionMenu();
62     tt->set_tip(*dash, _("Dash pattern"));
63     dash->show();
64     this->pack_start(*dash, false, false, 0);
66     Gtk::Menu *m = new Gtk::Menu();
67     m->show();
68     for (int i = 0; dashes[i]; i++) {
69         Gtk::MenuItem *mi = menu_item_new(dashes[i]);
70         mi->show();
71         m->append(*mi);
72     }
73     dash->set_menu(*m);
75     offset = new Gtk::Adjustment(0.0, 0.0, 10.0, 0.1, 1.0, 1.0);
76     Gtk::SpinButton *sb = new Gtk::SpinButton(*offset, 0.1, 2);
77     tt->set_tip(*sb, _("Pattern offset"));
79     sp_dialog_defocus_on_enter_cpp(sb);
80     sb->show();
81     this->pack_start(*sb, false, false, 0);
82     offset->signal_value_changed().connect(sigc::mem_fun(*this, &SPDashSelector::offset_value_changed));
84     this->set_data("pattern", dashes[0]);
85 }
87 SPDashSelector::~SPDashSelector() {
88     // FIXME: for some reason this doesn't get called; does the call to manage() in
89     // sp_stroke_style_line_widget_new() not processed correctly?
90     delete dash;
91     delete offset;
92 }
94 void
95 SPDashSelector::init_dashes() {
96     if (!dashes) {
97         
98         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
99         std::vector<Glib::ustring> dash_prefs = prefs->getAllDirs(_prefs_path);
100         
101         if (!dash_prefs.empty()) {
102             int pos = 0;
103             SPStyle *style = sp_style_new (NULL);
104             dashes = g_new (double *, dash_prefs.size() + 1);
105             
106             for (std::vector<Glib::ustring>::iterator i = dash_prefs.begin(); i != dash_prefs.end(); ++i) {
107                 sp_style_read_from_prefs(style, *i);
108                 
109                 if (style->stroke_dash.n_dash > 0) {
110                     dashes[pos] = g_new (double, style->stroke_dash.n_dash + 1);
111                     double *d = dashes[pos];
112                     int i = 0;
113                     for (; i < style->stroke_dash.n_dash; i++) {
114                         d[i] = style->stroke_dash.dash[i];
115                     }
116                     d[i] = -1;
117                 } else {
118                     dashes[pos] = dash_0;
119                 }
120                 pos += 1;
121             }
122         } else {
123             dashes = builtin_dashes;
124         }
125     }
128 void
129 SPDashSelector::set_dash (int ndash, double *dash, double o)
131     int pos = 0;
132     if (ndash > 0) {
133         double delta = 0.0;
134         for (int i = 0; i < ndash; i++)
135             delta += dash[i];
136         delta /= 1000.0;
138         for (int i = 0; dashes[i]; i++) {
139             double *pattern = dashes[i];
140             int np = 0;
141             while (pattern[np] >= 0.0)
142                 np += 1;
143             if (np == ndash) {
144                 int j;
145                 for (j = 0; j < ndash; j++) {
146                     if (!NR_DF_TEST_CLOSE (dash[j], pattern[j], delta))
147                         break;
148                 }
149                 if (j == ndash) {
150                     pos = i;
151                     break;
152                 }
153             }
154         }
155     }
157     this->set_data("pattern", dashes[pos]);
158     this->dash->set_history(pos);
159     this->offset->set_value(o);
162 void
163 SPDashSelector::get_dash(int *ndash, double **dash, double *off)
165     double *pattern = (double*) this->get_data("pattern");
167     int nd = 0;
168     while (pattern[nd] >= 0.0)
169         nd += 1;
171     if (nd > 0) {
172         if (ndash)
173             *ndash = nd;
174         if (dash) {
175             *dash = g_new (double, nd);
176             memcpy (*dash, pattern, nd * sizeof (double));
177         }
178         if (off)
179             *off = offset->get_value();
180     } else {
181         if (ndash)
182             *ndash = 0;
183         if (dash)
184             *dash = NULL;
185         if (off)
186             *off = 0.0;
187     }
190 Gtk::MenuItem *
191 SPDashSelector::menu_item_new(double *pattern)
193     Gtk::MenuItem *mi = new Gtk::MenuItem();
194     Gtk::Image *px = new Gtk::Image();
196     px->show();
197     mi->add(*px);
199     mi->set_data("pattern", pattern);
200     mi->set_data("px", px);
201     mi->signal_activate().connect(sigc::bind(sigc::mem_fun(*this, &SPDashSelector::dash_activate), mi));
203     px->signal_realize().connect(sigc::bind(sigc::ptr_fun(&sp_dash_selector_menu_item_image_realize), px, pattern));
205     return mi;
208 static bool
209 all_even_are_zero (double *pattern, int n)
211         for (int i = 0; i < n; i += 2) {
212                 if (pattern[i] != 0)
213                         return false;
214         }
215         return true;
218 static bool
219 all_odd_are_zero (double *pattern, int n)
221         for (int i = 1; i < n; i += 2) {
222                 if (pattern[i] != 0)
223                         return false;
224         }
225         return true;
228 static void sp_dash_selector_menu_item_image_realize(Gtk::Image *px, double *pattern) {
229     Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create(px->get_window(), DASH_PREVIEW_LENGTH + 4, 16, -1);
230     Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(pixmap);
232     gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
233     pixmap->draw_rectangle(gc, true, 0, 0, DASH_PREVIEW_LENGTH + 4, 16);
235     // FIXME: all of the below twibblering is due to the limitations of gdk_gc_set_dashes (only integers, no zeroes).
236     // Perhaps would make sense to rework this with manually drawn dashes.
238     // Fill in the integer array of pixel-lengths, for display
239     gint8 pixels_i[64];
240     gdouble pixels_d[64];
241     int n_source_dashes = 0;
242     int n_pixel_dashes = 0;
244     signed int i_s, i_p;
245     for (i_s = 0, i_p = 0; pattern[i_s] >= 0.0; i_s ++, i_p ++) {
246         pixels_d[i_p] = 0.0;
247     }
249     n_source_dashes = i_s;
251     for (i_s = 0, i_p = 0; i_s < n_source_dashes; i_s ++, i_p ++) {
252         // calculate the pixel length corresponding to the current dash
253         gdouble pixels = DASH_PREVIEW_WIDTH * pattern[i_s];
255         if (pixels > 0.0)
256             pixels_d [i_p] += pixels;
257         else {
258             if (i_p >= 1) {
259                 // dash is zero, skip this element in the array, and set pointer backwards
260                 // so the next dash is added to the previous
261                 i_p -= 2;
262             } else {
263                 // the first dash is zero; bad luck, gdk cannot start pattern with non-stroke, so we
264                 // put a 1-pixel stub here (it may turn out not shown, though, see special cases below)
265                 pixels_d [i_p] = 1.0;
266             }
267         }
268     }
270     n_pixel_dashes = i_p;
272     gdouble longest_dash = 0.0;
274     // after summation, convert double dash lengths to ints
275     for (i_p = 0; i_p < n_pixel_dashes; i_p ++) {
276         pixels_i [i_p] = (gint8) (pixels_d [i_p] + 0.5);
277         // zero-length dashes are already eliminated, so the <1 dash is short but not zero;
278         // we approximate it with a one-pixel mark
279         if (pixels_i [i_p] < 1)
280             pixels_i [i_p] = 1;
281         if (i_p % 2 == 0) { // it's a dash
282             if (pixels_d [i_p] > longest_dash)
283                 longest_dash = pixels_d [i_p];
284         }
285     }
287     Gdk::Color color;
288     if (longest_dash > 1e-18 && longest_dash < 0.5) {
289         // fake "shortening" of one-pixel marks by painting them lighter-than-black
290         gint rgb = 0xffff - (gint) (0xffff * longest_dash / 0.5);
291         color.set_rgb(rgb, rgb, rgb);
292         gc->set_rgb_fg_color(color);
293     } else {
294         color.set_rgb(0, 0, 0);
295         gc->set_rgb_fg_color(color);
296     }
298     if (n_source_dashes > 0) {
299         // special cases:
300         if (all_even_are_zero (pattern, n_source_dashes)) {
301             ; // do not draw anything, only gaps are non-zero
302         } else if (all_odd_are_zero (pattern, n_source_dashes)) {
303             // draw solid line, only dashes are non-zero
304             gc->set_line_attributes(DASH_PREVIEW_WIDTH,
305                                     Gdk::LINE_SOLID, Gdk::CAP_BUTT,
306                                     Gdk::JOIN_MITER);
307             pixmap->draw_line(gc, 4, 8, DASH_PREVIEW_LENGTH, 8);
308         } else {
309             // regular pattern with both gaps and dashes non-zero
310             gc->set_line_attributes(DASH_PREVIEW_WIDTH, Gdk::LINE_ON_OFF_DASH, Gdk::CAP_BUTT, Gdk::JOIN_MITER);
311             gc->set_dashes(0, pixels_i, n_pixel_dashes);
312             pixmap->draw_line(gc, 4, 8, DASH_PREVIEW_LENGTH, 8);
313         }
314     } else {
315         // no pattern, draw solid line
316         gc->set_line_attributes(DASH_PREVIEW_WIDTH, Gdk::LINE_SOLID, Gdk::CAP_BUTT, Gdk::JOIN_MITER);
317         pixmap->draw_line(gc, 4, 8, DASH_PREVIEW_LENGTH, 8);
318     }
320     Glib::RefPtr<Gdk::Bitmap> null_ptr;
321     px->set(pixmap, null_ptr);
324 void
325 SPDashSelector::dash_activate (Gtk::MenuItem *mi)
327     double *pattern = (double*) mi->get_data("pattern");
328     this->set_data ("pattern", pattern);
330     changed_signal.emit();
334 void
335 SPDashSelector::offset_value_changed()
337     changed_signal.emit();
340 /*
341   Local Variables:
342   mode:c++
343   c-file-style:"stroustrup"
344   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
345   indent-tabs-mode:nil
346   fill-column:99
347   End:
348 */
349 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :