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/rc_configuration.h" // for widget prelight preference
38 #include "ardour_knob.h"
40 #include "ui_config.h"
42 #include "canvas/colors.h"
43 #include "canvas/utils.h"
47 using namespace Gtkmm2ext;
56 ArdourKnob::Element ArdourKnob::default_elements = ArdourKnob::Element (ArdourKnob::Arc);
58 ArdourKnob::ArdourKnob (Element e, Flags flags)
65 , _dead_zone_delta (0)
69 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourKnob::color_handler));
71 // watch automation :(
72 Timers::rapid_connect (sigc::mem_fun (*this, &ArdourKnob::controllable_changed));
75 ArdourKnob::~ArdourKnob()
80 ArdourKnob::render (cairo_t* cr, cairo_rectangle_t *)
82 cairo_pattern_t* shade_pattern;
84 float width = get_width();
85 float height = get_height();
87 const float scale = min(width, height);
88 const float pointer_thickness = 3.0 * (scale/80); //(if the knob is 80 pixels wide, we want a 3-pix line on it)
90 const float start_angle = ((180 - 65) * G_PI) / 180;
91 const float end_angle = ((360 + 65) * G_PI) / 180;
94 if (_flags & ArcToZero) {
98 const float value_angle = start_angle + (_val * (end_angle - start_angle));
99 const float zero_angle = start_angle + (zero * (end_angle - start_angle));
101 float value_x = cos (value_angle);
102 float value_y = sin (value_angle);
104 float xc = 0.5 + width/ 2.0;
105 float yc = 0.5 + height/ 2.0;
107 cairo_translate (cr, xc, yc); //after this, everything is based on the center of the knob
109 //get the knob color from the theme
110 ArdourCanvas::Color knob_color = UIConfiguration::instance().color (string_compose ("%1", get_name()));
112 float center_radius = 0.48*scale;
113 float border_width = 0.8;
115 bool arc = (_elements & Arc)==Arc;
116 bool bevel = (_elements & Bevel)==Bevel;
117 bool flat = _flat_buttons;
120 center_radius = scale*0.33;
122 float inner_progress_radius = scale*0.38;
123 float outer_progress_radius = scale*0.48;
124 float progress_width = (outer_progress_radius-inner_progress_radius);
125 float progress_radius = inner_progress_radius + progress_width/2.0;
127 //dark arc background
128 cairo_set_source_rgb (cr, 0.3, 0.3, 0.3 );
129 cairo_set_line_width (cr, progress_width);
130 cairo_arc (cr, 0, 0, progress_radius, start_angle, end_angle);
133 //look up the arc colors from the config
134 double red_start, green_start, blue_start, unused;
135 ArdourCanvas::Color arc_start_color = UIConfiguration::instance().color ( string_compose ("%1: arc start", get_name()));
136 ArdourCanvas::color_to_rgba( arc_start_color, red_start, green_start, blue_start, unused );
137 double red_end, green_end, blue_end;
138 ArdourCanvas::Color arc_end_color = UIConfiguration::instance().color ( string_compose ("%1: arc end", get_name()) );
139 ArdourCanvas::color_to_rgba( arc_end_color, red_end, green_end, blue_end, unused );
141 //vary the arc color over the travel of the knob
142 float intensity = fabsf (_val - zero) / std::max(zero, (1.f - zero));
143 const float intensity_inv = 1.0 - intensity;
144 float r = intensity_inv * red_end + intensity * red_start;
145 float g = intensity_inv * green_end + intensity * green_start;
146 float b = intensity_inv * blue_end + intensity * blue_start;
149 cairo_set_source_rgb (cr, r,g,b);
150 cairo_set_line_width (cr, progress_width);
151 if (zero_angle > value_angle) {
152 cairo_arc (cr, 0, 0, progress_radius, value_angle, zero_angle);
154 cairo_arc (cr, 0, 0, progress_radius, zero_angle, value_angle);
160 shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0, yc); //note we have to offset the pattern from our centerpoint
161 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.15);
162 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.5, 1,1,1, 0.0);
163 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 1,1,1, 0.0);
164 cairo_set_source (cr, shade_pattern);
165 cairo_arc (cr, 0, 0, outer_progress_radius-1, 0, 2.0*G_PI);
167 cairo_pattern_destroy (shade_pattern);
171 const float start_angle_x = cos (start_angle);
172 const float start_angle_y = sin (start_angle);
173 const float end_angle_x = cos (end_angle);
174 const float end_angle_y = sin (end_angle);
176 cairo_set_source_rgb (cr, 0, 0, 0 );
177 cairo_set_line_width (cr, border_width);
178 cairo_move_to (cr, (outer_progress_radius * start_angle_x), (outer_progress_radius * start_angle_y));
179 cairo_line_to (cr, (inner_progress_radius * start_angle_x), (inner_progress_radius * start_angle_y));
181 cairo_move_to (cr, (outer_progress_radius * end_angle_x), (outer_progress_radius * end_angle_y));
182 cairo_line_to (cr, (inner_progress_radius * end_angle_x), (inner_progress_radius * end_angle_y));
184 cairo_arc (cr, 0, 0, outer_progress_radius, start_angle, end_angle);
192 cairo_translate(cr, pointer_thickness+1, pointer_thickness+1 );
193 cairo_set_source_rgba (cr, 0, 0, 0, 0.1 );
194 cairo_arc (cr, 0, 0, center_radius-1, 0, 2.0*G_PI);
199 ArdourCanvas::set_source_rgba(cr, knob_color);
200 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
206 shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0, yc); //note we have to offset the gradient from our centerpoint
207 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
208 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.2, 1,1,1, 0.2);
209 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.8, 0,0,0, 0.2);
210 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.2);
211 cairo_set_source (cr, shade_pattern);
212 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
214 cairo_pattern_destroy (shade_pattern);
216 //flat top over beveled edge
217 ArdourCanvas::set_source_rgb_a (cr, knob_color, 0.5 );
218 cairo_arc (cr, 0, 0, center_radius-pointer_thickness, 0, 2.0*G_PI);
222 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
223 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
224 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.3);
225 cairo_set_source (cr, shade_pattern);
226 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
228 cairo_pattern_destroy (shade_pattern);
233 ArdourCanvas::set_source_rgba(cr, knob_color);
234 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
240 cairo_set_line_width (cr, border_width);
241 cairo_set_source_rgba (cr, 0,0,0, 1 );
242 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
248 cairo_translate(cr, 1, 1 );
249 cairo_set_source_rgba (cr, 0,0,0,0.3 );
250 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
251 cairo_set_line_width (cr, pointer_thickness);
252 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
253 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
259 cairo_set_source_rgba (cr, 1,1,1, 1 );
260 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
261 cairo_set_line_width (cr, pointer_thickness);
262 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
263 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
266 //highlight if grabbed or if mouse is hovering over me
267 if (_tooltip.dragging() || (_hovering && UIConfiguration::instance().get_widget_prelight() ) ) {
268 cairo_set_source_rgba (cr, 1,1,1, 0.12 );
269 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
273 cairo_identity_matrix(cr);
277 ArdourKnob::on_size_request (Gtk::Requisition* req)
279 // see ardour-button VectorIcon size, use font scaling as default
280 CairoWidget::on_size_request (req); // allow to override
283 if (req->width < req->height) {
284 req->width = req->height;
286 if (req->height < req->width) {
287 req->height = req->width;
292 ArdourKnob::on_scroll_event (GdkEventScroll* ev)
296 float scale = 0.05; //by default, we step in 1/20ths of the knob travel
297 if (ev->state & Keyboard::GainFineScaleModifier) {
298 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
305 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
307 float val = c->get_interface();
309 if ( ev->direction == GDK_SCROLL_UP )
314 c->set_interface(val);
321 ArdourKnob::on_motion_notify_event (GdkEventMotion *ev)
323 if (!(ev->state & Gdk::BUTTON1_MASK)) {
327 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
333 //scale the adjustment based on keyboard modifiers & GUI size
334 const float ui_scale = max (1.f, UIConfiguration::instance().get_ui_scale());
335 float scale = 0.0025 / ui_scale;
337 if (ev->state & Keyboard::GainFineScaleModifier) {
338 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
345 //calculate the travel of the mouse
346 int delta = (_grabbed_y - ev->y) - (_grabbed_x - ev->x);
353 float val = c->get_interface();
355 if (_flags & Detent) {
356 const float px_deadzone = 42.f * ui_scale;
358 if ((val - _normal) * (val - _normal + delta * scale) < 0) {
360 const int tozero = (_normal - val) * scale;
361 int remain = delta - tozero;
362 if (abs (remain) > px_deadzone) {
363 /* slow down passing the default value */
364 remain += (remain > 0) ? px_deadzone * -.5 : px_deadzone * .5;
365 delta = tozero + remain;
366 _dead_zone_delta = 0;
368 c->set_value (c->normal(), Controllable::NoGroup);
369 _dead_zone_delta = remain / px_deadzone;
374 if (fabsf (rintf((val - _normal) / scale) + _dead_zone_delta) < 1) {
375 c->set_value (c->normal(), Controllable::NoGroup);
376 _dead_zone_delta += delta / px_deadzone;
380 _dead_zone_delta = 0;
383 val += delta * scale;
384 c->set_interface(val);
390 ArdourKnob::on_button_press_event (GdkEventButton *ev)
394 _dead_zone_delta = 0;
396 if (ev->type != GDK_BUTTON_PRESS) {
399 gdk_pointer_ungrab (GDK_CURRENT_TIME);
404 if (binding_proxy.button_press_handler (ev)) {
408 if (ev->button != 1 && ev->button != 2) {
412 set_active_state (Gtkmm2ext::ExplicitActive);
413 _tooltip.start_drag();
416 gdk_pointer_grab(ev->window,false,
417 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
423 ArdourKnob::on_button_release_event (GdkEventButton *ev)
425 _tooltip.stop_drag();
428 gdk_pointer_ungrab (GDK_CURRENT_TIME);
430 if ( (_grabbed_y == ev->y && _grabbed_x == ev->x) && Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) { //no move, shift-click sets to default
431 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
432 if (!c) return false;
433 c->set_value (c->normal(), Controllable::NoGroup);
437 unset_active_state ();
443 ArdourKnob::color_handler ()
449 ArdourKnob::on_size_allocate (Allocation& alloc)
451 CairoWidget::on_size_allocate (alloc);
455 ArdourKnob::set_controllable (boost::shared_ptr<Controllable> c)
457 watch_connection.disconnect (); //stop watching the old controllable
461 binding_proxy.set_controllable (c);
463 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourKnob::controllable_changed, this), gui_context());
465 _normal = c->internal_to_interface(c->normal());
467 controllable_changed();
471 ArdourKnob::controllable_changed ()
473 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
476 float val = c->get_interface();
477 val = min( max(0.0f, val), 1.0f); // clamp
484 if (!_tooltip_prefix.empty()) {
485 _tooltip.set_tip (_tooltip_prefix + c->get_user_string());
491 ArdourKnob::on_style_changed (const RefPtr<Gtk::Style>&)
497 ArdourKnob::on_name_changed ()
504 ArdourKnob::set_active_state (Gtkmm2ext::ActiveState s)
506 if (_active_state != s)
507 CairoWidget::set_active_state (s);
511 ArdourKnob::set_visual_state (Gtkmm2ext::VisualState s)
513 if (_visual_state != s)
514 CairoWidget::set_visual_state (s);
519 ArdourKnob::on_focus_in_event (GdkEventFocus* ev)
522 return CairoWidget::on_focus_in_event (ev);
526 ArdourKnob::on_focus_out_event (GdkEventFocus* ev)
529 return CairoWidget::on_focus_out_event (ev);
533 ArdourKnob::on_enter_notify_event (GdkEventCrossing* ev)
539 return CairoWidget::on_enter_notify_event (ev);
543 ArdourKnob::on_leave_notify_event (GdkEventCrossing* ev)
549 return CairoWidget::on_leave_notify_event (ev);
553 ArdourKnob::set_elements (Element e)
559 ArdourKnob::add_elements (Element e)
561 _elements = (ArdourKnob::Element) (_elements | e);
565 KnobPersistentTooltip::KnobPersistentTooltip (Gtk::Widget* w)
566 : PersistentTooltip (w, true, 3)
572 KnobPersistentTooltip::start_drag ()
578 KnobPersistentTooltip::stop_drag ()
584 KnobPersistentTooltip::dragging () const