home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
macintosh-c
/
macc-carbon-demos-nonbinhex.sit
/
macc-carbon-demos-nonbinhex
/
chap02-demo
/
LowEvents.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-05-04
|
7KB
|
271 lines
// *******************************************************************************************
// LowEvents.c CLASSIC EVENT MODEL
// *******************************************************************************************
//
// This program contains a main event loop function, together with subsidiary functions which
// perform nominal handling only of low-level and Operating System events. It opens a window
// in which the types of all received low-level and Operating System events are displayed. It
// terminates when the user clicks the window's close box.
//
// Event handling is only nominal in this program because its main purpose is to demonstrate
// the basics of an application's main event loop. Programs in later chapters demonstrate
// the full gamut of individual event handling.
//
// The program utilises the following resources:
//
// • A 'plst' resource.
//
// • A 'WIND' resource (purgeable).
//
// • A 'SIZE' resource with the acceptSuspendResumeEvents, canBackground,
// doesActivateOnFGSwitch, and isHighLevelEventAware flags set.
//
// *******************************************************************************************
// ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
#include <Carbon.h>
// …………………………………………………………………………………………………………………………………………………………………………………………………………………………… defines
#define rWindowResource 128
#define topLeft(r) (((Point *) &(r))[0])
#define botRight(r) (((Point *) &(r))[1])
// …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
Boolean gDone;
RgnHandle gCursorRegionHdl;
// …………………………………………………………………………………………………………………………………………………………………………………………… function prototypes
void main (void);
void doPreliminaries (void);
void doNewWindow (void);
void eventLoop (void);
void doEvents (EventRecord *);
void doMouseDown (EventRecord *);
void doUpdate (EventRecord *);
void doOSEvent (EventRecord *);
void drawEventString (Str255);
void doAdjustCursor (WindowRef);
// ************************************************************************************** main
void main(void)
{
doPreliminaries();
doNewWindow();
eventLoop();
}
// *************************************************************************** doPreliminaries
void doPreliminaries(void)
{
MoreMasterPointers(48);
InitCursor();
FlushEvents(everyEvent,0);
}
// ******************************************************************************* doNewWindow
void doNewWindow(void)
{
WindowRef windowRef;
if(!(windowRef = GetNewCWindow(rWindowResource,NULL,(WindowRef) -1)))
{
SysBeep(10);
ExitToShell();
}
SetPortWindowPort(windowRef);
TextSize(10);
}
// ********************************************************************************* eventLoop
void eventLoop(void)
{
EventRecord eventStructure;
Boolean gotEvent;
gDone = false;
gCursorRegionHdl = NewRgn();
doAdjustCursor(FrontWindow());
while(!gDone)
{
gotEvent = WaitNextEvent(everyEvent,&eventStructure,180,gCursorRegionHdl);
if(gotEvent)
doEvents(&eventStructure);
else
{
if(eventStructure.what == nullEvent)
drawEventString("\p • nullEvent");
}
}
}
// ********************************************************************************** doEvents
void doEvents(EventRecord *eventStrucPtr)
{
switch(eventStrucPtr->what)
{
case mouseDown:
drawEventString("\p • mouseDown");
doMouseDown(eventStrucPtr);
break;
case mouseUp:
drawEventString("\p • mouseUp");
break;
case keyDown:
drawEventString("\p • keyDown");
break;
case autoKey:
drawEventString("\p • autoKey");
break;
case updateEvt:
drawEventString("\p • updateEvt");
doUpdate(eventStrucPtr);
break;
case activateEvt:
drawEventString("\p • activateEvt");
break;
case osEvt:
drawEventString("\p • osEvt - ");
doOSEvent(eventStrucPtr);
break;
}
}
// ******************************************************************************* doMouseDown
void doMouseDown(EventRecord *eventStrucPtr)
{
WindowPartCode partCode;
WindowRef windowRef;
partCode = FindWindow(eventStrucPtr->where,&windowRef);
switch(partCode)
{
case inContent:
if(windowRef != FrontWindow())
SelectWindow(windowRef);
break;
case inDrag:
DragWindow(windowRef,eventStrucPtr->where,NULL);
doAdjustCursor(windowRef);
break;
case inGoAway:
if(TrackGoAway(windowRef,eventStrucPtr->where))
gDone = true;
break;
}
}
// ********************************************************************************** doUpdate
void doUpdate(EventRecord *eventStrucPtr)
{
BeginUpdate((WindowRef) eventStrucPtr->message);
EndUpdate((WindowRef) eventStrucPtr->message);
}
// ********************************************************************************* doOSEvent
void doOSEvent(EventRecord *eventStrucPtr)
{
Cursor arrow;
switch((eventStrucPtr->message >> 24) & 0x000000FF)
{
case suspendResumeMessage:
if((eventStrucPtr->message & resumeFlag) == 1)
{
SetCursor(GetQDGlobalsArrow(&arrow));
DrawString("\pResume");
}
else
DrawString("\pSuspend");
break;
case mouseMovedMessage:
doAdjustCursor(FrontWindow());
DrawString("\pMouse-moved");
break;
}
}
// *************************************************************************** drawEventString
void drawEventString(Str255 eventString)
{
WindowRef windowRef;
RgnHandle tempRegion;
Rect portRect;
windowRef = FrontWindow();
tempRegion = NewRgn();
GetWindowPortBounds(windowRef,&portRect);
ScrollRect(&portRect,0,-15,tempRegion);
DisposeRgn(tempRegion);
MoveTo(8,340);
DrawString(eventString);
}
// **************************************************************************** doAdjustCursor
void doAdjustCursor(WindowRef windowRef)
{
RgnHandle myArrowRegion;
RgnHandle myIBeamRegion;
Rect cursorRect;
Point mousePt;
Cursor arrow;
myArrowRegion = NewRgn();
myIBeamRegion = NewRgn();
SetRectRgn(myArrowRegion,-32768,-32768,32767,32767);
GetWindowPortBounds(windowRef,&cursorRect);
SetPortWindowPort(windowRef);
LocalToGlobal(&topLeft(cursorRect));
LocalToGlobal(&botRight(cursorRect));
RectRgn(myIBeamRegion,&cursorRect);
DiffRgn(myArrowRegion,myIBeamRegion,myArrowRegion);
GetGlobalMouse(&mousePt);
if(PtInRgn(mousePt,myIBeamRegion))
{
SetCursor(*(GetCursor(iBeamCursor)));
CopyRgn(myIBeamRegion,gCursorRegionHdl);
}
else
{
SetCursor(GetQDGlobalsArrow(&arrow));
CopyRgn(myArrowRegion,gCursorRegionHdl);
}
DisposeRgn(myArrowRegion);
DisposeRgn(myIBeamRegion);
}
// *******************************************************************************************