disable Midi Audition Synth if it cannot be found
[ardour.git] / libs / gtkmm2ext / cairo_widget.cc
1 /*
2     Copyright (C) 2009 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 "gtkmm2ext/cairo_widget.h"
21 #include "gtkmm2ext/gui_thread.h"
22
23 #include "i18n.h"
24
25 static const char* has_cairo_widget_background_info = "has_cairo_widget_background_info";
26
27 bool CairoWidget::_flat_buttons = false;
28
29 void CairoWidget::set_source_rgb_a( cairo_t* cr, Gdk::Color col, float a)  //ToDo:  this one and the Canvas version should be in a shared file (?)
30 {
31         float r = col.get_red_p ();
32         float g = col.get_green_p ();
33         float b = col.get_blue_p ();
34         
35         cairo_set_source_rgba(cr, r, g, b, a);
36 }
37
38 CairoWidget::CairoWidget ()
39         : _active_state (Gtkmm2ext::Off)
40         , _visual_state (Gtkmm2ext::NoVisualState)
41         , _need_bg (true)
42         , _grabbed (false)
43         , _name_proxy (this, X_("name"))
44         , _current_parent (0)
45 {
46         _name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed));
47 }
48
49 CairoWidget::~CairoWidget ()
50 {
51         if (_parent_style_change) _parent_style_change.disconnect();
52 }
53
54 bool
55 CairoWidget::on_expose_event (GdkEventExpose *ev)
56 {
57         cairo_t* cr = gdk_cairo_create (get_window ()->gobj());
58         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
59         cairo_clip_preserve (cr);
60
61         /* paint expose area the color of the parent window bg
62         */
63         
64         Gdk::Color bg (get_parent_bg());
65         
66         cairo_set_source_rgb (cr, bg.get_red_p(), bg.get_green_p(), bg.get_blue_p());
67         cairo_fill (cr);
68
69         cairo_rectangle_t expose_area;
70         expose_area.x = ev->area.x;
71         expose_area.y = ev->area.y;
72         expose_area.width = ev->area.width;
73         expose_area.height = ev->area.height;
74
75         render (cr, &expose_area);
76
77         cairo_destroy (cr);
78
79         return true;
80 }
81
82 /** Marks the widget as dirty, so that render () will be called on
83  *  the next GTK expose event.
84  */
85
86 void
87 CairoWidget::set_dirty ()
88 {
89         ENSURE_GUI_THREAD (*this, &CairoWidget::set_dirty);
90         queue_draw ();
91 }
92
93 /** Handle a size allocation.
94  *  @param alloc GTK allocation.
95  */
96 void
97 CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
98 {
99         Gtk::EventBox::on_size_allocate (alloc);
100
101         set_dirty ();
102 }
103
104 Gdk::Color
105 CairoWidget::get_parent_bg ()
106 {
107         Widget* parent;
108
109         parent = get_parent ();
110
111         while (parent) {
112                 void* p = g_object_get_data (G_OBJECT(parent->gobj()), has_cairo_widget_background_info);
113
114                 if (p) {
115                         Glib::RefPtr<Gtk::Style> style = parent->get_style();
116                         if (_current_parent != parent) {
117                                 if (_parent_style_change) _parent_style_change.disconnect();
118                                 _current_parent = parent;
119                                 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &CairoWidget::on_style_changed));
120                         }
121                         return style->get_bg (get_state());
122                 }
123
124                 if (!parent->get_has_window()) {
125                         parent = parent->get_parent();
126                 } else {
127                         break;
128                 }
129         }
130
131         if (parent && parent->get_has_window()) {
132                 if (_current_parent != parent) {
133                         if (_parent_style_change) _parent_style_change.disconnect();
134                         _current_parent = parent;
135                         _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &CairoWidget::on_style_changed));
136                 }
137                 return parent->get_style ()->get_bg (parent->get_state());
138         }
139
140         return get_style ()->get_bg (get_state());
141 }
142
143 void
144 CairoWidget::set_active_state (Gtkmm2ext::ActiveState s)
145 {
146         if (_active_state != s) {
147                 _active_state = s;
148                 StateChanged ();
149         }
150 }
151
152 void
153 CairoWidget::set_visual_state (Gtkmm2ext::VisualState s)
154 {
155         if (_visual_state != s) {
156                 _visual_state = s;
157                 StateChanged ();
158         }
159 }
160
161 void
162 CairoWidget::set_active (bool yn)
163 {
164         /* this is an API simplification for buttons
165            that only use the Active and Normal states.
166         */
167
168         if (yn) {
169                 set_active_state (Gtkmm2ext::ExplicitActive);
170         } else {
171                 unset_active_state ();
172         }
173 }
174
175 void
176 CairoWidget::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
177 {
178         queue_draw();
179 }
180
181 void
182 CairoWidget::on_state_changed (Gtk::StateType)
183 {
184         /* this will catch GTK-level state changes from calls like
185            ::set_sensitive()
186         */
187
188         if (get_state() == Gtk::STATE_INSENSITIVE) {
189                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
190         } else {
191                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
192         }
193
194         queue_draw ();
195 }
196
197 void
198 CairoWidget::set_draw_background (bool yn)
199 {
200         _need_bg = yn;
201 }
202
203 void
204 CairoWidget::provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg)
205 {
206         /* set up @w to be able to provide bg information to
207            any CairoWidgets that are packed inside it.
208         */
209
210         w.modify_bg (Gtk::STATE_NORMAL, bg);
211         w.modify_bg (Gtk::STATE_INSENSITIVE, bg);
212         w.modify_bg (Gtk::STATE_ACTIVE, bg);
213         w.modify_bg (Gtk::STATE_SELECTED, bg);
214
215         g_object_set_data (G_OBJECT(w.gobj()), has_cairo_widget_background_info, (void*) 0xfeedface);
216 }
217
218 void
219 CairoWidget::set_flat_buttons (bool yn)
220 {
221         _flat_buttons = yn;
222 }