home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / Imageer 1.0.0d3 / source / offscrnGraphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-16  |  8.9 KB  |  2 lines  |  [TEXT/MPS ]

  1. /****************************************************/
  2. /*                                                    */
  3. /*    File:        offscrnGraphics.c                    */
  4. /*                                                    */
  5. /*    Program:    Imageer                                */
  6. /*                                                    */
  7. /*    By:            Jason Hodges-Harris                    */
  8. /*                                                    */
  9. /*    Created:    26/10/95  00:00:00 AM                */
  10. /*                                                    */
  11. /*    Version:    1.0.0d3                                */
  12. /*                                                    */
  13. /*    Copyright:    © 1995-96 Apple Computer, Inc.,        */ 
  14. /*                    all rights reserved.            */        
  15. /*                                                    */
  16. /****************************************************/
  17.  
  18.  
  19. /**** Macintosh Toolbox Headers *****/
  20.  
  21. #ifndef __PALETTES__
  22. #include <Palettes.h>
  23. #endif
  24.  
  25. #ifndef __QDOFFSCREEN__
  26. #include <QDOffscreen.h>
  27. #endif
  28.  
  29. #ifndef __TYPES__
  30. #include <Types.h>
  31. #endif
  32.  
  33.  
  34. /****   Application headers and prototypes   ****/
  35.  
  36.  
  37. #ifndef __IMAGEERAPPHEADER__
  38. #include "Imageer.app.h"
  39. #endif
  40.  
  41. #ifndef __IMAGEERPROTOSHEADER__
  42. #include "Imageer.protos.h"
  43. #endif
  44.  
  45.  
  46. /****    Default RGB Colors    ****/
  47.  
  48. static const RGBColor     kRGBBlack = {0x0000, 0x0000, 0x0000};
  49. static const RGBColor    kRGBWhite = {0xFFFF, 0xFFFF, 0xFFFF};
  50.  
  51.  
  52. /**** Create GWorld attached to the image window document structure    ****/
  53.  
  54. #pragma segment ImageWorld
  55. GWorldPtr    CreateOffscreen (CTabHandle theCTabHndl, short theXsize, short theYsize, short theBitDepth,GWorldFlags theFlags)
  56. {
  57.     GWorldPtr    theGWorldPtr = nil;
  58.     QDErr        theError;
  59.     Rect        theRect;
  60.     
  61.     SetRect(&theRect,0,0,theXsize,theYsize);
  62.     theError = NewGWorld (&theGWorldPtr,theBitDepth,&theRect,theCTabHndl,nil,theFlags);
  63.     if (theError)
  64.     {
  65.         DisplayAlert (rGenAlert,rQDMessages,iFailGWorldAlloc);
  66.         return nil;
  67.     }
  68.     return theGWorldPtr;
  69. }
  70.  
  71.  
  72. /**** Draw image palette colors    ****/
  73.  
  74. #pragma segment ImageWorld
  75. OSErr        DrawImageColors(WindowPtr    theWindow)
  76. {
  77.     ImageDocHndl    theColorsDocHndl = nil,
  78.                     theFrontDocHndl = nil;
  79.     PaletteHandle    windowPalette;
  80.     WindowPtr        frontWindow,
  81.                     theNextWindow;
  82.     RGBColor        oldForeColor,
  83.                     newForeColor;
  84.     Rect            colorRect;
  85.     short            countX,
  86.                     countY,
  87.                     colorIndex = 0,
  88.                     numberColors;
  89.     OSErr            error = noErr;
  90.     Boolean            isImageWindow = false;
  91.     
  92.     GetForeColor (&oldForeColor);
  93.     frontWindow = FrontWindow();
  94.     while (!isImageWindow && frontWindow)
  95.     {
  96.         theFrontDocHndl = (ImageDocHndl)GetWRefCon(theWindow);
  97.         if (!(**theFrontDocHndl).isColorsWindow)
  98.         {
  99.             isImageWindow = true;
  100.         }
  101.         else
  102.         {
  103.             theNextWindow = &((WindowPeek)frontWindow)->nextWindow->port;
  104.             frontWindow = theNextWindow;
  105.         }
  106.     }
  107.     theColorsDocHndl = (ImageDocHndl)GetWRefCon(theWindow);
  108.     if (theColorsDocHndl)
  109.     {
  110.         switch ((**theColorsDocHndl).theImageDepth)
  111.         {
  112.             case    1:
  113.                 DisplayAlert (rGenWarning,rErrMessages,iNotImplemented);
  114.             break;
  115.             case    2:
  116.             windowPalette = (**theColorsDocHndl).theColorsPalette;
  117.             numberColors = (**windowPalette).pmEntries;
  118.             for (countX = 0;countX<128;countX+=32)
  119.             {
  120.                 GetEntryColor (windowPalette, colorIndex, &newForeColor);
  121.                 SetRect(&colorRect,16+ countX,16,countX+20,52);
  122.                 RGBForeColor(&newForeColor);
  123.                 PaintRect(&colorRect);
  124.                 colorIndex++;
  125.             }
  126.             RGBForeColor(&oldForeColor);
  127.             return error;
  128.             break;
  129.             case    4:
  130.                 windowPalette = (**theColorsDocHndl).theColorsPalette;
  131.                 numberColors = (**windowPalette).pmEntries;
  132.                 for (countY = 0;countY<32;countY+=16)
  133.                 {
  134.                     for (countX = 0;countX<128;countX+=32)
  135.                     {
  136.                         GetEntryColor (windowPalette, colorIndex, &newForeColor);
  137.                         SetRect(&colorRect,16+ countX,16+countY,countX+20,32+countY);
  138.                         RGBForeColor(&newForeColor);
  139.                         PaintRect(&colorRect);
  140.                         colorIndex++;
  141.                     }
  142.                 }
  143.                 RGBForeColor(&oldForeColor);
  144.                 return error;
  145.             break;
  146.             case    8:
  147.                 windowPalette = (**theColorsDocHndl).theColorsPalette;
  148.                 numberColors = (**windowPalette).pmEntries;
  149.                 for (countY = 0;countY<32;countY+=4)
  150.                 {
  151.                     for (countX = 0;countX<128;countX+=4)
  152.                     {
  153.                         GetEntryColor (windowPalette, colorIndex, &newForeColor);
  154.                         SetRect(&colorRect,16+ countX,16+ countY,countX+20,countY+20);
  155.                         RGBForeColor(&newForeColor);
  156.                         PaintRect(&colorRect);
  157.                         colorIndex++;
  158.                     }
  159.                 }
  160.                 RGBForeColor(&oldForeColor);
  161.                 return error;
  162.             break;
  163.             case    16:
  164.             case    32:
  165.                 for (countY = 0;countY<4;countY++)
  166.                 {
  167.                     for (countX = 0;countX<128;countX+=2)
  168.                     {
  169.                         SetRect(&colorRect,16+ countX,16+(countY*8),countX+18,24+(countY*8));
  170.                         switch (countY)
  171.                         {
  172.                             case 0:
  173.                                 newForeColor.red = countX << 9;
  174.                                 newForeColor.green = countX << 9;
  175.                                 newForeColor.blue = countX << 9;
  176.                                 RGBForeColor(&newForeColor);
  177.                                 PaintRect(&colorRect);
  178.                             break;
  179.                             case 1:
  180.                                 newForeColor.red = countX << 9;
  181.                                 newForeColor.green = 0;
  182.                                 newForeColor.blue = 0;
  183.                                 RGBForeColor(&newForeColor);
  184.                                 PaintRect(&colorRect);
  185.                             break;
  186.                             case 2:
  187.                                 newForeColor.red = 0;
  188.                                 newForeColor.green = countX << 9;
  189.                                 newForeColor.blue = 0;
  190.                                 RGBForeColor(&newForeColor);
  191.                                 PaintRect(&colorRect);
  192.                             break;
  193.                             case 3:
  194.                                 newForeColor.red = 0;
  195.                                 newForeColor.green = 0;
  196.                                 newForeColor.blue = countX << 9;
  197.                                 RGBForeColor(&newForeColor);
  198.                                 PaintRect(&colorRect);
  199.                             break;
  200.                         }
  201.                     }
  202.                 }
  203.                 RGBForeColor(&oldForeColor);
  204.                 return error;
  205.             break;
  206.         }
  207.     }
  208.     return kDefaultAppError;
  209. }
  210.  
  211.  
  212. /****    Force the colortable to be reset    ****/
  213.  
  214. #pragma segment ImageWorld
  215. void    ForceRestoreColors(void)
  216. {
  217.     RestoreDeviceClut(nil);
  218.     PaintBehind(nil, GetGrayRgn());
  219.     DrawMenuBar();
  220. }
  221.  
  222.  
  223. /****    Transfer visible portion of GWorld to current window    ****/
  224.  
  225. #pragma segment ImageWorld
  226. OSErr    TransferImageToWindow(ImageDocHndl theWindDocHndl, WindowPtr theWindow)
  227. {
  228.     PixMapHandle    thePixMapHndl = nil;
  229.     CGrafPtr        oldPort;
  230.     GDHandle        oldGDev;
  231.     RGBColor        oldForeColor,
  232.                     oldBackColor;
  233.     Rect            sourceRect,
  234.                     destRect;
  235.     OSErr            error = noErr;
  236.     
  237.     GetGWorld(&oldPort,&oldGDev);
  238.     SetGWorld((**theWindDocHndl).theImageWorld,nil);
  239.     GetForeColor(&oldForeColor);
  240.     GetBackColor(&oldBackColor);
  241.     RGBForeColor(&kRGBBlack);
  242.     RGBBackColor(&kRGBWhite);
  243.     thePixMapHndl = GetGWorldPixMap((**theWindDocHndl).theImageWorld);
  244.     if (PixMap32Bit(thePixMapHndl))                    // if 32bit mode needed == true
  245.         SaveSetMMUMode(true);
  246.     if (!LockPixels(thePixMapHndl))
  247.         error = kFailedLockPixels;
  248.     if (!error)
  249.     {
  250.         sourceRect = (**theWindDocHndl).theImageWorld->portRect;
  251.         destRect = theWindow->portRect;
  252.         destRect.right -= kScrollBarWidth;    // resize port rect to exclude controller area
  253.         destRect.bottom -= kScrollBarWidth;
  254.         if (!EqualRect(&destRect,&sourceRect))
  255.             sourceRect = *CalcVisibleImage(theWindDocHndl,&destRect);
  256.         SetGWorld(oldPort,oldGDev);
  257.         CopyBits((BitMap*)(*thePixMapHndl),
  258.                  (BitMap*)&((*theWindow).portBits),
  259.                  &sourceRect,&destRect,srcCopy,nil);
  260.     }
  261.     UnlockPixels(thePixMapHndl);        // unlock GWorld
  262.     SaveSetMMUMode(false);
  263.     return error;
  264. }
  265.  
  266.  
  267. /****    Calculate the visible area of window contents region ****/
  268.  
  269. #pragma segment ImageWorld
  270. Rect*    CalcVisibleImage(ImageDocHndl theWindDocHndl,const Rect *destRect)
  271. {
  272.     Rect    displayRect;
  273.     short    theXvalue,
  274.             theYvalue;
  275.  
  276.     theXvalue = GetControlValue((**theWindDocHndl).theHScrollBar);
  277.     theYvalue = GetControlValue((**theWindDocHndl).theVScrollBar);
  278.     SetRect(&displayRect,theXvalue,theYvalue,destRect->right+theXvalue,destRect->bottom+theYvalue);
  279.     return &displayRect;
  280. }
  281.  
  282.  
  283. /****    Set and Reset the address mode between 24 and 32 bits    ****/
  284.  
  285. #pragma segment ImageWorld
  286. void    SaveSetMMUMode(Boolean isSaveMode)
  287. {
  288.     static signed char        theAddrMode = true32b;
  289.     static Boolean            swapModeNeeded = false;
  290.     
  291.     if (isSaveMode == true)
  292.     {
  293.         if(GetMMUMode() == false32b)                // get current addr mode
  294.         {
  295.             swapModeNeeded = true;                    // in 24 bit mode swap needed
  296.             SwapMMUMode (&theAddrMode);                // switch to 32 bit mode
  297.         }
  298.     }
  299.     else if (swapModeNeeded == true)
  300.         SwapMMUMode (&theAddrMode);
  301. }
  302.  
  303.  
  304. /****    Calc new index depth colors window palette    ****/
  305.  
  306. #pragma segment ImageWorld
  307. OSErr    UpdateColorsWindPalette(ImageDocHndl theImageWindDocHndl, WindowPtr theImageWindow)
  308. {
  309.     ImageDocHndl    theFrntWindHndl;
  310.     GrafPtr            oldPort;
  311.     WindowPtr        theWindow,
  312.                     theNextWindow;
  313.     OSErr            error = noErr;
  314.     
  315.     GetPort(&oldPort);
  316.     theWindow = FrontWindow();
  317.     while (theWindow)
  318.     {
  319.         theFrntWindHndl = (ImageDocHndl)GetWRefCon(theWindow);
  320.         if ((*theFrntWindHndl)->isColorsWindow)
  321.         {
  322.             if ((*theImageWindDocHndl)->theImageDepth != (*theFrntWindHndl)->theImageDepth)
  323.                 (*theFrntWindHndl)->theImageDepth = (*theImageWindDocHndl)->theImageDepth;
  324.             if ((*theFrntWindHndl)->theImageDepth <= 8 && !(*theImageWindDocHndl)->isUsingQDGX)
  325.             {
  326.                 if ((*theFrntWindHndl)->theColorsPalette)
  327.                     DisposePalette((*theFrntWindHndl)->theColorsPalette);
  328.                 (*theFrntWindHndl)->theColorsPalette = 
  329.                     NewPalette(256,
  330.                                (*(*theImageWindDocHndl)->theImageWorld->portPixMap)->pmTable,
  331.                                pmTolerant+pmExplicit,0);
  332.                 if ((*theFrntWindHndl)->theColorsPalette)
  333.                      NSetPalette(theWindow,(*theFrntWindHndl)->theColorsPalette,pmAllUpdates);
  334.                 else
  335.                     error = kFailMakePalette;
  336.             }
  337.             SetPort(theWindow);
  338.             InvalRect(&theWindow->portRect);
  339.             SetPort(oldPort);
  340.             theWindow = nil;
  341.         }
  342.         if (theWindow)
  343.         {
  344.             theNextWindow = &((WindowPeek)theWindow)->nextWindow->port;
  345.             theWindow = theNextWindow;
  346.         }
  347.     }
  348.     return error;
  349. }