2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2011 Paul Davis
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.
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.
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.
20 #include "gtkmm2ext/gui_thread.h"
21 #include "gtkmm2ext/utils.h"
22 #include "gtkmm2ext/keyboard.h"
24 #include "ardour/value_as_string.h"
26 #include "ardour_spinner.h"
28 using namespace ARDOUR;
30 ArdourSpinner::ArdourSpinner (
31 boost::shared_ptr<ARDOUR::AutomationControl> c,
33 boost::shared_ptr<ARDOUR::Automatable> p)
34 : _btn (ArdourButton::Text)
36 , _spin_adj (0, c->lower (), c->upper (), .1, .01)
37 , _spinner (_spin_adj)
39 , _switch_on_release (false)
40 , _ctrl_ignore (false)
41 , _spin_ignore (false)
45 add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
46 set (.5, .5, 1.0, 1.0);
49 _btn.set_controllable (c);
50 _btn.set_fallthrough_to_parent (true);
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");
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 ());
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());
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);
73 controllable_changed();
78 ArdourSpinner::~ArdourSpinner ()
83 ArdourSpinner::on_button_press_event (GdkEventButton* ev)
85 if (get_child() != &_btn) {
89 if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
90 _switch_on_release = true;
93 _switch_on_release = false;
99 ArdourSpinner::on_button_release_event (GdkEventButton* ev)
101 if (get_child() != &_btn) {
104 if (ev->button == 1 && _switch_on_release) {
105 Glib::signal_idle().connect (mem_fun (*this, &ArdourSpinner::switch_to_spinner));
112 ArdourSpinner::on_scroll_event (GdkEventScroll* ev)
115 if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
116 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
123 boost::shared_ptr<PBD::Controllable> c = _btn.get_controllable();
125 float val = c->get_interface();
127 if ( ev->direction == GDK_SCROLL_UP )
128 val += 0.05 * scale; //by default, we step in 1/20ths of the knob travel
132 c->set_interface(val);
139 ArdourSpinner::switch_to_button ()
141 if (_switching || get_child() == &_btn) {
154 ArdourSpinner::switch_to_spinner ()
156 if (_switching || get_child() != &_btn) {
163 _spinner.select_region (0, _spinner.get_text_length ());
164 _spinner.grab_focus ();
170 ArdourSpinner::entry_activated ()
176 ArdourSpinner::entry_focus_out (GdkEventFocus* /*ev*/)
183 ArdourSpinner::ctrl_adjusted ()
189 _spin_adj.set_value (_controllable->interface_to_internal (_ctrl_adj->get_value ()));
190 _ctrl_ignore = false;
194 ArdourSpinner::spin_adjusted ()
200 _ctrl_adj->set_value (_controllable->internal_to_interface (_spin_adj.get_value ()));
201 _spin_ignore = false;
205 ArdourSpinner::controllable_changed ()
208 _btn.set_text (_printer->value_as_string (_controllable));
210 _btn.set_text (_controllable->get_user_string());