home *** CD-ROM | disk | FTP | other *** search
- /* CIconBuddy.c
- * An icon pane/button class based on the System 7 icon buddy calls that
- * draw/track icon suites. See Tech Note M.IM.IconDrawing for more details.
- *
- * SUPERCLASS = CPane.c
- *
- * Copyright ©1992, Mark/Space Softworks, All Rights Reserved
- *
- * This class is buyware, if you use it, buy a Mark/Space Softworks product.
- * Send email to one of the below addresses for more details.
- * Internet: mspace@netcom.com
- * AppleLink: MARKSPACE
- * AOL: markspace
- *
- * 12/12/92 bhall Created
- * 12/14/92 bhall Cleaned up for submission to TCL-TALK
- * 10/10/93 bhall Fixed leak in DrawIcon
- */
-
- #include <CIconBuddy.h>
- #include <Commands.h>
- #include <Global.h>
- #include <SUtil.h>
-
- #include <Icons.h>
-
- RgnHandle CIconBuddy::cIconRgn;
-
- /*******************************************************************************\
- * IIconBuddy *
- * Initialize an Icon Buddy. The icons are taken from an icon family with the *
- * specified ID. *
- * *
- \*******************************************************************************/
- void CIconBuddy::IIconBuddy(
- CView *anEnclosure,
- CBureaucrat *aSupervisor,
- short aWidth,
- short aHeight,
- short aHEncl,
- short aVEncl,
- SizingOption aHSizing,
- SizingOption aVSizing,
- short iconID,
- Boolean fPreferColor)
- {
- /*
- * Initialize our superclass
- */
- CPane::IPane(
- anEnclosure, aSupervisor,
- aWidth, aHeight,
- aHEncl, aVEncl,
- aHSizing, aVSizing);
-
- /*
- * Init our instance variables
- */
- this->iconID = iconID;
- clickCmd = cmdNull;
-
- IIconBuddyX(fPreferColor);
- }
-
-
- /*******************************************************************************\
- * IViewTemp *
- * Initialize from a 'IcPn' resource. *
- * *
- \*******************************************************************************/
- void CIconBuddy::IViewTemp(
- CView *anEnclosure, CBureaucrat *aSupervisor, Ptr viewData)
- {
- tIconPaneTempP iconData = (tIconPaneTempP) viewData;
-
- inherited::IViewTemp(anEnclosure, aSupervisor, viewData);
-
- /*
- * Init our instance variables
- */
- iconID = iconData->iconID;
- clickCmd = iconData->clickCmd;
-
- IIconBuddyX(iconData->fPreferColor);
- }
-
-
- /*******************************************************************************\
- * IIconPaneX *
- * Perform common initialization. *
- * *
- \*******************************************************************************/
- void CIconBuddy::IIconBuddyX(Boolean fPreferColor)
- {
- /*
- * Init instance data not handled by IViewTemp or IIconBuddy
- */
- align = atNone;
- allowDistortion = FALSE;
-
- if (!cIconRgn)
- cIconRgn = NewRgn();
- }
-
-
- #pragma mark -
-
- /*******************************************************************************\
- * Get/SetIconID *
- * Get/Set the id of the icon suite to use for this button. *
- * *
- \*******************************************************************************/
- void CIconBuddy::SetIconID(short anID)
- {
- iconID = anID;
- }
-
-
- short CIconBuddy::GetIconID(void)
- {
- return iconID;
- }
-
-
- /*******************************************************************************\
- * Get/SetAlignment *
- * Get/Set the alignment used in PlotIconID calls. *
- * *
- \*******************************************************************************/
- void CIconBuddy::SetAlignment(IconAlignmentType anAlign)
- {
- align = anAlign;
- }
-
-
- IconAlignmentType CIconBuddy::GetAlignment(void)
- {
- return align;
- }
-
-
- #pragma mark -
-
- /*******************************************************************************\
- * Activate/Deactivate *
- * We need to redraw on an activate or deactivate event. *
- * *
- \*******************************************************************************/
- void CIconBuddy::Activate(void)
- {
- Boolean wasActive = active;
-
- inherited::Activate();
-
- /*
- * Don't draw if we don't have to
- */
- if (!wasActive && ReallyVisible()) {
- Prepare();
- DrawIcon(FALSE);
- }
- }
-
-
- void CIconBuddy::Deactivate(void)
- {
- Boolean wasActive = active;
-
- inherited::Deactivate();
-
- /*
- * Don't draw if we don't have to
- */
- if (wasActive && ReallyVisible()) {
- Prepare();
- DrawIcon(FALSE);
- }
- }
-
-
- #pragma mark -
-
- /*******************************************************************************\
- * GetIconRect *
- * This routine bottlenecks the retrieval of the rect used in the icon buddy *
- * calls. If allowDistortion is TRUE, we just return the frame. If it is *
- * false, we snap the rect to a valid 32, 16, or 12 pixel square. This makes *
- * sure that the icon is not shrunk or blown up when drawn. *
- * *
- * For example, you may have a 16 * 14 images (in an ics) that you would like *
- * to display next to a scroll bar. To do this you would draw the icon in *
- * a 16 * 16 rect, but specify a 16 * 14 frame. If allowDistortion is true, *
- * the 16*16 ics would be distorted to fit the 16 * 14 frame. If it were *
- * set to false, it would draw as intended, with view clipping preventing the *
- * rest of the (unused) imgae from stamping on the surrounding area. *
- * *
- \*******************************************************************************/
- void CIconBuddy::GetIconRect(Rect *theRect)
- {
- LongToQDRect(&frame, theRect);
- if (!allowDistortion) {
- if (theRect->right > 16) {
- theRect->right = 32;
- theRect->bottom = 32;
- } else if (theRect->right > 12) {
- theRect->right = 16;
- theRect->bottom = 16;
- } else {
- theRect->right = 12;
- theRect->bottom = 12;
- }
- }
- }
-
-
- /*******************************************************************************\
- * DrawIcon *
- * Draw the icon normally or hilited. *
- * *
- \*******************************************************************************/
- void CIconBuddy::DrawIcon(Boolean fHilite)
- {
- IconTransformType transform;
- Rect iconRect;
- RgnHandle iconRgn = NULL;
-
- /*
- * Figure out the transform to use
- */
- if (fHilite)
- transform = ttSelected;
- else
- transform = ttNone;
-
- if (!IsActive())
- transform |= ttDisabled;
-
-
- /*
- * Draw the icon
- */
- GetIconRect(&iconRect);
- FailOSErr(PlotIconID(&iconRect, align, transform, iconID));
-
- /*
- * Validate the icon region, so we don't draw squares
- */
- IconIDToRgn(cIconRgn, &iconRect, align, iconID);
- ValidRgn(cIconRgn);
- SetEmptyRgn(cIconRgn);
- }
-
-
- /*******************************************************************************\
- * Draw *
- * Draw the icon in response to an update. *
- * *
- \*******************************************************************************/
- void CIconBuddy::Draw(Rect *area)
- {
- DrawIcon(FALSE);
- }
-
-
- #pragma mark -
-
- /*******************************************************************************\
- * SetClickCmd *
- * Set the clickCmd for an iconPane. This command will be sent to *
- * itsSupervisor when the icon is clicked and the mouse is released *
- * inside the icon. *
- * *
- \*******************************************************************************/
- void CIconBuddy::SetClickCmd(long aCmd)
- {
- clickCmd = aCmd;
- }
-
-
- /*******************************************************************************\
- * GetClickCmd *
- * Return the pane's clickCmd. *
- * *
- \*******************************************************************************/
- long CIconBuddy::GetClickCmd(void)
- {
- return clickCmd;
- }
-
-
- /*******************************************************************************\
- * SimulateClick *
- * Simulate a click in the button, usually requested in response to *
- * a keyboard shortcut. *
- * *
- \*******************************************************************************/
- void CIconBuddy::SimulateClick(void)
- {
- #define kFlashDelay 8 // number of ticks to hilite button
- long ticks;
-
- if (!IsActive())
- return;
-
- /*
- * Flash the button
- */
- Prepare();
- DrawIcon(TRUE);
- Delay(kFlashDelay, &ticks);
- DrawIcon(FALSE);
-
- /*
- * Do the command
- */
- itsSupervisor->DoCommand(GetClickCmd());
- }
-
-
- /*******************************************************************************\
- * DoClick *
- * Respond to a click. The wantsClicks instance variable must be *
- * TRUE for this method ever to be called. *
- * *
- \*******************************************************************************/
- void CIconBuddy::DoClick(Point hitPt, short modifierKeys, long when)
- {
- Rect iconRect;
-
- if (!IsActive())
- return;
-
- GetIconRect(&iconRect);
- if (PtInIconID(hitPt, &iconRect, align, iconID)) {
- if (Track())
- itsSupervisor->DoCommand(GetClickCmd());
- }
- }
-
-
- /*******************************************************************************\
- * Track *
- * Tracks the mouse, returns TRUE if the mouse was in the pane when released. *
- * *
- \*******************************************************************************/
- Boolean CIconBuddy::Track(void)
- {
- Boolean inBtn = TRUE;
- Point where;
- Rect iconRect;
-
- DrawIcon(TRUE);
- GetIconRect(&iconRect);
-
- while (StillDown()) {
- GetMouse(&where);
- if (PtInIconID(where, &iconRect, align, iconID)) {
- if (!inBtn) DrawIcon(TRUE);
- inBtn = TRUE;
- }
- else {
- if (inBtn) DrawIcon(FALSE);
- inBtn = FALSE;
- }
- }
-
- if (inBtn)
- DrawIcon(FALSE);
-
- return inBtn;
- }
-