Code

Updated the gtkmmified fill and stroke dialog with the filter effects
[inkscape.git] / src / ui / dialog / fill-and-stroke.cpp
1 /**
2  * \brief Fill and Stroke dialog, 
3  * based on sp_object_properties_dialog
4  *
5  * Authors:
6  *   Bryce W. Harrington <bryce@bryceharrington.org>
7  *   Gustav Broberg <broberg@kth.se>
8  *
9  * Copyright (C) 2004--2007 Authors
10  *
11  * Released under GNU GPL.  Read the file 'COPYING' for more information.
12  */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #include "desktop-handles.h"
19 #include "desktop-style.h"
20 #include "document.h"
21 #include "fill-and-stroke.h"
22 #include "filter-chemistry.h"
23 #include "inkscape.h"
24 #include "inkscape-stock.h"
25 #include "selection.h"
26 #include "style.h"
27 #include "svg/css-ostringstream.h"
28 #include "verbs.h"
29 #include "xml/repr.h"
30 #include "widgets/icon.h"
33 #include "dialogs/fill-style.h"
34 #include "dialogs/stroke-style.h"
36 #include <widgets/paint-selector.h>
38 namespace Inkscape {
39 namespace UI {
40 namespace Dialog {
42 void on_selection_changed(Inkscape::Application *inkscape, 
43                           Inkscape::Selection *selection,
44                           FillAndStroke *dlg)
45 {
46     dlg->selectionChanged(inkscape, selection);
47 }
49 void on_selection_modified(Inkscape::Application *inkscape, 
50                            Inkscape::Selection *selection, 
51                            guint flags,
52                            FillAndStroke *dlg)
53 {
54     dlg->selectionChanged(inkscape, selection);
55 }
58 FillAndStroke::FillAndStroke() 
59     : Dialog ("dialogs.fillstroke", SP_VERB_DIALOG_FILL_STROKE),
60       _page_fill(1, 1, true, true),
61       _page_stroke_paint(1, 1, true, true),
62       _page_stroke_style(1, 1, true, true),
63       _fe_vbox(false, 0),
64       _fe_alignment(1, 1, 1, 1),
65       _opacity_vbox(false, 0),
66       _opacity_label_box(false, 0),
67       _opacity_label(_("Master _opacity, %"), 0.0, 1.0, true),
68       _opacity_adjustment(100.0, 0.0, 100.0, 1.0, 1.0, 0.0),
69       _opacity_hscale(_opacity_adjustment),
70       _opacity_spin_button(_opacity_adjustment, 0.01, 1),
71       _blocked(false)
72 {
73     Gtk::VBox *vbox = get_vbox();
74     vbox->set_spacing(0);
76     vbox->pack_start(_notebook, true, true);
78     _notebook.append_page(_page_fill, _createPageTabLabel(_("Fill"), INKSCAPE_STOCK_PROPERTIES_FILL_PAGE));
79     _notebook.append_page(_page_stroke_paint, _createPageTabLabel(_("Stroke _paint"), INKSCAPE_STOCK_PROPERTIES_STROKE_PAINT_PAGE));
80     _notebook.append_page(_page_stroke_style, _createPageTabLabel(_("Stroke st_yle"), INKSCAPE_STOCK_PROPERTIES_STROKE_PAGE));
82     _layoutPageFill();
83     _layoutPageStrokePaint();
84     _layoutPageStrokeStyle();
86     // Filter Effects
87     vbox->pack_start(_fe_vbox, false, false, 2);
88     _fe_alignment.set_padding(0, 0, 4, 0);
89     _fe_alignment.add(_fe_cb);
90     _fe_vbox.pack_start(_fe_alignment, false, false, 0);
92     _fe_cb.signal_selection_changed().connect(sigc::mem_fun(*this, &Inkscape::UI::Dialog::FillAndStroke::_blendBlurValueChanged));
93     _fe_cb.signal_blend_blur_changed().connect(sigc::mem_fun(*this, &Inkscape::UI::Dialog::FillAndStroke::_blendBlurValueChanged));
94     
95     // Opacity
96     vbox->pack_start(_opacity_vbox, false, false, 2);
97     _opacity_label_box.pack_start(_opacity_label, false, false, 4);
98     _opacity_vbox.pack_start(_opacity_label_box, false, false, 0);
99     _opacity_vbox.pack_start(_opacity_hbox, false, false, 0);
100     _opacity_hbox.pack_start(_opacity_hscale, true, true, 4);
101     _opacity_hbox.pack_start(_opacity_spin_button, false, false, 0);
102     _opacity_hscale.set_draw_value(false);
103     _opacity_adjustment.signal_value_changed().connect(sigc::mem_fun(*this, &Inkscape::UI::Dialog::FillAndStroke::_opacityValueChanged));
105     // these callbacks are only for the blur and master opacity update; the tabs above take care of themselves
106     g_signal_connect ( G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (on_selection_changed), this );
107     g_signal_connect ( G_OBJECT (INKSCAPE), "change_subselection", G_CALLBACK (on_selection_changed), this );
108     g_signal_connect ( G_OBJECT (INKSCAPE), "modify_selection", G_CALLBACK (on_selection_modified), this );
109     g_signal_connect ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (on_selection_changed), this );
111     selectionChanged(INKSCAPE, sp_desktop_selection(SP_ACTIVE_DESKTOP));
113     show_all_children();
116 FillAndStroke::~FillAndStroke() 
120 void
121 FillAndStroke::_layoutPageFill()
123     Gtk::Widget *fs = manage(Glib::wrap(sp_fill_style_widget_new()));
124     _page_fill.table().attach(*fs, 0, 1, 0, 1);
127 void
128 FillAndStroke::_layoutPageStrokePaint()
130     Gtk::Widget *ssp = manage(Glib::wrap(sp_stroke_style_paint_widget_new()));
131     _page_stroke_paint.table().attach(*ssp, 0, 1, 0, 1);
134 void
135 FillAndStroke::_layoutPageStrokeStyle()
137     Gtk::Widget *ssl = manage(Glib::wrap(sp_stroke_style_line_widget_new()));
138     _page_stroke_style.table().attach(*ssl, 0, 1, 0, 1);
141 void
142 FillAndStroke::_blendBlurValueChanged()
144     if (_blocked)
145         return;
146     _blocked = true;
148     //get desktop
149     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
150     if (!desktop) {
151         return;
152     }
154     // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
155     sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
156     
157     //get current selection
158     Inkscape::Selection *selection = sp_desktop_selection (desktop);
160     NR::Maybe<NR::Rect> bbox = selection->bounds();
161     if (!bbox) {
162         return;
163     }
164     //get list of selected items
165     GSList const *items = selection->itemList();
166     //get current document
167     SPDocument *document = sp_desktop_document (desktop);
169     double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
171     const Glib::ustring blendmode = _fe_cb.get_blend_mode();
172     double radius = _fe_cb.get_blur_value() * perimeter / 400;
174     SPFilter *filter = _fe_cb.get_selected_filter();
175     const bool remfilter = (blendmode == "normal" && radius == 0) || (blendmode == "filter" && !filter);
176         
177     if(blendmode != "filter" || filter) {
178         //apply created filter to every selected item
179         for (GSList const *i = items; i != NULL; i = i->next) {
180             SPItem * item = SP_ITEM(i->data);
181             SPStyle *style = SP_OBJECT_STYLE(item);
182             g_assert(style != NULL);
183             
184             if(remfilter) {
185                 remove_filter (item, false);
186             }
187             else {
188                 if(blendmode != "filter")
189                     filter = new_filter_simple_from_item(document, item, blendmode.c_str(), radius);
190                 sp_style_set_property_url (SP_OBJECT(item), "filter", SP_OBJECT(filter), false);
191             }
192             
193             //request update
194             SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
195                                                     SP_OBJECT_STYLE_MODIFIED_FLAG ));
196         }
197     }
199     sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:blur", SP_VERB_DIALOG_FILL_STROKE,  _("Change blur"));
201     // resume interruptibility
202     sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
204     _blocked = false;
207 void
208 FillAndStroke::_opacityValueChanged()
210     if (_blocked)
211         return;
212     _blocked = true;
214     // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
215     // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
216     // (though this only fixes the crash, not the multiple change events)
217     sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(SP_ACTIVE_DESKTOP), 0);
219     SPCSSAttr *css = sp_repr_css_attr_new ();
221     Inkscape::CSSOStringStream os;
222     os << CLAMP (_opacity_adjustment.get_value() / 100, 0.0, 1.0);
223     sp_repr_css_set_property (css, "opacity", os.str().c_str());
225     sp_desktop_set_style (SP_ACTIVE_DESKTOP, css);
227     sp_repr_css_attr_unref (css);
229     sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:opacity", SP_VERB_DIALOG_FILL_STROKE, 
230                             _("Change opacity"));
232     // resume interruptibility
233     sp_canvas_end_forced_full_redraws(sp_desktop_canvas(SP_ACTIVE_DESKTOP));
235     _blocked = false;
238 void
239 FillAndStroke::selectionChanged(Inkscape::Application *inkscape,
240                                 Inkscape::Selection *selection)
242     if (_blocked)
243         return;
244     _blocked = true;
246     // create temporary style
247     SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
248     // query style from desktop into it. This returns a result flag and fills query with the style of subselection, if any, or selection
249     int result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
251     switch (result) {
252         case QUERY_STYLE_NOTHING:
253             _opacity_hbox.set_sensitive(false);
254             // gtk_widget_set_sensitive (opa, FALSE);
255             break;
256         case QUERY_STYLE_SINGLE:
257         case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
258         case QUERY_STYLE_MULTIPLE_SAME: 
259             _opacity_hbox.set_sensitive(true);
260             _opacity_adjustment.set_value(100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
261             break;
262     }
264     //query now for current filter mode and average blurring of selection
265     const int blend_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLEND);
266     switch(blend_result) {
267         case QUERY_STYLE_NOTHING:
268             _fe_cb.set_sensitive(false);
269             break;
270         case QUERY_STYLE_SINGLE:
271         case QUERY_STYLE_MULTIPLE_SAME:
272             _fe_cb.set_blend_mode(query->filter_blend_mode.value);
273             _fe_cb.set_sensitive(true);
274             break;
275         case QUERY_STYLE_MULTIPLE_DIFFERENT:
276             // TODO: set text
277             _fe_cb.set_sensitive(false);
278             break;
279     }
281     if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
282         int blur_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLUR);
283         switch (blur_result) {
284             case QUERY_STYLE_NOTHING: //no blurring
285                 _fe_cb.set_blur_sensitive(false);
286                 break;
287             case QUERY_STYLE_SINGLE:
288             case QUERY_STYLE_MULTIPLE_AVERAGED:
289             case QUERY_STYLE_MULTIPLE_SAME: 
290                 NR::Maybe<NR::Rect> bbox = sp_desktop_selection(SP_ACTIVE_DESKTOP)->bounds();
291                 if (bbox) {
292                     double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
293                     _fe_cb.set_blur_sensitive(true);
294                     //update blur widget value
295                     float radius = query->filter_gaussianBlur_deviation.value;
296                     float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
297                     _fe_cb.set_blur_value(percent);
298                 }
299                 break;
300         }
301     }
302     
303     sp_style_unref(query);
305     _blocked = false;
308 Gtk::HBox&
309 FillAndStroke::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
311     Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
312     _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
313                                                        label_image)));
315     Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
316     _tab_label_box->pack_start(*_tab_label);
317     _tab_label_box->show_all();
319     return *_tab_label_box;
322 } // namespace Dialog
323 } // namespace UI
324 } // namespace Inkscape
326 /*
327   Local Variables:
328   mode:c++
329   c-file-style:"stroustrup"
330   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
331   indent-tabs-mode:nil
332   fill-column:99
333   End:
334 */
335 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :