Code

fix 1767940
[inkscape.git] / src / dialogs / object-properties.cpp
1 #define __OBJECT_PROPERTIES_C__
3 /**
4  * \brief  Fill, stroke, and stroke style dialog
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Frank Felfe <innerspace@iname.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *   Johan Engelen <goejendaagh@zonnet.nl>
11  *
12  * Copyright (C) 1999-2006 authors
13  * Copyright (C) 2001-2002 Ximian, Inc.
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include <gtk/gtk.h>
24 #include <glibmm/i18n.h>
25 #include "helper/window.h"
26 #include "widgets/sp-widget.h"
27 #include "widgets/icon.h"
28 #include "desktop.h"
29 #include "macros.h"
30 #include "inkscape.h"
31 #include "fill-style.h"
32 #include "stroke-style.h"
33 #include "dialog-events.h"
34 #include "verbs.h"
35 #include "interface.h"
36 #include "style.h"
37 #include "inkscape-stock.h"
38 #include "prefs-utils.h"
39 #include "svg/css-ostringstream.h"
40 #include "sp-gaussian-blur.h"
41 #include "sp-filter.h"
42 #include "filter-chemistry.h"
43 #include "desktop-handles.h"
44 #include "desktop-style.h"
45 #include "document.h"
46 #include "document-private.h"
47 #include <selection.h>
48 #include <ui/dialog/dialog-manager.h>
49 #include "ui/widget/filter-effect-chooser.h"
50 #include "xml/repr.h"
51 #include "display/sp-canvas.h"
53 #define MIN_ONSCREEN_DISTANCE 50
55 static GtkWidget *dlg = NULL;
56 static win_data wd;
58 // impossible original values to make sure they are read from prefs
59 static gint x = -1000, y = -1000, w = 0, h = 0;
60 static gchar *prefs_path = "dialogs.fillstroke";
62 static void sp_fillstroke_selection_modified ( Inkscape::Application *inkscape, Inkscape::Selection *selection, guint flags, GtkObject *base );
63 static void sp_fillstroke_selection_changed ( Inkscape::Application *inkscape, Inkscape::Selection *selection, GtkObject *base );
64 static void sp_fillstroke_opacity_changed (GtkAdjustment *a, SPWidget *dlg);
66 using Inkscape::UI::Widget::SimpleFilterModifier;
67 static void sp_fillstroke_blend_blur_changed (SimpleFilterModifier *);
69 static void
70 sp_object_properties_dialog_destroy (GtkObject *object, gpointer data)
71 {
72     sp_signal_disconnect_by_data (INKSCAPE, dlg);
73     wd.win = dlg = NULL;
74     wd.stop = 0;
75 }
77 static gboolean
78 sp_object_properties_dialog_delete ( GtkObject *object,
79                                      GdkEvent *event,
80                                      gpointer data )
81 {
83     gtk_window_get_position ((GtkWindow *) dlg, &x, &y);
84     gtk_window_get_size ((GtkWindow *) dlg, &w, &h);
86     if (x<0) x=0;
87     if (y<0) y=0;
89     prefs_set_int_attribute (prefs_path, "x", x);
90     prefs_set_int_attribute (prefs_path, "y", y);
91     prefs_set_int_attribute (prefs_path, "w", w);
92     prefs_set_int_attribute (prefs_path, "h", h);
94     return FALSE; // which means, go ahead and destroy it
96 }
99 void
100 sp_object_properties_page( GtkWidget *nb,
101                            GtkWidget *page,
102                            char *label,
103                            char *dlg_name,
104                            char *label_image )
106     GtkWidget *hb, *l, *px;
108     hb = gtk_hbox_new (FALSE, 0);
109     gtk_widget_show (hb);
111     px = sp_icon_new( Inkscape::ICON_SIZE_DECORATION, label_image );
112     gtk_widget_show (px);
113     gtk_box_pack_start (GTK_BOX (hb), px, FALSE, FALSE, 2);
115     l = gtk_label_new_with_mnemonic (label);
116     gtk_widget_show (l);
117     gtk_box_pack_start (GTK_BOX (hb), l, FALSE, FALSE, 0);
119     gtk_widget_show (page);
120     gtk_notebook_append_page (GTK_NOTEBOOK (nb), page, hb);
121     gtk_object_set_data (GTK_OBJECT (dlg), dlg_name, page);
124 void
125 sp_object_properties_dialog (void)
127     if (!dlg) {
128         gchar title[500];
129         sp_ui_dialog_title_string (Inkscape::Verb::get(SP_VERB_DIALOG_FILL_STROKE), title);
131         dlg = sp_window_new (title, TRUE);
132         if (x == -1000 || y == -1000) {
133             x = prefs_get_int_attribute (prefs_path, "x", -1000);
134             y = prefs_get_int_attribute (prefs_path, "y", -1000);
135         }
136         if (w ==0 || h == 0) {
137             w = prefs_get_int_attribute (prefs_path, "w", 0);
138             h = prefs_get_int_attribute (prefs_path, "h", 0);
139         }
140         
141 //        if (x<0) x=0;
142 //        if (y<0) y=0;
144         if (w && h) gtk_window_resize ((GtkWindow *) dlg, w, h);
145         if (x >= 0 && y >= 0 && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE)))
146             gtk_window_move ((GtkWindow *) dlg, x, y);
147         else
148             gtk_window_set_position(GTK_WINDOW(dlg), GTK_WIN_POS_CENTER);
149         sp_transientize (dlg);
150         wd.win = dlg;
151         wd.stop = 0;
153         g_signal_connect ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_transientize_callback), &wd );
155         gtk_signal_connect ( GTK_OBJECT (dlg), "event", GTK_SIGNAL_FUNC (sp_dialog_event_handler), dlg );
157         gtk_signal_connect ( GTK_OBJECT (dlg), "destroy", G_CALLBACK (sp_object_properties_dialog_destroy), dlg );
158         gtk_signal_connect ( GTK_OBJECT (dlg), "delete_event", G_CALLBACK (sp_object_properties_dialog_delete), dlg );
159         g_signal_connect ( G_OBJECT (INKSCAPE), "shut_down", G_CALLBACK (sp_object_properties_dialog_delete), dlg );
161         g_signal_connect ( G_OBJECT (INKSCAPE), "dialogs_hide", G_CALLBACK (sp_dialog_hide), dlg );
162         g_signal_connect ( G_OBJECT (INKSCAPE), "dialogs_unhide", G_CALLBACK (sp_dialog_unhide), dlg );
164         GtkWidget *vb = gtk_vbox_new (FALSE, 0);
165         gtk_widget_show (vb);
166         gtk_container_add (GTK_CONTAINER (dlg), vb);
168         GtkWidget *nb = gtk_notebook_new ();
169         gtk_widget_show (nb);
170         gtk_box_pack_start (GTK_BOX (vb), nb, TRUE, TRUE, 0);
171         gtk_object_set_data (GTK_OBJECT (dlg), "notebook", nb);
173         /* Fill page */
174         {
175             GtkWidget *page = sp_fill_style_widget_new ();
176             sp_object_properties_page(nb, page, _("_Fill"), "fill",
177                                       INKSCAPE_STOCK_PROPERTIES_FILL_PAGE);
178         }
180         /* Stroke paint page */
181         {
182             GtkWidget *page = sp_stroke_style_paint_widget_new ();
183             sp_object_properties_page(nb, page, _("Stroke _paint"), "stroke-paint",
184                                       INKSCAPE_STOCK_PROPERTIES_STROKE_PAINT_PAGE);
185         }
187         /* Stroke style page */
188         {
189             GtkWidget *page = sp_stroke_style_line_widget_new ();
190             sp_object_properties_page(nb, page, _("Stroke st_yle"), "stroke-line",
191                                       INKSCAPE_STOCK_PROPERTIES_STROKE_PAGE);
192         }
195         /* Filter Effects (gtkmm) */
196         GtkWidget *al_fe = gtk_alignment_new(1, 1, 1, 1);
197         gtk_alignment_set_padding(GTK_ALIGNMENT(al_fe), 0, 0, 4, 0);
198         SimpleFilterModifier *cb_fe = Gtk::manage(new SimpleFilterModifier);
199         g_object_set_data(G_OBJECT(dlg), "filter_modifier", cb_fe);
200         cb_fe->signal_blend_blur_changed().connect(
201             sigc::bind(sigc::ptr_fun(sp_fillstroke_blend_blur_changed), cb_fe));
202         gtk_container_add(GTK_CONTAINER(al_fe), GTK_WIDGET(cb_fe->gobj()));
203         
204         GtkWidget *b_vb = gtk_vbox_new (FALSE, 0);
205         gtk_box_pack_start (GTK_BOX (vb), b_vb, FALSE, FALSE, 2);
206         gtk_object_set_data (GTK_OBJECT (dlg), "blur", b_vb);
207         gtk_box_pack_start (GTK_BOX (b_vb), al_fe, FALSE, FALSE, 0);
208         gtk_widget_show_all (b_vb);
211         /* Opacity */
213         GtkWidget *o_vb = gtk_vbox_new (FALSE, 0);
214         gtk_box_pack_start (GTK_BOX (vb), o_vb, FALSE, FALSE, 2);
215         gtk_object_set_data (GTK_OBJECT (dlg), "master_opacity", o_vb);
217         GtkWidget *l_hb = gtk_hbox_new (FALSE, 0);
218         GtkWidget *l = gtk_label_new_with_mnemonic (_("Master _opacity, %"));
219         gtk_misc_set_alignment (GTK_MISC (l), 0.0, 1.0);
220         gtk_box_pack_start (GTK_BOX (l_hb), l, FALSE, FALSE, 4);
221         gtk_box_pack_start (GTK_BOX (o_vb), l_hb, FALSE, FALSE, 0);
223         GtkWidget *hb = gtk_hbox_new (FALSE, 4);
224         gtk_box_pack_start (GTK_BOX (o_vb), hb, FALSE, FALSE, 0);
226         GtkObject *a = gtk_adjustment_new (1.0, 0.0, 100.0, 1.0, 1.0, 0.0);
227         gtk_object_set_data(GTK_OBJECT(dlg), "master_opacity_adjustment", a);
229         GtkWidget *s = gtk_hscale_new (GTK_ADJUSTMENT (a));
230         gtk_scale_set_draw_value (GTK_SCALE (s), FALSE);
231         gtk_box_pack_start (GTK_BOX (hb), s, TRUE, TRUE, 4);
232         gtk_label_set_mnemonic_widget (GTK_LABEL(l), s);
234         GtkWidget *sb = gtk_spin_button_new (GTK_ADJUSTMENT (a), 0.01, 1);
235         gtk_box_pack_start (GTK_BOX (hb), sb, FALSE, FALSE, 0);
237         gtk_signal_connect ( a, "value_changed",
238                              GTK_SIGNAL_FUNC (sp_fillstroke_opacity_changed),
239                              dlg );
241         gtk_widget_show_all (o_vb);
243         // these callbacks are only for the master opacity update; the tabs above take care of themselves
244         g_signal_connect ( G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (sp_fillstroke_selection_changed), dlg );
245         g_signal_connect ( G_OBJECT (INKSCAPE), "change_subselection", G_CALLBACK (sp_fillstroke_selection_changed), dlg );
246         g_signal_connect ( G_OBJECT (INKSCAPE), "modify_selection", G_CALLBACK (sp_fillstroke_selection_modified), dlg );
247         g_signal_connect ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_fillstroke_selection_changed), dlg );
249         sp_fillstroke_selection_changed(INKSCAPE, sp_desktop_selection(SP_ACTIVE_DESKTOP), NULL);
251         gtk_widget_show (dlg);
253     } else {
254         gtk_window_present (GTK_WINDOW (dlg));
255     }
257 } // end of sp_object_properties_dialog()
259 void sp_object_properties_fill (void)
261     sp_object_properties_dialog ();
262     GtkWidget *nb = (GtkWidget *)gtk_object_get_data (GTK_OBJECT (dlg), "notebook");
263     gtk_notebook_set_page (GTK_NOTEBOOK (nb), 0);
266 void sp_object_properties_stroke (void)
268     sp_object_properties_dialog ();
269     GtkWidget *nb = (GtkWidget *)gtk_object_get_data (GTK_OBJECT (dlg), "notebook");
270     gtk_notebook_set_page (GTK_NOTEBOOK (nb), 1);
273 void sp_object_properties_stroke_style (void)
275     sp_object_properties_dialog ();
276     GtkWidget *nb = (GtkWidget *)gtk_object_get_data (GTK_OBJECT (dlg), "notebook");
277     gtk_notebook_set_page (GTK_NOTEBOOK (nb), 2);
282 static void
283 sp_fillstroke_selection_modified ( Inkscape::Application *inkscape,
284                               Inkscape::Selection *selection,
285                               guint flags,
286                               GtkObject *base )
288     sp_fillstroke_selection_changed ( inkscape, selection, base );
292 static void
293 sp_fillstroke_selection_changed ( Inkscape::Application *inkscape,
294                               Inkscape::Selection *selection,
295                               GtkObject *base )
297     if (gtk_object_get_data (GTK_OBJECT (dlg), "blocked"))
298         return;
299     gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (TRUE));
301     GtkWidget *opa = GTK_WIDGET (gtk_object_get_data (GTK_OBJECT (dlg), "master_opacity"));
302     GtkAdjustment *a = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(dlg), "master_opacity_adjustment"));
304     // create temporary style
305     SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
306     // query style from desktop into it. This returns a result flag and fills query with the style of subselection, if any, or selection
307     int result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
309     switch (result) {
310         case QUERY_STYLE_NOTHING:
311             gtk_widget_set_sensitive (opa, FALSE);
312             break;
313         case QUERY_STYLE_SINGLE:
314         case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
315         case QUERY_STYLE_MULTIPLE_SAME: 
316             gtk_widget_set_sensitive (opa, TRUE);
317             gtk_adjustment_set_value(a, 100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
318             break;
319     }
321     SimpleFilterModifier *mod = (SimpleFilterModifier*)g_object_get_data(G_OBJECT(dlg), "filter_modifier");
323     //query now for current filter mode and average blurring of selection
324     const int blend_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLEND);
325     switch(blend_result) {
326         case QUERY_STYLE_NOTHING:
327             mod->set_sensitive(false);
328             break;
329         case QUERY_STYLE_SINGLE:
330         case QUERY_STYLE_MULTIPLE_SAME:
331             mod->set_blend_mode(query->filter_blend_mode.value);
332             mod->set_sensitive(true);
333             break;
334         case QUERY_STYLE_MULTIPLE_DIFFERENT:
335             // TODO: set text
336             mod->set_sensitive(false);
337             break;
338     }
340     if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
341         int blur_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLUR);
342         switch (blur_result) {
343             case QUERY_STYLE_NOTHING: //no blurring
344                 mod->set_blur_sensitive(false);
345                 break;
346             case QUERY_STYLE_SINGLE:
347             case QUERY_STYLE_MULTIPLE_AVERAGED:
348             case QUERY_STYLE_MULTIPLE_SAME: 
349                 NR::Maybe<NR::Rect> bbox = sp_desktop_selection(SP_ACTIVE_DESKTOP)->bounds();
350                 if (bbox) {
351                     double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
352                     mod->set_blur_sensitive(true);
353                     //update blur widget value
354                     float radius = query->filter_gaussianBlur_deviation.value;
355                     float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
356                     mod->set_blur_value(percent);
357                 }
358                 break;
359         }
360     }
361     
362     sp_style_unref(query);
363     gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (FALSE));
366 static void
367 sp_fillstroke_opacity_changed (GtkAdjustment *a, SPWidget *base)
369     if (gtk_object_get_data (GTK_OBJECT (dlg), "blocked"))
370         return;
372     gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (TRUE));
374     // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
375     // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
376     // (though this only fixes the crash, not the multiple change events)
377     sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(SP_ACTIVE_DESKTOP), 0);
379     SPCSSAttr *css = sp_repr_css_attr_new ();
381     Inkscape::CSSOStringStream os;
382     os << CLAMP (a->value / 100, 0.0, 1.0);
383     sp_repr_css_set_property (css, "opacity", os.str().c_str());
385     sp_desktop_set_style (SP_ACTIVE_DESKTOP, css);
387     sp_repr_css_attr_unref (css);
389     sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:opacity", SP_VERB_DIALOG_FILL_STROKE, 
390                             _("Change opacity"));
392     // resume interruptibility
393     sp_canvas_end_forced_full_redraws(sp_desktop_canvas(SP_ACTIVE_DESKTOP));
395     gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (FALSE));
398 static void
399 sp_fillstroke_blend_blur_changed (SimpleFilterModifier *m)
401     //if dialog is locked, return 
402     if (gtk_object_get_data (GTK_OBJECT (dlg), "blocked"))
403         return;
405      //lock dialog
406     gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (TRUE));
407   
408     //get desktop
409     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
410     if (!desktop) {
411         return;
412     }
414     // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
415     sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
416     
417     //get current selection
418     Inkscape::Selection *selection = sp_desktop_selection (desktop);
420     NR::Maybe<NR::Rect> bbox = selection->bounds();
421     if (!bbox) {
422         return;
423     }
424     //get list of selected items
425     GSList const *items = selection->itemList();
426     //get current document
427     SPDocument *document = sp_desktop_document (desktop);
429     double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
430     const Glib::ustring blendmode = m->get_blend_mode();
431     double radius = m->get_blur_value() * perimeter / 400;
433     SPFilter *filter = NULL;
434     const bool remfilter = (blendmode == "normal" && radius == 0) || (blendmode == "filter" && !filter);
435         
436     if(blendmode != "filter" || filter) {
437         //apply created filter to every selected item
438         for (GSList const *i = items; i != NULL; i = i->next) {
439             SPItem * item = SP_ITEM(i->data);
440             SPStyle *style = SP_OBJECT_STYLE(item);
441             g_assert(style != NULL);
442             
443             if(remfilter) {
444                 remove_filter (item, false);
445             }
446             else {
447                 if(blendmode != "filter")
448                     filter = new_filter_simple_from_item(document, item, blendmode.c_str(), radius);
449                 sp_style_set_property_url (SP_OBJECT(item), "filter", SP_OBJECT(filter), false);
450             }
451             
452             //request update
453             SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
454                                                     SP_OBJECT_STYLE_MODIFIED_FLAG ));
455         }
456     }
458     sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:blur", SP_VERB_DIALOG_FILL_STROKE,  _("Change blur"));
460     // resume interruptibility
461     sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
463     gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (FALSE));
467 /*
468   Local Variables:
469   mode:c++
470   c-file-style:"stroustrup"
471   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
472   indent-tabs-mode:nil
473   fill-column:99
474   End:
475 */
476 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :