a-EQ: Tweak default values and inline display scale +-20dB
[ardour.git] / gtk2_ardour / ardour_spinner.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2011 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include "gtkmm2ext/gui_thread.h"
21 #include "gtkmm2ext/keyboard.h"
22
23 #include "ardour_spinner.h"
24
25 using namespace ARDOUR;
26
27 ArdourSpinner::ArdourSpinner (
28                 boost::shared_ptr<ARDOUR::AutomationControl> c,
29                 Gtk::Adjustment* adj,
30                 boost::shared_ptr<ARDOUR::Automatable> p)
31         : _btn (ArdourButton::Text)
32         , _ctrl_adj (adj)
33         , _spin_adj (0, c->lower (), c->upper (), .1, .01)
34         , _spinner (_spin_adj)
35         , _switching (false)
36         , _switch_on_release (false)
37         , _ctrl_ignore (false)
38         , _spin_ignore (false)
39         , _controllable (c)
40         , _printer (p)
41 {
42         add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
43         set (.5, .5, 1.0, 1.0);
44         set_border_width (0);
45
46         _btn.set_controllable (c);
47         _btn.set_fallthrough_to_parent (true);
48
49         _spinner.signal_activate().connect (mem_fun (*this, &ArdourSpinner::entry_activated));
50         _spinner.signal_focus_out_event().connect (mem_fun (*this, &ArdourSpinner::entry_focus_out));
51         _spinner.set_digits (4);
52         _spinner.set_numeric (true);
53         _spinner.set_name ("BarControlSpinner");
54
55         _spin_adj.set_step_increment(c->interface_to_internal(_ctrl_adj->get_step_increment()) - c->lower ());
56         _spin_adj.set_page_increment(c->interface_to_internal(_ctrl_adj->get_page_increment()) - c->lower ());
57
58         _spin_adj.signal_value_changed().connect (sigc::mem_fun(*this, &ArdourSpinner::spin_adjusted));
59         adj->signal_value_changed().connect (sigc::mem_fun(*this, &ArdourSpinner::ctrl_adjusted));
60         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourSpinner::controllable_changed, this), gui_context());
61
62         add (_btn);
63         show_all ();
64
65         controllable_changed();
66         ctrl_adjusted ();
67 }
68
69
70 ArdourSpinner::~ArdourSpinner ()
71 {
72 }
73
74 bool
75 ArdourSpinner::on_button_press_event (GdkEventButton* ev)
76 {
77         if (get_child() != &_btn) {
78                 return false;
79         }
80
81         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
82                 _switch_on_release = true;
83                 return true;
84         } else {
85                 _switch_on_release = false;
86         }
87         return false;
88 }
89
90 bool
91 ArdourSpinner::on_button_release_event (GdkEventButton* ev)
92 {
93         if (get_child() != &_btn) {
94                 return false;
95         }
96         if (ev->button == 1 && _switch_on_release) {
97                 Glib::signal_idle().connect (mem_fun (*this, &ArdourSpinner::switch_to_spinner));
98                 return true;
99         }
100         return false;
101 }
102
103 bool
104 ArdourSpinner::on_scroll_event (GdkEventScroll* ev)
105 {
106         float scale = 1.0;
107         if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
108                 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
109                         scale *= 0.01;
110                 } else {
111                         scale *= 0.10;
112                 }
113         }
114
115         boost::shared_ptr<PBD::Controllable> c = _btn.get_controllable();
116         if (c) {
117                 float val = c->get_interface();
118
119                 if ( ev->direction == GDK_SCROLL_UP )
120                         val += 0.05 * scale;  //by default, we step in 1/20ths of the knob travel
121                 else
122                         val -= 0.05 * scale;
123
124                 c->set_interface(val);
125         }
126
127         return true;
128 }
129
130 gint
131 ArdourSpinner::switch_to_button ()
132 {
133         if (_switching || get_child() == &_btn) {
134                 return false;
135         }
136         _switching = true;
137         remove ();
138         add (_btn);
139         _btn.show ();
140         _btn.set_dirty ();
141         _switching = false;
142         return false;
143 }
144
145 gint
146 ArdourSpinner::switch_to_spinner ()
147 {
148         if (_switching || get_child() != &_btn) {
149                 return false;
150         }
151         _switching = true;
152         remove ();
153         add (_spinner);
154         _spinner.show ();
155         _spinner.select_region (0, _spinner.get_text_length ());
156         _spinner.grab_focus ();
157         _switching = false;
158         return false;
159 }
160
161 void
162 ArdourSpinner::entry_activated ()
163 {
164         switch_to_button ();
165 }
166
167 bool
168 ArdourSpinner::entry_focus_out (GdkEventFocus* /*ev*/)
169 {
170         entry_activated ();
171         return true;
172 }
173
174 void
175 ArdourSpinner::ctrl_adjusted ()
176 {
177         if (_spin_ignore) {
178                 return;
179         }
180         _ctrl_ignore = true;
181         _spin_adj.set_value (_controllable->interface_to_internal (_ctrl_adj->get_value ()));
182         _ctrl_ignore = false;
183 }
184
185 void
186 ArdourSpinner::spin_adjusted ()
187 {
188         if (_ctrl_ignore) {
189                 return;
190         }
191         _spin_ignore = true;
192         _ctrl_adj->set_value (_controllable->internal_to_interface (_spin_adj.get_value ()));
193         _spin_ignore = false;
194 }
195
196 void
197 ArdourSpinner::controllable_changed ()
198 {
199         if (_printer) {
200                 _btn.set_text (_printer->value_as_string (_controllable));
201         } else {
202                 _btn.set_text (_controllable->get_user_string());
203         }
204         _btn.set_dirty();
205 }