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