int main(int argc, char* argv[])
{
WindowRef aWindowRef;
IBNibRef nibRef;
OSStatus err;
EventTypeSpec aEventWindow = {kEventClassWindow, kEventWindowClose};
EventTypeSpec aEventCommand = {kEventClassCommand, kEventCommandProcess};
int aDisplayPixelsWide;
// Create a Nib reference
err = CreateNibReference(CFSTR("main"), &nibRef);
require_noerr( err, CantGetNibRef );
// Set the menu bar
err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
require_noerr( err, CantSetMenuBar );
err = CreateWindowFromNib (nibRef, CFSTR("MainWindow"), &aWindowRef);
require_noerr (err, CantCreateWindow);
DisposeNibReference (nibRef);
// Install Event Handlers
err = InstallWindowEventHandler (aWindowRef, NewEventHandlerUPP(MainWindow_EventHandler),
1, &aEventWindow, (void *)aWindowRef, NULL);
InstallApplicationEventHandler( NewEventHandlerUPP( App_EventHandler ),
GetEventTypeCount(aEventCommand), &aEventCommand, 0, NULL );
// Making Window
SetWindowTitleWithCFString (aWindowRef, CFSTR("FloatingScale"));
aDisplayPixelsWide = CGDisplayPixelsWide(kCGDirectMainDisplay);
SizeWindow (aWindowRef, aDisplayPixelsWide * 0.9, 4 + 12 * 4 + 2 * 3, false);
MoveWindow (aWindowRef, (int) aDisplayPixelsWide * 0.05, (int) aDisplayPixelsWide * 0.05, false);
ShowWindow (aWindowRef);
SelectWindow (aWindowRef);
DrawScale (aWindowRef);
RunApplicationEventLoop ();
HideWindow (aWindowRef);
DisposeWindow (aWindowRef);
CantCreateWindow:
CantSetMenuBar:
CantGetNibRef:
return err;
}
void DrawScale (WindowRef pWindowRef)
{
CGContextRef aContext;
int aDisplayPixelsWide, aDisplayPixelsHigh;
Rect aBounds;
CGRect aFillArea = { {10, 10}, {50, 50}};
float x, aFactor;
char aStr255[256] = "Hello, Quartz";
/* For retrieving machine model */
int mib[2];
size_t aLen;
char *aStringMachine, *aStringModel;
/* For retrieving system version */
long aResponse;
SetPortWindowPort(pWindowRef);
QDBeginCGContext(GetWindowPort(pWindowRef), &aContext);
aDisplayPixelsWide = CGDisplayPixelsWide(kCGDirectMainDisplay);
aDisplayPixelsHigh = CGDisplayPixelsHigh(kCGDirectMainDisplay);
GetWindowBounds (pWindowRef, kWindowContentRgn, &aBounds);
CGContextSetRGBFillColor (aContext, 1.0, 1.0, 1.0, 1.0);
aFillArea.origin.x = aFillArea.origin.y = 0;
aFillArea.size.width = aBounds.right - aBounds.left;
aFillArea.size.height = aBounds.bottom - aBounds.top;
CGContextFillRect (aContext, aFillArea);
CGContextSelectFont (aContext, "Courier", 12, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (aContext, kCGTextFill);
mib[0] = CTL_HW;
mib[1] = HW_MACHINE;
sysctl(mib, 2, NULL, &aLen, NULL, 0);
aStringMachine = malloc(aLen);
sysctl(mib, 2, aStringMachine, &aLen, NULL, 0);
mib[1] = HW_MODEL;
sysctl(mib, 2, NULL, &aLen, NULL, 0);
aStringModel = malloc(aLen);
sysctl(mib, 2, aStringModel, &aLen, NULL, 0);
if (gFlagScale == kScaleDPI72) {
aFactor = 1.0;
CGContextSetRGBFillColor (aContext, 0.5, 0.5, 0.0, 1.0);
gFlagScale = kScaleReal;
sprintf(aStr255,"Scale = 72dpi, ");
} else {
aFactor = DetermineScalingFactor(aStringModel);
CGContextSetRGBFillColor (aContext, 0.0, 0.5, 0.5, 1.0);
gFlagScale = kScaleDPI72;
sprintf(aStr255,"Scale = Real, ");
}
CGContextShowTextAtPoint(aContext, 4, 4, aStr255, strlen(aStr255));
Gestalt (gestaltSystemVersion, &aResponse);
sprintf(aStr255,"Display = %d x %d, Machine = %s, Model = %s, System = %#x",
aDisplayPixelsWide, aDisplayPixelsHigh, aStringMachine, aStringModel, (int)aResponse);
CGContextShowText(aContext, aStr255, strlen(aStr255));
sprintf(aStr255,"100 Pixels:");
CGContextShowTextAtPoint(aContext, 4, 16, aStr255, strlen(aStr255));
sprintf(aStr255,"Inch:");
CGContextShowTextAtPoint(aContext, 4, 30, aStr255, strlen(aStr255));
sprintf(aStr255,"Centimeter:");
CGContextShowTextAtPoint(aContext, 4, 44, aStr255, strlen(aStr255));
CGContextSetLineCap (aContext, kCGLineCapButt);
// Drawing Pixel scale
CGContextMoveToPoint(aContext, 96, 16);
CGContextAddLineToPoint(aContext, aBounds.right - aBounds.left - 8, 16);
CGContextStrokePath(aContext);
for (x = 96; x < aBounds.right - aBounds.left - 8; x += 100) {
CGContextMoveToPoint(aContext, x, 16);
CGContextAddLineToPoint(aContext, x, 16 + 8);
CGContextStrokePath(aContext);
}
// Drawing Inch scale
CGContextMoveToPoint(aContext, 96, 30);
CGContextAddLineToPoint(aContext, aBounds.right - aBounds.left - 8, 30);
CGContextStrokePath(aContext);
for (x = 96; x < aBounds.right - aBounds.left - 8; x += 72 / aFactor) {
CGContextMoveToPoint(aContext, x, 30);
CGContextAddLineToPoint(aContext, x, 30 + 8);
CGContextStrokePath(aContext);
}
// Drawing Centimeter scale
CGContextMoveToPoint(aContext, 96, 44);
CGContextAddLineToPoint(aContext, aBounds.right - aBounds.left - 8, 44);
CGContextStrokePath(aContext);
for (x = 96; x < aBounds.right - aBounds.left - 8; x += 72 / 2.54 / aFactor) {
CGContextMoveToPoint(aContext, x, 44);
CGContextAddLineToPoint(aContext, x, 44 + 8);
CGContextStrokePath(aContext);
}
CGContextFlush(aContext);
QDEndCGContext(GetWindowPort(pWindowRef), &aContext);
return;
}