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