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 */
19 #ifndef INKSCAPE_UTIL_ENUMS_H
20 #define INKSCAPE_UTIL_ENUMS_H
22 #include <glibmm/ustring.h>
24 namespace Inkscape {
25 namespace Util {
27 template<typename E> class EnumData
28 {
29 public:
30 E id;
31 const Glib::ustring label;
32 const Glib::ustring key;
33 };
35 template<typename E> class EnumDataConverter
36 {
37 public:
38 typedef EnumData<E> Data;
40 EnumDataConverter(const EnumData<E>* cd, const int endval)
41 : end(endval), _data(cd)
42 {}
44 E get_id_from_label(const Glib::ustring& label) const
45 {
46 for(int i = 0; i < end; ++i) {
47 if(_data[i].label == label)
48 return (E)i;
49 }
51 return (E)0;
52 }
54 E get_id_from_key(const Glib::ustring& key) const
55 {
56 for(int i = 0; i < end; ++i) {
57 if(_data[i].key == key)
58 return (E)i;
59 }
61 return (E)0;
62 }
64 bool is_valid_key(const Glib::ustring& key) const
65 {
66 for(int i = 0; i < end; ++i) {
67 if(_data[i].key == key)
68 return true;
69 }
71 return false;
72 }
74 const Glib::ustring& get_label(const E e) const
75 {
76 return _data[e].label;
77 }
79 const Glib::ustring& get_key(const E e) const
80 {
81 return _data[e].key;
82 }
84 const EnumData<E>& data(const int i) const
85 {
86 return _data[i];
87 }
89 const int end;
90 private:
91 const EnumData<E>* _data;
92 };
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 :