Source

App::Harmonograph / lib / Wx / Perl / Smart / Factory.pm

Full commit
use v5.12;
use warnings;
use Wx;

package Wx::Perl::Smart::Factory;
our $VERSION = '0.1';
use Scalar::Util qw(blessed looks_like_number);

sub Make {
	my @widget;
	for my $widget_def (@_){
		next unless defined $widget_def;
		warn ((caller(0))[3].': need an ARRAY ref as parameter, not $widget_def'), next unless ref $widget_def eq 'ARRAY';
		my $widget;
		if ($widget_def->[0] eq '-Button'){
			warn ((caller(0))[3].': @$widget_def is no proper WxButton definition (-Button => $parent, "label", &callback)'),
				next if not(blessed($widget_def->[1]) and $widget_def->[1]->isa('Wx::Window'))
				or ref $widget_def->[2]
				or ref $widget_def->[3] ne 'CODE';
			$widget = Wx::Button->new($widget_def->[1], -1, $widget_def->[2]);
			Wx::Event::EVT_BUTTON( $widget, $widget, $widget_def->[3] );
		} elsif ($widget_def->[0] eq '-CheckBox'){
			warn ((caller(0))[3].': @$widget_def is no proper Checkbox definition (-CheckBox => $parent, "label", checked?, &callback)'),
				next if not(blessed($widget_def->[1]) and $widget_def->[1]->isa('Wx::Window'))
				or ref $widget_def->[2]
				or not looks_like_number($widget_def->[3])
				or ($widget_def->[4] and ref $widget_def->[4] ne 'CODE');
			$widget = Wx::CheckBox->new($widget_def->[1], -1, $widget_def->[2]);
			$widget->SetValue($widget_def->[3]);
			Wx::Event::EVT_CHECKBOX( $widget, -1, $widget_def->[4] ) if $widget_def->[4];
		} elsif ($widget_def->[0] eq '-ComboBox'){
			warn ((caller(0))[3].': @$widget_def is no proper ComboBox definition (-ComboBox => $parent, [options], "visible value", length, &callback)'),
				next if not(blessed($widget_def->[1]) and $widget_def->[1]->isa('Wx::Window'))
				or ref $widget_def->[2] ne 'ARRAY'
				or ref $widget_def->[3] 
				or ref $widget_def->[4]
				or ($widget_def->[5] and ref $widget_def->[5] ne 'CODE');
			my $textvalue = $widget_def->[3];
			if (not defined $textvalue or not $textvalue){$textvalue = $widget_def->[2][0]   } 
			elsif (looks_like_number($textvalue)) {$textvalue = $widget_def->[2][$textvalue] }
			$textvalue = '' unless defined $textvalue;  # combobox doesnt like an undef in text field
			my $length = $widget_def->[4];
			$length = -1 unless $length;
			$widget = Wx::ComboBox->new(
				$widget_def->[1], -1, $textvalue, [-1,-1], [$length,-1], $widget_def->[2]
			);
			Wx::Event::EVT_COMBOBOX( $widget, -1, $widget_def->[5]) if $widget_def->[5];
		}
		push @widget, $widget if defined $widget and blessed($widget);
	}
	return $widget[0] if scalar @widget == 1;
	return @widget;
}


1;