# Sudoku Show
# 02/08/2006
# Partially based on demo code by Steve Lidie, April 1999, on the PerlTK.org website
# http://www.perltk.org/index.php?option=com_content&task=view&id=15&Itemid=28

#use 5.005; 
#use Tk 8.0; 
use Tk;
use Tk::widgets qw/Dialog/; 
use Text::ParseWords;
use subs qw/build_menubar build_box main/; 
use vars qw/$SudokuShow $VERSION @Sudoku/; 
use strict; 

$SudokuShow = MainWindow->new;
print "@_\nor else not";
main(@_); 
MainLoop; 

sub main {		# Where the action happens

    $VERSION = '0.0.3 Alpha'; 	# What version I'm on

    $SudokuShow->title("Sudoku Show"); 
    $SudokuShow->iconname('S'); 
    my $bmp = $SudokuShow->Photo(-file => "s.bmp", -format => 'bmp');		# A nifty little corner-icon thingy
    $SudokuShow->Icon(-image => $bmp);

#	my $fileName = @_;						# I haven't gotten the hang of passing arguments into the file yet
	my $fileName = "sudoku.txt";			# So I've hard-coded the filename in.
	open (input, $fileName) or die "\nCan't open $fileName : $!";
	my @slurp = <input>;					# I had to try it.
	my @Sudoku = nested_quotewords(" ", 0, @slurp);		# When I get a better handle on regular expressions, the input file can be more flexible than single space delimiting.
#	@Sudoku = (	[1,2,3,4,5,6,7,8,9],		# A full Sudoku, for testing without inputs.
#				[4,5,6,7,8,9,1,2,3],
#				[7,8,9,1,2,3,4,5,6],
#				[2,3,4,5,6,7,8,9,1],
#				[5,6,7,8,9,1,2,3,4],
#				[8,9,1,2,3,4,5,6,7],
#				[3,4,5,6,7,8,9,1,2],
#				[6,7,8,9,1,2,3,4,5],
#				[9,1,2,3,4,5,6,7,8]);
#	@Sudoku = (	[11,12,13],		# A small test grid to get the GUI working
#				[14,15,16],
#				[17,18,19]);
	my $i;
	for $i (0..$#Sudoku) {		# Once I've got things working, I may split this into 9 pieces to get better spacing.
		for (0..$#{@Sudoku[$i]}) {
			print " $Sudoku[$i][$_]";
			my $box = build_box($Sudoku[$i][$_],$i,$_);
#			$box->place(-x => $i, -y => $_);
		}
	}
    my $menubar = build_menubar; 
#    my $frame = $SudokuShow->Frame(qw/-width 500 -height 500 -background cyan/)->pack; 

}

sub build_box {		# Creates a label widget for the number
	my $num = shift;
	my $yPos = shift;
	my $xPos = shift;
#	$xPos = $xPos*10;			# For when I tried place
#	$yPos = $yPos*10;
#	print "[$xPos][$yPos]=$num ";
	my $box = $SudokuShow->Label(-text => $num,
								-background => 'white',
								-relief => 'groove',	# The border of the boxes
								-height => 5,
								-width => 10);	# Size and color of the boxes
#	$box->pack(-fill => 'both');
#	$box->pack;					# Pack just doesn't do it
	$box->grid(	-column => $xPos,	# Positional Data
				-row => $yPos,
				-ipadx => 1,		# I'm trying to put a border on the boxes here, but groove did it better
				-ipady => 1);
#	$box->place(-in => $SudokuShow,
#				-x => $xPos);		# Positional data
#				-y => $yPos);
#	$box->grid(-column => 1
#			-row => 1);
	$box;						# To return something, and because I was trying to map it outside the subroutine for a while.
}

sub build_menubar {		# It builds the menu bar.  It will be important later, and it was useful practice.

    my $menubar = $SudokuShow->Menu; 
    $SudokuShow->configure(-menu => $menubar); 
    my $file = $menubar->cascade(-label => '~File', -tearoff => 0);		# I don't care for the tearoff option.  It's ugly.
    my $help = $menubar->cascade(-label => '~Help', -tearoff => 0); 

    $file->command(-label => "Quit", -command => \&exit);	# In case somebody doesn't want to use the corner x.

    $help->command(-label => 'Version');	# Version info
    $help->separator; 
    $help->command(-label => 'About');		# What it does

    my $version_dialog =   $SudokuShow->Dialog(-title   => 'SudokuShow Version', 
                                -text    => "SudokuShow\n\nVersion $VERSION",		#Reads the version info and displays it
                                -buttons => ['OK'], 
                                -bitmap  => 'info'); 
    my $about_dialog = $SudokuShow->Dialog(-title   => 'About SudokuShow', 
                                -text    => 'This program will someday be able to show sudoku puzzles.',	# I sure hope so.
                                -buttons => ['OK']); 
    my $menu = $help->cget('-menu'); 
    $menu->entryconfigure('Version', -command => [$version_dialog   => 'Show']); 
    $menu->entryconfigure('About',   -command => [$about_dialog => 'Show']); 

    $menubar;

}