home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / macify13.shr / macify.hqx / Source / P / Main_Macify.c next >
Text File  |  1991-03-15  |  17KB  |  446 lines

  1. /* Main_Macify */
  2.  
  3. /* Program name:  Main_Macify.c   */
  4. /* Function:  This is the main module for this program.   */
  5. /* History: 3/15/91 Original by Prototyper 3.0   */
  6.  
  7. #include <stdio.h>            /* Standard I/O functions */
  8.  
  9. #include "PCommonMacify.h"    /* Common */
  10. #include "Common_Macify.h"    /* Common */
  11. #include "PUtils_Macify.h"    /* General Utilities */
  12. #include "Utils_Macify.h"    /* General Utilities */
  13.  
  14. #include "InitExitMacify.h"    /* Init, Exit handlers */
  15. #include "EventsMacify.h"    /* Extra event handlers */
  16.  
  17. #include "PD_About_Dialog.h"    /* Modal Dialog */
  18. #include "PIMenu_Macify.h"    /* Init menus */
  19. #include "PDoMenuMacify.h"    /* Handle menus */
  20.  
  21. Boolean        DoIt;                                                            /* Flag saying an event is ready */
  22. short    code;                                                                 /* Determine event type */
  23. WindowPtr    whichWindow;                                                /* See which window for event */
  24. long    mResult;                                                              /* Menu list and item selected values */
  25. short    theMenu,theItem;                                                  /* Menu list and item selected */
  26.  
  27. /* Prototypes */
  28.  
  29. /* See if WaitNextEvent is available */
  30. static Boolean WNEIsImplemented(void);
  31.  
  32. /* Check for user events */
  33. static void Handle_User_Event(void);
  34.  
  35. /* Handle key strokes */
  36. static void DoKeyEvent(void);
  37.  
  38. /* Handle a diskette inserted */
  39. static void DoDiskEvent(void);
  40.  
  41. /* Handle a window being resized */
  42. static void DoGrow(WindowPtr whichWindow);
  43.  
  44. /* Handle a window being dragged */
  45. static void DoDrag(WindowPtr whichWindow);
  46.  
  47. /* Handle a window goaway box */
  48. static void DoGoAway(WindowPtr whichWindow);
  49.  
  50. /* Handle a hit in the window */
  51. static void DoInContent(WindowPtr whichWindow);
  52.  
  53. /* Handle an update to the window */
  54. static void DoUpdate(void);
  55.  
  56. /* Handle an activate of the window */
  57. static void DoActivate(void);
  58.  
  59.  
  60.  
  61. /* MAIN entry point */
  62. void main(void);
  63.  
  64.  
  65.  
  66. /* ======================================================= */
  67.  
  68. /* Routine: WNEIsImplemented */
  69. /* Purpose: See if the MultiFinder trap, WaitNextEvent, is available */
  70.  
  71. Boolean WNEIsImplemented()                                               /* See if WaitNextEvent is available */
  72. {
  73. #define    WNETrapNumber    0xA860                                    /* The expected trap number */
  74. SysEnvRec    theWorld;                                                     /* Environment record */
  75. OSErr    discardError;                                                      /* Error code returned */
  76. Boolean    theWNEIsImplemented;                                         /* Value to return */
  77.  
  78. HasColorQD = FALSE;                                                       /* Init to no color QuickDraw */
  79. HasFPU = FALSE;                                                             /* Init to no floating point chip */
  80. InTheForeground = TRUE;                                                   /* Init to a foreground app */
  81. discardError = SysEnvirons(1, &theWorld);                            /* Check how old this system is */
  82. if (theWorld.machineType < 0)                                             /* Negative means really old */
  83.     theWNEIsImplemented = FALSE;                                        /* Really old ROMs, no WNE possible */
  84. else
  85.     {
  86.     theWNEIsImplemented = TrapAvailable(WNETrapNumber, ToolTrap);/* See if trap is there */
  87.     HasColorQD = theWorld.hasColorQD;                                  /* Flag for Color QuickDraw being available */
  88.     HasFPU = theWorld.hasFPU;                                             /* Flag for Floating Point Math Chip being available */
  89.     }
  90. return(theWNEIsImplemented);
  91. }
  92.  
  93. /* ======================================================= */
  94.  
  95. /* Routine: Handle_User_Event */
  96. /* Purpose: Check for user events */
  97.  
  98. void Handle_User_Event()                                                 /* Check for user events */
  99. {
  100. UserEventRec    TheUserEvent;                                            /* The user event */
  101.  
  102. GetUserEvent(&TheUserEvent);                                           /* Check for any user events */
  103. if (TheUserEvent.ID != UserEvent_None)                                 /* Only do if we have any */
  104.     {
  105.  
  106.     switch (TheUserEvent.ID)                                               /* Key off the Event ID */
  107.         {
  108.         case UserEvent_Open_Window:                                      /* Open a Window or Modeless dialog */
  109.             switch (TheUserEvent.ID2)                                       /* Do the appropiate window */
  110.                 {
  111.                 case Res_D_About_Dialog:
  112.                     PD_About_Dialog();                                         /* Open this modal dialog */
  113.                     break;
  114.                 default:                                                           /* Handle others */
  115.                     break;
  116.                 }                                                                   /* End of the switch */
  117.             break;
  118.  
  119.         case UserEvent_Close_Window:                                     /* Close a Window or Modeless dialog */
  120.             break;
  121.         default:                                                                 /* Not standard, must be program specific */
  122.             Handle_UserEvent(&TheUserEvent);                            /* Let program specific handle it */
  123.             break;
  124.         }                                                                         /* End of switch */
  125.  
  126.         }                                                                         /* End of handling a user event */
  127. }
  128.  
  129. /* ======================================================= */
  130.  
  131. /* Routine: DoKeyEvent */
  132. /* Purpose: Handle a key pressed */
  133.  
  134. void DoKeyEvent()                                                           /* Handle key presses */
  135. {
  136. short    charCode;                                                           /* Key code */
  137. char    ch;                                                                     /* Key pressed in Ascii */
  138. long    mResult;                                                              /* Menu list and item, if a command key */
  139. short    theMenu,theItem;                                                  /* Menu list and item, if command key */
  140.  
  141. if (HandleKey(&myEvent))                                                  /* Allow for special key handling */
  142.     {
  143.  
  144.     charCode = myEvent.message & charCodeMask;                     /* Get the character */
  145.     ch = (char)charCode;                                                     /* Change it to ASCII */
  146.  
  147.     if ((myEvent.modifiers / cmdKey) & 1)                              /* See if Command key is down */
  148.         {
  149.         mResult = MenuKey(ch);                                             /* See if a menu selection */
  150.         theMenu = HiWord(mResult);                                        /* Get the menu list number */
  151.         theItem = LoWord(mResult);                                         /* Get the menu item number */
  152.         if (theMenu != 0)                                                      /* See if a list was selected */
  153.             Handle_My_Menu(theMenu, theItem);                          /* Do the menu selection */
  154.  
  155.         if (((ch == 'x') || (ch == 'X')) && (theInput != NIL))              /* See if a standard Cut */
  156.             TECut(theInput);                                                    /* Handle a Cut in a TE area */
  157.         if (((ch == 'c') || (ch == 'C')) && (theInput != NIL))              /* See if a standard Copy */
  158.             TECopy(theInput);                                                 /* Handle a Copy in a TE area */
  159.         if (((ch == 'v')  ||  (ch == 'V')) && (theInput != NIL))             /* See if a standard Paste */
  160.             TEPaste(theInput);                                                 /* Handle a Paste in a TE area */
  161.         }                                                                         /* End of Command key special condition */
  162.     else if (theInput != NIL)
  163.         TEKey(ch,theInput);                                                  /* Place the normal key stroke */
  164.  
  165.     }                                                                             /* End of Standard keystroke handling */
  166. }
  167.  
  168. /* ======================================================= */
  169.  
  170. /* Routine: DoDiskEvent */
  171. /* Purpose: Handle a diskette inserted */
  172.  
  173. void DoDiskEvent()                                                          /* Handle disk inserted */
  174. {
  175. short    theError;                                                            /* Error returned from mount */
  176.  
  177. if (HandleDisk(&myEvent))                                                 /* Allow for special disk inserted handling */
  178.     {
  179.  
  180.     if (HiWord(myEvent.message) != noErr)                              /* See if a diskette mount error */
  181.         {                                                                         /* due to unformatted diskette inserted */
  182.         myEvent.where.h = ((screenBits.bounds.right - screenBits.bounds.left) / 2) - (304 / 2);/* Center horz */
  183.         myEvent.where.v = ((screenBits.bounds.bottom - screenBits.bounds.top) / 3) - (104 / 2);/* Top 3ed vertically */
  184.         InitCursor();                                                           /* Make sure it has an arrow cursor */
  185.         theError = DIBadMount(myEvent.where, myEvent.message);/* Let the OS handle the diskette */
  186.         }
  187.  
  188.     }                                                                             /* End of Standard disk inserted handling */
  189. }
  190.  
  191. /* ======================================================= */
  192.  
  193. /* Routine: DoGrow */
  194. /* Purpose: Handle a window resize */
  195.  
  196. void DoGrow(whichWindow)                                                /* Handle a window being resized */
  197. WindowPtr    whichWindow;
  198. {
  199. Rect    OldRect;                                                              /* Window rect before the grow */
  200. Point    myPt;                                                                /* Point for tracking grow box */
  201. Rect    GrowRect;                                                            /* Set the grow bounds */
  202. long    mResult;                                                              /* Result from the grow */
  203.  
  204. if (whichWindow != NIL)                                                     /* See if we have a legal window */
  205.     {
  206.     SetPort(whichWindow);                                                 /* Get ready to draw in this window */
  207.  
  208.     myPt = myEvent.where;                                                 /* Get mouse position */
  209.     GlobalToLocal(&myPt);                                                 /* Make it relative */
  210.  
  211.     OldRect = whichWindow->portRect;                                   /* Save the rect before resizing */
  212.  
  213.     SetRect(&GrowRect, 4, 4, (screenBits.bounds.right - screenBits.bounds.left)-4,  (screenBits.bounds.bottom - screenBits.bounds.top) - 4);/* l,t,r,b */
  214.     mResult = GrowWindow(whichWindow, myEvent.where, &GrowRect);/* Grow it */
  215.     SizeWindow(whichWindow, LoWord(mResult), HiWord(mResult), TRUE);/* Resize to result */
  216.  
  217.  
  218.     SetPort(whichWindow);                                                 /* Get ready to draw in this window */
  219.  
  220.     myPt.h = whichWindow->portRect.right - whichWindow->portRect.left; /* Local right edge */
  221.     myPt.v = whichWindow->portRect.bottom - whichWindow->portRect.top; /* Local bottom edge */
  222.  
  223.     SetRect(&GrowRect, 0, myPt.v - 15, myPt.h + 15, myPt.v + 15); /* Position for horz scrollbar area */
  224.     EraseRect(&GrowRect);                                                 /* Erase old area */
  225.     InvalRect(&GrowRect);                                                 /* Flag us to update it */
  226.  
  227.     SetRect(&GrowRect, myPt.h - 15, 0, myPt.h + 15, myPt.v + 15);  /* Position for vert scrollbar area */
  228.     EraseRect(&GrowRect);                                                 /* Erase old area */
  229.     InvalRect(&GrowRect);                                                 /* Flag us to update it */
  230.  
  231.     DrawGrowIcon(whichWindow);                                         /* Draw the grow Icon again */
  232.     }                                                                             /* End of (WhichWindow <> nil) */
  233. }
  234.  
  235. /* ======================================================= */
  236.  
  237. /* Routine: DoDrag */
  238. /* Purpose: Drag a window around */
  239.  
  240. void DoDrag( whichWindow)                                                /* Handle a window being dragged */
  241. WindowPtr    whichWindow;
  242. {
  243. Rect    OldRect;                                                              /* Window rect before the drag */
  244. Rect    tempRect;                                                            /* temporary rect */
  245.  
  246. OldRect = whichWindow->portRect;                                      /* Save the rect before resizing */
  247.  
  248. tempRect = screenBits.bounds;                                            /* Get screen area,  l,t,r,b, drag area */
  249. SetRect(&tempRect,tempRect.left+4,tempRect.top+4,tempRect.right-4,tempRect.bottom - 4);
  250. DragWindow(whichWindow, myEvent.where, &tempRect);           /* Drag the window */
  251.  
  252.  
  253. }
  254.  
  255. /* ======================================================= */
  256.  
  257. /* Routine: DoGoAway */
  258. /* Purpose: Close a window */
  259.  
  260. void DoGoAway( whichWindow)                                           /* Handle a window goaway box */
  261. WindowPtr    whichWindow;
  262. {
  263. Rect    OldRect;                                                              /* Window rect before the drag */
  264. Rect    tempRect;                                                            /* temporary rect */
  265.  
  266. if (TrackGoAway(whichWindow,myEvent.where) == TRUE)          /* See if mouse released in GoAway box */
  267.     {                                                                             /* Handle the GoAway */
  268.     }                                                                             /* End of TrackGoAway */
  269.  
  270. }
  271.  
  272. /* ======================================================= */
  273.  
  274. /* Routine: DoInContent */
  275. /* Purpose: Pressed in the content area */
  276.  
  277. void DoInContent( whichWindow)                                          /* Handle a hit in the window */
  278. WindowPtr    whichWindow;
  279. {
  280.  
  281. if (whichWindow != FrontWindow())                                      /* See if already selected or not, in front if selected */
  282.     SelectWindow(whichWindow);                                          /* Select this window to make it active */
  283. else                                                                            /* If already in front the already selected */
  284.     {                                                                             /* Handle the press in the content */
  285.     SetPort(whichWindow);                                                 /* Get ready to draw in this window */
  286.     }                                                                             /* End of else */
  287.  
  288. }
  289.  
  290. /* ======================================================= */
  291.  
  292. /* Routine: DoUpdate */
  293. /* Purpose: Got an update event */
  294.  
  295. void DoUpdate()                                                              /* Handle an update to the window */
  296. {
  297. WindowPtr    whichWindow;                                                /* See which window for event */
  298.  
  299. whichWindow = (WindowPtr)myEvent.message;                        /* Get the window the update is for */
  300.  
  301. BeginUpdate(whichWindow);                                               /* Set the clipping to the update area */
  302. EndUpdate(whichWindow);                                                 /* Return to normal clipping area */
  303. }
  304.  
  305. /* ======================================================= */
  306.  
  307. /* Routine: DoActivate */
  308. /* Purpose: Got an activate or deactivate event */
  309.  
  310. void DoActivate()                                                            /* Handle an activate of the window */
  311. {
  312. Boolean    Do_An_Activate;                                                /* Flag to pass */
  313. WindowPtr    whichWindow;                                                /* See which window for event */
  314.  
  315. whichWindow = (WindowPtr)myEvent.message;                        /* Get the window the update is for */
  316.  
  317. Do_An_Activate = ((myEvent.modifiers & 0x0001) != 0);           /* Make sure it is Activate and not DeActivate */
  318. }
  319.  
  320. /* ======================================================= */
  321.  
  322.  
  323.  void main()                                                                  /* Start of main body */
  324. {
  325.  
  326. MoreMasters();                                                             /* This reserves space for more handles */
  327. MaxApplZone();                                                              /* Give us room for memory allocation */
  328. InitGraf(&thePort);                                                         /* Quickdraw Init */
  329. InitFonts();                                                                   /* Font manager init */
  330. InitWindows();                                                               /* Window manager init */
  331. InitMenus();                                                                  /* Menu manager init */
  332. TEInit();                                                                       /* Text edit init */
  333. InitDialogs(NIL);                                                             /* Dialog manager */
  334.  
  335. FlushEvents ( everyEvent , 0 );                                           /* Clear out all events */
  336. InitCursor();                                                                 /* Make an arrow cursor */
  337.  
  338. doneFlag = FALSE;                                                           /* Do not exit program yet */
  339.  
  340. Init_My_Menus();                                                           /* Initialize menu bar */
  341.  
  342. theInput = NIL;                                                               /* Init to no text edit selection active */
  343.  
  344. SleepValue = 40;                                                             /* Set sleep value */
  345. WNE = WNEIsImplemented();                                               /* See if WaitNextEvent is available */
  346.  
  347. UserEventList = NIL;                                                        /* No user events yet */
  348.  
  349. ApplInit_Macify();                                                          /* Handle extra program initialization */
  350.  
  351. I_PD_About_Dialog();                                                       /* Initialize the modal dialog globals */
  352.  
  353.  
  354. do                                                                              /* Start of main event loop */
  355.     {
  356.  
  357.     ApplLoop_Macify();                                                      /* Let into main loop */
  358.  
  359.     Handle_User_Event();                                                   /* Check for user events */
  360.  
  361.     if (theInput != NIL)                                                        /* See if a TE is active */
  362.         TEIdle(theInput);                                                      /* Blink the cursor if everything is ok */
  363.  
  364.     if (WNE == TRUE)                                                         /* See if do the MultiFinder way */
  365.         DoIt = WaitNextEvent(everyEvent, &myEvent, SleepValue, NIL);/* Wait for an event */
  366.     else
  367.         {
  368.         SystemTask();                                                         /* For support of desk accessories */
  369.         DoIt = GetNextEvent(everyEvent, &myEvent);                   /* See if an event is ready */
  370.         }
  371.  
  372.     ApplEvent_Macify(&DoIt,&myEvent);                                /* Let us at the event first */
  373.  
  374.     if (DoIt == TRUE)                                                         /* If event then... */
  375.         {                                                                         /* Start handling the event */
  376.  
  377.  
  378.         switch (myEvent.what)                                              /* Decide type of event */
  379.             {
  380.             case mouseDown :                                                 /* Mouse button pressed */
  381.                 code = FindWindow(myEvent.where, &whichWindow);/* Get which window the event happened in */
  382.  
  383.                 switch (code)                                                    /* Decide type of event again */
  384.                     {
  385.                     case inMenuBar :                                             /* In the menubar */
  386.                         mResult = MenuSelect(myEvent.where);             /* Do menu selection */
  387.                         theMenu = HiWord(mResult);                           /* Get the menu list number */
  388.                         theItem = LoWord(mResult);                            /* Get the menu list item number */
  389.                         Handle_My_Menu( theMenu, theItem);                /* Handle the menu */
  390.                         break;                                                      /* End of inMenuBar */
  391.  
  392.                     case inDrag :                                                 /* In window drag area */
  393.                         DoDrag(whichWindow);                                  /* Go drag the window */
  394.                         break;                                                      /* End of InDrag */
  395.  
  396.                     case inGrow :                                                 /* In window grow area */
  397.                         DoGrow(whichWindow);                                 /* Handle the growing */
  398.                         break;                                                      /* End of inGrow */
  399.  
  400.                     case inGoAway :                                             /* In window goaway area */
  401.                         DoGoAway(whichWindow);                             /* Handle the goaway button */
  402.                         break;                                                      /* End of inGoAway */
  403.  
  404.                     case inContent :                                             /* In window  contents */
  405.                         DoInContent(whichWindow);                             /* Handle the hit inside a window */
  406.                         break;                                                      /* End of inContent */
  407.  
  408.                     case inSysWindow :                                         /* See if a DA selectio */
  409.                         SystemClick(&myEvent, whichWindow);             /* Let other programs in */
  410.                         break;                                                      /* End of inSysWindow */
  411.  
  412.                     default:                                                        /* Handle others */
  413.                         break;                                                      /* End of otherwise */
  414.                     }                                                                /* End of code case */
  415.                 break;                                                             /* End of MouseDown */
  416.  
  417.             case keyDown:                                                     /* Handle key inputs */
  418.             case autoKey:                                                      /* and auto repeats */
  419.                 DoKeyEvent();                                                   /* Get the key and handle it */
  420.                 break;                                                             /* End of otherwise */
  421.  
  422.             case updateEvt :                                                    /* Update event for a window */
  423.                 DoUpdate();                                                      /* Handle the update */
  424.                 break;                                                             /* End of otherwise */
  425.  
  426.             case diskEvt :                                                      /* Disk inserted event */
  427.                 DoDiskEvent();                                                  /* Handle a disk event */
  428.                 break;                                                             /* End of otherwise */
  429.  
  430.             case activateEvt :                                                 /* Window activated event */
  431.                 DoActivate();                                                    /* Handle the activation */
  432.                 break;                                                             /* End of otherwise */
  433.  
  434.             default:                                                              /* Used for debugging, to see what other events are coming in */
  435.                 break;                                                             /* End of otherwise */
  436.  
  437.             }                                                                      /* End of case */
  438.  
  439.         }                                                                         /* end of GetNextEvent */
  440.     }                                                                             /* end of while */
  441. while (doneFlag == FALSE);                                                /* End of the event loop */
  442.  
  443. ApplExit_Macify();                                                         /* Handle extra program termination code */
  444.  
  445. }                                                                                /* end of main */
  446.