home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / DataScope 2.0.3 / DataScope2l / DSSource / vdevice.c < prev   
Encoding:
Text File  |  1994-05-04  |  5.6 KB  |  183 lines  |  [TEXT/MPS ]

  1. ce()       set the color palette for the 8-bit vdevice.
  2. */
  3. GDHandle v_savegd;        /* the saved gdevice */
  4. CGrafPtr v_saveport;    /* the saved port from SetPort */
  5.  
  6. /*************************************************************************/
  7. /* InitVDevice
  8. *  allocate an off-screen pixmap and off-screen gdevice which can be drawn
  9. *  in without the palette manager affecting the colors.  If we use MakeITable
  10. *  ourselves and don't install the gdevice into the gdevice list or make it
  11. *  active, then the palette manager ignores it, but QuickDraw works on it.
  12. *
  13. *  set vdev->bounds to the size you need before calling.
  14. *  allocate vdev->bp enough space to hold one byte per pixel of a rectangle that
  15. *  size.
  16. *
  17. *  returns 0 if ok,
  18. *  returns -1 or other negative on other errors.
  19. */
  20. InitVDevice(vdev)
  21.     VDevicePtr vdev;
  22. {
  23.     GDHandle thegd;
  24.     PixMapHandle pm;
  25.     Rect tr;
  26.     CTabHandle ct;
  27.     int width;
  28.  
  29.     GetPort((GrafPtr *)&v_saveport);
  30.     v_savegd = GetGDevice();            /* get old values */
  31.     
  32.     tr = vdev->bounds;                    /* get size of device to create */
  33.     if (tr.right - tr.left < 1 ||
  34.         tr.bottom - tr.top < 1)
  35.         return(-1);                        /* check for simple mistake */
  36.         
  37.     width = tr.right - tr.left;
  38.     
  39.     if (!vdev->bp)
  40.         return(-2);                        /* another simple mistake */
  41. /*
  42. *  Allocate the off-screen PixMap for drawing a duplicate copy.  The off-screen
  43. *  gdevice, CPort and PixMap form a virtual drawing space with its own color map.
  44. *  For this program, we have chosen to make this virtual space an 8-bit drawing
  45. *  device.  When drawing, use SetVDevice and UnsetVDevice to turn on and off.
  46. */
  47.     thegd = vdev->vgd = NewGDevice( 0, -1 );
  48.     
  49.     pm = (PixMapHandle) NewHandle(sizeof(PixMap));
  50.  
  51.     if (width & 1)    {                    /* must be even */
  52.         --tr.right;
  53.         --width;
  54.     }
  55.     (*pm)->baseAddr = vdev->bp;            /* get memory to use */
  56.     (*pm)->rowBytes = width | 0x8000;    /* setting high flag bit */
  57.     (*pm)->bounds = tr;
  58.     (*pm)->pixelSize = 8;                /* source is 8-bits */
  59.     (*pm)->pixelType = 0;                /* chunky */
  60.     (*pm)->cmpCount = 1;                /* chunky */
  61.     (*pm)->cmpSize = 8;                    /* chunky */
  62.     (*pm)->hRes = 72;
  63.     (*pm)->vRes = 72;                    /* resolution = 72dpi */
  64.     (*pm)->planeBytes = 0;                /* chunky */
  65.     (*pm)->pmVersion = 0;                /* chunky */
  66.     (*pm)->packSize = 0;                /* chunky */
  67.     (*pm)->packType = 0;                /* chunky */
  68.     ct = (*pm)->pmTable = (CTabHandle)NewHandle(sizeof(ColorTable));
  69.     (*ct)->ctSeed = GetCTSeed();
  70.     (*ct)->ctFlags = 0x8000;
  71.     (*ct)->ctSize = 1;                    /* 1-length color table (empty) */
  72.     
  73.     (*thegd)->gdResPref = 3;            /* inverse table size preferred */
  74.     (*thegd)->gdType = 0;                /* type = CLUT type */
  75.     (*thegd)->gdRect = tr;                /* device size boundary */
  76.  
  77.     (*thegd)->gdPMap = pm;                /* copy pixmap handle */
  78.     
  79.     SetDeviceAttribute(thegd,noDriver,true);
  80.     SetDeviceAttribute(thegd,gdDevType,true);    /* set to color, not monochrome */
  81.     
  82.     SetGDevice(thegd);
  83.                         /* CPort inherits from the gdevice, including pixmap */
  84.     OpenCPort(&vdev->vport);            /* initialize the port struct */
  85.     SetPort((GrafPtr)&vdev->vport);
  86.     ClipRect(&tr);                        /* set clip region */
  87.  
  88. /*
  89. *  Erase the image region in the virtual device. 
  90. */
  91.     PaintRect(&tr);
  92.  
  93. /*
  94. *  Restore the environment.
  95. */
  96.     SetGDevice(v_savegd);
  97.     SetPort((GrafPtr) v_saveport);
  98.     
  99.     return(0);
  100. }
  101.  
  102. /*******************************************************************************/
  103. /* SetVDevice
  104. *  Set the gdevice and port to our off-screen space.
  105. *  Save the old values for unset.
  106. */
  107. SetVDevice(vdev)
  108.     VDevicePtr vdev;
  109. {
  110.  
  111.     GetPort((GrafPtr *)&v_saveport);
  112.     v_savegd = GetGDevice();
  113.     if (!vdev->vgd)
  114.         return(-1);
  115.     SetGDevice(vdev->vgd);
  116.     SetPort((GrafPtr) &vdev->vport);
  117.     
  118.     return(0);
  119. }
  120.  
  121. /*******************************************************************************/
  122. /* UnsetVDevice
  123. *  Set the vdevice back to the saved values.
  124. */
  125. UnsetVDevice()
  126. {
  127.     SetGDevice(v_savegd);
  128.     SetPort((GrafPtr) v_saveport);
  129. }
  130.  
  131. /*******************************************************************************/
  132. /* TrashVDevice
  133. *  Get rid of the devices that we created with InitVDevice.
  134. *
  135. *  Remember to free up the vdev->bp after the gdevice is gone.
  136. */
  137. TrashVDevice(vdev)
  138.     VDevicePtr vdev;
  139. {
  140.     
  141.     (*(*vdev->vgd)->gdPMap)->baseAddr = NULL;    /* drop old value, bp has a copy */
  142.     
  143.     /* dispose of the color table. CloseCPort seems not to want to do that. */
  144.     
  145.     DisposHandle ((Handle) (**(**vdev->vgd).gdPMap).pmTable);
  146.     
  147.     CloseCPort(&vdev->vport);        /* lose the cport, disposes the pixmap */
  148.     (*vdev->vgd)->gdPMap = NULL;    /* destroy second copy of the pixmaphandle */
  149.     DisposGDevice (vdev->vgd);        /* disposes current gdevice and pixmap */
  150.     DisposPtr ((Ptr) vdev->bp);
  151.  
  152. }
  153.  
  154. /*******************************************************************************/
  155. /*  ColorVDevice
  156. *   input:  vdev and 
  157. *        palette handle.
  158. *   
  159. *   Use palette2ctab to install the color table into the off-screen gdevice and
  160. *   make the inverse color table for it.  Also install the palette into the current
  161. *   window.
  162. */
  163. ColorVDevice(vdev,pal)
  164.     VDevicePtr vdev;
  165.     PaletteHandle pal;
  166. {
  167.     CTabHandle ct;
  168.  
  169.     ct = (*vdev->vport.portPixMap)->pmTable;
  170.     if (!ct)
  171.         return;
  172.  
  173.     Palette2CTab( pal, ct );
  174.  
  175.     (*ct)->ctSeed = GetCTSeed();                    /* give it a unique seed */
  176.     (*ct)->ctFlags = 0  /* 0x8000 */;
  177.     
  178.     MakeITable( ct, (*vdev->vgd)->gdITable, 3 );    /* 3-bit inverse table  */
  179.  
  180. }
  181.  
  182.