Merge branch 'export-dialog' into cairocanvas
[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         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;
61
62         render (cr, &expose_area);
63
64         cairo_destroy (cr);
65
66         return true;
67 }
68
69 /** Marks the widget as dirty, so that render () will be called on
70  *  the next GTK expose event.
71  */
72
73 void
74 CairoWidget::set_dirty ()
75 {
76         ENSURE_GUI_THREAD (*this, &CairoWidget::set_dirty);
77         queue_draw ();
78 }
79
80 /** Handle a size allocation.
81  *  @param alloc GTK allocation.
82  */
83 void
84 CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
85 {
86         Gtk::EventBox::on_size_allocate (alloc);
87
88         set_dirty ();
89 }
90
91 Gdk::Color
92 CairoWidget::get_parent_bg ()
93 {
94         Widget* parent;
95
96         parent = get_parent ();
97
98         while (parent) {
99                 void* p = g_object_get_data (G_OBJECT(parent->gobj()), has_cairo_widget_background_info);
100
101                 if (p) {
102                         Glib::RefPtr<Gtk::Style> style = parent->get_style();
103                         return style->get_bg (get_state());
104                 }
105                 
106                 if (!parent->get_has_window()) {
107                         parent = parent->get_parent();
108                 } else {
109                         break;
110                 }
111         }
112
113         if (parent && parent->get_has_window()) {
114                 return parent->get_style ()->get_bg (parent->get_state());
115         } 
116
117         return get_style ()->get_bg (get_state());
118 }
119
120 void
121 CairoWidget::set_active_state (Gtkmm2ext::ActiveState s)
122 {
123         if (_active_state != s) {
124                 _active_state = s;
125                 StateChanged ();
126         }
127 }
128
129 void
130 CairoWidget::set_visual_state (Gtkmm2ext::VisualState s)
131 {
132         if (_visual_state != s) {
133                 _visual_state = s;
134                 StateChanged ();
135         }
136 }
137
138 void
139 CairoWidget::set_active (bool yn)
140 {
141         /* this is an API simplification for buttons
142            that only use the Active and Normal states.
143         */
144
145         if (yn) {
146                 set_active_state (Gtkmm2ext::ExplicitActive);
147         } else {
148                 unset_active_state ();
149         }
150 }
151
152 void
153 CairoWidget::on_state_changed (Gtk::StateType)
154 {
155         /* this will catch GTK-level state changes from calls like
156            ::set_sensitive() 
157         */
158
159         if (get_state() == Gtk::STATE_INSENSITIVE) {
160                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
161         } else {
162                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
163         }
164
165         queue_draw ();
166 }
167
168 void
169 CairoWidget::set_draw_background (bool yn)
170 {
171         _need_bg = yn;
172 }
173
174 void
175 CairoWidget::provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg)
176 {
177         /* set up @w to be able to provide bg information to 
178            any CairoWidgets that are packed inside it.
179         */
180
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);
185
186         g_object_set_data (G_OBJECT(w.gobj()), has_cairo_widget_background_info, (void*) 0xfeedface);
187 }