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