home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / ncsat.cpt / Telnet2.5 final / vr / vdevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-23  |  5.8 KB  |  215 lines

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