File Coverage

blib/lib/Wx/DemoModules/wxGridTable.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #############################################################################
2             ## Name: lib/Wx/DemoModules/wxGridTable.pm
3             ## Purpose: wxPerl demo hlper for wxGrid custom wxGridTable
4             ## Author: Mattia Barbon
5             ## Modified by:
6             ## Created: 05/08/2003
7             ## RCS-ID: $Id: wxGridTable.pm 3118 2011-11-18 09:58:12Z mdootson $
8             ## Copyright: (c) 2003, 2005, 2006, 2011 Mattia Barbon
9             ## Licence: This program is free software; you can redistribute it and/or
10             ## modify it under the same terms as Perl itself
11             #############################################################################
12              
13 1     1   1731 use Wx::Grid;
  0            
  0            
14              
15             package Wx::DemoWindows::wxGridTable::Table;
16              
17             use strict;
18             use base qw(Wx::PlGridTable);
19              
20             use Wx qw(wxRED wxGREEN);
21              
22             sub new {
23             my( $class ) = @_;
24             my $self = $class->SUPER::new;
25              
26             $self->{default} = Wx::GridCellAttr->new;
27             $self->{red_bg} = Wx::GridCellAttr->new;
28             $self->{green_fg} = Wx::GridCellAttr->new;
29              
30             $self->{red_bg}->SetBackgroundColour( wxRED );
31             $self->{green_fg}->SetTextColour( wxGREEN );
32              
33             return $self;
34             }
35              
36             sub GetNumberRows { 100000 }
37             sub GetNumberCols { 100000 }
38             sub IsEmptyCell { 0 }
39              
40             sub GetValue {
41             my( $this, $y, $x ) = @_;
42              
43             return "($y, $x)";
44             }
45              
46             sub SetValue {
47             my( $this, $x, $y, $value ) = @_;
48              
49             die "Read-Only table";
50             }
51              
52             sub GetTypeName {
53             my( $this, $r, $c ) = @_;
54              
55             return $c == 0 ? 'bool' :
56             $c == 1 ? 'double' :
57             'string';
58             }
59              
60             sub CanGetValueAs {
61             my( $this, $r, $c, $type ) = @_;
62              
63             return $c == 0 ? $type eq 'bool' :
64             $c == 1 ? $type eq 'double' :
65             $type eq 'string';
66             }
67              
68             sub GetValueAsBool {
69             my( $this, $r, $c ) = @_;
70              
71             return $r % 2;
72             }
73              
74             sub GetValueAsDouble {
75             my( $this, $r, $c ) = @_;
76              
77             return $r + $c / 1000;
78             }
79              
80             sub GetAttr {
81             my( $self, $row, $col, $kind ) = @_;
82              
83             return $self->{default} if $row % 2 && $col % 2;
84             return $self->{red_bg} if $row % 2;
85             return $self->{green_fg} if $col % 2;
86             return Wx::GridCellAttr->new;
87             }
88              
89             package Wx::DemoModules::wxGridTable;
90              
91             use strict;
92             use base qw(Wx::Grid);
93              
94             use Wx::Event qw(EVT_GRID_CELL_LEFT_CLICK EVT_GRID_CELL_RIGHT_CLICK
95             EVT_GRID_CELL_LEFT_DCLICK EVT_GRID_CELL_RIGHT_DCLICK
96             EVT_GRID_LABEL_LEFT_CLICK EVT_GRID_LABEL_RIGHT_CLICK
97             EVT_GRID_LABEL_LEFT_DCLICK EVT_GRID_LABEL_RIGHT_DCLICK
98             EVT_GRID_ROW_SIZE EVT_GRID_COL_SIZE EVT_GRID_RANGE_SELECT
99             EVT_GRID_SELECT_CELL);
100            
101             # events changed names in version 2.9.x
102             my $events29plus = ( defined(&Wx::Event::EVT_GRID_CELL_CHANGED) );
103              
104             sub new {
105             my $class = shift;
106             my $this = $class->SUPER::new( $_[0], -1 );
107              
108             my $table = Wx::DemoWindows::wxGridTable::Table->new;
109              
110             $this->SetTable( $table );
111              
112             EVT_GRID_CELL_LEFT_CLICK( $this, c_log_skip( "Cell left click" ) );
113             EVT_GRID_CELL_RIGHT_CLICK( $this, c_log_skip( "Cell right click" ) );
114             EVT_GRID_CELL_LEFT_DCLICK( $this, c_log_skip( "Cell left double click" ) );
115             EVT_GRID_CELL_RIGHT_DCLICK( $this, c_log_skip( "Cell right double click" ) );
116             EVT_GRID_LABEL_LEFT_CLICK( $this, c_log_skip( "Label left click" ) );
117             EVT_GRID_LABEL_RIGHT_CLICK( $this, c_log_skip( "Label right click" ) );
118             EVT_GRID_LABEL_LEFT_DCLICK( $this, c_log_skip( "Label left double click" ) );
119             EVT_GRID_LABEL_RIGHT_DCLICK( $this, c_log_skip( "Label right double click" ) );
120              
121             EVT_GRID_ROW_SIZE( $this, sub {
122             Wx::LogMessage( "%s %s", "Row size", GS2S( $_[1] ) );
123             $_[1]->Skip;
124             } );
125             EVT_GRID_COL_SIZE( $this, sub {
126             Wx::LogMessage( "%s %s", "Col size", GS2S( $_[1] ) );
127             $_[1]->Skip;
128             } );
129              
130             EVT_GRID_RANGE_SELECT( $this, sub {
131             Wx::LogMessage( "Range %sselect (%d, %d, %d, %d)",
132             ( $_[1]->Selecting ? '' : 'de' ),
133             $_[1]->GetLeftCol, $_[1]->GetTopRow,
134             $_[1]->GetRightCol,
135             $_[1]->GetBottomRow );
136             $_[0]->ShowSelections;
137             $_[1]->Skip;
138             } );
139            
140             if( $events29plus ) {
141             Wx::Event::EVT_GRID_CELL_CHANGED( $this, c_log_skip( "Cell content changed" ) );
142             } else {
143             Wx::Event::EVT_GRID_CELL_CHANGE( $this, c_log_skip( "Cell content changed" ) );
144             }
145            
146             EVT_GRID_SELECT_CELL( $this, c_log_skip( "Cell select" ) );
147              
148             return $this;
149             }
150              
151             sub ShowSelections {
152             my $this = shift;
153              
154             my @cells = $this->GetSelectedCells;
155             if( @cells ) {
156             Wx::LogMessage( "Cells %s selected", join ', ',
157             map { "(" . $_->GetCol .
158             ", " . $_->GetRow . ")"
159             } @cells );
160             } else {
161             Wx::LogMessage( "No cells selected" );
162             }
163              
164             my @tl = $this->GetSelectionBlockTopLeft;
165             my @br = $this->GetSelectionBlockBottomRight;
166             if( @tl && @br ) {
167             Wx::LogMessage( "Blocks %s selected",
168             join ', ',
169             map { "(" . $tl[$_]->GetCol .
170             ", " . $tl[$_]->GetRow . "-" .
171             $br[$_]->GetCol . ", " .
172             $br[$_]->GetRow . ")"
173             } 0 .. $#tl );
174             } else {
175             Wx::LogMessage( "No blocks selected" );
176             }
177              
178             my @rows = $this->GetSelectedRows;
179             if( @rows ) {
180             Wx::LogMessage( "Rows %s selected", join ', ', @rows );
181             } else {
182             Wx::LogMessage( "No rows selected" );
183             }
184              
185             my @cols = $this->GetSelectedCols;
186             if( @cols ) {
187             Wx::LogMessage( "Columns %s selected", join ', ', @cols );
188             } else {
189             Wx::LogMessage( "No columns selected" );
190             }
191             }
192              
193             # pretty printer for Wx::GridEvent
194             sub G2S {
195             my $event = shift;
196             my( $x, $y ) = ( $event->GetCol, $event->GetRow );
197              
198             return "( $x, $y )";
199             }
200              
201             # prety printer for Wx::GridSizeEvent
202             sub GS2S {
203             my $event = shift;
204             my $roc = $event->GetRowOrCol;
205              
206             return "( $roc )";
207             }
208              
209             # creates an anonymous sub that logs and skips any grid event
210             sub c_log_skip {
211             my $text = shift;
212              
213             return sub {
214             Wx::LogMessage( "%s %s", $text, G2S( $_[1] ) );
215             $_[0]->ShowSelections;
216             $_[1]->Skip;
217             };
218             }
219              
220             sub add_to_tags { qw(controls/grid) }
221             sub title { 'Custom wxGridTable' }
222              
223             1;