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