refactor flat_buttons into cairowidget, and add a canvas-like convenience function...
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / cairo_widget.h
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 #ifndef __gtk2_ardour_cairo_widget_h__
21 #define __gtk2_ardour_cairo_widget_h__
22
23 #include <gtkmm/eventbox.h>
24
25 #include "gtkmm2ext/visibility.h"
26 #include "gtkmm2ext/widget_state.h"
27
28 /** A parent class for widgets that are rendered using Cairo.
29  */
30
31 class LIBGTKMM2EXT_API CairoWidget : public Gtk::EventBox
32 {
33 public:
34         CairoWidget ();
35         virtual ~CairoWidget ();
36
37         void set_dirty ();
38
39         Gtkmm2ext::ActiveState active_state() const { return _active_state; }
40         Gtkmm2ext::VisualState visual_state() const { return _visual_state; }
41         
42         /* derived widgets can override these two to catch 
43            changes in active & visual state
44         */
45         
46         virtual void set_active_state (Gtkmm2ext::ActiveState);
47         virtual void set_visual_state (Gtkmm2ext::VisualState);
48
49         void unset_active_state () { set_active_state (Gtkmm2ext::Off); }
50         void unset_visual_state () { set_visual_state (Gtkmm2ext::NoVisualState); }
51
52         /* this is an API simplification for widgets
53            that only use the Active and Normal active states.
54         */
55         void set_active (bool);
56         bool get_active () { return active_state() != Gtkmm2ext::Off; }
57
58         /* widgets can be told to only draw their "foreground, and thus leave
59            in place whatever background is drawn by their parent. the default
60            is that the widget will fill its event window with the background
61            color of the parent container.
62         */
63
64         void set_draw_background (bool yn);
65
66         sigc::signal<void> StateChanged;
67
68         static void provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg);
69
70         virtual void render (cairo_t *, cairo_rectangle_t*) = 0;
71
72         static void set_flat_buttons (bool yn);
73         static bool flat_buttons() { return _flat_buttons; }
74
75         static void set_source_rgb_a( cairo_t* cr, Gdk::Color, float a=1.0 );
76
77 protected:
78         /** Render the widget to the given Cairo context */
79         virtual bool on_expose_event (GdkEventExpose *);
80         void on_size_allocate (Gtk::Allocation &);
81         void on_state_changed (Gtk::StateType);
82         Gdk::Color get_parent_bg ();
83         
84         /* this is an additional virtual "on_..." method. Glibmm does not
85            provide a direct signal for name changes, so this acts as a proxy.
86         */
87
88         virtual void on_name_changed () {};
89
90         Gtkmm2ext::ActiveState _active_state;
91         Gtkmm2ext::VisualState _visual_state;
92         bool                   _need_bg;
93
94         static bool     _flat_buttons;
95         bool            _grabbed;
96
97   private:
98         Glib::SignalProxyProperty _name_proxy;
99 };
100
101 #endif