TS3Events.c

/*
 *  File:       TS3Events.c
 *
 *  Contents:   Handles the events.
 *
 *  Copyright © 1996 Apple Computer, Inc.
 */
 
#include <assert.h>
 
#include <AppleEvents.h>
#include <Events.h>
#include <ToolUtils.h>
#include <Types.h>
#include <Windows.h>
 
#include "TS3Events.h"
#include "TS3Main.h"
#include "TS3Menu.h"
#include "TS3Window.h"
 
 
static void Events_MouseDown(
    const EventRecord*      inEvent);
 
static void Events_KeyDown(
    const EventRecord*      inEvent);
 
static void Events_AutoKey(
    const EventRecord*      inEvent);
 
static void Events_Update(
    const EventRecord*      inEvent);
 
static void Events_Activate(
    const EventRecord*      inEvent);
 
static void Events_OS(
    const EventRecord*      inEvent);
 
static void Events_HighLevel(
    const EventRecord*      inEvent);
 
 
/* =============================================================================
 *      Events_Init (external)
 *
 *  Initializes the events stuff.
 * ========================================================================== */
void Events_Init(
    void)
{
    FlushEvents(everyEvent, 0);
}
 
 
/* =============================================================================
 *      Events_Exit (external)
 *
 *  Prepares for exit.
 * ========================================================================== */
void Events_Exit(
    void)
{
}
 
 
/* =============================================================================
 *      Events_Process (external)
 *
 *  Processes any pending events.
 * ========================================================================== */
void Events_Process(
    void)
{
    UInt32                  sleep;
    EventRecord             ev;
    Boolean                 consumed;
    
    Window_GetSleep(FrontWindow(), &sleep);
    
    while (WaitNextEvent(everyEvent, &ev, sleep, NULL))
    {
        Window_ConsumeEvent(FrontWindow(), &ev, &consumed);
        
        if (!consumed)
        {
            switch (ev.what)
            {
                case mouseDown:
                    Events_MouseDown(&ev);
                break;
                
                case keyDown:
                    Events_KeyDown(&ev);
                break;
                
                case autoKey:
                    Events_AutoKey(&ev);
                break;
                
                case updateEvt:
                    Events_Update(&ev);
                break;
                
                case activateEvt:
                    Events_Activate(&ev);
                break;
                
                case osEvt:
                    Events_OS(&ev);
                break;
                
                case kHighLevelEvent:
                    Events_HighLevel(&ev);
                break;
            }
        }
    }
    
    // Pass on a NULL event when WaitNextEvent returns false to keep dialog caret blinking
    ev.what = nullEvent;
    Window_ConsumeEvent(FrontWindow(), &ev, &consumed);
}
 
 
/* =============================================================================
 *      Events_MouseDown (internal)
 *
 *  Processes a mouse-down event.
 * ========================================================================== */
void Events_MouseDown(
    const EventRecord*      inEvent)
{
    short                   part;
    WindowPtr               wind;
    long                    menu;
    
    assert(inEvent != NULL);
    
    part = FindWindow(inEvent->where, &wind);
    
    switch (part)
    {
        case inMenuBar:
            menu = MenuSelect(inEvent->where);
            Menu_Select(HiWord(menu), LoWord(menu));
        break;
            
        case inSysWindow:
            SystemClick(inEvent, wind);
        break;
            
        case inDrag:
            DragWindow(wind, inEvent->where, &qd.screenBits.bounds);
        break;
            
        case inGoAway:
            Main_LastRoundup();
        break;
                
        case inContent:
            if (wind == FrontWindow())
            {
                Window_MouseDown(wind, inEvent->where);
            }
            else
            {
                SelectWindow(wind);
            }
        break;
            
        case inGrow:
            // nothing yet...
        break;
    }
}
 
 
/* =============================================================================
 *      Events_KeyDown (internal)
 *
 *  Processes a key-down event.
 * ========================================================================== */
void Events_KeyDown(
    const EventRecord*      inEvent)
{
    unsigned long           ch;
    unsigned long           cap;
    long                    menu;
    
    assert(inEvent != NULL);
    
    ch = inEvent->message & charCodeMask;
    cap = (inEvent->message & keyCodeMask) >> 8;
    
    if ((inEvent->modifiers & cmdKey) == 0)
    {
        Window_KeyDown(FrontWindow(), ch, cap, inEvent->modifiers, false);
    }
    else
    {
        menu = MenuKey(ch);
        Menu_Select(HiWord(menu), LoWord(menu));
    }
}
 
 
/* =============================================================================
 *      Events_AutoKey (internal)
 *
 *  Processes an auto-key event.
 * ========================================================================== */
void Events_AutoKey(
    const EventRecord*      inEvent)
{
    unsigned long           ch;
    unsigned long           cap;
    
    assert(inEvent != NULL);
    
    ch = inEvent->message & charCodeMask;
    cap = (inEvent->message & keyCodeMask) >> 8;
    
    if ((inEvent->modifiers & cmdKey) == 0)
    {
        Window_KeyDown(FrontWindow(), ch, cap, inEvent->modifiers, true);
    }
}
 
 
/* =============================================================================
 *      Events_Update (internal)
 *
 *  Processes an update event.
 * ========================================================================== */
void Events_Update(
    const EventRecord*      inEvent)
{
    WindowPtr               wind;
    
    assert(inEvent != NULL);
    
    wind = (WindowPtr) inEvent->message;
    
    if (Window_IsMine(wind))
    {
        SetPort(wind);
        BeginUpdate(wind);
        Window_Update(wind);
        EndUpdate(wind);
    }
}
 
 
/* =============================================================================
 *      Events_Activate (internal)
 *
 *  Processes an activate event.
 * ========================================================================== */
void Events_Activate(
    const EventRecord*      inEvent)
{
    WindowPtr               wind;
    
    assert(inEvent != NULL);
    
    wind = (WindowPtr) inEvent->message;
    
    if (inEvent->modifiers & activeFlag)
    {
        Window_Activate(wind);
    }
    else
    {
        Window_Deactivate(wind);
    }
}
 
 
/* =============================================================================
 *      Events_OS (internal)
 *
 *  Processes an OS event.
 * ========================================================================== */
void Events_OS(
    const EventRecord*      inEvent)
{
    assert(inEvent != NULL);
    
    if ((inEvent->message >> 24) & 0xFF == suspendResumeMessage)
    {
        // nothing yet...
    }
}
 
 
/* =============================================================================
 *      Events_HighLevel (internal)
 *
 *  Processes a high-level event.
 * ========================================================================== */
void Events_HighLevel(
    const EventRecord*      inEvent)
{
    assert(inEvent != NULL);
    
    AEProcessAppleEvent(inEvent);
}