| 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.43'; | ||
| 8 | $DIST_VERSION = '0.9943_52'; | ||
| 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 | for my $event ('<Control-minus>', '<Control-5>') { | ||
| 359 | $w->bind($path, $event => sub { $w->zoom_out; Tk->break }); | ||
| 360 | } | ||
| 361 | for my $event ('<Control-plus>', '<Control-4>') { | ||
| 362 | $w->bind($path, $event => sub { $w->zoom_in; Tk->break }); | ||
| 363 | } | ||
| 364 | $w->bind($path, "<F3>" => sub { $w->openfile; Tk->break }); | ||
| 365 | $w->bind($path, "<Control-o>" => sub { $w->openpod($p); Tk->break }); | ||
| 366 | $w->bind($path, "<Control-n>" => sub { $w->newwindow($p); Tk->break }); | ||
| 367 | $w->bind($path, "<Control-r>" => sub { $p->reload; Tk->break }); | ||
| 368 | $w->bind($path, "<Control-p>" => sub { $p->Print; Tk->break }); | ||
| 369 | $w->bind($path, "<Print>" => sub { $p->Print; Tk->break }); | ||
| 370 | $w->bind($path, "<Control-u>" => sub { $p->view_source; Tk->break }); | ||
| 371 | $w->bind($path, "<Control-w>" => sub { $w->quit; Tk->break }); | ||
| 372 | $w->bind($path, "<Control-q>" => sub { $p->MainWindow->destroy; Tk->break }) | ||
| 373 | if $exitbutton; | ||
| 374 | } | ||
| 375 | |||
| 376 | $w->protocol('WM_DELETE_WINDOW',['quit',$w]); | ||
| 377 | } | ||
| 378 | |||
| 379 | my $fsbox; | ||
| 380 | |||
| 381 | sub openfile { | ||
| 382 | my ($cw,$p) = @_; | ||
| 383 | my $file; | ||
| 384 | if ($cw->can("getOpenFile")) { | ||
| 385 | $file = $cw->getOpenFile | ||
| 386 | (-title => "Choose Pod file", | ||
| 387 | -filetypes => [['Pod containing files', ['*.pod', | ||
| 388 | '*.pl', | ||
| 389 | '*.pm']], | ||
| 390 | ['Pod files', '*.pod'], | ||
| 391 | ['Perl scripts', '*.pl'], | ||
| 392 | ['Perl modules', '*.pm'], | ||
| 393 | ['All files', '*']]); | ||
| 394 | } else { | ||
| 395 | unless (defined $fsbox && $fsbox->IsWidget) { | ||
| 396 | require Tk::FileSelect; | ||
| 397 | $fsbox = $cw->FileSelect(); | ||
| 398 | } | ||
| 399 | $file = $fsbox->Show(); | ||
| 400 | } | ||
| 401 | $cw->configure(-file => $file) if defined $file && -r $file; | ||
| 402 | } | ||
| 403 | |||
| 404 | sub openpod { | ||
| 405 | my($cw,$p) = @_; | ||
| 406 | my $t = $cw->Toplevel(-title => "Open Pod by Name"); | ||
| 407 | $t->transient($cw); | ||
| 408 | $t->grab; | ||
| 409 | my($pod, $e, $go); | ||
| 410 | { | ||
| 411 | my $Entry = 'Entry'; | ||
| 412 | eval { | ||
| 413 | require Tk::HistEntry; | ||
| 414 | Tk::HistEntry->VERSION(0.40); | ||
| 415 | $Entry = "HistEntry"; | ||
| 416 | }; | ||
| 417 | |||
| 418 | my $f = $t->Frame->pack(-fill => "x"); | ||
| 419 | $f->Label(-text => "Pod:")->pack(-side => "left"); | ||
| 420 | $e = $f->$Entry(-textvariable => \$pod)->pack(-side => "left", -fill => "x", -expand => 1); | ||
| 421 | if ($e->can('history') && $openpod_history) { | ||
| 422 | $e->history($openpod_history); | ||
| 423 | } | ||
| 424 | $e->focus; | ||
| 425 | $go = 0; | ||
| 426 | $e->bind("<Return>" => sub { $go = 1 }); | ||
| 427 | $e->bind("<Escape>" => sub { $go = -1 }); | ||
| 428 | } | ||
| 429 | |||
| 430 | { | ||
| 431 | my $f = $t->Frame->pack; | ||
| 432 | Tk::grid($f->Label(-text => "Use 'Module::Name' for module documentation"), -sticky => "w"); | ||
| 433 | Tk::grid($f->Label(-text => "Use '-f function' for function documentation"), -sticky => "w"); | ||
| 434 | Tk::grid($f->Label(-text => "Use '-q terms' for FAQ entries"), -sticky => "w"); | ||
| 435 | } | ||
| 436 | |||
| 437 | { | ||
| 438 | my $f = $t->Frame->pack; | ||
| 439 | $f->Button(-text => "OK", | ||
| 440 | -command => sub { $go = 1 })->pack(-side => "left"); | ||
| 441 | $f->Button(-text => "New window", | ||
| 442 | -command => sub { $go = 2 })->pack(-side => "left"); | ||
| 443 | $f->Button(-text => "Cancel", | ||
| 444 | -command => sub { $go = -1 })->pack(-side => "left"); | ||
| 445 | } | ||
| 446 | $t->Popup(-popover => $cw); | ||
| 447 | $t->OnDestroy(sub { $go = -1 unless $go }); | ||
| 448 | $t->waitVariable(\$go); | ||
| 449 | if (Tk::Exists($t)) { | ||
| 450 | if (defined $pod && $pod ne "" && $go > 0 && $e->can('historyAdd')) { | ||
| 451 | $e->historyAdd($pod); | ||
| 452 | $openpod_history = [ $e->history ]; | ||
| 453 | } | ||
| 454 | $t->grabRelease; | ||
| 455 | $t->destroy; | ||
| 456 | } | ||
| 457 | |||
| 458 | my %pod_args; | ||
| 459 | if (defined $pod && $pod =~ /^(-[fq])\s+(.+)/) { | ||
| 460 | my $switch = $1; | ||
| 461 | my $func = $2; | ||
| 462 | %pod_args = $cw->getpodargs($switch, $func); | ||
| 463 | } else { | ||
| 464 | %pod_args = $cw->getpodargs($pod); | ||
| 465 | } | ||
| 466 | |||
| 467 | if (defined $pod && $pod ne "") { | ||
| 468 | if ($go == 1) { | ||
| 469 | $cw->configure(%pod_args); | ||
| 470 | } elsif ($go == 2) { | ||
| 471 | my $new_cw = $cw->clone(%pod_args); | ||
| 472 | } | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | sub getpodargs { | ||
| 477 | my($cw, @args) = @_; | ||
| 478 | my @pod_args; | ||
| 479 | if (@args == 1) { | ||
| 480 | @pod_args = ('-file' => $args[0]); | ||
| 481 | } elsif (@args == 2 && $args[0] =~ /^-([fq])$/) { | ||
| 482 | my $switch = $1; | ||
| 483 | my $func = $args[1]; | ||
| 484 | my $func_pod = ""; | ||
| 485 | open(FUNCPOD, "-|") or do { | ||
| 486 | exec "perldoc", "-u", "-$switch", $func; | ||
| 487 | warn "Can't execute perldoc: $!"; | ||
| 488 | CORE::exit(1); | ||
| 489 | }; | ||
| 490 | local $/ = undef; | ||
| 491 | $func_pod = join "", <FUNCPOD>; | ||
| 492 | close FUNCPOD; | ||
| 493 | if ($func_pod ne "") { | ||
| 494 | push @pod_args, '-text' => $func_pod; | ||
| 495 | if ($switch eq "f") { | ||
| 496 | push @pod_args, '-title' => "Function $func"; | ||
| 497 | } else { | ||
| 498 | push @pod_args, '-title' => "FAQ $func"; | ||
| 499 | } | ||
| 500 | } | ||
| 501 | } | ||
| 502 | @pod_args; | ||
| 503 | } | ||
| 504 | |||
| 505 | sub newwindow { | ||
| 506 | shift->clone; | ||
| 507 | } | ||
| 508 | |||
| 509 | sub Dir { | ||
| 510 | require Tk::Pod::Text; | ||
| 511 | require Tk::Pod::Tree; | ||
| 512 | Tk::Pod::Text::Dir(@_); | ||
| 513 | Tk::Pod::Tree::Dir(@_); | ||
| 514 | } | ||
| 515 | |||
| 516 | |||
| 517 | sub quit { shift->destroy } | ||
| 518 | |||
| 519 | sub help { | ||
| 520 | my $w = shift; | ||
| 521 | $w->clone('-tree' => 0, | ||
| 522 | '-file' => 'Tk::Pod_usage.pod', | ||
| 523 | ); | ||
| 524 | } | ||
| 525 | |||
| 526 | sub help_programming { | ||
| 527 | my $w = shift; | ||
| 528 | $w->clone('-tree' => 0, | ||
| 529 | '-file' => 'Tk/Pod.pm', | ||
| 530 | ); | ||
| 531 | } | ||
| 532 | |||
| 533 | sub about { | ||
| 534 | my $w = shift; | ||
| 535 | require Tk::DialogBox; | ||
| 536 | require Tk::ROText; | ||
| 537 | my $d = $w->DialogBox(-title => "About Tk::Pod", | ||
| 538 | -buttons => ["OK"], | ||
| 539 | ); | ||
| 540 | my $message = <<EOF; | ||
| 541 | Tk::Pod - a Pod viewer written in Perl/Tk | ||
| 542 | |||
| 543 | Version information: | ||
| 544 | Tk-Pod distribution $DIST_VERSION | ||
| 545 | Tk::Pod module $VERSION | ||
| 546 | |||
| 547 | System information: | ||
| 548 | @{[ $Pod::Simple::VERSION ? "Pod::Simple $Pod::Simple::VERSION\n" | ||
| 549 | : "" | ||
| 550 | ]} Tk $Tk::VERSION | ||
| 551 | Perl $] | ||
| 552 | OS $^O | ||
| 553 | |||
| 554 | Please contact <srezic\@cpan.org> in case of problems. | ||
| 555 | Send the contents of this window for diagnostics. | ||
| 556 | |||
| 557 | EOF | ||
| 558 | my @lines = split /\n/, $message, -1; | ||
| 559 | my $width = 0; | ||
| 560 | for (@lines) { | ||
| 561 | $width = length $_ if length $_ > $width; | ||
| 562 | } | ||
| 563 | my $txt = $d->add("Scrolled", "ROText", | ||
| 564 | -height => scalar @lines, | ||
| 565 | -width => $width + 1, | ||
| 566 | -relief => "flat", | ||
| 567 | -scrollbars => "oe", | ||
| 568 | )->pack(-expand => 1, -fill => "both"); | ||
| 569 | $txt->insert("end", $message); | ||
| 570 | $d->Show; | ||
| 571 | } | ||
| 572 | |||
| 573 | sub add_section_menu { | ||
| 574 | my($pod) = @_; | ||
| 575 | |||
| 576 | my $screenheight = $pod->screenheight; | ||
| 577 | my $mbar = $pod->Subwidget('menubar'); | ||
| 578 | my $sectionmenu = $mbar->Subwidget('sectionmenu'); | ||
| 579 | if (defined $sectionmenu) { | ||
| 580 | $sectionmenu->delete(0, 'end'); | ||
| 581 | } else { | ||
| 582 | $mbar->insert($mbar->index("last"), "cascade", | ||
| 583 | '-label' => 'Section', -underline => 1); | ||
| 584 | $sectionmenu = $mbar->Menu; | ||
| 585 | $mbar->entryconfigure($mbar->index("last")-1, -menu => $sectionmenu); | ||
| 586 | $mbar->Advertise(sectionmenu => $sectionmenu); | ||
| 587 | } | ||
| 588 | |||
| 589 | my $podtext = $pod->Subwidget('pod'); | ||
| 590 | my $text = $podtext->Subwidget('more')->Subwidget('text'); | ||
| 591 | |||
| 592 | $text->tag('configure', '_section_mark', | ||
| 593 | -background => 'red', | ||
| 594 | -foreground => 'black', | ||
| 595 | ); | ||
| 596 | |||
| 597 | my $sdef; | ||
| 598 | foreach $sdef (@{$podtext->{'sections'}}) { | ||
| 599 | my($head_level, $subject, $pos) = @$sdef; | ||
| 600 | |||
| 601 | my @args; | ||
| 602 | if ($sectionmenu && | ||
| 603 | $sectionmenu->yposition("last") > $screenheight-40) { | ||
| 604 | push @args, -columnbreak => 1; | ||
| 605 | } | ||
| 606 | |||
| 607 | $sectionmenu->command | ||
| 608 | (-label => (" " x ($head_level-1)) . $subject, | ||
| 609 | -command => sub { | ||
| 610 | my($line) = split(/\./, $pos); | ||
| 611 | $text->tag('remove', '_section_mark', qw/0.0 end/); | ||
| 612 | $text->tag('add', '_section_mark', | ||
| 613 | $line-1 . ".0", | ||
| 614 | $line-1 . ".0 lineend"); | ||
| 615 | $text->yview("_section_mark.first"); | ||
| 616 | $text->after(500, [$text, qw/tag remove _section_mark 0.0 end/]); | ||
| 617 | }, | ||
| 618 | @args, | ||
| 619 | ); | ||
| 620 | } | ||
| 621 | } | ||
| 622 | |||
| 623 | sub tree { | ||
| 624 | my $w = shift; | ||
| 625 | if (@_) { | ||
| 626 | my $val = shift; | ||
| 627 | $w->{Tree_on} = $val; | ||
| 628 | my $tree = $w->Subwidget('tree'); | ||
| 629 | my $p = $w->Subwidget("pod"); | ||
| 630 | if ($val) { | ||
| 631 | $p->packForget; | ||
| 632 | $tree->packAdjust(-side => 'left', -fill => 'y'); | ||
| 633 | $p->pack(-side => "left", -expand => 1, -fill => 'both'); | ||
| 634 | if (!$tree->Filled) { | ||
| 635 | $w->_configure_tree; | ||
| 636 | $w->Busy(-recurse => 1); | ||
| 637 | eval { | ||
| 638 | $tree->Fill(-fillcb => sub { | ||
| 639 | $tree->SeePath("file:" . $p->cget(-path)) if $p->cget(-path); | ||
| 640 | }); | ||
| 641 | }; | ||
| 642 | my $err = $@; | ||
| 643 | $w->Unbusy; | ||
| 644 | if ($err) { | ||
| 645 | die $err; | ||
| 646 | } | ||
| 647 | } | ||
| 648 | } else { | ||
| 649 | if ($tree && $tree->manager) { | ||
| 650 | $tree->packForget; | ||
| 651 | $p->packForget; | ||
| 652 | eval { | ||
| 653 | $w->Walk | ||
| 654 | (sub { | ||
| 655 | my $w = shift; | ||
| 656 | if ($w->isa('Tk::Adjuster') && | ||
| 657 | $w->cget(-widget) eq $tree) { | ||
| 658 | $w->destroy; | ||
| 659 | die; | ||
| 660 | } | ||
| 661 | }); | ||
| 662 | }; | ||
| 663 | $p->pack(-side => "left", -expand => 1, -fill => 'both'); | ||
| 664 | } | ||
| 665 | } | ||
| 666 | } | ||
| 667 | $w->{Tree_on}; | ||
| 668 | } | ||
| 669 | |||
| 670 | sub _configure_tree { | ||
| 671 | my($w) = @_; | ||
| 672 | my $tree = $w->Subwidget("tree"); | ||
| 673 | my $p = $w->Subwidget("pod"); | ||
| 674 | |||
| 675 | my $common_showcommand = sub { | ||
| 676 | my($e) = @_; | ||
| 677 | my $uri = $e->uri; | ||
| 678 | my $type = $e->type; | ||
| 679 | if (defined $type && $type eq 'func') { | ||
| 680 | my $text = $Tk::Pod::Tree::FindPods->function_pod($e->name); | ||
| 681 | (-text => $text, -title => $e->name); | ||
| 682 | } elsif (defined $uri && $uri =~ /^file:(.*)/) { | ||
| 683 | (-file => $1); | ||
| 684 | } else { | ||
| 685 | # ignore | ||
| 686 | } | ||
| 687 | }; | ||
| 688 | |||
| 689 | $tree->configure | ||
| 690 | (-showcommand => sub { | ||
| 691 | my $e = $_[1]; | ||
| 692 | my %args = $common_showcommand->($e); | ||
| 693 | my $title = delete $args{-title}; | ||
| 694 | $p->configure(-title => $title) if defined $title; | ||
| 695 | $p->configure(%args); | ||
| 696 | }, | ||
| 697 | -showcommand2 => sub { | ||
| 698 | my $e = $_[1]; | ||
| 699 | my @args = $common_showcommand->($e); | ||
| 700 | # XXX -title? | ||
| 701 | $w->clone(-tree => !!$tree, | ||
| 702 | @args); | ||
| 703 | }, | ||
| 704 | ); | ||
| 705 | } | ||
| 706 | |||
| 707 | sub SearchFAQ { | ||
| 708 | my($cw, $p) = @_; | ||
| 709 | my $t = $cw->Toplevel(-title => "Perl FAQ Search"); | ||
| 710 | $t->transient($cw); | ||
| 711 | $t->grab; | ||
| 712 | my($keyword, $go, $e); | ||
| 713 | { | ||
| 714 | my $Entry = 'Entry'; | ||
| 715 | eval { | ||
| 716 | require Tk::HistEntry; | ||
| 717 | Tk::HistEntry->VERSION(0.40); | ||
| 718 | $Entry = "HistEntry"; | ||
| 719 | }; | ||
| 720 | |||
| 721 | my $f = $t->Frame->pack(-fill => "x"); | ||
| 722 | $f->Label(-text => "FAQ keyword:")->pack(-side => "left"); | ||
| 723 | $e = $f->$Entry(-textvariable => \$keyword)->pack(-side => "left"); | ||
| 724 | if ($e->can('history') && $searchfaq_history) { | ||
| 725 | $e->history($searchfaq_history); | ||
| 726 | } | ||
| 727 | $e->focus; | ||
| 728 | $go = 0; | ||
| 729 | $e->bind("<Return>" => sub { $go = 1 }); | ||
| 730 | $e->bind("<Escape>" => sub { $go = -1 }); | ||
| 731 | } | ||
| 732 | { | ||
| 733 | my $f = $t->Frame->pack; | ||
| 734 | $f->Button(-text => "OK", | ||
| 735 | -command => sub { $go = 1 })->pack(-side => "left"); | ||
| 736 | $f->Button(-text => "New window", | ||
| 737 | -command => sub { $go = 2 })->pack(-side => "left"); | ||
| 738 | $f->Button(-text => "Cancel", | ||
| 739 | -command => sub { $go = -1 })->pack(-side => "left"); | ||
| 740 | } | ||
| 741 | $t->Popup(-popover => $cw); | ||
| 742 | $t->OnDestroy(sub { $go = -1 unless $go }); | ||
| 743 | $t->waitVariable(\$go); | ||
| 744 | if (Tk::Exists($t)) { | ||
| 745 | if (defined $keyword && $keyword ne "" && $go > 0 && $e->can('historyAdd')) { | ||
| 746 | $e->historyAdd($keyword); | ||
| 747 | $searchfaq_history = [ $e->history ]; | ||
| 748 | } | ||
| 749 | $t->grabRelease; | ||
| 750 | $t->destroy; | ||
| 751 | } | ||
| 752 | if (defined $keyword && $keyword ne "") { | ||
| 753 | if ($go) { | ||
| 754 | require File::Temp; | ||
| 755 | my($fh, $pod) = File::Temp::tempfile(UNLINK => 1, | ||
| 756 | SUFFIX => "_tkpod.pod"); | ||
| 757 | my $out = `perldoc -u -q $keyword`; # XXX protect keyword | ||
| 758 | print $fh $out; | ||
| 759 | close $fh; | ||
| 760 | |||
| 761 | if (-z $pod) { | ||
| 762 | $cw->messageBox(-title => "No FAQ keyword", | ||
| 763 | -icon => "error", | ||
| 764 | -message => "FAQ keyword not found", | ||
| 765 | ); | ||
| 766 | } else { | ||
| 767 | if ($go == 1) { | ||
| 768 | $cw->configure(-file => $pod); | ||
| 769 | } elsif ($go == 2) { | ||
| 770 | my $new_cw = $cw->clone('-file' => $pod); | ||
| 771 | } | ||
| 772 | } | ||
| 773 | } | ||
| 774 | } | ||
| 775 | } | ||
| 776 | |||
| 777 | sub zoom { | ||
| 778 | my($w, $method) = @_; | ||
| 779 | my $p = $w->Subwidget("pod"); | ||
| 780 | $p->$method(); | ||
| 781 | $w->set_base_font_size($p->base_font_size); | ||
| 782 | } | ||
| 783 | |||
| 784 | sub zoom_in { shift->zoom("zoom_in") } | ||
| 785 | sub zoom_out { shift->zoom("zoom_out") } | ||
| 786 | sub zoom_normal { shift->zoom("zoom_normal") } | ||
| 787 | |||
| 788 | sub base_font_size { | ||
| 789 | my $w = shift; | ||
| 790 | $w->{Base_Font_Size}; | ||
| 791 | } | ||
| 792 | |||
| 793 | sub set_base_font_size { | ||
| 794 | my($w, $font_size) = @_; | ||
| 795 | $w->{Base_Font_Size} = $font_size; | ||
| 796 | } | ||
| 797 | |||
| 798 | sub clone { | ||
| 799 | my($w, %pod_args) = @_; | ||
| 800 | my %pre_args; | ||
| 801 | for ('-tree', '-exitbutton') { | ||
| 802 | if (exists $pod_args{$_}) { | ||
| 803 | $pre_args{$_} = delete $pod_args{$_}; | ||
| 804 | } else { | ||
| 805 | $pre_args{$_} = $w->cget($_); | ||
| 806 | } | ||
| 807 | } | ||
| 808 | my $new_w = $w->MainWindow->Pod | ||
| 809 | (%pre_args, | ||
| 810 | '-basefontsize' => $w->base_font_size, | ||
| 811 | ); | ||
| 812 | $new_w->configure(%pod_args) if %pod_args; | ||
| 813 | $new_w; | ||
| 814 | } | ||
| 815 | |||
| 816 | 1; | ||
| 817 | |||
| 818 | __END__ |