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 | # +-----------------------------------------------------------------------+ |
---|
20 | package Uploader::GUI::DlgCommon; |
---|
21 | use strict; |
---|
22 | use Wx qw/wxDP_ALLOWNONE wxDefaultPosition wxDefaultSize/; |
---|
23 | use Wx::Calendar; |
---|
24 | use Wx::Locale qw/:default/; |
---|
25 | use Data::Dumper; |
---|
26 | use base qw/ |
---|
27 | Class::Accessor::Fast |
---|
28 | /; |
---|
29 | |
---|
30 | __PACKAGE__->mk_accessors( |
---|
31 | qw/ |
---|
32 | properties |
---|
33 | frame_callback |
---|
34 | / |
---|
35 | ); |
---|
36 | |
---|
37 | sub 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 | }; |
---|
49 | |
---|
50 | map { |
---|
51 | my $ctrl =$self->FindWindow($_); |
---|
52 | if(defined $ctrl){ |
---|
53 | $ctrl_handlers->{ ref $ctrl}->($ctrl) if exists $ctrl_handlers->{ ref $ctrl}; |
---|
54 | |
---|
55 | $ctrl->SetValidator( |
---|
56 | $self->properties->{$_}->{validator} |
---|
57 | ) if exists $self->properties->{$_}->{validator}; |
---|
58 | } |
---|
59 | } |
---|
60 | keys %{$self->properties}; |
---|
61 | |
---|
62 | |
---|
63 | } |
---|
64 | |
---|
65 | # what does happen when the text changes |
---|
66 | sub OnCheckBox { |
---|
67 | my ( $self, $ctrl, $event ) = @_; |
---|
68 | |
---|
69 | my $id = $ctrl->GetId; |
---|
70 | # change the property value |
---|
71 | $self->properties->{$id}->{value}->( |
---|
72 | $event->IsChecked |
---|
73 | ) if exists $self->properties->{$id}->{value}; |
---|
74 | |
---|
75 | # exec the callback |
---|
76 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
77 | $self->properties->{$id}->{frame_callback}; |
---|
78 | |
---|
79 | $event->Skip; |
---|
80 | } |
---|
81 | |
---|
82 | # what does happen when selection changes |
---|
83 | |
---|
84 | |
---|
85 | # what does happen when the text changes |
---|
86 | sub OnTextCtrl { |
---|
87 | my ( $self, $ctrl, $event ) = @_; |
---|
88 | |
---|
89 | my $id = $ctrl->GetId; |
---|
90 | # change the property value |
---|
91 | $self->properties->{$id}->{value}->( |
---|
92 | $event->GetString |
---|
93 | ) if exists $self->properties->{$id}->{value}; |
---|
94 | |
---|
95 | # exec the callback |
---|
96 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
97 | $self->properties->{$id}->{frame_callback}; |
---|
98 | |
---|
99 | $event->Skip; |
---|
100 | } |
---|
101 | |
---|
102 | sub OnSpinCtrl { |
---|
103 | my ( $self, $ctrl, $event ) = @_; |
---|
104 | |
---|
105 | my $id = $ctrl->GetId; |
---|
106 | # change the property value |
---|
107 | $self->properties->{$id}->{value}->( |
---|
108 | $event->GetInt |
---|
109 | ) if exists $self->properties->{$id}->{value}; |
---|
110 | |
---|
111 | # exec the callback |
---|
112 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
113 | $self->properties->{$id}->{frame_callback}; |
---|
114 | |
---|
115 | $event->Skip; |
---|
116 | } |
---|
117 | |
---|
118 | sub OnRadioBox { |
---|
119 | my ( $self, $ctrl, $event ) = @_; |
---|
120 | |
---|
121 | my $id = $ctrl->GetId; |
---|
122 | |
---|
123 | # if a preprocess value is required |
---|
124 | my $value = $event->GetSelection; |
---|
125 | $value = $self->properties->{$id}->{pre_process}->( $value ) if exists |
---|
126 | $self->properties->{$id}->{pre_process}; |
---|
127 | # change the property value. use the index selection |
---|
128 | $self->properties->{$id}->{selection}->( |
---|
129 | $value |
---|
130 | ) if exists $self->properties->{$id}->{selection}; |
---|
131 | |
---|
132 | # exec the callback |
---|
133 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
134 | $self->properties->{$id}->{frame_callback}; |
---|
135 | |
---|
136 | $event->Skip; |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | sub OnChoice { |
---|
141 | my ( $self, $ctrl, $event ) = @_; |
---|
142 | |
---|
143 | my $id = $ctrl->GetId; |
---|
144 | |
---|
145 | # if a preprocess value is required |
---|
146 | my $value = exists $self->properties->{$id}->{string_selection} ? $event->GetString : $event->GetSelection; |
---|
147 | $value = $self->properties->{$id}->{pre_process}->( $value ) if exists |
---|
148 | $self->properties->{$id}->{pre_process}; |
---|
149 | # change the property value. use the index selection |
---|
150 | $self->properties->{$id}->{selection}->( |
---|
151 | $value |
---|
152 | ) if exists $self->properties->{$id}->{selection}; |
---|
153 | |
---|
154 | # change the property value. use the string |
---|
155 | $self->properties->{$id}->{string_selection}->( |
---|
156 | $value |
---|
157 | ) if exists $self->properties->{$id}->{string_selection}; |
---|
158 | |
---|
159 | # exec the callback |
---|
160 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
161 | $self->properties->{$id}->{frame_callback}; |
---|
162 | |
---|
163 | $event->Skip; |
---|
164 | } |
---|
165 | |
---|
166 | sub OnDatePicker { |
---|
167 | my ( $self, $ctrl, $event ) = @_; |
---|
168 | |
---|
169 | my $id = $ctrl->GetId; |
---|
170 | |
---|
171 | # change the property value |
---|
172 | $self->properties->{$id}->{value}->( |
---|
173 | $event->GetDate->FormatISODate |
---|
174 | ) if exists $self->properties->{$id}->{value}; |
---|
175 | # exec the callback |
---|
176 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
177 | $self->properties->{$id}->{frame_callback}; |
---|
178 | |
---|
179 | $event->Skip; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | my $change_value = { |
---|
184 | 'Wx::SpinCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)}, |
---|
185 | 'Wx::TextCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->ChangeValue($value)}, |
---|
186 | 'Wx::CheckBox' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)}, |
---|
187 | 'Wx::DatePickerCtrl' => sub { my ($ctrl, $value ) = @_; |
---|
188 | my $date = Wx::DateTime->new; |
---|
189 | my ($yyyy, $mm, $dd, $hh, $mi, $ss ) = split(/[:\/\\\-\.\s]/, $value); |
---|
190 | $date->ParseDate( |
---|
191 | sprintf("%s/%s/%s", $yyyy, $mm, $dd) |
---|
192 | ); |
---|
193 | if(-1 eq $value){ |
---|
194 | $date->SetToCurrent; |
---|
195 | } |
---|
196 | $ctrl->SetValue($date); |
---|
197 | }, |
---|
198 | }; |
---|
199 | |
---|
200 | my $clear_value = { |
---|
201 | 'Wx::TextCtrl' => sub { my ($ctrl) = @_; $ctrl->Clear}, |
---|
202 | 'Wx::CheckBox' => sub { my ($ctrl) = @_; $ctrl->SetValue(0)}, |
---|
203 | 'Wx::DatePickerCtrl' => sub { my ($ctrl) = @_; $ctrl->SetValue(Wx::DateTime->new->SetToCurrent)}, |
---|
204 | }; |
---|
205 | |
---|
206 | sub SetProperties { |
---|
207 | my ( $self ) = @_; |
---|
208 | |
---|
209 | map { |
---|
210 | my $ctrl =$self->FindWindow($_); |
---|
211 | if(defined $ctrl){ |
---|
212 | # checkbox, static text |
---|
213 | $change_value->{ref $ctrl}->( |
---|
214 | $ctrl, |
---|
215 | $self->properties->{$_}->{value}->() |
---|
216 | ) if exists $self->properties->{$_}->{value}; |
---|
217 | # only works for control with items |
---|
218 | $ctrl->SetSelection( |
---|
219 | $self->properties->{$_}->{selection}->() |
---|
220 | ) if exists $self->properties->{$_}->{selection}; |
---|
221 | |
---|
222 | $ctrl->SetStringSelection( |
---|
223 | gettext( |
---|
224 | $self->properties->{$_}->{string_selection}->() |
---|
225 | ) |
---|
226 | ) if exists $self->properties->{$_}->{string_selection}; |
---|
227 | } |
---|
228 | } |
---|
229 | keys %{$self->properties}; |
---|
230 | } |
---|
231 | |
---|
232 | sub InitLabels { |
---|
233 | my ( $self ) = @_; |
---|
234 | |
---|
235 | map { |
---|
236 | my $ctrl =$self->FindWindow($_)||$self->{$_}; |
---|
237 | #printf("ctrl %s : %s\n", $_, $ctrl); |
---|
238 | if(defined $ctrl){ |
---|
239 | # checkbox, static text |
---|
240 | $ctrl->SetLabel( |
---|
241 | $self->properties->{$_}->{label} |
---|
242 | ) if exists $self->properties->{$_}->{label}; |
---|
243 | |
---|
244 | $ctrl->GetStaticBox->SetLabel( |
---|
245 | $self->properties->{$_}->{staticbox_label} |
---|
246 | ) if exists $self->properties->{$_}->{staticbox_label}; |
---|
247 | |
---|
248 | # radiobox |
---|
249 | my $labels =$self->properties->{$_}->{labels}; |
---|
250 | $labels||=[]; |
---|
251 | for(my $i=0; $i < scalar @$labels ; $i++){ |
---|
252 | $ctrl->SetItemLabel($i, $labels->[$i]); |
---|
253 | } |
---|
254 | |
---|
255 | # notebook pages |
---|
256 | my $texts =$self->properties->{$_}->{texts}; |
---|
257 | $texts||=[]; |
---|
258 | for(my $i=0; $i < scalar @$texts ; $i++){ |
---|
259 | $ctrl->SetPageText($i, $texts->[$i]); |
---|
260 | } |
---|
261 | |
---|
262 | } |
---|
263 | } |
---|
264 | keys %{$self->properties}; |
---|
265 | |
---|
266 | } |
---|
267 | |
---|
268 | sub InitChoices { |
---|
269 | my ( $self ) = @_; |
---|
270 | |
---|
271 | map { |
---|
272 | my $ctrl =$self->FindWindow($_); |
---|
273 | #printf("ctrl %s : %s\n", $_, $ctrl); |
---|
274 | if(defined $ctrl){ |
---|
275 | # choice |
---|
276 | my $choices =$self->properties->{$_}->{choices}; |
---|
277 | $choices||=[]; |
---|
278 | map{ |
---|
279 | $ctrl->Append($_); |
---|
280 | }@$choices; |
---|
281 | } |
---|
282 | } |
---|
283 | keys %{$self->properties}; |
---|
284 | |
---|
285 | } |
---|
286 | |
---|
287 | sub GetProperties { |
---|
288 | my ( $self ) = @_; |
---|
289 | |
---|
290 | map { |
---|
291 | my $ctrl = $self->FindWindow($_); |
---|
292 | #printf("ctrl %s : %s\n", $_, $ctrl); |
---|
293 | if(defined $ctrl){ |
---|
294 | # checkbox, static text |
---|
295 | $self->properties->{$_}->{value}->( |
---|
296 | $ctrl->GetValue() |
---|
297 | ) if exists $self->properties->{$_}->{value}; |
---|
298 | |
---|
299 | $self->properties->{$_}->{selection}->( |
---|
300 | $ctrl->GetSelection() |
---|
301 | ) if exists $self->properties->{$_}->{selection}; |
---|
302 | |
---|
303 | $self->properties->{$_}->{string_selection}->( |
---|
304 | $ctrl->GetStringSelection() |
---|
305 | ) if exists $self->properties->{$_}->{string_selection}; |
---|
306 | } |
---|
307 | } |
---|
308 | keys %{$self->properties}; |
---|
309 | } |
---|
310 | |
---|
311 | sub ClearProperties { |
---|
312 | my ( $self ) = @_; |
---|
313 | |
---|
314 | map { |
---|
315 | my $ctrl =$self->FindWindow($_); |
---|
316 | if(defined $ctrl){ |
---|
317 | # checkbox, static text |
---|
318 | $clear_value->{ref $ctrl}->( |
---|
319 | $ctrl |
---|
320 | ) if exists $clear_value->{ref $ctrl}; |
---|
321 | # only works for control with items |
---|
322 | $ctrl->SetSelection( |
---|
323 | -1 |
---|
324 | ) if exists $self->properties->{$_}->{selection}; |
---|
325 | } |
---|
326 | |
---|
327 | $ctrl->SetStringSelection( |
---|
328 | -1 |
---|
329 | ) if exists $self->properties->{$_}->{string_selection}; |
---|
330 | } |
---|
331 | keys %{$self->properties}; |
---|
332 | |
---|
333 | } |
---|
334 | 1; |
---|