1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Authors: 4 * Nicholas Bishop <nicholasbishop@gmail.com> 5 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl> 6 * 7 * Copyright (C) 2007 Authors 8 * 9 * Released under GNU GPL v2+, read the file 'COPYING' for more information. 10 */ 11 #ifndef INKSCAPE_UTIL_ENUMS_H 12 #define INKSCAPE_UTIL_ENUMS_H 13 14 #include <glibmm/ustring.h> 15 16 namespace Inkscape { 17 namespace Util { 18 19 /** 20 * Simplified management of enumerations of svg items with UI labels. 21 * IMPORTANT: 22 * When initializing the EnumData struct, you cannot use _(...) to translate strings. 23 * Instead, one must use N_(...) and do the translation every time the string is retrieved. 24 */ 25 template<typename E> 26 struct EnumData 27 { 28 E id; 29 const Glib::ustring label; 30 const Glib::ustring key; 31 }; 32 33 const Glib::ustring empty_string(""); 34 35 /** 36 * Simplified management of enumerations of svg items with UI labels. 37 * 38 * @note that get_id_from_key and get_id_from_label return 0 if it cannot find an entry for that key string. 39 * @note that get_label and get_key return an empty string when the requested id is not in the list. 40 */ 41 template<typename E> class EnumDataConverter 42 { 43 public: 44 typedef EnumData<E> Data; 45 EnumDataConverter(const EnumData<E> * cd,const unsigned int length)46 EnumDataConverter(const EnumData<E>* cd, const unsigned int length) 47 : _length(length), _data(cd) 48 {} 49 get_id_from_label(const Glib::ustring & label)50 E get_id_from_label(const Glib::ustring& label) const 51 { 52 for(unsigned int i = 0; i < _length; ++i) { 53 if(_data[i].label == label) 54 return _data[i].id; 55 } 56 57 return (E)0; 58 } 59 get_id_from_key(const Glib::ustring & key)60 E get_id_from_key(const Glib::ustring& key) const 61 { 62 for(unsigned int i = 0; i < _length; ++i) { 63 if(_data[i].key == key) 64 return _data[i].id; 65 } 66 67 return (E)0; 68 } 69 is_valid_key(const Glib::ustring & key)70 bool is_valid_key(const Glib::ustring& key) const 71 { 72 for(unsigned int i = 0; i < _length; ++i) { 73 if(_data[i].key == key) 74 return true; 75 } 76 77 return false; 78 } 79 is_valid_id(const E id)80 bool is_valid_id(const E id) const 81 { 82 for(unsigned int i = 0; i < _length; ++i) { 83 if(_data[i].id == id) 84 return true; 85 } 86 return false; 87 } 88 get_label(const E id)89 const Glib::ustring& get_label(const E id) const 90 { 91 for(unsigned int i = 0; i < _length; ++i) { 92 if(_data[i].id == id) 93 return _data[i].label; 94 } 95 96 return empty_string; 97 } 98 get_key(const E id)99 const Glib::ustring& get_key(const E id) const 100 { 101 for(unsigned int i = 0; i < _length; ++i) { 102 if(_data[i].id == id) 103 return _data[i].key; 104 } 105 106 return empty_string; 107 } 108 data(const unsigned int i)109 const EnumData<E>& data(const unsigned int i) const 110 { 111 return _data[i]; 112 } 113 114 const unsigned int _length; 115 private: 116 const EnumData<E>* _data; 117 }; 118 119 120 } 121 } 122 123 #endif 124 125 /* 126 Local Variables: 127 mode:c++ 128 c-file-style:"stroustrup" 129 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) 130 indent-tabs-mode:nil 131 fill-column:99 132 End: 133 */ 134 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : 135