4547fcd3bbbff00ff81a8094b54d040e45da7443
[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_button.h"
34 #include "ardour_ui.h"
35 #include "global_signals.h"
36
37 #include "i18n.h"
38
39 using namespace Gdk;
40 using namespace Gtk;
41 using namespace Glib;
42 using namespace PBD;
43 using std::max;
44 using std::min;
45 using namespace std;
46
47 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
48 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
49 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
50
51 ArdourButton::ArdourButton (Element e)
52         : _elements (e)
53         , _act_on_release (true)
54         , _text_width (0)
55         , _text_height (0)
56         , _diameter (11.0)
57         , _corner_radius (9.0)
58         , edge_pattern (0)
59         , fill_pattern (0)
60         , led_inset_pattern (0)
61         , reflection_pattern (0)
62         , _led_left (false)
63         , _fixed_diameter (true)
64         , _distinct_led_click (false)
65         , _led_rect (0)
66 {
67         ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
68         StateChanged.connect (sigc::mem_fun (*this, &ArdourButton::state_handler));
69 }
70
71 ArdourButton::~ArdourButton()
72 {
73         delete _led_rect;
74 }
75
76 void
77 ArdourButton::set_text (const std::string& str)
78 {
79         _text = str;
80
81         if (!_layout && !_text.empty()) {
82                 _layout = Pango::Layout::create (get_pango_context());
83         } 
84         
85         _layout->set_text (str);
86
87         queue_resize ();
88 }
89
90 void
91 ArdourButton::set_markup (const std::string& str)
92 {
93         _text = str;
94
95         if (!_layout) {
96                 _layout = Pango::Layout::create (get_pango_context());
97         } 
98
99         _layout->set_text (str);
100         queue_resize ();
101 }
102
103 void
104 ArdourButton::render (cairo_t* cr)
105 {
106         if (!_fixed_diameter) {
107                 _diameter = std::min (_width, _height);
108         }
109
110         /* background fill. use parent window style, so that we fit in nicely.
111          */
112         
113         Color c = get_parent_bg ();
114
115         cairo_rectangle (cr, 0, 0, _width, _height);
116         cairo_stroke_preserve (cr);
117         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
118         cairo_fill (cr);
119
120         if (_elements & Edge) {
121                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
122                 cairo_set_source (cr, edge_pattern);
123                 cairo_fill (cr);
124         }
125
126         if (_elements & Body) {
127                 if (_elements & Edge) {
128                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, _width-2, _height-2, _corner_radius - 1.0);
129                 } else {
130                         Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius - 1.0);
131                 }
132                 cairo_set_source (cr, fill_pattern);
133                 cairo_fill (cr);
134         }
135
136         /* text, if any */
137
138         int text_margin;
139
140         if (_width < 75) {
141                 text_margin = 3;
142         } else {
143                 text_margin = 10;
144         }
145
146         if ((_elements & Text) && !_text.empty()) {
147
148                 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
149
150                 if (_elements & Indicator) {
151                         if (_led_left) {
152                                 cairo_move_to (cr, text_margin + _diameter + 4, _height/2.0 - _text_height/2.0);
153                         } else {
154                                 cairo_move_to (cr, text_margin, _height/2.0 - _text_height/2.0);
155                         }
156                 } else {
157                         /* center text */
158                         cairo_move_to (cr, (_width - _text_width)/2.0, _height/2.0 - _text_height/2.0);
159                 }
160
161                 pango_cairo_show_layout (cr, _layout->gobj());
162         } 
163
164         if (_elements & Indicator) {
165
166                 /* move to the center of the indicator/led */
167
168                 cairo_save (cr);
169
170                 if (_elements & Text) {
171                         if (_led_left) {
172                                 cairo_translate (cr, text_margin + (_diameter/2.0), _height/2.0);
173                         } else {
174                                 cairo_translate (cr, _width - ((_diameter/2.0) + 4.0), _height/2.0);
175                         }
176                 } else {
177                         cairo_translate (cr, _width/2.0, _height/2.0);
178                 }
179                 
180                 //inset
181                 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
182                 cairo_set_source (cr, led_inset_pattern);
183                 cairo_fill (cr);
184                 
185                 //black ring
186                 cairo_set_source_rgb (cr, 0, 0, 0);
187                 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
188                 cairo_fill(cr);
189                 
190                 //led color
191                 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
192                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
193                 cairo_fill(cr);
194                 
195                 //reflection
196                 cairo_scale(cr, 0.7, 0.7);
197                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
198                 cairo_set_source (cr, reflection_pattern);
199                 cairo_fill (cr);
200
201                 cairo_restore (cr);
202
203         }
204
205         /* a partially transparent gray layer to indicate insensitivity */
206
207         if ((visual_state() & Insensitive)) {
208                 cairo_rectangle (cr, 0, 0, _width, _height);
209                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
210                 cairo_fill (cr);
211         }
212 }
213
214 void
215 ArdourButton::state_handler ()
216 {
217         set_colors ();
218 }
219
220 void
221 ArdourButton::set_diameter (float d)
222 {
223         _diameter = (d*2) + 5.0;
224
225         if (_diameter != 0.0) {
226                 _fixed_diameter = true;
227         }
228
229         set_colors ();
230 }
231
232 void
233 ArdourButton::set_corner_radius (float r)
234 {
235         _corner_radius = r;
236         set_dirty ();
237 }
238
239 void
240 ArdourButton::on_size_request (Gtk::Requisition* req)
241 {
242         int xpad = 0;
243         int ypad = 6;
244
245         CairoWidget::on_size_request (req);
246
247         if ((_elements & Text) && !_text.empty()) {
248                 _layout->get_pixel_size (_text_width, _text_height);
249                 if (_text_width + _diameter < 75) {
250                         xpad = 7;
251                 } else {
252                         xpad = 20;
253                 }
254         } else {
255                 _text_width = 0;
256                 _text_height = 0;
257         }
258
259         if ((_elements & Indicator) && _fixed_diameter) {
260                 req->width = _text_width + lrint (_diameter) + xpad;
261                 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
262         } else {
263                 req->width = _text_width + xpad;
264                 req->height = _text_height + ypad;
265         }
266 }
267
268 void
269 ArdourButton::set_colors ()
270 {
271         uint32_t start_color;
272         uint32_t end_color;
273         uint32_t r, g, b, a;
274         uint32_t text_color;
275         uint32_t led_color;
276
277         /* we use the edge of the button to show Selected state, so the
278          * color/pattern used there will vary depending on that
279          */
280         
281         if (edge_pattern) {
282                 cairo_pattern_destroy (edge_pattern);
283         }
284
285         if (_elements & Edge) {
286
287                 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
288                 if (visual_state() & CairoWidget::Selected) {
289                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border start selected", get_name()));
290                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border end selected", get_name()));
291                 } else {
292                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border start", get_name()));
293                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border end", get_name()));
294                 }
295                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
296                 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
297                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
298                 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
299         }
300
301
302         /* the fill pattern is used to indicate Normal/Active/Mid state
303          */
304
305         if (fill_pattern) {
306                 cairo_pattern_destroy (fill_pattern);
307         }
308
309         if (_elements & Body) {
310                 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
311                 
312                 if (active_state() == Mid) {
313                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill start mid", get_name()));
314                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill end mid", get_name()));
315                 } else if (active_state() == Active) {
316                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill start active", get_name()));
317                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill end active", get_name()));
318                 } else {
319                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill start", get_name()));
320                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill end", get_name()));
321                 }
322                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
323                 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
324                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
325                 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
326         }
327
328         if (led_inset_pattern) {
329                 cairo_pattern_destroy (led_inset_pattern);
330         }
331         
332         if (reflection_pattern) {
333                 cairo_pattern_destroy (reflection_pattern);
334         }
335
336         if (_elements & Indicator) {
337                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
338                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
339                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
340
341                 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
342                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
343                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
344         }
345         
346         /* text and LED colors depend on Active/Normal/Mid */
347
348         if (active_state() == Active) {
349                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 text active", get_name()));
350                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 led active", get_name()));
351         } else if (active_state() == Mid) {
352                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 text mid", get_name()));
353                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 led active", get_name()));
354         } else {
355                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 text", get_name()));
356                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 led", get_name()));
357         }
358
359         UINT_TO_RGBA (text_color, &r, &g, &b, &a);
360         text_r = r/255.0;
361         text_g = g/255.0;
362         text_b = b/255.0;
363         text_a = a/255.0;
364         UINT_TO_RGBA (led_color, &r, &g, &b, &a);
365         led_r = r/255.0;
366         led_g = g/255.0;
367         led_b = b/255.0;
368         led_a = a/255.0;
369
370         set_dirty ();
371 }
372
373 void
374 ArdourButton::set_led_left (bool yn)
375 {
376         _led_left = yn;
377 }
378
379 bool
380 ArdourButton::on_button_press_event (GdkEventButton *ev)
381 {
382 #if 0
383         cerr << "OBPE, rect = " << _led_rect << ' ' << _led_rect->x << ' ' << _led_rect->y << ' ' << _led_rect->width << ' ' << _led_rect->height
384              << " event at " << ev->x << ", " << ev->y 
385              << endl;
386 #endif
387
388         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
389                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
390                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
391                         cerr << "LED PRESS\n";
392                         return true;
393                 } else {
394                         cerr << "missed LED\n";
395                 }
396         }
397
398         if (binding_proxy.button_press_handler (ev)) {
399                 return true;
400         }
401
402         if (!_act_on_release) {
403                 if (_action) {
404                         _action->activate ();
405                         return true;
406                 }
407         }
408
409         return false;
410 }
411
412 bool
413 ArdourButton::on_button_release_event (GdkEventButton *ev)
414 {
415         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
416                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
417                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
418                         signal_led_clicked(); /* EMIT SIGNAL */
419                         return true;
420                 }
421         }
422
423         if (_act_on_release) {
424                 if (_action) {
425                         _action->activate ();
426                         return true;
427                 }
428         }
429
430         return false;
431 }
432
433 void
434 ArdourButton::set_distinct_led_click (bool yn)
435 {
436         _distinct_led_click = yn;
437         setup_led_rect ();
438 }
439
440 void
441 ArdourButton::color_handler ()
442 {
443         set_colors ();
444         set_dirty ();
445 }
446
447 void
448 ArdourButton::on_size_allocate (Allocation& alloc)
449 {
450         CairoWidget::on_size_allocate (alloc);
451         setup_led_rect ();
452         set_colors ();
453 }
454
455 void
456 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
457 {
458         watch_connection.disconnect ();
459         binding_proxy.set_controllable (c);
460 }
461
462 void
463 ArdourButton::watch ()
464 {
465         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
466
467         if (!c) {
468                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
469                 return;
470         }
471
472         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
473 }
474
475 void
476 ArdourButton::controllable_changed ()
477 {
478         float val = binding_proxy.get_controllable()->get_value();
479
480         if (fabs (val) >= 0.5f) {
481                 set_active_state (CairoWidget::Active);
482         } else {
483                 unset_active_state ();
484         }
485 }
486
487 void
488 ArdourButton::set_related_action (RefPtr<Action> act)
489 {
490         _action = act;
491
492         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
493         if (tact) {
494                 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
495         }
496 }
497
498 void
499 ArdourButton::action_toggled ()
500 {
501         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
502
503         if (tact) {
504                 if (tact->get_active()) {
505                         set_active_state (CairoWidget::Active);
506                 } else {
507                         unset_active_state ();
508                 }
509         }
510 }       
511
512 void
513 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
514 {
515         set_colors ();
516 }
517
518 void
519 ArdourButton::setup_led_rect ()
520 {
521         int text_margin;
522
523         if (_width < 75) {
524                 text_margin = 3;
525         } else {
526                 text_margin = 10;
527         }
528
529         if (_elements & Indicator) {
530                 _led_rect = new cairo_rectangle_t;
531                 
532                 if (_elements & Text) {
533                         if (_led_left) {
534                                 _led_rect->x = text_margin;
535                         } else {
536                                 _led_rect->x = _width - text_margin - _diameter/2.0;
537                         }
538                 } else {
539                         /* centered */
540                         _led_rect->x = _width/2.0 - _diameter/2.0;
541                 }
542
543                 _led_rect->y = _height/2.0 - _diameter/2.0;
544                 _led_rect->width = _diameter;
545                 _led_rect->height = _diameter;
546
547         } else {
548                 delete _led_rect;
549                 _led_rect = 0;
550         }
551 }
552