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