App::Harmonograph / lib / Wx / Perl / TextSlider.pm

use v5.12;
use warnings;
use Wx;

package Wx::Perl::TextSlider;
use base qw(Wx::Panel);

sub new {
	my ($class, $parent, $label, $default, $min, $max, $callback) = @_;
	$default = $min if $default < $min;
	$default = $max if $default > $max;
    
	my ($self) = $class->SUPER::new( $parent );
	$self->{'min'} = $min;
	$self->{'max'} = $max;
	$self->{'default'} = $default;
	$callback = sub {} unless ref $callback eq 'CODE'; 
	my $frontlabel = Wx::StaticText->new($self, -1, "$label : ");
	my $slider = $self->{'slider'}= Wx::Slider->new($self,-1,$default,$min,$max);
	$slider->SetMinSize([300,-1]);
	my $text =   $self->{'text'}  = Wx::TextCtrl->new(
			$self, -1, $default, [-1,-1], [40, -1], &Wx::wxTE_PROCESS_ENTER| &Wx::wxTE_RIGHT
	);

	my $sizer = Wx::BoxSizer->new( &Wx::wxHORIZONTAL);
	my $grow_center_all = &Wx::wxGROW | &Wx::wxALL| &Wx::wxALIGN_CENTER_VERTICAL;
	$sizer->Add($frontlabel, 0, $grow_center_all, 10 );
	$sizer->Add($slider,     1, $grow_center_all,  5 );
	$sizer->Add($text,       0, $grow_center_all,  5 );

	Wx::Event::EVT_SCROLL( $slider, sub { # text always shows current value of slider
		$text->SetValue( $slider->GetValue) if $text->GetValue != $slider->GetValue;
	} );
	Wx::Event::EVT_SCROLL_THUMBRELEASE( $slider, sub {
		#return if $slider->GetValue == $text->GetValue;
		$text->SetValue( $slider->GetValue );
		$callback->(); 
	} );
	Wx::Event::EVT_TEXT_ENTER($text, -1, sub {
		return if $slider->GetValue == $text->GetValue;
		$slider->SetValue( $text->GetValue );
		$callback->(); 
	});
	Wx::Event::EVT_KILL_FOCUS($text, sub {
		$_[1]->Skip;
		if ($slider->GetValue != $text->GetValue){
			$slider->SetValue( $text->GetValue );
			$callback->();
		}
	});
	Wx::Event::EVT_MIDDLE_DOWN( $slider, sub { $self->ResetValue; $callback->() } );
	Wx::Event::EVT_LEFT_DCLICK( $slider, sub { $self->ResetValue; $callback->() } );
    
    $self->SetSizerAndFit( $sizer );
    $self->ResetValue();
    $self;
}

sub GetValue {
	my ($self) = shift;
	$self->{'text'}->GetValue;
}
sub SetValue{
	my ($self, $value) = @_;
	$value = 0 unless defined $value;
	$value = 0 + $value;
	return if $value < $self->{'min'};
	return if $value > $self->{'max'};
	$self->{'text'}->SetValue( $value );
	$self->{'slider'}->SetValue( $value );
}
sub ResetValue {
	my ($self) = shift;
	$self->SetValue( $self->{'default'} );
}

1;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.