Source

Kephra / lib / Kephra / App / Panel.pm

Full commit
use strict;
use warnings;
use Kephra::API;

package Kephra::App::Panel;
our @ISA = 'Wx::Panel';
my $is_widget = Kephra::App::Util::get('is_widget');

sub new {
	my( $class, $parent, $children) = @_;
	$parent = Kephra::API::widget('main_window')
		unless defined $parent and $is_widget->($parent);
	my $self = $class->SUPER::new( $parent );
	my $sizer = $self->{'sizer'} = Wx::BoxSizer->new( &Wx::wxVERTICAL );
	$self->SetSizer($sizer);

	$self->{'widgets'} = [];
	# if list of widgets came in a ref, flat them
	my @children = ref $children eq ref [] ? @$children : $children;
	# if there is only one widget without further info, make it to fill space
	if (@children == 1 and not ref $children[0] eq ref {}){
		$children = $children[0];
		$children[0] = \$children;
	}
	$self->append(@children);
	$self;
}

sub prepend {
	my ($self, @widgets) = @_;
	my %presets = ( position => 0 );
	$self->_insert($_, \%presets) for @widgets;
}

sub append {
	my ($self, @widgets) = @_;
	my %presets = ( position => $self->{'sizer'}->GetChildren || 0 );
	$self->_insert($_, \%presets) for @widgets;
}

sub insert {
	my ($self, $position, @widgets) = @_;
	my %presets = ( position => $position );
	$self->_insert($_, \%presets) for @widgets;
}

sub _insert { # only one item
	my ($self, $item_def, $presets) = @_;
	my %item = (
		position => $self->{'sizer'}->GetChildren || 0,
		proportion => 0,
		style => &Wx::wxGROW,
		border => 0,
	);
	if (ref $item_def eq ref {}) {
		die __PACKAGE__."::_insert got no proper item definition"
			unless $is_widget->( $item_def->{'widget'} );

		for (qw/proportion widget border/)
			{ $item{$_} = $item_def->{$_} if exists $item_def->{$_} }
	} elsif (ref $item_def eq ref \$item_def ) {
		die __PACKAGE__."::_insert got no proper item definition"
			unless $is_widget->($$item_def);

		$item {'widget'} = $$item_def;
		$item {'proportion'} = 1;
	} else {
		die __PACKAGE__."::_insert got no proper item definition"
			unless $is_widget->($item_def);

		$item {'widget'} = $item_def;
	}
	if (defined $presets and ref $presets eq ref {}){
		for (qw/position proportion style border/)
			{ $item{$_} = $presets->{$_} if exists $presets->{$_} }
	}

	splice @{$self->{'widgets'}}, $item{'position'}, 0, $item{'widget'};
	$self->{'sizer'}->Insert(
		$item{'position'}, $item{'widget'}, $item{'proportion'},
		$item{'style'},    $item{'border'}
	);
	$item{'widget'}->Reparent($self) if $is_widget->( $item{'widget'} );
}

sub remove {}
sub show {}

sub all_widgets { shift->{'widgets'} }
sub widget_by_nr {
	my ($self, $nr) = @_;
	$self->{'widgets'}[$nr]
}

1;