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

Re: [MacPerl] EditText question



Richard Christansen's question recently about creating an editable box in a
MacWindow has raised some questions obout TextEdit.pm. The script below is
an attempt to explain the behaviour.

1 Cursor position
-----------------

Run the script and enter some text in the box in the usual way. You will
see that the cursor position is printed out after each character.

When done, run the cursor back using the left arrow-key. The position
printed out changes as expected. Now index the cursor forwards towards the
end using the right arrow-key. The position changes as expected _until_ the
cursor reaches the very end of the string, when the position is shown as
*the start* of the string even though the cursor is visually at the far
end.

But now hit shift-cmd-N which also prints out the horizontal position of
GetPen(), and the correct value is returned. So what seems to be happening
is, that at the end of the string, GetPen() returns the wrong value on the
first access and the right value subsequently.(?).

I think this behaviour is undesirable if not a bug. For instance one would
expect to be able to use the cursor position to scroll the string backwards
when the cursor reaches the right-hand end of the box. That works properly
when text is _entered_, but fails miserably (at the end of the string) when
the right arrow-key is used.

The problem does not arise at the begining of the string. The correct
cursor position is always returned, and scrolling difficulties do not
therefore arise.

2 Deleting text
---------------

Run the script and put some text into the box. Hit shift-cmd-D and the text
vanishes, and the cursor returns to the start position. OK so far.

Now hit shift-cmd-P to print the string held by the handle TEGetText(). At
the first run it returns the deleted string plus some garbage. If the
process is repeated the string returned contains bits of previous runs and
other strange characters, and is clearly all nonsense.

You can adjust the number of characters deleted by altering the second
parameter in TESetSelect(). For instance TESetSelect(O, 4, $edit) will
delete the first four characters of the string. You will find partial
deletions  work as expected -- it is only when the whole string is deleted
that the problem arises.

3 Disposing the handle
----------------------

There is an option in the script, shift-cmd-Q, to dispose of the handle.
This is commented out because it causes a hang-up. By all means try it but
be prepared for the crash. The problem is that after the TextEdit handle is
destroyed, Windows.pm, via the pane, calls TEIdle(hTE) at the first
opportunity. MacPerl hangs (I think) because hTE has gone away.

This raise the question of what should go in the END block. If the TE
handle is disposed first the script crashes for the above reason on
'StopScript' or 'cmd-period'. If the window handle is disposed first, and
then the TextEdit handle, MacPerl has no objection, but I suspect the TE
handle memory is not cleared since the TE field is registered as a pane in
the window. If the window has gone away what meaning has $hte->dispose?

---

I am not at all sure whether these phenomena are all bugs in TextEdit.pm or
whether I am misusing it in some way or expecting the wrong things of it
and would value comments.

Alan Fry

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#!perl

$|=1;

use Mac::Windows;
use Mac::Events;
use Mac::Events qw($CurrentEvent);
use Mac::QuickDraw;
use Mac::TextEdit;

my($win, $bnd, $dest, $view, $hte);

$bnd = Rect->new(10, 50, 260, 95);
$win = MacWindow->new($bnd, "TextEdit", 1, 4, 1);
$win->sethook('drawgrowicon', sub { 1 });

$view = Rect->new(10, 10, 240, 35);
$dest = Rect->new(15, 15, 800, 40);

$hte = $win->new_textedit($dest, $view);
$hte->sethook('key', \&get_key);

WaitNextEvent while $win->window;
$win->dispose;
$hte->dispose;

END { $win->dispose; $hte->dispose }

sub get_key {
    my ($hte, $win, $key) = @_;
    my $edit = $hte->edit;
    my $mods = $CurrentEvent->modifiers & 768;
    if ($mods == 768) {            # shift+cmd keys
        if ($key == 100) {         # key D
        TESetSelect(0, 256, $edit);
        TEDelete($edit)
        }
        elsif ($key == 112) {      # key P
            print TEGetText($edit)->get, "\n";
        }
        elsif ($key == 110) {      # key N
            print GetPen()->h, "\n";
        }
        #elsif ($key == 113) {     # key Q
        #    $hte->dispose         # hangs
        #}
    }
    else {
        if ($key == 13) { return 1 }
        else {
           TEKey(chr($key), $edit);
           print GetPen()->h, "\n"
        }
    }
    SelectWindow($win->window);
    return 1
}

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=









***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch