# test
use Win32::GUI;
use Win32;
use Win32::File;
use Win32::DriveInfo;

# ImageLists
my $Tb1ImageList = new Win32::GUI::ImageList(16, 16, 0, 0, 10);
$Tb1ImageList->Add(new Win32::GUI::Bitmap("test.bmp"));
my $ExplorerImageList = new Win32::GUI::ImageList(16, 16, 0, 3, 3); # 0x00000020
$ExplorerImageList->Add(new Win32::GUI::Bitmap("file_folder.bmp"));
$ExplorerImageList->Add(new Win32::GUI::Bitmap("file_txt.bmp"));
$ExplorerImageList->Add(new Win32::GUI::Bitmap("file_unknown.bmp"));

my $Window = new Win32::GUI::Window (
  -name => 'Window',
  -caption => 'Test',
  -pos => [0,0],
  -size => [800,600],
  -onTerminate => sub {return -1},
  -onResize    => sub {return Window_Resize() },
);

my $Status = $Window->AddStatusBar (
  -name => 'Status',
);

my $Toolbar1 = $Window->AddToolbar (
  -name => 'Toolbar1',
  -imagelist => $Tb1ImageList,
  -style => TBSTYLE_WRAPABLE | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_LIST | TBSTYLE_TOOLTIPS,# | TBSTYLE_TOOLTIPS | TBSTYLE_WRAPABLE,
  -onButtonClick => sub {
    my ($self,$button) = @_;
    print "button $button clicked\n";
    },
);
$Toolbar1->SetExtendedStyle(TBSTYLE_EX_MIXEDBUTTONS);

$Toolbar1->AddButtons(10,
  #BITMAP, COMMAND, STATE, STYLE, STRING
  0,1,4,0 | BTNS_SHOWTEXT ,0,
  0,2,4,0,1,
  0,3,4,0,2,
  0,4,4,0,3,
  0,0,4,BTNS_SEP,0,
  0,5,4,0,4,
  0,6,4,0,5,
  0,7,4,0,6,
  0,8,4,0,7,
  0,9,4,0,8,
);
$Toolbar1->AddString("A");
$Toolbar1->AddString('b');
$Toolbar1->AddString('c');
$Toolbar1->AddString('d');
$Toolbar1->AddString('e');
$Toolbar1->AddString('f');
$Toolbar1->AddString('e');
$Toolbar1->AddString('d');
$Toolbar1->AddString('c');

# The file explorer
my $ExplorerAllFiles = 1;
my $Explorer = $Window->AddTreeView (
  -name => 'Explorer',
  -pos => [100,100],
  -size => [200,400],
  #-imagelist => $ExplorerImageList,
  -lines     => 1,
  -rootlines => 1,
  -buttons   => 1,
  -visible   => 1,
  -checkboxes => 0,
  -onExpanding => sub {
    my ($self, $node) = @_;
    # Add childs of this node
    ExplorerLoadChilds($node);
    return 1;
    },
);
#$Explorer->SetImageList($ExplorerImageList);
foreach my $drive (Win32::DriveInfo::DrivesInUse ()) {
  # add all readable devices (A:, C: etc)
  my $item = $Explorer->InsertItem (
    #-image => 0,
    -text => $drive . ":",
    -item => #0xFFFF0001, # at the beginning of the list
             0xFFFF0002, # at the end of the list
             #0xFFFF0003, # in alphabetical order
    #-parent => NUMBER        handle of the parent node for the new node
    #  -selected => 0/1, default 0
    #  -selectedimage => NUMBER   index of an image from the associated ImageList
    );
  $Explorer->InsertItem (
    #-image => 0,
    -text => ".",
    -parent => $item,
    );
}

sub ExplorerLoadChilds {
  my ($Node) = @_;

  my $first = $Explorer->GetChild($Node);
  my %info = $Explorer->ItemInfo($first);
  #  print " - child: $first $info{-text}\n";
  return 1 unless $info{-text} eq ".";

  $Explorer->Clear($Node);
  my $directory = ExplorerCreatePath($Node)."/";
  unless (-r $directory) {
    Win32::MsgBox("directory $directory is not readable");
    }
  if (-d $directory) {
    opendir(DIR, $directory) or do {
      Win32::MsgBox("Can't load directory $directory: $!.");
      return 0;
      };
    while (my $file = readdir(DIR)) {
      next unless -e "$directory/$file"; # Exists?
      next if        $file =~ /^\.+$/;   # no .. and .
      my $isDir = (-d "$directory/$file");
      my $image = 0;
      unless ($isDir) {
        $image = (-T "$directory/$file" ? 1 : 2);
        }
      my $item = $Explorer->InsertItem (
        -name => "$directory/$file",
        #-image => $image,#($isDir ? 0 : 1),
        -text => $file,
        -item => #0xFFFF0001, # at the beginning of the list
                 #0xFFFF0002, # at the end of the list
                 #0xFFFF0003,# in alphabetical order
                 ($isDir ? 0xFFFF0001 : 0xFFFF0002),
        -parent => $Node, #        handle of the parent node for the new node
        #  -selected => 0/1, default 0
        -selectedimage => $image,#   index of an image from the associated ImageList
        );
      if ($isDir) {
        $Explorer->InsertItem (
          #-image => 0,
          -text => ".",
          -parent => $item,
          );
        }
      }
    }
  return 1;
}

sub ExplorerCreatePath {
  my ($Node) = @_;
# ItemInfo(NODE)
#
# Returns an associative array of information about the given NODE:
#     -children
#     -image
#     -parent
#     -selectedimage
#     -state
#     -text

  #my @nodes = ($Node->Text());
  my %info = $Explorer->ItemInfo($Node);
  my @nodes = ($info{-text});
  my $parent = $info{-parent};
  while ($parent) {
    %info = $Explorer->ItemInfo($parent);
    print "$parent = $info{-text}\n";
    unshift @nodes, $info{-text};
    $parent = $info{-parent};
    }
  return join("/",@nodes);
}

# Run
$Window->Show();
Win32::GUI::Dialog();

# Events

sub Window_Resize {
  #print "Resize...\n";
  $Toolbar1->AutoSize();
  $Toolbar1->Change(-top => 0);

  my $h = $Window->ScaleHeight - 20;
  my $w = $Window->ScaleWidth;
  $Status->Width($w);
  $Status->Top($h);


}