1 | package Uploader::GUI::DlgCommon; |
---|
2 | use strict; |
---|
3 | use Wx::Calendar; |
---|
4 | use Wx::Locale qw/:default/; |
---|
5 | |
---|
6 | use base qw/ |
---|
7 | Class::Accessor::Fast |
---|
8 | /; |
---|
9 | |
---|
10 | __PACKAGE__->mk_accessors( |
---|
11 | qw/ |
---|
12 | properties |
---|
13 | frame_callback |
---|
14 | / |
---|
15 | ); |
---|
16 | |
---|
17 | sub InitHandler { |
---|
18 | my ( $self ) = @_; |
---|
19 | |
---|
20 | # to connect the right event handler to each control |
---|
21 | my $ctrl_handlers = { |
---|
22 | 'Wx::SpinCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_SPINCTRL( $ctrl, $ctrl, sub { $self->OnSpinCtrl(@_) } ); }, |
---|
23 | 'Wx::TextCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_TEXT( $ctrl, $ctrl, sub { $self->OnTextCtrl(@_) } ); }, |
---|
24 | 'Wx::Choice' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_CHOICE( $ctrl, $ctrl, sub { $self->OnChoice(@_) } ); }, |
---|
25 | 'Wx::CheckBox' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_CHECKBOX( $ctrl, $ctrl, sub { $self->OnCheckBox(@_) } ); }, |
---|
26 | 'Wx::DatePickerCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_DATE_CHANGED( $ctrl, $ctrl, sub { $self->OnDatePicker(@_) } ); }, |
---|
27 | }; |
---|
28 | |
---|
29 | map { |
---|
30 | my $ctrl =$self->FindWindow($_); |
---|
31 | if(defined $ctrl){ |
---|
32 | $ctrl_handlers->{ ref $ctrl}->($ctrl) if exists $ctrl_handlers->{ ref $ctrl}; |
---|
33 | |
---|
34 | $ctrl->SetValidator( |
---|
35 | $self->properties->{$_}->{validator} |
---|
36 | ) if exists $self->properties->{$_}->{validator}; |
---|
37 | } |
---|
38 | } |
---|
39 | keys %{$self->properties}; |
---|
40 | |
---|
41 | |
---|
42 | } |
---|
43 | |
---|
44 | # what does happen when the text changes |
---|
45 | sub OnCheckBox { |
---|
46 | my ( $self, $ctrl, $event ) = @_; |
---|
47 | |
---|
48 | my $id = $ctrl->GetId; |
---|
49 | # change the property value |
---|
50 | $self->properties->{$id}->{value}->( |
---|
51 | $event->IsChecked |
---|
52 | ) if exists $self->properties->{$id}->{value}; |
---|
53 | |
---|
54 | # exec the callback |
---|
55 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
56 | $self->properties->{$id}->{frame_callback}; |
---|
57 | |
---|
58 | $event->Skip; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | # what does happen when the text changes |
---|
63 | sub OnTextCtrl { |
---|
64 | my ( $self, $ctrl, $event ) = @_; |
---|
65 | |
---|
66 | my $id = $ctrl->GetId; |
---|
67 | # change the property value |
---|
68 | $self->properties->{$id}->{value}->( |
---|
69 | $event->GetString |
---|
70 | ) if exists $self->properties->{$id}->{value}; |
---|
71 | |
---|
72 | # exec the callback |
---|
73 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
74 | $self->properties->{$id}->{frame_callback}; |
---|
75 | |
---|
76 | $event->Skip; |
---|
77 | } |
---|
78 | |
---|
79 | sub OnSpinCtrl { |
---|
80 | my ( $self, $ctrl, $event ) = @_; |
---|
81 | |
---|
82 | my $id = $ctrl->GetId; |
---|
83 | # change the property value |
---|
84 | $self->properties->{$id}->{value}->( |
---|
85 | $event->GetInt |
---|
86 | ) if exists $self->properties->{$id}->{value}; |
---|
87 | |
---|
88 | # exec the callback |
---|
89 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
90 | $self->properties->{$id}->{frame_callback}; |
---|
91 | |
---|
92 | $event->Skip; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | sub OnChoice { |
---|
97 | my ( $self, $ctrl, $event ) = @_; |
---|
98 | |
---|
99 | my $id = $ctrl->GetId; |
---|
100 | |
---|
101 | # if a preprocess value is required |
---|
102 | my $value = exists $self->properties->{$id}->{string_selection} ? $event->GetString : $event->GetSelection; |
---|
103 | $value = $self->properties->{$id}->{pre_process}->( $value ) if exists |
---|
104 | $self->properties->{$id}->{pre_process}; |
---|
105 | # change the property value. use the index selection |
---|
106 | $self->properties->{$id}->{selection}->( |
---|
107 | $value |
---|
108 | ) if exists $self->properties->{$id}->{selection}; |
---|
109 | |
---|
110 | # change the property value. use the string |
---|
111 | $self->properties->{$id}->{string_selection}->( |
---|
112 | $value |
---|
113 | ) if exists $self->properties->{$id}->{string_selection}; |
---|
114 | |
---|
115 | # exec the callback |
---|
116 | $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists |
---|
117 | $self->properties->{$id}->{frame_callback}; |
---|
118 | |
---|
119 | $event->Skip; |
---|
120 | } |
---|
121 | |
---|
122 | sub OnDatePicker { |
---|
123 | my ( $self, $ctrl, $event ) = @_; |
---|
124 | |
---|
125 | my $id = $ctrl->GetId; |
---|
126 | |
---|
127 | # change the property value |
---|
128 | $self->properties->{$id}->{value}->( |
---|
129 | $event->GetDate->FormatISODate |
---|
130 | ) if exists $self->properties->{$id}->{value}; |
---|
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 | |
---|
139 | my $change_value = { |
---|
140 | 'Wx::SpinCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)}, |
---|
141 | 'Wx::TextCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->ChangeValue($value)}, |
---|
142 | 'Wx::CheckBox' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)}, |
---|
143 | 'Wx::DatePickerCtrl' => sub { my ($ctrl, $value ) = @_; |
---|
144 | my $date = Wx::DateTime->new; |
---|
145 | my ($yyyy, $mm, $dd, $hh, $mi, $ss ) = split(/[:\/\\\-\.\s]/, $value); |
---|
146 | $date->ParseDate( |
---|
147 | sprintf("%s/%s/%s", $yyyy, $mm, $dd) |
---|
148 | ); |
---|
149 | $ctrl->SetValue($date); |
---|
150 | }, |
---|
151 | }; |
---|
152 | |
---|
153 | my $clear_value = { |
---|
154 | 'Wx::TextCtrl' => sub { my ($ctrl) = @_; $ctrl->Clear}, |
---|
155 | 'Wx::CheckBox' => sub { my ($ctrl) = @_; $ctrl->SetValue(0)}, |
---|
156 | 'Wx::DatePickerCtrl' => sub { my ($ctrl) = @_; $ctrl->SetValue(Wx::DateTime->new->SetToCurrent)}, |
---|
157 | }; |
---|
158 | |
---|
159 | sub SetProperties { |
---|
160 | my ( $self ) = @_; |
---|
161 | |
---|
162 | map { |
---|
163 | my $ctrl =$self->FindWindow($_); |
---|
164 | if(defined $ctrl){ |
---|
165 | # checkbox, static text |
---|
166 | $change_value->{ref $ctrl}->( |
---|
167 | $ctrl, |
---|
168 | $self->properties->{$_}->{value}->() |
---|
169 | ) if exists $self->properties->{$_}->{value}; |
---|
170 | # only works for control with items |
---|
171 | $ctrl->SetSelection( |
---|
172 | $self->properties->{$_}->{selection}->() |
---|
173 | ) if exists $self->properties->{$_}->{selection}; |
---|
174 | |
---|
175 | $ctrl->SetStringSelection( |
---|
176 | gettext( |
---|
177 | $self->properties->{$_}->{string_selection}->() |
---|
178 | ) |
---|
179 | ) if exists $self->properties->{$_}->{string_selection}; |
---|
180 | } |
---|
181 | } |
---|
182 | keys %{$self->properties}; |
---|
183 | } |
---|
184 | |
---|
185 | sub InitLabels { |
---|
186 | my ( $self ) = @_; |
---|
187 | |
---|
188 | map { |
---|
189 | my $ctrl =$self->FindWindow($_)||$self->{$_}; |
---|
190 | #printf("ctrl %s : %s\n", $_, $ctrl); |
---|
191 | if(defined $ctrl){ |
---|
192 | # checkbox, static text |
---|
193 | $ctrl->SetLabel( |
---|
194 | $self->properties->{$_}->{label} |
---|
195 | ) if exists $self->properties->{$_}->{label}; |
---|
196 | |
---|
197 | $ctrl->GetStaticBox->SetLabel( |
---|
198 | $self->properties->{$_}->{staticbox_label} |
---|
199 | ) if exists $self->properties->{$_}->{staticbox_label}; |
---|
200 | |
---|
201 | # radiobox |
---|
202 | my $labels =$self->properties->{$_}->{labels}; |
---|
203 | $labels||=[]; |
---|
204 | for(my $i=0; $i < scalar @$labels ; $i++){ |
---|
205 | $ctrl->SetItemLabel($i, $labels->[$i]); |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | keys %{$self->properties}; |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | sub InitChoices { |
---|
214 | my ( $self ) = @_; |
---|
215 | |
---|
216 | map { |
---|
217 | my $ctrl =$self->FindWindow($_); |
---|
218 | #printf("ctrl %s : %s\n", $_, $ctrl); |
---|
219 | if(defined $ctrl){ |
---|
220 | # choice |
---|
221 | my $choices =$self->properties->{$_}->{choices}; |
---|
222 | $choices||=[]; |
---|
223 | map{ |
---|
224 | $ctrl->Append($_); |
---|
225 | }@$choices; |
---|
226 | } |
---|
227 | } |
---|
228 | keys %{$self->properties}; |
---|
229 | |
---|
230 | } |
---|
231 | |
---|
232 | sub GetProperties { |
---|
233 | my ( $self ) = @_; |
---|
234 | |
---|
235 | map { |
---|
236 | my $ctrl = $self->FindWindow($_); |
---|
237 | #printf("ctrl %s : %s\n", $_, $ctrl); |
---|
238 | if(defined $ctrl){ |
---|
239 | # checkbox, static text |
---|
240 | $self->properties->{$_}->{value}->( |
---|
241 | $ctrl->GetValue() |
---|
242 | ) if exists $self->properties->{$_}->{value}; |
---|
243 | |
---|
244 | $self->properties->{$_}->{selection}->( |
---|
245 | $ctrl->GetSelection() |
---|
246 | ) if exists $self->properties->{$_}->{selection}; |
---|
247 | |
---|
248 | $self->properties->{$_}->{string_selection}->( |
---|
249 | $ctrl->GetStringSelection() |
---|
250 | ) if exists $self->properties->{$_}->{string_selection}; |
---|
251 | } |
---|
252 | } |
---|
253 | keys %{$self->properties}; |
---|
254 | } |
---|
255 | |
---|
256 | sub ClearProperties { |
---|
257 | my ( $self ) = @_; |
---|
258 | |
---|
259 | map { |
---|
260 | my $ctrl =$self->FindWindow($_); |
---|
261 | if(defined $ctrl){ |
---|
262 | # checkbox, static text |
---|
263 | $clear_value->{ref $ctrl}->( |
---|
264 | $ctrl |
---|
265 | ) if exists $clear_value->{ref $ctrl}; |
---|
266 | # only works for control with items |
---|
267 | $ctrl->SetSelection( |
---|
268 | -1 |
---|
269 | ) if exists $self->properties->{$_}->{selection}; |
---|
270 | } |
---|
271 | |
---|
272 | $ctrl->SetStringSelection( |
---|
273 | -1 |
---|
274 | ) if exists $self->properties->{$_}->{string_selection}; |
---|
275 | } |
---|
276 | keys %{$self->properties}; |
---|
277 | |
---|
278 | } |
---|
279 | 1; |
---|