Code

3ac68a16ff8eff3e063d1e5041341e1706e4bee3
[inkscape.git] / filter-effect-enums.h
1 /**
2  * \brief Simplified management of enumerations for filter effects
3  *
4  * Authors:
5  *   Nicholas Bishop <nicholasbishop@gmail.com>
6  *
7  * Copyright (C) 2007 Authors
8  *
9  * Released under GNU GPL.  Read the file 'COPYING' for more information.
10  */
12 #ifndef INKSCAPE_UI_WIDGET_FILTER_EFFECT_ENUMS_H
13 #define INKSCAPE_UI_WIDGET_FILTER_EFFECT_ENUMS_H
15 #include "display/nr-filter-blend.h"
16 #include "display/nr-filter-types.h"
18 namespace Inkscape {
19 namespace UI {
20 namespace Widget {
22 template<typename E> struct EnumData
23 {
24     E id;
25     const Glib::ustring label;
26     const Glib::ustring name;
27 };
29 template<typename E> class Converter
30 {
31 public:
32     typedef EnumData<E> Data;
34     Converter(const EnumData<E>* cd, const int endval)
35         : end(endval), _data(cd)
36     {}
38     E get_id_from_label(const Glib::ustring& label) const
39     {
40         for(int i = 0; i < end; ++i) {
41             if(_data[i].label == label)
42                 return (E)i;
43         }
45         return (E)0;
46     }
48     E get_id_from_name(const Glib::ustring& name) const
49     {
50         for(int i = 0; i < end; ++i) {
51             if(_data[i].name == name)
52                 return (E)i;
53         }
55         return (E)0;
56     }
58     const Glib::ustring& get_label(const E e) const
59     {
60         return _data[e].label;
61     }
63     const Glib::ustring& get_name(const E e) const
64     {
65         return _data[e].name;
66     }
68     const EnumData<E>& data(const int i) const
69     {
70         return _data[i];
71     }
73     const int end;
74 private:
75     const EnumData<E>* _data;
76 };
78 template<typename E> class ComboBoxEnum : public Gtk::ComboBox
79 {
80 public:
81     ComboBoxEnum(const Converter<E>& c)
82         : _converter(c)
83     {
84         _model = Gtk::ListStore::create(_columns);
85         set_model(_model);
87         pack_start(_columns.label);
89         // Initialize list
90         for(int i = 0; i < _converter.end; ++i) {
91             Gtk::TreeModel::Row row = *_model->append();
92             const EnumData<E>* data = &_converter.data(i);
93             row[_columns.data] = data;
94             row[_columns.label] = _converter.get_label(data->id);
95         }
97         set_active(0);
98     }
99     
100     const EnumData<E>* get_active_data()
101     {
102         Gtk::TreeModel::iterator i = this->get_active();
103         if(i)
104             return (*i)[_columns.data];
105         return 0;
106     }
108     void add_row(const Glib::ustring& s)
109     {
110         Gtk::TreeModel::Row row = *_model->append();
111         row[_columns.data] = 0;
112         row[_columns.label] = s;
113     }
114 private:
115     class Columns : public Gtk::TreeModel::ColumnRecord
116     {
117     public:
118         Columns()
119         {
120             add(data);
121             add(label);
122         }
124         Gtk::TreeModelColumn<const EnumData<E>*> data;
125         Gtk::TreeModelColumn<Glib::ustring> label;
126     };
128     Columns _columns;
129     Glib::RefPtr<Gtk::ListStore> _model;
130     const Converter<E>& _converter;
131 };
133 /*** Filter Primitives ***/
134 extern const EnumData<NR::FilterPrimitiveType> FPData[NR::NR_FILTER_ENDPRIMITIVETYPE];
135 extern const Converter<NR::FilterPrimitiveType> FPConverter;
137 /*** feBlend Mode ***/
138 extern const EnumData<NR::FilterBlendMode> BlendModeData[NR::BLEND_ENDMODE];
139 extern const Converter<NR::FilterBlendMode> BlendModeConverter;
145 #endif
147 /*
148   Local Variables:
149   mode:c++
150   c-file-style:"stroustrup"
151   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
152   indent-tabs-mode:nil
153   fill-column:99
154   End:
155 */
156 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :