home *** CD-ROM | disk | FTP | other *** search
- /* CursorControl */
- /* by Rob Gibson */
- /* August 22, 1989. */
-
- /* MacHeaders Included */
-
- /*********
- Project file:
- Control.c
- ControlMain.c
- Functions.c
- MacTraps
- MousePos.c
-
- Type: APPL
- Creator: CCTL
- **********/
-
-
-
- #define ControlDialogID 1000
- #define nil 0L
-
-
- /* important dialog items */
-
- enum{
- quitItem = 1,
- setItem,
- topPosItem,
- leftPosItem,
- bottomPosItem,
- rightPosItem,
- boxItem
- };
-
-
- /* Our global variables */
-
- DialogPtr ControlDialog;
- Rect boundsRect;
-
-
- /****
- * InitMacintosh()
- *
- * Initialize all the managers & memory
- *
- ****/
-
- InitMacintosh()
- {
- MaxApplZone();
-
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- }
- /* end InitMacintosh */
-
-
- /****
- * GetBounds()
- *
- * Get the rect specified in dialog
- *
- ****/
-
- GetBounds(theDialog)
- DialogPtr theDialog;
- {
- Str255 str;
- long dummy;
-
- boundsRect.top = GetETNum(theDialog, topPosItem);
- boundsRect.left = GetETNum(theDialog, leftPosItem);;
- boundsRect.bottom = GetETNum(theDialog, bottomPosItem);;
- boundsRect.right = GetETNum(theDialog, rightPosItem);;
- }
- /* end GetBounds */
-
-
- /****
- * TrackRect()
- *
- * Frame old and new bound rects in current GrafPort
- *
- ****/
-
- TrackRect(oldRect, r)
- Rect *oldRect;
- Rect *r;
- {
- FrameRect(oldRect);
- FrameRect(r);
- }
- /* end TrackRect */
-
-
- /****
- * DisplayBounds()
- *
- * Display a rect in the dialog
- *
- ****/
-
- DisplayBounds(theRect, theDialog)
- Rect *theRect;
- DialogPtr theDialog;
- {
- SetETNum(theDialog, topPosItem, (long)theRect->top);
- SetETNum(theDialog, leftPosItem, (long)theRect->left);
- SetETNum(theDialog, bottomPosItem, (long)theRect->bottom);
- SetETNum(theDialog, rightPosItem, (long)theRect->right);
-
- SelIText(theDialog, topPosItem, 0, 32767);
- }
- /* end DisplayBounds */
-
-
- /****
- * SetBoundsLoop()
- *
- * User drags to specify new rect
- *
- ****/
-
- SetBoundsLoop(theDialog)
- DialogPtr theDialog;
- {
- Rect oldRect;
- Rect newRect;
- GrafPtr savePort;
- GrafPtr deskPort;
- Point firstPoint;
- Point secondPoint;
- Point lastSecondPoint;
-
- GetPort(&savePort);
- OpenPort(deskPort = (GrafPtr)NewPtr(sizeof(GrafPort)));
- InitPort(deskPort);
- SetPort(deskPort);
- PenPat(gray);
- PenMode(notPatXor);
- PenSize(2, 2);
- while(!Button());
- GetMouse(&firstPoint);
- newRect.top = lastSecondPoint.v = firstPoint.v;
- newRect.left = lastSecondPoint.h = firstPoint.h;
- newRect.bottom = newRect.right = 0;
- while(Button())
- {
- oldRect = newRect;
- GetMouse(&secondPoint);
- /* If the mouse location has changed then track mouse */
- if (secondPoint.v != lastSecondPoint.v || secondPoint.h != lastSecondPoint.h)
- {
- /* Create a new Rect making sure it is not an empty Rect */
- if (secondPoint.v > firstPoint.v)
- {
- newRect.top = firstPoint.v;
- newRect.bottom = secondPoint.v;
- }
- else
- {
- newRect.top = secondPoint.v;
- newRect.bottom = firstPoint.v;
- }
- if (secondPoint.h > firstPoint.h)
- {
- newRect.left = firstPoint.h;
- newRect.right = secondPoint.h;
- }
- else
- {
- newRect.left = secondPoint.h;
- newRect.right = firstPoint.h;
- }
-
- lastSecondPoint = secondPoint;
-
- TrackRect(&oldRect, &newRect);
- DisplayBounds(&newRect, theDialog);
- }
- }
- FrameRect(&newRect);
- ClosePort(deskPort);
- DisposPtr((Ptr)deskPort);
- PenNormal();
- SetPort(savePort);
- boundsRect = newRect;
- }
- /* end SetBoundsLoop */
-
-
- /****
- * HandleControlDialog()
- *
- * Main event loop
- *
- ****/
-
- HandleControlDialog(theDialog)
- DialogPtr theDialog;
- {
- EventRecord event; /* Filled by GetNextEvent */
- Boolean finished = false; /* Are we done? */
- int chosen;
- char theChar;
-
- while (!finished) /* do this until we selected quit */
- { /* continue with the normal get next event stuff... */
-
- if (GetNextEvent(everyEvent, &event)) /* if there was an event... then */
- {
- if (event.what == keyDown || event.what == autoKey)
- {
- theChar = (char) (event.message & charCodeMask);
- switch(theChar)
- {
- case 'Q':
- case 'q':
- case '.':
- chosen = quitItem;
- event.what = 0; /* remove event */
- finished = true;
- ClickButton(theDialog, quitItem, 2);
- break;
-
- case '\t':
- case '\b':
- break;
-
- case '\r':
- case '\003':
- case 'S':
- case 's':
- chosen = setItem;
- event.what = 0; /* remove event */
- ClickButton(theDialog, setItem, true);
- SetBoundsLoop(theDialog);
- ClickButton(theDialog, setItem, false);
- break;
-
- default:
- if (theChar < '0' || theChar > '9')
- event.what = 0;
- break;
- }
- }
- else if (event.what == updateEvt)
- {
- SetPort(theDialog);
- BeginUpdate(theDialog);
- FrameItem(theDialog, boxItem);
- DrawDialog(theDialog);
- DrawDefaultBtn (theDialog, setItem);
- EndUpdate(theDialog);
- }
- }
-
- if (!finished)
- {
- if (IsDialogEvent(&event))
- if (DialogSelect(&event, &theDialog, &chosen))
- {
- GetBounds(theDialog);
- switch (chosen)
- {
- case 1:
- finished = true;
- break;
-
- case 2:
- ClickButton(theDialog, setItem, true);
- SetBoundsLoop(theDialog);
- ClickButton(theDialog, setItem, false);
- break;
-
- default:
- break;
- } /* end of if switch */
- }
- HandleMouse(&boundsRect);
- } /* end of if (!finished) */
- } /* of event loop */
-
- DisposDialog(theDialog);
- }
- /* end HandleControlDialog */
-
-
- /*****
- * SetUpDialog()
- *
- * Set up dialog stuff
- *
- *****/
-
- SetUpDialog()
- {
- int itemType;
- Handle Hdl;
-
- ControlDialog = GetNewDialog(ControlDialogID, nil, -1L);
- CenterWindow(ControlDialog, &screenBits.bounds);
- DisplayBounds(&screenBits.bounds, ControlDialog);
- ShowWindow(ControlDialog);
- boundsRect = screenBits.bounds;
- HandleControlDialog(ControlDialog);
- }
- /* end SetUpDialog */
-
-
- /*****
- * main()
- *
- * Call the main procedures
- *
- *****/
-
- main()
-
- {
- InitMacintosh();
- SetUpDialog();
- }
- /* end main */