Fix deletion of VCA with slaved controls.
[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/utils.h"
22 #include "gtkmm2ext/keyboard.h"
23
24 #include "ardour/value_as_string.h"
25
26 #include "ardour_spinner.h"
27
28 using namespace ARDOUR;
29
30 ArdourSpinner::ArdourSpinner (
31                 boost::shared_ptr<ARDOUR::AutomationControl> c,
32                 Gtk::Adjustment* adj)
33         : _btn (ArdourButton::Text)
34         , _ctrl_adj (adj)
35         , _spin_adj (0, c->lower (), c->upper (), .1, .01)
36         , _spinner (_spin_adj)
37         , _switching (false)
38         , _switch_on_release (false)
39         , _ctrl_ignore (false)
40         , _spin_ignore (false)
41         , _controllable (c)
42 {
43         add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
44         set (.5, .5, 1.0, 1.0);
45         set_border_width (0);
46
47         _btn.set_controllable (c);
48         _btn.set_fallthrough_to_parent (true);
49
50         _spinner.signal_activate().connect (mem_fun (*this, &ArdourSpinner::entry_activated));
51         _spinner.signal_focus_out_event().connect (mem_fun (*this, &ArdourSpinner::entry_focus_out));
52         _spinner.set_digits (4);
53         _spinner.set_numeric (true);
54         _spinner.set_name ("BarControlSpinner");
55
56         _spin_adj.set_step_increment(c->interface_to_internal(_ctrl_adj->get_step_increment()) - c->lower ());
57         _spin_adj.set_page_increment(c->interface_to_internal(_ctrl_adj->get_page_increment()) - c->lower ());
58
59         _spin_adj.signal_value_changed().connect (sigc::mem_fun(*this, &ArdourSpinner::spin_adjusted));
60         adj->signal_value_changed().connect (sigc::mem_fun(*this, &ArdourSpinner::ctrl_adjusted));
61         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourSpinner::controllable_changed, this), gui_context());
62
63
64         // this assume the "upper" value needs most space.
65         std::string txt = ARDOUR::value_as_string (c->desc(), c->upper ());
66         Gtkmm2ext::set_size_request_to_display_given_text (*this, txt, 2, 2);
67
68         add (_btn);
69         show_all ();
70
71         controllable_changed();
72         ctrl_adjusted ();
73 }
74
75
76 ArdourSpinner::~ArdourSpinner ()
77 {
78 }
79
80 bool
81 ArdourSpinner::on_button_press_event (GdkEventButton* ev)
82 {
83         if (get_child() != &_btn) {
84                 return false;
85         }
86
87         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
88                 _switch_on_release = true;
89                 return true;
90         } else {
91                 _switch_on_release = false;
92         }
93         return false;
94 }
95
96 bool
97 ArdourSpinner::on_button_release_event (GdkEventButton* ev)
98 {
99         if (get_child() != &_btn) {
100                 return false;
101         }
102         if (ev->button == 1 && _switch_on_release) {
103                 Glib::signal_idle().connect (mem_fun (*this, &ArdourSpinner::switch_to_spinner));
104                 return true;
105         }
106         return false;
107 }
108
109 bool
110 ArdourSpinner::on_scroll_event (GdkEventScroll* ev)
111 {
112         float scale = 1.0;
113         if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
114                 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
115                         scale *= 0.01;
116                 } else {
117                         scale *= 0.10;
118                 }
119         }
120
121         boost::shared_ptr<PBD::Controllable> c = _btn.get_controllable();
122         if (c) {
123                 float val = c->get_interface();
124
125                 if ( ev->direction == GDK_SCROLL_UP )
126                         val += 0.05 * scale;  //by default, we step in 1/20ths of the knob travel
127                 else
128                         val -= 0.05 * scale;
129
130                 c->set_interface(val);
131         }
132
133         return true;
134 }
135
136 gint
137 ArdourSpinner::switch_to_button ()
138 {
139         if (_switching || get_child() == &_btn) {
140                 return false;
141         }
142         _switching = true;
143         remove ();
144         add (_btn);
145         _btn.show ();
146         _btn.set_dirty ();
147         _switching = false;
148         return false;
149 }
150
151 gint
152 ArdourSpinner::switch_to_spinner ()
153 {
154         if (_switching || get_child() != &_btn) {
155                 return false;
156         }
157         _switching = true;
158         remove ();
159         add (_spinner);
160         _spinner.show ();
161         _spinner.select_region (0, _spinner.get_text_length ());
162         _spinner.grab_focus ();
163         _switching = false;
164         return false;
165 }
166
167 void
168 ArdourSpinner::entry_activated ()
169 {
170         switch_to_button ();
171 }
172
173 bool
174 ArdourSpinner::entry_focus_out (GdkEventFocus* /*ev*/)
175 {
176         entry_activated ();
177         return true;
178 }
179
180 void
181 ArdourSpinner::ctrl_adjusted ()
182 {
183         if (_spin_ignore) {
184                 return;
185         }
186         _ctrl_ignore = true;
187         _spin_adj.set_value (_controllable->interface_to_internal (_ctrl_adj->get_value ()));
188         _ctrl_ignore = false;
189 }
190
191 void
192 ArdourSpinner::spin_adjusted ()
193 {
194         if (_ctrl_ignore) {
195                 return;
196         }
197         _spin_ignore = true;
198         _ctrl_adj->set_value (_controllable->internal_to_interface (_spin_adj.get_value ()));
199         _spin_ignore = false;
200 }
201
202 void
203 ArdourSpinner::controllable_changed ()
204 {
205         _btn.set_text (_controllable->get_user_string());
206         _btn.set_dirty();
207 }