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