e2edc24bbcbc401a65b508abc45e710174d8dd5d
[ardour.git] / gtk2_ardour / 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 "cairo_widget.h"
21 #include "gui_thread.h"
22
23 CairoWidget::CairoWidget ()
24         : _width (1)
25         , _height (1)
26         , _active_state (Gtkmm2ext::ActiveState (0))
27         , _visual_state (Gtkmm2ext::VisualState (0))
28 {
29
30 }
31
32 CairoWidget::~CairoWidget ()
33 {
34 }
35
36 bool
37 CairoWidget::on_expose_event (GdkEventExpose *ev)
38 {
39         cairo_t* cr = gdk_cairo_create (get_window ()->gobj());
40         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
41         cairo_clip (cr);
42         render (cr);
43         cairo_destroy (cr);
44
45         return true;
46 }
47
48 /** Marks the widget as dirty, so that render () will be called on
49  *  the next GTK expose event.
50  */
51
52 void
53 CairoWidget::set_dirty ()
54 {
55         ENSURE_GUI_THREAD (*this, &CairoWidget::set_dirty)
56         queue_draw ();
57 }
58
59 /** Handle a size allocation.
60  *  @param alloc GTK allocation.
61  */
62 void
63 CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
64 {
65         Gtk::EventBox::on_size_allocate (alloc);
66
67         _width = alloc.get_width ();
68         _height = alloc.get_height ();
69
70         set_dirty ();
71 }
72
73 Gdk::Color
74 CairoWidget::get_parent_bg ()
75 {
76         Widget* parent;
77
78         parent = get_parent ();
79
80         while (parent && !parent->get_has_window()) {
81                 parent = parent->get_parent();
82         }
83
84         if (parent && parent->get_has_window()) {
85                 return parent->get_style ()->get_bg (parent->get_state());
86         } 
87
88         return get_style ()->get_bg (get_state());
89 }
90
91 void
92 CairoWidget::set_active_state (Gtkmm2ext::ActiveState s)
93 {
94         if (_active_state != s) {
95                 _active_state = s;
96                 StateChanged ();
97         }
98 }
99
100 void
101 CairoWidget::set_visual_state (Gtkmm2ext::VisualState s)
102 {
103         if (_visual_state != s) {
104                 _visual_state = s;
105                 StateChanged ();
106         }
107 }
108
109 void
110 CairoWidget::on_state_changed (Gtk::StateType)
111 {
112         /* this will catch GTK-level state changes from calls like
113            ::set_sensitive() 
114         */
115
116         if (get_state() == Gtk::STATE_INSENSITIVE) {
117                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
118         } else {
119                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
120         }
121
122         queue_draw ();
123 }