Code

fix 198818
[inkscape.git] / src / util / enums.h
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  */
21 #ifndef INKSCAPE_UTIL_ENUMS_H
22 #define INKSCAPE_UTIL_ENUMS_H
24 #include <glibmm/ustring.h>
26 namespace Inkscape {
27 namespace Util {
29 template<typename E> class EnumData
30 {
31 public:
32     E id;
33     const Glib::ustring label;
34     const Glib::ustring key;
35 };
37 template<typename E> class EnumDataConverter
38 {
39 public:
40     typedef EnumData<E> Data;
42     EnumDataConverter(const EnumData<E>* cd, const int endval)
43         : end(endval), _data(cd)
44     {}
46     E get_id_from_label(const Glib::ustring& label) const
47     {
48         for(int i = 0; i < end; ++i) {
49             if(_data[i].label == label)
50                 return (E)i;
51         }
53         return (E)0;
54     }
56     E get_id_from_key(const Glib::ustring& key) const
57     {
58         for(int i = 0; i < end; ++i) {
59             if(_data[i].key == key)
60                 return (E)i;
61         }
63         return (E)0;
64     }
66     bool is_valid_key(const Glib::ustring& key) const
67     {
68         for(int i = 0; i < end; ++i) {
69             if(_data[i].key == key)
70                 return true;
71         }
73         return false;
74     }
76     bool is_valid_id(const E e) const
77     {
78         return ( (int)e < end );
79     }
81     const Glib::ustring& get_label(const E e) const
82     {
83         return _data[e].label;
84     }
86     const Glib::ustring& get_key(const E e) const
87     {
88         return _data[e].key;
89     }
91     const EnumData<E>& data(const int i) const
92     {
93         return _data[i];
94     }
96     const int end;
97 private:
98     const EnumData<E>* _data;
99 };
105 #endif
107 /*
108   Local Variables:
109   mode:c++
110   c-file-style:"stroustrup"
111   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
112   indent-tabs-mode:nil
113   fill-column:99
114   End:
115 */
116 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :