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 / Event.c < prev    next >
Text File  |  1993-11-20  |  13KB  |  544 lines

  1.  
  2. #pragma segment EVENT
  3.  
  4. /*
  5. ** This source code was written by Tim Endres
  6. ** Email: time@ice.com.
  7. ** USMail: 8840 Main Street, Whitmore Lake, MI  48189
  8. **
  9. ** Some portions of this application utilize sources
  10. ** that are copyrighted by ICE Engineering, Inc., and
  11. ** ICE Engineering retains all rights to those sources.
  12. **
  13. ** Neither ICE Engineering, Inc., nor Tim Endres, 
  14. ** warrants this source code for any reason, and neither
  15. ** party assumes any responsbility for the use of these
  16. ** sources, libraries, or applications. The user of these
  17. ** sources and binaries assumes all responsbilities for
  18. ** any resulting consequences.
  19. */
  20.  
  21. #include "tickle.h"
  22. #include "tge.h"
  23. #include "tclMac.h"
  24.  
  25. #include <EPPC.h>
  26. #include <AppleEvents.h>
  27.  
  28.  
  29. static int        yield_ticks = 0;
  30.  
  31. pausing()
  32.     {
  33.     yield_ticks = 30;
  34.     DoYield();
  35.     yield_ticks = 0;
  36.     }
  37.  
  38. static int        UYieldDepth = 0;
  39.  
  40. UBegYield()
  41.     {
  42.     if (UYieldDepth++ == 0)
  43.         {
  44.         yielding_on = 1;
  45. #ifdef TCLAPPL
  46.         yield_menus(YIELD_ON);
  47. #endif
  48.         }
  49.     }
  50.     
  51. UEndYield()
  52.     {
  53.     if (--UYieldDepth == 0)
  54.         {
  55.         yielding_on = 0;
  56. #ifdef TCLAPPL
  57.         yield_menus(YIELD_OFF);
  58. #endif
  59.         }
  60.     }
  61.  
  62. extern int _ctb_is_available_;
  63.  
  64. DoYield()
  65.     {
  66.     int                got_event;
  67.     short            emask;
  68.     WindowPtr        whichwindow, frontwindow;
  69.     
  70.     frontwindow = FrontWindow();
  71.  
  72.     if (_ctb_is_available_)
  73.         Tcl_CTBIdleChecks();
  74.     
  75.     /* emask = osMask | activMask | updateMask | mDownMask | keyDownMask; */
  76.     emask = ( everyEvent & ~(autoKeyMask) );
  77.     
  78.     if (has_wait_next_event)
  79.         {
  80.         got_event =  WaitNextEvent(emask, &gEvent, 0, (RgnHandle)0);
  81.         }
  82.     else
  83.         {
  84.         SystemTask();
  85.         got_event = GetNextEvent(emask, &gEvent);
  86.         }
  87.     
  88.     /*
  89.     ** Has the user done something?
  90.     */
  91.     if (got_event)
  92.         {
  93.         switch (gEvent.what)
  94.             {
  95.             case mouseDown:
  96.                 switch (FindWindow(gEvent.where, &whichwindow))
  97.                     {
  98.                     case inMenuBar:
  99. #ifdef TCLAPPL
  100.                         InitCursor();
  101.                         do_command(MenuSelect(gEvent.where));
  102.                         WatchCursorOn();
  103. #endif
  104.                         break;
  105.                     case inDrag:
  106.                         DragWindow(whichwindow, gEvent.where, &qd.screenBits.bounds);
  107.                         break;
  108.                     case inGoAway:
  109.                         if (frontwindow != whichwindow) 
  110.                             SelectWindow(whichwindow);
  111.                         break;
  112.                     case inGrow:
  113.                         if (frontwindow == whichwindow) 
  114.                             wind_parse(whichwindow, &gEvent, wGrow);
  115.                         else
  116.                             SelectWindow(whichwindow);
  117.                         break;
  118.                     case inSysWindow:
  119.                         SystemClick(&gEvent, whichwindow);
  120.                         break;
  121.                     case inContent:
  122.                         if (frontwindow != whichwindow) 
  123.                             SelectWindow(whichwindow);
  124.                         break;
  125.                     }
  126.                 gLastDown = gEvent.when;
  127.                 break;
  128.             case keyDown:
  129. #ifdef TCLAPPL
  130.                 if ((gEvent.modifiers & 0x0100) != 0)    /* APPLE key was down also. */
  131.                     do_command(MenuKey((char)gEvent.message));
  132. #endif
  133.                 break;
  134.            case activateEvt:
  135.                 wind_parse((WindowPtr) gEvent.message, &gEvent, wActivate);
  136.                 break;
  137.            case updateEvt:
  138.                 wind_parse((WindowPtr) gEvent.message, &gEvent, wUpdate);
  139.                 break;
  140.             case MFOSEvent:
  141.                 switch ((gEvent.message >> 24) & 0x00FF) /* high byte of message */
  142.                     {
  143.                     case MFSuspendResumeMessage:
  144.                         /* suspend/resume is also an activate/deactivate! */
  145.                         in_back_ground =
  146.                                 ( (gEvent.message & MFResumeMask) == 0 );
  147.                         if (! in_back_ground)
  148.                             {
  149.                             /* RESUME */
  150.                             UResumeWindows();
  151. #ifdef TCLAPPL
  152.                             TEFromScrap();
  153. #endif
  154.                             WatchCursorOn();
  155.                             }
  156.                         else
  157.                             {
  158.                             /* SUSPEND */
  159.                             USuspendWindows();
  160.                             UInitCursor();
  161.                             }
  162.                         break;
  163.                     }
  164.                 break;
  165.             default:
  166.                 break;
  167.             }
  168.         
  169.         gLastEvent = gEvent;
  170.         }
  171.  
  172.     }
  173.  
  174. static short    unset_script_flag = 0;
  175.  
  176. PreMenuProcessing(modifiers)
  177.     int        modifiers;
  178.     {
  179.     menu_modifiers = modifiers;
  180.     if ((menu_modifiers & shiftKey) != 0)
  181.         {
  182.         SetItem(file_menu_hdl, new_item, "\pNew Global...");
  183.         SetItem(file_menu_hdl, open_item, "\pOpen Global...");
  184.         unset_script_flag = 1;
  185.         }
  186.     }
  187.  
  188. PostMenuProcessing()
  189.     {
  190.     if (unset_script_flag)
  191.         {
  192.         SetItem(file_menu_hdl, new_item, "\pNew...");
  193.         SetItem(file_menu_hdl, open_item, "\pOpen...");
  194.         unset_script_flag = 0;
  195.         }
  196.     }
  197.  
  198. /*
  199. ** This is the standard Macintosh Event Loop. Every Macintosh application
  200. ** spins on this loop, waiting for the user to do something. This drives
  201. ** the rest of the code by responding to the user's events (mouse clicks,
  202. ** menu picks, keyboard inputs, etc.
  203. */
  204. do_event()
  205.     {
  206.     int            got_event;
  207. #ifdef TCLAPPL
  208.     WindowPtr     whichwindow, frontwindow;
  209.     
  210.     /* if (gHTE != NULL) TEIdle(gHTE); */
  211.  
  212.     frontwindow = FrontWindow();
  213.  
  214.     if (frontwindow != NULL)
  215.         if (((WindowPeek) frontwindow)->windowKind == tgeWKind)
  216.             tge_idle(frontwindow);
  217.  
  218.     CheckCursor();
  219. #endif
  220.  
  221.     if (_ctb_is_available_)
  222.         Tcl_CTBIdleChecks();
  223.     
  224.     /*
  225.     ** Has the user done something?
  226.     */
  227.     if (has_wait_next_event)
  228.         {
  229.         got_event =  WaitNextEvent(everyEvent, &gEvent, 13, (RgnHandle)0);
  230.         }
  231.     else
  232.         {
  233.         SystemTask();
  234.         got_event = GetNextEvent(everyEvent, &gEvent);
  235.         }
  236.  
  237.     if (got_event) {
  238.         switch (gEvent.what) {
  239.             /*
  240.             ** The mouse button was pressed.
  241.             */
  242.             case mouseDown: 
  243. #ifdef TCLAPPL
  244.                 switch (FindWindow(gEvent.where, &whichwindow)) {
  245.                     /*
  246.                     ** The mouse button was pressed in the menu bar.
  247.                     */
  248.                     case inMenuBar:
  249.                         InitCursor();
  250.                         PreMenuProcessing(gEvent.modifiers);
  251.                         do_command(MenuSelect(gEvent.where));
  252.                         PostMenuProcessing();
  253.                         break;
  254.                     /*
  255.                     ** The mouse button was pressed in a window title bar.
  256.                     */
  257.                     case inDrag:
  258.                         dragrect = qd.screenBits.bounds;
  259.                         dragrect.top += 20;
  260.                         InsetRect(&dragrect, 4, 4);
  261.                         DragWindow(whichwindow, gEvent.where, &dragrect);
  262.                         break;
  263.                     /*
  264.                     ** The mouse button was pressed in a window "go away" box.
  265.                     */
  266.                     case inGoAway:
  267.                         if (TrackGoAway(whichwindow, gEvent.where))
  268.                             wind_parse(whichwindow, &gEvent, wClose);
  269.                         break;
  270.                     /*
  271.                     ** The mouse button was pressed in the grow box.
  272.                     */
  273.                     case inGrow:
  274.                         if (frontwindow == whichwindow) 
  275.                             wind_parse(whichwindow, &gEvent, wGrow);
  276.                         else
  277.                             SelectWindow(whichwindow);
  278.                         break;
  279.                     case inZoomIn:
  280.                         if (TrackBox(whichwindow, gEvent.where, inZoomIn)) 
  281.                             if (frontwindow == whichwindow) 
  282.                                 wind_parse(whichwindow, &gEvent, wZoomIn);
  283.                             else
  284.                                 SelectWindow(whichwindow);
  285.                         break;
  286.                     case inZoomOut:
  287.                         if (TrackBox(whichwindow, gEvent.where, inZoomOut)) 
  288.                             if (frontwindow == whichwindow) 
  289.                                 wind_parse(whichwindow, &gEvent, wZoomOut);
  290.                             else
  291.                                 SelectWindow(whichwindow);
  292.                         break;
  293.                     /*
  294.                     ** The mouse button was pressed in a Desk Accessory window.
  295.                     */
  296.                     case inSysWindow:
  297.                         SystemClick(&gEvent, whichwindow);
  298.                         break;
  299.                     /*
  300.                     ** The mouse button was pressed in the window contents area.
  301.                     */
  302.                     case inContent:
  303.                         if (frontwindow == whichwindow) 
  304.                             wind_parse(whichwindow, &gEvent, wContent);
  305.                             else SelectWindow(whichwindow);
  306.                         break;
  307.                     }
  308.                 gLastDown = gEvent.when;
  309. #endif
  310.                 break;
  311.             /*
  312.             ** A key was pressed on the keyboard.
  313.             */
  314.             case keyDown:
  315.             case autoKey:
  316. #ifdef TCLAPPL
  317.                 /*
  318.                 ** Check to see if it is a menu equivalent key (apple-key).
  319.                 */
  320.                 if ((gEvent.modifiers & 0x0100) != 0
  321.                     && (gEvent.message & 255) != 0x0D)
  322.                     {    /* APPLE key was down also (and not Return). */
  323.                     menu_modifiers = gEvent.modifiers;
  324.                     do_command(MenuKey((char)gEvent.message));
  325.                     }
  326.                 else {
  327.                     if (macplus_keybd) {
  328. #ifdef NEVER_DEFINED
  329. According to Tech Note 168(?):
  330.     Key                     ADB         NON-ADB
  331.     ----                    ---         -------
  332.     left arrow              $7B         $46
  333.     right arrow             $7C         $42
  334.     down arrow              $7D         $48
  335.     up arrow                $7E         $4D
  336.     keypad plus sign (+)    $45         $46 (with Shift bit set in modifiers)
  337.     keypad asterisk (*)     $43         $42 (with Shift bit set in modifiers)
  338.     keypad equal sign (=)   $51         $48 (with Shift bit set in modifiers)
  339.     keypad slash (/)        $4B         $4D (with Shift bit set in modifiers)
  340. #endif
  341.                         switch ((gEvent.message & keyCodeMask) >> 8) {
  342.                             case 0x46:
  343.                                 if ((gEvent.modifiers & shiftKey) == 0)
  344.                                     gEvent.message = (gEvent.message & ~keyCodeMask) | 0x00007B00;
  345.                                 break;
  346.                             case 0x42:
  347.                                 if ((gEvent.modifiers & shiftKey) == 0)
  348.                                     gEvent.message = (gEvent.message & ~keyCodeMask) | 0x00007C00;
  349.                                 break;
  350.                             case 0x48:
  351.                                 if ((gEvent.modifiers & shiftKey) == 0)
  352.                                     gEvent.message = (gEvent.message & ~keyCodeMask) | 0x00007D00;
  353.                                 break;
  354.                             case 0x4D:
  355.                                 if ((gEvent.modifiers & shiftKey) == 0)
  356.                                     gEvent.message = (gEvent.message & ~keyCodeMask) | 0x00007E00;
  357.                                 break;
  358.                             }
  359.                         }
  360.                     
  361.                     wind_parse(frontwindow, &gEvent, wKeyDown);
  362.                     }
  363.                 /* gLastKey = gEvent.when; */
  364. #endif
  365.                 break;
  366.             case activateEvt:
  367.                 wind_parse((WindowPtr) gEvent.message, &gEvent, wActivate);
  368.                 break;
  369.             case updateEvt:
  370.                 wind_parse((WindowPtr) gEvent.message, &gEvent, wUpdate);
  371.                 break;
  372.             case MFOSEvent:
  373.                 switch ((gEvent.message >> 24) & 0x00FF) {    /* high byte of message */
  374.                     case MFSuspendResumeMessage:    /* suspend/resume is also an activate/deactivate */
  375.                         in_back_ground = (gEvent.message & MFResumeMask) == 0;
  376.                         if (! in_back_ground) {
  377.                             /* RESUME */
  378.                             UResumeWindows();
  379. #ifdef TCLAPPL
  380.                             TEFromScrap();
  381. #endif
  382.                             UInitCursor();
  383.                             }
  384.                         else {
  385.                             /* SUSPEND */
  386.                             USuspendWindows();
  387.                             UInitCursor();
  388.                             }
  389.                         break;
  390.                     }
  391.                 break;
  392.             case kHighLevelEvent:
  393.                 DoHighLevelEvent(&gEvent);
  394.                 break;
  395.             }
  396.             
  397.         gLastEvent = gEvent;
  398.         }
  399.     
  400.     if (g_cron_interval > 0)
  401.         {
  402.         if (g_interp != (Tcl_Interp *)0 && TickCount() > g_next_cron_time)
  403.             {
  404.             PFI        saveproc;
  405.             int        result, tcl_dev_null_output();
  406.             char    *command =
  407. "if [info exists env(CRON_EXPR)] then { catch {eval \"$env(CRON_EXPR)\"} };\n" ;
  408.     
  409.             saveproc = Tcl_SetPrintProcedure(tcl_dev_null_output);
  410.     
  411.             result = Tcl_Eval(g_interp, command, 0, (char **)0);
  412.     
  413.             Tcl_SetPrintProcedure(saveproc);
  414.  
  415.             while (g_next_cron_time < TickCount())    /* CATCH UP!! */
  416.                 g_next_cron_time += g_cron_interval;
  417.             
  418.             UInitCursor();
  419.             }
  420.         }
  421.     }
  422.  
  423. wind_parse(myWindow, myEvent, wAction)
  424. WindowPtr        myWindow;
  425. EventRecord        *myEvent;
  426. int                wAction;
  427. {
  428. PFI        handler;
  429.     if (checkmywindow(myWindow))    {
  430.         handler = (PFI) GetWRefCon(myWindow);
  431.         /* Call the window's routine stored in refcon.    */
  432.         if (handler != (PFI) 0)
  433.             (*handler) (myWindow, myEvent, wAction);
  434.         }
  435.     }
  436.     
  437. checkmywindow(myWindow)
  438. WindowPeek        myWindow;
  439. {
  440.     /* This function is used to check that the window asked to perform
  441.     ** the event was created by this application.
  442.     */
  443.     return        (myWindow->windowKind == tgeWKind)
  444.             ||  (myWindow->windowKind == feedbackWKind)
  445.             ||  (myWindow->windowKind == progressWKind);
  446.     }
  447.  
  448. CheckCursor()
  449. {
  450. GrafPtr        saveport;
  451. WindowPtr    myWindow, fWindow;
  452. Point        cursorpt;
  453. Rect        myrect;
  454. int            want_ibeam = 0, in_front = 0;
  455.  
  456.     if (in_back_ground)
  457.         return;
  458.     
  459.     GetPort(&saveport);
  460.     fWindow = FrontWindow();    /* Be sure to check others' windows! */
  461.     if (! in_front && fWindow != NULL) {
  462.         if (! checkmywindow(fWindow)) {
  463.             myrect = fWindow->portRect;
  464.             SetPort(fWindow);
  465.             GetMouse(&cursorpt);
  466.             if (PtInRect(cursorpt, &myrect))
  467.                 in_front = 1;
  468.             }
  469.         }
  470.     SetPort(saveport);
  471.  
  472.     myWindow = FrontWindow();
  473.     if (! in_front && myWindow != NULL) {
  474.         /*
  475.         ** If the cursor is inside the front window, select on window type
  476.         ** and set cursor accordingly, otherwise, UInitCursor().
  477.         */
  478.         
  479.         /* These cover many cases... */
  480.         myrect = myWindow->portRect;
  481.         myrect.right -= 15;
  482.         myrect.bottom -= 15;
  483.         
  484.         GetPort(&saveport);
  485.         SetPort(myWindow);
  486.         
  487.         GetMouse(&cursorpt);
  488.         
  489.         switch (WPeek->windowKind) {
  490.             case tgeWKind:
  491.                 if (PtInRect(cursorpt, &myrect))
  492.                     want_ibeam = 1;
  493.                 break;
  494.             default:
  495.                 break;
  496.             }
  497.         
  498.         SetPort(saveport);
  499.         }
  500.  
  501.     if (! in_front && want_ibeam) {
  502.         if (! cursor_ibeam) {
  503.             cursor_ibeam = 1;
  504.             SetCursor(*GetCursor(iBeamCursor));
  505.             }
  506.         }
  507.     else {
  508.         if (cursor_ibeam) {
  509.             cursor_ibeam = 0;
  510.             UInitCursor();
  511.             }
  512.         }
  513.         
  514.     }
  515.  
  516. USuspendWindows()
  517. {
  518. WindowPtr    fWindow;
  519. EventRecord    myevent;
  520.  
  521.     fWindow = FrontWindow();
  522.     if (fWindow != NULL) {
  523.         HiliteWindow(fWindow, FALSE);
  524.         myevent.modifiers = 0x00000000;    /* Deactivate */
  525.         myevent.message = (unsigned long)fWindow;
  526.         wind_parse(myevent.message, &myevent, wActivate);
  527.         }
  528.     }
  529.  
  530. UResumeWindows()
  531. {
  532. WindowPtr    fWindow;
  533. EventRecord    myevent;
  534.  
  535.     fWindow = FrontWindow();
  536.     if (fWindow != NULL) {
  537.         HiliteWindow(fWindow, TRUE);
  538.         myevent.modifiers = 0x00000001;    /* Activate */
  539.         myevent.message = (unsigned long)fWindow;
  540.         wind_parse(myevent.message, &myevent, wActivate);
  541.         }
  542.     }
  543.  
  544.