and now without debug printf()
[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 "gtkmm2ext/cairo_widget.h"
27 #include <gtkmm/adjustment.h>
28 #include <gdkmm.h>
29
30 #include "gtkmm2ext/visibility.h"
31
32 namespace Gtkmm2ext {
33
34 class LIBGTKMM2EXT_API PixFader : public CairoWidget
35 {
36         public:
37         PixFader (Gtk::Adjustment& adjustment, int orientation, int span, int girth);
38         virtual ~PixFader ();
39         static void flush_pattern_cache();
40
41         sigc::signal<void> StartGesture;
42         sigc::signal<void> StopGesture;
43         sigc::signal<void> OnExpose;
44
45         void set_default_value (float);
46         void set_text (const std::string&, bool centered = true, bool expose = true);
47
48         enum Tweaks {
49                 NoShowUnityLine = 0x1,
50                 NoButtonForward = 0x2,
51                 NoVerticalScroll = 0x4,
52         };
53
54         Tweaks tweaks() const { return _tweaks; }
55         void set_tweaks (Tweaks);
56
57         protected:
58         void on_size_request (GtkRequisition*);
59         void on_size_allocate (Gtk::Allocation& alloc);
60
61         void render (cairo_t *, cairo_rectangle_t*);
62         bool on_grab_broken_event (GdkEventGrabBroken*);
63         bool on_button_press_event (GdkEventButton*);
64         bool on_button_release_event (GdkEventButton*);
65         bool on_motion_notify_event (GdkEventMotion*);
66         bool on_scroll_event (GdkEventScroll* ev);
67         bool on_enter_notify_event (GdkEventCrossing* ev);
68         bool on_leave_notify_event (GdkEventCrossing* ev);
69
70         void on_state_changed (Gtk::StateType);
71         void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
72
73         enum Orientation {
74                 VERT,
75                 HORIZ,
76         };
77
78         private:
79
80         Glib::RefPtr<Pango::Layout> _layout;
81         std::string                 _text;
82         Tweaks                      _tweaks;
83         Gtk::Adjustment&            _adjustment;
84         int _text_width;
85         int _text_height;
86
87         int _span, _girth;
88         int _min_span, _min_girth;
89         int _orien;
90         cairo_pattern_t* _pattern;
91         bool _hovering;
92         GdkWindow* _grab_window;
93         double _grab_loc;
94         double _grab_start;
95         bool _dragging;
96         float _default_value;
97         int _unity_loc;
98         bool _centered_text;
99
100         sigc::connection _parent_style_change;
101         Widget * _current_parent;
102         Gdk::Color get_parent_bg ();
103
104         void create_patterns();
105         void adjustment_changed ();
106         void set_adjustment_from_event (GdkEventButton *);
107         void update_unity_position ();
108         int  display_span ();
109
110         struct FaderImage {
111                 cairo_pattern_t* pattern;
112                 double fr;
113                 double fg;
114                 double fb;
115                 double br;
116                 double bg;
117                 double bb;
118                 int width;
119                 int height;
120
121                 FaderImage (cairo_pattern_t* p,
122                                 double afr, double afg, double afb,
123                                 double abr, double abg, double abb,
124                                 int w, int h)
125                         : pattern (p)
126                                 , fr (afr)
127                                  , fg (afg)
128                                  , fb (afb)
129                                  , br (abr)
130                                  , bg (abg)
131                                  , bb (abb)
132                                  , width (w)
133                                  , height (h)
134                 {}
135
136                 bool matches (double afr, double afg, double afb,
137                                 double abr, double abg, double abb,
138                                 int w, int h) {
139                         return width == w &&
140                                 height == h &&
141                                 afr == fr &&
142                                 afg == fg &&
143                                 afb == fb &&
144                                 abr == br &&
145                                 abg == bg &&
146                                 abb == bb;
147                 }
148         };
149
150         static std::list<FaderImage*> _patterns;
151         static cairo_pattern_t* find_pattern (double afr, double afg, double afb,
152                         double abr, double abg, double abb,
153                         int w, int h);
154
155 };
156
157
158 } /* namespace */
159
160 #endif /* __gtkmm2ext_pixfader_h__ */