add explanatory comment
[ardour.git] / gtk2_ardour / ardour_dropdown.cc
1 /*
2     Copyright (C) 2014 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
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/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
33
34 #include "ardour/rc_configuration.h" // for widget prelight preference
35
36 #include "ardour_dropdown.h"
37
38 #include "pbd/i18n.h"
39
40 #define REFLECTION_HEIGHT 2
41
42 using namespace Gdk;
43 using namespace Gtk;
44 using namespace Glib;
45 using namespace PBD;
46 using namespace std;
47
48
49 ArdourDropdown::ArdourDropdown (Element e)
50         : _scrolling_disabled(false)
51 {
52 //      signal_button_press_event().connect (sigc::mem_fun(*this, &ArdourDropdown::on_mouse_pressed));
53         _menu.signal_size_request().connect (sigc::mem_fun(*this, &ArdourDropdown::menu_size_request));
54
55         _menu.set_reserve_toggle_size(false);
56
57         add_elements(e);
58         add_elements(ArdourButton::Menu);
59 }
60
61 ArdourDropdown::~ArdourDropdown ()
62 {
63 }
64
65 void
66 ArdourDropdown::menu_size_request(Requisition *req) {
67         req->width = max(req->width, get_allocation().get_width());
68 }
69
70 void
71 ArdourDropdown::position_menu(int& x, int& y, bool& push_in) {
72         using namespace Menu_Helpers;
73
74          /* TODO: lacks support for rotated dropdown buttons */
75
76         if (!has_screen () || !get_has_window ()) {
77                 return;
78         }
79
80         Rectangle monitor;
81         {
82                 const int monitor_num = get_screen ()->get_monitor_at_window (get_window ());
83                 get_screen ()->get_monitor_geometry ((monitor_num < 0) ? 0 : monitor_num,
84                                                      monitor);
85         }
86
87         const Requisition menu_req = _menu.size_request();
88         const Rectangle allocation = get_allocation();
89
90         /* The x and y position are handled separately.
91          *
92          * For the x position if the direction is LTR (or RTL), then we try in order:
93          *  a) align the left (right) of the menu with the left (right) of the button
94          *     if there's enough room until the right (left) border of the screen;
95          *  b) align the right (left) of the menu with the right (left) of the button
96          *     if there's enough room until the left (right) border of the screen;
97          *  c) align the right (left) border of the menu with the right (left) border
98          *     of the screen if there's enough space;
99          *  d) align the left (right) border of the menu with the left (right) border
100          *     of the screen, with the rightmost (leftmost) part of the menu that
101          *     overflows the screen.
102          *     XXX We always align left regardless of the direction because if x is
103          *     left of the current monitor, the menu popup code after us notices it
104          *     and enforces that the menu stays in the monitor that's at the left...*/
105
106         get_window ()->get_origin (x, y);
107
108         if (get_direction() == TEXT_DIR_RTL) {
109                 if (monitor.get_x() <= x + allocation.get_width() - menu_req.width) {
110                         /* a) align menu right and button right */
111                         x += allocation.get_width() - menu_req.width;
112                 } else if (x + menu_req.width <= monitor.get_x() + monitor.get_width()) {
113                         /* b) align menu left and button left: nothing to do*/
114                 } else if (menu_req.width > monitor.get_width()) {
115                         /* c) align menu left and screen left, guaranteed to fit */
116                         x = monitor.get_x();
117                 } else {
118                         /* d) XXX align left or the menu might change monitors */
119                         x = monitor.get_x();
120                 }
121         } else { /* LTR */
122                 if (x + menu_req.width <= monitor.get_x() + monitor.get_width()) {
123                         /* a) align menu left and button left: nothing to do*/
124                 } else if (monitor.get_x() <= x + allocation.get_width() - menu_req.width) {
125                         /* b) align menu right and button right */
126                         x += allocation.get_width() - menu_req.width;
127                 } else if (menu_req.width > monitor.get_width()) {
128                         /* c) align menu right and screen right, guaranteed to fit */
129                         x = monitor.get_x() + monitor.get_width() - menu_req.width;
130                 } else {
131                         /* d) align left */
132                         x = monitor.get_x();
133                 }
134         }
135
136         /* For the y position, try in order:
137          *  a) if there is a menu item with the same text as the button, align it
138          *     with the button, unless that makes the menu overflow the monitor.
139          *  b) align the top of the menu with the bottom of the button if there is
140          *     enough room below the button;
141          *  c) align the bottom of the menu with the top of the button if there is
142          *     enough room above the button;
143          *  d) align the bottom of the menu with the bottom of the monitor if there
144          *     is enough room, but avoid moving the menu to another monitor */
145
146         const MenuList& items = _menu.items ();
147         const std::string button_text = get_text();
148         int offset = 0;
149
150         MenuList::const_iterator i = items.begin();
151         for ( ; i != items.end(); ++i) {
152                 if (button_text == ((std::string) i->get_label())) {
153                         break;
154                 }
155                 offset += i->size_request().height;
156         }
157         if (i != items.end() &&
158             y - offset >= monitor.get_y() &&
159             y - offset + menu_req.height <= monitor.get_y() + monitor.get_height()) {
160                 y -= offset;
161         } else if (y + allocation.get_height() + menu_req.height <= monitor.get_y() + monitor.get_height()) {
162                 y += allocation.get_height(); /* a) */
163         } else if ((y - menu_req.height) >= monitor.get_y()) {
164                 y -= menu_req.height; /* b) */
165         } else {
166                 y = monitor.get_y() + max(0, monitor.get_height() - menu_req.height);
167         }
168
169         push_in = false;
170 }
171
172 bool
173 ArdourDropdown::on_button_press_event (GdkEventButton* ev)
174 {
175         if (ev->type == GDK_BUTTON_PRESS) {
176                 _menu.popup (sigc::mem_fun(this, &ArdourDropdown::position_menu),
177                              1, ev->time);
178         }
179         return true;
180 }
181
182 bool
183 ArdourDropdown::on_scroll_event (GdkEventScroll* ev)
184 {
185         using namespace Menu_Helpers;
186
187         if (_scrolling_disabled) {
188                 return false;
189         }
190
191         const MenuItem * current_active = _menu.get_active();
192         const MenuList& items = _menu.items ();
193         int c = 0;
194
195         if (!current_active) {
196                 return true;
197         }
198
199         /* work around another gtkmm API clusterfuck
200          * const MenuItem* get_active () const
201          * void set_active (guint index)
202          *
203          * also MenuList.activate_item does not actually
204          * set it as active in the menu.
205          *
206          */
207
208         switch (ev->direction) {
209                 case GDK_SCROLL_UP:
210
211                         for (MenuList::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i, ++c) {
212                                 if ( &(*i) != current_active) {
213                                         continue;
214                                 }
215                                 if (++i != items.rend()) {
216                                         c = items.size() - 2 - c;
217                                         assert(c >= 0);
218                                         _menu.set_active(c);
219                                         _menu.activate_item(*i);
220                                 }
221                                 break;
222                         }
223                         break;
224                 case GDK_SCROLL_DOWN:
225                         for (MenuList::const_iterator i = items.begin(); i != items.end(); ++i, ++c) {
226                                 if ( &(*i) != current_active) {
227                                         continue;
228                                 }
229                                 if (++i != items.end()) {
230                                         assert(c + 1 < (int) items.size());
231                                         _menu.set_active(c + 1);
232                                         _menu.activate_item(*i);
233                                 }
234                                 break;
235                         }
236                         break;
237                 default:
238                         break;
239         }
240         return true;
241 }
242
243 void
244 ArdourDropdown::clear_items ()
245 {
246         _menu.items ().clear ();
247 }
248
249 void
250 ArdourDropdown::AddMenuElem (Menu_Helpers::Element e)
251 {
252         using namespace Menu_Helpers;
253
254         MenuList& items = _menu.items ();
255
256         items.push_back (e);
257 }
258
259 void
260 ArdourDropdown::disable_scrolling()
261 {
262         _scrolling_disabled = true;
263 }