home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / srcos2 / vmemdc.cpp < prev    next >
C/C++ Source or Header  |  1999-01-25  |  8KB  |  220 lines

  1. //===============================================================
  2. // vMemoryDC - a memory drawing canvas - Windows
  3. //
  4. // Copyright (C) 1995,1996,1997,1998  Bruce E. Wampler
  5. //
  6. // This file is part of the V C++ GUI Framework, and is covered
  7. // under the terms of the GNU Library General Public License,
  8. // Version 2. This library has NO WARRANTY. See the source file
  9. // vapp.cxx for more complete information about license terms.
  10. //===============================================================
  11. #include <v/vmemdc.h>
  12. #include <v/vapp.h>
  13. //================>>> vMemoryDC::vMemoryDC <<<========================
  14.   vMemoryDC::vMemoryDC(int width, int height)
  15.   {
  16.     SysDebug(Constructor,"vMemoryDC::vMemoryDC() constructor\n")
  17.     // Mem canvases are implemented with bitmaps.
  18.     // The idea is this. You create a bitmap (_memBM) that
  19.     // is compatible with the canvas DC -
  20.     // This gives the bitmap a nice color map. Then, the
  21.     // tricky part. To use a bitmap, you have to select it into
  22.     // a DC. So, you create a new DC (_hdc) , also compatible with
  23.     // the canvas DC. Then you select the bitmap into the new
  24.     // DC. Now you can draw into the new DC.
  25.     // When you are ALL done, finally free the bitmap.
  26.  
  27.     _physHeight = width;
  28.     _physWidth = height;
  29.  
  30.     if (_physWidth == 0)
  31.     _physWidth = theApp->DefaultWidth();
  32.     if (_physHeight == 0)
  33.     _physHeight = theApp->DefaultHeight();
  34.  
  35.     _hdc = 0;
  36.     _memBM = 0;
  37.  
  38.     // this is the contructor for the memory presentation space.  The first
  39.     // thing to do is to create and associate the PS, then set the
  40.     // defaults as needed.
  41.     GetHDC();
  42.   }
  43. //================>>> vMemoryDC::~vMemoryDC <<<========================
  44.   vMemoryDC::~vMemoryDC()
  45.   {
  46.     SysDebug(Destructor,"vMemoryDC::~vMemoryDC() destructor\n")
  47.  
  48.     if (!_memBM)
  49.     return;
  50.  
  51.     // disassociate PS from window, then  destroy PS
  52.     ReleaseHDC();
  53.  
  54.     GpiDeleteBitmap(_memBM);
  55.     _memBM = 0;
  56.   }
  57.  
  58. //====================>>> vMemoryDC::GetHDC <<<========================
  59.   void vMemoryDC::GetHDC() VCONST
  60.   {
  61.     // Create the DC to draw into
  62.  
  63.     // build a memory PS compatible with calling PS
  64.     HPS appDC = WinGetPS(theApp->winHwnd());
  65.     HDC DevCxtComp = GpiQueryDevice(appDC);
  66.  
  67.     _DevCxtMem = DevOpenDC(theApp->AppHab(), OD_MEMORY, "*", 0L, NULL, DevCxtComp);
  68.     SIZEL size = {_physWidth, _physHeight};
  69.     _hdc = GpiCreatePS (theApp->AppHab(), _DevCxtMem, &size,
  70.             PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC );
  71.  
  72.     // set the PS to RGB mode
  73.     GpiCreateLogColorTable(_hdc, 0L, LCOLF_RGB, 0L, 0L, NULL);
  74.  
  75.     // set model space transform to put origin at upper left corner
  76.     // since OS/2 puts the origin in the bottom left by default
  77. /*
  78.     MATRIXLF TransformMatrix;
  79.     POINTL translate, origin;
  80.     FIXED scale[2];
  81.     translate.x = 0;
  82.     translate.y = -_physHeight + 1;
  83.     GpiTranslate(_hdc, &TransformMatrix, TRANSFORM_REPLACE, &translate);
  84.     origin.x = 0; origin.y = 0;
  85.     scale[0] = MAKEFIXED( 1,0);   // x scale
  86.     scale[1] = MAKEFIXED(-1,0);   // y scale
  87.     GpiScale(_hdc, &TransformMatrix, TRANSFORM_ADD, scale, &origin);
  88.     GpiSetModelTransformMatrix(_hdc, 9, &TransformMatrix, TRANSFORM_REPLACE);
  89. */
  90.     // bitmap is never scaled so set scaling to unity
  91.     SetOS2Map(_physHeight, MAKEFIXED(1,0));
  92.  
  93.     // First, create a blank bitmap compatible with the
  94.     // current context that is size of bitmap.
  95.     BITMAPINFOHEADER2 bmih;
  96.     memset(&bmih, 0, sizeof(BITMAPINFOHEADER2));
  97.     bmih.cbFix = sizeof(BITMAPINFOHEADER2);
  98.     bmih.cx = _physWidth;
  99.     bmih.cy = _physHeight;
  100.     bmih.ulColorEncoding = BCE_RGB;
  101.     DevQueryCaps (_hdc, CAPS_COLOR_PLANES, 1, (PLONG) &bmih.cPlanes);
  102.     DevQueryCaps (_hdc, CAPS_COLOR_BITCOUNT, 1, (PLONG) &bmih.cBitCount);
  103.     _memBM = GpiCreateBitmap(_hdc, &bmih, 0L, NULL, NULL);
  104.  
  105.     // now we need to load the bitmap into the memory PS
  106.     // so we can draw into the memory PS with the
  107.     // GPI drawing API.
  108.     GpiSetBitmap(_hdc, _memBM);
  109.  
  110.     // no need to keep the compatibility DC anymore
  111.     WinReleasePS (appDC);
  112.     // We are now ready to use _hdc!
  113.     SysDebug1(Build,"vMemoryDC::GetHDC() _hdc=%u\n", _hdc);
  114.  
  115.   }
  116. //================>>> vMemoryDC::ReleaseHDC <<<========================
  117.   void vMemoryDC::ReleaseHDC() VCONST
  118.   {
  119.     if (_hdc == 0)
  120.     return;
  121.  
  122.     GpiSetBitmap (_hdc, NULLHANDLE);
  123.     GpiAssociate (_hdc, NULLHANDLE);
  124.     GpiDestroyPS (_hdc);
  125.     DevCloseDC (_DevCxtMem);
  126.     _hdc = 0;
  127.   }
  128.  
  129. //=====================>>> vCanvasPaneDC::Clear <<<==========================
  130.   void vMemoryDC::Clear(void)
  131.   {
  132.     // CAUTION: rc coords are inclusive/exclusive
  133.     RECTL rc={0, 0, _physWidth, _physHeight};
  134.     WinFillRect (_hdc, &rc, _canvasBG);
  135.  
  136.     // V to OS/2 Coord Transform Equations:
  137.     // (os/2)  y = -(1 - height + y)  (V)
  138.     //
  139.     // for inclusive/inclusive (ie. Gpi)
  140.     //         y = -(yBottom - yTop + y)
  141.     //  where  height = yTop - yBottom + 1
  142.     //
  143.     // for inclusive/exclusive (ie. Win)
  144.     //         y = -(1 + yBottom - yTop + y)
  145.     //  where    height = yTop - yBottom
  146. /*
  147.     MATRIXLF TransformMatrix;
  148.     POINTL translate, origin;
  149.     FIXED scale[2];
  150.     translate.x = 0;
  151.     translate.y = (rc.yBottom - rc.yTop) + 1;
  152.     GpiTranslate(_hdc, &TransformMatrix, TRANSFORM_REPLACE, &translate);
  153.     origin.x = 0; origin.y = 0;
  154.     scale[0] = MAKEFIXED( 1,0);   // x scale
  155.     scale[1] = MAKEFIXED(-1,0);   // y scale
  156.     GpiScale(_hdc, &TransformMatrix, TRANSFORM_ADD, scale, &origin);
  157.     GpiSetModelTransformMatrix(_hdc, 9, &TransformMatrix, TRANSFORM_REPLACE);
  158. */
  159.     // bitmap is never scaled so set scaling to unity
  160.     SetOS2Map(rc.yTop - rc.yBottom, MAKEFIXED(1,0));
  161.  
  162.   }
  163. //==================>>> vCanvasPaneDC::ClearRect <<<==========================
  164.   void vMemoryDC::ClearRect(int left, int bottom, int width, int height)
  165.   {
  166.     SysDebug(WindowEvents,"vMemoryDC::ClearRect()\n")
  167.  
  168.     // Clear a rectangular area starting at left, bottom of width and height
  169.     if (height == 0 || width == 0)
  170.     return;
  171.     RECTL rc;
  172.     rc.yBottom = bottom; rc.xLeft = left;
  173.     rc.yTop = bottom+height; rc.xRight = left+width;
  174.     WinFillRect (_hdc, &rc, _canvasBG);
  175.  
  176.     // CAUTION: returned rc coords are inclusive/exclusive
  177.     rc.yBottom = 0; rc.xLeft = 0;
  178.     rc.yTop = _physHeight; rc.xRight = _physWidth;
  179.  
  180.     // V to OS/2 Coord Transform Equations:
  181.     // (os/2)  y = -(1 - height + y)  (V)
  182.     //
  183.     // for inclusive/inclusive (ie. Gpi)
  184.     //         y = -(yBottom - yTop + y)
  185.     //  where  height = yTop - yBottom + 1
  186.     //
  187.     // for inclusive/exclusive (ie. Win)
  188.     //         y = -(1 + yBottom - yTop + y)
  189.     //  where    height = yTop - yBottom
  190. /*
  191.     MATRIXLF TransformMatrix;
  192.     POINTL translate, origin;
  193.     FIXED scale[2];
  194.     translate.x = 0;
  195.     translate.y = (rc.yBottom - rc.yTop) + 1;
  196.     GpiTranslate(_hdc, &TransformMatrix, TRANSFORM_REPLACE, &translate);
  197.     origin.x = 0; origin.y = 0;
  198.     scale[0] = MAKEFIXED( 1,0);   // x scale
  199.     scale[1] = MAKEFIXED(-1,0);   // y scale
  200.     GpiScale(_hdc, &TransformMatrix, TRANSFORM_ADD, scale, &origin);
  201.     GpiSetModelTransformMatrix(_hdc, 9, &TransformMatrix, TRANSFORM_REPLACE);
  202. */
  203.     // bitmap is never scaled so set scaling to unity
  204.     SetOS2Map(rc.yTop - rc.yBottom, MAKEFIXED(1,0));
  205.   }
  206.  
  207. //================>>> vCanvasPaneDC::SetBackground <<<==========================
  208.   void vMemoryDC::SetBackground(VCONST vColor& color)
  209.   {
  210.     _canvasBG = color.pixel();        // retrieve X pixel value
  211.     GpiSetBackColor (_hdc, _canvasBG);
  212.     Clear();
  213.   }
  214. //======================>>> vCanvasPaneDC::SetFont <<<===========================
  215.   void vMemoryDC::SetFont(VCONST vFont& vf)
  216.   {
  217.     // Change the font associated with this window.
  218.     _font = vf;
  219.   }
  220.