On Fri, 2 Mar 2001, Aldo Calpini wrote:

> > it seems to me that this module wasnt really meant to be used in
> > an OO application? can anyone out there tell me otherwise?
> 
> I maybe be ignorant, but what the heck do you mean by 'an OO
> application'? :-)

He probably means being able to open multiple instances of a window
or being able to subclass window or control behavior.

Right now the only connection a control has to the rest of the program is
through the control name.  This means that to open two identical windows
where the controls on each window refer to the right window's values you
have to use app-unique names for controls and then dynamically define subs
with lexical references to those values.

I was considering doing this with package names (one package per window,
consistant control names, package is destroyed after window closes) but
havn't actually coded it yet.

I hope I am missing the simple, good way to do this.  Example below.

   - Eric B.

--
"Pasteurized From Concentrate"

==============================================================

use Win32::GUI;

#
#       An example of window instances in practice.
#
#       Note that if we needed to catch Change events on the text boxes
#       those would need coded names too.
#
#       Some of this could be cleaned by having small lexical subs which
#       forward to normal methods but you still need the closures
#       somewhere to hold a reference to disambiguate the window.
#
#       In a real MVC app I would usually refer directly to controls very
#       little; instead the lexical ref would be to a model object doing
#       the work.  I left that out for clarity.
#

my $prior_unique_window_code = 0;

my $open_window_count = 0;


sub build_add_window {

        my $code = ++$prior_unique_window_code;

        my $Window = new Win32::GUI::Window(
            -title  => "Add Numbers ($code)",
            -left   => 100,
            -top    => 100,
            -width  => 220,
            -height => 200,
            -name   => "Window$code",
            -style  => WS_MINIMIZEBOX | WS_CAPTION | WS_SYSMENU,
        );
        
        $Window->AddTextfield(
            -name     => "InputA",
            -left     => 10,
            -top      => 10,
            -text     => "5",
            -width    => 180,
            -height   => 22,
        );
        
        $Window->AddTextfield(
            -name     => "InputB",
            -left     => 10,
            -top      => 38,
            -text     => "8",
            -width    => 180,
            -height   => 22,
        );
        
        $Window->AddButton(
            -name   => "Add$code",
            -text   => "Add",
            -left   => 80,
            -top    => 66,
        );
        
        $Window->AddTextfield(
            -name     => "Output",
            -left     => 10,
            -top      => 94,
            -text     => "?",
            -width    => 180,
            -height   => 22,
        );
        
        $Window->AddButton(
            -name   => "CloneWindow$code",
            -text   => "Clone Window",
            -left   => 55,
            -top    => 122,
        );
        
        $open_window_count++;
        $Window->Show();

        *{"Window$code\_Terminate"} = sub {
                # Free refs hidden in the subs
                undef *{"Window$code\_Terminate"};
                undef *{"Add$code\_Click"};
                undef *{"CloneWindow$code\_Click"};

                $open_window_count--;

                return $open_window_count > 0 ? 1 : -1;
        };

        *{"Add$code\_Click"} = sub {
                $Window->Output->Text(
                        $Window->InputA->Text + $Window->InputB->Text
                );
        };

        *{"CloneWindow$code\_Click"} = sub {
                my $NewWindow = build_add_window();

                $NewWindow->InputA->Text( $Window->InputA->Text );
                $NewWindow->InputB->Text( $Window->InputB->Text );
                $NewWindow->Output->Text( $Window->Output->Text );
        };

        return $Window;
}

build_add_window();

Win32::GUI::Dialog();







Reply via email to