Code

0e3727ce81ff1f3e97a13319171979bc7a0ed761
[inkscape.git] / src / ui / dialog / filter-effects-dialog.cpp
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-fecomponenttransfer.h"
41 #include "sp-fecomposite.h"
42 #include "sp-feconvolvematrix.h"
43 #include "sp-fedisplacementmap.h"
44 #include "sp-fedistantlight.h"
45 #include "sp-femerge.h"
46 #include "sp-femergenode.h"
47 #include "sp-feoffset.h"
48 #include "sp-fepointlight.h"
49 #include "sp-fespotlight.h"
50 #include "sp-filter-primitive.h"
51 #include "sp-gaussian-blur.h"
53 #include "style.h"
54 #include "svg/svg-color.h"
55 #include "verbs.h"
56 #include "xml/node.h"
57 #include "xml/node-observer.h"
58 #include "xml/repr.h"
59 #include <sstream>
61 #include <iostream>
63 using namespace NR;
65 namespace Inkscape {
66 namespace UI {
67 namespace Dialog {
69 // Returns the number of inputs available for the filter primitive type
70 int input_count(const SPFilterPrimitive* prim)
71 {
72     if(!prim)
73         return 0;
74     else if(SP_IS_FEBLEND(prim) || SP_IS_FECOMPOSITE(prim) || SP_IS_FEDISPLACEMENTMAP(prim))
75         return 2;
76     else if(SP_IS_FEMERGE(prim)) {
77         // Return the number of feMergeNode connections plus an extra
78         int count = 1;
79         for(const SPObject* o = prim->firstChild(); o; o = o->next, ++count);
80         return count;
81     }
82     else
83         return 1;
84 }
86 // Very simple observer that just emits a signal if anything happens to a node
87 class FilterEffectsDialog::SignalObserver : public XML::NodeObserver
88 {
89 public:
90     SignalObserver()
91         : _oldsel(0)
92     {}
94     // Add this observer to the SPObject and remove it from any previous object
95     void set(SPObject* o)
96     {
97         if(_oldsel && _oldsel->repr)
98             _oldsel->repr->removeObserver(*this);
99         if(o && o->repr)
100             o->repr->addObserver(*this);
101         _oldsel = o;
102     }
104     void notifyChildAdded(XML::Node&, XML::Node&, XML::Node*)
105     { signal_changed()(); }
107     void notifyChildRemoved(XML::Node&, XML::Node&, XML::Node*)
108     { signal_changed()(); }
110     void notifyChildOrderChanged(XML::Node&, XML::Node&, XML::Node*, XML::Node*)
111     { signal_changed()(); }
113     void notifyContentChanged(XML::Node&, Util::ptr_shared<char>, Util::ptr_shared<char>)
114     {}
116     void notifyAttributeChanged(XML::Node&, GQuark, Util::ptr_shared<char>, Util::ptr_shared<char>)
117     { signal_changed()(); }
119     sigc::signal<void>& signal_changed()
120     {
121         return _signal_changed;
122     }
123 private:
124     sigc::signal<void> _signal_changed;
125     SPObject* _oldsel;
126 };
128 class CheckButtonAttr : public Gtk::CheckButton, public AttrWidget
130 public:
131     CheckButtonAttr(const Glib::ustring& label,
132                     const Glib::ustring& tv, const Glib::ustring& fv,
133                     const SPAttributeEnum a)
134         : Gtk::CheckButton(label),
135           AttrWidget(a),
136           _true_val(tv), _false_val(fv)
137     {
138         signal_toggled().connect(signal_attr_changed().make_slot());
139     }
141     Glib::ustring get_as_attribute() const
142     {
143         return get_active() ? _true_val : _false_val;
144     }
146     void set_from_attribute(SPObject* o)
147     {
148         const gchar* val = attribute_value(o);
149         if(val) {
150             if(_true_val == val)
151                 set_active(true);
152             else if(_false_val == val)
153                 set_active(false);
154         }
155     }
156 private:
157     const Glib::ustring _true_val, _false_val;
158 };
160 class SpinButtonAttr : public Gtk::SpinButton, public AttrWidget
162 public:
163     SpinButtonAttr(double lower, double upper, double step_inc,
164                    double climb_rate, int digits, const SPAttributeEnum a)
165         : Gtk::SpinButton(climb_rate, digits),
166           AttrWidget(a)
167     {
168         set_range(lower, upper);
169         set_increments(step_inc, step_inc * 5);
171         signal_value_changed().connect(signal_attr_changed().make_slot());
172     }
174     Glib::ustring get_as_attribute() const
175     {
176         const double val = get_value();
177         
178         if(get_digits() == 0)
179             return Glib::Ascii::dtostr((int)val);
180         else
181             return Glib::Ascii::dtostr(val);
182     }
183     
184     void set_from_attribute(SPObject* o)
185     {
186         const gchar* val = attribute_value(o);
187         if(val)
188             set_value(Glib::Ascii::strtod(val));
189     }
190 };
192 // Contains an arbitrary number of spin buttons that use seperate attributes
193 class MultiSpinButton : public Gtk::HBox
195 public:
196     MultiSpinButton(double lower, double upper, double step_inc,
197                     double climb_rate, int digits, std::vector<SPAttributeEnum> attrs)
198     {
199         for(unsigned i = 0; i < attrs.size(); ++i) {
200             _spins.push_back(new SpinButtonAttr(lower, upper, step_inc, climb_rate, digits, attrs[i]));
201             pack_start(*_spins.back(), false, false);
202         }
203     }
205     ~MultiSpinButton()
206     {
207         for(unsigned i = 0; i < _spins.size(); ++i)
208             delete _spins[i];
209     }
211     std::vector<SpinButtonAttr*>& get_spinbuttons()
212     {
213         return _spins;
214     }
215 private:
216     std::vector<SpinButtonAttr*> _spins;
217 };
219 // Contains two spinbuttons that describe a NumberOptNumber
220 class DualSpinButton : public Gtk::HBox, public AttrWidget
222 public:
223     DualSpinButton(double lower, double upper, double step_inc,
224                    double climb_rate, int digits, const SPAttributeEnum a)
225         : AttrWidget(a),
226           _s1(climb_rate, digits), _s2(climb_rate, digits)
227     {
228         _s1.set_range(lower, upper);
229         _s2.set_range(lower, upper);
230         _s1.set_increments(step_inc, step_inc * 5);
231         _s2.set_increments(step_inc, step_inc * 5);
233         _s1.signal_value_changed().connect(signal_attr_changed().make_slot());
234         _s2.signal_value_changed().connect(signal_attr_changed().make_slot());
236         pack_start(_s1, false, false);
237         pack_start(_s2, false, false);
238     }
240     Gtk::SpinButton& get_spinbutton1()
241     {
242         return _s1;
243     }
245     Gtk::SpinButton& get_spinbutton2()
246     {
247         return _s2;
248     }
250     virtual Glib::ustring get_as_attribute() const
251     {
252         double v1 = _s1.get_value();
253         double v2 = _s2.get_value();
254         
255         if(_s1.get_digits() == 0) {
256             v1 = (int)v1;
257             v2 = (int)v2;
258         }
260         return Glib::Ascii::dtostr(v1) + " " + Glib::Ascii::dtostr(v2);
261     }
263     virtual void set_from_attribute(SPObject* o)
264     {
265         const gchar* val = attribute_value(o);
266         if(val) {
267             NumberOptNumber n;
268             n.set(val);
269             _s1.set_value(n.getNumber());
270             _s2.set_value(n.getOptNumber());
271         }
272     }
273 private:
274     Gtk::SpinButton _s1, _s2;
275 };
277 class ColorButton : public Gtk::ColorButton, public AttrWidget
279 public:
280     ColorButton(const SPAttributeEnum a)
281         : AttrWidget(a)
282     {
283         signal_color_set().connect(signal_attr_changed().make_slot());
285         Gdk::Color col;
286         col.set_rgb(65535, 65535, 65535);
287         set_color(col);
288     }
289     
290     // Returns the color in 'rgb(r,g,b)' form.
291     Glib::ustring get_as_attribute() const
292     {
293         std::ostringstream os;
294         const Gdk::Color c = get_color();
295         const int r = (c.get_red() + 1) / 256 - 1, g = (c.get_green() + 1) / 256 - 1, b = (c.get_blue() + 1) / 256 - 1;
296         os << "rgb(" << r << "," << g << "," << b << ")";
297         return os.str();
298     }
301     void set_from_attribute(SPObject* o)
302     {
303         const gchar* val = attribute_value(o);
304         if(val) {
305             const guint32 i = sp_svg_read_color(val, 0xFFFFFFFF);
306             const int r = SP_RGBA32_R_U(i) + 1, g = SP_RGBA32_G_U(i) + 1, b = SP_RGBA32_B_U(i) + 1;
307             Gdk::Color col;
308             col.set_rgb(r * 256 - 1, g * 256 - 1, b * 256 - 1);
309             set_color(col);
310         }
311     }
312 };
314 /* Displays/Edits the matrix for feConvolveMatrix or feColorMatrix */
315 class FilterEffectsDialog::MatrixAttr : public Gtk::Frame, public AttrWidget
317 public:
318     MatrixAttr(const SPAttributeEnum a)
319         : AttrWidget(a), _locked(false)
320     {
321         _model = Gtk::ListStore::create(_columns);
322         _tree.set_model(_model);
323         _tree.set_headers_visible(false);
324         _tree.show();
325         add(_tree);
326         set_shadow_type(Gtk::SHADOW_IN);
327     }
329     std::vector<double> get_values() const
330     {
331         std::vector<double> vec;
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                 vec.push_back((*iter)[_columns.cols[c]]);
336         }
337         return vec;
338     }
340     void set_values(const std::vector<double>& v)
341     {
342         unsigned i = 0;
343         for(Gtk::TreeIter iter = _model->children().begin();
344             iter != _model->children().end(); ++iter) {
345             for(unsigned c = 0; c < _tree.get_columns().size(); ++c) {
346                 if(i >= v.size())
347                     return;
348                 (*iter)[_columns.cols[c]] = v[i];
349                 ++i;
350             }
351         }
352     }
354     Glib::ustring get_as_attribute() const
355     {
356         std::ostringstream os;
357         
358         for(Gtk::TreeIter iter = _model->children().begin();
359             iter != _model->children().end(); ++iter) {
360             for(unsigned c = 0; c < _tree.get_columns().size(); ++c) {
361                 os << (*iter)[_columns.cols[c]] << " ";
362             }
363         }
364         
365         return os.str();
366     }
367     
368     void set_from_attribute(SPObject* o)
369     {
370         if(o) {
371             if(SP_IS_FECONVOLVEMATRIX(o)) {
372                 SPFeConvolveMatrix* conv = SP_FECONVOLVEMATRIX(o);
373                 int cols, rows;
374                 cols = (int)conv->order.getNumber();
375                 if(cols > 5)
376                     cols = 5;
377                 rows = conv->order.optNumber_set ? (int)conv->order.getOptNumber() : cols;
378                 update(o, rows, cols);
379             }
380             else if(SP_IS_FECOLORMATRIX(o))
381                 update(o, 4, 5);
382         }
383     }
384 private:
385     class MatrixColumns : public Gtk::TreeModel::ColumnRecord
386     {
387     public:
388         MatrixColumns()
389         {
390             cols.resize(5);
391             for(unsigned i = 0; i < cols.size(); ++i)
392                 add(cols[i]);
393         }
394         std::vector<Gtk::TreeModelColumn<double> > cols;
395     };
397     void update(SPObject* o, const int rows, const int cols)
398     {
399         if(_locked)
400             return;
402         _model->clear();
404         _tree.remove_all_columns();
406         std::vector<gdouble>* values = NULL;
407         if(SP_IS_FECOLORMATRIX(o))
408             values = &SP_FECOLORMATRIX(o)->values;
409         else if(SP_IS_FECONVOLVEMATRIX(o))
410             values = &SP_FECONVOLVEMATRIX(o)->kernelMatrix;
411         else
412             return;
414         if(o) {
415             int ndx = 0;
417             for(int i = 0; i < cols; ++i) {
418                 _tree.append_column_numeric_editable("", _columns.cols[i], "%.2f");
419                 dynamic_cast<Gtk::CellRendererText*>(
420                     _tree.get_column_cell_renderer(i))->signal_edited().connect(
421                         sigc::mem_fun(*this, &MatrixAttr::rebind));
422             }
424             for(int r = 0; r < rows; ++r) {
425                 Gtk::TreeRow row = *(_model->append());
426                 for(int c = 0; c < cols; ++c, ++ndx)
427                     row[_columns.cols[c]] = ndx < (int)values->size() ? (*values)[ndx] : 0;
428             }
429         }
430     }
432     void rebind(const Glib::ustring&, const Glib::ustring&)
433     {
434         _locked = true;
435         signal_attr_changed()();
436         _locked = false;
437     }
439     bool _locked;
440     Gtk::TreeView _tree;
441     Glib::RefPtr<Gtk::ListStore> _model;
442     MatrixColumns _columns;
443 };
445 // Displays a matrix or a slider for feColorMatrix
446 class FilterEffectsDialog::ColorMatrixValues : public Gtk::Frame, public AttrWidget
448 public:
449     ColorMatrixValues()
450         : AttrWidget(SP_ATTR_VALUES),
451           _matrix(SP_ATTR_VALUES),
452           _saturation(0, 0, 1, 0.1, 0.01, 2, SP_ATTR_VALUES),
453           _angle(0, 0, 360, 0.1, 0.01, 1, SP_ATTR_VALUES),
454           _label(_("None"), Gtk::ALIGN_LEFT),
455           _use_stored(false),
456           _saturation_store(0),
457           _angle_store(0)
458     {
459         _matrix.signal_attr_changed().connect(signal_attr_changed().make_slot());
460         _saturation.signal_attr_changed().connect(signal_attr_changed().make_slot());
461         _angle.signal_attr_changed().connect(signal_attr_changed().make_slot());
462         signal_attr_changed().connect(sigc::mem_fun(*this, &ColorMatrixValues::update_store));
464         _matrix.show();
465         _saturation.show();
466         _angle.show();
467         _label.show();
468         _label.set_sensitive(false);
470         set_shadow_type(Gtk::SHADOW_NONE);
471     }
473     virtual void set_from_attribute(SPObject* o)
474     {
475         if(SP_IS_FECOLORMATRIX(o)) {
476             SPFeColorMatrix* col = SP_FECOLORMATRIX(o);
477             remove();
478             switch(col->type) {
479                 case COLORMATRIX_SATURATE:
480                     add(_saturation);
481                     if(_use_stored)
482                         _saturation.set_value(_saturation_store);
483                     else
484                         _saturation.set_from_attribute(o);
485                     break;
486                 case COLORMATRIX_HUEROTATE:
487                     add(_angle);
488                     if(_use_stored)
489                         _angle.set_value(_angle_store);
490                     else
491                         _angle.set_from_attribute(o);
492                     break;
493                 case COLORMATRIX_LUMINANCETOALPHA:
494                     add(_label);
495                     break;
496                 case COLORMATRIX_MATRIX:
497                 default:
498                     add(_matrix);
499                     if(_use_stored)
500                         _matrix.set_values(_matrix_store);
501                     else
502                         _matrix.set_from_attribute(o);
503                     break;
504             }
505             _use_stored = true;
506         }
507     }
509     virtual Glib::ustring get_as_attribute() const
510     {
511         const Widget* w = get_child();
512         if(w == &_label)
513             return "";
514         else
515             return dynamic_cast<const AttrWidget*>(w)->get_as_attribute();
516     }
518     void clear_store()
519     {
520         _use_stored = false;
521     }
522 private:
523     void update_store()
524     {
525         const Widget* w = get_child();
526         if(w == &_matrix)
527             _matrix_store = _matrix.get_values();
528         else if(w == &_saturation)
529             _saturation_store = _saturation.get_value();
530         else if(w == &_angle)
531             _angle_store = _angle.get_value();
532     }
534     MatrixAttr _matrix;
535     SpinSlider _saturation;
536     SpinSlider _angle;
537     Gtk::Label _label;
539     // Store separate values for the different color modes
540     bool _use_stored;
541     std::vector<double> _matrix_store;
542     double _saturation_store;
543     double _angle_store;
544 };
546 class FilterEffectsDialog::Settings
548 public:
549     typedef sigc::slot<void, const AttrWidget*> SetAttrSlot;
551     Settings(FilterEffectsDialog& d, SetAttrSlot slot, const int maxtypes)
552         : _dialog(d), _set_attr_slot(slot), _current_type(-1), _max_types(maxtypes)
553     {
554         _groups.resize(_max_types);
555         _attrwidgets.resize(_max_types);
557         for(int i = 0; i < _max_types; ++i) {
558             _groups[i] = new Gtk::VBox;
559             d._settings_box.add(*_groups[i]);
560         }
561     }
563     ~Settings()
564     {
565         for(int i = 0; i < _max_types; ++i) {
566             delete _groups[i];
567             for(unsigned j = 0; j < _attrwidgets[i].size(); ++j)
568                 delete _attrwidgets[i][j];
569         }
570     }
572     // Show the active settings group and update all the AttrWidgets with new values
573     void show_and_update(const int t, SPObject* ob)
574     {
575         if(t != _current_type) {
576             type(t);
577             for(unsigned i = 0; i < _groups.size(); ++i)
578                 _groups[i]->hide();
579         }
580         _groups[t]->show_all();
582         _dialog.set_attrs_locked(true);
583         for(unsigned i = 0; i < _attrwidgets[_current_type].size(); ++i)
584             _attrwidgets[_current_type][i]->set_from_attribute(ob);
585         _dialog.set_attrs_locked(false);
586     }
588     int get_current_type() const
589     {
590         return _current_type;
591     }
593     void type(const int t)
594     {
595         _current_type = t;
596     }
598     // LightSource
599     LightSourceControl* add_lightsource(const Glib::ustring& label);
601     // CheckBox
602     CheckButtonAttr* add_checkbutton(const SPAttributeEnum attr, const Glib::ustring& label,
603                                      const Glib::ustring& tv, const Glib::ustring& fv)
604     {
605         CheckButtonAttr* cb = new CheckButtonAttr(label, tv, fv, attr);
606         add_widget(cb, "");
607         add_attr_widget(cb);
608         return cb;
609     }
611     // ColorButton
612     ColorButton* add_color(const SPAttributeEnum attr, const Glib::ustring& label)
613     {
614         ColorButton* col = new ColorButton(attr);
615         add_widget(col, label);
616         add_attr_widget(col);
617         return col;
618     }
620     // Matrix
621     MatrixAttr* add_matrix(const SPAttributeEnum attr, const Glib::ustring& label)
622     {
623         MatrixAttr* conv = new MatrixAttr(attr);
624         add_widget(conv, label);
625         add_attr_widget(conv);
626         return conv;
627     }
629     // ColorMatrixValues
630     ColorMatrixValues* add_colormatrixvalues(const Glib::ustring& label)
631     {
632         ColorMatrixValues* cmv = new ColorMatrixValues;
633         add_widget(cmv, label);
634         add_attr_widget(cmv);
635         return cmv;
636     }
638     // SpinSlider
639     SpinSlider* add_spinslider(const SPAttributeEnum attr, const Glib::ustring& label,
640                          const double lo, const double hi, const double step_inc, const double climb, const int digits)
641     {
642         SpinSlider* spinslider = new SpinSlider(lo, lo, hi, step_inc, climb, digits, attr);
643         add_widget(spinslider, label);
644         add_attr_widget(spinslider);
645         return spinslider;
646     }
648     // DualSpinSlider
649     DualSpinSlider* add_dualspinslider(const SPAttributeEnum attr, const Glib::ustring& label,
650                                        const double lo, const double hi, const double step_inc,
651                                        const double climb, const int digits)
652     {
653         DualSpinSlider* dss = new DualSpinSlider(lo, lo, hi, step_inc, climb, digits, attr);
654         add_widget(dss, label);
655         add_attr_widget(dss);
656         return dss;
657     }
659     // DualSpinButton
660     DualSpinButton* add_dualspinbutton(const SPAttributeEnum attr, const Glib::ustring& label,
661                                        const double lo, const double hi, const double step_inc,
662                                        const double climb, const int digits)
663     {
664         DualSpinButton* dsb = new DualSpinButton(lo, hi, step_inc, climb, digits, attr);
665         add_widget(dsb, label);
666         add_attr_widget(dsb);
667         return dsb;
668     }
670     // MultiSpinButton
671     MultiSpinButton* add_multispinbutton(const SPAttributeEnum attr1, const SPAttributeEnum attr2,
672                                          const Glib::ustring& label, const double lo, const double hi,
673                                          const double step_inc, const double climb, const int digits)
674     {
675         std::vector<SPAttributeEnum> attrs;
676         attrs.push_back(attr1);
677         attrs.push_back(attr2);
678         MultiSpinButton* msb = new MultiSpinButton(lo, hi, step_inc, climb, digits, attrs);
679         add_widget(msb, label);
680         for(unsigned i = 0; i < msb->get_spinbuttons().size(); ++i)
681             add_attr_widget(msb->get_spinbuttons()[i]);
682         return msb;
683     }
684     MultiSpinButton* add_multispinbutton(const SPAttributeEnum attr1, const SPAttributeEnum attr2,
685                                          const SPAttributeEnum attr3, const Glib::ustring& label, const double lo,
686                                          const double hi, const double step_inc, const double climb, const int digits)
687     {
688         std::vector<SPAttributeEnum> attrs;
689         attrs.push_back(attr1);
690         attrs.push_back(attr2);
691         attrs.push_back(attr3);
692         MultiSpinButton* msb = new MultiSpinButton(lo, hi, step_inc, climb, digits, attrs);
693         add_widget(msb, label);
694         for(unsigned i = 0; i < msb->get_spinbuttons().size(); ++i)
695             add_attr_widget(msb->get_spinbuttons()[i]);
696         return msb;
697     }
699     // ComboBoxEnum
700     template<typename T> ComboBoxEnum<T>* add_combo(const SPAttributeEnum attr,
701                                   const Glib::ustring& label,
702                                   const Util::EnumDataConverter<T>& conv)
703     {
704         ComboBoxEnum<T>* combo = new ComboBoxEnum<T>(conv, attr);
705         add_widget(combo, label);
706         add_attr_widget(combo);
707         return combo;
708     }
709 private:
710     void add_attr_widget(AttrWidget* a)
711     {    
712         _attrwidgets[_current_type].push_back(a);
713         a->signal_attr_changed().connect(sigc::bind(_set_attr_slot, a));
714     }
716     /* Adds a new settings widget using the specified label. The label will be formatted with a colon
717        and all widgets within the setting group are aligned automatically. */
718     void add_widget(Gtk::Widget* w, const Glib::ustring& label)
719     {
720         Gtk::Label *lbl = Gtk::manage(new Gtk::Label(label + (label == "" ? "" : ":"), Gtk::ALIGN_LEFT));
721         Gtk::HBox *hb = Gtk::manage(new Gtk::HBox);
722         hb->set_spacing(12);
723         hb->pack_start(*lbl, false, false);
724         hb->pack_start(*w);
725         _groups[_current_type]->pack_start(*hb);
727         _dialog._sizegroup->add_widget(*lbl);
729         hb->show();
730         lbl->show();
732         w->show();
733     }
735     std::vector<Gtk::VBox*> _groups;
737     FilterEffectsDialog& _dialog;
738     SetAttrSlot _set_attr_slot;
739     std::vector<std::vector<AttrWidget*> > _attrwidgets;
740     int _current_type, _max_types;
741 };
743 // Settings for the three light source objects
744 class FilterEffectsDialog::LightSourceControl : public AttrWidget
746 public:
747     LightSourceControl(FilterEffectsDialog& d)
748         : AttrWidget(SP_ATTR_INVALID),
749           _dialog(d),
750           _settings(d, sigc::mem_fun(_dialog, &FilterEffectsDialog::set_child_attr_direct), LIGHT_ENDSOURCE),
751           _light_source(LightSourceConverter)
752     {
753         _box.add(_light_source);
754         _box.reorder_child(_light_source, 0);
755         _light_source.signal_changed().connect(sigc::mem_fun(*this, &LightSourceControl::on_source_changed));
757         // FIXME: these range values are complete crap
759         _settings.type(LIGHT_DISTANT);
760         _settings.add_spinslider(SP_ATTR_AZIMUTH, _("Azimuth"), 0, 360, 1, 1, 0);
761         _settings.add_spinslider(SP_ATTR_AZIMUTH, _("Elevation"), 0, 360, 1, 1, 0);
763         _settings.type(LIGHT_POINT);
764         _settings.add_multispinbutton(SP_ATTR_X, SP_ATTR_Y, SP_ATTR_Z, _("Location"), -99999, 99999, 1, 100, 0);
766         _settings.type(LIGHT_SPOT);
767         _settings.add_multispinbutton(SP_ATTR_X, SP_ATTR_Y, SP_ATTR_Z, _("Location"), -99999, 99999, 1, 100, 0);
768         _settings.add_multispinbutton(SP_ATTR_POINTSATX, SP_ATTR_POINTSATY, SP_ATTR_POINTSATZ,
769                                       _("Points At"), -99999, 99999, 1, 100, 0);
770         _settings.add_spinslider(SP_ATTR_SPECULAREXPONENT, _("Specular Exponent"), 1, 100, 1, 1, 0);
771         _settings.add_spinslider(SP_ATTR_LIMITINGCONEANGLE, _("Cone Angle"), 1, 100, 1, 1, 0);
772     }
774     Gtk::VBox& get_box()
775     {
776         return _box;
777     }
778 protected:
779     Glib::ustring get_as_attribute() const
780     {
781         return "";
782     }
783     void set_from_attribute(SPObject* o)
784     {
785         SPObject* child = o->children;
786         
787         if(SP_IS_FEDISTANTLIGHT(child))
788             _light_source.set_active(0);
789         else if(SP_IS_FEPOINTLIGHT(child))
790             _light_source.set_active(1);
791         else if(SP_IS_FESPOTLIGHT(child))
792             _light_source.set_active(2);
794         update();
795     }
796 private:
797     void on_source_changed()
798     {
799         SPFilterPrimitive* prim = _dialog._primitive_list.get_selected();
800         if(prim) {
801             SPObject* child = prim->children;
802             const int ls = _light_source.get_active_row_number();
803             // Check if the light source type has changed
804             if(!(ls == 0 && SP_IS_FEDISTANTLIGHT(child)) &&
805                !(ls == 1 && SP_IS_FEPOINTLIGHT(child)) &&
806                !(ls == 2 && SP_IS_FESPOTLIGHT(child))) {
807                 if(child)
808                     sp_repr_unparent(child->repr);
810                 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(prim->document);
811                 Inkscape::XML::Node *repr = xml_doc->createElement(_light_source.get_active_data()->key.c_str());
812                 repr->setAttribute("inkscape:collect", "always");
813                 prim->repr->appendChild(repr);
814                 Inkscape::GC::release(repr);
815                 sp_document_done(prim->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("New light source"));
816                 update();
817             }
818         }
819     }
821     void update()
822     {
823         _box.hide_all();
824         _box.show();
825         _light_source.show_all();
826         
827         SPFilterPrimitive* prim = _dialog._primitive_list.get_selected();
828         if(prim && prim->children)
829             _settings.show_and_update(_light_source.get_active_data()->id, prim->children);
830     }
832     FilterEffectsDialog& _dialog;
833     Gtk::VBox _box;
834     Settings _settings;
835     ComboBoxEnum<LightSource> _light_source;
836 };
838 FilterEffectsDialog::LightSourceControl* FilterEffectsDialog::Settings::add_lightsource(const Glib::ustring& label)
840     LightSourceControl* ls = new LightSourceControl(_dialog);
841     add_attr_widget(ls);
842     add_widget(&ls->get_box(), label);
843     return ls;
846 Glib::RefPtr<Gtk::Menu> create_popup_menu(Gtk::Widget& parent, sigc::slot<void> dup,
847                                           sigc::slot<void> rem)
849     Glib::RefPtr<Gtk::Menu> menu(new Gtk::Menu);
851     menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("_Duplicate"), dup));
852     Gtk::MenuItem* mi = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::REMOVE));
853     menu->append(*mi);
854     mi->signal_activate().connect(rem);
855     mi->show();
856     menu->accelerate(parent);
858     return menu;
861 /*** FilterModifier ***/
862 FilterEffectsDialog::FilterModifier::FilterModifier(FilterEffectsDialog& d)
863     : _dialog(d), _add(Gtk::Stock::ADD), _observer(new SignalObserver)
865     Gtk::ScrolledWindow* sw = Gtk::manage(new Gtk::ScrolledWindow);
866     pack_start(*sw);
867     pack_start(_add, false, false);
868     sw->add(_list);
870     _model = Gtk::ListStore::create(_columns);
871     _list.set_model(_model);
872     const int selcol = _list.append_column("", _cell_sel);
873     Gtk::TreeViewColumn* col = _list.get_column(selcol - 1);
874     if(col)
875        col->add_attribute(_cell_sel.property_sel(), _columns.sel);
876     _list.append_column(_("_Filter"), _columns.label);
878     sw->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
879     sw->set_shadow_type(Gtk::SHADOW_IN);
880     show_all_children();
881     _add.signal_clicked().connect(sigc::mem_fun(*this, &FilterModifier::add_filter));
882     _list.signal_button_press_event().connect_notify(
883         sigc::mem_fun(*this, &FilterModifier::filter_list_button_press));
884     _list.signal_button_release_event().connect_notify(
885         sigc::mem_fun(*this, &FilterModifier::filter_list_button_release));
886     _menu = create_popup_menu(*this, sigc::mem_fun(*this, &FilterModifier::duplicate_filter),
887                               sigc::mem_fun(*this, &FilterModifier::remove_filter));
888     _menu->items().push_back(Gtk::Menu_Helpers::MenuElem(
889                                  _("R_ename"), sigc::mem_fun(*this, &FilterModifier::rename_filter)));
890     _menu->accelerate(*this);
892     _list.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &FilterModifier::on_filter_selection_changed));
893     _observer->signal_changed().connect(signal_filter_changed().make_slot());
894     g_signal_connect(G_OBJECT(INKSCAPE), "change_selection",
895                      G_CALLBACK(&FilterModifier::on_inkscape_change_selection), this);
897     g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
898                      G_CALLBACK(&FilterModifier::on_activate_desktop), this);
900     on_activate_desktop(INKSCAPE, SP_ACTIVE_DESKTOP, this);
901     update_filters();
904 FilterEffectsDialog::FilterModifier::~FilterModifier()
906    _resource_changed.disconnect();
907    _doc_replaced.disconnect();
910 FilterEffectsDialog::FilterModifier::CellRendererSel::CellRendererSel()
911     : Glib::ObjectBase(typeid(CellRendererSel)),
912       _size(10),
913       _sel(*this, "sel", 0)
914 {}
916 void FilterEffectsDialog::FilterModifier::CellRendererSel::get_size_vfunc(
917     Gtk::Widget&, const Gdk::Rectangle*, int* x, int* y, int* w, int* h) const
919     if(x)
920         (*x) = 0;
921     if(y)
922         (*y) = 0;
923     if(w)
924         (*w) = _size;
925     if(h)
926         (*h) = _size;
929 void FilterEffectsDialog::FilterModifier::CellRendererSel::render_vfunc(
930     const Glib::RefPtr<Gdk::Drawable>& win, Gtk::Widget& widget, const Gdk::Rectangle& bg_area,
931     const Gdk::Rectangle& cell_area, const Gdk::Rectangle& expose_area, Gtk::CellRendererState flags)
933     const int sel = _sel.get_value();
935     if(sel > 0) {
936         const int s = _size - 2;
937         const int w = cell_area.get_width();
938         const int h = cell_area.get_height();
939         const int x = cell_area.get_x() + w / 2 - s / 2;
940         const int y = cell_area.get_y() + h / 2 - s / 2;
942         win->draw_rectangle(widget.get_style()->get_text_gc(Gtk::STATE_NORMAL), (sel == 1), x, y, s, s);
943     }
946 void FilterEffectsDialog::FilterModifier::on_activate_desktop(Application*, SPDesktop* desktop, FilterModifier* me)
948     me->update_filters();
950     me->_doc_replaced.disconnect();
951     me->_doc_replaced = desktop->connectDocumentReplaced(
952         sigc::mem_fun(me, &FilterModifier::on_document_replaced));
954     me->_resource_changed.disconnect();
955     me->_resource_changed =
956         sp_document_resources_changed_connect(sp_desktop_document(desktop), "filter",
957                                               sigc::mem_fun(me, &FilterModifier::update_filters));
961 // When the selection changes, show the active filter(s) in the dialog
962 void FilterEffectsDialog::FilterModifier::on_inkscape_change_selection(Application *inkscape,
963                                                                        Selection *sel,
964                                                                        FilterModifier* fm)
966     if(fm && sel)
967         fm->update_selection(sel);
970 void FilterEffectsDialog::FilterModifier::update_selection(Selection *sel)
972     std::set<SPObject*> used;
974     for(GSList const *i = sel->itemList(); i != NULL; i = i->next) {
975         SPObject *obj = SP_OBJECT (i->data);
976         SPStyle *style = SP_OBJECT_STYLE (obj);
977         if(!style || !SP_IS_ITEM(obj)) continue;
979         if(style->filter.set && style->getFilter())
980             used.insert(style->getFilter());
981         else
982             used.insert(0);
983     }
985     const int size = used.size();
987     for(Gtk::TreeIter iter = _model->children().begin();
988         iter != _model->children().end(); ++iter) {
989         if(used.find((*iter)[_columns.filter]) != used.end()) {
990             // If only one filter is in use by the selection, select it
991             if(size == 1)
992                 _list.get_selection()->select(iter);
993             (*iter)[_columns.sel] = size;
994         }
995         else
996             (*iter)[_columns.sel] = 0;
997     }
1000 void FilterEffectsDialog::FilterModifier::on_filter_selection_changed()
1002     _observer->set(get_selected_filter());
1003     signal_filter_changed()();
1006 /* Add all filters in the document to the combobox.
1007    Keeps the same selection if possible, otherwise selects the first element */
1008 void FilterEffectsDialog::FilterModifier::update_filters()
1010     SPDesktop* desktop = SP_ACTIVE_DESKTOP;
1011     SPDocument* document = sp_desktop_document(desktop);
1012     const GSList* filters = sp_document_get_resource_list(document, "filter");
1014     _model->clear();
1016     for(const GSList *l = filters; l; l = l->next) {
1017         Gtk::TreeModel::Row row = *_model->append();
1018         SPFilter* f = (SPFilter*)l->data;
1019         row[_columns.filter] = f;
1020         const gchar* lbl = f->label();
1021         const gchar* id = SP_OBJECT_ID(f);
1022         row[_columns.label] = lbl ? lbl : (id ? id : "filter");
1023     }
1026 SPFilter* FilterEffectsDialog::FilterModifier::get_selected_filter()
1028     if(_list.get_selection()) {
1029         Gtk::TreeModel::iterator i = _list.get_selection()->get_selected();
1031         if(i)
1032             return (*i)[_columns.filter];
1033     }
1035     return 0;
1038 void FilterEffectsDialog::FilterModifier::select_filter(const SPFilter* filter)
1040     if(filter) {
1041         for(Gtk::TreeModel::iterator i = _model->children().begin();
1042             i != _model->children().end(); ++i) {
1043             if((*i)[_columns.filter] == filter) {
1044                 _list.get_selection()->select(i);
1045                 break;
1046             }
1047         }
1048     }
1051 void FilterEffectsDialog::FilterModifier::filter_list_button_press(GdkEventButton* e)
1053     // Double-click
1054     if(e->type == GDK_2BUTTON_PRESS) {
1055         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1056         SPDocument *doc = sp_desktop_document(desktop);
1057         SPFilter* filter = get_selected_filter();
1058         Inkscape::Selection *sel = sp_desktop_selection(desktop);
1060         GSList const *items = sel->itemList();
1062         for (GSList const *i = items; i != NULL; i = i->next) {
1063             SPItem * item = SP_ITEM(i->data);
1064             SPStyle *style = SP_OBJECT_STYLE(item);
1065             g_assert(style != NULL);
1066             
1067             sp_style_set_property_url(SP_OBJECT(item), "filter", SP_OBJECT(filter), false);
1068             SP_OBJECT(item)->requestDisplayUpdate((SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG ));
1069         }
1071         update_selection(sel);
1072         sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS,  _("Apply filter"));
1073     }
1076 void FilterEffectsDialog::FilterModifier::filter_list_button_release(GdkEventButton* event)
1078     if((event->type == GDK_BUTTON_RELEASE) && (event->button == 3)) {
1079         const bool sensitive = get_selected_filter() != NULL;
1080         _menu->items()[0].set_sensitive(sensitive);
1081         _menu->items()[1].set_sensitive(sensitive);
1082         _menu->popup(event->button, event->time);
1083     }
1086 void FilterEffectsDialog::FilterModifier::add_filter()
1088     SPDocument* doc = sp_desktop_document(SP_ACTIVE_DESKTOP);
1089     SPFilter* filter = new_filter(doc);
1091     const int count = _model->children().size();
1092     std::ostringstream os;
1093     os << "filter" << count;
1094     filter->setLabel(os.str().c_str());
1096     update_filters();
1098     select_filter(filter);
1100     sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter"));
1103 void FilterEffectsDialog::FilterModifier::remove_filter()
1105     SPFilter *filter = get_selected_filter();
1107     if(filter) {
1108         SPDocument* doc = filter->document;
1109         sp_repr_unparent(filter->repr);
1111         sp_document_done(doc, SP_VERB_DIALOG_FILTER_EFFECTS, _("Remove filter"));
1113         update_filters();
1114     }
1117 void FilterEffectsDialog::FilterModifier::duplicate_filter()
1119     SPFilter* filter = get_selected_filter();
1121     if(filter) {
1122         Inkscape::XML::Node* repr = SP_OBJECT_REPR(filter), *parent = repr->parent();
1123         repr = repr->duplicate(repr->document());
1124         parent->appendChild(repr);
1126         sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter"));
1128         update_filters();
1129     }
1132 void FilterEffectsDialog::FilterModifier::rename_filter()
1134     SPFilter* filter = get_selected_filter();
1135     Gtk::Dialog m("", _dialog, true);
1136     m.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1137     m.add_button(_("_Rename"), Gtk::RESPONSE_OK);
1138     m.set_default_response(Gtk::RESPONSE_OK);
1139     Gtk::Label lbl(_("Filter name:"));
1140     Gtk::Entry entry;
1141     entry.set_text(filter->label() ? filter->label() : "");
1142     Gtk::HBox hb;
1143     hb.add(lbl);
1144     hb.add(entry);
1145     hb.set_spacing(12);
1146     hb.show_all();
1147     m.get_vbox()->add(hb);
1148     const int res = m.run();
1149     if(res == Gtk::RESPONSE_OK) {
1150         filter->setLabel(entry.get_text().c_str());
1151         sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Rename filter"));
1152         Gtk::TreeIter iter = _list.get_selection()->get_selected();
1153         if(iter)
1154             (*iter)[_columns.label] = entry.get_text();
1155     }
1158 FilterEffectsDialog::CellRendererConnection::CellRendererConnection()
1159     : Glib::ObjectBase(typeid(CellRendererConnection)),
1160       _primitive(*this, "primitive", 0)
1161 {}
1163 Glib::PropertyProxy<void*> FilterEffectsDialog::CellRendererConnection::property_primitive()
1165     return _primitive.get_proxy();
1168 void FilterEffectsDialog::CellRendererConnection::set_text_width(const int w)
1170     _text_width = w;
1173 int FilterEffectsDialog::CellRendererConnection::get_text_width() const
1175     return _text_width;
1178 void FilterEffectsDialog::CellRendererConnection::get_size_vfunc(
1179     Gtk::Widget& widget, const Gdk::Rectangle* cell_area,
1180     int* x_offset, int* y_offset, int* width, int* height) const
1182     PrimitiveList& primlist = dynamic_cast<PrimitiveList&>(widget);
1184     if(x_offset)
1185         (*x_offset) = 0;
1186     if(y_offset)
1187         (*y_offset) = 0;
1188     if(width)
1189         (*width) = size * primlist.primitive_count() + _text_width * 7;
1190     if(height) {
1191         // Scale the height depending on the number of inputs, unless it's
1192         // the first primitive, in which case there are no connections
1193         SPFilterPrimitive* prim = (SPFilterPrimitive*)_primitive.get_value();
1194         (*height) = size * input_count(prim);
1195     }
1198 /*** PrimitiveList ***/
1199 FilterEffectsDialog::PrimitiveList::PrimitiveList(FilterEffectsDialog& d)
1200     : _dialog(d),
1201       _in_drag(0),
1202       _observer(new SignalObserver)
1204     add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
1205     signal_expose_event().connect(sigc::mem_fun(*this, &PrimitiveList::on_expose_signal));
1207     _model = Gtk::ListStore::create(_columns);
1209     set_reorderable(true);
1211     set_model(_model);
1212     append_column(_("_Type"), _columns.type);
1214     _observer->signal_changed().connect(signal_primitive_changed().make_slot());
1215     get_selection()->signal_changed().connect(sigc::mem_fun(*this, &PrimitiveList::on_primitive_selection_changed));
1216     signal_primitive_changed().connect(sigc::mem_fun(*this, &PrimitiveList::queue_draw));
1218     _connection_cell.set_text_width(init_text());
1220     int cols_count = append_column(_("Connections"), _connection_cell);
1221     Gtk::TreeViewColumn* col = get_column(cols_count - 1);
1222     if(col)
1223        col->add_attribute(_connection_cell.property_primitive(), _columns.primitive);
1226 // Sets up a vertical Pango context/layout, and returns the largest
1227 // width needed to render the FilterPrimitiveInput labels.
1228 int FilterEffectsDialog::PrimitiveList::init_text()
1230     // Set up a vertical context+layout
1231     Glib::RefPtr<Pango::Context> context = create_pango_context();
1232     const Pango::Matrix matrix = {0, -1, 1, 0, 0, 0};
1233     context->set_matrix(matrix);
1234     _vertical_layout = Pango::Layout::create(context);
1236     int maxfont = 0;
1237     for(int i = 0; i < FPInputConverter.end; ++i) {
1238         _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
1239         int fontw, fonth;
1240         _vertical_layout->get_pixel_size(fontw, fonth);
1241         if(fonth > maxfont)
1242             maxfont = fonth;
1243     }
1245     return maxfont;
1248 sigc::signal<void>& FilterEffectsDialog::PrimitiveList::signal_primitive_changed()
1250     return _signal_primitive_changed;
1253 void FilterEffectsDialog::PrimitiveList::on_primitive_selection_changed()
1255     _observer->set(get_selected());
1256     signal_primitive_changed()();
1257     _dialog._color_matrix_values->clear_store();
1260 /* Add all filter primitives in the current to the list.
1261    Keeps the same selection if possible, otherwise selects the first element */
1262 void FilterEffectsDialog::PrimitiveList::update()
1264     SPFilter* f = _dialog._filter_modifier.get_selected_filter();
1265     const SPFilterPrimitive* active_prim = get_selected();
1266     bool active_found = false;
1268     _model->clear();
1270     if(f) {
1271         _dialog._primitive_box.set_sensitive(true);
1273         for(SPObject *prim_obj = f->children;
1274                 prim_obj && SP_IS_FILTER_PRIMITIVE(prim_obj);
1275                 prim_obj = prim_obj->next) {
1276             SPFilterPrimitive *prim = SP_FILTER_PRIMITIVE(prim_obj);
1277             if(prim) {
1278                 Gtk::TreeModel::Row row = *_model->append();
1279                 row[_columns.primitive] = prim;
1280                 row[_columns.type_id] = FPConverter.get_id_from_key(prim->repr->name());
1281                 row[_columns.type] = FPConverter.get_label(row[_columns.type_id]);
1282                 row[_columns.id] = SP_OBJECT_ID(prim);
1284                 if(prim == active_prim) {
1285                     get_selection()->select(row);
1286                     active_found = true;
1287                 }
1288             }
1289         }
1291         if(!active_found && _model->children().begin())
1292             get_selection()->select(_model->children().begin());
1293     }
1294     else {
1295         _dialog._primitive_box.set_sensitive(false);
1296     }
1299 void FilterEffectsDialog::PrimitiveList::set_menu(Glib::RefPtr<Gtk::Menu> menu)
1301     _primitive_menu = menu;
1304 SPFilterPrimitive* FilterEffectsDialog::PrimitiveList::get_selected()
1306     if(_dialog._filter_modifier.get_selected_filter()) {
1307         Gtk::TreeModel::iterator i = get_selection()->get_selected();
1308         if(i)
1309             return (*i)[_columns.primitive];
1310     }
1312     return 0;
1315 void FilterEffectsDialog::PrimitiveList::select(SPFilterPrimitive* prim)
1317     for(Gtk::TreeIter i = _model->children().begin();
1318         i != _model->children().end(); ++i) {
1319         if((*i)[_columns.primitive] == prim)
1320             get_selection()->select(i);
1321     }
1326 bool FilterEffectsDialog::PrimitiveList::on_expose_signal(GdkEventExpose* e)
1328     Gdk::Rectangle clip(e->area.x, e->area.y, e->area.width, e->area.height);
1329     Glib::RefPtr<Gdk::Window> win = get_bin_window();
1330     Glib::RefPtr<Gdk::GC> darkgc = get_style()->get_dark_gc(Gtk::STATE_NORMAL);
1332     SPFilterPrimitive* prim = get_selected();
1333     int row_count = get_model()->children().size();
1335     int fheight = CellRendererConnection::size;
1336     Gdk::Rectangle rct, vis;
1337     Gtk::TreeIter row = get_model()->children().begin();
1338     int text_start_x = 0;
1339     if(row) {
1340         get_cell_area(get_model()->get_path(row), *get_column(1), rct);
1341         get_visible_rect(vis);
1342         int vis_x, vis_y;
1343         tree_to_widget_coords(vis.get_x(), vis.get_y(), vis_x, vis_y);
1345         text_start_x = rct.get_x() + rct.get_width() - _connection_cell.get_text_width() * (FPInputConverter.end + 1) + 1;
1346         for(int i = 0; i < FPInputConverter.end; ++i) {
1347             _vertical_layout->set_text(FPInputConverter.get_label((FilterPrimitiveInput)i));
1348             const int x = text_start_x + _connection_cell.get_text_width() * (i + 1);
1349             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());
1350             get_bin_window()->draw_layout(get_style()->get_text_gc(Gtk::STATE_NORMAL), x + 1, vis_y, _vertical_layout);
1351             get_bin_window()->draw_line(darkgc, x, vis_y, x, vis_y + vis.get_height());
1352         }
1353     }
1355     int row_index = 0;
1356     for(; row != get_model()->children().end(); ++row, ++row_index) {
1357         get_cell_area(get_model()->get_path(row), *get_column(1), rct);
1358         const int x = rct.get_x(), y = rct.get_y(), h = rct.get_height();
1360         // Check mouse state
1361         int mx, my;
1362         Gdk::ModifierType mask;
1363         get_bin_window()->get_pointer(mx, my, mask);
1365         // Outline the bottom of the connection area
1366         const int outline_x = x + fheight * (row_count - row_index);
1367         get_bin_window()->draw_line(darkgc, x, y + h, outline_x, y + h);
1369         // Side outline
1370         get_bin_window()->draw_line(darkgc, outline_x, y - 1, outline_x, y + h);
1372         std::vector<Gdk::Point> con_poly;
1373         int con_drag_y;
1374         bool inside;
1375         const SPFilterPrimitive* row_prim = (*row)[_columns.primitive];
1376         const int inputs = input_count(row_prim);
1378         if(SP_IS_FEMERGE(row_prim)) {
1379             for(int i = 0; i < inputs; ++i) {
1380                 inside = do_connection_node(row, i, con_poly, mx, my);
1381                 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
1382                                                darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
1383                                                inside, con_poly);
1385                 if(_in_drag == (i + 1))
1386                     con_drag_y = con_poly[2].get_y();
1388                 if(_in_drag != (i + 1) || row_prim != prim)
1389                     draw_connection(row, i, text_start_x, outline_x, con_poly[2].get_y(), row_count);
1390             }
1391         }
1392         else {
1393             // Draw "in" shape
1394             inside = do_connection_node(row, 0, con_poly, mx, my);
1395             con_drag_y = con_poly[2].get_y();
1396             get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
1397                                            darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
1398                                            inside, con_poly);
1400             // Draw "in" connection
1401             if(_in_drag != 1 || row_prim != prim)
1402                 draw_connection(row, SP_ATTR_IN, text_start_x, outline_x, con_poly[2].get_y(), row_count);
1404             if(inputs == 2) {
1405                 // Draw "in2" shape
1406                 inside = do_connection_node(row, 1, con_poly, mx, my);
1407                 if(_in_drag == 2)
1408                     con_drag_y = con_poly[2].get_y();
1409                 get_bin_window()->draw_polygon(inside && mask & GDK_BUTTON1_MASK ?
1410                                                darkgc : get_style()->get_dark_gc(Gtk::STATE_ACTIVE),
1411                                                inside, con_poly);
1412                 // Draw "in2" connection
1413                 if(_in_drag != 2 || row_prim != prim)
1414                     draw_connection(row, SP_ATTR_IN2, text_start_x, outline_x, con_poly[2].get_y(), row_count);
1415             }
1416         }
1418         // Draw drag connection
1419         if(row_prim == prim && _in_drag) {
1420             get_bin_window()->draw_line(get_style()->get_black_gc(), outline_x, con_drag_y,
1421                                         mx, con_drag_y);
1422             get_bin_window()->draw_line(get_style()->get_black_gc(), mx, con_drag_y, mx, my);
1423         }
1424     }
1426     return true;
1429 void FilterEffectsDialog::PrimitiveList::draw_connection(const Gtk::TreeIter& input, const int attr,
1430                                                          const int text_start_x, const int x1, const int y1,
1431                                                          const int row_count)
1433     int src_id;
1434     const Gtk::TreeIter res = find_result(input, attr, src_id);
1435     Glib::RefPtr<Gdk::GC> gc = get_style()->get_black_gc();
1436     
1437     if(res == input) {
1438         // Draw straight connection to a standard input
1439         const int tw = _connection_cell.get_text_width();
1440         gint end_x = text_start_x + tw * (src_id + 1) + (int)(tw * 0.5f) + 1;
1441         get_bin_window()->draw_rectangle(gc, true, end_x-2, y1-2, 5, 5);
1442         get_bin_window()->draw_line(gc, x1, y1, end_x, y1);
1443     }
1444     else if(res != _model->children().end()) {
1445         Gdk::Rectangle rct;
1447         get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
1448         const int fheight = CellRendererConnection::size;
1450         get_cell_area(get_model()->get_path(res), *get_column(1), rct);
1451         const int row_index = find_index(res);
1452         const int x2 = rct.get_x() + fheight * (row_count - row_index) - fheight / 2;
1453         const int y2 = rct.get_y() + rct.get_height();
1455         // Draw an 'L'-shaped connection to another filter primitive
1456         get_bin_window()->draw_line(gc, x1, y1, x2, y1);
1457         get_bin_window()->draw_line(gc, x2, y1, x2, y2);
1458     }
1461 // Creates a triangle outline of the connection node and returns true if (x,y) is inside the node
1462 bool FilterEffectsDialog::PrimitiveList::do_connection_node(const Gtk::TreeIter& row, const int input,
1463                                                             std::vector<Gdk::Point>& points,
1464                                                             const int ix, const int iy)
1466     Gdk::Rectangle rct;
1467     const int icnt = input_count((*row)[_columns.primitive]);
1469     get_cell_area(get_model()->get_path(_model->children().begin()), *get_column(1), rct);
1470     const int fheight = CellRendererConnection::size;
1472     get_cell_area(_model->get_path(row), *get_column(1), rct);
1473     const float h = rct.get_height() / icnt;
1475     const int x = rct.get_x() + fheight * (_model->children().size() - find_index(row));
1476     const int con_w = (int)(fheight * 0.35f);
1477     const int con_y = (int)(rct.get_y() + (h / 2) - con_w + (input * h));
1478     points.clear();
1479     points.push_back(Gdk::Point(x, con_y));
1480     points.push_back(Gdk::Point(x, con_y + con_w * 2));
1481     points.push_back(Gdk::Point(x - con_w, con_y + con_w));
1483     return ix >= x - h && iy >= con_y && ix <= x && iy <= points[1].get_y();
1486 const Gtk::TreeIter FilterEffectsDialog::PrimitiveList::find_result(const Gtk::TreeIter& start,
1487                                                                     const int attr, int& src_id)
1489     SPFilterPrimitive* prim = (*start)[_columns.primitive];
1490     Gtk::TreeIter target = _model->children().end();
1491     int image;
1493     if(SP_IS_FEMERGE(prim)) {
1494         int c = 0;
1495         for(const SPObject* o = prim->firstChild(); o; o = o->next, ++c) {
1496             if(c == attr && SP_IS_FEMERGENODE(o))
1497                 image = SP_FEMERGENODE(o)->input;
1498         }
1499     }
1500     else {
1501         if(attr == SP_ATTR_IN)
1502             image = prim->image_in;
1503         else if(attr == SP_ATTR_IN2) {
1504             if(SP_IS_FEBLEND(prim))
1505                 image = SP_FEBLEND(prim)->in2;
1506             else if(SP_IS_FECOMPOSITE(prim))
1507                 image = SP_FECOMPOSITE(prim)->in2;
1508             else if(SP_IS_FEDISPLACEMENTMAP(prim))
1509                 image = SP_FEDISPLACEMENTMAP(prim)->in2;
1510             else
1511                 return target;
1512         }
1513         else
1514             return target;
1515     }
1517     if(image >= 0) {
1518         for(Gtk::TreeIter i = _model->children().begin();
1519             i != start; ++i) {
1520             if(((SPFilterPrimitive*)(*i)[_columns.primitive])->image_out == image)
1521                 target = i;
1522         }
1523         return target;
1524     }
1525     else if(image < -1) {
1526         src_id = -(image + 2);
1527         return start;
1528     }
1530     return target;
1533 int FilterEffectsDialog::PrimitiveList::find_index(const Gtk::TreeIter& target)
1535     int i = 0;
1536     for(Gtk::TreeIter iter = _model->children().begin();
1537         iter != target; ++iter, ++i);
1538     return i;
1541 bool FilterEffectsDialog::PrimitiveList::on_button_press_event(GdkEventButton* e)
1543     Gtk::TreePath path;
1544     Gtk::TreeViewColumn* col;
1545     const int x = (int)e->x, y = (int)e->y;
1546     int cx, cy;
1548     _drag_prim = 0;
1549     
1550     if(get_path_at_pos(x, y, path, col, cx, cy)) {
1551         Gtk::TreeIter iter = _model->get_iter(path);
1552         std::vector<Gdk::Point> points;
1554         _drag_prim = (*iter)[_columns.primitive];
1555         const int icnt = input_count(_drag_prim);
1557         for(int i = 0; i < icnt; ++i) {
1558             if(do_connection_node(_model->get_iter(path), i, points, x, y)) {
1559                 _in_drag = i + 1;
1560                 break;
1561             }
1562         }
1563         
1564         queue_draw();
1565     }
1567     if(_in_drag) {
1568         _scroll_connection = Glib::signal_timeout().connect(sigc::mem_fun(*this, &PrimitiveList::on_scroll_timeout), 150);
1569         _autoscroll = 0;
1570         get_selection()->select(path);
1571         return true;
1572     }
1573     else
1574         return Gtk::TreeView::on_button_press_event(e);
1577 bool FilterEffectsDialog::PrimitiveList::on_motion_notify_event(GdkEventMotion* e)
1579     const int speed = 10;
1580     const int limit = 15;
1582     Gdk::Rectangle vis;
1583     get_visible_rect(vis);
1584     int vis_x, vis_y;
1585     tree_to_widget_coords(vis.get_x(), vis.get_y(), vis_x, vis_y);
1586     const int top = vis_y + vis.get_height();
1588     // When autoscrolling during a connection drag, set the speed based on
1589     // where the mouse is in relation to the edges.
1590     if(e->y < vis_y)
1591         _autoscroll = -(int)(speed + (vis_y - e->y) / 5);
1592     else if(e->y < vis_y + limit)
1593         _autoscroll = -speed;
1594     else if(e->y > top)
1595         _autoscroll = (int)(speed + (e->y - top) / 5);
1596     else if(e->y > top - limit)
1597         _autoscroll = speed;
1598     else
1599         _autoscroll = 0;
1601     queue_draw();
1603     return Gtk::TreeView::on_motion_notify_event(e);
1606 bool FilterEffectsDialog::PrimitiveList::on_button_release_event(GdkEventButton* e)
1608     SPFilterPrimitive *prim = get_selected(), *target;
1610     _scroll_connection.disconnect();
1612     if(_in_drag && prim) {
1613         Gtk::TreePath path;
1614         Gtk::TreeViewColumn* col;
1615         int cx, cy;
1616         
1617         if(get_path_at_pos((int)e->x, (int)e->y, path, col, cx, cy)) {
1618             const gchar *in_val = 0;
1619             Glib::ustring result;
1620             Gtk::TreeIter target_iter = _model->get_iter(path);
1621             target = (*target_iter)[_columns.primitive];
1623             Gdk::Rectangle rct;
1624             get_cell_area(path, *col, rct);
1625             const int twidth = _connection_cell.get_text_width();
1626             const int sources_x = rct.get_width() - twidth * FPInputConverter.end;
1627             if(cx > sources_x) {
1628                 int src = (cx - sources_x) / twidth;
1629                 if(src < 0)
1630                     src = 0;
1631                 else if(src >= FPInputConverter.end)
1632                     src = FPInputConverter.end - 1;
1633                 result = FPInputConverter.get_key((FilterPrimitiveInput)src);
1634                 in_val = result.c_str();
1635             }
1636             else {
1637                 // Ensure that the target comes before the selected primitive
1638                 for(Gtk::TreeIter iter = _model->children().begin();
1639                     iter != get_selection()->get_selected(); ++iter) {
1640                     if(iter == target_iter) {
1641                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(target);
1642                         // Make sure the target has a result
1643                         const gchar *gres = repr->attribute("result");
1644                         if(!gres) {
1645                             result = "result" + Glib::Ascii::dtostr(SP_FILTER(prim->parent)->_image_number_next);
1646                             repr->setAttribute("result", result.c_str());
1647                             in_val = result.c_str();
1648                         }
1649                         else
1650                             in_val = gres;
1651                         break;
1652                     }
1653                 }
1654             }
1656             if(SP_IS_FEMERGE(prim)) {
1657                 int c = 1;
1658                 bool handled = false;
1659                 for(SPObject* o = prim->firstChild(); o && !handled; o = o->next, ++c) {
1660                     if(c == _in_drag && SP_IS_FEMERGENODE(o)) {
1661                         // If input is null, delete it
1662                         if(!in_val) {
1663                             sp_repr_unparent(o->repr);
1664                             sp_document_done(prim->document, SP_VERB_DIALOG_FILTER_EFFECTS,
1665                                              _("Remove merge node"));
1666                             (*get_selection()->get_selected())[_columns.primitive] = prim;
1667                         }
1668                         else
1669                             _dialog.set_attr(o, SP_ATTR_IN, in_val);
1670                         handled = true;
1671                     }
1672                 }
1673                 // Add new input?
1674                 if(!handled && c == _in_drag) {
1675                     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(prim->document);
1676                     Inkscape::XML::Node *repr = xml_doc->createElement("svg:feMergeNode");
1677                     repr->setAttribute("inkscape:collect", "always");
1678                     prim->repr->appendChild(repr);
1679                     SPFeMergeNode *node = SP_FEMERGENODE(prim->document->getObjectByRepr(repr));
1680                     Inkscape::GC::release(repr);
1681                     _dialog.set_attr(node, SP_ATTR_IN, in_val);
1682                     (*get_selection()->get_selected())[_columns.primitive] = prim;
1683                 }
1684             }
1685             else {
1686                 if(_in_drag == 1)
1687                     _dialog.set_attr(prim, SP_ATTR_IN, in_val);
1688                 else if(_in_drag == 2)
1689                     _dialog.set_attr(prim, SP_ATTR_IN2, in_val);
1690             }
1691         }
1693         _in_drag = 0;
1694         queue_draw();
1696         _dialog.update_settings_view();
1697     }
1699     if((e->type == GDK_BUTTON_RELEASE) && (e->button == 3)) {
1700         const bool sensitive = get_selected() != NULL;
1701         _primitive_menu->items()[0].set_sensitive(sensitive);
1702         _primitive_menu->items()[1].set_sensitive(sensitive);
1703         _primitive_menu->popup(e->button, e->time);
1705         return true;
1706     }
1707     else
1708         return Gtk::TreeView::on_button_release_event(e);
1711 // Checks all of prim's inputs, removes any that use result
1712 void check_single_connection(SPFilterPrimitive* prim, const int result)
1714     if(prim && result >= 0) {
1716         if(prim->image_in == result)
1717             SP_OBJECT_REPR(prim)->setAttribute("in", 0);
1719         if(SP_IS_FEBLEND(prim)) {
1720             if(SP_FEBLEND(prim)->in2 == result)
1721                 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
1722         }
1723         else if(SP_IS_FECOMPOSITE(prim)) {
1724             if(SP_FECOMPOSITE(prim)->in2 == result)
1725                 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
1726         }
1727         else if(SP_IS_FEDISPLACEMENTMAP(prim)) {
1728             if(SP_FEDISPLACEMENTMAP(prim)->in2 == result)
1729                 SP_OBJECT_REPR(prim)->setAttribute("in2", 0);
1730         }
1731     }
1734 // Remove any connections going to/from prim_iter that forward-reference other primitives
1735 void FilterEffectsDialog::PrimitiveList::sanitize_connections(const Gtk::TreeIter& prim_iter)
1737     SPFilterPrimitive *prim = (*prim_iter)[_columns.primitive];
1738     bool before = true;
1740     for(Gtk::TreeIter iter = _model->children().begin();
1741         iter != _model->children().end(); ++iter) {
1742         if(iter == prim_iter)
1743             before = false;
1744         else {
1745             SPFilterPrimitive* cur_prim = (*iter)[_columns.primitive];
1746             if(before)
1747                 check_single_connection(cur_prim, prim->image_out);
1748             else
1749                 check_single_connection(prim, cur_prim->image_out);
1750         }
1751     }
1754 // Reorder the filter primitives to match the list order
1755 void FilterEffectsDialog::PrimitiveList::on_drag_end(const Glib::RefPtr<Gdk::DragContext>& dc)
1757     SPFilter* filter = _dialog._filter_modifier.get_selected_filter();
1758     int ndx = 0;
1760     for(Gtk::TreeModel::iterator iter = _model->children().begin();
1761         iter != _model->children().end(); ++iter, ++ndx) {
1762         SPFilterPrimitive* prim = (*iter)[_columns.primitive];
1763         if(prim && prim == _drag_prim) {
1764             SP_OBJECT_REPR(prim)->setPosition(ndx);
1765             break;
1766         }
1767     }
1769     for(Gtk::TreeModel::iterator iter = _model->children().begin();
1770         iter != _model->children().end(); ++iter, ++ndx) {
1771         SPFilterPrimitive* prim = (*iter)[_columns.primitive];
1772         if(prim && prim == _drag_prim) {
1773             sanitize_connections(iter);
1774             get_selection()->select(iter);
1775             break;
1776         }
1777     }
1779     filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
1781     sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Reorder filter primitive"));
1784 // If a connection is dragged towards the top or bottom of the list, the list should scroll to follow.
1785 bool FilterEffectsDialog::PrimitiveList::on_scroll_timeout()
1787     if(_autoscroll) {
1788         Gtk::Adjustment& a = *dynamic_cast<Gtk::ScrolledWindow*>(get_parent())->get_vadjustment();
1789         double v;
1791         v = a.get_value() + _autoscroll;
1792         if(v < 0)
1793             v = 0;
1794         if(v > a.get_upper() - a.get_page_size())
1795             v = a.get_upper() - a.get_page_size();
1797         a.set_value(v);
1799         queue_draw();
1800     }
1802     return true;
1805 int FilterEffectsDialog::PrimitiveList::primitive_count() const
1807     return _model->children().size();
1810 /*** FilterEffectsDialog ***/
1812 FilterEffectsDialog::FilterEffectsDialog() 
1813     : Dialog ("dialogs.filtereffects", SP_VERB_DIALOG_FILTER_EFFECTS),
1814       _filter_modifier(*this),
1815       _primitive_list(*this),
1816       _add_primitive_type(FPConverter),
1817       _add_primitive(Gtk::Stock::ADD),
1818       _empty_settings(_("No primitive selected"), Gtk::ALIGN_LEFT),
1819       _locked(false),
1820       _attr_lock(false)
1822     _settings = new Settings(*this, sigc::mem_fun(*this, &FilterEffectsDialog::set_attr_direct),
1823                              NR_FILTER_ENDPRIMITIVETYPE);
1824     _sizegroup = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL);
1825     _sizegroup->set_ignore_hidden();
1826         
1827     // Initialize widget hierarchy
1828     Gtk::HPaned* hpaned = Gtk::manage(new Gtk::HPaned);
1829     Gtk::ScrolledWindow* sw_prims = Gtk::manage(new Gtk::ScrolledWindow);
1830     Gtk::HBox* hb_prims = Gtk::manage(new Gtk::HBox);
1831     Gtk::Frame* fr_settings = Gtk::manage(new Gtk::Frame(_("<b>Settings</b>")));
1832     Gtk::Alignment* al_settings = Gtk::manage(new Gtk::Alignment);
1833     get_vbox()->add(*hpaned);
1834     hpaned->pack1(_filter_modifier);
1835     hpaned->pack2(_primitive_box);
1836     _primitive_box.pack_start(*sw_prims);
1837     _primitive_box.pack_start(*hb_prims, false, false);
1838     sw_prims->add(_primitive_list);
1839     hb_prims->pack_end(_add_primitive, false, false);
1840     hb_prims->pack_end(_add_primitive_type, false, false);
1841     get_vbox()->pack_start(*fr_settings, false, false);
1842     fr_settings->add(*al_settings);
1843     al_settings->add(_settings_box);
1845     _primitive_list.signal_primitive_changed().connect(
1846         sigc::mem_fun(*this, &FilterEffectsDialog::update_settings_view));
1847     _filter_modifier.signal_filter_changed().connect(
1848         sigc::mem_fun(_primitive_list, &PrimitiveList::update));
1850     sw_prims->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
1851     sw_prims->set_shadow_type(Gtk::SHADOW_IN);
1852     al_settings->set_padding(0, 0, 12, 0);
1853     fr_settings->set_shadow_type(Gtk::SHADOW_NONE);
1854     ((Gtk::Label*)fr_settings->get_label_widget())->set_use_markup();
1855     _add_primitive.signal_clicked().connect(sigc::mem_fun(*this, &FilterEffectsDialog::add_primitive));
1856     _primitive_list.set_menu(create_popup_menu(*this, sigc::mem_fun(*this, &FilterEffectsDialog::duplicate_primitive),
1857                                                sigc::mem_fun(*this, &FilterEffectsDialog::remove_primitive)));
1858     
1859     show_all_children();
1860     init_settings_widgets();
1861     _primitive_list.update();
1862     update_settings_view();
1865 FilterEffectsDialog::~FilterEffectsDialog()
1867     delete _settings;
1870 void FilterEffectsDialog::set_attrs_locked(const bool l)
1872     _locked = l;
1875 void FilterEffectsDialog::init_settings_widgets()
1877     // TODO: Find better range/climb-rate/digits values for the SpinSliders,
1878     //       most of the current values are complete guesses!
1880     _empty_settings.set_sensitive(false);
1881     _settings_box.pack_start(_empty_settings);
1883     _settings->type(NR_FILTER_BLEND);
1884     _settings->add_combo(SP_ATTR_MODE, _("Mode"), BlendModeConverter);
1886     _settings->type(NR_FILTER_COLORMATRIX);
1887     ComboBoxEnum<FilterColorMatrixType>* colmat = _settings->add_combo(SP_ATTR_TYPE, _("Type"), ColorMatrixTypeConverter);
1888     _color_matrix_values = _settings->add_colormatrixvalues(_("Value(s)"));
1889     colmat->signal_attr_changed().connect(sigc::mem_fun(*this, &FilterEffectsDialog::update_color_matrix));
1891     _settings->type(NR_FILTER_COMPONENTTRANSFER);
1892     _settings->add_combo(SP_ATTR_TYPE, _("Type"), ComponentTransferTypeConverter);
1893     _ct_slope = _settings->add_spinslider(SP_ATTR_SLOPE, _("Slope"), -100, 100, 1, 0.01, 1);
1894     _ct_intercept = _settings->add_spinslider(SP_ATTR_INTERCEPT, _("Intercept"), -100, 100, 1, 0.01, 1);
1895     _ct_amplitude = _settings->add_spinslider(SP_ATTR_AMPLITUDE, _("Amplitude"), 0, 100, 1, 0.01, 1);
1896     _ct_exponent = _settings->add_spinslider(SP_ATTR_EXPONENT, _("Exponent"), 0, 100, 1, 0.01, 1);
1897     _ct_offset = _settings->add_spinslider(SP_ATTR_OFFSET, _("Offset"), -100, 100, 1, 0.01, 1);
1899     _settings->type(NR_FILTER_COMPOSITE);
1900     _settings->add_combo(SP_ATTR_OPERATOR, _("Operator"), CompositeOperatorConverter);
1901     _k1 = _settings->add_spinslider(SP_ATTR_K1, _("K1"), -10, 10, 1, 0.01, 1);
1902     _k2 = _settings->add_spinslider(SP_ATTR_K2, _("K2"), -10, 10, 1, 0.01, 1);
1903     _k3 = _settings->add_spinslider(SP_ATTR_K3, _("K3"), -10, 10, 1, 0.01, 1);
1904     _k4 = _settings->add_spinslider(SP_ATTR_K4, _("K4"), -10, 10, 1, 0.01, 1);
1906     _settings->type(NR_FILTER_CONVOLVEMATRIX);
1907     _convolve_order = _settings->add_dualspinbutton(SP_ATTR_ORDER, _("Size"), 1, 5, 1, 1, 0);
1908     _convolve_target = _settings->add_multispinbutton(SP_ATTR_TARGETX, SP_ATTR_TARGETY, _("Target"), 0, 4, 1, 1, 0);
1909     _convolve_matrix = _settings->add_matrix(SP_ATTR_KERNELMATRIX, _("Kernel"));
1910     _convolve_order->signal_attr_changed().connect(sigc::mem_fun(*this, &FilterEffectsDialog::convolve_order_changed));
1911     _settings->add_spinslider(SP_ATTR_DIVISOR, _("Divisor"), 0.01, 10, 1, 0.01, 1);
1912     _settings->add_spinslider(SP_ATTR_BIAS, _("Bias"), -10, 10, 1, 0.01, 1);
1913     _settings->add_combo(SP_ATTR_EDGEMODE, _("Edge Mode"), ConvolveMatrixEdgeModeConverter);
1915     _settings->type(NR_FILTER_DIFFUSELIGHTING);
1916     _settings->add_color(SP_PROP_LIGHTING_COLOR, _("Diffuse Color"));
1917     _settings->add_spinslider(SP_ATTR_SURFACESCALE, _("Surface Scale"), -10, 10, 1, 0.01, 1);
1918     _settings->add_spinslider(SP_ATTR_DIFFUSECONSTANT, _("Constant"), 0, 100, 1, 0.01, 1);
1919     _settings->add_dualspinslider(SP_ATTR_KERNELUNITLENGTH, _("Kernel Unit Length"), 0.01, 10, 1, 0.01, 1);
1920     _settings->add_lightsource(_("Light Source"));
1922     _settings->type(NR_FILTER_DISPLACEMENTMAP);
1923     _settings->add_spinslider(SP_ATTR_SCALE, _("Scale"), 0, 100, 1, 0.01, 1);
1924     _settings->add_combo(SP_ATTR_XCHANNELSELECTOR, _("X Channel"), DisplacementMapChannelConverter);
1925     _settings->add_combo(SP_ATTR_YCHANNELSELECTOR, _("Y Channel"), DisplacementMapChannelConverter);
1927     _settings->type(NR_FILTER_FLOOD);
1928     _settings->add_color(SP_PROP_FLOOD_COLOR, _("Flood Color"));
1929     _settings->add_spinslider(SP_PROP_FLOOD_OPACITY, _("Opacity"), 0, 1, 0.1, 0.01, 2);
1930     
1931     _settings->type(NR_FILTER_GAUSSIANBLUR);
1932     _settings->add_dualspinslider(SP_ATTR_STDDEVIATION, _("Standard Deviation"), 0.01, 100, 1, 0.01, 1);
1934     _settings->type(NR_FILTER_MORPHOLOGY);
1935     _settings->add_combo(SP_ATTR_OPERATOR, _("Operator"), MorphologyOperatorConverter);
1936     _settings->add_dualspinslider(SP_ATTR_RADIUS, _("Radius"), 0, 100, 1, 0.01, 1);
1938     _settings->type(NR_FILTER_OFFSET);
1939     _settings->add_spinslider(SP_ATTR_DX, _("Delta X"), -100, 100, 1, 0.01, 1);
1940     _settings->add_spinslider(SP_ATTR_DY, _("Delta Y"), -100, 100, 1, 0.01, 1);
1942     _settings->type(NR_FILTER_SPECULARLIGHTING);
1943     _settings->add_color(SP_PROP_LIGHTING_COLOR, _("Specular Color"));
1944     _settings->add_spinslider(SP_ATTR_SURFACESCALE, _("Surface Scale"), -10, 10, 1, 0.01, 1);
1945     _settings->add_spinslider(SP_ATTR_SPECULARCONSTANT, _("Constant"), 0, 100, 1, 0.01, 1);
1946     _settings->add_spinslider(SP_ATTR_SPECULAREXPONENT, _("Exponent"), 1, 128, 1, 0.01, 1);
1947     _settings->add_dualspinslider(SP_ATTR_KERNELUNITLENGTH, _("Kernel Unit Length"), 0.01, 10, 1, 0.01, 1);
1948     _settings->add_lightsource(_("Light Source"));
1950     _settings->type(NR_FILTER_TURBULENCE);
1951     _settings->add_checkbutton(SP_ATTR_STITCHTILES, _("Stitch Tiles"), "stitch", "noStitch");
1952     _settings->add_combo(SP_ATTR_TYPE, _("Type"), TurbulenceTypeConverter);
1953     _settings->add_dualspinslider(SP_ATTR_BASEFREQUENCY, _("Base Frequency"), 0, 100, 1, 0.01, 1);
1954     _settings->add_spinslider(SP_ATTR_NUMOCTAVES, _("Octaves"), 1, 10, 1, 1, 0);
1955     _settings->add_spinslider(SP_ATTR_SEED, _("Seed"), 0, 1000, 1, 1, 0);
1958 void FilterEffectsDialog::add_primitive()
1960     SPFilter* filter = _filter_modifier.get_selected_filter();
1961     
1962     if(filter) {
1963         SPFilterPrimitive* prim = filter_add_primitive(filter, _add_primitive_type.get_active_data()->id);
1965         _primitive_list.update();
1966         _primitive_list.select(prim);
1968         sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Add filter primitive"));
1969     }
1972 void FilterEffectsDialog::remove_primitive()
1974     SPFilterPrimitive* prim = _primitive_list.get_selected();
1976     if(prim) {
1977         sp_repr_unparent(prim->repr);
1979         sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_DIALOG_FILTER_EFFECTS,
1980                          _("Remove filter primitive"));
1982         _primitive_list.update();
1983     }
1986 void FilterEffectsDialog::duplicate_primitive()
1988     SPFilter* filter = _filter_modifier.get_selected_filter();
1989     SPFilterPrimitive* origprim = _primitive_list.get_selected();
1991     if(filter && origprim) {
1992         Inkscape::XML::Node *repr;
1993         repr = SP_OBJECT_REPR(origprim)->duplicate(SP_OBJECT_REPR(origprim)->document());
1994         SP_OBJECT_REPR(filter)->appendChild(repr);
1996         sp_document_done(filter->document, SP_VERB_DIALOG_FILTER_EFFECTS, _("Duplicate filter primitive"));
1998         _primitive_list.update();
1999     }
2002 void FilterEffectsDialog::convolve_order_changed()
2004     _convolve_matrix->set_from_attribute(SP_OBJECT(_primitive_list.get_selected()));
2005     _convolve_target->get_spinbuttons()[0]->get_adjustment()->set_upper(_convolve_order->get_spinbutton1().get_value() - 1);
2006     _convolve_target->get_spinbuttons()[1]->get_adjustment()->set_upper(_convolve_order->get_spinbutton2().get_value() - 1);
2009 void FilterEffectsDialog::set_attr_direct(const AttrWidget* input)
2011     set_attr(_primitive_list.get_selected(), input->get_attribute(), input->get_as_attribute().c_str());
2014 void FilterEffectsDialog::set_child_attr_direct(const AttrWidget* input)
2016     set_attr(_primitive_list.get_selected()->children, input->get_attribute(), input->get_as_attribute().c_str());
2019 void FilterEffectsDialog::set_attr(SPObject* o, const SPAttributeEnum attr, const gchar* val)
2021     if(!_locked) {
2022         _attr_lock = true;
2024         SPFilter *filter = _filter_modifier.get_selected_filter();
2025         const gchar* name = (const gchar*)sp_attribute_name(attr);
2026         if(filter && name && o) {
2027             update_settings_sensitivity();
2029             SP_OBJECT_REPR(o)->setAttribute(name, val);
2030             filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
2032             Glib::ustring undokey = "filtereffects:";
2033             undokey += name;
2034             sp_document_maybe_done(filter->document, undokey.c_str(), SP_VERB_DIALOG_FILTER_EFFECTS,
2035                                    _("Set filter primitive attribute"));
2036         }
2038         _attr_lock = false;
2039     }
2042 void FilterEffectsDialog::update_settings_view()
2044     update_settings_sensitivity();
2046     if(_attr_lock)
2047         return;
2049     SPFilterPrimitive* prim = _primitive_list.get_selected();
2051     if(prim) {
2052         _settings->show_and_update(FPConverter.get_id_from_key(prim->repr->name()), prim);
2053         _empty_settings.hide();
2054     }
2055     else {
2056         _settings_box.hide_all();
2057         _settings_box.show();
2058         _empty_settings.show();
2059     }
2062 void FilterEffectsDialog::update_settings_sensitivity()
2064     SPFilterPrimitive* prim = _primitive_list.get_selected();
2065     const bool use_k = SP_IS_FECOMPOSITE(prim) && SP_FECOMPOSITE(prim)->composite_operator == COMPOSITE_ARITHMETIC;
2066     _k1->set_sensitive(use_k);
2067     _k2->set_sensitive(use_k);
2068     _k3->set_sensitive(use_k);
2069     _k4->set_sensitive(use_k);
2071     if(SP_IS_FECOMPONENTTRANSFER(prim)) {
2072         SPFeComponentTransfer* ct = SP_FECOMPONENTTRANSFER(prim);
2073         const bool linear = ct->type == COMPONENTTRANSFER_TYPE_LINEAR;
2074         const bool gamma = ct->type == COMPONENTTRANSFER_TYPE_GAMMA;
2075         //_ct_table->set_sensitive(ct->type == COMPONENTTRANSFER_TYPE_TABLE || ct->type == COMPONENTTRANSFER_TYPE_DISCRETE);
2076         _ct_slope->set_sensitive(linear);
2077         _ct_intercept->set_sensitive(linear);
2078         _ct_amplitude->set_sensitive(gamma);
2079         _ct_exponent->set_sensitive(gamma);
2080         _ct_offset->set_sensitive(gamma);
2081     }
2084 void FilterEffectsDialog::update_color_matrix()
2086     _color_matrix_values->set_from_attribute(_primitive_list.get_selected());
2089 } // namespace Dialog
2090 } // namespace UI
2091 } // namespace Inkscape
2093 /*
2094   Local Variables:
2095   mode:c++
2096   c-file-style:"stroustrup"
2097   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2098   indent-tabs-mode:nil
2099   fill-column:99
2100   End:
2101 */
2102 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :