home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / hexedit105 folder / HexEditSource / Source / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-13  |  9.4 KB  |  417 lines  |  [TEXT/KAHL]

  1. /************************************************************************************
  2.  * Main.c
  3.  *
  4.  * from HexEdit, a simple hex editor
  5.  * copyright 1993, Jim Bumgardner
  6.  *
  7.  * Revision History is in History.note
  8.  ************************************************************************************/
  9. #include "HexEdit.h"
  10. #include "AppleEvents.h"
  11. #include <Traps.h>
  12.  
  13.  
  14. void     MyInitMacintosh(void);
  15. void    MyInitMultifinder(void);
  16. void     InitAppleEvents(void);
  17. void    CheckEnvironment(void);
  18. void     MyHandleEvent(void);
  19. void     MyDoEvent(EventRecord *theEvent);
  20. void    IdleObjects(EventRecord *er);
  21.  
  22. Boolean    gWNEImplemented,gQuitFlag,gSys7Flag,gColorQDFlag;
  23. // Typical Macintosh Initialization Code
  24.  
  25.  
  26. // Main Entry Point
  27.  
  28. main()
  29. {
  30.     // Standard Mac Initialization
  31.     MyInitMacintosh();
  32.  
  33.     // Check if Multifinder (WaitNextEvent) is implemented
  34.     MyInitMultifinder();
  35.  
  36.     // Check if System 7
  37.     CheckEnvironment();
  38.  
  39.     // Init Apple Events
  40.     InitAppleEvents();
  41.  
  42.     // Set up the menu bar
  43.     MySetUpMenus();
  44.  
  45.     InitializeEditor();
  46.  
  47.     if (!gSys7Flag)
  48.         AskEditWindow();
  49.  
  50.     // main event loop
  51.     while (!gQuitFlag)            // Till the End of Time...
  52.         MyHandleEvent();        // Get an Event, do something about it
  53.  
  54.     CleanupEditor();
  55. }
  56.  
  57. // Standard Macintosh Initialization
  58.  
  59. void MyInitMacintosh(void)
  60. {
  61.     MaxApplZone();
  62.     
  63.     InitGraf(&thePort);
  64.     InitFonts();
  65.     FlushEvents(everyEvent, 0);
  66.     InitWindows();
  67.     InitMenus();
  68.     TEInit();
  69.     InitDialogs(0L);
  70.     InitCursor();
  71. }
  72.  
  73. // Check if WaitNextEvent (Multifinder) is implemented on this Macintosh
  74.  
  75. void MyInitMultifinder(void)
  76. {
  77.     gWNEImplemented = (NGetTrapAddress(_WaitNextEvent, ToolTrap) != 
  78.                        NGetTrapAddress(_Unimplemented,ToolTrap));
  79. }
  80.  
  81. // The Main Event Dispatcher - this routine should be called repeatedly
  82.  
  83. void MyHandleEvent(void)
  84.  
  85. {
  86.     EventRecord    theEvent;
  87.     Boolean        ok;
  88.  
  89.     // If the more modern WaitNextEvent is implemented, use it 
  90.  
  91.     if (gWNEImplemented)
  92.         // We don't have to call SystemTask because WaitNextEvent calls it for us
  93.         // Get the next event
  94.         ok = WaitNextEvent(everyEvent,&theEvent,0L,NULL);
  95.  
  96.     else {
  97.         // we are running in (Single) Finder under system 6 or less
  98.         // Give Desk Accessories some processing time
  99.         SystemTask ();
  100.  
  101.         // Get the next event
  102.         ok = GetNextEvent (everyEvent, &theEvent);
  103.     }
  104.  
  105.     if (IsDialogEvent(&theEvent)) {
  106.         DoModelessDialogEvent(&theEvent);
  107.     }
  108.     else if (ok) {
  109.             // Handle the Event
  110.             MyDoEvent(&theEvent);
  111.     }
  112.     else {
  113.         // Nothing happened, kick back...
  114.         IdleObjects(&theEvent);
  115.     }
  116. }
  117.  
  118.  
  119. void MyDoEvent(EventRecord *theEvent)
  120. {
  121.     short         windowCode;
  122.     WindowPtr    theWindow;
  123.  
  124.  
  125.     switch (theEvent->what) {
  126.     //
  127.     // Was the mouse button pressed?
  128.     case mouseDown:
  129.         // Find out where the mouse went down
  130.         windowCode = FindWindow (theEvent->where, &theWindow);
  131.  
  132.           switch (windowCode) {
  133.         case inSysWindow:     // Desk Accessory?
  134.             SystemClick (theEvent, theWindow);
  135.             break;
  136.             
  137.         case inMenuBar:        // Menu Bar?
  138.               MyAdjustMenus();
  139.             MyHandleMenu(MenuSelect(theEvent->where));
  140.             break;
  141.  
  142.         default:            // Cursor was inside our window
  143.             // If the window isn't in the front
  144.             if (theWindow != FrontWindow()) {
  145.                 // Make it so...
  146.                 SelectWindow(theWindow);
  147.                 MyAdjustMenus();
  148.             }
  149.             else {
  150.                 // Window is already in the front, handle the click
  151.                 switch (windowCode) {
  152.  
  153.                 case inContent:        // Content area?
  154.                     if (((WindowPeek) theWindow)->refCon == MyWindowID)
  155.                         ((ObjectWindowPtr) theWindow)->HandleClick(theWindow, theEvent->where, theEvent);
  156.                     break;
  157.  
  158.                 case inDrag:        // Dragbar?
  159.                     {
  160.                         Rect    dragRect;
  161.                         dragRect = screenBits.bounds;
  162.                         // Handle the dragging of the window
  163.                         DragWindow(theWindow, theEvent->where, &dragRect);
  164.                         if (!((ObjectWindowPtr) theWindow)->floating)
  165.                             SelectWindow(theWindow);
  166.                     }
  167.                     break;
  168.  
  169.                  case inGoAway:                        // close box?
  170.                       if (TrackGoAway(theWindow, theEvent->where)) {
  171.                         // If mouse is released inside the close box
  172.                         // Hide or close the window
  173.                           if (((WindowPeek) theWindow)->refCon == MyWindowID)
  174.                             CloseEditWindow(theWindow);
  175.                         else if (theWindow == gSearchWin) {
  176.                             DisposDialog(gSearchWin);
  177.                             gSearchWin = NULL;
  178.                         }
  179.                         MyAdjustMenus();
  180.                     }
  181.                       break;
  182.  
  183.                 case inGrow:                        // Grow box?
  184.                     {
  185.                         long    growResult;
  186.                         Rect    growRect;
  187.  
  188.                         SelectWindow(theWindow);
  189.  
  190.                         SetRect(&growRect,MaxWindowWidth+SBarSize-1,64,
  191.                                 MaxWindowWidth+SBarSize-1,gMaxHeight);
  192.  
  193.                         // Handle the mouse tracking for the resizing
  194.                         growResult = GrowWindow(theWindow,theEvent->where,&growRect);
  195.  
  196.                         // Change the size of the window
  197.                         SizeWindow(theWindow,LoWord(growResult),HiWord(growResult),true);
  198.  
  199.                         AdjustScrollBars(theWindow, true);
  200.                         DrawPage((EditWindowPtr) theWindow);
  201.  
  202.                         // Redraw the window
  203.                         SetPort(theWindow);
  204.                           InvalRect(&theWindow->portRect);
  205.                     }
  206.                     break;
  207.                 case inZoomIn:
  208.                 case inZoomOut:
  209.                     if (TrackBox(theWindow, theEvent->where, windowCode)) {
  210.                         SetPort(theWindow);
  211.                         EraseRect(&theWindow->portRect);
  212.                         ZoomWindow(theWindow, windowCode, true);
  213.                         AdjustScrollBars(theWindow, true);
  214.                         DrawPage((EditWindowPtr) theWindow);
  215.                         // Redraw the window
  216.                         SetPort(theWindow);
  217.                           InvalRect(&theWindow->portRect);
  218.                     }
  219.                 }
  220.             }
  221.             break;
  222.         }
  223.         break;
  224.         
  225.     // Was a key pressed?
  226.     case keyDown: 
  227.     case autoKey:
  228.         // Was the cmd-key being held down?  If so, process menu bar short cuts.
  229.         if ((theEvent->modifiers & cmdKey) != 0) {
  230.           MyAdjustMenus();
  231.           MyHandleMenu(MenuKey((char) (theEvent->message & charCodeMask)));
  232.         }
  233.         else {
  234.             theWindow = FrontWindow();
  235.             if (((WindowPeek) theWindow)->refCon == MyWindowID &&
  236.                 ((ObjectWindowPtr) theWindow)->ProcessKey != NULL)
  237.                 ((ObjectWindowPtr) theWindow)->ProcessKey(theWindow, theEvent);
  238.         }
  239.         break;
  240.  
  241.     // Does a window need to be redrawn?
  242.     case updateEvt:
  243.         theWindow = (WindowPtr) theEvent->message;
  244.         if (((WindowPeek) theWindow)->refCon == MyWindowID)
  245.             ((ObjectWindowPtr) theWindow)->Update(theWindow);
  246.         break;
  247.  
  248.     // Has a window been activated or deactivated?
  249.     case activateEvt:
  250.         theWindow = (WindowPtr) theEvent->message;
  251.  
  252.         // Force it to be redrawn
  253.         if (((WindowPeek) theWindow)->refCon == MyWindowID)
  254.             ((ObjectWindowPtr) theWindow)->Activate(theWindow,(theEvent->modifiers & activeFlag) > 0);
  255.  
  256.         break;
  257.     case osEvt:
  258.         // Force it to be redrawn
  259.         switch (theEvent->message >> 24) {
  260.         case suspendResumeMessage:
  261.             theWindow = FrontWindow();
  262.             if (theWindow && ((WindowPeek) theWindow)->refCon == MyWindowID)
  263.                 ((ObjectWindowPtr) theWindow)->Activate(theWindow,(theEvent->message & resumeFlag) > 0);
  264.             break;
  265.         }
  266.         break;        
  267.     case kHighLevelEvent:
  268.         if (gSys7Flag)
  269.             AEProcessAppleEvent(theEvent);
  270.         break;
  271.     }
  272. }        
  273.  
  274. // Do Idle Time Processing
  275.  
  276. void IdleObjects(EventRecord *er)
  277. {
  278.     WindowPeek    theWin;
  279.     theWin = (WindowPeek) FrontWindow();
  280.     while (theWin) {
  281.         if (theWin->refCon == MyWindowID &&
  282.             ((ObjectWindowPtr) theWin)->Idle)
  283.             ((ObjectWindowPtr) theWin)->Idle((WindowPtr) theWin, er);
  284.         theWin = theWin->nextWindow;
  285.     }
  286. }
  287.  
  288. Boolean GotRequiredParams(AppleEvent *theEvent)
  289. {
  290.    DescType returnedType;
  291.    Size     actualSize;
  292.    OSErr    err;
  293.    err = AEGetAttributePtr ( theEvent, keyMissedKeywordAttr, 
  294.                         typeWildCard, &returnedType, NULL, 0, 
  295.                         &actualSize);
  296.    
  297.    return err == errAEDescNotFound;
  298.    
  299.  }    /* CAppleEvent::GotRequiredParams */
  300.  
  301.  
  302. void DoOpenEvent(AppleEvent *theEvent)
  303. {
  304.     Handle        docList = NULL;
  305.     long        i, numDocs;
  306.     FSSpec        myFSS;
  307.     DescType    eventID;
  308.     AEDescList    theList;
  309.     AEKeyword    aeKeyword=keyDirectObject;
  310.     long        itemCount;
  311.     DescType    actualType;
  312.     Size        actualSize;
  313.     OSErr        oe;
  314.  
  315.     if ((oe = AEGetParamDesc( theEvent, keyDirectObject, typeAEList, &theList)) != noErr) {
  316.         DebugStr("\pAEGetParamDesc");
  317.         return;
  318.     }
  319.  
  320.  
  321.     if (!GotRequiredParams(theEvent)) {
  322.         DebugStr("\pGotRequiredParams");
  323.         return;
  324.     }
  325.  
  326.     if ((oe = AECountItems( &theList, &itemCount)) != noErr) {
  327.         DebugStr("\pAECountItems");
  328.         return;
  329.     }
  330.  
  331.  
  332.     for (i = 1; i <= itemCount; i++)
  333.     {
  334.         oe = AEGetNthPtr( &theList, i, typeFSS, &aeKeyword, &actualType,
  335.                         (Ptr) &myFSS, sizeof( FSSpec), &actualSize);
  336.  
  337.         if (oe == noErr) {
  338.             OpenEditWindow(&myFSS);
  339.         }
  340.     }
  341.     AEDisposeDesc(&theList);
  342.     // event was handled successfully
  343. }
  344.  
  345. pascal OSErr AppleEventHandler(AppleEvent *theEvent,AppleEvent *reply, long refCon)
  346. {    
  347.     OSErr        err;
  348.     DescType    actualType;
  349.     Size        actualSize;
  350.     DescType    eventClass, eventID;
  351.     OSErr        oe;
  352.  
  353.     if ((oe = AEGetAttributePtr( (AppleEvent*) theEvent, keyEventClassAttr,
  354.                     typeType, &actualType, (Ptr) &eventClass, 
  355.                     sizeof(eventClass), &actualSize)) != noErr)
  356.             return oe;
  357.                             
  358.     
  359.     if ((oe = AEGetAttributePtr(  (AppleEvent*) theEvent, keyEventIDAttr,
  360.                     typeType, &actualType, (Ptr) &eventID, 
  361.                     sizeof(eventID), &actualSize)) != noErr)
  362.             return oe;
  363.                                     
  364.     if (eventClass == kCoreEventClass)
  365.     {
  366.         switch (eventID)
  367.         {
  368.         case kAEOpenApplication:
  369.             if (GotRequiredParams(theEvent))
  370.             {
  371.                 // gGopher->DoCommand( cmdNew);
  372.                 // anAppleEvent->SetErrorResult( noErr);
  373.                 AskEditWindow();
  374.             }
  375.             break;
  376.                 
  377.         case kAEOpenDocuments:
  378.             DoOpenEvent( theEvent);
  379.             break;
  380.                 
  381.         case kAEPrintDocuments:
  382.             break;
  383.             
  384.         case kAEQuitApplication:
  385.             if (GotRequiredParams(theEvent))
  386.             {
  387.                 gQuitFlag = true;
  388.             }
  389.             break;
  390.         }        
  391.     }
  392.         
  393.     return noErr;
  394. }
  395.  
  396.  
  397.  
  398. void InitAppleEvents(void)
  399. {
  400.     if (gSys7Flag)
  401.         AEInstallEventHandler(typeWildCard, typeWildCard,
  402.                                 (EventHandlerProcPtr) AppleEventHandler,
  403.                                 0,FALSE);
  404. }
  405.  
  406. void CheckEnvironment(void)
  407. {
  408.     SysEnvRec    sEnv;
  409.     OSErr        oe;
  410.  
  411.     oe = SysEnvirons(1,&sEnv);
  412.  
  413.     gSys7Flag = sEnv.systemVersion >= 0x0700;
  414.     gColorQDFlag = sEnv.hasColorQD;
  415. }
  416.  
  417. /* end Evtlab.c */