allow addition of images to ArdourButtons (always centered)
[ardour.git] / gtk2_ardour / ardour_button.h
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 #ifndef __gtk2_ardour_ardour_button_h__
21 #define __gtk2_ardour_ardour_button_h__
22
23 #include <stdint.h>
24
25 #include <gtkmm/action.h>
26
27 #include "pbd/signals.h"
28 #include "gtkmm2ext/binding_proxy.h"
29
30 #include "cairo_widget.h"
31
32 class ArdourButton : public CairoWidget
33 {
34   public:
35         enum Element {
36                 Edge = 0x1,
37                 Body = 0x2,
38                 Text = 0x4,
39                 Indicator = 0x8,
40         };
41         
42         static Element default_elements;
43         static Element led_default_elements;
44         static Element just_led_default_elements;
45
46         ArdourButton (Element e = default_elements);
47         virtual ~ArdourButton ();
48
49         void set_elements (Element);
50         Element elements() const { return _elements; }
51
52         void set_corner_radius (float);
53         void set_diameter (float);
54
55         void set_text (const std::string&);
56         void set_markup (const std::string&);
57
58         void set_led_left (bool yn);
59         void set_distinct_led_click (bool yn);
60
61         sigc::signal<void> signal_led_clicked;
62
63         boost::shared_ptr<PBD::Controllable> get_controllable() { return binding_proxy.get_controllable(); }
64         void set_controllable (boost::shared_ptr<PBD::Controllable> c);
65         void watch ();
66
67         void set_related_action (Glib::RefPtr<Gtk::Action>);
68
69         bool on_button_press_event (GdkEventButton*);
70         bool on_button_release_event (GdkEventButton*);
71
72         void set_image (const Glib::RefPtr<Gdk::Pixbuf>&);
73
74   protected:
75         void render (cairo_t *);
76         void on_size_request (Gtk::Requisition* req);
77         void on_size_allocate (Gtk::Allocation&);
78         void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
79
80         void controllable_changed ();
81         PBD::ScopedConnection watch_connection;
82
83   private:
84         Glib::RefPtr<Pango::Layout> _layout;
85         Glib::RefPtr<Gdk::Pixbuf>   _pixbuf;
86         std::string                 _text;
87         Element                     _elements;
88         BindingProxy binding_proxy;
89         bool    _act_on_release;
90
91         int   _text_width;
92         int   _text_height;
93         float _diameter;
94         float _corner_radius;
95
96         cairo_pattern_t* edge_pattern;
97         cairo_pattern_t* fill_pattern;
98         cairo_pattern_t* led_inset_pattern;
99         cairo_pattern_t* reflection_pattern;
100
101         double text_r;
102         double text_g;
103         double text_b;
104         double text_a;
105
106         double led_r;
107         double led_g;
108         double led_b;
109         double led_a;
110
111         bool _led_left;
112         bool _fixed_diameter;
113         bool _distinct_led_click;
114         cairo_rectangle_t* _led_rect;
115
116         void setup_led_rect ();
117         void set_colors ();
118         void color_handler ();
119         void state_handler ();
120
121         Glib::RefPtr<Gtk::Action> _action;
122         void action_activated ();
123         void action_toggled ();
124 };
125
126 #endif /* __gtk2_ardour_ardour_button_h__ */