Hi,

I've just committed a change that added two new methods, GetParent and UserData - both of which can be called on controls or windows.

GetParent simply returns the parent window of the control or child window (if there was one).

UserData allows you to associate an SV to a control or window, allowing you to save instance data to that object.

When used together, these methods should allow the creation of an window object that encapsulates all events, methods and instance data without the use of closures and other 'tricks'.

I've created a test script for both of these functions but I am unsure of the naming scheme? I also assume that going forward we should be adding test scripts for any changes?

I would be grateful for any comments/suggestions - and once this functionality has bedded down I'll add a couple of samples before the next release. In the meantime, the script below will show this functionality in action.

Cheers,

jez.

-------------------
use strict;
use warnings;

use Win32::GUI;

# Create the main window
my $mainwindow = new Win32::GUI::Window(
  -name   => "Window",
  -title  => "Objects and Windows",
  -pos    => [100,100],
  -size   => [400,400],
);

#We now create several employee selector 'controls'.
my $search1=EmployeeSelector->new($mainwindow,20,20);
my $search2=EmployeeSelector->new($mainwindow,20,50);
my $search3=EmployeeSelector->new($mainwindow,20,80);
my $search4=EmployeeSelector->new($mainwindow,20,110);
my $search5=EmployeeSelector->new($mainwindow,20,140);
#Show all our controls
$search2->Move(200,200);

# Child windows don't show until their parent shows, so by
# showing the main window last we can create the child
# windows with WS_VISIBLE, and not see them drawing
$mainwindow->Show();
#Enter the message processing loop
Win32::GUI::Dialog();
exit(0);

# package to encapsulate the search window.
# one static (class) function that diaplays the
# search window and returns the selected item
# or undef if ESC/Cancel preseed
package SearchWindow;
use strict;
use warnings;
use Win32::GUI;

our $searchwindow;
our $ok;  # there has to be a better way to determine
         # which button was pressed perhaps buttons with -ok/-cancel
         # should exit the DoModal loop, returning some
         # well defined values?

sub DoSearch {

unless ($searchwindow) {
# create it first time we're called
# depending how much this gets called it's questionable
# whether it's worth keeping it hanging around

$searchwindow = Win32::GUI::DialogBox->new(
-title       => "Search for Employee",
-size        => [300,270],
-onTerminate => sub { $ok = 0; return -1;},
);
$searchwindow->AddListView(
-name          => "ListView",
-pos           => [8,8],
-size          => [280,189],
-singlesel     => 1,
-fullrowselect => 1,
-tabstop       => 1,
);
$searchwindow->AddButton (
-text    => 'OK',
-pos     => [164,208],
-size    => [60,21],
-tabstop => 1,
-default => 1,  # draw button in 'default' style
-ok      => 1,  # respond to 'return' key
-onClick => sub{ $ok = 1; return -1;},
);
$searchwindow->AddButton (
-text    => 'Cancel',
-pos     => [228,208],
-size    => [60,21],
-tabstop => 1,
-cancel  => 1,  # respond to ESC key
-onClick => sub{ $ok = 0; return -1;},
);

#populate the list view
$searchwindow->ListView->InsertColumn(-width => 55,-text  => 'Emp ID');
$searchwindow->ListView->InsertColumn(-width => 205,-text => 'Employee Name');
$searchwindow->ListView->InsertItem(-text => [1234, 'Bob Smith']);
$searchwindow->ListView->InsertItem(-text => [4321, 'Peter Jones']);
$searchwindow->ListView->InsertItem(-text => [7890, 'John Brown']);

}

# show the search window and return the searched out value
$searchwindow->Center();
$searchwindow->ListView->SetFocus();
$searchwindow->DoModal(1);

return undef unless $ok;  # pressed cancel or ESC (or X in titlebar)
 my $item=$searchwindow->ListView->SelectedItems();
return undef unless defined $item; # no selection (undef gets treated as zero by ItemInfo)
my %hItem=$searchwindow->ListView->ItemInfo($item,0);
 return $hItem{-text};
}

# I don't really understand why this end block is necessary to avoid warnings
END
{
undef $searchwindow;
}

#package to encapsulate the 'Employee Selector' control
# subclass of Window, so that I don't have to implement
# Move()/Show() etc.
package EmployeeSelector;
use strict;
use warnings;
use Win32::GUI;
use base qw(Win32::GUI::Window);

#The constructor
sub new
{
my ($class, $parent, $xcor, $ycor)[EMAIL PROTECTED];

#Create window
my $self = $class->SUPER::new(
-parent    => $parent,
-pos       => [$xcor,$ycor],
-size      => [150, 24],
-popstyle  => WS_CAPTION | WS_SIZEBOX,
-pushstyle => WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
);
$self->UserData(0);
#Create other controls
$self->AddLabel(
-text   => 'ID#',
-pos    => [0,6],
-size   => [20,20],
);
$self->AddTextfield(
-name   => "EMPID",
-pos   => [20,2],
-size  => [35,20],
-tip    => 'Employee ID',
);
 $self->AddButton (
-text   => 'Search',
-pos    => [60,2],
-size  => [60,20],
-tip    => 'Search for a Employee  ID',
-onClick => sub{my $self=shift;Search($self->GetParent)},
);
#Set the user data for the window to be an empty string
$self->UserData('');
return $self;
}

sub Search
{
my $self = shift;
my $result = SearchWindow::DoSearch();
if ($result) {
 $self->SetEmpID($result);
 print 'Previous ID for this window was '.$self->UserData()."\n";
 $self->UserData($result);
}
return 1;
}

sub SetEmpID
{
my $self=shift;
$self->EMPID->Text(shift);
}



Reply via email to