| 1 | package App::Codit::Plugins::PodViewer; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Codit::Plugins::PodViewer - plugin for App::Codit | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw( $VERSION ); | ||
| 12 | $VERSION = '0.21'; | ||
| 13 | |||
| 14 | use base qw( App::Codit::BaseClasses::TextModPlugin ); | ||
| 15 | |||
| 16 | require Tk::PodViewer::Full; | ||
| 17 | use Tk; | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Add a Perl pod viewer to your open files. | ||
| 22 | |||
| 23 | =head1 DETAILS | ||
| 24 | |||
| 25 | PodViewer adds a I<Pod> button to the toolbar. | ||
| 26 | When you click it the frame of the current selected document | ||
| 27 | will split and the bottom half will show the pod documentation | ||
| 28 | in your document. | ||
| 29 | |||
| 30 | The viewer is refreshed after you make an edit. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | sub new { | ||
| 35 | my $class = shift; | ||
| 36 | my $self = $class->SUPER::new(@_); | ||
| 37 | return undef unless defined $self; | ||
| 38 | |||
| 39 | $self->{ADJUSTER} = undef; | ||
| 40 | $self->{DOCS} = {}; | ||
| 41 | $self->{MODIFIEDSAVE} = {}; | ||
| 42 | $self->{VIEWER} = undef; | ||
| 43 | $self->{VISIBLE} = 0; | ||
| 44 | |||
| 45 | my $page = $self->ToolBottomPageAdd('Pod', 'documentation', undef, 'Show the documentation in your file'); | ||
| 46 | $self->sidebars->pageSelectCall('Pod', sub { | ||
| 47 | $self->after(10, sub { $self->Refresh }) | ||
| 48 | }); | ||
| 49 | my $art = $self->extGet('Art'); | ||
| 50 | |||
| 51 | #Getting the correct font | ||
| 52 | my $l = $page->Label; | ||
| 53 | my $lfont = $l->cget('-font'); | ||
| 54 | my $family = $l->fontActual($lfont, '-family'); | ||
| 55 | my $size = $l->fontActual($lfont, '-size'); | ||
| 56 | my $font = $l->Font(-family => $family, -size => $size); | ||
| 57 | $l->destroy; | ||
| 58 | |||
| 59 | #creating the viewer widget | ||
| 60 | my @vopt = (); | ||
| 61 | for ( | ||
| 62 | ['-nextimage', 'go-next', 'Next'], | ||
| 63 | ['-previmage', 'go-previous', 'Previous'], | ||
| 64 | ['-zoominimage', 'zoom-in', 'Zoom in'], | ||
| 65 | ['-zoomoutimage', 'zoom-out', 'Zoom out'], | ||
| 66 | ['-zoomresetimage', 'zoom-original', 'Zoom reset'], | ||
| 67 | ) { | ||
| 68 | my ($opt, $icon, $text) = @$_; | ||
| 69 | my $img = $art->getIcon($icon, 22); | ||
| 70 | push @vopt, $opt, $art->createCompound( | ||
| 71 | -image => $img, | ||
| 72 | -text => $text, | ||
| 73 | ) if defined $img; | ||
| 74 | } | ||
| 75 | my $pod = $page->PodViewerFull(@vopt, | ||
| 76 | -font => $font, | ||
| 77 | -linkcolor => $self->configGet('-linkcolor'), | ||
| 78 | )->pack(-expand => 1, -fill => 'both'); | ||
| 79 | my $sb = $self->sidebars; | ||
| 80 | $sb->pageSelectCall('Pod', sub { $self->after(100, ['Refresh', $self]) }); | ||
| 81 | $self->{PODWIDGET} = $pod; | ||
| 82 | $self->zoomLoad; | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub _visible { | ||
| 89 | my $self = shift; | ||
| 90 | return $self->{PODWIDGET}->ismapped; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub CanQuit { | ||
| 94 | my $self = shift; | ||
| 95 | $self->zoomSave; | ||
| 96 | return 1 | ||
| 97 | } | ||
| 98 | |||
| 99 | sub docBefore { | ||
| 100 | my $self = shift; | ||
| 101 | my ($name ) = @_; | ||
| 102 | if (defined $name) { | ||
| 103 | $self->{DOCNAME} = $name; | ||
| 104 | } | ||
| 105 | return @_; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub ReConfigure { | ||
| 109 | my $self = shift; | ||
| 110 | my $pod = $self->{PODWIDGET}; | ||
| 111 | $pod->configure(-linkcolor => $self->configGet('-linkcolor')); | ||
| 112 | $pod->configureTags; | ||
| 113 | return 1 | ||
| 114 | } | ||
| 115 | |||
| 116 | sub Refresh { | ||
| 117 | my $self = shift; | ||
| 118 | $self->SUPER::Refresh; | ||
| 119 | |||
| 120 | return unless $self->_visible; | ||
| 121 | |||
| 122 | my $widg = $self->mdi->docWidget; | ||
| 123 | return unless defined $widg; | ||
| 124 | |||
| 125 | my $text = $widg->get('1.0', 'end -1c'); | ||
| 126 | my $pod = $self->{PODWIDGET}; | ||
| 127 | my $podtxt = $pod->Subwidget('txt'); | ||
| 128 | my ($v) = $podtxt->yview; | ||
| 129 | $pod->load(\$text); | ||
| 130 | $podtxt->yviewMoveto($v); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub Unload { | ||
| 134 | my $self = shift; | ||
| 135 | $self->ToolBottomPageRemove('Pod'); | ||
| 136 | return $self->SUPER::Unload | ||
| 137 | } | ||
| 138 | |||
| 139 | sub zoomLoad { | ||
| 140 | my $self = shift; | ||
| 141 | my $file = "podviewer_zoom"; | ||
| 142 | my $cff = $self->extGet('ConfigFolder'); | ||
| 143 | my $widg = $self->{PODWIDGET}; | ||
| 144 | my ($zoom) = $cff->loadList($file, 'podviewer_zoom'); | ||
| 145 | $widg->configure('-zoom', $zoom) if defined $zoom; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub zoomSave { | ||
| 149 | my $self = shift; | ||
| 150 | my $file = "podviewer_zoom"; | ||
| 151 | my $cff = $self->extGet('ConfigFolder'); | ||
| 152 | my $widg = $self->{PODWIDGET}; | ||
| 153 | my $zoom = $widg->cget('-zoom'); | ||
| 154 | $cff->saveList($file, 'podviewer_zoom', ($zoom)); | ||
| 155 | } | ||
| 156 | |||
| 157 | =head1 LICENSE | ||
| 158 | |||
| 159 | Same as Perl. | ||
| 160 | |||
| 161 | =head1 AUTHOR | ||
| 162 | |||
| 163 | Hans Jeuken (hanje at cpan dot org) | ||
| 164 | |||
| 165 | =head1 TODO | ||
| 166 | |||
| 167 | =over 4 | ||
| 168 | |||
| 169 | =back | ||
| 170 | |||
| 171 | =head1 BUGS AND CAVEATS | ||
| 172 | |||
| 173 | If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>. | ||
| 174 | |||
| 175 | =head1 SEE ALSO | ||
| 176 | |||
| 177 | =over 4 | ||
| 178 | |||
| 179 | =back | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 |