File Coverage

blib/lib/Wx/DemoModules/wxGridCER.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             #############################################################################
2             ## Name: lib/Wx/DemoModules/wxGridCER.pm
3             ## Purpose: wxPerl demo hlper for wxGrid custom editors and renderers
4             ## Author: Mattia Barbon
5             ## Modified by:
6             ## Created: 05/06/2003
7             ## RCS-ID: $Id: wxGridCER.pm 2378 2008-04-26 04:21:45Z mdootson $
8             ## Copyright: (c) 2003, 2005-2006 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             package Wx::DemoModules::wxGridCER;
14              
15 1     1   1290 use strict;
  1         2  
  1         38  
16 1     1   5 use base qw(Wx::Grid);
  1         3  
  1         621  
17              
18             sub new {
19             my $class = shift;
20             my $this = $class->SUPER::new( $_[0], -1 );
21              
22             $this->CreateGrid( 3, 7 );
23             # set every cell read-only
24             for my $x ( 1 .. 7 ) {
25             for my $y ( 1 .. 3 ) {
26             $this->SetReadOnly( $y, $x, 1 ); # rows, cols
27             }
28             }
29              
30             $this->SetColSize( 0, 20 );
31             $this->SetColSize( 1, 150 );
32             $this->SetColSize( 2, 100 );
33             $this->SetColSize( 3, 20 );
34             $this->SetColSize( 4, 150 );
35             $this->SetColSize( 5, 100 );
36             $this->SetColSize( 6, 20 );
37              
38             $this->SetCellValue( 1, 1, "Custom editor" );
39             $this->SetCellValue( 1, 2, "Some value" );
40             $this->SetCellEditor( 1, 2, Wx::DemoModules::wxGridCER::CustomEditor->new );
41             $this->SetReadOnly( 1, 2, 0 );
42              
43             $this->SetCellValue( 1, 4, "Custom renderer" );
44             $this->SetCellValue( 1, 5, "SoMe TeXt!" );
45             $this->SetCellRenderer( 1, 5, Wx::DemoModules::wxGridCER::CustomRenderer->new );
46             $this->SetReadOnly( 1, 5, 0 );
47              
48             return $this;
49             }
50              
51             sub add_to_tags { 'controls/grid' }
52             sub title { 'Custom editors and renderers' }
53              
54             package Wx::DemoModules::wxGridCER::CustomRenderer;
55              
56             use strict;
57             use base 'Wx::PlGridCellRenderer';
58             use Wx qw(wxBLACK_PEN wxWHITE_BRUSH wxSYS_DEFAULT_GUI_FONT);
59              
60             sub Draw {
61             my( $self, $grid, $attr, $dc, $rect, $row, $col, $sel ) = ( shift, @_ );
62              
63             $self->SUPER::Draw( @_ );
64              
65             $dc->SetPen( wxBLACK_PEN );
66             $dc->SetBrush( wxWHITE_BRUSH );
67             $dc->SetFont(Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
68             $dc->DrawEllipse( $rect->x, $rect->y, $rect->width, $rect->height );
69            
70             # should check $attr->GetOverflow and then IsEmpty on every cell to the right
71             # to extend $rect if overflow is true.
72            
73             $dc->DestroyClippingRegion();
74             $dc->SetClippingRegion($rect->x, $rect->y, $rect->width, $rect->height);
75             $dc->DrawText( $grid->GetCellValue( $row, $col ), $rect->x, $rect->y );
76             $dc->DestroyClippingRegion();
77            
78             }
79              
80             sub Clone {
81             my $self = shift;
82              
83             return $self->new;
84             }
85              
86             package Wx::DemoModules::wxGridCER::CustomEditor;
87              
88             use strict;
89             use base 'Wx::PlGridCellEditor';
90              
91             sub new {
92             my $class = shift;
93             my $self = $class->SUPER::new;
94              
95             return $self;
96             }
97              
98             sub Create {
99             my( $self, $parent, $id, $evthandler ) = @_;
100              
101             $self->SetControl( Wx::TextCtrl->new( $parent, $id, 'Default value', [-1,-1], [-1,-1], Wx::wxTE_PROCESS_TAB ) );
102              
103             $self->GetControl->PushEventHandler( $evthandler );
104              
105             Wx::LogMessage( 'Create called' );
106             }
107              
108             sub Destroy {
109             my $self = shift;
110              
111             $self->GetControl->Destroy if $self->GetControl;
112             $self->SetControl( undef );
113             }
114              
115             sub SetSize {
116             my( $self, $size ) = @_;
117              
118             $self->GetControl->SetSize( $size );
119              
120             Wx::LogMessage( 'SetSize called' );
121             }
122              
123             sub Show {
124             my( $self, $show, $attr ) = @_;
125              
126             $self->GetControl->Show( $show );
127              
128             Wx::LogMessage( 'Show called' );
129             }
130              
131             sub EndEdit {
132             my( $self, $row, $col, $grid ) = @_;
133              
134             my $value = '>> ' . $self->GetControl->GetValue . ' <<';
135             my $oldValue = $grid->GetCellValue( $row, $col );
136              
137             my $changed = $value ne $oldValue;
138              
139             if( $changed ) { $grid->SetCellValue( $row, $col, $value ) }
140              
141             $self->GetControl->Destroy;
142             $self->SetControl( undef );
143              
144             Wx::LogMessage( 'EndEdit called' );
145              
146             return $changed;
147             }
148              
149             1;