home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Tickle-4.0.sit.hqx / Tickle-4.0 / src / tar_dialog.c < prev    next >
Text File  |  1993-10-28  |  10KB  |  482 lines

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