home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Directory source / event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-27  |  7.4 KB  |  447 lines  |  [TEXT/KAHL]

  1. /*    EVENT.C
  2.  *
  3.  *    Handling events that come up
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "res.h"
  8. #include "struct.h"
  9.  
  10.  
  11. extern MenuHandle applMenu;
  12. extern MenuHandle fileMenu;
  13. extern MenuHandle editMenu;
  14. extern unsigned char ColorQD;
  15. extern unsigned char MultiWidget;
  16. static unsigned char QuitFlag;
  17.  
  18.  
  19. /************************************************************************************************/
  20. /*                                                                                                */
  21. /*    EVENTS            How to handle the interface to the event stuff                                */
  22. /*                                                                                                */
  23. /************************************************************************************************/
  24.  
  25.  
  26. /*    GetEvent
  27.  *
  28.  *    Based upon the flag settings in ModeFlags, this calls either GetNextEvent or WaitNextEvent
  29.  *  for the next event in the event queue
  30.  */
  31.  
  32. int GetEvent(event)
  33. EventRecord *event;
  34. {
  35.     if (MultiWidget) {
  36.         return WaitNextEvent(everyEvent,event,30L,NULL);        /* Allow timeout for 1/2 second */
  37.     } else {
  38.         SystemTask();
  39.         return GetNextEvent(everyEvent,event);
  40.     }
  41. }
  42.  
  43.  
  44. /*    DoEvent
  45.  *
  46.  *    This fetches the next event from the event queue, dispatching the event as needed
  47.  */
  48.  
  49. int DoEvent()
  50. {
  51.     EventRecord event;
  52.     char c;
  53.     short wlow,whigh;
  54.     Point p;
  55.     char buffer[64];
  56.     long l;
  57.     short s;
  58.  
  59.     QuitFlag = 0;
  60.     GetEvent(&event);
  61.     switch (event.what) {
  62.         case nullEvent:
  63.             SearchDisks();
  64.             break;
  65.         case mouseDown:
  66.             DoMouse(&event);
  67.             break;
  68.         case keyDown:
  69.         case autoKey:
  70.             c = event.message & charCodeMask;
  71.             if (event.modifiers & cmdKey) {
  72.                 if (event.what == keyDown) {
  73.                     DoMenu(MenuKey(c));
  74.                 }
  75.             } else DoKey(c,FrontWindow());
  76.             break;
  77.         case updateEvt:
  78.             DoUpdate(event.message);
  79.             break;
  80.         case activateEvt:
  81.             DoActivate(event.message,(event.modifiers & activeFlag) ? 1 : 0);
  82.             break;
  83.         case diskEvt:
  84.             wlow = (event.message) & 0x0FFFF;
  85.             whigh = (event.message) >> 16;
  86.             if (whigh != 0) {
  87.                 DILoad();                        /* Prepare to init disk */
  88.                 p.h = p.v = 75;
  89.                 whigh = DIBadMount(&p,event.message);
  90.                 DIUnload();
  91.                 if (whigh != 0) break;
  92.             }
  93.             GetVInfo(wlow,buffer,&s,&l);
  94.             NewPlan(s);
  95.             break;
  96.     }
  97.  
  98.     return QuitFlag;
  99. }
  100.  
  101.  
  102.  
  103. /*    DoMouse
  104.  *
  105.  *    What to do when the mouse goes down somewhere in the application
  106.  */
  107.  
  108. DoMouse(event)
  109. EventRecord *event;
  110. {
  111.     int i;
  112.     WindowPtr w;
  113.     GrafPtr p;
  114.     Rect r;
  115.     long l;
  116.  
  117.     i = FindWindow(event->where,&w);
  118.     if (w != NULL) SetPort(w);
  119.  
  120.     switch (i) {
  121.         case inDesk:
  122.             break;
  123.         case inMenuBar:
  124.             DoMenu(MenuSelect(event->where));
  125.             break;
  126.         case inSysWindow:
  127.             SystemClick(event,w);
  128.             break;
  129.         case inGrow:
  130.             r.top = 100;
  131.             r.left = 150;
  132.             r.bottom = 32767;
  133.             r.right = 32767;            /* Absolutely rediculous size */
  134.             l = GrowWindow(w,event->where,&r);
  135.             if (l != 0) {
  136.                 DoResizeInit(w);
  137.                 EraseRect(&(w->portRect));
  138.                 SizeWindow(w,(short)(l & 0x0FFFF),(short)(l >> 16),false);
  139.                 DoResizeWindow(w);
  140.                 InvalRect(&(w->portRect));
  141.             }
  142.             break;
  143.         case inContent:
  144.             if (w != FrontWindow()) SelectWindow(w);
  145.             else DoWindowClick(event,w);
  146.             break;
  147.         case inDrag:
  148.             GetWMgrPort(&p);
  149.             r = p->portRect;
  150.             r.top += 38;
  151.             InsetRect(&r,4,4);
  152.             DragWindow(w,event->where,&r);
  153.             break;
  154.         case inGoAway:
  155.             if (TrackGoAway(w,event->where)) CloseWin(w);
  156.             break;
  157.         case inZoomIn:
  158.         case inZoomOut:
  159.             if (TrackBox(w,event->where,i)) {
  160.                 DoResizeInit(w);
  161.                 EraseRect(&(w->portRect));
  162.                 ZoomWindow(w,i,false);
  163.                 DoResizeWindow(w);
  164.                 InvalRect(&(w->portRect));
  165.             }
  166.             break;
  167.     }
  168. }
  169.  
  170.  
  171.  
  172.  
  173.  
  174. /************************************************************************************************/
  175. /*                                                                                                */
  176. /*    MENUS            How to handle the interface to the menu bar                                    */
  177. /*                                                                                                */
  178. /************************************************************************************************/
  179.  
  180.  
  181. /*    DoMenu
  182.  *
  183.  *    What to do when something happens in the menu bar
  184.  */
  185.  
  186. DoMenu(l)
  187. long l;
  188. {
  189.     short hi,lo;
  190.     short i;
  191.  
  192.     hi = (short)(l >> 16);
  193.     lo = (short)(l & 0x0FFFF);
  194.  
  195.     switch (hi) {
  196.         case APPLMENU:
  197.             if (lo == ABOUTMEMENU) DoAboutMe();
  198.             else {
  199.                 char buffer[64];
  200.                 GetItem(applMenu,lo,buffer);
  201.                 OpenDeskAcc(buffer);
  202.             }
  203.             break;
  204.         case FILEMENU:
  205.             switch (lo) {
  206.                 case CLOSEMENU:
  207.                     CloseWin(FrontWindow());
  208.                     break;
  209.                 case QUITMENU:
  210.                     QuitFlag = 1;
  211.                     return;
  212.                 case SAVEMENU:
  213.                     SaveText();
  214.                     break;
  215.                 case PRINTSTL:
  216.                     PrintStl();
  217.                     break;
  218.                 case PRINTJOB:
  219.                     PrintJob();
  220.                     break;
  221.             }
  222.             break;
  223.         case EDITMENU:
  224.             if (!SystemEdit(lo-1)) switch (lo) {
  225.                 case OPTIONS:
  226.                     OptionDialog();
  227.                     break;
  228.             }
  229.             break;
  230.     }
  231.     HiliteMenu(0);
  232. }
  233.  
  234.  
  235. /************************************************************************************************/
  236. /*                                                                                                */
  237. /*    WINDOWS            How to handle windows                                                        */
  238. /*                                                                                                */
  239. /************************************************************************************************/
  240.  
  241.  
  242.  
  243. /*    CloseWin
  244.  *
  245.  *    This closes a window.  Return TRUE if the window was closed successfully
  246.  */
  247.  
  248. int CloseWin(w)
  249. WindowPtr w;
  250. {
  251.     short i;
  252.  
  253.     if (w == NULL) return true;            /* No window to close */
  254.     i = ((WindowPeek)w)->windowKind;    /* Figure out what window we have and close it */
  255.     if (i < 0) {
  256.         CloseDeskAcc(i);                /* Shut down the desk accessory */
  257.         return true;
  258.     } else switch (i) {                    /*CH*/
  259.         case WK_PLAN:
  260.             return ClosePlan(w);
  261.         default:
  262.             DisposeWindow(w);
  263.             return true;
  264.     }
  265. }
  266.  
  267.  
  268. /*    DoWindowClick
  269.  *
  270.  *    This figures out what to do when the mouse goes down
  271.  */
  272.  
  273. DoWindowClick(e,w)
  274. EventRecord *e;
  275. WindowPtr w;
  276. {
  277.     short i;
  278.     Point p;
  279.     ControlHandle c;
  280.     short j,k;
  281.     Rect r;
  282.     Rect s;
  283.     WindowPtr port;
  284.  
  285.     if (w == NULL) return;
  286.     i = ((WindowPeek)w)->windowKind;
  287.     p = e->where;
  288.     GlobalToLocal(&p);
  289.     switch (i) {                        /*CH*/
  290.         case WK_PLAN:
  291.             MousePlan(w,e,&p);
  292.             break;
  293.     }
  294. }
  295.  
  296.  
  297. /*    DoResizeInit
  298.  *
  299.  *    Initialize any variables which need to be initialized before resizing a window
  300.  */
  301.  
  302. DoResizeInit(w)
  303. WindowPtr w;
  304. {
  305.     short i;
  306.  
  307.     if (w == NULL) return;
  308.     i = ((WindowPeek)w)->windowKind;
  309.     switch (i) {                        /*CH*/
  310.         case WK_PLAN:
  311.             ResizeInitPlan(w);
  312.             break;
  313.     }
  314. }
  315.  
  316.  
  317. /*    DoResizeWindow
  318.  *
  319.  *    This resizes the data structures for a new size window
  320.  */
  321.  
  322. DoResizeWindow(w)
  323. WindowPtr w;
  324. {
  325.     short i;
  326.  
  327.     if (w == NULL) return;
  328.     i = ((WindowPeek)w)->windowKind;
  329.     switch (i) {                        /*CH*/
  330.         case WK_PLAN:
  331.             ResizePlan(w);
  332.             break;
  333.     }
  334. }
  335.  
  336.  
  337. /*    DoKey
  338.  *
  339.  *    What to do when the user hits a key
  340.  */
  341.  
  342. DoKey(c,w)
  343. char c;
  344. WindowPtr w;
  345. {
  346.     short i;
  347.  
  348.     if (w == NULL) return;
  349.     i = ((WindowPeek)w)->windowKind;
  350.     switch (i) {
  351.     }
  352. }
  353.  
  354.  
  355. /*    DoUpdate
  356.  *
  357.  *    This does the window update
  358.  */
  359.  
  360. DoUpdate(w)
  361. WindowPtr w;
  362. {
  363.     short i;
  364.  
  365.     if (w == NULL) return;
  366.     i = ((WindowPeek)w)->windowKind;
  367.  
  368.     BeginUpdate(w);
  369.     SetPort(w);
  370.     switch (i) {                        /*CH*/
  371.         case WK_PLAN:
  372.             UpdatePlan(w);
  373.             break;
  374.     }
  375.     EndUpdate(w);
  376. }
  377.  
  378.  
  379. /*    DoActivate
  380.  *
  381.  *    Handle the activate event
  382.  */
  383.  
  384. DoActivate(w,flag)
  385. WindowPtr w;
  386. int flag;
  387. {
  388.     short i;
  389.  
  390.     if (w == NULL) return;
  391.     i = ((WindowPeek)w)->windowKind;
  392.     switch (i) {                        /*CH*/
  393.         case WK_PLAN:
  394.             ActivatePlan(w,flag);
  395.             break;
  396.     }
  397. }
  398.  
  399.  
  400.  
  401. /*    AboutMeDH
  402.  *
  403.  *    The function to pass to ModalDialog for the 'about me' dialog
  404.  */
  405.  
  406. pascal Boolean AboutMeDH(theDialog,theEvent,itemHit)
  407. DialogPtr *theDialog;
  408. EventRecord *theEvent;
  409. short *itemHit;
  410. {
  411.     if (theEvent->what == mouseDown) {
  412.         *itemHit = 1;
  413.         return true;
  414.     } else return false;
  415. }
  416.  
  417.  
  418.  
  419.  
  420. /*    DoAboutMe
  421.  *
  422.  *    How to handle the 'about me' box
  423.  */
  424.  
  425. DoAboutMe()
  426. {
  427.     DialogPtr dlog;
  428.     short i1;
  429.     Handle i2;
  430.     Rect i3;
  431.     short x,y;
  432.     char **ptr;
  433.     char buffer[32];
  434.  
  435.     dlog = GetNewDialog(ABOUTMEDLOG,NULL,(char *)-1);
  436.     if (dlog == NULL) return;
  437.     SetPort(dlog);
  438.  
  439.     for (;;) {
  440.         ModalDialog(AboutMeDH,&i1);
  441.         if (i1 == 1) break;
  442.     }
  443.  
  444.     DisposDialog(dlog);
  445. }
  446.  
  447.