Firstly my apologies for delay in answering which is not due to any
lack of interest but just pressure of other things in the last couple
of days.
At 11:05 pm -0500 10/03/01, Morbus Iff wrote:
>Welp, I'm trying to mess around with Mac::TextEdit, and have gotten this
>far. When I try to SetText, I get an error message about an HTE not being
>accurate. I don't even know what an HTE is. Here is my current code. Don't
>laugh and giggle until my back is turned...
No giggles at all, the script is very nearly there with just a few tweaks:
#!/usr/bin/perl
use Mac::Controls;
use Mac::Events;
use Mac::Menus;
use Mac::QuickDraw;
use Mac::TextEdit;
use Mac::Windows;
$win = MacWindow->new( Rect->new(50, 50, 550, 290),
"AmphetaDesk Console", 1,
floatProc, 1);
$list= $win->new_textedit_style(
new Rect(15, 15, 485, 195),
new Rect(10, 10, 490, 200));
TESetText("Bob!", $list->{edit});
Wait next Event while $win->window;
$win->dispose
>How do I send text to the textedit window I have created?
As you did above with TESetText(HTE) (or TEkey(HTE)) -- you got the
right function but the syntax wasn't quite right.
In this script you might note MacWindow->New() calls the package
MacWindow which in a sense is a mini program (which uses TextEdit)
with its own functionality. For instance it draws the thin black line
round the 'destination rectangle' and provides a key handler so that
you can type directly into the window. You might not want either of
these functions in your application.
As to the HTE it is the TextEdit handle. In Perl terms it is the
reference to the hash containing all the things you need to know
about the block of text, for instance 'teLength' (the number of
characters) 'nLines' (number of lines), font information, start and
finish of the selected text and so on. If you look at the beginning
of 'TextEdit.pm' you will see the list under "TEHandle: A TextEdit
Record". A TE program will make access to this record all the time
with calls (for example like '$number_of_lines = $hTE->nLines') to
get whatever information is needed about the block of text as the
program runs.
The TE handle is returned by TENew() which you will see is called in
the package MacTextEdit and the handle is recorded in a 'hash' with
the key 'edit'. So to recall the HTE in your script you have to write
'$list->{edit}'
For your purposes you might want to think about calling TENew()
rather than $win->new_textedit()' so your script might become as
appended below. Note that at the end of the script you also need to
dispose of the handle with TEDispose($hTE).
>Is there any code that you could demonstrate for me?
>I'm not sure exactly what I'm looking for in TextEdit.pm and MPEdit.
In MPEdit you will see much the same things happen. At line 605 is
'sub open_file()' which does what you'd expect, opens the file, turns
the contents into a string (line 615) and hands the string to
TESetText() at line 620, just like your script above.
To handle keystrokes, the key is 'captured' at line 191 and
dispatched to 'sub get_key()' at line 311 and finally put into the TE
text block by the one line 328 'TEKey(chr($key), $hte)'. Everything
that goes before that line is to do with filtering out "control+key"
events and everything that comes after is to do with scrolling the
text block up down or sideways should the caret run out of the
viewable area. TEKey() is a magic function which knows how to deal
with the arrow keys, the delete key and the return key. The only
thing it doesn't know about is the tab key: tabs are not supported by
TextEdit.
I hope this clarifies things a little. Again my apologies for the delay.
Alan Fry
#------------
#!perl
use Mac::Events;
use Mac::QuickDraw;
use Mac::TextEdit;
use Mac::Windows;
my $win = MacWindow->new( Rect->new(50, 50, 550, 290),
"AmphetaDesk Console", 1,
floatProc, 1);
SetPort $win->window;
$win->sethook('redraw', \&do_draw);
my $viewRect = $win->window->portRect;
my $destRect = OffsetRect($viewRect, 10, 10);
my $hTE = TENew($destRect, $viewRect);
TESetText("Bob!", $hTE);
WaitNextEvent while $win->window;
dispose $win;
TEDispose($hTE);
sub do_draw {
my($win) = @_;
TEActivate($hTE);
TEGetText($hTE);
TEUpdate($viewRect, $hTE);
}