#! /usr/bin/perl

# Name:			HtmlDocTest.pl
# Author:		James M. Lynes, Jr.
# Created:		January 19, 2016
# Modified By:		James M. Lynes, Jr.
# Last Modified:	January 28, 2016
# Environment:		Ubuntu 14.04LTS / perl v5.18.2 / wxPerl 3.0.1 / HP 15 Quad Core
# Change Log:		1/19/2016 - Program Created
#                       1/20/2016 - Sort the widget list, set initial pageload to index.html
#                       1/20/2016 - Change Title text
#			1/25/2016 - Reformat "ClassList1" to remove .html extension to 
#			            simplify the ListBox appearance. Test all 300+ widget filenames.
#			1/28/2016 - Fixed several widget filenames. Changed use case filename
#				    extension from .uc to .usc
#			
# Description:		Proof of Concept for a wxPerl/wxWidgets HTML Documentation Viewer.
#			The main purpose is to leverage the mass of existing wxWidgets HTML documenatation.
#                       Displays a list box of wxWidgets Classes,
#                        after a class is selected, the html file for that class is displayed,
#                        and the wxPerl use case file for the selected widget is
#			 also displayed(simulated at the moment)
#
# Notes:		Download wxWidgets 3.0.2 HTML documentation from wxwidgets.org/downloads/
#                       Path for my installation: /home/pete/Perl/wxDocs/wxWidgets-3.0.2
#                       Modify $htmlpath for your installation
#			Download the list of classes - "ClassList1" to same directory as
#			 HtmlDocTest.pl(sample attached to email)
#                       Download the Dummy.usc use case file for your installation(sample attached to email)
#
#                       Probably a directory of use case files named the same as the
#                       associated classwx files would be the easiest way to 
#                       implement the display of use cases. Would have to download
#                       this directory in addition to the html files.
#
#                       Need a "Splash.html" file for the initial display prior to a
#                       widget being selected. Change the first occurance of LoadPage
#                       to point to this file. Curently using index.html
#
# To Do:		Current widget names are in the filename format of the wxWidgets html
#			 filenaming. Provide wxWidget and wxPerl widget names. 
#			Display an image of the selected control.    
#

package main;
use strict;
use warnings;
my $app = App->new();
$app->MainLoop;

package App;
use strict;
use warnings;
use base 'Wx::App';
sub OnInit {
    Wx::InitAllImageHandlers();					# Required by Html module
    my $frame = Frame->new();
    $frame->Show(1);
}

package Frame;
use strict;
use warnings;
use Wx qw(:everything);
use base qw(Wx::Frame);
use Wx::Html;							# Not loaded by use Wx qw(:everything)
use Data::Dumper;

sub new {
    my ($class, $parent) = @_;

# Create Top Level Frame
    my $self = $class->SUPER::new($parent, wxID_ANY, "Proof of Concept wxPerl Documentation Viewer",
               wxDefaultPosition, wxDefaultSize);

    my $htmlpath = "/home/pete/Perl/wxDocs/wxWidgets-3.0.2";      # Path to wxWidgets HTML directory
    my $htmlfile = "/index.html";                                 # Initial HTML file to display

# Create Title Text
    $self->{titletext} = Wx::StaticText->new($self, wxID_ANY, "Proof of Concept wxPerl Documentation Viewer - v1/28/2016",
                         wxDefaultPosition, wxDefaultSize);

# Create HTML Window
    $self->{htmllabel} = Wx::StaticText->new($self, wxID_ANY, "Selected Class/Widget Documentation(C++ Syntax)",
                         wxDefaultPosition, wxDefaultSize); 
    $self->{htmlwindow} = Wx::HtmlWindow->new($self, wxID_ANY, wxDefaultPosition, [1000,500], wxHW_SCROLLBAR_AUTO);
    $self->{htmlwindow}->LoadPage($htmlpath.$htmlfile);            # Load initial HTML file

# Create a ListBox of wxWidgets Classes

    my $classlistpath = "ClassList1";

    my $fh;
    open($fh, "<", $classlistpath) or die "Can't open $classlistpath: $!";
    my @classes;
    while(<$fh>) {							# Read and format the widget names
        chomp;
        my($f1, $f2) = split /#/, $_;
        $f1 =~ s/\s+$//;						# Trim trailing spaces in name
        push @classes, $f1;
    }
    close($fh);

    my @sortedclasses = sort @classes;					# Sort the widget names

    $self->{listboxlabel} = Wx::StaticText->new($self, wxID_ANY, "Available Classes/Widgets", wxDefaultPosition, wxDefaultSize);
    $self->{listbox} = Wx::ListBox->new($self, wxID_ANY, wxDefaultPosition, [500,200],
                       \@sortedclasses, wxLB_SINGLE);

# Create TextCtrl for Use Case Display
    $self->{usecaselabel} = Wx::StaticText->new($self, wxID_ANY, "Use Case for Selected Class/Widget(wxPerl Syntax)",
                            wxDefaultPosition, wxDefaultSize);
    $self->{usecasetext} = Wx::TextCtrl->new($self, wxID_ANY, " ", wxDefaultPosition,
                           [500,200], wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP);
    $self->{usecasetext}->LoadFile("Dummy.usc");                   	 # Use Case file dummy

# Create Copyright Information
    $self->{copyright} = Wx::StaticText->new($self, wxID_ANY, "wxWidgets is free software licensed under the GNU GPL",
                         wxDefaultPosition, wxDefaultSize);

# Create Buttons
    $self->{quitbutton} = Wx::Button->new($self, wxID_ANY, "Quit", wxDefaultPosition, wxDefaultSize);

# Define Sizer Structure - My "Standard" Layout
# Assumes: One Main Sizer(Vertical)
#          One Header Sizer(Horizontal)
#	   One Body Sizer(Horizontal) containing
#              Left Body Sizer(Vertical)
#              Right Body Sizer(Vertical)
#          Three Footer Sizers(Horizontal)
#

# Create Sizers
    my $mainSizer = Wx::BoxSizer->new(wxVERTICAL);
    $self->SetSizer($mainSizer);

    my $headerSizer = Wx::BoxSizer->new(wxHORIZONTAL);
    my $bodySizer = Wx::BoxSizer->new(wxHORIZONTAL);
    my $leftbodySizer = Wx::BoxSizer->new(wxVERTICAL);
    my $rightbodySizer = Wx::BoxSizer->new(wxVERTICAL);
    my $footer1Sizer = Wx::BoxSizer->new(wxHORIZONTAL);
    my $footer2Sizer = Wx::BoxSizer->new(wxHORIZONTAL);
    my $footer3Sizer = Wx::BoxSizer->new(wxHORIZONTAL);

# Layout Main Sizer
    $mainSizer->Add($headerSizer,0,0,0);
    $mainSizer->AddSpacer(20);
    $mainSizer->Add($bodySizer,0,0,0);
    $mainSizer->AddSpacer(30);
    $mainSizer->Add($footer1Sizer,0,0,0);
    $mainSizer->AddSpacer(10);
    $mainSizer->Add($footer2Sizer,0,0,0);
    $mainSizer->AddSpacer(10);
    $mainSizer->Add($footer3Sizer,0,0,0);

# Layout Header Sizer
    $headerSizer->AddSpacer(350);
    $headerSizer->Add($self->{titletext},0,0,0);

# Layout Body Sizer
    $bodySizer->Add($leftbodySizer,0,0,0);
    $bodySizer->AddSpacer(50);
    $bodySizer->Add($rightbodySizer,0,0,0);

# Layout Left and Right Body Sizers
    $leftbodySizer->Add($self->{listboxlabel},0,0,0);
    $leftbodySizer->Add($self->{listbox},0,0,0);
    $leftbodySizer->AddSpacer(20);
    $leftbodySizer->Add($self->{usecaselabel},0,0,0);
    $leftbodySizer->Add($self->{usecasetext},0,0,0);
    $rightbodySizer->Add($self->{htmllabel},0,0,0);
    $rightbodySizer->Add($self->{htmlwindow},0,0,0);

# Layout Footer Sizers
    $footer1Sizer->Add($self->{quitbutton},0,0,0);
#    $footer2Sizer->Add($self->{},0,0,0);
    $footer3Sizer->Add($self->{copyright},0,0,0);

# Assign mainSizer to the Frame and trigger layout

    $mainSizer->Fit($self);
    $mainSizer->Layout();

# Event Handlers

    Wx::Event::EVT_BUTTON($self, $self->{quitbutton}, sub {
			  my ($self, $event) = @_;
			  $self->Close;
			  });

    Wx::Event::EVT_LISTBOX($self, $self->{listbox}, sub {
			my ($self, $event) = @_;
			my $selection = $self->{listbox}->GetSelection;		# Index Number
                        my $string = $self->{listbox}->GetString($selection);	# String at Index Number
                        $self->{htmlwindow}->LoadPage($htmlpath.lc("/class".$string.".html"));  # Load the selected html page
#			print Dumper $htmlpath.lc("/class".$string);		# Debug 
			});


    return $self;
}
1;

