[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] dialog box problems



At 15.36 -0400 1999.04.28, Noe Dinnerstein:  President, BodhiSoftWare wrote:
>reached.   I am unable to abort the script with command-period, and have to
>force MacPerl to quit.   If I try running the script with debug on, I get
>an out of memory error.   I'm running MacPerl 5.2.0r4 with System 8.5 on a
>PowerBook 1400c with a Newer G3 upgrade and 64 Megs of RAM.  I'm including
>the code.  Any help would be appreciated.  Hopefully it's some dumb,
>simple, newbie error.

Well, it is actually a few of them.  :)  I'll make some comments and then
give you a new version of the code.  Most of it is just logic flow, which
can be tough to get if you haven't dealt with event-driven programs before.


>#!perl -w
>
>#
>#use strict;

You should always use strict.  I don't think you have any problems in here
that relate directly to commenting it out, but you might have, and you
wouldn't have known.  Basically, you need to declare every variable with
my() in its outermost scope, and then let use strict do its job.

>use Mac::Windows;
>use Mac::QuickDraw;
>use Mac::Events;
>use Mac::StandardFile;
>use Mac::Dialogs;
>#******************************************************

You have two variables that should be my()'d here:

  my($DoSearch, $dlg);

That will satisfy use strict (for now).

>$DoSearch = 0;
>
>ShowDialog();
>while ($dlg->window() ) {
>  $dlg->modal();
>}
>
>if ($DoSearch == 1) {
>	SearchDirs();
>}
>dispose $dlg;

The problem here is that you never dispose $dlg, so the while() loop will
never end, and you will never get to the search.  We'll dispose it further
on down.


>#***************************************************
>sub ShowDialog {
>
>
>#------ SET UP DIALOG FOR SEARCH
>

Make this:

  my $dlg = MacDialog ...

>$dlg = MacDialog->new(
>	 Rect->new(50,50,450,155),    # dialog rectangle
>  'Search ACIP CD',            # dialog title
>		1,                           # is visible?
>		movableDBoxProc(),           # window style
>		0,                           # has go away box?
>		[ kButtonDialogItem(),
>   Rect->new(300, 30,380,50),  'Cancel'],
>   [ kButtonDialogItem(),
>     Rect->new(300, 70, 380,90),'Search'],
>  [kStaticTextDialogItem(),
>     Rect->new(10, 10, 220, 30),
>     'Search Any or All Directories'],
>  [ kEditTextDialogItem(),
>     Rect->new(15,50,180,65), ' '],

I would set this as '', not ' '.


> );
>}

Here is another big problem.  These functions and handler calls below are
outside the ShowDialog() function, and they are after the main loop above,
so they are ever called.  As such, you never get any default cancel or OK
buttons, and you never get your handlers defined.  They should be inside
the ShowDialog() function.


>#-----Define dialog items
>
>SetDialogCancelItem ($dlg->window(), 1);
>SetDialogDefaultItem($dlg->window(), 2);
>SetDialogTextDialogItemText($dlg->window(), 4);

This function is not a function.  :)  The actual function is
SelectDialogItemText().


>#------Define handlers for each button and text input area
>
>$dlg->item_hit(1 => \&d1);
>$dlg->item_hit(2 => \&d2);
>$dlg->item_hit(3 => \&d3);

Since item 3 is just a plain ol' static text field, you can't hit it, and
there is no need to set up a handler for it.


>
>#***************************************************
>#
>#    SUBROUTINES
>#
>#***************************************************
>sub d1 {
>  my($dlg, $item) = @_;
>	 $DoSearch = 0;
>  return(1)
>}

Since you are canceling the query, you should have $dlg->dispose after the
$DoSearch = 0 assignment.


>sub d2 {
>  my($dlg, $item) = @_;
>
>  if ($dlg->item_text(4) == ' ') {

Well, we changed the ' ' above to '', but more importantly, == is not for
strings, eq is, so this should be:

  if ($dlg->item_text(4) eq '') {

>     print "No search string entered\n";
>     $DoSearch = 3;
>   }
>	  else {
>      $DoSearch = 1;

Since the dialog is now finished (you have your text), go ahead and
$dlg->dispose so you can exit the main loop.

>	  }
>
>   return(1);
>}
>
>sub d3 {
>  my($dlg, $item) = @_;
>  return(1);
>{

This curly bracket is backward, but we don't need &d3 anymore since we
won't be hitting the static text, so we can just remove it.



>sub SearchDirs()

I didn't touch this stuff or look at it, except to add my() declarations
for the variables to satsify use strict.



So here is my version, including reformatting to make it easier (for me,
anyway :) to read.

#!perl -w

use strict;
use Mac::Windows;
use Mac::QuickDraw;
use Mac::Events;
use Mac::StandardFile;
use Mac::Dialogs;
#******************************************************
my $DoSearch = 0;

my $dlg = ShowDialog();
while ($dlg->window()) {
    $dlg->modal();
}

if ($DoSearch == 1) {
    SearchDirs();
}

#***************************************************
sub ShowDialog {

    #------ SET UP DIALOG FOR SEARCH

    my $dlg = MacDialog->new(
        Rect->new(50,50,450,155),    # dialog rectangle
        'Search ACIP CD',            # dialog title
        1,                           # is visible?
        movableDBoxProc(),           # window style
        0,                           # has go away box?
        [ kButtonDialogItem(),
            Rect->new(300, 30, 380, 50), 'Cancel'],
        [ kButtonDialogItem(),
            Rect->new(300, 70, 380, 90), 'Search'],
        [ kStaticTextDialogItem(),
            Rect->new( 10, 10, 220, 30),
            'Search Any or All Directories'],
        [ kEditTextDialogItem(),
            Rect->new( 15, 50, 180, 65), ''],
    );


    #-----Define dialog items

    SetDialogCancelItem ($dlg->window(), 1);
    SetDialogDefaultItem($dlg->window(), 2);
    SelectDialogItemText($dlg->window(), 4);

    #------Define handlers for each button and text input area

    $dlg->item_hit(1 => \&d1);
    $dlg->item_hit(2 => \&d2);
    return $dlg;
}


#***************************************************
#
#    SUBROUTINES
#
#***************************************************
sub d1 {
    my($dlg, $item) = @_;
    $DoSearch = 0;
    $dlg->dispose;
    return(1);
}

sub d2 {
    my($dlg, $item) = @_;

    if ($dlg->item_text(4) eq '') {
        $DoSearch = 3;
        $dlg->dispose;
        print "No search string entered\n";
    } else {
        $DoSearch = 1;
        $dlg->dispose;
    }

    return(1);
}

sub SearchDirs() {
    print "Done";exit;  # for testing
    my($myDisk, $rootdir, @files);

    $myDisk = 'ACIP R4 Mac';

    print ("\nroot volume -  $myDisk:\n");
    $rootdir = "$myDisk:Texts:";

    opendir(DIR, $rootdir) or die "cannot open $myDisk";
    @files = readdir(DIR);
    foreach my $file (@files) {
        my($newdir, @newfiles);
        print("$file\n");
        $newdir = $rootdir . $file;
        opendir(NEWDIR,$newdir);

        @newfiles = readdir(NEWDIR);
        foreach my $newfile(@newfiles) {
            print("\t $newfile\n");
        }


    }
    closedir(DIR);

}

__END__

Hope this helps you out,

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org