Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
CodeWarrior (OS 9)/MyDeviceLoop.c
/* |
File: MyDeviceLoop.c |
Contains: This snippet shows how to write a device loop that |
works under System 7 and pre-7.0 systems. As |
described on pages 21-23 and 21-24 of Inside Mac |
volume VI, a device loop procedure searches all |
active screen devices, calling a drawing procedure |
whenever it encounters a screen that intersects |
the drawing region. In this app the drawing |
region is the app's window bounds and the chosen |
drawing procedure simply displays the screen's |
colors for every device the window bounds intersect. |
Written by: EL |
Copyright: Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved. |
You may incorporate this Apple sample source code into your program(s) without |
restriction. This Apple sample source code has been provided "AS IS" and the |
responsibility for its operation is yours. You are not permitted to redistribute |
this Apple sample source code as "Apple sample source code" after having made |
changes. If you're going to re-distribute the source, we require that you make |
it clear in the source that the code was descended from Apple sample source |
code, but that you've made changes. |
Change History (most recent first): |
08/2000 JM Carbonized, non-Carbon code is commented out |
for demonstration purposes. |
7/12/1999 KG Updated for Metrowerks Codewarror Pro 2.1 |
*/ |
#include "CarbonPrefix.h" |
#include <Dialogs.h> |
#include <Fonts.h> |
/* Constant Declarations */ |
#define WWIDTH 400 |
#define WHEIGHT 256 |
//#define WLEFT (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2) |
//#define WTOP (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2) |
enum PointSelector {topLeft, |
botRight}; |
/* Global Variable Definitions */ |
WindowPtr gWindow; |
void initMac(); |
void createWindow(); |
void doMyDeviceLoop(); |
void doDraw(); |
void doEventLoop(); |
void main(void) |
{ |
initMac(); |
createWindow(); |
doEventLoop(); |
} |
void initMac() |
{ |
/*MaxApplZone(); |
InitGraf( &qd.thePort ); |
InitFonts(); |
InitWindows(); |
InitMenus(); |
TEInit(); |
InitDialogs( nil );*/ |
InitCursor(); |
FlushEvents( 0, everyEvent ); |
} |
void createWindow() |
{ |
Rect rect; |
BitMap bitMap; |
int top, left; |
GetQDGlobalsScreenBits(&bitMap); |
top = (((bitMap.bounds.bottom - bitMap.bounds.top) - WHEIGHT) / 2); |
left = (((bitMap.bounds.right - bitMap.bounds.left) - WWIDTH) / 2); |
//SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT ); |
SetRect( &rect, left, top, left + WWIDTH, top + WHEIGHT ); |
gWindow = NewCWindow( 0L, &rect, "\pMyDeviceLoop", true, documentProc, |
(WindowPtr)-1L, true, 0L ); |
//SetPort( gWindow ); |
SetPortWindowPort( gWindow ); |
TextFont( kFontIDTimes ); |
TextSize( 48 ); |
TextMode( srcCopy ); |
} |
// Old version of doMyDeviceLoop, new version capable of supporting multiple monitors |
/* |
void doMyDeviceLoop() |
{ |
int depth; |
Rect gDeviceRect; |
Rect intersectingRect; |
GDHandle gDevice; |
//Point point; |
Rect tempRect1; |
// Get the handle to the first device in the list. |
gDevice = GetDeviceList(); |
// Loop through all the devices in the list. |
while (gDevice != nil) |
{ |
// Get the device's gdRect and convert it to local coordinates. |
gDeviceRect = (**gDevice).gdRect; |
depth = (**(**gDevice).gdPMap).pixelSize; |
GlobalToLocal( topLeft ); |
GlobalToLocal( (Point *)botRight ); |
// Check if the app's window rect intersects the device's, and if it |
// does, set the clip region's rect to the intersection, then DRAW! |
//if (SectRect( &gWindow->portRect, &gDeviceRect, &intersectingRect )) |
if (SectRect( GetPortBounds(GetWindowPort(gWindow), &tempRect1), &gDeviceRect, &intersectingRect )) |
{ |
ClipRect( &intersectingRect ); |
doDraw( depth, &intersectingRect ); |
} |
// Get the next device in the list. |
gDevice = GetNextDevice( gDevice ); |
} |
} |
*/ |
// New doMyDeviceLoop, now works with multiple monitors |
void doMyDeviceLoop() |
{ |
int depth; |
Rect gDeviceRect; |
Rect intersectingRect; |
GDHandle gDevice; |
//WindowRecord *windowRec = (WindowRecord *)gWindow; |
WindowPtr windowRec = gWindow; |
Rect windowRect; //= (**windowRec->contRgn).rgnBBox; |
RgnHandle rgnHandle = NewRgn(); |
GetWindowRegion(windowRec, kWindowContentRgn, rgnHandle); |
GetRegionBounds(rgnHandle, &windowRect); |
// Get the handle to the first device in the list. |
gDevice = GetDeviceList(); |
// Loop through all the devices in the list. |
while (gDevice != nil) |
{ |
// Get the device's gdRect */ |
gDeviceRect = (**gDevice).gdRect; |
depth = (**(**gDevice).gdPMap).pixelSize; |
// Check if the app's window rect intersects the device's, and if it |
// does, set the clip region's rect to the intersection, then DRAW! |
if (SectRect( &windowRect, &gDeviceRect, &intersectingRect )) |
{ |
// The intersectingRect is in global coords. Convert to local |
GlobalToLocal((Point *)&intersectingRect.top); |
GlobalToLocal((Point *)&intersectingRect.bottom); |
ClipRect( &intersectingRect ); |
doDraw( depth, &intersectingRect ); |
} |
// Get the next device in the list. |
gDevice = GetNextDevice( gDevice ); |
} |
DisposeRgn(rgnHandle); |
} |
void doDraw( depth) |
int depth; |
{ |
int i; |
int totalColors; |
int penThickness; |
//RGBColor color; |
CTabHandle ctable; |
Str255 string = "\pDirect Colors"; |
Rect tempRect1; |
if (depth > 8) |
{ |
BackColor( blackColor ); |
ForeColor( blueColor ); |
/* Draw text for direct colors mode. */ |
//EraseRect( &gWindow->portRect ); |
EraseRect( GetPortBounds(GetWindowPort(gWindow), &tempRect1)); |
//MoveTo( (gWindow->portRect.right - StringWidth( string )) / 2, 130 ); |
MoveTo((GetPortBounds(GetWindowPort(gWindow), &tempRect1)->right - StringWidth(string)) / 2, 130); |
DrawString( string ); |
} |
else |
{ |
/* Get the colortable at this depth. */ |
ctable = GetCTable( depth ); |
/* Set the line thickness to a fraction of the window height. */ |
totalColors = (2 << depth) / 2; |
//penThickness = gWindow->portRect.bottom / totalColors; |
penThickness = GetPortBounds(GetWindowPort(gWindow), &tempRect1)->bottom / totalColors; |
PenSize( 1, penThickness ); |
/* Now draw the colors at this depth. */ |
for (i = 0; i < totalColors; i++) |
{ |
RGBForeColor( &(**ctable).ctTable[i].rgb ); |
MoveTo( 0, i * penThickness ); |
//LineTo( gWindow->portRect.right, i * penThickness ); |
LineTo(GetPortBounds(GetWindowPort(gWindow), &tempRect1)->right, i * penThickness); |
} |
/* Release the colortable memory. */ |
DisposeCTable( ctable ); |
} |
} |
void doEventLoop() |
{ |
EventRecord event; |
WindowPtr window; |
short clickArea; |
Rect screenRect; |
for (;;) |
{ |
if (WaitNextEvent( everyEvent, &event, 0, nil )) |
{ |
if (event.what == mouseDown) |
{ |
clickArea = FindWindow( event.where, &window ); |
if (clickArea == inDrag) |
{ |
//screenRect = (**GetGrayRgn()).rgnBBox; |
GetRegionBounds(GetGrayRgn(), &screenRect); |
DragWindow( window, event.where, &screenRect ); |
} |
else if (clickArea == inContent) |
{ |
if (window != FrontWindow()) |
SelectWindow( window ); |
} |
else if (clickArea == inGoAway) |
if (TrackGoAway( window, event.where )) |
return; |
} |
else if (event.what == updateEvt) |
{ |
window = (WindowPtr)event.message; |
//SetPort( window ); |
SetPortWindowPort( window ); |
BeginUpdate( window ); |
doMyDeviceLoop(); |
EndUpdate( window ); |
} |
} |
} |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-10-10