home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / tarsrc.sit / dialog.c < prev    next >
Text File  |  1989-09-14  |  8KB  |  402 lines

  1. /*
  2.  * Macintosh Tar
  3.  *
  4.  * The routines in this file deal with the dialogs presented to the user.
  5.  *
  6.  * Written by Craig Ruff.
  7.  *
  8.  * Note that the dialog dealing with directory selection is in dir.c.
  9.  */
  10.  
  11. #include "tar.h"
  12. #include <Resources.h>
  13. #include <SegLoad.h>
  14.  
  15. /*
  16.  * Dialog definitions
  17.  */
  18. #define aboutID        128        /* The "About ..." dialog */
  19. #define okItem        1
  20.  
  21. #define blockID        129        /* The block size dialog */
  22. #define sizeItem    3
  23.  
  24. #define typeID        131        /* Creator/Type dialog */
  25. #define creatorItem    3
  26. #define typeItem    4
  27.  
  28. /*
  29.  * Alert Definitions
  30.  */
  31. #define badBlockID    129        /* Block size incorrect alert */
  32. #define osErrID        130        /* Generic OS Error alert */
  33. #define pgmErrID    131        /* Program logic error alert */
  34. #define hfsID        132        /* Must run under HFS alert */
  35. #define askipID        133        /* Skipping archive file note */
  36. #define stkErrID    134        /* Stack error alert */
  37. #define dfID        135        /* Disk full alert */
  38.  
  39. /*
  40.  * AboutFilter - manage the "About ..." dialog
  41.  *
  42.  *    Returns when the mouse or a key is pressed anywhere.
  43.  */
  44. pascal Boolean
  45. AboutFilter(theDialog, theEvent, itemHit)
  46. DialogPtr    theDialog;
  47. EventRecord    *theEvent;
  48. short        *itemHit;
  49. {
  50. #pragma unused(theDialog)
  51.  
  52.     switch (theEvent->what) {
  53.     case keyDown:
  54.     case mouseDown:
  55.         *itemHit = 1;
  56.         return(true);
  57.         break;
  58.  
  59.     default:
  60.         return(false);
  61.         break;
  62.     }
  63. }
  64.  
  65. /*
  66.  * DoAboutBox - put the "About ..." dialog on the screen
  67.  */
  68. DoAboutBox() {
  69.     short        itemHit, itemType;
  70.     WindowPtr    myWindow;
  71.     GrafPtr        thePort;
  72.     PenState    pn;
  73.     Rect        okBox;
  74.     Handle        okHdl;
  75.  
  76.     GetPort(&thePort);
  77.     myWindow = GetNewDialog(aboutID, nil, (WindowPtr) -1);
  78.     GetDItem(myWindow, okItem, &itemType, &okHdl, &okBox);
  79.     SetPort(myWindow);
  80.     InsetRect(&okBox, -4, -4);
  81.     GetPenState(&pn);
  82.     PenSize(3, 3);
  83.     FrameRoundRect(&okBox, 16, 16);
  84.     SetPenState(&pn);
  85.     do {
  86.         ModalDialog(AboutFilter, &itemHit);
  87.     } while (itemHit != 1);
  88.  
  89.     DisposDialog(myWindow);
  90.     SetPort(thePort);
  91. }
  92.  
  93. /*
  94.  * ValidBlockSize - checks the user entered blocksize for validity.
  95.  *
  96.  *    Returns true if ok, false if bad.
  97.  */
  98. Boolean
  99. ValidBlockSize(theDialog)
  100. DialogPtr    theDialog;
  101. {
  102.     long    n;
  103.     short    type;
  104.     Handle    hdl;
  105.     Rect    box;
  106.     char    text[256];
  107.  
  108.     GetDItem(theDialog, sizeItem, &type, &hdl, &box);
  109.     GetIText(hdl, text);
  110.     StringToNum(text, &n);
  111.  
  112.     /*
  113.      * These limits are somewhat arbitrary.
  114.      */
  115.     if ((n < 1) || (n > 128)) {
  116.         NoteAlert(badBlockID, nil);
  117.         SelIText(theDialog, sizeItem, 0, 32767);
  118.         return(false);
  119.     }
  120.  
  121.     blocking = (int) n;
  122.     blockSize = blocking * RECORDSIZE;
  123.     return(true);
  124. }
  125.  
  126. Rect        okBox;        /* ok Box location (gotten once for speed) */
  127. ControlHandle    okHdl;        /* ok Box handle */
  128.  
  129. /*
  130.  * BlockFilter - manage user events during the block size dialog
  131.  *
  132.  *    Allows numeric entry and editing, validates size.
  133.  *    Returns true if the dialog should exit, false to continue.
  134.  */
  135. pascal Boolean
  136. BlockFilter(theDialog, theEvent, itemHit)
  137. DialogPtr    theDialog;
  138. EventRecord    *theEvent;
  139. short        *itemHit;
  140. {
  141.     long    t;
  142.     Point    lpt;
  143.     GrafPtr    savedPort;
  144.  
  145.     switch (theEvent->what) {
  146.     case keyDown:
  147.         switch ((char) (theEvent->message & charCodeMask)) {
  148.         case RETURN:
  149.         case ENTER:
  150.             goto validate;
  151.             break;
  152.  
  153.         case '0': case '1': case '2': case '3': case '4':
  154.         case '5': case '6': case '7': case '8': case '9':
  155.         case TAB: case  BS:
  156.             break;
  157.  
  158.         default:
  159.             SysBeep(5);
  160.             theEvent->what = nullEvent;
  161.             break;
  162.         }
  163.         break;
  164.  
  165.     case mouseDown:
  166.         lpt = theEvent->where;
  167.         GetPort(&savedPort);
  168.         SetPort((GrafPtr) theDialog);
  169.         GlobalToLocal(&lpt);
  170.         SetPort(savedPort);
  171.         if (PtInRect(lpt, &okBox)) {
  172. validate:        if (ValidBlockSize(theDialog)) {
  173.                 *itemHit = ok;
  174.                 HiliteControl(okHdl, 1);
  175.                 Delay(8L, &t);
  176.                 HiliteControl(okHdl, 0);
  177.                 return(true);
  178.             } else
  179.                 theEvent->what = nullEvent;
  180.         }
  181.         break;
  182.     }
  183.  
  184.     return(false);
  185. }
  186.  
  187. /*
  188.  * CreatorTypeFilter - manage user events during the Creator/Type dialog
  189.  *
  190.  */
  191. pascal Boolean
  192. CreatorTypeFilter(theDialog, theEvent, itemHit)
  193. DialogPtr    theDialog;
  194. EventRecord    *theEvent;
  195. short        *itemHit;
  196. {
  197.     short    type;
  198.     Handle    hdl;
  199.     Rect    box;
  200.     char    text[256];
  201.     int    n;
  202.     long    t;
  203.     Point    lpt;
  204.     GrafPtr    savedPort;
  205.  
  206.     switch (theEvent->what) {
  207.     case keyDown:
  208.         switch ((char) (theEvent->message & charCodeMask)) {
  209.         case RETURN:
  210.         case ENTER:
  211.             goto done;
  212.             break;
  213.  
  214.         default:
  215.             break;
  216.         }
  217.         break;
  218.  
  219.     case mouseDown:
  220.         lpt = theEvent->where;
  221.         GetPort(&savedPort);
  222.         SetPort((GrafPtr) theDialog);
  223.         GlobalToLocal(&lpt);
  224.         SetPort(savedPort);
  225.         if (PtInRect(lpt, &okBox)) {
  226. done:
  227.             GetDItem(theDialog, creatorItem, &type, &hdl, &box);
  228.             GetIText(hdl, text);
  229.             n = (unsigned char) text[0];
  230.             if (n > 4)
  231.                 n = 4;
  232.  
  233.             strncpy(fdCreator, &text[1], n);
  234.             GetDItem(theDialog, typeItem, &type, &hdl, &box);
  235.             GetIText(hdl, text);
  236.             n = (unsigned char) text[0];
  237.             if (n > 4)
  238.                 n = 4;
  239.  
  240.             strncpy(fdType, &text[1], n);
  241.             *itemHit = ok;
  242.             HiliteControl(okHdl, 1);
  243.             Delay(8L, &t);
  244.             HiliteControl(okHdl, 0);
  245.             return(true);
  246.         }
  247.         break;
  248.     }
  249.  
  250.     return(false);
  251. }
  252.  
  253. /*
  254.  * DoBlockSize - put the block size dialog on the screen
  255.  *
  256.  *    Puts the current block size into the edit field.
  257.  */
  258. DoBlockSize() {
  259.     short        itemHit;
  260.     DialogPtr    theDialog;
  261.     Handle        hdl;
  262.     short        type;
  263.     Rect        box;
  264.     char        text[256];
  265.     PenState    pn;
  266.     GrafPtr        thePort;
  267.  
  268.     GetPort(&thePort);
  269.     theDialog = GetNewDialog(blockID, nil, (WindowPtr) -1);
  270.     GetDItem(theDialog, sizeItem, &type, &hdl, &box);
  271.     NumToString((long) blocking, text);
  272.     SetIText(hdl, text);
  273.     SelIText(theDialog, sizeItem, 0, 32767);
  274.     GetDItem(theDialog, ok, &type, (Handle *) &okHdl, &okBox);
  275.     SetPort(theDialog);
  276.     InsetRect(&okBox, -4, -4);
  277.     GetPenState(&pn);
  278.     PenSize(3, 3);
  279.     FrameRoundRect(&okBox, 16, 16);
  280.     SetPenState(&pn);
  281.     do {
  282.         ModalDialog(BlockFilter, &itemHit);
  283.     } while ((itemHit != ok) && (itemHit != cancel));
  284.  
  285.     DisposDialog(theDialog);
  286.     SetPort(thePort);
  287. }
  288.  
  289. /*
  290.  * DoCreatorType - put the set Creator/Type dialog on the screen
  291.  *
  292.  *    Puts the current Creator and Type into the edit field.
  293.  */
  294. DoCreatorType() {
  295.     short        itemHit;
  296.     DialogPtr    theDialog;
  297.     Handle        hdl;
  298.     short        type;
  299.     Rect        box;
  300.     char        text[256];
  301.     PenState    pn;
  302.     GrafPtr        thePort;
  303.  
  304.     GetPort(&thePort);
  305.     theDialog = GetNewDialog(typeID, nil, (WindowPtr) -1);
  306.     GetDItem(theDialog, creatorItem, &type, &hdl, &box);
  307.     text[0] = 4;
  308.     strncpy(&text[1], fdCreator, 4);
  309.     SetIText(hdl, text);
  310.     GetDItem(theDialog, typeItem, &type, &hdl, &box);
  311.     text[0] = 4;
  312.     strncpy(&text[1], fdType, 4);
  313.     SetIText(hdl, text);
  314.     SelIText(theDialog, creatorItem, 0, 32767);
  315.     GetDItem(theDialog, ok, &type, (Handle *) &okHdl, &okBox);
  316.     SetPort(theDialog);
  317.     InsetRect(&okBox, -4, -4);
  318.     GetPenState(&pn);
  319.     PenSize(3, 3);
  320.     FrameRoundRect(&okBox, 16, 16);
  321.     SetPenState(&pn);
  322.     do {
  323.         ModalDialog(CreatorTypeFilter, &itemHit);
  324.     } while ((itemHit != ok) && (itemHit != cancel));
  325.  
  326.     DisposDialog(theDialog);
  327.     SetPort(thePort);
  328. }
  329.  
  330. /*
  331.  * OSAlert - put a generic OS Error Alert on the screen
  332.  *
  333.  *    Used for errors not handled in another way (yet).
  334.  */
  335. OSAlert(p0, p1, p2, err)
  336. char    *p0, *p1, *p2;
  337. OSErr    err;
  338. {
  339.     char    buf[256];
  340.  
  341.     /*
  342.      * Manage the contingency of being called for resources missing!
  343.      */
  344.     if (GetResource('ALRT', osErrID) == nil) {
  345.         GrafPtr    wmPort;
  346.         long    t;
  347.         Rect    r;
  348.  
  349.         GetWMgrPort(&wmPort);
  350.         SetPort(wmPort);
  351.         SetRect(&r, 140, 150, 360, 180);
  352.         EraseRect(&r);
  353.         MoveTo(150,170);
  354.         DrawString("\pPANIC! Resources Missing!");
  355.         Delay(600L, &t);
  356.         ExitToShell();
  357.     }
  358.  
  359.     NumToString((long) err, buf);
  360.     ParamText(p0, p1, p2, buf);
  361.     StopAlert(osErrID, nil);
  362. }
  363.  
  364. /*
  365.  * PgmAlert - put a program logic alert on the screen
  366.  */
  367. PgmAlert(p0, p1, p2)
  368. char    *p0, *p1, *p2;
  369. {
  370.     ParamText(p0, p1, p2, nil);
  371.     StopAlert(pgmErrID, nil);
  372. }
  373.  
  374. /*
  375.  * HFSAlert - put a "HFS only" alert on the screen
  376.  */
  377. HFSAlert() {
  378.     StopAlert(hfsID, nil);
  379. }
  380.  
  381. /*
  382.  * DFAlert -  Oops, disk is full
  383.  */
  384. DFAlert() { 
  385.     StopAlert(dfID, nil);
  386. }
  387.  
  388. /*
  389.  * ArSkipAlert - put a "skipping archive file" note on the screen
  390.  */
  391. ArSkipAlert() {
  392.     NoteAlert(askipID, nil);
  393. }
  394.  
  395. /*
  396.  * StkErrAlert - put a stack corrupted alert on the screen
  397.  */
  398. StkErrAlert() {
  399.     StopAlert(stkErrID, nil);
  400.     ExitToShell();
  401. }
  402.