2 Copyright (C) 2006 Paul Davis
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.
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.
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.
18 $Id: fastmeter.h 570 2006-06-07 21:21:21Z sampo $
23 #include "gtkmm2ext/pixfader.h"
24 #include "gtkmm2ext/keyboard.h"
26 using namespace Gtkmm2ext
;
32 int PixFader::fine_scale_modifier
= Keyboard::PrimaryModifier
;
33 int PixFader::extra_fine_scale_modifier
= Keyboard::SecondaryModifier
;
35 PixFader::PixFader (Glib::RefPtr
<Pixbuf
> belt
, Gtk::Adjustment
& adj
, int orientation
, int fader_length
)
42 default_value
= adjustment
.get_value();
48 if (orientation
== VERT
) {
49 view
.width
= girth
= pixbuf
->get_width();
51 view
.height
= girth
= pixbuf
->get_height();
54 set_fader_length (fader_length
);
56 add_events (Gdk::BUTTON_PRESS_MASK
|Gdk::BUTTON_RELEASE_MASK
|Gdk::POINTER_MOTION_MASK
|Gdk::SCROLL_MASK
|Gdk::ENTER_NOTIFY_MASK
|Gdk::LEAVE_NOTIFY_MASK
);
58 adjustment
.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed
));
59 adjustment
.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed
));
62 PixFader::~PixFader ()
67 PixFader::on_expose_event (GdkEventExpose
* ev
)
69 GdkRectangle intersection
;
72 int const ds
= display_span ();
74 int offset_into_pixbuf
= (int) floor (span
/ ((float) span
/ ds
));
76 /* account for fader lengths that are shorter than the fader pixbuf */
78 offset_into_pixbuf
+= pixbuf
->get_height() / 2 - view
.height
;
80 offset_into_pixbuf
+= pixbuf
->get_width() / 2 - view
.width
;
83 Glib::RefPtr
<Gdk::GC
> fg_gc (get_style()->get_fg_gc(get_state()));
85 if (gdk_rectangle_intersect (&view
, &ev
->area
, &intersection
)) {
87 srcx
= intersection
.x
;
88 srcy
= offset_into_pixbuf
+ intersection
.y
;
90 srcx
= offset_into_pixbuf
+ intersection
.x
;
91 srcy
= intersection
.y
;
93 get_window()->draw_pixbuf (fg_gc
, pixbuf
,
95 intersection
.x
, intersection
.y
,
96 intersection
.width
, intersection
.height
,
97 Gdk::RGB_DITHER_NONE
, 0, 0);
99 get_window()->draw_line (get_style()->get_bg_gc(STATE_ACTIVE
), 0, 0, view
.width
- 1, 0); /* top */
100 get_window()->draw_line (get_style()->get_bg_gc(STATE_ACTIVE
), 0, 0, 0, view
.height
- 1); /* left */
101 get_window()->draw_line (get_style()->get_bg_gc(STATE_NORMAL
), view
.width
- 1, 0, view
.width
- 1, view
.height
- 1); /* right */
102 get_window()->draw_line (get_style()->get_bg_gc(STATE_NORMAL
), 0, view
.height
- 1, view
.width
- 1, view
.height
- 1); /* bottom */
105 /* always draw the line */
106 if (_orien
== VERT
) {
107 get_window()->draw_line (fg_gc
, 1, unity_loc
, girth
- 2, unity_loc
);
109 get_window()->draw_line (fg_gc
, unity_loc
, 1, unity_loc
, girth
- 2);
116 PixFader::on_size_request (GtkRequisition
* req
)
118 req
->width
= view
.width
;
119 req
->height
= view
.height
;
123 PixFader::on_button_press_event (GdkEventButton
* ev
)
125 switch (ev
->button
) {
129 grab_loc
= (_orien
== VERT
) ? ev
->y
: ev
->x
;
130 grab_start
= (_orien
== VERT
) ? ev
->y
: ev
->x
;
131 grab_window
= ev
->window
;
143 PixFader::on_button_release_event (GdkEventButton
* ev
)
145 double fract
, ev_pos
;
147 ev_pos
= (_orien
== VERT
) ? ev
->y
: ev
->x
;
149 switch (ev
->button
) {
155 if (ev_pos
== grab_start
) {
157 /* no motion - just a click */
159 if (ev
->state
& Keyboard::TertiaryModifier
) {
160 adjustment
.set_value (default_value
);
161 } else if (ev
->state
& fine_scale_modifier
) {
162 adjustment
.set_value (adjustment
.get_lower());
163 } else if ((_orien
== VERT
&& ev_pos
< span
- display_span()) || (_orien
== HORIZ
&& ev_pos
> span
- display_span())) {
164 /* above the current display height, remember X Window coords */
165 adjustment
.set_value (adjustment
.get_value() + adjustment
.get_step_increment());
167 adjustment
.set_value (adjustment
.get_value() - adjustment
.get_step_increment());
179 fract
= 1.0 - (ev_pos
/ span
); // inverted X Window coordinates, grrr
181 fract
= min (1.0, fract
);
182 fract
= max (0.0, fract
);
184 adjustment
.set_value (fract
* (adjustment
.get_upper() - adjustment
.get_lower()));
196 PixFader::on_scroll_event (GdkEventScroll
* ev
)
201 if (ev
->state
& fine_scale_modifier
) {
202 if (ev
->state
& extra_fine_scale_modifier
) {
211 if (_orien
== VERT
) {
213 /* should left/right scroll affect vertical faders ? */
215 switch (ev
->direction
) {
218 adjustment
.set_value (adjustment
.get_value() + (adjustment
.get_page_increment() * scale
));
221 case GDK_SCROLL_DOWN
:
222 adjustment
.set_value (adjustment
.get_value() - (adjustment
.get_page_increment() * scale
));
230 /* up/down scrolls should definitely affect horizontal faders
231 because they are so much easier to use
234 switch (ev
->direction
) {
236 case GDK_SCROLL_RIGHT
:
238 adjustment
.set_value (adjustment
.get_value() + (adjustment
.get_page_increment() * scale
));
241 case GDK_SCROLL_LEFT
:
242 case GDK_SCROLL_DOWN
:
243 adjustment
.set_value (adjustment
.get_value() - (adjustment
.get_page_increment() * scale
));
254 PixFader::on_motion_notify_event (GdkEventMotion
* ev
)
257 double fract
, delta
, scale
, ev_pos
;
258 ev_pos
= (_orien
== VERT
) ? ev
->y
: ev
->x
;
259 //cerr << "PixFader::on_motion_notify_event() called x:y = " << ev->x << ":" << ev->y;
260 if (ev
->window
!= grab_window
) {
262 grab_window
= ev
->window
;
266 if (ev
->state
& fine_scale_modifier
) {
267 if (ev
->state
& extra_fine_scale_modifier
) {
275 //cerr << " ev_pos=" << ev_pos << " grab_loc=" << grab_loc;
276 delta
= ev_pos
- grab_loc
;
279 fract
= (delta
/ span
);
281 fract
= min (1.0, fract
);
282 fract
= max (-1.0, fract
);
284 // X Window is top->bottom for 0..Y
286 if (_orien
== VERT
) {
290 adjustment
.set_value (adjustment
.get_value() + scale
* fract
* (adjustment
.get_upper() - adjustment
.get_lower()));
291 //cerr << " adj=" << adjustment.get_value() << " fract=" << fract << " delta=" << delta << " scale=" << scale << endl;
298 PixFader::adjustment_changed ()
300 if (display_span() != last_drawn
) {
305 /** @return pixel offset of the current value from the right or bottom of the fader */
307 PixFader::display_span ()
309 float fract
= (adjustment
.get_upper() - adjustment
.get_value ()) / ((adjustment
.get_upper() - adjustment
.get_lower()));
310 return (_orien
== VERT
) ? (int)floor (span
* (1.0 - fract
)) : (int)floor (span
* fract
);
314 PixFader::set_fader_length (int l
)
316 if (_orien
== VERT
) {
317 view
.height
= span
= l
;
318 unity_loc
= (int) rint (view
.height
- (default_value
* view
.height
)) - 1;
320 view
.width
= span
= l
;
321 unity_loc
= (int) rint (default_value
* view
.width
) - 1;
328 PixFader::on_enter_notify_event (GdkEventCrossing
*)
330 Keyboard::magic_widget_grab_focus ();
335 PixFader::on_leave_notify_event (GdkEventCrossing
*)
337 Keyboard::magic_widget_drop_focus();