13
0

Abstract base class for ArdourFader

This allows derivative projects to use other fader
render implementations.
This commit is contained in:
Robin Gareus 2024-05-14 20:40:12 +02:00
parent dad32d8b11
commit a0e27495c6
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
7 changed files with 365 additions and 260 deletions

View File

@ -44,40 +44,23 @@ using namespace ArdourWidgets;
std::list<ArdourFader::FaderImage*> ArdourFader::_patterns;
ArdourFader::ArdourFader (Gtk::Adjustment& adj, int orientation, int fader_length, int fader_girth)
: _layout (0)
, _tweaks (Tweaks(0))
, _adjustment (adj)
: FaderWidget (adj, orientation)
, _layout (0)
, _text_width (0)
, _text_height (0)
, _span (fader_length)
, _girth (fader_girth)
, _min_span (fader_length)
, _min_girth (fader_girth)
, _orien (orientation)
, _pattern (0)
, _hovering (false)
, _dragging (false)
, _centered_text (true)
, _current_parent (0)
, have_explicit_bg (false)
, have_explicit_fg (false)
, outline_color (0)
{
_default_value = _adjustment.get_value();
update_unity_position ();
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
);
_adjustment.signal_value_changed().connect (mem_fun (*this, &ArdourFader::adjustment_changed));
_adjustment.signal_changed().connect (mem_fun (*this, &ArdourFader::adjustment_changed));
signal_grab_broken_event ().connect (mem_fun (*this, &ArdourFader::on_grab_broken_event));
if (_orien == VERT) {
CairoWidget::set_size_request(_girth, _span);
} else {
@ -85,7 +68,6 @@ ArdourFader::ArdourFader (Gtk::Adjustment& adj, int orientation, int fader_lengt
}
outline_color = UIConfigurationBase::instance().color ("fader outline");
}
ArdourFader::~ArdourFader ()
@ -102,7 +84,6 @@ ArdourFader::flush_pattern_cache () {
_patterns.clear();
}
cairo_pattern_t*
ArdourFader::find_pattern (double afr, double afg, double afb,
double abr, double abg, double abb,
@ -440,156 +421,6 @@ ArdourFader::on_size_allocate (Gtk::Allocation& alloc)
update_unity_position ();
}
bool
ArdourFader::on_grab_broken_event (GdkEventGrabBroken* ev)
{
if (_dragging) {
remove_modal_grab();
_dragging = false;
gdk_pointer_ungrab (GDK_CURRENT_TIME);
StopGesture (0);
}
return (_tweaks & NoButtonForward) ? true : false;
}
bool
ArdourFader::on_button_press_event (GdkEventButton* ev)
{
if (ev->type != GDK_BUTTON_PRESS) {
if (_dragging) {
remove_modal_grab();
_dragging = false;
gdk_pointer_ungrab (GDK_CURRENT_TIME);
StopGesture (ev->state);
}
return (_tweaks & NoButtonForward) ? true : false;
}
if (ev->button != 1 && ev->button != 2) {
return false;
}
add_modal_grab ();
StartGesture (ev->state);
_grab_loc = (_orien == VERT) ? ev->y : ev->x;
_grab_start = (_orien == VERT) ? ev->y : ev->x;
_grab_window = ev->window;
_dragging = true;
gdk_pointer_grab(ev->window,false,
GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
NULL,NULL,ev->time);
if (ev->button == 2) {
set_adjustment_from_event (ev);
}
return (_tweaks & NoButtonForward) ? true : false;
}
bool
ArdourFader::on_button_release_event (GdkEventButton* ev)
{
double ev_pos = (_orien == VERT) ? ev->y : ev->x;
switch (ev->button) {
case 1:
if (_dragging) {
remove_modal_grab();
_dragging = false;
gdk_pointer_ungrab (GDK_CURRENT_TIME);
StopGesture (ev->state);
if (!_hovering) {
if (!(_tweaks & NoVerticalScroll)) {
Keyboard::magic_widget_drop_focus();
}
queue_draw ();
}
if (ev_pos == _grab_start) {
/* no motion - just a click */
ev_pos = rint(ev_pos);
if (ev->state & Keyboard::TertiaryModifier) {
_adjustment.set_value (_default_value);
} else if (ev->state & Keyboard::GainFineScaleModifier) {
_adjustment.set_value (_adjustment.get_lower());
#if 0 // ignore clicks
} else if (ev_pos == slider_pos) {
; // click on current position, no move.
} else if ((_orien == VERT && ev_pos < slider_pos) || (_orien == HORIZ && ev_pos > slider_pos)) {
/* above the current display height, remember X Window coords */
_adjustment.set_value (_adjustment.get_value() + _adjustment.get_step_increment());
} else {
_adjustment.set_value (_adjustment.get_value() - _adjustment.get_step_increment());
#endif
}
}
return true;
}
break;
case 2:
if (_dragging) {
remove_modal_grab();
_dragging = false;
StopGesture (ev->state);
set_adjustment_from_event (ev);
gdk_pointer_ungrab (GDK_CURRENT_TIME);
return true;
}
break;
default:
break;
}
return false;
}
bool
ArdourFader::on_scroll_event (GdkEventScroll* ev)
{
double increment = 0;
if (ev->state & Keyboard::GainFineScaleModifier) {
if (ev->state & Keyboard::GainExtraFineScaleModifier) {
increment = 0.05 * _adjustment.get_step_increment();
} else {
increment = _adjustment.get_step_increment();
}
} else {
increment = _adjustment.get_page_increment();
}
bool vertical = false;
switch (ev->direction) {
case GDK_SCROLL_UP:
case GDK_SCROLL_DOWN:
vertical = !(ev->state & Keyboard::ScrollHorizontalModifier);
break;
default:
break;
}
if ((_orien == VERT && !vertical) ||
((_tweaks & NoVerticalScroll) && vertical)) {
return false;
}
switch (ev->direction) {
case GDK_SCROLL_UP:
case GDK_SCROLL_RIGHT:
_adjustment.set_value (_adjustment.get_value() + increment);
break;
case GDK_SCROLL_DOWN:
case GDK_SCROLL_LEFT:
_adjustment.set_value (_adjustment.get_value() - increment);
break;
default:
return false;
}
return true;
}
bool
ArdourFader::on_motion_notify_event (GdkEventMotion* ev)
{
@ -633,12 +464,6 @@ ArdourFader::on_motion_notify_event (GdkEventMotion* ev)
return true;
}
void
ArdourFader::adjustment_changed ()
{
queue_draw ();
}
/** @return pixel offset of the current value from the right or bottom of the fader */
int
ArdourFader::display_span ()
@ -672,30 +497,6 @@ ArdourFader::update_unity_position ()
queue_draw ();
}
bool
ArdourFader::on_enter_notify_event (GdkEventCrossing*)
{
_hovering = true;
if (!(_tweaks & NoVerticalScroll)) {
Keyboard::magic_widget_grab_focus ();
}
queue_draw ();
return false;
}
bool
ArdourFader::on_leave_notify_event (GdkEventCrossing*)
{
if (!_dragging) {
_hovering = false;
if (!(_tweaks & NoVerticalScroll)) {
Keyboard::magic_widget_drop_focus();
}
queue_draw ();
}
return false;
}
void
ArdourFader::set_adjustment_from_event (GdkEventButton* ev)
{
@ -716,19 +517,6 @@ ArdourFader::set_default_value (float d)
update_unity_position ();
}
void
ArdourFader::set_tweaks (Tweaks t)
{
bool need_redraw = false;
if ((_tweaks & NoShowUnityLine) ^ (t & NoShowUnityLine)) {
need_redraw = true;
}
_tweaks = t;
if (need_redraw) {
queue_draw();
}
}
void
ArdourFader::set_text (const std::string& str, bool centered, bool expose)
{

View File

@ -0,0 +1,247 @@
/*
* Copyright (C) 2024 Robin Gareus <robin@gareus.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "gtkmm2ext/keyboard.h"
#include "widgets/fader_widget.h"
using namespace Gtk;
using namespace ArdourWidgets;
using namespace Gtkmm2ext;
FaderWidget::FaderWidget (Gtk::Adjustment& adj, int orien)
: _adjustment (adj)
, _tweaks (Tweaks(0))
, _orien (orien)
, _dragging (false)
, _hovering (false)
, _grab_window (0)
, _grab_loc (0)
, _grab_start (0)
{
_default_value = _adjustment.get_value();
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
);
_adjustment.signal_value_changed().connect (mem_fun (*this, &FaderWidget::adjustment_changed));
_adjustment.signal_changed().connect (mem_fun (*this, &FaderWidget::adjustment_changed));
signal_grab_broken_event ().connect (mem_fun (*this, &FaderWidget::on_grab_broken_event));
}
void
FaderWidget::set_tweaks (Tweaks t)
{
bool need_redraw = false;
if ((_tweaks & NoShowUnityLine) ^ (t & NoShowUnityLine)) {
need_redraw = true;
}
_tweaks = t;
if (need_redraw) {
queue_draw();
}
}
bool
FaderWidget::on_button_press_event (GdkEventButton* ev)
{
if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS && (_tweaks & DoubleClickReset)) {
_adjustment.set_value (_default_value);
return true;
}
if (ev->type != GDK_BUTTON_PRESS) {
if (_dragging) {
remove_modal_grab();
_dragging = false;
gdk_pointer_ungrab (GDK_CURRENT_TIME);
StopGesture (ev->state);
}
return (_tweaks & NoButtonForward) ? true : false;
}
if (ev->button != 1 && ev->button != 2) {
return false;
}
add_modal_grab ();
StartGesture (ev->state);
_grab_loc = (_orien == VERT) ? ev->y : ev->x;
_grab_start = (_orien == VERT) ? ev->y : ev->x;
_grab_window = ev->window;
_dragging = true;
gdk_pointer_grab (ev->window,false,
GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
NULL,NULL,ev->time);
if (ev->button == 2) {
set_adjustment_from_event (ev);
}
return (_tweaks & NoButtonForward) ? true : false;
}
bool
FaderWidget::on_enter_notify_event (GdkEventCrossing*)
{
_hovering = true;
if (!(_tweaks & NoVerticalScroll)) {
Keyboard::magic_widget_grab_focus ();
}
queue_draw ();
return false;
}
bool
FaderWidget::on_leave_notify_event (GdkEventCrossing*)
{
if (!_dragging) {
_hovering = false;
if (!(_tweaks & NoVerticalScroll)) {
Keyboard::magic_widget_drop_focus();
}
queue_draw ();
}
return false;
}
bool
FaderWidget::on_button_release_event (GdkEventButton* ev)
{
double ev_pos = (_orien == VERT) ? ev->y : ev->x;
switch (ev->button) {
case 1:
if (_dragging) {
remove_modal_grab();
_dragging = false;
gdk_pointer_ungrab (GDK_CURRENT_TIME);
StopGesture (ev->state);
if (!_hovering) {
if (!(_tweaks & NoVerticalScroll)) {
Keyboard::magic_widget_drop_focus();
}
queue_draw ();
}
if (ev_pos == _grab_start) {
/* no motion - just a click */
ev_pos = rint(ev_pos);
if (ev->state & Keyboard::TertiaryModifier) {
_adjustment.set_value (_default_value);
} else if (ev->state & Keyboard::GainFineScaleModifier) {
_adjustment.set_value (_adjustment.get_lower());
#if 0 // ignore clicks
} else if (ev_pos == slider_pos) {
; // click on current position, no move.
} else if ((_orien == VERT && ev_pos < slider_pos) || (_orien == HORIZ && ev_pos > slider_pos)) {
/* above the current display height, remember X Window coords */
_adjustment.set_value (_adjustment.get_value() + _adjustment.get_step_increment());
} else {
_adjustment.set_value (_adjustment.get_value() - _adjustment.get_step_increment());
#endif
}
}
return true;
}
break;
case 2:
if (_dragging) {
remove_modal_grab();
_dragging = false;
StopGesture (ev->state);
set_adjustment_from_event (ev);
gdk_pointer_ungrab (GDK_CURRENT_TIME);
return true;
}
break;
default:
break;
}
return false;
}
bool
FaderWidget::on_scroll_event (GdkEventScroll* ev)
{
double increment = 0;
if (ev->state & Keyboard::GainFineScaleModifier) {
if (ev->state & Keyboard::GainExtraFineScaleModifier) {
increment = 0.05 * _adjustment.get_step_increment();
} else {
increment = _adjustment.get_step_increment();
}
} else {
increment = _adjustment.get_page_increment();
}
bool vertical = false;
switch (ev->direction) {
case GDK_SCROLL_UP:
case GDK_SCROLL_DOWN:
vertical = !(ev->state & Keyboard::ScrollHorizontalModifier);
break;
default:
break;
}
if ((_orien == VERT && !vertical) ||
((_tweaks & NoVerticalScroll) && vertical)) {
return false;
}
switch (ev->direction) {
case GDK_SCROLL_UP:
case GDK_SCROLL_RIGHT:
_adjustment.set_value (_adjustment.get_value() + increment);
break;
case GDK_SCROLL_DOWN:
case GDK_SCROLL_LEFT:
_adjustment.set_value (_adjustment.get_value() - increment);
break;
default:
return false;
}
return true;
}
void
FaderWidget::adjustment_changed ()
{
queue_draw ();
}
bool
FaderWidget::on_grab_broken_event (GdkEventGrabBroken* ev)
{
if (_dragging) {
remove_modal_grab();
_dragging = false;
gdk_pointer_ungrab (GDK_CURRENT_TIME);
StopGesture (0);
}
return (_tweaks & NoButtonForward) ? true : false;
}

View File

@ -30,8 +30,8 @@
using namespace PBD;
using namespace ArdourWidgets;
SliderController::SliderController (Gtk::Adjustment *adj, std::shared_ptr<PBD::Controllable> mc, int orientation, int fader_length, int fader_girth)
: ArdourFader (*adj, orientation, fader_length, fader_girth)
SliderController::SliderController (Gtk::Adjustment *adj, std::shared_ptr<PBD::Controllable> mc, int orien)
: FaderWidget (*adj, orien)
, _ctrl (mc)
, _ctrl_adj (adj)
, _spin_adj (0, 0, 1.0, .1, .01)
@ -75,7 +75,7 @@ SliderController::on_button_press_event (GdkEventButton *ev)
return true;
}
return ArdourFader::on_button_press_event (ev);
return FaderWidget::on_button_press_event (ev);
}
bool
@ -85,7 +85,7 @@ SliderController::on_enter_notify_event (GdkEventCrossing* ev)
if (c) {
PBD::Controllable::GUIFocusChanged (std::weak_ptr<PBD::Controllable> (c));
}
return ArdourFader::on_enter_notify_event (ev);
return FaderWidget::on_enter_notify_event (ev);
}
bool
@ -94,7 +94,7 @@ SliderController::on_leave_notify_event (GdkEventCrossing* ev)
if (_binding_proxy.get_controllable()) {
PBD::Controllable::GUIFocusChanged (std::weak_ptr<PBD::Controllable> ());
}
return ArdourFader::on_leave_notify_event (ev);
return FaderWidget::on_leave_notify_event (ev);
}
void
@ -133,11 +133,24 @@ SliderController::spin_adjusted ()
VSliderController::VSliderController (Gtk::Adjustment *adj, std::shared_ptr<PBD::Controllable> mc, int fader_length, int fader_girth)
: SliderController (adj, mc, VERT, fader_length, fader_girth)
: FaderWidget (*adj, VERT)
, SliderController (adj, mc, VERT)
, ArdourFader (*adj, VERT, fader_length, fader_girth)
{
}
HSliderController::HSliderController (Gtk::Adjustment *adj, std::shared_ptr<PBD::Controllable> mc, int fader_length, int fader_girth)
: SliderController (adj, mc, HORIZ, fader_length, fader_girth)
: FaderWidget (*adj, HORIZ)
, SliderController (adj, mc, HORIZ)
, ArdourFader (*adj, HORIZ, fader_length, fader_girth)
{
}
#ifdef LIVETRAX
TraxSliderController::TraxSliderController (Gtk::Adjustment *adj, std::shared_ptr<PBD::Controllable> mc, int fader_length, int fader_girth)
: FaderWidget (*adj, VERT)
, SliderController (adj, mc, VERT)
, TraxFader (*adj, fader_length, fader_girth)
{
}
#endif

View File

@ -28,33 +28,22 @@
#include "gtkmm2ext/cairo_widget.h"
#include "gtkmm2ext/colors.h"
#include "widgets/fader_widget.h"
#include "widgets/visibility.h"
namespace ArdourWidgets {
class LIBWIDGETS_API ArdourFader : public CairoWidget
class LIBWIDGETS_API ArdourFader : virtual public FaderWidget
{
public:
ArdourFader (Gtk::Adjustment& adjustment, int orientation, int span, int girth);
virtual ~ArdourFader ();
static void flush_pattern_cache();
sigc::signal<void,int> StartGesture;
sigc::signal<void,int> StopGesture;
sigc::signal<void> OnExpose;
void set_default_value (float);
void set_text (const std::string&, bool centered = true, bool expose = true);
enum Tweaks {
NoShowUnityLine = 0x1,
NoButtonForward = 0x2,
NoVerticalScroll = 0x4,
};
Tweaks tweaks() const { return _tweaks; }
void set_tweaks (Tweaks);
void set_bg (Gtkmm2ext::Color);
void set_fg (Gtkmm2ext::Color);
void unset_bg ();
@ -65,40 +54,21 @@ protected:
void on_size_allocate (Gtk::Allocation& alloc);
void render (Cairo::RefPtr<Cairo::Context> const&, cairo_rectangle_t*);
bool on_grab_broken_event (GdkEventGrabBroken*);
bool on_button_press_event (GdkEventButton*);
bool on_button_release_event (GdkEventButton*);
bool on_motion_notify_event (GdkEventMotion*);
bool on_scroll_event (GdkEventScroll* ev);
bool on_enter_notify_event (GdkEventCrossing* ev);
bool on_leave_notify_event (GdkEventCrossing* ev);
void on_state_changed (Gtk::StateType);
void on_style_changed (const Glib::RefPtr<Gtk::Style>&);
enum Orientation {
VERT,
HORIZ,
};
private:
Glib::RefPtr<Pango::Layout> _layout;
std::string _text;
Tweaks _tweaks;
Gtk::Adjustment& _adjustment;
int _text_width;
int _text_height;
int _span, _girth;
int _min_span, _min_girth;
int _orien;
cairo_pattern_t* _pattern;
bool _hovering;
GdkWindow* _grab_window;
double _grab_loc;
double _grab_start;
bool _dragging;
float _default_value;
int _unity_loc;
bool _centered_text;
@ -113,7 +83,6 @@ private:
uint32_t outline_color;
void create_patterns();
void adjustment_changed ();
void set_adjustment_from_event (GdkEventButton *);
void update_unity_position ();
int display_span ();

View File

@ -0,0 +1,86 @@
/*
* Copyright (C) 2024 Robin Gareus <robin@gareus.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include <gtkmm/adjustment.h>
#include "gtkmm2ext/cairo_widget.h"
#include "widgets/visibility.h"
namespace ArdourWidgets {
class LIBWIDGETS_API FaderWidget : public virtual CairoWidget
{
public:
FaderWidget (Gtk::Adjustment&, int orient);
virtual ~FaderWidget () {};
sigc::signal<void,int> StartGesture;
sigc::signal<void,int> StopGesture;
sigc::signal<void> OnExpose;
virtual void set_default_value (float) = 0;
enum Tweaks {
NoShowUnityLine = 0x1,
NoButtonForward = 0x2,
NoVerticalScroll = 0x4,
DoubleClickReset = 0x8,
};
enum Orientation {
VERT,
HORIZ,
};
void set_tweaks (Tweaks);
Tweaks tweaks() const { return _tweaks; }
virtual void set_bg (Gtkmm2ext::Color) = 0;
virtual void set_fg (Gtkmm2ext::Color) = 0;
virtual void unset_bg () = 0;
virtual void unset_fg () = 0;
protected:
bool on_button_press_event (GdkEventButton*);
bool on_button_release_event (GdkEventButton*);
bool on_enter_notify_event (GdkEventCrossing* ev);
bool on_leave_notify_event (GdkEventCrossing* ev);
bool on_scroll_event (GdkEventScroll* ev);
bool on_grab_broken_event (GdkEventGrabBroken*);
void adjustment_changed ();
virtual void set_adjustment_from_event (GdkEventButton*) = 0;
Gtk::Adjustment& _adjustment;
Tweaks _tweaks;
int _orien;
bool _dragging;
bool _hovering;
float _default_value;
GdkWindow* _grab_window;
double _grab_loc;
double _grab_start;
};
}

View File

@ -29,6 +29,7 @@
#include <gtkmm/adjustment.h>
#include <gtkmm/spinbutton.h>
#include "widgets/fader_widget.h"
#include "widgets/ardour_fader.h"
#include "widgets/binding_proxy.h"
#include "widgets/visibility.h"
@ -39,10 +40,10 @@ namespace PBD {
namespace ArdourWidgets {
class LIBWIDGETS_API SliderController : public ArdourWidgets::ArdourFader
class LIBWIDGETS_API SliderController : virtual public ArdourWidgets::FaderWidget
{
public:
SliderController (Gtk::Adjustment* adj, std::shared_ptr<PBD::Controllable> mc, int orientation, int, int);
SliderController (Gtk::Adjustment* adj, std::shared_ptr<PBD::Controllable> mc, int orien);
virtual ~SliderController () {}
@ -65,13 +66,13 @@ protected:
bool _spin_ignore;
};
class LIBWIDGETS_API VSliderController : public SliderController
class LIBWIDGETS_API VSliderController : public SliderController, public ArdourFader
{
public:
VSliderController (Gtk::Adjustment *adj, std::shared_ptr<PBD::Controllable> mc, int, int);
};
class LIBWIDGETS_API HSliderController : public SliderController
class LIBWIDGETS_API HSliderController : public SliderController, public ArdourFader
{
public:
HSliderController (Gtk::Adjustment *adj, std::shared_ptr<PBD::Controllable> mc, int, int);

View File

@ -29,6 +29,7 @@ widgets_sources = [
'binding_proxy.cc',
'eventboxext.cc',
'choice.cc',
'fader_widget.cc',
'fastmeter.cc',
'frame.cc',
'focus_entry.cc',