1 /*
2 * Copyright (C) 2006 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2017-2018 Robin Gareus <robin@gareus.org>
4 * Copyright (C) 2017 Julien "_FrnchFrgg_" RIVAUD <frnchfrgg@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21
22 #include <iostream>
23 #include <assert.h>
24
25 #include "gtkmm2ext/cairo_widget.h"
26 #include "gtkmm2ext/colors.h"
27 #include "gtkmm2ext/keyboard.h"
28 #include "gtkmm2ext/rgb_macros.h"
29 #include "gtkmm2ext/utils.h"
30
31 #include "widgets/ardour_fader.h"
32
33 using namespace Gtk;
34 using namespace std;
35 using namespace Gtkmm2ext;
36 using namespace ArdourWidgets;
37
38 #define CORNER_RADIUS 2.5
39 #define CORNER_SIZE 2
40 #define CORNER_OFFSET 1
41 #define FADER_RESERVE 6
42
43 std::list<ArdourFader::FaderImage*> ArdourFader::_patterns;
44
ArdourFader(Gtk::Adjustment & adj,int orientation,int fader_length,int fader_girth)45 ArdourFader::ArdourFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
46 : _layout (0)
47 , _tweaks (Tweaks(0))
48 , _adjustment (adj)
49 , _text_width (0)
50 , _text_height (0)
51 , _span (fader_length)
52 , _girth (fader_girth)
53 , _min_span (fader_length)
54 , _min_girth (fader_girth)
55 , _orien (orientation)
56 , _pattern (0)
57 , _hovering (false)
58 , _dragging (false)
59 , _centered_text (true)
60 , _current_parent (0)
61 {
62 _default_value = _adjustment.get_value();
63 update_unity_position ();
64
65 add_events (
66 Gdk::BUTTON_PRESS_MASK
67 | Gdk::BUTTON_RELEASE_MASK
68 | Gdk::POINTER_MOTION_MASK
69 | Gdk::SCROLL_MASK
70 | Gdk::ENTER_NOTIFY_MASK
71 | Gdk::LEAVE_NOTIFY_MASK
72 );
73
74 _adjustment.signal_value_changed().connect (mem_fun (*this, &ArdourFader::adjustment_changed));
75 _adjustment.signal_changed().connect (mem_fun (*this, &ArdourFader::adjustment_changed));
76 signal_grab_broken_event ().connect (mem_fun (*this, &ArdourFader::on_grab_broken_event));
77 if (_orien == VERT) {
78 CairoWidget::set_size_request(_girth, _span);
79 } else {
80 CairoWidget::set_size_request(_span, _girth);
81 }
82 }
83
~ArdourFader()84 ArdourFader::~ArdourFader ()
85 {
86 if (_parent_style_change) _parent_style_change.disconnect();
87 if (_layout) _layout.clear (); // drop reference to existing layout
88 }
89
90 void
flush_pattern_cache()91 ArdourFader::flush_pattern_cache () {
92 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
93 cairo_pattern_destroy ((*f)->pattern);
94 }
95 _patterns.clear();
96 }
97
98
99 cairo_pattern_t*
find_pattern(double afr,double afg,double afb,double abr,double abg,double abb,int w,int h)100 ArdourFader::find_pattern (double afr, double afg, double afb,
101 double abr, double abg, double abb,
102 int w, int h)
103 {
104 for (list<FaderImage*>::iterator f = _patterns.begin(); f != _patterns.end(); ++f) {
105 if ((*f)->matches (afr, afg, afb, abr, abg, abb, w, h)) {
106 return (*f)->pattern;
107 }
108 }
109 return 0;
110 }
111
112 void
create_patterns()113 ArdourFader::create_patterns ()
114 {
115 Gdk::Color c = get_style()->get_fg (get_state());
116 float fr, fg, fb;
117 float br, bg, bb;
118
119 fr = c.get_red_p ();
120 fg = c.get_green_p ();
121 fb = c.get_blue_p ();
122
123 c = get_style()->get_bg (get_state());
124
125 br = c.get_red_p ();
126 bg = c.get_green_p ();
127 bb = c.get_blue_p ();
128
129 cairo_surface_t* surface;
130 cairo_t* tc = 0;
131
132 if (get_width() <= 1 || get_height() <= 1) {
133 return;
134 }
135
136 if ((_pattern = find_pattern (fr, fg, fb, br, bg, bb, get_width(), get_height())) != 0) {
137 /* found it - use it */
138 return;
139 }
140
141 if (_orien == VERT) {
142
143 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width(), get_height() * 2.0);
144 tc = cairo_create (surface);
145
146 /* paint background + border */
147
148 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width(), 0);
149 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.4,bg*0.4,bb*0.4, 1.0);
150 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.25, br*0.6,bg*0.6,bb*0.6, 1.0);
151 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.8,bg*0.8,bb*0.8, 1.0);
152 cairo_set_source (tc, shade_pattern);
153 cairo_rectangle (tc, 0, 0, get_width(), get_height() * 2.0);
154 cairo_fill (tc);
155
156 cairo_pattern_destroy (shade_pattern);
157
158 /* paint lower shade */
159
160 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, get_width() - 2 - CORNER_OFFSET , 0);
161 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
162 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
163 cairo_set_source (tc, shade_pattern);
164 Gtkmm2ext::rounded_top_half_rectangle (tc, CORNER_OFFSET, get_height() + CORNER_OFFSET,
165 get_width() - CORNER_SIZE, get_height(), CORNER_RADIUS);
166 cairo_fill (tc);
167
168 cairo_pattern_destroy (shade_pattern);
169
170 _pattern = cairo_pattern_create_for_surface (surface);
171
172 } else {
173
174 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, get_width() * 2.0, get_height());
175 tc = cairo_create (surface);
176
177 /* paint right shade (background section)*/
178
179 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
180 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, br*0.4,bg*0.4,bb*0.4, 1.0);
181 cairo_pattern_add_color_stop_rgba (shade_pattern, 0.25, br*0.6,bg*0.6,bb*0.6, 1.0);
182 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, br*0.8,bg*0.8,bb*0.8, 1.0);
183 cairo_set_source (tc, shade_pattern);
184 cairo_rectangle (tc, 0, 0, get_width() * 2.0, get_height());
185 cairo_fill (tc);
186
187 /* paint left shade (active section/foreground) */
188
189 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
190 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, fr*0.8,fg*0.8,fb*0.8, 1.0);
191 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, fr*0.6,fg*0.6,fb*0.6, 1.0);
192 cairo_set_source (tc, shade_pattern);
193 Gtkmm2ext::rounded_right_half_rectangle (tc, CORNER_OFFSET, CORNER_OFFSET,
194 get_width() - CORNER_OFFSET, get_height() - CORNER_SIZE, CORNER_RADIUS);
195 cairo_fill (tc);
196 cairo_pattern_destroy (shade_pattern);
197
198 _pattern = cairo_pattern_create_for_surface (surface);
199 }
200
201 /* cache it for others to use */
202
203 _patterns.push_back (new FaderImage (_pattern, fr, fg, fb, br, bg, bb, get_width(), get_height()));
204
205 cairo_destroy (tc);
206 cairo_surface_destroy (surface);
207 }
208
209 void
render(Cairo::RefPtr<Cairo::Context> const & ctx,cairo_rectangle_t * area)210 ArdourFader::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t* area)
211 {
212 cairo_t* cr = ctx->cobj();
213
214 if (!_pattern) {
215 create_patterns();
216 }
217
218 if (!_pattern) {
219 /* this isn't supposed to be happen, but some wackiness whereby
220 * the pixfader ends up with a 1xN or Nx1 size allocation
221 * leads to it. the basic wackiness needs fixing but we
222 * shouldn't crash. just fill in the expose area with
223 * our bg color.
224 */
225
226 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
227 cairo_rectangle (cr, area->x, area->y, area->width, area->height);
228 cairo_fill (cr);
229 return;
230 }
231
232 OnExpose();
233 int ds = display_span ();
234 const float w = get_width();
235 const float h = get_height();
236
237 CairoWidget::set_source_rgb_a (cr, get_parent_bg(), 1);
238 cairo_rectangle (cr, 0, 0, w, h);
239 cairo_fill(cr);
240
241 cairo_set_line_width (cr, 2);
242 cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
243
244 cairo_matrix_t matrix;
245 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
246 // we use a 'trick' here: The stoke is off by .5px but filling the interior area
247 // after a stroke of 2px width results in an outline of 1px
248 cairo_stroke_preserve(cr);
249
250 if (_orien == VERT) {
251
252 if (ds > h - FADER_RESERVE - CORNER_OFFSET) {
253 ds = h - FADER_RESERVE - CORNER_OFFSET;
254 }
255
256 if (!CairoWidget::flat_buttons() ) {
257 cairo_set_source (cr, _pattern);
258 cairo_matrix_init_translate (&matrix, 0, (h - ds));
259 cairo_pattern_set_matrix (_pattern, &matrix);
260 } else {
261 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
262 cairo_fill (cr);
263 CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
264 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, ds + CORNER_OFFSET,
265 w - CORNER_SIZE, h - ds - CORNER_SIZE, CORNER_RADIUS);
266 }
267 cairo_fill (cr);
268
269 } else {
270
271 if (ds < FADER_RESERVE) {
272 ds = FADER_RESERVE;
273 }
274 assert(ds <= w);
275
276 /*
277 * if ds == w, the pattern does not need to be translated
278 * if ds == 0 (or FADER_RESERVE), the pattern needs to be moved
279 * w to the left, which is -w in pattern space, and w in user space
280 * if ds == 10, then the pattern needs to be moved w - 10
281 * to the left, which is -(w-10) in pattern space, which
282 * is (w - 10) in user space
283 * thus: translation = (w - ds)
284 */
285
286 if (!CairoWidget::flat_buttons() ) {
287 cairo_set_source (cr, _pattern);
288 cairo_matrix_init_translate (&matrix, w - ds, 0);
289 cairo_pattern_set_matrix (_pattern, &matrix);
290 } else {
291 CairoWidget::set_source_rgb_a (cr, get_style()->get_bg (get_state()), 1);
292 cairo_fill (cr);
293 CairoWidget::set_source_rgb_a (cr, get_style()->get_fg (get_state()), 1);
294 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET,
295 ds - CORNER_SIZE, h - CORNER_SIZE, CORNER_RADIUS);
296 }
297 cairo_fill (cr);
298 }
299
300 /* draw the unity-position line if it's not at either end*/
301 if (!(_tweaks & NoShowUnityLine) && _unity_loc > CORNER_RADIUS) {
302 cairo_set_line_width(cr, 1);
303 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
304 Gdk::Color c = get_style()->get_fg (Gtk::STATE_ACTIVE);
305 cairo_set_source_rgba (cr, c.get_red_p() * 1.5, c.get_green_p() * 1.5, c.get_blue_p() * 1.5, 0.85);
306 if (_orien == VERT) {
307 if (_unity_loc < h - CORNER_RADIUS) {
308 cairo_move_to (cr, 1.5, _unity_loc + CORNER_OFFSET + .5);
309 cairo_line_to (cr, _girth - 1.5, _unity_loc + CORNER_OFFSET + .5);
310 cairo_stroke (cr);
311 }
312 } else {
313 if (_unity_loc < w - CORNER_RADIUS) {
314 cairo_move_to (cr, _unity_loc - CORNER_OFFSET + .5, 1.5);
315 cairo_line_to (cr, _unity_loc - CORNER_OFFSET + .5, _girth - 1.5);
316 cairo_stroke (cr);
317 }
318 }
319 }
320
321 if (_layout && !_text.empty() && _orien == HORIZ) {
322 Gdk::Color bg_color;
323 cairo_save (cr);
324 if (_centered_text) {
325 /* center text */
326 cairo_move_to (cr, (w - _text_width)/2.0, h/2.0 - _text_height/2.0);
327 bg_color = get_style()->get_bg (get_state());
328 } else if (ds > .5 * w) {
329 cairo_move_to (cr, CORNER_OFFSET + 3, h/2.0 - _text_height/2.0);
330 bg_color = get_style()->get_fg (get_state());
331 } else {
332 cairo_move_to (cr, w - _text_width - CORNER_OFFSET - 3, h/2.0 - _text_height/2.0);
333 bg_color = get_style()->get_bg (get_state());
334 }
335
336 const uint32_t r = bg_color.get_red_p () * 255.0;
337 const uint32_t g = bg_color.get_green_p () * 255.0;
338 const uint32_t b = bg_color.get_blue_p () * 255.0;
339 const uint32_t a = 0xff;
340 uint32_t rgba = RGBA_TO_UINT (r, g, b, a);
341 rgba = contrasting_text_color (rgba);
342 Gdk::Color text_color;
343 text_color.set_rgb ((rgba >> 24)*256, ((rgba & 0xff0000) >> 16)*256, ((rgba & 0xff00) >> 8)*256);
344 CairoWidget::set_source_rgb_a (cr, text_color, 1.);
345 pango_cairo_show_layout (cr, _layout->gobj());
346 cairo_restore (cr);
347 }
348
349 if (!get_sensitive()) {
350 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
351 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.4);
352 cairo_fill (cr);
353 } else if (_hovering && CairoWidget::widget_prelight()) {
354 Gtkmm2ext::rounded_rectangle (cr, CORNER_OFFSET, CORNER_OFFSET, w-CORNER_SIZE, h-CORNER_SIZE, CORNER_RADIUS);
355 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.1);
356 cairo_fill (cr);
357 }
358 }
359
360 void
on_size_request(GtkRequisition * req)361 ArdourFader::on_size_request (GtkRequisition* req)
362 {
363 if (_orien == VERT) {
364 req->width = (_min_girth ? _min_girth : -1);
365 req->height = (_min_span ? _min_span : -1);
366 } else {
367 req->height = (_min_girth ? _min_girth : -1);
368 req->width = (_min_span ? _min_span : -1);
369 }
370 }
371
372 void
on_size_allocate(Gtk::Allocation & alloc)373 ArdourFader::on_size_allocate (Gtk::Allocation& alloc)
374 {
375 int old_girth = _girth;
376 int old_span = _span;
377
378 CairoWidget::on_size_allocate(alloc);
379
380 if (_orien == VERT) {
381 _girth = alloc.get_width ();
382 _span = alloc.get_height ();
383 } else {
384 _girth = alloc.get_height ();
385 _span = alloc.get_width ();
386 }
387
388 if (is_realized() && ((old_girth != _girth) || (old_span != _span))) {
389 /* recreate patterns in case we've changed size */
390 create_patterns ();
391 }
392
393 update_unity_position ();
394 }
395
396 bool
on_grab_broken_event(GdkEventGrabBroken * ev)397 ArdourFader::on_grab_broken_event (GdkEventGrabBroken* ev)
398 {
399 if (_dragging) {
400 remove_modal_grab();
401 _dragging = false;
402 gdk_pointer_ungrab (GDK_CURRENT_TIME);
403 StopGesture ();
404 }
405 return (_tweaks & NoButtonForward) ? true : false;
406 }
407
408 bool
on_button_press_event(GdkEventButton * ev)409 ArdourFader::on_button_press_event (GdkEventButton* ev)
410 {
411 if (ev->type != GDK_BUTTON_PRESS) {
412 if (_dragging) {
413 remove_modal_grab();
414 _dragging = false;
415 gdk_pointer_ungrab (GDK_CURRENT_TIME);
416 StopGesture ();
417 }
418 return (_tweaks & NoButtonForward) ? true : false;
419 }
420
421 if (ev->button != 1 && ev->button != 2) {
422 return false;
423 }
424
425 add_modal_grab ();
426 StartGesture ();
427 _grab_loc = (_orien == VERT) ? ev->y : ev->x;
428 _grab_start = (_orien == VERT) ? ev->y : ev->x;
429 _grab_window = ev->window;
430 _dragging = true;
431 gdk_pointer_grab(ev->window,false,
432 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
433 NULL,NULL,ev->time);
434
435 if (ev->button == 2) {
436 set_adjustment_from_event (ev);
437 }
438
439 return (_tweaks & NoButtonForward) ? true : false;
440 }
441
442 bool
on_button_release_event(GdkEventButton * ev)443 ArdourFader::on_button_release_event (GdkEventButton* ev)
444 {
445 double ev_pos = (_orien == VERT) ? ev->y : ev->x;
446
447 switch (ev->button) {
448 case 1:
449 if (_dragging) {
450 remove_modal_grab();
451 _dragging = false;
452 gdk_pointer_ungrab (GDK_CURRENT_TIME);
453 StopGesture ();
454
455 if (!_hovering) {
456 if (!(_tweaks & NoVerticalScroll)) {
457 Keyboard::magic_widget_drop_focus();
458 }
459 queue_draw ();
460 }
461
462 if (ev_pos == _grab_start) {
463 /* no motion - just a click */
464 ev_pos = rint(ev_pos);
465
466 if (ev->state & Keyboard::TertiaryModifier) {
467 _adjustment.set_value (_default_value);
468 } else if (ev->state & Keyboard::GainFineScaleModifier) {
469 _adjustment.set_value (_adjustment.get_lower());
470 #if 0 // ignore clicks
471 } else if (ev_pos == slider_pos) {
472 ; // click on current position, no move.
473 } else if ((_orien == VERT && ev_pos < slider_pos) || (_orien == HORIZ && ev_pos > slider_pos)) {
474 /* above the current display height, remember X Window coords */
475 _adjustment.set_value (_adjustment.get_value() + _adjustment.get_step_increment());
476 } else {
477 _adjustment.set_value (_adjustment.get_value() - _adjustment.get_step_increment());
478 #endif
479 }
480 }
481 return true;
482 }
483 break;
484
485 case 2:
486 if (_dragging) {
487 remove_modal_grab();
488 _dragging = false;
489 StopGesture ();
490 set_adjustment_from_event (ev);
491 gdk_pointer_ungrab (GDK_CURRENT_TIME);
492 return true;
493 }
494 break;
495
496 default:
497 break;
498 }
499 return false;
500 }
501
502 bool
on_scroll_event(GdkEventScroll * ev)503 ArdourFader::on_scroll_event (GdkEventScroll* ev)
504 {
505 double increment = 0;
506 if (ev->state & Keyboard::GainFineScaleModifier) {
507 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
508 increment = 0.05 * _adjustment.get_step_increment();
509 } else {
510 increment = _adjustment.get_step_increment();
511 }
512 } else {
513 increment = _adjustment.get_page_increment();
514 }
515
516 bool vertical = false;
517 switch (ev->direction) {
518 case GDK_SCROLL_UP:
519 case GDK_SCROLL_DOWN:
520 vertical = !(ev->state & Keyboard::ScrollHorizontalModifier);
521 break;
522 default:
523 break;
524 }
525 if ((_orien == VERT && !vertical) ||
526 ((_tweaks & NoVerticalScroll) && vertical)) {
527 return false;
528 }
529
530 switch (ev->direction) {
531 case GDK_SCROLL_UP:
532 case GDK_SCROLL_RIGHT:
533 _adjustment.set_value (_adjustment.get_value() + increment);
534 break;
535 case GDK_SCROLL_DOWN:
536 case GDK_SCROLL_LEFT:
537 _adjustment.set_value (_adjustment.get_value() - increment);
538 break;
539 default:
540 return false;
541 }
542
543 return true;
544 }
545
546 bool
on_motion_notify_event(GdkEventMotion * ev)547 ArdourFader::on_motion_notify_event (GdkEventMotion* ev)
548 {
549 if (_dragging) {
550 double scale = 1.0;
551 double const ev_pos = (_orien == VERT) ? ev->y : ev->x;
552
553 if (ev->window != _grab_window) {
554 _grab_loc = ev_pos;
555 _grab_window = ev->window;
556 return true;
557 }
558
559 if (ev->state & Keyboard::GainFineScaleModifier) {
560 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
561 scale = 0.005;
562 } else {
563 scale = 0.1;
564 }
565 }
566
567 double const delta = ev_pos - _grab_loc;
568 _grab_loc = ev_pos;
569
570 const double off = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
571 const double span = _span - off;
572 double fract = (delta / span);
573
574 fract = min (1.0, fract);
575 fract = max (-1.0, fract);
576
577 // X Window is top->bottom for 0..Y
578
579 if (_orien == VERT) {
580 fract = -fract;
581 }
582
583 _adjustment.set_value (_adjustment.get_value() + scale * fract * (_adjustment.get_upper() - _adjustment.get_lower()));
584 }
585
586 return true;
587 }
588
589 void
adjustment_changed()590 ArdourFader::adjustment_changed ()
591 {
592 queue_draw ();
593 }
594
595 /** @return pixel offset of the current value from the right or bottom of the fader */
596 int
display_span()597 ArdourFader::display_span ()
598 {
599 float fract = (_adjustment.get_value () - _adjustment.get_lower()) / ((_adjustment.get_upper() - _adjustment.get_lower()));
600 int ds;
601 if (_orien == VERT) {
602 const double off = FADER_RESERVE + CORNER_OFFSET;
603 const double span = _span - off;
604 ds = (int)rint (span * (1.0 - fract));
605 } else {
606 const double off = FADER_RESERVE;
607 const double span = _span - off;
608 ds = (int)rint (span * fract + off);
609 }
610
611 return ds;
612 }
613
614 void
update_unity_position()615 ArdourFader::update_unity_position ()
616 {
617 if (_orien == VERT) {
618 const double span = _span - FADER_RESERVE - CORNER_OFFSET;
619 _unity_loc = (int) rint (span * (1 - ((_default_value - _adjustment.get_lower()) / (_adjustment.get_upper() - _adjustment.get_lower())))) - 1;
620 } else {
621 const double span = _span - FADER_RESERVE;
622 _unity_loc = (int) rint (FADER_RESERVE + (_default_value - _adjustment.get_lower()) * span / (_adjustment.get_upper() - _adjustment.get_lower()));
623 }
624
625 queue_draw ();
626 }
627
628 bool
on_enter_notify_event(GdkEventCrossing *)629 ArdourFader::on_enter_notify_event (GdkEventCrossing*)
630 {
631 _hovering = true;
632 if (!(_tweaks & NoVerticalScroll)) {
633 Keyboard::magic_widget_grab_focus ();
634 }
635 queue_draw ();
636 return false;
637 }
638
639 bool
on_leave_notify_event(GdkEventCrossing *)640 ArdourFader::on_leave_notify_event (GdkEventCrossing*)
641 {
642 if (!_dragging) {
643 _hovering = false;
644 if (!(_tweaks & NoVerticalScroll)) {
645 Keyboard::magic_widget_drop_focus();
646 }
647 queue_draw ();
648 }
649 return false;
650 }
651
652 void
set_adjustment_from_event(GdkEventButton * ev)653 ArdourFader::set_adjustment_from_event (GdkEventButton* ev)
654 {
655 const double off = FADER_RESERVE + ((_orien == VERT) ? CORNER_OFFSET : 0);
656 const double span = _span - off;
657 double fract = (_orien == VERT) ? (1.0 - ((ev->y - off) / span)) : ((ev->x - off) / span);
658
659 fract = min (1.0, fract);
660 fract = max (0.0, fract);
661
662 _adjustment.set_value (fract * (_adjustment.get_upper () - _adjustment.get_lower ()));
663 }
664
665 void
set_default_value(float d)666 ArdourFader::set_default_value (float d)
667 {
668 _default_value = d;
669 update_unity_position ();
670 }
671
672 void
set_tweaks(Tweaks t)673 ArdourFader::set_tweaks (Tweaks t)
674 {
675 bool need_redraw = false;
676 if ((_tweaks & NoShowUnityLine) ^ (t & NoShowUnityLine)) {
677 need_redraw = true;
678 }
679 _tweaks = t;
680 if (need_redraw) {
681 queue_draw();
682 }
683 }
684
685 void
set_text(const std::string & str,bool centered,bool expose)686 ArdourFader::set_text (const std::string& str, bool centered, bool expose)
687 {
688 if (_layout && _text == str) {
689 return;
690 }
691 if (!_layout && !str.empty()) {
692 _layout = Pango::Layout::create (get_pango_context());
693 }
694
695 _text = str;
696 _centered_text = centered;
697 if (_layout) {
698 _layout->set_text (str);
699 _layout->get_pixel_size (_text_width, _text_height);
700 // queue_resize ();
701 if (expose) queue_draw ();
702 }
703 }
704
705 void
on_state_changed(Gtk::StateType old_state)706 ArdourFader::on_state_changed (Gtk::StateType old_state)
707 {
708 Widget::on_state_changed (old_state);
709 create_patterns ();
710 queue_draw ();
711 }
712
713 void
on_style_changed(const Glib::RefPtr<Gtk::Style> & style)714 ArdourFader::on_style_changed (const Glib::RefPtr<Gtk::Style>& style)
715 {
716 CairoWidget::on_style_changed (style);
717 if (_layout) {
718 std::string txt = _layout->get_text();
719 _layout.clear (); // drop reference to existing layout
720 _text = "";
721 set_text (txt, _centered_text, false);
722 queue_resize ();
723 }
724 /* patterns are cached and re-created as needed
725 * during 'expose' in the GUI thread */
726 _pattern = 0;
727 queue_draw ();
728 }
729
730 Gdk::Color
get_parent_bg()731 ArdourFader::get_parent_bg ()
732 {
733 Widget* parent = get_parent ();
734
735 while (parent) {
736 if (parent->get_has_window()) {
737 break;
738 }
739 parent = parent->get_parent();
740 }
741
742 if (parent && parent->get_has_window()) {
743 if (_current_parent != parent) {
744 if (_parent_style_change) _parent_style_change.disconnect();
745 _current_parent = parent;
746 _parent_style_change = parent->signal_style_changed().connect (mem_fun (*this, &ArdourFader::on_style_changed));
747 }
748 return parent->get_style ()->get_bg (parent->get_state());
749 }
750
751 return get_style ()->get_bg (get_state());
752 }
753