home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / luschsrc.sit / dialog.c < prev    next >
Text File  |  1990-05-23  |  4KB  |  231 lines

  1. /********************************************************************************
  2.  *    dialog.c
  3.  *
  4.  *    Dialog Management Package
  5.  *
  6.  *    Written by Paco Xander Nathan
  7.  *    ⌐1990, Motorola Inc.  Public domain source code.
  8.  ********************************************************************************/
  9.  
  10. #include "applic.h"
  11. #include "window.h"
  12. #include "dialog.h"
  13. #include "ascii.h"
  14. #include "about.h"
  15. #include "test.h"
  16. #include "error.h"
  17.  
  18.  
  19. /* External Data Structures
  20.  */
  21. DialogPtr
  22.     dPtrAbout = NULL,
  23.     dPtrAnal = NULL;
  24.  
  25.  
  26. /* Local Function Prototypes
  27.  */
  28. #ifdef PROTOTYPES
  29. #endif
  30.  
  31.  
  32. /* Generate a random number between zero and the given ceiling number
  33.  */
  34. short
  35. DlogRandom (ceiling)
  36.     register short ceiling;
  37. {
  38.     register short result;
  39.     
  40.     if ((result = Random()) < 0)
  41.         result = -result;
  42.  
  43.     if (ceiling)
  44.         result %= ceiling;
  45.  
  46.     return result;
  47. }
  48.  
  49.  
  50. /* Set the coordinates for centering SFGet/Put and other standard alerts
  51.  */
  52. void
  53. DlogOrigin (alertID, thePoint)
  54.     register short alertID;
  55.     register Point *thePoint;
  56. {
  57.     ;
  58. }
  59.  
  60.  
  61. /* Filter alert and modal dialog events to keep the background events processed
  62.  * and outline the default button in a special way
  63.  */
  64. pascal Boolean 
  65. DlogModalEvent (theDialog, theEvent, itemNum)
  66.     register DialogPtr theDialog;
  67.     register EventRecord *theEvent;
  68.     register short *itemNum;
  69. {
  70.     register Boolean filtered = FALSE;
  71.     register char theChar;
  72.     Rect bounds;
  73.     short kind;
  74.     long finalTick;
  75.     Handle theItem;
  76.  
  77.     /* Now take care of the important, boring stuff...
  78.      */
  79.     if (theEvent->what == keyDown) {
  80.         theChar = (char) BitAnd(theEvent->message, charCodeMask);
  81.  
  82.         if ((theChar == asciiReturn) || (theChar == asciiEnter)) {
  83.             GetDItem(theDialog, 1, &kind, &theItem, &bounds);
  84.             HiliteControl((ControlHandle) theItem, 1);
  85.             Delay(3, &finalTick);
  86.  
  87.             *itemNum = 1;
  88.             filtered = TRUE;
  89.         }
  90.     }
  91.  
  92.     return filtered;
  93. }
  94.  
  95.  
  96. /* Create the specified dialog and center it
  97.  */
  98. pascal DialogPtr
  99. DlogOpenModal (dialogID)
  100.     register short dialogID;
  101. {
  102.     register DialogPtr theDialog = NULL;
  103.  
  104.     /* Create and error check the new modal dialog
  105.      */
  106.     if (theDialog = GetNewDialog(dialogID, NULL, -1L)) {
  107.         /* Center and display
  108.          */
  109.         WindCenter(theDialog);
  110.         WindZapPort(theDialog, TRUE);
  111.         BringToFront(theDialog);
  112.     
  113.         /* Make sure we have an arrow cursor before returning
  114.          */
  115.          InitCursor();
  116.     }
  117.  
  118.      return theDialog;
  119. }
  120.  
  121.  
  122. /* Dispose the specified modal dialog
  123.  */
  124. void
  125. DlogCloseModal (theDialog, trips)
  126.     register DialogPtr theDialog;
  127.     register Boolean trips;
  128. {
  129.     if (theDialog) {
  130.         if (trips)
  131.             WindZapPort(theDialog, FALSE);
  132.     
  133.         DisposDialog(theDialog);
  134.     }
  135. }
  136.  
  137.  
  138. /* Short cut to set a control item value in an active dialog list
  139.  */
  140. void
  141. DlogSetItem (theDialog, itemNum, value)
  142.     register DialogPtr theDialog;
  143.     register short itemNum;
  144.     register short value;
  145. {
  146.     short kind;
  147.     Handle theItem;
  148.     Rect bounds;
  149.  
  150.     GetDItem(theDialog, itemNum, &kind, &theItem, &bounds);
  151.     SetCtlValue((ControlHandle) theItem, value);
  152. }
  153.  
  154.  
  155. /* Modeless dialog event handler
  156.  */
  157. void
  158. DlogModelessEvent (theEvent)
  159.     register EventRecord *theEvent;
  160. {
  161.     DialogPtr theDialog;
  162.     short dialogID, itemNum, kind;
  163.     Handle theItem;
  164.     Rect bounds;
  165.     
  166.     if (DialogSelect(theEvent, &theDialog, &itemNum)) {
  167.         if (theDialog == dPtrAbout)
  168.             AboutEvent(itemNum);
  169.     }
  170. }
  171.  
  172.  
  173. /* Create a modeless dialog window; dialog window template should have proc type 4
  174.  * and needs to have a goAwayBox
  175.  */
  176. pascal DialogPtr
  177. DlogWindow (dlogID)
  178.     register short dlogID;
  179. {
  180.     register DialogPtr theDialog;
  181.     register InfoPtr infoPtr;
  182.     GrafPtr savePort;
  183.  
  184.      /* Create the dialog
  185.       */
  186.      if (theDialog = GetNewDialog(dlogID, NULL, -1L)) {
  187.         GetPort(&savePort);
  188.         SetPort(theDialog);
  189.     
  190.         WindCenter(theDialog);
  191.         WindZapPort(theDialog, TRUE);
  192.     
  193.         /* Create the window info record
  194.          */
  195.         if (infoPtr = (InfoPtr) ErrNewPtr(sizeof(InfoRecord))) {
  196.             infoPtr->kind = wkDlog;
  197.             infoPtr->active = infoPtr->dirty = FALSE;
  198.             infoPtr->named = TRUE;
  199.             infoPtr->item.dlog = theDialog;
  200.             SetWRefCon(theDialog, (long) infoPtr);
  201.         }
  202.         else {
  203.             DisposDialog(theDialog);
  204.             theDialog = NULL;
  205.         }
  206.  
  207.         SetPort(savePort);
  208.     }
  209.  
  210.     return theDialog;
  211. }
  212.  
  213.  
  214. /* Dispose a modeless dialog window
  215.  */
  216. void
  217. DlogDispose (theWindow)
  218.     register WindowPtr theWindow;
  219. {
  220.     register InfoPtr infoPtr = (InfoPtr) GetWRefCon(theWindow);
  221.     register DialogPtr theDialog = infoPtr->item.dlog;
  222.  
  223.     if (theDialog == dPtrAbout)
  224.         dPtrAbout = NULL;
  225.     else if (theDialog == dPtrAnal)
  226.         dPtrAnal = NULL;
  227.  
  228.     DisposPtr(infoPtr);
  229.     DisposDialog(theDialog);
  230. }
  231.