1 /**
2 * \brief Simplified management of enumerations in the UI as combobox.
3 *
4 * Authors:
5 * Nicholas Bishop <nicholasbishop@gmail.com>
6 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
7 *
8 * Copyright (C) 2007 Authors
9 *
10 * Released under GNU GPL. Read the file 'COPYING' for more information.
11 */
13 #ifndef INKSCAPE_UI_WIDGET_COMBO_ENUMS_H
14 #define INKSCAPE_UI_WIDGET_COMBO_ENUMS_H
16 #include <gtkmm/combobox.h>
17 #include <gtkmm/liststore.h>
18 #include "attr-widget.h"
19 #include "util/enums.h"
20 #include "ui/widget/labelled.h"
22 namespace Inkscape {
23 namespace UI {
24 namespace Widget {
26 template<typename E> class ComboBoxEnum : public Gtk::ComboBox, public AttrWidget
27 {
28 private:
29 int on_sort_compare( const Gtk::TreeModel::iterator & a, const Gtk::TreeModel::iterator & b)
30 {
31 Glib::ustring an=(*a)[_columns.label];
32 Glib::ustring bn=(*b)[_columns.label];
33 return an.compare(bn);
34 }
36 bool _sort;
38 public:
39 ComboBoxEnum(E default_value, const Util::EnumDataConverter<E>& c, const SPAttributeEnum a = SP_ATTR_INVALID, bool sort = true)
40 : AttrWidget(a, (unsigned int)default_value), setProgrammatically(false), _converter(c)
41 {
42 _sort = sort;
44 signal_changed().connect(signal_attr_changed().make_slot());
46 _model = Gtk::ListStore::create(_columns);
47 set_model(_model);
49 pack_start(_columns.label);
51 // Initialize list
52 for(int i = 0; i < static_cast<int>(_converter._length); ++i) {
53 Gtk::TreeModel::Row row = *_model->append();
54 const Util::EnumData<E>* data = &_converter.data(i);
55 row[_columns.data] = data;
56 row[_columns.label] = _( _converter.get_label(data->id).c_str() );
57 }
58 set_active_by_id(default_value);
60 // Sort the list
61 if (sort) {
62 _model->set_default_sort_func(sigc::mem_fun(*this, &ComboBoxEnum<E>::on_sort_compare));
63 _model->set_sort_column(_columns.label, Gtk::SORT_ASCENDING);
64 }
65 }
67 ComboBoxEnum(const Util::EnumDataConverter<E>& c, const SPAttributeEnum a = SP_ATTR_INVALID, bool sort = true)
68 : AttrWidget(a, (unsigned int) 0), setProgrammatically(false), _converter(c)
69 {
70 _sort = sort;
72 signal_changed().connect(signal_attr_changed().make_slot());
74 _model = Gtk::ListStore::create(_columns);
75 set_model(_model);
77 pack_start(_columns.label);
79 // Initialize list
80 for(unsigned int i = 0; i < _converter._length; ++i) {
81 Gtk::TreeModel::Row row = *_model->append();
82 const Util::EnumData<E>* data = &_converter.data(i);
83 row[_columns.data] = data;
84 row[_columns.label] = _( _converter.get_label(data->id).c_str() );
85 }
86 set_active(0);
88 // Sort the list
89 if (_sort) {
90 _model->set_default_sort_func(sigc::mem_fun(*this, &ComboBoxEnum<E>::on_sort_compare));
91 _model->set_sort_column(_columns.label, Gtk::SORT_ASCENDING);
92 }
93 }
95 virtual Glib::ustring get_as_attribute() const
96 {
97 return get_active_data()->key;
98 }
100 virtual void set_from_attribute(SPObject* o)
101 {
102 setProgrammatically = true;
103 const gchar* val = attribute_value(o);
104 if(val)
105 set_active_by_id(_converter.get_id_from_key(val));
106 else
107 set_active(get_default()->as_uint());
108 }
110 const Util::EnumData<E>* get_active_data() const
111 {
112 Gtk::TreeModel::iterator i = this->get_active();
113 if(i)
114 return (*i)[_columns.data];
115 return 0;
116 }
118 void add_row(const Glib::ustring& s)
119 {
120 Gtk::TreeModel::Row row = *_model->append();
121 row[_columns.data] = 0;
122 row[_columns.label] = s;
123 }
125 void remove_row(E id) {
126 Gtk::TreeModel::iterator i;
128 for(i = _model->children().begin(); i != _model->children().end(); ++i) {
129 const Util::EnumData<E>* data = (*i)[_columns.data];
131 if(data->id == id)
132 break;
133 }
135 if(i != _model->children().end())
136 _model->erase(i);
137 }
139 void set_active_by_id(E id) {
140 setProgrammatically = true;
141 for(Gtk::TreeModel::iterator i = _model->children().begin();
142 i != _model->children().end(); ++i)
143 {
144 const Util::EnumData<E>* data = (*i)[_columns.data];
145 if(data->id == id) {
146 set_active(i);
147 break;
148 }
149 }
150 };
152 void set_active_by_key(const Glib::ustring& key) {
153 setProgrammatically = true;
154 set_active_by_id( _converter.get_id_from_key(key) );
155 };
157 bool setProgrammatically;
159 private:
160 class Columns : public Gtk::TreeModel::ColumnRecord
161 {
162 public:
163 Columns()
164 {
165 add(data);
166 add(label);
167 }
169 Gtk::TreeModelColumn<const Util::EnumData<E>*> data;
170 Gtk::TreeModelColumn<Glib::ustring> label;
171 };
173 Columns _columns;
174 Glib::RefPtr<Gtk::ListStore> _model;
175 const Util::EnumDataConverter<E>& _converter;
176 };
179 template<typename E> class LabelledComboBoxEnum : public Labelled
180 {
181 public:
182 LabelledComboBoxEnum( Glib::ustring const &label,
183 Glib::ustring const &tooltip,
184 const Util::EnumDataConverter<E>& c,
185 Glib::ustring const &suffix = "",
186 Glib::ustring const &icon = "",
187 bool mnemonic = true)
188 : Labelled(label, tooltip, new ComboBoxEnum<E>(c), suffix, icon, mnemonic)
189 { }
191 ComboBoxEnum<E>* getCombobox() {
192 return static_cast< ComboBoxEnum<E>* > (_widget);
193 }
194 };
196 }
197 }
198 }
200 #endif
202 /*
203 Local Variables:
204 mode:c++
205 c-file-style:"stroustrup"
206 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
207 indent-tabs-mode:nil
208 fill-column:99
209 End:
210 */
211 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :