1 /**
2 * \brief Filter Effects dialog
3 *
4 * Authors:
5 * Nicholas Bishop <nicholasbishop@gmail.org>
6 *
7 * Copyright (C) 2007 Authors
8 *
9 * Released under GNU GPL. Read the file 'COPYING' for more information.
10 */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
16 #include <gtk/gtktreeview.h>
17 #include <gtkmm/cellrenderertext.h>
18 #include <gtkmm/colorbutton.h>
19 #include <gtkmm/messagedialog.h>
20 #include <gtkmm/paned.h>
21 #include <gtkmm/scale.h>
22 #include <gtkmm/scrolledwindow.h>
23 #include <gtkmm/spinbutton.h>
24 #include <gtkmm/stock.h>
25 #include <glibmm/i18n.h>
27 #include "application/application.h"
28 #include "application/editor.h"
29 #include "desktop.h"
30 #include "desktop-handles.h"
31 #include "dialog-manager.h"
32 #include "document.h"
33 #include "filter-chemistry.h"
34 #include "filter-effects-dialog.h"
35 #include "inkscape.h"
36 #include "selection.h"
37 #include "sp-feblend.h"
38 #include "sp-fecolormatrix.h"
39 #include "sp-fecomposite.h"
40 #include "sp-feconvolvematrix.h"
41 #include "sp-fedisplacementmap.h"
42 #include "sp-fedistantlight.h"
43 #include "sp-femerge.h"
44 #include "sp-femergenode.h"
45 #include "sp-feoffset.h"
46 #include "sp-fepointlight.h"
47 #include "sp-fespotlight.h"
48 #include "sp-filter-primitive.h"
49 #include "sp-gaussian-blur.h"
51 #include "style.h"
52 #include "svg/svg-color.h"
53 #include "verbs.h"
54 #include "xml/node.h"
55 #include "xml/repr.h"
56 #include <sstream>
58 #include <iostream>
60 using namespace NR;
62 namespace Inkscape {
63 namespace UI {
64 namespace Dialog {
66 // Returns the number of inputs available for the filter primitive type
67 int input_count(const SPFilterPrimitive* prim)
68 {
69 if(!prim)
70 return 0;
71 else if(SP_IS_FEBLEND(prim) || SP_IS_FECOMPOSITE(prim) || SP_IS_FEDISPLACEMENTMAP(prim))
72 return 2;
73 else if(SP_IS_FEMERGE(prim)) {
74 // Return the number of feMergeNode connections plus an extra
75 int count = 1;
76 for(const SPObject* o = prim->firstChild(); o; o = o->next, ++count);
77 return count;
78 }
79 else
80 return 1;
81 }
83 class CheckButtonAttr : public Gtk::CheckButton, public AttrWidget
84 {
85 public:
86 CheckButtonAttr(const Glib::ustring& label,
87 const Glib::ustring& tv, const Glib::ustring& fv,
88 const SPAttributeEnum a)
89 : Gtk::CheckButton(label),
90 AttrWidget(a),
91 _true_val(tv), _false_val(fv)
92 {
93 signal_toggled().connect(signal_attr_changed().make_slot());
94 }
96 Glib::ustring get_as_attribute() const
97 {
98 return get_active() ? _true_val : _false_val;
99 }
101 void set_from_attribute(SPObject* o)
102 {
103 const gchar* val = attribute_value(o);
104 if(val) {
105 if(_true_val == val)
106 set_active(true);
107 else if(_false_val == val)
108 set_active(false);
109 }
110 }
111 private:
112 const Glib::ustring _true_val, _false_val;
113 };
115 class SpinButtonAttr : public Gtk::SpinButton, public AttrWidget
116 {
117 public:
118 SpinButtonAttr(double lower, double upper, double step_inc,
119 double climb_rate, int digits, const SPAttributeEnum a)
120 : Gtk::SpinButton(climb_rate, digits),
121 AttrWidget(a)
122 {
123 set_range(lower, upper);
124 set_increments(step_inc, step_inc * 5);
126 signal_value_changed().connect(signal_attr_changed().make_slot());
127 }
129 Glib::ustring get_as_attribute() const
130 {
131 const double val = get_value();
133 if(get_digits() == 0)
134 return Glib::Ascii::dtostr((int)val);
135 else
136 return Glib::Ascii::dtostr(val);
137 }
139 void set_from_attribute(SPObject* o)
140 {
141 const gchar* val = attribute_value(o);
142 if(val)
143 set_value(Glib::Ascii::strtod(val));
144 }
145 };
147 // Contains an arbitrary number of spin buttons that use seperate attributes
148 class MultiSpinButton : public Gtk::HBox
149 {
150 public:
151 MultiSpinButton(double lower, double upper, double step_inc,
152 double climb_rate, int digits, std::vector<SPAttributeEnum> attrs)
153 {
154 for(unsigned i = 0; i < attrs.size(); ++i) {
155 _spins.push_back(new SpinButtonAttr(lower, upper, step_inc, climb_rate, digits, attrs[i]));
156 pack_start(*_spins.back(), false, false);
157 }
158 }
160 ~MultiSpinButton()
161 {
162 for(unsigned i = 0; i < _spins.size(); ++i)
163 delete _spins[i];
164 }
166 std::vector<SpinButtonAttr*>& get_spinbuttons()
167 {
168 return _spins;
169 }
170 private:
171 std::vector<SpinButtonAttr*> _spins;
172 };
174 // Contains two spinbuttons that describe a NumberOptNumber
175 class DualSpinButton : public Gtk::HBox, public AttrWidget
176 {
177 public:
178 DualSpinButton(double lower, double upper, double step_inc,
179 double climb_rate, int digits, const SPAttributeEnum a)
180 : AttrWidget(a),
181 _s1(climb_rate, digits), _s2(climb_rate, digits)
182 {
183 _s1.set_range(lower, upper);
184 _s2.set_range(lower, upper);
185 _s1.set_increments(step_inc, step_inc * 5);
186 _s2.set_increments(step_inc, step_inc * 5);
188 _s1.signal_value_changed().connect(signal_attr_changed().make_slot());
189 _s2.signal_value_changed().connect(signal_attr_changed().make_slot());
191 pack_start(_s1, false, false);
192 pack_start(_s2, false, false);
193 }
195 Gtk::SpinButton& get_spinbutton1()
196 {
197 return _s1;
198 }
200 Gtk::SpinButton& get_spinbutton2()
201 {
202 return _s2;
203 }
205 virtual Glib::ustring get_as_attribute() const
206 {
207 double v1 = _s1.get_value();
208 double v2 = _s2.get_value();
210 if(_s1.get_digits() == 0) {
211 v1 = (int)v1;
212 v2 = (int)v2;
213 }
215 return Glib::Ascii::dtostr(v1) + " " + Glib::Ascii::dtostr(v2);
216 }
218 virtual void set_from_attribute(SPObject* o)
219 {
220 const gchar* val = attribute_value(o);
221 if(val) {
222 NumberOptNumber n;
223 n.set(val);
224 _s1.set_value(n.getNumber());
225 _s2.set_value(n.getOptNumber());
226 }
227 }
228 private:
229 Gtk::SpinButton _s1, _s2;
230 };
232 class ColorButton : public Gtk::ColorButton, public AttrWidget
233 {
234 public:
235 ColorButton(const SPAttributeEnum a)
236 : AttrWidget(a)
237 {
238 signal_color_set().connect(signal_attr_changed().make_slot());
240 Gdk::Color col;
241 col.set_rgb(65535, 65535, 65535);
242 set_color(col);
243 }
245 // Returns the color in 'rgb(r,g,b)' form.
246 Glib::ustring get_as_attribute() const
247 {
248 std::ostringstream os;
249 const Gdk::Color c = get_color();
250 const int r = (c.get_red() + 1) / 256 - 1, g = (c.get_green() + 1) / 256 - 1, b = (c.get_blue() + 1) / 256 - 1;
251 os << "rgb(" << r << "," << g << "," << b << ")";
252 return os.str();
253 }
256 void set_from_attribute(SPObject* o)
257 {
258 const gchar* val = attribute_value(o);
259 if(val) {
260 const guint32 i = sp_svg_read_color(val, 0xFFFFFFFF);
261 const int r = SP_RGBA32_R_U(i) + 1, g = SP_RGBA32_G_U(i) + 1, b = SP_RGBA32_B_U(i) + 1;
262 Gdk::Color col;
263 col.set_rgb(r * 256 - 1, g * 256 - 1, b * 256 - 1);
264 set_color(col);
265 }
266 }
267 };
269 /* Displays/Edits the matrix for feConvolveMatrix or feColorMatrix */
270 class FilterEffectsDialog::MatrixAttr : public Gtk::Frame, public AttrWidget
271 {
272 public:
273 MatrixAttr(const SPAttributeEnum a)
274 : AttrWidget(a)
275 {
276 _model = Gtk::ListStore::create(_columns);
277 _tree.set_model(_model);
278 _tree.set_headers_visible(false);
279 _tree.show();
280 add(_tree);
281 set_shadow_type(Gtk::SHADOW_IN);
282 }
284 Glib::ustring get_as_attribute() const
285 {
286 std::ostringstream os;
288 for(Gtk::TreeIter iter = _model->children().begin();
289 iter != _model->children().end(); ++iter) {
290 for(unsigned c = 0; c < _tree.get_columns().size(); ++c) {
291 os << (*iter)[_columns.cols[c]] << " ";
292 }
293 }
295 return os.str();
296 }
298 void set_from_attribute(SPObject* o)
299 {
300 if(o) {
301 if(SP_IS_FECONVOLVEMATRIX(o)) {
302 SPFeConvolveMatrix* conv = SP_FECONVOLVEMATRIX(o);
303 int cols, rows;
304 cols = (int)conv->order.getNumber();
305 if(cols > 5)
306 cols = 5;
307 rows = conv->order.optNumber_set ? (int)conv->order.getOptNumber() : cols;
308 update(o, rows, cols);
309 }
310 else if(SP_IS_FECOLORMATRIX(o))
311 update(o, 4, 5);
312 }
313 }
314 private:
315 class MatrixColumns : public Gtk::TreeModel::ColumnRecord
316 {
317 public:
318 MatrixColumns()
319 {
320 cols.resize(5);
321 for(unsigned i = 0; i < cols.size(); ++i)
322 add(cols[i]);
323 }
324 std::vector<Gtk::TreeModelColumn<double> > cols;
325 };
327 void update(SPObject* o, const int rows, const int cols)
328 {
329 _model->clear();
331 _tree.remove_all_columns();
333 SPFeColorMatrix* col = 0;
334 SPFeConvolveMatrix* conv = 0;
335 if(SP_IS_FECOLORMATRIX(o))
336 col = SP_FECOLORMATRIX(o);
337 else if(SP_IS_FECONVOLVEMATRIX(o))
338 conv = SP_FECONVOLVEMATRIX(o);
339 else
340 return;
342 if(o) {
343 int ndx = 0;
345 for(int i = 0; i < cols; ++i) {
346 _tree.append_column_numeric_editable("", _columns.cols[i], "%.2f");
347 dynamic_cast<Gtk::CellRendererText*>(_tree.get_column(i)->get_first_cell_renderer())->signal_edited().connect(
348 sigc::mem_fun(*this, &MatrixAttr::rebind));
349 }
351 for(int r = 0; r < rows; ++r) {
352 Gtk::TreeRow row = *(_model->append());
353 for(int c = 0; c < cols; ++c, ++ndx) {
354 if(col)
355 row[_columns.cols[c]] = ndx < (int)col->values.size() ? col->values[ndx] : 0;
356 else
357 row[_columns.cols[c]] = ndx < (int)conv->kernelMatrix.size() ? conv->kernelMatrix[ndx] : 0;
358 }
359 }
360 }
361 }
363 void rebind(const Glib::ustring&, const Glib::ustring&)
364 {
365 signal_attr_changed()();
366 }
368 Gtk::TreeView _tree;
369 Glib::RefPtr<Gtk::ListStore> _model;
370 MatrixColumns _columns;
371 };
373 // Displays a matrix or a slider for feColorMatrix
374 class FilterEffectsDialog::ColorMatrixValues : public Gtk::Frame, public AttrWidget
375 {
376 public:
377 ColorMatrixValues()
378 : AttrWidget(SP_ATTR_VALUES),
379 _matrix(SP_ATTR_VALUES),
380 _saturation(0, 0, 1, 0.1, 0.01, 2, SP_ATTR_VALUES),
381 _angle(0, 0, 360, 0.1, 0.01, 1, SP_ATTR_VALUES),
382 _label(_("None"), Gtk::ALIGN_LEFT)
383 {
384 _matrix.show();
385 _saturation.show();
386 _angle.show();
388 _label.set_sensitive(false);
389 _label.show();
391 set_shadow_type(Gtk::SHADOW_NONE);
392 }
394 virtual void set_from_attribute(SPObject* o)
395 {
396 if(SP_IS_FECOLORMATRIX(o)) {
397 SPFeColorMatrix* col = SP_FECOLORMATRIX(o);
398 remove();
399 switch(col->type) {
400 case COLORMATRIX_SATURATE:
401 add(_saturation);
402 _saturation.set_from_attribute(o);
403 break;
404 case COLORMATRIX_HUEROTATE:
405 add(_angle);
406 _angle.set_from_attribute(o);
407 break;
408 case COLORMATRIX_LUMINANCETOALPHA:
409 add(_label);
410 break;
411 case COLORMATRIX_MATRIX:
412 default:
413 add(_matrix);
414 _matrix.set_from_attribute(o);
415 break;
416 }
417 }
418 }
420 virtual Glib::ustring get_as_attribute() const
421 {
422 const Widget* w = get_child();
423 if(w == &_label)
424 return "";
425 else
426 return dynamic_cast<const AttrWidget*>(w)->get_as_attribute();
427 }
428 private:
429 MatrixAttr _matrix;
430 SpinSlider _saturation;
431 SpinSlider _angle;
432 Gtk::Label _label;
433 };
435 class FilterEffectsDialog::Settings
436 {
437 public:
438 typedef sigc::slot<void, const AttrWidget*> SetAttrSlot;
440 Settings(FilterEffectsDialog& d, SetAttrSlot slot, const int maxtypes)
441 : _dialog(d), _set_attr_slot(slot), _max_types(maxtypes)
442 {
443 _groups.resize(_max_types);
444 _attrwidgets.resize(_max_types);
446 for(int i = 0; i < _max_types; ++i) {
447 _groups[i] = new Gtk::VBox;
448 d._settings_box.add(*_groups[i]);
449 }
450 }
452 ~Settings()
453 {
454 for(int i = 0; i < _max_types; ++i) {
455 delete _groups[i];
456 for(unsigned j = 0; j < _attrwidgets[i].size(); ++j)
457 delete _attrwidgets[i][j];
458 }
459 }
461 // Show the active settings group and update all the AttrWidgets with new values
462 void show_and_update(const int t, SPObject* ob)
463 {
464 type(t);
465 for(unsigned i = 0; i < _groups.size(); ++i)
466 _groups[i]->hide();
467 _groups[t]->show_all();
469 _dialog.set_attrs_locked(true);
470 for(unsigned i = 0; i < _attrwidgets[_current_type].size(); ++i)
471 _attrwidgets[_current_type][i]->set_from_attribute(ob);
472 _dialog.set_attrs_locked(false);
473 }
475 void type(const int t)
476 {
477 _current_type = t;
478 }
480 // LightSource
481 LightSourceControl* add_lightsource(const Glib::ustring& label);
483 // CheckBox
484 CheckButtonAttr* add_checkbutton(const SPAttributeEnum attr, const Glib::ustring& label,
485 const Glib::ustring& tv, const Glib::ustring& fv)
486 {
487 CheckButtonAttr* cb = new CheckButtonAttr(label, tv, fv, attr);
488 add_widget(cb, "");
489 add_attr_widget(cb);
490 return cb;
491 }
493 // ColorButton
494 ColorButton* add_color(const SPAttributeEnum attr, const Glib::ustring& label)
495 {
496 ColorButton* col = new ColorButton(attr);
497 add_widget(col, label);
498 add_attr_widget(col);
499 return col;
500 }
502 // Matrix
503 MatrixAttr* add_matrix(const SPAttributeEnum attr, const Glib::ustring& label)
504 {
505 MatrixAttr* conv = new MatrixAttr(attr);
506 add_widget(conv, label);
507 add_attr_widget(conv);
508 return conv;
509 }
511 // ColorMatrixValues
512 ColorMatrixValues* add_colormatrixvalues(const Glib::ustring& label)
513 {
514 ColorMatrixValues* cmv = new ColorMatrixValues;
515 add_widget(cmv, label);
516 add_attr_widget(cmv);
517 return cmv;
518 }
520 // SpinSlider
521 SpinSlider* add_spinslider(const SPAttributeEnum attr, const Glib::ustring& label,
522 const double lo, const double hi, const double step_inc, const double climb, const int digits)
523 {
524 SpinSlider* spinslider = new SpinSlider(lo, lo, hi, step_inc, climb, digits, attr);
525 add_widget(spinslider, label);
526 add_attr_widget(spinslider);
527 return spinslider;
528 }
530 // DualSpinSlider
531 DualSpinSlider* add_dualspinslider(const SPAttributeEnum attr, const Glib::ustring& label,
532 const double lo, const double hi, const double step_inc,
533 const double climb, const int digits)
534 {
535 DualSpinSlider* dss = new DualSpinSlider(lo, lo, hi, step_inc, climb, digits, attr);
536 add_widget(dss, label);
537 add_attr_widget(dss);
538 return dss;
539 }
541 // DualSpinButton
542 DualSpinButton* add_dualspinbutton(const SPAttributeEnum attr, const Glib::ustring& label,
543 const double lo, const double hi, const double step_inc,
544 const double climb, const int digits)
545 {
546 DualSpinButton* dsb = new DualSpinButton(lo, hi, step_inc, climb, digits, attr);
547 add_widget(dsb, label);
548 add_attr_widget(dsb);
549 return dsb;
550 }
552 // MultiSpinButton
553 MultiSpinButton* add_multispinbutton(const SPAttributeEnum attr1, const SPAttributeEnum attr2,
554 const Glib::ustring& label, const double lo, const double hi,
555 const double step_inc, const double climb, const int digits)
556 {
557 std::vector<SPAttributeEnum> attrs;
558 attrs.push_back(attr1);
559 attrs.push_back(attr2);
560 MultiSpinButton* msb = new MultiSpinButton(lo, hi, step_inc, climb, digits, attrs);
561 add_widget(msb, label);
562 for(unsigned i = 0; i < msb->get_spinbuttons().size(); ++i)
563 add_attr_widget(msb->get_spinbuttons()[i]);
564 return msb;
565 }
566 MultiSpinButton* add_multispinbutton(const SPAttributeEnum attr1, const SPAttributeEnum attr2,
567 const SPAttributeEnum attr3, const Glib::ustring& label, const double lo,
568 const double hi, const double step_inc, const double climb, const int digits)
569 {
570 std::vector<SPAttributeEnum> attrs;
571 attrs.push_back(attr1);
572 attrs.push_back(attr2);
573 attrs.push_back(attr3);
574 MultiSpinButton* msb = new MultiSpinButton(lo, hi, step_inc, climb, digits, attrs);
575 add_widget(msb, label);
576 for(unsigned i = 0; i < msb->get_spinbuttons().size(); ++i)
577 add_attr_widget(msb->get_spinbuttons()[i]);
578 return msb;
579 }
581 // ComboBoxEnum
582 template<typename T> ComboBoxEnum<T>* add_combo(const SPAttributeEnum attr,
583 const Glib::ustring& label,
584 const Util::EnumDataConverter<T>& conv)
585 {
586 ComboBoxEnum<T>* combo = new ComboBoxEnum<T>(conv, attr);
587 add_widget(combo, label);
588 add_attr_widget(combo);
589 return combo;
590 }
591 private:
592 void add_attr_widget(AttrWidget* a)
593 {
594 _attrwidgets[_current_type].push_back(a);
595 a->signal_attr_changed().connect(sigc::bind(_set_attr_slot, a));
596 }
598 /* Adds a new settings widget using the specified label. The label will be formatted with a colon
599 and all widgets within the setting group are aligned automatically. */
600 void add_widget(Gtk::Widget* w, const Glib::ustring& label)
601 {
602 Gtk::Label *lbl = Gtk::manage(new Gtk::Label(label + (label == "" ? "" : ":"), Gtk::ALIGN_LEFT));
603 Gtk::HBox *hb = Gtk::manage(new Gtk::HBox);
604 hb->set_spacing(12);
605 hb->pack_start(*lbl, false, false);
606 hb->pack_start(*w);
607 _groups[_current_type]->pack_start(*hb);
609 _dialog._sizegroup->add_widget(*lbl);
611 hb->show();
612 lbl->show();
614 w->show();
615 }
617 std::vector<Gtk::VBox*> _groups;
619 FilterEffectsDialog& _dialog;
620 SetAttrSlot _set_attr_slot;
621 std::vector<std::vector<AttrWidget*> > _attrwidgets;
622 int _current_type, _max_types;
623 };
625 // Settings for the three light source objects
626 class FilterEffectsDialog::LightSourceControl : public AttrWidget
627 {
628 public:
629 LightSourceControl(FilterEffectsDialog& d)
630 : AttrWidget(SP_ATTR_INVALID),
631 _dialog(d),
632 _settings(d, sigc::mem_fun(_dialog, &FilterEffectsDialog::set_child_attr_direct), LIGHT_ENDSOURCE),
633 _light_source(LightSourceConverter)
634 {
635 _box.add(_light_source);
636 _box.reorder_child(_light_source, 0);
637 _light_source.signal_changed().connect(sigc::mem_fun(*this, &LightSourceControl::on_source_changed));
639 // FIXME: these range values are complete crap
641 _settings.type(LIGHT_DISTANT);
642 _settings.add_spinslider(SP_ATTR_AZIMUTH, _("Azimuth"), 0, 360, 1, 1, 0);
643 _settings.add_spinslider(SP_ATTR_AZIMUTH, _("Elevation"), 0, 360, 1, 1, 0);
645 _settings.type(LIGHT_POINT);
646 _settings.add_multispinbutton(SP_ATTR_X, SP_ATTR_Y, SP_ATTR_Z, _("Location"), -99999, 99999, 1, 100, 0);
648 _settings.type(LIGHT_SPOT);
649 _settings.add_multispinbutton(SP_ATTR_X, SP_ATTR_Y, SP_ATTR_Z, _("Location"), -99999, 99999, 1, 100, 0);
650 _settings.add_multispinbutton(SP_ATTR_POINTSATX, SP_ATTR_POINTSATY, SP_ATTR_POINTSATZ,
651 _("Points At"), -99999, 99999, 1, 100, 0);
652 _settings.add_spinslider(SP_ATTR_SPECULAREXPONENT, _("Specular Exponent"), 1, 100, 1, 1, 0);
653 _settings.add_spinslider(SP_ATTR_LIMITINGCONEANGLE, _("Cone Angle"), 1, 100, 1, 1, 0);
654 }
656 Gtk::VBox& get_box()
657 {
658 return _box;
659 }
660 protected:
661 Glib::ustring get_as_attribute() const
662 {
663 return "";
664 }
665 void set_from_attribute(SPObject* o)
666 {
667 SPObject* child = o->children;
669 if(SP_IS_FEDISTANTLIGHT(child))
670 _light_source.set_active(0);
671 else if(SP_IS_FEPOINTLIGHT(child))
672 _light_source.set_active(1);
673 else if(SP_IS_FESPOTLIGHT(child))
674 _light_source.set_active(2);
676 update();
677 }
678 private:
679 void on_source_changed()
680 {
681 SPFilterPrimitive* prim = _dialog._primitive_list.get_selected();
682 if(prim) {
683 SPObject* child = prim->children;
684 const int ls = _light_source.get_active_row_number();
685 // Check if the light source type has changed
686 if(!(ls == 0 && SP_IS_FEDISTANTLIGHT(child)) &&
687 !(ls == 1 && SP_IS_FEPOINTLIGHT(child)) &&
688 !(ls == 2 && SP_IS_FESPOTLIGHT(child))) {
689 if(child)
690 sp_repr_unparent(child->repr);
692 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(prim->document);
693 Inkscape::XML::Node *repr = xml_doc->createElement(_light_source.get_active_data()->key.c_str());
694 repr->setAttribute("inkscape:collect", "always");
695 prim->repr->appendChild(repr);
696 Inkscape::GC::release(repr);
697 sp_document_done(prim->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("New light source"));
698 update();
699 }
700 }
701 }
703 void update()
704 {
705 _box.hide_all();
706 _box.show();
707 _light_source.show_all();
709 SPFilterPrimitive* prim = _dialog._primitive_list.get_selected();
710 if(prim && prim->children)
711 _settings.show_and_update(_light_source.get_active_data()->id, prim->children);
712 }
714 FilterEffectsDialog& _dialog;
715 Gtk::VBox _box;
716 Settings _settings;
717 ComboBoxEnum<LightSource> _light_source;
718 };
720 FilterEffectsDialog::LightSourceControl* FilterEffectsDialog::Settings::add_lightsource(const Glib::ustring& label)
721 {
722 LightSourceControl* ls = new LightSourceControl(_dialog);
723 add_attr_widget(ls);
724 add_widget(&ls->get_box(), label);
725 return ls;
726 }
728 Glib::RefPtr<Gtk::Menu> create_popup_menu(Gtk::Widget& parent, sigc::slot<void> dup,
729 sigc::slot<void> rem)
730 {
731 Glib::RefPtr<Gtk::Menu> menu(new Gtk::Menu);
733 menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("_Duplicate"), dup));
734 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
735 menu->append(*mi);
736 mi->signal_activate().connect(rem);
737 mi->show();
738 menu->accelerate(parent);
740 return menu;
741 }
743 /*** FilterModifier ***/
744 FilterEffectsDialog::FilterModifier::FilterModifier(FilterEffectsDialog& d)
745 : _dialog(d), _add(Gtk::Stock::ADD)
746 {
747 Gtk::ScrolledWindow* sw = Gtk::manage(new Gtk::ScrolledWindow);
748 pack_start(*sw);
749 pack_start(_add, false, false);
750 sw->add(_list);
752 _list.set_model(_model);
753 const int selcol = _list.append_column("", _cell_sel);
754 Gtk::TreeViewColumn* col = _list.get_column(selcol - 1);
755 if(col)
756 col->add_attribute(_cell_sel.property_sel(), _columns.sel);
757 _list.append_column(_("_Filter"), _columns.label);
759 sw->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
760 sw->set_shadow_type(Gtk::SHADOW_IN);
761 show_all_children();
762 _add.signal_clicked().connect(sigc::mem_fun(*this, &FilterModifier::add_filter));
763 _list.signal_button_press_event().connect_notify(
764 sigc::mem_fun(*this, &FilterModifier::filter_list_button_press));
765 _list.signal_button_release_event().connect_notify(
766 sigc::mem_fun(*this, &FilterModifier::filter_list_button_release));
767 _menu = create_popup_menu(*this, sigc::mem_fun(*this, &FilterModifier::duplicate_filter),
768 sigc::mem_fun(*this, &FilterModifier::remove_filter));
769 _menu->items().push_back(Gtk::Menu_Helpers::MenuElem(
770 _("R_ename"), sigc::mem_fun(*this, &FilterModifier::rename_filter)));
771 _menu->accelerate(*this);
773 g_signal_connect(G_OBJECT(INKSCAPE), "change_selection",
774 G_CALLBACK(&FilterModifier::on_inkscape_change_selection), this);
776 update_filters();
777 }
779 FilterEffectsDialog::FilterModifier::CellRendererSel::CellRendererSel()
780 : Glib::ObjectBase(typeid(CellRendererSel)),
781 _size(10),
782 _sel(*this, "sel", 0)
783 {}
785 Glib::PropertyProxy<int> FilterEffectsDialog::FilterModifier::CellRendererSel::property_sel()
786 {
787 return _sel.get_proxy();
788 }
790 void FilterEffectsDialog::FilterModifier::CellRendererSel::get_size_vfunc(
791 Gtk::Widget&, const Gdk::Rectangle*, int* x, int* y, int* w, int* h) const
792 {
793 if(x)
794 (*x) = 0;
795 if(y)
796 (*y) = 0;
797 if(w)
798 (*w) = _size;
799 if(h)
800 (*h) = _size;
801 }
803 void FilterEffectsDialog::FilterModifier::CellRendererSel::render_vfunc(
804 const Glib::RefPtr<Gdk::Drawable>& win, Gtk::Widget& widget, const Gdk::Rectangle& bg_area,
805 const Gdk::Rectangle& cell_area, const Gdk::Rectangle& expose_area, Gtk::CellRendererState flags)
806 {
807 const int sel = _sel.get_value();
809 if(sel > 0) {
810 const int s = _size - 2;
811 const int w = cell_area.get_width();
812 const int h = cell_area.get_height();
813 const int x = cell_area.get_x() + w / 2 - s / 2;
814 const int y = cell_area.get_y() + h / 2 - s / 2;
816 win->draw_rectangle(widget.get_style()->get_text_gc(Gtk::STATE_NORMAL), (sel == 1), x, y, s, s);
817 }
818 }
820 // When the selection changes, show the active filter(s) in the dialog
821 void FilterEffectsDialog::FilterModifier::on_inkscape_change_selection(Application *inkscape,
822 Selection *sel,
823 FilterModifier* fm)
824 {
825 if(fm && sel)
826 fm->update_selection(sel);
827 }
829 void FilterEffectsDialog::FilterModifier::update_selection(Selection *sel)
830 {
831 std::set<SPObject*> used;
833 for(GSList const *i = sel->itemList(); i != NULL; i = i->next) {
834 SPObject *obj = SP_OBJECT (i->data);
835 SPStyle *style = SP_OBJECT_STYLE (obj);
836 if(!style || !SP_IS_ITEM(obj)) continue;
838 if(style->filter.set && style->getFilter())
839 used.insert(style->getFilter());
840 else
841 used.insert(0);
842 }
844 const int size = used.size();
846 for(Gtk::TreeIter iter = _model->children().begin();
847 iter != _model->children().end(); ++iter) {
848 if(used.find((*iter)[_columns.filter]) != used.end()) {
849 // If only one filter is in use by the selection, select it
850 if(size == 1)
851 _list.get_selection()->select(iter);
852 (*iter)[_columns.sel] = size;
853 }
854 else
855 (*iter)[_columns.sel] = 0;
856 }
857 }
859 Glib::SignalProxy0<void> FilterEffectsDialog::FilterModifier::signal_selection_changed()
860 {
861 return _list.get_selection()->signal_changed();
862 }
864 SPFilter* FilterEffectsDialog::FilterModifier::get_selected_filter()
865 {
866 if(_list.get_selection()) {
867 Gtk::TreeModel::iterator i = _list.get_selection()->get_selected();
869 if(i)
870 return (*i)[_columns.filter];
871 }
873 return 0;
874 }
876 void FilterEffectsDialog::FilterModifier::select_filter(const SPFilter* filter)
877 {
878 if(filter) {
879 for(Gtk::TreeModel::iterator i = _model->children().begin();
880 i != _model->children().end(); ++i) {
881 if((*i)[_columns.filter] == filter) {
882 _list.get_selection()->select(i);
883 break;
884 }
885 }
886 }
887 }
889 void FilterEffectsDialog::FilterModifier::filter_list_button_press(GdkEventButton* e)
890 {
891 // Double-click
892 if(e->type == GDK_2BUTTON_PRESS) {
893 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
894 SPDocument *doc = sp_desktop_document(desktop);
895 SPFilter* filter = get_selected_filter();
896 Inkscape::Selection *sel = sp_desktop_selection(desktop);
898 GSList const *items = sel->itemList();
900 for (GSList const *i = items; i != NULL; i = i->next) {
901 SPItem * item = SP_ITEM(i->data);
902 SPStyle *style = SP_OBJECT_STYLE(item);
903 g_assert(style != NULL);
905 sp_style_set_property_url(SP_OBJECT(item), "filter", SP_OBJECT(filter), false);
906 SP_OBJECT(item)->requestDisplayUpdate((SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG ));
907 }
909 update_selection(sel);
910 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Apply filter"));
911 }
912 }
914 void FilterEffectsDialog::FilterModifier::filter_list_button_release(GdkEventButton* event)
915 {
916 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
917 const bool sensitive = get_selected_filter() != NULL;
918 _menu->items()[0].set_sensitive(sensitive);
919 _menu->items()[1].set_sensitive(sensitive);
920 _menu->popup(event->button, event->time);
921 }
922 }
924 void FilterEffectsDialog::FilterModifier::add_filter()
925 {
926 SPDocument* doc = sp_desktop_document(SP_ACTIVE_DESKTOP);
927 SPFilter* filter = new_filter(doc);
929 const int count = _model->children().size();
930 std::ostringstream os;
931 os << "filter" << count;
932 filter->setLabel(os.str().c_str());
934 update_filters();
936 select_filter(filter);
938 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter"));
939 }
941 void FilterEffectsDialog::FilterModifier::remove_filter()
942 {
943 SPFilter *filter = get_selected_filter();
945 if(filter) {
946 SPDocument* doc = filter->document;
947 sp_repr_unparent(filter->repr);
949 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Remove filter"));
951 update_filters();
952 }
953 }
955 void FilterEffectsDialog::FilterModifier::duplicate_filter()
956 {
957 SPFilter* filter = get_selected_filter();
959 if(filter) {
960 Inkscape::XML::Node* repr = SP_OBJECT_REPR(filter), *parent = repr->parent();
961 repr = repr->duplicate(repr->document());
962 parent->appendChild(repr);
964 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter"));
966 update_filters();
967 }
968 }
970 void FilterEffectsDialog::FilterModifier::rename_filter()
971 {
972 SPFilter* filter = get_selected_filter();
973 Gtk::Dialog m("", _dialog, true);
974 m.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
975 m.add_button(_("_Rename"), Gtk::RESPONSE_OK);
976 m.set_default_response(Gtk::RESPONSE_OK);
977 Gtk::Label lbl(_("Filter name:"));
978 Gtk::Entry entry;
979 entry.set_text(filter->label() ? filter->label() : "");
980 Gtk::HBox hb;
981 hb.add(lbl);
982 hb.add(entry);
983 hb.set_spacing(12);
984 hb.show_all();
985 m.get_vbox()->add(hb);
986 const int res = m.run();
987 if(res == Gtk::RESPONSE_OK) {
988 filter->setLabel(entry.get_text().c_str());
989 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Rename filter"));
990 Gtk::TreeIter iter = _list.get_selection()->get_selected();
991 if(iter)
992 (*iter)[_columns.label] = entry.get_text();
993 }
994 }
996 FilterEffectsDialog::CellRendererConnection::CellRendererConnection()
997 : Glib::ObjectBase(typeid(CellRendererConnection)),
998 _primitive(*this, "primitive", 0)
999 {}
1001 Glib::PropertyProxy<void*> FilterEffectsDialog::CellRendererConnection::property_primitive()
1002 {
1003 return _primitive.get_proxy();
1004 }
1006 void FilterEffectsDialog::CellRendererConnection::set_text_width(const int w)
1007 {
1008 _text_width = w;
1009 }
1011 int FilterEffectsDialog::CellRendererConnection::get_text_width() const
1012 {
1013 return _text_width;
1014 }
1016 void FilterEffectsDialog::CellRendererConnection::get_size_vfunc(
1017 Gtk::Widget& widget, const Gdk::Rectangle* cell_area,
1018 int* x_offset, int* y_offset, int* width, int* height) const
1019 {
1020 PrimitiveList& primlist = dynamic_cast<PrimitiveList&>(widget);
1022 if(x_offset)
1023 (*x_offset) = 0;
1024 if(y_offset)
1025 (*y_offset) = 0;
1026 if(width)
1027 (*width) = size * primlist.primitive_count() + _text_width * 7;
1028 if(height) {
1029 // Scale the height depending on the number of inputs, unless it's
1030 // the first primitive, in which case there are no connections
1031 SPFilterPrimitive* prim = (SPFilterPrimitive*)_primitive.get_value();
1032 (*height) = size * input_count(prim);
1033 }
1034 }
1036 /*** PrimitiveList ***/
1037 FilterEffectsDialog::PrimitiveList::PrimitiveList(FilterEffectsDialog& d)
1038 : _dialog(d),
1039 _in_drag(0)
1040 {
1041 add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
1042 signal_expose_event().connect(sigc::mem_fun(*this, &PrimitiveList::on_expose_signal));
1044 _model = Gtk::ListStore::create(_columns);
1046 set_reorderable(true);
1048 set_model(_model);
1049 append_column(_("_Type"), _columns.type);
1051 signal_selection_changed().connect(sigc::mem_fun(*this, &PrimitiveList::queue_draw));
1053 _connection_cell.set_text_width(init_text());
1055 int cols_count = append_column(_("Connections"), _connection_cell);
1056 Gtk::TreeViewColumn* col = get_column(cols_count - 1);
1057 if(col)
1058 col->add_attribute(_connection_cell.property_primitive(), _columns.primitive);
1059 }
1061 // Sets up a vertical Pango context/layout, and returns the largest
1062 // width needed to render the FilterPrimitiveInput labels.
1063 int FilterEffectsDialog::PrimitiveList::init_text()
1064 {
1065 // Set up a vertical context+layout
1066 Glib::RefPtr<Pango::Context> context = create_pango_context();
1067 const Pango::Matrix matrix = {0, -1, 1, 0, 0, 0};
1068 context->set_matrix(matrix);
1069 _vertical_layout = Pango::Layout::create(context);
1071 int maxfont = 0;
1072 for(int i = 0; i < FPInputConverter.end; ++i) {
1073 _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
1074 int fontw, fonth;
1075 _vertical_layout->get_pixel_size(fontw, fonth);
1076 if(fonth > maxfont)
1077 maxfont = fonth;
1078 }
1080 return maxfont;
1081 }
1083 Glib::SignalProxy0<void> FilterEffectsDialog::PrimitiveList::signal_selection_changed()
1084 {
1085 return get_selection()->signal_changed();
1086 }
1088 /* Add all filter primitives in the current to the list.
1089 Keeps the same selection if possible, otherwise selects the first element */
1090 void FilterEffectsDialog::PrimitiveList::update()
1091 {
1092 SPFilter* f = _dialog._filter_modifier.get_selected_filter();
1093 const SPFilterPrimitive* active_prim = get_selected();
1094 bool active_found = false;
1096 _model->clear();
1098 if(f) {
1099 _dialog._primitive_box.set_sensitive(true);
1101 for(SPObject *prim_obj = f->children;
1102 prim_obj && SP_IS_FILTER_PRIMITIVE(prim_obj);
1103 prim_obj = prim_obj->next) {
1104 SPFilterPrimitive *prim = SP_FILTER_PRIMITIVE(prim_obj);
1105 if(prim) {
1106 Gtk::TreeModel::Row row = *_model->append();
1107 row[_columns.primitive] = prim;
1108 row[_columns.type_id] = FPConverter.get_id_from_key(prim->repr->name());
1109 row[_columns.type] = FPConverter.get_label(row[_columns.type_id]);
1110 row[_columns.id] = SP_OBJECT_ID(prim);
1112 if(prim == active_prim) {
1113 get_selection()->select(row);
1114 active_found = true;
1115 }
1116 }
1117 }
1119 if(!active_found && _model->children().begin())
1120 get_selection()->select(_model->children().begin());
1121 }
1122 else {
1123 _dialog._primitive_box.set_sensitive(false);
1124 }
1125 }
1127 void FilterEffectsDialog::PrimitiveList::set_menu(Glib::RefPtr<Gtk::Menu> menu)
1128 {
1129 _primitive_menu = menu;
1130 }
1132 SPFilterPrimitive* FilterEffectsDialog::PrimitiveList::get_selected()
1133 {
1134 if(_dialog._filter_modifier.get_selected_filter()) {
1135 Gtk::TreeModel::iterator i = get_selection()->get_selected();
1136 if(i)
1137 return (*i)[_columns.primitive];
1138 }
1140 return 0;
1141 }
1143 void FilterEffectsDialog::PrimitiveList::select(SPFilterPrimitive* prim)
1144 {
1145 for(Gtk::TreeIter i = _model->children().begin();
1146 i != _model->children().end(); ++i) {
1147 if((*i)[_columns.primitive] == prim)
1148 get_selection()->select(i);
1149 }
1150 }
1154 bool FilterEffectsDialog::PrimitiveList::on_expose_signal(GdkEventExpose* e)
1155 {
1156 Gdk::Rectangle clip(e->area.x, e->area.y, e->area.width, e->area.height);
1157 Glib::RefPtr<Gdk::Window> win = get_bin_window();
1158 Glib::RefPtr<Gdk::GC> darkgc = get_style()->get_dark_gc(Gtk::STATE_NORMAL);
1160 SPFilterPrimitive* prim = get_selected();
1161 int row_count = get_model()->children().size();
1163 int fheight = CellRendererConnection::size;
1164 Gdk::Rectangle rct, vis;
1165 Gtk::TreeIter row = get_model()->children().begin();
1166 int text_start_x = 0;
1167 if(row) {
1168 get_cell_area(get_model()->get_path(row), *get_column(1), rct);
1169 get_visible_rect(vis);
1170 int vis_x, vis_y;
1171 tree_to_widget_coords(vis.get_x(), vis.get_y(), vis_x, vis_y);
1173 text_start_x = rct.get_x() + rct.get_width() - _connection_cell.get_text_width() * (FPInputConverter.end + 1) + 1;
1174 for(int i = 0; i < FPInputConverter.end; ++i) {
1175 _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
1176 const int x = text_start_x + _connection_cell.get_text_width() * (i + 1);
1177 get_bin_window()->draw_rectangle(get_style()->get_bg_gc(Gtk::STATE_NORMAL), true, x, vis_y, _connection_cell.get_text_width(), vis.get_height());
1178 get_bin_window()->draw_layout(get_style()->get_text_gc(Gtk::STATE_NORMAL), x + 1, vis_y, _vertical_layout);
1179 get_bin_window()->draw_line(darkgc, x, vis_y, x, vis_y + vis.get_height());
1180 }
1181 }
1183 int row_index = 0;
1184 for(; row != get_model()->children().end(); ++row, ++row_index) {
1185 get_cell_area(get_model()->get_path(row), *get_column(1), rct);
1186 const int x = rct.get_x(), y = rct.get_y(), h = rct.get_height();
1188 // Check mouse state
1189 int mx, my;
1190 Gdk::ModifierType mask;
1191 get_bin_window()->get_pointer(mx, my, mask);
1193 // Outline the bottom of the connection area
1194 const int outline_x = x + fheight * (row_count - row_index);
1195 get_bin_window()->draw_line(darkgc, x, y + h, outline_x, y + h);
1197 // Side outline
1198 get_bin_window()->draw_line(darkgc, outline_x, y - 1, outline_x, y + h);
1200 std::vector<Gdk::Point> con_poly;
1201 int con_drag_y;
1202 bool inside;
1203 const SPFilterPrimitive* row_prim = (*row)[_columns.primitive];
1204 const int inputs = input_count(row_prim);
1206 if(SP_IS_FEMERGE(row_prim)) {
1207 for(int i = 0; i < inputs; ++i) {
1208 inside = do_connection_node(row, i, con_poly, mx, my);
1209 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
1210 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
1211 inside, con_poly);
1213 if(_in_drag == (i + 1))
1214 con_drag_y = con_poly[2].get_y();
1216 if(_in_drag != (i + 1) || row_prim != prim)
1217 draw_connection(row, i, text_start_x, outline_x, con_poly[2].get_y(), row_count);
1218 }
1219 }
1220 else {
1221 // Draw "in" shape
1222 inside = do_connection_node(row, 0, con_poly, mx, my);
1223 con_drag_y = con_poly[2].get_y();
1224 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
1225 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
1226 inside, con_poly);
1228 // Draw "in" connection
1229 if(_in_drag != 1 || row_prim != prim)
1230 draw_connection(row, SP_ATTR_IN, text_start_x, outline_x, con_poly[2].get_y(), row_count);
1232 if(inputs == 2) {
1233 // Draw "in2" shape
1234 inside = do_connection_node(row, 1, con_poly, mx, my);
1235 if(_in_drag == 2)
1236 con_drag_y = con_poly[2].get_y();
1237 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
1238 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
1239 inside, con_poly);
1240 // Draw "in2" connection
1241 if(_in_drag != 2 || row_prim != prim)
1242 draw_connection(row, SP_ATTR_IN2, text_start_x, outline_x, con_poly[2].get_y(), row_count);
1243 }
1244 }
1246 // Draw drag connection
1247 if(row_prim == prim && _in_drag) {
1248 get_bin_window()->draw_line(get_style()->get_black_gc(), outline_x, con_drag_y,
1249 mx, con_drag_y);
1250 get_bin_window()->draw_line(get_style()->get_black_gc(), mx, con_drag_y, mx, my);
1251 }
1252 }
1254 return true;
1255 }
1257 void FilterEffectsDialog::PrimitiveList::draw_connection(const Gtk::TreeIter& input, const int attr,
1258 const int text_start_x, const int x1, const int y1,
1259 const int row_count)
1260 {
1261 int src_id;
1262 const Gtk::TreeIter res = find_result(input, attr, src_id);
1263 Glib::RefPtr<Gdk::GC> gc = get_style()->get_black_gc();
1265 if(res == input) {
1266 // Draw straight connection to a standard input
1267 const int tw = _connection_cell.get_text_width();
1268 gint end_x = text_start_x + tw * (src_id + 1) + (int)(tw * 0.5f) + 1;
1269 get_bin_window()->draw_rectangle(gc, true, end_x-2, y1-2, 5, 5);
1270 get_bin_window()->draw_line(gc, x1, y1, end_x, y1);
1271 }
1272 else if(res != _model->children().end()) {
1273 Gdk::Rectangle rct;
1275 get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
1276 const int fheight = CellRendererConnection::size;
1278 get_cell_area(get_model()->get_path(res), *get_column(1), rct);
1279 const int row_index = find_index(res);
1280 const int x2 = rct.get_x() + fheight * (row_count - row_index) - fheight / 2;
1281 const int y2 = rct.get_y() + rct.get_height();
1283 // Draw an 'L'-shaped connection to another filter primitive
1284 get_bin_window()->draw_line(gc, x1, y1, x2, y1);
1285 get_bin_window()->draw_line(gc, x2, y1, x2, y2);
1286 }
1287 }
1289 // Creates a triangle outline of the connection node and returns true if (x,y) is inside the node
1290 bool FilterEffectsDialog::PrimitiveList::do_connection_node(const Gtk::TreeIter& row, const int input,
1291 std::vector<Gdk::Point>& points,
1292 const int ix, const int iy)
1293 {
1294 Gdk::Rectangle rct;
1295 const int icnt = input_count((*row)[_columns.primitive]);
1297 get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
1298 const int fheight = CellRendererConnection::size;
1300 get_cell_area(_model->get_path(row), *get_column(1), rct);
1301 const float h = rct.get_height() / icnt;
1303 const int x = rct.get_x() + fheight * (_model->children().size() - find_index(row));
1304 const int con_w = (int)(fheight * 0.35f);
1305 const int con_y = (int)(rct.get_y() + (h / 2) - con_w + (input * h));
1306 points.clear();
1307 points.push_back(Gdk::Point(x, con_y));
1308 points.push_back(Gdk::Point(x, con_y + con_w * 2));
1309 points.push_back(Gdk::Point(x - con_w, con_y + con_w));
1311 return ix >= x - h && iy >= con_y && ix <= x && iy <= points[1].get_y();
1312 }
1314 const Gtk::TreeIter FilterEffectsDialog::PrimitiveList::find_result(const Gtk::TreeIter& start,
1315 const int attr, int& src_id)
1316 {
1317 SPFilterPrimitive* prim = (*start)[_columns.primitive];
1318 Gtk::TreeIter target = _model->children().end();
1319 int image;
1321 if(SP_IS_FEMERGE(prim)) {
1322 int c = 0;
1323 for(const SPObject* o = prim->firstChild(); o; o = o->next, ++c) {
1324 if(c == attr && SP_IS_FEMERGENODE(o))
1325 image = SP_FEMERGENODE(o)->input;
1326 }
1327 }
1328 else {
1329 if(attr == SP_ATTR_IN)
1330 image = prim->image_in;
1331 else if(attr == SP_ATTR_IN2) {
1332 if(SP_IS_FEBLEND(prim))
1333 image = SP_FEBLEND(prim)->in2;
1334 else if(SP_IS_FECOMPOSITE(prim))
1335 image = SP_FECOMPOSITE(prim)->in2;
1336 else if(SP_IS_FEDISPLACEMENTMAP(prim))
1337 image = SP_FEDISPLACEMENTMAP(prim)->in2;
1338 else
1339 return target;
1340 }
1341 else
1342 return target;
1343 }
1345 if(image >= 0) {
1346 for(Gtk::TreeIter i = _model->children().begin();
1347 i != start; ++i) {
1348 if(((SPFilterPrimitive*)(*i)[_columns.primitive])->image_out == image)
1349 target = i;
1350 }
1351 return target;
1352 }
1353 else if(image < -1) {
1354 src_id = -(image + 2);
1355 return start;
1356 }
1358 return target;
1359 }
1361 int FilterEffectsDialog::PrimitiveList::find_index(const Gtk::TreeIter& target)
1362 {
1363 int i = 0;
1364 for(Gtk::TreeIter iter = _model->children().begin();
1365 iter != target; ++iter, ++i);
1366 return i;
1367 }
1369 bool FilterEffectsDialog::PrimitiveList::on_button_press_event(GdkEventButton* e)
1370 {
1371 Gtk::TreePath path;
1372 Gtk::TreeViewColumn* col;
1373 const int x = (int)e->x, y = (int)e->y;
1374 int cx, cy;
1376 _drag_prim = 0;
1378 if(get_path_at_pos(x, y, path, col, cx, cy)) {
1379 Gtk::TreeIter iter = _model->get_iter(path);
1380 std::vector<Gdk::Point> points;
1382 _drag_prim = (*iter)[_columns.primitive];
1383 const int icnt = input_count(_drag_prim);
1385 for(int i = 0; i < icnt; ++i) {
1386 if(do_connection_node(_model->get_iter(path), i, points, x, y)) {
1387 _in_drag = i + 1;
1388 break;
1389 }
1390 }
1392 queue_draw();
1393 }
1395 if(_in_drag) {
1396 get_selection()->select(path);
1397 return true;
1398 }
1399 else
1400 return Gtk::TreeView::on_button_press_event(e);
1401 }
1403 bool FilterEffectsDialog::PrimitiveList::on_motion_notify_event(GdkEventMotion* e)
1404 {
1405 queue_draw();
1407 return Gtk::TreeView::on_motion_notify_event(e);
1408 }
1410 bool FilterEffectsDialog::PrimitiveList::on_button_release_event(GdkEventButton* e)
1411 {
1412 SPFilterPrimitive *prim = get_selected(), *target;
1414 if(_in_drag && prim) {
1415 Gtk::TreePath path;
1416 Gtk::TreeViewColumn* col;
1417 int cx, cy;
1419 if(get_path_at_pos((int)e->x, (int)e->y, path, col, cx, cy)) {
1420 const gchar *in_val = 0;
1421 Glib::ustring result;
1422 Gtk::TreeIter target_iter = _model->get_iter(path);
1423 target = (*target_iter)[_columns.primitive];
1425 Gdk::Rectangle rct;
1426 get_cell_area(path, *col, rct);
1427 const int twidth = _connection_cell.get_text_width();
1428 const int sources_x = rct.get_width() - twidth * FPInputConverter.end;
1429 if(cx > sources_x) {
1430 int src = (cx - sources_x) / twidth;
1431 if(src < 0)
1432 src = 0;
1433 else if(src >= FPInputConverter.end)
1434 src = FPInputConverter.end - 1;
1435 result = FPInputConverter.get_key((FilterPrimitiveInput)src);
1436 in_val = result.c_str();
1437 }
1438 else {
1439 // Ensure that the target comes before the selected primitive
1440 for(Gtk::TreeIter iter = _model->children().begin();
1441 iter != get_selection()->get_selected(); ++iter) {
1442 if(iter == target_iter) {
1443 Inkscape::XML::Node *repr = SP_OBJECT_REPR(target);
1444 // Make sure the target has a result
1445 const gchar *gres = repr->attribute("result");
1446 if(!gres) {
1447 result = "result" + Glib::Ascii::dtostr(SP_FILTER(prim->parent)->_image_number_next);
1448 repr->setAttribute("result", result.c_str());
1449 in_val = result.c_str();
1450 }
1451 else
1452 in_val = gres;
1453 break;
1454 }
1455 }
1456 }
1458 if(SP_IS_FEMERGE(prim)) {
1459 int c = 1;
1460 bool handled = false;
1461 for(SPObject* o = prim->firstChild(); o && !handled; o = o->next, ++c) {
1462 if(c == _in_drag && SP_IS_FEMERGENODE(o)) {
1463 // If input is null, delete it
1464 if(!in_val) {
1465 sp_repr_unparent(o->repr);
1466 sp_document_done(prim->document, SP_VERB_DIALOG_FILTER_EFFECTS,
1467 _("Remove merge node"));
1468 (*get_selection()->get_selected())[_columns.primitive] = prim;
1469 }
1470 else
1471 _dialog.set_attr(o, SP_ATTR_IN, in_val);
1472 handled = true;
1473 }
1474 }
1475 // Add new input?
1476 if(!handled && c == _in_drag) {
1477 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(prim->document);
1478 Inkscape::XML::Node *repr = xml_doc->createElement("svg:feMergeNode");
1479 repr->setAttribute("inkscape:collect", "always");
1480 prim->repr->appendChild(repr);
1481 SPFeMergeNode *node = SP_FEMERGENODE(prim->document->getObjectByRepr(repr));
1482 Inkscape::GC::release(repr);
1483 _dialog.set_attr(node, SP_ATTR_IN, in_val);
1484 (*get_selection()->get_selected())[_columns.primitive] = prim;
1485 }
1486 }
1487 else {
1488 if(_in_drag == 1)
1489 _dialog.set_attr(prim, SP_ATTR_IN, in_val);
1490 else if(_in_drag == 2)
1491 _dialog.set_attr(prim, SP_ATTR_IN2, in_val);
1492 }
1493 }
1495 _in_drag = 0;
1496 queue_draw();
1498 _dialog.update_settings_view();
1499 }
1501 if((e->type == GDK_BUTTON_RELEASE) && (e->button == 3)) {
1502 const bool sensitive = get_selected() != NULL;
1503 _primitive_menu->items()[0].set_sensitive(sensitive);
1504 _primitive_menu->items()[1].set_sensitive(sensitive);
1505 _primitive_menu->popup(e->button, e->time);
1507 return true;
1508 }
1509 else
1510 return Gtk::TreeView::on_button_release_event(e);
1511 }
1513 // Checks all of prim's inputs, removes any that use result
1514 void check_single_connection(SPFilterPrimitive* prim, const int result)
1515 {
1516 if(prim && result >= 0) {
1518 if(prim->image_in == result)
1519 SP_OBJECT_REPR(prim)->setAttribute("in", 0);
1521 if(SP_IS_FEBLEND(prim)) {
1522 if(SP_FEBLEND(prim)->in2 == result)
1523 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
1524 }
1525 else if(SP_IS_FECOMPOSITE(prim)) {
1526 if(SP_FECOMPOSITE(prim)->in2 == result)
1527 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
1528 }
1529 }
1530 }
1532 // Remove any connections going to/from prim_iter that forward-reference other primitives
1533 void FilterEffectsDialog::PrimitiveList::sanitize_connections(const Gtk::TreeIter& prim_iter)
1534 {
1535 SPFilterPrimitive *prim = (*prim_iter)[_columns.primitive];
1536 bool before = true;
1538 for(Gtk::TreeIter iter = _model->children().begin();
1539 iter != _model->children().end(); ++iter) {
1540 if(iter == prim_iter)
1541 before = false;
1542 else {
1543 SPFilterPrimitive* cur_prim = (*iter)[_columns.primitive];
1544 if(before)
1545 check_single_connection(cur_prim, prim->image_out);
1546 else
1547 check_single_connection(prim, cur_prim->image_out);
1548 }
1549 }
1550 }
1552 // Reorder the filter primitives to match the list order
1553 void FilterEffectsDialog::PrimitiveList::on_drag_end(const Glib::RefPtr<Gdk::DragContext>&)
1554 {
1555 SPFilter* filter = _dialog._filter_modifier.get_selected_filter();
1556 int ndx = 0;
1558 for(Gtk::TreeModel::iterator iter = _model->children().begin();
1559 iter != _model->children().end(); ++iter, ++ndx) {
1560 SPFilterPrimitive* prim = (*iter)[_columns.primitive];
1561 if(prim) {
1562 SP_OBJECT_REPR(prim)->setPosition(ndx);
1563 if(_drag_prim == prim) {
1564 sanitize_connections(iter);
1565 get_selection()->select(iter);
1566 }
1567 }
1568 }
1570 filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
1572 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Reorder filter primitive"));
1573 }
1575 int FilterEffectsDialog::PrimitiveList::primitive_count() const
1576 {
1577 return _model->children().size();
1578 }
1580 /*** FilterEffectsDialog ***/
1582 FilterEffectsDialog::FilterEffectsDialog()
1583 : Dialog ("dialogs.filtereffects", SP_VERB_DIALOG_FILTER_EFFECTS),
1584 _filter_modifier(*this),
1585 _primitive_list(*this),
1586 _add_primitive_type(FPConverter),
1587 _add_primitive(Gtk::Stock::ADD),
1588 _empty_settings(_("No primitive selected"), Gtk::ALIGN_LEFT),
1589 _locked(false)
1590 {
1591 _settings = new Settings(*this, sigc::mem_fun(*this, &FilterEffectsDialog::set_attr_direct),
1592 NR_FILTER_ENDPRIMITIVETYPE);
1593 _sizegroup = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL);
1594 _sizegroup->set_ignore_hidden();
1596 // Initialize widget hierarchy
1597 Gtk::HPaned* hpaned = Gtk::manage(new Gtk::HPaned);
1598 Gtk::ScrolledWindow* sw_prims = Gtk::manage(new Gtk::ScrolledWindow);
1599 Gtk::HBox* hb_prims = Gtk::manage(new Gtk::HBox);
1600 Gtk::Frame* fr_settings = Gtk::manage(new Gtk::Frame(_("<b>Settings</b>")));
1601 Gtk::Alignment* al_settings = Gtk::manage(new Gtk::Alignment);
1602 get_vbox()->add(*hpaned);
1603 hpaned->pack1(_filter_modifier);
1604 hpaned->pack2(_primitive_box);
1605 _primitive_box.pack_start(*sw_prims);
1606 _primitive_box.pack_start(*hb_prims, false, false);
1607 sw_prims->add(_primitive_list);
1608 hb_prims->pack_end(_add_primitive, false, false);
1609 hb_prims->pack_end(_add_primitive_type, false, false);
1610 get_vbox()->pack_start(*fr_settings, false, false);
1611 fr_settings->add(*al_settings);
1612 al_settings->add(_settings_box);
1614 _primitive_list.signal_selection_changed().connect(
1615 sigc::mem_fun(*this, &FilterEffectsDialog::update_settings_view));
1616 _filter_modifier.signal_selection_changed().connect(
1617 sigc::mem_fun(_primitive_list, &PrimitiveList::update));
1619 sw_prims->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
1620 sw_prims->set_shadow_type(Gtk::SHADOW_IN);
1621 al_settings->set_padding(0, 0, 12, 0);
1622 fr_settings->set_shadow_type(Gtk::SHADOW_NONE);
1623 ((Gtk::Label*)fr_settings->get_label_widget())->set_use_markup();
1624 _add_primitive.signal_clicked().connect(sigc::mem_fun(*this, &FilterEffectsDialog::add_primitive));
1625 _primitive_list.set_menu(create_popup_menu(*this, sigc::mem_fun(*this, &FilterEffectsDialog::duplicate_primitive),
1626 sigc::mem_fun(*this, &FilterEffectsDialog::remove_primitive)));
1628 show_all_children();
1629 init_settings_widgets();
1630 _primitive_list.update();
1631 update_settings_view();
1632 }
1634 FilterEffectsDialog::~FilterEffectsDialog()
1635 {
1636 delete _settings;
1637 }
1639 void FilterEffectsDialog::set_attrs_locked(const bool l)
1640 {
1641 _locked = l;
1642 }
1644 void FilterEffectsDialog::init_settings_widgets()
1645 {
1646 // TODO: Find better range/climb-rate/digits values for the SpinSliders,
1647 // most of the current values are complete guesses!
1649 _empty_settings.set_sensitive(false);
1650 _settings_box.pack_start(_empty_settings);
1652 _settings->type(NR_FILTER_BLEND);
1653 _settings->add_combo(SP_ATTR_MODE, _("Mode"), BlendModeConverter);
1655 _settings->type(NR_FILTER_COLORMATRIX);
1656 _settings->add_combo(SP_ATTR_TYPE, _("Type"), ColorMatrixTypeConverter);
1657 _settings->add_colormatrixvalues(_("Value(s)"));
1659 _settings->type(NR_FILTER_COMPOSITE);
1660 _settings->add_combo(SP_ATTR_OPERATOR, _("Operator"), CompositeOperatorConverter);
1661 _k1 = _settings->add_spinslider(SP_ATTR_K1, _("K1"), -10, 10, 1, 0.01, 1);
1662 _k2 = _settings->add_spinslider(SP_ATTR_K2, _("K2"), -10, 10, 1, 0.01, 1);
1663 _k3 = _settings->add_spinslider(SP_ATTR_K3, _("K3"), -10, 10, 1, 0.01, 1);
1664 _k4 = _settings->add_spinslider(SP_ATTR_K4, _("K4"), -10, 10, 1, 0.01, 1);
1666 _settings->type(NR_FILTER_CONVOLVEMATRIX);
1667 _convolve_order = _settings->add_dualspinbutton(SP_ATTR_ORDER, _("Size"), 1, 5, 1, 1, 0);
1668 _convolve_target = _settings->add_multispinbutton(SP_ATTR_TARGETX, SP_ATTR_TARGETY, _("Target"), 0, 4, 1, 1, 0);
1669 _convolve_matrix = _settings->add_matrix(SP_ATTR_KERNELMATRIX, _("Kernel"));
1670 _convolve_order->signal_attr_changed().connect(sigc::mem_fun(*this, &FilterEffectsDialog::convolve_order_changed));
1671 _settings->add_spinslider(SP_ATTR_DIVISOR, _("Divisor"), 0.01, 10, 1, 0.01, 1);
1672 _settings->add_spinslider(SP_ATTR_BIAS, _("Bias"), -10, 10, 1, 0.01, 1);
1673 _settings->add_combo(SP_ATTR_EDGEMODE, _("Edge Mode"), ConvolveMatrixEdgeModeConverter);
1675 _settings->type(NR_FILTER_DIFFUSELIGHTING);
1676 _settings->add_color(SP_PROP_LIGHTING_COLOR, _("Diffuse Color"));
1677 _settings->add_spinslider(SP_ATTR_SURFACESCALE, _("Surface Scale"), -10, 10, 1, 0.01, 1);
1678 _settings->add_spinslider(SP_ATTR_DIFFUSECONSTANT, _("Constant"), 0, 100, 1, 0.01, 1);
1679 _settings->add_dualspinslider(SP_ATTR_KERNELUNITLENGTH, _("Kernel Unit Length"), 0.01, 10, 1, 0.01, 1);
1680 _settings->add_lightsource(_("Light Source"));
1682 _settings->type(NR_FILTER_DISPLACEMENTMAP);
1683 _settings->add_spinslider(SP_ATTR_SCALE, _("Scale"), 0, 100, 1, 0.01, 1);
1684 _settings->add_combo(SP_ATTR_XCHANNELSELECTOR, _("X Channel"), DisplacementMapChannelConverter);
1685 _settings->add_combo(SP_ATTR_YCHANNELSELECTOR, _("Y Channel"), DisplacementMapChannelConverter);
1687 _settings->type(NR_FILTER_GAUSSIANBLUR);
1688 _settings->add_dualspinslider(SP_ATTR_STDDEVIATION, _("Standard Deviation"), 0.01, 100, 1, 0.01, 1);
1690 _settings->type(NR_FILTER_OFFSET);
1691 _settings->add_spinslider(SP_ATTR_DX, _("Delta X"), -100, 100, 1, 0.01, 1);
1692 _settings->add_spinslider(SP_ATTR_DY, _("Delta Y"), -100, 100, 1, 0.01, 1);
1694 _settings->type(NR_FILTER_SPECULARLIGHTING);
1695 _settings->add_color(SP_PROP_LIGHTING_COLOR, _("Specular Color"));
1696 _settings->add_spinslider(SP_ATTR_SURFACESCALE, _("Surface Scale"), -10, 10, 1, 0.01, 1);
1697 _settings->add_spinslider(SP_ATTR_SPECULARCONSTANT, _("Constant"), 0, 100, 1, 0.01, 1);
1698 _settings->add_spinslider(SP_ATTR_SPECULAREXPONENT, _("Exponent"), 1, 128, 1, 0.01, 1);
1699 _settings->add_dualspinslider(SP_ATTR_KERNELUNITLENGTH, _("Kernel Unit Length"), 0.01, 10, 1, 0.01, 1);
1700 _settings->add_lightsource(_("Light Source"));
1702 _settings->type(NR_FILTER_TURBULENCE);
1703 _settings->add_checkbutton(SP_ATTR_STITCHTILES, _("Stitch Tiles"), "stitch", "noStitch");
1704 _settings->add_dualspinslider(SP_ATTR_BASEFREQUENCY, _("Base Frequency"), 0, 100, 1, 0.01, 1);
1705 _settings->add_spinslider(SP_ATTR_NUMOCTAVES, _("Octaves"), 1, 10, 1, 1, 0);
1706 _settings->add_spinslider(SP_ATTR_SEED, _("Seed"), 0, 1000, 1, 1, 0);
1707 }
1709 void FilterEffectsDialog::add_primitive()
1710 {
1711 SPFilter* filter = _filter_modifier.get_selected_filter();
1713 if(filter) {
1714 SPFilterPrimitive* prim = filter_add_primitive(filter, _add_primitive_type.get_active_data()->id);
1716 _primitive_list.update();
1717 _primitive_list.select(prim);
1719 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter primitive"));
1720 }
1721 }
1723 void FilterEffectsDialog::remove_primitive()
1724 {
1725 SPFilterPrimitive* prim = _primitive_list.get_selected();
1727 if(prim) {
1728 sp_repr_unparent(prim->repr);
1730 sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_FILTER_EFFECTS,
1731 _("Remove filter primitive"));
1733 _primitive_list.update();
1734 }
1735 }
1737 void FilterEffectsDialog::duplicate_primitive()
1738 {
1739 SPFilter* filter = _filter_modifier.get_selected_filter();
1740 SPFilterPrimitive* origprim = _primitive_list.get_selected();
1742 if(filter && origprim) {
1743 Inkscape::XML::Node *repr;
1744 repr = SP_OBJECT_REPR(origprim)->duplicate(SP_OBJECT_REPR(origprim)->document());
1745 SP_OBJECT_REPR(filter)->appendChild(repr);
1747 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter primitive"));
1749 _primitive_list.update();
1750 }
1751 }
1753 void FilterEffectsDialog::convolve_order_changed()
1754 {
1755 _convolve_matrix->set_from_attribute(SP_OBJECT(_primitive_list.get_selected()));
1756 _convolve_target->get_spinbuttons()[0]->get_adjustment()->set_upper(_convolve_order->get_spinbutton1().get_value() - 1);
1757 _convolve_target->get_spinbuttons()[1]->get_adjustment()->set_upper(_convolve_order->get_spinbutton2().get_value() - 1);
1758 }
1760 void FilterEffectsDialog::set_attr_direct(const AttrWidget* input)
1761 {
1762 set_attr(_primitive_list.get_selected(), input->get_attribute(), input->get_as_attribute().c_str());
1763 }
1765 void FilterEffectsDialog::set_child_attr_direct(const AttrWidget* input)
1766 {
1767 set_attr(_primitive_list.get_selected()->children, input->get_attribute(), input->get_as_attribute().c_str());
1768 }
1770 void FilterEffectsDialog::set_attr(SPObject* o, const SPAttributeEnum attr, const gchar* val)
1771 {
1772 if(!_locked) {
1773 SPFilter *filter = _filter_modifier.get_selected_filter();
1774 const gchar* name = (const gchar*)sp_attribute_name(attr);
1775 if(filter && name && o) {
1776 update_settings_sensitivity();
1778 SP_OBJECT_REPR(o)->setAttribute(name, val);
1779 filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
1781 Glib::ustring undokey = "filtereffects:";
1782 undokey += name;
1783 sp_document_maybe_done(filter->document, undokey.c_str(), SP_VERB_DIALOG_FILTER_EFFECTS,
1784 _("Set filter primitive attribute"));
1785 }
1786 }
1787 }
1789 void FilterEffectsDialog::update_settings_view()
1790 {
1791 SPFilterPrimitive* prim = _primitive_list.get_selected();
1793 // Hide all the settings
1794 _settings_box.hide_all();
1795 _settings_box.show();
1797 _settings_box.set_sensitive(false);
1798 _empty_settings.show();
1800 if(prim) {
1801 _settings->show_and_update(FPConverter.get_id_from_key(prim->repr->name()), prim);
1802 _settings_box.set_sensitive(true);
1803 _empty_settings.hide();
1804 }
1806 update_settings_sensitivity();
1807 }
1809 void FilterEffectsDialog::update_settings_sensitivity()
1810 {
1811 SPFilterPrimitive* prim = _primitive_list.get_selected();
1812 const bool use_k = SP_IS_FECOMPOSITE(prim) && SP_FECOMPOSITE(prim)->composite_operator == COMPOSITE_ARITHMETIC;
1813 _k1->set_sensitive(use_k);
1814 _k2->set_sensitive(use_k);
1815 _k3->set_sensitive(use_k);
1816 _k4->set_sensitive(use_k);
1817 }
1819 } // namespace Dialog
1820 } // namespace UI
1821 } // namespace Inkscape
1823 /*
1824 Local Variables:
1825 mode:c++
1826 c-file-style:"stroustrup"
1827 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1828 indent-tabs-mode:nil
1829 fill-column:99
1830 End:
1831 */
1832 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :