fix issue with color and position of mixer strip plugin control sliders (#5275)
[ardour.git] / libs / gtkmm2ext / pixfader.cc
1 /*
2     Copyright (C) 2006 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     $Id: fastmeter.h 570 2006-06-07 21:21:21Z sampo $
19 */
20
21
22 #include <iostream>
23
24 #include "gtkmm2ext/pixfader.h"
25 #include "gtkmm2ext/keyboard.h"
26 #include "gtkmm2ext/rgb_macros.h"
27 #include "gtkmm2ext/utils.h"
28
29 using namespace Gtkmm2ext;
30 using namespace Gtk;
31 using namespace std;
32
33 #define CORNER_RADIUS 4
34 #define FADER_RESERVE (2*CORNER_RADIUS)
35
36 std::list<PixFader::FaderImage*> PixFader::_patterns;
37
38 PixFader::PixFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
39         : adjustment (adj)
40         , span (fader_length)
41         , girth (fader_girth)
42         , _orien (orientation)
43         , pattern (0)
44         , _hovering (false)
45         , last_drawn (-1)
46         , dragging (false)
47 {
48         default_value = adjustment.get_value();
49         update_unity_position ();
50
51         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK|Gdk::SCROLL_MASK|Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
52
53         adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
54         adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
55 }
56
57 PixFader::~PixFader ()
58 {
59 }
60
61 cairo_pattern_t*
62 PixFader::find_pattern (double afr, double afg, double afb, 
63                         double abr, double abg, double abb, 
64                         int w, int h)
65 {
66         for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
67                 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
68                         return (*f)->pattern;
69                 }
70         }
71         return 0;
72 }
73
74 void
75 PixFader::create_patterns ()
76 {
77         Gdk::Color c = get_style()->get_fg (get_state());
78         float fr, fg, fb;
79         float br, bg, bb;
80
81         fr = c.get_red_p ();
82         fg = c.get_green_p ();
83         fb = c.get_blue_p ();
84
85         c = get_style()->get_bg (get_state());
86
87         br = c.get_red_p ();
88         bg = c.get_green_p ();
89         bb = c.get_blue_p ();
90
91         if ( !_text.empty()) {
92                 _layout->get_pixel_size (_text_width, _text_height);
93         } else {
94                 _text_width = 0;
95                 _text_height = 0;
96         }
97
98         c = get_style()->get_text (get_state());
99
100         text_r = c.get_red_p ();
101         text_g = c.get_green_p ();
102         text_b = c.get_blue_p ();
103
104         cairo_surface_t* surface;
105         cairo_t* tc = 0;
106         float radius = CORNER_RADIUS;
107
108         double w = get_width();
109
110         if ((pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
111                 /* found it - use it */
112                 return;
113         }
114
115         if (_orien == VERT) {
116                 
117                 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
118                 tc = cairo_create (surface);
119
120                 /* paint background + border */
121
122                 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
123                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
124                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
125                 cairo_set_source (tc, shade_pattern);
126                 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
127                 cairo_fill (tc);
128
129                 cairo_pattern_destroy (shade_pattern);
130                 
131                 /* paint lower shade */
132                 
133                 w -= 2.0;
134
135                 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, w, 0);
136                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
137                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
138                 cairo_set_source (tc, shade_pattern);
139                 Gtkmm2ext::rounded_top_half_rectangle (tc, 1.0, get_height(), w, get_height(), radius-1.5);
140                 cairo_fill (tc);
141
142                 cairo_pattern_destroy (shade_pattern);
143
144                 pattern = cairo_pattern_create_for_surface (surface);
145
146         } else {
147
148                 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
149                 tc = cairo_create (surface);
150
151                 /* paint right shade (background section)*/
152
153                 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
154                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
155                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
156                 cairo_set_source (tc, shade_pattern);
157                 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
158                 cairo_fill (tc);
159
160                 /* paint left shade (active section/foreground) */
161                 
162                 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
163                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
164                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
165                 cairo_set_source (tc, shade_pattern);
166                 Gtkmm2ext::rounded_right_half_rectangle (tc, 0, 1, get_width(), get_height() - 2.0, radius-1.5);
167                 cairo_fill (tc);
168                 cairo_pattern_destroy (shade_pattern);
169                 
170                 pattern = cairo_pattern_create_for_surface (surface);
171         }
172
173         /* cache it for others to use */
174
175         _patterns.push_back (new FaderImage (pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
176
177         cairo_destroy (tc);
178         cairo_surface_destroy (surface);
179 }
180
181 bool
182 PixFader::on_expose_event (GdkEventExpose* ev)
183 {
184         Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
185         cairo_t* cr = context->cobj();
186
187         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
188         cairo_clip (cr);
189
190         if (!pattern) {
191                 create_patterns();
192         }
193         
194         int ds = display_span ();
195         float w = get_width();
196         float h = get_height();
197
198         cairo_matrix_t matrix;
199
200         if (_orien == VERT) {
201
202                 if (ds > h - FADER_RESERVE) {
203                         ds = h - FADER_RESERVE;
204                 }
205
206                 cairo_set_source (cr, pattern);
207                 cairo_matrix_init_translate (&matrix, 0, (h - ds));
208                 cairo_pattern_set_matrix (pattern, &matrix);
209                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
210                 cairo_fill (cr);
211
212         } else {
213
214                 if (ds < FADER_RESERVE) {
215                         ds = FADER_RESERVE;
216                 }
217
218                 /*
219                   if ds == w, the pattern does not need to be translated
220                   if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
221                       w to the left, which is -w in pattern space, and w in
222                       user space
223                   if ds == 10, then the pattern needs to be moved w - 10
224                       to the left, which is -(w-10) in pattern space, which 
225                       is (w - 10) in user space
226
227                   thus: translation = (w - ds)
228                  */
229
230                 cairo_set_source (cr, pattern);
231                 cairo_matrix_init_translate (&matrix, w - ds, 0);
232                 cairo_pattern_set_matrix (pattern, &matrix);
233                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
234                 cairo_fill (cr);
235         }
236                 
237         /* draw the unity-position line if it's not at either end*/
238         if (unity_loc > 0) {
239                 if ( _orien == VERT) {
240                         if (unity_loc < h ) {
241                                         context->set_line_width (1); 
242                                         context->set_source_rgb (0.0, 1.0, 0.0);
243                                         context->move_to (1, unity_loc);
244                                         context->line_to (girth - 2.0, unity_loc);
245                                         context->stroke ();
246                         }
247                 } else {
248                         if ( unity_loc < w ){
249                                 context->set_line_width (1); 
250                                 context->set_source_rgb (0.0, 1.0, 0.0);
251                                 context->move_to (unity_loc, 1);
252                                 context->line_to (unity_loc, girth - 2.0);
253                                 context->stroke ();
254                         }
255                 }
256         }
257         
258         if ( !_text.empty() ) {
259
260                 cairo_new_path (cr);    
261
262                 /* center text */
263                 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
264                 cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
265                 pango_cairo_show_layout (cr, _layout->gobj());
266         } 
267         
268 //      if (Config->get_widget_prelight()) {  //pixfader does not have access to config
269                 if (_hovering) {
270                         Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
271                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
272                         cairo_fill (cr);
273                 }
274 //      }
275
276         last_drawn = ds;
277
278         return true;
279 }
280
281 void
282 PixFader::on_size_request (GtkRequisition* req)
283 {
284         if (_orien == VERT) {
285                 req->width = girth;
286                 req->height = span;
287         } else {
288                 req->height = girth;
289                 req->width = span;
290         }
291 }
292
293 void
294 PixFader::on_size_allocate (Gtk::Allocation& alloc)
295 {
296         DrawingArea::on_size_allocate(alloc);
297
298         if (_orien == VERT) {
299                 girth = alloc.get_width ();
300                 span = alloc.get_height ();
301         } else {
302                 girth = alloc.get_height ();
303                 span = alloc.get_width ();
304         }
305
306         update_unity_position ();
307
308         if (is_realized()) {
309                 create_patterns();
310                 queue_draw ();
311         }
312 }
313
314 bool
315 PixFader::on_button_press_event (GdkEventButton* ev)
316 {
317         if (ev->type != GDK_BUTTON_PRESS) {
318                 return true;
319         }
320
321         if (ev->button != 1 && ev->button != 2) {
322                 return false;
323         }
324
325         add_modal_grab ();
326         grab_loc = (_orien == VERT) ? ev->y : ev->x;
327         grab_start = (_orien == VERT) ? ev->y : ev->x;
328         grab_window = ev->window;
329         dragging = true;
330         gdk_pointer_grab(ev->window,false,
331                         GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
332                         NULL,NULL,ev->time);
333
334         if (ev->button == 2) {
335                 set_adjustment_from_event (ev);
336         }
337         
338         return true;
339 }
340
341 bool
342 PixFader::on_button_release_event (GdkEventButton* ev)
343 {
344         double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
345         
346         switch (ev->button) {
347         case 1:
348                 if (dragging) {
349                         remove_modal_grab();
350                         dragging = false;
351                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
352
353                         if (!_hovering) {
354                                 Keyboard::magic_widget_drop_focus();
355                                 queue_draw ();
356                         }
357
358                         if (ev_pos == grab_start) {
359
360                                 /* no motion - just a click */
361
362                                 if (ev->state & Keyboard::TertiaryModifier) {
363                                         adjustment.set_value (default_value);
364                                 } else if (ev->state & Keyboard::GainFineScaleModifier) {
365                                         adjustment.set_value (adjustment.get_lower());
366                                 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
367                                         /* above the current display height, remember X Window coords */
368                                         adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
369                                 } else {
370                                         adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
371                                 }
372                         }
373                         return true;
374                 } 
375                 break;
376                 
377         case 2:
378                 if (dragging) {
379                         remove_modal_grab();
380                         dragging = false;
381                         set_adjustment_from_event (ev);
382                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
383                         return true;
384                 }
385                 break;
386
387         default:
388                 break;
389         }
390
391         return false;
392 }
393
394 bool
395 PixFader::on_scroll_event (GdkEventScroll* ev)
396 {
397         double scale;
398         bool ret = false;
399
400         if (ev->state & Keyboard::GainFineScaleModifier) {
401                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
402                         scale = 0.01;
403                 } else {
404                         scale = 0.05;
405                 }
406         } else {
407                 scale = 0.25;
408         }
409
410         if (_orien == VERT) {
411
412                 /* should left/right scroll affect vertical faders ? */
413
414                 switch (ev->direction) {
415
416                 case GDK_SCROLL_UP:
417                         adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
418                         ret = true;
419                         break;
420                 case GDK_SCROLL_DOWN:
421                         adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
422                         ret = true;
423                         break;
424                 default:
425                         break;
426                 }
427         } else {
428
429                 /* up/down scrolls should definitely affect horizontal faders
430                    because they are so much easier to use
431                 */
432
433                 switch (ev->direction) {
434
435                 case GDK_SCROLL_RIGHT:
436                 case GDK_SCROLL_UP:
437                         adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
438                         ret = true;
439                         break;
440                 case GDK_SCROLL_LEFT:
441                 case GDK_SCROLL_DOWN:
442                         adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
443                         ret = true;
444                         break;
445                 default:
446                         break;
447                 }
448         }
449         return ret;
450 }
451
452 bool
453 PixFader::on_motion_notify_event (GdkEventMotion* ev)
454 {
455         if (dragging) {
456                 double scale = 1.0;
457                 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
458                 
459                 if (ev->window != grab_window) {
460                         grab_loc = ev_pos;
461                         grab_window = ev->window;
462                         return true;
463                 }
464                 
465                 if (ev->state & Keyboard::GainFineScaleModifier) {
466                         if (ev->state & Keyboard::GainExtraFineScaleModifier) {
467                                 scale = 0.05;
468                         } else {
469                                 scale = 0.1;
470                         }
471                 }
472
473                 double const delta = ev_pos - grab_loc;
474                 grab_loc = ev_pos;
475
476                 double fract = (delta / span);
477
478                 fract = min (1.0, fract);
479                 fract = max (-1.0, fract);
480
481                 // X Window is top->bottom for 0..Y
482                 
483                 if (_orien == VERT) {
484                         fract = -fract;
485                 }
486
487                 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
488         }
489
490         return true;
491 }
492
493 void
494 PixFader::adjustment_changed ()
495 {
496         if (display_span() != last_drawn) {
497                 queue_draw ();
498         }
499 }
500
501 /** @return pixel offset of the current value from the right or bottom of the fader */
502 int
503 PixFader::display_span ()
504 {
505         float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
506         int ds;
507         if (_orien == VERT) {
508                 ds = (int)floor ( span * (1.0 - fract));
509         } else {
510                 ds = (int)floor (span * fract);
511         }
512         
513         return ds;
514 }
515
516 void
517 PixFader::set_fader_length (int l)
518 {
519         span = l;
520         update_unity_position ();
521         queue_resize ();
522 }
523
524 void
525 PixFader::update_unity_position ()
526 {
527         if (_orien == VERT) {
528                 unity_loc = (int) rint (span * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
529         } else {
530                 unity_loc = (int) rint (default_value * span / (adjustment.get_upper() - adjustment.get_lower()));
531         }
532
533         queue_draw ();
534 }
535
536 bool
537 PixFader::on_enter_notify_event (GdkEventCrossing*)
538 {
539         _hovering = true;
540         Keyboard::magic_widget_grab_focus ();
541         queue_draw ();
542         return false;
543 }
544
545 bool
546 PixFader::on_leave_notify_event (GdkEventCrossing*)
547 {
548         if (!dragging) {
549                 _hovering = false;
550                 Keyboard::magic_widget_drop_focus();
551                 queue_draw ();
552         }
553         return false;
554 }
555
556 void
557 PixFader::set_adjustment_from_event (GdkEventButton* ev)
558 {
559         double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
560
561         fract = min (1.0, fract);
562         fract = max (0.0, fract);
563
564         adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
565 }
566
567 void
568 PixFader::set_default_value (float d)
569 {
570         default_value = d;
571         update_unity_position ();
572 }
573
574 void
575 PixFader::set_text (const std::string& str)
576 {
577         _text = str;
578
579         if (!_layout && !_text.empty()) {
580                 _layout = Pango::Layout::create (get_pango_context());
581         } 
582
583         if (_layout) {
584                 _layout->set_text (str);
585                 _layout->get_pixel_size (_text_width, _text_height);
586         }
587
588         queue_resize ();
589 }
590
591 void
592 PixFader::on_state_changed (Gtk::StateType old_state)
593 {
594         Widget::on_state_changed (old_state);
595         create_patterns ();
596 }