Pod.pm untested

filter: Statementstmt 0.0% 0 / 518 Branchbran 0.0% 0 / 126 Conditioncond 0.0% 0 / 68 MC/DCmcdc 0.0% 0 / 41 Subroutinesub 0.0% 0 / 48 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 826 CC 64 SCAR 51.5
CC
64
Cov
0%
Populate
650.0
SearchFAQ
110.0
openpod
90.0
CRAP 173.0
1 package Tk::Pod;
2 use strict;
3 use Tk ();
4 use Tk::Toplevel;
5  
6 use vars qw($VERSION $DIST_VERSION @ISA);
7 $VERSION = '5.41';
8 $DIST_VERSION = '0.9943';
9  
10 @ISA = qw(Tk::Toplevel);
11  
12 Construct Tk::Widget 'Pod';
13  
14 my $openpod_history;
15 my $searchfaq_history;
16  
17 sub Pod_Text_Widget { "PodText" }
18 sub Pod_Text_Module { "Tk::Pod::Text" }
19  
20 sub Pod_Tree_Widget { "PodTree" }
21 sub Pod_Tree_Module { "Tk::Pod::Tree" }
22  
23 sub Populate
24 {
25 my ($w,$args) = @_;
26  
27 if ($w->Pod_Text_Module)
28 {
29 eval q{ require } . $w->Pod_Text_Module;
30 die $@ if $@;
31 }
32 if ($w->Pod_Tree_Module)
33 {
34 eval q{ require } . $w->Pod_Tree_Module;
35 die $@ if $@;
36 }
37  
38 $w->SUPER::Populate($args);
39  
40 my $tree = $w->Scrolled($w->Pod_Tree_Widget,
41 -scrollbars => 'oso'.($Tk::platform eq 'MSWin32'?'e':'w')
42 );
43 $w->Advertise('tree' => $tree);
44  
45 my $searchcase = 0;
46 my $p = $w->Component($w->Pod_Text_Widget => 'pod', -searchcase => $searchcase)->pack(-expand => 1, -fill => 'both');
47  
48 my $exitbutton = delete $args->{-exitbutton} || 0;
49  
50 # Experimental menu compound images:
51 # XXX Maybe there should be a way to turn this off, as the extra
52 # icons might be memory consuming...
53 my $compound = sub { ($_[0]) };
54 if ($Tk::VERSION >= 800 && eval { require Tk::ToolBar; 1 }) {
55 $w->ToolBar->destroy; # hack to load images
56 if (!$Tk::Pod::empty_image_16) { # XXX multiple MainWindows?
57 $Tk::Pod::empty_image_16 = $w->MainWindow->Photo(-data => <<EOF);
58 R0lGODlhEAAQAIAAAP///////yH+FUNyZWF0ZWQgd2l0aCBUaGUgR0lNUAAh+QQBCgABACwA
59 AAAAEAAQAAACDoyPqcvtD6OctNqLsz4FADs=
60 EOF
61 }
62 if ($Tk::VERSION >= 804) {
63 # Tk804 has native menu item compounds
64 $compound = sub {
65 my($text, $image) = @_;
66 if ($image) {
67 ($text, -image => $image . "16", -compound => "left");
68 } else {
69 ($text, -image => $Tk::Pod::empty_image_16, -compound => "left");
70 }
71 };
72 } elsif (eval { require Tk::Compound; 1 }) {
73 # For Tk800 we have to create our own compounds using Tk::Compund
74 # get the default font (taken from bbbike):
75 my $std_font = $w->optionGet('font', 'Font');
76 if (!defined $std_font || $std_font eq '') {
77 my $l = $w->Label;
78 $std_font = $l->cget(-font);
79 $l->destroy;
80 }
81 my %std_font = $w->fontActual($std_font);
82 # create an underlined font which matches the default font
83 my $underline_font = join(" ", map { "{" . $std_font{$_} . "}" } qw(-family -size -weight -slant));
84 $underline_font .= " overstrike" if $std_font{-overstrike};
85 $underline_font .= " underline";
86 $compound = sub {
87 my($text, $image) = @_;
88 my $c = $w->MainWindow->Compound; # XXX multiple MainWindows?
89 if ($image) {
90 $c->Image(-image => $image."16");
91 } else {
92 $c->Image(-image => $Tk::Pod::empty_image_16);
93 }
94 $c->Space(-width => 4);
95 my($text_before, $underlined_text, $text_after) = $text =~ /^(.*)~(.)(.*)/;
96 if (defined $underlined_text) {
97 $c->Text(-text => $text_before) if $text_before ne "";
98 $c->Text(-text => $underlined_text, -font => $underline_font);
99 $c->Text(-text => $text_after) if $text_after ne "";
100 } else {
101 $c->Text(-text => $text);
102 }
103 ($text, -image => $c);
104 };
105 }
106 }
107  
108 my $menuitems =
109 [
110  
111 [Cascade => '~File', -menuitems =>
112 [
113 [Button => $compound->('~Open File...', "fileopen"),
114 '-accelerator' => 'F3',
115 '-command' => ['openfile',$w],
116 ],
117 [Button => $compound->('Open ~by Name...'),
118 '-accelerator' => 'Ctrl+O',
119 '-command' => ['openpod',$w,$p],
120 ],
121 [Button => $compound->('~New Window...'),
122 '-accelerator' => 'Ctrl+N',
123 '-command' => ['newwindow',$w,$p],
124 ],
125 [Button => $compound->('~Edit', "edit"),
126 '-command' => ['edit',$p],
127 ],
128 [Button => $compound->('Edit with p~tked'),
129 '-command' => ['edit',$p,'ptked'],
130 ],
131 [Button => $compound->('~Print'. ($p->PrintHasDialog ? '...' : ''), "fileprint"),
132 '-accelerator' => 'Ctrl+P',
133 '-command' => ['Print',$p],
134 ],
135 [Separator => ""],
136 [Button => $compound->('~Close', "fileclose"),
137 '-accelerator' => 'Ctrl+W',
138 '-command' => ['quit',$w],
139 ],
140 ($exitbutton
141 ? [Button => $compound->('E~xit', "actexit"),
142 '-accelerator' => 'Ctrl+Q',
143 '-command' => sub { $p->MainWindow->destroy },
144 ]
145 : ()
146 ),
147 ]
148 ],
149  
150 [Cascade => '~View', -menuitems =>
151 [
152 [Checkbutton => $compound->('Pod ~Tree'),
153 '-variable' => \$w->{Tree_on},
154 '-command' => sub { $w->tree($w->{Tree_on}) },
155 ],
156 '-',
157 [Button => $compound->("Zoom ~in", "viewmag+"),
158 '-accelerator' => 'Ctrl++',
159 '-command' => [$w, 'zoom_in'],
160 ],
161 [Button => $compound->("~Normal"),
162 '-command' => [$w, 'zoom_normal'],
163 ],
164 [Button => $compound->("Zoom ~out", "viewmag-"),
165 '-accelerator' => 'Ctrl+-',
166 '-command' => [$w, 'zoom_out'],
167 ],
168 '-',
169 [Button => $compound->('~Reload', "actreload"),
170 '-accelerator' => 'Ctrl+R',
171 '-command' => ['reload',$p],
172 ],
173 [Button => $compound->("~View source"),
174 '-accelerator' => 'Ctrl+U',
175 '-command' => ['view_source',$p],
176 ],
177 '-',
178 [Button => $compound->('Pod on ~search.cpan.org'),
179 '-command' => sub {
180 require Tk::Pod::Util;
181 my $url = $p->{pod_title};
182 eval {
183 require URI::Escape;
184 $url = URI::Escape::uri_escape($url);
185 };
186 Tk::Pod::Util::start_browser("http://search.cpan.org/perldoc?" . $url);
187 },
188 ],
189 [Button => $compound->('Pod on ~metacpan.org'),
190 '-command' => sub {
191 require Tk::Pod::Util;
192 my $url = $p->{pod_title};
193 eval {
194 require URI::Escape;
195 $url = URI::Escape::uri_escape($url);
196 };
197 Tk::Pod::Util::start_browser("https://metacpan.org/module/" . $url);
198 },
199 ],
200 [Button => $compound->('Pod on ~annocpan.org'),
201 '-command' => sub {
202 require Tk::Pod::Util;
203 my $url = $p->{pod_title};
204 eval {
205 require URI::Escape;
206 $url = URI::Escape::uri_escape($url);
207 };
208 ## It seems that the search works better than the direct link on annocpan.org...
209 Tk::Pod::Util::start_browser("http://www.annocpan.org/?mode=search&field=Module&name=$url");
210 #Tk::Pod::Util::start_browser("http://www.annocpan.org/perldoc?" . $url);
211 },
212 ],
213 ]
214 ],
215  
216 [Cascade => '~Search', -menuitems =>
217 [
218 [Button => $compound->('~Search', "viewmag"),
219 '-accelerator' => '/',
220 '-command' => ['Search', $p, 'Next'],
221 ],
222 [Button => $compound->('Search ~backwards'),
223 '-accelerator' => '?',
224 '-command' => ['Search', $p, 'Prev'],
225 ],
226 [Button => $compound->('~Repeat search'),
227 '-accelerator' => 'n',
228 '-command' => ['ShowMatch', $p, 'Next'],
229 ],
230 [Button => $compound->('R~epeat backwards'),
231 '-accelerator' => 'N',
232 '-command' => ['ShowMatch', $p, 'Prev'],
233 ],
234 [Checkbutton => $compound->('~Case sensitive'),
235 '-variable' => \$searchcase,
236 '-command' => sub { $p->configure(-searchcase => $searchcase) },
237 ],
238 [Separator => ""],
239 [Button => $compound->('Search ~full text', "filefind"),
240 '-command' => ['SearchFullText', $p],
241 ],
242 [Button => $compound->('Search FA~Q'),
243 '-command' => ['SearchFAQ', $w, $p],
244 ],
245 ]
246 ],
247  
248 [Cascade => 'H~istory', -menuitems =>
249 [
250 [Button => $compound->('~Back', "navback"),
251 '-accelerator' => 'Alt-Left',
252 '-command' => ['history_move', $p, -1],
253 ],
254 [Button => $compound->('~Forward', "navforward"),
255 '-accelerator' => 'Alt-Right',
256 '-command' => ['history_move', $p, +1],
257 ],
258 [Button => $compound->('~View'),
259 '-command' => ['history_view', $p],
260 ],
261 '-',
262 [Button => $compound->('Clear cache'),
263 '-command' => ['clear_cache', $p],
264 ],
265 ]
266 ],
267  
268 [Cascade => '~Help', -menuitems =>
269 [
270 # XXX restructure to not reference to tkpod
271 [Button => '~Usage...', -command => ['help', $w]],
272 [Button => '~Programming...', -command => ['help_programming', $w]],
273 [Button => '~About...', -command => ['about', $w]],
274 ($ENV{'TKPODDEBUG'}
275 ? ('-',
276 [Button => 'WidgetDump', -command => sub { $w->WidgetDump }],
277 [Button => 'Ptksh', -command => sub {
278 # Code taken from bbbike
279 # Is there already a (withdrawn) ptksh?
280 foreach my $mw0 (Tk::MainWindow::Existing()) {
281 if ($mw0->title =~ /^ptksh/) {
282 $mw0->deiconify;
283 $mw0->raise;
284 return;
285 }
286 }
287  
288 require Config;
289 my $perldir = $Config::Config{'scriptdir'};
290 require "$perldir/ptksh";
291  
292 # Code taken from bbbike and slightly modified
293 foreach my $mw0 (Tk::MainWindow::Existing()) {
294 if ($mw0->title eq 'ptksh') {
295 $mw0->protocol('WM_DELETE_WINDOW' => [$mw0, 'withdraw']);
296 }
297 }
298 }],
299 [Button => 'Reloader', -command => sub {
300 if (eval { require Module::Refresh; 1 }) {
301 Module::Refresh->refresh;
302 $w->messageBox(-title => "Reloader",
303 -icon => "info",
304 -message => "Modules were reloaded.",
305 );
306 } else {
307 $w->messageBox(-title => "Reloader",
308 -icon => "error",
309 -message => "To use this functionality you have to install Module::Refresh from CPAN",
310 );
311 # So we have a chance to try it again...
312 delete $INC{"Module/Refresh.pm"};
313 }
314 }],
315 )
316 : ()
317 ),
318 ]
319 ]
320 ];
321  
322 my $mbar = $w->Menu(-menuitems => $menuitems);
323 $w->configure(-menu => $mbar);
324 $w->Advertise(menubar => $mbar);
325  
326 $w->Delegates('Menubar' => $mbar);
327 $w->ConfigSpecs(
328 -tree => ['METHOD', 'tree', 'Tree', 0],
329 -exitbutton => ['PASSIVE', 'exitButton', 'ExitButton', $exitbutton],
330 -background => ['PASSIVE'], # XXX see comment in Tk::More
331 -cursor => ['CHILDREN'],
332 'DEFAULT' => [$p],
333 );
334  
335 {
336 my $path = $w->toplevel->PathName;
337  
338 # This is somewhat hackish: to make sure that the Tk::Pod bindings
339 # win over the embedded Tk::More/Tk::Text bindings, the bindtags of
340 # all child widgets are re-shuffled, so the Tk::Pod bindings come
341 # first. Additionally, all the Tk::Pod bindings need additionally a
342 # Tk->break call, so no other binding of embedded widgets is fired.
343 $p->Walk(sub {
344 my $w = shift;
345 my @bindtags = $w->bindtags;
346 if (grep { $_ eq $path } @bindtags)
347 {
348 $w->bindtags([$path, grep { $_ ne $path } @bindtags]);
349 }
350 });
351  
352 foreach my $mod (qw(Alt Meta))
353 {
354 $w->bind($path, "<$mod-Left>" => sub { $p->history_move(-1); Tk->break });
355 $w->bind($path, "<$mod-Right>" => sub { $p->history_move(+1); Tk->break });
356 }
357  
358 $w->bind($path, "<Control-minus>" => sub { $w->zoom_out; Tk->break });
359 $w->bind($path, "<Control-plus>" => sub { $w->zoom_in; Tk->break });
360 $w->bind($path, "<F3>" => sub { $w->openfile; Tk->break });
361 $w->bind($path, "<Control-o>" => sub { $w->openpod($p); Tk->break });
362 $w->bind($path, "<Control-n>" => sub { $w->newwindow($p); Tk->break });
363 $w->bind($path, "<Control-r>" => sub { $p->reload; Tk->break });
364 $w->bind($path, "<Control-p>" => sub { $p->Print; Tk->break });
365 $w->bind($path, "<Print>" => sub { $p->Print; Tk->break });
366 $w->bind($path, "<Control-u>" => sub { $p->view_source; Tk->break });
367 $w->bind($path, "<Control-w>" => sub { $w->quit; Tk->break });
368 $w->bind($path, "<Control-q>" => sub { $p->MainWindow->destroy; Tk->break })
369 if $exitbutton;
370 }
371  
372 $w->protocol('WM_DELETE_WINDOW',['quit',$w]);
373 }
374  
375 my $fsbox;
376  
377 sub openfile {
378 my ($cw,$p) = @_;
379 my $file;
380 if ($cw->can("getOpenFile")) {
381 $file = $cw->getOpenFile
382 (-title => "Choose Pod file",
383 -filetypes => [['Pod containing files', ['*.pod',
384 '*.pl',
385 '*.pm']],
386 ['Pod files', '*.pod'],
387 ['Perl scripts', '*.pl'],
388 ['Perl modules', '*.pm'],
389 ['All files', '*']]);
390 } else {
391 unless (defined $fsbox && $fsbox->IsWidget) {
392 require Tk::FileSelect;
393 $fsbox = $cw->FileSelect();
394 }
395 $file = $fsbox->Show();
396 }
397 $cw->configure(-file => $file) if defined $file && -r $file;
398 }
399  
400 sub openpod {
401 my($cw,$p) = @_;
402 my $t = $cw->Toplevel(-title => "Open Pod by Name");
403 $t->transient($cw);
404 $t->grab;
405 my($pod, $e, $go);
406 {
407 my $Entry = 'Entry';
408 eval {
409 require Tk::HistEntry;
410 Tk::HistEntry->VERSION(0.40);
411 $Entry = "HistEntry";
412 };
413  
414 my $f = $t->Frame->pack(-fill => "x");
415 $f->Label(-text => "Pod:")->pack(-side => "left");
416 $e = $f->$Entry(-textvariable => \$pod)->pack(-side => "left", -fill => "x", -expand => 1);
417 if ($e->can('history') && $openpod_history) {
418 $e->history($openpod_history);
419 }
420 $e->focus;
421 $go = 0;
422 $e->bind("<Return>" => sub { $go = 1 });
423 $e->bind("<Escape>" => sub { $go = -1 });
424 }
425  
426 {
427 my $f = $t->Frame->pack;
428 Tk::grid($f->Label(-text => "Use 'Module::Name' for module documentation"), -sticky => "w");
429 Tk::grid($f->Label(-text => "Use '-f function' for function documentation"), -sticky => "w");
430 Tk::grid($f->Label(-text => "Use '-q terms' for FAQ entries"), -sticky => "w");
431 }
432  
433 {
434 my $f = $t->Frame->pack;
435 $f->Button(-text => "OK",
436 -command => sub { $go = 1 })->pack(-side => "left");
437 $f->Button(-text => "New window",
438 -command => sub { $go = 2 })->pack(-side => "left");
439 $f->Button(-text => "Cancel",
440 -command => sub { $go = -1 })->pack(-side => "left");
441 }
442 $t->Popup(-popover => $cw);
443 $t->OnDestroy(sub { $go = -1 unless $go });
444 $t->waitVariable(\$go);
445 if (Tk::Exists($t)) {
446 if (defined $pod && $pod ne "" && $go > 0 && $e->can('historyAdd')) {
447 $e->historyAdd($pod);
448 $openpod_history = [ $e->history ];
449 }
450 $t->grabRelease;
451 $t->destroy;
452 }
453  
454 my %pod_args;
455 if (defined $pod && $pod =~ /^(-[fq])\s+(.+)/) {
456 my $switch = $1;
457 my $func = $2;
458 %pod_args = $cw->getpodargs($switch, $func);
459 } else {
460 %pod_args = $cw->getpodargs($pod);
461 }
462  
463 if (defined $pod && $pod ne "") {
464 if ($go == 1) {
465 $cw->configure(%pod_args);
466 } elsif ($go == 2) {
467 my $new_cw = $cw->clone(%pod_args);
468 }
469 }
470 }
471  
472 sub getpodargs {
473 my($cw, @args) = @_;
474 my @pod_args;
475 if (@args == 1) {
476 @pod_args = ('-file' => $args[0]);
477 } elsif (@args == 2 && $args[0] =~ /^-([fq])$/) {
478 my $switch = $1;
479 my $func = $args[1];
480 my $func_pod = "";
481 open(FUNCPOD, "-|") or do {
482 exec "perldoc", "-u", "-$switch", $func;
483 warn "Can't execute perldoc: $!";
484 CORE::exit(1);
485 };
486 local $/ = undef;
487 $func_pod = join "", <FUNCPOD>;
488 close FUNCPOD;
489 if ($func_pod ne "") {
490 push @pod_args, '-text' => $func_pod;
491 if ($switch eq "f") {
492 push @pod_args, '-title' => "Function $func";
493 } else {
494 push @pod_args, '-title' => "FAQ $func";
495 }
496 }
497 }
498 @pod_args;
499 }
500  
501 sub newwindow {
502 shift->clone;
503 }
504  
505 sub Dir {
506 require Tk::Pod::Text;
507 require Tk::Pod::Tree;
508 Tk::Pod::Text::Dir(@_);
509 Tk::Pod::Tree::Dir(@_);
510 }
511  
512  
513 sub quit { shift->destroy }
514  
515 sub help {
516 my $w = shift;
517 $w->clone('-tree' => 0,
518 '-file' => 'Tk::Pod_usage.pod',
519 );
520 }
521  
522 sub help_programming {
523 my $w = shift;
524 $w->clone('-tree' => 0,
525 '-file' => 'Tk/Pod.pm',
526 );
527 }
528  
529 sub about {
530 my $w = shift;
531 require Tk::DialogBox;
532 require Tk::ROText;
533 my $d = $w->DialogBox(-title => "About Tk::Pod",
534 -buttons => ["OK"],
535 );
536 my $message = <<EOF;
537 Tk::Pod - a Pod viewer written in Perl/Tk
538  
539 Version information:
540 Tk-Pod distribution $DIST_VERSION
541 Tk::Pod module $VERSION
542  
543 System information:
544 @{[ $Pod::Simple::VERSION ? "Pod::Simple $Pod::Simple::VERSION\n"
545 : ""
546 ]} Tk $Tk::VERSION
547 Perl $]
548 OS $^O
549  
550 Please contact <srezic\@cpan.org> in case of problems.
551 Send the contents of this window for diagnostics.
552  
553 EOF
554 my @lines = split /\n/, $message, -1;
555 my $width = 0;
556 for (@lines) {
557 $width = length $_ if length $_ > $width;
558 }
559 my $txt = $d->add("Scrolled", "ROText",
560 -height => scalar @lines,
561 -width => $width + 1,
562 -relief => "flat",
563 -scrollbars => "oe",
564 )->pack(-expand => 1, -fill => "both");
565 $txt->insert("end", $message);
566 $d->Show;
567 }
568  
569 sub add_section_menu {
570 my($pod) = @_;
571  
572 my $screenheight = $pod->screenheight;
573 my $mbar = $pod->Subwidget('menubar');
574 my $sectionmenu = $mbar->Subwidget('sectionmenu');
575 if (defined $sectionmenu) {
576 $sectionmenu->delete(0, 'end');
577 } else {
578 $mbar->insert($mbar->index("last"), "cascade",
579 '-label' => 'Section', -underline => 1);
580 $sectionmenu = $mbar->Menu;
581 $mbar->entryconfigure($mbar->index("last")-1, -menu => $sectionmenu);
582 $mbar->Advertise(sectionmenu => $sectionmenu);
583 }
584  
585 my $podtext = $pod->Subwidget('pod');
586 my $text = $podtext->Subwidget('more')->Subwidget('text');
587  
588 $text->tag('configure', '_section_mark',
589 -background => 'red',
590 -foreground => 'black',
591 );
592  
593 my $sdef;
594 foreach $sdef (@{$podtext->{'sections'}}) {
595 my($head_level, $subject, $pos) = @$sdef;
596  
597 my @args;
598 if ($sectionmenu &&
599 $sectionmenu->yposition("last") > $screenheight-40) {
600 push @args, -columnbreak => 1;
601 }
602  
603 $sectionmenu->command
604 (-label => (" " x ($head_level-1)) . $subject,
605 -command => sub {
606 my($line) = split(/\./, $pos);
607 $text->tag('remove', '_section_mark', qw/0.0 end/);
608 $text->tag('add', '_section_mark',
609 $line-1 . ".0",
610 $line-1 . ".0 lineend");
611 $text->yview("_section_mark.first");
612 $text->after(500, [$text, qw/tag remove _section_mark 0.0 end/]);
613 },
614 @args,
615 );
616 }
617 }
618  
619 sub tree {
620 my $w = shift;
621 if (@_) {
622 my $val = shift;
623 $w->{Tree_on} = $val;
624 my $tree = $w->Subwidget('tree');
625 my $p = $w->Subwidget("pod");
626 if ($val) {
627 $p->packForget;
628 $tree->packAdjust(-side => 'left', -fill => 'y');
629 $p->pack(-side => "left", -expand => 1, -fill => 'both');
630 if (!$tree->Filled) {
631 $w->_configure_tree;
632 $w->Busy(-recurse => 1);
633 eval {
634 $tree->Fill(-fillcb => sub {
635 $tree->SeePath("file:" . $p->cget(-path)) if $p->cget(-path);
636 });
637 };
638 my $err = $@;
639 $w->Unbusy;
640 if ($err) {
641 die $err;
642 }
643 }
644 } else {
645 if ($tree && $tree->manager) {
646 $tree->packForget;
647 $p->packForget;
648 eval {
649 $w->Walk
650 (sub {
651 my $w = shift;
652 if ($w->isa('Tk::Adjuster') &&
653 $w->cget(-widget) eq $tree) {
654 $w->destroy;
655 die;
656 }
657 });
658 };
659 $p->pack(-side => "left", -expand => 1, -fill => 'both');
660 }
661 }
662 }
663 $w->{Tree_on};
664 }
665  
666 sub _configure_tree {
667 my($w) = @_;
668 my $tree = $w->Subwidget("tree");
669 my $p = $w->Subwidget("pod");
670  
671 my $common_showcommand = sub {
672 my($e) = @_;
673 my $uri = $e->uri;
674 my $type = $e->type;
675 if (defined $type && $type eq 'func') {
676 my $text = $Tk::Pod::Tree::FindPods->function_pod($e->name);
677 (-text => $text, -title => $e->name);
678 } elsif (defined $uri && $uri =~ /^file:(.*)/) {
679 (-file => $1);
680 } else {
681 # ignore
682 }
683 };
684  
685 $tree->configure
686 (-showcommand => sub {
687 my $e = $_[1];
688 my %args = $common_showcommand->($e);
689 my $title = delete $args{-title};
690 $p->configure(-title => $title) if defined $title;
691 $p->configure(%args);
692 },
693 -showcommand2 => sub {
694 my $e = $_[1];
695 my @args = $common_showcommand->($e);
696 # XXX -title?
697 $w->clone(-tree => !!$tree,
698 @args);
699 },
700 );
701 }
702  
703 sub SearchFAQ {
704 my($cw, $p) = @_;
705 my $t = $cw->Toplevel(-title => "Perl FAQ Search");
706 $t->transient($cw);
707 $t->grab;
708 my($keyword, $go, $e);
709 {
710 my $Entry = 'Entry';
711 eval {
712 require Tk::HistEntry;
713 Tk::HistEntry->VERSION(0.40);
714 $Entry = "HistEntry";
715 };
716  
717 my $f = $t->Frame->pack(-fill => "x");
718 $f->Label(-text => "FAQ keyword:")->pack(-side => "left");
719 $e = $f->$Entry(-textvariable => \$keyword)->pack(-side => "left");
720 if ($e->can('history') && $searchfaq_history) {
721 $e->history($searchfaq_history);
722 }
723 $e->focus;
724 $go = 0;
725 $e->bind("<Return>" => sub { $go = 1 });
726 $e->bind("<Escape>" => sub { $go = -1 });
727 }
728 {
729 my $f = $t->Frame->pack;
730 $f->Button(-text => "OK",
731 -command => sub { $go = 1 })->pack(-side => "left");
732 $f->Button(-text => "New window",
733 -command => sub { $go = 2 })->pack(-side => "left");
734 $f->Button(-text => "Cancel",
735 -command => sub { $go = -1 })->pack(-side => "left");
736 }
737 $t->Popup(-popover => $cw);
738 $t->OnDestroy(sub { $go = -1 unless $go });
739 $t->waitVariable(\$go);
740 if (Tk::Exists($t)) {
741 if (defined $keyword && $keyword ne "" && $go > 0 && $e->can('historyAdd')) {
742 $e->historyAdd($keyword);
743 $searchfaq_history = [ $e->history ];
744 }
745 $t->grabRelease;
746 $t->destroy;
747 }
748 if (defined $keyword && $keyword ne "") {
749 if ($go) {
750 require File::Temp;
751 my($fh, $pod) = File::Temp::tempfile(UNLINK => 1,
752 SUFFIX => "_tkpod.pod");
753 my $out = `perldoc -u -q $keyword`; # XXX protect keyword
754 print $fh $out;
755 close $fh;
756  
757 if (-z $pod) {
758 $cw->messageBox(-title => "No FAQ keyword",
759 -icon => "error",
760 -message => "FAQ keyword not found",
761 );
762 } else {
763 if ($go == 1) {
764 $cw->configure(-file => $pod);
765 } elsif ($go == 2) {
766 my $new_cw = $cw->clone('-file' => $pod);
767 }
768 }
769 }
770 }
771 }
772  
773 sub zoom {
774 my($w, $method) = @_;
775 my $p = $w->Subwidget("pod");
776 $p->$method();
777 $w->set_base_font_size($p->base_font_size);
778 }
779  
780 sub zoom_in { shift->zoom("zoom_in") }
781 sub zoom_out { shift->zoom("zoom_out") }
782 sub zoom_normal { shift->zoom("zoom_normal") }
783  
784 sub base_font_size {
785 my $w = shift;
786 $w->{Base_Font_Size};
787 }
788  
789 sub set_base_font_size {
790 my($w, $font_size) = @_;
791 $w->{Base_Font_Size} = $font_size;
792 }
793  
794 sub clone {
795 my($w, %pod_args) = @_;
796 my %pre_args;
797 for ('-tree', '-exitbutton') {
798 if (exists $pod_args{$_}) {
799 $pre_args{$_} = delete $pod_args{$_};
800 } else {
801 $pre_args{$_} = $w->cget($_);
802 }
803 }
804 my $new_w = $w->MainWindow->Pod
805 (%pre_args,
806 '-basefontsize' => $w->base_font_size,
807 );
808 $new_w->configure(%pod_args) if %pod_args;
809 $new_w;
810 }
811  
812 1;
813  
814 __END__