1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #ifndef SPECTMORPH_ENUM_VIEW_HH
4 #define SPECTMORPH_ENUM_VIEW_HH
5 
6 namespace SpectMorph
7 {
8 
9 class EnumView : public SignalReceiver
10 {
11   struct Entry
12   {
13     int i;
14     std::string s;
15   };
16   std::vector<Entry> entries;
17 public:
18   void
add_item(int i,const std::string & s)19   add_item (int i, const std::string& s)
20   {
21     entries.push_back (Entry { i, s });
22   }
23   ComboBox *
create_combobox(Widget * parent,int initial_value,std::function<void (int)> setter)24   create_combobox (Widget *parent, int initial_value, std::function<void(int)> setter)
25   {
26     ComboBox *combobox = new ComboBox (parent);
27     for (auto entry : entries)
28       {
29         combobox->add_item (entry.s);
30         if (initial_value == entry.i)
31           combobox->set_text (entry.s);
32       }
33     connect (combobox->signal_item_changed, [=]()
34       {
35         std::string text = combobox->text();
36         for (auto entry : entries)
37           if (entry.s == text)
38             setter (entry.i);
39       });
40 
41     return combobox;
42   }
43 };
44 
45 }
46 
47 #endif
48