RubberBandit.c

/*
    File:       RubberBandit.c
 
    Contains:   Rubber Bandit is a simple srcXor rubber-banding example.
 
    Written by:     
 
    Copyright:  Copyright © 19911999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/14/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                9/23/94     nick            make RubberBandIt return the bounding rect, 
                                            cleaned up discarded some fns
 
*/
#include <Types.h>
#include <Quickdraw.h>
#include <Events.h>
// function prototypes:
 
Rect    RubberBandIt(Point anchorPt);
Rect    *CheckRect(Rect *theRect);
void    DrawStuff(Rect *rubberRect);
 
 
 
/*  ================================================
    RubberBandIt follows the mouse and rubber-bands
    a design based on its position.  It retains
    control until the mouse button is released.
    ================================================    */
 
Rect RubberBandIt(Point anchorPt)
{
    Point   curMousePt = {0, 0};
    Rect    rubberRect;
 
    PenMode(srcXor);                                /* Set pen mode to exclusive or.*/
    PenPat(&qd.gray);
 
    GetMouse(&curMousePt);                          /* Get current mouse pos.       */
 
    rubberRect.top = anchorPt.v;                    /* Draw initial rectangle.      */
    rubberRect.left = anchorPt.h;
    rubberRect.bottom = curMousePt.v;
    rubberRect.right = curMousePt.h;
    DrawStuff(&rubberRect);
 
    while (Button())
    {
        GetMouse(&curMousePt);                      /* Get current mouse posÉ       */
 
        if (curMousePt.h != rubberRect.right ||     /* If mouse movedÉ              */
            curMousePt.v != rubberRect.bottom)
        {
            DrawStuff(&rubberRect);                 /* Erase oldÉ                   */
            rubberRect.bottom = curMousePt.v;
            rubberRect.right = curMousePt.h;
            DrawStuff(&rubberRect);                 /* and draw new.                */
        }
    }
    
    DrawStuff(&rubberRect);                         // Erase old at end.            */
    return rubberRect ;
}
 
 
/*  ================================================
    DrawStuff draws designs in the rectangle
    passed, using the current pen mode, etc.
    ================================================    */
 
void DrawStuff(rubberRect)
Rect    *rubberRect;
{
    Rect    curRect;
    
    curRect = *rubberRect;
    FrameRect(CheckRect(&curRect));
}
 
 
 
 
/*  ================================================
    CheckRect makes sure that the top of the
    rectangle passed is ² the bottom and the left is
    ² the right.  FrameRect needs things this way.
    ================================================    */
 
Rect *CheckRect(theRect)
Rect    *theRect;
{
    short   temp;
 
    if (theRect->top > theRect->bottom)     /* Need to reverse top and bottom? */
    {
        temp = theRect->top;
        theRect->top = theRect->bottom;
        theRect->bottom = temp;
    }
 
    if (theRect->left > theRect->right)     /* Need to reverse left and right? */
    {
        temp = theRect->left;
        theRect->left = theRect->right;
        theRect->right = temp;
    }
    
    return theRect;                         /* This makes nested calls easier.  */
}