pixfaders should invalidate their patterns and layout on style change, helps fix...
[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 (w <= 1 || get_height() <= 1) {
111                 return;
112         }
113
114         if ((pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
115                 /* found it - use it */
116                 return;
117         }
118
119         if (_orien == VERT) {
120                 
121                 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
122                 tc = cairo_create (surface);
123
124                 /* paint background + border */
125
126                 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
127                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
128                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
129                 cairo_set_source (tc, shade_pattern);
130                 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
131                 cairo_fill (tc);
132
133                 cairo_pattern_destroy (shade_pattern);
134                 
135                 /* paint lower shade */
136                 
137                 w -= 2.0;
138
139                 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, w, 0);
140                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
141                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
142                 cairo_set_source (tc, shade_pattern);
143                 Gtkmm2ext::rounded_top_half_rectangle (tc, 1.0, get_height(), w, get_height(), radius-1.5);
144                 cairo_fill (tc);
145
146                 cairo_pattern_destroy (shade_pattern);
147
148                 pattern = cairo_pattern_create_for_surface (surface);
149
150         } else {
151
152                 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
153                 tc = cairo_create (surface);
154
155                 /* paint right shade (background section)*/
156
157                 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
158                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.8,bg*0.8,bb*0.8, 1.0);
159                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.6,bg*0.6,bb*0.6, 1.0);
160                 cairo_set_source (tc, shade_pattern);
161                 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
162                 cairo_fill (tc);
163
164                 /* paint left shade (active section/foreground) */
165                 
166                 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
167                 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
168                 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
169                 cairo_set_source (tc, shade_pattern);
170                 Gtkmm2ext::rounded_right_half_rectangle (tc, 0, 1, get_width(), get_height() - 2.0, radius-1.5);
171                 cairo_fill (tc);
172                 cairo_pattern_destroy (shade_pattern);
173                 
174                 pattern = cairo_pattern_create_for_surface (surface);
175         }
176
177         /* cache it for others to use */
178
179         _patterns.push_back (new FaderImage (pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
180
181         cairo_destroy (tc);
182         cairo_surface_destroy (surface);
183 }
184
185 bool
186 PixFader::on_expose_event (GdkEventExpose* ev)
187 {
188         Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
189         cairo_t* cr = context->cobj();
190
191         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
192         cairo_clip (cr);
193
194         if (!pattern) {
195                 create_patterns();
196         }
197
198         int ds = display_span ();
199         float w = get_width();
200         float h = get_height();
201
202         cairo_matrix_t matrix;
203
204         if (_orien == VERT) {
205
206                 if (ds > h - FADER_RESERVE) {
207                         ds = h - FADER_RESERVE;
208                 }
209
210                 cairo_set_source (cr, pattern);
211                 cairo_matrix_init_translate (&matrix, 0, (h - ds));
212                 cairo_pattern_set_matrix (pattern, &matrix);
213                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
214                 cairo_fill (cr);
215
216         } else {
217
218                 if (ds < FADER_RESERVE) {
219                         ds = FADER_RESERVE;
220                 }
221
222                 /*
223                   if ds == w, the pattern does not need to be translated
224                   if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
225                       w to the left, which is -w in pattern space, and w in
226                       user space
227                   if ds == 10, then the pattern needs to be moved w - 10
228                       to the left, which is -(w-10) in pattern space, which 
229                       is (w - 10) in user space
230
231                   thus: translation = (w - ds)
232                  */
233
234                 cairo_set_source (cr, pattern);
235                 cairo_matrix_init_translate (&matrix, w - ds, 0);
236                 cairo_pattern_set_matrix (pattern, &matrix);
237                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, w, h, CORNER_RADIUS-1.5);
238                 cairo_fill (cr);
239         }
240                 
241         /* draw the unity-position line if it's not at either end*/
242         if (unity_loc > 0) {
243                 if ( _orien == VERT) {
244                         if (unity_loc < h ) {
245                                         context->set_line_width (1); 
246                                         context->set_source_rgb (0.0, 1.0, 0.0);
247                                         context->move_to (1, unity_loc);
248                                         context->line_to (girth - 2.0, unity_loc);
249                                         context->stroke ();
250                         }
251                 } else {
252                         if ( unity_loc < w ){
253                                 context->set_line_width (1); 
254                                 context->set_source_rgb (0.0, 1.0, 0.0);
255                                 context->move_to (unity_loc, 1);
256                                 context->line_to (unity_loc, girth - 2.0);
257                                 context->stroke ();
258                         }
259                 }
260         }
261         
262         if ( !_text.empty() ) {
263
264                 cairo_new_path (cr);    
265
266                 /* center text */
267                 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
268                 cairo_set_source_rgba (cr, text_r, text_g, text_b, 0.9);
269                 pango_cairo_show_layout (cr, _layout->gobj());
270         } 
271         
272 //      if (Config->get_widget_prelight()) {  //pixfader does not have access to config
273                 if (_hovering) {
274                         Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3);
275                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
276                         cairo_fill (cr);
277                 }
278 //      }
279
280         last_drawn = ds;
281
282         return true;
283 }
284
285 void
286 PixFader::on_size_request (GtkRequisition* req)
287 {
288         if (_orien == VERT) {
289                 req->width = girth;
290                 req->height = span;
291         } else {
292                 req->height = girth;
293                 req->width = span;
294         }
295 }
296
297 void
298 PixFader::on_size_allocate (Gtk::Allocation& alloc)
299 {
300         DrawingArea::on_size_allocate(alloc);
301
302         if (_orien == VERT) {
303                 girth = alloc.get_width ();
304                 span = alloc.get_height ();
305         } else {
306                 girth = alloc.get_height ();
307                 span = alloc.get_width ();
308         }
309
310         update_unity_position ();
311 }
312
313 bool
314 PixFader::on_button_press_event (GdkEventButton* ev)
315 {
316         if (ev->type != GDK_BUTTON_PRESS) {
317                 return true;
318         }
319
320         if (ev->button != 1 && ev->button != 2) {
321                 return false;
322         }
323
324         add_modal_grab ();
325         grab_loc = (_orien == VERT) ? ev->y : ev->x;
326         grab_start = (_orien == VERT) ? ev->y : ev->x;
327         grab_window = ev->window;
328         dragging = true;
329         gdk_pointer_grab(ev->window,false,
330                         GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
331                         NULL,NULL,ev->time);
332
333         if (ev->button == 2) {
334                 set_adjustment_from_event (ev);
335         }
336         
337         return true;
338 }
339
340 bool
341 PixFader::on_button_release_event (GdkEventButton* ev)
342 {
343         double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
344         
345         switch (ev->button) {
346         case 1:
347                 if (dragging) {
348                         remove_modal_grab();
349                         dragging = false;
350                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
351
352                         if (!_hovering) {
353                                 Keyboard::magic_widget_drop_focus();
354                                 queue_draw ();
355                         }
356
357                         if (ev_pos == grab_start) {
358
359                                 /* no motion - just a click */
360
361                                 if (ev->state & Keyboard::TertiaryModifier) {
362                                         adjustment.set_value (default_value);
363                                 } else if (ev->state & Keyboard::GainFineScaleModifier) {
364                                         adjustment.set_value (adjustment.get_lower());
365                                 } else if ((_orien == VERT && ev_pos < display_span()) || (_orien == HORIZ && ev_pos > display_span())) {
366                                         /* above the current display height, remember X Window coords */
367                                         adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
368                                 } else {
369                                         adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
370                                 }
371                         }
372                         return true;
373                 } 
374                 break;
375                 
376         case 2:
377                 if (dragging) {
378                         remove_modal_grab();
379                         dragging = false;
380                         set_adjustment_from_event (ev);
381                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
382                         return true;
383                 }
384                 break;
385
386         default:
387                 break;
388         }
389
390         return false;
391 }
392
393 bool
394 PixFader::on_scroll_event (GdkEventScroll* ev)
395 {
396         double scale;
397         bool ret = false;
398
399         if (ev->state & Keyboard::GainFineScaleModifier) {
400                 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
401                         scale = 0.01;
402                 } else {
403                         scale = 0.05;
404                 }
405         } else {
406                 scale = 0.25;
407         }
408
409         if (_orien == VERT) {
410
411                 /* should left/right scroll affect vertical faders ? */
412
413                 switch (ev->direction) {
414
415                 case GDK_SCROLL_UP:
416                         adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
417                         ret = true;
418                         break;
419                 case GDK_SCROLL_DOWN:
420                         adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
421                         ret = true;
422                         break;
423                 default:
424                         break;
425                 }
426         } else {
427
428                 /* up/down scrolls should definitely affect horizontal faders
429                    because they are so much easier to use
430                 */
431
432                 switch (ev->direction) {
433
434                 case GDK_SCROLL_RIGHT:
435                 case GDK_SCROLL_UP:
436                         adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
437                         ret = true;
438                         break;
439                 case GDK_SCROLL_LEFT:
440                 case GDK_SCROLL_DOWN:
441                         adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
442                         ret = true;
443                         break;
444                 default:
445                         break;
446                 }
447         }
448         return ret;
449 }
450
451 bool
452 PixFader::on_motion_notify_event (GdkEventMotion* ev)
453 {
454         if (dragging) {
455                 double scale = 1.0;
456                 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
457                 
458                 if (ev->window != grab_window) {
459                         grab_loc = ev_pos;
460                         grab_window = ev->window;
461                         return true;
462                 }
463                 
464                 if (ev->state & Keyboard::GainFineScaleModifier) {
465                         if (ev->state & Keyboard::GainExtraFineScaleModifier) {
466                                 scale = 0.05;
467                         } else {
468                                 scale = 0.1;
469                         }
470                 }
471
472                 double const delta = ev_pos - grab_loc;
473                 grab_loc = ev_pos;
474
475                 double fract = (delta / span);
476
477                 fract = min (1.0, fract);
478                 fract = max (-1.0, fract);
479
480                 // X Window is top->bottom for 0..Y
481                 
482                 if (_orien == VERT) {
483                         fract = -fract;
484                 }
485
486                 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
487         }
488
489         return true;
490 }
491
492 void
493 PixFader::adjustment_changed ()
494 {
495         if (display_span() != last_drawn) {
496                 queue_draw ();
497         }
498 }
499
500 /** @return pixel offset of the current value from the right or bottom of the fader */
501 int
502 PixFader::display_span ()
503 {
504         float fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
505         int ds;
506         if (_orien == VERT) {
507                 ds = (int)floor ( span * (1.0 - fract));
508         } else {
509                 ds = (int)floor (span * fract);
510         }
511         
512         return ds;
513 }
514
515 void
516 PixFader::set_fader_length (int l)
517 {
518         span = l;
519         update_unity_position ();
520         queue_resize ();
521 }
522
523 void
524 PixFader::update_unity_position ()
525 {
526         if (_orien == VERT) {
527                 unity_loc = (int) rint (span * (1 - (default_value / (adjustment.get_upper() - adjustment.get_lower())))) - 1;
528         } else {
529                 unity_loc = (int) rint (default_value * span / (adjustment.get_upper() - adjustment.get_lower()));
530         }
531
532         queue_draw ();
533 }
534
535 bool
536 PixFader::on_enter_notify_event (GdkEventCrossing*)
537 {
538         _hovering = true;
539         Keyboard::magic_widget_grab_focus ();
540         queue_draw ();
541         return false;
542 }
543
544 bool
545 PixFader::on_leave_notify_event (GdkEventCrossing*)
546 {
547         if (!dragging) {
548                 _hovering = false;
549                 Keyboard::magic_widget_drop_focus();
550                 queue_draw ();
551         }
552         return false;
553 }
554
555 void
556 PixFader::set_adjustment_from_event (GdkEventButton* ev)
557 {
558         double fract = (_orien == VERT) ? (1.0 - (ev->y / span)) : (ev->x / span);
559
560         fract = min (1.0, fract);
561         fract = max (0.0, fract);
562
563         adjustment.set_value (fract * (adjustment.get_upper () - adjustment.get_lower ()));
564 }
565
566 void
567 PixFader::set_default_value (float d)
568 {
569         default_value = d;
570         update_unity_position ();
571 }
572
573 void
574 PixFader::set_text (const std::string& str)
575 {
576         _text = str;
577
578         if (!_layout && !_text.empty()) {
579                 _layout = Pango::Layout::create (get_pango_context());
580         } 
581
582         if (_layout) {
583                 _layout->set_text (str);
584                 _layout->get_pixel_size (_text_width, _text_height);
585         }
586
587         queue_resize ();
588 }
589
590 void
591 PixFader::on_state_changed (Gtk::StateType old_state)
592 {
593         Widget::on_state_changed (old_state);
594         create_patterns ();
595 }
596
597 void
598 PixFader::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
599 {
600         if (_layout) {
601                 std::string txt = _layout->get_text();
602                 _layout.clear (); // drop reference to existing layout
603                 set_text (txt);
604         }
605
606         /* remember that all patterns are cached and not owned by an individual
607            pixfader. we will lazily create a new pattern when needed.
608         */
609
610         pattern = 0;
611 }