ardour-button-ize zoom buttons; move MIDI panic button to transport bar
[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         if (_act_on_release) {
477                 if (_action) {
478                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
479                         _action->activate ();
480                         return true;
481                 }
482         }
483
484
485         return false;
486 }
487
488 void
489 ArdourButton::set_distinct_led_click (bool yn)
490 {
491         _distinct_led_click = yn;
492         setup_led_rect ();
493 }
494
495 void
496 ArdourButton::color_handler ()
497 {
498         set_colors ();
499         set_dirty ();
500 }
501
502 void
503 ArdourButton::on_size_allocate (Allocation& alloc)
504 {
505         CairoWidget::on_size_allocate (alloc);
506         setup_led_rect ();
507         set_colors ();
508 }
509
510 void
511 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
512 {
513         watch_connection.disconnect ();
514         binding_proxy.set_controllable (c);
515 }
516
517 void
518 ArdourButton::watch ()
519 {
520         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
521
522         if (!c) {
523                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
524                 return;
525         }
526
527         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
528 }
529
530 void
531 ArdourButton::controllable_changed ()
532 {
533         float val = binding_proxy.get_controllable()->get_value();
534
535         if (fabs (val) >= 0.5f) {
536                 set_active_state (Gtkmm2ext::Active);
537         } else {
538                 unset_active_state ();
539         }
540 }
541
542 void
543 ArdourButton::set_related_action (RefPtr<Action> act)
544 {
545         _action = act;
546
547         if (_action) {
548
549                 string str = _action->property_tooltip().get_value();
550                 if (!str.empty()) {
551                         ARDOUR_UI::instance()->set_tip (*this, str);
552                 }
553
554                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
555                 if (tact) {
556                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
557                 } 
558
559                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
560                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
561                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
562         }
563 }
564
565 void
566 ArdourButton::action_toggled ()
567 {
568         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
569
570         if (tact) {
571                 if (tact->get_active()) {
572                         set_active_state (Gtkmm2ext::Active);
573                 } else {
574                         unset_active_state ();
575                 }
576         }
577 }       
578
579 void
580 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
581 {
582         set_colors ();
583 }
584
585 void
586 ArdourButton::setup_led_rect ()
587 {
588         int text_margin;
589
590         if (_width < 75) {
591                 text_margin = 3;
592         } else {
593                 text_margin = 10;
594         }
595
596         if (_elements & Indicator) {
597                 _led_rect = new cairo_rectangle_t;
598                 
599                 if (_elements & Text) {
600                         if (_led_left) {
601                                 _led_rect->x = text_margin;
602                         } else {
603                                 _led_rect->x = _width - text_margin - _diameter/2.0;
604                         }
605                 } else {
606                         /* centered */
607                         _led_rect->x = _width/2.0 - _diameter/2.0;
608                 }
609
610                 _led_rect->y = _height/2.0 - _diameter/2.0;
611                 _led_rect->width = _diameter;
612                 _led_rect->height = _diameter;
613
614         } else {
615                 delete _led_rect;
616                 _led_rect = 0;
617         }
618 }
619
620 void
621 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
622 {
623         _pixbuf = img;
624         queue_draw ();
625 }
626
627 void
628 ArdourButton::set_active (bool yn)
629 {
630         /* this is an API simplification for buttons
631            that only use the Active and Normal states.
632         */
633
634         if (yn) {
635                 set_active_state (Gtkmm2ext::Active);
636         } else {
637                 unset_active_state ();
638         }
639 }
640
641 void
642 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
643 {
644         bool changed = (_active_state != s);
645         CairoWidget::set_active_state (s);
646         if (changed) {
647                 set_colors ();
648         }
649 }
650         
651 void
652 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
653 {
654         bool changed = (_visual_state != s);
655         CairoWidget::set_visual_state (s);
656         if (changed) {
657                 set_colors ();
658         }
659 }
660         
661 bool
662 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
663 {
664         _hovering = true;
665
666         if (ARDOUR::Config->get_widget_prelight()) {
667                 queue_draw ();
668         }
669
670         return CairoWidget::on_enter_notify_event (ev);
671 }
672
673 bool
674 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
675 {
676         _hovering = false;
677
678         if (ARDOUR::Config->get_widget_prelight()) {
679                 queue_draw ();
680         }
681
682         return CairoWidget::on_leave_notify_event (ev);
683 }
684
685 void
686 ArdourButton::set_tweaks (Tweaks t)
687 {
688         if (_tweaks != t) {
689                 _tweaks = t;
690                 queue_draw ();
691         }
692 }
693
694 void
695 ArdourButton::action_sensitivity_changed ()
696 {
697         if (_action->property_sensitive ()) {
698                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
699         } else {
700                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
701         }
702         
703 }
704
705
706 void
707 ArdourButton::action_visibility_changed ()
708 {
709         if (_action->property_visible ()) {
710                 show ();
711         } else {
712                 hide ();
713         }
714 }
715
716 void
717 ArdourButton::action_tooltip_changed ()
718 {
719         string str = _action->property_tooltip().get_value();
720         ARDOUR_UI::instance()->set_tip (*this, str);
721 }
722