75cdddf1b9c2c42bc771da40e3504ad3e4bc8a29
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"
21 namespace Inkscape {
22 namespace UI {
23 namespace Widget {
25 template<typename E> class ComboBoxEnum : public Gtk::ComboBox, public AttrWidget
26 {
27 public:
28 ComboBoxEnum(const Util::EnumDataConverter<E>& c, const SPAttributeEnum a = SP_ATTR_INVALID)
29 : AttrWidget(a), _converter(c)
30 {
31 _model = Gtk::ListStore::create(_columns);
32 set_model(_model);
34 pack_start(_columns.label);
36 // Initialize list
37 for(int i = 0; i < _converter.end; ++i) {
38 Gtk::TreeModel::Row row = *_model->append();
39 const Util::EnumData<E>* data = &_converter.data(i);
40 row[_columns.data] = data;
41 row[_columns.label] = _converter.get_label(data->id);
42 }
44 set_active(0);
45 }
47 virtual Glib::ustring get_as_attribute() const
48 {
49 return get_active_data()->key;
50 }
52 virtual void set_from_attribute(SPObject* o)
53 {
54 const gchar* val = attribute_value(o);
55 if(val)
56 set_active(_converter.get_id_from_key(val));
57 else
58 set_active(0);
59 }
61 const Util::EnumData<E>* get_active_data() const
62 {
63 Gtk::TreeModel::iterator i = this->get_active();
64 if(i)
65 return (*i)[_columns.data];
66 return 0;
67 }
69 void add_row(const Glib::ustring& s)
70 {
71 Gtk::TreeModel::Row row = *_model->append();
72 row[_columns.data] = 0;
73 row[_columns.label] = s;
74 }
75 private:
76 class Columns : public Gtk::TreeModel::ColumnRecord
77 {
78 public:
79 Columns()
80 {
81 add(data);
82 add(label);
83 }
85 Gtk::TreeModelColumn<const Util::EnumData<E>*> data;
86 Gtk::TreeModelColumn<Glib::ustring> label;
87 };
89 Columns _columns;
90 Glib::RefPtr<Gtk::ListStore> _model;
91 const Util::EnumDataConverter<E>& _converter;
92 };
94 }
95 }
96 }
98 #endif
100 /*
101 Local Variables:
102 mode:c++
103 c-file-style:"stroustrup"
104 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
105 indent-tabs-mode:nil
106 fill-column:99
107 End:
108 */
109 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :