Move DPIReset and ColorsChanged signals into UIConfiguration
[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 #include "ardour_ui.h"
38
39 #include "i18n.h"
40
41 #define REFLECTION_HEIGHT 2
42
43 using namespace Gdk;
44 using namespace Gtk;
45 using namespace Glib;
46 using namespace PBD;
47 using std::max;
48 using std::min;
49 using namespace std;
50
51
52 ArdourDropdown::ArdourDropdown (Element e)
53 {
54 //      signal_button_press_event().connect (sigc::mem_fun(*this, &ArdourDropdown::on_mouse_pressed));
55
56         add_elements(e);
57         add_elements(ArdourButton::Menu);
58 }
59
60 ArdourDropdown::~ArdourDropdown ()
61 {
62 }
63
64 bool
65 ArdourDropdown::on_button_press_event (GdkEventButton* ev)
66 {
67         if (ev->type == GDK_BUTTON_PRESS) {
68                 _menu.popup (1, gtk_get_current_event_time());
69         }
70         return true;
71 }
72
73 bool
74 ArdourDropdown::on_scroll_event (GdkEventScroll* ev)
75 {
76         using namespace Menu_Helpers;
77
78         const MenuItem * current_active = _menu.get_active();
79         const MenuList& items = _menu.items ();
80         int c = 0;
81
82         if (!current_active) {
83                 return true;
84         }
85
86         /* work around another gtkmm API clusterfuck
87          * const MenuItem* get_active () const
88          * void set_active (guint index)
89          *
90          * also MenuList.activate_item does not actually
91          * set it as active in the menu.
92          *
93          */
94
95         switch (ev->direction) {
96                 case GDK_SCROLL_UP:
97
98                         for (MenuList::const_reverse_iterator i = items.rbegin(); i != items.rend(); ++i, ++c) {
99                                 if ( &(*i) != current_active) {
100                                         continue;
101                                 }
102                                 if (++i != items.rend()) {
103                                         c = items.size() - 2 - c;
104                                         assert(c >= 0);
105                                         _menu.set_active(c);
106                                         _menu.activate_item(*i);
107                                 }
108                                 break;
109                         }
110                         break;
111                 case GDK_SCROLL_DOWN:
112                         for (MenuList::const_iterator i = items.begin(); i != items.end(); ++i, ++c) {
113                                 if ( &(*i) != current_active) {
114                                         continue;
115                                 }
116                                 if (++i != items.end()) {
117                                         assert(c + 1 < (int) items.size());
118                                         _menu.set_active(c + 1);
119                                         _menu.activate_item(*i);
120                                 }
121                                 break;
122                         }
123                         break;
124                 default:
125                         break;
126         }
127         return true;
128 }
129
130 void
131 ArdourDropdown::clear_items ()
132 {
133         _menu.items ().clear ();
134 }
135
136 void
137 ArdourDropdown::AddMenuElem (Menu_Helpers::Element e)
138 {
139         using namespace Menu_Helpers;
140
141         MenuList& items = _menu.items ();
142         
143         items.push_back (e);
144 }
145
146