mirror of
https://github.com/aaronsgiles/uuUndo.git
synced 2024-11-22 01:32:02 +00:00
1 line
18 KiB
C
1 line
18 KiB
C
|
#include <Folders.h>
#include <ImageCompression.h>
#include <AppleEvents.h>
#include "DSDialogs.h"
#include "DSUserProcs.h"
#include "DSUtilsASG.h"
PrefsHandle gPrefs = nil;
Prefs gDefaultPrefs = {
kVersion,
false,
false,
{ 0, 0 },
{ 0, 0 }
};
Handle gMapResource = nil;
extern void DoMenu ( long retVal );
#define kPrefsFileName "\puuUndo Preferences"
static Fixed gProgressPercent = 0;
//=====================================================================================
// pascal OSErr GenericProgress(short theMessage, Fixed thePercent, long refCon)
//=====================================================================================
// Generic progress procedure, which displays a dialog with a progress bar and a cancel
// button. This function can be called as often as desired; the display is never
// updated more than 6 times/second.
//=====================================================================================
pascal OSErr GenericProgress(short theMessage, Fixed thePercent, long refCon)
{
static UserItemUPP gUpdateBar = nil;
static DialogPtr gTheDialog = nil;
static long gLastTime;
OSErr theErr = noErr;
GrafPtr oldPort;
Point thePoint;
long ticks;
GetPort(&oldPort);
switch (theMessage) {
case codecProgressOpen:
if (gTheDialog = GetNewDialog(rProgressDialog, nil, (WindowPtr)-1)) {
SetPort(gTheDialog);
// set up the user item handler, either for indefinite progress
// (thePercent == kIndefProgress) or for standard progress
if (!gUpdateBar) gUpdateBar = NewUserItemProc((ProcPtr)UpdateBar);
SetItemHandle(gTheDialog, progressUser, (Handle)gUpdateBar);
SetWindowPosition(gTheDialog, (*gPrefs)->progressLoc);
UserDialogActivate(FrontWindow(), false);
ShowWindow(gTheDialog);
DrawDialog(gTheDialog);
}
// reset the last time so that we draw right away, and update the initial
// percentage
gLastTime = 0;
theErr = GenericProgress(codecProgressForceUpdatePercent, 0, refCon);
break;
case codecProgressForceUpdatePercent:
gLastTime = 0;
case codecProgressUpdatePercent:
if (gTheDialog) {
// update our user item no more than 3x a second; do this by setting the
// gProgressPercent global, and forcing an update of the bar userItem
if ((TickCount() - gLastTime) < 10L) break;
gLastTime = TickCount();
gProgressPercent = thePercent;
UpdateDialogItem(gTheDialog, progressUser);
}
break;
case codecProgressClose:
// display the final percentage and delay for an instant
GenericProgress(codecProgressForceUpdatePercent, 0x10000, refCon);
Delay(1, &ticks);
GetWindowPosition(gTheDialog, &thePoint);
(*gPrefs)->progressLoc = thePoint;
if (gTheDialog) DisposeDialog(gTheDialog);
gTheDialog = nil;
UserDialogActivate(FrontWindow(), true);
break;
}
// restore the original port and check for aborts
SetPort(oldPort);
if (theErr == noErr && theMessage != codecProgressForceUpdatePercent) theErr = (CheckAbort()) ? codecAbortErr : noErr;
// if there was an abort signalled, and the dialog is visible, hide it immediately
// to give better user feedback
if (theErr == codecAbortErr && gTheDialog && ((WindowPeek)gTheDialog)->visible) HideWindow(gTheDialog);
return theErr;
}
/*
* CheckAbort()
*
* Purpose: Scans the event queue for an aborting keypress
* Inputs: none
* Returns: true if we should abort
*
*/
Boolean CheckAbort()
{
static long gLastTime = 0;
Boolean aborted = false, callWNE = false;
EventRecord theEvent;
GrafPtr oldPort;
// if we're not in the background, ensure that we get at least 2 ticks of continuous
// operation before giving up a full amount of time to others
if ((TickCount() - gLastTime) >= 5) callWNE = true;
GetPort(&oldPort);
SetPort(FrontWindow());
// get the next event; if sleepTime is zero, check event availability first
if (callWNE || EventAvail(everyEvent, &theEvent)) {
// handle any pending events normally
while (WaitNextEvent(everyEvent, &theEvent, 0, nil)) {
// dispatch the event as appropriate
switch
|