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

  1. //===============================================================
  2. // vCanvasPaneDC - a basic canvas for drawing
  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.  
  12. #include <v/vos2.h>        // for OS/2 stuff
  13. extern "C"
  14. {
  15.   #include <math.h>
  16. }
  17. #include <v/vcpdc.h>
  18. #include <v/vapp.h>        // need access to the app
  19. #include <v/vcanvas.h>        // our own canvas widget
  20.  
  21. //================>>> vCanvasPaneDC::vCanvasPaneDC <<<========================
  22.   vCanvasPaneDC::vCanvasPaneDC(vCanvasPane* parentPane) : vWinDC()
  23.   {
  24.     SysDebug(Constructor,"vCanvasPaneDC::vCanvasPaneDC() constructor\n")
  25.     _parentPane = parentPane;
  26.     _drawWidget = _parentPane->DrawingWindow();    // client window handle
  27.  
  28.     // this is the contructor for the presentation space.  The first
  29.     // thing to do is to create and associate the PS, then set the
  30.     // defaults as needed.
  31.     GetHDC();
  32.  
  33.     // set the PS to RGB mode
  34.     GpiCreateLogColorTable(_hdc, 0L, LCOLF_RGB, 0L, 0L, NULL);
  35.  
  36.     // set model space transform to put origin at upper left corner
  37.     // since OS/2 puts the origin in the bottom left by default
  38.     // V to OS/2 Coord Transform Equations:
  39.     //
  40.     // (os/2)  y = -(1 - height + y)  (V)
  41.     //
  42.     // for inclusive/inclusive (ie. Gpi*)
  43.     //         y = -(yBottom - yTop + y)
  44.     //  where  height = yTop - yBottom + 1
  45.     //
  46.     // for inclusive/exclusive (ie. Win*)
  47.     //         y = -(1 + yBottom - yTop + y)
  48.     //  where    height = yTop - yBottom
  49. /*
  50.     MATRIXLF TransformMatrix;
  51.     POINTL translate, origin;
  52.     FIXED scale[2];
  53.     translate.x = 0;
  54.     translate.y = 1 -_parentPane->GetHeight();
  55.     GpiTranslate(_hdc, &TransformMatrix, TRANSFORM_REPLACE, &translate);
  56.     origin.x = 0; origin.y = 0;
  57.     scale[0] = MAKEFIXED( 1,0);   // x scale
  58.     scale[1] = MAKEFIXED(-1,0);   // y scale
  59.     GpiScale(_hdc, &TransformMatrix, TRANSFORM_ADD, scale, &origin);
  60.     GpiSetModelTransformMatrix(_hdc, 9, &TransformMatrix, TRANSFORM_REPLACE);
  61. */
  62.     // display is never scaled so set scaling to unity
  63.     SetOS2Map(_parentPane->GetHeight(), MAKEFIXED(1,0));
  64.   }
  65.  
  66. //================>>> vCanvasPaneDC::~vCanvasPaneDC <<<========================
  67.   vCanvasPaneDC::~vCanvasPaneDC()
  68.   {
  69.     SysDebug(Destructor,"vCanvasPaneDC::~vCanvasPaneDC() destructor\n")
  70.     // disassociate PS from window, then  destroy PS
  71.     ReleaseHDC();
  72.   }
  73.  
  74. //================>>> vCanvasPaneDC::GetHDC <<<========================
  75.   void vCanvasPaneDC::GetHDC(void)
  76.   {
  77.     _devCtxt = WinQueryWindowDC(_drawWidget);  // the client window handle
  78.     if (_devCtxt == NULLHANDLE)
  79.       _devCtxt = WinOpenWindowDC(_drawWidget);
  80.     if (_hdc == 0)
  81.     {
  82.       // Create a normal presentation space and associate with window
  83.       _pageSize.cx = 0L;   // using PELS so default pagesize is okay
  84.       _pageSize.cy = 0L;
  85.       _hdc = GpiCreatePS(theApp->AppHab(), _devCtxt, &_pageSize,
  86.         PU_PELS | GPIF_DEFAULT | GPIT_NORMAL | GPIA_ASSOC );
  87.       SysDebug1(Build,"vCanvasPaneDC::GetHDC() _hdc=%u\n", _hdc);
  88.     }
  89.   }
  90.  
  91. //================>>> vCanvasPaneDC::ReleaseHDC <<<========================
  92.   void vCanvasPaneDC::ReleaseHDC(void)
  93.   {
  94.     SysDebug(Build,"vCanvasPaneDC::ReleaseHDC() entry point\n");
  95.     if (_hdc != 0)
  96.     {
  97.       GpiAssociate(_hdc, NULLHANDLE);
  98.       GpiDestroyPS(_hdc);
  99.       _hdc = 0;
  100.     }
  101.   }
  102.  
  103. //=====================>>> vCanvasPaneDC::Clear <<<==========================
  104.   void vCanvasPaneDC::Clear(void)
  105.   {
  106.     RECTL rc;
  107.     BeginPaint();
  108.     // CAUTION: returned coords are inclusive/exclusive
  109.     WinQueryWindowRect(_drawWidget, &rc);
  110.     WinFillRect (_hdc, &rc, _canvasBG);
  111.  
  112.     // update the transformation in case the size of the
  113.     // canvas has been changed.
  114.  
  115.     // V to OS/2 Coord Transform Equations:
  116.     // (os/2)  y = -(1 - height + y)  (V)
  117.     //
  118.     // for inclusive/inclusive (ie. Gpi)
  119.     //         y = -(yBottom - yTop + y)
  120.     //  where  height = yTop - yBottom + 1
  121.     //
  122.     // for inclusive/exclusive (ie. Win)
  123.     //         y = -(1 + yBottom - yTop + y)
  124.     //  where    height = yTop - yBottom
  125.  
  126. /*
  127.     MATRIXLF TransformMatrix;
  128.     POINTL translate, origin;
  129.     FIXED scale[2];
  130.     translate.x = 0;
  131.     translate.y = 1 + rc.yBottom - rc.yTop;
  132.     GpiTranslate(_hdc, &TransformMatrix, TRANSFORM_REPLACE, &translate);
  133.     origin.x = 0; origin.y = 0;
  134.     scale[0] = MAKEFIXED( 1,0);   // x scale
  135.     scale[1] = MAKEFIXED(-1,0);   // y scale
  136.     GpiScale(_hdc, &TransformMatrix, TRANSFORM_ADD, scale, &origin);
  137.     GpiSetModelTransformMatrix(_hdc, 9, &TransformMatrix, TRANSFORM_REPLACE);
  138. */
  139.     // display is never scaled so set scaling to unity
  140.     SetOS2Map(rc.yTop - rc.yBottom, MAKEFIXED(1,0));
  141.  
  142.     EndPaint();
  143.   }
  144.  
  145. //==================>>> vCanvasPaneDC::ClearRect <<<==========================
  146. // note: coords are in V space
  147.   void vCanvasPaneDC::ClearRect(int x, int y, int width, int height)
  148.   {
  149.     SysDebug2(WindowEvents,"vCanvasPaneDC::ClearRect() w=%d h=%d \n", width, height)
  150.  
  151.     if (height == 0 || width == 0)
  152.     return;
  153.  
  154.     BeginPaint();
  155.     // update the transformation in case the size of the
  156.     // canvas has been changed. This ensures that all GPI
  157.     // calls in V coords will map to the right place in OS/2
  158.     RECTL rc;
  159.     // CAUTION: returned rc coords are inclusive/exclusive
  160.     WinQueryWindowRect(_drawWidget, &rc);
  161.  
  162.     // V to OS/2 Coord Transform Equations:
  163.     // (os/2)  y = -(1 - height + y)  (V)
  164.     //
  165.     // for inclusive/inclusive (ie. Gpi)
  166.     //         y = -(yBottom - yTop + y)
  167.     //  where  height = yTop - yBottom + 1
  168.     //
  169.     // for inclusive/exclusive (ie. Win)
  170.     //         y = -(1 + yBottom - yTop + y)
  171.     //  where    height = yTop - yBottom
  172. /*
  173.     MATRIXLF TransformMatrix;
  174.     POINTL translate, origin;
  175.     FIXED scale[2];
  176.     translate.x = 0;
  177.     translate.y = 1 + rc.yBottom - rc.yTop;
  178.     GpiTranslate(_hdc, &TransformMatrix, TRANSFORM_REPLACE, &translate);
  179.     origin.x = 0; origin.y = 0;
  180.     scale[0] = MAKEFIXED( 1,0);   // x scale
  181.     scale[1] = MAKEFIXED(-1,0);   // y scale
  182.     GpiScale(_hdc, &TransformMatrix, TRANSFORM_ADD, scale, &origin);
  183.     GpiSetModelTransformMatrix(_hdc, 9, &TransformMatrix, TRANSFORM_REPLACE);
  184. */
  185.     // display is never scaled so set scaling to unity
  186.     SetOS2Map(rc.yTop - rc.yBottom, MAKEFIXED(1,0));
  187.  
  188.     // Clear a rectangular area starting at x, y of width and height
  189.     // the function inputs are assumed in V coords space
  190.     int h = rc.yTop - rc.yBottom;  // h>0
  191.     y = h - 1 - y;   // transform y to OS/2 coords
  192.  
  193.     // calc coords as inclusive/exclusive
  194.     rc.yTop = y + 1;           // exclusive coord
  195.     rc.yBottom = y - height;   // inclusive coord
  196.     rc.xLeft = x;              // inclusive coord
  197.     rc.xRight = x + width;     // exclusive coord
  198.  
  199.     // coords are inclusive/exclusive
  200.     WinFillRect (_hdc, &rc, _canvasBG);
  201.     EndPaint();
  202.   }
  203.  
  204. //================>>> vCanvasPaneDC::SetBackground <<<==========================
  205.   void vCanvasPaneDC::SetBackground(VCONST vColor& color)
  206.   {
  207.     BeginPaint();
  208.     _canvasBG = color.pixel();        // retrieve X pixel value
  209.     GpiSetBackColor (_hdc, _canvasBG);
  210.     Clear();
  211.     _parentPane->Redraw(0,0,0,0);
  212.     EndPaint();
  213.   }
  214.  
  215. //======================>>> vCanvasPaneDC::SetFont <<<===========================
  216.   void vCanvasPaneDC::SetFont(VCONST vFont& vf)
  217.   {
  218.     // Change the font associated with this window.
  219.     _font = vf;
  220.     // First, make sure the font is loaded
  221.     _parentPane->FontChanged(_font); // We have changed the font,
  222.                     // so we may need to do some things
  223.   }
  224.