Herbert Breunung avatar Herbert Breunung committed 946fc94

introducing App::Panel to easu up parenting und sizer management, will become maybe own cpan module

Comments (0)

Files changed (6)

 	App::Panel::TextLab App::Panel::TreeLib
 	CommandList Config 
 	Edit File EventTable PluginRegistrar SandrumInterpreter/;
-our @oop_interfaces = qw/App::Editor App::Bar::Document Document/;
+our @oop_interfaces = qw/App::Editor App::Panel App::Bar::Document Document/;
 
 sub start {
 	start_worker_thread();

lib/Kephra/App/Bar/Document.pm

 use Wx::DND;
 use Kephra::API;
 use Kephra::App::Editor;
+use Kephra::App::Panel;
 use Kephra::App::Util;
 use Kephra::DocumentStash;
 
 	$position = $self->leftmost_panel_pos     if $position eq 'leftmost';
 	$set_active = 1                           unless defined $set_active;
 
-	unless ($new_panel->isa('Wx::Panel')) { # out panel under a none-panel
-		my $real_panel = Wx::Panel->new( $self );
-		$new_panel->Reparent($real_panel);
-		my $sizer = Wx::BoxSizer->new( &Wx::wxVERTICAL );
-		$sizer->Add($new_panel, 1, &Wx::wxGROW );
-		$real_panel->SetSizer($sizer);
-		$new_panel = $real_panel;
-	} 
+	unless ($new_panel->isa('Wx::Panel')) { # put panel under a none-panel
+		$new_panel = Kephra::App::Panel->new( $self, $new_panel);
+	}
 	else { $new_panel->Reparent( $self ) }
+
 	# document property panel ref has to be set before EVT_AUINOTEBOOK_PAGE_CHANGED 
 	$panel_candidate->panel( $new_panel ) if $panel_candidate->isa('Kephra::Document');
 

lib/Kephra/App/Panel.pm

 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->{'widgets'} = [];
+	$self->append(ref $children eq ref [] ? @$children : $children);
+	$self->SetSizer($sizer);
+	$self;
+}
+
+sub append {
+	my ($self, @widgets) = @_;
+	for my $w (@widgets){
+		next unless $w and $is_widget->($w);
+		push @{$self->{'widgets'}}, $w;
+		$self->{'sizer'}->Add($w, 1, &Wx::wxGROW );
+		$w->Reparent($self);
+	}
+}
+
+sub prepend {
+	my ($self, @widgets) = @_;
+	for my $w (@widgets){
+		next unless $w and $is_widget->($w);
+		unshift @{$self->{'widgets'}}, $w;
+		$self->{'sizer'}->Prepend($w, 1, &Wx::wxGROW );
+		$w->Reparent($self);
+	}
+}
+
+sub insert {
+	
+}
+
+sub remove {
+	
+}
+
+sub all_widgets { shift->{'widgets'} }
+sub widget_by_nr {
+	my ($self, $nr) = @_;
+	$self->{'widgets'}[$nr]
+}
 
 1;

lib/Kephra/App/Util.pm

 
 sub is_object { ( ref $_[0] and index(ref $_[0], '=') == -1) ? 1 : 0 }
 sub is_widget { ( is_object( $_[0] ) and $_[0]->isa('Wx::Control') ) ? 1 : 0 }
-sub is_panel  { ( is_object( $_[0] ) and $_[0]->isa('Wx::Panel') ) ? 1 : 0 }
+sub is_panel  {
+	(  ($_[0] ref $_[0] and $_[0]->isa('Wx::Panel') ) ? 1 : 0 
+}
 sub is_sizer  { ( is_object( $_[0] ) and $_[0]->isa('Wx::Sizer') ) ? 1 : 0 }
-
-sub put_on_panel {
-    my ($widget, $parent) = @_;
-    my ($panel, $sizer);
-    my $name = __PACKAGE__ . '::put_on_panel';
-    if ( is_widget($widget) ){
-        $panel = Wx::Panel->new( $widget->GetParent );
-        $widget->Reparent( $panel );
-        $sizer = Wx::BoxSizer->new( &Wx::wxVERTICAL );
-        $sizer->Add( $widget, 1, &Wx::wxGROW );
-    } 
-    elsif ( is_sizer($widget) ) {
-        die "$name needs parent widget when andling a sizer"
-            unless is_widget( $parent );
-        $panel = Wx::Panel->new( $parent );
-        $sizer = $widget;
-        my $cc = 0;
-        while (my $child = $sizer->GetItem($cc++)){
-            $child->Reparent( $panel );
-        }
-    }
-    else { die "$name needs widget or sizer as first parameter" }
-
-    $panel->SetSizer( $sizer );
-    return $panel;
-}
-
-sub is_color { ( is_object( $_[0] ) and $_[0]->isa('Wx::Colour') and $_[0]->IsOk ) ? 1 : 0 }
+sub is_color  { ( is_object( $_[0] ) and $_[0]->isa('Wx::Colour') and $_[0]->IsOk ) ? 1 : 0 }
 
 sub make_color {
     my ($r, $g, $b, $t ) = @_;

lib/Kephra/Document.pm

 );
 has panel => (
 	is  => 'rw',
-	isa => sub {die "$_[0] is no panel" unless ref $_[0] eq 'Wx::Panel'},
+	isa => sub {die "$_[0] is no panel" unless ref $_[0] eq 'Wx::Panel' or ref $_[0] eq 'Kephra::App::Panel'},
 );
 has file_path    => ( is  => 'rw', );
 has file_dir     => ( is  => 'rw', );
 # making local lib favoured
 BEGIN { unshift @INC, -d 't' ? 'lib' : '../lib' }
 
-use Test::More tests => 15;
+use Test::More tests => 12  ;
 #use Text::Exception;
 use Test::NoWarnings;
 
 is( $testcolor->( $mk_color->("blue") ), 1,'"colorname" input layout works');
 is( $testcolor->( $mk_color->("WHEAT") ), 1,'"colorname" input layout works with db names');
 
-# gui contruction helper
-my $popanel = Kephra::App::Util::get('put_on_panel');
-my $testpanel = Kephra::App::Util::get('is_panel');
-my $button = Wx::Button->new(undef,-1,'');
-my $panel = Wx::Panel->new(undef);
-is( $testpanel->( $white ), 0, 'can detect what is not a panel');
-is( $testpanel->( $panel ), 1, 'can detect panel');
-is( $testpanel->( $popanel->($button) ), 1, 'can detect panel');
-
 exit(0);
 
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.