home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / krcls012.zip / KrClass / source / krbwin.cpp < prev    next >
Text File  |  1997-02-12  |  8KB  |  207 lines

  1. // Kroni's Classes: objects for buffered twodimensional graphics
  2. // (c) 1997 Wolfgang Kronberg
  3. // file: krbwin.cpp
  4.  
  5.  
  6. #include "krbwin.hpp"
  7.  
  8. #define INCL_GPIBITMAPS                          // For GpiQueryDeviceBitmapFormats & Co.
  9. #include <os2.h>
  10.  
  11. #include <ithread.hpp>                           // IThread
  12. #include <igrafctx.hpp>                          // IGraphicContext
  13. #include <igline.hpp>                            // IGLine
  14.  
  15.  
  16.  
  17. KrMemoryBitmap::KrMemoryBitmap (int aWidth, int aHeight)
  18.   : IGBitmap (IBitmapHandle(createHbm(aWidth, aHeight)))
  19. {
  20.   hdcMem = hdcMemS;
  21.   hpsMem = hpsMemS;
  22.   hbmMem = hbmMemS;
  23.   width = widthS;
  24.   height = heightS;
  25.   graphicsList = 0;
  26.   trans = 0;
  27. };
  28.  
  29.  
  30. HBITMAP KrMemoryBitmap::createHbm (SHORT aWidth, SHORT aHeight)
  31. {
  32.  
  33.   const LONG deviceFormats = 1;                  // Display has only one device format
  34.   const LONG alDataSize = 2*deviceFormats;
  35.   LONG alData [alDataSize];                      // Pair planes / bitCount for this device
  36.   BITMAPINFOHEADER2 bmp;                         // Structure for bitmap on screen
  37.   HAB hab = IThread::current().anchorBlock();    // Anchor-Block handle
  38.   const LONG pszDataSize = 4;                    // So many fields of the data structure are used
  39.   PSZ pszData[pszDataSize] = { "Display", NULL, NULL, NULL };
  40.                                                  // Use "Display" as the device
  41.   SIZEL sizePage = { aWidth, aHeight };          // Page size of the presentation space
  42.  
  43.   widthS = aWidth;
  44.   heightS = aHeight;
  45.  
  46.   hdcMemS = DevOpenDC (
  47.     hab,                                         // Always used for this kind of API call
  48.     OD_MEMORY,                                   // Memory Device Context
  49.     "*",                                         // OS/2 ignores this
  50.     pszDataSize, (PDEVOPENDATA) pszData,         // We open indeed the display
  51.     NULLHANDLE                                   // Device Context will be compatible to display
  52.   );
  53.  
  54.   hpsMemS = GpiCreatePS (
  55.     hab, hdcMemS, &sizePage,
  56.     PU_PELS |                                    // Unit size of the presentation space is one pixel
  57.     GPIA_ASSOC | GPIT_MICRO                      // Needed to create a Micro-PS
  58.   );
  59.  
  60.   GpiQueryDeviceBitmapFormats(hpsMemS, alDataSize, alData);
  61.  
  62.   bmp.cbFix = (ULONG) sizeof(BITMAPINFOHEADER2); // Size of the structure
  63.   bmp.cx = widthS;                               // Size of the bitmap
  64.   bmp.cy = heightS;
  65.   bmp.cPlanes = alData[0];                       // Number of bit planes
  66.   bmp.cBitCount = alData[1];                     // Number of bits per pixel within each plane
  67.   bmp.ulCompression = BCA_UNCOMP;                // No compression
  68.   bmp.cbImage = (((widthS * (1 << bmp.cPlanes) * (1 << bmp.cBitCount)) + 31) / 32) * heightS;
  69.                                                  // Size of shown bitmap data in bytes
  70.                                                  //   Organization of bitmaps is padded to DWORD per row
  71.   bmp.cxResolution = 70;                         // One might want to change this
  72.   bmp.cyResolution = 70;
  73.   bmp.cclrUsed = 0;                              // Don't restrict the colors to use by the bitmap
  74.   bmp.cclrImportant = 0;                         // All colors are important
  75.   bmp.usUnits = BRU_METRIC;                      // Measure in pixels per meter
  76.   bmp.usReserved = 0;                            // This is reserved and must be zero
  77.   bmp.usRecording = BRA_BOTTOMUP;                // First logical row is the lowest on screen
  78.   bmp.usRendering = BRH_NOTHALFTONED;            // We don't do any rendering
  79.   bmp.cSize1 = 0;                                // Belongs to usRendering
  80.   bmp.cSize2 = 0;                                // Belongs to usRendering
  81.   bmp.ulColorEncoding = BCE_RGB;                 // This is the default
  82.   bmp.ulIdentifier = 0;                          // Application data
  83.  
  84.   hbmMemS = GpiCreateBitmap (                    // Actually create the bitmap
  85.     hpsMemS, &bmp,
  86.     FALSE,                                       // We don't want to initialize it
  87.     NULL,NULL                                    // There's no initialization data
  88.   );
  89.   return hbmMemS;
  90. };
  91.  
  92.  
  93. SHORT KrMemoryBitmap::widthS, KrMemoryBitmap::heightS;
  94. HDC KrMemoryBitmap::hdcMemS;
  95. HPS KrMemoryBitmap::hpsMemS;
  96. HBITMAP KrMemoryBitmap::hbmMemS;
  97.  
  98.  
  99. void KrMemoryBitmap::drawOnBitmap (IGList * list)
  100. {
  101.   GpiSetBitmap (hpsMem, hbmMem);                 // Drawing in hpsMem will now fill the Bitmap
  102.  
  103.   IPresSpaceHandle ihpsMem (hpsMem);             // We declare the UICL versions of the handles
  104.   IGraphicContext gc (ihpsMem);
  105.  
  106.   if (list) list->drawOn (gc);                   // We draw the list itself
  107.  
  108.   GpiSetBitmap (hpsMem, NULLHANDLE);             // The bitmap must be freed again
  109. };
  110.  
  111.  
  112. IGList * KrMemoryBitmap::setList (IGList & list)
  113. {
  114.   IGList * rc = graphicsList;                    // Save the old value...
  115.   graphicsList = &list;                          // ...and set the new one
  116.  
  117.   erase ();                                      // Invalidate anything old
  118.   drawOnBitmap (graphicsList);                   // Draw the new graphics immediately
  119.  
  120.   return rc;                                     // Return the old list
  121. };
  122.  
  123.  
  124. IGList * KrMemoryBitmap::list ()
  125. {
  126.   return graphicsList;
  127. };
  128.  
  129.  
  130. void KrMemoryBitmap::addList (IGList & list)
  131. {
  132.   graphicsList->addAsLast (list);                // Add the new list
  133.   drawOnBitmap (&list);                          // Draw the new graphics immediately
  134. };
  135.  
  136.  
  137. void KrMemoryBitmap::erase (const IColor & color)
  138. {
  139.   GpiSetBitmap (hpsMem, hbmMem);                 // Drawing in hpsMem will now fill the Bitmap
  140.  
  141.   // Some secret initializations must be done before calling GpiSetColor.
  142.   //   I don't know them, but the UICL does, so let's draw some dummy using it.
  143.  
  144.   IPresSpaceHandle ihpsMem (hpsMem);             // We declare the UICL versions of the handles
  145.   IGraphicContext gc (ihpsMem);
  146.  
  147.   IGList tempList;
  148.   IGLine tempLine (IPoint (0,0), IPoint(1,1));
  149.   tempList.addAsFirst (tempLine);
  150.   tempList.drawOn (gc);                          // Just to draw anything
  151.  
  152.   // Now, back to work.
  153.  
  154.   POINTL p = { width, height };
  155.   POINTL o = { 0,0 };
  156.   GpiMove (hpsMem, &o);
  157.   GpiSetColor (hpsMem, color.asRGBLong());
  158.   GpiBox (hpsMem, DRO_FILL, &p, 0, 0);
  159.  
  160.   GpiSetBitmap (hpsMem, NULLHANDLE);             // The bitmap must be freed again
  161. };
  162.  
  163.  
  164. KrCoordSystemTranslator * KrMemoryBitmap::setCoordSystem (KrCoordSystemTranslator * aTrans,
  165.    const KrPoint & aP1, const KrPoint & aP2)
  166. {
  167.   KrCoordSystemTranslator * oldTrans = trans;
  168.   trans = aTrans;
  169.   p1 = aP1;
  170.   p2 = aP2;
  171.   return oldTrans;
  172. };
  173.  
  174.  
  175. KrMemoryBitmap & KrMemoryBitmap::drawOn (IGraphicContext & graphicContext)
  176. {
  177.   return drawOn (graphicContext, normal);        // This is this function's definition
  178. };
  179.  
  180.  
  181. KrMemoryBitmap & KrMemoryBitmap::drawOn( IGraphicContext & graphicContext, const IPoint& targetBottomLeft,
  182.              const IPoint& targetTopRight, const IPoint& sourceBottomLeft, const IPoint& sourceTopRight,
  183.              long rasterOperation, CompressMode compressMode)
  184. {
  185.   IGBitmap::drawOn (graphicContext, targetBottomLeft, targetTopRight, sourceBottomLeft, sourceTopRight,
  186.      rasterOperation, compressMode);             // Just forward this call to IGBitmap. It does not make
  187.                                                  //   sense to use this with a coordinate system translator.
  188.   return (*this);
  189. };
  190.  
  191.  
  192. KrMemoryBitmap & KrMemoryBitmap::drawOn( IGraphicContext& graphicContext, long rasterOperation)
  193. {
  194.   if (trans)
  195.      {
  196.      IPoint dest1 = trans -> translate (p1);
  197.      IPoint dest2 = trans -> translate (p2);
  198.      IPoint source1 (0,0);
  199.      IPoint source2 (width,height);
  200.      drawOn (graphicContext, dest1, dest2, source1, source2, rasterOperation);
  201.      }
  202.   else
  203.      IGBitmap::drawOn (graphicContext, rasterOperation);
  204.   return (*this);
  205. };
  206.  
  207.