home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM 1995 Fall / PD-ROM F95.toast / Programming / Programming Languages / UCB Logo 3.0 ƒ / sources / MiniEdit Folder / miniEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-29  |  6.5 KB  |  300 lines  |  [TEXT/ttxt]

  1. /*********************************************************************
  2.  
  3.     Buggy MiniEdit.c
  4.     
  5.     The sample application from Inside Macintosh (RoadMap p.15-17)
  6.     beefed up a bit by Stephen Z. Stein, Symantec Corp.
  7.     Use this file with the “MiniEdit” chapter of your manual.
  8.     
  9.     The resources used in this program are in the file MiniEdit.π.rsrc.
  10.     The file was created with ResEdit, so there is no RMaker source
  11.     for it.
  12.     
  13.     In order for THINK C to find the resource file for this
  14.     project, be sure you’ve named the project MiniEdit.π
  15.     
  16. *********************************************************************/
  17.  
  18. /*
  19.  
  20. #include <QuickDraw.h>
  21. #include <MacTypes.h>
  22. #include <FontMgr.h>
  23. #include <WindowMgr.h>
  24. #include <MenuMgr.h>
  25. #include <TextEdit.h>
  26. #include <DialogMgr.h>
  27. #include <EventMgr.h>
  28. #include <DeskMgr.h>
  29. #include <FileMgr.h>
  30. #include <ToolboxUtil.h>
  31. #include <ControlMgr.h>
  32.  
  33. */
  34.  
  35. #include "MiniEdit.h"
  36.  
  37. #include "logo.h"
  38. #include "globals.h"
  39.  
  40. WindowRecord    wRecord;
  41. WindowPtr        myWindow;
  42. TEHandle        TEH;
  43. int                linesInFolder;
  44. Rect            dragRect = { 0, 0, 1024, 1024 };
  45. ControlHandle     vScroll;
  46. Cursor            editCursor;
  47. Cursor            waitCursor;
  48. BOOLEAN            load_p;
  49.  
  50. BOOLEAN mac_edit()
  51. {
  52.     CursHandle    hCurs;
  53.     MenuHandle    menu_handle;
  54.     WindowPtr    cWindow;
  55.     int         count;
  56.     FILE        *holdstrm;
  57.     
  58.     holdstrm = fopen("temp.txt", "a"); /* need to guarantee file exists on mac */
  59.     if (holdstrm != NULL) fclose(holdstrm);
  60.     else { err_logo(FILE_ERROR,
  61.               make_static_strnode("Could not create editor file"));
  62.            return(0); }
  63.  
  64.     cWindow = FrontWindow();
  65.     HideWindow(cWindow);
  66.     
  67.     menu_handle = GetMHandle(2);
  68.     DisableItem(menu_handle, 1);
  69.     menu_handle = GetMHandle(3);
  70.     for (count = 8; count <= 14; count++)
  71.         EnableItem(menu_handle, count);
  72.     DisableItem(menu_handle, 10);
  73.     DisableItem(menu_handle, 13);
  74.     
  75.     hCurs = GetCursor(1);
  76.     editCursor = **hCurs;
  77.     hCurs = GetCursor(watchCursor);
  78.     waitCursor = **hCurs;
  79.  
  80.     SetUpFile();
  81.     load_p = TRUE;
  82.     while (MainEvent()) ;
  83.     for (count = 8; count <= 14; count++)
  84.         DisableItem(menu_handle, count);
  85.     menu_handle = GetMHandle(2);
  86.     EnableItem(menu_handle, 1);
  87.     CloseMyWindow();
  88.     ShowWindow(cWindow);
  89.     return(load_p);
  90. }
  91.  
  92. int MainEvent() 
  93. {
  94.     EventRecord        myEvent;
  95.     WindowPtr        whichWindow;
  96.     Rect            r;
  97.     
  98.     MaintainCursor();
  99.     SystemTask();
  100.     TEIdle(TEH);
  101.     if (GetNextEvent(everyEvent, &myEvent)) {
  102.         switch (myEvent.what) {
  103.         case mouseDown:
  104.             switch (FindWindow( myEvent.where, &whichWindow )) {
  105.             case inDesk: 
  106.                 /* SysBeep(10); */
  107.                 break;
  108.             case inGoAway:
  109.                 if (ours(whichWindow))
  110.                     if (TrackGoAway( myWindow, myEvent.where) )
  111.                         switch (Alert(SaveChangesP, 0L)) {
  112.                             case choseYes: SaveFile();
  113.                                            return(0);
  114.                                            break;
  115.                             case choseNo:  if (Alert(AreYouSureP, 0L) == 2) {  /* 2 = sure */
  116.                                                 load_p = 0;
  117.                                                 return(0);
  118.                                            }
  119.                                            else
  120.                                                    return(1);
  121.                         }
  122.                 break;
  123.             case inMenuBar:
  124.                 return( DoCommand( MenuSelect(myEvent.where) ) );
  125.             case inSysWindow:
  126.                 SystemClick( &myEvent, whichWindow );
  127.                 break;
  128.             case inDrag:
  129.                 if (ours(whichWindow))
  130.                     DragWindow( whichWindow, myEvent.where, &dragRect );
  131.                 break;
  132.             case inGrow:
  133.                 if (ours(whichWindow))
  134.                     MyGrowWindow( whichWindow, myEvent.where );
  135.                 break;
  136.             case inContent:
  137.                 if (whichWindow != FrontWindow())
  138.                     SelectWindow(whichWindow);
  139.                 else 
  140.                     if (ours(whichWindow))
  141.                         DoContent(whichWindow, &myEvent);
  142.                 break;
  143.             default: ;
  144.             } /* end switch FindWindow */
  145.             break;
  146.         case keyDown:
  147.         case autoKey: 
  148.             {
  149.             register char    theChar;
  150.             
  151.             theChar = myEvent.message & charCodeMask;
  152.             if ((myEvent.modifiers & cmdKey) != 0) 
  153.                 return( DoCommand( MenuKey( theChar ) ));
  154.             else {
  155.                 if ( (theChar != DELETE_KEY && (**TEH).teLength > 30000 ) ||
  156.                      ((unsigned char)theChar < 0x1c && theChar != DELETE_KEY && theChar != TAB_KEY && theChar != RETURN_KEY) ||
  157.                      ((unsigned char)theChar > 0xd8))
  158.                     SysBeep(1);
  159.                 else
  160.                     TEKey( theChar, TEH );
  161.                 ShowSelect();
  162.                 }
  163.             }
  164.             break;
  165.         case activateEvt:
  166.             if (ours((WindowPtr)myEvent.message)) {
  167.                 r=(*myWindow).portRect;
  168.                 r.top = r.bottom - (SBarWidth+1);
  169.                 r.left = r.left - (SBarWidth+1);
  170.                 InvalRect(&r);
  171.                 if ( myEvent.modifiers & activeFlag ) {
  172.                     TEActivate( TEH );
  173.                     ShowControl( vScroll );
  174.                     DisableItem( GetMHandle(3), undoCommand );
  175.                     TEFromScrap();
  176.                 }
  177.                 else {
  178.                     TEDeactivate(TEH);
  179.                     HideControl( vScroll );
  180.                     ZeroScrap();
  181.                     TEToScrap();
  182.                 }
  183.             }
  184.             break;
  185.         case updateEvt: 
  186.             if (ours((WindowPtr)myEvent.message)) UpdateWindow(myWindow);
  187.             if ((WindowPtr)myEvent.message == graphics_window) {
  188.                 SetPort(graphics_window);
  189.                 BeginUpdate(graphics_window);
  190.                 redraw_graphics();
  191.                 EndUpdate(graphics_window);
  192.             }
  193.             break;
  194.         default: ;
  195.         } /* end of case myEvent.what */
  196.     }
  197.     return(1);
  198. }
  199.  
  200. int DoCommand( mResult )
  201. long mResult;
  202. {
  203.     int        theItem, temp;
  204.     Str255    name;
  205.     WindowPeek wPtr;
  206.     DialogPtr myDialog;
  207.     
  208.     theItem = LoWord( mResult );
  209.     switch (HiWord(mResult)) {
  210.     case appleM:
  211.         if (theItem == 1) {
  212.             myDialog = GetNewDialog(100,NULL,(WindowPtr)(-1L));
  213.             ModalDialog(NULL,&theItem);
  214.             DisposDialog(myDialog);
  215.         }
  216.         else {
  217.             GetItem(GetMHandle(1), theItem, &name);
  218.             OpenDeskAcc( &name );
  219.             SetPort( myWindow );
  220.         }
  221.         break;
  222.     case fileM:
  223.         Alert(CantQuit, 0L);
  224.         break;
  225.     case editM:
  226.         if (SystemEdit(theItem-1)==0) {
  227.             wPtr = (WindowPeek) FrontWindow();
  228.             switch (theItem) {
  229.             case cutCommand:
  230.                 TECut( TEH );
  231.                 ZeroScrap();
  232.                 TEToScrap();
  233.                 break;
  234.             case copyCommand:
  235.                 TECopy( TEH );
  236.                 ZeroScrap();
  237.                 TEToScrap();
  238.                 break;
  239.             case pasteCommand:
  240.                 if ((**TEH).teLength > 30000)
  241.                     SysBeep(1);
  242.                 else {
  243.                     TEFromScrap();
  244.                     TEPaste( TEH );
  245.                     }
  246.                 break;
  247.             case clearCommand:
  248.                 TEDelete( TEH );
  249.                 break;
  250.             case acceptCommand:
  251.                 SaveFile();
  252.                 HiliteMenu(0);
  253.                 return(0);
  254.             case cancelCommand:
  255.                 HiliteMenu(0);
  256.                 if (Alert(AreYouSureP, 0L) == 2) {  /* 2 = sure */
  257.                     load_p = 0;
  258.                     return(0);
  259.                 }
  260.                 else
  261.                     return(1);
  262.             case psuCommand:
  263.                 DoPageSetUp();
  264.                 break;
  265.             case printCommand:
  266.                 PrintText((**TEH).hText, (long)(**TEH).teLength, (GrafPtr)myWindow,
  267.                           StringWidth("\pmmmm"), 0L);
  268.                 break;
  269.             }
  270.             ShowSelect();
  271.         }
  272.         break;
  273.     }
  274.     HiliteMenu(0);
  275.     return(1);
  276. }
  277.  
  278. MaintainCursor()
  279. {
  280.     Point        pt;
  281.     WindowPeek    wPtr;
  282.     GrafPtr        savePort;
  283.     
  284.     if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
  285.         GetPort( &savePort );
  286.         SetPort( (GrafPtr)wPtr );
  287.         GetMouse(&pt);
  288.         if ( PtInRect(pt, &(**TEH).viewRect )  )
  289.             SetCursor( &editCursor);
  290.         else InitCursor(); /* SetCursor( &arrow ); */
  291.         SetPort( savePort );
  292.     }
  293. }
  294.  
  295. ours(w)
  296. WindowPtr w;
  297. {
  298.     return( (myWindow!=NULL) && (w==myWindow) );
  299. }
  300.