c991087f259d4a1b3b37bf9addd4565401e0b349
[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         add_elements(e);
56         add_elements(ArdourButton::Menu);
57 }
58
59 ArdourDropdown::~ArdourDropdown ()
60 {
61 }
62
63 void
64 ArdourDropdown::menu_size_request(Requisition *req) {
65         req->width = max(req->width, get_allocation().get_width());
66 }
67
68 void
69 ArdourDropdown::position_menu(int& x, int& y, bool& push_in) {
70         using namespace Menu_Helpers;
71
72          /* TODO: lacks support for rotated dropdown buttons */
73
74         if (!has_screen () || !get_has_window ()) {
75                 return;
76         }
77
78         Rectangle monitor;
79         {
80                 const int monitor_num = get_screen ()->get_monitor_at_window (get_window ());
81                 get_screen ()->get_monitor_geometry ((monitor_num < 0) ? 0 : monitor_num,
82                                                      monitor);
83         }
84
85         const Requisition menu_req = _menu.size_request();
86         const Rectangle allocation = get_allocation();
87
88         /* The x and y position are handled separately.
89          *
90          * For the x position if the direction is LTR (or RTL), then we try in order:
91          *  a) align the left (right) of the menu with the left (right) of the button
92          *     if there's enough room until the right (left) border of the screen;
93          *  b) align the right (left) of the menu with the right (left) of the button
94          *     if there's enough room until the left (right) border of the screen;
95          *  c) align the right (left) border of the menu with the right (left) border
96          *     of the screen if there's enough space;
97          *  d) align the left (right) border of the menu with the left (right) border
98          *     of the screen, with the rightmost (leftmost) part of the menu that
99          *     overflows the screen.
100          *     XXX We always align left regardless of the direction because if x is
101          *     left of the current monitor, the menu popup code after us notices it
102          *     and enforces that the menu stays in the monitor that's at the left...*/
103
104         get_window ()->get_origin (x, y);
105
106         if (get_direction() == TEXT_DIR_RTL) {
107                 if (monitor.get_x() <= x + allocation.get_width() - menu_req.width) {
108                         /* a) align menu right and button right */
109                         x += allocation.get_width() - menu_req.width;
110                 } else if (x + menu_req.width <= monitor.get_x() + monitor.get_width()) {
111                         /* b) align menu left and button left: nothing to do*/
112                 } else if (menu_req.width > monitor.get_width()) {
113                         /* c) align menu left and screen left, guaranteed to fit */
114                         x = monitor.get_x();
115                 } else {
116                         /* d) XXX align left or the menu might change monitors */
117                         x = monitor.get_x();
118                 }
119         } else { /* LTR */
120                 if (x + menu_req.width <= monitor.get_x() + monitor.get_width()) {
121                         /* a) align menu left and button left: nothing to do*/
122                 } else if (monitor.get_x() <= x + allocation.get_width() - menu_req.width) {
123                         /* b) align menu right and button right */
124                         x += allocation.get_width() - menu_req.width;
125                 } else if (menu_req.width > monitor.get_width()) {
126                         /* c) align menu right and screen right, guaranteed to fit */
127                         x = monitor.get_x() + monitor.get_width() - menu_req.width;
128                 } else {
129                         /* d) align left */
130                         x = monitor.get_x();
131                 }
132         }
133
134         /* For the y position, try in order:
135          *  a) if there is a menu item with the same text as the button, align it
136          *     with the button, unless that makes the menu overflow the monitor.
137          *  b) align the top of the menu with the bottom of the button if there is
138          *     enough room below the button;
139          *  c) align the bottom of the menu with the top of the button if there is
140          *     enough room above the button;
141          *  d) align the bottom of the menu with the bottom of the monitor if there
142          *     is enough room, but avoid moving the menu to another monitor */
143
144         const MenuList& items = _menu.items ();
145         const std::string button_text = get_text();
146         int offset = 0;
147
148         MenuList::const_iterator i = items.begin();
149         for ( ; i != items.end(); ++i) {
150                 if (button_text == ((std::string) i->get_label())) {
151                         break;
152                 }
153                 offset += i->size_request().height;
154         }
155         if (i != items.end() &&
156             y - offset >= monitor.get_y() &&
157             y - offset + menu_req.height <= monitor.get_y() + monitor.get_height()) {
158                 y -= offset;
159         } else if (y + allocation.get_height() + menu_req.height <= monitor.get_y() + monitor.get_height()) {
160                 y += allocation.get_height(); /* a) */
161         } else if ((y - menu_req.height) >= monitor.get_y()) {
162                 y -= menu_req.height; /* b) */
163         } else {
164                 y = monitor.get_y() + max(0, monitor.get_height() - menu_req.height);
165         }
166
167         push_in = false;
168 }
169
170 bool
171 ArdourDropdown::on_button_press_event (GdkEventButton* ev)
172 {
173         if (ev->type == GDK_BUTTON_PRESS) {
174                 _menu.popup (sigc::mem_fun(this, &ArdourDropdown::position_menu),
175                              1, ev->time);
176         }
177         return true;
178 }
179
180 bool
181 ArdourDropdown::on_scroll_event (GdkEventScroll* ev)
182 {
183         using namespace Menu_Helpers;
184
185         if (_scrolling_disabled) {
186                 return false;
187         }
188
189         const MenuItem * current_active = _menu.get_active();
190         const MenuList& items = _menu.items ();
191         int c = 0;
192
193         if (!current_active) {
194                 return true;
195         }
196
197         /* work around another gtkmm API clusterfuck
198          * const MenuItem* get_active () const
199          * void set_active (guint index)
200          *
201          * also MenuList.activate_item does not actually
202          * set it as active in the menu.
203          *
204          */
205
206         switch (ev->direction) {
207                 case GDK_SCROLL_UP:
208
209                         for (MenuList::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i, ++c) {
210                                 if ( &(*i) != current_active) {
211                                         continue;
212                                 }
213                                 if (++i != items.rend()) {
214                                         c = items.size() - 2 - c;
215                                         assert(c >= 0);
216                                         _menu.set_active(c);
217                                         _menu.activate_item(*i);
218                                 }
219                                 break;
220                         }
221                         break;
222                 case GDK_SCROLL_DOWN:
223                         for (MenuList::const_iterator i = items.begin(); i != items.end(); ++i, ++c) {
224                                 if ( &(*i) != current_active) {
225                                         continue;
226                                 }
227                                 if (++i != items.end()) {
228                                         assert(c + 1 < (int) items.size());
229                                         _menu.set_active(c + 1);
230                                         _menu.activate_item(*i);
231                                 }
232                                 break;
233                         }
234                         break;
235                 default:
236                         break;
237         }
238         return true;
239 }
240
241 void
242 ArdourDropdown::clear_items ()
243 {
244         _menu.items ().clear ();
245 }
246
247 void
248 ArdourDropdown::AddMenuElem (Menu_Helpers::Element e)
249 {
250         using namespace Menu_Helpers;
251
252         MenuList& items = _menu.items ();
253
254         items.push_back (e);
255 }
256
257 void
258 ArdourDropdown::disable_scrolling()
259 {
260         _scrolling_disabled = true;
261 }