home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / macintosh-c / macc-carbon-demos-nonbinhex.sit / macc-carbon-demos-nonbinhex / chap11-demo / PreQuickDraw.c < prev    next >
C/C++ Source or Header  |  2001-07-09  |  17KB  |  608 lines

  1. // *******************************************************************************************
  2. // PreQuickDraw.c                                                          CLASSIC EVENT MODEL
  3. // *******************************************************************************************
  4. // 
  5. // This program opens a window in which is displayed some information retrieved from the
  6. // GDevice structure for the main video device, from the graphics port's pixel map, and from
  7. // the graphics port object using QuickDraw functions.  
  8. //
  9. // A Demonstration menu allows the user to set the monitor to various pixel depths and to 
  10. // restore the original pixel depth.  Setting the monitor to a pixel depth of 8 (256 colours)
  11. // or less causes the colours in the colour table to be displayed.
  12. //
  13. // The program utilises 'plst', 'MBAR', 'MENU', 'WIND', and 'STR#' resources, and a 'SIZE'
  14. // resource with the acceptSuspendResumeEvents, canBackground, doesActivateOnFGSwitch, and 
  15. // isHighLevelEventAware flags set.
  16. //
  17. // *******************************************************************************************
  18.  
  19. // ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
  20.  
  21. #include <Carbon.h>
  22.  
  23. // …………………………………………………………………………………………………………………………………………………………………………………………………………………………… defines
  24.  
  25. #define rMenubar                            128
  26. #define rWindow                                128
  27. #define mAppleApplication            128
  28. #define  iAbout                                1
  29. #define mFile                                    129
  30. #define  iQuit                                12
  31. #define mDemonstration                131
  32. #define  iSetDepth8                        1
  33. #define  iSetDepth16                    2
  34. #define  iSetDepth32                    3
  35. #define  iRestoreStartDepth        5
  36. #define rIndexedStrings                128
  37. #define  sMonitorInadequate        1
  38. #define  sMonitorAtThatDepth    2  
  39. #define  sMonitorAtStartDepth    3 
  40. #define  sRestoringMonitor        4
  41. #define MAX_UINT32                        0xFFFFFFFF
  42.  
  43. // …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
  44.  
  45. Boolean    gDone;
  46. SInt16    gStartupPixelDepth;
  47.  
  48. // …………………………………………………………………………………………………………………………………………………………………………………………… function prototypes
  49.  
  50. void        main                                                (void);
  51. void        doPreliminaries                            (void);
  52. OSErr        quitAppEventHandler                    (AppleEvent *,AppleEvent *,SInt32);
  53. void        doEvents                                        (EventRecord *);
  54. void        doDisplayInformation                (WindowRef);
  55. Boolean    doCheckMonitor                            (void);
  56. void        doSetMonitorPixelDepth            (SInt16);
  57. void        doRestoreMonitorPixelDepth    (void);
  58. void        doMonitorAlert                            (Str255);
  59.  
  60. // ************************************************************************************** main
  61.  
  62. void  main(void)
  63. {
  64.     MenuBarHandle    menubarHdl;
  65.     SInt32                response;
  66.     MenuRef                menuRef;
  67.     WindowRef            windowRef;
  68.     SInt16                entries = 0;
  69.     Str255                theString;
  70.     EventRecord        EventStructure;
  71.  
  72.     // ……………………………………………………………………………………………………………………………………………………………………………………………… do preliminaries
  73.  
  74.     doPreliminaries();
  75.     
  76.     // ……………………………………………………………………………………………………………………………………………………………………… set up menu bar and menus
  77.     
  78.     menubarHdl = GetNewMBar(rMenubar);
  79.     if(menubarHdl == NULL)
  80.         ExitToShell();
  81.     SetMenuBar(menubarHdl);
  82.     DrawMenuBar();
  83.  
  84.     Gestalt(gestaltMenuMgrAttr,&response);
  85.     if(response & gestaltMenuMgrAquaLayoutMask)
  86.     {
  87.         menuRef = GetMenuRef(mFile);
  88.         if(menuRef != NULL)
  89.         {
  90.             DeleteMenuItem(menuRef,iQuit);
  91.             DeleteMenuItem(menuRef,iQuit - 1);
  92.             DisableMenuItem(menuRef,0);
  93.         }
  94.     }
  95.  
  96.     // ………………………………………………………………………………………………… check if monitor can display at least 16-bit colour
  97.  
  98.     if(!doCheckMonitor())
  99.     {
  100.         GetIndString(theString,rIndexedStrings,sMonitorInadequate);
  101.         doMonitorAlert(theString);
  102.     }
  103.  
  104.     // …………………………………………………………………………………………………… open windows, set font, show windows, move windows
  105.  
  106.     if(!(windowRef = GetNewCWindow(rWindow,NULL,(WindowRef)-1)))
  107.         ExitToShell();
  108.  
  109.     SetPortWindowPort(windowRef);
  110.     TextSize(10);
  111.  
  112.     // ………………………………………………………………………………………………………………………………………………………………………………………………… enter eventLoop
  113.  
  114.     gDone = false;
  115.  
  116.     while(!gDone)
  117.     {
  118.         if(WaitNextEvent(everyEvent,&EventStructure,MAX_UINT32,NULL))
  119.             doEvents(&EventStructure);
  120.     }
  121. }
  122.  
  123. // *************************************************************************** doPreliminaries
  124.  
  125. void  doPreliminaries(void)
  126. {
  127.     OSErr    osError;
  128.  
  129.     MoreMasterPointers(32);
  130.     InitCursor();
  131.     FlushEvents(everyEvent,0);
  132.  
  133.     osError = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  134.                                                         NewAEEventHandlerUPP((AEEventHandlerProcPtr) quitAppEventHandler),
  135.                                                         0L,false);
  136.     if(osError != noErr)
  137.         ExitToShell();
  138. }
  139.  
  140. // **************************************************************************** doQuitAppEvent
  141.  
  142. OSErr  quitAppEventHandler(AppleEvent *appEvent,AppleEvent *reply,SInt32 handlerRefcon)
  143. {
  144.     OSErr            osError;
  145.     DescType    returnedType;
  146.     Size            actualSize;
  147.  
  148.     osError = AEGetAttributePtr(appEvent,keyMissedKeywordAttr,typeWildCard,&returnedType,NULL,0,
  149.                                                             &actualSize);
  150.  
  151.     if(osError == errAEDescNotFound)
  152.     {
  153.         gDone = true;
  154.         osError = noErr;
  155.     } 
  156.     else if(osError == noErr)
  157.         osError = errAEParamMissed;
  158.  
  159.     return osError;
  160. }
  161.  
  162. // ********************************************************************************** doEvents
  163.  
  164. void    doEvents(EventRecord *eventStrucPtr)
  165. {
  166.     SInt32                    menuChoice;
  167.     MenuID                    menuID;
  168.     MenuItemIndex        menuItem;
  169.     WindowPartCode    partCode;
  170.     WindowRef                windowRef;
  171.     Rect                        portRect;
  172.         
  173.     switch(eventStrucPtr->what)
  174.     {
  175.         case kHighLevelEvent:
  176.             AEProcessAppleEvent(eventStrucPtr);
  177.             break;
  178.  
  179.         case keyDown:
  180.             if((eventStrucPtr->modifiers & cmdKey) != 0)
  181.             {
  182.                 menuChoice = MenuEvent(eventStrucPtr);
  183.                 menuID = HiWord(menuChoice);
  184.                 menuItem = LoWord(menuChoice);
  185.                 if(menuID == mFile && menuItem  == iQuit)
  186.                     gDone = true;
  187.             }
  188.             break;
  189.  
  190.         case mouseDown:
  191.             if(partCode = FindWindow(eventStrucPtr->where,&windowRef))
  192.             {
  193.                 switch(partCode)
  194.                 {
  195.                     case inMenuBar:
  196.                         menuChoice = MenuSelect(eventStrucPtr->where);
  197.                         menuID = HiWord(menuChoice);
  198.                         menuItem = LoWord(menuChoice);
  199.  
  200.                         if(menuID == 0)
  201.                             return;
  202.  
  203.                         switch(menuID)
  204.                         {
  205.                             case mAppleApplication:
  206.                                 if(menuItem == iAbout)
  207.                                     SysBeep(10);
  208.                                 break;
  209.  
  210.                             case mFile:
  211.                                 if(menuItem == iQuit)
  212.                                     gDone = true;
  213.                                 break;
  214.     
  215.                             case mDemonstration:
  216.                                 if(menuItem == iSetDepth8)
  217.                                     doSetMonitorPixelDepth(8);
  218.                                 else if(menuItem == iSetDepth16)
  219.                                     doSetMonitorPixelDepth(16);
  220.                                 else if(menuItem == iSetDepth32)
  221.                                     doSetMonitorPixelDepth(32);
  222.                                 else if(menuItem == iRestoreStartDepth)
  223.                                     doRestoreMonitorPixelDepth();
  224.                                 break;
  225.                         }
  226.                         HiliteMenu(0);
  227.                         break;
  228.                     
  229.                     case inDrag:
  230.                         DragWindow(windowRef,eventStrucPtr->where,NULL);
  231.                         GetWindowPortBounds(windowRef,&portRect);
  232.                         InvalWindowRect(windowRef,&portRect);
  233.                         break;
  234.                 }
  235.             }
  236.             break;
  237.  
  238.         case updateEvt:
  239.             windowRef = (WindowRef) eventStrucPtr->message;
  240.             BeginUpdate(windowRef);
  241.             SetPortWindowPort(windowRef);
  242.             doDisplayInformation(windowRef);
  243.             EndUpdate(windowRef);
  244.             break;
  245.     }
  246. }
  247.  
  248. // ********************************************************************** doDisplayInformation
  249.  
  250. void  doDisplayInformation(WindowRef windowRef)
  251. {
  252.     RGBColor            whiteColour = { 0xFFFF, 0xFFFF, 0xFFFF };
  253.     RGBColor            blueColour  = { 0x3333, 0x3333, 0x9999 };
  254.     Rect                    portRect;
  255.     GDHandle            deviceHdl;
  256.     SInt16                videoDeviceCount = 0;    
  257.     Str255                theString;
  258.     SInt16                deviceType, pixelDepth, bytesPerRow;
  259.     Rect                    theRect;
  260.     GrafPtr                grafPort;
  261.     PixMapHandle    pixMapHdl;
  262.     CTabHandle        colorTableHdl;
  263.     SInt16                entries = 0, vert = 28, horiz = 250, index = 0;
  264.     RGBColor            getPixelColour,colourTableColour;
  265.  
  266.     RGBForeColor(&whiteColour);
  267.     RGBBackColor(&blueColour);
  268.     GetWindowPortBounds(windowRef,&portRect);
  269.     EraseRect(&portRect);
  270.     QDFlushPortBuffer(GetWindowPort(FrontWindow()),NULL);
  271.  
  272.     // ………………………………………………………………………………………………………………………………………………………………………………………………… Get Device List
  273.  
  274.     deviceHdl = GetDeviceList();
  275.  
  276.     // ...................................................... count video devices in device list
  277.  
  278.     while(deviceHdl != NULL)
  279.     {
  280.         if(TestDeviceAttribute(deviceHdl,screenDevice))
  281.             videoDeviceCount ++;
  282.  
  283.         deviceHdl = GetNextDevice(deviceHdl);
  284.     }
  285.  
  286.     NumToString(videoDeviceCount,theString);
  287.     MoveTo(10,20);
  288.     DrawString(theString);
  289.     if(videoDeviceCount < 2)
  290.         DrawString("\p video device in the device list.");
  291.     else
  292.         DrawString("\p video devices in the device list.");
  293.  
  294.     // ………………………………………………………………………………………………………………………………………………………………………………………………… Get Main Device
  295.  
  296.     deviceHdl = GetMainDevice();
  297.  
  298.     // ................................................................... determine device type
  299.  
  300.     MoveTo(10,35);
  301.  
  302.     if(((1 << gdDevType) & (*deviceHdl)->gdFlags) != 0)
  303.         DrawString("\pThe main video device is a colour device.");
  304.     else
  305.         DrawString("\pThe main video device is a monochrome device.");
  306.  
  307.     MoveTo(10,50);
  308.     deviceType = (*deviceHdl)->gdType;
  309.     switch(deviceType)
  310.     {
  311.         case clutType:
  312.             DrawString("\pIt is an indexed device with variable CLUT.");
  313.             break;
  314.  
  315.         case fixedType:
  316.             DrawString("\pIt is is an indexed device with fixed CLUT.");
  317.             break;
  318.  
  319.         case directType:
  320.             DrawString("\pIt is a direct device.");
  321.             break;
  322.     }
  323.  
  324.     // …………………………………………………………………………………………………………………………………………………………………………… Get Handle to Pixel Map
  325.  
  326.     grafPort = GetWindowPort(windowRef);
  327.     pixMapHdl = GetPortPixMap(grafPort);
  328.     // pixMapHdl = (*deviceHdl)->gdPMap; // alternative method
  329.  
  330.     // ............................................................. get and display pixel depth
  331.  
  332.     MoveTo(10,70);
  333.     DrawString("\pPixel depth = ");
  334.  
  335.     pixelDepth = GetPixDepth(pixMapHdl);
  336.     // pixelDepth = (*(*deviceHdl)->gdPMap)->pixelSize;  // alternative method
  337.  
  338.     NumToString(pixelDepth,theString);
  339.     DrawString(theString);
  340.  
  341.     // ........................................................... get and display bytes per row 
  342.  
  343.     MoveTo(10,90);
  344.     bytesPerRow = (*pixMapHdl)->rowBytes & 0x7FFF;
  345.     DrawString("\pBytes per row = ");
  346.     NumToString(bytesPerRow,theString);
  347.     DrawString(theString);
  348.  
  349.     // …………………………………………………………………………………………………………………………………… Get Device's Global Boundary Rectangle
  350.  
  351.     theRect = (*deviceHdl)->gdRect;
  352.  
  353.     // ........................................... calculate and display total pixel image bytes
  354.  
  355.     MoveTo(10,105);
  356.     DrawString("\pTotal pixel image bytes = ");
  357.     NumToString(bytesPerRow * theRect.bottom,theString);
  358.     DrawString(theString);
  359.  
  360.     // ..................................................... display device's boundary rectangle
  361.  
  362.     MoveTo(10,130);
  363.     TextFace(bold);
  364.     DrawString("\pGraphics Device's Boundary Rectangle");
  365.     TextFace(normal);
  366.     MoveTo(10,145);
  367.     DrawString("\p(gdRect field of GDevice structure)");
  368.  
  369.     MoveTo(10,160);
  370.     DrawString("\pBoundary rectangle top = ");
  371.     NumToString(theRect.top,theString);
  372.     DrawString(theString);
  373.  
  374.     MoveTo(10,175);
  375.     DrawString("\pBoundary rectangle left = ");
  376.     NumToString(theRect.left,theString);
  377.     DrawString(theString);
  378.  
  379.     MoveTo(10,190);
  380.     DrawString("\pBoundary rectangle bottom = ");
  381.     NumToString(theRect.bottom,theString);
  382.     DrawString(theString);
  383.  
  384.     MoveTo(10,205);
  385.     DrawString("\pBoundary rectangle right = ");
  386.     NumToString(theRect.right,theString);
  387.     DrawString(theString);
  388.  
  389.     // ……………………………………………………………………………………………………………… Get and Display Pixel Map's Boundary Rectangle
  390.  
  391.     GetPixBounds(pixMapHdl,&theRect);
  392.  
  393.     MoveTo(10,225);
  394.     TextFace(bold);
  395.     DrawString("\pPixel Map's Boundary Rectangle");
  396.     TextFace(normal);
  397.     MoveTo(10,240);
  398.     DrawString("\p(bounds field of PixMap structure)");
  399.  
  400.     MoveTo(10,255);
  401.     DrawString("\pBoundary rectangle top = ");
  402.     NumToString(theRect.top,theString);
  403.     DrawString(theString);
  404.  
  405.     MoveTo(10,270);
  406.     DrawString("\pBoundary rectangle left = ");
  407.     NumToString(theRect.left,theString);
  408.     DrawString(theString);
  409.  
  410.     MoveTo(10,285);
  411.     DrawString("\pBoundary rectangle bottom = ");
  412.     NumToString(theRect.bottom,theString);
  413.     DrawString(theString);
  414.  
  415.     MoveTo(10,300);
  416.     DrawString("\pBoundary rectangle right = ");
  417.     NumToString(theRect.right,theString);
  418.     DrawString(theString);
  419.  
  420.     MoveTo(10,320);
  421.     DrawString("\pOn Mac OS X, drag window after pixel depth and screen resolution changes to");
  422.     DrawString("\p ensure that");
  423.     MoveTo(10,333);
  424.     DrawString("\pbytes per row, pixel image bytes, and colour values are updated.");
  425.  
  426.     // ……………………………………………………………………… Get and Display RGB Components of Requested Background Colour
  427.  
  428.     MoveTo(250,255);
  429.     GetBackColor(&blueColour);
  430.     DrawString("\pRequested background colour (rgb) = ");
  431.     MoveTo(250,270);
  432.     NumToString(blueColour.red,theString);
  433.     DrawString(theString);
  434.     DrawString("\p  ");
  435.     NumToString(blueColour.green,theString);
  436.     DrawString(theString);
  437.     DrawString("\p  ");
  438.     NumToString(blueColour.blue,theString);
  439.     DrawString(theString);
  440.  
  441.     // …………………… If Direct Device, Get and Display RGB Components of Colour Returned by GetCPixel
  442.  
  443.     if(deviceType == directType)
  444.     {
  445.         MoveTo(250,285);
  446.         GetCPixel(10,10,&getPixelColour);
  447.         DrawString("\pColour returned by CetCPixel (rgb) = ");
  448.         MoveTo(250,300);
  449.         NumToString(getPixelColour.red,theString);
  450.         DrawString(theString);
  451.         DrawString("\p  ");
  452.         NumToString(getPixelColour.green,theString);
  453.         DrawString(theString);
  454.         DrawString("\p  ");
  455.         NumToString(getPixelColour.blue,theString);
  456.         DrawString(theString);
  457.     }
  458.  
  459.     // .............................................. else prepare to display colour table index
  460.  
  461.     else
  462.     {
  463.         MoveTo(250,285);
  464.         DrawString("\pBackground colour (colour table index):");
  465.     }
  466.  
  467.     // …………………………………………………………………………………………………………………………………………………………………… Get Handle to Colour Table
  468.  
  469.     colorTableHdl = (*pixMapHdl)->pmTable;
  470.  
  471.     // ........................................ if any entries in colour table, draw the colours
  472.  
  473.     MoveTo(250,20);
  474.     DrawString("\pColour table:");
  475.  
  476.     entries = (*colorTableHdl)->ctSize;
  477.  
  478.     if(entries < 2)
  479.     {
  480.         MoveTo(260,100);
  481.         DrawString("\pOnly one (dummy) entry in the colour");
  482.         MoveTo(260,115);
  483.         DrawString("\ptable.  To cause the colour table to be");
  484.         MoveTo(260,130);
  485.         DrawString("\pbuilt, set the monitor to bit depth 8");
  486.         MoveTo(260,145);
  487.         DrawString("\p(256 colours), causing it to act like ");
  488.         MoveTo(260,160);
  489.         DrawString("\pan indexed device.");
  490.         SetRect(&theRect,250,28,458,236);
  491.         FrameRect(&theRect);
  492.     }
  493.  
  494.     for(index = 0;index <= entries;index++)
  495.     {
  496.         SetRect(&theRect,horiz,vert,horiz+12,vert+12);
  497.         colourTableColour = (*colorTableHdl)->ctTable[index].rgb;
  498.         RGBForeColor(&colourTableColour);
  499.         PaintRect(&theRect);
  500.  
  501.         // .... also, if device is not a  direct device, and current colour matches background ...
  502.  
  503.         if(deviceType == clutType || deviceType == fixedType)
  504.         {
  505.             if(colourTableColour.red == blueColour.red && 
  506.                  colourTableColour.green == blueColour.green && 
  507.                  colourTableColour.blue == blueColour.blue)
  508.             {
  509.  
  510.                 // ....................... outline the drawn colour and display the colour table index
  511.  
  512.                 RGBForeColor(&whiteColour);
  513.                 InsetRect(&theRect,-1,-1);
  514.                 FrameRect(&theRect);
  515.                 MoveTo(250,300);
  516.                 NumToString(index,theString);
  517.                 DrawString(theString);
  518.             }
  519.         }
  520.  
  521.         horiz += 13;
  522.         if(horiz > 445)
  523.         {
  524.             horiz = 250;
  525.             vert += 13;
  526.         }
  527.     }
  528.  
  529.     QDFlushPortBuffer(GetWindowPort(FrontWindow()),NULL);
  530. }
  531.  
  532. // **************************************************************************** doCheckMonitor
  533.  
  534. Boolean  doCheckMonitor(void)
  535. {
  536.     GDHandle    mainDeviceHdl;
  537.  
  538.     mainDeviceHdl = GetMainDevice();
  539.  
  540.     if(!(HasDepth(mainDeviceHdl,16,gdDevType,1)))
  541.     {
  542.         DisableMenuItem(GetMenuRef(mDemonstration),0);
  543.         return false;
  544.     }
  545.     else
  546.     {
  547.         gStartupPixelDepth = (**((**mainDeviceHdl).gdPMap)).pixelSize;
  548.         return true;
  549.     }
  550. }
  551.  
  552. // ******************************************************************** doSetMonitorPixelDepth
  553.  
  554. void  doSetMonitorPixelDepth(SInt16 requiredDepth)
  555. {
  556.     GDHandle    mainDeviceHdl;
  557.     Str255        alertString;    
  558.     SInt16        currentPixelDepth;
  559.  
  560.     mainDeviceHdl = GetMainDevice();
  561.     currentPixelDepth = (**((**mainDeviceHdl).gdPMap)).pixelSize;
  562.  
  563.     if(currentPixelDepth != requiredDepth)
  564.     {
  565.         SetDepth(mainDeviceHdl,requiredDepth,gdDevType,1);
  566.     }
  567.     else
  568.     {
  569.         GetIndString(alertString,rIndexedStrings,sMonitorAtThatDepth);
  570.         doMonitorAlert(alertString);
  571.     }
  572. }
  573.  
  574. // **************************************************************** doRestoreMonitorPixelDepth
  575.  
  576. void  doRestoreMonitorPixelDepth(void)
  577. {
  578.     GDHandle    mainDeviceHdl;
  579.     Str255        alertString;    
  580.     SInt16        pixelDepth;
  581.  
  582.     mainDeviceHdl = GetMainDevice();
  583.     pixelDepth = (**((**mainDeviceHdl).gdPMap)).pixelSize;
  584.  
  585.     if(pixelDepth != gStartupPixelDepth)
  586.     {
  587.         GetIndString(alertString,rIndexedStrings,sRestoringMonitor);
  588.         doMonitorAlert(alertString);
  589.         SetDepth(mainDeviceHdl,gStartupPixelDepth,gdDevType,1);
  590.     }
  591.     else
  592.     {
  593.         GetIndString(alertString,rIndexedStrings,sMonitorAtStartDepth);
  594.         doMonitorAlert(alertString);
  595.     }
  596. }
  597.  
  598. // **************************************************************************** doMonitorAlert
  599.  
  600. void  doMonitorAlert(Str255 labelText)
  601. {
  602.     SInt16    itemHit;
  603.  
  604.     StandardAlert(kAlertNoteAlert,labelText,NULL,NULL,&itemHit);
  605. }
  606.  
  607. // *******************************************************************************************
  608.