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 using namespace Menu_Helpers;
66 /* TODO: lacks support for rotated dropdown buttons */
68 if (!has_screen () || !get_has_window ()) {
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,
79 const Requisition menu_req = _menu.size_request();
80 const Rectangle allocation = get_allocation();
82 /* The x and y position are handled separately.
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...*/
98 get_window ()->get_origin (x, y);
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 */
110 /* d) XXX align left or the menu might change monitors */
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;
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 */
138 const MenuList& items = _menu.items ();
139 const std::string button_text = get_text();
142 MenuList::const_iterator i = items.begin();
143 for ( ; i != items.end(); ++i) {
144 if (button_text == ((std::string) i->get_label())) {
147 offset += i->size_request().height;
149 if (i != items.end() &&
150 y - offset >= monitor.get_y() &&
151 y - offset + menu_req.height <= monitor.get_y() + monitor.get_height()) {
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) */
158 y = monitor.get_y() + max(0, monitor.get_height() - menu_req.height);
165 ArdourDropdown::on_button_press_event (GdkEventButton* ev)
167 if (ev->type == GDK_BUTTON_PRESS) {
168 _menu.popup (sigc::mem_fun(this, &ArdourDropdown::position_menu),
175 ArdourDropdown::on_scroll_event (GdkEventScroll* ev)
177 using namespace Menu_Helpers;
179 if (_scrolling_disabled) {
183 const MenuItem * current_active = _menu.get_active();
184 const MenuList& items = _menu.items ();
187 if (!current_active) {
191 /* work around another gtkmm API clusterfuck
192 * const MenuItem* get_active () const
193 * void set_active (guint index)
195 * also MenuList.activate_item does not actually
196 * set it as active in the menu.
200 switch (ev->direction) {
203 for (MenuList::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i, ++c) {
204 if ( &(*i) != current_active) {
207 if (++i != items.rend()) {
208 c = items.size() - 2 - c;
211 _menu.activate_item(*i);
216 case GDK_SCROLL_DOWN:
217 for (MenuList::const_iterator i = items.begin(); i != items.end(); ++i, ++c) {
218 if ( &(*i) != current_active) {
221 if (++i != items.end()) {
222 assert(c + 1 < (int) items.size());
223 _menu.set_active(c + 1);
224 _menu.activate_item(*i);
236 ArdourDropdown::clear_items ()
238 _menu.items ().clear ();
242 ArdourDropdown::AddMenuElem (Menu_Helpers::Element e)
244 using namespace Menu_Helpers;
246 MenuList& items = _menu.items ();
252 ArdourDropdown::disable_scrolling()
254 _scrolling_disabled = true;