Source/Viewer_Events.c

/* 
 *  Viewer_Events.c
 *
 *  QuickDraw 3D 1.6 Sample
 *  Robert Dierkes
 *
 *   12/22/98   RDD     Created.
 */
 
/*------------------*/
/*  Include Files   */
/*------------------*/
#include "QD3D.h"
#include "QD3DViewer.h"
 
#if defined(OS_MACINTOSH) && OS_MACINTOSH
#include <MacWindows.h>
#include <Events.h>
#include <DiskInit.h>
#include <ToolUtils.h>
#endif
 
#include "Viewer_Error.h"
#include "Viewer_Events.h"
#include "Viewer_Main.h"
#include "Viewer_Menu.h"
#include "Viewer_Window.h"
#include "Viewer_System.h"
 
 
/*------------------*/
/*    Constants     */
/*------------------*/
 
/*----------------------*/
/*  Extern Declarations */
/*----------------------*/
extern  TQ3Boolean  gTimeToQuit;
extern  WindowRef   gAppResizeWindow; /* For example 2 */
 
 
/*----------------------*/
/*  Global Declarations */
/*----------------------*/
 
/*----------------------*/
/*   Type Definitions   */
/*----------------------*/
 
/*----------------------*/
/*   Local Prototypes   */
/*----------------------*/
static
Boolean Events_HandleViewerEvents(
            EventRecord             *pEvent);
 
static
OSErr   Events_MouseDown(
            EventRecord             *pEvent);
 
static
OSErr   Events_KeyPress(
            EventRecord             *pEvent);
 
static
OSErr   Events_UpdateWindow(
            EventRecord             *pEvent);
 
static
OSErr   Events_Disk(
            EventRecord             *pEvent);
 
static
OSErr   Events_Activate(
            EventRecord             *pEvent);
 
static
OSErr   Events_ClickInContent(
            EventRecord             *pEvent,
            WindowPtr               pWindow);
 
static
OSErr   Events_ClickInDrag(
            EventRecord             *pEvent,
            WindowPtr               window);
 
//TODO  #if 0 /* SAVE */
static
OSErr   Events_ClickInGrowBox(
            EventRecord             *pEvent,
            WindowPtr               pWindow);
#if 0 /* SAVE */
static
OSErr   Events_ClickInZoomBox(
            EventRecord             *pEvent,
            WindowPtr               pWindow,
            short                   windowPart);
#endif /* SAVE */
 
static
OSErr   Events_ClickInGoAway(
            EventRecord             *pEvent,
            WindowPtr               *hWindow);
 
 
/*
 *  Events_Initialize
 */
Boolean Events_Initialize(
            void)
{
    FlushEvents(everyEvent, 0);
 
    return true;
}
 
 
/*
 *  Events_Process
 */
void Events_Process(
            void)
{
    EventRecord     event;
    OSErr           theErr      = noErr;
    Boolean         doEvent     = false;
    Boolean         wasHandled  = false;
 
    while (! gTimeToQuit) {
        /* Is there an event in the queue? */
        doEvent = WaitNextEvent(everyEvent, &event, 0, NULL);
 
        wasHandled = Events_HandleViewerEvents(&event);
 
        if (! wasHandled) {
            /* Viewer didn't handle event so do it here */
            switch (event.what) {
                case nullEvent:
                    break;
 
                case mouseDown:
                    theErr = Events_MouseDown(&event);
                    break;
 
                case keyDown:
                case autoKey:
                    theErr = Events_KeyPress(&event);
                    break;
 
                case updateEvt:
                    theErr = Events_UpdateWindow(&event);
                    break;
 
                case diskEvt:
                    theErr = Events_Disk(&event);
                    break;
 
                case activateEvt:
                    theErr = Events_Activate(&event);
                    break;
 
                case mouseUp:
                case keyUp:
                case osEvt:
                case kHighLevelEvent:
                    break;
            }
        }
        else {
            /* Viewer already handled event */
        }
    }
}
 
 
#pragma mark -
 
/*
 *  Events_HandleViewerEvents
 */
static
Boolean Events_HandleViewerEvents(
            EventRecord *pEvent)
{
    WindowPtr       pWindow         = NULL;
    TQ3ViewerObject viewer          = NULL;
    Boolean         wasHandled      = false;
    Boolean         cursorChanged   = false;
    Point           mousePt;
 
    /* Get front window */
    if ((pWindow = FrontWindow()) == NULL) {
        return false;
    }
 
    /* Get Viewer reference from window's refcon */
    if ((viewer = Window_GetViewer(pWindow)) == NULL) {
        return false;
    }
 
    /* Let Viewer handle this event */
    wasHandled = Q3ViewerEvent(viewer, pEvent);
 
    /* Update cursor for current mouse location */
    GetMouse(&mousePt);
    cursorChanged = Q3ViewerAdjustCursor(viewer, &mousePt);
 
    return wasHandled;
}
 
 
/*
 *  Events_UpdateWindow
 */
OSErr Events_UpdateWindow(
            EventRecord *pEvent)
{
    OSErr           theErr = noErr;
    WindowPtr       pUpdateWindow   = NULL;
    GrafPtr         savedPort       = NULL;
    TQ3ViewerObject viewer          = NULL;
 
    if ((pUpdateWindow = (WindowPtr) pEvent->message) == NULL) {
        return paramErr;
    }
 
    if ((viewer = Window_GetViewer(pUpdateWindow)) == NULL) {
        return paramErr;
    }
 
    /* Save port and set to Viewer's */
    GetPort(&savedPort);
    SetPort(pUpdateWindow);
 
    BeginUpdate(pUpdateWindow);
 
        /* Update the Viewer model and button control strip */
        theErr = Q3ViewerDraw(viewer);
 
    EndUpdate(pUpdateWindow);
 
    /* Restore port */
    SetPort(savedPort);
 
    return theErr;
}
 
 
/*
 *  Events_MouseDown
 */
static
OSErr Events_MouseDown(
            EventRecord *pEvent)
{
    OSErr           theErr = noErr;
    WindowPtr       pWindow = NULL;
    short           windowPart;
 
    windowPart = FindWindow(pEvent->where, &pWindow);
 
    switch (windowPart) {
    case inMenuBar: 
        theErr = Menu_Command(MenuSelect (pEvent->where));
        break;
 
    case inSysWindow:
        SystemClick(pEvent, pWindow);
        break;
 
    case inContent:
        if (FrontWindow() != pWindow) {
            SelectWindow(pWindow);
        }
        theErr = Events_ClickInContent(pEvent, pWindow);
    break;
 
    case inDrag:
        theErr = Events_ClickInDrag(pEvent, pWindow);
        break;
 
    case inGrow:
//TODO
if(1){//TODO        if (pWindow == gAppResizeWindow) {
            theErr = Events_ClickInGrowBox(pEvent, pWindow);
        }
        else {
            /*
             * Handllng mouse down events in the Viewer's grow box are controlled
             * by setting the Viewer's kQ3ViewerDrawGrowBox flag.
             */
            theErr = noErr;
        }
//TODO
#if 0 /* SAVE */
        theErr = Events_ClickInGrowBox(pEvent, pWindow);
//#else
        /*
         * Handllng mouse down events in the Viewer's grow box are controlled
         * by setting the Viewer's kQ3ViewerDrawGrowBox flag.
         */
        theErr = noErr;
#endif /* SAVE */
        break;
 
    case inGoAway:
        theErr = Events_ClickInGoAway(pEvent, &pWindow);
        break;
 
    case inZoomIn:
    case inZoomOut:
#if 0 /* SAVE */
        theErr = Events_ClickInZoomBox(pEvent, pWindow, windowPart);
#else
        theErr = noErr;
#endif /* SAVE */
        break;
 
    case inCollapseBox:
        theErr = noErr;
        break;
 
    default:
        theErr = paramErr;
        break;
    }
 
    return theErr;
}
 
 
/*
 *  Events_KeyPress
 */
static
OSErr Events_KeyPress(
            EventRecord     *pEvent)
{
    OSErr       theErr = noErr;
    char        charCode;
 
    charCode = ((pEvent->message) & charCodeMask);
 
    if (pEvent->modifiers & btnState) {
        /* Button is UP with a key */
        if (pEvent->modifiers & cmdKey) {
            theErr = Menu_Command(MenuKey(charCode));
        }
        else {
        }
    }
    else {
        /* Button is DOWN with a key */
    }
 
    return theErr;
}
 
 
/*
 *  Events_Disk
 */
static
OSErr Events_Disk(
            EventRecord *pEvent)
{
    OSErr       theErr = noErr;
    Point       topLeft;
 
    if ((theErr = HiWord(pEvent->message)) != noErr) {
        return theErr;
    }
 
    SetPt(&topLeft, 100, 100);
    (void) DIBadMount(topLeft, pEvent->message);
 
    return theErr;
}
 
 
/*
 *  Events_Activate
 */
static
OSErr Events_Activate(
            EventRecord *pEvent)
{
    OSErr       theErr = noErr;
    WindowPtr   pActiveWindow;
 
    if ((pActiveWindow = (WindowPtr) pEvent->message) == NULL) {
        return paramErr;
    }
 
    /* Activate window */
    if (pEvent->modifiers & activeFlag)
    {
        /* Enable/Disable items */
        SetPort (pActiveWindow);
    }
    else
    /* Deactivate window */
    {
        /* Enable/Disable items */
    }
 
    return theErr;
}
 
 
#pragma mark -
 
/*
 *  Events_ClickInContent
 */
static
OSErr Events_ClickInContent(
            EventRecord     *pEvent,
            WindowPtr       pWindow)
{
    TQ3ViewerObject viewer = NULL;
    Boolean         wasHandled = false;
    GrafPtr         savedPort;
 
    if ((viewer = Window_GetViewer(pWindow)) == NULL) {
        return paramErr;
    }
 
    /*
     * There is a bug in versions 1.0.4 and earlier of the Viewer,
     * so the port has to be set and restored.
     */
    GetPort(&savedPort);
    SetPort((GrafPtr) pWindow);
 
    /* Handle mouse click inside Viewer */
    wasHandled = Q3ViewerEvent(viewer, pEvent);
 
    SetPort(savedPort);
 
    return noErr;
}
 
 
/*
 *  Events_ClickInDrag
 */
static
OSErr Events_ClickInDrag(
            EventRecord     *pEvent,
            WindowPtr       window)
{
    GrafPtr         savedPort;
    RgnHandle       tempRgn;
    Rect            dragRect;
 
    GetPort(&savedPort);
    SetPort((GrafPtr) window);
 
    tempRgn = GetGrayRgn();
    dragRect = (**tempRgn).rgnBBox;
 
    DragWindow(window, pEvent->where, &dragRect);
 
    SetPort(savedPort);
 
    return noErr;
}
 
 
//TODO  #if 0 /* SAVE */
/*
 *  Events_ClickInGrowBox
 */
static
OSErr Events_ClickInGrowBox (
            EventRecord     *pEvent,
            WindowPtr       pWindow)
{
    long        newSize;
    Rect        newRect,
                oldRect,
                screenRect;
    GrafPtr     savedPort;
 
    screenRect = (**GetGrayRgn()).rgnBBox;
    screenRect.left =
    screenRect.top  = 64;
 
    oldRect = newRect = pWindow->portRect;
 
    GetPort (&savedPort);
    SetPort (pWindow);
 
    newSize = GrowWindow (pWindow, pEvent->where, &screenRect);
    if (newSize != 0) {
        newRect.right   = newRect.left + ((short) newSize);
        newRect.bottom  = newRect.top  + ((short) (newSize >> 16));
        SizeWindow (pWindow,
                    (short)(newRect.right  - newRect.left),
                    (short)(newRect.bottom - newRect.top),
                    true);
        InvalRect (&oldRect);
    }
 
    SetPort (savedPort);
 
    return noErr;
}
 
 
#if 0 /* SAVE */
/*
 *  Events_ClickInZoomBox
 */
static
OSErr Events_ClickInZoomBox (
            EventRecord     *pEvent,
            WindowPtr       pWindow,
            short           windowPart)
{
    Rect    bounds;
    GrafPtr savedPort;
 
    GetPort(&savedPort);
    SetPort(pWindow);
 
    if (TrackBox(pWindow, pEvent->where, windowPart))
    {
        ZoomWindow(pWindow, windowPart, true);
 
        bounds = pWindow->portRect;
        InvalRect(&bounds);
    }
 
    SetPort(savedPort);
 
    return noErr;
}
#endif /* SAVE */
 
 
/*
 * Events_ClickInGoAway
 */
OSErr Events_ClickInGoAway(
            EventRecord             *pEvent,
            WindowPtr               *hWindow)
{
    OSErr   theErr = noErr;
 
    if (TrackGoAway(*hWindow, pEvent->where)) {
 
        theErr = Window_CloseViewer(hWindow);
    }
 
    return theErr;
}