573b8084147b77b469880933380ef815d1ab90fe
[ardour.git] / ardour_dropdown.cc
1 /*
2  * Copyright (C) 2014 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <iostream>
21 #include <cmath>
22 #include <algorithm>
23
24 #include <pangomm/layout.h>
25
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/stacktrace.h"
29
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/menu_elems.h"
32 #include "gtkmm2ext/rgb_macros.h"
33 #include "gtkmm2ext/gui_thread.h"
34
35 #include "widgets/ardour_dropdown.h"
36
37 #include "pbd/i18n.h"
38
39 #define REFLECTION_HEIGHT 2
40
41 using namespace Gdk;
42 using namespace Gtk;
43 using namespace Glib;
44 using namespace PBD;
45 using namespace ArdourWidgets;
46 using namespace std;
47
48
49 ArdourDropdown::ArdourDropdown (Element e)
50         : _scrolling_disabled(false)
51 {
52         _menu.signal_size_request().connect (sigc::mem_fun(*this, &ArdourDropdown::menu_size_request));
53
54         _menu.set_reserve_toggle_size(false);
55
56         add_elements(e);
57         add_elements(ArdourButton::Menu);
58 }
59
60 ArdourDropdown::~ArdourDropdown ()
61 {
62 }
63
64 void
65 ArdourDropdown::menu_size_request(Requisition *req) {
66         req->width = max(req->width, get_allocation().get_width());
67 }
68
69 bool
70 ArdourDropdown::on_button_press_event (GdkEventButton* ev)
71 {
72         if (binding_proxy.button_press_handler (ev)) {
73                 return true;
74         }
75
76         if (ev->type == GDK_BUTTON_PRESS && ev->button == 1) {
77                 Gtkmm2ext::anchored_menu_popup(&_menu, this, get_text(), 1, ev->time);
78         }
79
80         return true;
81 }
82
83 void
84 ArdourDropdown::set_active (std::string const& text)
85 {
86         const MenuItem* current_active = _menu.get_active();
87         if (current_active && current_active->get_label() == text) {
88                 set_text (text);
89                 return;
90         }
91         using namespace Menu_Helpers;
92         const MenuList& items = _menu.items ();
93         int c = 0;
94         for (MenuList::const_iterator i = items.begin(); i != items.end(); ++i, ++c) {
95                 if (i->get_label() == text) {
96                         _menu.set_active(c);
97                         _menu.activate_item(*i);
98                         break;
99                 }
100         }
101         set_text (text);
102 }
103
104 bool
105 ArdourDropdown::on_scroll_event (GdkEventScroll* ev)
106 {
107         using namespace Menu_Helpers;
108
109         if (_scrolling_disabled) {
110                 return false;
111         }
112
113         const MenuItem * current_active = _menu.get_active();
114         const MenuList& items = _menu.items ();
115         int c = 0;
116
117         if (!current_active) {
118                 return true;
119         }
120
121         /* work around another gtkmm API clusterfuck
122          * const MenuItem* get_active () const
123          * void set_active (guint index)
124          *
125          * also MenuList.activate_item does not actually
126          * set it as active in the menu.
127          *
128          */
129
130         switch (ev->direction) {
131                 case GDK_SCROLL_UP:
132
133                         for (MenuList::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i, ++c) {
134                                 if ( &(*i) != current_active) {
135                                         continue;
136                                 }
137                                 if (++i != items.rend()) {
138                                         c = items.size() - 2 - c;
139                                         assert(c >= 0);
140                                         _menu.set_active(c);
141                                         _menu.activate_item(*i);
142                                 }
143                                 break;
144                         }
145                         break;
146                 case GDK_SCROLL_DOWN:
147                         for (MenuList::const_iterator i = items.begin(); i != items.end(); ++i, ++c) {
148                                 if ( &(*i) != current_active) {
149                                         continue;
150                                 }
151                                 if (++i != items.end()) {
152                                         assert(c + 1 < (int) items.size());
153                                         _menu.set_active(c + 1);
154                                         _menu.activate_item(*i);
155                                 }
156                                 break;
157                         }
158                         break;
159                 default:
160                         break;
161         }
162         return true;
163 }
164
165 void
166 ArdourDropdown::clear_items ()
167 {
168         _menu.items ().clear ();
169 }
170
171 void
172 ArdourDropdown::AddMenuElem (Menu_Helpers::Element e)
173 {
174         using namespace Menu_Helpers;
175
176         MenuList& items = _menu.items ();
177
178         items.push_back (e);
179 }
180
181 void
182 ArdourDropdown::disable_scrolling()
183 {
184         _scrolling_disabled = true;
185 }
186
187 void
188 ArdourDropdown::append_text_item (std::string const& text) {
189         using namespace Gtkmm2ext;
190         AddMenuElem (MenuElemNoMnemonic (text, sigc::bind (sigc::mem_fun (*this, &ArdourDropdown::default_text_handler), text)));
191 }
192
193 void
194 ArdourDropdown::default_text_handler (std::string const& text) {
195         set_text (text);
196         StateChanged (); /* EMIT SIGNAL */
197 }