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

Re: [MacPerl] periodic text to window



At 5:36 pm -0400 23/04/00, Adam Stern wrote:

>I wrote a test thing that opened up a window, and in the MacPerl window, I
>had it send a line of text, and I used DrawString to display it.  I moved
>the cursor down and then I tried sending another line, but i can't make it
>upddate the window unless I do some window event, like resizing.  Here is
>what I got.  Any help would be appreciated.

You are very nearly there. To force a window redraw you call 
'$win->redraw' otherwise, as you say, the window will wait for an 
'update' event before it does so.

A few tweaks to your script (see below) and it does more or less what 
you want I think.

However, since you call 'print()' in the 'test()' function you must 
reset the graphics port to 'your window' before calling 
'DrawString()' otherwise you will probably draw the string in 
MacPerl's window. Secondly you want to move the '$cursor +=14' 
statement out of the 'redraw' routine otherwise the line position 
gets incremented after any redraw due to an unconnected update (say, 
uncovering the window) which you don't want to do.

If you look at QuickDraw.pm you will see that the font details (size, 
face etc.) are all stored in the GrafPtr record. So once you have got 
the information lodged there you don't need to call TextFont() etc. 
during the lifetime of window unless of course you deliberately want 
to _change_ the font. Resetting the font each 'redraw' can sometimes 
be redundant; but it depends on the circumstances.

Although it can be made to work, <STDIN> is not really the best way 
of acquiring a 'test string'. For instance it is difficult to find a 
condition to end the program when the MacPerl window is closed, since 
while MacPerl is busy waiting for a key stroke from <STDIN> it 
remains blissfully unaware that the MacWindow has been dismissed.

HTH,

Alan Fry

#-------------

#!perl -w

use Mac::Events;
use Mac::Windows;
use Mac::QuickDraw;
use Mac::Fonts;
use strict;

my $helvetica = GetFNum "Helvetica" or die $^E;
my $times = GetFNum "Times" or die $^E;
my $bounds = new Rect 50, 50, 550, 290;
my $win = new MacWindow $bounds, "Hello, World!", 1, documentProc, 1;
my $curpos = 12;
my $line = '';

$win->sethook("redraw", \&DrawOurWindow);

test();

WaitNextEvent while $win->window;

$win->dispose;


sub test {
     my $str = '';
     print "text: ";   # NB resets the graphics port
     while ($str !~ /\n/) {
          $str .= <STDIN>;
     }
     chomp($str);
     $line = $str;
     $curpos += 14;
     $str = '';
     if ($win->window) {
         $win->redraw; # force a MacWindow update
         test();       # get the next line
     } else { exit }   # a not-very-nice way to end the program
}

sub DrawOurWindow {
     my($win) = @_;
     SetPort($win->window) if $win->window;
     TextFont($helvetica);
     TextSize(12);
     TextFace(normal);
     MoveTo 0,$curpos;
     DrawString "$line $curpos";
}

#-------------

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