I'm not very good at explaining things, but I'l try, wxLC_xREPORT, wxBORDER_SUNKEN,... are constant integers defined in the
Wx package but not imported by default (for example wxLC_xREPORT ==
32). After import you can use the short form. If a constant is not
imported you can prepend the class name like this: Wx::wxLC_xREPORT. (I'm not sure
about the '&').
I did not change everywhere because it was almost midnight and missed it, no other reason :)
The sole purpose of the $idx variable is numbering in the label. It uses a closure , see Closure on Closures
The $list_ctrl variable, in your version of the example, is the reference to
the list control object. It is assigned once at the creation of
the widget object. This object has methods to add columns and rows
to the list, so we need to save it somewhere so we can use it later. One
method is to use a hash reference to save it so i can refer to
the list object later from other subs.
Here is how to add data to columns, also using the shortcut form from Anonymous Monk - (thanks!)
package MyForm;
use strict;
use warnings;
use Wx qw[:everything]; # easy when testing
use Wx::Event qw(EVT_BUTTON);
use base 'Wx::Frame';
use Wx::Perl::ListCtrl;
sub new {
my $class = shift;
my $self = $class->SUPER::new(
undef, # parent window
-1, # ID -1 means any
'List Control Tutorial', # title
[ -1, -1 ], # default position
[ 400, 300 ], # size
);
# Add a panel so it looks correct on all platforms
my $panel = Wx::Panel->new(
$self, # parent window
-1, # ID
);
my $idx = 0;
$self->{list_ctrl} = Wx::Perl::ListCtrl->new(
$panel,
-1, # ID
#'Report', # label
[ 20, 30 ], # position
[ -1, 100 ], # size
wxLC_REPORT | wxBORDER_SUNKEN, # window style
);
print " wxLC_REPORT is ", wxLC_REPORT,"\n";
print " wxBORDER_SUNKEN is ", wxBORDER_SUNKEN,"\n";
$self->{list_ctrl}->InsertColumn( 0, 'Subject' );
$self->{list_ctrl}->InsertColumn( 0, 'Due' );
$self->{list_ctrl}->InsertColumn( 0, 'Location' );
my $btn = Wx::Button->new(
$panel, # parent window
-1, # ID
'Add Line', # label
[ -1, -1 ], # default position
[ -1, -1 ], # default size
);
EVT_BUTTON( $self, $btn, sub { $self->add_line($idx); $idx++; } );
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
$sizer->Add( $self->{list_ctrl}, 0, wxALL | wxEXPAND, 5 );
$sizer->Add( $btn, 0, wxALL | wxCENTER, 5 );
$panel->SetSizer($sizer);
return $self;
}
sub add_line {
my ($self, $item) = @_;
#$item = 0; # unexpected behaviour
print " Inserting item $item\n";
my ($col, $text) = (0, 'text');
$self->{list_ctrl}->InsertStringItem( $item, 'dummy' );
$self->{list_ctrl}->SetItemText( $item, $col , "$text.$item.0" );
$self->{list_ctrl}->SetItemText( $item, $col+1, "$text.$item.1" );
$self->{list_ctrl}->SetItemText( $item, $col+2, "$text.$item.2" );
return;
}
#--
my $app = Wx::SimpleApp->new;
my $frame = MyForm->new;
$frame->Show(1);
$app->MainLoop;
What is interesting and unespected for me, is that item can be 0 and still adds rows, I was expectig to overwrite the first item.
Stefan
Update: Replaced '32' with 'wxLC_REPORT'. I was trying to show that wxLC_REPORT is just a constant, but the result was a wrong example. Also removed 'Wx::' from the other flag. |