changes required to operate with the Evoral::Beats ticktime commit
[ardour.git] / libs / widgets / 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 "widgets/ardour_dropdown.h"
35
36 #include "pbd/i18n.h"
37
38 #define REFLECTION_HEIGHT 2
39
40 using namespace Gdk;
41 using namespace Gtk;
42 using namespace Glib;
43 using namespace PBD;
44 using namespace ArdourWidgets;
45 using namespace std;
46
47
48 ArdourDropdown::ArdourDropdown (Element e)
49         : _scrolling_disabled(false)
50 {
51 //      signal_button_press_event().connect (sigc::mem_fun(*this, &ArdourDropdown::on_mouse_pressed));
52         _menu.signal_size_request().connect (sigc::mem_fun(*this, &ArdourDropdown::menu_size_request));
53
54         _menu.set_reserve_toggle_size(false);
55
56         add_elements(e);
57         add_elements(ArdourButton::Menu);
58 }
59
60 ArdourDropdown::~ArdourDropdown ()
61 {
62 }
63
64 void
65 ArdourDropdown::menu_size_request(Requisition *req) {
66         req->width = max(req->width, get_allocation().get_width());
67 }
68
69 bool
70 ArdourDropdown::on_button_press_event (GdkEventButton* ev)
71 {
72         if (binding_proxy.button_press_handler (ev)) {
73                 return true;
74         }
75
76         if (ev->type == GDK_BUTTON_PRESS && ev->button == 1) {
77                 Gtkmm2ext::anchored_menu_popup(&_menu, this, get_text(), 1, ev->time);
78         }
79
80         return true;
81 }
82
83 bool
84 ArdourDropdown::on_scroll_event (GdkEventScroll* ev)
85 {
86         using namespace Menu_Helpers;
87
88         if (_scrolling_disabled) {
89                 return false;
90         }
91
92         const MenuItem * current_active = _menu.get_active();
93         const MenuList& items = _menu.items ();
94         int c = 0;
95
96         if (!current_active) {
97                 return true;
98         }
99
100         /* work around another gtkmm API clusterfuck
101          * const MenuItem* get_active () const
102          * void set_active (guint index)
103          *
104          * also MenuList.activate_item does not actually
105          * set it as active in the menu.
106          *
107          */
108
109         switch (ev->direction) {
110                 case GDK_SCROLL_UP:
111
112                         for (MenuList::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i, ++c) {
113                                 if ( &(*i) != current_active) {
114                                         continue;
115                                 }
116                                 if (++i != items.rend()) {
117                                         c = items.size() - 2 - c;
118                                         assert(c >= 0);
119                                         _menu.set_active(c);
120                                         _menu.activate_item(*i);
121                                 }
122                                 break;
123                         }
124                         break;
125                 case GDK_SCROLL_DOWN:
126                         for (MenuList::const_iterator i = items.begin(); i != items.end(); ++i, ++c) {
127                                 if ( &(*i) != current_active) {
128                                         continue;
129                                 }
130                                 if (++i != items.end()) {
131                                         assert(c + 1 < (int) items.size());
132                                         _menu.set_active(c + 1);
133                                         _menu.activate_item(*i);
134                                 }
135                                 break;
136                         }
137                         break;
138                 default:
139                         break;
140         }
141         return true;
142 }
143
144 void
145 ArdourDropdown::clear_items ()
146 {
147         _menu.items ().clear ();
148 }
149
150 void
151 ArdourDropdown::AddMenuElem (Menu_Helpers::Element e)
152 {
153         using namespace Menu_Helpers;
154
155         MenuList& items = _menu.items ();
156
157         items.push_back (e);
158 }
159
160 void
161 ArdourDropdown::disable_scrolling()
162 {
163         _scrolling_disabled = true;
164 }