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"
25 #include "gtkmm2ext/rgb_macros.h"
29 #include "gtkmm2ext/nsglview.h"
34 static const char* has_cairo_widget_background_info = "has_cairo_widget_background_info";
36 bool CairoWidget::_flat_buttons = false;
37 bool CairoWidget::_widget_prelight = true;
39 sigc::slot<void,Gtk::Widget*> CairoWidget::focus_handler;
41 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 (?)
43 float r = col.get_red_p ();
44 float g = col.get_green_p ();
45 float b = col.get_blue_p ();
47 cairo_set_source_rgba(cr, r, g, b, a);
50 CairoWidget::CairoWidget ()
51 : _active_state (Gtkmm2ext::Off)
52 , _visual_state (Gtkmm2ext::NoVisualState)
55 , _name_proxy (this, X_("name"))
57 , _canvas_widget (false)
60 _name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed));
63 CairoWidget::~CairoWidget ()
66 gtk_widget_set_realized (GTK_WIDGET(gobj()), false);
68 if (_parent_style_change) _parent_style_change.disconnect();
72 CairoWidget::set_canvas_widget ()
75 assert (!_canvas_widget);
77 gtk_widget_set_realized (GTK_WIDGET(gobj()), true);
78 _canvas_widget = true;
82 CairoWidget::use_nsglview ()
85 assert (!_canvas_widget);
86 assert (!is_realized());
87 #ifdef ARDOUR_CANVAS_NSVIEW_TAG // patched gdkquartz.h
88 _nsglview = Gtkmm2ext::nsglview_create (this);
93 CairoWidget::get_width () const
96 return _allocation.get_width ();
98 return Gtk::EventBox::get_width ();
102 CairoWidget::get_height () const
104 if (_canvas_widget) {
105 return _allocation.get_height ();
107 return Gtk::EventBox::get_height ();
111 CairoWidget::size_allocate (Gtk::Allocation& alloc)
113 if (_canvas_widget) {
114 memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation));
117 Gtk::EventBox::size_allocate (alloc);
122 CairoWidget::on_button_press_event (GdkEventButton*)
124 focus_handler (this);
129 CairoWidget::background_color ()
132 Gdk::Color bg (get_parent_bg());
133 return RGBA_TO_UINT (bg.get_red() / 255, bg.get_green() / 255, bg.get_blue() / 255, 255);
139 #ifdef USE_TRACKS_CODE_FEATURES
141 /* This is Tracks version of this method.
143 The use of get_visible_window() in this method is an abuse of the GDK/GTK
144 semantics. It can and may break on different GDK backends, and uses a
145 side-effect/unintended behaviour in GDK/GTK to try to accomplish something
146 which should be done differently. I (Paul) have confirmed this with the GTK
149 For this reason, this code is not acceptable for ordinary merging into the Ardour libraries.
151 Ardour Developers: you are not obligated to maintain the internals of this
152 implementation in the face of build-time environment changes (e.g. -D
157 CairoWidget::on_expose_event (GdkEventExpose *ev)
159 cairo_rectangle_t expose_area;
160 expose_area.width = ev->area.width;
161 expose_area.height = ev->area.height;
163 #ifdef USE_CAIRO_IMAGE_SURFACE_FOR_CAIRO_WIDGET
164 Cairo::RefPtr<Cairo::Context> cr;
165 if (get_visible_window ()) {
168 if (!_image_surface) {
169 _image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
171 cr = Cairo::Context::create (_image_surface);
173 expose_area.x = ev->area.x;
174 expose_area.y = ev->area.y;
175 cr = get_window()->create_cairo_context ();
178 expose_area.x = ev->area.x;
179 expose_area.y = ev->area.y;
180 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context ();
183 cr->rectangle (expose_area.x, expose_area.y, expose_area.width, expose_area.height);
186 /* paint expose area the color of the parent window bg
189 if (get_visible_window ()) {
190 Gdk::Color bg (get_parent_bg());
191 cr->rectangle (expose_area.x, expose_area.y, expose_area.width, expose_area.height);
192 cr->set_source_rgb (bg.get_red_p(), bg.get_green_p(), bg.get_blue_p());
196 render (cr, &expose_area);
198 #ifdef USE_CAIRO_IMAGE_SURFACE_FOR_CAIRO_WIDGET
199 if(get_visible_window ()) {
200 _image_surface->flush();
201 /* now blit our private surface back to the GDK one */
203 Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context ();
205 cairo_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
206 cairo_context->clip ();
207 cairo_context->set_source (_image_surface, ev->area.x, ev->area.y);
208 cairo_context->set_operator (Cairo::OPERATOR_OVER);
209 cairo_context->paint ();
213 Gtk::Widget* child = get_child ();
216 propagate_expose (*child, ev);
224 /* Ardour mainline: not using Tracks code features.
226 Tracks Developers: please do not modify this version of
227 ::on_expose_event(). The version used by Tracks is before the preceding
228 #else and contains hacks required for the Tracks GUI to work.
232 CairoWidget::on_expose_event (GdkEventExpose *ev)
236 Gtkmm2ext::nsglview_queue_draw (_nsglview, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
240 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
241 Cairo::RefPtr<Cairo::Context> cr;
242 if (getenv("ARDOUR_IMAGE_SURFACE")) {
243 if (!image_surface) {
244 image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
246 cr = Cairo::Context::create (image_surface);
248 cr = get_window()->create_cairo_context ();
250 #elif defined USE_CAIRO_IMAGE_SURFACE
252 if (!image_surface) {
253 image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
256 Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (image_surface);
258 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context ();
261 cr->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
264 cr->clip_preserve ();
266 /* paint expose area the color of the parent window bg
269 Gdk::Color bg (get_parent_bg());
271 cr->set_source_rgb (bg.get_red_p(), bg.get_green_p(), bg.get_blue_p());
274 std::cerr << get_name() << " skipped bg fill\n";
278 cairo_rectangle_t expose_area;
279 expose_area.x = ev->area.x;
280 expose_area.y = ev->area.y;
281 expose_area.width = ev->area.width;
282 expose_area.height = ev->area.height;
284 render (cr, &expose_area);
286 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
287 if (getenv("ARDOUR_IMAGE_SURFACE")) {
289 #if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE
290 image_surface->flush();
291 /* now blit our private surface back to the GDK one */
293 Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context ();
295 cairo_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
296 cairo_context->clip ();
297 cairo_context->set_source (image_surface, 0, 0);
298 cairo_context->set_operator (Cairo::OPERATOR_SOURCE);
299 cairo_context->paint ();
301 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
310 /** Marks the widget as dirty, so that render () will be called on
311 * the next GTK expose event.
315 CairoWidget::set_dirty (cairo_rectangle_t *area)
317 ENSURE_GUI_THREAD (*this, &CairoWidget::set_dirty);
321 // TODO emit QueueDrawArea -> ArdourCanvas::Widget
325 queue_draw_area (area->x, area->y, area->width, area->height);
330 CairoWidget::queue_draw ()
335 Gtk::EventBox::queue_draw ();
339 CairoWidget::queue_resize ()
341 if (QueueResize ()) {
344 Gtk::EventBox::queue_resize ();
347 /** Handle a size allocation.
348 * @param alloc GTK allocation.
351 CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
353 if (!_canvas_widget) {
354 Gtk::EventBox::on_size_allocate (alloc);
356 memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation));
359 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
360 if (getenv("ARDOUR_IMAGE_SURFACE")) {
362 #if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE
363 image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, alloc.get_width(), alloc.get_height());
365 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
369 if (_canvas_widget) {
375 gtk_widget_translate_coordinates(
377 GTK_WIDGET(get_toplevel()->gobj()),
379 Gtkmm2ext::nsglview_resize (_nsglview, xx, yy, alloc.get_width(), alloc.get_height());
386 CairoWidget::get_parent_bg ()
390 parent = get_parent ();
393 void* p = g_object_get_data (G_OBJECT(parent->gobj()), has_cairo_widget_background_info);
396 Glib::RefPtr<Gtk::Style> style = parent->get_style();
397 if (_current_parent != parent) {
398 if (_parent_style_change) _parent_style_change.disconnect();
399 _current_parent = parent;
400 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &CairoWidget::on_style_changed));
402 return style->get_bg (get_state());
405 if (!parent->get_has_window()) {
406 parent = parent->get_parent();
412 if (parent && parent->get_has_window()) {
413 if (_current_parent != parent) {
414 if (_parent_style_change) _parent_style_change.disconnect();
415 _current_parent = parent;
416 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &CairoWidget::on_style_changed));
418 return parent->get_style ()->get_bg (parent->get_state());
421 return get_style ()->get_bg (get_state());
425 CairoWidget::set_active_state (Gtkmm2ext::ActiveState s)
427 if (_active_state != s) {
434 CairoWidget::set_visual_state (Gtkmm2ext::VisualState s)
436 if (_visual_state != s) {
443 CairoWidget::set_active (bool yn)
445 /* this is an API simplification for buttons
446 that only use the Active and Normal states.
450 set_active_state (Gtkmm2ext::ExplicitActive);
452 unset_active_state ();
457 CairoWidget::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
463 CairoWidget::on_realize ()
465 Gtk::EventBox::on_realize();
468 Gtkmm2ext::nsglview_overlay (_nsglview, get_window()->gobj());
474 CairoWidget::on_map ()
476 Gtk::EventBox::on_map();
479 Gtkmm2ext::nsglview_set_visible (_nsglview, true);
480 Gtk::Allocation a = get_allocation();
482 gtk_widget_translate_coordinates(
484 GTK_WIDGET(get_toplevel()->gobj()),
486 Gtkmm2ext::nsglview_resize (_nsglview, xx, yy, a.get_width(), a.get_height());
492 CairoWidget::on_unmap ()
494 Gtk::EventBox::on_unmap();
497 Gtkmm2ext::nsglview_set_visible (_nsglview, false);
503 CairoWidget::on_state_changed (Gtk::StateType)
505 /* this will catch GTK-level state changes from calls like
509 if (get_state() == Gtk::STATE_INSENSITIVE) {
510 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
512 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
519 CairoWidget::set_draw_background (bool yn)
525 CairoWidget::provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg)
527 /* set up @w to be able to provide bg information to
528 any CairoWidgets that are packed inside it.
531 w.modify_bg (Gtk::STATE_NORMAL, bg);
532 w.modify_bg (Gtk::STATE_INSENSITIVE, bg);
533 w.modify_bg (Gtk::STATE_ACTIVE, bg);
534 w.modify_bg (Gtk::STATE_SELECTED, bg);
536 g_object_set_data (G_OBJECT(w.gobj()), has_cairo_widget_background_info, (void*) 0xfeedface);
540 CairoWidget::set_flat_buttons (bool yn)
546 CairoWidget::set_widget_prelight (bool yn)
548 _widget_prelight = yn;
552 CairoWidget::set_focus_handler (sigc::slot<void,Gtk::Widget*> s)