File Coverage

blib/lib/Wx/DemoModules/lib/BaseModule.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/lib/BaseModule.pm
3             ## Purpose: wxPerl demo helper base class
4             ## Author: Mattia Barbon
5             ## Modified by:
6             ## Created: 25/08/2006
7             ## RCS-ID: $Id: BaseModule.pm 3043 2011-03-21 17:25:36Z mdootson $
8             ## Copyright: (c) 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::lib::BaseModule;
14 1     1   1596 use strict;
  1         2  
  1         55  
15              
16             =head1 NAME
17              
18             Wx::DemoModules::lib::BaseModule - wxPerl demo helper base class
19              
20             =head1 METHODS
21              
22             =cut
23              
24 1     1   6 use base qw(Wx::Panel Class::Accessor::Fast);
  1         3  
  1         911  
25              
26             use Wx qw(:sizer);
27             use Wx::Event qw(EVT_CHECKBOX EVT_BUTTON EVT_SIZE);
28              
29             __PACKAGE__->mk_accessors( qw(style control_sizer ) );
30              
31             =head2 new
32              
33             Constructor.
34              
35             Called automatically by Wx::Demo when the specific Demo Module is selected.
36              
37             =cut
38              
39             sub new {
40             my( $class, $parent ) = @_;
41             my $self = $class->SUPER::new( $parent );
42              
43             my $sizer = Wx::BoxSizer->new( wxHORIZONTAL );
44              
45             $self->style( 0 );
46              
47             # styles
48             if( $self->styles ) {
49             my $box = Wx::StaticBox->new( $self, -1, 'Styles' );
50             my $stysizer = Wx::StaticBoxSizer->new( $box, wxVERTICAL );
51              
52             $self->add_styles( $stysizer );
53             $sizer->Add( $stysizer, 0, wxGROW|wxALL, 5 );
54             }
55              
56             # commands
57             if( $self->commands ) {
58             my $box = Wx::StaticBox->new( $self, -1, 'Commands' );
59             my $cmdsizer = Wx::StaticBoxSizer->new( $box, wxVERTICAL );
60              
61             $self->add_commands( $cmdsizer );
62             $sizer->Add( $cmdsizer, 0, wxGROW|wxALL, 5 );
63             }
64              
65             # the control (for Mac, the box must be created before the control)
66             my $box = Wx::StaticBox->new( $self, -1, 'Control' );
67             if( my $control = $self->create_control ) {
68             my $ctrlsz = Wx::StaticBoxSizer->new( $box, wxVERTICAL );
69              
70             $self->control_sizer( $ctrlsz );
71             if($self->expandinsizer) {
72             $ctrlsz->Add( $control, 1, wxALL|wxEXPAND, 5 );
73            
74             } else {
75             $ctrlsz->Add( $control, 0,
76             wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,
77             5 );
78             }
79             $sizer->Add( $ctrlsz, 1, wxGROW|wxALL, 5 );
80             } else {
81             $box->Destroy;
82             }
83              
84             $self->SetSizerAndFit( $sizer );
85              
86             return $self;
87             }
88              
89             =head2 add_styles
90              
91             =cut
92              
93             sub add_styles {
94             my( $self, $sizer ) = @_;
95              
96             foreach my $style ( $self->styles ) {
97             my $cbox = Wx::CheckBox->new( $self, -1, $style->[1] );
98             EVT_CHECKBOX( $self, $cbox, sub {
99             my( $self, $event ) = @_;
100              
101             if( $event->GetInt ) {
102             $self->style( $self->style | $style->[0] );
103             } else {
104             $self->style( $self->style & ~$style->[0] );
105             }
106             $self->recreate_control;
107             } );
108             $sizer->Add( $cbox, 0, wxGROW|wxALL, 3 );
109             }
110             }
111              
112             sub add_commands {
113             my( $self, $sizer ) = @_;
114              
115             foreach my $command ( $self->commands ) {
116             if( $command->{with_value} ) {
117             my $sz = Wx::BoxSizer->new( wxHORIZONTAL );
118             my $but = Wx::Button->new( $self, -1, $command->{label} );
119             my @val = map { Wx::TextCtrl->new( $self, -1, '' ) }
120             ( 1 .. $command->{with_value} );
121             $sz->Add( $but, 1, wxRIGHT, 5 );
122             $sz->Add( $_, 1 ) foreach @val;
123             EVT_BUTTON( $self, $but, sub {
124             $command->{action}->( map { $_->GetValue } @val );
125             } );
126             $sizer->Add( $sz, 0, wxGROW|wxALL, 3 );
127             } else {
128             my $but = Wx::Button->new( $self, -1, $command->{label} );
129             EVT_BUTTON( $self, $but, $command->{action} );
130             $sizer->Add( $but, 0, wxGROW|wxALL, 3 );
131             }
132             }
133             }
134              
135             sub recreate_control {
136             my( $self ) = @_;
137              
138             if( $self->control_sizer ) {
139             # work with 2.5.3 amd 2.6.3 (and hopefully other versions)
140             if( $self->control_sizer->GetChildren ) {
141             my $window = $self->control_sizer->GetItem( 0 )->GetWindow;
142             $self->control_sizer->Detach( 0 );
143             $window->Destroy;
144             }
145             if($self->expandinsizer) {
146             $self->control_sizer->Add
147             ( $self->create_control, 1, wxALL|wxEXPAND, 5 );
148            
149             } else {
150             $self->control_sizer->Add
151             ( $self->create_control, 0,
152             wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5 );
153             }
154              
155            
156             $self->Layout;
157             }
158             }
159              
160             sub expandinsizer { 0 };
161              
162              
163             =head1 METHODS designated to be overridden
164              
165             =head2 styles
166              
167             Need to return a list of array refs containing pairs
168             of ??? and title.
169              
170              
171              
172             return (
173             [ wxSB_HORIZONTAL, 'Horizontal' ],
174             [ wxSB_VERTICAL, 'Vertical' ],
175             );
176              
177              
178             =head2 commands
179              
180             Should return a list of hashes.
181             Each hash contains a B
182             and an B key with a subroutine reference to become
183             the event handler as the value.
184              
185             Eg.:
186              
187             return (
188             {
189             label => 'Simple about dialog',
190             action => \&simple_about_dialog,
191             },
192             {
193             label => 'Complex about dialog',
194             action => \&complex_about_dialog,
195             },
196             );
197              
198             =head2 create_control
199              
200             =cut
201              
202             sub styles { }
203             sub commands { }
204             sub create_control { }
205              
206             1;