home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / QuickDraw / MyDeviceLoop / MyDeviceLoop.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  4.8 KB  |  201 lines  |  [TEXT/KAHL]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    MyDeviceLoop                                            */
  4. /*                                                                            */
  5. /*    Description:    This snippet shows how to write a device loop that        */
  6. /*                    works under System 7 and pre-7.0 systems.  As            */
  7. /*                    described on pages 21-23 and 21-24 of Inside Mac        */
  8. /*                    volume VI, a device loop procedure searches all            */
  9. /*                    active screen devices, calling a drawing procedure        */
  10. /*                    whenever it encounters a screen that intersects            */
  11. /*                    the drawing region.  In this app the drawing            */
  12. /*                    region is the app's window bounds and the chosen        */
  13. /*                    drawing procedure simply displays the screen's            */
  14. /*                    colors for every device the window bounds intersect.    */
  15. /*                                                                            */
  16. /*    Files:            MyDeviceLoop.π                                            */
  17. /*                    MyDeviceLoop.c                                            */
  18. /*                                                                            */
  19. /*    Programmer:        Edgar Lee                                                */
  20. /*    Organization:    Apple Computer, Inc.                                    */
  21. /*    Department:        Developer Technical Support, DTS                        */
  22. /*    Language:        C (Think C version 5.0.1)                                */
  23. /*    Date Created:    02-21-92                                                */
  24. /*                                                                            */
  25. /****************************************************************************/
  26.  
  27. /* Constant Declarations */
  28.  
  29. #define    WWIDTH        400
  30. #define    WHEIGHT        256
  31.  
  32. #define WLEFT        (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  33. #define WTOP        (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  34.  
  35. /* Global Variable Definitions */
  36.  
  37. WindowPtr    gWindow;
  38.  
  39. void initMac();
  40. void createWindow();
  41. void doMyDeviceLoop();
  42. void doDraw();
  43. void doEventLoop();
  44.  
  45. main()
  46. {
  47.     initMac();
  48.     
  49.     createWindow();
  50.  
  51.     doEventLoop();
  52. }
  53.  
  54. void initMac()
  55. {
  56.     MaxApplZone();
  57.  
  58.     InitGraf( &thePort );
  59.     InitFonts();
  60.     InitWindows();
  61.     InitMenus();
  62.     TEInit();
  63.     InitDialogs( nil );
  64.     InitCursor();
  65.     FlushEvents( 0, everyEvent );
  66. }
  67.  
  68. void createWindow()
  69. {
  70.     Rect rect;
  71.     
  72.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  73.     gWindow = NewCWindow( 0L, &rect, "\pMyDeviceLoop", true, documentProc,
  74.                             (WindowPtr)-1L, true, 0L );                        
  75.     SetPort( gWindow );
  76.     
  77.     TextFont( times );
  78.     TextSize( 48 );
  79.     TextMode( srcCopy );
  80. }
  81.  
  82. void doMyDeviceLoop()
  83. {
  84.     int            depth;
  85.     Rect        gDeviceRect;
  86.     Rect        intersectingRect;
  87.     GDHandle    gDevice;
  88.     Point        point;
  89.     
  90.     /* Get the handle to the first device in the list. */
  91.     gDevice = GetDeviceList();
  92.     
  93.     /* Loop through all the devices in the list. */
  94.     while (gDevice != nil)
  95.     {
  96.         /* Get the device's gdRect and convert it to local coordinates. */
  97.         gDeviceRect = (**gDevice).gdRect;
  98.         depth = (**(**gDevice).gdPMap).pixelSize;
  99.             
  100.         GlobalToLocal( &topLeft( gDeviceRect ) );
  101.         GlobalToLocal( &botRight( gDeviceRect ) );
  102.         
  103.         /* Check if the app's window rect intersects the device's, and if it */
  104.         /*    does, set the clip region's rect to the intersection, then DRAW! */
  105.         
  106.         if (SectRect( &gWindow->portRect, &gDeviceRect, &intersectingRect ))
  107.         {
  108.             ClipRect( &intersectingRect );
  109.             doDraw( depth, &intersectingRect );
  110.         }
  111.         
  112.         /* Get the next device in the list. */
  113.         gDevice = GetNextDevice( gDevice );
  114.     }
  115. }
  116.  
  117. void doDraw( depth, rect )
  118. int        depth;
  119. Rect    *rect;
  120. {
  121.     int                i;
  122.     int                totalColors;
  123.     int                penThickness;
  124.     RGBColor        color;
  125.     CTabHandle        ctable;
  126.     Str255            string = "\pDirect Colors";
  127.     
  128.     if (depth > 8)
  129.     {
  130.         BackColor( blackColor );
  131.         ForeColor( blueColor );
  132.         
  133.         /* Draw text for direct colors mode. */
  134.         EraseRect( &gWindow->portRect );
  135.         MoveTo( (gWindow->portRect.right - StringWidth( string )) / 2, 130 );
  136.         DrawString( string );
  137.     }
  138.     else
  139.     {
  140.         /* Get the colortable at this depth. */
  141.         ctable = GetCTable( depth );
  142.         
  143.         /* Set the line thickness to a fraction of the window height. */
  144.         totalColors = (2 << depth) / 2;
  145.         penThickness = gWindow->portRect.bottom / totalColors;
  146.         PenSize( 1, penThickness );
  147.     
  148.         /* Now draw the colors at this depth. */
  149.         for (i = 0; i < totalColors; i++)
  150.         {
  151.             RGBForeColor( &(**ctable).ctTable[i].rgb );
  152.             MoveTo( 0, i * penThickness );
  153.             LineTo( gWindow->portRect.right, i * penThickness );
  154.         }
  155.         
  156.         /* Release the colortable memory. */
  157.         DisposCTable( ctable );
  158.     }
  159. }
  160.  
  161. void doEventLoop()
  162. {
  163.     EventRecord event;
  164.     WindowPtr   window;
  165.     short       clickArea;
  166.     Rect        screenRect;
  167.  
  168.     for (;;)
  169.     {
  170.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  171.         {
  172.             if (event.what == mouseDown)
  173.             {
  174.                 clickArea = FindWindow( event.where, &window );
  175.                 
  176.                 if (clickArea == inDrag)
  177.                 {
  178.                     screenRect = (**GetGrayRgn()).rgnBBox;
  179.                     DragWindow( window, event.where, &screenRect );
  180.                 }
  181.                 else if (clickArea == inContent)
  182.                 {
  183.                     if (window != FrontWindow())
  184.                         SelectWindow( window );
  185.                 }
  186.                 else if (clickArea == inGoAway)
  187.                     if (TrackGoAway( window, event.where ))
  188.                         return;
  189.             }
  190.             else if (event.what == updateEvt)
  191.             {
  192.                 window = (WindowPtr)event.message;    
  193.                 SetPort( window );
  194.                 
  195.                 BeginUpdate( window );
  196.                 doMyDeviceLoop();
  197.                 EndUpdate( window );
  198.             }
  199.         }
  200.     }
  201. }