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

use v5.12;
use warnings;
use Wx;
use Wx::Perl::Smart::Sizer;
use Wx::Perl::Smart::Panel;
use Wx::Perl::Smart::Factory;

package Wx::Perl::Smart::Frame;
our $VERSION = '0.20';
use base qw(Wx::Frame);
use Scalar::Util qw(blessed looks_like_number);

my %widget_io = (
	'Wx::ComboBox' =>1, 'Wx::CheckBox' =>1, 
	'Wx::TextCtrl' =>1, 'Wx::StyledTextCtrl' =>1, 
	'Wx::Perl::TextSlider' => 1, 'Wx::Perl::RadioGroup' =>1,
);

sub new {
	my ($class, $label, $mode) = @_; #$parent, $widgets, $border
	my ($self) = $class->SUPER::new(
		undef, -1, $label, [1,1], [-1,-1],
		&Wx::wxCAPTION | &Wx::wxCLOSE_BOX | &Wx::wxFRAME_TOOL_WINDOW | &Wx::wxSYSTEM_MENU #| &Wx::wxRESIZE_BORDER
	);
	$self->SetIcon( Wx::GetWxPerlIcon() );
	$self->{'mode'} = $mode;
	Wx::InitAllImageHandlers();
	$self;
}

sub SubscribeWidgets {
	my ($self, $widgets) = @_;
	warn ((caller(0))[3].'needs a hashref as parameter') unless ref $widgets eq 'HASH';
	while (my ($name, $widget) = each %$widgets) {
		warn ((caller(0))[3].": $name is already subscribed"), next if $self->{'subwidget'}{$name};
		if (ref $widget eq 'ARRAY'){
			splice (@$widget, 1, 0, $self);          # frames becomes the parent
			$widget = Wx::Perl::Smart::Factory::Make($widget);
		}
		warn ((caller(0))[3].": $widget is not a widget"), next
			unless blessed($widget) and $widget->isa('Wx::Window');
		$self->{'subwidget'}{$name} = $widget;
		$self->{'default_val'}{$name} = $widget->GetValue if $widget_io{ref $widget};
		$widget->Reparent($self);
		$widget->Show(0);
	 }
}
sub UnsubscribeWidgets {
	my ($self) = shift;
	for my $name (@_){
		warn ((caller(0))[3].": no widget subscribed as $name") unless exists $self->{'subwidget'}{$name};
		$self->{'subwidget'}{$name} = '';
		$self->{'default_val'}{$name} = '';
	}
}
sub GetSubscribedWidget {
	my ($self) = shift;
	my @widget;
	for my $name (@_){
		warn ((caller(0))[3].": there is no widget subscribed as $name") unless exists $self->{'subwidget'}{$name};
		push @widget, $self->{'subwidget'}{$name};
	}
	return $widget[0] if scalar @widget == 1;
	return @widget;
}
sub w { shift->GetSubscribedWidget(@_) }
#sub WidgetSubscribed {shift->{'subwidget'}{shift;}}


sub GetValues {
	my ($self) = shift;
	my %value;
	$value{$_} = $self->{'subwidget'}{$_}->GetValue() for keys %{$self->{'default_val'}};
	return \%value;
}
sub SetValues {
	my ($self, $value) = @_;
	return warn ((caller(0))[3].': need a Hashref a parameter') unless ref $value eq 'HASH';
	while (my ($name, $value) = each %$value) {
		warn ((caller(0))[3].": $name is not a subscribed widget"), next
			unless exists $self->{'default_val'}{$name};
		$self->{'subwidget'}{$name}->SetValue(  $value );
	}
	$self;
}
sub ResetValues { $_[0]->ResetValue }     # for naming consistency
sub ResetValue {
	my ($self) = shift;
	my ($name) = shift // ':all';   # value name
	return warn ((caller(0))[3].": $name is not a subscribed widget")
		unless exists $self->{'default_val'}{$name} or $name eq ':all';
	if ($name eq ':all'){
		$self->{'subwidget'}{$_}->SetValue( $self->{'default_val'}{$_} )
			for keys %{ $self->{'default_val'} };
	} 
	else { $self->{'subwidget'}{$name}->SetValue( $self->{'default_val'}{$name} ) }
	$self;
}


sub SetSmartLayout {
	my ($self) = shift;
	$self->SetSizerAndFit( 
		Wx::Perl::Smart::Sizer->new($self, [ 
			Wx::Perl::Smart::Panel->new( $self, [ @_ ] ) 
		])
	);
	$self->Center();
	$self->Show(1);
	$self;

}

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.