#!/usr/bin/perl package wxToplevelFrame; use strict; use warnings; use Wx qw(:everything); use Wx::Grid; use Wx::XRC; use base 'Wx::Frame'; use Data::Dumper; use diagnostics; # import event registration function use Wx::Event qw(:everything); use base qw(Wx::Panel Class::Accessor::Fast); __PACKAGE__->mk_ro_accessors( qw(xrc) ); $| = 1; ### GUI CODE BEGINS ### # create specialized constructor for new subclass sub new { my $class = shift; my $self = $class->SUPER::new(); $self->initialize(); return $self; } sub initialize { my ($self) = @_; my $xrc_path = 'noname.xrc'; Wx::InitAllImageHandlers(); $self->{ xrc } = Wx::XmlResource->new(); $self->xrc->InitAllHandlers(); $self->xrc->Load($xrc_path); $self->xrc->LoadFrame($self, undef, 'MyFrame1',); # main frame close EVT_CLOSE( $self, sub { my ($self) = @_; $self->Destroy(); } ); # LEFT GRID my $grid_left = $self->{ grid_left } = $self->FindWindow('m_grid4'); $grid_left->CreateGrid(0, 3); $grid_left->SetColLabelSize(25); # label height $grid_left->SetRowLabelSize(25); # row width (far left, the numbers column basically) $grid_left->SetColLabelValue(0, 'col_1'); $grid_left->SetColLabelValue(1, 'col_2'); $grid_left->SetColLabelValue(2, 'col_3'); $grid_left->EnableDragColSize(0); $grid_left->EnableDragRowSize(0); $grid_left->EnableEditing(0); $grid_left->SetDefaultRowSize(20); $grid_left->SetColSize( 0, 190 ); # col 1 $grid_left->SetColSize( 1, 50 ); # col 2 $grid_left->SetColSize( 2, 179 ); # col 3 # RIGHT GRID my $grid_right = $self->{ grid_right } = $self->FindWindow('m_grid5'); $grid_right->CreateGrid(0, 3); $grid_right->SetColLabelSize(25); # label height $grid_right->SetRowLabelSize(25); # row width (far left, the numbers column basically) $grid_right->SetColLabelValue(0, 'col_'); $grid_right->SetColLabelValue(1, 'col_2'); $grid_right->SetColLabelValue(2, 'col_3'); $grid_right->EnableDragColSize(0); $grid_right->EnableDragRowSize(0); $grid_right->EnableEditing(0); $grid_right->SetDefaultRowSize(20); $grid_right->SetColSize( 0, 190 ); # col 1 $grid_right->SetColSize( 1, 50 ); # col 2 $grid_right->SetColSize( 2, 179 ); # col 3 #EVT_SIZE( $grid_left, \&on_size ); # adjust grid when size changes #EVT_SIZE( $grid_right, \&on_size ); # adjust grid when size changes my $on_size = \&on_size; EVT_SIZE($grid_left, callback($on_size, $self, $grid_left) ); my $button = $self->{ button } = $self->FindWindow('m_button3'); EVT_BUTTON( $button, $button->GetId(), callback(my $button_sub = \&add_row, $self, $button), ); my $gui_actions_text_ctrl = $self->FindWindow('m_textCtrl1'); my $gui_log = Wx::LogTextCtrl->new( $gui_actions_text_ctrl ); $self->{ gui_log_text_ctrl } = Wx::Log::SetActiveTarget( $gui_log ); $self->SetStatusBarPane(-1); return; } sub callback { my $coderef = shift; my @args = @_; #print "what is args\n"; #print Dumper \@args; sub { $coderef->(@args) } # callback function. when we want to pass addition args to a method call, such as an event (EVT_*), we can use this # example: # my $on_size_test = \&on_size_test; # EVT_SIZE($grid_left, callback($on_size, $grid_left, $self) ); } sub on_close { my ($self) = @_; $self->Destroy(); } sub on_size { my ($self, $grid) = @_; print "in on_size sub\n"; # get the number of columns and the initial width which includes the far left number column (gutter column, with row indicators 1, 2, 3 ...) my $num_cols = $grid->GetNumberCols(); my $width = $grid->GetRowLabelSize(); my $gutter_col = $width; print "num_cols: $num_cols\n"; print "initial width: $width\n"; my $col_0; my $col_1; my $col_2; # iterate over each column, get size and add it to our width for (my $col = 0; $col < $num_cols; $col++) { # conditionals of no importance currently, but used during testing if ($col == 0) { $col_0 = $grid->GetColSize($col); print "col: $col, width col_0: $col_0\n"; } elsif ($col == 1) { $col_1 = $grid->GetColSize($col); print "col: $col, width col_1: $col_1\n"; } elsif ($col == 2) { $col_2 = $grid->GetColSize($col); print "col: $col, width col_2: $col_2\n"; } $width += $grid->GetColSize($col); } print "width after iterating over each column: $width\n"; # get sizer that grid is contained in # get width of sizer and subtract it from the total width # the difference will be the size we set for our last column if ($num_cols > 0) { my $static_box_sizer = $grid->GetContainingSizer(); my $sizer = $static_box_sizer->GetSize(); my $sizer_width = $sizer->GetWidth(); print "sizer_width: $sizer_width\n"; print "we are setting width for the last column. sizer_width: $sizer_width minus the total width: $width (gutter_col: $gutter_col, col_0, col_1, col_2)\n"; my $width_diff = $sizer_width - $width + $col_2 - 11; # ideally this should be 10 for padding (5x2) on each side of sizer # but setting it at 10 causes the col to keep expanding... print "width_diff result of above calc: $width_diff\n"; $grid->SetColSize($num_cols - 1, $width_diff); # last column # adjust grid right to reflect same proportions $self->{ grid_right }->SetColSize($num_cols - 1, $width_diff); # last column } return; } sub add_row { my ($self, $button) = @_; $self->{ grid_left }->AppendRows(1); my $col = 0; foreach my $value ('testing', 100, 'Sat Aug 20 20:52:20 2016') { my $row; if ($col == 3) { $self->{ grid_left }->AppendRows(1); $row = $self->{ grid_left }->GetNumberRows() - 1; # minus one from total rows print "col == 2\n"; print "reset\n"; print "what is row: $row\n"; $col = 0; } else { $row = $self->{ grid_left }->GetNumberRows() - 1; } print "value: $value\n"; $self->{ grid_left }->SetCellValue( $row, $col, $value, ); if ($col == 1) { $self->{ grid_left }->SetCellAlignment( $row, $col, wxALIGN_RIGHT, wxALIGN_CENTRE, ); } elsif ($col == 2) { $self->{ grid_left }->SetCellAlignment( $row, $col, wxALIGN_CENTRE, wxALIGN_CENTRE, ); } $col++; } } ### GUI CODE ENDS ### # END: wxToplevelFrame package # create an app object package MyApp; use strict; use warnings; use base 'Wx::App'; # OnInit is called automatically when an app object is first constructed sub OnInit { my $self = shift; my $toplevel_frame = wxToplevelFrame->new(); $self->SetTopWindow($toplevel_frame); $toplevel_frame->Show(1); } # END: MyApp package package main; use strict; use warnings; use Data::Dumper; $| = 1; my $app = MyApp->new(); # instantiate our app first $app->MainLoop(); exit; #### 998,700 Grid Issues 1 0 wxVERTICAL wxALL|wxEXPAND 5 0 6 0 0 wxALL 5 0 wxEXPAND|wxLEFT|wxRIGHT 5 0 0 0,2 0 0,0 1,1 wxALL|wxEXPAND 5 wxHORIZONTAL wxALL|wxEXPAND 5 0,1 1,1 wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL 5 wxVERTICAL wxALL 5 0 wxALL 5 0 0,2 1,1 wxALL|wxEXPAND 5 wxHORIZONTAL wxALL|wxEXPAND 5 wxEXPAND | wxALL 5 0 wxHORIZONTAL wxALL|wxEXPAND 5 2