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