[PYTHONMAC-SIG] Framework/Setting the port
James B. Wilkinson
Sat, 21 Feb 1998 01:07:12 -0500
I decided to try to do some work on the Mac Framework and got myself into a
heap of trouble. I can't find any way to explain the symptoms. I'm hoping
that some of you can shed some light.
In the process of debugging I found that I wasn't seeing all the events. I
finally remembered having asked about that previously, and somebody had
said that SIOUX sometimes steals events, and they never make it to your own
app. So I stole the TEWindow code from ped and put it into the Framework.
Then I made a subclass of it called DebugWindow and had the Application
class create one when it starts up. I gave the Application class a method
that subclasses of Window can call to write into the debug window, and sure
enough, I get to see all the events that way. So far so good.
Then I started really messing with the Framework. I had had some previous
experience with another framework in C before I started playing Python.
That framework uses a very different (it seemed to me) scheme for setting
the port. I liked that scheme and thought I'd change this Framework to
match. It sets the port only in the following places:
1) In the event loop it sets the port to any window that is getting an
idle proc run on it. It restores the original port at the end of this.
2) In DoDialog if the event is activate on a dialog window it sets the
port to that window before calling the handler. This was a special case to
ward off problems in a particular case that I don't remember now.
3) In DoUpdate it sets the port to the window being updated before
calling the handler. Restore the original on exit.
4) In DoActivate it sets the port to the window getting the activate
event before calling the handler. Restore the original on exit if it was a
deactivate. Leave it set to the activating window on activates.
5) Set the port to a window being destroyed before calling the handler.
Restore the original on return from the handler.
6) In the routine used to register a window with the framework, set the
port to the new window and leave it there.
It was number 4) that I particularly missed in the Python Framework. I
couldn't figure out how it could be right NOT to set the port to a window
that is coming active. So I put that in. The application I have been
working on works ok if I disable my debug window, but if that is turned on,
things get funny. The debug window comes up first, and works ok. My app
window them comes up and activates, but the text boxes work funny. If I
type in one, I just get extra copies of the caret, but not the characters
that I type. All this fixes itself if I click the mouse in the app window,
and things are fine from there on. So I started looking at what happens on
mouse clicks and found that things don't get fixed if I comment out the
SetPort in the do_inContent method of the Window class. Clearly that line
of code is what is fixing my problem. But why is it not ok to let the
SetPort get called earlier when the activate event came along? I can't
figure that. Even more strange is the results I got when I started doing
GetPorts and printing out what they returned. The port is the *SAME*
before and after the SetPort in do_inContent. That sounds like the SetPort
in the activate handler did its job. So how can a SetPort that doesn't
actually change the port make the textboxes start behaving correctly? Or
is GetPort lying to me? It's all very strange.
Oh, yeah, the routine that writes into the debug window sets the port
before doing so and restores it afterward.
In practical terms this isn't a serious problem. If the debug window isn't
there, the problem doesn't come up, at least so far. If the debug window
is there, then I'm debugging my program, and I know how to work around the
problem. Still, I don't like mysteries. They're a sure indication that I
don't know enough. In this case I obviously don't know as much as I
thought I did when I started messing with this thing.
Any suggestions welcome, and thanks.
Jimmy Wilkinson | Perfessor of Computer Science
jimmy@cs.CofC.edu | The College of Charleston
(803) 953-8160 | Charleston SC 29424
If there is one word to describe me,
that word would have to be "profectionist".
PYTHONMAC-SIG - SIG on Python for the Apple Macintosh
send messages to: email@example.com
administrivia to: firstname.lastname@example.org