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 )
105 {
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);
122 }
124 void
125 sp_object_properties_dialog (void)
126 {
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 }
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_selection_changed().connect(
201 sigc::bind(sigc::ptr_fun(sp_fillstroke_blend_blur_changed), cb_fe));
202 cb_fe->signal_blend_blur_changed().connect(
203 sigc::bind(sigc::ptr_fun(sp_fillstroke_blend_blur_changed), cb_fe));
204 gtk_container_add(GTK_CONTAINER(al_fe), GTK_WIDGET(cb_fe->gobj()));
206 GtkWidget *b_vb = gtk_vbox_new (FALSE, 0);
207 gtk_box_pack_start (GTK_BOX (vb), b_vb, FALSE, FALSE, 2);
208 gtk_object_set_data (GTK_OBJECT (dlg), "blur", b_vb);
209 gtk_box_pack_start (GTK_BOX (b_vb), al_fe, FALSE, FALSE, 0);
210 gtk_widget_show_all (b_vb);
213 /* Opacity */
215 GtkWidget *o_vb = gtk_vbox_new (FALSE, 0);
216 gtk_box_pack_start (GTK_BOX (vb), o_vb, FALSE, FALSE, 2);
217 gtk_object_set_data (GTK_OBJECT (dlg), "master_opacity", o_vb);
219 GtkWidget *l_hb = gtk_hbox_new (FALSE, 0);
220 GtkWidget *l = gtk_label_new_with_mnemonic (_("Master _opacity, %"));
221 gtk_misc_set_alignment (GTK_MISC (l), 0.0, 1.0);
222 gtk_box_pack_start (GTK_BOX (l_hb), l, FALSE, FALSE, 4);
223 gtk_box_pack_start (GTK_BOX (o_vb), l_hb, FALSE, FALSE, 0);
225 GtkWidget *hb = gtk_hbox_new (FALSE, 4);
226 gtk_box_pack_start (GTK_BOX (o_vb), hb, FALSE, FALSE, 0);
228 GtkObject *a = gtk_adjustment_new (1.0, 0.0, 100.0, 1.0, 1.0, 0.0);
229 gtk_object_set_data(GTK_OBJECT(dlg), "master_opacity_adjustment", a);
231 GtkWidget *s = gtk_hscale_new (GTK_ADJUSTMENT (a));
232 gtk_scale_set_draw_value (GTK_SCALE (s), FALSE);
233 gtk_box_pack_start (GTK_BOX (hb), s, TRUE, TRUE, 4);
234 gtk_label_set_mnemonic_widget (GTK_LABEL(l), s);
236 GtkWidget *sb = gtk_spin_button_new (GTK_ADJUSTMENT (a), 0.01, 1);
237 gtk_box_pack_start (GTK_BOX (hb), sb, FALSE, FALSE, 0);
239 gtk_signal_connect ( a, "value_changed",
240 GTK_SIGNAL_FUNC (sp_fillstroke_opacity_changed),
241 dlg );
243 gtk_widget_show_all (o_vb);
245 // these callbacks are only for the master opacity update; the tabs above take care of themselves
246 g_signal_connect ( G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (sp_fillstroke_selection_changed), dlg );
247 g_signal_connect ( G_OBJECT (INKSCAPE), "change_subselection", G_CALLBACK (sp_fillstroke_selection_changed), dlg );
248 g_signal_connect ( G_OBJECT (INKSCAPE), "modify_selection", G_CALLBACK (sp_fillstroke_selection_modified), dlg );
249 g_signal_connect ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_fillstroke_selection_changed), dlg );
251 sp_fillstroke_selection_changed(INKSCAPE, sp_desktop_selection(SP_ACTIVE_DESKTOP), NULL);
253 gtk_widget_show (dlg);
255 } else {
256 gtk_window_present (GTK_WINDOW (dlg));
257 }
259 } // end of sp_object_properties_dialog()
261 void sp_object_properties_fill (void)
262 {
263 sp_object_properties_dialog ();
264 GtkWidget *nb = (GtkWidget *)gtk_object_get_data (GTK_OBJECT (dlg), "notebook");
265 gtk_notebook_set_page (GTK_NOTEBOOK (nb), 0);
266 }
268 void sp_object_properties_stroke (void)
269 {
270 sp_object_properties_dialog ();
271 GtkWidget *nb = (GtkWidget *)gtk_object_get_data (GTK_OBJECT (dlg), "notebook");
272 gtk_notebook_set_page (GTK_NOTEBOOK (nb), 1);
273 }
275 void sp_object_properties_stroke_style (void)
276 {
277 sp_object_properties_dialog ();
278 GtkWidget *nb = (GtkWidget *)gtk_object_get_data (GTK_OBJECT (dlg), "notebook");
279 gtk_notebook_set_page (GTK_NOTEBOOK (nb), 2);
280 }
284 static void
285 sp_fillstroke_selection_modified ( Inkscape::Application *inkscape,
286 Inkscape::Selection *selection,
287 guint flags,
288 GtkObject *base )
289 {
290 sp_fillstroke_selection_changed ( inkscape, selection, base );
291 }
294 static void
295 sp_fillstroke_selection_changed ( Inkscape::Application *inkscape,
296 Inkscape::Selection *selection,
297 GtkObject *base )
298 {
299 if (gtk_object_get_data (GTK_OBJECT (dlg), "blocked"))
300 return;
301 gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (TRUE));
303 GtkWidget *opa = GTK_WIDGET (gtk_object_get_data (GTK_OBJECT (dlg), "master_opacity"));
304 GtkAdjustment *a = GTK_ADJUSTMENT(gtk_object_get_data(GTK_OBJECT(dlg), "master_opacity_adjustment"));
306 // create temporary style
307 SPStyle *query = sp_style_new (SP_ACTIVE_DOCUMENT);
308 // query style from desktop into it. This returns a result flag and fills query with the style of subselection, if any, or selection
309 int result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_MASTEROPACITY);
311 switch (result) {
312 case QUERY_STYLE_NOTHING:
313 gtk_widget_set_sensitive (opa, FALSE);
314 break;
315 case QUERY_STYLE_SINGLE:
316 case QUERY_STYLE_MULTIPLE_AVERAGED: // TODO: treat this slightly differently
317 case QUERY_STYLE_MULTIPLE_SAME:
318 gtk_widget_set_sensitive (opa, TRUE);
319 gtk_adjustment_set_value(a, 100 * SP_SCALE24_TO_FLOAT(query->opacity.value));
320 break;
321 }
323 SimpleFilterModifier *mod = (SimpleFilterModifier*)g_object_get_data(G_OBJECT(dlg), "filter_modifier");
325 //query now for current filter mode and average blurring of selection
326 const int blend_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLEND);
327 switch(blend_result) {
328 case QUERY_STYLE_NOTHING:
329 mod->set_sensitive(false);
330 break;
331 case QUERY_STYLE_SINGLE:
332 case QUERY_STYLE_MULTIPLE_SAME:
333 mod->set_blend_mode(query->filter_blend_mode.value);
334 mod->set_sensitive(true);
335 break;
336 case QUERY_STYLE_MULTIPLE_DIFFERENT:
337 // TODO: set text
338 mod->set_sensitive(false);
339 break;
340 }
342 if(blend_result == QUERY_STYLE_SINGLE || blend_result == QUERY_STYLE_MULTIPLE_SAME) {
343 int blur_result = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_BLUR);
344 switch (blur_result) {
345 case QUERY_STYLE_NOTHING: //no blurring
346 mod->set_blur_sensitive(false);
347 break;
348 case QUERY_STYLE_SINGLE:
349 case QUERY_STYLE_MULTIPLE_AVERAGED:
350 case QUERY_STYLE_MULTIPLE_SAME:
351 NR::Maybe<NR::Rect> bbox = sp_desktop_selection(SP_ACTIVE_DESKTOP)->bounds();
352 if (bbox) {
353 double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
354 mod->set_blur_sensitive(true);
355 //update blur widget value
356 float radius = query->filter_gaussianBlur_deviation.value;
357 float percent = radius * 400 / perimeter; // so that for a square, 100% == half side
358 mod->set_blur_value(percent);
359 }
360 break;
361 }
362 }
364 sp_style_unref(query);
365 gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (FALSE));
366 }
368 static void
369 sp_fillstroke_opacity_changed (GtkAdjustment *a, SPWidget *base)
370 {
371 if (gtk_object_get_data (GTK_OBJECT (dlg), "blocked"))
372 return;
374 gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (TRUE));
376 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
377 // UPDATE: crash fixed in GTK+ 2.10.7 (bug 374378), remove this as soon as it's reasonably common
378 // (though this only fixes the crash, not the multiple change events)
379 sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(SP_ACTIVE_DESKTOP), 0);
381 SPCSSAttr *css = sp_repr_css_attr_new ();
383 Inkscape::CSSOStringStream os;
384 os << CLAMP (a->value / 100, 0.0, 1.0);
385 sp_repr_css_set_property (css, "opacity", os.str().c_str());
387 sp_desktop_set_style (SP_ACTIVE_DESKTOP, css);
389 sp_repr_css_attr_unref (css);
391 sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:opacity", SP_VERB_DIALOG_FILL_STROKE,
392 _("Change opacity"));
394 // resume interruptibility
395 sp_canvas_end_forced_full_redraws(sp_desktop_canvas(SP_ACTIVE_DESKTOP));
397 gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (FALSE));
398 }
400 static void
401 sp_fillstroke_blend_blur_changed (SimpleFilterModifier *m)
402 {
403 //if dialog is locked, return
404 if (gtk_object_get_data (GTK_OBJECT (dlg), "blocked"))
405 return;
407 //lock dialog
408 gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (TRUE));
410 //get desktop
411 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
412 if (!desktop) {
413 return;
414 }
416 // FIXME: fix for GTK breakage, see comment in SelectedStyle::on_opacity_changed; here it results in crash 1580903
417 sp_canvas_force_full_redraw_after_interruptions(sp_desktop_canvas(desktop), 0);
419 //get current selection
420 Inkscape::Selection *selection = sp_desktop_selection (desktop);
422 NR::Maybe<NR::Rect> bbox = selection->bounds();
423 if (!bbox) {
424 return;
425 }
426 //get list of selected items
427 GSList const *items = selection->itemList();
428 //get current document
429 SPDocument *document = sp_desktop_document (desktop);
431 double perimeter = bbox->extent(NR::X) + bbox->extent(NR::Y);
432 const Glib::ustring blendmode = m->get_blend_mode();
433 double radius = m->get_blur_value() * perimeter / 400;
435 SPFilter *filter = m->get_selected_filter();
436 const bool remfilter = (blendmode == "normal" && radius == 0) || (blendmode == "filter" && !filter);
438 if(blendmode != "filter" || filter) {
439 //apply created filter to every selected item
440 for (GSList const *i = items; i != NULL; i = i->next) {
441 SPItem * item = SP_ITEM(i->data);
442 SPStyle *style = SP_OBJECT_STYLE(item);
443 g_assert(style != NULL);
445 if(remfilter) {
446 remove_filter (item, false);
447 }
448 else {
449 if(blendmode != "filter")
450 filter = new_filter_simple_from_item(document, item, blendmode.c_str(), radius);
451 sp_style_set_property_url (SP_OBJECT(item), "filter", SP_OBJECT(filter), false);
452 }
454 //request update
455 SP_OBJECT(item)->requestDisplayUpdate(( SP_OBJECT_MODIFIED_FLAG |
456 SP_OBJECT_STYLE_MODIFIED_FLAG ));
457 }
458 }
460 sp_document_maybe_done (sp_desktop_document (SP_ACTIVE_DESKTOP), "fillstroke:blur", SP_VERB_DIALOG_FILL_STROKE, _("Change blur"));
462 // resume interruptibility
463 sp_canvas_end_forced_full_redraws(sp_desktop_canvas(desktop));
465 gtk_object_set_data (GTK_OBJECT (dlg), "blocked", GUINT_TO_POINTER (FALSE));
466 }
469 /*
470 Local Variables:
471 mode:c++
472 c-file-style:"stroustrup"
473 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
474 indent-tabs-mode:nil
475 fill-column:99
476 End:
477 */
478 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :