merge with master.
[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 #ifndef __gtk2_ardour_ardour_button_h__
20 #define __gtk2_ardour_ardour_button_h__
21
22 #include <list>
23 #include <stdint.h>
24
25 #include <gtkmm/action.h>
26
27 #include "pbd/signals.h"
28 #include "gtkmm2ext/binding_proxy.h"
29 #include "gtkmm2ext/activatable.h"
30 #include "gtkmm2ext/cairo_widget.h"
31
32 class ArdourButton : public CairoWidget , public Gtkmm2ext::Activatable
33 {
34   public:
35         enum Element {
36                 Edge = 0x1,
37                 Body = 0x2,
38                 Text = 0x4,
39                 Indicator = 0x8,
40                 Inset = 0x10,
41                 Menu = 0x20,
42         };
43
44         static Element default_elements;
45         static Element led_default_elements;
46         static Element just_led_default_elements;
47
48         static void set_flat_buttons (bool yn);
49         static bool flat_buttons() { return _flat_buttons; }
50
51         ArdourButton (Element e = default_elements);
52         ArdourButton (const std::string&, Element e = default_elements);
53         virtual ~ArdourButton ();
54
55         enum Tweaks {
56                 ShowClick = 0x1,
57                 NoModel = 0x2,
58                 ImplicitUsesSolidColor = 0x4,
59         };
60
61         Tweaks tweaks() const { return _tweaks; }
62         void set_tweaks (Tweaks);
63
64         void set_active_state (Gtkmm2ext::ActiveState);
65         void set_visual_state (Gtkmm2ext::VisualState);
66
67         Element elements() const { return _elements; }
68         void set_elements (Element);
69         void add_elements (Element);
70         
71         void set_corner_radius (float);
72         void set_rounded_corner_mask (int);
73         void set_diameter (float);
74
75         void set_text (const std::string&);
76         const std::string& get_text () {return _text;}
77         void set_markup (const std::string&);
78         void set_angle (const double);
79         void set_alignment (const float, const float);
80         void get_alignment (float& xa, float& ya) {xa = _xalign; ya = _yalign;};
81
82         void set_led_left (bool yn);
83         void set_distinct_led_click (bool yn);
84
85         Glib::RefPtr<Pango::Layout> layout() const { return _layout; }
86
87         sigc::signal<void> signal_led_clicked;
88         sigc::signal<void> signal_clicked;
89
90         boost::shared_ptr<PBD::Controllable> get_controllable() { return binding_proxy.get_controllable(); }
91         void set_controllable (boost::shared_ptr<PBD::Controllable> c);
92         void watch ();
93
94         void set_related_action (Glib::RefPtr<Gtk::Action>);
95
96         bool on_button_press_event (GdkEventButton*);
97         bool on_button_release_event (GdkEventButton*);
98
99         void set_image (const Glib::RefPtr<Gdk::Pixbuf>&);
100
101         void set_fixed_colors (const uint32_t active_color, const uint32_t inactive_color);
102
103   protected:
104         void render (cairo_t *, cairo_rectangle_t *);
105         void on_size_request (Gtk::Requisition* req);
106         void on_size_allocate (Gtk::Allocation&);
107         void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
108         void on_name_changed ();
109         bool on_enter_notify_event (GdkEventCrossing*);
110         bool on_leave_notify_event (GdkEventCrossing*);
111         bool on_focus_in_event (GdkEventFocus*);
112         bool on_focus_out_event (GdkEventFocus*);
113         bool on_key_release_event (GdkEventKey *);
114
115         void controllable_changed ();
116         PBD::ScopedConnection watch_connection;
117
118   private:
119         Glib::RefPtr<Pango::Layout> _layout;
120         Glib::RefPtr<Gdk::Pixbuf>   _pixbuf;
121         std::string                 _text;
122         Element                     _elements;
123         Tweaks                      _tweaks;
124         BindingProxy                binding_proxy;
125
126         int   _text_width;
127         int   _text_height;
128         float _diameter;
129         float _corner_radius;
130         int   _corner_mask;
131
132         double _angle;
133         float _xalign, _yalign;
134
135         uint32_t bg_color;
136         uint32_t border_color;
137         uint32_t fill_start_inactive_color;
138         uint32_t fill_end_inactive_color;
139         uint32_t fill_start_active_color;
140         uint32_t fill_end_active_color;
141         uint32_t text_active_color;
142         uint32_t text_inactive_color;
143         uint32_t led_active_color;
144         uint32_t led_inactive_color;
145     
146         cairo_pattern_t* fill_pattern;
147         cairo_pattern_t* fill_pattern_active;
148         cairo_pattern_t* shine_pattern;
149         cairo_pattern_t* led_inset_pattern;
150         cairo_pattern_t* reflection_pattern;
151     
152         cairo_rectangle_t* _led_rect;
153
154         double text_r;
155         double text_g;
156         double text_b;
157         double text_a;
158
159         double led_r;
160         double led_g;
161         double led_b;
162         double led_a;
163
164         double active_r;
165         double active_g;
166         double active_b;
167         double active_a;
168
169         bool _act_on_release;
170         bool _led_left;
171         bool _fixed_diameter;
172         bool _distinct_led_click;
173         bool _hovering;
174         bool _focused;
175     
176         static bool _flat_buttons;
177
178         void setup_led_rect ();
179         void set_colors ();
180         void color_handler ();
181         void build_patterns ();
182
183         void action_toggled ();
184
185         void action_sensitivity_changed ();
186         void action_visibility_changed ();
187         void action_tooltip_changed ();
188 };
189
190 #endif /* __gtk2_ardour_ardour_button_h__ */