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.
20 #include "gtkmm2ext/cairo_widget.h"
21 #include "gtkmm2ext/gui_thread.h"
25 static const char* has_cairo_widget_background_info = "has_cairo_widget_background_info";
27 CairoWidget::CairoWidget ()
28 : _active_state (Gtkmm2ext::Off)
29 , _visual_state (Gtkmm2ext::NoVisualState)
31 , _name_proxy (this, X_("name"))
33 _name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed));
36 CairoWidget::~CairoWidget ()
41 CairoWidget::on_expose_event (GdkEventExpose *ev)
43 cairo_t* cr = gdk_cairo_create (get_window ()->gobj());
44 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
47 /* paint expose area the color of the parent window bg
50 Gdk::Color bg (get_parent_bg());
52 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
53 cairo_set_source_rgb (cr, bg.get_red_p(), bg.get_green_p(), bg.get_blue_p());
56 cairo_rectangle_t expose_area;
57 expose_area.x = ev->area.x;
58 expose_area.y = ev->area.y;
59 expose_area.width = ev->area.width;
60 expose_area.height = ev->area.height;
62 render (cr, &expose_area);
69 /** Marks the widget as dirty, so that render () will be called on
70 * the next GTK expose event.
74 CairoWidget::set_dirty ()
76 ENSURE_GUI_THREAD (*this, &CairoWidget::set_dirty);
80 /** Handle a size allocation.
81 * @param alloc GTK allocation.
84 CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
86 Gtk::EventBox::on_size_allocate (alloc);
92 CairoWidget::get_parent_bg ()
96 parent = get_parent ();
99 void* p = g_object_get_data (G_OBJECT(parent->gobj()), has_cairo_widget_background_info);
102 Glib::RefPtr<Gtk::Style> style = parent->get_style();
103 return style->get_bg (get_state());
106 if (!parent->get_has_window()) {
107 parent = parent->get_parent();
113 if (parent && parent->get_has_window()) {
114 return parent->get_style ()->get_bg (parent->get_state());
117 return get_style ()->get_bg (get_state());
121 CairoWidget::set_active_state (Gtkmm2ext::ActiveState s)
123 if (_active_state != s) {
130 CairoWidget::set_visual_state (Gtkmm2ext::VisualState s)
132 if (_visual_state != s) {
139 CairoWidget::set_active (bool yn)
141 /* this is an API simplification for buttons
142 that only use the Active and Normal states.
146 set_active_state (Gtkmm2ext::ExplicitActive);
148 unset_active_state ();
153 CairoWidget::on_state_changed (Gtk::StateType)
155 /* this will catch GTK-level state changes from calls like
159 if (get_state() == Gtk::STATE_INSENSITIVE) {
160 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
162 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
169 CairoWidget::set_draw_background (bool yn)
175 CairoWidget::provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg)
177 /* set up @w to be able to provide bg information to
178 any CairoWidgets that are packed inside it.
181 w.modify_bg (Gtk::STATE_NORMAL, bg);
182 w.modify_bg (Gtk::STATE_INSENSITIVE, bg);
183 w.modify_bg (Gtk::STATE_ACTIVE, bg);
184 w.modify_bg (Gtk::STATE_SELECTED, bg);
186 g_object_set_data (G_OBJECT(w.gobj()), has_cairo_widget_background_info, (void*) 0xfeedface);