Pvoice/Panel.pm untested

filter: Statementstmt 0.0% 0 / 297 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 527 CC 42 SCAR 34.0
CC
42
Cov
0%
Next
90.0
Select
72.0
new
42.0
CRAP 30.0
1 package AAC::Pvoice::Panel;
2 use strict;
3 use warnings;
4
5 our $VERSION = sprintf("%d.%02d", q$Revision: 1.15 $=~/(\d+)\.(\d+)/);
6
7 use Wx qw(:everything);
8 use Wx::Perl::Carp;
9 use Wx::Event qw(EVT_PAINT EVT_UPDATE_UI);
10 use base qw(Wx::Panel);
11
12 #----------------------------------------------------------------------
13 sub new
14 {
15 my $class = shift;
16 $_[2] ||= wxDefaultPosition;
17 $_[3] ||= wxDefaultSize;
18 $_[4] ||= wxTAB_TRAVERSAL;
19 my $self = $class->SUPER::new(@_[0..4]);
20
21 $self->SetBackgroundColour(wxWHITE);
22
23 $self->{parent} = $_[0];
24 $self->{position} = $_[2];
25 $self->{size} = $_[3];
26 $self->{disabletextrow} = $_[5] || 0;
27 $self->{itemspacing} = $_[6] || 0;
28 $self->{selectionborder}= $_[7] || int($self->{itemspacing}/2)||1;
29 $self->{disabletitle} = $_[8] || 0;
30 $self->{tls} = Wx::FlexGridSizer->new(0,1);
31 $self->{totalrows} = 0;
32 $self->{lastrow} = 0;
33 $self->{currentpage} = 0;
34 $self->{unfinished} = 1;
35
36 $self->RoundCornerRadius(10);
37
38 my ($x, $y);
39
40 # to get the right dimensions for the items, we maximize the
41 # frame, show it, get the dimensions, and hide it again
42 if (ref($self->{parent}) =~ /Frame/)
43 {
44 if ($self->{parent}->IsMaximized)
45 {
46 ($x, $y) = @{$self->{size}};
47 }
48 else
49 {
50 $self->{parent}->Maximize(1);
51 $self->{parent}->Show(1);
52 ($x, $y) = ( $self->GetSize()->GetWidth(),
53 $self->GetSize()->GetHeight() );
54 $self->{parent}->Show(0);
55 }
56 }
57 else
58 {
59 # if our parent isn't a frame, we're not able to maximize
60 # it, so we just get the dimensions of the parent
61 ($x, $y) = @{$self->{size}};
62 }
63 $self->{realx} = $x;
64 $self->{realy} = $y;
65 $self->xsize($x);
66 $self->ysize($y);
67 unless ($self->{disabletitle})
68 {
69 # to be able to calculate the useable y-size, we need to add the
70 # title and get its height. Since we don't know what the title
71 # will be, we'll simple draw an empty title.
72 $self->TitleFont(Wx::Font->new( 18, # font size
73 wxDECORATIVE, # font family
74 wxNORMAL, # style
75 wxNORMAL, # weight
76 0,
77 'Comic Sans MS', # face name
78 wxFONTENCODING_SYSTEM)) unless $self->TitleFont;
79 $self->AddTitle('');
80 my $usableysize = $y - 2*$self->{title}->GetSize()->GetHeight();
81 # If we want to use a textrow, we have to subtract another 60
82 # pixels from the y size, since the textrow is always 60 pixels high.
83 $usableysize-=60 unless $self->{disabletextrow};
84 $self->ysize($usableysize);
85 }
86 # set the default colours...these can of course be changed...
87 $self->SelectColour(Wx::Colour->new(255,131,131));
88 $self->BackgroundColour(Wx::Colour->new(220,220,220));
89
90 $self->SetSizer($self->{tls});
91 $self->SetAutoLayout(1);
92
93 $self->{displaytextsave} = [];
94 $self->{speechtextsave} = [];
95
96 # Initialize the input stuff
97 $self->{input} = AAC::Pvoice::Input->new($self);
98 $self->{input}->Next( sub{$self->Next});
99 $self->{input}->Select(sub{$self->Select});
100
101 $self->{rowcolumnscanning} = ($self->{input}->GetDevice ne 'mouse');
102 EVT_PAINT($self, \&OnPaint);
103 EVT_UPDATE_UI($self, $self, \&OnPaint);
104 return $self;
105 }
106
107 sub SetEditmode
108 {
109 my $self = shift;
110 $self->{editmode} = shift;
111 }
112
113 sub OnPaint
114 {
115 my ($self, $event) = @_;
116 $self->{setselection} = 1;
117 my $dc = Wx::PaintDC->new($self);
118 $self->SetBackgroundColour($self->{parent}->GetBackgroundColour);
119 $self->DrawBackground($dc);
120 if ($self->{rowcolumnscanning} && not $self->{editmode})
121 {
122 $dc = Wx::WindowDC->new($self->{selectedwindow}->GetParent);
123 $self->DrawBorder($dc);
124 }
125 $event->Skip;
126 }
127
128 sub DrawBorder
129 {
130 my $self = shift;
131 my $dc = shift;
132 my $window = $self->{selectedwindow};
133 my ($x, $y) = $window->GetPositionXY;
134 my $size = $window->GetSize;
135 my ($xsize, $ysize) = ($size->GetWidth, $size->GetHeight);
136 $dc->BeginDrawing;
137 $dc->SetBrush(wxTRANSPARENT_BRUSH);
138 $dc->SetPen(Wx::Pen->new($self->{setselection} ? $self->SelectColour :
139 $self->BackgroundColour, $self->{selectionborder}, wxSOLID));
140 $dc->DrawRoundedRectangle($x-($self->{itemspacing}/2-1), $y-($self->{itemspacing}/2-1), $xsize+($self->{itemspacing}/2+1), $ysize+($self->{itemspacing}/2+1), $self->RoundCornerRadius);
141 $dc->EndDrawing;
142 }
143
144 sub SetSelectionBorder
145 {
146 my $self = shift;
147 $self->{selectedwindow} = shift;
148 $self->{setselection} = 1;
149 my $dc = Wx::WindowDC->new($self->{selectedwindow}->GetParent);
150 $self->DrawBorder($dc);
151 }
152
153 sub SetNormalBorder
154 {
155 my $self = shift;
156 $self->{selectedwindow} = shift;
157 $self->{setselection} = 0;
158 my $dc = Wx::WindowDC->new($self->{selectedwindow}->GetParent);
159 $self->DrawBorder($dc);
160 }
161
162 sub RoundCornerRadius
163 {
164 my $self = shift;
165 $self->{radius} = shift || $self->{radius};
166 return $self->{radius};
167 }
168
169 sub xsize
170 {
171 my $self = shift;
172 $self->{xsize} = shift || $self->{xsize};
173 return $self->{xsize}-2*$self->RoundCornerRadius;
174 }
175
176 sub ysize
177 {
178 my $self = shift;
179 $self->{ysize} = shift || $self->{ysize};
180 return $self->{ysize}-2*$self->RoundCornerRadius;
181 }
182
183 sub lastrow
184 {
185 my $self = shift;
186 return $self->{lastrow};
187 }
188
189
190 sub SelectColour
191 {
192 my $self = shift;
193 $self->{selectcolour} = shift || $self->{selectcolour};
194 return $self->{selectcolour};
195 }
196
197 sub BackgroundColour
198 {
199 my $self = shift;
200 $self->{backgroundcolour} = shift || $self->{backgroundcolour};
201 return $self->{backgroundcolour};
202 }
203
204 sub DrawBackground
205 {
206 my $self = shift;
207 my $dc = shift;
208 $dc->SetBrush(Wx::Brush->new($self->BackgroundColour, wxSOLID));
209 $dc->SetPen(Wx::Pen->new($self->BackgroundColour, 1, wxSOLID));
210 $dc->DrawRoundedRectangle(0,0,$self->{realx}, $self->{realy}, $self->RoundCornerRadius);
211 }
212
213 sub AddTitle
214 {
215 my ($self, $title) = @_;
216 return if $self->{disabletitle};
217 my $titleupdate = exists $self->{title};
218 if ($titleupdate)
219 {
220 $self->{tls}->Remove($self->{title});
221 }
222 # Create the TextControl
223 $self->{title} = Wx::StaticText->new( $self,
224 -1,
225 $title,
226 wxDefaultPosition,
227 wxDefaultSize,
228 wxALIGN_CENTRE);
229 $self->TitleFont();
230 # Don't use 'Add' here...the title should be on top!!
231 $self->{tls}->Prepend($self->{title},0, wxALIGN_CENTRE, 0);
232 }
233
234 sub TitleFont
235 {
236 my $self = shift;
237 $self->{titlefont} = shift || $self->{titlefont};
238 return if not $self->{titlefont};
239 $self->{title}->SetFont($self->{titlefont}) if $self->{title};
240 return $self->{titlefont};
241 }
242
243 sub Append
244 {
245 my $self = shift;
246 my $row = shift;
247 my $unselectable = shift;
248 $self->{tls}->Add($row, # what to add
249 0, # unused
250 wxALIGN_CENTRE, # style
251 0); # padding
252
253 # setup the input event handling unless we're in editmode
254 unless ($self->{editmode})
255 {
256 $row->{input} = AAC::Pvoice::Input->newchild($row);
257 $row->{input}->Next( sub{$self->Next});
258 $row->{input}->Select(sub{$self->Select});
259 my $index=0;
260 foreach my $child ($row->GetChildren)
261 {
262 $child->{input} = AAC::Pvoice::Input->newchild($child);
263 $child->{input}->Next( sub{$self->Next});
264 $child->{input}->Select(sub{$self->Select});
265 if ((defined $row->{ids}->[$index]) && ($child->GetId == $row->{ids}->[$index]))
266 {
267 my $action = $row->{actions}->[$index];
268 $self->{input}->SetupMouse($child, sub{$self->SetSelectionBorder($child)}, $action, sub{$self->SetNormalBorder($child)});
269 $index++;
270 }
271 }
272 }
273
274 $self->{totalrows}++ if not $unselectable;
275 $self->{lastrow}++;
276 push @{$self->{rows}}, $row if not $unselectable;
277 push @{$self->{unselectablerows}}, $row if $unselectable;
278 }
279
280 sub PauseInput
281 {
282 my $self = shift;
283 my $bool = shift;
284 $self->{input}->PauseMonitor($bool);
285 $self->{input}->PauseAutoscan($bool);
286 $self->{input}->Pause($bool);
287 }
288
289 sub Clear
290 {
291 my $self = shift;
292 $self->{tls}->Remove($_) for (0..$self->{lastrow});
293 foreach my $row (@{$self->{rows}})
294 {
295 $_->Destroy for $row->GetChildren();
296 $row->Destroy
297 }
298 $_->Destroy for @{$self->{unselectablerows}};
299 $self->{text}->Destroy if exists $self->{text};
300 $self->{title}->Destroy if exists $self->{title};
301 $self->{rows} = [];
302 $self->{unselectablerows} = [];
303 $self->SUPER::Clear();
304 $self->{totalrows} = 0;
305 $self->{lastrow} = 0;
306 $self->Refresh;
307 }
308
309 sub Finalize
310 {
311 my $self = shift;
312 my $dc = Wx::WindowDC->new($self);
313 $self->DrawBackground($dc);
314
315 unless ($self->{disabletextrow})
316 {
317 # Create the TextControl
318 my $font = Wx::Font->new( 24, # font size
319 wxSWISS, # font family
320 wxNORMAL, # style
321 wxNORMAL, # weight
322 0,
323 'Comic Sans MS',# face name
324 wxFONTENCODING_SYSTEM);
325 $font->SetUnderlined(1);
326 $self->{ta} = Wx::TextAttr->new( Wx::Colour->new(0,0,0), # textcol
327 Wx::Colour->new(255,255,255),# backgr.
328 $font); # font
329 my $rowsizer = Wx::GridSizer->new(1,0);
330 $self->{text} = Wx::TextCtrl->new( $self, # parent
331 -1, # id
332 '', # text
333 wxDefaultPosition,# position
334 [4*($self->{realx}/5),60], # size
335 wxTE_RICH|
336 wxTE_MULTILINE); # style
337 # set the text-attributes
338 $self->{text}->SetDefaultStyle($self->{ta});
339 $rowsizer->Add($self->{text}, # what to add
340 0, # unused
341 wxALIGN_CENTRE|wxALL, # style
342 $self->{itemspacing}); # padding
343 $self->{tls}->Add($rowsizer, 0, wxALIGN_CENTRE, 0);
344 $self->{textrowsizer} = $rowsizer;
345
346 $self->{text}->SetValue(my $x = $self->RetrieveText);
347 $self->{text}->SetStyle(0, length($self->{text}->GetValue), $self->{ta});
348 $self->{text}->Refresh(); # Added to test it on the Mercury...added text
349 # isn't visible there...
350 }
351 $self->{title}->SetBackgroundColour($self->BackgroundColour) unless $self->{disabletitle};
352
353 $self->{tls}->AddGrowableCol(0);
354 $self->Layout;
355
356 # Select the first row
357 $self->{selectedrow} = 0;
358 $self->{selecteditem} = 0;
359 $self->SetSelectionBorder($self->{rows}->[$self->{selectedrow}]) if $self->{rowcolumnscanning} && not $self->{editmode};
360 $self->{rowselection} = 1;
361 $self->Refresh;
362 $self->Update();
363 $self->{unfinished} = 0;
364 }
365
366 sub Next
367 {
368 my $self = shift;
369 return if ($self->{editmode} || $self->{unfinished});
370 $self->{input}->QuitAutoscan;
371 if ($self->{rowselection})
372 {
373 $self->SetNormalBorder($self->{rows}->[$self->{selectedrow}]) if $self->{rowcolumnscanning};
374 if ($self->{selectedrow} < ($self->{totalrows}-1))
375 {
376 $self->{selectedrow}++;
377 }
378 else
379 {
380 $self->{selectedrow} = 0;
381 }
382 $self->SetSelectionBorder($self->{rows}->[$self->{selectedrow}]) if $self->{rowcolumnscanning};
383 }
384 else
385 {
386 $self->SetNormalBorder($self->{rows}->[$self->{selectedrow}]->{items}->[$self->{selecteditem}]) if $self->{rowcolumnscanning};
387 if ($self->{selecteditem} < ($self->{rows}->[$self->{selectedrow}]->{totalitems}-1))
388 {
389 $self->{selecteditem}++;
390 }
391 else
392 {
393 $self->{selecteditem} = 0;
394 }
395 $self->SetSelectionBorder($self->{rows}->[$self->{selectedrow}]->{items}->[$self->{selecteditem}]) if $self->{rowcolumnscanning};
396 }
397 $self->{input}->StartAutoscan;
398 }
399
400 sub Select
401 {
402 my $self = shift;
403 return if $self->{editmode};
404 $self->{input}->QuitAutoscan;
405 if (($self->{rowselection}) && (@{$self->{rows}->[$self->{selectedrow}]->{items}} == 1))
406 {
407 $self->{rowselection} = 0;
408 $self->{selecteditem} = 0;
409 }
410 if ($self->{rowselection})
411 {
412 $self->SetNormalBorder($self->{rows}->[$self->{selectedrow}]) if $self->{rowcolumnscanning};
413 $self->{rowselection} = 0;
414 $self->{selecteditem} = 0;
415 $self->SetSelectionBorder($self->{rows}->[$self->{selectedrow}]->{items}->[$self->{selecteditem}]) if $self->{rowcolumnscanning};
416 }
417 else
418 {
419 $self->SetNormalBorder($self->{rows}->[$self->{selectedrow}]->{items}->[$self->{selecteditem}]) if $self->{rowcolumnscanning};
420 $self->SetSelectionBorder($self->{rows}->[$self->{selectedrow}]) if $self->{rowcolumnscanning};
421
422 &{$self->{rows}->[$self->{selectedrow}]->{actions}->[$self->{selecteditem}]};
423 $self->{rowselection} = 1;
424 }
425 $self->{input}->StartAutoscan;
426 }
427
428 sub ToRowSelection
429 {
430 my $self = shift;
431 return if $self->{editmode};
432 $self->SetNormalBorder($self->{rows}->[$self->{selectedrow}]->{items}->[$self->{selecteditem}]) if $self->{rowcolumnscanning};
433 $self->SetSelectionBorder($self->{rows}->[$self->{selectedrow}]) if $self->{rowcolumnscanning};
434 $self->{rowselection} = 1;
435 }
436
437 sub DisplayAddText
438 {
439 my $self = shift;
440 push @{$self->{displaytextsave}}, $_[0];
441 $self->{text}->AppendText($_[0]);
442 $self->{text}->Refresh(); # Added to test it on the Mercury...added text
443 # isn't visible there...
444 }
445
446 sub SpeechAddText
447 {
448 my $self = shift;
449 push @{$self->{speechtextsave}}, $_[0];
450 }
451
452 sub RetrieveText
453 {
454 my $self = shift;
455 return wantarray ? @{$self->{displaytextsave}} : join('', @{$self->{displaytextsave}});
456 }
457
458 sub ClearText
459 {
460 my $self = shift;
461 $self->{displaytextsave}=[];
462 $self->{speechtextsave}=[];
463 $self->{text}->SetValue('');
464 $self->{text}->Refresh(); # Added to test it on the Mercury...added text
465 # isn't visible there...
466 }
467
468 sub BackspaceText
469 {
470 my $self = shift;
471 pop @{$self->{displaytextsave}};
472 pop @{$self->{speechtextsave}};
473 $self->{text}->SetValue(my $x = $self->RetrieveText);
474 $self->{text}->SetStyle(0, length($self->{text}->GetValue), $self->{ta});
475 $self->{text}->Refresh(); # Added to test it on the Mercury...added text
476 # isn't visible there...
477 }
478
479 sub SpeechRetrieveText
480 {
481 my $self = shift;
482 return wantarray ? @{$self->{speechtextsave}} : join('', @{$self->{speechtextsave}});
483 }
484
485 1;
486
487 __END__