home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Containers / SimpleApp / Main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-20  |  7.9 KB  |  435 lines  |  [TEXT/CWIE]

  1. /***************************************
  2.  
  3. Main.c
  4.     Driver Module for Application
  5.     
  6. *****************************************/
  7.  
  8. #include "headers.h"
  9. #include <limits.h>
  10. #include <Threads.h>
  11. #include "Protos.h"
  12. #include "menus.h"
  13. #include "CActiveXScheduler.h"
  14. #include "CSimpleAppSite.h"
  15. #include "App.h"
  16.  
  17.  
  18. /************************* Globals ***********************/
  19.  
  20. static Boolean gControlHasFocus = false;
  21.  
  22. Boolean    gInBackGround = FALSE;        /* Am I in the background? */
  23. Boolean gSendMouseUp = FALSE;        /* Send the next mouse up event to the control? */
  24. Boolean gMouseInControl = false;    // Is the mouse currently inside the control?
  25.  
  26.  
  27. #define OPTION_KEY(Event)    ( (Event->modifiers & optionKey) != 0 )
  28.  
  29. /*******************  function prototypes *******************/
  30.  
  31. static void AdjustCursor(EventRecord *theEvent);
  32.  
  33. static void DoIdle(EventRecord* theEvent);
  34. static void DoKeyDown(EventRecord *theEvent);
  35. static void DoEvent(EventRecord *theEvent);
  36. static void DoActivateEvent(EventRecord *theEvent);
  37. static void DoHighLevelEvent(EventRecord *theEvent);
  38. static void DoMenu(long menuResult);
  39. static void DoMouseDown(EventRecord *theEvent);
  40. static void DoUpdate(WindowPtr window);
  41. static void DoCloseWindow(WindowPtr window, EventRecord *theEvent);
  42. static void DoContent(WindowPtr window, EventRecord *theEvent);
  43. static void DrawWindow(WindowPtr window);
  44.  
  45. static void EventLoop(void);
  46.  
  47. static long GetSleep(void);
  48.  
  49.  
  50.  
  51.  
  52. void main(void)
  53. {
  54.     Initialize();
  55.     EventLoop();
  56. }
  57.  
  58. void EventLoop()
  59. /* 
  60.     Continuously get events and dispatch to the appropriate handler
  61. */
  62. {
  63.     RgnHandle    cursorRgn;
  64.     Boolean        ignoreResult;
  65.     EventRecord    theEvent;
  66.  
  67.     cursorRgn = NewRgn();
  68.     while (TRUE) 
  69.     {
  70.         ignoreResult = WaitNextEvent(everyEvent,&theEvent, 0,cursorRgn);
  71.         
  72.         AdjustCursor(&theEvent);
  73.         DoEvent(&theEvent);
  74.     } 
  75. }  
  76.  
  77. void DoEvent(EventRecord *theEvent)
  78. /*
  79.     Dispatch the event to the appropriate handler
  80. */
  81. {
  82.     switch (theEvent->what) 
  83.     {
  84.         case nullEvent:
  85.             DoIdle(theEvent);
  86.             break;
  87.  
  88.         case mouseDown: 
  89.             DoMouseDown(theEvent);
  90.             break;
  91.             
  92.         case mouseUp:
  93.             if ( gControl && gSendMouseUp )
  94.             {
  95.                 gControl->DoMouse(MouseUp, theEvent);
  96.                 gSendMouseUp = FALSE;
  97.             }
  98.             break;
  99.             
  100.         case keyDown:
  101.         case autoKey:
  102.             DoKeyDown(theEvent);
  103.             break;
  104.             
  105.         case activateEvt:
  106.             DoActivateEvent(theEvent);
  107.             break;   
  108.                
  109.         case updateEvt:              
  110.             DoUpdate((WindowPtr) theEvent->message);
  111.             break;
  112.  
  113.         case kHighLevelEvent:
  114.             DoHighLevelEvent(theEvent);
  115.             break;
  116.             
  117.     } 
  118.  
  119.     CActiveXScheduler*    Scheduler = CActiveXScheduler::GetActiveXScheduler();
  120.     if (Scheduler)
  121.         Scheduler->Idle(theEvent->what == nullEvent);
  122. }  
  123.  
  124. void DoIdle(EventRecord* theEvent)
  125. {
  126.     Point testPt;
  127.  
  128.     YieldToAnyThread();
  129.  
  130.     if ( gControl )
  131.     {
  132.         // if the mouse is in a control rectangle, 
  133.         testPt = theEvent->where;
  134.         GlobalToLocal(&testPt);
  135.  
  136.         // if the click is in the control, send it the mouse down event
  137.         if ( PtInRect(testPt, &gControlRect) )
  138.         {
  139.             if ( !gMouseInControl )
  140.             {
  141.                 gControl->DoMouse(MouseEnter, theEvent);
  142.                 gMouseInControl = true;
  143.             }
  144.         }
  145.         else if ( gMouseInControl )
  146.         {
  147.             gControl->DoMouse(MouseLeave, theEvent);
  148.             gMouseInControl = false;
  149.             InitCursor();
  150.         }
  151.     }
  152. }
  153.  
  154. void DoActivateEvent(EventRecord *theEvent)
  155. {
  156.     DrawContext Context = {(PortType)0};
  157.     WindowPtr    window;
  158.     
  159.     window = (WindowPtr) theEvent->message;
  160.     if ((theEvent->modifiers & activeFlag) != 0)
  161.     {
  162.         DoActivate(window);
  163.  
  164.         if ( gControl )
  165.         {
  166.             gControl->DoActivate(WindowActivate, 1, theEvent);
  167.         }
  168.     }
  169.     else
  170.     {
  171.         if ( gControl )
  172.         {
  173.             gControl->DoActivate(WindowDeactivate, 1, theEvent);
  174.         }
  175.  
  176.         DoDeactivate(window);
  177.     }
  178. }
  179.  
  180. void DoHighLevelEvent(EventRecord *theEvent)
  181. {
  182.     if ( gControl )
  183.         gControl->DoSystemEvent(theEvent);
  184. }
  185.  
  186.  
  187. void AdjustCursor(EventRecord *theEvent)
  188. {
  189.     Point testPt;
  190.     
  191.     testPt = theEvent->where;
  192.     GlobalToLocal(&testPt);
  193.  
  194. void DoUpdate(WindowPtr window)
  195. /*
  196.     handle an update event detected by GetNextEvent
  197. */
  198. {
  199.     BeginUpdate(window);                    
  200.     DrawWindow(window);
  201.     EndUpdate(window);                        
  202.  
  203. void DoMouseDown(EventRecord *theEvent)
  204. /*
  205.     handle a mouseDown event
  206. */
  207. {
  208.     short        part;
  209.     WindowPtr    window = NULL;
  210.  
  211.     part = FindWindow(theEvent->where, &window);
  212.     
  213.     switch (part) 
  214.     {
  215.         case inMenuBar: 
  216.             AdjustMenus();
  217.             DoMenu(MenuSelect(theEvent->where) );
  218.             break;
  219.  
  220.         case inSysWindow: 
  221.             SystemClick(theEvent,window);
  222.             break;
  223.  
  224.         case inContent:
  225.             DoContent(window,theEvent);
  226.             break;
  227.  
  228.         case inDrag: 
  229.             DragWindow(window,theEvent->where,&qd.screenBits.bounds);
  230.             SetPort(window);
  231.             break;
  232.  
  233.         case inGoAway: 
  234.             if (TrackGoAway(window,theEvent->where))
  235.                 DoCloseWindow(window, theEvent);
  236.             break;
  237.  
  238.         case inGrow:
  239.             //GrowWindow(theEvent->where);
  240.             break;
  241.  
  242.         case inZoomIn:
  243.         case inZoomOut:
  244.             ZoomWindow(window, part, TRUE);
  245.             break;
  246.     }
  247.  
  248. long GetSleep()
  249. {
  250.     long        sleep;
  251.     
  252.     sleep = LONG_MAX;
  253.     return sleep;
  254.  
  255. void DoMenu(long menuResult)
  256. /*
  257.     Handle a user menu selection
  258. */
  259. {
  260.     short    menuID;
  261.     short    menuItem;
  262.     
  263.     menuID = HiWord(menuResult);    
  264.     menuItem = LoWord(menuResult);    
  265.  
  266.     switch (menuID) 
  267.     {
  268.         case mApple: 
  269.             DoAppleMenu(menuID,menuItem);
  270.             break;
  271.  
  272.         case mFile:  
  273.             DoFileMenu(menuItem);
  274.             break;
  275.  
  276.         case mEdit:  
  277.             DoEditMenu(menuItem);
  278.             break;
  279.  
  280.         case mColor:
  281.             DoColorMenu(menuItem);
  282.             break;
  283.  
  284.         }
  285.     HiliteMenu(0);                    
  286. }  
  287.  
  288. void DrawWindow(WindowPtr window)
  289. /*
  290.     draw the window for which an update event was detected
  291. */
  292. {
  293.     GrafPtr        SavePort;
  294.     DrawContext Context = {(PortType)0};
  295.     
  296.     GetPort(&SavePort);
  297.     SetPort(window);
  298.  
  299.     ::EraseRect(&window->portRect);
  300.  
  301.     if ( gControl )
  302.     {
  303.         // Draw a border around the control
  304.         Point    InPenSize = window->pnSize;
  305.         Rect    BorderRect = gControlRect;
  306.  
  307.         ::InsetRect(&BorderRect, -8, -8);
  308.         ::PenSize(7, 7);
  309.         ::FrameRect(&BorderRect);
  310.         ::PenSize(InPenSize.h, InPenSize.v);
  311.  
  312.         // Draw the control
  313.         if (gSite->AcquireContext(WindowContextID, &Context) == S_OK)
  314.         {
  315.             gControl->Draw(&Context);
  316.             gSite->ReleaseContext(&Context);
  317.         }
  318. #ifdef USE_OFFSCREEN
  319.         if (gSite->AcquireContext(OffscreenContextID, &Context) == S_OK)
  320.         {
  321.             gControl->Draw(&Context);
  322.             gSite->ReleaseContext(&Context);
  323.             {
  324.                 Rect        DestRect = { 0, 300, 290, 496};
  325.  
  326.                 //    blit to the screen
  327.                 ::ForeColor(blackColor);
  328.                 ::BackColor(whiteColor);
  329.                 CopyBits((BitMap*)(*(gAppData.Offscreen->portPixMap)),
  330.                         &((GrafPtr)gAppData.Window)->portBits, &gControlRect, &DestRect, srcCopy, NULL);
  331.             }
  332.         }
  333. #endif // USE_OFFSCREEN
  334.     }
  335.     else
  336.     {
  337.         short OldTextSize = window->txSize;
  338.  
  339.         TextSize(24);
  340.         MoveTo(50, 50);
  341.         DrawString("\pDrop a control into the window.");
  342.         TextSize(OldTextSize);
  343.     }
  344.  
  345.     SetPort(SavePort);
  346.  
  347. void DoContent(WindowPtr window, EventRecord *theEvent)
  348. /*
  349.     Handle a user mouse click in a particular window
  350. */
  351. {
  352.     Point    testPt;
  353.     
  354.     if ( IsAppWindow(window) && gControl ) 
  355.     {    
  356.         SetPort(window);
  357.         testPt = theEvent->where;
  358.         GlobalToLocal(&testPt);
  359.     
  360.         // if the click is in the control, send it the mouse down event
  361.         if ( PtInRect(testPt, &gControlRect) )
  362.         {
  363.             gControl->SetFocus(TakeNextCommand, FullFocusSet);
  364.             gControl->DoMouse(MouseDown, theEvent);
  365.             gSendMouseUp = true;
  366.             gControlHasFocus = true;
  367.         }
  368.         else
  369.         {
  370.             if (gControlHasFocus)
  371.             {
  372.                 gControl->SetFocus(ReleaseCommand, FullFocusSet);
  373.                 gControlHasFocus = false;
  374.             }
  375.         }
  376.     }
  377.     
  378.  
  379. void DoKeyDown(EventRecord *theEvent)
  380. /*
  381.     respond to user keypresses
  382. */
  383. {
  384.     char            key;
  385.     WindowPtr        frontMost;
  386.     long            menuResult;
  387.     KeyEventType    theKeyET;
  388.     
  389.     frontMost = FrontWindow();
  390.     if (!IsDAWindow(frontMost)) 
  391.     {
  392.         key = theEvent->message & charCodeMask;
  393.         menuResult = 0;
  394.         
  395.         if ((theEvent->modifiers & cmdKey) != 0) 
  396.         {
  397.             unsigned char menukey = key;
  398.             
  399.             AdjustMenus();
  400.             
  401.             menuResult = MenuKey(menukey);
  402.             if (menuResult != 0)
  403.                 DoMenu(menuResult);
  404.         }
  405.  
  406.         if ( menuResult == 0 && gControlHasFocus )
  407.         {
  408.             if (theEvent->what == autoKey)
  409.                 theKeyET = AutoKey;
  410.             else
  411.                 theKeyET = KeyDown;
  412.  
  413.             if ( gControl )
  414.                 gControl->DoKey(theKeyET, key, theEvent);
  415.         }
  416.         
  417.     } 
  418.  
  419. void DoCloseWindow(WindowPtr window, EventRecord *theEvent)
  420. {
  421. #pragma unused (theEvent)
  422.     if ( gControl )
  423.         RemoveControl();
  424.     DisposeWindow(window);
  425. }    
  426.  
  427.  
  428.