TweetFollow Us on Twitter

CDEF Messages
Volume Number:5
Issue Number:8
Column Tag:TechNotes

Related Info: Control Manager

New CDEF Messages

By James Plamondon, Berkeley, CA

System 7.0 will present all Macintosh programmers with new opportunities and new challenges. One of the challenges will be making your code 32-bit clean. This can be particularly difficult if your code uses parts of the Toolbox that are not 32-bit clean. Some of the routines in the Control Manager, for example, are at best only 31-bit clean -- that’s close, but not close enough for System 7.0. This article will answer the challenge by explaining both the problem with, and the solution to, the Control Manager’s unclean habits.

The specific problem that this article addresses lies in the calcCRgns CDEF message sent to all CDEFs. The message is sent to a control’s CDEF to calculate the control’s enclosing region, or the enclosing region of its indicator (if any). In Inside Macintosh [IM v1 p330], the calcCRgns CDEF message is explained as follows: “Param is a QuickDraw region handle . If the high-order bit of param is set, the region requested is that of the control’s indicator rather than the control as a whole. The definition function should clear the high byte (not just the high bit) of the region handle before attempting to update the region.”

You can see that it’s pretty hard to be 32-bit clean when you’re supposed to go around clearing bytes in handles. The handle could contain a perfectly valid 32-bit address; clearing the high byte would change the address to something entirely different -- and accessing or changing anything via that address would be a terrible mistake. Yet, it is required by the Control Manager, so what is a programmer to do?

First, one can at least improve the situation by not clearing the whole high byte, as directed. Instead, just interrogate the high bit to see what region needs to be calculated, and then clear it -- leaving the rest of the bits alone. This makes the code 31-bit clean; that’s not good enough for System 7.0, but it’ll do for the old systems.

For System 7.0, Apple has defined two new CDEF messages: calcCntlRgn (10) and calcThumbRgn (11). Instead of sending a calcCRgns message and having the CDEF figure out whether the system wants the control region or the indicator (thumb) region, the system will figure out which it wants, and send the CDEF the appropriate (new) message. The CDEF can then treat the region handle like a region handle, instead of like a secret message that has to be decoded.

To make sure the world is a happy place, and that backward compatibility is maintained, a CDEF should always implement both the calcCRgns message and the new CDEF messages. That way, whether the CDEF finds itself in use under either System 7.0 or a pre-7.0 system (which is pretty likely for the next year or two), it will respond in the manner the system expects.

There are a couple of related issues. First, a control’s variation code used to be stored in the high byte of the control record’s contrlDefProc field. To get the variation code, the programmer would mask it out of the high byte. This is unclean, since the contrlDefProc field may contain a valid 32-bit address. A new routine, GetCVariant(), was provided a couple of years ago (IM v5 p222) to return the control’s variation code. Be sure to use it.

Also, whenever writing custom CDEFs or/and other DEFs, remember that The Management Reserves the Right to Add New Messages at Any Time. Therefore, be sure that if your DEF receives a message it doesn’t recognize, it just returns what it was given without doing anything (and without blowing up).

Lastly, if you’re going to add any custom messages to your custom DEF, set the new message numbers to constants greater than 128. This gives Apple the freedom it needs to add new messages, without tromping on your custom messages.

That’s about all the discussion this issue will bear. See the revision to Tech Note #212 for the official version of this same information. Below, I have appended the old, unclean calcCRgns implementation, the new and improved 31-bit clean calcCRgns implementation, and a shell CDEF entry-point function that shows how all these messages should be handled. Note that the ‘param’ actual argument is a valid handle to an empty region, so the content of the region indicated by param needs to be changed, not the value of param itself. You couldn’t make any change to param stick anyway, since it’s not a ‘var’ parameter (i.e., the CDEF gets a copy of param’s value, not its address). Also, I’m assuming the existence of some routines, such as MyCalcThumbRgn() and MyCalcCtlRgn(), that are different for each different CDEF, and which are therefore not given here.

I’d like to thank Andrew Shebanow, Larry Tesler, Charlie Oppenheimer, and Steve Goldberg at Apple -- way to go, guys!

(*****************************************************
MyControl: Main entry point.  Call appropriate
 message-handling routine.
*****************************************************)

FUNCTION MyControl(varCode: INTEGER;
 theCntl: ControlHandle;
 message: INTEGER;
 param: LONGINT): LONGINT;

BEGIN
 MyControl := 0;  { default }

 CASE message OF
 drawCntl:
 doDrawCntl(theCntl, varCode, param);
 testCntl:
 MyControl := doTestCntl(theCntl, param);
 initCntl:
 doInitCntl(theCntl, varCode);
 dispCntl:
 doDispCntl(theCntl, varCode);
 autoTrack:
 doAutoTrack(theCntl, varCode, param);

 calcCRgns: { support for pre-7.0 }
 doCalcCRgnsNew(theCntl, param);
 calcCntlRgn:  { new in System 7.0 }
 MyCalcCtlRgn(theCntl, RgnHandle(param));
 calcThumbRgn:{ new in System 7.0 }
 MyCalcThumbRgn(theCntl, RgnHandle(param));

 OTHERWISE
 ; { ignore all other messages }
 END;  { case }
END;  { MyControl }

(*****************************************************
doCalcCRgnsOld: Calculate the region the
 control or its indicator occupies in its
 window.  Old method: Only 24-bit clean
 -- don’t use this method anymore; use 
 doCalcCRgnsNew(), below.
*****************************************************)

PROCEDURE doCalcCRgnsOld(theCntl: ControlHandle;
 param: LONGINT);

BEGIN
 if (param < 0) then  { high bit is sign bit }
 begin
 { clear high BYTE and set thumb rgn }
 param := BitAnd(param, $0FFFFFFF);
 MyCalcThumbRgn(theCntl, RgnHandle(param));
 end
 else
 begin
 { clear high BYTE and set control rgn }
 param := BitAnd(param, $0FFFFFFF);
 MyCalcCtlRgn(theCntl,
 RgnHandle(param));
 end;
END;  { doCalcCRgnsOld }

(*****************************************************
doCalcCRgnsNew: New method: Only 31-bit
 clean, but that’s the best we can do.
*****************************************************)
PROCEDURE doCalcCRgnsNew(theCntl: ControlHandle;
 param: LONGINT);
BEGIN
 if (param < 0) then  { high bit is sign bit }
 begin 
 { clear high BIT and set thumb rgn }
 param := BitAnd(param, $7FFFFFFF);
 MyCalcThumbRgn(theCntl, RgnHandle(param));
 end
 else
 begin
  { clear high BIT and set control’s rgn }
 param := BitAnd(param, $7FFFFFFF);
 MyCalcCtlRgn(theCntl, RgnHandle(param));
 end;
END;  { doCalcCRgnsNew }

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »

Price Scanner via MacPrices.net

Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more
Every version of Apple Pencil is on sale toda...
Best Buy has all Apple Pencils on sale today for $79, ranging up to 39% off MSRP for some models. Sale prices for online orders only, in-store prices may vary. Order online and choose free shipping... Read more
Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more
Apple is offering significant discounts on 16...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more
Apple HomePods on sale for $30-$50 off MSRP t...
Best Buy is offering a $30-$50 discount on Apple HomePods this weekend on their online store. The HomePod mini is on sale for $69.99, $30 off MSRP, while Best Buy has the full-size HomePod on sale... Read more
Limited-time sale: 13-inch M3 MacBook Airs fo...
Amazon has the base 13″ M3 MacBook Air (8GB/256GB) in stock and on sale for a limited time for $989 shipped. That’s $110 off MSRP, and it’s the lowest price we’ve seen so far for an M3-powered... Read more
13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more

Jobs Board

Licensed Practical Nurse - Womens Imaging *A...
Licensed Practical Nurse - Womens Imaging Apple Hill - PRN Location: York Hospital, York, PA Schedule: PRN/Per Diem Sign-On Bonus Eligible Remote/Hybrid Regular Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Operating Room Assistant - *Apple* Hill Sur...
Operating Room Assistant - Apple Hill Surgical Center - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.