96fd2bb96f80bb449775c3e8bbfa0c1c4f81027f
[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/colors.h"
42 #include "canvas/utils.h"
43
44 #include "i18n.h"
45
46 using namespace Gtkmm2ext;
47 using namespace Gdk;
48 using namespace Gtk;
49 using namespace Glib;
50 using namespace PBD;
51 using std::max;
52 using std::min;
53 using namespace std;
54
55 ArdourKnob::Element ArdourKnob::default_elements = ArdourKnob::Element (ArdourKnob::Arc);
56
57 ArdourKnob::ArdourKnob (Element e)
58         : _elements (e)
59         , _hovering (false)
60         , _tooltip (this)
61 {
62         ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourKnob::color_handler));
63 }
64
65 ArdourKnob::~ArdourKnob()
66 {
67 }
68
69 void
70 ArdourKnob::render (cairo_t* cr, cairo_rectangle_t *)
71 {
72         cairo_pattern_t* shade_pattern;
73         
74         float width = get_width();
75         float height = get_height();
76         
77         const float scale = min(width, height);
78         const float pointer_thickness = 3.0 * (scale/80);  //(if the knob is 80 pixels wide, we want a 3-pix line on it)
79         
80         float start_angle = ((180 - 65) * G_PI) / 180;
81         float end_angle = ((360 + 65) * G_PI) / 180;
82         float value_angle = start_angle + (_val * (end_angle - start_angle));
83         
84         float value_x = cos (value_angle);
85         float value_y = sin (value_angle);
86
87         float xc =  0.5 + width/ 2.0;
88         float yc = 0.5 + height/ 2.0;
89         
90         cairo_translate (cr, xc, yc);  //after this, everything is based on the center of the knob
91
92         //get the knob color from the theme
93         ArdourCanvas::Color knob_color = ARDOUR_UI::config()->color (string_compose ("%1", get_name()));
94
95         float center_radius = 0.48*scale;
96         float border_width = 0.8;
97         
98         bool arc = (_elements & Arc)==Arc;
99         bool bevel = (_elements & Bevel)==Bevel;
100         bool flat = _flat_buttons;
101         
102         if ( arc ) {
103                 center_radius = scale*0.33;
104
105                 float inner_progress_radius = scale*0.38;
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;
109                 
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);
114
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);
119                 cairo_stroke (cr);
120
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 ( string_compose ("%1: arc start", get_name()));
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 ( string_compose ("%1: arc end", get_name()) );
127                 ArdourCanvas::color_to_rgba( arc_end_color, red_end, green_end, blue_end, unused );
128
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;
133
134                 //draw the arc
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);
138                 cairo_stroke (cr);
139
140                 //shade the arc
141                 if (!flat) {
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);
148                         cairo_fill (cr);
149                         cairo_pattern_destroy (shade_pattern);
150                 }
151 #if 0 //black border
152                 cairo_set_source_rgb (cr, 0, 0, 0 );
153                 cairo_set_line_width (cr, border_width);
154                 cairo_move_to (cr, (outer_progress_radius * start_angle_x), (outer_progress_radius * start_angle_y));
155                 cairo_line_to (cr, (inner_progress_radius * start_angle_x), (inner_progress_radius * start_angle_y));
156                 cairo_stroke (cr);
157                 cairo_move_to (cr, (outer_progress_radius * end_angle_x), (outer_progress_radius * end_angle_y));
158                 cairo_line_to (cr, (inner_progress_radius * end_angle_x), (inner_progress_radius * end_angle_y));
159                 cairo_stroke (cr);
160                 cairo_arc (cr, 0, 0, outer_progress_radius, start_angle, end_angle);
161                 cairo_stroke (cr);
162 #endif
163         }
164         
165         if (!flat) {
166                 //knob shadow
167                 cairo_save(cr);
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);
171                 cairo_fill (cr);
172                 cairo_restore(cr);
173
174                 //inner circle
175                 ArdourCanvas::set_source_rgba(cr, knob_color);
176                 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
177                 cairo_fill (cr);
178                 
179                 //gradient      
180                 if (bevel) {
181                         //knob gradient
182                         shade_pattern = cairo_pattern_create_linear (0.0, -yc, 0.0,  yc);  //note we have to offset the gradient from our centerpoint
183                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
184                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.2, 1,1,1, 0.2);
185                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.8, 0,0,0, 0.2);
186                         cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.2);
187                         cairo_set_source (cr, shade_pattern);
188                         cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
189                         cairo_fill (cr);
190                         cairo_pattern_destroy (shade_pattern);
191
192                         //flat top over beveled edge
193                         ArdourCanvas::set_source_rgb_a (cr, knob_color, 0.5 );
194                         cairo_arc (cr, 0, 0, center_radius-pointer_thickness, 0, 2.0*G_PI);
195                         cairo_fill (cr);
196                 } else {        
197                         //radial gradient
198                         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
199                         cairo_pattern_add_color_stop_rgba (shade_pattern, 0.0, 1,1,1, 0.2);
200                         cairo_pattern_add_color_stop_rgba (shade_pattern, 1.0, 0,0,0, 0.3);
201                         cairo_set_source (cr, shade_pattern);
202                         cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
203                         cairo_fill (cr);
204                         cairo_pattern_destroy (shade_pattern);
205                 }
206                 
207         } else {
208                 //inner circle
209                 ArdourCanvas::set_source_rgba(cr, knob_color);
210                 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
211                 cairo_fill (cr);
212         }
213         
214
215         //black knob border
216         cairo_set_line_width (cr, border_width);
217         cairo_set_source_rgba (cr, 0,0,0, 1 );
218         cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
219         cairo_stroke (cr);
220                         
221         //line shadow
222         if (!flat) {
223                 cairo_save(cr);
224                 cairo_translate(cr, 1, 1 );
225                 cairo_set_source_rgba (cr, 0,0,0,0.3 );
226                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
227                 cairo_set_line_width (cr, pointer_thickness);
228                 cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
229                 cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
230                 cairo_stroke (cr);
231                 cairo_restore(cr);
232         }
233         
234         //line
235         cairo_set_source_rgba (cr, 1,1,1, 1 );
236         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
237         cairo_set_line_width (cr, pointer_thickness);
238         cairo_move_to (cr, (center_radius * value_x), (center_radius * value_y));
239         cairo_line_to (cr, ((center_radius*0.4) * value_x), ((center_radius*0.4) * value_y));
240         cairo_stroke (cr);
241
242         //highlight if grabbed or if mouse is hovering over me
243         if (_tooltip.dragging() || (_hovering && ARDOUR_UI::config()->get_widget_prelight() ) ) {
244                 cairo_set_source_rgba (cr, 1,1,1, 0.12 );
245                 cairo_arc (cr, 0, 0, center_radius, 0, 2.0*G_PI);
246                 cairo_fill (cr);
247         }
248         
249         cairo_identity_matrix(cr);      
250 }
251
252 void
253 ArdourKnob::on_size_request (Gtk::Requisition* req)
254 {
255         // see ardour-button VectorIcon size, use font scaling as default
256         CairoWidget::on_size_request (req); // allow to override
257
258         // we're square
259         if (req->width < req->height) {
260                 req->width = req->height;
261         }
262         if (req->height < req->width) {
263                 req->height = req->width;
264         }
265 }
266
267 bool
268 ArdourKnob::on_scroll_event (GdkEventScroll* ev)
269 {
270         /* mouse wheel */
271
272         float scale = 0.05;  //by default, we step in 1/20ths of the knob travel
273         if (ev->state & Keyboard::GainFineScaleModifier) {
274                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
275                         scale *= 0.01;
276                 } else {
277                         scale *= 0.10;
278                 }
279         }
280
281         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
282         if (c) {
283                 float val = c->get_interface();
284         
285                 if ( ev->direction == GDK_SCROLL_UP )
286                         val += scale;  
287                 else
288                         val -= scale;                   
289
290                 c->set_interface(val);
291         }
292
293         return true;
294 }
295
296 bool
297 ArdourKnob::on_motion_notify_event (GdkEventMotion *ev) 
298 {
299         //scale the adjustment based on keyboard modifiers
300         float scale = 0.0025;
301         if (ev->state & Keyboard::GainFineScaleModifier) {
302                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
303                         scale *= 0.01;
304                 } else {
305                         scale *= 0.10;
306                 }
307         }
308
309         //calculate the travel of the mouse
310         int y_delta = 0;
311         if (ev->state & Gdk::BUTTON1_MASK) {
312                 y_delta = _grabbed_y - ev->y;
313                 _grabbed_y = ev->y;
314                 if (y_delta == 0) return TRUE;
315         }
316
317         //step the value of the controllable
318         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
319         if (c) {
320                 float val = c->get_interface();
321                 val += y_delta * scale;
322                 c->set_interface(val);
323         }
324
325         return true;
326 }
327
328 bool
329 ArdourKnob::on_button_press_event (GdkEventButton *ev)
330 {
331         _grabbed_y = ev->y;
332         _tooltip.start_drag();
333         
334         set_active_state (Gtkmm2ext::ExplicitActive);
335
336         if (binding_proxy.button_press_handler (ev)) {
337                 return true;
338         }
339
340         return false;
341 }
342
343 bool
344 ArdourKnob::on_button_release_event (GdkEventButton *ev)
345 {
346         if ( (_grabbed_y == ev->y) && Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {  //no move, shift-click sets to default
347                 boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
348                 if (!c) return false;
349                 c->set_value (c->normal());
350         return true;
351         }
352
353         //_tooltip.target_stop_drag ();
354         _tooltip.stop_drag();
355         unset_active_state ();
356
357         return false;
358 }
359
360 void
361 ArdourKnob::color_handler ()
362 {
363         set_dirty ();
364 }
365
366 void
367 ArdourKnob::on_size_allocate (Allocation& alloc)
368 {
369         CairoWidget::on_size_allocate (alloc);
370 }
371
372 void
373 ArdourKnob::set_controllable (boost::shared_ptr<Controllable> c)
374 {
375     watch_connection.disconnect ();  //stop watching the old controllable
376
377         if (!c) return;
378
379         binding_proxy.set_controllable (c);
380
381         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourKnob::controllable_changed, this), gui_context());
382
383         controllable_changed();
384 }
385
386 void
387 ArdourKnob::controllable_changed ()
388 {
389         boost::shared_ptr<PBD::Controllable> c = binding_proxy.get_controllable();
390
391         _val = c->get_interface();  //% of knob travel
392         _val = min( max(0.0f, _val), 1.0f);  //range check
393
394         if (!_tooltip_prefix.empty()) {
395                 _tooltip.set_tip (_tooltip_prefix + c->get_user_string());
396         }
397         set_dirty();
398 }
399
400 void
401 ArdourKnob::on_style_changed (const RefPtr<Gtk::Style>&)
402 {
403         set_dirty ();
404 }
405
406 void
407 ArdourKnob::on_name_changed ()
408 {
409         set_dirty ();
410 }
411
412
413 void
414 ArdourKnob::set_active_state (Gtkmm2ext::ActiveState s)
415 {
416         if (_active_state != s)
417                 CairoWidget::set_active_state (s);
418 }
419         
420 void
421 ArdourKnob::set_visual_state (Gtkmm2ext::VisualState s)
422 {
423         if (_visual_state != s)
424                 CairoWidget::set_visual_state (s);
425 }
426         
427
428 bool
429 ArdourKnob::on_focus_in_event (GdkEventFocus* ev)
430 {
431         set_dirty ();
432         return CairoWidget::on_focus_in_event (ev);
433 }
434
435 bool
436 ArdourKnob::on_focus_out_event (GdkEventFocus* ev)
437 {
438         set_dirty ();
439         return CairoWidget::on_focus_out_event (ev);
440 }
441
442 bool
443 ArdourKnob::on_enter_notify_event (GdkEventCrossing* ev)
444 {
445         _hovering = true;
446
447         set_dirty ();
448
449         return CairoWidget::on_enter_notify_event (ev);
450 }
451
452 bool
453 ArdourKnob::on_leave_notify_event (GdkEventCrossing* ev)
454 {
455         _hovering = false;
456
457         set_dirty ();
458
459         return CairoWidget::on_leave_notify_event (ev);
460 }
461
462 void
463 ArdourKnob::set_elements (Element e)
464 {
465         _elements = e;
466 }
467
468 void
469 ArdourKnob::add_elements (Element e)
470 {
471         _elements = (ArdourKnob::Element) (_elements | e);
472 }
473
474
475 KnobPersistentTooltip::KnobPersistentTooltip (Gtk::Widget* w)
476         : PersistentTooltip (w)
477         , _dragging (false)
478 {
479 }
480
481 void
482 KnobPersistentTooltip::start_drag ()
483 {
484         _dragging = true;
485 }
486
487 void
488 KnobPersistentTooltip::stop_drag ()
489 {
490         _dragging = false;
491 }
492
493 bool
494 KnobPersistentTooltip::dragging () const
495 {
496         return _dragging;
497 }