http://qs321.pair.com?node_id=15641
Category: Win32::GUI
Author/Contact Info Jonathan Southwick Hydro jsouthwi@alleg.edu Technical and Network Services Allegheny Col
Description: This cool utility is included as a mini-demonstation of
what is possible with Perl on Win32 using Win32::GUI.
This code, the associated database and bitmap may be downloaded from here
If anyone can figure out why the "Stop" button does not
work properly or how to read the individual items of each
column based on the index item clicked on then Jonathan would appreciate the input.
Also Jonathan will be happy to answer anybody's questions
about the code if he can.
"It was done on a trial and error basis ... definitely not written off the cuff and then worked properly.
It required some rewrites of course. ;] Anyway, have fun with the code and any experimenting you want to do on it."
use Win32::GUI;
use Win32::Internet;
require Win32::Sound;
require Mail::Sender;

($DOShwnd, $DOShinstance) = GUI::GetPerlWindow();
#GUI::CloseWindow($DOShwnd);
#GUI::Hide($DOShwnd);

my $screen_width = Win32::GUI::GetSystemMetrics(0);
my $screen_height = Win32::GUI::GetSystemMetrics(1);
my $minwidth = 600;
my $minheight = 240;


if (@ARGV[0] eq "-t") {
   $test = 1;
}

my $dbv_icon = new Win32::GUI::Icon("gator.ico");

my $dbv_class = new Win32::GUI::Class(
       -name => "DatabaseViewer Class",
       -icon => $dbv_icon,
);

my $DataMenu = new Win32::GUI::Menu(
    "&File" => "File",
    "   >   Retrieve &Data" => "GetData",
    "   >   -" => 0,
    "   >   E&xit" => "FileExit",
    "&Search" => "Search",
    "   >   Find &Name" => "FindName",
    "   >   Find &Building" => "FindBuilding",
    "   >   Find &Adapter" => "FindAdapter",
    "&Tools" => "Tools",
    "   >   Report &Problem" => "ReportProblem",
);

$DataWindow = new Win32::GUI::Window(
    -name   => "DataWindow",
    -top    => ($screen_width - $minwidth)/2,
    -left   => ($screen_height - $minheight)/2,
    -width  => $minwidth,
    -height => $minheight,
    -title  => "GatorNet Database Information",
    -menu   => $DataMenu,
    -class  => $dbv_class,
);

$Status = $DataWindow->AddStatusBar(
    -name   => "Status",
    -text   => "GatorNet Database : Ready",
);

$DataTab = $DataWindow->AddTabStrip(
    -left   => 10,   
    -top    => 10, 
    -width  => $DataWindow->ScaleWidth - 105, 
    -height => 175,
    -name   => "DataTab",
);

$DataTab->InsertItem(-text => "Name");
$DataTab->InsertItem(-text => "Building");
$DataTab->InsertItem(-text => "Adapter");

$DataWindow->AddLabel(
    -name   => "FN_Label",
    -text   => "First Name: ",
    -top    => 55,
    -left   => 25,
);

$FirstName = $DataWindow->AddTextfield(
    -top    => 52,
    -left   => 90,
    -width   => 115,
    -height  => 23,
    -tabstop => 1,
);

$DataWindow->AddLabel(
    -name   => "LN_Label",
    -text   => "Last Name: ",
    -top    => 85,
    -left   => 25,
);

$LastName = $DataWindow->AddTextfield(
    -top    => 82,
    -left   => 90,
    -width   => 115,
    -height  => 23,
    -tabstop => 1,
);

$DataWindow->AddLabel(
    -name   => "Bld_Label",
    -text   => "Building: ",
    -top    => 55,
    -left   => 25,
);

$Building = $DataWindow->AddCombobox( 
    -name   => "Dropdown",
    -top    => 52,
    -left   => 90,
    -width   => 125,
    -height  => 110,
    -tabstop => 1,
    -style => WS_VISIBLE | 3 | WS_VSCROLL | WS_TABSTOP,
);

$Building->InsertItem("Baldwin");
$Building->InsertItem("Brooks");
$Building->InsertItem("Caflish");
$Building->InsertItem("College Court");
$Building->InsertItem("Crawford");
$Building->InsertItem("Edwards");
$Building->InsertItem("PKP");
$Building->InsertItem("Ravine");
$Building->InsertItem("Schultz");
$Building->InsertItem("South Highland");
$Building->InsertItem("Walker");
$Building->InsertItem("Walker Annex");

$DataWindow->AddLabel(
    -name   => "Adapt_Label",
    -text   => "Adapter: ",
    -top    => 55,
    -left   => 25,
);

$Adapter = $DataWindow->AddTextfield(
    -top    => 52,
    -left   => 90,
    -width   => 115,
    -height  => 23,
    -tabstop => 1,
);

$FindNow = $DataWindow->AddButton(
    -name   => "FindNow",
    -text   => "F&ind Now",
    -top    => 30,
    -left   => $DataWindow->ScaleWidth - 83,
    -width  => 71,
    -height => 25,
);

$Stop = $DataWindow->AddButton(
    -name   => "Stop",
    -text   => "Sto&p",
    -top    => 60,
    -left   => $DataWindow->ScaleWidth - 83,
    -width  => 71,
    -height => 25,
);

$NewSearch = $DataWindow->AddButton(
    -name   => "NewSearch",
    -text   => "Ne&w Search",
    -top    => 90,
    -left   => $DataWindow->ScaleWidth - 83,
    -width  => 71,
    -height => 25,
);

$DataView = new Win32::GUI::ListView($DataWindow,
    -name   => "DataView",
    -top    => $DataWindow->ScaleHeight,
    -left   => 0,
    -width  => $DataWindow->ScaleWidth,
    -height => $DataWindow->ScaleHeight - 213,
);

$DataView->InsertColumn(
    -name   => "Col1",
    -inndex => 0,
    -width  => 95,
    -text   => "First Name",
);

$DataView->InsertColumn(
    -index  => 1,
    -subitem=> 1,
    -width  => 95,
    -text   => "Last Name",
);

$DataView->InsertColumn(
    -index  => 2,
    -subitem=> 1,
    -width  => 100,
    -text   => "Building",
);

$DataView->InsertColumn(
    -index  => 3,
    -subitem=> 1,
    -width  => 55,
    -text   => "Room",
);

$DataView->InsertColumn(
    -index  => 4,
    -subitem=> 1,
    -width  => 114,
    -text   => "Adapter Address",
);

$DataView->InsertColumn(
    -index  => 5,
    -subitem=> 1,
    -width  => 117,
    -text   => "IP Address",
);

$Report = new Win32::GUI::Window(
    -name   => "Report",
    -top    => 100,
    -left   => 100,
    -width  => $minwidth - 50,
    -height => $minheight + 100,
    -title  => "Report Problem",
    -class  => $dbv_class,
);

$Report->AddLabel(
    -name   => "YN_Label",
    -text   => "Your Name: ",
    -top    => 5,
    -left   => 5,
);

$YourName = $Report->AddTextfield(
    -name   => "YourName",
    -top    => 2,
    -left   => 70,
    -width  => 300,
    -height => 23,
    -tabstop=> 1,
);

$ReportMessage = $Report->AddRichEdit(
    -name    => "Text",
    -text    => $text,
    -left    => 2, 
    -top     => 27,
    -width   => $Report->ScaleWidth - 5, 
    -height  => $Report->ScaleHeight - 65,
    -style   => WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULT
+ILINE | ES_AUTOVSCROLL,
    -exstyle => WS_EX_CLIENTEDGE,
);

$Send = $Report->AddButton(
    -name   => "Send",
    -text   => "&Send",
    -top    => $Report->ScaleHeight - 30,
    -left   => ($Report->ScaleWidth - 71)/2,
    -width  => 71,
    -height => 25,
);


$DataView->Hide();
$Status->Hide();
$Building->Select(-1);
#$FindNow->Disable();
$Stop->Disable();
$NewSearch->Disable();
$DataTab->Select(0);
&DataTab_Click;
$resized = 0;

$DataWindow->Show();
Win32::GUI::Dialog();

sub GetData_Click {
   $FindNow->Enable();
   $NewSearch->Enable();
   if ($test != 1) {
      $connect = new Win32::Internet();
      $connect->FTP($FTP, "server", "userid", "password");
      $FTP->Cd("directory");
      $FTP->Ascii();
      $FTP->Get("filename");
      ($FTPErrNumb, $FTPErrText) = $FTP->Error();
      $FTP->Close();
   }
   $dataretrieved = 1;
}

sub FileExit_Click {
   exit(0);
}

sub FindName_Click {
   $DataTab->Select(0);
   &DataTab_Click;
}

sub FindBuilding_Click {
   $DataTab->Select(1);
   &DataTab_Click;
}

sub FindAdapter_Click {
   $DataTab->Select(2);
   &DataTab_Click;
}

sub ReportProblem_Click {
   $Send->Disable();
   $Report->Show();
   $Report->BringToFront();
   $ReportMessage->Text("");
   $YourName->Text("");
}

sub DataWindow_Resize {
   $DataView->Resize($DataWindow->ScaleWidth, $DataWindow->ScaleHeight
+ - 213);
   $Status->Resize($DataWindow->ScaleWidth, $Status->Height);
   $Status->Move(0, $DataWindow->ScaleHeight-$Status->Height);
   $DataTab->Resize($DataWindow->ScaleWidth - 105, 175);
   $FindNow->Move($DataWindow->ScaleWidth - 83, 30);
   $Stop->Move($DataWindow->ScaleWidth - 83, 60);
   $NewSearch->Move($DataWindow->ScaleWidth - 83, 90);
}

sub Report_Resize {
   $ReportMessage->Resize($Report->ScaleWidth - 5, $Report->ScaleHeigh
+t - 65);
   $Send->Move(($Report->ScaleWidth - 71)/2, $Report->ScaleHeight - 30
+);
}

sub DataTab_Click {
   if ($DataTab->
   SelectedItem == 0) {
      $DataWindow->Bld_Label->Hide();
      $Building->Hide();
      $DataWindow->Adapt_Label->Hide();
      $Adapter->Hide();
      $DataWindow->FN_Label->Show();
      $FirstName->Show();
      $DataWindow->LN_Label->Show();
      $LastName->Show();
      $FirstName->SetFocus();
   }
   if ($DataTab->SelectedItem == 1) {
      $DataWindow->FN_Label->Hide();
      $FirstName->Hide();
      $DataWindow->LN_Label->Hide();
      $LastName->Hide();
      $DataWindow->Adapt_Label->Hide();
      $Adapter->Hide();
      $DataWindow->Bld_Label->Show();
      $Building->Show();
      $Building->SetFocus();
   }
   if ($DataTab->SelectedItem == 2) {
      $DataWindow->FN_Label->Hide();
      $FirstName->Hide();
      $DataWindow->LN_Label->Hide();
      $LastName->Hide();
      $DataWindow->Bld_Label->Hide();
      $Building->Hide();
      $DataWindow->Adapt_Label->Show();
      $Adapter->Show();
      $Adapter->SetFocus();
   }
}

sub DataWindow_Terminate {
   exit(0);
}

sub FindNow_Click {
   my ($index,$fname,$lname,$build,$room,$adap,$ip);
   $lastcolumn = "";
   if ($dataretrieved == 1) {
      if ($resized == 0) {
     $DataWindow->Resize($minwidth, $minheight + 118);
      }
      $DataView->View(1);
      $DataView->Clear();
      $DataView->Show();
      $Status->Show();
      $Stop->Enable();
      $index = 0;
      %data = "";

      open (DATABASE, "database.txt");
      
      while (<DATABASE>) {
     chomp;
     ($id,$fname,$lname,$build,$room,$adap,$ip) = split(/,/);
     
     if ($breakloop == 1) {
        last;
     }

     if ((($DataTab->SelectedItem == 0) && (lc(substr($fname,0,length(
+$FirstName->Text))) eq lc($FirstName->Text)) && (lc(substr($lname,0,l
+ength($LastName->Text))) eq lc($LastName->Text))) 
        || (($DataTab->SelectedItem == 1) && (lc($build) eq lc($Buildi
+ng->Text)))
        || (($DataTab->SelectedItem == 2) && (lc(substr($adap,0,length
+($Adapter->Text))) eq lc($Adapter->Text)))) {
           
           my @temp = split/,/,$_;
           s/^\d{1,3},//;
           $data{$temp[0]} = $_;

           $DataView->InsertItem(-item => $index, -text => "$fname");
           $DataView->SetItem(-item => $index, -subitem => 1, -text =>
+ "$lname");
           $DataView->SetItem(-item => $index, -subitem => 2, -text =>
+ "$build");
           $DataView->SetItem(-item => $index, -subitem => 3, -text =>
+ "$room");
           $DataView->SetItem(-item => $index, -subitem => 4, -text =>
+ "$adap");
           $DataView->SetItem(-item => $index, -subitem => 5, -text =>
+ "$ip");
           ++$index;
     }
     $DataView->Update();
     $Status->Text("$index record(s) found");
      }
   close DATABASE;
   $Stop->Disable();
   $resized = 1;
   }
   else {
      Win32::Sound::Play("SystemDefault", SND_ASYNC);
      Win32::GUI::MessageBox(0,"You must retrieve the data from the ne
+twork\r\nfirst.  Goto File, Retrieve Data.","Error: No Data Loaded",6
+4);
   }
}

sub Stop_Click {
   print "Stop button pressed\n";
   $Stop->Disable();
   $breakloop = 1;
}

sub NewSearch_Click {
   $FirstName->Text("");
   $LastName->Text("");
   $Building->Text("");
   $Building->Select(-1);
   $Adapter->Text("");
   $DataWindow->Resize($minwidth, $minheight);
   $DataView->Hide();
   $DataView->Clear();
   $Status->Hide();
   $resized = 0;
}

sub Dropdown_Change {
    $Building->Text($Building->GetString($Building->SelectedItem));
}

sub DataView_ColumnClick {
   my $column = shift;
   if ($lastcolumn == $column) {
      $sortorder = 1 - $sortorder;
   }
   else {
      $sortorder = 0;
   }
   my %sortcol = &NewList($column, %data);
   &SortListItem(\%data,\%sortcol);
   $lastcolumn = $column;
}

sub SortListItem {
  my ($data,$sortcol) = @_;
  my $check;
  my %data = %$data;
  my %sortcol = %$sortcol;

  $check = "$_" foreach (values %sortcol);

  $DataView->Clear();
  $index = 0;
  if ($sortorder == 0) {
     foreach (sort { uc($sortcol{$a}) cmp uc($sortcol{$b}) } keys %sor
+tcol)  {
    my @newdata = split/,/,$data{$_};
    ($fname,$lname,$build,$room,$adap,$ip) = @newdata;
    if ($fname ne "") {
       $DataView->InsertItem(-item => $index, -text => "$fname");
       $DataView->SetItem(-item => $index, -subitem => 1, -text => "$l
+name");
       $DataView->SetItem(-item => $index, -subitem => 2, -text => "$b
+uild");
       $DataView->SetItem(-item => $index, -subitem => 3, -text => "$r
+oom");
       $DataView->SetItem(-item => $index, -subitem => 4, -text => "$a
+dap");
       $DataView->SetItem(-item => $index, -subitem => 5, -text => "$i
+p");
       $DataView->Update();
       ++$index;
    }
     }
  }
  else {
     foreach (sort { uc($sortcol{$b}) cmp uc($sortcol{$a}) } keys %sor
+tcol)  {
    my @newdata = split/,/,$data{$_};
    ($fname,$lname,$build,$room,$adap,$ip) = @newdata;
    if ($fname ne "") {
       $DataView->InsertItem(-item => $index, -text => "$fname");
       $DataView->SetItem(-item => $index, -subitem => 1, -text => "$l
+name");
       $DataView->SetItem(-item => $index, -subitem => 2, -text => "$b
+uild");
       $DataView->SetItem(-item => $index, -subitem => 3, -text => "$r
+oom");
       $DataView->SetItem(-item => $index, -subitem => 4, -text => "$a
+dap");
       $DataView->SetItem(-item => $index, -subitem => 5, -text => "$i
+p");
       $DataView->Update();
       ++$index;
    }
     }
  }
}


sub NewList  {
   ## This creates another hash to use only for sorting purposes.
   my ($column,%sortcol) = @_;
   my $sortthis;
                                                                      
+                                
   foreach (keys %sortcol) {
      my @info = split /,/, $sortcol{$_};
      $sortthis = $info[$column];
      $sortcol{$_} = "$sortthis";
   }
   return(%sortcol);
}


sub Report_Terminate {
   $ReportMessage->Text("");
   $YourName->Text("");
   $Report->Hide();
   return;
}

sub YourName_Change {
   if ($YourName->Text() ne "") {
      $Send->Enable();
   }
   else {
      $Send->Disable();
   }
}

sub Send_Click {
   if ($ReportMessage->Text() eq "") {
      Win32::Sound::Play("SystemDefault", SND_ASYNC);
      Win32::GUI::MessageBox(0,"Message can not be left blank. Please\
+r\nenter a message and try again.",64);
   }
   else {
      $name = $YourName->Text;
      $message = $ReportMessage->Text;
      if ($test != 1) {
     ref ($sender = new Mail::Sender ({ from => $name, smtp => "mailse
+rver"}));
     $sender->OpenMultipart({to => "recipient", subject => "Problem in
+ Database"});
     $sender->Body;
     $sender->Send($message);
     $sender->Send(<<'*END*');

*END*
     $sender->SendLine("\n$name");
     $sender->Close;
      }
      $Report->Hide();
   }
}