home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / TE32K 1.2 / main.c next >
Encoding:
C/C++ Source or Header  |  1993-03-07  |  23.4 KB  |  1,173 lines  |  [TEXT/KAHL]

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "TE32K.h"
  4.  
  5.  
  6. #define EQSTR    0
  7.  
  8.  
  9. #define    yesNoCancelDLOG    132
  10. #define    errorDLOG        256
  11.  
  12. #define    numMenus    4
  13.  
  14. #define    appleMenu    0
  15. #define    fileMenu    1
  16. #define    editMenu    2
  17. #define fontMenu    3
  18.  
  19. #define    appleID        128
  20. #define    fileID        129
  21. #define    editID        130
  22. #define fontID        131
  23.  
  24. #define    newCommand        1
  25. #define    openCommand        2
  26. #define insertCommand    3
  27. #define    closeCommand    4
  28. #define    saveCommand        5
  29. #define    saveAsCommand    6
  30. #define makeBigCommand    8
  31. #define    quitCommand        10
  32.  
  33. #define    undoCommand        1
  34. #define    cutCommand        3
  35. #define    copyCommand        4
  36. #define pasteCommand    5
  37. #define    clearCommand    6
  38. #define    selectCommand    7
  39. #define    wrapCommand        9
  40.  
  41.  
  42.  
  43. char            alive;
  44. MenuHandle        myMenus[numMenus];
  45. char            tempString[256];
  46. WindowPtr        theWPtr = 0L;
  47. TE32KHandle        theTEH32K = 0L;
  48. ControlHandle    theVScroll = 0L, theHScroll = 0L;
  49. Cursor            editCursor,waitCursor;
  50. SFReply            mySFReply;
  51. char            changed;
  52. int                defaultFont,defaultFontSize;
  53. char            *theTestString = "This is terminated by a \\n character\n";
  54.  
  55. RestartProc()
  56. {
  57.     ExitToShell();
  58. }
  59.  
  60.  
  61.  
  62. main()
  63. {
  64. EventRecord        theEvent;
  65. WindowPtr        whichWindow;
  66. unsigned char    theChar;
  67.  
  68.     InitGraf(&thePort);
  69.     InitFonts();
  70.     FlushEvents(everyEvent,0);
  71.     InitWindows();
  72.     InitMenus();
  73.     TEInit();
  74.     InitDialogs(RestartProc);
  75.     InitCursor();
  76.     
  77.     TE32KInit();
  78.     
  79.     SetUpCursors();
  80.     
  81.     SetUpMenus();
  82.     
  83.     defaultFont = monaco;
  84.     defaultFontSize = 9;
  85.     MaintainFontMenu();
  86.     
  87.     FlushEvents(everyEvent,0);
  88.     
  89.     alive = TRUE;
  90.     
  91.     while (alive)
  92.     {
  93.         SystemTask();
  94.         MaintainCursor();
  95.         MaintainMenus();
  96.         DoIdle();
  97.         
  98.         if (GetNextEvent(everyEvent,&theEvent))
  99.         {
  100.             switch (theEvent.what)
  101.             {
  102.                 case mouseDown:
  103.                     DoMouseDown(&theEvent);
  104.                     break;
  105.                 
  106.                 case keyDown:
  107.                 case autoKey: 
  108.                     
  109.                     theChar = theEvent.message & charCodeMask;
  110.                     
  111.                     if (theEvent.modifiers & cmdKey)
  112.                         DoCommand(MenuKey((unsigned char) theChar));
  113.                     
  114.                     else
  115.                         DoKey((unsigned char) theChar);
  116.                     
  117.                     break;
  118.                     
  119.                 case activateEvt:
  120.                     if (IsOurWindow((WindowPtr) theEvent.message)) 
  121.                         DoActivateDeactivate((WindowPtr) theEvent.message,(char) (theEvent.modifiers & activeFlag));
  122.                     
  123.                     break;
  124.                     
  125.                 case updateEvt: 
  126.                     if (IsOurWindow((WindowPtr) theEvent.message)) 
  127.                         UpdateWindow((WindowPtr) theEvent.message);
  128.                     
  129.                     break;
  130.             }
  131.         }
  132.     }
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139. pascal void HiliteDefaultButton(theDPtr, whichItem)
  140. DialogPtr theDPtr; int whichItem;
  141. {
  142. int            type;
  143. Handle        theItem;
  144. Rect        theRect;
  145.  
  146.     GetDItem(theDPtr, ((DialogPeek) theDPtr)->aDefItem, &type, &theItem, &theRect);
  147.     PenNormal();
  148.     PenSize(3,3);
  149.     InsetRect(&theRect,-4,-4);
  150.     FrameRoundRect(&theRect,16,16);
  151.     PenSize(1,1);
  152. }
  153.  
  154.  
  155.  
  156. ErrorAlert(p0)
  157. char    *p0;
  158. {
  159. GrafPtr            oldPort;
  160. DialogPtr        theDPtr,tempDPtr;
  161. int                itemHit, type;
  162. Handle            theItem;
  163. Rect            theRect;
  164. EventRecord        theEvent;
  165.  
  166.     GetPort(&oldPort);
  167.     
  168.     InitCursor();
  169.     
  170.     if (!(theDPtr = GetNewDialog(errorDLOG, NULL,(WindowPtr) -1L)))
  171.     {
  172.         SysBeep(1);
  173.         ExitToShell();
  174.     }
  175.     
  176.     SetPort(theDPtr);
  177.     
  178.     CenterWindow(theDPtr);
  179.     ShowWindow(theDPtr);
  180.     
  181.     ((DialogPeek) theDPtr)->aDefItem = 1;
  182.     
  183.     GetDItem(theDPtr, 3, &type, &theItem, &theRect);
  184.     SetDItem(theDPtr, 3, type, HiliteDefaultButton, &theRect);
  185.     
  186.     GetDItem(theDPtr, 2, &type, &theItem, &theRect);
  187.     CtoPstr(p0);
  188.     SetIText(theItem, p0);
  189.     PtoCstr(p0);
  190.     
  191.     while (!GetNextEvent(updateMask,&theEvent));
  192.     
  193.     if (theEvent.message == (long) theDPtr)
  194.         DialogSelect(&theEvent,&tempDPtr,&itemHit);
  195.     
  196.     SysBeep(1);
  197.     
  198.     do 
  199.     {
  200.         itemHit = 0;
  201.         
  202.         while (!GetNextEvent(everyEvent,&theEvent));
  203.         
  204.         if (theEvent.what==keyDown || theEvent.what==autoKey)
  205.         {
  206.             if ((theEvent.message & charCodeMask)=='\r' || (theEvent.message & charCodeMask)==0x03)
  207.                 itemHit = ((DialogPeek) theDPtr)->aDefItem;
  208.             else
  209.                 SysBeep(1);
  210.         }
  211.         
  212.         else
  213.         {
  214.             tempDPtr = (DialogPtr) 0L;
  215.             
  216.             if (!IsDialogEvent(&theEvent) || !DialogSelect(&theEvent,&tempDPtr,&itemHit) || tempDPtr!=theDPtr)
  217.                 itemHit = 0;
  218.         }
  219.         
  220.     } while (itemHit!=1);
  221.     
  222.     DisposDialog(theDPtr);
  223.     SetPort(oldPort);
  224. }
  225.  
  226.  
  227.  
  228.  
  229.  
  230. SetUpMenus()
  231. {
  232. int            i,numDAs;
  233.  
  234.     myMenus[appleMenu] = GetMenu(appleID);
  235.     AddResMenu(myMenus[appleMenu],'DRVR');
  236.     myMenus[fileMenu] = GetMenu(fileID);
  237.     myMenus[editMenu] = GetMenu(editID);
  238.     myMenus[fontMenu] = GetMenu(fontID);
  239.     InsertResMenu(myMenus[fontMenu],'FONT',0);
  240.     
  241.     InsertMenu(myMenus[appleMenu],0);
  242.     InsertMenu(myMenus[fileMenu],0);
  243.     InsertMenu(myMenus[editMenu],0);
  244.     InsertMenu(myMenus[fontMenu],0);
  245.     
  246.     DrawMenuBar();
  247.     
  248.     numDAs = CountMItems(myMenus[appleMenu]);
  249.     
  250.     for (i=1;i<=numDAs;i++)
  251.         EnableItem(myMenus[appleMenu],i);
  252. }
  253.  
  254.  
  255.  
  256. MaintainMenus()
  257. {
  258. int        i,numItems;
  259.  
  260.     if (theWPtr && FrontWindow() == theWPtr)
  261.     {
  262.         DisableItem(myMenus[fileMenu],newCommand);
  263.         DisableItem(myMenus[fileMenu],openCommand);
  264.         EnableItem(myMenus[fileMenu],insertCommand);
  265.         EnableItem(myMenus[fileMenu],closeCommand);
  266.         
  267.         if (changed)
  268.             EnableItem(myMenus[fileMenu],saveCommand);
  269.         else
  270.             DisableItem(myMenus[fileMenu],saveCommand);
  271.         
  272.         EnableItem(myMenus[fileMenu],saveAsCommand);
  273.         
  274.         
  275.         DisableItem(myMenus[editMenu],undoCommand);
  276.         
  277.         if (theTEH32K)
  278.         {
  279.             if ((**theTEH32K).selStart == (**theTEH32K).selEnd)
  280.             {
  281.                 DisableItem(myMenus[editMenu],cutCommand);
  282.                 DisableItem(myMenus[editMenu],copyCommand);
  283.                 DisableItem(myMenus[editMenu],clearCommand);
  284.             }
  285.             
  286.             else
  287.             {
  288.                 EnableItem(myMenus[editMenu],cutCommand);
  289.                 EnableItem(myMenus[editMenu],copyCommand);
  290.                 EnableItem(myMenus[editMenu],clearCommand);
  291.             }
  292.             
  293.             if (TE32KGetScrapLen() > 0L)
  294.                 EnableItem(myMenus[editMenu],pasteCommand);
  295.             else
  296.                 DisableItem(myMenus[editMenu],pasteCommand);
  297.             
  298.             EnableItem(myMenus[editMenu],selectCommand);
  299.             EnableItem(myMenus[editMenu],wrapCommand);
  300.             
  301.             if (theTEH32K && !(**theTEH32K).crOnly)
  302.             {
  303.                 CheckItem(myMenus[editMenu],wrapCommand,TRUE);
  304.                 SetItemMark(myMenus[editMenu],wrapCommand,checkMark);
  305.             }
  306.                 
  307.             else
  308.                 CheckItem(myMenus[editMenu],wrapCommand,FALSE);
  309.         }
  310.         
  311.         
  312.         numItems = CountMItems(myMenus[fontMenu]);
  313.         
  314.         for (i=1;i<=numItems;i++)
  315.             EnableItem(myMenus[fontMenu],i);
  316.         
  317.         MaintainFontMenu();
  318.     }
  319.     
  320.     else
  321.     {
  322.         EnableItem(myMenus[fileMenu],newCommand);
  323.         EnableItem(myMenus[fileMenu],openCommand);
  324.         DisableItem(myMenus[fileMenu],insertCommand);
  325.         DisableItem(myMenus[fileMenu],closeCommand);
  326.         DisableItem(myMenus[fileMenu],saveCommand);
  327.         DisableItem(myMenus[fileMenu],saveAsCommand);
  328.         
  329.         
  330.         EnableItem(myMenus[editMenu],undoCommand);
  331.         EnableItem(myMenus[editMenu],cutCommand);
  332.         EnableItem(myMenus[editMenu],copyCommand);
  333.         EnableItem(myMenus[editMenu],pasteCommand);
  334.         EnableItem(myMenus[editMenu],clearCommand);
  335.         DisableItem(myMenus[editMenu],selectCommand);
  336.         DisableItem(myMenus[editMenu],wrapCommand);
  337.         CheckItem(myMenus[editMenu],wrapCommand,FALSE);
  338.         
  339.         numItems = CountMItems(myMenus[fontMenu]);
  340.         
  341.         for (i=1;i<=numItems;i++)
  342.         {
  343.             DisableItem(myMenus[fontMenu],i);
  344.             CheckItem(myMenus[fontMenu],i,FALSE);
  345.         }
  346.     }
  347.  
  348. }
  349.  
  350.  
  351. DoCommand(mResult)
  352. long mResult;
  353. {
  354. int            theItem,numItems,userChoice;
  355. WindowPeek     wPtr;
  356. GrafPtr        oldPort;
  357. SFReply        oldSFReply;
  358. Rect        tempRect;
  359.  
  360.  
  361.     theItem = LoWord(mResult);
  362.     
  363.     switch (HiWord(mResult)) 
  364.     {
  365.         case appleID:
  366.             if (theItem == 1)
  367.             {
  368.                 DoAboutBox(0);
  369.             }
  370.             
  371.             else
  372.             {
  373.                 GetPort(&oldPort);
  374.                 GetItem(myMenus[appleMenu], theItem, &tempString);
  375.                 OpenDeskAcc(&tempString);
  376.                 SetPort(oldPort);
  377.             }
  378.             
  379.             break;
  380.             
  381.         case fileID:
  382.             switch (theItem)
  383.             {
  384.                 case newCommand:
  385.                     if (!theTEH32K)
  386.                         DoShowWindow();
  387.                     else
  388.                         SysBeep(1);
  389.                     
  390.                     break;
  391.                 
  392.                 case openCommand:
  393.                     if (theTEH32K && DoCloseWindow(theWPtr))
  394.                     {
  395.                         DoShowWindow();
  396.                         
  397.                         if (OpenTextFile(theTEH32K,&mySFReply))
  398.                         {
  399.                             SetWTitle(theWPtr,mySFReply.fName);
  400.                             AdjustScrollBar();
  401.                             changed = FALSE;
  402.                         }
  403.                     }
  404.                     
  405.                     else if (!theTEH32K)
  406.                     {
  407.                         DoShowWindow();
  408.                         
  409.                         if (OpenTextFile(theTEH32K,&mySFReply))
  410.                         {
  411.                             SetWTitle(theWPtr,mySFReply.fName);
  412.                             AdjustScrollBar();
  413.                             changed = FALSE;
  414.                         }
  415.                     }
  416.                     
  417.                     break;
  418.                 
  419.                 case insertCommand:
  420.                     if (theTEH32K)
  421.                     {
  422.                         changed = InsertTextFile(theTEH32K);
  423.                         
  424.                         AdjustScrollBar();
  425.                     }
  426.                     
  427.                     break;
  428.                     
  429.                 case closeCommand:
  430.                     if (theTEH32K)
  431.                         DoCloseWindow(theWPtr);
  432.                     else
  433.                         SysBeep(1);
  434.                         
  435.                     break;
  436.                 
  437.                 case saveCommand:
  438.                     if (theTEH32K)
  439.                     {
  440.                         if (SaveTextFile(theTEH32K,&mySFReply))
  441.                         {
  442.                             SetWTitle(theWPtr,mySFReply.fName);
  443.                             changed = FALSE;
  444.                         }
  445.                     }
  446.                         
  447.                     else
  448.                         SysBeep(1);
  449.                     
  450.                     break;
  451.                 
  452.                 case saveAsCommand:
  453.                     if (theTEH32K)
  454.                     {
  455.                         oldSFReply = mySFReply;
  456.                         mySFReply.good = FALSE;
  457.                         
  458.                         if (SaveTextFile(theTEH32K,&mySFReply))
  459.                         {
  460.                             changed = FALSE;
  461.                             SetWTitle(theWPtr,mySFReply.fName);
  462.                         }
  463.                         else
  464.                             mySFReply = oldSFReply;
  465.                     }
  466.                         
  467.                     else
  468.                         SysBeep(1);
  469.                     
  470.                     break;
  471.                 
  472.                 case makeBigCommand:
  473.                     CreateBigFile();
  474.                     break;
  475.                 
  476.                 case quitCommand:
  477.                     if (theTEH32K)
  478.                     {
  479.                         if (DoCloseWindow(theWPtr))
  480.                             alive = FALSE;
  481.                     }
  482.                     else
  483.                         alive = FALSE;
  484.                     
  485.                     break;
  486.             }
  487.             
  488.             break;
  489.         
  490.         case editID:
  491.             switch(theItem)
  492.             {
  493.                 case cutCommand:
  494.                     if (!DoCut())
  495.                         SystemEdit(theItem - 1);
  496.                     break;
  497.                 
  498.                 case copyCommand:
  499.                     if (!DoCopy())
  500.                         SystemEdit(theItem - 1);
  501.                     break;
  502.                 
  503.                 case pasteCommand:
  504.                     if (!DoPaste())
  505.                         SystemEdit(theItem - 1);
  506.                     
  507.                     break;
  508.                 
  509.                 case clearCommand:
  510.                     if (!DoClear())
  511.                         SystemEdit(theItem - 1);
  512.                     
  513.                     break;
  514.                 
  515.                 case selectCommand:
  516.                     if (!DoSelectAll())
  517.                         SystemEdit(theItem - 1);
  518.                     
  519.                     break;
  520.                 
  521.                 case wrapCommand:
  522.                     if (theWPtr && theWPtr == FrontWindow() && theTEH32K)
  523.                     {
  524.                         (**theTEH32K).crOnly = !(**theTEH32K).crOnly;
  525.                         
  526.                         TE32KCalText(theTEH32K);
  527.                         
  528.                         tempRect = theWPtr->portRect;
  529.                         GetPort(&oldPort);
  530.                         SetPort(theWPtr);
  531.                         InvalRect(&tempRect);
  532.                         SetPort(oldPort);
  533.                         
  534.                         AdjustForResizedWindow();
  535.                     }
  536.                     
  537.                     break;
  538.                 
  539.                 
  540.                 default:
  541.                     SystemEdit(theItem - 1);
  542.                     break;
  543.             }
  544.             
  545.             break;
  546.             
  547.         case fontID:
  548.             numItems = CountMItems(myMenus[fontMenu]);
  549.             
  550.             if (theItem <= numItems-7)
  551.             {
  552.                 GetItem(myMenus[fontMenu], theItem, &tempString);
  553.                 PtoCstr(tempString);
  554.                 DoFontSize(tempString,-1);
  555.             }
  556.             
  557.             else
  558.             {
  559.                 GetItem(myMenus[fontMenu], theItem, &tempString);
  560.                 PtoCstr(tempString);
  561.                 
  562.                 if (sscanf(tempString,"%d",&numItems)==1)
  563.                 {
  564.                     tempString[0] = 0;
  565.                     DoFontSize(tempString,numItems);
  566.                 }
  567.             }
  568.             
  569.             break;
  570.     }
  571.     
  572.     HiliteMenu(0);
  573. }
  574.  
  575.  
  576.  
  577.  
  578. OpenTextFile(theTEH,mySFReply)
  579. TE32KHandle    theTEH;SFReply    *mySFReply;
  580. {
  581. SFTypeList    mySFTypes;
  582. Point        theTopLeft;
  583. int            errCode,fRefNum;
  584. long        byteCount;
  585. Ptr            theText;
  586.  
  587.  
  588.     if (theTEH)
  589.     {
  590.         theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 348)/2;;
  591.         theTopLeft.v = screenBits.bounds.top + 50;
  592.         
  593.         mySFTypes[0] = 'TEXT';
  594.         
  595.         InitCursor();
  596.         
  597.         SFGetFile(theTopLeft,"\Select Text File To Open:",0L,1,&mySFTypes,0L,mySFReply);
  598.         
  599.         if (mySFReply->good)
  600.         {
  601.             if ((errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum))!=noErr)
  602.             {
  603.                 mySFReply->good = FALSE;
  604.                 PtoCstr((char *) mySFReply->fName);
  605.                 sprintf(tempString,"OpenTextFile: FSOpen('%s') Error %d",mySFReply->fName,errCode);
  606.                 ErrorAlert(tempString);
  607.                 return(FALSE);
  608.             }
  609.             
  610.             if ((errCode = GetEOF(fRefNum,&byteCount))!=noErr)
  611.             {
  612.                 mySFReply->good = FALSE;
  613.                 FSClose(fRefNum);
  614.                 PtoCstr((char *) mySFReply->fName);
  615.                 sprintf(tempString,"OpenTextFile: GetEOF('%s',0) Error %d",mySFReply->fName,errCode);
  616.                 ErrorAlert(tempString);
  617.                 return(FALSE);
  618.             }
  619.             
  620.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  621.             {
  622.                 mySFReply->good = FALSE;
  623.                 FSClose(fRefNum);
  624.                 PtoCstr((char *) mySFReply->fName);
  625.                 sprintf(tempString,"OpenTextFile: SetFPos('%s',0) Error %d",mySFReply->fName,errCode);
  626.                 ErrorAlert(tempString);
  627.                 return(FALSE);
  628.             }
  629.             
  630.             theText = NewPtr(byteCount);
  631.             
  632.             if (StripAddress(theText)==0L || MemError())
  633.             {
  634.                 mySFReply->good = FALSE;
  635.                 FSClose(fRefNum);
  636.                 sprintf(tempString,"OpenTextFile: NewPtr(%ld) Error %d",byteCount,MemError());
  637.                 ErrorAlert(tempString);
  638.                 return(FALSE);
  639.             }
  640.             
  641.             SetCursor(&waitCursor);
  642.             
  643.             if ((errCode = FSRead(fRefNum,&byteCount,theText))!=noErr)
  644.             {
  645.                 InitCursor();
  646.                 mySFReply->good = FALSE;
  647.                 DisposPtr(theText);
  648.                 FSClose(fRefNum);
  649.                 PtoCstr((char *) mySFReply->fName);
  650.                 sprintf(tempString,"OpenTextFile: FSRead('%s') Error %d",mySFReply->fName,errCode);
  651.                 ErrorAlert(tempString);
  652.                 return(FALSE);
  653.             }
  654.             
  655.             if ((errCode = FSClose(fRefNum))!=noErr)
  656.             {
  657.                 InitCursor();
  658.                 mySFReply->good = FALSE;
  659.                 DisposPtr(theText);
  660.                 PtoCstr((char *) mySFReply->fName);
  661.                 sprintf(tempString,"OpenTextFile: FSClose('%s',0) Error %d",mySFReply->fName,errCode);
  662.                 ErrorAlert(tempString);
  663.                 return(FALSE);
  664.             }
  665.             
  666.             TE32KSetText(theText,byteCount,theTEH);
  667.             
  668.             DisposPtr(theText);
  669.             
  670.             InitCursor();
  671.             
  672.             return(TRUE);
  673.         }
  674.         
  675.         else
  676.             return(FALSE);
  677.     }
  678.     
  679.     else
  680.         return(FALSE);
  681. }
  682.  
  683.  
  684.  
  685. SaveTextFile(theTEH,mySFReply)
  686. TE32KHandle    theTEH;SFReply    *mySFReply;
  687. {
  688. Point        theTopLeft;
  689. int            errCode,fRefNum;
  690. long        byteCount;
  691. Handle        theText;
  692.  
  693.     if (theTEH)
  694.     {
  695.         theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 304)/2;;
  696.         theTopLeft.v = screenBits.bounds.top + 50;
  697.         
  698.         InitCursor();
  699.         
  700.         if (!(mySFReply->good))
  701.             SFPutFile(theTopLeft,"\pSave Text File As:","\p",0L,mySFReply);
  702.         
  703.         if (mySFReply->good)
  704.         {
  705.             if ((errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum))!=noErr)
  706.             {
  707.                 if (errCode==fnfErr)
  708.                 {
  709.                     if ((errCode = Create(mySFReply->fName,mySFReply->vRefNum,'????','TEXT'))!=noErr)
  710.                     {
  711.                         mySFReply->good = FALSE;
  712.                         PtoCstr((char *) mySFReply->fName);
  713.                         sprintf(tempString,"SaveTextFile: Create('%s') Error %d",mySFReply->fName,errCode);
  714.                         ErrorAlert(tempString);
  715.                         return(FALSE);
  716.                     }
  717.                     
  718.                     errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum);
  719.                 }
  720.                 
  721.                 if (errCode != noErr)
  722.                 {
  723.                     mySFReply->good = FALSE;
  724.                     PtoCstr((char *) mySFReply->fName);
  725.                     sprintf(tempString,"SaveTextFile: FSOpen('%s') Error %d",mySFReply->fName,errCode);
  726.                     ErrorAlert(tempString);
  727.                     return(FALSE);
  728.                 }
  729.             }
  730.             
  731.             if ((errCode = SetEOF(fRefNum,0L))!=noErr)
  732.             {
  733.                 mySFReply->good = FALSE;
  734.                 FSClose(fRefNum);
  735.                 PtoCstr((char *) mySFReply->fName);
  736.                 sprintf(tempString,"SaveTextFile: SetEOF('%s',0) Error %d",mySFReply->fName,errCode);
  737.                 ErrorAlert(tempString);
  738.                 return(FALSE);
  739.             }
  740.             
  741.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  742.             {
  743.                 mySFReply->good = FALSE;
  744.                 FSClose(fRefNum);
  745.                 PtoCstr((char *) mySFReply->fName);
  746.                 sprintf(tempString,"SaveTextFile: SetFPos('%s',0) Error %d",mySFReply->fName,errCode);
  747.                 ErrorAlert(tempString);
  748.                 return(FALSE);
  749.             }
  750.             
  751.             theText = (**theTEH).hText;
  752.             byteCount = (**theTEH).teLength;
  753.             
  754.             HLock(theText);
  755.             
  756.             SetCursor(&waitCursor);
  757.             
  758.             if ((errCode = FSWrite(fRefNum,&byteCount,*theText))!=noErr)
  759.             {
  760.                 InitCursor();
  761.                 mySFReply->good = FALSE;
  762.                 HUnlock(theText);
  763.                 FSClose(fRefNum);
  764.                 PtoCstr((char *) mySFReply->fName);
  765.                 sprintf(tempString,"SaveTextFile: FSWrite('%s') Error %d",mySFReply->fName,errCode);
  766.                 ErrorAlert(tempString);
  767.                 return(FALSE);
  768.             }
  769.             
  770.             HUnlock(theText);
  771.             
  772.             if ((errCode = FSClose(fRefNum))!=noErr)
  773.             {
  774.                 InitCursor();
  775.                 mySFReply->good = FALSE;
  776.                 PtoCstr((char *) mySFReply->fName);
  777.                 sprintf(tempString,"SaveTextFile: FSClose('%s',0) Error %d",mySFReply->fName,errCode);
  778.                 ErrorAlert(tempString);
  779.                 return(FALSE);
  780.             }
  781.             
  782.             FlushVol("\p",mySFReply->vRefNum);
  783.             
  784.             InitCursor();
  785.             
  786.             return(TRUE);
  787.         }
  788.         
  789.         else
  790.             return(FALSE);
  791.     }
  792.     
  793.     else
  794.         return(FALSE);
  795. }
  796.  
  797.  
  798.  
  799.  
  800. YesNoCancel(thePrompt,theDefault)
  801. char    *thePrompt;int    theDefault;
  802. {
  803. DialogPtr        theDPtr,tempDPtr;
  804. int                itemHit, type;
  805. Handle            theItem;
  806. Rect            tempRect;
  807. GrafPtr            oldPort;
  808. char            tempString[256];
  809. EventRecord        theEvent;
  810.  
  811.     GetPort(&oldPort);
  812.     
  813.     InitCursor();
  814.     
  815.     theDPtr = GetNewDialog(yesNoCancelDLOG, 0L,(WindowPtr) -1L);
  816.     
  817.     CenterWindow(theDPtr);
  818.     ShowWindow(theDPtr);
  819.     
  820.     SetPort(theDPtr);
  821.     
  822.     ((DialogPeek) theDPtr)->aDefItem = theDefault;
  823.     
  824.     GetDItem(theDPtr, 4, &type, &theItem, &tempRect);
  825.     SetDItem(theDPtr, 4, type, HiliteDefaultButton, &tempRect);
  826.     
  827.     strcpy(tempString,thePrompt);
  828.     CtoPstr(tempString);
  829.     GetDItem(theDPtr, 5, &type, &theItem, &tempRect);
  830.     SetIText(theItem, tempString);
  831.     
  832.     if (GetNextEvent(updateMask,&theEvent) && theEvent.message == (long) theDPtr)
  833.         DialogSelect(&theEvent,&tempDPtr,&itemHit);
  834.     
  835.     SysBeep(1);
  836.     
  837.     do 
  838.     {
  839.         ModalDialog(0L,&itemHit);
  840.         
  841.     } while (!(itemHit>=1 && itemHit<=3));
  842.     
  843.     DisposDialog(theDPtr);
  844.     SetPort(oldPort);
  845.     
  846.     return(itemHit);
  847. }
  848.  
  849.  
  850.  
  851.  
  852. CenterWindow(wPtr)
  853. WindowPtr    wPtr;
  854. {
  855. int        screenWidth,screenHeight,windowWidth,windowHeight,left,top;
  856.  
  857.     if (wPtr == 0L)
  858.         return;
  859.     
  860.     screenWidth = screenBits.bounds.right - screenBits.bounds.left;
  861.     screenHeight = screenBits.bounds.bottom - screenBits.bounds.top - 20;
  862.     windowWidth = wPtr->portRect.right - wPtr->portRect.left;
  863.     windowHeight = wPtr->portRect.bottom - wPtr->portRect.top;
  864.     
  865.     left = screenBits.bounds.left + (screenWidth - windowWidth)/2;
  866.     top = screenBits.bounds.top + 20 + (screenHeight - windowHeight)/2;
  867.     
  868.     if (left < 0)
  869.         left = 0;
  870.     
  871.     if (top < 20)
  872.         top = 20;
  873.     
  874.     MoveWindow(wPtr,left,top,FALSE);
  875. }
  876.  
  877.  
  878.  
  879.  
  880. InsertTextFile(theTEH)
  881. TE32KHandle    theTEH;
  882. {
  883. SFReply        mySFReply;
  884. SFTypeList    mySFTypes;
  885. Point        theTopLeft;
  886. int            errCode,fRefNum;
  887. long        byteCount;
  888. Ptr            theText;
  889. char        tempString[256];
  890.  
  891.     if (theTEH)
  892.     {
  893.         theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 348)/2;;
  894.         theTopLeft.v = screenBits.bounds.top + 50;
  895.         
  896.         mySFTypes[0] = 'TEXT';
  897.         
  898.         InitCursor();
  899.         
  900.         SFGetFile(theTopLeft,"\Select Text File To Insert:",0L,1,&mySFTypes,0L,&mySFReply);
  901.         
  902.         if (mySFReply.good)
  903.         {
  904.             if ((errCode = FSOpen(mySFReply.fName,mySFReply.vRefNum,&fRefNum))!=noErr)
  905.             {
  906.                 PtoCstr((char *) mySFReply.fName);
  907.                 sprintf(tempString,"InsertTextFile: FSOpen('%s') Error %d",mySFReply.fName,errCode);
  908.                 ErrorAlert(tempString);
  909.                 return(TRUE);
  910.             }
  911.             
  912.             if ((errCode = GetEOF(fRefNum,&byteCount))!=noErr)
  913.             {
  914.                 FSClose(fRefNum);
  915.                 PtoCstr((char *) mySFReply.fName);
  916.                 sprintf(tempString,"InsertTextFile: GetEOF('%s',0) Error %d",mySFReply.fName,errCode);
  917.                 ErrorAlert(tempString);
  918.                 return(TRUE);
  919.             }
  920.             
  921.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  922.             {
  923.                 FSClose(fRefNum);
  924.                 PtoCstr((char *) mySFReply.fName);
  925.                 sprintf(tempString,"InsertTextFile: SetFPos('%s',0) Error %d",mySFReply.fName,errCode);
  926.                 ErrorAlert(tempString);
  927.                 return(TRUE);
  928.             }
  929.             
  930.             theText = NewPtr(byteCount);
  931.             
  932.             if (StripAddress(theText)==0L || MemError())
  933.             {
  934.                 FSClose(fRefNum);
  935.                 sprintf(tempString,"InsertTextFile: NewPtr(%ld) Error %d",byteCount,MemError());
  936.                 ErrorAlert(tempString);
  937.                 return(TRUE);
  938.             }
  939.             
  940.             if ((errCode = FSRead(fRefNum,&byteCount,theText))!=noErr)
  941.             {
  942.                 DisposPtr(theText);
  943.                 FSClose(fRefNum);
  944.                 PtoCstr((char *) mySFReply.fName);
  945.                 sprintf(tempString,"InsertTextFile: FSRead('%s') Error %d",mySFReply.fName,errCode);
  946.                 ErrorAlert(tempString);
  947.                 return(TRUE);
  948.             }
  949.             
  950.             if ((errCode = FSClose(fRefNum))!=noErr)
  951.             {
  952.                 DisposPtr(theText);
  953.                 PtoCstr((char *) mySFReply.fName);
  954.                 sprintf(tempString,"InsertTextFile: FSClose('%s',0) Error %d",mySFReply.fName,errCode);
  955.                 ErrorAlert(tempString);
  956.                 return(TRUE);
  957.             }
  958.             
  959.             SetCursor(&waitCursor);
  960.             
  961.             TE32KInsert(theText,byteCount,theTEH);
  962.             
  963.             InitCursor();
  964.             
  965.             DisposPtr(theText);
  966.         }
  967.         
  968.         return(TRUE);
  969.     }
  970.     
  971.     else
  972.         return(FALSE);
  973. }
  974.  
  975.  
  976.  
  977.  
  978. DoFontSize(theFontName,theFontSize)
  979. char *theFontName;int    theFontSize;
  980. {
  981. WindowPtr    theWPtr;
  982. int            theFontNum;
  983. Rect        tempRect;
  984. GrafPtr        oldPort;
  985.     
  986.     if (theTEH32K)
  987.     {
  988.         if (theFontName[0])
  989.         {
  990.             CtoPstr(theFontName);
  991.             GetFNum(theFontName,&theFontNum);
  992.         }
  993.         else
  994.             theFontNum = -1;
  995.         
  996.         
  997.         if (theFontNum < 0)
  998.             theFontNum = defaultFont;
  999.         
  1000.         if (theFontSize <= 0)
  1001.             theFontSize = defaultFontSize;
  1002.             
  1003.         defaultFont = theFontNum;
  1004.         defaultFontSize = theFontSize;
  1005.         
  1006.         TE32KSetFontStuff(theFontNum,0,srcCopy,theFontSize,theTEH32K);
  1007.         
  1008.         AdjustForResizedWindow();
  1009.         
  1010.         GetPort(&oldPort);
  1011.         SetPort((**theTEH32K).inPort);
  1012.         tempRect = (*(**theTEH32K).inPort).portRect;
  1013.         InvalRect(&tempRect);
  1014.         SetPort(oldPort);
  1015.         
  1016.         MaintainFontMenu();
  1017.     }
  1018. }
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024. MaintainFontMenu()
  1025. {
  1026. int        i,numItems,tempSize;
  1027. char    theFontString[128];
  1028. char    tempString[256];
  1029.  
  1030.     numItems = CountMItems(myMenus[fontMenu]);
  1031.     
  1032.     GetFontName(defaultFont,theFontString);
  1033.     PtoCstr(theFontString);
  1034.     
  1035.     for (i=1;i<=numItems-7;i++)
  1036.     {
  1037.         GetItem(myMenus[fontMenu],i,&tempString);
  1038.         PtoCstr(tempString);
  1039.         
  1040.         if (strcmp(tempString,theFontString) == EQSTR)
  1041.         {
  1042.             CheckItem(myMenus[fontMenu],i,TRUE);
  1043.             SetItemMark(myMenus[fontMenu],i,checkMark);
  1044.         }
  1045.         else
  1046.             CheckItem(myMenus[fontMenu],i,FALSE);
  1047.     }
  1048.     
  1049.     
  1050.     sprintf(theFontString,"%d",defaultFontSize);
  1051.     
  1052.     for (i=numItems-6;i<=numItems;i++)
  1053.     {
  1054.         GetItem(myMenus[fontMenu],i,&tempString);
  1055.         PtoCstr(tempString);
  1056.         
  1057.         if (sscanf(tempString,"%d",&tempSize) != 1)
  1058.             tempSize = -1;
  1059.         
  1060.         if (strcmp(tempString,theFontString) == EQSTR)
  1061.         {
  1062.             CheckItem(myMenus[fontMenu],i,TRUE);
  1063.             SetItemMark(myMenus[fontMenu],i,checkMark);
  1064.         }
  1065.         else
  1066.             CheckItem(myMenus[fontMenu],i,FALSE);
  1067.         
  1068.         if (RealFont(defaultFont,tempSize))
  1069.             SetItemStyle(myMenus[fontMenu],i,outline);
  1070.         else
  1071.             SetItemStyle(myMenus[fontMenu],i,0);
  1072.     }
  1073. }
  1074.  
  1075.  
  1076.  
  1077.  
  1078. CreateBigFile()
  1079. {
  1080. int            errCode,fRefNum;
  1081. long        byteCount,i;
  1082. char        tempString[256];
  1083. SFReply        mySFReply;
  1084. Point        theTopLeft;
  1085.  
  1086.     theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 304)/2;;
  1087.     theTopLeft.v = screenBits.bounds.top + 50;
  1088.     
  1089.     InitCursor();
  1090.     
  1091.     strcpy((char *) mySFReply.fName,"Big Text File");
  1092.     CtoPstr((char *) mySFReply.fName);
  1093.     
  1094.     SFPutFile(theTopLeft,"\pSave 10,000 line file as:",mySFReply.fName,0L,&mySFReply);
  1095.     
  1096.     if (!(mySFReply.good))
  1097.         return;
  1098.     
  1099.     if ((errCode = FSOpen(mySFReply.fName,mySFReply.vRefNum,&fRefNum))!=noErr)
  1100.     {
  1101.         if (errCode==fnfErr)
  1102.         {
  1103.             if ((errCode = Create(mySFReply.fName,mySFReply.vRefNum,'????','TEXT'))!=noErr)
  1104.             {
  1105.                 PtoCstr((char *) mySFReply.fName);
  1106.                 sprintf(tempString,"createBigFile: Create('%s') Error %d",(char *) mySFReply.fName,errCode);
  1107.                 ErrorAlert(tempString);
  1108.                 return(FALSE);
  1109.             }
  1110.             
  1111.             errCode = FSOpen(mySFReply.fName,mySFReply.vRefNum,&fRefNum);
  1112.         }
  1113.         
  1114.         if (errCode != noErr)
  1115.         {
  1116.             PtoCstr((char *) mySFReply.fName);
  1117.             sprintf(tempString,"createBigFile: FSOpen('%s') Error %d",(char *) mySFReply.fName,errCode);
  1118.             ErrorAlert(tempString);
  1119.             return(FALSE);
  1120.         }
  1121.     }
  1122.             
  1123.     if ((errCode = SetEOF(fRefNum,0L))!=noErr)
  1124.     {
  1125.         FSClose(fRefNum);
  1126.         PtoCstr((char *) mySFReply.fName);
  1127.         sprintf(tempString,"createBigFile: SetEOF('%s',0L) Error %d",(char *) mySFReply.fName,errCode);
  1128.         ErrorAlert(tempString);
  1129.         return(FALSE);
  1130.     }
  1131.     
  1132.     if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  1133.     {
  1134.         FSClose(fRefNum);
  1135.         PtoCstr((char *) mySFReply.fName);
  1136.         sprintf(tempString,"createBigFile: SetFPos('%s',0) Error %d",(char *) mySFReply.fName,errCode);
  1137.         ErrorAlert(tempString);
  1138.         return(FALSE);
  1139.     }
  1140.     
  1141.     SetCursor(&waitCursor);
  1142.     
  1143.     for (i=0;i<10000;i++)
  1144.     {
  1145.         sprintf(tempString,"line %ld\r",i);
  1146.         byteCount = strlen(tempString);
  1147.         
  1148.         if ((errCode = FSWrite(fRefNum,&byteCount,tempString))!=noErr)
  1149.         {
  1150.             InitCursor();
  1151.             FSClose(fRefNum);
  1152.             PtoCstr((char *) mySFReply.fName);
  1153.             sprintf(tempString,"createBigFile: FSWrite('%s') Error %d",(char *) mySFReply.fName,errCode);
  1154.             ErrorAlert(tempString);
  1155.             return(FALSE);
  1156.         }
  1157.     }
  1158.         
  1159.     if ((errCode = FSClose(fRefNum))!=noErr)
  1160.     {
  1161.         InitCursor();
  1162.         PtoCstr((char *) mySFReply.fName);
  1163.         sprintf(tempString,"createBigFile: FSClose('%s') Error %d",(char *) mySFReply.fName,errCode);
  1164.         ErrorAlert(tempString);
  1165.         return(FALSE);
  1166.     }
  1167.     
  1168.     FlushVol("\p",mySFReply.vRefNum);
  1169.     
  1170.     InitCursor();
  1171.     
  1172.     return(TRUE);
  1173. }