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/error.h"
28 #include "pbd/stacktrace.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
33 #include "gtkmm2ext/keyboard.h"
35 #include "ardour/rc_configuration.h" // for widget prelight preference
37 #include "ardour_knob.h"
38 #include "ardour_ui.h"
39 #include "global_signals.h"
41 #include "canvas/utils.h"
45 using namespace Gtkmm2ext;
54 ArdourKnob::Element ArdourKnob::default_elements = ArdourKnob::Element (ArdourKnob::Arc);
56 ArdourKnob::ArdourKnob (Element e)
61 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourKnob::color_handler));
64 ArdourKnob::~ArdourKnob()
69 ArdourKnob::render (cairo_t* cr, cairo_rectangle_t *)
71 cairo_pattern_t* shade_pattern;
73 float width = get_width();
74 float height = get_height();
76 const float scale = min(width, height);
77 const float pointer_thickness = 3.0 * (scale/80); //(if the knob is 80 pixels wide, we want a 3-pix line on it)
79 float start_angle = ((180 - 65) * G_PI) / 180;
80 float end_angle = ((360 + 65) * G_PI) / 180;
81 float value_angle = start_angle + (_val * (end_angle - start_angle));
83 float value_x = cos (value_angle);
84 float value_y = sin (value_angle);
86 cairo_set_antialias( cr, CAIRO_ANTIALIAS_BEST );
88 float xc = 0.5 + width/ 2.0;
89 float yc = 0.5 + height/ 2.0;
91 cairo_translate (cr, xc, yc); //after this, everything is based on the center of the knob
93 //get the knob color from the theme
94 ArdourCanvas::Color knob_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1", get_name()));
96 float center_radius = 0.48*scale;
98 bool arc = (_elements & Arc)==Arc;
99 bool bevel = (_elements & Bevel)==Bevel;
100 bool flat = ARDOUR_UI::config()->get_flat_buttons();
103 center_radius = scale*0.25;
105 float inner_progress_radius = scale*0.25;
106 float outer_progress_radius = scale*0.48;
107 float progress_width = (outer_progress_radius-inner_progress_radius);
108 float progress_radius = inner_progress_radius + progress_width/2.0;
110 float start_angle_x = cos (start_angle);
111 float start_angle_y = sin (start_angle);
112 float end_angle_x = cos (end_angle);
113 float end_angle_y = sin (end_angle);
115 //dark arc background
116 cairo_set_source_rgb (cr, 0.3, 0.3, 0.3 );
117 cairo_set_line_width (cr, progress_width);
118 cairo_arc (cr, 0, 0, progress_radius, start_angle, end_angle);
121 //look up the arc colors from the config
122 double red_start, green_start, blue_start, unused;
123 ArdourCanvas::Color arc_start_color = ARDOUR_UI::config()->color_by_name ( "processor fader: fill start");
124 ArdourCanvas::color_to_rgba( arc_start_color, red_start, green_start, blue_start, unused );
125 double red_end, green_end, blue_end;
126 ArdourCanvas::Color arc_end_color = ARDOUR_UI::config()->color_by_name ( "processor fader: fill end" );
127 ArdourCanvas::color_to_rgba( arc_end_color, red_end, green_end, blue_end, unused );
129 //vary the arc color over the travel of the knob
130 float r = (1.0-_val) * red_end + _val * red_start;
131 float g = (1.0-_val) * green_end + _val * green_start;
132 float b = (1.0-_val) * blue_end + _val * blue_start;
135 cairo_set_source_rgb (cr, r,g,b);
136 cairo_set_line_width (cr, progress_width);
137 cairo_arc (cr, 0, 0, progress_radius, start_angle, value_angle);
142 shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0, yc); //note we have to offset the pattern from our centerpoint
143 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.15);
144 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.5, 1,1,1, 0.0);
145 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 1,1,1, 0.0);
146 cairo_set_source (cr, shade_pattern);
147 cairo_arc (cr, 0, 0, outer_progress_radius-1, 0, 2.0*G_PI);
149 cairo_pattern_destroy (shade_pattern);
153 cairo_set_source_rgb (cr, 0, 0, 0 );
154 cairo_set_line_width (cr, 1.0);
155 cairo_move_to (cr, (outer_progress_radius * start_angle_x), (outer_progress_radius * start_angle_y));
156 cairo_line_to (cr, (inner_progress_radius * start_angle_x), (inner_progress_radius * start_angle_y));
158 cairo_move_to (cr, (outer_progress_radius * end_angle_x), (outer_progress_radius * end_angle_y));
159 cairo_line_to (cr, (inner_progress_radius * end_angle_x), (inner_progress_radius * end_angle_y));
161 cairo_arc (cr, 0, 0, outer_progress_radius, start_angle, end_angle);
168 cairo_translate(cr, pointer_thickness+1, pointer_thickness+1 );
169 cairo_set_source_rgba (cr, 0, 0, 0, 0.1 );
170 cairo_arc (cr, 0, 0, center_radius-1, 0, 2.0*G_PI);
175 cairo_set_source_rgb (cr, 0, 0, 0 );
176 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
180 ArdourCanvas::set_source_rgba(cr, knob_color);
181 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
187 shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0, yc); //note we have to offset the gradient from our centerpoint
188 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
189 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.2, 1,1,1, 0.2);
190 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.8, 0,0,0, 0.2);
191 cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.2);
192 cairo_set_source (cr, shade_pattern);
193 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
195 cairo_pattern_destroy (shade_pattern);
197 //flat top over beveled edge
198 ArdourCanvas::set_source_rgb_a (cr, knob_color, 0.5 );
199 cairo_arc (cr, 0, 0, center_radius-pointer_thickness, 0, 2.0*G_PI);
203 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
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, 1.0, 0,0,0, 0.3);
206 cairo_set_source (cr, shade_pattern);
207 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
209 cairo_pattern_destroy (shade_pattern);
214 ArdourCanvas::set_source_rgba(cr, knob_color);
215 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
221 cairo_set_line_width (cr, 1);
222 cairo_set_source_rgba (cr, 0,0,0, 1 );
223 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
229 cairo_translate(cr, 2, 2 );
230 cairo_set_source_rgba (cr, 0,0,0,0.5 );
231 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
232 cairo_set_line_width (cr, pointer_thickness);
233 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
234 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
240 cairo_set_source_rgba (cr, 1,1,1, 1 );
241 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
242 cairo_set_line_width (cr, pointer_thickness);
243 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
244 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
247 //highlight if grabbed or if mouse is hovering over me
248 if ( _grabbed || (_hovering && ARDOUR::Config->get_widget_prelight() ) ) {
249 cairo_set_source_rgba (cr, 1,1,1, 0.12 );
250 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
254 cairo_identity_matrix(cr);
258 ArdourKnob::on_size_request (Gtk::Requisition* req)
260 CairoWidget::on_size_request (req);
262 //perhaps render the knob base into a cached image here?
266 ArdourKnob::on_scroll_event (GdkEventScroll* ev)
270 float scale = 0.05; //by default, we step in 1/20ths of the knob travel
271 if (ev->state & Keyboard::GainFineScaleModifier) {
272 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
279 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
281 float val = c->get_interface();
283 if ( ev->direction == GDK_SCROLL_UP )
288 c->set_interface(val);
295 ArdourKnob::on_motion_notify_event (GdkEventMotion *ev)
297 //scale the adjustment based on keyboard modifiers
298 float scale = 0.0025;
299 if (ev->state & Keyboard::GainFineScaleModifier) {
300 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
307 //calculate the travel of the mouse
309 if (ev->state & Gdk::BUTTON1_MASK) {
310 y_delta = _grabbed_y - ev->y;
312 if (y_delta == 0) return TRUE;
315 //step the value of the controllable
316 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
318 float val = c->get_interface();
319 val += y_delta * scale;
320 c->set_interface(val);
327 ArdourKnob::on_button_press_event (GdkEventButton *ev)
332 set_active_state (Gtkmm2ext::ExplicitActive);
334 if (binding_proxy.button_press_handler (ev)) {
342 ArdourKnob::on_button_release_event (GdkEventButton *ev)
345 unset_active_state ();
351 ArdourKnob::color_handler ()
357 ArdourKnob::on_size_allocate (Allocation& alloc)
359 CairoWidget::on_size_allocate (alloc);
363 ArdourKnob::set_controllable (boost::shared_ptr<Controllable> c)
365 watch_connection.disconnect (); //stop watching the old controllable
369 binding_proxy.set_controllable (c);
371 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourKnob::controllable_changed, this), gui_context());
373 controllable_changed();
377 ArdourKnob::controllable_changed ()
379 _val = binding_proxy.get_controllable()->get_interface(); //% of knob travel
381 _val = min( max(0.0f, _val), 1.0f); //range check
387 ArdourKnob::on_style_changed (const RefPtr<Gtk::Style>&)
393 ArdourKnob::on_name_changed ()
400 ArdourKnob::set_active_state (Gtkmm2ext::ActiveState s)
402 if (_active_state != s)
403 CairoWidget::set_active_state (s);
407 ArdourKnob::set_visual_state (Gtkmm2ext::VisualState s)
409 if (_visual_state != s)
410 CairoWidget::set_visual_state (s);
415 ArdourKnob::on_focus_in_event (GdkEventFocus* ev)
418 return CairoWidget::on_focus_in_event (ev);
422 ArdourKnob::on_focus_out_event (GdkEventFocus* ev)
425 return CairoWidget::on_focus_out_event (ev);
429 ArdourKnob::on_enter_notify_event (GdkEventCrossing* ev)
435 return CairoWidget::on_enter_notify_event (ev);
439 ArdourKnob::on_leave_notify_event (GdkEventCrossing* ev)
445 return CairoWidget::on_leave_notify_event (ev);
449 ArdourKnob::set_elements (Element e)
455 ArdourKnob::add_elements (Element e)
457 _elements = (ArdourKnob::Element) (_elements | e);