Gtk2-Ex-PodViewer-0.18/0000755000076500007650000000000011071673532013536 5ustar gavingavinGtk2-Ex-PodViewer-0.18/lib/0000755000076500007650000000000011071673532014304 5ustar gavingavinGtk2-Ex-PodViewer-0.18/lib/Gtk2/0000755000076500007650000000000011071673532015113 5ustar gavingavinGtk2-Ex-PodViewer-0.18/lib/Gtk2/Ex/0000755000076500007650000000000011071673532015467 5ustar gavingavinGtk2-Ex-PodViewer-0.18/lib/Gtk2/Ex/PodViewer/0000755000076500007650000000000011071673532017373 5ustar gavingavinGtk2-Ex-PodViewer-0.18/lib/Gtk2/Ex/PodViewer/Parser.pm0000755000076500007650000002734111071673370021177 0ustar gavingavin# $Id: Parser.pm,v 1.26 2008/10/04 14:00:24 gavin Exp $ # Copyright (c) 2003-2008 Gavin Brown. All rights reserved. This program is # free software; you can redistribute it and/or modify it under the same # terms as Perl itself. package Gtk2::Ex::PodViewer::Parser; use base 'Pod::Parser'; use Carp; use IO::Scalar; use vars qw(%ENTITIES $LINK_TEXT_TEMPLATE $GETTEXT); use Exporter; use bytes; use strict; require 5.8.0; our @EXPORT_OK = qw(&decode_entities); # This table is taken near verbatim from Pod::PlainText in Pod::Parser, which # got it near verbatim from the original Pod::Text. It is therefore credited # to Tom Christiansen, and I'm glad I didn't have to write it. :) "iexcl" to # "divide" added by Tim Jenness. our %ENTITIES = ( 'amp' => '&', # ampersand 'apos' => "'", # apostrophe 'lt' => '<', # left chevron, less-than 'gt' => '>', # right chevron, greater-than 'quot' => '"', # double quote 'sol' => '/', # solidus (forward slash) 'verbar' => '|', # vertical bar "Aacute" => "\xC1", # capital A, acute accent "aacute" => "\xE1", # small a, acute accent "Acirc" => "\xC2", # capital A, circumflex accent "acirc" => "\xE2", # small a, circumflex accent "AElig" => "\xC6", # capital AE diphthong (ligature) "aelig" => "\xE6", # small ae diphthong (ligature) "Agrave" => "\xC0", # capital A, grave accent "agrave" => "\xE0", # small a, grave accent "Aring" => "\xC5", # capital A, ring "aring" => "\xE5", # small a, ring "Atilde" => "\xC3", # capital A, tilde "atilde" => "\xE3", # small a, tilde "Auml" => "\xC4", # capital A, dieresis or umlaut mark "auml" => "\xE4", # small a, dieresis or umlaut mark "Ccedil" => "\xC7", # capital C, cedilla "ccedil" => "\xE7", # small c, cedilla "Eacute" => "\xC9", # capital E, acute accent "eacute" => "\xE9", # small e, acute accent "Ecirc" => "\xCA", # capital E, circumflex accent "ecirc" => "\xEA", # small e, circumflex accent "Egrave" => "\xC8", # capital E, grave accent "egrave" => "\xE8", # small e, grave accent "ETH" => "\xD0", # capital Eth, Icelandic "eth" => "\xF0", # small eth, Icelandic "Euml" => "\xCB", # capital E, dieresis or umlaut mark "euml" => "\xEB", # small e, dieresis or umlaut mark "Iacute" => "\xCD", # capital I, acute accent "iacute" => "\xED", # small i, acute accent "Icirc" => "\xCE", # capital I, circumflex accent "icirc" => "\xEE", # small i, circumflex accent "Igrave" => "\xCC", # capital I, grave accent "igrave" => "\xEC", # small i, grave accent "Iuml" => "\xCF", # capital I, dieresis or umlaut mark "iuml" => "\xEF", # small i, dieresis or umlaut mark "Ntilde" => "\xD1", # capital N, tilde "ntilde" => "\xF1", # small n, tilde "Oacute" => "\xD3", # capital O, acute accent "oacute" => "\xF3", # small o, acute accent "Ocirc" => "\xD4", # capital O, circumflex accent "ocirc" => "\xF4", # small o, circumflex accent "Ograve" => "\xD2", # capital O, grave accent "ograve" => "\xF2", # small o, grave accent "Oslash" => "\xD8", # capital O, slash "oslash" => "\xF8", # small o, slash "Otilde" => "\xD5", # capital O, tilde "otilde" => "\xF5", # small o, tilde "Ouml" => "\xD6", # capital O, dieresis or umlaut mark "ouml" => "\xF6", # small o, dieresis or umlaut mark "szlig" => "\xDF", # small sharp s, German (sz ligature) "THORN" => "\xDE", # capital THORN, Icelandic "thorn" => "\xFE", # small thorn, Icelandic "Uacute" => "\xDA", # capital U, acute accent "uacute" => "\xFA", # small u, acute accent "Ucirc" => "\xDB", # capital U, circumflex accent "ucirc" => "\xFB", # small u, circumflex accent "Ugrave" => "\xD9", # capital U, grave accent "ugrave" => "\xF9", # small u, grave accent "Uuml" => "\xDC", # capital U, dieresis or umlaut mark "uuml" => "\xFC", # small u, dieresis or umlaut mark "Yacute" => "\xDD", # capital Y, acute accent "yacute" => "\xFD", # small y, acute accent "yuml" => "\xFF", # small y, dieresis or umlaut mark "laquo" => "\xAB", # left pointing double angle quotation mark "lchevron" => "\xAB", # synonym (backwards compatibility) "raquo" => "\xBB", # right pointing double angle quotation mark "rchevron" => "\xBB", # synonym (backwards compatibility) "iexcl" => "\xA1", # inverted exclamation mark "cent" => "\xA2", # cent sign "pound" => "\xA3", # (UK) pound sign "curren" => "\xA4", # currency sign "yen" => "\xA5", # yen sign "brvbar" => "\xA6", # broken vertical bar "sect" => "\xA7", # section sign "uml" => "\xA8", # diaresis "copy" => "\xA9", # Copyright symbol "ordf" => "\xAA", # feminine ordinal indicator "not" => "\xAC", # not sign "shy" => '', # soft (discretionary) hyphen "reg" => "\xAE", # registered trademark "macr" => "\xAF", # macron, overline "deg" => "\xB0", # degree sign "plusmn" => "\xB1", # plus-minus sign "sup2" => "\xB2", # superscript 2 "sup3" => "\xB3", # superscript 3 "acute" => "\xB4", # acute accent "micro" => "\xB5", # micro sign "para" => "\xB6", # pilcrow sign = paragraph sign "middot" => "\xB7", # middle dot = Georgian comma "cedil" => "\xB8", # cedilla "sup1" => "\xB9", # superscript 1 "ordm" => "\xBA", # masculine ordinal indicator "frac14" => "\xBC", # vulgar fraction one quarter "frac12" => "\xBD", # vulgar fraction one half "frac34" => "\xBE", # vulgar fraction three quarters "iquest" => "\xBF", # inverted question mark "times" => "\xD7", # multiplication sign "divide" => "\xF7", # division sign "nbsp" => "\x01", # non-breaking space ); our $LINK_TEXT_TEMPLATE = '{section} in the {document} manpage'; our $GETTEXT = 0; eval qq( use Locale::gettext; $GETTEXT = 1; ); =pod =head1 NAME Gtk2::Ex::PodViewer::Parser - a custom POD Parser for Gtk2::Ex::PodViewer. =head1 SYNOPSIS $Gtk2::Ex::PodViewer::Parser::LINK_TEXT_TEMPLATE = '{section} in the {document} manpage'; my $parser = Gtk2::Ex::PodViewer::Parser->new( buffer => $Gtk2TextView->get_buffer, ); $parser->parse_from_file($file); =head1 DESCRIPTION Gtk2::Ex::PodViewer::Parser is a custom Pod parser for the Gtk2::Ex::PodViewer widget. You should never need to use it directly. It is based on L. =head1 METHODS =cut sub new { my $package = shift; my %args = @_; my $parser = $package->SUPER::new; $parser->{buffer} = $args{buffer}; $parser->{iter} = $parser->{buffer}->get_iter_at_offset(0); bless($parser, $package); return $parser; } sub command { my ($parser, $command, $paragraph, $line_num) = @_; if ($command =~ /^head/i) { $paragraph =~ s/[\s\r\n]*$//g; my $mark = $parser->{buffer}->create_mark($paragraph, $parser->{iter}, 1); push(@{$parser->{marks}}, [$paragraph, $mark, $parser->{iter}]); $parser->insert_text($paragraph, $line_num, $command); $parser->insert_text("\n\n", $line_num); } elsif (lc($command) eq 'item') { my $dot = chr(183); $paragraph =~ s/\n*$//g; if ($paragraph eq '*') { $parser->insert_text("$dot ", $line_num, qw(word_wrap bold indented)); } elsif ($paragraph =~ /^\*\s*/) { $paragraph =~ s/^\*\s*//; $parser->insert_text("$dot ", $line_num, qw(word_wrap bold indented)); $parser->insert_text("$paragraph\n\n", $line_num, qw(word_wrap indented)); } elsif ($paragraph =~ /^\d+$/i) { $parser->insert_text("$paragraph ", $line_num, qw(word_wrap bold indented)); } else { $parser->insert_text("$dot ", $line_num, qw(word_wrap bold indented)); $parser->insert_text("$paragraph\n\n", $line_num, qw(word_wrap indented)); } } elsif ($command !~ /^(pod|cut|for|over|back)$/i) { carp("unknown command: '$command' on line $line_num"); $parser->insert_text($paragraph, $line_num, qw(word_wrap)); } } sub verbatim { my ($parser, $paragraph, $line_num) = @_; $parser->insert_text($paragraph, $line_num, qw(monospace)); } sub textblock { my ($parser, $paragraph, $line_num) = @_; $paragraph =~ s/[\r\n]/ /sg; $paragraph .= "\n\n"; $parser->insert_text($paragraph, $line_num, qw(word_wrap)); } sub insert_text { my ($parser, $paragraph, $line_num, @tags) = @_; my %tagnames = ( I => 'italic', B => 'bold', C => 'typewriter', L => 'link', F => 'italic', S => 'monospace', E => 'word_wrap', X => 'normal', Z => 'normal', ); $parser->parse_text( { -expand_ptree => sub { my ($parser, $ptree) = @_; foreach ($ptree->children) { if (ref($_) eq 'Pod::InteriorSequence') { my $sequence = $_; my $command = $sequence->cmd_name; my $text = $sequence->parse_tree->raw_text; if ($command eq 'E') { $text = $ENTITIES{$text} || $text; } elsif ($command eq 'L') { push(@{$parser->{links}}, [$text, $parser->{iter}->get_offset]); if ($text =~ /\|/) { ($text, undef) = split(/\|/, $text, 2); } if ($text =~ /\/[^:]/ && $text !~ /:\/\//) { my ($doc, $section) = split(/\//, $text, 2); if ($doc eq '') { $text = $section; } else { $text = ($GETTEXT ? gettext($LINK_TEXT_TEMPLATE) : $LINK_TEXT_TEMPLATE); $text =~ s/\{section\}/$section/g; $text =~ s/\{document\}/$doc/g; } } } if (!exists($tagnames{$command})) { carp("warning: unknown formatting code '$command'\n"); } else { $parser->{buffer}->insert_with_tags_by_name($parser->{iter}, decode_entities($text), $tagnames{$command}, @tags); } } else { my $text = $_; $parser->{buffer}->insert_with_tags_by_name($parser->{iter}, decode_entities($text), @tags); } } } }, $paragraph, $line_num ); return 1; } sub clear_marks { $_[0]->{marks} = []; return 1; } sub get_marks { my @names; map { push(@names, @{$_}[0]) } @{$_[0]->{marks} }; return @names; } sub get_mark { my ($parser, $name) = @_; foreach my $mark (@{$parser->{marks}}) { return @{$mark}[1] if (@{$mark}[0] eq $name); } return undef; } sub parse_from_file { my ($self, $file) = @_; if (!open(FILE, '<:utf8', $file)) { carp("Cannot open '$file': $!"); return undef; } else { my $data; while () { $data .= $_; } close(FILE); return $self->parse_from_string($data); } } =pod One neat method not implemented by Pod::Parser is $parser->parse_from_string($string); This parses a scalar containing POD data, using IO::Scalar to create a tied filehandle. =cut sub parse_from_string { my ($self, $string) = @_; my $handle = IO::Scalar->new(\$string); $self->{_source} = $string; $self->parse_from_filehandle($handle); $handle->close; return 1; } =pod =head1 IMPORTABLE FUNCTIONS use Gtk2::Ex::PodViewer::Parser qw(decode_entities); my $text = decode_entities($pod); This function takes a string of POD, and returns it with all the POD entities (eg CgtE> =E "E") decoded into readable characters. =cut sub decode_entities { my $text = shift; $text =~ s/E<([^<]*)>/$ENTITIES{$1}/g; $text =~ s/\w{1}<([^<]*)>/$1/g; return $text; } sub source { my $self = shift; return $self->{_source}; } =pod =head1 VARIABLES The C<$LINK_TEXT_TEMPLATE> class variable contains a string that is used to generate link text for POD links for the form LEfoo/barE This string is run through the C function from L (if installed) before it is used, so if your application supports internationalisation, then the string will be translated if it appears in your translation domain. It contains two tokens, C<{section}> and C<{document}>, that are replaced with C and C respectively. =head1 SEE ALSO =over =item * L =item * L =item * L =back =head1 AUTHORS Gavin Brown, Torsten Schoenfeld and Scott Arrington. =head1 COPYRIGHT (c) 2003-2005 Gavin Brown (gavin.brown@uk.com). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1; Gtk2-Ex-PodViewer-0.18/lib/Gtk2/Ex/PodViewer.pm0000644000076500007650000003542511071673463017745 0ustar gavingavin# $Id: PodViewer.pm,v 1.44 2008/10/04 14:01:23 gavin Exp $ # Copyright (c) 2003-2008 Gavin Brown. All rights reserved. This program is # free software; you can redistribute it and/or modify it under the same # terms as Perl itself. package Gtk2::Ex::PodViewer; use Gtk2; use Gtk2::Ex::PodViewer::Parser; use vars qw($VERSION $self->{db}); use Gtk2::Pango; use Pod::Simple::Search; use strict; our $VERSION = '0.18'; # # we want to create a new signal for this object, which means we need to # create a new GType. however, using Glib::Object::Subclass (as of Glib # 1.031 and all previous) makes it impossible for us to be require'd, # thanks to the CHECK block madness. so, we use Glib::Type::register # directly. # # inherit new from Glib::Object. *new = \&Glib::Object::new; Glib::Type->register( Gtk2::TextView::, __PACKAGE__, signals => { link_clicked => { param_types => [qw/Glib::String/], }, 'link_enter' => { param_types => [qw/Glib::String/], }, 'link_leave' => { }, }, properties => [ ], ); sub INIT_INSTANCE { my $self = shift; $self->set_editable(0); $self->set_wrap_mode('word'); $self->{parser} = Gtk2::Ex::PodViewer::Parser->new(buffer => $self->get_buffer); $self->get_buffer->create_tag( 'bold', weight => PANGO_WEIGHT_BOLD ); $self->get_buffer->create_tag( 'italic', style => 'italic', ); $self->get_buffer->create_tag( 'word_wrap', wrap_mode => 'word', ); $self->get_buffer->create_tag( 'head1', weight => PANGO_WEIGHT_BOLD, size => 15 * PANGO_SCALE, wrap_mode => 'word', foreground => '#404080', ); $self->get_buffer->create_tag( 'head2', weight => PANGO_WEIGHT_BOLD, size => 12 * PANGO_SCALE, wrap_mode => 'word', foreground => '#404080', ); $self->get_buffer->create_tag( 'head3', weight => PANGO_WEIGHT_BOLD, size => 9 * PANGO_SCALE, wrap_mode => 'word', foreground => '#404080', ); $self->get_buffer->create_tag( 'head4', weight => PANGO_WEIGHT_BOLD, size => 6 * PANGO_SCALE, wrap_mode => 'word', foreground => '#404080', ); $self->get_buffer->create_tag( 'monospace', family => 'monospace', wrap_mode => 'none', foreground => '#606060', ); $self->get_buffer->create_tag( 'typewriter', family => 'monospace', wrap_mode => 'word', ); $self->get_buffer->create_tag( 'link', foreground => 'blue', underline => 'single', wrap_mode => 'word', ); $self->get_buffer->create_tag( 'indented', left_margin => 40, ); $self->get_buffer->create_tag( 'normal', wrap_mode => 'word', ); # put a 6pixel white border around the text: $self->set_border_window_size('left', 6); $self->set_border_window_size('top', 6); $self->set_border_window_size('right', 6); $self->set_border_window_size('bottom', 6); $self->modify_bg('normal', Gtk2::Gdk::Color->new(65535, 65535, 65535)); my $cursor = Gtk2::Gdk::Cursor->new('xterm'); my $url_cursor = Gtk2::Gdk::Cursor->new('hand2'); $self->signal_connect('button_release_event', sub { $self->clicked($_[1]) ; return 0 }); $self->signal_connect_after('realize' => sub { my $view = shift; $view->get_window('text')->set_events([qw(exposure-mask pointer-motion-mask button-press-mask button-release-mask key-press-mask structure-mask property-change-mask scroll-mask)]); return 0; }); $self->signal_connect('motion_notify_event' => sub { my ($view, $event) = @_; my ($x, $y) = $view->window_to_buffer_coords('text', $event->x, $event->y); my $over_link = $view->get_iter_at_location($x, $y)->has_tag($view->get_buffer()->get_tag_table()->lookup("link")); if ($over_link && !$self->{was_over_link}) { # user has just brought the mouse over a link: $self->{was_over_link} = 1; my $text = $self->get_link_text_at_iter($view->get_iter_at_location($x, $y)); $self->signal_emit('link_enter', $text) if ($text ne ''); } elsif (!$over_link && $self->{was_over_link}) { # user has just moved the mouse away from a link: $self->{was_over_link} = 0; $self->signal_emit('link_leave'); } $view->get_window('text')->set_cursor($over_link ? $url_cursor : $cursor); return 0; }); } =pod =head1 NAME Gtk2::Ex::PodViewer - a Gtk2 widget for displaying Plain old Documentation (POD). NB: This module used to be called L. That module is now a stub that points to this module. =head1 SYNOPSIS use Gtk2 -init; use Gtk2::Ex::PodViewer; my $viewer = Gtk2::Ex::PodViewer->new; $viewer->load('/path/to/file.pod'); # load a file $viewer->load('IO::Scalar'); # load a module $viewer->load('perlvar'); # load a perldoc $viewer->load('bless'); # load a function $viewer->show; # see, it's a widget! my $window = Gtk2::Window->new; $window->add($viewer); $window->show; Gtk2->main; =head1 DESCRIPTION Gtk2::Ex::PodViewer is a widget for rendering Perl POD documents. It is based on the Gtk2::TextView widget and uses Pod::Parser for manipulating POD data. Gtk2::Ex::PodViewer widgets inherit all the methods and properties of Gtk2::TextView widgets. Full information about text buffers can be found in the Gtk+ documentation at L. =head1 OBJECT HIERARCHY L +--- L +--- L +--- L +--- L +--- L =head1 CONSTRUCTOR my $view = Gtk2::Ex::PodViewer->new; creates and returns a new Gtk2::Ex::PodViewer widget. =head1 ADDITIONAL METHODS $viewer->clear; This clears the viewer's buffer and resets the iter. You should never need to use this method since the loader methods (see L below) will do it for you. =cut sub _init_db { my $self = shift; $self->{db} = Pod::Simple::Search->new->survey; } =pod my $db = $viewer->get_db; This method returns a hashref that contains the POD document database used internally by Gtk2::Ex::PodViewer. If you want to improve startup performance, you can cache this database using a module like C. To load a cached database into a viewer object, call $viewer->set_db($db); before making a call to any of the document loader methods below (otherwise, Gtk2::Ex::PodViewer will create a new database for itself). If you want to tell Gtk2::Ex::PodViewer to create a new document database (for example, after a new module has been installed), use $viewer->reinitialize_db; =cut sub set_db { $_[0]->{db} = $_[1] } sub get_db { $_[0]->{db} } sub reinitialize_db { shift()->_init_db } sub clear { my $self = shift; $self->get_buffer->set_text(''); $self->{parser}{iter} = $self->get_buffer->get_iter_at_offset(0); return 1; } =pod @marks = $view->get_marks; This returns an array of section headers. So for example, a POD document of the form =pod =head1 NAME =head1 SYNOPSIS =cut would result in @marks = ('NAME', 'SYNOPSIS'); You can then use the contents of this array to create a document index. =cut sub get_marks { return $_[0]->{parser}->get_marks; } =pod $name = 'SYNOPSIS'; $mark = $view->get_mark($name); returns the GtkTextMark object referred to by C<$name>. =cut sub get_mark { return $_[0]->{parser}->get_mark($_[1]); } =pod $name = 'SYNOPSIS'; $view->jump_to($name); this scrolls the PodViewer window to the selected mark. =cut sub jump_to { my ($self, $name) = @_; my $mark = $self->get_mark($name); return undef unless (ref($mark) eq 'Gtk2::TextMark'); return $self->scroll_to_mark($mark, undef, 1, 0, 0); } =pod $viewer->load($document); Loads a given document. C<$document> can be a perldoc name (eg., C<'perlvar'>), a module (eg. C<'IO::Scalar'>), a filename or the name of a Perl builtin function from L. Documents are searched for in that order, that is, the L document will be loaded before a file called C in the current directory. =cut sub load { my ($self, $name) = @_; $self->_init_db if (!defined($self->{db})); return 1 if $self->load_function($name); return 1 if $self->load_doc($name); return 1 if $self->load_file($name); return undef; } =pod =head1 DOCUMENT LOADERS The C method is a wrapper to a number of specialised document loaders. You can call one of these loaders directly to override the order in which Gtk2::Ex::PodViewer searches for documents: $viewer->load_doc($perldoc); loads a perldoc file or Perl module documentation, or undef on failure. $viewer->load_file($file); loads POD from a file, or returns undef on failure. $viewer->load_function($function); This method scans the L POD document for the documentation for a given function. The algorithm for this is lifted from the L module, so it should work identically to C. $viewer->load_string($string); This method renders the POD data in the C<$string> variable. =head2 DEPRECATED DOCUMENT LOADERS The following document loads are now deprecated, and are now just wrapper of the C method: $viewer->load_perldoc($perldoc); $viewer->load_module($module); =cut sub load_perldoc { $_[0]->load_doc($_[1]) } sub load_module { $_[0]->load_doc($_[1]) } sub load_doc { my ($self, $doc) = @_; return ($self->{db}->{$doc} ? $self->load_file($self->{db}->{$doc}) : undef); } sub load_function { my ($self, $function) = @_; my $perlfunc = $self->perlfunc; return undef if ($perlfunc eq ''); open(PERLFUNC, $perlfunc) or return undef; # ignore everything up to here: while () { last if /^=head2 Alphabetical Listing of Perl Functions/; } # this is lifted straight from Pod/Perldoc.pm, with only a couple # of modifications: my $found = 0; my $inlist = 0; my $pod = ''; while () { if (/^=item\s+\Q$function\E\b/) { $found = 1; } elsif (/^=item/) { last if $found > 1 and not $inlist; } next unless $found; if (/^=over/) { ++$inlist; } elsif (/^=back/) { --$inlist; } $pod .= $_; ++$found if /^\w/; } close(PERLFUNC) or return undef; return undef if ($pod eq ''); $self->load_string($pod); return 1; } sub load_file { my ($self, $file) = @_; if (-e $file) { $self->clear; $self->parser->clear_marks; $self->parser->parse_from_file($file); return 1; } else { return undef; } } sub load_string { my ($self, $string) = @_; $self->clear; $self->parser->clear_marks; $self->parser->parse_from_string($string); return 1; } sub perlfunc { my $self = shift; return $self->{perlfunc} if (defined($self->{perlfunc})); foreach my $dir (@INC) { my $file = sprintf('%s/pod/perlfunc.pod', $dir); if (-e $file) { $self->{perlfunc} = $file; return $self->{perlfunc}; } } } =pod $parser = $view->parser; returns the C object used to render the POD data. =cut sub parser { return $_[0]->{parser}; } sub clicked { my ($self, $event) = @_; my ($x, $y) = $self->window_to_buffer_coords('widget', $event->get_coords); my $iter = $self->get_iter_at_location($x, $y); my $text = $self->get_link_text_at_iter($iter); if (defined($text) && $text ne '') { $self->signal_emit('link_clicked', $text); } return 1; } sub get_link_text_at_iter { my ($self, $iter) = @_; my $tag = $self->get_buffer->get_tag_table->lookup('link'); if ($iter->has_tag($tag)) { my $offset = $iter->get_offset; for (my $i = 0 ; $i < scalar(@{$self->parser->{links}}) ; $i++) { my ($text, $this_offset) = @{@{$self->parser->{links}}[$i]}; if ($offset >= $this_offset && $offset <= ($this_offset + length($text))) { return $text; } } } return undef; } =pod =head1 SIGNALS Gtk2::Ex::PodViewer inherits all of Gtk2::TextView's signals, and has the following: =head2 The C<'link_clicked'> signal $viewer->signal_connect('link_clicked', \&clicked); sub clicked { my ($viewer, $link_text) = @_; print "user clicked on '$link_text'\n"; } Emitted when the user clicks on a hyperlink within the POD. This may be a section title, a document name, or a URL. The receiving function will be giving two arguments: a reference to the Gtk2::Ex::PodViewer object, and a scalar containing the link text. =head2 The C<'link_enter'> signal $viewer->signal_connect('link_enter', \&enter); sub enter { my ($viewer, $link_text) = @_; print "user moused over '$link_text'\n"; } Emitted when the user moves the mouse pointer over a hyperlink within the POD. This may be a section title, a document name, or a URL. The receiving function will be giving two arguments: a reference to the Gtk2::Ex::PodViewer object, and a scalar containing the link text. =head2 The C<'link_leave'> signal $viewer->signal_connect('link_leave', \&leave); sub clicked { my $viewer = shift; print "user moused out\n"; } Emitted when the user moves the mouse pointer out from a hyperlink within the POD. =head1 Getting and Setting Font preferences You can set the font used to render text in a Gtk2::Ex::PodViewer widget like so: $viewer->modify_font(Gtk2::Pango::FontDescription->from_string($FONT_NAME); To modify the appearance of the various elements of the page, you need to extract the L from the viewer's buffer: my $tag = $viewer->get_buffer->get_tag_table->lookup('monospace'); $tag->set('font' => $FONT_NAME); The tags used by Gtk2::Ex::PodViewer are: =over =item C Used to format bold text. =item C Used to format italic text. =item C ... C Used to format headers. =item C Used to format preformatted text. =item C Used to format inline preformatted text. =item C Used to format hyperlinks. =back =head1 THE podviewer PROGRAM C is installed with Gtk2::Ex::PodViewer. It is a simple Pod viewing program. It is pretty minimal, but does do the job quite well. Those looking for a more feature-complete documentation browser should try PodBrowser, available from L. =head1 BUGS AND TASKS Gtk2::Ex::PodViewer is a work in progress. All comments, complaints, offers of help and patches are welcomed. We currently know about these issues: =over =item * When rendering long documents the UI freezes for too long. =item * Some strangeness with Unicode. =back =head1 PREREQUISITES =over =item * L (obviously). The most recent version will be from L. =item * L =item * L =item * L =back L, which is part of the L distribution, also requires L. =head1 SEE ALSO =over =item * L or L =item * L =item * L =back =head1 AUTHORS Gavin Brown, Torsten Schoenfeld and Scott Arrington. =head1 COPYRIGHT (c) 2003-2005 Gavin Brown (gavin.brown@uk.com). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1; Gtk2-Ex-PodViewer-0.18/Makefile.PL0000644000076500007650000000114111071673370015505 0ustar gavingavin#!/usr/bin/perl # $Id: Makefile.PL,v 1.9 2008/10/04 14:00:24 gavin Exp $ use ExtUtils::MakeMaker; use strict; my $version_ok = 0; eval { require 5.8.0; $version_ok = 1; }; if ($version_ok < 1) { print STDERR "This module requires Perl 5.8.0 or later!\n"; exit 1; } WriteMakefile( 'NAME' => 'Gtk2::Ex::PodViewer', 'VERSION_FROM' => 'lib/Gtk2/Ex/PodViewer.pm', 'PREREQ_FATAL' => 1, 'PREREQ_PM' => { 'Gtk2' => 0, 'Pod::Parser' => 0, 'IO::Scalar' => 0, 'File::Basename' => 0, 'Pod::Simple::Search' => 0, }, 'EXE_FILES' => [ 'src/podviewer' ] ); Gtk2-Ex-PodViewer-0.18/README0000644000076500007650000000137611071673370014425 0ustar gavingavinINSTALLATION To install this module type the following: perl Makefile.PL make make test make install Debian packages have been created by James Curbo - get them at http://teyandei.net/debian/. DEPENDENCIES This module requires these other modules and libraries: Gtk2 IO::Scalar Pod::Parser File::Basename Locale::gettext BUGS Something is wrong with the way Unicode characters are handled; it needs some attention. The next version will probably address this. Thanks to Marc Lehmann to spotting this. COPYRIGHT AND LICENCE This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Copyright (C) 2003-2008 Gavin Brown. -- $Id: README,v 1.6 2008/10/04 14:00:24 gavin Exp $ Gtk2-Ex-PodViewer-0.18/src/0000755000076500007650000000000011071673532014325 5ustar gavingavinGtk2-Ex-PodViewer-0.18/src/podviewer0000755000076500007650000006010510320462354016253 0ustar gavingavin#!/usr/bin/perl # Copyright (c) 2003-2005 Gavin Brown. All rights reserved. This program is # free software; you can redistribute it and/or modify it under the same # terms as Perl itself. # $Id: podviewer,v 1.32 2005/10/04 11:18:04 jodrell Exp $ use lib 'lib/'; use Gtk2 -init; use Gtk2::Ex::PodViewer; use Gtk2::Ex::Simple::List; use Gtk2::Gdk::Keysyms; use File::Basename qw(basename); use Data::Dumper; use strict; my $NAME = 'Pod Viewer'; my $SNAME = 'podview'; my $HISTORY_FILE = sprintf('%s/.%s_history', $ENV{HOME}, $SNAME); my $RCFILE = sprintf('%s/.%src', $ENV{HOME}, $SNAME); my $SEARCH_OFFSET = 0; my $LAST_SEARCH_STR; my $BLANK_STRING = 'podviewer:blank'; # navigation breadcrumbs my @BACK_BREADCRUMBS = (); my @FORWARD_BREADCRUMBS = (); # history for the combo entry my @HISTORY; if (open(HISTORY_FILE, $HISTORY_FILE)) { while () { chomp; push(@HISTORY, $_); } close(HISTORY_FILE); } my %CONFIG = ( pane_pos => 125, pane_vis => 1, icon_size => 'large-toolbar', x => 600, y => 500, home => $BLANK_STRING, ); if (open(RCFILE, $RCFILE)) { while () { chomp; my ($name, $value) = split(/=/, $_, 2); $CONFIG{lc($name)} = $value; } close(RCFILE); } chomp (my $htmlview = `which htmlview 2> /dev/null`); chomp (my $gnome_moz_remote = `which gnome-moz-remote 2> /dev/null`); my $BROWSER; if (defined($ENV{BROWSER})) { $BROWSER = $ENV{BROWSER}; } elsif (-x $htmlview) { $BROWSER = $htmlview; } elsif (-x $gnome_moz_remote) { $BROWSER = $gnome_moz_remote; } my $NORMAL_CURSOR = Gtk2::Gdk::Cursor->new('left_ptr'); my $BUSY_CURSOR = Gtk2::Gdk::Cursor->new('watch'); my $idx_pbf = Gtk2::Image->new->render_icon('gtk-jump-to', 'menu'); my $tips = Gtk2::Tooltips->new; my $accels = Gtk2::AccelGroup->new; my $back_button = Gtk2::Button->new; $back_button->add(Gtk2::Image->new_from_stock('gtk-go-back', $CONFIG{icon_size})); $back_button->set_relief('none'); $back_button->signal_connect('clicked', \&go_back); $back_button->add_accelerator('clicked', $accels, $Gtk2::Gdk::Keysyms{Left}, 'mod1-mask', 'visible'); $tips->set_tip($back_button, 'Go back (Alt-Left)'); $back_button->set_sensitive(0); # no breadcrumbs at startup my $up_button = Gtk2::Button->new; $up_button->add(Gtk2::Image->new_from_stock('gtk-go-up', $CONFIG{icon_size})); $up_button->set_relief('none'); $up_button->signal_connect('clicked', \&go_up); $up_button->add_accelerator('clicked', $accels, ord('u'), 'control-mask', 'visible'); $up_button->add_accelerator('clicked', $accels, $Gtk2::Gdk::Keysyms{Up}, 'mod1-mask', 'visible'); $tips->set_tip($up_button, 'Go up a level (Ctrl-U)'); my $forward_button = Gtk2::Button->new; $forward_button->add(Gtk2::Image->new_from_stock('gtk-go-forward', $CONFIG{icon_size})); $forward_button->set_relief('none'); $forward_button->signal_connect('clicked', \&go_forward); $forward_button->add_accelerator('clicked', $accels, $Gtk2::Gdk::Keysyms{Right}, 'mod1-mask', 'visible'); $tips->set_tip($forward_button, 'Forward (Alt-Right)'); $forward_button->set_sensitive(0); # no breadcrumbs at startup my $home_button = Gtk2::Button->new; $home_button->add(Gtk2::Image->new_from_stock('gtk-home', $CONFIG{icon_size})); $home_button->set_relief('none'); $home_button->signal_connect('clicked', \&go_home); $home_button->add_accelerator('clicked', $accels, ord('h'), 'control-mask', 'visible'); $tips->set_tip($home_button, 'Go Home (Ctrl-H)'); my $index_button = Gtk2::ToggleButton->new; $index_button->add(Gtk2::Image->new_from_stock('gtk-index', $CONFIG{icon_size})); $index_button->set_relief('none'); $index_button->add_accelerator('clicked', $accels, ord('l'), 'control-mask', 'visible'); $tips->set_tip($index_button, 'Show/hide the index (Ctrl-L)'); my $browse_button = Gtk2::Button->new; $browse_button->add(Gtk2::Image->new_from_stock('gtk-open', $CONFIG{icon_size})); $browse_button->set_relief('none'); $browse_button->signal_connect('clicked', \&browse); $browse_button->add_accelerator('clicked', $accels, ord('o'), 'control-mask', 'visible'); $tips->set_tip($browse_button, 'Select a file to display (Ctrl-0)'); my $combo = Gtk2::Combo->new; $combo->disable_activate; $combo->entry->signal_connect('activate', \&load, 1); $combo->set_popdown_strings(@HISTORY); $combo->set_case_sensitive(1); $combo->entry->set_text(undef); $tips->set_tip($combo->entry, 'Enter a perldoc, module, file or function'); my $load_button = Gtk2::Button->new; $load_button->add(Gtk2::Image->new_from_stock('gtk-jump-to', $CONFIG{icon_size})); $load_button->set_relief('none'); $load_button->signal_connect('clicked', \&load); $load_button->add_accelerator('clicked', $accels, ord('r'), 'control-mask', 'visible'); $load_button->add_accelerator('clicked', $accels, Gtk2::Accelerator->parse('F5'), 'visible'); $tips->set_tip($load_button, 'Load the selected document (Ctrl-R)'); my $about_button = Gtk2::Button->new; $about_button->add(Gtk2::Image->new_from_stock('gtk-dialog-info', $CONFIG{icon_size})); $about_button->set_relief('none'); $about_button->signal_connect('clicked', \&about); $tips->set_tip($about_button, 'About this program'); # # this button is never visible - it's here to let users quit the program with ^Q # my $quit_button = Gtk2::Button->new; $quit_button->signal_connect('clicked', \&close_program); $quit_button->add_accelerator('clicked', $accels, ord('q'), 'control-mask', 'visible'); $combo->entry->signal_connect('changed', sub { if ($combo->entry->get_text eq '') { $load_button->set_sensitive(0); } else { $load_button->set_sensitive(1); if ($combo->entry->get_text =~ /::/) { $up_button->set_sensitive(1); } else { $up_button->set_sensitive(0); } } }); my $search_entry = Gtk2::Entry->new; $search_entry->signal_connect('activate', \&do_search); $tips->set_tip($search_entry, 'Enter a substring to search for'); # # more invisible buttons # my $new_search_button = Gtk2::Button->new; $new_search_button->signal_connect('clicked', \&search_dialog); $new_search_button->add_accelerator('clicked', $accels, ord('f'), 'control-mask', 'visible'); my $search_again_button = Gtk2::Button->new; $search_again_button->signal_connect('clicked', \&do_search); $search_again_button->add_accelerator('clicked', $accels, ord('g'), 'control-mask', 'visible'); my $help_button = Gtk2::Button->new; $help_button->signal_connect('clicked', \&load_help); $help_button->add_accelerator('clicked', $accels, Gtk2::Accelerator->parse('F1'), 'visible'); my $index = Gtk2::Ex::Simple::List->new('icon' => 'pixbuf', 'title' => 'text', 'link' => 'hidden'); $index->set_headers_visible(0); $index->get_column(1)->set_sizing('autosize'); my $index_scrwin = Gtk2::ScrolledWindow->new; $index_scrwin->set_shadow_type('in'); $index_scrwin->set_policy('automatic', 'automatic'); $index_scrwin->add_with_viewport($index); $index_scrwin->get_child->set_shadow_type('none'); $index_button->signal_connect('toggled', sub { if ($index_button->get_active) { $index_scrwin->show_all; } else { $index_scrwin->hide_all; } }); my $viewer = Gtk2::Ex::PodViewer->new; $viewer->set_border_width(2); $viewer->set_cursor_visible(0); $viewer->signal_connect(link_clicked => \&link_clicked); $viewer->signal_connect('link_enter', sub { set_status("Go to $_[1]") }); $viewer->signal_connect('link_leave', sub { set_status('') }); $index->get_selection->signal_connect('changed', sub { my $idx = ($index->get_selected_indices)[0]; my $mark = $index->{data}[$idx][2]; $viewer->jump_to($mark); return 1; }); my $viewer_scrwin = Gtk2::ScrolledWindow->new; $viewer_scrwin->set_shadow_type('in'); $viewer_scrwin->set_policy('automatic', 'automatic'); $viewer_scrwin->add($viewer); my $pane = Gtk2::HPaned->new; $pane->set_position($CONFIG{pane_pos}); $pane->add1($index_scrwin); $pane->add2($viewer_scrwin); my $hbox = Gtk2::HBox->new; $hbox->set_spacing(6); $hbox->pack_start($back_button, 0, 0, 0); $hbox->pack_start($up_button, 0, 0, 0); $hbox->pack_start($forward_button, 0, 0, 0); $hbox->pack_start($home_button, 0, 0, 0) unless ($CONFIG{home} eq ''); $hbox->pack_start($index_button, 0, 0, 0); $hbox->pack_start($browse_button, 0, 0, 0); $hbox->pack_start(Gtk2::Label->new('Document:'), 0, 0, 0); $hbox->pack_start($combo, 1, 1, 0); $hbox->pack_start($load_button, 0, 0, 0); $hbox->pack_start(Gtk2::VSeparator->new, 0, 0, 0); $hbox->pack_start(Gtk2::Label->new('Search:'), 0, 0, 0); $hbox->pack_start($search_entry, 0, 0, 0); $hbox->pack_start($about_button, 0, 0, 0); my $status = Gtk2::Statusbar->new; my $vbox = Gtk2::VBox->new; $vbox->set_border_width(2); $vbox->set_spacing(2); $vbox->pack_start($hbox, 0, 0, 0); $vbox->pack_start($pane, 1, 1, 0); $vbox->pack_start($status, 0, 0, 0); my $window = Gtk2::Window->new('toplevel'); $window->set_position('center'); $window->set_default_size($CONFIG{x}, $CONFIG{y}); $window->set_border_width(0); $window->set_title($NAME); $window->signal_connect('delete_event', \&close_program); $window->add_accel_group($accels); $window->add($vbox); $window->set_icon($window->render_icon('gtk-dnd', 'dialog')); my $MAXIMIZED = 0; my $FULLSCREEN = 0; $window->signal_connect('window-state-event', sub { my $mask = $_[1]->changed_mask; if ("$mask" eq '[ withdrawn ]') { $MAXIMIZED = 0; $FULLSCREEN = 0; } elsif ("$mask" eq '[ maximized ]') { if ($MAXIMIZED == 1) { $MAXIMIZED = 0; } else { $MAXIMIZED = 1; } $FULLSCREEN = 0; } elsif ("$mask" eq '[ fullscreen ]') { $MAXIMIZED = 0; if ($FULLSCREEN == 1) { $FULLSCREEN = 0; } else { $FULLSCREEN = 1; } } }); $window->show_all; if ($CONFIG{pane_vis} != 1) { $index_scrwin->hide_all; $index_button->set_active(0); } else { $index_button->set_active(1); } $combo->entry->grab_focus; $load_button->set_sensitive(0); $up_button->set_sensitive(0); blank_status(); $combo->entry->set_text(($ARGV[0] ne '' ? $ARGV[0] : $CONFIG{home})); load(); $window->maximize if ($CONFIG{maximized} == 1); Gtk2->main; exit; sub browse { my $dialog = Gtk2::Dialog->new; $dialog->set_title('Open Document'); $dialog->set_modal(1); $dialog->add_buttons( 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok', ); $dialog->set_resizable(0); $dialog->set_icon($window->get_icon); my $label = Gtk2::Label->new('Enter the builtin function, perl module, or filename that you wish to view:'); $label->set_line_wrap(1); my $alignment = Gtk2::Alignment->new(0, 0, 0, 0); $alignment->add($label); my $open_combo = Gtk2::Combo->new; $open_combo->set_popdown_strings(@HISTORY); $open_combo->entry->set_text($combo->entry->get_text); $open_combo->disable_activate; $open_combo->entry->signal_connect('activate', sub { $dialog->response('ok') }); my $button = Gtk2::Button->new; $button->add(Gtk2::Alignment->new(0.5, 0.5, 0, 0)); $button->child->add(Gtk2::HBox->new); $button->child->child->pack_start(Gtk2::Image->new_from_stock('gtk-open', 'button'), 0, 0, 0); $button->child->child->pack_start(Gtk2::Label->new('Browse...'), 1, 1, 0); $button->signal_connect('clicked', sub { my $selection; if ('' ne (my $msg = Gtk2->check_version (2, 4, 0)) or $Gtk2::VERSION < 1.040) { $selection = Gtk2::FileSelection->new('Select File'); } else { $selection = Gtk2::FileChooserDialog->new( 'Select File', $window, 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok' ); } $selection->set_icon($window->get_icon); $selection->set_filename($open_combo->entry->get_text) if (-e $open_combo->entry->get_text); $selection->signal_connect('response', sub { if ($_[1] eq 'ok') { $open_combo->entry->set_text($selection->get_filename); $selection->destroy; } else { $selection->destroy; } }); $selection->run; return 1; }); my $table = Gtk2::Table->new(2, 2); $table->set_border_width(6); $table->set_col_spacings(12); $table->set_row_spacings(12); $table->attach_defaults($alignment, 0, 2, 0, 1); $table->attach_defaults($open_combo, 0, 1, 1, 2); $table->attach_defaults($button, 1, 2, 1, 2); $dialog->vbox->pack_start($table, 1, 1, 0); $dialog->signal_connect('response', sub { if ($_[1] eq 'ok') { $combo->entry->set_text($open_combo->entry->get_text()); $dialog->destroy; load(); } else { $dialog->destroy; } }); $open_combo->entry->grab_focus; $dialog->show_all; } sub strippod { my $text = shift; $text =~ s/B<([^<]*)>/$1/g; $text =~ s/E/>/g; $text } sub load { my $FORWARD_HISTORY_RESET = shift; my $text = $combo->entry->get_text; $text =~ s/^\s+//g; $text =~ s/\s+$//g; return undef if ($text eq ''); set_status('Loading...'); $window->window->set_cursor($BUSY_CURSOR); $viewer->get_window('text')->set_cursor($BUSY_CURSOR); Gtk2->main_iteration while (Gtk2->events_pending); $browse_button->set_sensitive(0); $load_button->set_sensitive(0); $up_button->set_sensitive(0); if ($text eq $BLANK_STRING) { $combo->entry->set_text(''); $viewer->clear; $window->window->set_cursor($NORMAL_CURSOR); $viewer->get_window('text')->set_cursor($NORMAL_CURSOR); $window->set_title($NAME); $browse_button->set_sensitive(1); @{$index->{data}} = (); blank_status(); } elsif ($viewer->load($text)) { $viewer->get_buffer->move_mark( $viewer->get_buffer->get_mark('insert'), $viewer->get_buffer->get_iter_at_line(0) ); $viewer->get_buffer->move_mark( $viewer->get_buffer->get_mark('selection_bound'), $viewer->get_buffer->get_iter_at_line(0) ); $window->window->set_cursor($NORMAL_CURSOR); $viewer->get_window('text')->set_cursor($NORMAL_CURSOR); @{$index->{data}} = (); map { push(@{$index->{data}}, [ $idx_pbf, strippod ($_), $_ ]) } $viewer->get_marks; if (-e $text) { $window->set_title(sprintf('%s: %s', $NAME, basename($text))); } else { $window->set_title(sprintf('%s: %s', $NAME, $text)); } @HISTORY = grep { $text ne $_ } @HISTORY; splice(@HISTORY, 0, 0, $text); $combo->set_popdown_strings(@HISTORY); blank_status(); $browse_button->set_sensitive(1); $load_button->set_sensitive(1); if ($text =~ /::/) { $up_button->set_sensitive(1); } $SEARCH_OFFSET = 0; push(@BACK_BREADCRUMBS, $text); @FORWARD_BREADCRUMBS = () if ($FORWARD_HISTORY_RESET == 1); $back_button->set_sensitive(1 < scalar(@BACK_BREADCRUMBS)); $forward_button->set_sensitive(0 < scalar(@FORWARD_BREADCRUMBS)); $viewer->grab_focus; return 1; } else { $window->window->set_cursor($NORMAL_CURSOR); $viewer->get_window('text')->set_cursor($NORMAL_CURSOR); my $dialog = Gtk2::MessageDialog->new($window, 'modal', 'error', 'ok', "Couldn't find a POD document for '$text'."); $dialog->set_icon($window->get_icon); $dialog->signal_connect('response', sub { $dialog->destroy }); blank_status(); $browse_button->set_sensitive(1); $load_button->set_sensitive(1); if ($text =~ /::/) { $up_button->set_sensitive(1); } $dialog->run; return undef; } } sub close_program { save_history(); save_options(); exit; } sub save_history { open(HISTORY_FILE, ">$HISTORY_FILE"); print HISTORY_FILE join("\n", @HISTORY); close(HISTORY_FILE); return 1; } sub save_options { $window->unfullscreen; $window->unmaximize; Gtk2->main_iteration while (Gtk2->events_pending); $CONFIG{pane_vis} = ($index_button->get_active ? 1 : 0); $CONFIG{pane_pos} = $pane->get_position; if ($MAXIMIZED == 1) { $CONFIG{maximized} = 1; } else { $CONFIG{maximized} = 0; ($CONFIG{x}, $CONFIG{y}) = $window->get_size if ($FULLSCREEN == 0); } open(RCFILE, ">$RCFILE"); foreach my $key (sort keys %CONFIG) { print RCFILE "$key=$CONFIG{$key}\n"; } close(RCFILE); return 1; } sub about { my $about = Gtk2::Dialog->new; $about->set_icon($window->get_icon); $about->set_resizable(0); my $label = Gtk2::Label->new; my $browser_string = ($BROWSER ne '' ? "$NAME will use '".basename($BROWSER)."' for URLs" : 'no browser defined.'); my $markup = <<"END"; Pod Viewer Using Gtk2::Ex::PodViewer v$Gtk2::Ex::PodViewer::VERSION Copyright (c) 2003-2005 Gavin Brown Torsten Schoenfeld Scott Arrington This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. $browser_string END $label->set_markup($markup); $label->set_justify('center'); $label->set_line_wrap(1); $about->set_title('About Pod Viewer'); $about->set_border_width(8); $about->add_buttons('gtk-help' => 'help', 'gtk-ok' => 'ok'); $about->vbox->pack_start(Gtk2::Image->new_from_stock('gtk-dialog-info', 'dialog'), 0, 0, 0); $about->vbox->pack_start($label, 1, 1, 0); $about->show_all; $about->signal_connect('response', sub { $about->destroy; if ($_[1] eq 'help') { load_help(); } }); $about->run; return 1; } sub load_help { $combo->entry->set_text($0); load(); return 1; } sub blank_status { return set_status(''); } sub set_status { my $str = shift; $status->push($status->get_context_id($str), $str); return 1; } sub go_back { unshift(@FORWARD_BREADCRUMBS, pop(@BACK_BREADCRUMBS)); # put the current into the front of the forward array my $location = pop(@BACK_BREADCRUMBS); # this is previous return undef unless ($location); $combo->entry->set_text($location); load(); return 1; } sub go_up { my $location = $combo->entry->get_text; return undef unless ($location =~ /::/); my @parts = split(/::/, $location); pop(@parts); $combo->entry->set_text(join('::', @parts)); load(); return 1; } sub go_forward { my $location = shift(@FORWARD_BREADCRUMBS); return undef unless ($location); $combo->entry->set_text($location); load(); return 1; } sub go_home { $combo->entry->set_text($CONFIG{home}); load(); return 1; } sub in_array { my ($needle, @haystack) = @_; my $in = 0; map { $in++ if ($needle eq $_) } @haystack; return ($in > 0 ? 1 : undef); } sub do_search { my $str = $search_entry->get_text; $str =~ s/^\s*$//g; if ($str eq '') { search_dialog(); return undef; } set_status('Searching...'); $window->window->set_cursor($BUSY_CURSOR); $viewer->get_window('text')->set_cursor($BUSY_CURSOR); my $doc = $viewer->get_buffer->get_text( $viewer->get_buffer->get_start_iter, $viewer->get_buffer->get_end_iter, 1 ); $str = quotemeta($str); $search_entry->set_sensitive(0); if ($str ne $LAST_SEARCH_STR) { $SEARCH_OFFSET = 0; } $LAST_SEARCH_STR = $str; for ($SEARCH_OFFSET ; $SEARCH_OFFSET < length($doc) ; $SEARCH_OFFSET++) { Gtk2->main_iteration while (Gtk2->events_pending); if (substr($doc, $SEARCH_OFFSET) =~ /^$str/i) { my $iter = $viewer->get_buffer->get_iter_at_offset($SEARCH_OFFSET); $viewer->scroll_to_iter($iter, undef, 1, 0, 0); $search_entry->set_sensitive(1); $search_entry->grab_focus(); $viewer->get_buffer->move_mark( $viewer->get_buffer->get_mark('insert'), $viewer->get_buffer->get_iter_at_offset($SEARCH_OFFSET) ); $viewer->get_buffer->move_mark( $viewer->get_buffer->get_mark('selection_bound'), $viewer->get_buffer->get_iter_at_offset($SEARCH_OFFSET + length($str)) ); blank_status(); $window->window->set_cursor($NORMAL_CURSOR); $SEARCH_OFFSET += length($str); return 1; } } $search_entry->set_sensitive(1); $search_entry->grab_focus(); blank_status(); $SEARCH_OFFSET = 0; $window->window->set_cursor($NORMAL_CURSOR); $viewer->get_window('text')->set_cursor($NORMAL_CURSOR); my $dialog = Gtk2::MessageDialog->new($window, 'modal', 'info', 'ok', "The string '$str' was not found."); $dialog->set_icon($window->get_icon); $dialog->signal_connect('response', sub { $dialog->destroy }); $dialog->run; return undef; } sub search_dialog { my $entry = Gtk2::Entry->new; my $table = Gtk2::Table->new(2, 2, 0); $table->set_col_spacings(8); $table->set_row_spacings(8); $table->attach_defaults(Gtk2::Image->new_from_stock('gtk-dialog-question', 'dialog'), 0, 1, 0, 2); $table->attach_defaults(Gtk2::Label->new('Enter search string:'), 1, 2, 0, 1); $table->attach_defaults($entry, 1, 2, 1, 2); my $dialog = Gtk2::Dialog->new; $dialog->set_icon($window->get_icon); $dialog->set_title('Find'); $dialog->set_modal(1); $dialog->add_buttons('gtk-cancel' => 1, 'gtk-ok' => 0); $dialog->vbox->set_spacing(8); $dialog->set_border_width(8); $dialog->vbox->pack_start($table, 1, 1, 0); $dialog->show_all; $entry->signal_connect('activate', sub { $dialog->signal_emit('response', 0) }); $dialog->signal_connect('response', sub { $dialog->destroy; if ($_[1] == 0 && $entry->get_text ne '') { $search_entry->set_text($entry->get_text); $search_entry->grab_focus; do_search(); } }); $dialog->run; return 1; } sub link_clicked { my (undef, $text) = @_; $text =~ s/\"$//g; $text =~ s/^\"//g; my @marks = $viewer->get_marks; my $seen = 0; map { s/^[\"\']//g ; s/[\"\']$//g ; $seen++ if (lc($_) eq lc($text)) } @marks; if ($seen > 0) { for (my $i = 0 ; $i < scalar(@marks) ; $i++) { $marks[$i] =~ s/^[\"\']//g; $marks[$i] =~ s/[\"\']$//g; if (lc($marks[$i]) eq lc($text)) { $index->select($i); return 1; } } } elsif ($text =~ /^(\w+)\:\/\//) { if ($BROWSER ne '') { system(sprintf('%s %s &', $BROWSER, quotemeta($text))); } else { my $dialog = Gtk2::MessageDialog->new($window, 'modal', 'error', 'ok', "Couldn't find a web browser. Consider setting the \$BROWSER variable."); $dialog->set_icon($window->get_icon); $dialog->signal_connect('response', sub { $dialog->destroy }); $dialog->run; } } elsif ($text =~ /^\// && ! -e $text) { $text =~ s/^\///; link_clicked(undef, $text); } elsif ($text =~ /\// && ! -e $text) { my ($doc, $section) = split(/\//, $text, 2); my $old = $combo->entry->get_text; $combo->entry->set_text($doc); if (load()) { link_clicked(undef, $section); } else { $combo->entry->set_text($old); } } else { my $old = $combo->entry->get_text; $combo->entry->set_text($text); $combo->entry->set_text($old) unless (load()); } return 1; } __END__ =pod =head1 NAME podviewer - a Gtk2-Perl POD Reading Program =head1 SYNOPSIS podviewer [FILE|MODULE|FUNCTION|POD] =head1 DESCRIPTION C provides a simple and attractive way to read Perl's POD documentation. You can use it to read the Perl POD pages, module documentation and information about Perl's builtin functions. =head1 KEYBOARD SHORTCUTS C supports a wide number of keyboard shortcuts. They are documented here. =over =item * C or C Go up a level. That is, if you're reading the documentation for C, typing C will take you to the C page. =item * C Go back in your browsing history. =item * C Go forward in your browsing history. =item * C Go home. If you have the C option set in your config (see below), entering C will take you there. =item * C This toggles the document index. =item * C This pops up a dialog allowing you to choose a document to read. =item * C or C This reloads the current document. =item * C This quits the program. =item * C This pops up a dialog for you to enter some search text. =item * C This performs the previous search, but from the most recently found result. You can also repeat the previous search by pressing enter when the cursor is in the search box. =item * C Loads this document. =back =head1 CONFIGURATION OPTIONS C stores its configuration settings in a resource file, located at C<$HOME/.podviewrc>. This file contains simple C pairs for various things: =over =item * C If you want to change the size of the icons on the toolbar, change this value. The values are standard Gtk+ stock values. =item * C This is the name of the document you want to be your home page. This page is loaded when you start the program without arguments, hit the Home button, or type C. =back =head1 SEE ALSO =over =item * L or L =item * L =item * L =back =head1 AUTHORS Gavin Brown, Torsten Schoenfeld and Scott Arrington. =head1 COPYRIGHT (c) 2003-2005 Gavin Brown (gavin.brown@uk.com). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut