From 36ae58c369195c6e3c74540c4d143862cc2fe126 Mon Sep 17 00:00:00 2001 From: johanengelen Date: Wed, 16 Jul 2008 21:38:16 +0000 Subject: [PATCH] fix combo enum, to handle enums of all types (not only the ones that range from 0..10) --- src/ui/dialog/filter-effects-dialog.cpp | 12 +++--- src/ui/widget/combo-enums.h | 6 +-- src/util/enums.h | 51 ++++++++++++++++--------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/ui/dialog/filter-effects-dialog.cpp b/src/ui/dialog/filter-effects-dialog.cpp index 1ded9e255..2cec40860 100644 --- a/src/ui/dialog/filter-effects-dialog.cpp +++ b/src/ui/dialog/filter-effects-dialog.cpp @@ -1467,7 +1467,7 @@ int FilterEffectsDialog::PrimitiveList::init_text() _vertical_layout = Pango::Layout::create(context); int maxfont = 0; - for(int i = 0; i < FPInputConverter.end; ++i) { + for(unsigned int i = 0; i < FPInputConverter._length; ++i) { _vertical_layout->set_text(_(FPInputConverter.get_label((FilterPrimitiveInput)i).c_str())); int fontw, fonth; _vertical_layout->get_pixel_size(fontw, fonth); @@ -1591,8 +1591,8 @@ bool FilterEffectsDialog::PrimitiveList::on_expose_signal(GdkEventExpose* e) int vis_x, vis_y; tree_to_widget_coords(vis.get_x(), vis.get_y(), vis_x, vis_y); - text_start_x = rct.get_x() + rct.get_width() - _connection_cell.get_text_width() * (FPInputConverter.end + 1) + 1; - for(int i = 0; i < FPInputConverter.end; ++i) { + text_start_x = rct.get_x() + rct.get_width() - _connection_cell.get_text_width() * (FPInputConverter._length + 1) + 1; + for(unsigned int i = 0; i < FPInputConverter._length; ++i) { _vertical_layout->set_text(_(FPInputConverter.get_label((FilterPrimitiveInput)i).c_str())); const int x = text_start_x + _connection_cell.get_text_width() * (i + 1); get_bin_window()->draw_rectangle(get_style()->get_bg_gc(Gtk::STATE_NORMAL), true, x, vis_y, _connection_cell.get_text_width(), vis.get_height()); @@ -1898,13 +1898,13 @@ bool FilterEffectsDialog::PrimitiveList::on_button_release_event(GdkEventButton* Gdk::Rectangle rct; get_cell_area(path, *col, rct); const int twidth = _connection_cell.get_text_width(); - const int sources_x = rct.get_width() - twidth * FPInputConverter.end; + const int sources_x = rct.get_width() - twidth * FPInputConverter._length; if(cx > sources_x) { int src = (cx - sources_x) / twidth; if(src < 0) src = 0; - else if(src >= FPInputConverter.end) - src = FPInputConverter.end - 1; + else if(src >= FPInputConverter._length) + src = FPInputConverter._length - 1; result = FPInputConverter.get_key((FilterPrimitiveInput)src); in_val = result.c_str(); } diff --git a/src/ui/widget/combo-enums.h b/src/ui/widget/combo-enums.h index cab93f557..11640f3ca 100644 --- a/src/ui/widget/combo-enums.h +++ b/src/ui/widget/combo-enums.h @@ -37,7 +37,7 @@ public: pack_start(_columns.label); // Initialize list - for(int i = 0; i < _converter.end; ++i) { + for(int i = 0; i < _converter._length; ++i) { Gtk::TreeModel::Row row = *_model->append(); const Util::EnumData* data = &_converter.data(i); row[_columns.data] = data; @@ -57,7 +57,7 @@ public: pack_start(_columns.label); // Initialize list - for(int i = 0; i < _converter.end; ++i) { + for(unsigned int i = 0; i < _converter._length; ++i) { Gtk::TreeModel::Row row = *_model->append(); const Util::EnumData* data = &_converter.data(i); row[_columns.data] = data; @@ -76,7 +76,7 @@ public: setProgrammatically = true; const gchar* val = attribute_value(o); if(val) - set_active(_converter.get_id_from_key(val)); + set_active_by_id(_converter.get_id_from_key(val)); else set_active(get_default()->as_uint()); } diff --git a/src/util/enums.h b/src/util/enums.h index 69514b202..cbb1cb9a1 100644 --- a/src/util/enums.h +++ b/src/util/enums.h @@ -15,6 +15,7 @@ * Instead, one must use N_(...) and do the translation every time the string is retreived. * * Note that get_id_from_key and get_id_from_label return 0 if it cannot find an entry for that key string + * Note that get_label and get_key return an empty string when the requested id is not in the list. */ @@ -26,28 +27,30 @@ namespace Inkscape { namespace Util { -template class EnumData +template +struct EnumData { -public: E id; const Glib::ustring label; const Glib::ustring key; }; +const Glib::ustring empty_string(""); + template class EnumDataConverter { public: typedef EnumData Data; - EnumDataConverter(const EnumData* cd, const int endval) - : end(endval), _data(cd) + EnumDataConverter(const EnumData* cd, const unsigned int length) + : _length(length), _data(cd) {} E get_id_from_label(const Glib::ustring& label) const { - for(int i = 0; i < end; ++i) { + for(unsigned int i = 0; i < _length; ++i) { if(_data[i].label == label) - return (E)i; + return _data[i].id; } return (E)0; @@ -55,9 +58,9 @@ public: E get_id_from_key(const Glib::ustring& key) const { - for(int i = 0; i < end; ++i) { + for(unsigned int i = 0; i < _length; ++i) { if(_data[i].key == key) - return (E)i; + return _data[i].id; } return (E)0; @@ -65,7 +68,7 @@ public: bool is_valid_key(const Glib::ustring& key) const { - for(int i = 0; i < end; ++i) { + for(unsigned int i = 0; i < _length; ++i) { if(_data[i].key == key) return true; } @@ -73,27 +76,41 @@ public: return false; } - bool is_valid_id(const E e) const + bool is_valid_id(const E id) const { - return ( (int)e < end ); + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].id == id) + return true; + } + return false; } - const Glib::ustring& get_label(const E e) const + const Glib::ustring& get_label(const E id) const { - return _data[e].label; + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].id == id) + return _data[i].label; + } + + return empty_string; } - const Glib::ustring& get_key(const E e) const + const Glib::ustring& get_key(const E id) const { - return _data[e].key; + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].id == id) + return _data[i].key; + } + + return empty_string; } - const EnumData& data(const int i) const + const EnumData& data(const unsigned int i) const { return _data[i]; } - const int end; + const unsigned int _length; private: const EnumData* _data; }; -- 2.30.2