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

use v5.12;
use warnings;
use Wx;

#use Wx::Perl::RadioGroup;
#use Wx::Perl::DisplaySlider;


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

sub new {
	my ($class, $admin, $parent) = @_;
	$parent = $admin unless defined $parent;
	return warn ((caller(0))[3].': first parameter $admin has to be a smart widget')
		if defined $admin and ref $admin ne 'Wx::Perl::Smart::Frame';
	return warn ((caller(0))[3].': second parameter $parent has to be a widget')
		if defined $parent and not(blessed($parent) and $parent->isa('Wx::Window'));

	my $self = bless { admin => $admin, parent => $parent};
	$self;
}

sub Make {
	my $self = shift;
	# switch for a dual oo and none-oop interface
	if (defined $self and ref $self ne __PACKAGE__){ unshift @_, $self;  $self = '' }

	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';

		# eval string placeholder
		if ($self and $self->{'admin'}){
			for (0 .. $#{ $widget_def}) {
				$widget_def->[$_] = $self->{'admin'}->EvalSubscribedStrings($widget_def->[$_]) unless ref $widget_def->[$_]
			}
		}
		# insert missing parent into widget definition
		unless (blessed($widget_def->[1]) and $widget_def->[1]->isa('Wx::Window')){
			warn ((caller(0))[3].': no parent to insert into widget definition - left out second "new" paramter'), next
				unless $self and $self->{'parent'};
			splice ($widget_def, 1, 0, $self->{'parent'});
		}

		my $widget;
		#if (substr($widget_def->[0],0,3) eq '---' or substr($widget_def->[0],0,3) eq '|||'){	}
		if ($widget_def->[0] eq '-Button'){
			warn ((caller(0))[3].': second parameter of Button definition has to be a string'), next if ref $widget_def->[2]; 
			warn ((caller(0))[3].': third parameter of Button definition is a callback (CODE ref)'), next if 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].': second parameter of CheckBox definition has to be a string'), next if ref $widget_def->[2]; 
			warn ((caller(0))[3].': thrid parameter of CheckBox definition has to be the chacked status (0|1)'), next if not looks_like_number($widget_def->[3]);
			warn ((caller(0))[3].': forth parameter of CheckBox definition has to be a CODE ref'), next if 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 '-Textslider'){

				#$widget = TextSlider->new ($widget_def->[1], @{$range_defaults{$_}}, $repaint);
		} elsif ($widget_def->[0] eq '-ComboBox'){
			warn ((caller(0))[3].': second paramter in ComboBox definition has to be an ARRAY ref'), next if 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;
}

sub GetWidgetByNr {
	my $self = shift;
	
}

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.