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