home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Snippets / Background ppat 1.0.2 / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-06  |  5.1 KB  |  249 lines  |  [TEXT/CWIE]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     Background ppat
  4.     demonstrates how to create a full-screen background window using a
  5.     ppat. This version uses the System ppat, but you could use any ppat
  6.     you want.
  7.     
  8.     Written by: Paul Celestin
  9.     
  10.     950620 - work begins on the project
  11.     951215 - updated for CW7
  12.     960704 - updated for CW9
  13.  
  14. ---------------------------------------------------------------------- */
  15.  
  16.  
  17. /* ----------------------------------------------------------------------
  18. includes
  19. ---------------------------------------------------------------------- */
  20.  
  21. #include    "main.h"
  22.  
  23. Boolean                gDone,
  24.                     gWNEImplemented,
  25.                     gInBackground;
  26. EventRecord            gTheEvent;
  27. MenuHandle            gAppleMenu,
  28.                     gFileMenu;
  29. WindowPtr            gTheWindow;
  30. PixPatHandle        gPixPat;
  31.  
  32. /* ----------------------------------------------------------------------
  33. main - here is where it all began...
  34. ---------------------------------------------------------------------- */
  35. void main()
  36. {
  37.     InitToolBox();
  38.     InitMenuBar();
  39.     MainLoop();
  40.     ExitToShell();
  41. }
  42.  
  43.  
  44. /* ----------------------------------------------------------------------
  45. InitToolBox
  46. ---------------------------------------------------------------------- */
  47. void InitToolBox()
  48. {
  49.     InitGraf(&qd.thePort);
  50.     InitFonts();
  51.     FlushEvents(everyEvent, 0);
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs(0L);
  56.     InitCursor();
  57. }
  58.  
  59.  
  60. /* ----------------------------------------------------------------------
  61. InitMenuBar
  62. ---------------------------------------------------------------------- */
  63. void InitMenuBar()
  64. {
  65.     Handle    myMenuBar;
  66.     
  67.     myMenuBar = GetNewMBar(MENU_BASE_ID);
  68.     if (myMenuBar == NIL)
  69.         SysBeep(1);
  70.     else
  71.         SetMenuBar(myMenuBar);
  72.     
  73.     gAppleMenu = GetMHandle(MENU_APPLE_ID);
  74.     if (gAppleMenu == NIL)
  75.         SysBeep(1);
  76.     else
  77.         AddResMenu(gAppleMenu,'DRVR');
  78.     
  79.     gFileMenu = GetMHandle(MENU_FILE_ID);
  80.     if (gFileMenu == NIL)
  81.         SysBeep(1);
  82.  
  83.     DrawMenuBar();
  84. }
  85.  
  86.  
  87. /* ----------------------------------------------------------------------
  88. MainLoop
  89. ---------------------------------------------------------------------- */
  90. void MainLoop()
  91. {
  92.     RgnHandle        cursorRgn;
  93.     Boolean            gotEvent;
  94.     WindowRecord    *myWinRecord;
  95.     Rect            myWinRect;
  96.     Str255             myTitle;
  97.     
  98.     gInBackground = false;
  99.     
  100.     cursorRgn = NewRgn();
  101.     
  102.     gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM,ToolTrap) !=
  103.         NGetTrapAddress(UNIMPL_TRAP_NUM,ToolTrap));
  104.  
  105.     gPixPat = GetPixPat(SYSTEM_PPAT);
  106.  
  107.     myWinRect = qd.screenBits.bounds;
  108.     myWinRecord = NIL;
  109.     gTheWindow =
  110.         NewCWindow(myWinRecord,&myWinRect,"\p",FALSE,plainDBox,(WindowPtr)-1L,FALSE,0L);
  111.     
  112.     if (gTheWindow)
  113.     {
  114.         ShowWindow(gTheWindow);
  115.  
  116.         gDone = false;
  117.  
  118.         while (gDone == false)
  119.         {
  120.             if (gWNEImplemented)
  121.                 gotEvent = WaitNextEvent(everyEvent,&gTheEvent,MIN_SLEEP,cursorRgn);
  122.             else
  123.             {
  124.                 SystemTask();
  125.                 gotEvent = GetNextEvent(everyEvent,&gTheEvent);
  126.             }
  127.         
  128.             if (gotEvent)
  129.                 Do();
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135. /* ----------------------------------------------------------------------
  136. Do
  137. ---------------------------------------------------------------------- */
  138. void Do()
  139. {
  140.     char    c;
  141.     
  142.     switch (gTheEvent.what)
  143.     {
  144.         case nullEvent:
  145.             break;
  146.         case mouseDown:
  147.             DoMouseDown();
  148.             break;
  149.         case keyDown:
  150.         case autoKey:
  151.             c = gTheEvent.message & charCodeMask;
  152.             if ((gTheEvent.modifiers & cmdKey) != 0)
  153.             {
  154.                 DoMenu(MenuKey(c));
  155.             }
  156.             break;
  157.         case activateEvt:
  158.             DoUpdate();
  159.             break;
  160.         case updateEvt:
  161.             DoUpdate();
  162.             break;
  163.         case app4Evt:
  164.             if ((gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING)
  165.                 gInBackground = (gTheEvent.message & 0x01) == 0;
  166.             break;
  167.     }
  168. }
  169.  
  170. /* ----------------------------------------------------------------------
  171. DoUpdate
  172. ---------------------------------------------------------------------- */
  173. void DoUpdate()
  174. {
  175.     GrafPtr            savedPort;
  176.     Rect            theRect;
  177.     
  178.     SetCursor(&qd.arrow);
  179.     GetPort(&savedPort);
  180.     SetPort(gTheWindow);
  181.     BeginUpdate(gTheWindow);
  182.         SetRect(&theRect,gTheWindow->portRect.left,
  183.             gTheWindow->portRect.top,
  184.             gTheWindow->portRect.right,
  185.             gTheWindow->portRect.bottom);
  186.         if (gPixPat != NIL)
  187.             FillCRect(&theRect,gPixPat);
  188.     EndUpdate(gTheWindow);
  189.     SetPort(savedPort);
  190. }
  191.  
  192. /* ----------------------------------------------------------------------
  193. DoMouseDown
  194. ---------------------------------------------------------------------- */
  195. void DoMouseDown()
  196. {
  197.     WindowPtr    window;
  198.     short int    thePart;
  199.     long int    menuChoice, windSize, newSize;
  200.     Boolean        doZoom, doGoAway;
  201.     Point        p;
  202.     
  203.     thePart = FindWindow(gTheEvent.where,&window);
  204.     switch (thePart)
  205.     {
  206.         case inMenuBar:
  207.             menuChoice = MenuSelect(gTheEvent.where);
  208.             DoMenu(menuChoice);
  209.             break;
  210.         case inSysWindow:
  211.             SystemClick(&gTheEvent,window);
  212.             break;
  213.         case inContent:
  214.             if (window != FrontWindow())
  215.                 SelectWindow(window);
  216.             break;
  217.         default:
  218.             break;
  219.     }
  220. }
  221.  
  222. /* ----------------------------------------------------------------------
  223. DoMenu
  224. ---------------------------------------------------------------------- */
  225. void DoMenu(menuChoice)
  226. long int menuChoice;
  227. {
  228.     int    theMenu;
  229.     int    theItem;
  230.     
  231.     if (menuChoice != 0)
  232.     {
  233.         theMenu = HiWord(menuChoice);
  234.         theItem = LoWord(menuChoice);
  235.         switch (theMenu)
  236.         {
  237.             case MENU_APPLE_ID:
  238.                 DoMenuApple(theItem);
  239.                 break;
  240.             case MENU_FILE_ID:
  241.                 DoMenuFile(theItem);
  242.                 break;
  243.             default:
  244.                 break;
  245.         }
  246.         HiliteMenu(0);
  247.     }
  248. }
  249.