plugin widgets were written to use Internal values, so use that for now
[ardour.git] / libs / gtkmm2ext / barcontroller.cc
1 /*
2     Copyright (C) 2004 Paul Davis
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
17
18 #include <string>
19 #include <sstream>
20 #include <climits>
21 #include <cstdio>
22 #include <cmath>
23 #include <algorithm>
24
25 #include <pbd/controllable.h>
26 #include <pbd/locale_guard.h>
27
28 #include "gtkmm2ext/gtk_ui.h"
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/keyboard.h"
31 #include "gtkmm2ext/barcontroller.h"
32 #include "gtkmm2ext/cairo_widget.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace Gtk;
38 using namespace Gtkmm2ext;
39
40 BarController::BarController (Gtk::Adjustment& adj,
41                 boost::shared_ptr<PBD::Controllable> mc)
42         : _slider (&adj, 60, 16)
43         , _switching (false)
44         , _switch_on_release (false)
45 {
46
47         add_events (Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
48         set (.5, .5, 1.0, 1.0);
49         set_border_width (0);
50         _slider.set_controllable (mc);
51         _slider.set_tweaks (PixFader::NoShowUnityLine);
52
53         _slider.StartGesture.connect (sigc::mem_fun(*this, &BarController::passtrhu_gesture_start));
54         _slider.StopGesture.connect (sigc::mem_fun(*this, &BarController::passtrhu_gesture_stop));
55         _slider.OnExpose.connect (sigc::mem_fun(*this, &BarController::before_expose));
56         _slider.set_name (get_name());
57
58         Gtk::SpinButton& spinner = _slider.get_spin_button();
59         spinner.signal_activate().connect (mem_fun (*this, &BarController::entry_activated));
60         spinner.signal_focus_out_event().connect (mem_fun (*this, &BarController::entry_focus_out));
61         spinner.set_digits (9);
62         spinner.set_numeric (true);
63         spinner.set_name ("BarControlSpinner");
64         add (_slider);
65         show_all ();
66 }
67
68 BarController::~BarController ()
69 {
70 }
71
72 bool
73 BarController::on_button_press_event (GdkEventButton* ev)
74 {
75         if (get_child() != &_slider) {
76                 return false;
77         }
78         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
79                 _switch_on_release = true;
80                 return true;
81         } else {
82                 _switch_on_release = false;
83         }
84         return false;
85 }
86
87 bool
88 BarController::on_button_release_event (GdkEventButton* ev)
89 {
90         if (get_child() != &_slider) {
91                 return false;
92         }
93         if (ev->button == 1 && _switch_on_release) {
94                 Glib::signal_idle().connect (mem_fun (*this, &BarController::switch_to_spinner));
95                 return true;
96         }
97         return false;
98 }
99
100 void
101 BarController::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
102 {
103         _slider.set_name (get_name());
104 }
105
106 gint
107 BarController::switch_to_bar ()
108 {
109         if (_switching || get_child() == &_slider) {
110                 return FALSE;
111         }
112         _switching = true;
113         remove ();
114         add (_slider);
115         _slider.show ();
116         _slider.queue_draw ();
117         _switching = false;
118         SpinnerActive (false); /* EMIT SIGNAL */
119         return FALSE;
120 }
121
122 gint
123 BarController::switch_to_spinner ()
124 {
125         if (_switching || get_child() != &_slider) {
126                 return FALSE;
127         }
128
129         _switching = true;
130         Gtk::SpinButton& spinner = _slider.get_spin_button();
131         if (spinner.get_parent()) {
132                 spinner.get_parent()->remove(spinner);
133         }
134         remove ();
135         add (spinner);
136         spinner.show ();
137         spinner.select_region (0, spinner.get_text_length());
138         spinner.grab_focus ();
139         _switching = false;
140         SpinnerActive (true); /* EMIT SIGNAL */
141         return FALSE;
142 }
143
144 void
145 BarController::entry_activated ()
146 {
147         switch_to_bar ();
148 }
149
150 bool
151 BarController::entry_focus_out (GdkEventFocus* /*ev*/)
152 {
153         entry_activated ();
154         return true;
155 }
156
157 void
158 BarController::before_expose ()
159 {
160         double xpos = -1;
161         _slider.set_text (get_label (xpos), false, false);
162 }
163
164 void
165 BarController::set_sensitive (bool yn)
166 {
167         Alignment::set_sensitive (yn);
168         _slider.set_sensitive (yn);
169 }