1 | package Uploader::GUI::DlgCommon; |
---|
2 | use strict; |
---|
3 | use Wx::Calendar; |
---|
4 | |
---|
5 | use base qw/ |
---|
6 | Class::Accessor::Fast |
---|
7 | /; |
---|
8 | |
---|
9 | __PACKAGE__->mk_accessors( |
---|
10 | qw/ |
---|
11 | properties |
---|
12 | / |
---|
13 | ); |
---|
14 | |
---|
15 | sub InitHandler { |
---|
16 | my ( $self ) = @_; |
---|
17 | |
---|
18 | # to connect the right event handler to each control |
---|
19 | my $ctrl_handlers = { |
---|
20 | 'Wx::TextCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_TEXT( $ctrl, $ctrl, sub { $self->OnTextCtrl(@_) } ); }, |
---|
21 | 'Wx::Choice' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_CHOICE( $ctrl, $ctrl, sub { $self->OnChoice(@_) } ); }, |
---|
22 | 'Wx::DatePickerCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_DATE_CHANGED( $ctrl, $ctrl, sub { $self->OnDatePicker(@_) } ); }, |
---|
23 | }; |
---|
24 | |
---|
25 | map { |
---|
26 | my $ctrl =$self->FindWindow($_); |
---|
27 | if(defined $ctrl){ |
---|
28 | $ctrl_handlers->{ ref $ctrl}->($ctrl) if exists $ctrl_handlers->{ ref $ctrl}; |
---|
29 | } |
---|
30 | } |
---|
31 | keys %{$self->properties}; |
---|
32 | |
---|
33 | |
---|
34 | } |
---|
35 | |
---|
36 | # what does happen when the text changes |
---|
37 | sub OnTextCtrl { |
---|
38 | my ( $self, $ctrl, $event ) = @_; |
---|
39 | |
---|
40 | my $id = $ctrl->GetId; |
---|
41 | # change the property value |
---|
42 | $self->properties->{$id}->{value}->( |
---|
43 | $event->GetString |
---|
44 | ) if exists $self->properties->{$id}->{value}; |
---|
45 | |
---|
46 | # exec the callback |
---|
47 | $self->properties->{$id}->{frame_callback}->() if exists |
---|
48 | $self->properties->{$id}->{frame_callback}; |
---|
49 | |
---|
50 | } |
---|
51 | |
---|
52 | sub OnChoice { |
---|
53 | my ( $self, $ctrl, $event ) = @_; |
---|
54 | |
---|
55 | my $id = $ctrl->GetId; |
---|
56 | # change the property value |
---|
57 | $self->properties->{$id}->{selection}->( |
---|
58 | $event->GetSelection |
---|
59 | ) if exists $self->properties->{$id}->{selection}; |
---|
60 | |
---|
61 | # exec the callback |
---|
62 | $self->properties->{$id}->{frame_callback}->() if exists |
---|
63 | $self->properties->{$id}->{frame_callback}; |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | sub OnDatePicker { |
---|
68 | my ( $self, $ctrl, $event ) = @_; |
---|
69 | |
---|
70 | my $id = $ctrl->GetId; |
---|
71 | |
---|
72 | # change the property value |
---|
73 | $self->properties->{$id}->{value}->( |
---|
74 | $event->GetDate->FormatISODate |
---|
75 | ) if exists $self->properties->{$id}->{value}; |
---|
76 | # exec the callback |
---|
77 | $self->properties->{$id}->{frame_callback}->() if exists |
---|
78 | $self->properties->{$id}->{frame_callback}; |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | my $change_value = { |
---|
84 | 'Wx::TextCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->ChangeValue($value)}, |
---|
85 | 'Wx::CheckBox' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)}, |
---|
86 | 'Wx::DatePickerCtrl' => sub { my ($ctrl, $value ) = @_; |
---|
87 | my $date = Wx::DateTime->new; |
---|
88 | my ($yyyy, $mm, $dd, $hh, $mi, $ss ) = split(/[:\/\\\-\.\s]/, $value); |
---|
89 | $date->ParseDate( |
---|
90 | sprintf("%s/%s/%s", $yyyy, $mm, $dd) |
---|
91 | ); |
---|
92 | $ctrl->SetValue($date); |
---|
93 | }, |
---|
94 | }; |
---|
95 | |
---|
96 | my $clear_value = { |
---|
97 | 'Wx::TextCtrl' => sub { my ($ctrl) = @_; $ctrl->Clear}, |
---|
98 | 'Wx::CheckBox' => sub { my ($ctrl) = @_; $ctrl->SetValue(0)}, |
---|
99 | 'Wx::DatePickerCtrl' => sub { my ($ctrl) = @_; $ctrl->SetValue(Wx::DateTime->new->SetToCurrent)}, |
---|
100 | }; |
---|
101 | |
---|
102 | sub SetProperties { |
---|
103 | my ( $self ) = @_; |
---|
104 | |
---|
105 | map { |
---|
106 | my $ctrl =$self->FindWindow($_); |
---|
107 | if(defined $ctrl){ |
---|
108 | # checkbox, static text |
---|
109 | $change_value->{ref $ctrl}->( |
---|
110 | $ctrl, |
---|
111 | $self->properties->{$_}->{value}->() |
---|
112 | ) if exists $self->properties->{$_}->{value}; |
---|
113 | # only works for control with items |
---|
114 | $ctrl->SetSelection( |
---|
115 | $self->properties->{$_}->{selection}->() |
---|
116 | ) if exists $self->properties->{$_}->{selection}; |
---|
117 | } |
---|
118 | } |
---|
119 | keys %{$self->properties}; |
---|
120 | } |
---|
121 | |
---|
122 | sub InitLabels { |
---|
123 | my ( $self ) = @_; |
---|
124 | |
---|
125 | map { |
---|
126 | my $ctrl =$self->FindWindow($_); |
---|
127 | #printf("ctrl %s : %s\n", $_, $ctrl); |
---|
128 | if(defined $ctrl){ |
---|
129 | # checkbox, static text |
---|
130 | $ctrl->SetLabel( |
---|
131 | $self->properties->{$_}->{label} |
---|
132 | ) if exists $self->properties->{$_}->{label}; |
---|
133 | |
---|
134 | # radiobox |
---|
135 | my $labels =$self->properties->{$_}->{labels}; |
---|
136 | $labels||=[]; |
---|
137 | for(my $i=0; $i < scalar @$labels ; $i++){ |
---|
138 | $ctrl->SetItemLabel($i, $labels->[$i]); |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | keys %{$self->properties}; |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | sub InitChoices { |
---|
147 | my ( $self ) = @_; |
---|
148 | |
---|
149 | map { |
---|
150 | my $ctrl =$self->FindWindow($_); |
---|
151 | #printf("ctrl %s : %s\n", $_, $ctrl); |
---|
152 | if(defined $ctrl){ |
---|
153 | # choice |
---|
154 | my $choices =$self->properties->{$_}->{choices}; |
---|
155 | $choices||=[]; |
---|
156 | map{ |
---|
157 | $ctrl->Append($_); |
---|
158 | }@$choices; |
---|
159 | } |
---|
160 | } |
---|
161 | keys %{$self->properties}; |
---|
162 | |
---|
163 | } |
---|
164 | |
---|
165 | sub GetProperties { |
---|
166 | my ( $self ) = @_; |
---|
167 | |
---|
168 | map { |
---|
169 | my $ctrl = $self->FindWindow($_); |
---|
170 | #printf("ctrl %s : %s\n", $_, $ctrl); |
---|
171 | if(defined $ctrl){ |
---|
172 | # checkbox, static text |
---|
173 | $self->properties->{$_}->{value}->( |
---|
174 | $ctrl->GetValue() |
---|
175 | ) if exists $self->properties->{$_}->{value}; |
---|
176 | |
---|
177 | $self->properties->{$_}->{selection}->( |
---|
178 | $ctrl->GetSelection() |
---|
179 | ) if exists $self->properties->{$_}->{selection}; |
---|
180 | } |
---|
181 | } |
---|
182 | keys %{$self->properties}; |
---|
183 | } |
---|
184 | |
---|
185 | sub ClearProperties { |
---|
186 | my ( $self ) = @_; |
---|
187 | |
---|
188 | map { |
---|
189 | my $ctrl =$self->FindWindow($_); |
---|
190 | if(defined $ctrl){ |
---|
191 | # checkbox, static text |
---|
192 | $clear_value->{ref $ctrl}->( |
---|
193 | $ctrl |
---|
194 | ) if exists $clear_value->{ref $ctrl}; |
---|
195 | # only works for control with items |
---|
196 | $ctrl->SetSelection( |
---|
197 | -1 |
---|
198 | ) if exists $self->properties->{$_}->{selection}; |
---|
199 | } |
---|
200 | } |
---|
201 | keys %{$self->properties}; |
---|
202 | |
---|
203 | } |
---|
204 | 1; |
---|