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 public:
29 ComboBoxEnum(const Util::EnumDataConverter<E>& c, const SPAttributeEnum a = SP_ATTR_INVALID)
30 : AttrWidget(a), _converter(c)
31 {
32 signal_changed().connect(signal_attr_changed().make_slot());
34 _model = Gtk::ListStore::create(_columns);
35 set_model(_model);
37 pack_start(_columns.label);
39 // Initialize list
40 for(int i = 0; i < _converter.end; ++i) {
41 Gtk::TreeModel::Row row = *_model->append();
42 const Util::EnumData<E>* data = &_converter.data(i);
43 row[_columns.data] = data;
44 row[_columns.label] = _converter.get_label(data->id);
45 }
47 set_active(0);
48 }
50 virtual Glib::ustring get_as_attribute() const
51 {
52 return get_active_data()->key;
53 }
55 virtual void set_from_attribute(SPObject* o)
56 {
57 const gchar* val = attribute_value(o);
58 if(val)
59 set_active(_converter.get_id_from_key(val));
60 else
61 set_active(0);
62 }
64 const Util::EnumData<E>* get_active_data() const
65 {
66 Gtk::TreeModel::iterator i = this->get_active();
67 if(i)
68 return (*i)[_columns.data];
69 return 0;
70 }
72 void add_row(const Glib::ustring& s)
73 {
74 Gtk::TreeModel::Row row = *_model->append();
75 row[_columns.data] = 0;
76 row[_columns.label] = s;
77 }
79 void set_active_by_id(E id) {
80 for(Gtk::TreeModel::iterator i = _model->children().begin();
81 i != _model->children().end(); ++i)
82 {
83 const Util::EnumData<E>* data = (*i)[_columns.data];
84 if(data->id == id) {
85 set_active(i);
86 break;
87 }
88 }
89 };
91 void set_active_by_key(const Glib::ustring& key) {
92 set_active_by_id( _converter.get_id_from_key(key) );
93 };
95 private:
96 class Columns : public Gtk::TreeModel::ColumnRecord
97 {
98 public:
99 Columns()
100 {
101 add(data);
102 add(label);
103 }
105 Gtk::TreeModelColumn<const Util::EnumData<E>*> data;
106 Gtk::TreeModelColumn<Glib::ustring> label;
107 };
109 Columns _columns;
110 Glib::RefPtr<Gtk::ListStore> _model;
111 const Util::EnumDataConverter<E>& _converter;
112 };
115 template<typename E> class LabelledComboBoxEnum : public Labelled
116 {
117 public:
118 LabelledComboBoxEnum( Glib::ustring const &label,
119 Glib::ustring const &tooltip,
120 const Util::EnumDataConverter<E>& c,
121 Glib::ustring const &suffix = "",
122 Glib::ustring const &icon = "",
123 bool mnemonic = true)
124 : Labelled(label, tooltip, new ComboBoxEnum<E>(c), suffix, icon, mnemonic)
125 { }
127 ComboBoxEnum<E>* getCombobox() {
128 return static_cast< ComboBoxEnum<E>* > (_widget);
129 }
130 };
132 }
133 }
134 }
136 #endif
138 /*
139 Local Variables:
140 mode:c++
141 c-file-style:"stroustrup"
142 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
143 indent-tabs-mode:nil
144 fill-column:99
145 End:
146 */
147 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :