Separate Ardour UI widgets into dedicated library
[ardour.git] / libs / widgets / 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/controllable.h"
28 #include "pbd/error.h"
29 #include "pbd/stacktrace.h"
30
31 #include "gtkmm2ext/utils.h"
32 #include "gtkmm2ext/rgb_macros.h"
33 #include "gtkmm2ext/gui_thread.h"
34 #include "gtkmm2ext/keyboard.h"
35
36 #include "widgets/ardour_display.h"
37
38 #include "pbd/i18n.h"
39
40 using namespace Gtkmm2ext;
41 using namespace Gdk;
42 using namespace Gtk;
43 using namespace Glib;
44 using namespace PBD;
45 using namespace ArdourWidgets;
46 using std::max;
47 using std::min;
48 using namespace std;
49
50 ArdourDisplay::ArdourDisplay (Element e)
51 {
52         add_elements(ArdourButton::Text);
53 }
54
55 ArdourDisplay::~ArdourDisplay ()
56 {
57 }
58
59
60 bool
61 ArdourDisplay::on_scroll_event (GdkEventScroll* ev)
62 {
63         /* mouse wheel */
64
65         float scale = 1.0;
66         if (ev->state & Keyboard::GainFineScaleModifier) {
67                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
68                         scale *= 0.01;
69                 } else {
70                         scale *= 0.10;
71                 }
72         }
73
74         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
75         if (c) {
76                 float val = c->get_interface();
77
78                 if ( ev->direction == GDK_SCROLL_UP )
79                         val += 0.05 * scale;  //by default, we step in 1/20ths of the knob travel
80                 else
81                         val -= 0.05 * scale;
82
83                 c->set_interface(val);
84         }
85
86         return true;
87 }
88
89
90 void
91 ArdourDisplay::add_controllable_preset (const char *txt, float val)
92 {
93         using namespace Menu_Helpers;
94         AddMenuElem(MenuElem (txt, sigc::bind (sigc::mem_fun(*this, &ArdourDisplay::handle_controllable_preset), val)));
95 }
96
97 static inline float dB_to_coefficient (float dB) {
98         return dB > -318.8f ? pow (10.0f, dB * 0.05f) : 0.0f;
99 }
100
101 void
102 ArdourDisplay::handle_controllable_preset (float p)
103 {
104         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
105
106         if (!c) return;
107
108         /* This should not use dB_to_coefficient(), but the Controllable's value.
109          *
110          * The only user of this API is currently monitor_section.cc which conveniently
111          * binds dB values. Once there are other use-cases, for this, this (GUI only) API
112          * needs fixing.
113          */
114         c->set_value(dB_to_coefficient (p), Controllable::NoGroup);
115 }
116
117
118 void
119 ArdourDisplay::set_controllable (boost::shared_ptr<Controllable> c)
120 {
121     watch_connection.disconnect ();  //stop watching the old controllable
122
123         if (!c) return;
124
125         binding_proxy.set_controllable (c);
126
127         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourDisplay::controllable_changed, this), gui_context());
128
129         controllable_changed();
130 }
131
132 void
133 ArdourDisplay::controllable_changed ()
134 {
135         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
136
137         if (!c) return;
138
139         set_text(c->get_user_string());
140
141         set_dirty();
142 }