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