5543950ac17ee349b857a6082cacc7323a6f3c30
[ardour.git] / gtk2_ardour / ardour_knob.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <iostream>
21 #include <cmath>
22 #include <algorithm>
23
24 #include <pangomm/layout.h>
25
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/stacktrace.h"
29
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
33 #include "gtkmm2ext/keyboard.h"
34
35 #include "ardour/rc_configuration.h" // for widget prelight preference
36
37 #include "ardour_knob.h"
38 #include "ardour_ui.h"
39 #include "global_signals.h"
40
41 #include "canvas/utils.h"
42
43 #include "i18n.h"
44
45 using namespace Gtkmm2ext;
46 using namespace Gdk;
47 using namespace Gtk;
48 using namespace Glib;
49 using namespace PBD;
50 using std::max;
51 using std::min;
52 using namespace std;
53
54 ArdourKnob::Element ArdourKnob::default_elements = ArdourKnob::Element (ArdourKnob::Arc);
55
56 ArdourKnob::ArdourKnob (Element e)
57         : _elements (e)
58         , _hovering (false)
59 {
60         ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourKnob::color_handler));
61 }
62
63 ArdourKnob::~ArdourKnob()
64 {
65 }
66
67 void
68 ArdourKnob::render (cairo_t* cr, cairo_rectangle_t *)
69 {
70         cairo_pattern_t* shade_pattern;
71         
72         float width = get_width();
73         float height = get_height();
74         
75         const float scale = min(width, height);
76         const float pointer_thickness = 3.0 * (scale/80);  //(if the knob is 80 pixels wide, we want a 3-pix line on it)
77         
78         float start_angle = ((180 - 65) * G_PI) / 180;
79         float end_angle = ((360 + 65) * G_PI) / 180;
80         float value_angle = start_angle + (_val * (end_angle - start_angle));
81         
82         float value_x = cos (value_angle);
83         float value_y = sin (value_angle);
84
85         float xc =  0.5 + width/ 2.0;
86         float yc = 0.5 + height/ 2.0;
87         
88         cairo_translate (cr, xc, yc);  //after this, everything is based on the center of the knob
89
90         //get the knob color from the theme
91         ArdourCanvas::Color knob_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1", get_name()));
92
93         float center_radius = 0.48*scale;
94         float border_width = 0.8;
95         
96         bool arc = (_elements & Arc)==Arc;
97         bool bevel = (_elements & Bevel)==Bevel;
98         bool flat = _flat_buttons;
99         
100         if ( arc ) {
101                 center_radius = scale*0.30;
102
103                 float inner_progress_radius = scale*0.30;
104                 float outer_progress_radius = scale*0.48;
105                 float progress_width = (outer_progress_radius-inner_progress_radius);
106                 float progress_radius = inner_progress_radius + progress_width/2.0;
107                 
108                 float start_angle_x = cos (start_angle);
109                 float start_angle_y = sin (start_angle);
110                 float end_angle_x = cos (end_angle);
111                 float end_angle_y = sin (end_angle);
112
113                 //dark arc background
114                 cairo_set_source_rgb (cr, 0.3, 0.3, 0.3 );
115                 cairo_set_line_width (cr, progress_width);
116                 cairo_arc (cr, 0, 0, progress_radius, start_angle, end_angle);
117                 cairo_stroke (cr);
118
119                 //look up the arc colors from the config
120                 double red_start, green_start, blue_start, unused;
121                 ArdourCanvas::Color arc_start_color = ARDOUR_UI::config()->color_by_name ( string_compose ("%1: arc start", get_name()));
122                 ArdourCanvas::color_to_rgba( arc_start_color, red_start, green_start, blue_start, unused );
123                 double red_end, green_end, blue_end;
124                 ArdourCanvas::Color arc_end_color = ARDOUR_UI::config()->color_by_name ( string_compose ("%1: arc end", get_name()) );
125                 ArdourCanvas::color_to_rgba( arc_end_color, red_end, green_end, blue_end, unused );
126
127                 //vary the arc color over the travel of the knob
128                 float r = (1.0-_val) * red_end + _val * red_start;
129                 float g = (1.0-_val) * green_end + _val * green_start;
130                 float b = (1.0-_val) * blue_end + _val * blue_start;
131
132                 //draw the arc
133                 cairo_set_source_rgb (cr, r,g,b);
134                 cairo_set_line_width (cr, progress_width);
135                 cairo_arc (cr, 0, 0, progress_radius, start_angle, value_angle);
136                 cairo_stroke (cr);
137
138                 //shade the arc
139                 if (!flat) {
140                         shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0,  yc);  //note we have to offset the pattern from our centerpoint
141                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.15);
142                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.5, 1,1,1, 0.0);
143                         cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 1,1,1, 0.0);
144                         cairo_set_source (cr, shade_pattern);
145                         cairo_arc (cr, 0, 0, outer_progress_radius-1, 0, 2.0*G_PI);
146                         cairo_fill (cr);
147                         cairo_pattern_destroy (shade_pattern);
148                 }
149                 
150                 //black border
151                 cairo_set_source_rgb (cr, 0, 0, 0 );
152                 cairo_set_line_width (cr, border_width);
153                 cairo_move_to (cr, (outer_progress_radius * start_angle_x), (outer_progress_radius * start_angle_y));
154                 cairo_line_to (cr, (inner_progress_radius * start_angle_x), (inner_progress_radius * start_angle_y));
155                 cairo_stroke (cr);
156                 cairo_move_to (cr, (outer_progress_radius * end_angle_x), (outer_progress_radius * end_angle_y));
157                 cairo_line_to (cr, (inner_progress_radius * end_angle_x), (inner_progress_radius * end_angle_y));
158                 cairo_stroke (cr);
159                 cairo_arc (cr, 0, 0, outer_progress_radius, start_angle, end_angle);
160                 cairo_stroke (cr);
161         }
162         
163         if (!flat) {
164                 //knob shadow
165                 cairo_save(cr);
166                 cairo_translate(cr, pointer_thickness+1, pointer_thickness+1 );
167                 cairo_set_source_rgba (cr, 0, 0, 0, 0.1 );
168                 cairo_arc (cr, 0, 0, center_radius-1, 0, 2.0*G_PI);
169                 cairo_fill (cr);
170                 cairo_restore(cr);
171
172                 //inner circle
173                 ArdourCanvas::set_source_rgba(cr, knob_color);
174                 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
175                 cairo_fill (cr);
176                 
177                 //gradient      
178                 if (bevel) {
179                         //knob gradient
180                         shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0,  yc);  //note we have to offset the gradient from our centerpoint
181                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
182                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.2, 1,1,1, 0.2);
183                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.8, 0,0,0, 0.2);
184                         cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.2);
185                         cairo_set_source (cr, shade_pattern);
186                         cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
187                         cairo_fill (cr);
188                         cairo_pattern_destroy (shade_pattern);
189
190                         //flat top over beveled edge
191                         ArdourCanvas::set_source_rgb_a (cr, knob_color, 0.5 );
192                         cairo_arc (cr, 0, 0, center_radius-pointer_thickness, 0, 2.0*G_PI);
193                         cairo_fill (cr);
194                 } else {        
195                         //radial gradient
196                         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
197                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
198                         cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.3);
199                         cairo_set_source (cr, shade_pattern);
200                         cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
201                         cairo_fill (cr);
202                         cairo_pattern_destroy (shade_pattern);
203                 }
204                 
205         } else {
206                 //inner circle
207                 ArdourCanvas::set_source_rgba(cr, knob_color);
208                 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
209                 cairo_fill (cr);
210         }
211         
212
213         //black knob border
214         cairo_set_line_width (cr, border_width);
215         cairo_set_source_rgba (cr, 0,0,0, 1 );
216         cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
217         cairo_stroke (cr);
218                         
219         //line shadow
220         if (!flat) {
221                 cairo_save(cr);
222                 cairo_translate(cr, 1, 1 );
223                 cairo_set_source_rgba (cr, 0,0,0,0.3 );
224                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
225                 cairo_set_line_width (cr, pointer_thickness);
226                 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
227                 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
228                 cairo_stroke (cr);
229                 cairo_restore(cr);
230         }
231         
232         //line
233         cairo_set_source_rgba (cr, 1,1,1, 1 );
234         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
235         cairo_set_line_width (cr, pointer_thickness);
236         cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
237         cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
238         cairo_stroke (cr);
239
240         //highlight if grabbed or if mouse is hovering over me
241         if ( _grabbed || (_hovering && ARDOUR::Config->get_widget_prelight() ) ) {
242                 cairo_set_source_rgba (cr, 1,1,1, 0.12 );
243                 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
244                 cairo_fill (cr);
245         }
246         
247         cairo_identity_matrix(cr);      
248 }
249
250 void
251 ArdourKnob::on_size_request (Gtk::Requisition* req)
252 {
253         CairoWidget::on_size_request (req);
254         
255         //perhaps render the knob base into a cached image here?
256 }
257
258 bool
259 ArdourKnob::on_scroll_event (GdkEventScroll* ev)
260 {
261         /* mouse wheel */
262
263         float scale = 0.05;  //by default, we step in 1/20ths of the knob travel
264         if (ev->state & Keyboard::GainFineScaleModifier) {
265                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
266                         scale *= 0.01;
267                 } else {
268                         scale *= 0.10;
269                 }
270         }
271
272         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
273         if (c) {
274                 float val = c->get_interface();
275         
276                 if ( ev->direction == GDK_SCROLL_UP )
277                         val += scale;  
278                 else
279                         val -= scale;                   
280
281                 c->set_interface(val);
282         }
283
284         return true;
285 }
286
287 bool
288 ArdourKnob::on_motion_notify_event (GdkEventMotion *ev) 
289 {
290         //scale the adjustment based on keyboard modifiers
291         float scale = 0.0025;
292         if (ev->state & Keyboard::GainFineScaleModifier) {
293                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
294                         scale *= 0.01;
295                 } else {
296                         scale *= 0.10;
297                 }
298         }
299
300         //calculate the travel of the mouse
301         int y_delta = 0;
302         if (ev->state & Gdk::BUTTON1_MASK) {
303                 y_delta = _grabbed_y - ev->y;
304                 _grabbed_y = ev->y;
305                 if (y_delta == 0) return TRUE;
306         }
307
308         //step the value of the controllable
309         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
310         if (c) {
311                 float val = c->get_interface();
312                 val += y_delta * scale;
313                 c->set_interface(val);
314         }
315
316         return true;
317 }
318
319 bool
320 ArdourKnob::on_button_press_event (GdkEventButton *ev)
321 {
322         _grabbed_y = ev->y;
323         _grabbed = true;
324         
325         set_active_state (Gtkmm2ext::ExplicitActive);
326
327         if (binding_proxy.button_press_handler (ev)) {
328                 return true;
329         }
330
331         return false;
332 }
333
334 bool
335 ArdourKnob::on_button_release_event (GdkEventButton *ev)
336 {
337         if ( (_grabbed_y == ev->y) && Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {  //no move, shift-click sets to default
338                 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
339                 if (!c) return false;
340                 c->set_value (c->normal());
341         return true;
342         }
343
344         _grabbed = false;
345         unset_active_state ();
346
347         return false;
348 }
349
350 void
351 ArdourKnob::color_handler ()
352 {
353         set_dirty ();
354 }
355
356 void
357 ArdourKnob::on_size_allocate (Allocation& alloc)
358 {
359         CairoWidget::on_size_allocate (alloc);
360 }
361
362 void
363 ArdourKnob::set_controllable (boost::shared_ptr<Controllable> c)
364 {
365     watch_connection.disconnect ();  //stop watching the old controllable
366
367         if (!c) return;
368
369         binding_proxy.set_controllable (c);
370
371         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourKnob::controllable_changed, this), gui_context());
372
373         controllable_changed();
374 }
375
376 void
377 ArdourKnob::controllable_changed ()
378 {
379         _val = binding_proxy.get_controllable()->get_interface();  //% of knob travel
380
381         _val = min( max(0.0f, _val), 1.0f);  //range check
382
383         set_dirty();
384 }
385
386 void
387 ArdourKnob::on_style_changed (const RefPtr<Gtk::Style>&)
388 {
389         set_dirty ();
390 }
391
392 void
393 ArdourKnob::on_name_changed ()
394 {
395         set_dirty ();
396 }
397
398
399 void
400 ArdourKnob::set_active_state (Gtkmm2ext::ActiveState s)
401 {
402         if (_active_state != s)
403                 CairoWidget::set_active_state (s);
404 }
405         
406 void
407 ArdourKnob::set_visual_state (Gtkmm2ext::VisualState s)
408 {
409         if (_visual_state != s)
410                 CairoWidget::set_visual_state (s);
411 }
412         
413
414 bool
415 ArdourKnob::on_focus_in_event (GdkEventFocus* ev)
416 {
417         set_dirty ();
418         return CairoWidget::on_focus_in_event (ev);
419 }
420
421 bool
422 ArdourKnob::on_focus_out_event (GdkEventFocus* ev)
423 {
424         set_dirty ();
425         return CairoWidget::on_focus_out_event (ev);
426 }
427
428 bool
429 ArdourKnob::on_enter_notify_event (GdkEventCrossing* ev)
430 {
431         _hovering = true;
432
433         set_dirty ();
434
435         return CairoWidget::on_enter_notify_event (ev);
436 }
437
438 bool
439 ArdourKnob::on_leave_notify_event (GdkEventCrossing* ev)
440 {
441         _hovering = false;
442
443         set_dirty ();
444
445         return CairoWidget::on_leave_notify_event (ev);
446 }
447
448 void
449 ArdourKnob::set_elements (Element e)
450 {
451         _elements = e;
452 }
453
454 void
455 ArdourKnob::add_elements (Element e)
456 {
457         _elements = (ArdourKnob::Element) (_elements | e);
458 }