source: extensions/pLoader/trunk/src/Uploader/GUI/DlgCommon.pm @ 4779

Last change on this file since 4779 was 4779, checked in by ronosman, 14 years ago

Change copyright notice to add 2010.

File size: 10.9 KB
Line 
1# +-----------------------------------------------------------------------+
2# | pLoader - a Perl photo uploader for Piwigo                            |
3# +-----------------------------------------------------------------------+
4# | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
5# +-----------------------------------------------------------------------+
6# | This program is free software; you can redistribute it and/or modify  |
7# | it under the terms of the GNU General Public License as published by  |
8# | the Free Software Foundation                                          |
9# |                                                                       |
10# | This program is distributed in the hope that it will be useful, but   |
11# | WITHOUT ANY WARRANTY; without even the implied warranty of            |
12# | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
13# | General Public License for more details.                              |
14# |                                                                       |
15# | You should have received a copy of the GNU General Public License     |
16# | along with this program; if not, write to the Free Software           |
17# | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18# | USA.                                                                  |
19# +-----------------------------------------------------------------------+
20package Uploader::GUI::DlgCommon;
21use strict;
22use Wx::Calendar;
23use Wx::Locale qw/:default/;
24
25use base qw/
26               Class::Accessor::Fast
27           /; 
28
29__PACKAGE__->mk_accessors(     
30    qw/
31        properties
32        frame_callback
33      /
34);
35
36sub InitHandler {
37    my ( $self ) = @_;
38
39    # to connect the right event handler to each control
40    my $ctrl_handlers = {
41        'Wx::SpinCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_SPINCTRL( $ctrl, $ctrl, sub { $self->OnSpinCtrl(@_) } ); },
42        'Wx::TextCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_TEXT( $ctrl, $ctrl, sub { $self->OnTextCtrl(@_) } ); },
43        'Wx::Choice' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_CHOICE( $ctrl, $ctrl, sub { $self->OnChoice(@_) } ); },
44        'Wx::RadioBox' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_RADIOBOX( $ctrl, $ctrl, sub { $self->OnRadioBox(@_) } ); },
45        'Wx::CheckBox' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_CHECKBOX( $ctrl, $ctrl, sub { $self->OnCheckBox(@_) } ); },
46        'Wx::DatePickerCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_DATE_CHANGED( $ctrl, $ctrl, sub { $self->OnDatePicker(@_) } ); },
47    };
48   
49    map {
50        my $ctrl =$self->FindWindow($_);
51        if(defined $ctrl){
52            $ctrl_handlers->{ ref $ctrl}->($ctrl) if exists $ctrl_handlers->{ ref $ctrl};
53
54            $ctrl->SetValidator(
55                $self->properties->{$_}->{validator}
56            ) if exists $self->properties->{$_}->{validator};
57        }
58    }
59    keys %{$self->properties};
60   
61
62}
63
64# what does happen when the text changes
65sub OnCheckBox {
66    my ( $self, $ctrl, $event ) = @_;
67   
68    my $id = $ctrl->GetId;
69    # change the property value
70    $self->properties->{$id}->{value}->(
71        $event->IsChecked
72    ) if exists $self->properties->{$id}->{value};
73
74    # exec the callback
75    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
76        $self->properties->{$id}->{frame_callback};   
77
78    $event->Skip;
79}
80
81# what does happen when selection changes
82
83
84# what does happen when the text changes
85sub OnTextCtrl {
86    my ( $self, $ctrl, $event ) = @_;
87   
88    my $id = $ctrl->GetId;
89    # change the property value
90    $self->properties->{$id}->{value}->(
91        $event->GetString
92    ) if exists $self->properties->{$id}->{value};
93
94    # exec the callback
95    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
96        $self->properties->{$id}->{frame_callback};   
97   
98    $event->Skip;
99}
100
101sub OnSpinCtrl {
102    my ( $self, $ctrl, $event ) = @_;
103   
104    my $id = $ctrl->GetId;
105    # change the property value
106    $self->properties->{$id}->{value}->(
107        $event->GetInt
108    ) if exists $self->properties->{$id}->{value};
109
110    # exec the callback
111    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
112        $self->properties->{$id}->{frame_callback};   
113   
114    $event->Skip;
115}
116
117sub OnRadioBox {
118    my ( $self, $ctrl, $event ) = @_;
119   
120    my $id = $ctrl->GetId;
121
122    # if a preprocess value is required
123    my $value = $event->GetSelection;
124    $value = $self->properties->{$id}->{pre_process}->( $value ) if exists
125        $self->properties->{$id}->{pre_process};   
126    # change the property value. use the index selection
127    $self->properties->{$id}->{selection}->(
128        $value
129    ) if exists $self->properties->{$id}->{selection};
130
131    # exec the callback
132    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
133        $self->properties->{$id}->{frame_callback};   
134   
135    $event->Skip;
136
137}
138
139sub OnChoice {
140    my ( $self, $ctrl, $event ) = @_;
141   
142    my $id = $ctrl->GetId;
143
144    # if a preprocess value is required
145    my $value = exists $self->properties->{$id}->{string_selection} ? $event->GetString : $event->GetSelection;
146    $value = $self->properties->{$id}->{pre_process}->( $value ) if exists
147        $self->properties->{$id}->{pre_process};   
148    # change the property value. use the index selection
149    $self->properties->{$id}->{selection}->(
150        $value
151    ) if exists $self->properties->{$id}->{selection};
152
153    # change the property value. use the string
154    $self->properties->{$id}->{string_selection}->(
155        $value
156    ) if exists $self->properties->{$id}->{string_selection};
157
158    # exec the callback
159    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
160        $self->properties->{$id}->{frame_callback};   
161   
162    $event->Skip;
163}
164
165sub OnDatePicker {
166    my ( $self, $ctrl, $event ) = @_;
167   
168    my $id = $ctrl->GetId;
169
170    # change the property value
171    $self->properties->{$id}->{value}->(
172        $event->GetDate->FormatISODate
173    ) if exists $self->properties->{$id}->{value};
174    # exec the callback
175    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
176        $self->properties->{$id}->{frame_callback};   
177   
178    $event->Skip;
179}
180
181
182my $change_value = {
183    'Wx::SpinCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)},
184    'Wx::TextCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->ChangeValue($value)},
185    'Wx::CheckBox' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)},
186    'Wx::DatePickerCtrl' => sub { my ($ctrl, $value ) = @_;
187                                  my $date = Wx::DateTime->new;   
188                                  my ($yyyy, $mm, $dd, $hh, $mi, $ss ) = split(/[:\/\\\-\.\s]/, $value);
189                                  $date->ParseDate(
190                                    sprintf("%s/%s/%s", $yyyy, $mm, $dd)
191                                  );
192                                  $ctrl->SetValue($date);
193                                },
194};
195
196my $clear_value = {
197    'Wx::TextCtrl' => sub { my ($ctrl) = @_; $ctrl->Clear},
198    'Wx::CheckBox' => sub { my ($ctrl) = @_; $ctrl->SetValue(0)},
199    'Wx::DatePickerCtrl' => sub { my ($ctrl) = @_; $ctrl->SetValue(Wx::DateTime->new->SetToCurrent)},
200};   
201
202sub SetProperties {
203    my ( $self ) = @_;
204   
205    map {
206        my $ctrl =$self->FindWindow($_);
207        if(defined $ctrl){
208            # checkbox, static text
209            $change_value->{ref $ctrl}->(
210                $ctrl,
211                $self->properties->{$_}->{value}->()
212            ) if exists $self->properties->{$_}->{value};
213            # only works for control with items
214            $ctrl->SetSelection(
215                $self->properties->{$_}->{selection}->()
216            ) if exists $self->properties->{$_}->{selection};
217
218            $ctrl->SetStringSelection(
219                gettext(
220                    $self->properties->{$_}->{string_selection}->()
221                )
222            ) if exists $self->properties->{$_}->{string_selection};
223        }
224    }
225    keys %{$self->properties};
226}
227
228sub InitLabels {
229    my ( $self ) = @_;
230   
231    map {
232        my $ctrl =$self->FindWindow($_)||$self->{$_};
233        #printf("ctrl %s : %s\n", $_, $ctrl);       
234        if(defined $ctrl){
235            # checkbox, static text
236            $ctrl->SetLabel(
237               $self->properties->{$_}->{label}
238            ) if exists $self->properties->{$_}->{label};
239
240            $ctrl->GetStaticBox->SetLabel(
241                $self->properties->{$_}->{staticbox_label}
242            ) if exists $self->properties->{$_}->{staticbox_label};
243       
244            # radiobox
245            my $labels =$self->properties->{$_}->{labels};
246            $labels||=[];
247            for(my $i=0; $i < scalar @$labels ; $i++){
248                $ctrl->SetItemLabel($i, $labels->[$i]);
249            }
250        }
251    }
252    keys %{$self->properties};
253
254}
255
256sub InitChoices {
257    my ( $self ) = @_;
258
259        map {
260        my $ctrl =$self->FindWindow($_);
261        #printf("ctrl %s : %s\n", $_, $ctrl);       
262        if(defined $ctrl){
263            # choice
264            my $choices =$self->properties->{$_}->{choices};
265            $choices||=[];
266            map{
267                $ctrl->Append($_);
268            }@$choices;
269        }
270    }
271    keys %{$self->properties};
272
273}
274
275sub GetProperties {
276    my ( $self ) = @_;
277
278    map {
279        my $ctrl = $self->FindWindow($_);
280        #printf("ctrl %s : %s\n", $_, $ctrl);       
281        if(defined $ctrl){
282        # checkbox, static text
283            $self->properties->{$_}->{value}->(
284                $ctrl->GetValue()
285            ) if exists $self->properties->{$_}->{value};
286
287            $self->properties->{$_}->{selection}->(
288                $ctrl->GetSelection()           
289            ) if exists $self->properties->{$_}->{selection};
290
291            $self->properties->{$_}->{string_selection}->(
292                $ctrl->GetStringSelection()           
293            ) if exists $self->properties->{$_}->{string_selection};
294        }
295    }
296    keys %{$self->properties};
297}
298
299sub ClearProperties {
300    my ( $self ) = @_;
301   
302    map {
303        my $ctrl =$self->FindWindow($_);
304        if(defined $ctrl){
305             # checkbox, static text
306            $clear_value->{ref $ctrl}->(
307                $ctrl
308            ) if exists  $clear_value->{ref $ctrl};
309            # only works for control with items
310            $ctrl->SetSelection(
311                -1           
312            ) if exists $self->properties->{$_}->{selection};
313        }
314
315            $ctrl->SetStringSelection(
316                -1           
317            ) if exists $self->properties->{$_}->{string_selection};
318    }
319    keys %{$self->properties};
320
321}
3221;
Note: See TracBrowser for help on using the repository browser.