[comp.sys.mac.programmer] Problem at closeDev

resnick@cogsci.uiuc.edu (Pete Resnick) (09/19/90)

I have a real big problem in my cdev. I hope that someone will say,
"Pete, you dope. This is simple. You forgot the semi-colon." Whatever...

I have a routine that updates a resource according to what's in an
editText box in my cdev. I call the routine when the OK button is
hit and when the cdev gets closed. On the hitDev, everything works
just ducky. I call the routine, passing it CPDialog, a handle to my
data (I do it the old fashioned way), and numItems. I check each to
make sure they have valid values entering the routine. They are all
constant on entering. Then I do a GetDItem for the editText box.
Boom. The value returned in the item Handle when I am on the hitDev
is some longword. On the closeDev, I get a 0. Now, I don't know for
sure that all the values in CPDialog and my data handle are correct
(doing that in Macsbug would've been ugly), but I am sure numItems
has not changed and the actual value of CPDialog and my data handle
are perfectly constant. Do the dialog items disappear before the
closeDev (that would be very bad, and unlikely since I have seen other
cdevs check data on the close), or am I doing something stupid?

If you would like to see actual code, I would be willing to trim down
to a small non-working piece and send it to you. Any help would be
*greatly* appreciated.

Thanks,
pr
--
Pete Resnick             (...so what is a mojo, and why would one be rising?)
Graduate assistant - Philosophy Department, Gregory Hall, UIUC
System manager - Cognitive Science Group, Beckman Institute, UIUC
Internet/ARPAnet/EDUnet  : resnick@cogsci.uiuc.edu
BITNET (if no other way) : FREE0285@UIUCVMD

a347@mindlink.UUCP (John Miller) (09/19/90)

Pete Resnick writes
> I have a routine that updates a resource according to what's in an
> editText box in my cdev. I call the routine when the OK button is
> hit and when the cdev gets closed. On the hitDev, everything works
> just ducky. I call the routine, passing it CPDialog, a handle to my
> data (I do it the old fashioned way), and numItems. I check each to
> make sure they have valid values entering the routine. They are all
> constant on entering. Then I do a GetDItem for the editText box.
> Boom. The value returned in the item Handle when I am on the hitDev
> is some longword. On the closeDev, I get a 0. Now, I don't know for
> sure that all the values in CPDialog and my data handle are correct
> (doing that in Macsbug would've been ugly), but I am sure numItems
> has not changed and the actual value of CPDialog and my data handle
> are perfectly constant. Do the dialog items disappear before the
> closeDev (that would be very bad, and unlikely since I have seen other
> cdevs check data on the close), or am I doing something stupid?

The dialog items do indeed disappear before the closeDev message,
and yes, it is very bad.  Of course, the items don't always
disappear before the closeDev.  If the user causes the closeDev to
occur by closing the Control Panel window, the items are still
there.  When the closeDev is caused by the user clicking on another
cdev icon, that's when the Control Panel graciously removes the
items before sending closeDev to your cdev.  Of course, the fact
that it works in a desirable fashion some of the time isn't
much help to you.

The best thing to do is to update your stored information whenever
a change is made to the text edit item.  You do this by passing any keystrokes
directly to the Dialog Manager and then turn them
into null events so that the Control Panel will ignore them.
Your routines that handle the editing commands should also update
the stored information whenever the user uses one of the edit
commands.  Then on the closeDev command, your code looks only at
values stored in your private cdev storage; it does not look
at anything in the dialog item list.

Here is a crude fragment of what to:

case keyEvtDev:    /* respond to keydown */
    /* I stripped out the code that directs cut/copy/paste commands
       to the appropriate routine and that also rejects other
       command-key combinations.  */

      /* UpdateSavedValues() is a routine in your code that extracts
         the information from the dialog and saves it in your
         private data structure.  */

       DialogSelect(theEvent, &CPDialog, &dummyItemHit);
       UpdateSavedValues(cdevStorage, CPDialog, numItems);
       theEvent->what = nullEvent;  /* Hide the key event
from the control panel.  */

    break;

This design flaw appears in at least 2 versions of the 6.0.x
system software, so I guess we are all stuck with it.  It
is unfortunate because it makes cdev code more complicated
than it has to be.  I find a worrisome parallel between this
design flaw and the startup/openStack message ordering
problem that Apple fixes in HyperCard 2.0.  (I'd include
good old DragGrayRgn() as well, but that is rather old news.)
I don't know what type of internal design checklists Apple
may have, but I would hope that they could be strengthened
to better check for these types of issue.

Apple should also consider updating its DTS TextEdit cdev example
code.  The current version is misleading:  it doesn't include
any code that handles the closeDev problem.

----------------------------------------------------------------------
John Miller                     (604) 433-1795
Symplex Systems                 AppleLink (rarely)  CDA0461
Burnaby, British Columbia       Fax: (604) 430-8516
Canada                          Internet:  john_miller@mindlink.uucp
----------------------------------------------------------------------

stanbach@Apple.COM (Francis Stanbach) (09/23/90)

In article <1990Sep18.234445.28711@ux1.cso.uiuc.edu> resnick@cogsci.uiuc.edu (Pete Resnick) writes:
>"Pete, you dope. This is simple. You forgot the semi-colon." Whatever...
>
>hit and when the cdev gets closed. On the hitDev, everything works
>just ducky. I call the routine, passing it CPDialog, a handle to my
>data (I do it the old fashioned way), and numItems. I check each to
>make sure they have valid values entering the routine. They are all
>constant on entering. Then I do a GetDItem for the editText box.
>Boom. The value returned in the item Handle when I am on the hitDev
>is some longword. On the closeDev, I get a 0. Now, I don't know for
>sure that all the values in CPDialog and my data handle are correct
>(doing that in Macsbug would've been ugly), but I am sure numItems
>has not changed and the actual value of CPDialog and my data handle
>are perfectly constant. Do the dialog items disappear before the
>closeDev (that would be very bad, and unlikely since I have seen other
>cdevs check data on the close), or am I doing something stupid?
>
>If you would like to see actual code, I would be willing to trim down
>to a small non-working piece and send it to you. Any help would be
>*greatly* appreciated.

This got me and almost everyone who has written a cdev. The
Control Panel destroys the dialog before it sends the closedev
message. You need to maintain your own copy of the
values of the controls in your cdev. Use the closedev message to
cleanup globals or whatever, but the dialog isn't there.

Francis
-- 
wmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm
Francis Stanbach                          Finder Engineer Apple Computer, Inc.
wmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwmwm