home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / Skeleton ƒ / Skeleton.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-27  |  5.9 KB  |  200 lines  |  [TEXT/CWIE]

  1. #include <skeleton.h>    /* Note:  You must give you C++ linker the access path to the  */
  2.                         /*         Skeleton ƒ folder that is in you machine or it will  */
  3.                         /*           not be able to find it.                               */
  4.     
  5.  
  6. /* This is where your program will first start to execute.*/
  7.  
  8. void main(void)
  9. {
  10.     
  11.     InitToolBox();
  12.     
  13.     InitOtherStuff();                
  14.  
  15.     SetUpMenus();
  16.     
  17.     EventLoop();
  18.     
  19. }
  20.  
  21. /*---------------------------------------------------------------------------------------------*/
  22. /*---------------------------------------------------------------------------------------------*/
  23.  
  24. void InitToolBox(void)
  25. {
  26.  
  27.     InitGraf(&qd.thePort);            /* These are calls to the Macintosh Programers ToolBox.     */
  28.     InitFonts();                    /* the calls tell the Macinthos OS what types of functions  */
  29.     InitWindows();                    /* will using in our program.  For instance if we ar going  */
  30.     InitMenus();                    /* to use menus in the MenuBar, we must first, make a call  */
  31.     TEInit();                        /* to InitMenus(); to let the OS know that we want to use   */
  32.     InitDialogs(nil);                /* the MenuHandler built into the OS.  This is also true    */ 
  33.     InitCursor();                    /* windows, dialogs and many other things we want to use.   */
  34. }
  35.  
  36. /*---------------------------------------------------------------------------------------------*/
  37. /*---------------------------------------------------------------------------------------------*/
  38.  
  39. void InitOtherStuff(void)
  40. {
  41.     /*Creat a rect with the boundries of 4 pixles from the edge of the screen.  The 24 pixle*/
  42.     /*boder on the top ajusts for the Menu Bar.  This rect will be used to confine the area */
  43.     /*in which windows can be moved.*/
  44.     SetRect(&dragRect, 4, 24, qd.screenBits.bounds.right - 4, qd.screenBits.bounds.bottom - 4);
  45.     
  46.     appleMenu = GetMenu(appleID);                 /* Read Apple menu from resource file and */
  47.                                                 /* We will need to reference this in the */
  48.                                                 /* SetUpMenus routine.*/
  49. }    
  50.  
  51. /*---------------------------------------------------------------------------------------------*/
  52. /*---------------------------------------------------------------------------------------------*/
  53.  
  54. void SetUpMenus(void)
  55. {
  56.     Handle             MenuBar;
  57.     
  58.     MenuBar = GetNewMBar(128);                    /* read Mbar #128 from recource file */
  59.                                                 
  60.     SetMenuBar(MenuBar);                        /* Set the current MenuBar to "MenuBar" */                        
  61.     
  62.     AddResMenu(appleMenu, 'DRVR');                /* add desk accessory names to Apple menu */
  63.     
  64.     DrawMenuBar();                                /* and draw menu bar */
  65. }
  66.  
  67. /*---------------------------------------------------------------------------------------------*/
  68. /*---------------------------------------------------------------------------------------------*/
  69.  
  70. void EventLoop(void)
  71. {
  72.     
  73.     do {
  74.         SystemTask();
  75.  
  76.         if (WaitNextEvent(everyEvent, &myEvent, 5L, NULL)) {
  77.             switch (myEvent.what) {                /* The case on event type */
  78.  
  79.                 case mouseDown:
  80.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  81.  
  82.                         case inSysWindow:        /* The desk accessory window */
  83.                             SystemClick(&myEvent, whichWindow);
  84.                             break;
  85.  
  86.                         case inMenuBar:            /* The Menu bar  */
  87.                             MenuHandler(MenuSelect(myEvent.where));  /* Determine Which menu it was */
  88.                             break;
  89.  
  90.                         case inDrag:            /* Title bar */
  91.                             DragWindow(whichWindow, myEvent.where, &dragRect);  /* Drag Manager */
  92.                             break;
  93.  
  94.                         case inContent:            /* The body of one of our application's windows */
  95.                             if (whichWindow != FrontWindow())  
  96.                                 SelectWindow(whichWindow); /* Make it active if not if it wasn't*/
  97.                             break;
  98.                             
  99.                         case inGoAway:            /* Close box of a window */
  100.                             if (TrackGoAway( whichWindow, myEvent.where ))
  101.                                 DisposeWindow(whichWindow);
  102.                             break;
  103.                     }
  104.                     break;
  105.  
  106.                 case updateEvt:                    /* Update the window. */
  107.                     if ((WindowPtr) myEvent.message == FrontWindow()) {
  108.                         BeginUpdate((WindowPtr) myEvent.message);
  109.                 
  110.                         EndUpdate((WindowPtr) myEvent.message);
  111.                     }
  112.                     break;
  113.                             
  114.                 case keyDown:
  115.                 case autoKey:                    /* A key was pressed once or held down to repeat */
  116.                         theChar = (myEvent.message & charCodeMask); /* get the char */
  117.                         /* 
  118.                         **    If Command key down, do it as a Menu Command.
  119.                         */
  120.                     if (myEvent.modifiers & cmdKey)
  121.                         MenuHandler(MenuKey(theChar));
  122.                     
  123.                     break;
  124.  
  125.             }
  126.         }
  127.  
  128.         
  129.     } while (!quit);
  130.     
  131. }
  132.  
  133. /*---------------------------------------------------------------------------------------------*/
  134. /*---------------------------------------------------------------------------------------------*/
  135.  
  136. void MenuHandler(long int mResult)
  137. {
  138.     
  139.     short    theItem,                            /* menu item number from mResult low-order word */
  140.             theMenu;                            /* menu number from mResult high-order word */
  141.     Str255    name;                                /* desk accessory name */
  142.     
  143.     
  144.  
  145.     theItem = LoWord(mResult);                    /* call Toolbox Utility routines to */
  146.     theMenu = HiWord(mResult);                    /* set menu item number and menu */
  147.  
  148.     switch (theMenu) {                            /* switch on menu ID */
  149.  
  150.         case appleID:
  151.             if (theItem == 1){
  152.                 ShowAboutMeDialog();
  153.                 
  154.             }
  155.             
  156.             else {
  157.                 GetItem(appleMenu, theItem, name);
  158.                 OpenDeskAcc(name);
  159.             }
  160.             break;
  161.  
  162.         case fileID:
  163.             if (theItem == 1){
  164.                 quit = true;
  165.             }
  166.             
  167.         
  168.             break;
  169.             
  170.             
  171.             
  172.                     
  173.     }
  174.  
  175.     HiliteMenu(0);                                /* Unhighlight menu title */
  176.                                                 /* (highlighted by MenuSelect) */
  177. }
  178.  
  179. /*---------------------------------------------------------------------------------------------*/
  180. /*---------------------------------------------------------------------------------------------*/
  181.  
  182. void ShowAboutMeDialog(void)
  183. {
  184.     DialogPtr    aboutDialog;
  185.     short        itemHit;
  186.  
  187.     aboutDialog = GetNewDialog(128, nil, (WindowPtr) -1);        /* Put up Dialog number 128 */
  188.     
  189.     SetDialogDefaultItem(aboutDialog, 1);                        /* Frame the ok button, and */
  190.                                                                 /* Link it to the Return key*/
  191.     
  192.     ModalDialog(nil, nil);                                        /* Wait for a key to be hit */
  193.     
  194.     DisposDialog(aboutDialog);                                    /* Dispose of the Dlog box. */
  195. }
  196.  
  197. /*---------------------------------------------------------------------------------------------*/
  198. /*---------------------------------------------------------------------------------------------*/
  199.  
  200.