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

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

Bug fixed : use caption as prefix does not work on Ubuntu. EVT_COMBOBOX_TEXT is triggered before EVT_COMBOBOX.

File size: 14.7 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 qw/wxDP_ALLOWNONE wxDefaultPosition wxDefaultSize/;
23use Wx::Calendar;
24use Wx::Locale qw/:default/;
25use Data::Dumper;
26use base qw/
27               Class::Accessor::Fast
28           /; 
29
30__PACKAGE__->mk_accessors(     
31    qw/
32        properties
33        frame_callback
34      /
35);
36
37sub InitHandler {
38    my ( $self ) = @_;
39
40    # to connect the right event handler to each control
41    my $ctrl_handlers = {
42        'Wx::SpinCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_SPINCTRL( $ctrl, $ctrl, sub { $self->OnSpinCtrl(@_) } ); },
43        'Wx::TextCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_TEXT( $ctrl, $ctrl, sub { $self->OnTextCtrl(@_) } ); },
44        'Wx::Choice' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_CHOICE( $ctrl, $ctrl, sub { $self->OnChoice(@_) } ); },
45        'Wx::RadioBox' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_RADIOBOX( $ctrl, $ctrl, sub { $self->OnRadioBox(@_) } ); },
46        'Wx::CheckBox' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_CHECKBOX( $ctrl, $ctrl, sub { $self->OnCheckBox(@_) } ); },
47        'Wx::DatePickerCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_DATE_CHANGED( $ctrl, $ctrl, sub { $self->OnDatePicker(@_) } ); },
48        'Wx::ComboBox' => sub { my ( $ctrl ) = @_;
49                              Wx::Event::EVT_COMBOBOX( $ctrl, $ctrl, sub { $self->OnComboBox(@_) } );
50                              Wx::Event::EVT_TEXT( $ctrl, $ctrl, sub { $self->OnComboBoxText(@_) } );
51                          },
52        'Uploader::GUI::wxCategoryTreeCtrl' => sub { my ( $ctrl ) = @_;
53                                                   Wx::Event::EVT_TREE_SEL_CHANGED(
54                                                       $ctrl,
55                                                       $ctrl,
56                                                       sub { 
57                                                           $self->OnCategoryTreeSelChanged(@_); 
58                                                       }
59                                                   );
60                                               },
61    };
62   
63    map {
64        my $ctrl =$self->FindWindow($_);
65
66        if(defined $ctrl){
67            $ctrl_handlers->{ ref $ctrl}->($ctrl) if exists $ctrl_handlers->{ ref $ctrl};
68
69            $ctrl->SetValidator(
70                $self->properties->{$_}->{validator}
71            ) if exists $self->properties->{$_}->{validator};
72        }
73    }
74    keys %{$self->properties};
75   
76
77}
78
79# what does happen when the text changes
80sub OnCheckBox {
81    my ( $self, $ctrl, $event ) = @_;
82   
83    my $id = $ctrl->GetId;
84    # change the property value
85    $self->properties->{$id}->{value}->(
86        $event->IsChecked
87    ) if exists $self->properties->{$id}->{value};
88
89    # exec the callback
90    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
91        $self->properties->{$id}->{frame_callback};   
92
93    $event->Skip;
94}
95
96# what does happen when selection changes
97
98
99sub OnComboBoxText {
100    my ( $self, $ctrl, $event ) = @_;
101
102    # text changed by user, not selection
103    if( -1 eq $ctrl->GetSelection){
104       my $id = $ctrl->GetId;
105        # change the property value
106        $self->properties->{$id}->{value}->(
107            $event->GetString
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
115}
116
117
118# what does happen when the text changes
119sub OnTextCtrl {
120    my ( $self, $ctrl, $event ) = @_;
121   
122    my $id = $ctrl->GetId;
123    # change the property value
124    $self->properties->{$id}->{value}->(
125        $event->GetString
126    ) if exists $self->properties->{$id}->{value};
127
128    # exec the callback
129    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
130        $self->properties->{$id}->{frame_callback};   
131   
132}
133
134sub OnSpinCtrl {
135    my ( $self, $ctrl, $event ) = @_;
136   
137    my $id = $ctrl->GetId;
138    # change the property value
139    $self->properties->{$id}->{value}->(
140        $event->GetInt
141    ) if exists $self->properties->{$id}->{value};
142
143    # exec the callback
144    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
145        $self->properties->{$id}->{frame_callback};   
146   
147    $event->Skip;
148}
149
150sub OnRadioBox {
151    my ( $self, $ctrl, $event ) = @_;
152   
153    my $id = $ctrl->GetId;
154
155    # if a preprocess value is required
156    my $value = $event->GetSelection;
157    $value = $self->properties->{$id}->{pre_process}->( $value ) if exists
158        $self->properties->{$id}->{pre_process};   
159    # change the property value. use the index selection
160    $self->properties->{$id}->{selection}->(
161        $value
162    ) if exists $self->properties->{$id}->{selection};
163
164    # exec the callback
165    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
166        $self->properties->{$id}->{frame_callback};   
167   
168    $event->Skip;
169
170}
171
172sub OnChoice {
173    my ( $self, $ctrl, $event ) = @_;
174   
175    my $id = $ctrl->GetId;
176
177    # if a preprocess value is required
178    my $value = exists $self->properties->{$id}->{string_selection} ? $event->GetString : $event->GetSelection;
179    $value = $self->properties->{$id}->{pre_process}->( $value ) if exists
180        $self->properties->{$id}->{pre_process};   
181    # change the property value. use the index selection
182    $self->properties->{$id}->{selection}->(
183        $value
184    ) if exists $self->properties->{$id}->{selection};
185
186    # change the property value. use the string
187    $self->properties->{$id}->{string_selection}->(
188        $value
189    ) if exists $self->properties->{$id}->{string_selection};
190
191    # exec the callback
192    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
193        $self->properties->{$id}->{frame_callback};   
194   
195    $event->Skip;
196}
197
198
199sub OnComboBox {
200    my ( $self, $ctrl, $event ) = @_;
201     
202    my $id = $ctrl->GetId;
203
204    $ctrl->{_multi_selection_mode} = $self->properties->{$id}->{multi_selection_mode}->()
205        if exists $self->properties->{$id}->{multi_selection_mode};
206
207    my $selection = exists $self->properties->{$id}->{string_selection} ? $event->GetString : $event->GetSelection;
208
209    my $value = $self->properties->{$id}->{pre_process}->( $selection ) if exists
210        $self->properties->{$id}->{pre_process};   
211
212    if( exists $self->properties->{$id}->{value} ){
213        $value = $self->properties->{$id}->{value}->(
214            $value,
215            $selection,
216        );
217        # the item selected in the list is not the real value
218        # we place here the actual value
219        # to use in EVT_IDLE event, and overwrite the text field with it
220        $ctrl->{_value} = $value;
221    }
222
223    # exec the callback
224    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
225        $self->properties->{$id}->{frame_callback};   
226}
227
228
229sub OnDatePicker {
230    my ( $self, $ctrl, $event ) = @_;
231   
232    my $id = $ctrl->GetId;
233
234    # change the property value
235    $self->properties->{$id}->{value}->(
236        $event->GetDate->FormatISODate
237    ) if exists $self->properties->{$id}->{value};
238    # exec the callback
239    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
240        $self->properties->{$id}->{frame_callback};   
241   
242    $event->Skip;
243}
244
245
246my $change_value = {
247    'Wx::SpinCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)},
248    'Wx::TextCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->ChangeValue($value)},
249    'Wx::CheckBox' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)},
250    'Wx::DatePickerCtrl' => sub { my ($ctrl, $value ) = @_;
251                                  my $date = Wx::DateTime->new;   
252                                  my ($yyyy, $mm, $dd, $hh, $mi, $ss ) = split(/[:\/\\\-\.\s]/, $value);
253                                  $date->ParseDate(
254                                    sprintf("%s/%s/%s", $yyyy, $mm, $dd)
255                                  );
256                                  if(-1 eq $value){
257                                      $date->SetToCurrent;
258                                  }
259                                  $ctrl->SetValue($date);
260                            },
261    'Wx::ComboBox' => sub { 
262                          my ($ctrl, $value ) = @_; 
263                          $ctrl->SetValue($value) ; 
264                      },
265};
266
267
268sub OnCategoryTreeSelChanged {
269    my ( $self, $ctrl, $event ) = @_;
270
271    my $id = $ctrl->GetId;
272
273    # change the property value.
274    $self->properties->{$id}->{id_selection}->(
275        $event->GetEventObject->GetSelectionsIds
276    ) if exists $self->properties->{$id}->{id_selection};
277
278    #printf("OnCategoryTreeSelChanged %s\n", Dumper $self->properties->{$id}->{id_selection}->());
279
280    # exec the callback
281    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
282        $self->properties->{$id}->{frame_callback};   
283   
284    $event->Skip;
285}
286
287
288my $clear_value = {
289    'Wx::TextCtrl' => sub { my ($ctrl) = @_; $ctrl->Clear},
290    'Wx::CheckBox' => sub { my ($ctrl) = @_; $ctrl->SetValue(0)},
291    'Wx::DatePickerCtrl' => sub { my ($ctrl) = @_; $ctrl->SetValue(Wx::DateTime->new->SetToCurrent)},
292    'Uploader::GUI::wxCategoryTreeCtrl' => sub { my ( $ctrl ) = @_;
293                                               $ctrl->SelectItem($ctrl->GetRootItem);
294                                           },
295};   
296
297sub SetProperties {
298    my ( $self ) = @_;
299   
300    map {
301        my $ctrl =$self->FindWindow($_);
302        if(defined $ctrl){
303            #printf("%s\n", $ctrl);
304            # checkbox, static text
305            $change_value->{ref $ctrl}->(
306                $ctrl,
307                $self->properties->{$_}->{value}->()
308            ) if exists $self->properties->{$_}->{value};
309            # only works for control with items
310            $ctrl->SetSelection(
311                $self->properties->{$_}->{selection}->()
312            ) if exists $self->properties->{$_}->{selection};
313
314            $ctrl->SetStringSelection(
315                gettext(
316                    $self->properties->{$_}->{string_selection}->()
317                )
318            ) if exists $self->properties->{$_}->{string_selection};
319        }
320    }
321    keys %{$self->properties};
322}
323
324sub InitLabels {
325    my ( $self ) = @_;
326   
327    map {
328        my $ctrl =$self->FindWindow($_)||$self->{$_};
329        #printf("ctrl %s : %s\n", $_, $ctrl);       
330        if(defined $ctrl){
331            # checkbox, static text
332            $ctrl->SetLabel(
333               $self->properties->{$_}->{label}
334            ) if exists $self->properties->{$_}->{label};
335
336            $ctrl->GetStaticBox->SetLabel(
337                $self->properties->{$_}->{staticbox_label}
338            ) if exists $self->properties->{$_}->{staticbox_label};
339
340            # radiobox
341            my $labels =$self->properties->{$_}->{labels};
342            $labels||=[];
343            for(my $i=0; $i < scalar @$labels ; $i++){
344                $ctrl->SetItemLabel($i, $labels->[$i]);
345            }
346
347            # notebook pages
348            my $texts =$self->properties->{$_}->{texts};
349            $texts||=[];
350            for(my $i=0; $i < scalar @$texts ; $i++){
351                $ctrl->SetPageText($i, $texts->[$i]);
352            }
353
354        }
355    }
356    keys %{$self->properties};
357
358}
359
360sub InitChoices {
361    my ( $self ) = @_;
362
363    map {
364        my $ctrl =$self->FindWindow($_);
365        #printf("ctrl %s : %s\n", $_, $ctrl);       
366        if(defined $ctrl){
367            # choice
368            my $choices =$self->properties->{$_}->{choices};
369            $choices||=[];
370            map{
371                $ctrl->Append($_);
372            }@$choices;
373        }
374    }
375    keys %{$self->properties};
376
377}
378
379sub GetProperties {
380    my ( $self ) = @_;
381
382    map {
383        my $ctrl = $self->FindWindow($_);
384        #printf("ctrl %s : %s\n", $_, $ctrl);       
385        if(defined $ctrl){
386        # checkbox, static text
387            $self->properties->{$_}->{value}->(
388                $ctrl->GetValue()
389            ) if exists $self->properties->{$_}->{value};
390
391            $self->properties->{$_}->{selection}->(
392                $ctrl->GetSelection()           
393            ) if exists $self->properties->{$_}->{selection};
394
395            $self->properties->{$_}->{string_selection}->(
396                $ctrl->GetStringSelection()           
397            ) if exists $self->properties->{$_}->{string_selection};
398        }
399    }
400    keys %{$self->properties};
401}
402
403sub ClearProperties {
404    my ( $self ) = @_;
405   
406    map {
407        my $ctrl =$self->FindWindow($_);
408        if(defined $ctrl){
409             # checkbox, static text
410            $clear_value->{ref $ctrl}->(
411                $ctrl
412            ) if exists  $clear_value->{ref $ctrl};
413            # only works for control with items
414            $ctrl->SetSelection(
415                -1           
416            ) if exists $self->properties->{$_}->{selection};
417        }
418
419            $ctrl->SetStringSelection(
420                -1           
421            ) if exists $self->properties->{$_}->{string_selection};
422    }
423    keys %{$self->properties};
424
425}
4261;
Note: See TracBrowser for help on using the repository browser.