home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.09 Sep 90 / CursorControl Folder / ControlMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-26  |  6.1 KB  |  334 lines  |  [TEXT/KAHL]

  1. /* CursorControl */
  2. /* by Rob Gibson */
  3. /* August 22, 1989. */
  4.  
  5. /* MacHeaders Included */
  6.  
  7. /*********
  8.     Project file:
  9.         Control.c
  10.         ControlMain.c
  11.         Functions.c
  12.         MacTraps
  13.         MousePos.c
  14.  
  15.     Type: APPL
  16.     Creator: CCTL
  17. **********/
  18.  
  19.  
  20.  
  21. #define ControlDialogID    1000
  22. #define nil                0L
  23.  
  24.  
  25. /* important dialog items */
  26.  
  27. enum{
  28.     quitItem = 1,
  29.     setItem,
  30.     topPosItem,
  31.     leftPosItem,
  32.     bottomPosItem,
  33.     rightPosItem,
  34.     boxItem
  35.     };
  36.  
  37.     
  38. /* Our global variables */
  39.  
  40. DialogPtr                ControlDialog;
  41. Rect                    boundsRect;
  42.  
  43.  
  44. /****
  45.  * InitMacintosh()
  46.  *
  47.  * Initialize all the managers & memory
  48.  *
  49.  ****/
  50.  
  51. InitMacintosh()
  52. {
  53.     MaxApplZone();
  54.     
  55.     InitGraf(&thePort);
  56.     InitFonts();
  57.     FlushEvents(everyEvent, 0);
  58.     InitWindows();
  59.     InitMenus();
  60.     TEInit();
  61.     InitDialogs(0L);
  62.     InitCursor();
  63.  
  64. }
  65. /* end InitMacintosh */
  66.  
  67.  
  68. /****
  69.  * GetBounds()
  70.  *
  71.  * Get the rect specified in dialog
  72.  *
  73.  ****/
  74.  
  75. GetBounds(theDialog)
  76. DialogPtr    theDialog;
  77. {
  78.     Str255    str;
  79.     long    dummy;
  80.  
  81.     boundsRect.top = GetETNum(theDialog, topPosItem);
  82.     boundsRect.left = GetETNum(theDialog, leftPosItem);;
  83.     boundsRect.bottom = GetETNum(theDialog, bottomPosItem);;
  84.     boundsRect.right = GetETNum(theDialog, rightPosItem);;
  85. }
  86. /* end GetBounds */
  87.  
  88.  
  89. /****
  90.  * TrackRect()
  91.  *
  92.  * Frame old and new bound rects in current GrafPort
  93.  *
  94.  ****/
  95.  
  96. TrackRect(oldRect, r)
  97. Rect    *oldRect;
  98. Rect    *r;
  99. {
  100.     FrameRect(oldRect);
  101.     FrameRect(r);
  102. }
  103. /* end TrackRect */
  104.  
  105.  
  106. /****
  107.  * DisplayBounds()
  108.  *
  109.  * Display a rect in the dialog
  110.  *
  111.  ****/
  112.  
  113. DisplayBounds(theRect, theDialog)
  114. Rect        *theRect;
  115. DialogPtr    theDialog;
  116. {
  117.     SetETNum(theDialog, topPosItem, (long)theRect->top);
  118.     SetETNum(theDialog, leftPosItem, (long)theRect->left);
  119.     SetETNum(theDialog, bottomPosItem, (long)theRect->bottom);
  120.     SetETNum(theDialog, rightPosItem, (long)theRect->right);
  121.     
  122.     SelIText(theDialog, topPosItem, 0, 32767);
  123. }
  124. /* end DisplayBounds */
  125.  
  126.  
  127. /****
  128.  * SetBoundsLoop()
  129.  *
  130.  * User drags to specify new rect
  131.  *
  132.  ****/
  133.  
  134. SetBoundsLoop(theDialog)
  135. DialogPtr    theDialog;
  136. {
  137.     Rect        oldRect;
  138.     Rect        newRect;
  139.     GrafPtr        savePort;
  140.     GrafPtr        deskPort;
  141.     Point        firstPoint;
  142.     Point        secondPoint;
  143.     Point        lastSecondPoint;
  144.  
  145.     GetPort(&savePort);
  146.     OpenPort(deskPort = (GrafPtr)NewPtr(sizeof(GrafPort)));
  147.     InitPort(deskPort);
  148.     SetPort(deskPort);
  149.     PenPat(gray);
  150.     PenMode(notPatXor);
  151.     PenSize(2, 2);
  152.     while(!Button());
  153.     GetMouse(&firstPoint);
  154.     newRect.top = lastSecondPoint.v = firstPoint.v;
  155.     newRect.left = lastSecondPoint.h = firstPoint.h;
  156.     newRect.bottom = newRect.right = 0;
  157.     while(Button())
  158.         {
  159.             oldRect = newRect;
  160.             GetMouse(&secondPoint);
  161.             /* If the mouse location has changed then track mouse */
  162.             if (secondPoint.v != lastSecondPoint.v || secondPoint.h != lastSecondPoint.h)
  163.                 {
  164.                     /* Create a new Rect making sure it is not an empty Rect */
  165.                     if (secondPoint.v > firstPoint.v)
  166.                         {
  167.                             newRect.top = firstPoint.v;
  168.                             newRect.bottom = secondPoint.v;
  169.                         }
  170.                     else
  171.                         {
  172.                             newRect.top = secondPoint.v;
  173.                             newRect.bottom = firstPoint.v;
  174.                         }
  175.                     if (secondPoint.h > firstPoint.h)
  176.                         {
  177.                             newRect.left = firstPoint.h;
  178.                             newRect.right = secondPoint.h;
  179.                         }
  180.                     else
  181.                         {
  182.                             newRect.left = secondPoint.h;
  183.                             newRect.right = firstPoint.h;
  184.                         }
  185.                     
  186.                     lastSecondPoint = secondPoint;
  187.  
  188.                     TrackRect(&oldRect, &newRect);
  189.                     DisplayBounds(&newRect, theDialog);
  190.                 }
  191.         }
  192.     FrameRect(&newRect);
  193.     ClosePort(deskPort);
  194.     DisposPtr((Ptr)deskPort);
  195.     PenNormal();
  196.     SetPort(savePort);
  197.     boundsRect = newRect;
  198. }
  199. /* end SetBoundsLoop */
  200.  
  201.  
  202. /****
  203.  * HandleControlDialog()
  204.  *
  205.  * Main event loop
  206.  *
  207.  ****/
  208.  
  209. HandleControlDialog(theDialog)
  210. DialogPtr    theDialog;
  211. {
  212.     EventRecord        event;                /*  Filled by GetNextEvent            */
  213.     Boolean            finished = false;    /*  Are we done? */
  214.     int                chosen;
  215.     char            theChar;
  216.  
  217.     while (!finished)                /*  do this until we selected quit */
  218.         {                            /* continue with the normal get next event stuff... */
  219.     
  220.             if (GetNextEvent(everyEvent, &event))        /*  if there was an event... then     */
  221.                 {
  222.                     if (event.what == keyDown || event.what == autoKey)
  223.                         {
  224.                             theChar = (char) (event.message & charCodeMask);
  225.                             switch(theChar)
  226.                                 {
  227.                                     case 'Q':
  228.                                     case 'q':
  229.                                     case '.':
  230.                                         chosen = quitItem;
  231.                                         event.what = 0;    /* remove event */
  232.                                         finished = true;
  233.                                         ClickButton(theDialog, quitItem, 2);
  234.                                         break;
  235.                                     
  236.                                     case '\t':
  237.                                     case '\b':
  238.                                         break;
  239.                                     
  240.                                     case '\r':
  241.                                     case '\003':
  242.                                     case 'S':
  243.                                     case 's':
  244.                                         chosen = setItem;
  245.                                         event.what = 0;    /* remove event */
  246.                                         ClickButton(theDialog, setItem, true);
  247.                                         SetBoundsLoop(theDialog);
  248.                                         ClickButton(theDialog, setItem, false);
  249.                                         break;
  250.                                     
  251.                                     default:
  252.                                         if (theChar < '0' || theChar > '9')
  253.                                             event.what = 0;
  254.                                         break;
  255.                                 }        
  256.                         }
  257.                     else if (event.what == updateEvt)
  258.                         {
  259.                             SetPort(theDialog);
  260.                             BeginUpdate(theDialog);
  261.                             FrameItem(theDialog, boxItem);
  262.                             DrawDialog(theDialog);
  263.                             DrawDefaultBtn (theDialog, setItem);
  264.                             EndUpdate(theDialog);
  265.                         }        
  266.                 }
  267.  
  268.             if (!finished)
  269.                 {
  270.                     if     (IsDialogEvent(&event))
  271.                         if (DialogSelect(&event, &theDialog, &chosen))
  272.                             {
  273.                                 GetBounds(theDialog);
  274.                                 switch (chosen)
  275.                                     {
  276.                                         case 1:
  277.                                             finished = true;
  278.                                             break;
  279.                                         
  280.                                         case 2:
  281.                                             ClickButton(theDialog, setItem, true);
  282.                                             SetBoundsLoop(theDialog);
  283.                                             ClickButton(theDialog, setItem, false);
  284.                                             break;
  285.                 
  286.                                         default:
  287.                                             break;
  288.                                     }    /*  end of if switch     */
  289.                             }
  290.                     HandleMouse(&boundsRect);
  291.                   }    /*  end of if (!finished)     */
  292.         }    /*  of event loop         */
  293.  
  294.     DisposDialog(theDialog);
  295. }
  296. /* end HandleControlDialog */
  297.  
  298.  
  299. /*****
  300.  * SetUpDialog()
  301.  *
  302.  * Set up dialog stuff
  303.  *
  304.  *****/
  305.  
  306. SetUpDialog()
  307. {
  308.     int        itemType;
  309.     Handle    Hdl;
  310.  
  311.     ControlDialog = GetNewDialog(ControlDialogID, nil, -1L);
  312.     CenterWindow(ControlDialog, &screenBits.bounds);
  313.     DisplayBounds(&screenBits.bounds, ControlDialog);
  314.     ShowWindow(ControlDialog);
  315.     boundsRect = screenBits.bounds;
  316.     HandleControlDialog(ControlDialog);
  317. }
  318. /* end SetUpDialog */
  319.  
  320.  
  321. /*****
  322.  * main()
  323.  *
  324.  * Call the main procedures
  325.  *
  326.  *****/
  327.  
  328. main()
  329.  
  330. {
  331.     InitMacintosh();
  332.     SetUpDialog();
  333. }
  334. /* end main */