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/colors.h"
32 #include "gtkmm2ext/gui_thread.h"
33 #include "gtkmm2ext/keyboard.h"
34 #include "gtkmm2ext/rgb_macros.h"
35 #include "gtkmm2ext/utils.h"
37 #include "widgets/ardour_knob.h"
38 #include "widgets/ui_config.h"
42 using namespace Gtkmm2ext;
43 using namespace ArdourWidgets;
51 ArdourKnob::Element ArdourKnob::default_elements = ArdourKnob::Element (ArdourKnob::Arc);
53 ArdourKnob::ArdourKnob (Element e, Flags flags)
60 , _dead_zone_delta (0)
64 UIConfigurationBase::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourKnob::color_handler));
66 // watch automation :(
67 // TODO only use for GainAutomation
68 //Timers::rapid_connect (sigc::bind (sigc::mem_fun (*this, &ArdourKnob::controllable_changed), false));
71 ArdourKnob::~ArdourKnob()
76 ArdourKnob::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
78 cairo_t* cr = ctx->cobj();
79 cairo_pattern_t* shade_pattern;
81 float width = get_width();
82 float height = get_height();
84 const float scale = min(width, height);
85 const float pointer_thickness = 3.0 * (scale/80); //(if the knob is 80 pixels wide, we want a 3-pix line on it)
87 const float start_angle = ((180 - 65) * G_PI) / 180;
88 const float end_angle = ((360 + 65) * G_PI) / 180;
91 if (_flags & ArcToZero) {
95 const float value_angle = start_angle + (_val * (end_angle - start_angle));
96 const float zero_angle = start_angle + (zero * (end_angle - start_angle));
98 float value_x = cos (value_angle);
99 float value_y = sin (value_angle);
101 float xc = 0.5 + width/ 2.0;
102 float yc = 0.5 + height/ 2.0;
104 cairo_translate (cr, xc, yc); //after this, everything is based on the center of the knob
106 //get the knob color from the theme
107 Gtkmm2ext::Color knob_color = UIConfigurationBase::instance().color (string_compose ("%1", get_name()));
109 float center_radius = 0.48*scale;
110 float border_width = 0.8;
112 bool arc = (_elements & Arc)==Arc;
113 bool bevel = (_elements & Bevel)==Bevel;
114 bool flat = _flat_buttons;
117 center_radius = scale*0.33;
119 float inner_progress_radius = scale*0.38;
120 float outer_progress_radius = scale*0.48;
121 float progress_width = (outer_progress_radius-inner_progress_radius);
122 float progress_radius = inner_progress_radius + progress_width/2.0;
124 //dark arc background
125 cairo_set_source_rgb (cr, 0.3, 0.3, 0.3 );
126 cairo_set_line_width (cr, progress_width);
127 cairo_arc (cr, 0, 0, progress_radius, start_angle, end_angle);
130 //look up the arc colors from the config
131 double red_start, green_start, blue_start, unused;
132 Gtkmm2ext::Color arc_start_color = UIConfigurationBase::instance().color ( string_compose ("%1: arc start", get_name()));
133 Gtkmm2ext::color_to_rgba( arc_start_color, red_start, green_start, blue_start, unused );
134 double red_end, green_end, blue_end;
135 Gtkmm2ext::Color arc_end_color = UIConfigurationBase::instance().color ( string_compose ("%1: arc end", get_name()) );
136 Gtkmm2ext::color_to_rgba( arc_end_color, red_end, green_end, blue_end, unused );
138 //vary the arc color over the travel of the knob
139 float intensity = fabsf (_val - zero) / std::max(zero, (1.f - zero));
140 const float intensity_inv = 1.0 - intensity;
141 float r = intensity_inv * red_end + intensity * red_start;
142 float g = intensity_inv * green_end + intensity * green_start;
143 float b = intensity_inv * blue_end + intensity * blue_start;
146 cairo_set_source_rgb (cr, r,g,b);
147 cairo_set_line_width (cr, progress_width);
148 if (zero_angle > value_angle) {
149 cairo_arc (cr, 0, 0, progress_radius, value_angle, zero_angle);
151 cairo_arc (cr, 0, 0, progress_radius, zero_angle, value_angle);
157 shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0, yc); //note we have to offset the pattern from our centerpoint
158 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.15);
159 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.5, 1,1,1, 0.0);
160 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 1,1,1, 0.0);
161 cairo_set_source (cr, shade_pattern);
162 cairo_arc (cr, 0, 0, outer_progress_radius-1, 0, 2.0*G_PI);
164 cairo_pattern_destroy (shade_pattern);
168 const float start_angle_x = cos (start_angle);
169 const float start_angle_y = sin (start_angle);
170 const float end_angle_x = cos (end_angle);
171 const float end_angle_y = sin (end_angle);
173 cairo_set_source_rgb (cr, 0, 0, 0 );
174 cairo_set_line_width (cr, border_width);
175 cairo_move_to (cr, (outer_progress_radius * start_angle_x), (outer_progress_radius * start_angle_y));
176 cairo_line_to (cr, (inner_progress_radius * start_angle_x), (inner_progress_radius * start_angle_y));
178 cairo_move_to (cr, (outer_progress_radius * end_angle_x), (outer_progress_radius * end_angle_y));
179 cairo_line_to (cr, (inner_progress_radius * end_angle_x), (inner_progress_radius * end_angle_y));
181 cairo_arc (cr, 0, 0, outer_progress_radius, start_angle, end_angle);
189 cairo_translate(cr, pointer_thickness+1, pointer_thickness+1 );
190 cairo_set_source_rgba (cr, 0, 0, 0, 0.1 );
191 cairo_arc (cr, 0, 0, center_radius-1, 0, 2.0*G_PI);
196 Gtkmm2ext::set_source_rgba(cr, knob_color);
197 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
203 shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0, yc); //note we have to offset the gradient from our centerpoint
204 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
205 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.2, 1,1,1, 0.2);
206 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.8, 0,0,0, 0.2);
207 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.2);
208 cairo_set_source (cr, shade_pattern);
209 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
211 cairo_pattern_destroy (shade_pattern);
213 //flat top over beveled edge
214 Gtkmm2ext::set_source_rgb_a (cr, knob_color, 0.5 );
215 cairo_arc (cr, 0, 0, center_radius-pointer_thickness, 0, 2.0*G_PI);
219 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
220 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
221 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.3);
222 cairo_set_source (cr, shade_pattern);
223 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
225 cairo_pattern_destroy (shade_pattern);
230 Gtkmm2ext::set_source_rgba(cr, knob_color);
231 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
237 cairo_set_line_width (cr, border_width);
238 cairo_set_source_rgba (cr, 0,0,0, 1 );
239 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
245 cairo_translate(cr, 1, 1 );
246 cairo_set_source_rgba (cr, 0,0,0,0.3 );
247 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
248 cairo_set_line_width (cr, pointer_thickness);
249 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
250 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
256 cairo_set_source_rgba (cr, 1,1,1, 1 );
257 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
258 cairo_set_line_width (cr, pointer_thickness);
259 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
260 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
263 //highlight if grabbed or if mouse is hovering over me
264 if (_tooltip.dragging() || (_hovering && UIConfigurationBase::instance().get_widget_prelight() ) ) {
265 cairo_set_source_rgba (cr, 1,1,1, 0.12 );
266 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
270 cairo_identity_matrix(cr);
274 ArdourKnob::on_size_request (Gtk::Requisition* req)
276 // see ardour-button VectorIcon size, use font scaling as default
277 CairoWidget::on_size_request (req); // allow to override
280 if (req->width < req->height) {
281 req->width = req->height;
283 if (req->height < req->width) {
284 req->height = req->width;
289 ArdourKnob::on_scroll_event (GdkEventScroll* ev)
293 float scale = 0.05; //by default, we step in 1/20ths of the knob travel
294 if (ev->state & Keyboard::GainFineScaleModifier) {
295 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
302 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
304 float val = c->get_interface();
306 if ( ev->direction == GDK_SCROLL_UP )
311 c->set_interface(val);
318 ArdourKnob::on_motion_notify_event (GdkEventMotion *ev)
320 if (!(ev->state & Gdk::BUTTON1_MASK)) {
324 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
330 //scale the adjustment based on keyboard modifiers & GUI size
331 const float ui_scale = max (1.f, UIConfigurationBase::instance().get_ui_scale());
332 float scale = 0.0025 / ui_scale;
334 if (ev->state & Keyboard::GainFineScaleModifier) {
335 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
342 //calculate the travel of the mouse
343 int delta = (_grabbed_y - ev->y) - (_grabbed_x - ev->x);
350 float val = c->get_interface();
352 if (_flags & Detent) {
353 const float px_deadzone = 42.f * ui_scale;
355 if ((val - _normal) * (val - _normal + delta * scale) < 0) {
357 const int tozero = (_normal - val) * scale;
358 int remain = delta - tozero;
359 if (abs (remain) > px_deadzone) {
360 /* slow down passing the default value */
361 remain += (remain > 0) ? px_deadzone * -.5 : px_deadzone * .5;
362 delta = tozero + remain;
363 _dead_zone_delta = 0;
365 c->set_value (c->normal(), Controllable::NoGroup);
366 _dead_zone_delta = remain / px_deadzone;
371 if (fabsf (rintf((val - _normal) / scale) + _dead_zone_delta) < 1) {
372 c->set_value (c->normal(), Controllable::NoGroup);
373 _dead_zone_delta += delta / px_deadzone;
377 _dead_zone_delta = 0;
380 val += delta * scale;
381 c->set_interface(val);
387 ArdourKnob::on_button_press_event (GdkEventButton *ev)
391 _dead_zone_delta = 0;
393 if (ev->type != GDK_BUTTON_PRESS) {
398 gdk_pointer_ungrab (GDK_CURRENT_TIME);
403 if (binding_proxy.button_press_handler (ev)) {
407 if (ev->button != 1 && ev->button != 2) {
411 set_active_state (Gtkmm2ext::ExplicitActive);
412 _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();
429 gdk_pointer_ungrab (GDK_CURRENT_TIME);
431 if ( (_grabbed_y == ev->y && _grabbed_x == ev->x) && Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) { //no move, shift-click sets to default
432 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
433 if (!c) return false;
434 c->set_value (c->normal(), Controllable::NoGroup);
438 unset_active_state ();
444 ArdourKnob::color_handler ()
450 ArdourKnob::on_size_allocate (Allocation& alloc)
452 CairoWidget::on_size_allocate (alloc);
456 ArdourKnob::set_controllable (boost::shared_ptr<Controllable> c)
458 watch_connection.disconnect (); //stop watching the old controllable
462 binding_proxy.set_controllable (c);
464 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourKnob::controllable_changed, this, false), gui_context());
466 _normal = c->internal_to_interface(c->normal());
468 controllable_changed();
472 ArdourKnob::controllable_changed (bool force_update)
474 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
477 float val = c->get_interface();
478 val = min( max(0.0f, val), 1.0f); // clamp
480 if (val == _val && !force_update) {
485 if (!_tooltip_prefix.empty()) {
486 _tooltip.set_tip (_tooltip_prefix + c->get_user_string());
492 ArdourKnob::on_style_changed (const RefPtr<Gtk::Style>&)
498 ArdourKnob::on_name_changed ()
505 ArdourKnob::set_active_state (Gtkmm2ext::ActiveState s)
507 if (_active_state != s)
508 CairoWidget::set_active_state (s);
512 ArdourKnob::set_visual_state (Gtkmm2ext::VisualState s)
514 if (_visual_state != s)
515 CairoWidget::set_visual_state (s);
520 ArdourKnob::on_focus_in_event (GdkEventFocus* ev)
523 return CairoWidget::on_focus_in_event (ev);
527 ArdourKnob::on_focus_out_event (GdkEventFocus* ev)
530 return CairoWidget::on_focus_out_event (ev);
534 ArdourKnob::on_enter_notify_event (GdkEventCrossing* ev)
540 boost::shared_ptr<PBD::Controllable> c (binding_proxy.get_controllable ());
542 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> (c));
545 return CairoWidget::on_enter_notify_event (ev);
549 ArdourKnob::on_leave_notify_event (GdkEventCrossing* ev)
555 if (binding_proxy.get_controllable()) {
556 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> ());
559 return CairoWidget::on_leave_notify_event (ev);
563 ArdourKnob::set_elements (Element e)
569 ArdourKnob::add_elements (Element e)
571 _elements = (ArdourKnob::Element) (_elements | e);
575 KnobPersistentTooltip::KnobPersistentTooltip (Gtk::Widget* w)
576 : PersistentTooltip (w, true, 3)
582 KnobPersistentTooltip::start_drag ()
588 KnobPersistentTooltip::stop_drag ()
594 KnobPersistentTooltip::dragging () const