Hello and merry christmas to everybody,
I am facing a problem here with Layout of BoxSizer not being updated: I want to
dynamically add/remove widgets to/from a sizer and have the Sizer and Scrollbars
adjust according to the new content of the Sizer. The only way I found to force
Layout updating was to change the size of the toplevel window (see EVT_BUTTON...
below).
Seems to work but looks like a dirty hack.
Being new to Wx I am quite surely missing something here:
Cheers, Christoph
use strict;
use warnings;
package TestApp;
use Wx qw(:sizer wxDefaultPosition wxDefaultSize wxVSCROLL wxHSCROLL
wxDEFAULT_DIALOG_STYLE wxRESIZE_BORDER wxSIZE_FORCE );
use Wx::Event qw/ EVT_CLOSE EVT_BUTTON /;
use base 'Wx::App';
sub OnInit{
my $this = shift;
my $top = Wx::Dialog->new( undef, -1,'TestApp',
wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER );
my $scr = Wx::ScrolledWindow->new($top,-1,
wxDefaultPosition,
wxDefaultSize,
wxHSCROLL | wxVSCROLL
);
$scr->SetScrollRate(5,5);
# a 'toplevel' sizer:
my $sz = Wx::StaticBoxSizer->new( Wx::StaticBox->new($scr,
-1,
'static_box',
),wxVERTICAL);
$scr->SetSizer( $sz );
# one sub-sizer at the top to hold dynamically generated widgets:
my $top_sz = Wx::StaticBoxSizer->new( Wx::StaticBox->new($scr,
-1,
'static_box',
),wxVERTICAL);
$sz->Add( $top_sz, 0, wxALL, 5 );
my $b = Wx::Button->new( $scr, -1, 'Create Label' );
$sz->Add( $b, 0, wxALL, 10 );
# on button press:
# create a new Label in $top_sz and try to properly update the sizer
# and scrollbars
EVT_BUTTON( $scr, $b, sub{
$top_sz->Add( Wx::StaticText->new( $scr,
-1,
"Testlabel" ) ,
0,
wxALL|wxALIGN_LEFT,
5 );
## Layout does not help:
##$top_sz->Layout;
## wxSIZE_FORCE does not help:
#$top->SetSize(-1,-1,-1,-1,wxSIZE_FORCE);
## dirty fix: simulate a manual Window resize:
$top->SetSize(-1,-1,map{$_ + 1} $top->GetSizeWH);
});
EVT_CLOSE( $top, sub{shift->Destroy} );
#$sz->Layout;
#$top_sz->Layout;
## SetSize once explicitly to force Layout:
$top->SetSize(-1,-1,200,200);
$this->SetTopWindow( $top );
$top->Show( 1 );
1;
};
package main;
my $app = TestApp->new;
$app->MainLoop;