home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-13 | 11.7 KB | 553 lines | [TEXT/CWIE] |
- //===============================================================================
- //===============================================================================
- //
- // TestPickerInterface.c
- //
- // john calhoun 1997
- //
- //===============================================================================
- //===============================================================================
-
-
- #include "TestPicker.h"
- #include "GetMonitorProfile.h"
-
-
- Rect bounceRect;
- GWorldPtr theGWorld;
- UInt32 wasTicks;
- SInt16 x1, y1, x2, y2, deltaX1, deltaX2, deltaY1, deltaY2;
-
-
- OSErr GetProfileRef (CMProfileRef *);
-
-
- //===================================================================== Functions
- //--------------------------------------------------------------------- UpdateVennColors
-
- void UpdateVennColors (void)
- {
- RGBColor theColor;
-
- theColor.red = theRGBColor.red;
- theColor.green = 0;
- theColor.blue = 0;
- RGBForeColor(&theColor);
- PaintRgn(regionR);
-
- theColor.red = 0;
- theColor.green = theRGBColor.green;
- theColor.blue = 0;
- RGBForeColor(&theColor);
- PaintRgn(regionG);
-
- theColor.red = 0;
- theColor.green = 0;
- theColor.blue = theRGBColor.blue;
- RGBForeColor(&theColor);
- PaintRgn(regionB);
-
- theColor.red = theRGBColor.red;
- theColor.green = theRGBColor.green;
- theColor.blue = 0;
- RGBForeColor(&theColor);
- PaintRgn(regionRG);
-
- theColor.red = 0;
- theColor.green = theRGBColor.green;
- theColor.blue = theRGBColor.blue;
- RGBForeColor(&theColor);
- PaintRgn(regionGB);
-
- theColor.red = theRGBColor.red;
- theColor.green = 0;
- theColor.blue = theRGBColor.blue;
- RGBForeColor(&theColor);
- PaintRgn(regionBR);
-
- theColor.red = theRGBColor.red;
- theColor.green = theRGBColor.green;
- theColor.blue = theRGBColor.blue;
- RGBForeColor(&theColor);
- PaintRgn(regionRGB);
-
- ForeColor(blackColor);
- }
-
- //--------------------------------------------------------------------- UpdateCMYKColors
-
- void UpdateCMYKColors (void)
- {
- CMYColor theCMYColor;
- RGBColor theColor;
- Str255 tempStr;
- GrafPtr wasPort;
-
- GetPort(&wasPort);
- SetPort(simpleWindow);
-
- theCMYColor.cyan = cmykColor.cmyk.cyan;
- theCMYColor.magenta = 0;
- theCMYColor.yellow = 0;
- CMY2RGB(&theCMYColor, &theColor);
- RGBForeColor(&theColor);
- PaintRect(&rectC);
-
- theCMYColor.cyan = 0;
- theCMYColor.magenta = cmykColor.cmyk.magenta;
- theCMYColor.yellow = 0;
- CMY2RGB(&theCMYColor, &theColor);
- RGBForeColor(&theColor);
- PaintRect(&rectM);
-
- theCMYColor.cyan = 0;
- theCMYColor.magenta = 0;
- theCMYColor.yellow = cmykColor.cmyk.yellow;
- CMY2RGB(&theCMYColor, &theColor);
- RGBForeColor(&theColor);
- PaintRect(&rectY);
-
- theColor.red = 65535 - cmykColor.cmyk.black;
- theColor.green = 65535 - cmykColor.cmyk.black;
- theColor.blue = 65535 - cmykColor.cmyk.black;
- RGBForeColor(&theColor);
- PaintRect(&rectK);
- ForeColor(blackColor);
-
- // Draw CMYK percentages.
- NumToString(((long)cmykColor.cmyk.cyan * 100L + 328) / 65535L, tempStr);
- PasStringConcat(tempStr, "\p%");
- MoveTo(rectC.left + 17, rectC.top + 20);
- DrawString(tempStr);
-
- NumToString(((long)cmykColor.cmyk.magenta * 100L + 328) / 65535L, tempStr);
- PasStringConcat(tempStr, "\p%");
- MoveTo(rectM.left + 17, rectM.top + 20);
- DrawString(tempStr);
-
- NumToString(((long)cmykColor.cmyk.yellow * 100L + 328) / 65535L, tempStr);
- PasStringConcat(tempStr, "\p%");
- MoveTo(rectY.left + 17, rectY.top + 20);
- DrawString(tempStr);
-
- if (cmykColor.cmyk.black < 32768)
- ForeColor(blackColor);
- else
- ForeColor(whiteColor);
- NumToString(((long)cmykColor.cmyk.black * 100L + 328) / 65535L, tempStr);
- PasStringConcat(tempStr, "\p%");
- MoveTo(rectK.left + 17, rectK.top + 20);
- DrawString(tempStr);
- ForeColor(blackColor);
-
- SetPort(wasPort);
- }
-
- //--------------------------------------------------------------------- UpdateBouncingLine
-
- void UpdateBouncingLine (void)
- {
- UInt32 tickDiff;
- GDHandle wasWorld;
- CGrafPtr wasCPort;
-
- tickDiff = (TickCount() - wasTicks) / 2;
-
- PenSize(kLineThick, kLineThick);
- RGBForeColor(&theRGBColor);
-
- if (!theGWorld)
- goto skipGWorldSetup;
-
- GetGWorld(&wasCPort, &wasWorld);
- LockPixels(GetGWorldPixMap(theGWorld));
- SetGWorld(theGWorld, 0L);
- RGBForeColor(&theRGBColor);
- SetGWorld(wasCPort, wasWorld);
-
- skipGWorldSetup:
-
- while (tickDiff > 0)
- {
- x1 += deltaX1;
- y1 += deltaY1;
- if (x1 > bounceRect.right)
- {
- deltaX1 = -deltaX1;
- x1 = bounceRect.right;
- }
- else if (x1 < bounceRect.left)
- {
- deltaX1 = -deltaX1;
- x1 = bounceRect.left;
- }
- if (y1 > bounceRect.bottom)
- {
- deltaY1 = -deltaY1;
- y1 = bounceRect.bottom;
- }
- else if (y1 < bounceRect.top)
- {
- deltaY1 = -deltaY1;
- y1 = bounceRect.top;
- }
-
- x2 += deltaX2;
- y2 += deltaY2;
- if (x2 > bounceRect.right)
- {
- deltaX2 = -deltaX2;
- x2 = bounceRect.right;
- }
- else if (x2 < bounceRect.left)
- {
- deltaX2 = -deltaX2;
- x2 = bounceRect.left;
- }
- if (y2 > bounceRect.bottom)
- {
- deltaY2 = -deltaY2;
- y2 = bounceRect.bottom;
- }
- else if (y2 < bounceRect.top)
- {
- deltaY2 = -deltaY2;
- y2 = bounceRect.top;
- }
-
- MoveTo(x1 >> 2, y1 >> 2);
- LineTo(x2 >> 2, y2 >> 2);
-
- if (!theGWorld)
- goto skipGWorldDraw;
-
- SetGWorld(theGWorld, 0L);
- MoveTo((x1 >> 2) - (76 >> 2), (y1 >> 2) - (240 >> 2));
- LineTo((x2 >> 2) - (76 >> 2), (y2 >> 2) - (240 >> 2));
- SetGWorld(wasCPort, wasWorld);
-
- skipGWorldDraw:
-
- tickDiff--;
- wasTicks = TickCount();
- }
-
- if (theGWorld)
- UnlockPixels(GetGWorldPixMap(theGWorld));
- PenSize(1, 1);
- ForeColor(blackColor);
- }
-
- //--------------------------------------------------------------------- UpdateMainWindowText
-
- void UpdateMainWindowText (void)
- {
- Rect destRect, srcRect;
- PicHandle thePict;
- Str255 messageStr, tempStr;
- long value;
- OSErr theErr;
-
- theErr = Gestalt(gestaltColorPickerVersion, &value);
- if (theErr)
- PasStringCopy("\pOriginal Color Picker.", messageStr);
- else
- {
- PasStringCopy("\pColor Picker version is: ", messageStr);
- NumToString((value / 256) / 16, tempStr);
- PasStringConcat(messageStr, tempStr);
- NumToString((value / 256) % 16, tempStr);
- PasStringConcat(messageStr, tempStr);
- PasStringConcat(messageStr, "\p.");
- NumToString((value % 256) / 16, tempStr);
- PasStringConcat(messageStr, tempStr);
- NumToString((value % 256) % 16, tempStr);
- PasStringConcat(messageStr, tempStr);
- }
-
- TextFace(bold);
- MoveTo(20, 16);
- DrawString(messageStr);
-
- // Draw text around venn diagram.
- PaintRgn(blackRgn);
- ForeColor(whiteColor);
- MoveTo(78, 258);
- DrawString("\pRed");
- MoveTo(35, 390);
- DrawString("\pGreen");
- MoveTo(120, 390);
- DrawString("\pBlue");
- ForeColor(blackColor);
-
- // Draw CMYK Rects.
- MoveTo(rectC.left + 13, rectC.top - 4);
- DrawString("\pCyan");
- MoveTo(rectM.left + 1, rectM.top - 4);
- DrawString("\pMagenta");
- MoveTo(rectY.left + 6, rectY.top - 4);
- DrawString("\pYellow");
- MoveTo(rectK.left + 10, rectK.top - 4);
- DrawString("\pBlack");
-
- // Draw PICT.
- SetRect(&destRect, 0, 0, 416, 312);
- OffsetRect(&destRect, windowBounds.right - (destRect.right + 8),
- windowBounds.bottom - (destRect.bottom + 8));
- thePict = GetPicture(129);
- if (thePict)
- {
- DrawPicture(thePict, &destRect);
- ReleaseResource((Handle)thePict);
- }
-
- // Draw bouncing line box.
- destRect.left = bounceRect.left >> 2;
- destRect.top = bounceRect.top >> 2;
- destRect.right = (bounceRect.right >> 2) + kLineThick;
- destRect.bottom = (bounceRect.bottom >> 2) + kLineThick;
- srcRect = destRect;
- OffsetRect(&srcRect, -srcRect.left, -srcRect.top);
- if (theGWorld)
- {
- LockPixels(GetGWorldPixMap(theGWorld));
- CopyBits(&((GrafPtr)theGWorld)->portBits, &simpleWindow->portBits,
- &srcRect, &destRect, srcCopy, 0L);
- UnlockPixels(GetGWorldPixMap(theGWorld));
- }
- else
- PaintRect(&destRect);
-
- InsetRect(&destRect, -2, -2);
- FrameRect(&destRect);
- }
-
- //--------------------------------------------------------------------- DoAKeyDownEvent
-
- void DoAKeyDownEvent (EventRecord *theEvent)
- {
- long menuAndItem;
- char theChar;
- Boolean commandDown;
-
- theChar = theEvent->message & charCodeMask;
- commandDown = ((theEvent->modifiers & cmdKey) != 0);
- if (commandDown)
- {
- menuAndItem = MenuKey(theChar);
- DoMenuChoice(menuAndItem);
- }
- }
-
- //--------------------------------------------------------------------- DoActivateEvent
-
- void DoActivateEvent (EventRecord *theEvent)
- {
- if (theEvent->modifiers & activeFlag)
- SelectWindow((WindowPtr)theEvent->message);
- }
-
- //--------------------------------------------------------------------- DoUpdateEvent
-
- Boolean DoUpdateEvent (EventRecord *theEvent)
- {
- GrafPtr wasPort;
-
- if ((WindowPtr)theEvent->message == simpleWindow)
- {
- GetPort(&wasPort);
- SetPort(simpleWindow);
-
- BeginUpdate(simpleWindow);
- UpdateMainWindowText();
- UpdateVennColors();
- UpdateCMYKColors();
- EndUpdate(simpleWindow);
-
- SetPort(wasPort);
- return (true);
- }
- else
- return (false);
- }
-
- //--------------------------------------------------------------------- GetProfileRef
-
- OSErr GetProfileRef (CMProfileRef *destProfile)
- {
- CMProfileLocation destLocation;
- OSErr theErr;
- Boolean sfGood;
-
- theErr = StandardGetProfile(&destLocation, &sfGood, cmRGBData);
- if (sfGood)
- theErr = CMOpenProfile(destProfile, &destLocation);
- else
- theErr = 1;
-
- return theErr;
- }
-
- //-------------------------------------------------------------- DoAppleMenu
- // Handle the Apple menu (About box and desk accessories).
-
- Boolean DoAppleMenu (short theItem)
- {
- Str255 daName;
- GrafPtr wasPort;
- short daNumber;
- Boolean handledIt;
-
- handledIt = false;
-
- switch (theItem)
- {
- case 1:
- break;
-
- default:
- GetMenuItemText(GetMenuHandle(128), theItem, daName);
- GetPort(&wasPort);
- daNumber = OpenDeskAcc(daName);
- SetPort(wasPort);
- handledIt = true;
- break;
- }
-
- return handledIt;
- }
-
- //-------------------------------------------------------------- DoPickerMenu
- // Handle the Picker menu.
-
- Boolean DoPickerMenu (short theItem)
- {
- CMProfileRef destProfile, inOutProfile;
- OSErr theErr;
- Boolean handledIt;
-
- handledIt = false;
-
- switch (theItem)
- {
- case 1: // GetColor().
- SimpleGetColor();
- handledIt = true;
- break;
-
- case 2: // PickColor().
- PickColorMovableModal(0L);
- handledIt = true;
- break;
-
- case 3: // HTML Picker Only.
- PickColorMovableModal('HTML');
- handledIt = true;
- break;
-
- case 4: // NPickColor().
- NPickColorMovableModal();
- handledIt = true;
- break;
-
- case 5: // CMYK Color Space.
- NPickColorCMYK();
- handledIt = true;
- break;
- }
-
- return handledIt;
- }
-
- //--------------------------------------------------------------------- DoMenuChoice
-
- Boolean DoMenuChoice (long menuChoice)
- {
- short theMenu, theItem;
- Boolean handledIt;
-
- handledIt = false;
- if (menuChoice == 0)
- return;
-
- theMenu = HiWord(menuChoice);
- theItem = LoWord(menuChoice);
-
- switch (theMenu)
- {
- case 128: // Apple menu.
- handledIt = DoAppleMenu(theItem);
- break;
-
- case 129: // File menu.
- if (theItem == 1)
- {
- quitting = true;
- handledIt = true;
- }
- break;
-
- case 130: // Edit menu.
- break;
-
- case 131: // Demo menu.
- handledIt = DoPickerMenu(theItem);
- break;
- }
-
- HiliteMenu(0);
-
- return handledIt;
- }
-
- //--------------------------------------------------------------------- EventLoop
-
- void EventLoop (void)
- {
- EventRecord theEvent;
- WindowPtr whichWindow;
- long menuChoice;
- short thePart;
- Boolean whoCares;
-
- if (WaitNextEvent(everyEvent, &theEvent, 1L, nil))
- {
- switch (theEvent.what)
- {
- case mouseDown:
- thePart = FindWindow(theEvent.where, &whichWindow);
- switch (thePart)
- {
- case inSysWindow:
- SystemClick(&theEvent, whichWindow);
- break;
-
- case inMenuBar:
- menuChoice = MenuSelect(theEvent.where);
- DoMenuChoice(menuChoice);
- break;
-
- case inDrag:
- DragWindow(whichWindow, theEvent.where, &((*LMGetGrayRgn())->rgnBBox));
- break;
- }
- break;
-
- case keyDown:
- DoAKeyDownEvent(&theEvent);
- break;
-
- case activateEvt:
- DoActivateEvent(&theEvent);
- break;
-
- case updateEvt:
- whoCares = DoUpdateEvent(&theEvent);
- break;
- }
- }
-
- UpdateBouncingLine();
- }
-
-