#!/usr/bin/perl

use strict;
use warnings;
use vars qw ($VERSION);
$VERSION =  0.05;

my $fileopenimg = '/* XPM */
static char * fileopen_xpm[] = {
"22 22 191 2",
"  	c None",
". 	c #000000",
"+ 	c #26190E",
"@ 	c #271C11",
"# 	c #271A0F",
"$ 	c #3B2714",
"% 	c #F4AD6A",
"& 	c #FEC280",
"* 	c #FEC27F",
"= 	c #FDB773",
"- 	c #E5A566",
"; 	c #FECB93",
"> 	c #FED39A",
", 	c #FECE93",
"\' 	c #FEC88B",
") 	c #F6B675",
"! 	c #150F09",
"~ 	c #FEC385",
"{ 	c #FED59C",
"] 	c #FECE92",
"^ 	c #FCB269",
"/ 	c #F9AC62",
"( 	c #F8AC62",
"_ 	c #FEC888",
": 	c #FED199",
"< 	c #FEC282",
"[ 	c #EEA059",
"} 	c #80532C",
"| 	c #4B3119",
"1 	c #4A3019",
"2 	c #452C17",
"3 	c #402915",
"4 	c #3C2714",
"5 	c #372312",
"6 	c #322011",
"7 	c #2E1D0F",
"8 	c #291A0E",
"9 	c #25170C",
"0 	c #FECA8F",
"a 	c #FDB56E",
"b 	c #A86F3C",
"c 	c #53504D",
"d 	c #F5F5F5",
"e 	c #F7F7F7",
"f 	c #F1F1F1",
"g 	c #EAEAEA",
"h 	c #E3E3E3",
"i 	c #DDDDDC",
"j 	c #D6D6D5",
"k 	c #D0D0CE",
"l 	c #C8C8C7",
"m 	c #C2C2C1",
"n 	c #BBBBBA",
"o 	c #B4B4B3",
"p 	c #AFAFAF",
"q 	c #AEAEAE",
"r 	c #343434",
"s 	c #FEC685",
"t 	c #FEC182",
"u 	c #F2A55C",
"v 	c #E7E7E7",
"w 	c #E4E4E4",
"x 	c #E1E1E1",
"y 	c #D9D9D8",
"z 	c #D1D1D0",
"A 	c #C7C7C6",
"B 	c #C0C0BF",
"C 	c #B8B8B7",
"D 	c #B0B0AF",
"E 	c #A7A7A6",
"F 	c #9E9E9D",
"G 	c #959595",
"H 	c #8B8B89",
"I 	c #818180",
"J 	c #5E5E5D",
"K 	c #191919",
"L 	c #FEC283",
"M 	c #FBB269",
"N 	c #AB6F3B",
"O 	c #F2F2F2",
"P 	c #E2E2E1",
"Q 	c #EAEAE9",
"R 	c #EBEBEA",
"S 	c #DDDDDB",
"T 	c #D3D3D1",
"U 	c #CACAC9",
"V 	c #C1C1C0",
"W 	c #AFAFAD",
"X 	c #A5A5A3",
"Y 	c #9B9B9A",
"Z 	c #8E8E8D",
"` 	c #7B7B79",
" .	c #363635",
"..	c #FEB971",
"+.	c #EA9E58",
"@.	c #4E331B",
"#.	c #CDCDCC",
"$.	c #D5D5D4",
"%.	c #E1E1E0",
"&.	c #E9E9E8",
"*.	c #E7E7E6",
"=.	c #E5E5E3",
"-.	c #DEDEDD",
";.	c #C4C4C2",
">.	c #BABAB9",
",.	c #B1B1B0",
"\'.	c #9E9E9C",
").	c #8C8C8B",
"!.	c #6D6D6C",
"~.	c #FEB469",
"{.	c #A66D3A",
"].	c #D4D4D3",
"^.	c #E0E0DF",
"/.	c #E5E5E4",
"(.	c #D8D8D6",
"_.	c #CFCFCE",
":.	c #C0C0BE",
"<.	c #B7B7B6",
"[.	c #AEAEAD",
"}.	c #A4A4A3",
"|.	c #979796",
"1.	c #292928",
"2.	c #F3A45A",
"3.	c #ACACAC",
"4.	c #C9C9C8",
"5.	c #DCDCDB",
"6.	c #D0D0CF",
"7.	c #B9B9B8",
"8.	c #B2B2B1",
"9.	c #A9A9A8",
"0.	c #A0A09E",
"a.	c #90908F",
"b.	c #5E5E5C",
"c.	c #161615",
"d.	c #B57740",
"e.	c #C4C4C4",
"f.	c #D2D2D1",
"g.	c #CDCDCB",
"h.	c #B3B3B2",
"i.	c #ACACAB",
"j.	c #A3A3A2",
"k.	c #9A9A98",
"l.	c #818181",
"m.	c #383837",
"n.	c #472E19",
"o.	c #BDBDBB",
"p.	c #C3C3C1",
"q.	c #C7C7C5",
"r.	c #C8C8C6",
"s.	c #C6C6C5",
"t.	c #C3C3C2",
"u.	c #BFBFBE",
"v.	c #B9B9B7",
"w.	c #A5A5A4",
"x.	c #9D9D9B",
"y.	c #8D8D8C",
"z.	c #676766",
"A.	c #0B0704",
"B.	c #6D6864",
"C.	c #ADADAC",
"D.	c #B6B6B5",
"E.	c #A9A9A7",
"F.	c #A2A2A1",
"G.	c #9D9D9C",
"H.	c #919190",
"I.	c #7F7F7E",
"J.	c #2D2D2C",
"K.	c #171717",
"L.	c #919191",
"M.	c #9A9A9A",
"N.	c #999998",
"O.	c #9C9C9B",
"P.	c #9F9F9D",
"Q.	c #9A9A99",
"R.	c #939393",
"S.	c #8B8B8A",
"T.	c #8A8A88",
"U.	c #7D7D7C",
"V.	c #5A5A58",
"W.	c #141413",
"X.	c #131313",
"Y.	c #323232",
"Z.	c #2B2B2B",
"`.	c #2C2C2B",
" +	c #2D2D2D",
".+	c #2A2A2A",
"++	c #282828",
"@+	c #272727",
"#+	c #262625",
"$+	c #252524",
"%+	c #101010",
"                                            ",
"                  . . . . .                 ",
"              . . . . . . . . .   .         ",
"            . .             . . . .         ",
"          .                   . . .         ",
"                            . . . .         ",
"    + @ @ #                                 ",
"  $ % & * = $                               ",
"$ - ; > , \' ) $ ! ! ! ! ! ! ! !             ",
"$ ~ { ] & ^ / ( ( ( ( ( ( ( ( ( $           ",
"$ _ : < [ } | 1 1 1 2 3 4 5 6 7 8 9 . . . . ",
"$ _ 0 a b c d e d f g h i j k l m n o p q r ",
"$ s t u 1 l v w x y z A B C D E F G H I J K ",
"$ L M N c O P Q R h S T U V C W X Y Z `  .  ",
"$ ..+.@.#.$.%.&.*.=.-.$.#.;.>.,.E \'.).!..   ",
"$ ~.{.c -.].^./.P i (._.l :.<.[.}.|.I 1.    ",
"$ 2.1 3.4.].i 5.y $.6.4.m 7.8.9.0.a.b.c.    ",
"$ d.c _.e._.].f.6.g.l V n h.i.j.k.l.m.      ",
"$ n.3.h.o.p.q.r.s.t.u.v.h.i.w.x.y.z..       ",
"A.B.l C.h.D.v.n >.D.h.[.E.F.G.H.I.J.        ",
"K.L.M.N.O.P.P.P.x.Q.|.R.a.S.T.U.V.W.        ",
"X.Y.Z.`.J. + + +J.`.Z..+++@+#+$+%+          "};
';

use Config;
my $mswin = $Config{'osname'} eq 'MSWin32';

use Getopt::Long;
use Tk;
require Tk::Compound;
require Tk::PodViewer::Full;
require Tk::DialogBox;

#hard code defaults
my $deffont = 'Noto 10';
my $deffixed = 'Hack';
if ($mswin) {
	$deffont = 'Helvetica 10';
	$deffixed = 'Courier';
}

#option variables;
my $font;
my $fixed;
my $help = 0;
my $version = 0;
my $zoom;

#main help text
my $help_text = <<__EOF;
Usage: podviewer [options] [file_or_module_name]
Options:
-f -font:     Specify font to use.
-i -fixed:    Specify fixed font to use.
-h -help:     Displays this message.
-v -version:  Shows version and exits'. 
-z -zoom:     Specify zoom setting.

You may use the following keyboard shortcuts:
CTRL+O        Select a module or file.
CTRL+F        Show search bar.
__EOF

#application variables
my $app;
my $current = '';
my $dialog;
my $viewer;
my @bpack = (-side => 'left', -padx => 2, -pady => 2);

#load command line options
GetOptions(
	#font
	'f=s' => \$font,
	'font=s' => \$font,
	#fixed
	'i=s' => \$fixed,
	'fixed=s' => \$fixed,
	#help
	'h' => \$help,
	'help' => \$help,
	#version
	'v' => \$version,
	'version' => \$version,
	#zoom
	'z=s' => \$zoom,
	'zoom=s' => \$zoom,
) or die $help_text;

if ($help) {
	print $help_text;
	exit;
}

if ($version) {
	print "PodViewer version ", Tk::PodViewer->VERSION, "\n";
	exit;
}
my %options = ();
$fixed = $deffixed unless defined $fixed;
$options{'-fixedfontfamily'} = $fixed;
$font = $deffont unless defined $font;
$options{'-font'} = $font if defined $font;
$options{'-zoom'} = $zoom if defined $zoom;

$app = new MainWindow;
$app->after(300, sub {
	if (@ARGV) {
		my $source = shift @ARGV;
		$viewer->load($source);
	}
});

my $toolframe = $app->Frame->pack(
	-fill => 'x',
	-padx => 2,
	-pady => 2,
);

#creating the dialog box
$dialog = $app->DialogBox(
	-buttons => ['Ok', 'Cancel'],
);
my $df = $dialog->add('Frame')->pack(-expand => 1, -fill => 'both');
$df->Label(
	-text => 'Enter a module name or file name:'
)->pack(-padx => 10, -pady => 10);
my $tf = $df->Frame->pack(-fill => 'x', -padx => 10, -pady => 10);
my $entry = $tf->Entry(-textvariable => \$current)->pack(@bpack, -expand => 1, -fill => 'x');
$tf->Button(
	-image => $app->Pixmap(-data => $fileopenimg),
	-command => sub {
		my @op = ();
		@op = (-popover => $app) unless $mswin;
		my $new = $app->getOpenFile(@op);
		if (defined $new) {
			$current = $new;
			$dialog->{'selected_button'} = 'Ok';
		}
	},
)->pack(@bpack);
$dialog->configure(-focus => $entry);

#creating the viewer
$viewer = $app->PodViewerFull(%options,
	-loadcall => sub {
		my $source = shift;
		if ($source) {
			$app->configure(-title => "$source - Podviewer")
		} else {
			$app->configure(-title => 'Podviewer')
		}
	}
)->pack(-expand => 1, -fill => 'both');
$app->bind('<Control-o>', \&dialogPop);


$app->geometry('800x600+150+150');
$viewer->focus;
$app->MainLoop;

sub dialogPop {
	my $button = $dialog->Show(-popover => $app);
	$viewer->load($current) if $button eq 'Ok';
}

