home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-01 | 13.6 KB | 654 lines | [TEXT/CWIE] |
- // Imaging Examples
- // by Ken Long <kenlong@netcom.com>
- // updated for CW7 and THINK 7 on 951201
-
- #include <GestaltEqu.h>
-
- #define over qd.screenBits.bounds.right
- #define down qd.screenBits.bounds.bottom
-
- MenuHandle appleMenu, fileMenu, editMenu, widthMenu;
-
- enum {
- appleID = 1,
- fileID,
- editID,
- widthID
- };
-
- enum {
- openItem = 1,
- closeItem,
- quitItem = 4
- };
-
- enum {
- lineItem = 1,
- sizeItem,
- pattItem,
- rectItem,
- fillItem,
- ovalItem,
- arcsItem,
- dumbItem,
- clipItem,
- triaItem,
- filCItem,
- cPatItem
- };
-
- WindowPtr shell_window, aboutWindow;
- Rect dragRect;
- Rect windRect;
- short width = 5;
- //OSErr QDError;
-
- //• Prototypes:
- void MyDrawLines (void);
- void MyResizePens (void);
- void MyRepatternPens (void);
- void MyDrawRects (void);
- void MyPaintAndFillRects (void);
- void MyDrawOvals (void);
- void MyDrawArcAndPaintWedge (void);
- void MyDrawDumbbell (void);
- RgnHandle MyFillClipRegion (void);
- void MyDrawTriangle (void);
- void MyPaintAndFillColorRects (void);
- void SetUpWindow (void);
- void SetUpMenus (void);
- void AdjustMenus (void);
- short enable (MenuHandle menu, short item, short ok);
- void HandleMenu (long mSelect);
- void InitMacintosh (void);
- void HandleMouseDown (EventRecord *theEvent);
- void HandleEvent (void);
- int DoAbout (void);
- Boolean ColorChecksOut (void);
- void main (void);
-
- //• Routines:
-
- void MyDrawLines ()
- {
- MoveTo (20, 20);
- LineTo (70, 20);
- LineTo (70, 70);
- LineTo (20, 70);
- LineTo (20, 20);
-
- Move (70, 0);
- Line (50, 0);
- Line (0, 50);
- Line (-50, 0);
- Line (0, -50);
- }
-
- void MyResizePens ()
- {
- PenSize (2, 8);
- MoveTo (20, 20);
- LineTo (70, 20);
- LineTo (70, 70);
- LineTo (20, 70);
- LineTo (20, 20);
- PenSize (8, 2);
- Move (70, 0);
- Line (50, 0);
- Line (0, 50);
- Line (-50, 0);
- Line (0, -50);
- PenNormal ();
- }
-
- void MyRepatternPens ()
- {
- PenSize (2, 8);
- PenPat (&qd.ltGray);
- MoveTo (20, 20);
- LineTo (70, 20);
- LineTo (70, 70);
- LineTo (20, 70);
- LineTo (20, 20);
-
- PenSize (8, 2);
- PenPat (&qd.dkGray);
- Move (70, 0);
- Line (50, 0);
- Line (0, 50);
- Line (-50, 0);
- Line (0, -50);
- PenNormal ();
- }
-
- void MyDrawRects ()
- {
- Rect firstRect, secondRect;
-
- PenSize (2, 2);
-
- firstRect.top = 20;
- firstRect.left = 20;
- firstRect.bottom = 70;
- firstRect.right = 70;
- FrameRect (&firstRect);
-
- SetRect (&secondRect, 90, 20, 140, 70);
- FrameRect (&secondRect);
-
- PenNormal ();
- }
-
- void MyPaintAndFillRects ()
- {
- Rect firstRect, secondRect;
-
- PenNormal ();
- SetRect (&firstRect, 20, 20, 70, 70);
- PaintRect (&firstRect);
- SetRect (&secondRect, 90, 20, 140, 70);
- FillRect (&secondRect, &qd.ltGray);
- }
-
- void MyDrawOvals ()
- {
- Rect firstRect, secondRect;
-
- PenSize (2, 2);
- SetRect (&firstRect, 20, 20, 70, 70); //• Create a bounding rectangle.
- FrameOval (&firstRect); //• Draw the oval.
- SetRect (&secondRect, 90, 20, 140, 70); //• Create a bounding rectangle.
- FrameOval (&secondRect); //• Draw the oval.
- PenNormal ();
- }
-
- void MyDrawArcAndPaintWedge ()
- {
- Rect firstRect, secondRect;
-
- SetRect (&firstRect, 20, 20, 70, 70); //• create a bounding rectangle.
- FrameArc (&firstRect, 45, 135); //• draw an arc.
- SetRect (&secondRect, 90, 20, 140, 70); //• create a bounding rectangle.
- PaintArc (&secondRect, 45, 135); //• draw a wedge.
- }
-
- void MyDrawDumbbell ()
- {
- long grow;
- RgnHandle dumbbell;
- Rect tempRect;
- Size *kMinReserve;
-
- // if (MaxMem (&grow) > kMinReserve)
- // {
- dumbbell = NewRgn (); //• Create a new region.
- OpenRgn (); //• Begin drawing instructions.
- SetRect (&tempRect, 20, 20, 30, 50);
- FrameOval (&tempRect); //• Form the left "weight".
- SetRect (&tempRect, 25, 30, 85, 40);
- FrameRect (&tempRect); //• Form the bar.
- SetRect (&tempRect, 80, 20, 90, 50);
- FrameOval (&tempRect); //• Form the right "weight".
- CloseRgn (dumbbell); //• Stop collecting.
- FillRgn (dumbbell, &qd.black); //• Draw the shape onscreen.
- // if (QDError != noErr)
- // ; //• Likely error is that there is insufficient memory.
- DisposeRgn (dumbbell); //• Dispose of the region.
- // }
- }
-
- RgnHandle MyFillClipRegion ()
- {
- long grow;
- Rect newClip;
- RgnHandle oldClipRegion;
- RgnHandle newClipRegion;
- WindowPtr myWindow;
-
- //• ("if" statement ommented out because it's not a memory demo)
- // if (MaxMem (&grow) > kMinReserve)
- // {
- //• Allocate old clipping region.
- oldClipRegion = NewRgn ();
- myWindow = FrontWindow (); //• Get the front window.
-
- //• Make the front window the current graphics port.
- SetPort (myWindow);
-
- //• Save the old clipping region.
- GetClip (oldClipRegion);
-
- //• Create a new rectangle.
- newClip = myWindow->portRect;
-
- //• Exclude vertical scroll bar.
- newClip.right = newClip.right - 15;
-
- //• Exclude horizontal scroll bar.
- newClip.bottom = newClip.bottom - 15;
-
- //• Make the new rectangle the clipping region.
- ClipRect (&newClip);
-
- //• Allocate new clipping region.
- newClipRegion = NewRgn ();
- RectRgn (newClipRegion, &newClip);
-
- //• Paint clipping region gray.
- FillRgn (newClipRegion, &qd.ltGray);
-
- //• Restore previous clipping region.
- SetClip (oldClipRegion);
-
- // if (QDError != noErr)
- // ; //• likely error is that there is insufficient memory.
-
- //• Dispose previous clipping region.
- DisposeRgn (oldClipRegion);
- return (newClipRegion);
- // }
- }
-
- void MyDrawTriangle ()
- {
- PolyHandle triPoly;
-
- triPoly = OpenPoly (); //• Save handle and begin collecting lines.
- MoveTo (300, 100); //• Move to first point.
- LineTo (400, 200); //• Form the triangle's sides.
- LineTo (200, 200);
- ClosePoly (); //• Stop collecting lines.
- FillPoly (triPoly, &qd.gray);//• Fill the polygon with gray.
-
- // if (QDError != noErr)
- // ; //• likely error is that there is insufficient memory.
-
- KillPoly (triPoly) ; //• dispose of its memory.
- }
-
- void MyPaintAndFillColorRects ()
- {
- Rect firstRect, secondRect;
-
- RGBColor myDarkBlue;
- RGBColor myMediumGreen;
-
- //• create dark blue color.
- myDarkBlue.red = 0x0000;
- myDarkBlue.green = 0x0000;
- myDarkBlue.blue = 0x9999;
-
- //• create medium green color.
- myMediumGreen.red = 0x3206;
- myMediumGreen.green = 0x9038;
- myMediumGreen.blue = 0x013D;
- RGBForeColor (&myDarkBlue); //• Draw with dark blue pen.
-
- PenMode (patCopy);
- SetRect (&firstRect, 20, 20, 70, 70);
- PaintRect (&firstRect); //• Paint a dark blue rectangle.
- RGBForeColor (&myMediumGreen); //• Draw with a medium green pen.
- SetRect (&secondRect, 90, 20, 140, 70);
- FillRect (&secondRect, &qd.ltGray); //• Paint a medium green rectangle.
- }
-
- void MyPaintPixelPatternRects ()
- {
- Rect firstRect, secondRect;
- PixPatHandle myPenPattern, myFillPattern;
-
- myPenPattern = GetPixPat (128); //• Get a pixel pattern.
- PenPixPat (myPenPattern); //• Assign the pattern to the pen.
- SetRect (&firstRect, 20, 20, 70, 70);
- PaintRect (&firstRect); //• Paint with the pen's pixel pattern.
- DisposePixPat (myPenPattern); //• Dispose of the pixel pattern.
-
- myFillPattern = GetPixPat (129); //• Get another pixel pattern.
- SetRect (&secondRect, 90, 20, 140, 70);
- FillCRect (&secondRect, myFillPattern); //• Fill with this pattern.
- DisposePixPat (myFillPattern); //• Dispose of the pixel pattern.
- }
-
- void SetUpWindow (void)
- {
- dragRect = qd.screenBits.bounds;
-
- SetRect (&windRect, over / 2 - 240,
- down / 2 - 150,
- over / 2 + 240,
- down / 2 + 150);
-
- shell_window = NewCWindow (0L, &windRect, "\pitty bitty bytes™", true, noGrowDocProc, (WindowPtr) -1L, true, 0);
- SetPort (shell_window);
- }
-
- void SetUpMenus (void)
- {
- InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
- AppendMenu (appleMenu, "\pAbout the demo");
- AddResMenu (appleMenu, 'DRVR');
-
- InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
- AppendMenu (fileMenu, "\pOpen/O;Close/W;(-;Quit/Q");
-
- InsertMenu (editMenu = NewMenu (editID, "\pEdit"), 0);
- AppendMenu (editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
-
- InsertMenu (widthMenu = NewMenu (widthID, "\pExamples"), 0);
- AppendMenu (widthMenu, "\pMyDrawLines/1");
- AppendMenu (widthMenu, "\pMyResizePens/2");
- AppendMenu (widthMenu, "\pMyRepatternPens/3");
- AppendMenu (widthMenu, "\pMyDrawRects/4");
- AppendMenu (widthMenu, "\pMyPaintAndFillRects/5");
- AppendMenu (widthMenu, "\pMyDrawOvals/6");
- AppendMenu (widthMenu, "\pMyDrawArcAndPaintWedge/7");
- AppendMenu (widthMenu, "\pMyDrawDumbell/8");
- AppendMenu (widthMenu, "\pMyFillClipRegion/9");
- AppendMenu (widthMenu, "\pMyDrawTriangle/0");
- AppendMenu (widthMenu, "\pMyPaintAndFillColorRects/-");
- AppendMenu (widthMenu, "\pMyPaintPixelPatternRects/=");
- DrawMenuBar ();
- }
-
- void AdjustMenus (void)
- {
- register WindowPeek wp = (WindowPeek) FrontWindow ();
- short kind = wp ? wp->windowKind : 0;
- Boolean DA = kind < 0;
-
- enable (editMenu, 1, DA);
- enable (editMenu, 3, DA);
- enable (editMenu, 4, DA);
- enable (editMenu, 5, DA);
- enable (editMenu, 6, DA);
-
- enable (fileMenu, openItem, ! ((WindowPeek) shell_window)->visible);
- enable (fileMenu, closeItem, DA || ((WindowPeek) shell_window)->visible);
-
- // CheckItem (widthMenu, width, true);
- }
-
- short enable (MenuHandle menu, short item, short ok)
- {
- if (ok)
- EnableItem (menu, item);
- else
- DisableItem (menu, item);
- }
-
- Nag ()
- {
- MoveTo (40, 40);
- DrawString ("\pBetter get a COLOR Mac!");
- }
-
- void HandleMenu (long mSelect)
- {
- short menuID = HiWord (mSelect);
- short menuItem = LoWord (mSelect);
- Str255 name;
- GrafPtr savePort;
- WindowPeek frontWindow;
- RGBColor none;
-
- Rect circleStart = shell_window->portRect;
-
- switch (menuID)
- {
- case appleID:
- if (menuItem == 1)
- {
- EraseRect (&circleStart);
- DoAbout ();
- }
- else
- {
- GetPort (&savePort);
- GetItem (appleMenu, menuItem, name);
- OpenDeskAcc (name);
- SetPort (savePort);
- }
- break;
-
- case fileID:
- switch (menuItem)
- {
- case openItem:
- ShowWindow (shell_window);
- SelectWindow (shell_window);
- break;
-
- case closeItem:
- if ((frontWindow = (WindowPeek) FrontWindow ()) == 0L)
- break;
-
- if (frontWindow->windowKind < 0)
- CloseDeskAcc (frontWindow->windowKind);
- else
- if (frontWindow = (WindowPeek) shell_window)
- ExitToShell ();
- break;
-
- case quitItem:
- ExitToShell ();
- break;
- }
- break;
-
- case editID:
- if (!SystemEdit (menuItem-1))
- SysBeep (5);
- break;
-
- case widthID:
- {
- SetPort (shell_window); //• This is to make sure.
-
- //• Wipe the slate before ____.
- EraseRect (&shell_window->portRect);
- ForeColor (blackColor); //• In case it changed.
-
- switch (menuItem) //• Pick a number between 1 and 10.
- {
- case lineItem:
- MyDrawLines ();
- break;
-
- case sizeItem:
- MyResizePens ();
- break;
-
- case pattItem:
- MyRepatternPens ();
- break;
-
- case rectItem:
- MyDrawRects ();
- break;
-
- case fillItem:
- MyPaintAndFillRects ();
- break;
-
- case ovalItem:
- MyDrawOvals ();
- break;
-
- case arcsItem:
- MyDrawArcAndPaintWedge ();
- break;
-
- case dumbItem:
- MyDrawDumbbell ();
- break;
-
- case clipItem:
- MyFillClipRegion ();
- break;
-
- case triaItem:
- MyDrawTriangle ();
- break;
-
- case filCItem:
- if (ColorChecksOut ())
- MyPaintAndFillColorRects ();
- else
- Nag ();
- break;
-
- case cPatItem:
- if (ColorChecksOut ())
- MyPaintPixelPatternRects ();
- else
- Nag ();
- break;
- }
- BackColor (whiteColor);
- ForeColor (blackColor);
- PenNormal ();
- }
- break;
- }
- }
-
- void InitMacintosh (void)
- {
- MaxApplZone ();
-
- InitGraf (&qd.thePort);
- InitFonts ();
- FlushEvents (everyEvent, 0);
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (0L);
- InitCursor ();
- }
-
- void HandleMouseDown (EventRecord *theEvent)
- {
- WindowPtr theWindow;
- short windowCode = FindWindow (theEvent->where, &theWindow);
-
- switch (windowCode)
- {
- case inSysWindow:
- SystemClick (theEvent, theWindow);
- break;
-
- case inMenuBar:
- AdjustMenus ();
- HandleMenu (MenuSelect (theEvent->where));
- break;
-
- case inDrag:
- if (theWindow == shell_window)
- DragWindow (shell_window, theEvent->where, &dragRect);
- break;
-
- case inContent:
- if (theWindow == shell_window)
- {
- if (theWindow != FrontWindow ())
- SelectWindow (shell_window);
- else
- InvalRect (&shell_window->portRect);
- }
- break;
-
- case inGoAway:
- if (theWindow == shell_window &&
- TrackGoAway (shell_window, theEvent->where))
- ExitToShell ();
- if (theWindow == aboutWindow &&
- TrackGoAway (aboutWindow, theEvent->where))
- HideWindow (aboutWindow);
- break;
- }
- }
-
- void HandleEvent (void)
- {
- short ok;
- EventRecord theEvent;
-
- HiliteMenu (0);
- SystemTask (); /* Handle desk accessories */
-
- ok = GetNextEvent (everyEvent, &theEvent);
- if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- HandleMouseDown (&theEvent);
- break;
-
- case keyDown:
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- AdjustMenus ();
- HandleMenu (MenuKey ((char) (theEvent.message & charCodeMask)));
- }
- break;
-
- case updateEvt:
- BeginUpdate (shell_window);
- EndUpdate (shell_window);
- break;
-
- case activateEvt:
- InvalRect (&shell_window->portRect);
- break;
- }
- }
-
- DoAbout ()
- {
- Rect boutRect;
-
- SetRect (&boutRect, over / 2 - 200,
- down / 2 - 100,
- over / 2 + 200,
- down / 2 + 100);
-
- aboutWindow = NewWindow (0L, &boutRect, "\pSay WHAT?",
- true, noGrowDocProc, (WindowPtr) -1L,
- true, 0);
- SetPort (aboutWindow);
-
- MoveTo (20,20);
- DrawString ("\pFrustrated by no C source examples in IM?");
- MoveTo (20,40);
- DrawString ("\pWell, itty bitty bytes™ to the rescue!");
- MoveTo (20,60);
- DrawString ("\pTranslated to C by Kenneth A. Long.");
- MoveTo (20,80);
- DrawString ("\pThese are 12 examples from \"Imaging\"");
- }
-
- Boolean ColorChecksOut (void)
- {
- unsigned char version[ 4 ];
- OSErr err;
-
- err = Gestalt( gestaltQuickdrawVersion, (long *)version );
-
- if ( version[ 2 ] > 0 )
- return( true );
- else
- return( false );
- }
-
- void main (void)
- {
- InitMacintosh ();
- SetUpMenus ();
- SetUpWindow ();
-
- for (;;)
- HandleEvent ();
- }
-
-