2 Copyright (C) 2009 Paul Davis
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.
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.
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.
19 #if !defined USE_CAIRO_IMAGE_SURFACE && !defined NDEBUG
20 #define OPTIONAL_CAIRO_IMAGE_SURFACE
23 #include "gtkmm2ext/cairo_widget.h"
24 #include "gtkmm2ext/gui_thread.h"
28 static const char* has_cairo_widget_background_info = "has_cairo_widget_background_info";
30 bool CairoWidget::_flat_buttons = false;
31 bool CairoWidget::_widget_prelight = true;
33 sigc::slot<void,Gtk::Widget*> CairoWidget::focus_handler;
35 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 (?)
37 float r = col.get_red_p ();
38 float g = col.get_green_p ();
39 float b = col.get_blue_p ();
41 cairo_set_source_rgba(cr, r, g, b, a);
44 CairoWidget::CairoWidget ()
45 : _active_state (Gtkmm2ext::Off)
46 , _visual_state (Gtkmm2ext::NoVisualState)
49 , _name_proxy (this, X_("name"))
52 _name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed));
55 CairoWidget::~CairoWidget ()
57 if (_parent_style_change) _parent_style_change.disconnect();
61 CairoWidget::on_button_press_event (GdkEventButton*)
68 #ifdef USE_TRACKS_CODE_FEATURES
70 /* This is Tracks version of this method.
72 The use of get_visible_window() in this method is an abuse of the GDK/GTK
73 semantics. It can and may break on different GDK backends, and uses a
74 side-effect/unintended behaviour in GDK/GTK to try to accomplish something
75 which should be done differently. I (Paul) have confirmed this with the GTK
78 For this reason, this code is not acceptable for ordinary merging into the Ardour libraries.
80 Ardour Developers: you are not obligated to maintain the internals of this
81 implementation in the face of build-time environment changes (e.g. -D
86 CairoWidget::on_expose_event (GdkEventExpose *ev)
88 cairo_rectangle_t expose_area;
89 expose_area.width = ev->area.width;
90 expose_area.height = ev->area.height;
92 #ifdef USE_CAIRO_IMAGE_SURFACE_FOR_CAIRO_WIDGET
93 Cairo::RefPtr<Cairo::Context> cr;
94 if (get_visible_window ()) {
97 if (!_image_surface) {
98 _image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
100 cr = Cairo::Context::create (_image_surface);
102 expose_area.x = ev->area.x;
103 expose_area.y = ev->area.y;
104 cr = get_window()->create_cairo_context ();
107 expose_area.x = ev->area.x;
108 expose_area.y = ev->area.y;
109 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context ();
112 cr->rectangle (expose_area.x, expose_area.y, expose_area.width, expose_area.height);
115 /* paint expose area the color of the parent window bg
118 if (get_visible_window ()) {
119 Gdk::Color bg (get_parent_bg());
120 cr->rectangle (expose_area.x, expose_area.y, expose_area.width, expose_area.height);
121 cr->set_source_rgb (bg.get_red_p(), bg.get_green_p(), bg.get_blue_p());
125 render (cr->cobj(), &expose_area);
127 #ifdef USE_CAIRO_IMAGE_SURFACE_FOR_CAIRO_WIDGET
128 if(get_visible_window ()) {
129 _image_surface->flush();
130 /* now blit our private surface back to the GDK one */
132 Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context ();
134 cairo_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
135 cairo_context->clip ();
136 cairo_context->set_source (_image_surface, ev->area.x, ev->area.y);
137 cairo_context->set_operator (Cairo::OPERATOR_OVER);
138 cairo_context->paint ();
142 Gtk::Widget* child = get_child ();
145 propagate_expose (*child, ev);
153 /* Ardour mainline: not using Tracks code features.
155 Tracks Developers: please do not modify this version of
156 ::on_expose_event(). The version used by Tracks is before the preceding
157 #else and contains hacks required for the Tracks GUI to work.
161 CairoWidget::on_expose_event (GdkEventExpose *ev)
163 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
164 Cairo::RefPtr<Cairo::Context> cr;
165 if (getenv("ARDOUR_IMAGE_SURFACE")) {
166 if (!image_surface) {
167 image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
169 cr = Cairo::Context::create (image_surface);
171 cr = get_window()->create_cairo_context ();
173 #elif defined USE_CAIRO_IMAGE_SURFACE
175 if (!image_surface) {
176 image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
179 Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (image_surface);
181 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context ();
184 cr->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
187 cr->clip_preserve ();
189 /* paint expose area the color of the parent window bg
192 Gdk::Color bg (get_parent_bg());
194 cr->set_source_rgb (bg.get_red_p(), bg.get_green_p(), bg.get_blue_p());
197 std::cerr << get_name() << " skipped bg fill\n";
201 cairo_rectangle_t expose_area;
202 expose_area.x = ev->area.x;
203 expose_area.y = ev->area.y;
204 expose_area.width = ev->area.width;
205 expose_area.height = ev->area.height;
207 render (cr->cobj(), &expose_area);
209 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
210 if (getenv("ARDOUR_IMAGE_SURFACE")) {
212 #if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE
213 image_surface->flush();
214 /* now blit our private surface back to the GDK one */
216 Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context ();
218 cairo_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
219 cairo_context->clip ();
220 cairo_context->set_source (image_surface, 0, 0);
221 cairo_context->set_operator (Cairo::OPERATOR_SOURCE);
222 cairo_context->paint ();
224 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
233 /** Marks the widget as dirty, so that render () will be called on
234 * the next GTK expose event.
238 CairoWidget::set_dirty (cairo_rectangle_t *area)
240 ENSURE_GUI_THREAD (*this, &CairoWidget::set_dirty);
244 queue_draw_area (area->x, area->y, area->width, area->height);
248 /** Handle a size allocation.
249 * @param alloc GTK allocation.
252 CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
254 Gtk::EventBox::on_size_allocate (alloc);
256 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
257 if (getenv("ARDOUR_IMAGE_SURFACE")) {
259 #if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE
260 image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, alloc.get_width(), alloc.get_height());
262 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
270 CairoWidget::get_parent_bg ()
274 parent = get_parent ();
277 void* p = g_object_get_data (G_OBJECT(parent->gobj()), has_cairo_widget_background_info);
280 Glib::RefPtr<Gtk::Style> style = parent->get_style();
281 if (_current_parent != parent) {
282 if (_parent_style_change) _parent_style_change.disconnect();
283 _current_parent = parent;
284 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &CairoWidget::on_style_changed));
286 return style->get_bg (get_state());
289 if (!parent->get_has_window()) {
290 parent = parent->get_parent();
296 if (parent && parent->get_has_window()) {
297 if (_current_parent != parent) {
298 if (_parent_style_change) _parent_style_change.disconnect();
299 _current_parent = parent;
300 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &CairoWidget::on_style_changed));
302 return parent->get_style ()->get_bg (parent->get_state());
305 return get_style ()->get_bg (get_state());
309 CairoWidget::set_active_state (Gtkmm2ext::ActiveState s)
311 if (_active_state != s) {
318 CairoWidget::set_visual_state (Gtkmm2ext::VisualState s)
320 if (_visual_state != s) {
327 CairoWidget::set_active (bool yn)
329 /* this is an API simplification for buttons
330 that only use the Active and Normal states.
334 set_active_state (Gtkmm2ext::ExplicitActive);
336 unset_active_state ();
341 CairoWidget::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
347 CairoWidget::on_state_changed (Gtk::StateType)
349 /* this will catch GTK-level state changes from calls like
353 if (get_state() == Gtk::STATE_INSENSITIVE) {
354 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
356 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
363 CairoWidget::set_draw_background (bool yn)
369 CairoWidget::provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg)
371 /* set up @w to be able to provide bg information to
372 any CairoWidgets that are packed inside it.
375 w.modify_bg (Gtk::STATE_NORMAL, bg);
376 w.modify_bg (Gtk::STATE_INSENSITIVE, bg);
377 w.modify_bg (Gtk::STATE_ACTIVE, bg);
378 w.modify_bg (Gtk::STATE_SELECTED, bg);
380 g_object_set_data (G_OBJECT(w.gobj()), has_cairo_widget_background_info, (void*) 0xfeedface);
384 CairoWidget::set_flat_buttons (bool yn)
390 CairoWidget::set_widget_prelight (bool yn)
392 _widget_prelight = yn;
396 CairoWidget::set_focus_handler (sigc::slot<void,Gtk::Widget*> s)