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