1 /**
2 * \brief Simplified management of enumerations of svg items with UI labels
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 /* IMPORTANT
14 * When initializing the EnumData struct, you cannot use _(...) to translate strings.
15 * Instead, one must use N_(...) and do the translation every time the string is retreived.
16 *
17 * Note that get_id_from_key and get_id_from_label return 0 if it cannot find an entry for that key string
18 * Note that get_label and get_key return an empty string when the requested id is not in the list.
19 */
22 #ifndef INKSCAPE_UTIL_ENUMS_H
23 #define INKSCAPE_UTIL_ENUMS_H
25 #include <glibmm/ustring.h>
27 namespace Inkscape {
28 namespace Util {
30 template<typename E>
31 struct EnumData
32 {
33 E id;
34 const Glib::ustring label;
35 const Glib::ustring key;
36 };
38 const Glib::ustring empty_string("");
40 template<typename E> class EnumDataConverter
41 {
42 public:
43 typedef EnumData<E> Data;
45 EnumDataConverter(const EnumData<E>* cd, const unsigned int length)
46 : _length(length), _data(cd)
47 {}
49 E get_id_from_label(const Glib::ustring& label) const
50 {
51 for(unsigned int i = 0; i < _length; ++i) {
52 if(_data[i].label == label)
53 return _data[i].id;
54 }
56 return (E)0;
57 }
59 E get_id_from_key(const Glib::ustring& key) const
60 {
61 for(unsigned int i = 0; i < _length; ++i) {
62 if(_data[i].key == key)
63 return _data[i].id;
64 }
66 return (E)0;
67 }
69 bool is_valid_key(const Glib::ustring& key) const
70 {
71 for(unsigned int i = 0; i < _length; ++i) {
72 if(_data[i].key == key)
73 return true;
74 }
76 return false;
77 }
79 bool is_valid_id(const E id) const
80 {
81 for(unsigned int i = 0; i < _length; ++i) {
82 if(_data[i].id == id)
83 return true;
84 }
85 return false;
86 }
88 const Glib::ustring& get_label(const E id) const
89 {
90 for(unsigned int i = 0; i < _length; ++i) {
91 if(_data[i].id == id)
92 return _data[i].label;
93 }
95 return empty_string;
96 }
98 const Glib::ustring& get_key(const E id) const
99 {
100 for(unsigned int i = 0; i < _length; ++i) {
101 if(_data[i].id == id)
102 return _data[i].key;
103 }
105 return empty_string;
106 }
108 const EnumData<E>& data(const unsigned int i) const
109 {
110 return _data[i];
111 }
113 const unsigned int _length;
114 private:
115 const EnumData<E>* _data;
116 };
119 }
120 }
122 #endif
124 /*
125 Local Variables:
126 mode:c++
127 c-file-style:"stroustrup"
128 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
129 indent-tabs-mode:nil
130 fill-column:99
131 End:
132 */
133 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :