Merge branch 'master' into cairocanvas
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / pixfader.h
1 /*
2     Copyright (C) 2006 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 __gtkmm2ext_pixfader_h__
21 #define __gtkmm2ext_pixfader_h__
22
23 #include <cmath>
24 #include <stdint.h>
25
26 #include <gtkmm/drawingarea.h>
27 #include <gtkmm/adjustment.h>
28 #include <gdkmm.h>
29
30 namespace Gtkmm2ext {
31
32 class PixFader : public Gtk::DrawingArea
33 {
34   public:
35         PixFader (Gtk::Adjustment& adjustment, int orientation, int span, int girth);
36         virtual ~PixFader ();
37
38         void set_default_value (float);
39         void set_text (const std::string&);
40
41   protected:
42         Glib::RefPtr<Pango::Layout> _layout;
43         std::string                 _text;
44         int   _text_width;
45         int   _text_height;
46         double text_r;
47         double text_g;
48         double text_b;
49
50         Gtk::Adjustment& adjustment;
51
52         void on_size_request (GtkRequisition*);
53         void on_size_allocate (Gtk::Allocation& alloc);
54
55         bool on_expose_event (GdkEventExpose*);
56         bool on_button_press_event (GdkEventButton*);
57         bool on_button_release_event (GdkEventButton*);
58         bool on_motion_notify_event (GdkEventMotion*);
59         bool on_scroll_event (GdkEventScroll* ev);
60         bool on_enter_notify_event (GdkEventCrossing* ev);
61         bool on_leave_notify_event (GdkEventCrossing* ev);
62         void on_state_changed (Gtk::StateType);
63         void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
64
65         enum Orientation {
66                 VERT,
67                 HORIZ,
68         };
69
70   private:
71         int span, girth;
72         int _orien;
73         cairo_pattern_t* pattern;
74
75         struct FaderImage {
76             cairo_pattern_t* pattern;
77             double fr;
78             double fg;
79             double fb;
80             double br;
81             double bg;
82             double bb;
83             int width;
84             int height;
85
86             FaderImage (cairo_pattern_t* p, 
87                         double afr, double afg, double afb, 
88                         double abr, double abg, double abb,
89                         int w, int h) 
90                     : pattern (p)
91                     , fr (afr)
92                     , fg (afg)
93                     , fb (afb)
94                     , br (abr)
95                     , bg (abg)
96                     , bb (abb)
97                     , width (w)
98                     , height (h)
99             {}
100
101             bool matches (double afr, double afg, double afb, 
102                           double abr, double abg, double abb,
103                           int w, int h) {
104                     return width == w && 
105                             height == h &&
106                             afr == fr &&
107                             afg == fg && 
108                             afb == fb &&
109                             abr == br &&
110                             abg == bg && 
111                             abb == bb;
112             }
113         };
114
115         static std::list<FaderImage*> _patterns;
116         static cairo_pattern_t* find_pattern (double afr, double afg, double afb, 
117                                               double abr, double abg, double abb, 
118                                               int w, int h);
119
120         bool _hovering;
121
122         GdkWindow* grab_window;
123         double grab_loc;
124         double grab_start;
125         int last_drawn;
126         bool dragging;
127         float default_value;
128         int unity_loc;
129
130         void adjustment_changed ();
131         int display_span ();
132         void set_adjustment_from_event (GdkEventButton *);
133         void update_unity_position ();
134         void create_patterns();
135 };
136
137
138 } /* namespace */
139
140  #endif /* __gtkmm2ext_pixfader_h__ */