2 Copyright (C) 2014 Waves Audio Ltd.
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 $
24 #include "pbd/stacktrace.h"
26 #include "gtkmm2ext/fader.h"
27 #include "gtkmm2ext/keyboard.h"
28 #include "gtkmm2ext/rgb_macros.h"
29 #include "gtkmm2ext/utils.h"
30 #include "pbd/failed_constructor.h"
31 #include "pbd/file_utils.h"
32 #include "ardour/filesystem_paths.h"
34 using namespace Gtkmm2ext;
38 #define CORNER_RADIUS 4
40 #define CORNER_OFFSET 1
41 #define FADER_RESERVE 5
44 static void get_closest_point_on_line(double xa, double ya, double xb, double yb, double xp, double yp, double& xl, double& yl )
46 // Storing vector A->B
47 double a_to_b_x = xb - xa;
48 double a_to_b_y = yb - ya;
50 // Storing vector A->P
51 double a_to_p_x = xp - xa;
52 double a_to_p_y = yp - ya;
55 // Basically finding the squared magnitude
57 double atb2 = a_to_b_x * a_to_b_x + a_to_b_y * a_to_b_y;
59 // The dot product of a_to_p and a_to_b
60 double atp_dot_atb = a_to_p_x * a_to_b_x + a_to_p_y * a_to_b_y;
62 // The normalized "distance" from a to
64 double t = atp_dot_atb / atb2;
66 // Add the distance to A, moving
68 double x = xa + a_to_b_x * t;
69 double y = ya + a_to_b_y * t;
72 if ((x < xa) && (x < xb)) {
80 } else if ((x > xa) && (x > xb)) {
90 if ((y < ya) && (y < yb)) {
98 } else if ((y > ya) && (y > yb)) {
113 Fader::Fader (Gtk::Adjustment& adj,
114 const Glib::RefPtr<Gdk::Pixbuf>& face_pixbuf,
115 const Glib::RefPtr<Gdk::Pixbuf>& active_face_pixbuf,
116 const Glib::RefPtr<Gdk::Pixbuf>& underlay_pixbuf,
117 const Glib::RefPtr<Gdk::Pixbuf>& handle_pixbuf,
118 const Glib::RefPtr<Gdk::Pixbuf>& active_handle_pixbuf,
125 , _face_pixbuf (face_pixbuf)
126 , _active_face_pixbuf (active_face_pixbuf)
127 , _underlay_pixbuf (underlay_pixbuf)
128 , _handle_pixbuf (handle_pixbuf)
129 , _active_handle_pixbuf (active_handle_pixbuf)
130 , _min_pos_x (min_pos_x)
131 , _min_pos_y (min_pos_y)
132 , _max_pos_x (max_pos_x)
133 , _max_pos_y (max_pos_y)
137 , _default_value (adjustment.get_value())
138 , _read_only (read_only)
140 update_unity_position ();
143 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);
146 adjustment.signal_value_changed().connect (mem_fun (*this, &Fader::adjustment_changed));
147 adjustment.signal_changed().connect (mem_fun (*this, &Fader::adjustment_changed));
148 CairoWidget::set_size_request(_face_pixbuf->get_width(), _face_pixbuf->get_height());
154 delete _touch_cursor;
159 Fader::get_image_scales (double &x_scale, double &y_scale)
161 int pbwidth = _face_pixbuf->get_width ();
162 int pbheight = _face_pixbuf->get_height ();
163 int width = get_width ();
164 int height = get_height ();
166 if ((width != pbwidth) || (height != pbheight)) {
167 x_scale = double (width) / double (pbwidth);
168 if (x_scale == 0.0) {
171 y_scale = double (height) / double (pbheight);
172 if (y_scale == 0.0) {
176 x_scale = y_scale = 1.0;
181 Fader::set_touch_cursor (const Glib::RefPtr<Gdk::Pixbuf>& touch_cursor)
183 _touch_cursor = new Gdk::Cursor (Gdk::Display::get_default(), touch_cursor, 12, 12);
187 Fader::render (cairo_t* cr, cairo_rectangle_t*)
193 get_image_scales (xscale, yscale);
195 cairo_matrix_t matrix;
196 cairo_get_matrix (cr, &matrix);
197 cairo_matrix_scale (&matrix, xscale, yscale);
198 cairo_set_matrix (cr, &matrix);
200 get_handle_position (_last_drawn_x, _last_drawn_y);
202 if (_underlay_pixbuf != 0) {
203 gdk_cairo_set_source_pixbuf (cr,
204 _underlay_pixbuf->gobj(),
205 (_last_drawn_x - (int)((_underlay_pixbuf->get_width() * xscale) / 2.0 + 0.5)) / xscale,
206 (_last_drawn_y - (int)((_underlay_pixbuf->get_height() * yscale) / 2.0 + 0.5)) / yscale);
210 gdk_cairo_set_source_pixbuf (cr,
211 ((get_state () == Gtk::STATE_ACTIVE) && (_active_face_pixbuf != 0)) ?
212 _active_face_pixbuf->gobj() :
213 _face_pixbuf->gobj(),
218 const Glib::RefPtr<Gdk::Pixbuf> handle_pixbuf (_dragging ? _active_handle_pixbuf : _handle_pixbuf);
219 gdk_cairo_set_source_pixbuf (cr,
220 handle_pixbuf->gobj(),
221 (_last_drawn_x - (int)((handle_pixbuf->get_width() * xscale) / 2.0 + 0.5)) / xscale,
222 (_last_drawn_y - (int)((handle_pixbuf->get_height() * yscale) / 2.0 + 0.5)) / yscale);
227 Fader::on_size_request (GtkRequisition* req)
229 req->width = _face_pixbuf->get_width();
230 req->height = _face_pixbuf->get_height();
234 Fader::on_size_allocate (Gtk::Allocation& alloc)
236 CairoWidget::on_size_allocate(alloc);
237 update_unity_position ();
241 Fader::on_button_press_event (GdkEventButton* ev)
249 if (ev->type != GDK_BUTTON_PRESS) {
253 if (ev->button != 1 && ev->button != 2) {
258 get_window()->set_cursor (*_touch_cursor);
261 _grab_start_mouse_x = ev->x;
262 _grab_start_mouse_y = ev->y;
263 get_handle_position (_grab_start_handle_x, _grab_start_handle_y);
268 get_image_scales (xscale, yscale);
270 double hw = _handle_pixbuf->get_width() * xscale;
271 double hh = _handle_pixbuf->get_height() * yscale;
273 if ((ev->x < (_grab_start_handle_x - hw/2)) || (ev->x > (_grab_start_handle_x + hw/2)) || (ev->y < (_grab_start_handle_y - hh/2)) || (ev->y > (_grab_start_handle_y + hh/2))) {
280 get_closest_point_on_line(_min_pos_x, _min_pos_y,
281 _max_pos_x, _max_pos_y,
283 ev_pos_x, ev_pos_y );
286 _grab_window = ev->window;
289 gdk_pointer_grab(ev->window,false,
290 GdkEventMask (Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK),
301 Fader::on_button_release_event (GdkEventButton* ev)
308 get_window()->set_cursor ();
311 if (_dragging) { //temp
314 gdk_pointer_ungrab (GDK_CURRENT_TIME);
321 Fader::on_scroll_event (GdkEventScroll* ev)
329 switch (ev->direction) {
330 case GDK_SCROLL_RIGHT:
333 if ( ev->state & GDK_SHIFT_MASK ) {
342 case GDK_SCROLL_LEFT:
343 case GDK_SCROLL_DOWN:
345 if ( ev->state & GDK_SHIFT_MASK ) {
357 adjustment.set_value (adjustment.get_value() + step_factor * (adjustment.get_step_increment() ));
362 Fader::on_motion_notify_event (GdkEventMotion* ev)
372 if (ev->window != _grab_window) {
373 _grab_window = ev->window;
377 get_closest_point_on_line(_min_pos_x, _min_pos_y,
378 _max_pos_x, _max_pos_y,
379 _grab_start_handle_x + (ev->x - _grab_start_mouse_x), _grab_start_handle_y + (ev->y - _grab_start_mouse_y),
380 ev_pos_x, ev_pos_y );
382 double const fract = sqrt((ev_pos_x - _min_pos_x) * (ev_pos_x - _min_pos_x) +
383 (ev_pos_y - _min_pos_y) * (ev_pos_y - _min_pos_y)) /
384 sqrt((double)((_max_pos_x - _min_pos_x) * (_max_pos_x - _min_pos_x) +
385 (_max_pos_y - _min_pos_y) * (_max_pos_y - _min_pos_y)));
387 adjustment.set_value (adjustment.get_lower() + (adjustment.get_upper() - adjustment.get_lower()) * fract);
393 Fader::adjustment_changed ()
397 get_handle_position (handle_x, handle_y);
399 if ((handle_x != _last_drawn_x) || (handle_y != _last_drawn_y)) {
404 /** @return pixel offset of the current value from the right or bottom of the fader */
406 Fader::get_handle_position (double& x, double& y)
408 double fract = (adjustment.get_value () - adjustment.get_lower()) / ((adjustment.get_upper() - adjustment.get_lower()));
410 x = (int)(_min_pos_x + (_max_pos_x - _min_pos_x) * fract);
411 y = (int)(_min_pos_y + (_max_pos_y - _min_pos_y) * fract);
415 Fader::on_enter_notify_event (GdkEventCrossing*)
418 Keyboard::magic_widget_grab_focus ();
424 Fader::on_leave_notify_event (GdkEventCrossing*)
432 Keyboard::magic_widget_drop_focus();
439 Fader::set_default_value (float d)
442 update_unity_position ();
446 Fader::update_unity_position ()