2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <pangomm/layout.h>
26 #include "pbd/compose.h"
27 #include "pbd/controllable.h"
28 #include "pbd/error.h"
29 #include "pbd/stacktrace.h"
31 #include "gtkmm2ext/utils.h"
32 #include "gtkmm2ext/rgb_macros.h"
33 #include "gtkmm2ext/gui_thread.h"
34 #include "gtkmm2ext/keyboard.h"
36 #include "ardour/automation_control.h"
37 #include "ardour/rc_configuration.h" // for widget prelight preference
39 #include "ardour_knob.h"
41 #include "ui_config.h"
43 #include "canvas/colors.h"
44 #include "canvas/utils.h"
48 using namespace Gtkmm2ext;
57 ArdourKnob::Element ArdourKnob::default_elements = ArdourKnob::Element (ArdourKnob::Arc);
59 ArdourKnob::ArdourKnob (Element e, Flags flags)
66 , _dead_zone_delta (0)
70 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourKnob::color_handler));
72 // watch automation :(
73 // TODO only use for GainAutomation
74 Timers::rapid_connect (sigc::bind (sigc::mem_fun (*this, &ArdourKnob::controllable_changed), false));
77 ArdourKnob::~ArdourKnob()
82 ArdourKnob::render (cairo_t* cr, cairo_rectangle_t *)
84 cairo_pattern_t* shade_pattern;
86 float width = get_width();
87 float height = get_height();
89 const float scale = min(width, height);
90 const float pointer_thickness = 3.0 * (scale/80); //(if the knob is 80 pixels wide, we want a 3-pix line on it)
92 const float start_angle = ((180 - 65) * G_PI) / 180;
93 const float end_angle = ((360 + 65) * G_PI) / 180;
96 if (_flags & ArcToZero) {
100 const float value_angle = start_angle + (_val * (end_angle - start_angle));
101 const float zero_angle = start_angle + (zero * (end_angle - start_angle));
103 float value_x = cos (value_angle);
104 float value_y = sin (value_angle);
106 float xc = 0.5 + width/ 2.0;
107 float yc = 0.5 + height/ 2.0;
109 cairo_translate (cr, xc, yc); //after this, everything is based on the center of the knob
111 //get the knob color from the theme
112 ArdourCanvas::Color knob_color = UIConfiguration::instance().color (string_compose ("%1", get_name()));
114 float center_radius = 0.48*scale;
115 float border_width = 0.8;
117 bool arc = (_elements & Arc)==Arc;
118 bool bevel = (_elements & Bevel)==Bevel;
119 bool flat = _flat_buttons;
122 center_radius = scale*0.33;
124 float inner_progress_radius = scale*0.38;
125 float outer_progress_radius = scale*0.48;
126 float progress_width = (outer_progress_radius-inner_progress_radius);
127 float progress_radius = inner_progress_radius + progress_width/2.0;
129 //dark arc background
130 cairo_set_source_rgb (cr, 0.3, 0.3, 0.3 );
131 cairo_set_line_width (cr, progress_width);
132 cairo_arc (cr, 0, 0, progress_radius, start_angle, end_angle);
135 //look up the arc colors from the config
136 double red_start, green_start, blue_start, unused;
137 ArdourCanvas::Color arc_start_color = UIConfiguration::instance().color ( string_compose ("%1: arc start", get_name()));
138 ArdourCanvas::color_to_rgba( arc_start_color, red_start, green_start, blue_start, unused );
139 double red_end, green_end, blue_end;
140 ArdourCanvas::Color arc_end_color = UIConfiguration::instance().color ( string_compose ("%1: arc end", get_name()) );
141 ArdourCanvas::color_to_rgba( arc_end_color, red_end, green_end, blue_end, unused );
143 //vary the arc color over the travel of the knob
144 float intensity = fabsf (_val - zero) / std::max(zero, (1.f - zero));
145 const float intensity_inv = 1.0 - intensity;
146 float r = intensity_inv * red_end + intensity * red_start;
147 float g = intensity_inv * green_end + intensity * green_start;
148 float b = intensity_inv * blue_end + intensity * blue_start;
151 cairo_set_source_rgb (cr, r,g,b);
152 cairo_set_line_width (cr, progress_width);
153 if (zero_angle > value_angle) {
154 cairo_arc (cr, 0, 0, progress_radius, value_angle, zero_angle);
156 cairo_arc (cr, 0, 0, progress_radius, zero_angle, value_angle);
162 shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0, yc); //note we have to offset the pattern from our centerpoint
163 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.15);
164 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.5, 1,1,1, 0.0);
165 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 1,1,1, 0.0);
166 cairo_set_source (cr, shade_pattern);
167 cairo_arc (cr, 0, 0, outer_progress_radius-1, 0, 2.0*G_PI);
169 cairo_pattern_destroy (shade_pattern);
173 const float start_angle_x = cos (start_angle);
174 const float start_angle_y = sin (start_angle);
175 const float end_angle_x = cos (end_angle);
176 const float end_angle_y = sin (end_angle);
178 cairo_set_source_rgb (cr, 0, 0, 0 );
179 cairo_set_line_width (cr, border_width);
180 cairo_move_to (cr, (outer_progress_radius * start_angle_x), (outer_progress_radius * start_angle_y));
181 cairo_line_to (cr, (inner_progress_radius * start_angle_x), (inner_progress_radius * start_angle_y));
183 cairo_move_to (cr, (outer_progress_radius * end_angle_x), (outer_progress_radius * end_angle_y));
184 cairo_line_to (cr, (inner_progress_radius * end_angle_x), (inner_progress_radius * end_angle_y));
186 cairo_arc (cr, 0, 0, outer_progress_radius, start_angle, end_angle);
194 cairo_translate(cr, pointer_thickness+1, pointer_thickness+1 );
195 cairo_set_source_rgba (cr, 0, 0, 0, 0.1 );
196 cairo_arc (cr, 0, 0, center_radius-1, 0, 2.0*G_PI);
201 ArdourCanvas::set_source_rgba(cr, knob_color);
202 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
208 shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0, yc); //note we have to offset the gradient from our centerpoint
209 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
210 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.2, 1,1,1, 0.2);
211 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.8, 0,0,0, 0.2);
212 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.2);
213 cairo_set_source (cr, shade_pattern);
214 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
216 cairo_pattern_destroy (shade_pattern);
218 //flat top over beveled edge
219 ArdourCanvas::set_source_rgb_a (cr, knob_color, 0.5 );
220 cairo_arc (cr, 0, 0, center_radius-pointer_thickness, 0, 2.0*G_PI);
224 shade_pattern = cairo_pattern_create_radial ( -center_radius, -center_radius, 1, -center_radius, -center_radius, center_radius*2.5 ); //note we have to offset the gradient from our centerpoint
225 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
226 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.3);
227 cairo_set_source (cr, shade_pattern);
228 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
230 cairo_pattern_destroy (shade_pattern);
235 ArdourCanvas::set_source_rgba(cr, knob_color);
236 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
242 cairo_set_line_width (cr, border_width);
243 cairo_set_source_rgba (cr, 0,0,0, 1 );
244 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
250 cairo_translate(cr, 1, 1 );
251 cairo_set_source_rgba (cr, 0,0,0,0.3 );
252 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
253 cairo_set_line_width (cr, pointer_thickness);
254 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
255 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
261 cairo_set_source_rgba (cr, 1,1,1, 1 );
262 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
263 cairo_set_line_width (cr, pointer_thickness);
264 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
265 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
268 //highlight if grabbed or if mouse is hovering over me
269 if (_tooltip.dragging() || (_hovering && UIConfiguration::instance().get_widget_prelight() ) ) {
270 cairo_set_source_rgba (cr, 1,1,1, 0.12 );
271 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
275 cairo_identity_matrix(cr);
279 ArdourKnob::on_size_request (Gtk::Requisition* req)
281 // see ardour-button VectorIcon size, use font scaling as default
282 CairoWidget::on_size_request (req); // allow to override
285 if (req->width < req->height) {
286 req->width = req->height;
288 if (req->height < req->width) {
289 req->height = req->width;
294 ArdourKnob::on_scroll_event (GdkEventScroll* ev)
298 float scale = 0.05; //by default, we step in 1/20ths of the knob travel
299 if (ev->state & Keyboard::GainFineScaleModifier) {
300 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
307 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
309 float val = c->get_interface();
311 if ( ev->direction == GDK_SCROLL_UP )
316 c->set_interface(val);
323 ArdourKnob::on_motion_notify_event (GdkEventMotion *ev)
325 if (!(ev->state & Gdk::BUTTON1_MASK)) {
329 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
335 //scale the adjustment based on keyboard modifiers & GUI size
336 const float ui_scale = max (1.f, UIConfiguration::instance().get_ui_scale());
337 float scale = 0.0025 / ui_scale;
339 if (ev->state & Keyboard::GainFineScaleModifier) {
340 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
347 //calculate the travel of the mouse
348 int delta = (_grabbed_y - ev->y) - (_grabbed_x - ev->x);
355 float val = c->get_interface();
357 if (_flags & Detent) {
358 const float px_deadzone = 42.f * ui_scale;
360 if ((val - _normal) * (val - _normal + delta * scale) < 0) {
362 const int tozero = (_normal - val) * scale;
363 int remain = delta - tozero;
364 if (abs (remain) > px_deadzone) {
365 /* slow down passing the default value */
366 remain += (remain > 0) ? px_deadzone * -.5 : px_deadzone * .5;
367 delta = tozero + remain;
368 _dead_zone_delta = 0;
370 c->set_value (c->normal(), Controllable::NoGroup);
371 _dead_zone_delta = remain / px_deadzone;
376 if (fabsf (rintf((val - _normal) / scale) + _dead_zone_delta) < 1) {
377 c->set_value (c->normal(), Controllable::NoGroup);
378 _dead_zone_delta += delta / px_deadzone;
382 _dead_zone_delta = 0;
385 val += delta * scale;
386 c->set_interface(val);
392 ArdourKnob::on_button_press_event (GdkEventButton *ev)
396 _dead_zone_delta = 0;
398 if (ev->type != GDK_BUTTON_PRESS) {
401 gdk_pointer_ungrab (GDK_CURRENT_TIME);
406 if (binding_proxy.button_press_handler (ev)) {
410 if (ev->button != 1 && ev->button != 2) {
414 set_active_state (Gtkmm2ext::ExplicitActive);
415 _tooltip.start_drag();
418 gdk_pointer_grab(ev->window,false,
419 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
425 ArdourKnob::on_button_release_event (GdkEventButton *ev)
427 _tooltip.stop_drag();
430 gdk_pointer_ungrab (GDK_CURRENT_TIME);
432 if ( (_grabbed_y == ev->y && _grabbed_x == ev->x) && Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) { //no move, shift-click sets to default
433 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
434 if (!c) return false;
435 c->set_value (c->normal(), Controllable::NoGroup);
439 unset_active_state ();
445 ArdourKnob::color_handler ()
451 ArdourKnob::on_size_allocate (Allocation& alloc)
453 CairoWidget::on_size_allocate (alloc);
457 ArdourKnob::set_controllable (boost::shared_ptr<Controllable> c)
459 watch_connection.disconnect (); //stop watching the old controllable
463 binding_proxy.set_controllable (c);
465 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourKnob::controllable_changed, this, false), gui_context());
467 _normal = c->internal_to_interface(c->normal());
469 controllable_changed();
473 ArdourKnob::controllable_changed (bool force_update)
475 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
478 float val = c->get_interface();
479 val = min( max(0.0f, val), 1.0f); // clamp
481 if (val == _val && !force_update) {
486 if (!_tooltip_prefix.empty()) {
487 boost::shared_ptr<ARDOUR::AutomationControl> ac = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (c);
488 if (_printer && ac) {
490 _tooltip.set_tip (_tooltip_prefix + _printer->value_as_string (ac));
492 _tooltip.set_tip (_tooltip_prefix + c->get_user_string());
499 ArdourKnob::on_style_changed (const RefPtr<Gtk::Style>&)
505 ArdourKnob::on_name_changed ()
512 ArdourKnob::set_active_state (Gtkmm2ext::ActiveState s)
514 if (_active_state != s)
515 CairoWidget::set_active_state (s);
519 ArdourKnob::set_visual_state (Gtkmm2ext::VisualState s)
521 if (_visual_state != s)
522 CairoWidget::set_visual_state (s);
527 ArdourKnob::on_focus_in_event (GdkEventFocus* ev)
530 return CairoWidget::on_focus_in_event (ev);
534 ArdourKnob::on_focus_out_event (GdkEventFocus* ev)
537 return CairoWidget::on_focus_out_event (ev);
541 ArdourKnob::on_enter_notify_event (GdkEventCrossing* ev)
547 return CairoWidget::on_enter_notify_event (ev);
551 ArdourKnob::on_leave_notify_event (GdkEventCrossing* ev)
557 return CairoWidget::on_leave_notify_event (ev);
561 ArdourKnob::set_elements (Element e)
567 ArdourKnob::add_elements (Element e)
569 _elements = (ArdourKnob::Element) (_elements | e);
573 KnobPersistentTooltip::KnobPersistentTooltip (Gtk::Widget* w)
574 : PersistentTooltip (w, true, 3)
580 KnobPersistentTooltip::start_drag ()
586 KnobPersistentTooltip::stop_drag ()
592 KnobPersistentTooltip::dragging () const