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

use v5.12;
use warnings;

package Wx::Perl::RadioGroup;

use base qw(Wx::Panel);
use Scalar::Util qw(blessed looks_like_number);


sub new {
	my ($class, $parent, $label, $default, $orient, $callback) = @_;

	return warn ((caller(0))[3].' needs a Widget ref as first parameter')
		unless blessed($parent) and $parent->isa('Wx::Window');
	return warn ((caller(0))[3].' needs a ARRAY ref as second parameter')
		unless ref $label eq 'ARRAY';

	$default = 0 if not defined $default or not $default;
	for (0 .. $#{$label}){ $default = $_ if $default eq $label->[$_] }
	$orient = &Wx::wxHORIZONTAL unless defined $orient;
	$callback = sub {} if not defined $callback or ref $callback ne 'CODE';
	
	my ($self) = $class->SUPER::new( $parent );
	my ($sizer) = Wx::BoxSizer->new( $orient );
	$self->{'default'} = $default;
	$self->{'label'}   = $label;

	for (0 .. scalar @$label - 1) {
		next unless $label->[$_];
		$self->{'button'}[$_] = Wx::RadioButton->new(
			$self, -1, $label->[$_], [-1,-1],[-1,-1], $_ == 0 ? &Wx::wxRB_GROUP : 0
		);
		$sizer->Add( $self->{'button'}[$_], 0, &Wx::wxGROW | &Wx::wxALL, 5);
		Wx::Event::EVT_RADIOBUTTON($self->{'button'}[$_], -1, $callback );
	}

	$self->SetSizerAndFit( $sizer );
	$self->ResetValue ();
	$self;
}
sub GetSelectedLabel {}

sub GetValue {
	my ($self) = shift;
	for (0 .. scalar @{$self->{'button'}} - 1) {
		return $_ if $self->{'button'}[$_]->GetValue;
	}
	return -1;
}
sub SetValue {
	my ($self, $value) = @_;
	return warn ((caller(0))[3].": $value is outside range of existing widgets")
		if $value < 0 or $value >= scalar @{$self->{'button'}};
	$self->{'button'}[$value]->SetValue(1);
}
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.