add plural forms for pt to gtk2_ardour/po/pt.po
[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         _menu.signal_size_request().connect (sigc::mem_fun(*this, &ArdourDropdown::menu_size_request));
54
55         _menu.set_reserve_toggle_size(false);
56
57         add_elements(e);
58         add_elements(ArdourButton::Menu);
59 }
60
61 ArdourDropdown::~ArdourDropdown ()
62 {
63 }
64
65 void
66 ArdourDropdown::menu_size_request(Requisition *req) {
67         req->width = max(req->width, get_allocation().get_width());
68 }
69
70 void
71 ArdourDropdown::position_menu(int& x, int& y, bool& push_in) {
72     Gtkmm2ext::position_menu_anchored (&_menu, this, get_text(), x, y, push_in);
73 }
74
75 bool
76 ArdourDropdown::on_button_press_event (GdkEventButton* ev)
77 {
78         if (ev->type == GDK_BUTTON_PRESS) {
79                 _menu.popup (sigc::mem_fun(this, &ArdourDropdown::position_menu),
80                              1, ev->time);
81         }
82         return true;
83 }
84
85 bool
86 ArdourDropdown::on_scroll_event (GdkEventScroll* ev)
87 {
88         using namespace Menu_Helpers;
89
90         if (_scrolling_disabled) {
91                 return false;
92         }
93
94         const MenuItem * current_active = _menu.get_active();
95         const MenuList& items = _menu.items ();
96         int c = 0;
97
98         if (!current_active) {
99                 return true;
100         }
101
102         /* work around another gtkmm API clusterfuck
103          * const MenuItem* get_active () const
104          * void set_active (guint index)
105          *
106          * also MenuList.activate_item does not actually
107          * set it as active in the menu.
108          *
109          */
110
111         switch (ev->direction) {
112                 case GDK_SCROLL_UP:
113
114                         for (MenuList::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i, ++c) {
115                                 if ( &(*i) != current_active) {
116                                         continue;
117                                 }
118                                 if (++i != items.rend()) {
119                                         c = items.size() - 2 - c;
120                                         assert(c >= 0);
121                                         _menu.set_active(c);
122                                         _menu.activate_item(*i);
123                                 }
124                                 break;
125                         }
126                         break;
127                 case GDK_SCROLL_DOWN:
128                         for (MenuList::const_iterator i = items.begin(); i != items.end(); ++i, ++c) {
129                                 if ( &(*i) != current_active) {
130                                         continue;
131                                 }
132                                 if (++i != items.end()) {
133                                         assert(c + 1 < (int) items.size());
134                                         _menu.set_active(c + 1);
135                                         _menu.activate_item(*i);
136                                 }
137                                 break;
138                         }
139                         break;
140                 default:
141                         break;
142         }
143         return true;
144 }
145
146 void
147 ArdourDropdown::clear_items ()
148 {
149         _menu.items ().clear ();
150 }
151
152 void
153 ArdourDropdown::AddMenuElem (Menu_Helpers::Element e)
154 {
155         using namespace Menu_Helpers;
156
157         MenuList& items = _menu.items ();
158
159         items.push_back (e);
160 }
161
162 void
163 ArdourDropdown::disable_scrolling()
164 {
165         _scrolling_disabled = true;
166 }