don't cache width & height for CairoWidget; provide option for all CairoWidgets to...
[ardour.git] / gtk2_ardour / ardour_button.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
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
31 #include "gtkmm2ext/gui_thread.h"
32
33 #include "ardour/rc_configuration.h" // for widget prelight preference
34
35 #include "ardour_button.h"
36 #include "ardour_ui.h"
37 #include "global_signals.h"
38
39 #include "i18n.h"
40
41 using namespace Gdk;
42 using namespace Gtk;
43 using namespace Glib;
44 using namespace PBD;
45 using std::max;
46 using std::min;
47 using namespace std;
48
49 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
50 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
51 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
52
53 ArdourButton::ArdourButton (Element e)
54         : _elements (e)
55         , _tweaks (Tweaks (0))
56         , _act_on_release (true)
57         , _text_width (0)
58         , _text_height (0)
59         , _diameter (11.0)
60         , _corner_radius (9.0)
61         , edge_pattern (0)
62         , fill_pattern (0)
63         , led_inset_pattern (0)
64         , reflection_pattern (0)
65         , _led_left (false)
66         , _fixed_diameter (true)
67         , _distinct_led_click (false)
68         , _led_rect (0)
69         , _hovering (false)
70 {
71         ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
72 }
73
74 ArdourButton::ArdourButton (const std::string& str, Element e)
75         : _elements (e)
76         , _act_on_release (true)
77         , _text_width (0)
78         , _text_height (0)
79         , _diameter (11.0)
80         , _corner_radius (9.0)
81         , edge_pattern (0)
82         , fill_pattern (0)
83         , led_inset_pattern (0)
84         , reflection_pattern (0)
85         , _led_left (false)
86         , _fixed_diameter (true)
87         , _distinct_led_click (false)
88         , _led_rect (0)
89         , _hovering (false)
90 {
91         set_text (str);
92 }
93
94 ArdourButton::~ArdourButton()
95 {
96         delete _led_rect;
97 }
98
99 void
100 ArdourButton::set_text (const std::string& str)
101 {
102         _text = str;
103
104         if (!_layout && !_text.empty()) {
105                 _layout = Pango::Layout::create (get_pango_context());
106         } 
107
108         if (_layout) {
109                 _layout->set_text (str);
110         }
111
112         queue_resize ();
113 }
114
115 void
116 ArdourButton::set_markup (const std::string& str)
117 {
118         _text = str;
119
120         if (!_layout) {
121                 _layout = Pango::Layout::create (get_pango_context());
122         } 
123
124         _layout->set_text (str);
125         queue_resize ();
126 }
127
128 void
129 ArdourButton::render (cairo_t* cr)
130 {
131         if (!_fixed_diameter) {
132                 _diameter = std::min (get_width(), get_height());
133         }
134
135         if (_elements & Edge) {
136                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), _corner_radius);
137                 cairo_set_source (cr, edge_pattern);
138                 cairo_fill (cr);
139         }
140
141         if (_elements & Body) {
142                 if (_elements & Edge) {
143                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.0);
144                 } else {
145                         Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), _corner_radius - 1.0);
146                 }
147                 cairo_set_source (cr, fill_pattern);
148                 cairo_fill (cr);
149         }
150
151         if (_pixbuf) {
152
153                 double x,y;
154                 x = (get_width() - _pixbuf->get_width())/2.0;
155                 y = (get_height() - _pixbuf->get_height())/2.0;
156
157                 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
158                 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
159                 cairo_fill (cr);
160         }
161
162         /* text, if any */
163
164         int text_margin;
165
166         if (get_width() < 75) {
167                 text_margin = 3;
168         } else {
169                 text_margin = 10;
170         }
171
172         if ((_elements & Text) && !_text.empty()) {
173
174                 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
175
176                 if (_elements & Indicator) {
177                         if (_led_left) {
178                                 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
179                         } else {
180                                 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
181                         }
182                 } else {
183                         /* center text */
184                         cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
185                 }
186
187                 pango_cairo_show_layout (cr, _layout->gobj());
188         } 
189
190         if (_elements & Indicator) {
191
192                 /* move to the center of the indicator/led */
193
194                 cairo_save (cr);
195
196                 if (_elements & Text) {
197                         if (_led_left) {
198                                 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
199                         } else {
200                                 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
201                         }
202                 } else {
203                         cairo_translate (cr, get_width()/2.0, get_height()/2.0);
204                 }
205                 
206                 //inset
207                 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
208                 cairo_set_source (cr, led_inset_pattern);
209                 cairo_fill (cr);
210                 
211                 //black ring
212                 cairo_set_source_rgb (cr, 0, 0, 0);
213                 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
214                 cairo_fill(cr);
215                 
216                 //led color
217                 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
218                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
219                 cairo_fill(cr);
220                 
221                 //reflection
222                 cairo_scale(cr, 0.7, 0.7);
223                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
224                 cairo_set_source (cr, reflection_pattern);
225                 cairo_fill (cr);
226
227                 cairo_restore (cr);
228
229         }
230
231
232         /* a partially transparent gray layer to indicate insensitivity */
233
234         if ((visual_state() & Gtkmm2ext::Insensitive)) {
235                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), _corner_radius);
236                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
237                 cairo_fill (cr);
238         }
239
240         /* if requested, show hovering */
241         
242         if (ARDOUR::Config->get_widget_prelight()) {
243                 if (_hovering) {
244                         Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), _corner_radius);
245                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
246                         cairo_fill (cr);
247                 }
248         }
249 }
250
251 void
252 ArdourButton::set_diameter (float d)
253 {
254         _diameter = (d*2) + 5.0;
255
256         if (_diameter != 0.0) {
257                 _fixed_diameter = true;
258         }
259
260         set_colors ();
261 }
262
263 void
264 ArdourButton::set_corner_radius (float r)
265 {
266         _corner_radius = r;
267         set_dirty ();
268 }
269
270 void
271 ArdourButton::on_size_request (Gtk::Requisition* req)
272 {
273         int xpad = 0;
274         int ypad = 6;
275
276         CairoWidget::on_size_request (req);
277
278         if ((_elements & Text) && !_text.empty()) {
279                 _layout->get_pixel_size (_text_width, _text_height);
280                 if (_text_width + _diameter < 75) {
281                         xpad = 7;
282                 } else {
283                         xpad = 20;
284                 }
285         } else {
286                 _text_width = 0;
287                 _text_height = 0;
288         }
289
290         if (_pixbuf) {
291                 xpad = 6;
292         }
293
294         if ((_elements & Indicator) && _fixed_diameter) {
295                 if (_pixbuf) {
296                         req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
297                         req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
298                 } else {
299                         req->width = _text_width + lrint (_diameter) + xpad;
300                         req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
301                 }
302         } else {
303                 if (_pixbuf) {
304                         req->width = _pixbuf->get_width() + xpad;
305                         req->height = _pixbuf->get_height() + ypad;
306                 }  else {
307                         req->width = _text_width + xpad;
308                         req->height = _text_height + ypad;
309                 }
310         }
311 }
312
313 void
314 ArdourButton::set_colors ()
315 {
316         uint32_t start_color;
317         uint32_t end_color;
318         uint32_t r, g, b, a;
319         uint32_t text_color;
320         uint32_t led_color;
321
322         /* we use the edge of the button to show Selected state, so the
323          * color/pattern used there will vary depending on that
324          */
325         
326         if (edge_pattern) {
327                 cairo_pattern_destroy (edge_pattern);
328         }
329
330         if (_elements & Edge) {
331
332                 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
333                 if (visual_state() & Gtkmm2ext::Selected) {
334                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start selected", get_name()));
335                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end selected", get_name()));
336                 } else {
337                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start", get_name()));
338                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end", get_name()));
339                 }
340                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
341                 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
342                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
343                 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
344         }
345
346
347         /* the fill pattern is used to indicate Normal/Active/Mid state
348          */
349
350         if (fill_pattern) {
351                 cairo_pattern_destroy (fill_pattern);
352         }
353
354         if (_elements & Body) {
355                 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
356                 
357                 if (active_state() == Gtkmm2ext::Mid) {
358                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start mid", get_name()));
359                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end mid", get_name()));
360                 } else if (active_state() == Gtkmm2ext::Active) {
361                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
362                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
363                 } else {
364                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
365                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
366                 }
367                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
368                 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
369                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
370                 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
371         }
372
373         if (led_inset_pattern) {
374                 cairo_pattern_destroy (led_inset_pattern);
375         }
376         
377         if (reflection_pattern) {
378                 cairo_pattern_destroy (reflection_pattern);
379         }
380
381         if (_elements & Indicator) {
382                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
383                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
384                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
385
386                 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
387                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
388                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
389         }
390         
391         /* text and LED colors depend on Active/Normal/Mid */
392
393         if (active_state() == Gtkmm2ext::Active) {
394                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
395                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
396         } else if (active_state() == Gtkmm2ext::Mid) {
397                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text mid", get_name()));
398                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led mid", get_name()));
399         } else {
400                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
401                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
402         }
403
404         UINT_TO_RGBA (text_color, &r, &g, &b, &a);
405         text_r = r/255.0;
406         text_g = g/255.0;
407         text_b = b/255.0;
408         text_a = a/255.0;
409         UINT_TO_RGBA (led_color, &r, &g, &b, &a);
410         led_r = r/255.0;
411         led_g = g/255.0;
412         led_b = b/255.0;
413         led_a = a/255.0;
414
415         set_dirty ();
416 }
417
418 void
419 ArdourButton::set_led_left (bool yn)
420 {
421         _led_left = yn;
422 }
423
424 bool
425 ArdourButton::on_button_press_event (GdkEventButton *ev)
426 {
427         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
428                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
429                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
430                         return true;
431                 }
432         }
433
434         if (_tweaks & ShowClick) {
435                 set_active_state (Gtkmm2ext::Active);
436         }
437
438         if (binding_proxy.button_press_handler (ev)) {
439                 return true;
440         }
441
442         if (!_act_on_release) {
443                 if (_action) {
444                         _action->activate ();
445                         return true;
446                 }
447         }
448
449         return false;
450 }
451
452 bool
453 ArdourButton::on_button_release_event (GdkEventButton *ev)
454 {
455         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
456                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
457                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
458                         signal_led_clicked(); /* EMIT SIGNAL */
459                         return true;
460                 }
461         }
462
463         if (_tweaks & ShowClick) {
464                 unset_active_state ();
465         }
466
467         signal_clicked ();
468
469         if (_act_on_release) {
470                 if (_action) {
471                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
472                         _action->activate ();
473                         return true;
474                 }
475         }
476
477
478         return false;
479 }
480
481 void
482 ArdourButton::set_distinct_led_click (bool yn)
483 {
484         _distinct_led_click = yn;
485         setup_led_rect ();
486 }
487
488 void
489 ArdourButton::color_handler ()
490 {
491         set_colors ();
492         set_dirty ();
493 }
494
495 void
496 ArdourButton::on_size_allocate (Allocation& alloc)
497 {
498         CairoWidget::on_size_allocate (alloc);
499         setup_led_rect ();
500         set_colors ();
501 }
502
503 void
504 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
505 {
506         watch_connection.disconnect ();
507         binding_proxy.set_controllable (c);
508 }
509
510 void
511 ArdourButton::watch ()
512 {
513         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
514
515         if (!c) {
516                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
517                 return;
518         }
519
520         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
521 }
522
523 void
524 ArdourButton::controllable_changed ()
525 {
526         float val = binding_proxy.get_controllable()->get_value();
527
528         if (fabs (val) >= 0.5f) {
529                 set_active_state (Gtkmm2ext::Active);
530         } else {
531                 unset_active_state ();
532         }
533 }
534
535 void
536 ArdourButton::set_related_action (RefPtr<Action> act)
537 {
538         _action = act;
539
540         if (_action) {
541
542                 string str = _action->property_tooltip().get_value();
543                 if (!str.empty()) {
544                         ARDOUR_UI::instance()->set_tip (*this, str);
545                 }
546
547                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
548                 if (tact) {
549                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
550                 } 
551
552                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
553                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
554                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
555         }
556 }
557
558 void
559 ArdourButton::action_toggled ()
560 {
561         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
562
563         if (tact) {
564                 if (tact->get_active()) {
565                         set_active_state (Gtkmm2ext::Active);
566                 } else {
567                         unset_active_state ();
568                 }
569         }
570 }       
571
572 void
573 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
574 {
575         set_colors ();
576 }
577
578 void
579 ArdourButton::setup_led_rect ()
580 {
581         int text_margin;
582
583         if (get_width() < 75) {
584                 text_margin = 3;
585         } else {
586                 text_margin = 10;
587         }
588
589         if (_elements & Indicator) {
590                 _led_rect = new cairo_rectangle_t;
591                 
592                 if (_elements & Text) {
593                         if (_led_left) {
594                                 _led_rect->x = text_margin;
595                         } else {
596                                 _led_rect->x = get_width() - text_margin - _diameter/2.0;
597                         }
598                 } else {
599                         /* centered */
600                         _led_rect->x = get_width()/2.0 - _diameter/2.0;
601                 }
602
603                 _led_rect->y = get_height()/2.0 - _diameter/2.0;
604                 _led_rect->width = _diameter;
605                 _led_rect->height = _diameter;
606
607         } else {
608                 delete _led_rect;
609                 _led_rect = 0;
610         }
611 }
612
613 void
614 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
615 {
616         _pixbuf = img;
617         queue_draw ();
618 }
619
620 void
621 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
622 {
623         bool changed = (_active_state != s);
624         CairoWidget::set_active_state (s);
625         if (changed) {
626                 set_colors ();
627         }
628 }
629         
630 void
631 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
632 {
633         bool changed = (_visual_state != s);
634         CairoWidget::set_visual_state (s);
635         if (changed) {
636                 set_colors ();
637         }
638 }
639         
640 bool
641 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
642 {
643         _hovering = true;
644
645         if (ARDOUR::Config->get_widget_prelight()) {
646                 queue_draw ();
647         }
648
649         return CairoWidget::on_enter_notify_event (ev);
650 }
651
652 bool
653 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
654 {
655         _hovering = false;
656
657         if (ARDOUR::Config->get_widget_prelight()) {
658                 queue_draw ();
659         }
660
661         return CairoWidget::on_leave_notify_event (ev);
662 }
663
664 void
665 ArdourButton::set_tweaks (Tweaks t)
666 {
667         if (_tweaks != t) {
668                 _tweaks = t;
669                 queue_draw ();
670         }
671 }
672
673 void
674 ArdourButton::action_sensitivity_changed ()
675 {
676         if (_action->property_sensitive ()) {
677                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
678         } else {
679                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
680         }
681         
682 }
683
684
685 void
686 ArdourButton::action_visibility_changed ()
687 {
688         if (_action->property_visible ()) {
689                 show ();
690         } else {
691                 hide ();
692         }
693 }
694
695 void
696 ArdourButton::action_tooltip_changed ()
697 {
698         string str = _action->property_tooltip().get_value();
699         ARDOUR_UI::instance()->set_tip (*this, str);
700 }
701