package Perlbox::perlboxTK::pFileDialog::pFileSel;
use Tk;
use strict;
use Perlbox::perlboxTK::pFileDialog::pbFileTable;

use vars qw( $VERSION @ISA) ;

$VERSION = '0.1.x' ;

@ISA = qw( Tk::Toplevel ) ;


require Tk::Toplevel ;

@ISA = qw( Tk::Toplevel ) ;

Construct Tk::Widget 'pFileSel' ;


   #declare vars
   my($cmdUp,$cmdRe,$cmdHome,$cmdApply,$cmdCancel);
   my($txtFile, $lblFile, $lblFilter, $txtFilter);
   my $showhidden=0;
   my($cmd,$homeDir, $filesel, $filtersel, $fTable);
   my $textVar;
   my $mw;
   my $original_value_var;
   return 1;


sub InitObject(){
       my $args;
       ($mw, $args) = @_;
        $mw->SUPER::Populate($args);
	$textVar= delete $args->{"-var"};
	$original_value_var=$$textVar;
    $filesel=$$textVar;
   chdir();

   #start at home directory
   $mw->geometry("400x325+10+10");
   $mw->title("perlBox fileSelector");

    #make top row of buttons using 'place()' geometry
    $cmdUp=  $mw->Button(-text=>"UP", -relief=>"groove", -command => sub{&myUp;})->
                place(-y=>10,-x=>10,-width=>90, -height=>25);
    $cmdHome=$mw->Button(-text=>"HOME", -relief=>"groove" ,-command=>sub{&myHome})->
                place(-y=>10,-x=>100,-width=>90, -height=>25);
    $cmdRe=  $mw->Button(-text=>"REFRESH", -relief=>"groove",-command=>sub{&myRe})->
                place(-y=>10,-x=>190,-width=>90, -height=>25);
my $chk_showgrid=$mw->Checkbutton(-text=>"HIDDEN?", -variable=>\$showhidden, -indicatoron=>1, -relief=>"groove" )->
                place(-y=>10,-x=>280, -width=>90, -height=>25);
    #here is our pbFileTable()
    $fTable=$mw->pbFileTable()->
       		place(-y=>40,-x=>5,-width=>390,-height=>200);

    #now the bottom rows
    $lblFile=$mw->Label(-text=>"SELECTED:", -anchor=>"w")->
                place(-y=>250,-x=>5,-width=>100);
    $txtFile=$mw->Entry(-textvariable=>\$filesel)->
                place(-y=>250,-x=>100,-width=>295);
    $lblFile=$mw->Label(-text=>"FILTER:", -anchor=>"w")->
                place(-y=>275,-x=>5,-width=>100);
    $txtFilter=$mw->Entry(-textvariable=>\$filtersel)->
                place(-y=>275,-x=>100,-width=>150);

    $cmdApply= $mw->Button(-text=>"APPLY", -relief=>"groove", -command => sub{&myApply;})->
                  place(-y=>275,-x=>260,-width=>70);
    $cmdCancel=$mw->Button(-text=>"CANCEL", -relief=>"groove", -command => sub{&myCancel;})->
                  place(-y=>275,-x=>330,-width=>70);


    $filtersel="*";

    $fTable->setCurrentDir($filesel,\$filesel);

    $fTable->medList($showhidden);

    return 1;
}
###############CALLBACK#SUBS###CALLBACK#SUBS###CALLBACK#SUBS##################

#move up one directory
sub myUp(){
  $fTable->goUp();
  $fTable->medList($showhidden);
}

#goto users home directory
sub myHome(){
  $filesel=$ENV{"HOME"}."/";
  $fTable->setCurrentDir($filesel,\$filesel);
  $fTable->medList($showhidden);
}

#refresh the view in the current directory
sub myRe(){
  $fTable->setCurrentDir($filesel,\$filesel);
  $fTable->medList($showhidden);
}

sub myApply(){
   $$textVar=$filesel;
   $mw->destroy;
}

sub myCancel(){
   $$textVar=$original_value_var;
   $mw->destroy;
}




