2 Copyright (C) 2014 Paul Davis
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.
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.
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.
24 #include <pangomm/layout.h>
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/stacktrace.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
34 #include "ardour/rc_configuration.h" // for widget prelight preference
36 #include "ardour_dropdown.h"
40 #define REFLECTION_HEIGHT 2
49 ArdourDropdown::ArdourDropdown (Element e)
50 : _scrolling_disabled(false)
52 // signal_button_press_event().connect (sigc::mem_fun(*this, &ArdourDropdown::on_mouse_pressed));
55 add_elements(ArdourButton::Menu);
58 ArdourDropdown::~ArdourDropdown ()
63 ArdourDropdown::position_menu(int& x, int& y, bool& push_in) {
64 /* TODO: lacks support for rotated dropdown buttons */
66 if (!has_screen () || !get_has_window ()) {
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,
77 const Requisition menu_req = _menu.size_request();
78 const Rectangle allocation = get_allocation();
80 /* The x and y position are handled separately.
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...*/
96 get_window ()->get_origin (x, y);
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 */
108 /* d) XXX align left or the menu might change monitors */
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;
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 */
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) */
139 y = monitor.get_y() + max(0, monitor.get_height() - menu_req.height);
146 ArdourDropdown::on_button_press_event (GdkEventButton* ev)
148 if (ev->type == GDK_BUTTON_PRESS) {
149 _menu.popup (sigc::mem_fun(this, &ArdourDropdown::position_menu),
156 ArdourDropdown::on_scroll_event (GdkEventScroll* ev)
158 using namespace Menu_Helpers;
160 if (_scrolling_disabled) {
164 const MenuItem * current_active = _menu.get_active();
165 const MenuList& items = _menu.items ();
168 if (!current_active) {
172 /* work around another gtkmm API clusterfuck
173 * const MenuItem* get_active () const
174 * void set_active (guint index)
176 * also MenuList.activate_item does not actually
177 * set it as active in the menu.
181 switch (ev->direction) {
184 for (MenuList::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i, ++c) {
185 if ( &(*i) != current_active) {
188 if (++i != items.rend()) {
189 c = items.size() - 2 - c;
192 _menu.activate_item(*i);
197 case GDK_SCROLL_DOWN:
198 for (MenuList::const_iterator i = items.begin(); i != items.end(); ++i, ++c) {
199 if ( &(*i) != current_active) {
202 if (++i != items.end()) {
203 assert(c + 1 < (int) items.size());
204 _menu.set_active(c + 1);
205 _menu.activate_item(*i);
217 ArdourDropdown::clear_items ()
219 _menu.items ().clear ();
223 ArdourDropdown::AddMenuElem (Menu_Helpers::Element e)
225 using namespace Menu_Helpers;
227 MenuList& items = _menu.items ();
233 ArdourDropdown::disable_scrolling()
235 _scrolling_disabled = true;