Merge branch 'waveview_hacks' of https://github.com/nmains/ardour into cairocanvas
[ardour.git] / gtk2_ardour / ardour_display.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 #include "gtkmm2ext/keyboard.h"
34
35 #include "ardour/rc_configuration.h" // for widget prelight preference
36
37 #include "ardour_display.h"
38 #include "ardour_ui.h"
39 #include "global_signals.h"
40
41 #include "i18n.h"
42
43 using namespace Gtkmm2ext;
44 using namespace Gdk;
45 using namespace Gtk;
46 using namespace Glib;
47 using namespace PBD;
48 using std::max;
49 using std::min;
50 using namespace std;
51
52
53 ArdourDisplay::ArdourDisplay (Element e)
54 {
55         signal_button_press_event().connect (sigc::mem_fun(*this, &ArdourDisplay::on_mouse_pressed));
56
57         add_elements(e);
58         add_elements(ArdourButton::Menu);
59         add_elements(ArdourButton::Text);
60 }
61
62 ArdourDisplay::~ArdourDisplay ()
63 {
64 }
65
66 bool
67 ArdourDisplay::on_mouse_pressed (GdkEventButton*)
68 {
69         _menu.popup (1, gtk_get_current_event_time());  
70         return true;
71 }
72
73 bool
74 ArdourDisplay::on_scroll_event (GdkEventScroll* ev)
75 {
76         /* mouse wheel */
77
78         float scale = 1.0;
79         if (ev->state & Keyboard::GainFineScaleModifier) {
80                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
81                         scale *= 0.01;
82                 } else {
83                         scale *= 0.10;
84                 }
85         }
86
87         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
88         if (c) {
89                 float val = c->get_interface();
90         
91                 if ( ev->direction == GDK_SCROLL_UP )
92                         val += 0.05 * scale;  //by default, we step in 1/20ths of the knob travel
93                 else
94                         val -= 0.05 * scale;
95                         
96                 c->set_interface(val);
97         }
98
99         return true;
100 }
101
102
103 void
104 ArdourDisplay::add_controllable_preset (const char *txt, float val)
105 {
106         using namespace Menu_Helpers;
107
108         MenuList& items = _menu.items ();
109
110         items.push_back (MenuElem (txt, sigc::bind (sigc::mem_fun(*this, &ArdourDisplay::handle_controllable_preset), val)));
111 }
112
113
114 void
115 ArdourDisplay::handle_controllable_preset (float p)
116 {
117         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
118
119         if (!c) return;
120
121         c->set_user(p);
122 }
123
124
125 void
126 ArdourDisplay::set_controllable (boost::shared_ptr<Controllable> c)
127 {
128     watch_connection.disconnect ();  //stop watching the old controllable
129
130         if (!c) return;
131
132         binding_proxy.set_controllable (c);
133
134         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourDisplay::controllable_changed, this), gui_context());
135
136         controllable_changed();
137 }
138
139 void
140 ArdourDisplay::controllable_changed ()
141 {
142         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
143
144         if (!c) return;
145
146         set_text(c->get_user_string());
147
148         set_dirty();
149 }