TS3Window.c

/*
 *  File:       TS3Window.c
 *
 *  Copyright © 1996 Apple Computer, Inc.
 */
 
#include <assert.h>
 
#include "TS3Window.h"
 
enum {
    kWindowDataThumbprint = 'wdat'
};
 
 
typedef struct WindowData {
    unsigned long       thumbprint;
    void*               method[kWindowMethod_COUNT];
} WindowData;
 
 
static WindowData** Window_GetData(
    WindowPtr           inWindow);
 
static WindowMethodPtr Window_GetMethod(
    WindowPtr           inWindow,
    WindowMethod        inMethod);
 
 
/* =============================================================================
 *      Window_Init (external)
 *
 *  Initializes our window stuff.
 * ========================================================================== */
void Window_Init(
    void)
{
}
 
 
/* =============================================================================
 *      Window_Exit (external)
 *
 *  Cleans up.
 * ========================================================================== */
void Window_Exit(
    void)
{
}
 
 
/* =============================================================================
 *      Window_GetData (internal)
 *
 *  Returns the WindowData record associated with this window.  Returns NULL
 *  if the window doesn't have a WindowData record associated with it.
 * ========================================================================== */
WindowData** Window_GetData(
    WindowPtr           inWindow)
{
    WindowData**        windowData;
    
    assert(inWindow != NULL);
    
    windowData = (WindowData**) GetWRefCon(inWindow);
    
    if (windowData != NULL && (*windowData)->thumbprint != kWindowDataThumbprint)
    {
        windowData = NULL;
    }
    
    return windowData;
}
 
 
/* =============================================================================
 *      Window_GetMethod (internal)
 *
 *  Returns the function pointer for the given method ID.  The result may be
 *  NULL.
 * ========================================================================== */
WindowMethodPtr Window_GetMethod(
    WindowPtr           inWindow,
    WindowMethod        inMethod)
{
    WindowMethodPtr     result;
    WindowData**        windowData;
    
    assert(inMethod >= kWindowMethod_FIRST);
    assert(inMethod <  kWindowMethod_COUNT);
    
    result = NULL;
    
    windowData = Window_GetData(inWindow);
    if (windowData != NULL)
    {
        result = (*windowData)->method[inMethod];
    }
    
    return result;
}
 
 
/* =============================================================================
 *      Window_New (external)
 *
 *  Makes a new WindowData structure for the window, points the window's refCon
 *  at it, and fills it in using the metahandler.  The metahandler should return
 *  a window method pointer that corresponds to the given method ID, or NULL
 *  if none.
 * ========================================================================== */
void Window_New(
    WindowPtr           inWindow,
    WindowMethodPtr     (*inMetaHandler)(WindowMethod inMethod))
{
    WindowData**        windowData;
    WindowMethod        method;
    
    assert(inWindow != NULL);
    assert(inMetaHandler != NULL);
    
    // Allocate the window data
    windowData = (WindowData**) NewHandle(sizeof(WindowData));
    assert(windowData != NULL);
    
    // Fill it in
    (*windowData)->thumbprint = kWindowDataThumbprint;
    
    for (method = kWindowMethod_FIRST; method < kWindowMethod_COUNT; method++)
    {
        (*windowData)->method[method] = (*inMetaHandler)(method);
    }
    
    // Point the window refcon at it
    SetWRefCon(inWindow, (long) windowData);
}
 
 
/* =============================================================================
 *      Window_Dispose (external)
 *
 *  Disposes of the window's WindowData structure.
 * ========================================================================== */
void Window_Dispose(
    WindowPtr           inWindow)
{
    WindowData**        windowData;
    
    windowData = Window_GetData(inWindow);
    if (windowData != NULL)
    {
        DisposeHandle((Handle) windowData);
        SetWRefCon(inWindow, 0);
    }
}
 
 
/* =============================================================================
 *      Window_IsMine (external)
 *
 *  Returns true if this is window has been set up with this stuff.
 * ========================================================================== */
Boolean Window_IsMine(
    WindowPtr           inWindow)
{
    return Window_GetData(inWindow) != NULL;
}
 
 
/* =============================================================================
 *      Window_GetSleep (external)
 *
 *  Sets *outSleep to the value that should be passed to WaitNextEvent when this
 *  is the front window.
 * ========================================================================== */
void Window_GetSleep(
    WindowPtr           inWindow,
    UInt32*             outSleep)
{
    WindowMethodPtr     getSleepMethod;
    
    assert(outSleep != NULL);
    
    *outSleep = 0xFFFFFFFF;
    
    if (inWindow != NULL)
    {
        getSleepMethod = Window_GetMethod(inWindow, kWindowMethod_GetSleep);
        if (getSleepMethod != NULL)
        {
            (*getSleepMethod)(inWindow, outSleep);
        }
    }
}
 
 
/* =============================================================================
 *      Window_ConsumeEvent (external)
 *
 *  Sets *outConsumed to true if the window consumed the event, or false if not.
 *  Overriding this method allows the window to do idle time things at null-
 *  event times, or dialog things.
 * ========================================================================== */
void Window_ConsumeEvent(
    WindowPtr           inWindow,
    const EventRecord*  inEvent,
    Boolean*            outConsumed)
{
    WindowMethodPtr     consumeEventMethod;
    
    assert(inEvent != NULL);
    assert(outConsumed != NULL);
    
    *outConsumed = false;
    
    if (inWindow != NULL)
    {
        consumeEventMethod = Window_GetMethod(inWindow, kWindowMethod_ConsumeEvent);
        if (consumeEventMethod != NULL)
        {
            (*consumeEventMethod)(inWindow, inEvent, outConsumed);
        }
    }
}
 
 
/* =============================================================================
 *      Window_MouseDown (external)
 *
 *  Processes a mouse-down in the window content area.
 * ========================================================================== */
void Window_MouseDown(
    WindowPtr           inWindow,
    Point               inWhere)
{
    WindowMethodPtr     mouseDownMethod;
    
    if (inWindow != NULL)
    {
        mouseDownMethod = Window_GetMethod(inWindow, kWindowMethod_MouseDown);
        if (mouseDownMethod != NULL)
        {
            (*mouseDownMethod)(inWindow, inWhere);
        }
    }
}
 
 
/* =============================================================================
 *      Window_KeyDown (external)
 *
 *  Processes a key-down or auto-key event.
 * ========================================================================== */
void Window_KeyDown(
    WindowPtr           inWindow,
    char                inChar,
    char                inKeyCap,
    short               inModifiers,
    Boolean             inAutoKey)
{
    WindowMethodPtr     keyDownMethod;
    
    if (inWindow != NULL)
    {
        keyDownMethod = Window_GetMethod(inWindow, kWindowMethod_KeyDown);
        if (keyDownMethod != NULL)
        {
            (*keyDownMethod)(inWindow, inChar, inKeyCap, inModifiers, inAutoKey);
        }
    }
}
 
 
/* =============================================================================
 *      Window_Update (external)
 *
 *  Redraws the contents of the window in response to an update event.
 * ========================================================================== */
void Window_Update(
    WindowPtr           inWindow)
{
    WindowMethodPtr     updateMethod;
    
    if (inWindow != NULL)
    {
        updateMethod = Window_GetMethod(inWindow, kWindowMethod_Update);
        if (updateMethod != NULL)
        {
            (*updateMethod)(inWindow);
        }
    }
}
 
 
/* =============================================================================
 *      Window_Activate (external)
 *
 *  Handles window activation.
 * ========================================================================== */
void Window_Activate(
    WindowPtr           inWindow)
{
    WindowMethodPtr     activateMethod;
    
    if (inWindow != NULL)
    {
        activateMethod = Window_GetMethod(inWindow, kWindowMethod_Activate);
        if (activateMethod != NULL)
        {
            (*activateMethod)(inWindow);
        }
    }
}
 
 
/* =============================================================================
 *      Window_Deactivate (external)
 *
 *  Handles window deactivation.
 * ========================================================================== */
void Window_Deactivate(
    WindowPtr           inWindow)
{
    WindowMethodPtr     deactivateMethod;
    
    if (inWindow != NULL)
    {
        deactivateMethod = Window_GetMethod(inWindow, kWindowMethod_Deactivate);
        if (deactivateMethod != NULL)
        {
            (*deactivateMethod)(inWindow);
        }
    }
}