2 * Copyright (C) 2011-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2014-2018 Robin Gareus <robin@gareus.org>
4 * Copyright (C) 2014 Ben Loftis <ben@harrisonconsoles.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #if !defined USE_CAIRO_IMAGE_SURFACE && !defined NDEBUG
21 #define OPTIONAL_CAIRO_IMAGE_SURFACE
24 #include "gtkmm2ext/cairo_widget.h"
25 #include "gtkmm2ext/gui_thread.h"
26 #include "gtkmm2ext/rgb_macros.h"
30 #include "gtkmm2ext/nsglview.h"
35 static const char* has_cairo_widget_background_info = "has_cairo_widget_background_info";
37 bool CairoWidget::_flat_buttons = false;
38 bool CairoWidget::_boxy_buttons = false;
39 bool CairoWidget::_widget_prelight = true;
41 sigc::slot<void,Gtk::Widget*> CairoWidget::focus_handler;
43 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 (?)
45 float r = col.get_red_p ();
46 float g = col.get_green_p ();
47 float b = col.get_blue_p ();
49 cairo_set_source_rgba(cr, r, g, b, a);
52 CairoWidget::CairoWidget ()
53 : _active_state (Gtkmm2ext::Off)
54 , _visual_state (Gtkmm2ext::NoVisualState)
57 , _name_proxy (this, X_("name"))
59 , _canvas_widget (false)
62 _name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed));
65 CairoWidget::~CairoWidget ()
68 gtk_widget_set_realized (GTK_WIDGET(gobj()), false);
70 if (_parent_style_change) _parent_style_change.disconnect();
74 CairoWidget::set_canvas_widget ()
77 assert (!_canvas_widget);
79 gtk_widget_set_realized (GTK_WIDGET(gobj()), true);
80 _canvas_widget = true;
84 CairoWidget::use_nsglview ()
87 assert (!_canvas_widget);
88 assert (!is_realized());
89 #ifdef ARDOUR_CANVAS_NSVIEW_TAG // patched gdkquartz.h
90 _nsglview = Gtkmm2ext::nsglview_create (this);
95 CairoWidget::get_width () const
98 return _allocation.get_width ();
100 return Gtk::EventBox::get_width ();
104 CairoWidget::get_height () const
106 if (_canvas_widget) {
107 return _allocation.get_height ();
109 return Gtk::EventBox::get_height ();
113 CairoWidget::size_allocate (Gtk::Allocation& alloc)
115 if (_canvas_widget) {
116 memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation));
119 Gtk::EventBox::size_allocate (alloc);
124 CairoWidget::on_button_press_event (GdkEventButton*)
126 focus_handler (this);
131 CairoWidget::background_color ()
134 Gdk::Color bg (get_parent_bg());
135 return RGBA_TO_UINT (bg.get_red() / 255, bg.get_green() / 255, bg.get_blue() / 255, 255);
141 #ifdef USE_TRACKS_CODE_FEATURES
143 /* This is Tracks version of this method.
145 The use of get_visible_window() in this method is an abuse of the GDK/GTK
146 semantics. It can and may break on different GDK backends, and uses a
147 side-effect/unintended behaviour in GDK/GTK to try to accomplish something
148 which should be done differently. I (Paul) have confirmed this with the GTK
151 For this reason, this code is not acceptable for ordinary merging into the Ardour libraries.
153 Ardour Developers: you are not obligated to maintain the internals of this
154 implementation in the face of build-time environment changes (e.g. -D
159 CairoWidget::on_expose_event (GdkEventExpose *ev)
161 cairo_rectangle_t expose_area;
162 expose_area.width = ev->area.width;
163 expose_area.height = ev->area.height;
165 #ifdef USE_CAIRO_IMAGE_SURFACE_FOR_CAIRO_WIDGET
166 Cairo::RefPtr<Cairo::Context> cr;
167 if (get_visible_window ()) {
170 if (!_image_surface) {
171 _image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
173 cr = Cairo::Context::create (_image_surface);
175 expose_area.x = ev->area.x;
176 expose_area.y = ev->area.y;
177 cr = get_window()->create_cairo_context ();
180 expose_area.x = ev->area.x;
181 expose_area.y = ev->area.y;
182 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context ();
185 cr->rectangle (expose_area.x, expose_area.y, expose_area.width, expose_area.height);
188 /* paint expose area the color of the parent window bg
191 if (get_visible_window ()) {
192 Gdk::Color bg (get_parent_bg());
193 cr->rectangle (expose_area.x, expose_area.y, expose_area.width, expose_area.height);
194 cr->set_source_rgb (bg.get_red_p(), bg.get_green_p(), bg.get_blue_p());
198 render (cr, &expose_area);
200 #ifdef USE_CAIRO_IMAGE_SURFACE_FOR_CAIRO_WIDGET
201 if(get_visible_window ()) {
202 _image_surface->flush();
203 /* now blit our private surface back to the GDK one */
205 Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context ();
207 cairo_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
208 cairo_context->clip ();
209 cairo_context->set_source (_image_surface, ev->area.x, ev->area.y);
210 cairo_context->set_operator (Cairo::OPERATOR_OVER);
211 cairo_context->paint ();
215 Gtk::Widget* child = get_child ();
218 propagate_expose (*child, ev);
226 /* Ardour mainline: not using Tracks code features.
228 Tracks Developers: please do not modify this version of
229 ::on_expose_event(). The version used by Tracks is before the preceding
230 #else and contains hacks required for the Tracks GUI to work.
234 CairoWidget::on_expose_event (GdkEventExpose *ev)
238 Gtkmm2ext::nsglview_queue_draw (_nsglview, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
242 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
243 Cairo::RefPtr<Cairo::Context> cr;
244 if (getenv("ARDOUR_IMAGE_SURFACE")) {
245 if (!image_surface) {
246 image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
248 cr = Cairo::Context::create (image_surface);
250 cr = get_window()->create_cairo_context ();
252 #elif defined USE_CAIRO_IMAGE_SURFACE
254 if (!image_surface) {
255 image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
258 Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (image_surface);
260 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context ();
263 cr->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
266 cr->clip_preserve ();
268 /* paint expose area the color of the parent window bg
271 Gdk::Color bg (get_parent_bg());
273 cr->set_source_rgb (bg.get_red_p(), bg.get_green_p(), bg.get_blue_p());
276 std::cerr << get_name() << " skipped bg fill\n";
280 cairo_rectangle_t expose_area;
281 expose_area.x = ev->area.x;
282 expose_area.y = ev->area.y;
283 expose_area.width = ev->area.width;
284 expose_area.height = ev->area.height;
286 render (cr, &expose_area);
288 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
289 if (getenv("ARDOUR_IMAGE_SURFACE")) {
291 #if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE
292 image_surface->flush();
293 /* now blit our private surface back to the GDK one */
295 Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context ();
297 cairo_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
298 cairo_context->clip ();
299 cairo_context->set_source (image_surface, 0, 0);
300 cairo_context->set_operator (Cairo::OPERATOR_SOURCE);
301 cairo_context->paint ();
303 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
312 /** Marks the widget as dirty, so that render () will be called on
313 * the next GTK expose event.
317 CairoWidget::set_dirty (cairo_rectangle_t *area)
319 ENSURE_GUI_THREAD (*this, &CairoWidget::set_dirty);
323 // TODO emit QueueDrawArea -> ArdourCanvas::Widget
327 queue_draw_area (area->x, area->y, area->width, area->height);
332 CairoWidget::queue_draw ()
337 Gtk::EventBox::queue_draw ();
341 CairoWidget::queue_resize ()
343 if (QueueResize ()) {
346 Gtk::EventBox::queue_resize ();
349 /** Handle a size allocation.
350 * @param alloc GTK allocation.
353 CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
355 if (!_canvas_widget) {
356 Gtk::EventBox::on_size_allocate (alloc);
358 memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation));
361 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
362 if (getenv("ARDOUR_IMAGE_SURFACE")) {
364 #if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE
365 image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, alloc.get_width(), alloc.get_height());
367 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
371 if (_canvas_widget) {
377 gtk_widget_translate_coordinates(
379 GTK_WIDGET(get_toplevel()->gobj()),
381 Gtkmm2ext::nsglview_resize (_nsglview, xx, yy, alloc.get_width(), alloc.get_height());
388 CairoWidget::get_parent_bg ()
392 parent = get_parent ();
395 void* p = g_object_get_data (G_OBJECT(parent->gobj()), has_cairo_widget_background_info);
398 Glib::RefPtr<Gtk::Style> style = parent->get_style();
399 if (_current_parent != parent) {
400 if (_parent_style_change) _parent_style_change.disconnect();
401 _current_parent = parent;
402 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &CairoWidget::on_style_changed));
404 return style->get_bg (get_state());
407 if (!parent->get_has_window()) {
408 parent = parent->get_parent();
414 if (parent && parent->get_has_window()) {
415 if (_current_parent != parent) {
416 if (_parent_style_change) _parent_style_change.disconnect();
417 _current_parent = parent;
418 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &CairoWidget::on_style_changed));
420 return parent->get_style ()->get_bg (parent->get_state());
423 return get_style ()->get_bg (get_state());
427 CairoWidget::set_active_state (Gtkmm2ext::ActiveState s)
429 if (_active_state != s) {
436 CairoWidget::set_visual_state (Gtkmm2ext::VisualState s)
438 if (_visual_state != s) {
445 CairoWidget::set_active (bool yn)
447 /* this is an API simplification for buttons
448 that only use the Active and Normal states.
452 set_active_state (Gtkmm2ext::ExplicitActive);
454 unset_active_state ();
459 CairoWidget::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
465 CairoWidget::on_realize ()
467 Gtk::EventBox::on_realize();
470 Gtkmm2ext::nsglview_overlay (_nsglview, get_window()->gobj());
476 CairoWidget::on_map ()
478 Gtk::EventBox::on_map();
481 Gtkmm2ext::nsglview_set_visible (_nsglview, true);
482 Gtk::Allocation a = get_allocation();
484 gtk_widget_translate_coordinates(
486 GTK_WIDGET(get_toplevel()->gobj()),
488 Gtkmm2ext::nsglview_resize (_nsglview, xx, yy, a.get_width(), a.get_height());
494 CairoWidget::on_unmap ()
496 Gtk::EventBox::on_unmap();
499 Gtkmm2ext::nsglview_set_visible (_nsglview, false);
505 CairoWidget::on_state_changed (Gtk::StateType)
507 /* this will catch GTK-level state changes from calls like
511 if (get_state() == Gtk::STATE_INSENSITIVE) {
512 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
514 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
521 CairoWidget::set_draw_background (bool yn)
527 CairoWidget::provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg)
529 /* set up @w to be able to provide bg information to
530 any CairoWidgets that are packed inside it.
533 w.modify_bg (Gtk::STATE_NORMAL, bg);
534 w.modify_bg (Gtk::STATE_INSENSITIVE, bg);
535 w.modify_bg (Gtk::STATE_ACTIVE, bg);
536 w.modify_bg (Gtk::STATE_SELECTED, bg);
538 g_object_set_data (G_OBJECT(w.gobj()), has_cairo_widget_background_info, (void*) 0xfeedface);
542 CairoWidget::set_flat_buttons (bool yn)
548 CairoWidget::set_boxy_buttons (bool yn)
555 CairoWidget::set_widget_prelight (bool yn)
557 _widget_prelight = yn;
561 CairoWidget::set_focus_handler (sigc::slot<void,Gtk::Widget*> s)