new fader design from thorwil, mostly
[ardour.git] / libs / gtkmm2ext / pixfader.cc
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     $Id: fastmeter.h 570 2006-06-07 21:21:21Z sampo $
19 */
20
21
22 #include <iostream>
23 #include <gtkmm2ext/pixfader.h>
24
25 using namespace Gtkmm2ext;
26 using namespace Gtk;
27 using namespace Gdk;
28 using namespace std;
29
30 PixFader::PixFader (Glib::RefPtr<Pixbuf> base, Glib::RefPtr<Pixbuf> handle, Gtk::Adjustment& adj)
31         : adjustment (adj),
32           base_pixbuf (base),
33           handle_pixbuf (handle)
34 {
35         dragging = false;
36         default_value = adjustment.get_value();
37         last_drawn = -1;
38         pixrect.x = 0;
39         pixrect.y = 0;
40         pixrect.width  = base_pixbuf->get_width();
41         pixrect.height  = base_pixbuf->get_height();
42         pixheight = pixrect.height;
43
44         unity_y = (int) rint (pixrect.height - (default_value * pixrect.height));
45
46         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK|Gdk::SCROLL_MASK);
47
48         adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
49         adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
50 }
51
52 PixFader::~PixFader ()
53 {
54 }
55
56 bool
57 PixFader::on_expose_event (GdkEventExpose* ev)
58 {
59         GdkRectangle intersection;
60         GdkRectangle background;
61
62         pixrect.height = display_height ();
63
64         background.x = 0;
65         background.y = 0;
66         background.width = pixrect.width;
67         background.height = pixheight - pixrect.height;
68
69         if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
70                 get_window()->draw_pixbuf (get_style()->get_fg_gc (get_state()), base_pixbuf,
71                                            intersection.x, intersection.y,
72                                            0, 0,
73                                            intersection.width, intersection.height,
74                                            Gdk::RGB_DITHER_NONE, 0, 0);
75                                           
76         } 
77
78         /* recompute the height of the handle area to use X Window's top->bottom coordinate
79            system.
80         */
81
82         pixrect.y = pixheight - pixrect.height;
83         
84         if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
85                 get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), handle_pixbuf, 
86                                           intersection.x, intersection.y,
87                                           0, pixrect.y, 
88                                           intersection.width, intersection.height,
89                                           Gdk::RGB_DITHER_NONE, 0, 0);
90         }
91
92         /* always draw the line */
93
94         get_window()->draw_line (get_style()->get_fg_gc(get_state()), 0, unity_y, pixrect.width, unity_y);
95
96         last_drawn = pixrect.height;
97         return true;
98 }
99
100 void
101 PixFader::on_size_request (GtkRequisition* req)
102 {
103         req->width = base_pixbuf->get_width();
104         req->height = base_pixbuf->get_height ();
105 }
106
107 bool
108 PixFader::on_button_press_event (GdkEventButton* ev)
109 {
110         switch (ev->button) {
111         case 1:
112                 if (!(ev->state & Gdk::SHIFT_MASK)) {
113                         add_modal_grab();
114                         grab_y = ev->y;
115                         grab_start = ev->y;
116                         grab_window = ev->window;
117                         dragging = true;
118                 }
119                 break;
120         default:
121                 break;
122         } 
123                                
124
125         return false;
126 }
127
128 bool
129 PixFader::on_button_release_event (GdkEventButton* ev)
130 {
131         double scale;
132         
133         if (ev->state & GDK_CONTROL_MASK) {
134                 if (ev->state & GDK_MOD1_MASK) {
135                         scale = 0.05;
136                 } else {
137                         scale = 0.1;
138                 }
139         } else {
140                 scale = 1.0;
141         }
142
143         switch (ev->button) {
144         case 1:
145                 if (dragging) {
146                         remove_modal_grab();
147                         dragging = false;
148
149                         if (ev->y == grab_start) {
150                                 /* no motion - just a click */
151                                 double fract;
152                                 
153                                 if (ev->y < (pixheight/2)) {
154                                         /* near the top */
155                                         fract = 1.0;
156                                 } else {
157                                         fract = 1.0 - (ev->y - pixheight);
158                                 }
159
160                                 fract = min (1.0, fract);
161                                 fract = max (0.0, fract);
162
163                                 adjustment.set_value (scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
164                         }
165                 } else {
166                         if (ev->state & Gdk::SHIFT_MASK) {
167                                 adjustment.set_value (default_value);
168                         }
169                 }
170                 break;
171         default:
172                 break;
173         }
174         return false;
175 }
176
177 bool
178 PixFader::on_scroll_event (GdkEventScroll* ev)
179 {
180         double scale;
181         
182         if (ev->state & GDK_CONTROL_MASK) {
183                 if (ev->state & GDK_MOD1_MASK) {
184                         scale = 0.05;
185                 } else {
186                         scale = 0.1;
187                 }
188         } else {
189                 scale = 0.5;
190         }
191
192         switch (ev->direction) {
193
194         case GDK_SCROLL_UP:
195                 /* wheel up */
196                 adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
197                 break;
198         case GDK_SCROLL_DOWN:
199                 /* wheel down */
200                 adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
201                 break;
202         default:
203                 break;
204         }
205         return false;
206 }
207
208 bool
209 PixFader::on_motion_notify_event (GdkEventMotion* ev)
210 {
211         if (dragging) {
212                 double fract;
213                 double delta;
214                 double scale;
215
216                 if (ev->window != grab_window) {
217                         grab_y = ev->y;
218                         grab_window = ev->window;
219                         return true;
220                 }
221                 
222                 if (ev->state & GDK_CONTROL_MASK) {
223                         if (ev->state & GDK_MOD1_MASK) {
224                                 scale = 0.05;
225                         } else {
226                                 scale = 0.1;
227                         }
228                 } else {
229                         scale = 1.0;
230                 }
231
232                 delta = ev->y - grab_y;
233                 grab_y = ev->y;
234
235                 fract = (delta / pixheight);
236
237                 fract = min (1.0, fract);
238                 fract = max (-1.0, fract);
239
240                 // X Window is top->bottom for 0..Y
241                 
242                 fract = -fract;
243
244                 adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
245         }
246
247         return true;
248 }
249
250 void
251 PixFader::adjustment_changed ()
252 {
253         if (display_height() != last_drawn) {
254                 queue_draw ();
255         }
256 }