home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / source / xgraphdv.cpp < prev    next >
C/C++ Source or Header  |  1998-04-06  |  8KB  |  307 lines

  1. #include "XGraphDv.h"
  2. #include "XGraphOb.h"
  3. #include "XWindow.h"
  4. #include "XColor.h"
  5. #include "XMsgBox.h"
  6. #include "XFont.h"
  7. #include "xexcept.h"
  8. #include "xapp.h"
  9. #include <stdlib.h>
  10.  
  11.  
  12. /*@
  13. @class XGraphicDevice
  14. @type overview
  15. @symbol _
  16. @remarks XGraphicDevice is a context in which graphic object can be created and drawn.
  17. There are two methods to use XGraphicDevice
  18. <OL>
  19. <LI><B>Using one graphic device</B>
  20. <P>The most simple way is to use one graphic device
  21. in this case you only need to create the objects
  22. to draw in this device, every time it has to be
  23. redrawn (when the method Draw() of the owning
  24. window is called) you call the method Draw() of
  25. the device.
  26. <LI><B>Using two graphic devices</B>
  27. <P>If you have a litle bit more objects you should use
  28. this method because it works faster. Create one
  29. device in memory (the second parameter of the
  30. constructor must be TRUE) and create the objects
  31. to draw on this device. Only call Draw() if you
  32. have inserted/removed one or more objcts. Create
  33. a second graphic device associated to the owning
  34. window. If the method Draw() of the owning window
  35. is called, copy the content of the memory-device
  36. to the window-associated device with Copy().
  37. </OL>
  38. You can draw on a XGraphicDevice (and derived classes) with following classes:
  39. <UL>
  40. <LI>XArc
  41. <LI>XBitmap
  42. <LI>XBox
  43. <LI>XCircle
  44. <LI>XLine
  45. <LI>XMarker
  46. <LI>XText
  47. </UL>
  48. @seealso XLayer
  49. */
  50.  
  51.  
  52. void XGraphicDevice::RegisterFont(XFont * font)
  53. {
  54.    fonts++;
  55.    fontList = (XFont **) realloc(fontList, fonts * sizeof(void *));
  56.    fontList[fonts - 1] = font;
  57. }
  58.  
  59.  
  60. /*@ XGraphicDevice::Copy(const XGraphicDevice * s, const XRect * target, const XRect * source)
  61. @group drawing
  62. @remarks Copy the content of one graphic to another
  63. @parameters   <t '°' c=2>
  64.             °XGraphicDevice * source      °the source from which to copy
  65.             °XRect * targetRect         °rectangle which defines the target
  66.             °XRect * sourceRect         °rectangle which defines the source (default is NULL).
  67.                                        If the width of the rect is set greater zero, the
  68.                                        content will be stretched
  69.             </t>
  70. */
  71. void XGraphicDevice::Copy(const XGraphicDevice * s, const XRect * target, const XRect * source)
  72. {
  73.    POINTL p[4];
  74.    LONG m = 3;
  75.  
  76.    p[0].x = target->GetX();
  77.    p[0].y = target->GetY();
  78.    p[1].x = p[0].x + target->GetWidth();
  79.    p[1].y = p[0].y + target->GetHeight();
  80.    if (source)
  81.    {
  82.       p[2].x = source->GetX();
  83.       p[2].y = source->GetY();
  84.       if (source->GetWidth() > 0)
  85.       {
  86.          m = 4;
  87.          p[3].x = p[2].x + source->GetWidth();
  88.          p[3].y = p[2].x + source->GetHeight();
  89.       }
  90.    }
  91.    else
  92.       p[2].x = p[2].y = 0;
  93.    GpiBitBlt(hps, s->hps, m, p, ROP_SRCCOPY, BBO_IGNORE);
  94. }
  95.  
  96.  
  97.  
  98. XGraphicDevice :: XGraphicDevice(const LONG r)
  99. {
  100.    fonts = 0;
  101.    fontList = NULL;
  102.    // fontId = 0;
  103.    xOffset = yOffset = 0;
  104.    width = height = 0;
  105.    aktPattern = 0;
  106.    hwnd = NULL;
  107.    hab = NULL;
  108.    mode = r;
  109.    aktLineEnding = LINEEND_DEFAULT;
  110.    aktLineJoining = LINEJOIN_DEFAULT;
  111.    aktLineWidth = LINEWIDTH_DEFAULT;
  112.    aktLineType = LINETYPE_DEFAULT;
  113. }
  114.  
  115.  
  116. /*@ XGraphicDevice :: XGraphicDevice(const XWindow * w, const BOOL memory, const BOOL fillBack, LONG m)
  117. @remarks Constructs a grphic device where objects like XLine, XText etc can be drawn
  118. @parameters   <t '°' c=2>
  119.             °XWindow * window      °window to draw to (if XGraphicDevice is not constructed in memory)
  120.             °BOOL memory            °construct in memory (default is FALSE)
  121.             °BOOL fillBackground   °fill the background
  122.             °LONG resolution      °possible values:
  123. <BR>
  124. XGRAPH_PELS
  125. <BR>
  126. XGRAPH_LOMETRIC
  127. <BR>
  128. XGRAPH_HIMETRIC
  129. <BR>
  130. XGRAPH_LOENGLISH
  131. <BR>
  132. XGRAPH_HIENGLISH
  133. <BR>
  134. XGRAPH_TWIPS
  135.             °LONG mode            °mode, see OS/2 API documentation ( GPIA_ASSOC and related )
  136.             </t>
  137. */
  138. extern char *pszdata[9];
  139.  
  140. XGraphicDevice :: XGraphicDevice(const XWindow * w, const BOOL memory, const BOOL fillBack, LONG r, LONG m)
  141. {
  142.    fonts = 0;
  143.    fontList = NULL;
  144.    fill = fillBack;
  145.    // fontId = 0;
  146.    xOffset = yOffset = 0;
  147.    width = height = 0;
  148.    SIZEL g = {0, 0};
  149.    mode = r;
  150.  
  151.    hwnd = w->GetHandle();//Client
  152.    hab = WinQueryAnchorBlock(hwnd);
  153.  
  154.    if (!(memory))
  155.    {
  156.       hdc = WinOpenWindowDC(hwnd);
  157.       hps = GpiCreatePS(hab, hdc, &g, mode | m); //| GPIF_LONG | GPIA_ASSOC| GPIF_LONG | GPIA_ASSOC
  158.       if( hps == GPI_ERROR )
  159.          OOLThrow( "couldn∩t create HPS", GPI_ERROR);
  160.    }
  161.    else
  162.    {
  163.       hdc = DevOpenDC(hab, OD_MEMORY, (PSZ) "*", 4, (PDEVOPENDATA) pszdata, 0L);
  164.       hps = GpiCreatePS(hab, hdc, &g, mode | m);//| PU_PELS | GPIA_ASSOC);
  165.  
  166.       if( hps == GPI_ERROR )
  167.          OOLThrow( "couldn∩t create HPS", GPI_ERROR);
  168.       BITMAPINFOHEADER2 h;
  169.  
  170.       memset(&h, 0, sizeof(BITMAPINFOHEADER2));
  171.       h.cbFix = sizeof(BITMAPINFOHEADER2);
  172.       width = h.cx = 2000;
  173.       height = h.cy = 2000;
  174.       h.cBitCount = 8;
  175.       h.cPlanes = 1;
  176.       HBITMAP hbm = GpiCreateBitmap(hps, &h, 0, NULL, NULL);
  177.  
  178.       GpiSetBitmap(hps, hbm);
  179.    }
  180.  
  181.    GpiCreateLogColorTable(hps, LCOL_RESET, LCOLF_RGB, 0, 1, &backCol);
  182.  
  183.    backCol = 16777216;
  184.    aktColor = 0;
  185.    aktLineEnding = LINEEND_DEFAULT;
  186.    aktLineJoining = LINEJOIN_DEFAULT;
  187.    aktLineWidth = LINEWIDTH_DEFAULT;
  188.    aktLineType = LINETYPE_DEFAULT;
  189.    aktPattern = 0;
  190.    aktFont = 0;
  191.    GpiSetCharMode(hps, CM_MODE2);
  192.    if (memory)
  193.       hwnd = 0;
  194. }
  195.  
  196.  
  197. /*@ XGraphicDevice::SetBackgroundColor(const XColor * c)
  198. @remarks Set the background color (only used if fillBackgound is set TRUE in the constructor)
  199. @parameters   XColor * color      color to fill the background with
  200. */
  201. void XGraphicDevice::SetBackgroundColor(const XColor * c)
  202. {
  203.    backCol = c->GetColor();
  204.    GpiSetBackColor(hps, backCol);
  205. }
  206.  
  207.  
  208. XGraphicDevice :: ~XGraphicDevice()
  209. {
  210.    for (int i = 0; i < fonts; i++)
  211.    {
  212.       GpiDeleteSetId(hps, fontList[i]->fontId);
  213.       delete fontList[i];
  214.    }
  215.    if(fontList)
  216.       free(fontList);
  217.  
  218.    if (hps)
  219.       GpiDestroyPS(hps);
  220. }
  221.  
  222.  
  223.  
  224. /*@ XGraphicDevice::FillBackground(void)
  225. @remarks Fill the background
  226. */
  227. void XGraphicDevice::FillBackground(void)
  228. {
  229.    if (hwnd)
  230.    {
  231.       if (fill)
  232.       {
  233.          RECTL rect;
  234.          WinQueryUpdateRect(hwnd, &rect);
  235.          WinFillRect(hps, &rect, backCol);
  236.       }
  237.    }
  238.    else
  239.    {
  240.       if (fill)
  241.       {
  242.          RECTL rect = {0, 0, width, height};
  243.          WinFillRect(hps, &rect, backCol);
  244.       }
  245.    }
  246. }
  247.  
  248.  
  249. void XGraphicDevice::SetLineWidth(const LONG width)
  250. {
  251.    GpiSetLineWidth(hps, width);
  252.    aktLineWidth = width;
  253. }
  254.  
  255.  
  256. void XGraphicDevice::ResetFont(void)
  257. {
  258.    GpiSetCharSet(hps, 0);
  259.    aktFont = 0;
  260. }
  261.  
  262.  
  263. void XGraphicDevice::SetFont(XFont * font)
  264. {
  265.    EnableFont(font, (SHORT) (font->fontSize * 1.8 / 10));
  266. }
  267.  
  268.  
  269. void XGraphicDevice::EnableFont( XFont * font, LONG fontSize)
  270. {
  271.    aktFont = font->fontId;
  272.    GpiSetCharSet(hps, aktFont);
  273.    SIZEF sc = {0, 0};
  274.    SIZEL s;
  275.  
  276.    if (font->fixed == FALSE)
  277.    {
  278.       switch(mode)
  279.       {
  280.          case PU_LOMETRIC:
  281.             fontSize = fontSize * 2 * 254 / 1440;
  282.             break;
  283.          case PU_HIMETRIC:
  284.             fontSize = fontSize * 2 * 2540 / 1440;
  285.             break;
  286.          case PU_LOENGLISH:
  287.             fontSize = fontSize * 2 * 100 / 1440;
  288.             break;
  289.          case PU_HIENGLISH:
  290.             fontSize = fontSize * 2 * 1000 / 1440;
  291.             break;
  292.          case PU_PELS:
  293.             break;
  294.          default:
  295.             fontSize = fontSize * 2;
  296.       }
  297.  
  298.       sc.cx = sc.cy = MAKEFIXED(fontSize, 0);
  299.       GpiSetCharBox(hps, &sc);
  300.    }
  301.    FONTMETRICS fm;
  302.  
  303.    GpiQueryFontMetrics(hps, sizeof(fm), &fm);
  304.    font->realSize = fm.lMaxBaselineExt;
  305.    font->pixelSize = fontSize;
  306. }
  307.