/* CMUgraphics Package Macintosh version 1.0 Copyright (c) 1998 by Mark Stehlik. All rights reserved. Portions of the code in this file may be derived from code written by Geoff Washburn, Patrick Doane, Jim Roberts, and Lily Hou. Please send all bug reports via e-mail to Mark Stehlik at 'mjs@cs.cmu.edu'. Redistribution and use in source and binary forms of this library, with or without modification, are permitted provided that the following conditions are met: 1. Redistribution of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistribution in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. When redistributing modified version of this library it must carry prominent notices stating the name of individual(s) that altered the files, the nature of the modifications, and the date they were performed. 4. No fee is charged for redistribution or use without prior written permission from the author. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This file was last modified on 10.06.1998 */ #include #include "127Graphics.h" WindowPtr DrawingWindow; int CurrentColor = 10; const SInt16 MAGICVALUE = '127g'; // Pat's new const void SelectConsoleWindow() { // We assume the first window to not have the magic // value to be the console WindowRef scan = FrontWindow(); while (scan && GetWindowKind(scan) == MAGICVALUE) { scan = GetNextWindow(scan); } if (scan != NULL) { SelectWindow(scan); } } void SelectDrawingWindow() { WindowRef scan = FrontWindow(); while (scan) { if (GetWindowKind(scan) == MAGICVALUE) { SelectWindow(scan); } scan = GetNextWindow(scan); } } Point GetWindowDimensions(int windowID) { Point dim; // First field of the 'WIND' reource is the bounds Handle windResource = ::GetResource('WIND', windowID); if (windResource != NULL) { Rect bounds = **(Rect**)windResource; dim.h = bounds.right - bounds.left; dim.v = bounds.bottom - bounds.top; } else { dim.h = 0; dim.v = 0; } return dim; } void DrawStringVariable(apstring word) {char pascalWord[200]; int wordLength = word.length(); pascalWord[0] = wordLength; // the 0th element of a pascal string // stores the length of the string for (int i = 0; i < wordLength; i++) { pascalWord[i+1] = word[i]; } DrawString((ConstStr255Param)pascalWord); // this is the magic that converts our string // thru the Mac OS } void GetKey(char & dir) // allows input without requiring to flush the buffer {EventRecord event; if (GetNextEvent(everyEvent, &event)) if (event.what == keyDown) dir = char(event.message & charCodeMask); else dir = ' '; } void SetColor(int whichcolor) { RGBColor color; color.red = 0; color.green = 0; color.blue = 0; CurrentColor = whichcolor; switch(whichcolor){ case RED: color.red = 65535; break; case GREEN: color.green = 65535; break; case BLUE: color.blue = 65535; break; case YELLOW:color.red = 65535; color.green = 65535; break; case PINK: color.red = 65535; color.green = 5837; color.blue = 22993; break; case PURPLE:color.red = 65535; color.blue = 65535; break; case BROWN: color.red = 65535; color.green = 24000; color.blue = 6000; break; case AQUA: color.red = 22158; color.green = 62402; color.blue = 65535; break; case WHITE: color.red = 65535; color.green = 65535; color.blue = 65535; break; case DKGREY: color.red = 37941; color.green = 37941; color.blue = 37941; break; case LTBLUE:color.red = 21519; color.green = 37173; color.blue = 47427; break; case ALMOND:color.red = 63810; color.green = 61003; color.blue = 45672; break; case LTGREY: color.red = 46437; color.green = 46603; color.blue = 47103; break; case TAN: color.red = 54702; color.green = 47663; color.blue = 39973; break; case DKGREEN:color.red = 3655; color.green = 31905; color.blue = 0; break; case BLACK: break; default: cout << "Bogus color!\n"; break; } RGBForeColor(&color); } void ToolboxInit( void ) { InitGraf( &qd.thePort ); InitFonts(); InitWindows(); InitMenus(); TEInit(); InitDialogs( 0L ); InitCursor(); } void WindowInit(int windowID) { WindowPtr window; window = GetNewCWindow(windowID,nil,kMoveToFront); if (window == nil) { SysBeep(10); ExitToShell(); } SetWindowKind(window, MAGICVALUE);// <-- new code ShowWindow(window); SetPort(window); DrawingWindow = window; } void PlaySound(int sndID) { SndListHandle mySndHandle; SndChannelPtr mySndChan; OSErr myErr; mySndChan = nil; mySndHandle = (SndListHandle) GetResource('snd ',sndID); if (mySndHandle != nil ) { myErr = SndPlay(mySndChan,mySndHandle,kAsync); if (myErr != noErr) { cout << "SoSndPlay Error" << endl; } } else { cout <<"Error in getting sound resource!!!\n\n\n" << endl; } } Point GetMouseLocation() { Point pt; GetMouse(&pt); return (pt); } void GetMouseLocation (int &x, int &y) { Point pt; GetMouse(&pt); x = pt.h; y = pt.v; } int FoundAClick(Point *pt) { int ok; EventRecord theEvent; WindowPtr theWindow; int windowCode; ok = GetNextEvent (everyEvent, &theEvent); windowCode = FindWindow (theEvent.where, &theWindow); if (theEvent.what == mouseDown) switch (windowCode) { case inContent: SetPort(theWindow); GlobalToLocal(& (theEvent.where)); (*pt).h = theEvent.where.h; (*pt).v = theEvent.where.v; return 1; break; } return 0; } Point GetClick() { Point pt; InitCursor(); while (!FoundAClick(&pt)) { ; // do nothing until a click occurs } return pt; } void GetClick(int &x, int &y) { Point pt; InitCursor(); while (!FoundAClick(&pt)) { ; // do nothing until a click occurs } x = pt.h; y = pt.v; } void /* top left and bottom right x and y coords */ RectangleDraw(int x1, int y1 , int x2, int y2) { Rect r; int temp; if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { temp = y1; y1 = y2; y2 = temp; } SetRect(&r, x1,y1,x2,y2); PaintRect(&r); } /*wire frame rectangle */ /* given x, y points and radius, inverts the pixels in the rect */ void RectangleInvert(int x1, int y1 , int x2, int y2) { Rect r; int temp; if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { temp = y1; y1 = y2; y2 = temp; } SetRect(&r, x1,y1,x2,y2); InvertRect(&r); } void /* top left and bottom right x and y coords */ RectangleFrame(int x1, int y1 , int x2, int y2) { Rect r; int temp; if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { temp = y1; y1 = y2; y2 = temp; } SetRect(&r, x1,y1,x2,y2); FrameRect(&r); } /* top left and bottom right x and y coords */ void /* given x, y points and corner width and height draws round rectangel */ RoundRectangleDraw(int x1, int y1, int x2, int y2, int ovWd, int ovHt) { Rect r; int temp; if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { temp = y1; y1 = y2; y2 = temp; } SetRect(&r, x1, y1, x2, y2); PaintRoundRect(&r, ovWd, ovHt); } /* given x, y points and corner width and height, inverts the round rectangle*/ void RoundRectangleInvert(int x1, int y1, int x2, int y2, int ovWd, int ovHt) { Rect r; int temp; if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { temp = y1; y1 = y2; y2 = temp; } SetRect(&r, x1, y1, x2, y2); InvertRoundRect(&r, ovWd, ovHt); } /* given x, y points and corner width and height, draws wire frame round rectangle*/ void RoundRectangleFrame(int x1, int y1, int x2, int y2, int ovWd, int ovHt) { Rect r; int temp; if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { temp = y1; y1 = y2; y2 = temp; } SetRect(&r, x1, y1, x2, y2); FrameRoundRect(&r, ovWd, ovHt); } /* given x ,y points, and radius - draw filled circle */ void CircleDraw(int x, int y, int rad) { Rect r; SetRect(&r,x-rad,y-rad,x+rad,y+rad); PaintOval(&r); } /* given x, y points and radius, inverts the pixels in the rect */ void CircleInvert( int x, int y, int rad) { Rect r; SetRect(&r,x-rad,y-rad,x+rad,y+rad); InvertOval(&r); } /* given x ,y points, and radius - draw wire frame circle */ void CircleFrame(int x, int y, int rad) { Rect r; SetRect(&r,x-rad,y-rad,x+rad,y+rad); FrameOval(&r); } void OvalDraw(int x1, int y1 , int x2, int y2) { Rect r; int temp; if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { temp = y1; y1 = y2; y2 = temp; } SetRect(&r, x1, y1 , x2, y2); PaintOval(&r); } /* given x, y points and radius, inverts the pixels in the rect */ void OvalInvert(int x1, int y1 , int x2, int y2) { Rect r;int temp; if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { temp = y1; y1 = y2; y2 = temp; } SetRect(&r, x1, y1 , x2, y2); InvertOval(&r); } /* given x ,y points, and radius - draw wire frame oval */ void OvalFrame(int x1, int y1 , int x2, int y2) { Rect r;int temp; if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if (y1 > y2) { temp = y1; y1 = y2; y2 = temp; } SetRect(&r, x1, y1 , x2, y2); FrameOval(&r); } void LineDraw(int x1, int y1, int x2, int y2) { MoveTo(x1,y1); LineTo(x2,y2); } void ArcDraw( int x, int y,int radius,int startDegrees, int NumDegrees) { Rect r; SetRect (&r, x - radius, y - radius,x + radius, y + radius); PaintArc(&r,startDegrees,NumDegrees); } void ArcInvert( int x, int y,int radius,int startDegrees, int NumDegrees) { Rect r; SetRect (&r, x - radius, y - radius,x + radius, y + radius); InvertArc(&r,startDegrees,NumDegrees); } void ArcFrame( int x, int y,int radius,int startDegrees, int NumDegrees) { Rect r; SetRect (&r, x - radius, y - radius,x + radius, y + radius); FrameArc(&r,startDegrees,NumDegrees); } /* displays the x-y coord for drawing window */ void ShowWindowDimensions() { MoveTo(5,10); TextSize(10); DrawString("\p(10,10) "); TextSize(9); MoveTo(5,340); DrawString("\p(10,330) "); TextSize(9); MoveTo(460,340); DrawString("\p(500,330) "); LineDraw(10,10,10,330); LineDraw(10,330,500,330); } void NumToStr(int n, char str[]) { //Max value of int is 9999. int d1,d2,d3,d4; int foundNonZero = 0; d1 = n / 1000; d2 = n/100 %10; d3 = n/10 % 10; d4 = n % 10; str[0] = 4; //This must be the length of the string since //it must be a Pascal string. if (d1!=0 ) { str[1] = d1 + 48; foundNonZero = 1; } else { str[1] =' '; } if (d2 != 0 || foundNonZero == 1) { str[2] = d2 + 48; foundNonZero = 1; } else { str[2] =' '; } if (d3 != 0 || foundNonZero == 1) { str[3] = d3 + 48; foundNonZero = 1; } else { str[3] =' '; } str[4] = d4 + 48; str[5] = '\0'; } void DrawInt(int n) { //Max value of int is 9999. char str[6]; if (n >= 0 && n <= 9999) { NumToStr(n, str); DrawString((ConstStr255Param)str); } else { cout << "ERROR attempting to DrawInt!\n"; n >= 0 ? cout << " Integer exceeds max of 9999\n" : cout << " Integer below min of 0\n"; } }