Source

Kephra / lib / Kephra / App / Panel.pm

Diff from to

lib/Kephra/App/Panel.pm

 
 package Kephra::App::Panel;
 our @ISA = 'Wx::Panel';
-my $is_widget = Kephra::App::Util::get('is_widget');
+
+my ($is_widget, $is_sizer) = Kephra::App::Util::get('is_widget', 'is_sizer');
 
 
 sub new {
 	my $class = shift;
 	my $parent = shift;
-	my @children = @_;
 	$parent = Kephra::API::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'} = [];
-	for my $child (@_) {
-		$self->append($child);
-	}
+	$self->{'sizer'} = Kephra::App::Sizer->new('vertical');
+	$self->SetSizer( $self->{'sizer'}->bare_sizer );
+	$self->append(@_);
 	$self;
 }
 
-sub prepend {
-	my ($self, @widgets) = @_;
-	$self->_insert({widget => $_, position => 0}) for @widgets;
-}
-sub append {
-	my ($self, @widgets) = @_;
-	$self->_insert({widget => $_, 
-	                position => $self->{'sizer'}->GetChildren || 0}) for @widgets;
-}
-sub append_expanded {
-	my ($self, @widgets) = @_;
-	$self->_insert({
-		widget => $_, 
-		position => $self->{'sizer'}->GetChildren || 0, 
-		proportion => 1                               }) for @widgets;
-}
-sub insert {
-	my ($self, $widget, $position ) = @_;
-	$self->_insert( $widget) if ref $widget eq 'HASH';
-	$self->_insert({widget => $widget, position => $position})
-		if ref $widget and $is_widget->($widget) 
-		or ref $widget eq 'REF'and ref $$widget and $is_widget->( $$widget );
+sub sizer { shift->{'sizer'} }
+
+sub prepend         { shift->insert(  0, 0, @_ ) }
+sub append          { shift->insert( -1, 0, @_ ) }
+sub append_expanded { shift->insert( -1, 1, @_ ) }
+sub insert_before   { my($self)=shift; $self->insert( $self->get_position(shift)  , 1, @_ ) }
+sub insert_after    { my($self)=shift; $self->insert( $self->get_position(shift)+1, 1, @_ ) }
+sub insert          { 
+	my ($self) = shift; 
+	my ($position) = shift;
+	my ($proportion)  = shift;
+	$self->adopt(@_);
+	$self->sizer->insert($position, $proportion, @_);
+	$self
 }
 
-sub _insert { # only one item
-	my ($self, $item) = @_;
-	return Kephra::Log::error('got no hash as item def') unless ref $item eq 'HASH';
-	return Kephra::Log::error('got no proper widget, but '.$item->{'widget'})
-		unless $is_widget->( $item->{'widget'} )
-		or $is_widget->( ${$item->{'widget'}} );
-	# ref to widget ref can signal to expand that widget
-	if (ref $item->{'widget'} eq 'REF'){
-		$item->{'widget'} = ${ $item->{'widget'} };
-		$item->{'proportion'} = 1;
+sub adopt {
+	my ($self) = shift;
+	for my $item (@_) {
+		$self->adopt( @$item ) if ref $item eq 'ARRAY';
+		$item = $item->{'child'} if ref $item eq 'HASH';
+		next unless $is_widget->($item);
+		$item->Reparent($self) unless $item->GetParent eq $self;
 	}
-	my %presets = (
-		position => $self->{'sizer'}->GetChildren || 0,
-		proportion => 0,
-		style => &Wx::wxGROW,
-		border => 0,
-	);
-	for (qw/position proportion style border/)
-		{ $item->{$_} = $presets{$_} unless exists $item->{$_} }
-
-	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) unless $item->{'widget'}->GetParent eq $self;
-	$item->{'position'};
 }
 
 sub remove {}
 sub detach {}
-sub show {
-	my ($self, $widget) = @_;
-	$self->{'sizer'}->Show($widget, 1);
-}
-sub hide {
-	my ($self, $widget) = @_;
-	$self->{'sizer'}->Hide($widget);
-}
-
-sub all_widgets { shift->{'widgets'} }
-sub widget_by_nr {
-	my ($self, $nr) = @_;
-	$self->{'widgets'}[$nr]
+sub show {}
+sub hide {}
+sub all_widgets {  }
+sub widget_by_nr { }
+sub get_position {
+	my ($self) = shift; 
+	my ($child) = shift;
+	$self->sizer->get_position($child);
 }
 
 1;