
# Tels: removed package stuff to get it to work. You either have to make a 
# complete package from it (including new) or drop it and include it flat 
# into main like it now is.
# Now $self refers to main, and when you make that a package here, then $self
# wont work any longer.

use vars qw($VERSION);
$VERSION = "0.0.3";

use Data::Dumper;
use Tk;
use strict;

use Cowed::Project;
use Cowgen::VWorld::Room;
use base 'Tk::Toplevel';

Construct Tk::Widget 'AddSplitCell';

sub Populate {
	my ($self, $args) = @_;

	$self->ConfigSpecs(
			-do_func	=> [ qw/PASSIVE		doFunc		doFunc/,	undef],
			-do_what	=> [ qw/PASSIVE		doWhat		doWhat/,	undef],
			-project	=> [ qw/PASSIVE		project		project/,	undef]
	);

	$self->do_func( delete $args->{-do_func} )	if exists( $args->{-do_func} );
	$self->do_what( delete $args->{-do_what} )	if exists( $args->{-do_what} );
	$self->project( delete $args->{-project} )	if exists( $args->{-project} );

	$self->SUPER::Populate($args);

	my $f;
	my $proj = ${ $self->project };

	my $num = 0;
	my $sizes = "";
	my $names = "";
	my $sb;
	my $split_dir;

	$f = $self->Frame->grid( qw/-sticky nwe/ );

	if ( $self->do_what eq "split" ) {
		$split_dir = "needs data";

		$f->Label( -text => 'Split in which coord:' )->grid(qw/-row 0 -column 0/);
		$f->Radiobutton( qw/-text X/, -value => inX(), -variable, \$split_dir )->grid( qw/-row 0 -column 1/ );
		$f->Radiobutton( qw/-text Y/, -value => inY(), -variable, \$split_dir )->grid( qw/-row 0 -column 2/ );
# XXX For later
#		$f->Radiobutton( qw/-text Z/, -value => inZ(), -variable, \$split_dir )->grid( qw/-row 0 -column 3/ );

		$self->split_dir( \$split_dir );
	} else {
		$f->Label( -text => "Current number of subcells:" )->grid( qw/-row 0 -column 0/ );
		$f->Label( -text => $#{ $self->project->getProperty("cells") } )->grid( qw/-row 0 -column 1/ );
	}

	$f->Label( -text => ($self->do_what eq "split" ? 'Split into' : 'Add') . ' how many:'
				)->grid( qw/-row 1 -column 0/ );
	$f->Entry( -textvariable => \$num, -width => 20 )->grid( qw/-row 1 -column 1 -columnspan 2/ );

	$f->Label( -text => 'Sizes, comma-delimited:' )->grid( qw/-row 2 -column 0/ );
	$f->Entry( -textvariable => \$sizes, -width => 20 )->grid( qw/-row 2 -column 1 -columnspan 2/ );

	$f->Label( -text => 'Names, comma-delimited:' )->grid( qw/-row 3 -column 0/ );
	$f->Entry( -textvariable => \$names, -width => 20 )->grid( qw/-row 3 -column 1 -columnspan 2/ );

	$f = $self->Frame->grid( qw/-row 4 -sticky s/ );

	$f->Button( -text => ucfirst($self->do_what), -command =>
				sub{ &validate($self, $num, $sizes, $names, $sb, $split_dir) })->grid( qw/-row 0 -column 0/ );

	$f->Button( qw/-text Cancel -command/, sub{ $self->destroy })->grid( qw/-row 0 -column 1/ );
	$f->Button( qw/-text Foo -command/, sub{ exit})->grid( qw/-row 0 -column 2/ );

	$sb = $self->Label( qw/-relief groove -text/, "")->grid( qw/-row 5 -sticky ew/ );

	$self->resizable(0,0);
	$self->focus;
	$self->grab;

	return $self;
}

sub do_func {
	my $self = shift || return;

	$self->{SaveLoad_opts}->{do_func} = shift() if defined($_[0]);

	return $self->{SaveLoad_opts}->{do_func};
}

sub do_what {
	my $self = shift || return;

	$self->{SaveLoad_opts}->{do_what} = shift() if defined($_[0]);

	return $self->{SaveLoad_opts}->{do_what};
}

sub project {
	my $self = shift || return;

	$self->{SaveLoad_opts}->{project} = shift() if defined($_[0]);

	return $self->{SaveLoad_opts}->{project};
}

sub split_dir {
	my $self = shift || return;

	$self->{SaveLoad_opts}->{split_dir} = shift() if defined($_[0]);

	return $self->{SaveLoad_opts}->{split_dir};
}

########################################################################
# subs to deal with validating data into the $dirdata window.
########################################################################

sub validate {
	my ($self, $num, $sizes, $names, $sb, $split_dir) = @_;

	if ( $split_dir eq "needs data" ) {
		return &StatusBar("Must specify a direction to split in.", 'red', $sb, $self);
	}

	if ( $num !~ s/^\s*(\d+)\s*$/$1/ ) {
		return &StatusBar("Must specify only a number for 'how many'.", 'red', $sb, $self);
	}

	# Calculate how many this would bring us to, total.
	$num += $#{ ${$self->project}->getProperty("cells") } + 1;

	# Then check to make sure they're not cheating.
	if ( 1 > $num || $num > 64 ) {
		return &StatusBar("Total num of subcells must be between 1 & 64, inclusive", 'red', $sb, $self);
	}

	my $func = $self->do_func;

	&{ $func }($num, $sizes, $names, $split_dir);

	$self->destroy;
}

######################################################################
# Funcs to pop up miscellaneous messages on the status bar & to clear them.
######################################################################

sub StatusBar {
	my ($msg, $color, $buffer, $self);

	if ( $#_ == 3 ) {
		($msg, $color, $buffer, $self) = @_;
	} else {
		print "Incorrect use of &StatusBar!\n";
		return;
	}

	chomp($msg);

	# 1st off, clear all bindings on '<Motion>'...
	$self->bind('all', '<Motion>', '');

	$buffer->configure( '-text' => $msg );
	$buffer->configure( '-foreground' => $color ) if defined($color);

	# Get the time as it is right this second.
	my $now = time();

	$self->bind('all', '<Motion>', sub{ ClearStatusBar($now, $buffer, $self) } );
}

sub ClearStatusBar {
	my ($then, $buffer, $self) = @_;

	if ( (time() - $then) >= 3 ) {

		# If 3 seconds have passed, unbind the '<Motion>' binding...
		$self->bind('all', '<Motion>', '');

		# And clear the status bar.
		$buffer->configure( '-text' => "" );
		$buffer->configure( '-foreground' => 'red' );
	}
}

1;
