Src/SpinCursor.c

/*                                  SpinCursor.c                                */
/*
 * SpinCursor.c
 * Copyright © 1993 Apple Computer Inc. All rights reserved.
 * This is based on a sample in Think Reference 2.0.
 */
#ifndef THINK_C             /* MPW includes         */
#include <Errors.h>
#include <Script.h>
#include <Types.h>
#include <Files.h>
#include <Resources.h>
#include <QuickDraw.h>
#include <Fonts.h>
#include <Events.h>
#include <Windows.h>
#include <ToolUtils.h>
#include <Memory.h>
#include <Menus.h>
#include <Lists.h>
#include <Printing.h>
#include <Dialogs.h>
#include <StandardFile.h>
#endif
#define kAnimationInterval      (6)         /* Ticks                */
/*
 * This is used by the animated cursor subroutine.
 */
typedef struct {
    unsigned short      nFrames;
    unsigned short      nextFrame;
    CursHandle          frame[1];
} ACUR_Record, *ACUR_Ptr, **ACUR_Handle;
ACUR_Handle             gACUR_Handle;
#define ACUR            (**gACUR_Handle)
unsigned long           gACUR_NextAnimation;
 
void                    SetupAnimatedCursor(
        short                       acurResID
    );
void                    SpinCursor(void);
 
 
/*
 * SetupAnimatedCursor
 * Build the animated cursor handle array (in global gACUR_Handle)
 */
void
SetupAnimatedCursor(
        short                       acurResID
    )
{
        short                   cursorID;
        short                   i;
 
        gACUR_Handle = (ACUR_Handle) GetResource('acur', acurResID);
        if (gACUR_Handle != NULL) {
            DetachResource((Handle) gACUR_Handle);
            for (i = 0; i < ACUR.nFrames; i++) {
                cursorID = ((unsigned long) ACUR.frame[i]) >> 16;
                ACUR.frame[i] = GetCursor(cursorID);
                if (ACUR.frame[i] == NULL)
                    break;
                HNoPurge((Handle) ACUR.frame[i]);
            }
            ACUR.nFrames = i;
            ACUR.nextFrame = 0;
            if (i == 0) {
                DisposeHandle((Handle) gACUR_Handle);
                gACUR_Handle = NULL;
            }
        }
        gACUR_NextAnimation = 0;
}
 
/*
 * SpinCursor
 * This is called repeatedly to change the cursor animation.
 */
void
SpinCursor(void)
{
        unsigned long               now;
        
        if (gACUR_Handle != NULL) {
            now = TickCount();
            if (now > gACUR_NextAnimation) {
                gACUR_NextAnimation = now + kAnimationInterval;
                if (ACUR.nextFrame >= ACUR.nFrames)
                    ACUR.nextFrame = 0;
                SetCursor(*ACUR.frame[ACUR.nextFrame]);
                ++ACUR.nextFrame;
            }
        }
}