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 "filter-enums.h"
36 #include "inkscape.h"
37 #include "selection.h"
38 #include "sp-feblend.h"
39 #include "sp-fecolormatrix.h"
40 #include "sp-fecomposite.h"
41 #include "sp-feconvolvematrix.h"
42 #include "sp-fedisplacementmap.h"
43 #include "sp-fedistantlight.h"
44 #include "sp-femerge.h"
45 #include "sp-femergenode.h"
46 #include "sp-feoffset.h"
47 #include "sp-fepointlight.h"
48 #include "sp-fespotlight.h"
49 #include "sp-filter-primitive.h"
50 #include "sp-gaussian-blur.h"
52 #include "style.h"
53 #include "svg/svg-color.h"
54 #include "verbs.h"
55 #include "xml/node.h"
56 #include "xml/node-observer.h"
57 #include "xml/repr.h"
58 #include <sstream>
60 #include <iostream>
62 using namespace NR;
64 namespace Inkscape {
65 namespace UI {
66 namespace Dialog {
68 // Returns the number of inputs available for the filter primitive type
69 int input_count(const SPFilterPrimitive* prim)
70 {
71 if(!prim)
72 return 0;
73 else if(SP_IS_FEBLEND(prim) || SP_IS_FECOMPOSITE(prim) || SP_IS_FEDISPLACEMENTMAP(prim))
74 return 2;
75 else if(SP_IS_FEMERGE(prim)) {
76 // Return the number of feMergeNode connections plus an extra
77 int count = 1;
78 for(const SPObject* o = prim->firstChild(); o; o = o->next, ++count);
79 return count;
80 }
81 else
82 return 1;
83 }
85 // Very simple observer that just emits a signal if anything happens to a node
86 class FilterEffectsDialog::SignalObserver : public XML::NodeObserver
87 {
88 public:
89 SignalObserver()
90 : _oldsel(0)
91 {}
93 // Add this observer to the SPObject and remove it from any previous object
94 void set(SPObject* o)
95 {
96 if(_oldsel && _oldsel->repr)
97 _oldsel->repr->removeObserver(*this);
98 if(o && o->repr)
99 o->repr->addObserver(*this);
100 _oldsel = o;
101 }
103 void notifyChildAdded(XML::Node&, XML::Node&, XML::Node*)
104 { signal_changed()(); }
106 void notifyChildRemoved(XML::Node&, XML::Node&, XML::Node*)
107 { signal_changed()(); }
109 void notifyChildOrderChanged(XML::Node&, XML::Node&, XML::Node*, XML::Node*)
110 { signal_changed()(); }
112 void notifyContentChanged(XML::Node&, Util::ptr_shared<char>, Util::ptr_shared<char>)
113 {}
115 void notifyAttributeChanged(XML::Node&, GQuark, Util::ptr_shared<char>, Util::ptr_shared<char>)
116 { signal_changed()(); }
118 sigc::signal<void>& signal_changed()
119 {
120 return _signal_changed;
121 }
122 private:
123 sigc::signal<void> _signal_changed;
124 SPObject* _oldsel;
125 };
127 class CheckButtonAttr : public Gtk::CheckButton, public AttrWidget
128 {
129 public:
130 CheckButtonAttr(const Glib::ustring& label,
131 const Glib::ustring& tv, const Glib::ustring& fv,
132 const SPAttributeEnum a)
133 : Gtk::CheckButton(label),
134 AttrWidget(a),
135 _true_val(tv), _false_val(fv)
136 {
137 signal_toggled().connect(signal_attr_changed().make_slot());
138 }
140 Glib::ustring get_as_attribute() const
141 {
142 return get_active() ? _true_val : _false_val;
143 }
145 void set_from_attribute(SPObject* o)
146 {
147 const gchar* val = attribute_value(o);
148 if(val) {
149 if(_true_val == val)
150 set_active(true);
151 else if(_false_val == val)
152 set_active(false);
153 }
154 }
155 private:
156 const Glib::ustring _true_val, _false_val;
157 };
159 class SpinButtonAttr : public Gtk::SpinButton, public AttrWidget
160 {
161 public:
162 SpinButtonAttr(double lower, double upper, double step_inc,
163 double climb_rate, int digits, const SPAttributeEnum a)
164 : Gtk::SpinButton(climb_rate, digits),
165 AttrWidget(a)
166 {
167 set_range(lower, upper);
168 set_increments(step_inc, step_inc * 5);
170 signal_value_changed().connect(signal_attr_changed().make_slot());
171 }
173 Glib::ustring get_as_attribute() const
174 {
175 const double val = get_value();
177 if(get_digits() == 0)
178 return Glib::Ascii::dtostr((int)val);
179 else
180 return Glib::Ascii::dtostr(val);
181 }
183 void set_from_attribute(SPObject* o)
184 {
185 const gchar* val = attribute_value(o);
186 if(val)
187 set_value(Glib::Ascii::strtod(val));
188 }
189 };
191 // Contains an arbitrary number of spin buttons that use seperate attributes
192 class MultiSpinButton : public Gtk::HBox
193 {
194 public:
195 MultiSpinButton(double lower, double upper, double step_inc,
196 double climb_rate, int digits, std::vector<SPAttributeEnum> attrs)
197 {
198 for(unsigned i = 0; i < attrs.size(); ++i) {
199 _spins.push_back(new SpinButtonAttr(lower, upper, step_inc, climb_rate, digits, attrs[i]));
200 pack_start(*_spins.back(), false, false);
201 }
202 }
204 ~MultiSpinButton()
205 {
206 for(unsigned i = 0; i < _spins.size(); ++i)
207 delete _spins[i];
208 }
210 std::vector<SpinButtonAttr*>& get_spinbuttons()
211 {
212 return _spins;
213 }
214 private:
215 std::vector<SpinButtonAttr*> _spins;
216 };
218 // Contains two spinbuttons that describe a NumberOptNumber
219 class DualSpinButton : public Gtk::HBox, public AttrWidget
220 {
221 public:
222 DualSpinButton(double lower, double upper, double step_inc,
223 double climb_rate, int digits, const SPAttributeEnum a)
224 : AttrWidget(a),
225 _s1(climb_rate, digits), _s2(climb_rate, digits)
226 {
227 _s1.set_range(lower, upper);
228 _s2.set_range(lower, upper);
229 _s1.set_increments(step_inc, step_inc * 5);
230 _s2.set_increments(step_inc, step_inc * 5);
232 _s1.signal_value_changed().connect(signal_attr_changed().make_slot());
233 _s2.signal_value_changed().connect(signal_attr_changed().make_slot());
235 pack_start(_s1, false, false);
236 pack_start(_s2, false, false);
237 }
239 Gtk::SpinButton& get_spinbutton1()
240 {
241 return _s1;
242 }
244 Gtk::SpinButton& get_spinbutton2()
245 {
246 return _s2;
247 }
249 virtual Glib::ustring get_as_attribute() const
250 {
251 double v1 = _s1.get_value();
252 double v2 = _s2.get_value();
254 if(_s1.get_digits() == 0) {
255 v1 = (int)v1;
256 v2 = (int)v2;
257 }
259 return Glib::Ascii::dtostr(v1) + " " + Glib::Ascii::dtostr(v2);
260 }
262 virtual void set_from_attribute(SPObject* o)
263 {
264 const gchar* val = attribute_value(o);
265 if(val) {
266 NumberOptNumber n;
267 n.set(val);
268 _s1.set_value(n.getNumber());
269 _s2.set_value(n.getOptNumber());
270 }
271 }
272 private:
273 Gtk::SpinButton _s1, _s2;
274 };
276 class ColorButton : public Gtk::ColorButton, public AttrWidget
277 {
278 public:
279 ColorButton(const SPAttributeEnum a)
280 : AttrWidget(a)
281 {
282 signal_color_set().connect(signal_attr_changed().make_slot());
284 Gdk::Color col;
285 col.set_rgb(65535, 65535, 65535);
286 set_color(col);
287 }
289 // Returns the color in 'rgb(r,g,b)' form.
290 Glib::ustring get_as_attribute() const
291 {
292 std::ostringstream os;
293 const Gdk::Color c = get_color();
294 const int r = (c.get_red() + 1) / 256 - 1, g = (c.get_green() + 1) / 256 - 1, b = (c.get_blue() + 1) / 256 - 1;
295 os << "rgb(" << r << "," << g << "," << b << ")";
296 return os.str();
297 }
300 void set_from_attribute(SPObject* o)
301 {
302 const gchar* val = attribute_value(o);
303 if(val) {
304 const guint32 i = sp_svg_read_color(val, 0xFFFFFFFF);
305 const int r = SP_RGBA32_R_U(i) + 1, g = SP_RGBA32_G_U(i) + 1, b = SP_RGBA32_B_U(i) + 1;
306 Gdk::Color col;
307 col.set_rgb(r * 256 - 1, g * 256 - 1, b * 256 - 1);
308 set_color(col);
309 }
310 }
311 };
313 /* Displays/Edits the matrix for feConvolveMatrix or feColorMatrix */
314 class FilterEffectsDialog::MatrixAttr : public Gtk::Frame, public AttrWidget
315 {
316 public:
317 MatrixAttr(const SPAttributeEnum a)
318 : AttrWidget(a)
319 {
320 _model = Gtk::ListStore::create(_columns);
321 _tree.set_model(_model);
322 _tree.set_headers_visible(false);
323 _tree.show();
324 add(_tree);
325 set_shadow_type(Gtk::SHADOW_IN);
326 }
328 Glib::ustring get_as_attribute() const
329 {
330 std::ostringstream os;
332 for(Gtk::TreeIter iter = _model->children().begin();
333 iter != _model->children().end(); ++iter) {
334 for(unsigned c = 0; c < _tree.get_columns().size(); ++c) {
335 os << (*iter)[_columns.cols[c]] << " ";
336 }
337 }
339 return os.str();
340 }
342 void set_from_attribute(SPObject* o)
343 {
344 if(o) {
345 if(SP_IS_FECONVOLVEMATRIX(o)) {
346 SPFeConvolveMatrix* conv = SP_FECONVOLVEMATRIX(o);
347 int cols, rows;
348 cols = (int)conv->order.getNumber();
349 if(cols > 5)
350 cols = 5;
351 rows = conv->order.optNumber_set ? (int)conv->order.getOptNumber() : cols;
352 update(o, rows, cols);
353 }
354 else if(SP_IS_FECOLORMATRIX(o))
355 update(o, 4, 5);
356 }
357 }
358 private:
359 class MatrixColumns : public Gtk::TreeModel::ColumnRecord
360 {
361 public:
362 MatrixColumns()
363 {
364 cols.resize(5);
365 for(unsigned i = 0; i < cols.size(); ++i)
366 add(cols[i]);
367 }
368 std::vector<Gtk::TreeModelColumn<double> > cols;
369 };
371 void update(SPObject* o, const int rows, const int cols)
372 {
373 _model->clear();
375 _tree.remove_all_columns();
377 std::vector<gdouble>* values = NULL;
378 if(SP_IS_FECOLORMATRIX(o))
379 values = &SP_FECOLORMATRIX(o)->values;
380 else if(SP_IS_FECONVOLVEMATRIX(o))
381 values = &SP_FECONVOLVEMATRIX(o)->kernelMatrix;
382 else
383 return;
385 if(o) {
386 int ndx = 0;
388 for(int i = 0; i < cols; ++i) {
389 _tree.append_column_numeric_editable("", _columns.cols[i], "%.2f");
390 dynamic_cast<Gtk::CellRendererText*>(
391 _tree.get_column(i)->get_first_cell_renderer())->signal_edited().connect(
392 sigc::mem_fun(*this, &MatrixAttr::rebind));
393 }
395 for(int r = 0; r < rows; ++r) {
396 Gtk::TreeRow row = *(_model->append());
397 for(int c = 0; c < cols; ++c, ++ndx)
398 row[_columns.cols[c]] = ndx < (int)values->size() ? (*values)[ndx] : 0;
399 }
400 }
401 }
403 void rebind(const Glib::ustring&, const Glib::ustring&)
404 {
405 signal_attr_changed()();
406 }
408 Gtk::TreeView _tree;
409 Glib::RefPtr<Gtk::ListStore> _model;
410 MatrixColumns _columns;
411 };
413 // Displays a matrix or a slider for feColorMatrix
414 class FilterEffectsDialog::ColorMatrixValues : public Gtk::Frame, public AttrWidget
415 {
416 public:
417 ColorMatrixValues()
418 : AttrWidget(SP_ATTR_VALUES),
419 _matrix(SP_ATTR_VALUES),
420 _saturation(0, 0, 1, 0.1, 0.01, 2, SP_ATTR_VALUES),
421 _angle(0, 0, 360, 0.1, 0.01, 1, SP_ATTR_VALUES),
422 _label(_("None"), Gtk::ALIGN_LEFT)
423 {
424 _matrix.signal_attr_changed().connect(signal_attr_changed().make_slot());
425 _saturation.signal_attr_changed().connect(signal_attr_changed().make_slot());
426 _angle.signal_attr_changed().connect(signal_attr_changed().make_slot());
428 _matrix.show();
429 _saturation.show();
430 _angle.show();
431 _label.show();
432 _label.set_sensitive(false);
434 set_shadow_type(Gtk::SHADOW_NONE);
435 }
437 virtual void set_from_attribute(SPObject* o)
438 {
439 if(SP_IS_FECOLORMATRIX(o)) {
440 SPFeColorMatrix* col = SP_FECOLORMATRIX(o);
441 remove();
442 switch(col->type) {
443 case COLORMATRIX_SATURATE:
444 add(_saturation);
445 _saturation.set_from_attribute(o);
446 break;
447 case COLORMATRIX_HUEROTATE:
448 add(_angle);
449 _angle.set_from_attribute(o);
450 break;
451 case COLORMATRIX_LUMINANCETOALPHA:
452 add(_label);
453 break;
454 case COLORMATRIX_MATRIX:
455 default:
456 add(_matrix);
457 _matrix.set_from_attribute(o);
458 break;
459 }
460 }
461 }
463 virtual Glib::ustring get_as_attribute() const
464 {
465 const Widget* w = get_child();
466 if(w == &_label)
467 return "";
468 else
469 return dynamic_cast<const AttrWidget*>(w)->get_as_attribute();
470 }
471 private:
472 MatrixAttr _matrix;
473 SpinSlider _saturation;
474 SpinSlider _angle;
475 Gtk::Label _label;
476 };
478 class FilterEffectsDialog::Settings
479 {
480 public:
481 typedef sigc::slot<void, const AttrWidget*> SetAttrSlot;
483 Settings(FilterEffectsDialog& d, SetAttrSlot slot, const int maxtypes)
484 : _dialog(d), _set_attr_slot(slot), _current_type(-1), _max_types(maxtypes)
485 {
486 _groups.resize(_max_types);
487 _attrwidgets.resize(_max_types);
489 for(int i = 0; i < _max_types; ++i) {
490 _groups[i] = new Gtk::VBox;
491 d._settings_box.add(*_groups[i]);
492 }
493 }
495 ~Settings()
496 {
497 for(int i = 0; i < _max_types; ++i) {
498 delete _groups[i];
499 for(unsigned j = 0; j < _attrwidgets[i].size(); ++j)
500 delete _attrwidgets[i][j];
501 }
502 }
504 // Show the active settings group and update all the AttrWidgets with new values
505 void show_and_update(const int t, SPObject* ob)
506 {
507 if(t != _current_type) {
508 type(t);
509 for(unsigned i = 0; i < _groups.size(); ++i)
510 _groups[i]->hide();
511 }
512 _groups[t]->show_all();
514 _dialog.set_attrs_locked(true);
515 for(unsigned i = 0; i < _attrwidgets[_current_type].size(); ++i)
516 _attrwidgets[_current_type][i]->set_from_attribute(ob);
517 _dialog.set_attrs_locked(false);
518 }
520 int get_current_type() const
521 {
522 return _current_type;
523 }
525 void type(const int t)
526 {
527 _current_type = t;
528 }
530 // LightSource
531 LightSourceControl* add_lightsource(const Glib::ustring& label);
533 // CheckBox
534 CheckButtonAttr* add_checkbutton(const SPAttributeEnum attr, const Glib::ustring& label,
535 const Glib::ustring& tv, const Glib::ustring& fv)
536 {
537 CheckButtonAttr* cb = new CheckButtonAttr(label, tv, fv, attr);
538 add_widget(cb, "");
539 add_attr_widget(cb);
540 return cb;
541 }
543 // ColorButton
544 ColorButton* add_color(const SPAttributeEnum attr, const Glib::ustring& label)
545 {
546 ColorButton* col = new ColorButton(attr);
547 add_widget(col, label);
548 add_attr_widget(col);
549 return col;
550 }
552 // Matrix
553 MatrixAttr* add_matrix(const SPAttributeEnum attr, const Glib::ustring& label)
554 {
555 MatrixAttr* conv = new MatrixAttr(attr);
556 add_widget(conv, label);
557 add_attr_widget(conv);
558 return conv;
559 }
561 // ColorMatrixValues
562 ColorMatrixValues* add_colormatrixvalues(const Glib::ustring& label)
563 {
564 ColorMatrixValues* cmv = new ColorMatrixValues;
565 add_widget(cmv, label);
566 add_attr_widget(cmv);
567 return cmv;
568 }
570 // SpinSlider
571 SpinSlider* add_spinslider(const SPAttributeEnum attr, const Glib::ustring& label,
572 const double lo, const double hi, const double step_inc, const double climb, const int digits)
573 {
574 SpinSlider* spinslider = new SpinSlider(lo, lo, hi, step_inc, climb, digits, attr);
575 add_widget(spinslider, label);
576 add_attr_widget(spinslider);
577 return spinslider;
578 }
580 // DualSpinSlider
581 DualSpinSlider* add_dualspinslider(const SPAttributeEnum attr, const Glib::ustring& label,
582 const double lo, const double hi, const double step_inc,
583 const double climb, const int digits)
584 {
585 DualSpinSlider* dss = new DualSpinSlider(lo, lo, hi, step_inc, climb, digits, attr);
586 add_widget(dss, label);
587 add_attr_widget(dss);
588 return dss;
589 }
591 // DualSpinButton
592 DualSpinButton* add_dualspinbutton(const SPAttributeEnum attr, const Glib::ustring& label,
593 const double lo, const double hi, const double step_inc,
594 const double climb, const int digits)
595 {
596 DualSpinButton* dsb = new DualSpinButton(lo, hi, step_inc, climb, digits, attr);
597 add_widget(dsb, label);
598 add_attr_widget(dsb);
599 return dsb;
600 }
602 // MultiSpinButton
603 MultiSpinButton* add_multispinbutton(const SPAttributeEnum attr1, const SPAttributeEnum attr2,
604 const Glib::ustring& label, const double lo, const double hi,
605 const double step_inc, const double climb, const int digits)
606 {
607 std::vector<SPAttributeEnum> attrs;
608 attrs.push_back(attr1);
609 attrs.push_back(attr2);
610 MultiSpinButton* msb = new MultiSpinButton(lo, hi, step_inc, climb, digits, attrs);
611 add_widget(msb, label);
612 for(unsigned i = 0; i < msb->get_spinbuttons().size(); ++i)
613 add_attr_widget(msb->get_spinbuttons()[i]);
614 return msb;
615 }
616 MultiSpinButton* add_multispinbutton(const SPAttributeEnum attr1, const SPAttributeEnum attr2,
617 const SPAttributeEnum attr3, const Glib::ustring& label, const double lo,
618 const double hi, const double step_inc, const double climb, const int digits)
619 {
620 std::vector<SPAttributeEnum> attrs;
621 attrs.push_back(attr1);
622 attrs.push_back(attr2);
623 attrs.push_back(attr3);
624 MultiSpinButton* msb = new MultiSpinButton(lo, hi, step_inc, climb, digits, attrs);
625 add_widget(msb, label);
626 for(unsigned i = 0; i < msb->get_spinbuttons().size(); ++i)
627 add_attr_widget(msb->get_spinbuttons()[i]);
628 return msb;
629 }
631 // ComboBoxEnum
632 template<typename T> ComboBoxEnum<T>* add_combo(const SPAttributeEnum attr,
633 const Glib::ustring& label,
634 const Util::EnumDataConverter<T>& conv)
635 {
636 ComboBoxEnum<T>* combo = new ComboBoxEnum<T>(conv, attr);
637 add_widget(combo, label);
638 add_attr_widget(combo);
639 return combo;
640 }
641 private:
642 void add_attr_widget(AttrWidget* a)
643 {
644 _attrwidgets[_current_type].push_back(a);
645 a->signal_attr_changed().connect(sigc::bind(_set_attr_slot, a));
646 }
648 /* Adds a new settings widget using the specified label. The label will be formatted with a colon
649 and all widgets within the setting group are aligned automatically. */
650 void add_widget(Gtk::Widget* w, const Glib::ustring& label)
651 {
652 Gtk::Label *lbl = Gtk::manage(new Gtk::Label(label + (label == "" ? "" : ":"), Gtk::ALIGN_LEFT));
653 Gtk::HBox *hb = Gtk::manage(new Gtk::HBox);
654 hb->set_spacing(12);
655 hb->pack_start(*lbl, false, false);
656 hb->pack_start(*w);
657 _groups[_current_type]->pack_start(*hb);
659 _dialog._sizegroup->add_widget(*lbl);
661 hb->show();
662 lbl->show();
664 w->show();
665 }
667 std::vector<Gtk::VBox*> _groups;
669 FilterEffectsDialog& _dialog;
670 SetAttrSlot _set_attr_slot;
671 std::vector<std::vector<AttrWidget*> > _attrwidgets;
672 int _current_type, _max_types;
673 };
675 // Settings for the three light source objects
676 class FilterEffectsDialog::LightSourceControl : public AttrWidget
677 {
678 public:
679 LightSourceControl(FilterEffectsDialog& d)
680 : AttrWidget(SP_ATTR_INVALID),
681 _dialog(d),
682 _settings(d, sigc::mem_fun(_dialog, &FilterEffectsDialog::set_child_attr_direct), LIGHT_ENDSOURCE),
683 _light_source(LightSourceConverter)
684 {
685 _box.add(_light_source);
686 _box.reorder_child(_light_source, 0);
687 _light_source.signal_changed().connect(sigc::mem_fun(*this, &LightSourceControl::on_source_changed));
689 // FIXME: these range values are complete crap
691 _settings.type(LIGHT_DISTANT);
692 _settings.add_spinslider(SP_ATTR_AZIMUTH, _("Azimuth"), 0, 360, 1, 1, 0);
693 _settings.add_spinslider(SP_ATTR_AZIMUTH, _("Elevation"), 0, 360, 1, 1, 0);
695 _settings.type(LIGHT_POINT);
696 _settings.add_multispinbutton(SP_ATTR_X, SP_ATTR_Y, SP_ATTR_Z, _("Location"), -99999, 99999, 1, 100, 0);
698 _settings.type(LIGHT_SPOT);
699 _settings.add_multispinbutton(SP_ATTR_X, SP_ATTR_Y, SP_ATTR_Z, _("Location"), -99999, 99999, 1, 100, 0);
700 _settings.add_multispinbutton(SP_ATTR_POINTSATX, SP_ATTR_POINTSATY, SP_ATTR_POINTSATZ,
701 _("Points At"), -99999, 99999, 1, 100, 0);
702 _settings.add_spinslider(SP_ATTR_SPECULAREXPONENT, _("Specular Exponent"), 1, 100, 1, 1, 0);
703 _settings.add_spinslider(SP_ATTR_LIMITINGCONEANGLE, _("Cone Angle"), 1, 100, 1, 1, 0);
704 }
706 Gtk::VBox& get_box()
707 {
708 return _box;
709 }
710 protected:
711 Glib::ustring get_as_attribute() const
712 {
713 return "";
714 }
715 void set_from_attribute(SPObject* o)
716 {
717 SPObject* child = o->children;
719 if(SP_IS_FEDISTANTLIGHT(child))
720 _light_source.set_active(0);
721 else if(SP_IS_FEPOINTLIGHT(child))
722 _light_source.set_active(1);
723 else if(SP_IS_FESPOTLIGHT(child))
724 _light_source.set_active(2);
726 update();
727 }
728 private:
729 void on_source_changed()
730 {
731 SPFilterPrimitive* prim = _dialog._primitive_list.get_selected();
732 if(prim) {
733 SPObject* child = prim->children;
734 const int ls = _light_source.get_active_row_number();
735 // Check if the light source type has changed
736 if(!(ls == 0 && SP_IS_FEDISTANTLIGHT(child)) &&
737 !(ls == 1 && SP_IS_FEPOINTLIGHT(child)) &&
738 !(ls == 2 && SP_IS_FESPOTLIGHT(child))) {
739 if(child)
740 sp_repr_unparent(child->repr);
742 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(prim->document);
743 Inkscape::XML::Node *repr = xml_doc->createElement(_light_source.get_active_data()->key.c_str());
744 repr->setAttribute("inkscape:collect", "always");
745 prim->repr->appendChild(repr);
746 Inkscape::GC::release(repr);
747 sp_document_done(prim->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("New light source"));
748 update();
749 }
750 }
751 }
753 void update()
754 {
755 _box.hide_all();
756 _box.show();
757 _light_source.show_all();
759 SPFilterPrimitive* prim = _dialog._primitive_list.get_selected();
760 if(prim && prim->children)
761 _settings.show_and_update(_light_source.get_active_data()->id, prim->children);
762 }
764 FilterEffectsDialog& _dialog;
765 Gtk::VBox _box;
766 Settings _settings;
767 ComboBoxEnum<LightSource> _light_source;
768 };
770 FilterEffectsDialog::LightSourceControl* FilterEffectsDialog::Settings::add_lightsource(const Glib::ustring& label)
771 {
772 LightSourceControl* ls = new LightSourceControl(_dialog);
773 add_attr_widget(ls);
774 add_widget(&ls->get_box(), label);
775 return ls;
776 }
778 Glib::RefPtr<Gtk::Menu> create_popup_menu(Gtk::Widget& parent, sigc::slot<void> dup,
779 sigc::slot<void> rem)
780 {
781 Glib::RefPtr<Gtk::Menu> menu(new Gtk::Menu);
783 menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("_Duplicate"), dup));
784 Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
785 menu->append(*mi);
786 mi->signal_activate().connect(rem);
787 mi->show();
788 menu->accelerate(parent);
790 return menu;
791 }
793 /*** FilterModifier ***/
794 FilterEffectsDialog::FilterModifier::FilterModifier(FilterEffectsDialog& d)
795 : _dialog(d), _add(Gtk::Stock::ADD), _observer(new SignalObserver)
796 {
797 Gtk::ScrolledWindow* sw = Gtk::manage(new Gtk::ScrolledWindow);
798 pack_start(*sw);
799 pack_start(_add, false, false);
800 sw->add(_list);
802 _model = Gtk::ListStore::create(_columns);
803 _list.set_model(_model);
804 const int selcol = _list.append_column("", _cell_sel);
805 Gtk::TreeViewColumn* col = _list.get_column(selcol - 1);
806 if(col)
807 col->add_attribute(_cell_sel.property_sel(), _columns.sel);
808 _list.append_column(_("_Filter"), _columns.label);
810 sw->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
811 sw->set_shadow_type(Gtk::SHADOW_IN);
812 show_all_children();
813 _add.signal_clicked().connect(sigc::mem_fun(*this, &FilterModifier::add_filter));
814 _list.signal_button_press_event().connect_notify(
815 sigc::mem_fun(*this, &FilterModifier::filter_list_button_press));
816 _list.signal_button_release_event().connect_notify(
817 sigc::mem_fun(*this, &FilterModifier::filter_list_button_release));
818 _menu = create_popup_menu(*this, sigc::mem_fun(*this, &FilterModifier::duplicate_filter),
819 sigc::mem_fun(*this, &FilterModifier::remove_filter));
820 _menu->items().push_back(Gtk::Menu_Helpers::MenuElem(
821 _("R_ename"), sigc::mem_fun(*this, &FilterModifier::rename_filter)));
822 _menu->accelerate(*this);
824 _list.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &FilterModifier::on_filter_selection_changed));
825 _observer->signal_changed().connect(signal_filter_changed().make_slot());
826 g_signal_connect(G_OBJECT(INKSCAPE), "change_selection",
827 G_CALLBACK(&FilterModifier::on_inkscape_change_selection), this);
829 g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
830 G_CALLBACK(&FilterModifier::on_activate_desktop), this);
832 on_activate_desktop(INKSCAPE, SP_ACTIVE_DESKTOP, this);
833 update_filters();
834 }
836 FilterEffectsDialog::FilterModifier::~FilterModifier()
837 {
838 _resource_changed.disconnect();
839 _doc_replaced.disconnect();
840 }
842 FilterEffectsDialog::FilterModifier::CellRendererSel::CellRendererSel()
843 : Glib::ObjectBase(typeid(CellRendererSel)),
844 _size(10),
845 _sel(*this, "sel", 0)
846 {}
848 void FilterEffectsDialog::FilterModifier::CellRendererSel::get_size_vfunc(
849 Gtk::Widget&, const Gdk::Rectangle*, int* x, int* y, int* w, int* h) const
850 {
851 if(x)
852 (*x) = 0;
853 if(y)
854 (*y) = 0;
855 if(w)
856 (*w) = _size;
857 if(h)
858 (*h) = _size;
859 }
861 void FilterEffectsDialog::FilterModifier::CellRendererSel::render_vfunc(
862 const Glib::RefPtr<Gdk::Drawable>& win, Gtk::Widget& widget, const Gdk::Rectangle& bg_area,
863 const Gdk::Rectangle& cell_area, const Gdk::Rectangle& expose_area, Gtk::CellRendererState flags)
864 {
865 const int sel = _sel.get_value();
867 if(sel > 0) {
868 const int s = _size - 2;
869 const int w = cell_area.get_width();
870 const int h = cell_area.get_height();
871 const int x = cell_area.get_x() + w / 2 - s / 2;
872 const int y = cell_area.get_y() + h / 2 - s / 2;
874 win->draw_rectangle(widget.get_style()->get_text_gc(Gtk::STATE_NORMAL), (sel == 1), x, y, s, s);
875 }
876 }
878 void FilterEffectsDialog::FilterModifier::on_activate_desktop(Application*, SPDesktop* desktop, FilterModifier* me)
879 {
880 me->update_filters();
882 me->_doc_replaced.disconnect();
883 me->_doc_replaced = desktop->connectDocumentReplaced(
884 sigc::mem_fun(me, &FilterModifier::on_document_replaced));
886 me->_resource_changed.disconnect();
887 me->_resource_changed =
888 sp_document_resources_changed_connect(sp_desktop_document(desktop), "filter",
889 sigc::mem_fun(me, &FilterModifier::update_filters));
890 }
893 // When the selection changes, show the active filter(s) in the dialog
894 void FilterEffectsDialog::FilterModifier::on_inkscape_change_selection(Application *inkscape,
895 Selection *sel,
896 FilterModifier* fm)
897 {
898 if(fm && sel)
899 fm->update_selection(sel);
900 }
902 void FilterEffectsDialog::FilterModifier::update_selection(Selection *sel)
903 {
904 std::set<SPObject*> used;
906 for(GSList const *i = sel->itemList(); i != NULL; i = i->next) {
907 SPObject *obj = SP_OBJECT (i->data);
908 SPStyle *style = SP_OBJECT_STYLE (obj);
909 if(!style || !SP_IS_ITEM(obj)) continue;
911 if(style->filter.set && style->getFilter())
912 used.insert(style->getFilter());
913 else
914 used.insert(0);
915 }
917 const int size = used.size();
919 for(Gtk::TreeIter iter = _model->children().begin();
920 iter != _model->children().end(); ++iter) {
921 if(used.find((*iter)[_columns.filter]) != used.end()) {
922 // If only one filter is in use by the selection, select it
923 if(size == 1)
924 _list.get_selection()->select(iter);
925 (*iter)[_columns.sel] = size;
926 }
927 else
928 (*iter)[_columns.sel] = 0;
929 }
930 }
932 void FilterEffectsDialog::FilterModifier::on_filter_selection_changed()
933 {
934 _observer->set(get_selected_filter());
935 signal_filter_changed()();
936 }
938 /* Add all filters in the document to the combobox.
939 Keeps the same selection if possible, otherwise selects the first element */
940 void FilterEffectsDialog::FilterModifier::update_filters()
941 {
942 SPDesktop* desktop = SP_ACTIVE_DESKTOP;
943 SPDocument* document = sp_desktop_document(desktop);
944 const GSList* filters = sp_document_get_resource_list(document, "filter");
946 _model->clear();
948 for(const GSList *l = filters; l; l = l->next) {
949 Gtk::TreeModel::Row row = *_model->append();
950 SPFilter* f = (SPFilter*)l->data;
951 row[_columns.filter] = f;
952 const gchar* lbl = f->label();
953 const gchar* id = SP_OBJECT_ID(f);
954 row[_columns.label] = lbl ? lbl : (id ? id : "filter");
955 }
956 }
958 SPFilter* FilterEffectsDialog::FilterModifier::get_selected_filter()
959 {
960 if(_list.get_selection()) {
961 Gtk::TreeModel::iterator i = _list.get_selection()->get_selected();
963 if(i)
964 return (*i)[_columns.filter];
965 }
967 return 0;
968 }
970 void FilterEffectsDialog::FilterModifier::select_filter(const SPFilter* filter)
971 {
972 if(filter) {
973 for(Gtk::TreeModel::iterator i = _model->children().begin();
974 i != _model->children().end(); ++i) {
975 if((*i)[_columns.filter] == filter) {
976 _list.get_selection()->select(i);
977 break;
978 }
979 }
980 }
981 }
983 void FilterEffectsDialog::FilterModifier::filter_list_button_press(GdkEventButton* e)
984 {
985 // Double-click
986 if(e->type == GDK_2BUTTON_PRESS) {
987 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
988 SPDocument *doc = sp_desktop_document(desktop);
989 SPFilter* filter = get_selected_filter();
990 Inkscape::Selection *sel = sp_desktop_selection(desktop);
992 GSList const *items = sel->itemList();
994 for (GSList const *i = items; i != NULL; i = i->next) {
995 SPItem * item = SP_ITEM(i->data);
996 SPStyle *style = SP_OBJECT_STYLE(item);
997 g_assert(style != NULL);
999 sp_style_set_property_url(SP_OBJECT(item), "filter", SP_OBJECT(filter), false);
1000 SP_OBJECT(item)->requestDisplayUpdate((SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG ));
1001 }
1003 update_selection(sel);
1004 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Apply filter"));
1005 }
1006 }
1008 void FilterEffectsDialog::FilterModifier::filter_list_button_release(GdkEventButton* event)
1009 {
1010 if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
1011 const bool sensitive = get_selected_filter() != NULL;
1012 _menu->items()[0].set_sensitive(sensitive);
1013 _menu->items()[1].set_sensitive(sensitive);
1014 _menu->popup(event->button, event->time);
1015 }
1016 }
1018 void FilterEffectsDialog::FilterModifier::add_filter()
1019 {
1020 SPDocument* doc = sp_desktop_document(SP_ACTIVE_DESKTOP);
1021 SPFilter* filter = new_filter(doc);
1023 const int count = _model->children().size();
1024 std::ostringstream os;
1025 os << "filter" << count;
1026 filter->setLabel(os.str().c_str());
1028 update_filters();
1030 select_filter(filter);
1032 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter"));
1033 }
1035 void FilterEffectsDialog::FilterModifier::remove_filter()
1036 {
1037 SPFilter *filter = get_selected_filter();
1039 if(filter) {
1040 SPDocument* doc = filter->document;
1041 sp_repr_unparent(filter->repr);
1043 sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Remove filter"));
1045 update_filters();
1046 }
1047 }
1049 void FilterEffectsDialog::FilterModifier::duplicate_filter()
1050 {
1051 SPFilter* filter = get_selected_filter();
1053 if(filter) {
1054 Inkscape::XML::Node* repr = SP_OBJECT_REPR(filter), *parent = repr->parent();
1055 repr = repr->duplicate(repr->document());
1056 parent->appendChild(repr);
1058 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter"));
1060 update_filters();
1061 }
1062 }
1064 void FilterEffectsDialog::FilterModifier::rename_filter()
1065 {
1066 SPFilter* filter = get_selected_filter();
1067 Gtk::Dialog m("", _dialog, true);
1068 m.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1069 m.add_button(_("_Rename"), Gtk::RESPONSE_OK);
1070 m.set_default_response(Gtk::RESPONSE_OK);
1071 Gtk::Label lbl(_("Filter name:"));
1072 Gtk::Entry entry;
1073 entry.set_text(filter->label() ? filter->label() : "");
1074 Gtk::HBox hb;
1075 hb.add(lbl);
1076 hb.add(entry);
1077 hb.set_spacing(12);
1078 hb.show_all();
1079 m.get_vbox()->add(hb);
1080 const int res = m.run();
1081 if(res == Gtk::RESPONSE_OK) {
1082 filter->setLabel(entry.get_text().c_str());
1083 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Rename filter"));
1084 Gtk::TreeIter iter = _list.get_selection()->get_selected();
1085 if(iter)
1086 (*iter)[_columns.label] = entry.get_text();
1087 }
1088 }
1090 FilterEffectsDialog::CellRendererConnection::CellRendererConnection()
1091 : Glib::ObjectBase(typeid(CellRendererConnection)),
1092 _primitive(*this, "primitive", 0)
1093 {}
1095 Glib::PropertyProxy<void*> FilterEffectsDialog::CellRendererConnection::property_primitive()
1096 {
1097 return _primitive.get_proxy();
1098 }
1100 void FilterEffectsDialog::CellRendererConnection::set_text_width(const int w)
1101 {
1102 _text_width = w;
1103 }
1105 int FilterEffectsDialog::CellRendererConnection::get_text_width() const
1106 {
1107 return _text_width;
1108 }
1110 void FilterEffectsDialog::CellRendererConnection::get_size_vfunc(
1111 Gtk::Widget& widget, const Gdk::Rectangle* cell_area,
1112 int* x_offset, int* y_offset, int* width, int* height) const
1113 {
1114 PrimitiveList& primlist = dynamic_cast<PrimitiveList&>(widget);
1116 if(x_offset)
1117 (*x_offset) = 0;
1118 if(y_offset)
1119 (*y_offset) = 0;
1120 if(width)
1121 (*width) = size * primlist.primitive_count() + _text_width * 7;
1122 if(height) {
1123 // Scale the height depending on the number of inputs, unless it's
1124 // the first primitive, in which case there are no connections
1125 SPFilterPrimitive* prim = (SPFilterPrimitive*)_primitive.get_value();
1126 (*height) = size * input_count(prim);
1127 }
1128 }
1130 /*** PrimitiveList ***/
1131 FilterEffectsDialog::PrimitiveList::PrimitiveList(FilterEffectsDialog& d)
1132 : _dialog(d),
1133 _in_drag(0),
1134 _observer(new SignalObserver)
1135 {
1136 add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
1137 signal_expose_event().connect(sigc::mem_fun(*this, &PrimitiveList::on_expose_signal));
1139 _model = Gtk::ListStore::create(_columns);
1141 set_reorderable(true);
1143 set_model(_model);
1144 append_column(_("_Type"), _columns.type);
1146 _observer->signal_changed().connect(signal_primitive_changed().make_slot());
1147 get_selection()->signal_changed().connect(sigc::mem_fun(*this, &PrimitiveList::on_primitive_selection_changed));
1148 signal_primitive_changed().connect(sigc::mem_fun(*this, &PrimitiveList::queue_draw));
1150 _connection_cell.set_text_width(init_text());
1152 int cols_count = append_column(_("Connections"), _connection_cell);
1153 Gtk::TreeViewColumn* col = get_column(cols_count - 1);
1154 if(col)
1155 col->add_attribute(_connection_cell.property_primitive(), _columns.primitive);
1156 }
1158 // Sets up a vertical Pango context/layout, and returns the largest
1159 // width needed to render the FilterPrimitiveInput labels.
1160 int FilterEffectsDialog::PrimitiveList::init_text()
1161 {
1162 // Set up a vertical context+layout
1163 Glib::RefPtr<Pango::Context> context = create_pango_context();
1164 const Pango::Matrix matrix = {0, -1, 1, 0, 0, 0};
1165 context->set_matrix(matrix);
1166 _vertical_layout = Pango::Layout::create(context);
1168 int maxfont = 0;
1169 for(int i = 0; i < FPInputConverter.end; ++i) {
1170 _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
1171 int fontw, fonth;
1172 _vertical_layout->get_pixel_size(fontw, fonth);
1173 if(fonth > maxfont)
1174 maxfont = fonth;
1175 }
1177 return maxfont;
1178 }
1180 sigc::signal<void>& FilterEffectsDialog::PrimitiveList::signal_primitive_changed()
1181 {
1182 return _signal_primitive_changed;
1183 }
1185 void FilterEffectsDialog::PrimitiveList::on_primitive_selection_changed()
1186 {
1187 _observer->set(get_selected());
1188 signal_primitive_changed()();
1189 }
1191 /* Add all filter primitives in the current to the list.
1192 Keeps the same selection if possible, otherwise selects the first element */
1193 void FilterEffectsDialog::PrimitiveList::update()
1194 {
1195 SPFilter* f = _dialog._filter_modifier.get_selected_filter();
1196 const SPFilterPrimitive* active_prim = get_selected();
1197 bool active_found = false;
1199 _model->clear();
1201 if(f) {
1202 _dialog._primitive_box.set_sensitive(true);
1204 for(SPObject *prim_obj = f->children;
1205 prim_obj && SP_IS_FILTER_PRIMITIVE(prim_obj);
1206 prim_obj = prim_obj->next) {
1207 SPFilterPrimitive *prim = SP_FILTER_PRIMITIVE(prim_obj);
1208 if(prim) {
1209 Gtk::TreeModel::Row row = *_model->append();
1210 row[_columns.primitive] = prim;
1211 row[_columns.type_id] = FPConverter.get_id_from_key(prim->repr->name());
1212 row[_columns.type] = FPConverter.get_label(row[_columns.type_id]);
1213 row[_columns.id] = SP_OBJECT_ID(prim);
1215 if(prim == active_prim) {
1216 get_selection()->select(row);
1217 active_found = true;
1218 }
1219 }
1220 }
1222 if(!active_found && _model->children().begin())
1223 get_selection()->select(_model->children().begin());
1224 }
1225 else {
1226 _dialog._primitive_box.set_sensitive(false);
1227 }
1228 }
1230 void FilterEffectsDialog::PrimitiveList::set_menu(Glib::RefPtr<Gtk::Menu> menu)
1231 {
1232 _primitive_menu = menu;
1233 }
1235 SPFilterPrimitive* FilterEffectsDialog::PrimitiveList::get_selected()
1236 {
1237 if(_dialog._filter_modifier.get_selected_filter()) {
1238 Gtk::TreeModel::iterator i = get_selection()->get_selected();
1239 if(i)
1240 return (*i)[_columns.primitive];
1241 }
1243 return 0;
1244 }
1246 void FilterEffectsDialog::PrimitiveList::select(SPFilterPrimitive* prim)
1247 {
1248 for(Gtk::TreeIter i = _model->children().begin();
1249 i != _model->children().end(); ++i) {
1250 if((*i)[_columns.primitive] == prim)
1251 get_selection()->select(i);
1252 }
1253 }
1257 bool FilterEffectsDialog::PrimitiveList::on_expose_signal(GdkEventExpose* e)
1258 {
1259 Gdk::Rectangle clip(e->area.x, e->area.y, e->area.width, e->area.height);
1260 Glib::RefPtr<Gdk::Window> win = get_bin_window();
1261 Glib::RefPtr<Gdk::GC> darkgc = get_style()->get_dark_gc(Gtk::STATE_NORMAL);
1263 SPFilterPrimitive* prim = get_selected();
1264 int row_count = get_model()->children().size();
1266 int fheight = CellRendererConnection::size;
1267 Gdk::Rectangle rct, vis;
1268 Gtk::TreeIter row = get_model()->children().begin();
1269 int text_start_x = 0;
1270 if(row) {
1271 get_cell_area(get_model()->get_path(row), *get_column(1), rct);
1272 get_visible_rect(vis);
1273 int vis_x, vis_y;
1274 tree_to_widget_coords(vis.get_x(), vis.get_y(), vis_x, vis_y);
1276 text_start_x = rct.get_x() + rct.get_width() - _connection_cell.get_text_width() * (FPInputConverter.end + 1) + 1;
1277 for(int i = 0; i < FPInputConverter.end; ++i) {
1278 _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
1279 const int x = text_start_x + _connection_cell.get_text_width() * (i + 1);
1280 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());
1281 get_bin_window()->draw_layout(get_style()->get_text_gc(Gtk::STATE_NORMAL), x + 1, vis_y, _vertical_layout);
1282 get_bin_window()->draw_line(darkgc, x, vis_y, x, vis_y + vis.get_height());
1283 }
1284 }
1286 int row_index = 0;
1287 for(; row != get_model()->children().end(); ++row, ++row_index) {
1288 get_cell_area(get_model()->get_path(row), *get_column(1), rct);
1289 const int x = rct.get_x(), y = rct.get_y(), h = rct.get_height();
1291 // Check mouse state
1292 int mx, my;
1293 Gdk::ModifierType mask;
1294 get_bin_window()->get_pointer(mx, my, mask);
1296 // Outline the bottom of the connection area
1297 const int outline_x = x + fheight * (row_count - row_index);
1298 get_bin_window()->draw_line(darkgc, x, y + h, outline_x, y + h);
1300 // Side outline
1301 get_bin_window()->draw_line(darkgc, outline_x, y - 1, outline_x, y + h);
1303 std::vector<Gdk::Point> con_poly;
1304 int con_drag_y;
1305 bool inside;
1306 const SPFilterPrimitive* row_prim = (*row)[_columns.primitive];
1307 const int inputs = input_count(row_prim);
1309 if(SP_IS_FEMERGE(row_prim)) {
1310 for(int i = 0; i < inputs; ++i) {
1311 inside = do_connection_node(row, i, con_poly, mx, my);
1312 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
1313 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
1314 inside, con_poly);
1316 if(_in_drag == (i + 1))
1317 con_drag_y = con_poly[2].get_y();
1319 if(_in_drag != (i + 1) || row_prim != prim)
1320 draw_connection(row, i, text_start_x, outline_x, con_poly[2].get_y(), row_count);
1321 }
1322 }
1323 else {
1324 // Draw "in" shape
1325 inside = do_connection_node(row, 0, con_poly, mx, my);
1326 con_drag_y = con_poly[2].get_y();
1327 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
1328 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
1329 inside, con_poly);
1331 // Draw "in" connection
1332 if(_in_drag != 1 || row_prim != prim)
1333 draw_connection(row, SP_ATTR_IN, text_start_x, outline_x, con_poly[2].get_y(), row_count);
1335 if(inputs == 2) {
1336 // Draw "in2" shape
1337 inside = do_connection_node(row, 1, con_poly, mx, my);
1338 if(_in_drag == 2)
1339 con_drag_y = con_poly[2].get_y();
1340 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
1341 darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
1342 inside, con_poly);
1343 // Draw "in2" connection
1344 if(_in_drag != 2 || row_prim != prim)
1345 draw_connection(row, SP_ATTR_IN2, text_start_x, outline_x, con_poly[2].get_y(), row_count);
1346 }
1347 }
1349 // Draw drag connection
1350 if(row_prim == prim && _in_drag) {
1351 get_bin_window()->draw_line(get_style()->get_black_gc(), outline_x, con_drag_y,
1352 mx, con_drag_y);
1353 get_bin_window()->draw_line(get_style()->get_black_gc(), mx, con_drag_y, mx, my);
1354 }
1355 }
1357 return true;
1358 }
1360 void FilterEffectsDialog::PrimitiveList::draw_connection(const Gtk::TreeIter& input, const int attr,
1361 const int text_start_x, const int x1, const int y1,
1362 const int row_count)
1363 {
1364 int src_id;
1365 const Gtk::TreeIter res = find_result(input, attr, src_id);
1366 Glib::RefPtr<Gdk::GC> gc = get_style()->get_black_gc();
1368 if(res == input) {
1369 // Draw straight connection to a standard input
1370 const int tw = _connection_cell.get_text_width();
1371 gint end_x = text_start_x + tw * (src_id + 1) + (int)(tw * 0.5f) + 1;
1372 get_bin_window()->draw_rectangle(gc, true, end_x-2, y1-2, 5, 5);
1373 get_bin_window()->draw_line(gc, x1, y1, end_x, y1);
1374 }
1375 else if(res != _model->children().end()) {
1376 Gdk::Rectangle rct;
1378 get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
1379 const int fheight = CellRendererConnection::size;
1381 get_cell_area(get_model()->get_path(res), *get_column(1), rct);
1382 const int row_index = find_index(res);
1383 const int x2 = rct.get_x() + fheight * (row_count - row_index) - fheight / 2;
1384 const int y2 = rct.get_y() + rct.get_height();
1386 // Draw an 'L'-shaped connection to another filter primitive
1387 get_bin_window()->draw_line(gc, x1, y1, x2, y1);
1388 get_bin_window()->draw_line(gc, x2, y1, x2, y2);
1389 }
1390 }
1392 // Creates a triangle outline of the connection node and returns true if (x,y) is inside the node
1393 bool FilterEffectsDialog::PrimitiveList::do_connection_node(const Gtk::TreeIter& row, const int input,
1394 std::vector<Gdk::Point>& points,
1395 const int ix, const int iy)
1396 {
1397 Gdk::Rectangle rct;
1398 const int icnt = input_count((*row)[_columns.primitive]);
1400 get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
1401 const int fheight = CellRendererConnection::size;
1403 get_cell_area(_model->get_path(row), *get_column(1), rct);
1404 const float h = rct.get_height() / icnt;
1406 const int x = rct.get_x() + fheight * (_model->children().size() - find_index(row));
1407 const int con_w = (int)(fheight * 0.35f);
1408 const int con_y = (int)(rct.get_y() + (h / 2) - con_w + (input * h));
1409 points.clear();
1410 points.push_back(Gdk::Point(x, con_y));
1411 points.push_back(Gdk::Point(x, con_y + con_w * 2));
1412 points.push_back(Gdk::Point(x - con_w, con_y + con_w));
1414 return ix >= x - h && iy >= con_y && ix <= x && iy <= points[1].get_y();
1415 }
1417 const Gtk::TreeIter FilterEffectsDialog::PrimitiveList::find_result(const Gtk::TreeIter& start,
1418 const int attr, int& src_id)
1419 {
1420 SPFilterPrimitive* prim = (*start)[_columns.primitive];
1421 Gtk::TreeIter target = _model->children().end();
1422 int image;
1424 if(SP_IS_FEMERGE(prim)) {
1425 int c = 0;
1426 for(const SPObject* o = prim->firstChild(); o; o = o->next, ++c) {
1427 if(c == attr && SP_IS_FEMERGENODE(o))
1428 image = SP_FEMERGENODE(o)->input;
1429 }
1430 }
1431 else {
1432 if(attr == SP_ATTR_IN)
1433 image = prim->image_in;
1434 else if(attr == SP_ATTR_IN2) {
1435 if(SP_IS_FEBLEND(prim))
1436 image = SP_FEBLEND(prim)->in2;
1437 else if(SP_IS_FECOMPOSITE(prim))
1438 image = SP_FECOMPOSITE(prim)->in2;
1439 else if(SP_IS_FEDISPLACEMENTMAP(prim))
1440 image = SP_FEDISPLACEMENTMAP(prim)->in2;
1441 else
1442 return target;
1443 }
1444 else
1445 return target;
1446 }
1448 if(image >= 0) {
1449 for(Gtk::TreeIter i = _model->children().begin();
1450 i != start; ++i) {
1451 if(((SPFilterPrimitive*)(*i)[_columns.primitive])->image_out == image)
1452 target = i;
1453 }
1454 return target;
1455 }
1456 else if(image < -1) {
1457 src_id = -(image + 2);
1458 return start;
1459 }
1461 return target;
1462 }
1464 int FilterEffectsDialog::PrimitiveList::find_index(const Gtk::TreeIter& target)
1465 {
1466 int i = 0;
1467 for(Gtk::TreeIter iter = _model->children().begin();
1468 iter != target; ++iter, ++i);
1469 return i;
1470 }
1472 bool FilterEffectsDialog::PrimitiveList::on_button_press_event(GdkEventButton* e)
1473 {
1474 Gtk::TreePath path;
1475 Gtk::TreeViewColumn* col;
1476 const int x = (int)e->x, y = (int)e->y;
1477 int cx, cy;
1479 _drag_prim = 0;
1481 if(get_path_at_pos(x, y, path, col, cx, cy)) {
1482 Gtk::TreeIter iter = _model->get_iter(path);
1483 std::vector<Gdk::Point> points;
1485 _drag_prim = (*iter)[_columns.primitive];
1486 const int icnt = input_count(_drag_prim);
1488 for(int i = 0; i < icnt; ++i) {
1489 if(do_connection_node(_model->get_iter(path), i, points, x, y)) {
1490 _in_drag = i + 1;
1491 break;
1492 }
1493 }
1495 queue_draw();
1496 }
1498 if(_in_drag) {
1499 get_selection()->select(path);
1500 return true;
1501 }
1502 else
1503 return Gtk::TreeView::on_button_press_event(e);
1504 }
1506 bool FilterEffectsDialog::PrimitiveList::on_motion_notify_event(GdkEventMotion* e)
1507 {
1508 queue_draw();
1510 return Gtk::TreeView::on_motion_notify_event(e);
1511 }
1513 bool FilterEffectsDialog::PrimitiveList::on_button_release_event(GdkEventButton* e)
1514 {
1515 SPFilterPrimitive *prim = get_selected(), *target;
1517 if(_in_drag && prim) {
1518 Gtk::TreePath path;
1519 Gtk::TreeViewColumn* col;
1520 int cx, cy;
1522 if(get_path_at_pos((int)e->x, (int)e->y, path, col, cx, cy)) {
1523 const gchar *in_val = 0;
1524 Glib::ustring result;
1525 Gtk::TreeIter target_iter = _model->get_iter(path);
1526 target = (*target_iter)[_columns.primitive];
1528 Gdk::Rectangle rct;
1529 get_cell_area(path, *col, rct);
1530 const int twidth = _connection_cell.get_text_width();
1531 const int sources_x = rct.get_width() - twidth * FPInputConverter.end;
1532 if(cx > sources_x) {
1533 int src = (cx - sources_x) / twidth;
1534 if(src < 0)
1535 src = 0;
1536 else if(src >= FPInputConverter.end)
1537 src = FPInputConverter.end - 1;
1538 result = FPInputConverter.get_key((FilterPrimitiveInput)src);
1539 in_val = result.c_str();
1540 }
1541 else {
1542 // Ensure that the target comes before the selected primitive
1543 for(Gtk::TreeIter iter = _model->children().begin();
1544 iter != get_selection()->get_selected(); ++iter) {
1545 if(iter == target_iter) {
1546 Inkscape::XML::Node *repr = SP_OBJECT_REPR(target);
1547 // Make sure the target has a result
1548 const gchar *gres = repr->attribute("result");
1549 if(!gres) {
1550 result = "result" + Glib::Ascii::dtostr(SP_FILTER(prim->parent)->_image_number_next);
1551 repr->setAttribute("result", result.c_str());
1552 in_val = result.c_str();
1553 }
1554 else
1555 in_val = gres;
1556 break;
1557 }
1558 }
1559 }
1561 if(SP_IS_FEMERGE(prim)) {
1562 int c = 1;
1563 bool handled = false;
1564 for(SPObject* o = prim->firstChild(); o && !handled; o = o->next, ++c) {
1565 if(c == _in_drag && SP_IS_FEMERGENODE(o)) {
1566 // If input is null, delete it
1567 if(!in_val) {
1568 sp_repr_unparent(o->repr);
1569 sp_document_done(prim->document, SP_VERB_DIALOG_FILTER_EFFECTS,
1570 _("Remove merge node"));
1571 (*get_selection()->get_selected())[_columns.primitive] = prim;
1572 }
1573 else
1574 _dialog.set_attr(o, SP_ATTR_IN, in_val);
1575 handled = true;
1576 }
1577 }
1578 // Add new input?
1579 if(!handled && c == _in_drag) {
1580 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(prim->document);
1581 Inkscape::XML::Node *repr = xml_doc->createElement("svg:feMergeNode");
1582 repr->setAttribute("inkscape:collect", "always");
1583 prim->repr->appendChild(repr);
1584 SPFeMergeNode *node = SP_FEMERGENODE(prim->document->getObjectByRepr(repr));
1585 Inkscape::GC::release(repr);
1586 _dialog.set_attr(node, SP_ATTR_IN, in_val);
1587 (*get_selection()->get_selected())[_columns.primitive] = prim;
1588 }
1589 }
1590 else {
1591 if(_in_drag == 1)
1592 _dialog.set_attr(prim, SP_ATTR_IN, in_val);
1593 else if(_in_drag == 2)
1594 _dialog.set_attr(prim, SP_ATTR_IN2, in_val);
1595 }
1596 }
1598 _in_drag = 0;
1599 queue_draw();
1601 _dialog.update_settings_view();
1602 }
1604 if((e->type == GDK_BUTTON_RELEASE) && (e->button == 3)) {
1605 const bool sensitive = get_selected() != NULL;
1606 _primitive_menu->items()[0].set_sensitive(sensitive);
1607 _primitive_menu->items()[1].set_sensitive(sensitive);
1608 _primitive_menu->popup(e->button, e->time);
1610 return true;
1611 }
1612 else
1613 return Gtk::TreeView::on_button_release_event(e);
1614 }
1616 // Checks all of prim's inputs, removes any that use result
1617 void check_single_connection(SPFilterPrimitive* prim, const int result)
1618 {
1619 if(prim && result >= 0) {
1621 if(prim->image_in == result)
1622 SP_OBJECT_REPR(prim)->setAttribute("in", 0);
1624 if(SP_IS_FEBLEND(prim)) {
1625 if(SP_FEBLEND(prim)->in2 == result)
1626 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
1627 }
1628 else if(SP_IS_FECOMPOSITE(prim)) {
1629 if(SP_FECOMPOSITE(prim)->in2 == result)
1630 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
1631 }
1632 }
1633 }
1635 // Remove any connections going to/from prim_iter that forward-reference other primitives
1636 void FilterEffectsDialog::PrimitiveList::sanitize_connections(const Gtk::TreeIter& prim_iter)
1637 {
1638 SPFilterPrimitive *prim = (*prim_iter)[_columns.primitive];
1639 bool before = true;
1641 for(Gtk::TreeIter iter = _model->children().begin();
1642 iter != _model->children().end(); ++iter) {
1643 if(iter == prim_iter)
1644 before = false;
1645 else {
1646 SPFilterPrimitive* cur_prim = (*iter)[_columns.primitive];
1647 if(before)
1648 check_single_connection(cur_prim, prim->image_out);
1649 else
1650 check_single_connection(prim, cur_prim->image_out);
1651 }
1652 }
1653 }
1655 // Reorder the filter primitives to match the list order
1656 void FilterEffectsDialog::PrimitiveList::on_drag_end(const Glib::RefPtr<Gdk::DragContext>&)
1657 {
1658 SPFilter* filter = _dialog._filter_modifier.get_selected_filter();
1659 int ndx = 0;
1661 for(Gtk::TreeModel::iterator iter = _model->children().begin();
1662 iter != _model->children().end(); ++iter, ++ndx) {
1663 SPFilterPrimitive* prim = (*iter)[_columns.primitive];
1664 if(prim) {
1665 SP_OBJECT_REPR(prim)->setPosition(ndx);
1666 if(_drag_prim == prim) {
1667 sanitize_connections(iter);
1668 get_selection()->select(iter);
1669 }
1670 }
1671 }
1673 filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
1675 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Reorder filter primitive"));
1676 }
1678 int FilterEffectsDialog::PrimitiveList::primitive_count() const
1679 {
1680 return _model->children().size();
1681 }
1683 /*** FilterEffectsDialog ***/
1685 FilterEffectsDialog::FilterEffectsDialog()
1686 : Dialog ("dialogs.filtereffects", SP_VERB_DIALOG_FILTER_EFFECTS),
1687 _filter_modifier(*this),
1688 _primitive_list(*this),
1689 _add_primitive_type(FPConverter),
1690 _add_primitive(Gtk::Stock::ADD),
1691 _empty_settings(_("No primitive selected"), Gtk::ALIGN_LEFT),
1692 _locked(false)
1693 {
1694 _settings = new Settings(*this, sigc::mem_fun(*this, &FilterEffectsDialog::set_attr_direct),
1695 NR_FILTER_ENDPRIMITIVETYPE);
1696 _sizegroup = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL);
1697 _sizegroup->set_ignore_hidden();
1699 // Initialize widget hierarchy
1700 Gtk::HPaned* hpaned = Gtk::manage(new Gtk::HPaned);
1701 Gtk::ScrolledWindow* sw_prims = Gtk::manage(new Gtk::ScrolledWindow);
1702 Gtk::HBox* hb_prims = Gtk::manage(new Gtk::HBox);
1703 Gtk::Frame* fr_settings = Gtk::manage(new Gtk::Frame(_("<b>Settings</b>")));
1704 Gtk::Alignment* al_settings = Gtk::manage(new Gtk::Alignment);
1705 get_vbox()->add(*hpaned);
1706 hpaned->pack1(_filter_modifier);
1707 hpaned->pack2(_primitive_box);
1708 _primitive_box.pack_start(*sw_prims);
1709 _primitive_box.pack_start(*hb_prims, false, false);
1710 sw_prims->add(_primitive_list);
1711 hb_prims->pack_end(_add_primitive, false, false);
1712 hb_prims->pack_end(_add_primitive_type, false, false);
1713 get_vbox()->pack_start(*fr_settings, false, false);
1714 fr_settings->add(*al_settings);
1715 al_settings->add(_settings_box);
1717 _primitive_list.signal_primitive_changed().connect(
1718 sigc::mem_fun(*this, &FilterEffectsDialog::update_settings_view));
1719 _filter_modifier.signal_filter_changed().connect(
1720 sigc::mem_fun(_primitive_list, &PrimitiveList::update));
1722 sw_prims->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
1723 sw_prims->set_shadow_type(Gtk::SHADOW_IN);
1724 al_settings->set_padding(0, 0, 12, 0);
1725 fr_settings->set_shadow_type(Gtk::SHADOW_NONE);
1726 ((Gtk::Label*)fr_settings->get_label_widget())->set_use_markup();
1727 _add_primitive.signal_clicked().connect(sigc::mem_fun(*this, &FilterEffectsDialog::add_primitive));
1728 _primitive_list.set_menu(create_popup_menu(*this, sigc::mem_fun(*this, &FilterEffectsDialog::duplicate_primitive),
1729 sigc::mem_fun(*this, &FilterEffectsDialog::remove_primitive)));
1731 show_all_children();
1732 init_settings_widgets();
1733 _primitive_list.update();
1734 update_settings_view();
1735 }
1737 FilterEffectsDialog::~FilterEffectsDialog()
1738 {
1739 delete _settings;
1740 }
1742 void FilterEffectsDialog::set_attrs_locked(const bool l)
1743 {
1744 _locked = l;
1745 }
1747 void FilterEffectsDialog::init_settings_widgets()
1748 {
1749 // TODO: Find better range/climb-rate/digits values for the SpinSliders,
1750 // most of the current values are complete guesses!
1752 _empty_settings.set_sensitive(false);
1753 _settings_box.pack_start(_empty_settings);
1755 _settings->type(NR_FILTER_BLEND);
1756 _settings->add_combo(SP_ATTR_MODE, _("Mode"), BlendModeConverter);
1758 _settings->type(NR_FILTER_COLORMATRIX);
1759 ComboBoxEnum<FilterColorMatrixType>* colmat = _settings->add_combo(SP_ATTR_TYPE, _("Type"), ColorMatrixTypeConverter);
1760 _color_matrix_values = _settings->add_colormatrixvalues(_("Value(s)"));
1761 colmat->signal_attr_changed().connect(sigc::mem_fun(*this, &FilterEffectsDialog::update_color_matrix));
1763 _settings->type(NR_FILTER_COMPOSITE);
1764 _settings->add_combo(SP_ATTR_OPERATOR, _("Operator"), CompositeOperatorConverter);
1765 _k1 = _settings->add_spinslider(SP_ATTR_K1, _("K1"), -10, 10, 1, 0.01, 1);
1766 _k2 = _settings->add_spinslider(SP_ATTR_K2, _("K2"), -10, 10, 1, 0.01, 1);
1767 _k3 = _settings->add_spinslider(SP_ATTR_K3, _("K3"), -10, 10, 1, 0.01, 1);
1768 _k4 = _settings->add_spinslider(SP_ATTR_K4, _("K4"), -10, 10, 1, 0.01, 1);
1770 _settings->type(NR_FILTER_CONVOLVEMATRIX);
1771 _convolve_order = _settings->add_dualspinbutton(SP_ATTR_ORDER, _("Size"), 1, 5, 1, 1, 0);
1772 _convolve_target = _settings->add_multispinbutton(SP_ATTR_TARGETX, SP_ATTR_TARGETY, _("Target"), 0, 4, 1, 1, 0);
1773 _convolve_matrix = _settings->add_matrix(SP_ATTR_KERNELMATRIX, _("Kernel"));
1774 _convolve_order->signal_attr_changed().connect(sigc::mem_fun(*this, &FilterEffectsDialog::convolve_order_changed));
1775 _settings->add_spinslider(SP_ATTR_DIVISOR, _("Divisor"), 0.01, 10, 1, 0.01, 1);
1776 _settings->add_spinslider(SP_ATTR_BIAS, _("Bias"), -10, 10, 1, 0.01, 1);
1777 _settings->add_combo(SP_ATTR_EDGEMODE, _("Edge Mode"), ConvolveMatrixEdgeModeConverter);
1779 _settings->type(NR_FILTER_DIFFUSELIGHTING);
1780 _settings->add_color(SP_PROP_LIGHTING_COLOR, _("Diffuse Color"));
1781 _settings->add_spinslider(SP_ATTR_SURFACESCALE, _("Surface Scale"), -10, 10, 1, 0.01, 1);
1782 _settings->add_spinslider(SP_ATTR_DIFFUSECONSTANT, _("Constant"), 0, 100, 1, 0.01, 1);
1783 _settings->add_dualspinslider(SP_ATTR_KERNELUNITLENGTH, _("Kernel Unit Length"), 0.01, 10, 1, 0.01, 1);
1784 _settings->add_lightsource(_("Light Source"));
1786 _settings->type(NR_FILTER_DISPLACEMENTMAP);
1787 _settings->add_spinslider(SP_ATTR_SCALE, _("Scale"), 0, 100, 1, 0.01, 1);
1788 _settings->add_combo(SP_ATTR_XCHANNELSELECTOR, _("X Channel"), DisplacementMapChannelConverter);
1789 _settings->add_combo(SP_ATTR_YCHANNELSELECTOR, _("Y Channel"), DisplacementMapChannelConverter);
1791 _settings->type(NR_FILTER_GAUSSIANBLUR);
1792 _settings->add_dualspinslider(SP_ATTR_STDDEVIATION, _("Standard Deviation"), 0.01, 100, 1, 0.01, 1);
1794 _settings->type(NR_FILTER_MORPHOLOGY);
1795 _settings->add_combo(SP_ATTR_OPERATOR, _("Operator"), MorphologyOperatorConverter);
1796 _settings->add_dualspinslider(SP_ATTR_RADIUS, _("Radius"), 0, 100, 1, 0.01, 1);
1798 _settings->type(NR_FILTER_OFFSET);
1799 _settings->add_spinslider(SP_ATTR_DX, _("Delta X"), -100, 100, 1, 0.01, 1);
1800 _settings->add_spinslider(SP_ATTR_DY, _("Delta Y"), -100, 100, 1, 0.01, 1);
1802 _settings->type(NR_FILTER_SPECULARLIGHTING);
1803 _settings->add_color(SP_PROP_LIGHTING_COLOR, _("Specular Color"));
1804 _settings->add_spinslider(SP_ATTR_SURFACESCALE, _("Surface Scale"), -10, 10, 1, 0.01, 1);
1805 _settings->add_spinslider(SP_ATTR_SPECULARCONSTANT, _("Constant"), 0, 100, 1, 0.01, 1);
1806 _settings->add_spinslider(SP_ATTR_SPECULAREXPONENT, _("Exponent"), 1, 128, 1, 0.01, 1);
1807 _settings->add_dualspinslider(SP_ATTR_KERNELUNITLENGTH, _("Kernel Unit Length"), 0.01, 10, 1, 0.01, 1);
1808 _settings->add_lightsource(_("Light Source"));
1810 _settings->type(NR_FILTER_TURBULENCE);
1811 _settings->add_checkbutton(SP_ATTR_STITCHTILES, _("Stitch Tiles"), "stitch", "noStitch");
1812 _settings->add_dualspinslider(SP_ATTR_BASEFREQUENCY, _("Base Frequency"), 0, 100, 1, 0.01, 1);
1813 _settings->add_spinslider(SP_ATTR_NUMOCTAVES, _("Octaves"), 1, 10, 1, 1, 0);
1814 _settings->add_spinslider(SP_ATTR_SEED, _("Seed"), 0, 1000, 1, 1, 0);
1815 }
1817 void FilterEffectsDialog::add_primitive()
1818 {
1819 SPFilter* filter = _filter_modifier.get_selected_filter();
1821 if(filter) {
1822 SPFilterPrimitive* prim = filter_add_primitive(filter, _add_primitive_type.get_active_data()->id);
1824 _primitive_list.update();
1825 _primitive_list.select(prim);
1827 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter primitive"));
1828 }
1829 }
1831 void FilterEffectsDialog::remove_primitive()
1832 {
1833 SPFilterPrimitive* prim = _primitive_list.get_selected();
1835 if(prim) {
1836 sp_repr_unparent(prim->repr);
1838 sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_FILTER_EFFECTS,
1839 _("Remove filter primitive"));
1841 _primitive_list.update();
1842 }
1843 }
1845 void FilterEffectsDialog::duplicate_primitive()
1846 {
1847 SPFilter* filter = _filter_modifier.get_selected_filter();
1848 SPFilterPrimitive* origprim = _primitive_list.get_selected();
1850 if(filter && origprim) {
1851 Inkscape::XML::Node *repr;
1852 repr = SP_OBJECT_REPR(origprim)->duplicate(SP_OBJECT_REPR(origprim)->document());
1853 SP_OBJECT_REPR(filter)->appendChild(repr);
1855 sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter primitive"));
1857 _primitive_list.update();
1858 }
1859 }
1861 void FilterEffectsDialog::convolve_order_changed()
1862 {
1863 _convolve_matrix->set_from_attribute(SP_OBJECT(_primitive_list.get_selected()));
1864 _convolve_target->get_spinbuttons()[0]->get_adjustment()->set_upper(_convolve_order->get_spinbutton1().get_value() - 1);
1865 _convolve_target->get_spinbuttons()[1]->get_adjustment()->set_upper(_convolve_order->get_spinbutton2().get_value() - 1);
1866 }
1868 void FilterEffectsDialog::set_attr_direct(const AttrWidget* input)
1869 {
1870 set_attr(_primitive_list.get_selected(), input->get_attribute(), input->get_as_attribute().c_str());
1871 }
1873 void FilterEffectsDialog::set_child_attr_direct(const AttrWidget* input)
1874 {
1875 set_attr(_primitive_list.get_selected()->children, input->get_attribute(), input->get_as_attribute().c_str());
1876 }
1878 void FilterEffectsDialog::set_attr(SPObject* o, const SPAttributeEnum attr, const gchar* val)
1879 {
1880 if(!_locked) {
1881 SPFilter *filter = _filter_modifier.get_selected_filter();
1882 const gchar* name = (const gchar*)sp_attribute_name(attr);
1883 if(filter && name && o) {
1884 update_settings_sensitivity();
1886 SP_OBJECT_REPR(o)->setAttribute(name, val);
1887 filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
1889 Glib::ustring undokey = "filtereffects:";
1890 undokey += name;
1891 sp_document_maybe_done(filter->document, undokey.c_str(), SP_VERB_DIALOG_FILTER_EFFECTS,
1892 _("Set filter primitive attribute"));
1893 }
1894 }
1895 }
1897 void FilterEffectsDialog::update_settings_view()
1898 {
1899 SPFilterPrimitive* prim = _primitive_list.get_selected();
1901 if(prim) {
1902 _settings->show_and_update(FPConverter.get_id_from_key(prim->repr->name()), prim);
1903 _empty_settings.hide();
1904 }
1905 else {
1906 _settings_box.hide_all();
1907 _settings_box.show();
1908 _empty_settings.show();
1909 }
1911 update_settings_sensitivity();
1912 }
1914 void FilterEffectsDialog::update_settings_sensitivity()
1915 {
1916 SPFilterPrimitive* prim = _primitive_list.get_selected();
1917 const bool use_k = SP_IS_FECOMPOSITE(prim) && SP_FECOMPOSITE(prim)->composite_operator == COMPOSITE_ARITHMETIC;
1918 _k1->set_sensitive(use_k);
1919 _k2->set_sensitive(use_k);
1920 _k3->set_sensitive(use_k);
1921 _k4->set_sensitive(use_k);
1922 }
1924 void FilterEffectsDialog::update_color_matrix()
1925 {
1926 _color_matrix_values->set_from_attribute(_primitive_list.get_selected());
1927 }
1929 } // namespace Dialog
1930 } // namespace UI
1931 } // namespace Inkscape
1933 /*
1934 Local Variables:
1935 mode:c++
1936 c-file-style:"stroustrup"
1937 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1938 indent-tabs-mode:nil
1939 fill-column:99
1940 End:
1941 */
1942 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :