add on_name_changed() virtual method to CairoWidget
[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 CairoWidget::CairoWidget ()
28         : _active_state (Gtkmm2ext::Off)
29         , _visual_state (Gtkmm2ext::NoVisualState)
30         , _need_bg (true)
31         , _name_proxy (this, X_("name"))
32 {
33         _name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed));
34 }
35
36 CairoWidget::~CairoWidget ()
37 {
38 }
39
40 bool
41 CairoWidget::on_expose_event (GdkEventExpose *ev)
42 {
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);
45         cairo_clip (cr);
46
47         /* paint expose area the color of the parent window bg 
48         */
49         
50         Gdk::Color bg (get_parent_bg());
51         
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());
54         cairo_fill (cr);
55
56         render (cr);
57
58         cairo_destroy (cr);
59
60         return true;
61 }
62
63 /** Marks the widget as dirty, so that render () will be called on
64  *  the next GTK expose event.
65  */
66
67 void
68 CairoWidget::set_dirty ()
69 {
70         ENSURE_GUI_THREAD (*this, &CairoWidget::set_dirty);
71         queue_draw ();
72 }
73
74 /** Handle a size allocation.
75  *  @param alloc GTK allocation.
76  */
77 void
78 CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
79 {
80         Gtk::EventBox::on_size_allocate (alloc);
81
82         set_dirty ();
83 }
84
85 Gdk::Color
86 CairoWidget::get_parent_bg ()
87 {
88         Widget* parent;
89
90         parent = get_parent ();
91
92         while (parent) {
93                 void* p = g_object_get_data (G_OBJECT(parent->gobj()), has_cairo_widget_background_info);
94
95                 if (p) {
96                         Glib::RefPtr<Gtk::Style> style = parent->get_style();
97                         return style->get_bg (get_state());
98                 }
99                 
100                 if (!parent->get_has_window()) {
101                         parent = parent->get_parent();
102                 } else {
103                         break;
104                 }
105         }
106
107         if (parent && parent->get_has_window()) {
108                 return parent->get_style ()->get_bg (parent->get_state());
109         } 
110
111         return get_style ()->get_bg (get_state());
112 }
113
114 void
115 CairoWidget::set_active_state (Gtkmm2ext::ActiveState s)
116 {
117         if (_active_state != s) {
118                 _active_state = s;
119                 StateChanged ();
120         }
121 }
122
123 void
124 CairoWidget::set_visual_state (Gtkmm2ext::VisualState s)
125 {
126         if (_visual_state != s) {
127                 _visual_state = s;
128                 StateChanged ();
129         }
130 }
131
132 void
133 CairoWidget::set_active (bool yn)
134 {
135         /* this is an API simplification for buttons
136            that only use the Active and Normal states.
137         */
138
139         if (yn) {
140                 set_active_state (Gtkmm2ext::ExplicitActive);
141         } else {
142                 unset_active_state ();
143         }
144 }
145
146 void
147 CairoWidget::on_state_changed (Gtk::StateType)
148 {
149         /* this will catch GTK-level state changes from calls like
150            ::set_sensitive() 
151         */
152
153         if (get_state() == Gtk::STATE_INSENSITIVE) {
154                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
155         } else {
156                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
157         }
158
159         queue_draw ();
160 }
161
162 void
163 CairoWidget::set_draw_background (bool yn)
164 {
165         _need_bg = yn;
166 }
167
168 void
169 CairoWidget::provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg)
170 {
171         /* set up @w to be able to provide bg information to 
172            any CairoWidgets that are packed inside it.
173         */
174
175         w.modify_bg (Gtk::STATE_NORMAL, bg);
176         w.modify_bg (Gtk::STATE_INSENSITIVE, bg);
177         w.modify_bg (Gtk::STATE_ACTIVE, bg);
178         w.modify_bg (Gtk::STATE_SELECTED, bg);
179
180         g_object_set_data (G_OBJECT(w.gobj()), has_cairo_widget_background_info, (void*) 0xfeedface);
181 }