Drawing Icons
The following information is valid for System 6.x and System 7.0. The
information provided at the end specifically pertains to System 7.0.
There are two resources that contain icons: 'ICON' and 'ICN#'. An 'ICON'
resource is useful if you need to draw an icon only in srcOr mode. You can't use
it to draw a grayed-out or selected icon, like you see in the Finder. An 'ICON'
resource is a 32 bit by 32 bit image of an icon. You draw it with this code:
An 'ICN#' resource is useful if you need to draw an icon in a wide variety of
ways, like the Finder does. An 'ICN#' resource contains a list of two 32 bit by
32 bit images. The first is the actual icon and the second is the mask for the
icon. Using these two images together lets you create different effects. The
rest of this section describes how to get the same effects as the Finder.
// This code fragment defines a structure for an 'ICN#' resource:
#typedef struct {
long int icon[31];
long int mask[31];
} iListStruct, *iListPtr, **iListHndl;
// This code fragment gets a handle to an 'ICN#' resource:
iListHndl myILHndl; [TOKEN:12074] Handle to an 'ICN#' resource. */
BitMap iBitMap; [TOKEN:12074] Bit map for the icon. */ BitMap mBitMap; /* Bit map for the mask. */ Rect icnRect; [TOKEN:12074] Rectangle for the icon */ if (MyILHndl == nil ) then
HandleError(); [TOKEN:12074] This function exits or does */
/* whatever is appropriate. */
// Once you have a handle to the icons, you must set up two bit maps to use
SetRect(&icnRect, 0, 0, 32, 32) /* Define the icon's "bounds." */ iBitMap.baseAddr = &((**MyILHndl).icon);
iBitMap.row bytes = 4; [TOKEN:12074] 4 rows * 8 columns = 32 */
iBitMap.bounds = icnRect;
mBitMap.baseAddr = &((**MyILHndl).icon);
mBitMap.row bytes = 4; [TOKEN:12074] 4 rows * 8 columns = 32 */
mBitMap.bounds = icnRect;
The Finder can draws an object's icon many different ways, depending on
whether it's selected or not, open or not, and online or offline. An object can
be none of these things, all of these things, or any combination of these
things. This illustration shows you all the ways the Finder can draw an
icon:
To draw an icon for an online object that's not opened, follow this
procedure:
• Draw the icon's mask. This leaves a "hole" in the desktop, shaped like
your icon.
• Draw the icon.
To draw an icon for an online object that's not open and not selected, draw
the mask with srcBit mode and the icon with srcXor mode, like this:
SetRect( & destRect, left, top, left+32, top+32); CopyBits( &mBitMap, &thePort->portBits, &icnRect, & destRect, srcBic, nil );
CopyBits( &iBitMap, &thePort->portBits, &icnRect, & destRect, srcXor, nil );
To draw an icon for an online object that's selected but not open, draw the
mask with srcOr mode and the icon with srcXor mode. Drawing the mask
with srcOr mode leaves a black "hole" in the desktop, instead of a white one:
SetRect( & destRect, left, top, left+32, top+32); CopyBits( &mBitMap, &thePort->portBits, &icnRect, & destRect, srcOr, nil );
CopyBits( &iBitMap, &thePort->portBits, &icnRect, & destRect, srcXor, nil );
Drawing icons for offline objects requires more work. Follow this
procedure:
• XOR a pattern in the boundsRect of the icon.
• Draw the icon's mask, like above
• XOR out the parts of pattern that don't fall inside the mask.
• Draw the icon, as above
To draw the icon for an offline object that's not opened and not selected, use
a ltGray pattern, like this:
SetRect( & destRect, left, top, left+32, top+32); PaintRect( &destRect ); [TOKEN:12074] Paint a light gray background */ /* for the icon..*/
CopyBits( &mBitMap, &thePort->portBits, &icnRect, & destRect, srcBic, nil ); [TOKEN:12074] Punch out mask-shaped hole. */
PaintRect( &destRect ); [TOKEN:12074] XOR out bits outside mask, */ /* leaving mask light gray. */
CopyBits( &iBitMap, &thePort->portBits, &icnRect, & destRect, srcOr, nil ); [TOKEN:12074] Draw the icon in the light */
/* gray mask with srcOr mode.*/
SetPenState( OldPen ); [TOKEN:12074] Restore the pen state. */ To draw the icon for an offline object that's selected but not opened, use a
dkGray pattern, like this:
SetRect( & destRect, left, top, left+32, top+32); PaintRect( &destRect ); [TOKEN:12074] Paint a dark gray background */ /* for the icon. */
CopyBits( &mBitMap, &thePort->portBits, &icnRect, & destRect, srcBic, nil ); [TOKEN:12074] Punch out mask-shaped hole. */
PaintRect( &destRect ); [TOKEN:12074] XOR out bits outside mask, */ /* leaving mask dark gray. */
CopyBits( &iBitMap, &thePort->portBits, &icnRect, & destRect, srcBic, nil ); [TOKEN:12074] Draw the icon in the dark */
/* gray mask with srcBic mode.*/
SetPenState( OldPen ); [TOKEN:12074] Restore the pen state. */ Drawing icons for opened objects requires less work. You don't need to
draw the icon, just its mask. Follow this procedure:
• XOR a pattern in the boundsRect of the icon.
• Draw the icon's mask, as above
• XOR out the parts of pattern that don't fall inside the mask.
Notice that online and offline objects are drawn the same way if they're
opened. This example shows how to draw an icon for an object that's opened
and selected:
SetRect( & destRect, left, top, left+32, top+32); PaintRect( &destRect ); [TOKEN:12074] Paint a dark gray background */ /* for the icon. */
CopyBits( &mBitMap, &thePort->portBits, &icnRect, & destRect, srcBic, nil ); [TOKEN:12074] Punch out mask-shaped hole. */
PaintRect( &destRect ); [TOKEN:12074] XOR out bits outside mask, */ /* leaving mask dark gray. */
SetPenState( OldPen ); [TOKEN:12074] Restore the pen state. */ To draw an icon for an object that's opened but not selected, you need to
change only one line in the example above. Change PenPat( &dkGray ) to