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