home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / DeskPat / deskpat.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.6 KB  |  217 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        deskpat.c
  3.  
  4.     Contains:    Getting Desktop and Utilities Pattern  snippet
  5.     
  6.                 This snippet how you can  access the "Desktop Pattern" and 
  7.                 the "Set Utilities Pattern" pattern.(This pattern is set by holding down \
  8.                 the option key in the Desktop Patterns Control Panel.)
  9.     
  10.                 This control panel uses resources of type 'ppat' to store 
  11.                 this pattern. The `ppat' resource is stored in the System file
  12.                 in your System Folder; the desktop pattern has an ID of 16 and 
  13.                 the utilities pattern has an ID of 42. Since this is not
  14.                 documented, it could be subject to change at any moment. 
  15.                 You should be careful when using this.
  16.  
  17.  
  18.     Written by: Albert Hui    
  19.  
  20.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  21.  
  22.                 You may incorporate this Apple sample source code into your program(s) without
  23.                 restriction. This Apple sample source code has been provided "AS IS" and the
  24.                 responsibility for its operation is yours. You are not permitted to redistribute
  25.                 this Apple sample source code as "Apple sample source code" after having made
  26.                 changes. If you're going to re-distribute the source, we require that you make
  27.                 it clear in the source that the code was descended from Apple sample source
  28.                 code, but that you've made changes.
  29.  
  30.     Change History (most recent first):
  31.                 8/5/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  32.                 
  33.  
  34. */
  35. #include <Events.h>
  36. #include <Windows.h>
  37. #include <Fonts.h>
  38. #include <TextEdit.h>
  39. #include <Dialogs.h>
  40. #include <Controls.h>
  41.  
  42. void MainLoop(void);
  43. void HandleKeyDown (EventRecord *);
  44. void MacInits(void);
  45. void FinishUp(void);
  46. void HandleUpdates(EventRecord *);
  47. void HandleMouseDown(EventRecord *);
  48.  
  49. Boolean   gDone = false;
  50. Boolean   gInBackground = false;
  51. WindowPtr gWindow1 = NULL;
  52. WindowPtr gWindow2 = NULL;
  53. static  PixPatHandle ppatHandle;
  54.  
  55.  
  56. void main()
  57. {
  58.   MacInits();
  59.  
  60.   gWindow1    = GetNewCWindow (128, NULL, (WindowPtr) -1);
  61.   gWindow2 = GetNewCWindow (129, NULL, (WindowPtr) -1);
  62.   SelectWindow(gWindow1);
  63.   SetPort (gWindow1);
  64.  
  65.   MainLoop();
  66.   FinishUp();
  67. }
  68.  
  69. void MacInits()
  70. {
  71.   MaxApplZone();
  72.   MoreMasters();
  73.   MoreMasters();
  74.  
  75.   InitGraf(&qd.thePort);
  76.   InitFonts(); 
  77.   InitWindows();
  78.   InitMenus();
  79.   TEInit();                               
  80.   InitDialogs(0L);                        
  81.   FlushEvents (everyEvent, 0);          
  82.   InitCursor(); 
  83. }
  84.  
  85.  
  86. void MainLoop(void)
  87. {
  88.   EventRecord    theEvent;
  89.  
  90.   while(!gDone) {
  91.     if (WaitNextEvent (everyEvent, &theEvent, 30, 0L)) {
  92.         switch (theEvent.what) {
  93.         case mouseDown:
  94.           HandleMouseDown(&theEvent);
  95.           break;
  96.  
  97.         case autoKey:
  98.         case keyDown:
  99.           HandleKeyDown(&theEvent);
  100.           break;
  101.  
  102.         case updateEvt:
  103.           HandleUpdates(&theEvent);
  104.           break;
  105.  
  106.         case activateEvt:
  107.           gInBackground = false;
  108.           break;
  109.       }
  110.     } 
  111.   }
  112. }
  113.  
  114.  
  115.  
  116. void HandleKeyDown (EventRecord *evt)
  117. {
  118.   char  theChar;
  119.  
  120.   theChar = (evt->message & charCodeMask);
  121.  
  122.   if ((evt->modifiers & cmdKey) != 0) {
  123.     switch (theChar) {
  124.       case 'q': case 'Q':
  125.         gDone = true;
  126.         break;
  127.     }
  128.   }
  129. }
  130.  
  131.  
  132. void HandleMouseDown(EventRecord *theEvent)
  133. {
  134.   WindowPtr     whichWindow;
  135.   short         thePart;
  136.  
  137.  
  138.   thePart = FindWindow (theEvent->where, &whichWindow);
  139.  
  140.   switch (thePart) {
  141.     case inSysWindow:
  142.       SystemClick (theEvent, whichWindow);
  143.       break;
  144.  
  145.     case inDrag:
  146.       DragWindow(whichWindow, theEvent->where, &qd.screenBits.bounds);
  147.       break;
  148.  
  149.     case inContent:
  150.       SetPort (whichWindow);
  151.       if (whichWindow != FrontWindow())
  152.         SelectWindow(whichWindow);
  153.       break;
  154.  
  155.     case inGoAway:
  156.       if (whichWindow == gWindow1 &&
  157.          TrackGoAway(gWindow1, theEvent->where) )
  158.         HideWindow(gWindow1);
  159.       gDone = true;
  160.       break;
  161.   }
  162. }
  163.  
  164. void HandleUpdates(EventRecord *theEventPtr)
  165. {
  166.   GrafPtr savedPort;
  167.   WindowPtr windowPtr;
  168.   Rect destRect;
  169.  
  170.   GetPort(&savedPort);
  171.  
  172.   windowPtr = (WindowPtr) theEventPtr->message;
  173.  
  174.   BeginUpdate(windowPtr);
  175.  
  176.   if(!EmptyRgn(windowPtr->visRgn))
  177.   {
  178.     SetPort(windowPtr);
  179.  
  180.     if (windowPtr == gWindow2) {
  181.       ppatHandle = (PixPatHandle) GetPixPat(16);
  182.       if (ppatHandle != NULL) {
  183.           SetRect(&destRect, 15, 125, 197, 164);
  184.           FrameRect(&destRect);
  185.           FillCRect(&destRect, ppatHandle);
  186.           DisposePixPat(ppatHandle);
  187.       }
  188.      }
  189.     if (windowPtr == gWindow1) {
  190.       ppatHandle = (PixPatHandle) GetPixPat(42);
  191.       if (ppatHandle != NULL) {
  192.           SetRect(&destRect, 15, 125, 197, 164);
  193.           FrameRect(&destRect);
  194.           FillCRect(&destRect, ppatHandle);
  195.           DisposePixPat(ppatHandle);
  196.       }
  197.      }
  198.   }
  199.  
  200.   EndUpdate(windowPtr);
  201.   SetPort(savedPort);
  202. }
  203.  
  204.  
  205.  
  206. void FinishUp()
  207. {
  208.   if (gWindow2)
  209.     DisposeWindow(gWindow2);
  210.  
  211.   if (gWindow1)
  212.     DisposeWindow(gWindow1);
  213.  
  214.   ExitToShell();
  215. }
  216.  
  217.