blib/lib/QWizard/Generator/Tk.pm untested

filter: Statementstmt 0.0% 0 / 543 Branchbran 0.0% 0 / 152 Conditioncond 0.0% 0 / 92 MC/DCmcdc 0.0% 0 / 52 Subroutinesub 0.0% 0 / 64 Podpod 0.0% 0 / 51 totaltotal 0.0% 0 / 954 CC 77 SCAR 32.0
CC
77
Cov
0%
make_top
72.0
put_it
72.0
do_ok_cancel
56.0
CRAP 24.5
1 package QWizard::Generator::Tk;
2  
3 #
4 # TODO:
5 # - better layout; currently ugly
6 # - bar support
7 # - left/right side support
8  
9 use strict;
10 my $VERSION = '3.15';
11 use Tk;
12 use Tk::Table;
13 use Tk::Pane;
14 use Tk::FileSelect;
15 require Exporter;
16 use QWizard::Generator;
17  
18 @QWizard::Generator::Tk::ISA = qw(Exporter QWizard::Generator);
19  
20 my $have_gd_graph = eval { require GD::Graph::lines; };
21 my $have_tk_tree = eval { require Tk::Tree; };
22 my $have_tk_png = eval { require Tk::PNG; };
23  
24 sub new {
25 my $type = shift;
26 my ($class) = ref($type) || $type;
27 my $self = {'keep_working_hook' => \&QWizard::Generator::backup_params};
28 for (my $i = 0; $i <= $#_; $i += 2) {
29 $self->{$_[$i]} = $_[$i+1];
30 }
31 bless($self, $class);
32 $self->add_handler('text',\&QWizard::Generator::Tk::do_entry,
33 [['single','name'],
34 ['default'],
35 ['forced','0'],
36 ['single','size'],
37 ['single','maxsize'],
38 ['single','submit'],
39 ['single','refresh_on_change']]);
40 # XXX: we need to do a real text box
41 $self->add_handler('textbox',\&QWizard::Generator::Tk::do_textbox,
42 [['single','name'],
43 ['default'],
44 ['single','size'],
45 ['single','width'],
46 ['single','height']]);
47 $self->add_handler('hidetext',\&QWizard::Generator::Tk::do_entry,
48 [['single','name'],
49 ['default'],
50 ['forced','1'],
51 ['single','size'],
52 ['single','maxsize'],
53 ['single','submit'],
54 ['single','refresh_on_change']]);
55 $self->add_handler('checkbox',\&QWizard::Generator::Tk::do_checkbox,
56 [['multi','values'],
57 ['default'],
58 ['single','button_label']]);
59 $self->add_handler('multi_checkbox',
60 \&QWizard::Generator::Tk::do_multicheckbox,
61 [['multi','default'],
62 ['values,labels']]);
63 $self->add_handler('menu',
64 \&QWizard::Generator::Tk::do_menu,
65 [['values,labels'],
66 ['default'],
67 ['single','name']]);
68 $self->add_handler('radio',
69 \&QWizard::Generator::Tk::do_radio,
70 [['values,labels', " "],
71 ['default'],
72 ['single','name']]);
73 $self->add_handler('label',
74 \&QWizard::Generator::Tk::do_label,
75 [['multi','values']]);
76 $self->add_handler('paragraph',
77 \&QWizard::Generator::Tk::do_paragraph,
78 [['multi','values'],
79 ['single','preformatted'],
80 ['single','width']]);
81 $self->add_handler('button',
82 \&QWizard::Generator::Tk::do_button,
83 [['single','values'],
84 ['default']]);
85 $self->add_handler('table',
86 \&QWizard::Generator::Tk::do_table,
87 [['norecurse','values'],
88 ['norecurse','headers']]);
89 $self->add_handler('graph',
90 \&QWizard::Generator::Tk::do_graph,
91 [['norecurse','values'],
92 ['norecursemulti','graph_options']]);
93 $self->add_handler('image',
94 \&QWizard::Generator::Tk::do_image,
95 [['norecurse','imgdata'],
96 ['norecurse','image'],
97 ['single','imagealt']]);
98 $self->add_handler('fileupload',
99 \&QWizard::Generator::Tk::do_fileupload,
100 [['default','values']]);
101 $self->add_handler('filedownload',
102 \&QWizard::Generator::Tk::do_filedownload,
103 [['default','values']]);
104  
105 $self->add_handler('unknown',
106 \&QWizard::Generator::Tk::do_unknown,
107 []);
108  
109 $self->init_default_storage();
110 return $self;
111 }
112  
113 sub goto_top {
114 my $self = shift;
115 my $wiz = shift;
116 $self->unmake_top();
117 $wiz->reset_qwizard();
118 }
119  
120 sub goto_next {
121 # print STDERR "-----\n";
122 # for (my $i = 0; $i <= $#_; $i++) {
123 # print STDERR "next $i: $_[$i]\n";
124 # }
125 # my @stuff = caller(1);
126 # print STDERR "$stuff[1] $stuff[2] -> $stuff[3]\n";
127 # my @stuff = caller(2);
128 # print STDERR "$stuff[1] $stuff[2] -> $stuff[3]\n";
129 # my @stuff = caller(3);
130 # print STDERR "$stuff[1] $stuff[2] -> $stuff[3]\n";
131 # my @stuff = caller(4);
132 # print STDERR "$stuff[1] $stuff[2] -> $stuff[3]\n";
133 # print STDERR "-----\n";
134 shift if (ref($_[0]) ne 'QWizard::Generator::Tk');
135 my ($self, $ignorefirst_or_varname, $refresh_on_change, $val) = @_;
136 if ($ignorefirst_or_varname &&
137 ref($ignorefirst_or_varname) eq 'SCALAR' && $$ignorefirst_or_varname) {
138 $$ignorefirst_or_varname--;
139 return;
140 } elsif (ref($ignorefirst_or_varname) ne 'SCALAR') {
141 if ($ignorefirst_or_varname) {
142 $self->qwparam($ignorefirst_or_varname, $val);
143 }
144 }
145 if ($refresh_on_change) {
146 $self->qwparam('redo_screen',1);
147 }
148  
149 $self->unmake_top();
150 # print STDERR "-----x\n";
151 }
152  
153 sub goto_prev {
154 my ($self) = @_;
155 $self->revert_params();
156 $self->unmake_top();
157 }
158  
159 sub our_mainloop {
160 my $self = shift;
161 while ($self->{'qtable'} && Tk::MainWindow->Count) {
162 if ($self->{'nextf'}) {
163 $self->{'nextf'}->focus();
164 $self->{'nextf'} = undef;
165 }
166 DoOneEvent(0);
167 }
168 }
169  
170 sub unmake_top {
171 my $self = shift;
172 $self->{'qtable'}->destroy() if ($self->{'qtable'});
173 $self->{'qtable'} = undef;
174 }
175  
176 sub make_top {
177 my $self = shift;
178 $self->unmake_top();
179  
180 if (!$self->{'top'}) {
181 $self->{'top'} = $self->{'window'}->Frame();
182 $self->{'top'}->pack(-expand => 1, -fill => 'both');
183 my $haveballoon = eval {require Tk::Balloon;};
184 if ($haveballoon) {
185 $self->{'balloon'} = $self->{'top'}->Balloon();
186 }
187 }
188  
189 my $px = $self->{'top'}->width();
190 my $py = $self->{'top'}->height();
191 $px = $self->{'qwidth'} || 600 if ($px < 600);
192 $py = $self->{'qheight'} || 500 if ($py < 500);
193  
194 if (!$self->{'qtitle'}) {
195 $self->{'qtitle'} = $self->{'top'}->Label();
196 $self->{'qtitle'}->pack(-side => 'top');
197 }
198 if (!$self->{'qintro'}) {
199 $self->{'qintro'} = $self->{'top'}->Scrolled('Text',
200 -scrollbars => 'w',
201 -width => $px,
202 -height => 200,
203 -wrap => 'word',
204 -relief => 'flat');
205 $self->{'qintro'}->pack(-side => 'top', -expand => 1, -fill => 'both');
206 }
207 if (!$self->{'qpane'}) {
208 $self->{'qpane'} = $self->{'top'}->Scrolled('Pane', -width => $px,
209 -height => $py,
210 -sticky => 'nsew');
211 $self->{'qpane'}->pack(-expand => 1, -fill => 'both');
212 }
213 $self->{'qtable'} = $self->{'qpane'}->Table(-rows => 200,
214 -columns => 10,
215 -scrollbars => '');
216 $self->{'qtable'}->pack(-expand => 1, -fill => 'both');
217  
218 # we make some decisions based on which table we're currently
219 # pointing at; thus remember the original.
220 $self->{'origqtable'} = $self->{'qtable'};
221 }
222  
223 sub init_screen {
224 my ($self, $wiz, $title) = @_;
225 if (!$self->{'window'}) {
226 $self->{'window'} = new MainWindow(
227 -title => $title,
228 #-background => $self->{'bgcolor'} || $wiz->{'bgcolor'} || "#ffffff"
229 );
230 $self->{'tktitle'} =
231 $self->{'window'}->Label(-text => $title,
232 -relief => 'raised',
233 -foreground => '#ffa26c',
234 -background => $self->{'bgcolor'} ||
235 $wiz->{'bgcolor'} ||
236 "#ffffff");
237 $self->{'tktitle'}->pack(-expand => 1, -fill => 'x', -side => 'top');
238 }
239 $self->make_top();
240 $self->{'qadd'} = 0;
241 }
242  
243 sub do_ok_cancel {
244 my ($self, $nexttext, $wiz, $dontdocan) = @_;
245 if (!$self->{'bot'}) {
246 $self->{'bot'} = $self->{'top'}->Frame(-relief => 'raised',
247 -borderwidth => 3);
248 if (!$self->{'prevbut'}) {
249 $self->{'prevbut'} =
250 $self->{'bot'}->Button(-text => ($wiz->{'back_text'} ||
251 'Back'),
252 -command => [\&goto_prev,
253 $self]);
254 $self->{'prevbut'}->pack(-side => 'left');
255 }
256 if (!$self->{'nextbut'}) {
257 my $text =
258 QWizard::Generator::remove_accelerator($nexttext ||
259 $wiz->{'next_text'} ||
260 'Next');
261 $self->{'nextbut'} =
262 $self->{'bot'}->Button(-text => $text,
263 -command => [\&goto_next,
264 $self]);
265 $self->{'nextbut'}->pack(-side => 'left');
266 }
267 if (! $dontdocan) {
268 if (!$self->{'canbut'}) {
269 $self->{'canbut'} =
270 $self->{'bot'}->Button(-text => ($wiz->{'cancel_text'} ||
271 'Cancel'),
272 -command => [\&goto_top, $self, $wiz]);
273 $self->{'canbut'}->pack(-side => 'right');
274 }
275 }
276 $self->{'bot'}->pack(-expand => 1, -fill => 'x');
277 } else {
278 my $text =
279 QWizard::Generator::remove_accelerator($nexttext || 'Ok');
280 $self->{'nextbut'}->configure(-text => $text);
281 }
282  
283 # see if we have backup places to get to. If not, grey out the button
284 if ($#{$self->{'backupvars'}} > -1) {
285 $self->{'prevbut'}->configure(-state => 'normal');
286 } else {
287 $self->{'prevbut'}->configure(-state => 'disabled');
288 }
289 }
290  
291  
292 # put stuff at a particular spot in the current table
293 sub put_it {
294 my ($self, $w, $row, $col) = @_;
295 if (!$row) {
296 if (exists($self->{'currentrow'}) && defined($self->{'currentrow'})) {
297 $row = $self->{'currentrow'};
298 } else {
299 $row = $self->{'currentq'};
300 }
301 }
302 if (!$col) {
303 if (exists($self->{'currentcol'}) && defined($self->{'currentcol'})) {
304 $col = $self->{'currentcol'};
305 } else {
306 $col = 2;
307 }
308 }
309  
310 # remove the temp assignments
311  
312 if (ref($w) eq '') {
313 $w = $self->{'qtable'}->Label(-text => $w,
314 -anchor => 'w');
315 }
316  
317 # place the item in the table
318 $self->{'qtable'}->put($row, $col, $w);
319  
320 # bind the tab and alt-tab key presses to forward and backward widgets
321 if (ref($w) =~ /Entry|Menu|Text|Button|Checkbutton|Radio|Optionmenu/) {
322 if ($self->{'lastw'}) {
323 $self->{'lastw'}->bind('<Tab>',[\&tab_next, $w, $self]);
324 $w->bind('<Alt-Key-Tab>',[\&tab_next, $self->{'lastw'}, $self]);
325 }
326 $self->{'lastw'} = $w;
327 }
328 }
329  
330 sub tab_next {
331 # forcing the focus here doesn't work (I suspect because the top
332 # level tab binding gets called after us and takes precidence and
333 # focuses away from our containing table. Thus we save our focus
334 # call for even later)
335 $_[2]->{'nextf'} = $_[1];
336 }
337  
338 sub set_default {
339 my ($self, $q, $def) = @_;
340 $self->qwparam($q->{'name'}, $def) if ($def && $self->qwparam($q->{'name'}) ne $def);
341 }
342  
343 ######################################################################
344 # QWizard functions for doing stuff.
345  
346 sub wait_for {
347 my ($self, $wiz, $next) = @_;
348 $self->do_ok_cancel($next, $wiz);
349 $self->our_mainloop();
350 return 1;
351 }
352  
353 sub do_error {
354 my ($self, $q, $wiz, $p, $err) = @_;
355 $self->{'currentq'}++;
356 $self->{'qadd'}++;
357 $self->put_it($self->{'qtable'}->Label(-text => $err,
358 -foreground => 'red'),
359 undef, 1);
360  
361 }
362  
363 sub do_question {
364 my ($self, $q, $wiz, $p, $text, $qcount) = @_;
365 my $top = $self->{'qtable'};
366  
367 $self->{'currentq'} = $qcount + $self->{'qadd'};
368 return if (!$text && $self->{'qtable'} != $self->{'origqtable'});
369  
370 #
371 # Get the actual help text, in case this is a subroutine.
372 #
373 my $helptext = $q->{'helpdesc'};
374 if (ref($helptext) eq "CODE") {
375 $helptext = $helptext->();
376 }
377  
378 $text = " $text" if ($q->{'indent'});
379 if ($helptext && !$self->qwpref('usehelpballons')) {
380 my $f = $top->Frame();
381 $f->Label(-text => $text, -anchor => 'nw')->pack(-anchor => 'w');
382 $helptext = " $helptext" if ($q->{'indent'});
383 $helptext = " $helptext";
384 my $height = int(length($helptext)/40)+1;
385 my $t = $f->Text(-width => 40,
386 -height => $height,
387 -relief => 'flat',
388 -wrap => 'word',
389 -font => 'Helvetica 12 italic')
390 ->pack(-anchor => 'w');
391 $t->insert('end', $helptext);
392 $self->put_it($f, undef, 1);
393 } else {
394 my $l = $top->Label(-text => $text, -anchor => 'nw');
395 $self->put_it($l, undef, 1);
396 if ($self->{'balloon'} && $helptext) {
397 $self->{'balloon'}->attach($l, -balloonmsg => $helptext);
398 # XXX: change the "help" window text, which doesn't exist yet.
399 }
400 }
401 }
402  
403 sub start_questions {
404 my ($self, $wiz, $p, $title, $intro) = @_;
405 if ($title) {
406 $self->{'qtitle'}->configure(-text => $title);
407 }
408
409 $self->{'qintro'}->delete('1.0','end');
410 if ($intro) {
411 $self->{'qintro'}->configure(-height => (length($intro)/80 + 1));
412 $self->{'qintro'}->insert('end',$intro);
413 } else {
414 $self->{'qintro'}->configure(-height => 0);
415 }
416 }
417  
418 sub end_questions {
419 my $self = shift;
420 # this makes us keep adding new table rows during a merge
421 $self->{'qadd'} = $self->{'currentq'} + 1;
422 $self->{'lastw'} = undef;
423 }
424  
425  
426 ##################################################
427 # widgets
428 ##################################################
429  
430 sub get_extra_args {
431 my ($self, $q, $wiz) = @_;
432  
433 my @args;
434  
435 if (($q->{'submit'} || $q->{'refresh_on_change'}) &&
436 $q->{type} ne 'text') {
437 my $ignorefirst = 0;
438 if ($q->{'type'} eq 'menu') {
439 # menus do an initial call immediately after being created.
440 # we use this hack to ignore the first call to the function.
441 # (did I mention "sigh"?)
442 # my $wehere = $self->qwparam('redo_screen');
443 # print "setting ignoring first: $wehere\n";
444 # $ignorefirst = 1 unless($wehere eq '') ;
445 $ignorefirst = 1;
446 }
447 push @args, '-command', [\&goto_next, $self, \$ignorefirst,
448 $q->{'refresh_on_change'}];
449 }
450 return \@args;
451 }
452  
453 sub make_check {
454 my ($self, $name, $text, $on, $off, $top, $defval) = @_;
455  
456 my $x = "hi";
457 $top = $self->{'qf'} if (!$top);
458 $top->Checkbutton(-textvariable => \$text,
459 -variable => \$self->{'datastore'}{'vars'}{$name},
460 -anchor => 'w')
461 ->pack(-side => 'top', -expand => 1, -fill => 'x');
462 }
463  
464  
465 sub do_button {
466 my ($self, $q, $wiz, $p, $vals, $def) = @_;
467 my $but = $self->{'qtable'}->Button(-text => $vals,
468 -command => [\&goto_next,
469 $self, $q->{'name'},
470 $q->{'refresh_on_change'},
471 $def]);
472 $self->put_it($but);
473 }
474  
475 sub do_checkbox {
476 my ($self, $q, $wiz, $p, $vals, $def, $button_label) = @_;
477 $vals = [1, 0] if ($#$vals == -1);
478 my $chk = $self->{'qtable'}->Checkbutton(-anchor => 'w',
479 -onvalue => $vals->[0],
480 -offvalue => $vals->[1],
481 -text => $button_label,
482 -variable =>
483 \$self->{'datastore'}{'vars'}{$q->{'name'}},
484 @{$self->get_extra_args($q, $wiz,
485 $p)}
486 );
487 $self->put_it($chk);
488 $self->set_default($q, $def);
489 }
490  
491 sub do_multicheckbox {
492 my ($self, $q, $wiz, $p, $defs, $vals, $labels) = @_;
493 my $tf = $self->{'qtable'}->Frame();
494 my $count = -1;
495 foreach my $v (@$vals) {
496 $count++;
497 my $l = (($labels->{$v}) ? $labels->{$v} : "$v");
498 make_check($self, $q->{'name'} . $l, $l, $v, '', $tf);
499 push @{$wiz->{'passvars'}},$q->{'name'} . $v;
500 $self->{'datastore'}->set($v, $defs->[$count]);
501 }
502 $self->put_it($tf);
503 }
504  
505 sub do_radio {
506 my ($self, $q, $wiz, $p, $vals, $labels, $def, $name) = @_;
507 my $tf = $self->{'qtable'}->Frame();
508  
509 my $widargs = $self->get_extra_args($q, $wiz, $p);
510  
511 foreach my $val (@$vals) {
512 my $text = (($labels->{$val}) ? $labels->{$val} : "$val");
513 $tf->Radiobutton(-value => $val, -textvariable => \$text,
514 -variable => \$self->{'datastore'}{'vars'}{$name},
515 -anchor => 'w',
516 @{$self->get_extra_args($q, $wiz, $p)})
517 ->pack(-side => 'top', -fill => 'x', -expand => 1);
518 }
519 $self->put_it($tf);
520  
521 $self->set_default($q, $def);
522 }
523  
524 sub do_label {
525 my ($self, $q, $wiz, $p, $vals, $def) = @_;
526 if (defined ($vals)) {
527 foreach my $i (@$vals) {
528 $self->put_it($i);
529 }
530 }
531 }
532  
533 sub do_paragraph {
534 my ($self, $q, $wiz, $p, $vals, $preformatted, $width) = @_;
535 my $w = $width || 80;
536 foreach my $i (@$vals) {
537 my $t;
538 if ($preformatted) {
539 my $c = $i;
540 $c =~ s/[^\n]//g; # XXX: must be a better and more efficient way
541 $t = $self->{'qtable'}->Scrolled('Text', -width => $w,
542 -height => length($c) || 24,
543 -wrap => 'none',
544 -relief => 'flat');
545 } else {
546 $t = $self->{'qtable'}->Text(-width => $w,
547 -height =>
548 int(length($i)/40) + 1,
549 -wrap => 'word',
550 -relief => 'flat');
551 }
552 $t->insert('end',$i);
553 $self->put_it($t);
554 }
555 }
556  
557 sub do_menu {
558 my ($self, $q, $wiz, $p, $vals, $labels, $def, $name) = @_;
559  
560 my @items;
561 foreach my $v (@$vals) {
562 if ($labels->{$v}) {
563 push @items, [ $labels->{$v} => $v ];
564 } else {
565 push @items, $v;
566 $labels->{$v} = $v;
567 }
568 if (defined($def) && $v eq $def) {
569 # Tk::Optionmenu sucks badly when it comes to default; the
570 # default value must be the first in the list because that
571 # is what is shown. ugh. XXX: maybe use a Tk::BrowseEntry?
572 unshift @items, (pop @items);
573 }
574 }
575  
576 $self->set_default($q, $def);
577 $self->put_it($self->{'qtable'}->Optionmenu(-options => \@items,
578 -variable => \$self->{'datastore'}{'vars'}{$name},
579 -relief => 'raised',
580 @{$self->get_extra_args($q, $wiz, $p)}));
581 }
582  
583 sub select_openfile {
584 my ($self, $name, $widget) = @_;
585  
586 my $file = $self->{'qtable'}->getOpenFile();
587 $widget->configure(-text => 'Select File: ' . $file);
588 $self->qwparam($name, $file) if ($file ne '');
589 }
590  
591 sub do_fileupload {
592 my ($self, $q, $wiz, $p, $def) = @_;
593  
594 my $openbutton =
595 $self->{'qtable'}->Button(-text => 'Select File...');
596 $openbutton->configure(-command => [\&select_openfile,
597 $self, $q->{'name'}, $openbutton]);
598 $self->put_it($openbutton);
599 $self->set_default($q, $def);
600 }
601  
602 sub select_savefile {
603 my ($self, $name, $data, $datafn, $qw, $q, $p, $widget) = @_;
604 my $file = $self->{'qtable'}->getSaveFile();
605 $widget->configure(-text => 'Select File: ' . $file);
606 my $fileh = new IO::File;
607 $fileh->open('>' . $file);
608  
609 # save the question data field
610 if ($data) {
611 print $fileh $data;
612 }
613  
614 # call the datafn routine as well
615 if ($datafn && ref($datafn) eq 'CODE') {
616 $datafn->($fileh, $file, $qw, $q, $p);
617 }
618  
619 # close the output file
620 $fileh->close();
621  
622 $self->qwparam($name, $file) if ($file ne '');
623 }
624  
625 sub do_filedownload {
626 my ($self, $q, $wiz, $p, $def) = @_;
627  
628 my $openbutton =
629 $self->{'qtable'}->Button(-text => 'Select File...');
630 $openbutton->configure(-command => [\&select_savefile,
631 $self, $q->{'name'},
632 $q->{'data'}, $q->{'datafn'},
633 $wiz, $q, $p, $openbutton]);
634 $self->put_it($openbutton);
635 $self->set_default($q, $def);
636 }
637  
638 sub do_entry {
639 my ($self, $q, $wiz, $p, $name, $def, $hide) = @_;
640 $self->{'datastore'}->set($q->{'name'}, $def);
641  
642 #
643 # Set up a value to use if the text shouldn't be echoed to the screen.
644 #
645 my $hideval;
646 if ($hide) {
647 $hideval = '*';
648 }
649  
650 $self->put_it($self->{'qtable'}->Entry(-textvariable => \$self->{'datastore'}{'vars'}{$name}, -show => $hideval, @{$self->get_extra_args($q, $wiz, $p)}));
651 $self->set_default($q, $def);
652 }
653  
654 sub do_textbox {
655 my ($self, $q, $wiz, $p, $vals, $def) = @_;
656 my ($self, $q, $wiz, $p, $name, $def, $size, $width, $height) = @_;
657 my $tb =
658 $self->{'qtable'}->Text(-width => ($size || $width || 80),
659 -height => ($height || 8),
660 -wrap => 'none',
661 # -relief => 'flat',
662 @{$self->get_extra_args($q, $wiz, $p)});
663  
664 $tb->bind('<Any-Leave>',
665 sub { $self->{'datastore'}{'vars'}{$name} =
666 $_[0]->get('0.0','end'); });
667 $tb->insert('end',$def || "",'geoqotext');
668 $self->set_default($q, $def);
669 $self->put_it($tb);
670 }
671  
672 sub do_separator {
673 my ($self, $q, $wiz, $p, $text) = @_;
674 my $where = $self->{'qf'};
675 $self->{'currentq'}++;
676 $self->{'qadd'}++;
677 if (!$where) {
678 $where = $self->{'top'}->Frame();
679 $where->pack(-expand => 1, -fill => 'x');
680 }
681 $self->put_it($text);
682 }
683  
684 ##################################################
685 # Display
686 ##################################################
687  
688 sub do_a_table {
689 my ($self, $table, $parentt, $rowc, $wiz, $q, $p) = @_;
690  
691 foreach my $row (@$table) {
692 my $col = 0;
693 $rowc++;
694 foreach my $column (@$row) {
695 if (ref($column) eq "ARRAY") {
696 # sub table
697 my $newt = $parentt->Table(-rows => 200,
698 -columns => 100,
699 -scrollbars => '');
700 $self->do_a_table($column, $newt, -1, $wiz, $q, $p);
701 $parentt->put($rowc, $col++, $newt);
702 } elsif (ref($column) eq "HASH") {
703 my $oldqt = $self->{'qtable'};
704 $self->{'qtable'} = $parentt;
705  
706 my $oldq = $self->{'currentq'};
707  
708 my $oldrow = $self->{'currentrow'};
709 $self->{'currentrow'} = $rowc;
710  
711 my $oldc = $self->{'currentcol'};
712 $self->{'currentcol'} = $col;
713 $col++;
714
715 my $subname = $wiz->ask_question($p, $column);
716 push @{$wiz->{'passvars'}}, $subname if ($subname);
717  
718 $self->{'qtable'} = $oldqt;
719 $self->{'currentq'} = $oldq;
720 if ($oldc) {
721 $self->{'currentcol'} = $oldc;
722 } else {
723 delete $self->{'currentcol'};
724 }
725 if ($oldrow) {
726 $self->{'currentrow'} = $oldrow;
727 } else {
728 delete $self->{'currentrow'};
729 }
730  
731 } else {
732 $parentt->put($rowc, $col++,
733 $parentt->Label(-text =>
734 $self->make_displayable($column),
735 -anchor => 'w'));
736 }
737 }
738 }
739 }
740  
741 sub do_table {
742 my ($self, $q, $wiz, $p, $table, $headers) = @_;
743  
744 my $fixed = ($headers) ? 1 : 0;
745 my $f = $self->{'qtable'}->Frame(-relief => 'raised', -border => 3);
746 my $tab = $f->Table(-rows => 1000, #($#$table + $fixed + 1),
747 -columns => 1000, #($#{$table->[0]} + 1),
748 -fixedrows => $fixed,
749 -scrollbars => '');
750 $tab->pack();
751 if ($headers) {
752 my $col = 0;
753 foreach my $column (@$headers) {
754 $tab->put(0, $col++, $tab->Label(-text => $column,
755 -relief => 'raised',
756 -anchor => 'w',
757 -border => 3));
758 }
759 }
760  
761 $self->do_a_table($table, $tab, $fixed-1, $wiz, $q, $p);
762  
763 $self->put_it($f);
764 }
765  
766 sub do_graph {
767 my $self = shift;
768 my ($q, $wiz, $p, $data, $gopts) = @_;
769  
770 if ($have_gd_graph) {
771 require MIME::Base64;
772 # grrr... photo requires data to be in base64 or a file. Why???
773 my $photo = $self->{'qtable'}->Photo(
774 -data =>
775 MIME::Base64::encode_base64(
776 $self->do_graph_data(@_)
777 ),
778 );
779 $self->put_it($self->{'qtable'}->Label(-image => $photo,
780 -anchor => 'w'));
781 } else {
782 $self->put_it("Graphing support not available.");
783 }
784 }
785  
786 ##############################################
787 #
788 sub do_image {
789 my $self = shift;
790 my ($q, $wiz, $p, $datastr, $filestr, $imgalt) = @_;
791  
792 my $ph;
793 if ($have_tk_png) {
794 if ($datastr) {
795 require MIME::Base64;
796 $ph = $self->{'qtable'}->Photo(
797 -format => 'png',
798 -data =>
799 MIME::Base64::encode_base64($datastr));
800  
801 } else {
802 # image file
803 $ph = $self->{'qtable'}->Photo(-format => 'png',
804 -file => $wiz->{'generator'}{'imagebase'} . $filestr);
805 }
806 }
807 if ($ph) {
808 $self->put_it($self->{'qtable'}->Label(-image => $ph,
809 -anchor => 'w'));
810 } else {
811 $self->put_it($self->{'qtable'}->Label(-text => $imgalt || "Broken Image"));
812 }
813 }
814  
815 ##################################################
816 # Trees
817 ##################################################
818  
819 sub do_tree {
820 my ($self, $q, $wiz, $p, $labels) = @_;
821  
822 if (!$have_tk_tree) {
823 print STDERR "Tree support not available. Install the Tk::Tree perl module\n";
824 }
825  
826 my $top = $self->{'qtable'} || $self->{'top'};
827 my $tree = $self->{'qtable'}->ScrlTree(-width => 40, #size that looked good to me
828 -height => 14,
829 -scrollbars => 'osoe');
830  
831 my @expand;
832 if ($q->{'default'}) {
833 #ensure that the default is initially visible
834 my $cur = $q->{'default'};
835 until ($cur eq $q->{'root'}) {
836 $cur = get_name($q->{'parent'}->($wiz, $cur));
837 unshift @expand, $cur;
838 }
839 $self->{'datastore'}->set($q->{'name'},$q->{'default'}) if $q->{'name'};
840 }
841  
842 add_node($wiz, $tree, $q->{'root'}, $q, "", $labels, @expand);
843  
844 $tree->configure( -opencmd => sub { my $branch = shift;
845 open_branch($wiz, $tree, $branch,
846 $q, $labels) } );
847 $tree->configure( -browsecmd => sub { if ($q->{'name'}) {
848 my @sel = $tree->infoSelection();
849 my $node = ($#sel > -1 ?
850 $tree->infoData($sel[0]) : "");
851 $self->{'datastore'}->set($q->{'name'}, $node);
852 } } );
853  
854 $self->put_it($tree);
855 }
856  
857 sub get_name {
858 my $node = shift;
859  
860 if (ref($node) eq 'HASH') {
861 return $node->{'name'};
862 } else {
863 return $node;
864 }
865 }
866  
867 sub add_node {
868 my ($wiz, $tree, $node, $q, $parent, $labels, @expand) = @_;
869  
870 my $label;
871 my $exp = shift @expand;
872 my $name = get_name($node);
873 if (ref($node) eq 'HASH') {
874 $label = $node->{'label'};
875 }
876 $label = $label || $labels->{$name} || $name;
877  
878 #text of the node is the label. data is the identifier.
879 my $child = $tree->addchild($parent, -text => $label,
880 -data => $name);
881 my $ans = $q->{'children'}->($wiz, $node);
882 $tree->setmode($child, ($ans && $#$ans > -1) ? 'open' : 'none');
883  
884 $tree->selectionSet($child) if ($name eq $q->{'default'});
885 if ($name eq $exp) {
886 $tree->open($child);
887 open_branch($wiz, $tree, $child, $q, $labels, @expand);
888 }
889 }
890  
891 sub open_branch {
892 my ($wiz, $tree, $branch, $q, $labels, @expand) = @_;
893  
894 if (my @children = $tree->infoChildren($branch)) {
895 #we've already opened this branch, so just reopen it
896 foreach my $child (@children) {
897 $tree->show( -entry => $child);
898 }
899 return;
900 }
901  
902 my $children = $q->{'children'}->($wiz, $tree->infoData($branch));
903 return if (!$children || $#$children == -1);
904 foreach my $child (@$children) {
905 add_node($wiz, $tree, $child, $q, $branch, $labels, @expand);
906 }
907 }
908  
909 ##################################################
910 #
911 # Automatic updating for monitors.
912 #
913  
914 sub do_autoupd
915 {
916 #
917 # Dummy routine for now!
918 #
919 warn "Tk.do_autoupd: currently no automatic updating is defined for Tk. This should be fixed RSN.\n"
920 }
921  
922 ##################################################
923 # unknown type errors
924 #
925 sub do_unknown {
926 my ($self, $q, $wiz, $p) = @_;
927 $self->{'currentq'}++;
928 $self->{'qadd'}++;
929 $self->put_it($self->{'qtable'}->Label(-text => "Unknown question type '$q->{type}' not handled in primary '$p->{module_name}'. It is highly likely this application will no longer function properly beyond this point.",
930 -foreground => 'red'));
931 }
932  
933  
934 ##################################################
935 # action confirm
936 ##################################################
937  
938 sub start_confirm {
939 my ($self, $wiz) = @_;
940  
941 $self->make_top();
942 $self->put_it('Wrapping up.',1,1);
943 $self->put_it('Do you want to commit the following changes:',2,1);
944 $self->{'resultf'} = $self->{'qtable'}->Frame(-relief => 'sunken',
945 -border => 3);
946 $self->put_it($self->{'resultf'},3,1);
947 }
948  
949 sub end_confirm {
950 my ($self, $wiz) = @_;
951 # this will be deleted by the cancel button if they press it.
952 $self->do_hidden($wiz, 'wiz_confirmed', 'Commit');
953 $self->do_ok_cancel("Commit", $wiz);
954 $self->our_mainloop();
955 return 1;
956 }
957  
958 sub do_confirm_message {
959 my ($self, $wiz, $msg) = @_;
960 $self->{'resultf'}->Label(-justify => 'left', -text => $msg, -anchor => 'w')
961 ->pack(-expand => 1, -fill => 'x');
962 }
963  
964 sub canceled_confirm {
965 my ($self, $wiz) = @_;
966 goto_top();
967 }
968  
969 ##################################################
970 # actions
971 ##################################################
972  
973 sub start_actions {
974 my ($self, $wiz) = @_;
975 $self->make_top();
976 $self->put_it('Processing your request...',1,1);
977 $self->{'resultf'} = $self->{'qtable'}->Frame(-relief => 'sunken',
978 -border => 3);
979 $self->put_it($self->{'resultf'},2,1);
980 }
981  
982 sub end_actions {
983 my ($self, $wiz) = @_;
984 $self->put_it('Done',3,1);
985 $self->do_ok_cancel("Finish", $wiz);
986 $self->clear_params();
987 $self->our_mainloop();
988 return 1;
989 }
990  
991 sub do_action_output {
992 my ($self, $wiz, $action) = @_;
993 $self->{'resultf'}->Label(-text => $action, -anchor => 'w')->pack(-expand => 1, -fill => 'x');
994 }
995  
996 sub do_action_error {
997 my ($self, $wiz, $errstr) = @_;
998 $self->{'resultf'}->Label(-text => $errstr, -foreground => 'red',
999 -anchor => 'w')
1000 ->pack(-expand => 1, -fill => 'x');
1001 }
1002  
1003 1;