[comp.sys.mac.programmer] What can you assume about the current port?

rothberg@polya.STANFORD.EDU (Edward Rothberg) (05/11/88)

Here's something I can't find documented anywhere.  I have an application, and
in it I assume that a certain port is the 'default'.  Whenever I change ports,
I always change back to this one after I'm done.  If I want to draw into this
port, I just go ahead and do it.  My question is: is this a safe thing to
assume?  I had thought so until I tried the alarm clock DA.  It changes the
current port to it's own port, and doesn't restore the old one.  I see three
possibilities:

1) What I'm doing is totally bogus, and I should always say SetPort before
	drawing into the port.
2) Alarm clock is broken and I shouldn't worry about it.
3) Alarm clock is broken, and I should be conservative about it and call
	SetPort just in case.

Which is the right answer?

Ed Rothberg
rothberg@polya.stanford.edu

dtw@f.gp.cs.cmu.edu (Duane Williams) (05/11/88)

I don't know what the current Apple dogma is regarding the User Interface
Guidelines, but an early version of the Guidelines said that "the user
should feel in control of the computer."  Selecting the active window (by
clicking in it) is supposed to be the prerogative of the user and is one of
the ways of making the user "feel in control" of the machine.  

There are only a few circumstances when the current port should be changed
by the application.  The primary one, in obedience to the above standard, is
when an activate event is received, generally caused by an explicit user
action, e.g., clicking in a window or creating a new window.  The activated
window should become the current port.

Update events also require changing the current port, but such a change
should be strictly temporary (lasting the lifetime of the redraw procedure
only).

There were documented problems with some DAs changing the current port non-
temporarily; so you may have to save and restore the current port around the
opening of a DA.

Of course, we all know that Apple changes the dogma from time to time --
after which we read in press reports about how awful it is that third party
developers are not following the rules.  And we all know that Apple has been
a prime violator of its own rules (even prior to changing them); witness the
Finder, MacPaint, and HyperCard.  So, feel free to change the current port
with the phase of the moon and keep those users guessing! :-)

Duane Williams

-- 
uucp: ...!seismo!cmucspt!me.ri.cmu.edu!dtw
arpa: dtw@cs.cmu.edu

dorourke@polyslo.UUCP (David M. O'Rourke) (05/11/88)

In article <2799@polya.STANFORD.EDU> rothberg@polya.STANFORD.EDU (Edward Rothberg) writes:
>
>1) What I'm doing is totally bogus, and I should always say SetPort before
>	drawing into the port.

  My training has told me this is the correct way to do it.  Since your
program might have to handle update events in any window, which is in effect,
a grafport.  The update routine you write should just set the port anyways.
So it shouldn't be a problem.  You might be drawing into a "non-active" window
in which case you should first save the port, set the port, draw the contents,
and then restore the old port.

  Also this is right out of Inside Macintosh Volume I, page 440.

  Warning:  Early versions of some desk accessories may set the current
            grafport to the accessory's port upon return from OpenDeskAcc.
            To be safe, you should bracket your call to OpenDeskAcc with calls
            to the QuickDraw procedures GetPort and SetPort, to save and
            restore the current port.

   Since the alarm clock is one of the oldest DA's I can think of, I guess
it comes under the catagory of "Early versions of some desk accessories".

  Hope this helps


David M. O'Rourke

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| dorourke@polyslo | Disclaimer:  All opinions in this message are mine, but  |
|                  |              if you like them they can be yours too.     |
|                  |              Besides I'm just a student so what do I     |
|                  |              know!                                       |
|-----------------------------------------------------------------------------|
|    When you have to place a disclaimer in your mail you know it's a sign    |
| that there are TOO many Lawyer's.                                           |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

guido@cwi.nl (Guido van Rossum) (05/11/88)

In article <1654@pt.cs.cmu.edu> dtw@f.gp.cs.cmu.edu (Duane Williams) writes:
>I don't know what the current Apple dogma is regarding the User Interface
>Guidelines, but an early version of the Guidelines said that "the user
>should feel in control of the computer."  Selecting the active window (by
>clicking in it) is supposed to be the prerogative of the user and is one of
>the ways of making the user "feel in control" of the machine.  

You are answering a different question.  This is comp.sys.mac.PROGRAMMER:
the question was about the 'current grafport', which is where quickdraw
calls (by definition) will draw.  It has nothing to do with the active
window; e.g., while repainting a window you have to (temporarily) make it
the current grafport, but of course that doesn't pop up its window.

To answer the right question: unless you want to fix the alarm clock and
distribute it with your program, you'll have to be conservative.  Yes,
it's a pain.  But there are only a few cases you have to worry about.

Inside Mac Volume I (is that volume still relevant? :-) says very
clearly (somewhere at the beginning ofthe QuickDraw chapter) that *all*
toolbox calls save and restore the port.  This means that if the
application sets the port, it'll in general stay set.  Most toolbox
routines also don't care what the port is set to when they are entered:
the set it explicitly to the window in which they want to draw (unless
by definition they draw in the current port, of course).  There's one
exception: ZoomWindow crashes spectacularly when the current port
doesn't match the window.  (I think this was a bug which was discovered
too late, so they "fixed" it by putting a warning in Inside Mac IV).

So, you don't have to be afraid that the toolbox changes the current
port.  For DA's, I think you are safe except just after an OpenDeskAcc;
I believe that when a DA reveices a Control call the Device Mgr saves,
sets and restores the port (but I haven't got my documentation handy).
--
Guido van Rossum, Centre for Mathematics and Computer Science (CWI), Amsterdam
guido@piring.cwi.nl or mcvax!piring!guido or guido%piring.cwi.nl@uunet.uu.net

dorourke@polyslo.UUCP (David M. O'Rourke) (05/12/88)

In article <314@piring.cwi.nl> guido@cwi.nl (Guido van Rossum) writes:
>So, you don't have to be afraid that the toolbox changes the current
>port.  For DA's, I think you are safe except just after an OpenDeskAcc;
>I believe that when a DA reveices a Control call the Device Mgr saves,
>sets and restores the port (but I haven't got my documentation handy).

  Read page 440 in volume 1, it has a nice little *warning*


David M. O'Rourke

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| dorourke@polyslo | Disclaimer:  All opinions in this message are mine, but  |
|                  |              if you like them they can be yours too.     |
|                  |              Besides I'm just a student so what do I     |
|                  |              know!                                       |
|-----------------------------------------------------------------------------|
|    When you have to place a disclaimer in your mail you know it's a sign    |
| that there are TOO many Lawyer's.                                           |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

carlton@ji.Berkeley.EDU.UUCP (05/13/88)

In article <314@piring.cwi.nl> guido@cwi.nl (Guido van Rossum) writes:
...
>
>Inside Mac Volume I (is that volume still relevant? :-) says very
>clearly (somewhere at the beginning ofthe QuickDraw chapter) that *all*
>toolbox calls save and restore the port.  This means that if the
...

Don't believe everything you read.  A while ago (> 2 yrs), some of the
low level window manager routines would change the port behind your back.
I think it was PaintBehind or something like that (it has been a while).
This makes sense, since these routines are normally only called from the
high level ones, but when the documentation says ALL, I expected all.  Oh
well.

This may have been fixed by now, I haven't checked recently. 

mike (carlton@ji  or  ...!ucbvax!ji!carlton)