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 : UI::Widget::Panel ("", "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(_("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::Box *contents = _getContents();
74 contents->set_spacing(0);
76 contents->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 contents->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_blend_blur_changed().connect(sigc::mem_fun(*this, &Inkscape::UI::Dialog::FillAndStroke::_blendBlurValueChanged));
94 // Opacity
95 contents->pack_start(_opacity_vbox, false, false, 2);
96 _opacity_label_box.pack_start(_opacity_label, false, false, 4);
97 _opacity_vbox.pack_start(_opacity_label_box, false, false, 0);
98 _opacity_vbox.pack_start(_opacity_hbox, false, false, 0);
99 _opacity_hbox.pack_start(_opacity_hscale, true, true, 4);
100 _opacity_hbox.pack_start(_opacity_spin_button, false, false, 0);
101 _opacity_hscale.set_draw_value(false);
102 _opacity_adjustment.signal_value_changed().connect(sigc::mem_fun(*this, &Inkscape::UI::Dialog::FillAndStroke::_opacityValueChanged));
104 // these callbacks are only for the blur and master opacity update; the tabs above take care of themselves
105 g_signal_connect ( G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (on_selection_changed), this );
106 g_signal_connect ( G_OBJECT (INKSCAPE), "change_subselection", G_CALLBACK (on_selection_changed), this );
107 g_signal_connect ( G_OBJECT (INKSCAPE), "modify_selection", G_CALLBACK (on_selection_modified), this );
108 g_signal_connect ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (on_selection_changed), this );
110 selectionChanged(INKSCAPE, sp_desktop_selection(SP_ACTIVE_DESKTOP));
112 show_all_children();
113 }
115 FillAndStroke::~FillAndStroke()
116 {
117 }
119 void
120 FillAndStroke::_layoutPageFill()
121 {
122 Gtk::Widget *fs = manage(Glib::wrap(sp_fill_style_widget_new()));
123 _page_fill.table().attach(*fs, 0, 1, 0, 1);
124 }
126 void
127 FillAndStroke::_layoutPageStrokePaint()
128 {
129 Gtk::Widget *ssp = manage(Glib::wrap(sp_stroke_style_paint_widget_new()));
130 _page_stroke_paint.table().attach(*ssp, 0, 1, 0, 1);
131 }
133 void
134 FillAndStroke::_layoutPageStrokeStyle()
135 {
136 Gtk::Widget *ssl = manage(Glib::wrap(sp_stroke_style_line_widget_new()));
137 _page_stroke_style.table().attach(*ssl, 0, 1, 0, 1);
138 }
140 void
141 FillAndStroke::showPageFill()
142 {
143 present();
144 _notebook.set_current_page(0);
145 }
147 void
148 FillAndStroke::showPageStrokePaint()
149 {
150 present();
151 _notebook.set_current_page(1);
152 }
154 void
155 FillAndStroke::showPageStrokeStyle()
156 {
157 present();
158 _notebook.set_current_page(2);
159 }
162 void
163 FillAndStroke::_blendBlurValueChanged()
164 {
165 if (_blocked)
166 return;
167 _blocked = true;
169 //get desktop
170 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
171 if (!desktop) {
172 return;
173 }
175 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
176 sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
178 //get current selection
179 Inkscape::Selection *selection = sp_desktop_selection (desktop);
181 NR::Maybe<NR::Rect> bbox = selection->bounds();
182 if (!bbox) {
183 return;
184 }
185 //get list of selected items
186 GSList const *items = selection->itemList();
187 //get current document
188 SPDocument *document = sp_desktop_document (desktop);
190 double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
192 const Glib::ustring blendmode = _fe_cb.get_blend_mode();
193 double radius = _fe_cb.get_blur_value() * perimeter / 400;
195 SPFilter *filter = 0;
196 const bool remfilter = (blendmode == "normal" && radius == 0) || (blendmode == "filter" && !filter);
198 if(blendmode != "filter" || filter) {
199 //apply created filter to every selected item
200 for (GSList const *i = items; i != NULL; i = i->next) {
201 SPItem * item = SP_ITEM(i->data);
202 SPStyle *style = SP_OBJECT_STYLE(item);
203 g_assert(style != NULL);
205 if(remfilter) {
206 remove_filter (item, false);
207 }
208 else {
209 if(blendmode != "filter")
210 filter = new_filter_simple_from_item(document, item, blendmode.c_str(), radius);
211 sp_style_set_property_url (SP_OBJECT(item), "filter", SP_OBJECT(filter), false);
212 }
214 //request update
215 SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
216 SP_OBJECT_STYLE_MODIFIED_FLAG ));
217 }
218 }
220 sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:blur", SP_VERB_DIALOG_FILL_STROKE, _("Change blur"));
222 // resume interruptibility
223 sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
225 _blocked = false;
226 }
228 void
229 FillAndStroke::_opacityValueChanged()
230 {
231 if (_blocked)
232 return;
233 _blocked = true;
235 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
236 // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
237 // (though this only fixes the crash, not the multiple change events)
238 sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(SP_ACTIVE_DESKTOP), 0);
240 SPCSSAttr *css = sp_repr_css_attr_new ();
242 Inkscape::CSSOStringStream os;
243 os << CLAMP (_opacity_adjustment.get_value() / 100, 0.0, 1.0);
244 sp_repr_css_set_property (css, "opacity", os.str().c_str());
246 sp_desktop_set_style (SP_ACTIVE_DESKTOP, css);
248 sp_repr_css_attr_unref (css);
250 sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:opacity", SP_VERB_DIALOG_FILL_STROKE,
251 _("Change opacity"));
253 // resume interruptibility
254 sp_canvas_end_forced_full_redraws(sp_desktop_canvas(SP_ACTIVE_DESKTOP));
256 _blocked = false;
257 }
259 void
260 FillAndStroke::selectionChanged(Inkscape::Application */*inkscape*/,
261 Inkscape::Selection */*selection*/)
262 {
263 if (_blocked)
264 return;
265 _blocked = true;
267 // create temporary style
268 SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
269 // query style from desktop into it. This returns a result flag and fills query with the style of subselection, if any, or selection
270 int result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
272 switch (result) {
273 case QUERY_STYLE_NOTHING:
274 _opacity_hbox.set_sensitive(false);
275 // gtk_widget_set_sensitive (opa, FALSE);
276 break;
277 case QUERY_STYLE_SINGLE:
278 case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
279 case QUERY_STYLE_MULTIPLE_SAME:
280 _opacity_hbox.set_sensitive(true);
281 _opacity_adjustment.set_value(100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
282 break;
283 }
285 //query now for current filter mode and average blurring of selection
286 const int blend_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLEND);
287 switch(blend_result) {
288 case QUERY_STYLE_NOTHING:
289 _fe_cb.set_sensitive(false);
290 break;
291 case QUERY_STYLE_SINGLE:
292 case QUERY_STYLE_MULTIPLE_SAME:
293 _fe_cb.set_blend_mode(query->filter_blend_mode.value);
294 _fe_cb.set_sensitive(true);
295 break;
296 case QUERY_STYLE_MULTIPLE_DIFFERENT:
297 // TODO: set text
298 _fe_cb.set_sensitive(false);
299 break;
300 }
302 if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
303 int blur_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLUR);
304 switch (blur_result) {
305 case QUERY_STYLE_NOTHING: //no blurring
306 _fe_cb.set_blur_sensitive(false);
307 break;
308 case QUERY_STYLE_SINGLE:
309 case QUERY_STYLE_MULTIPLE_AVERAGED:
310 case QUERY_STYLE_MULTIPLE_SAME:
311 NR::Maybe<NR::Rect> bbox = sp_desktop_selection(SP_ACTIVE_DESKTOP)->bounds();
312 if (bbox) {
313 double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
314 _fe_cb.set_blur_sensitive(true);
315 //update blur widget value
316 float radius = query->filter_gaussianBlur_deviation.value;
317 float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
318 _fe_cb.set_blur_value(percent);
319 }
320 break;
321 }
322 }
324 sp_style_unref(query);
326 _blocked = false;
327 }
329 Gtk::HBox&
330 FillAndStroke::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
331 {
332 Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
333 _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
334 label_image)));
336 Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
337 _tab_label_box->pack_start(*_tab_label);
338 _tab_label_box->show_all();
340 return *_tab_label_box;
341 }
343 } // namespace Dialog
344 } // namespace UI
345 } // namespace Inkscape
347 /*
348 Local Variables:
349 mode:c++
350 c-file-style:"stroustrup"
351 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
352 indent-tabs-mode:nil
353 fill-column:99
354 End:
355 */
356 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :