Code

search button and description label add to import from ocal dialog
[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_blend_blur_changed().connect(sigc::mem_fun(*this, &Inkscape::UI::Dialog::FillAndStroke::_blendBlurValueChanged));
93     
94     // Opacity
95     vbox->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();
115 FillAndStroke::~FillAndStroke() 
119 void
120 FillAndStroke::_layoutPageFill()
122     Gtk::Widget *fs = manage(Glib::wrap(sp_fill_style_widget_new()));
123     _page_fill.table().attach(*fs, 0, 1, 0, 1);
126 void
127 FillAndStroke::_layoutPageStrokePaint()
129     Gtk::Widget *ssp = manage(Glib::wrap(sp_stroke_style_paint_widget_new()));
130     _page_stroke_paint.table().attach(*ssp, 0, 1, 0, 1);
133 void
134 FillAndStroke::_layoutPageStrokeStyle()
136     Gtk::Widget *ssl = manage(Glib::wrap(sp_stroke_style_line_widget_new()));
137     _page_stroke_style.table().attach(*ssl, 0, 1, 0, 1);
140 void
141 FillAndStroke::_blendBlurValueChanged()
143     if (_blocked)
144         return;
145     _blocked = true;
147     //get desktop
148     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
149     if (!desktop) {
150         return;
151     }
153     // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
154     sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
155     
156     //get current selection
157     Inkscape::Selection *selection = sp_desktop_selection (desktop);
159     NR::Maybe<NR::Rect> bbox = selection->bounds();
160     if (!bbox) {
161         return;
162     }
163     //get list of selected items
164     GSList const *items = selection->itemList();
165     //get current document
166     SPDocument *document = sp_desktop_document (desktop);
168     double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
170     const Glib::ustring blendmode = _fe_cb.get_blend_mode();
171     double radius = _fe_cb.get_blur_value() * perimeter / 400;
173     SPFilter *filter = 0;
174     const bool remfilter = (blendmode == "normal" && radius == 0) || (blendmode == "filter" && !filter);
175         
176     if(blendmode != "filter" || filter) {
177         //apply created filter to every selected item
178         for (GSList const *i = items; i != NULL; i = i->next) {
179             SPItem * item = SP_ITEM(i->data);
180             SPStyle *style = SP_OBJECT_STYLE(item);
181             g_assert(style != NULL);
182             
183             if(remfilter) {
184                 remove_filter (item, false);
185             }
186             else {
187                 if(blendmode != "filter")
188                     filter = new_filter_simple_from_item(document, item, blendmode.c_str(), radius);
189                 sp_style_set_property_url (SP_OBJECT(item), "filter", SP_OBJECT(filter), false);
190             }
191             
192             //request update
193             SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
194                                                     SP_OBJECT_STYLE_MODIFIED_FLAG ));
195         }
196     }
198     sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:blur", SP_VERB_DIALOG_FILL_STROKE,  _("Change blur"));
200     // resume interruptibility
201     sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
203     _blocked = false;
206 void
207 FillAndStroke::_opacityValueChanged()
209     if (_blocked)
210         return;
211     _blocked = true;
213     // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
214     // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
215     // (though this only fixes the crash, not the multiple change events)
216     sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(SP_ACTIVE_DESKTOP), 0);
218     SPCSSAttr *css = sp_repr_css_attr_new ();
220     Inkscape::CSSOStringStream os;
221     os << CLAMP (_opacity_adjustment.get_value() / 100, 0.0, 1.0);
222     sp_repr_css_set_property (css, "opacity", os.str().c_str());
224     sp_desktop_set_style (SP_ACTIVE_DESKTOP, css);
226     sp_repr_css_attr_unref (css);
228     sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:opacity", SP_VERB_DIALOG_FILL_STROKE, 
229                             _("Change opacity"));
231     // resume interruptibility
232     sp_canvas_end_forced_full_redraws(sp_desktop_canvas(SP_ACTIVE_DESKTOP));
234     _blocked = false;
237 void
238 FillAndStroke::selectionChanged(Inkscape::Application *inkscape,
239                                 Inkscape::Selection *selection)
241     if (_blocked)
242         return;
243     _blocked = true;
245     // create temporary style
246     SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
247     // query style from desktop into it. This returns a result flag and fills query with the style of subselection, if any, or selection
248     int result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
250     switch (result) {
251         case QUERY_STYLE_NOTHING:
252             _opacity_hbox.set_sensitive(false);
253             // gtk_widget_set_sensitive (opa, FALSE);
254             break;
255         case QUERY_STYLE_SINGLE:
256         case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
257         case QUERY_STYLE_MULTIPLE_SAME: 
258             _opacity_hbox.set_sensitive(true);
259             _opacity_adjustment.set_value(100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
260             break;
261     }
263     //query now for current filter mode and average blurring of selection
264     const int blend_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLEND);
265     switch(blend_result) {
266         case QUERY_STYLE_NOTHING:
267             _fe_cb.set_sensitive(false);
268             break;
269         case QUERY_STYLE_SINGLE:
270         case QUERY_STYLE_MULTIPLE_SAME:
271             _fe_cb.set_blend_mode(query->filter_blend_mode.value);
272             _fe_cb.set_sensitive(true);
273             break;
274         case QUERY_STYLE_MULTIPLE_DIFFERENT:
275             // TODO: set text
276             _fe_cb.set_sensitive(false);
277             break;
278     }
280     if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
281         int blur_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLUR);
282         switch (blur_result) {
283             case QUERY_STYLE_NOTHING: //no blurring
284                 _fe_cb.set_blur_sensitive(false);
285                 break;
286             case QUERY_STYLE_SINGLE:
287             case QUERY_STYLE_MULTIPLE_AVERAGED:
288             case QUERY_STYLE_MULTIPLE_SAME: 
289                 NR::Maybe<NR::Rect> bbox = sp_desktop_selection(SP_ACTIVE_DESKTOP)->bounds();
290                 if (bbox) {
291                     double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
292                     _fe_cb.set_blur_sensitive(true);
293                     //update blur widget value
294                     float radius = query->filter_gaussianBlur_deviation.value;
295                     float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
296                     _fe_cb.set_blur_value(percent);
297                 }
298                 break;
299         }
300     }
301     
302     sp_style_unref(query);
304     _blocked = false;
307 Gtk::HBox&
308 FillAndStroke::_createPageTabLabel(const Glib::ustring& label, const char *label_image)
310     Gtk::HBox *_tab_label_box = manage(new Gtk::HBox(false, 0));
311     _tab_label_box->pack_start(*Glib::wrap(sp_icon_new(Inkscape::ICON_SIZE_DECORATION,
312                                                        label_image)));
314     Gtk::Label *_tab_label = manage(new Gtk::Label(label, true));
315     _tab_label_box->pack_start(*_tab_label);
316     _tab_label_box->show_all();
318     return *_tab_label_box;
321 } // namespace Dialog
322 } // namespace UI
323 } // namespace Inkscape
325 /*
326   Local Variables:
327   mode:c++
328   c-file-style:"stroustrup"
329   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
330   indent-tabs-mode:nil
331   fill-column:99
332   End:
333 */
334 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :