ProjectBuilder (OS X)/RubberBandit.c

/*
    File:       RubberBandit.c
 
    Contains:   a simple srcXor rubber-banding example.
 
    Written by: DH
 
    Copyright:  Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
 
    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
                ("Apple") in consideration of your agreement to the following terms, and your
                use, installation, modification or redistribution of this Apple software
                constitutes acceptance of these terms.  If you do not agree with these terms,
                please do not use, install, modify or redistribute this Apple software.
 
                In consideration of your agreement to abide by the following terms, and subject
                to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
                copyrights in this original Apple software (the "Apple Software"), to use,
                reproduce, modify and redistribute the Apple Software, with or without
                modifications, in source and/or binary forms; provided that if you redistribute
                the Apple Software in its entirety and without modifications, you must retain
                this notice and the following text and disclaimers in all such redistributions of
                the Apple Software.  Neither the name, trademarks, service marks or logos of
                Apple Computer, Inc. may be used to endorse or promote products derived from the
                Apple Software without specific prior written permission from Apple.  Except as
                expressly stated in this notice, no other rights or licenses, express or implied,
                are granted by Apple herein, including but not limited to any patent rights that
                may be infringed by your derivative works or by other works in which the Apple
                Software may be incorporated.
 
                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
                COMBINATION WITH YOUR PRODUCTS.
 
                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
    Change History (most recent first):
        6/2003      MK              Updated for Project Builder
        08/2000     JM              Carbonized, non-Carbon code is commented out
                                    for demonstration purposes.
        7/14/1999   KG              Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#include "CarbonPrefix.h"
 
/* constants:           */
 
#define testDLOG    1234
#define OK_button   1
 
/* function prototypes: */
 
extern void HandleDLOG(DialogPtr theDialog);
extern pascal Boolean RubberProc(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
extern void RubberBandIt(Point anchorPt);
extern Rect *CheckRect(Rect *theRect);
extern void DrawStuff(Rect *rubberRect);
extern void FanStuff(Rect *theRect);
 
 
int main() 
{
    DialogPtr   theDialog = NULL;
 
    /*InitGraf(&qd.thePort);
    InitFonts();
    FlushEvents(everyEvent, 0);
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs(0L);*/
    InitCursor();
    
    theDialog = GetNewDialog(testDLOG, NULL, (WindowPtr) -1L);
    if (theDialog != NULL) HandleDLOG(theDialog);
        
    ExitToShell();
        
        return 0;
}
 
 
/*  ================================================
    HandleDLOG calls ModalDialog and rubber-bands
    until the OK button is pressed.
    ================================================    */
 
void HandleDLOG(theDialog)
DialogPtr theDialog;
{
    short       itemHit;
    GrafPtr     oldPort;
    Point       anchorPt;
 
    do
    {
        ModalDialog(NULL, &itemHit);
 
        if (itemHit != OK_button)
        {
            GetPort(&oldPort);          /* Save the port, set it to our dialog, */
            //SetPort(theDialog);
            SetPortWindowPort(GetDialogWindow(theDialog));
            GetMouse(&anchorPt);        /* get the current mouse pos. and       */
            RubberBandIt(anchorPt);     /* rubber-band the image.               */
            SetPort(oldPort);           /* Restore the original port.           */
        }
    }
    while (itemHit != OK_button);
    
    DisposeDialog(theDialog);           /* Throw away the dialog.               */
}
 
 
/*  ================================================
    RubberBandIt follows the mouse and rubber-bands
    a design based on its position.  It retains
    control until the mouse button is released.
    ================================================    */
 
void RubberBandIt(anchorPt)
Point   anchorPt;
{
    Point   curMousePt = {0, 0};
    Rect    rubberRect;
    Pattern gray;
    CGrafPtr    currentPort;
    RgnHandle   rgnHandle = NewRgn();
    
    GetPort(&currentPort);
 
    PenMode(srcXor);                                /* Set pen mode to exclusive or.*/
    PenPat(&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.                */
            QDFlushPortBuffer(currentPort, GetPortVisibleRegion(currentPort, rgnHandle));
        }
    }
    
    DrawStuff(&rubberRect);                         /* Erase old at end.            */
    QDFlushPortBuffer(currentPort, GetPortVisibleRegion(currentPort, rgnHandle));
    DisposeRgn(rgnHandle);
}
 
 
/*  ================================================
    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));
    FanStuff(CheckRect(&curRect));
}
 
 
/*  ================================================
    FanStuff draws a fanlike thing in the rect
    passed, using the current pen mode, etc.
    ================================================    */
 
void    FanStuff(theRect)
Rect    *theRect;
{
    short   ang;
 
    for (ang = 0; ang < 360; ang += 60)
        PaintArc(theRect, ang, 45);
}
 
 
/*  ================================================
    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.  */
}