home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / dwgsurf.cxx < prev    next >
C/C++ Source or Header  |  1995-04-09  |  20KB  |  658 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. /*
  7.  *
  8.  *          Copyright (C) 1994, M. A. Sridhar
  9.  *  
  10.  *
  11.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  12.  *     to copy, modify or distribute this software  as you see fit,
  13.  *     and to use  it  for  any  purpose, provided   this copyright
  14.  *     notice and the following   disclaimer are included  with all
  15.  *     copies.
  16.  *
  17.  *                        DISCLAIMER
  18.  *
  19.  *     The author makes no warranties, either expressed or implied,
  20.  *     with respect  to  this  software, its  quality, performance,
  21.  *     merchantability, or fitness for any particular purpose. This
  22.  *     software is distributed  AS IS.  The  user of this  software
  23.  *     assumes all risks  as to its quality  and performance. In no
  24.  *     event shall the author be liable for any direct, indirect or
  25.  *     consequential damages, even if the  author has been  advised
  26.  *     as to the possibility of such damages.
  27.  *
  28.  */
  29.  
  30.  
  31. #if defined(__GNUC__)
  32. #pragma implementation
  33. #endif
  34.  
  35.  
  36. #include "ui/dwgsurf.h"
  37. #include "ui/visualob.h"
  38. #include "ui/bitmap.h"
  39. #include "ui/arc.h"
  40. #include "ui/chord.h"
  41. #include "ui/ellipse.h"
  42. #include "ui/piewedge.h"
  43.  
  44. #if defined(__MS_WINDOWS__)
  45.  
  46. #include <windows.h>
  47. static HRGN getRectRgnMS (const UI_Rectangle& r);
  48. static HRGN getPolyRgnMS (UI_Point par[], short numpts);
  49.  
  50. #elif defined(__OS2__)
  51. #include <string.h>
  52. #include "ui/applic.h"
  53. #include "ui/cntroler.h"
  54. #endif
  55.  
  56. UI_DrawingSurface::UI_DrawingSurface ()
  57. {
  58.     _pen        = NULL;
  59.     _font       = NULL;
  60.     _brush      = NULL;
  61.     _horzPixels = 0;
  62.     _vertPixels = 0;
  63. }
  64.  
  65.  
  66.  
  67. UI_Rectangle UI_DrawingSurface::DrawingArea () const
  68. {
  69. #if defined(__MS_WINDOWS__)
  70.     return UI_Rectangle (0, 0, _horzPixels, _vertPixels);
  71. #else
  72.     NotImplemented ("DrawingArea");
  73.     return UI_Rectangle (0, 0, 0, 0);
  74. #endif
  75. }
  76.  
  77.  
  78.  
  79. UI_Rectangle UI_DrawingSurface::DrawingAreaInMM () const
  80. {
  81. #if defined(__MS_WINDOWS__)
  82.     return UI_Rectangle (0, 0, GetDeviceCaps (_handle, HORZSIZE),
  83.                          GetDeviceCaps (_handle, VERTSIZE));
  84. #elif defined(__OS2__)
  85.     HDC hdc = GpiQueryDevice (_handle);
  86.     LONG w, h; // w and h will be in pixels per meter
  87.     DevQueryCaps (hdc, CAPS_HORIZONTAL_RESOLUTION, 1L, &w);
  88.     DevQueryCaps (hdc, CAPS_VERTICAL_RESOLUTION,   1L, &h);
  89.     UI_Rectangle r = DrawingArea();
  90.     return UI_Rectangle (0, 0, r.Width() * 100 / w,  r.Height() * 100 / h);
  91. #else
  92.     NotImplemented ("DrawingAreaInMM");
  93.     return UI_Rectangle (0, 0, 0, 0);
  94. #endif
  95. }
  96.  
  97.  
  98.  
  99. void UI_DrawingSurface::ColorRectangle (const UI_Rectangle& r,
  100.                                         const UI_Color& cs)
  101. {
  102. #if defined(__MS_WINDOWS__)
  103.     HBRUSH hbr = CreateSolidBrush (cs.NativeForm ());
  104.     HBRUSH old = SelectObject (_handle, hbr);
  105.     HRGN   hregion = getRectRgnMS (r);
  106.     PaintRgn(_handle, hregion);
  107.     SelectObject (_handle, old);
  108.     DeleteObject (hbr);
  109.     DeleteObject (hregion);
  110. #elif defined (__OS2__)
  111.     UI_Rectangle area = DrawingArea ();
  112.     RECTL rect;
  113.     rect.xLeft    = r.Left();
  114.     rect.yBottom  = area.Height() - r.Bottom () + 1;
  115.     rect.xRight   = r.Right();
  116.     rect.yTop     = area.Height() + 1 - r.Top ();
  117.     UI_NativeColorRep color = cs.NativeForm();
  118.     GpiSavePS (_handle);
  119.     if (SupportsColor())
  120.         GpiSetColor (_handle, color);
  121.     GpiMove (_handle, (PPOINTL) &rect);
  122.     GpiBox  (_handle, DRO_FILL, ((PPOINTL) &rect) + 1, 0, 0);
  123.     GpiRestorePS (_handle, -1);
  124. #endif 
  125. }
  126.  
  127.  
  128.  
  129. void UI_DrawingSurface::DrawEllipse (const UI_Rectangle& r,
  130.                                      ushort opt)
  131. {
  132.     if (!opt)
  133.         return;
  134. #if defined(__MS_WINDOWS__)
  135.     HBRUSH hbr, old;
  136.     if (opt & UID_Fill) {
  137.         old = SelectObject (_handle, _brush->Handle());
  138.     }
  139.     else {
  140.         hbr   = GetStockObject (HOLLOW_BRUSH);
  141.         old   = SelectObject (_handle, hbr);
  142.     }
  143.     if (! (opt & UID_Outline))
  144.         SelectObject (_handle, GetStockObject (NULL_PEN));
  145.     Ellipse (_handle, r.Left (), r.Top (), r.Right (), r.Bottom ());
  146.     if (! (opt & UID_Outline))
  147.         SelectObject (_handle, _pen->Handle());
  148.     SelectObject (_handle, old);
  149. #elif defined(__OS2__)
  150.     long width  = r.Width();
  151.     long height = r.Height ();
  152.     POINTL ptLB, ptTR;
  153.     ptLB.x = r.Left();
  154.     ptLB.y = DrawingArea().Height() - r.Bottom() - 1;
  155.     ptTR.x = r.Right();
  156.     ptTR.y = ptLB.y + r.Height() - 1;
  157.     GpiSavePS (_handle);
  158.     if ((opt & UID_Fill) && (_brush->Pattern() != UIBrush_Hollow)) {
  159.         if (SupportsColor())
  160.             GpiSetColor (_handle, _brush->Color().NativeForm());
  161.         GpiMove (_handle, &ptLB);
  162.         GpiBox  (_handle, DRO_FILL, &ptTR, width, height);
  163.     }
  164.     if (opt & UID_Outline) {
  165.         if (SupportsColor())
  166.             GpiSetColor (_handle, _pen->Color().NativeForm());
  167.         GpiMove (_handle, &ptLB);
  168.         GpiBox  (_handle, DRO_OUTLINE, &ptTR, width, height);
  169.     }
  170.     GpiRestorePS (_handle, -1);
  171. #endif
  172. }
  173.  
  174.  
  175.  
  176. void UI_DrawingSurface::DrawArc (const UI_Arc& arc)
  177. {
  178.     UI_Rectangle  rect  = arc.Ellipse().BoundingRectangle();
  179. #if defined (__MS_WINDOWS__)
  180.     UI_Point pa, pb;
  181.  
  182.     UI_PointPair pp = arc.EndPoints();
  183.     pa = pp.p1;
  184.     pb = pp.p2;
  185.  
  186.     Arc (Handle(),
  187.          rect.Left(),  rect.Top(),
  188.          rect.Right(), rect.Bottom(),
  189.          pa.XCoord(),  pa.YCoord(),
  190.          pb.XCoord(),  pb.YCoord());
  191. #elif defined(__OS2__)
  192.     long width  = rect.Width  ();
  193.     long height = rect.Height ();
  194.     ARCPARAMS arcParams = {width/2, height/2, 0, 0};
  195.     GpiSetArcParams (_handle, &arcParams);
  196.     POINTL center;
  197.     UI_Point centerPt = rect.Center();
  198.     center.x = centerPt.XCoord();
  199.     center.y = DrawingArea().Height() - 1 - centerPt.YCoord();
  200.     long start = arc.StartAngle(), sweep = arc.SubtendedAngle();
  201.     GpiSetLineType (_handle, LINETYPE_INVISIBLE);
  202.     GpiPartialArc (_handle, ¢er, MAKEFIXED(1,0),
  203.                    MAKEFIXED (start / 64, start % 64),
  204.                    MAKEFIXED (0, 0));
  205.     if (_pen)
  206.         GpiSetLineType (_handle, _pen->OS2Pattern());
  207.     GpiPartialArc (_handle, ¢er, MAKEFIXED(1,0),
  208.                    MAKEFIXED (start / 64, start % 64),
  209.                    MAKEFIXED (sweep / 64, sweep % 64));
  210. #else
  211.     NotImplemented ("DrawArc");
  212. #endif
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220. void UI_DrawingSurface::DrawChord (const UI_Chord& chord,
  221.                                    ushort opt)
  222. {
  223.     UI_Rectangle  rect  = chord.Ellipse().BoundingRectangle();
  224. #if defined (__MS_WINDOWS__)
  225.     HBRUSH hbr, old;
  226.     if (opt & UID_Fill) {
  227.         old = SelectObject (_handle, _brush->Handle());
  228.     }
  229.     else {
  230.         hbr   = GetStockObject (HOLLOW_BRUSH);
  231.         old   = SelectObject (_handle, hbr);
  232.     }
  233.     if (! (opt & UID_Outline))
  234.         SelectObject (_handle, GetStockObject (NULL_PEN));
  235.     UI_PointPair pp = chord.EndPoints();
  236.     Chord (Handle(),
  237.            rect.Left(),  rect.Top(),
  238.            rect.Right(), rect.Bottom(),
  239.            pp.p1.XCoord(),  pp.p1.YCoord(),
  240.            pp.p2.XCoord(),  pp.p2.YCoord());
  241.     if (! (opt & UID_Outline))
  242.         SelectObject (_handle, _pen->Handle());
  243.     SelectObject (_handle, old);
  244. #elif defined(__OS2__)
  245.     long width  = rect.Width  ();
  246.     long height = rect.Height ();
  247.     ARCPARAMS arcParams = {width/2, height/2, 0, 0};
  248.     GpiSetArcParams (_handle, &arcParams);
  249.     POINTL center;
  250.     UI_Point centerPt = rect.Center();
  251.     center.x = centerPt.XCoord();
  252.     center.y = DrawingArea().Height() - 1 - centerPt.YCoord();
  253.     long start = chord.StartAngle(), sweep = chord.SubtendedAngle();
  254.  
  255.     GpiSetLineType (_handle, LINETYPE_INVISIBLE);
  256.     GpiPartialArc (_handle, ¢er, MAKEFIXED(1,0),
  257.                    MAKEFIXED (start / 64, start % 64),
  258.                    MAKEFIXED (0, 0));
  259.     POINTL startPt;
  260.     GpiQueryCurrentPosition (_handle, &startPt);
  261.     if ((opt & UID_Fill) && (_brush->Pattern() != UIBrush_Hollow)) {
  262.         if (SupportsColor())
  263.             GpiSetColor (_handle, _brush->Color().NativeForm());
  264.         GpiBeginArea (_handle, 0L);
  265.         GpiPartialArc (_handle, ¢er, MAKEFIXED(1,0),
  266.                        MAKEFIXED (start / 64, start % 64),
  267.                        MAKEFIXED (sweep / 64, sweep % 64));
  268.         GpiEndArea (_handle);
  269.     }
  270.     if (opt & UID_Outline) {
  271.         if (_pen) {
  272.             if (SupportsColor())
  273.                 GpiSetColor (_handle, _pen->Color().NativeForm());
  274.             GpiSetLineType (_handle, _pen->OS2Pattern());
  275.         }
  276.         GpiPartialArc (_handle, ¢er, MAKEFIXED(1,0),
  277.                        MAKEFIXED (start / 64, start % 64),
  278.                        MAKEFIXED (sweep / 64, sweep % 64));
  279.         GpiLine (_handle, &startPt);
  280.     }
  281. #else
  282.     NotImplemented ("DrawChord");
  283.     
  284. #endif
  285. }
  286.  
  287.  
  288.  
  289.  
  290. void UI_DrawingSurface::DrawPieWedge (const UI_PieWedge& pie,
  291.                                       ushort opt)
  292. {
  293.     UI_Rectangle  rect  = pie.Ellipse().BoundingRectangle();
  294. #if defined (__MS_WINDOWS__)
  295.     HBRUSH hbr, old;
  296.     if (opt & UID_Fill) {
  297.         old = SelectObject (_handle, _brush->Handle());
  298.     }
  299.     else {
  300.         hbr   = GetStockObject (HOLLOW_BRUSH);
  301.         old   = SelectObject (_handle, hbr);
  302.     }
  303.     if (! (opt & UID_Outline))
  304.         SelectObject (_handle, GetStockObject (NULL_PEN));
  305.     UI_PointPair pp = pie.EndPoints();
  306.     Pie (Handle(),
  307.          rect.Left(),  rect.Top(),
  308.          rect.Right(), rect.Bottom(),
  309.          pp.p1.XCoord(),  pp.p1.YCoord(),
  310.          pp.p2.XCoord(),  pp.p2.YCoord());
  311.     if (! (opt & UID_Outline))
  312.         SelectObject (_handle, _pen->Handle());
  313.     SelectObject (_handle, old);
  314. #elif defined(__OS2__)
  315.     long width  = rect.Width  ();
  316.     long height = rect.Height ();
  317.     ARCPARAMS arcParams = {width/2, height/2, 0, 0};
  318.     GpiSetArcParams (_handle, &arcParams);
  319.     POINTL center;
  320.     UI_Point centerPt = rect.Center();
  321.     center.x = centerPt.XCoord();
  322.     center.y = DrawingArea().Height() - 1 - centerPt.YCoord();
  323.     long start = pie.StartAngle(), sweep = pie.SubtendedAngle();
  324.     GpiMove (_handle, ¢er);
  325.     if ((opt & UID_Fill) && (_brush->Pattern() != UIBrush_Hollow)) {
  326.         if (SupportsColor())
  327.             GpiSetColor (_handle, _brush->Color().NativeForm());
  328.         GpiBeginArea (_handle, 0L);
  329.         GpiPartialArc (_handle, ¢er, MAKEFIXED(1,0),
  330.                        MAKEFIXED (start / 64, start % 64),
  331.                        MAKEFIXED (sweep / 64, sweep % 64));
  332.         GpiLine (_handle, ¢er);
  333.         GpiEndArea (_handle);
  334.     }
  335.     if (opt & UID_Outline) {
  336.         if (_pen) {
  337.             if (SupportsColor())
  338.                 GpiSetColor (_handle, _pen->Color().NativeForm());
  339.             GpiSetLineType (_handle, _pen->OS2Pattern());
  340.         }
  341.         GpiPartialArc (_handle, ¢er, MAKEFIXED(1,0),
  342.                        MAKEFIXED (start / 64, start % 64),
  343.                        MAKEFIXED (sweep / 64, sweep % 64));
  344.         GpiLine (_handle, ¢er);
  345.     }
  346. #else
  347.     NotImplemented ("DrawPieWedge");
  348.     
  349. #endif
  350. }
  351.  
  352.  
  353. void UI_DrawingSurface::DrawPolygon
  354.      (UI_Point point[], short num_pts, ushort opt)
  355. {
  356.     if (num_pts < 2 || !opt)
  357.         return;
  358.     if (opt & UID_Fill) {
  359. #if defined(__MS_WINDOWS__)
  360.         HRGN h = getPolyRgnMS (point, num_pts);
  361.         FillRgn (_handle, h, _brush->Handle());
  362.         DeleteObject (h);
  363. #else
  364.         NotImplemented ("FillPolygon");
  365. #endif
  366.     }
  367.     DrawPolyLine (point, num_pts);
  368.     DrawLine (point[num_pts-1], point[0]);
  369. }
  370.  
  371.  
  372.  
  373. bool UI_DrawingSurface::DrawLine (const UI_Point& p, const UI_Point& q)
  374. {
  375. #if defined(__MS_WINDOWS__)
  376.     POINT array[2];
  377.     array[0].x = p.XCoord();
  378.     array[0].y = p.YCoord();
  379.     array[1].x = q.XCoord();
  380.     array[1].y = q.YCoord();
  381.     if (Polyline (Handle(), array, 2)) {
  382.         return TRUE;
  383.     }
  384.     return FALSE;
  385. #elif defined(__OS2__)
  386.     if (!_handle)
  387.         return FALSE;
  388.     long height = DrawingArea().Height();
  389.     POINTL pt;
  390.     pt.x = p.XCoord();
  391.     pt.y = height - p.YCoord() + 1;
  392.     GpiSavePS (_handle);
  393.     GpiMove (_handle, &pt);
  394.     if (SupportsColor())
  395.         GpiSetColor (_handle, _pen->Color().NativeForm());
  396.     pt.x = q.XCoord();
  397.     pt.y = height - q.YCoord() + 1;
  398.     GpiLine (_handle, &pt);
  399.     GpiRestorePS (_handle, -1);
  400.     return TRUE;
  401. #else
  402.     NotImplemented ("DrawLine");
  403.     return FALSE;
  404. #endif
  405. }
  406.  
  407.  
  408.  
  409.  
  410.  
  411. void UI_DrawingSurface::DrawPolyLine (UI_Point point[], short num_pts)
  412. {
  413.     if (num_pts <= 1)
  414.         return;
  415. #if defined(__MS_WINDOWS__)
  416.     for (short i = 1; i < num_pts; i++)
  417.         DrawLine (point[i-1], point[i]);
  418. #elif defined(__OS2__)
  419.     POINTL* pts = new POINTL [num_pts-1];
  420.     if (!pts)
  421.         return; // No memory
  422.     long height = DrawingArea().Height();
  423.     pts[0].x = point[0].XCoord();
  424.     pts[0].y = height - point[0].YCoord() - 1;
  425.     GpiSavePS (_handle);
  426.     GpiMove (_handle, pts);
  427.     for (short i = 1; i < num_pts; i++) {
  428.         pts[i-1].x = point[i].XCoord();
  429.         pts[i-1].y = height - point[i].YCoord() - 1;
  430.     }
  431.     GpiPolyLine (_handle, num_pts-1, pts);
  432.     GpiRestorePS (_handle, -1);
  433.     delete pts;
  434. #else
  435.     NotImplemented ("DrawPolyLine");
  436. #endif
  437. }
  438.  
  439.  
  440.  
  441. void UI_DrawingSurface::DrawRectangle (const UI_Rectangle& r,
  442.                                        ushort opt)
  443. {
  444.     if (!opt)
  445.         return;
  446. #if defined(__MS_WINDOWS__)
  447.     HBRUSH hbr, old;
  448.     if (opt & UID_Fill) {
  449.         old = SelectObject (_handle, _brush->Handle());
  450.     }
  451.     else {
  452.         hbr   = GetStockObject (HOLLOW_BRUSH);
  453.         old   = SelectObject (_handle, hbr);
  454.     }
  455.     if (! (opt & UID_Outline))
  456.         SelectObject (_handle, GetStockObject (NULL_PEN));
  457.     UI_Rectangle area = DrawingArea ();
  458.     long right   = minl (r.Right() + 1, area.Right());
  459.     long bottom  = minl (r.Bottom() + 1, area.Bottom());
  460.     Rectangle (_handle, r.Left (), r.Top (), right, bottom);
  461.     if (! (opt & UID_Outline))
  462.         SelectObject (_handle, _pen->Handle());
  463.     SelectObject (_handle, old);
  464. #elif defined(__OS2__)
  465.     POINTL ptLB, ptTR;
  466.     ptLB.x = r.Left();
  467.     ptLB.y = DrawingArea().Height() - r.Bottom() - 1;
  468.     ptTR.x = r.Right();
  469.     ptTR.y = ptLB.y + r.Height() - 1;
  470.     GpiSavePS (_handle);
  471.     if ((opt & UID_Fill) && (_brush->Pattern() != UIBrush_Hollow)) {
  472.         if (SupportsColor())
  473.             GpiSetColor (_handle, _brush->Color().NativeForm());
  474.         GpiMove (_handle, &ptLB);
  475.         GpiBox  (_handle, DRO_FILL, &ptTR, 0, 0);
  476.     }
  477.     if (opt & UID_Outline) {
  478.         if (SupportsColor())
  479.             GpiSetColor (_handle, _pen->Color().NativeForm());
  480.         GpiMove (_handle, &ptLB);
  481.         GpiBox  (_handle, DRO_OUTLINE, &ptTR, 0, 0);
  482.     }
  483.     GpiRestorePS (_handle, -1);
  484. #endif
  485. }
  486.  
  487.  
  488. void UI_DrawingSurface::DrawPoint (const UI_Point& p, const UI_Color& color)
  489. {
  490. #if defined(__MS_WINDOWS__)
  491.     SetPixel (_handle, p.XCoord(), p.YCoord(), color.NativeForm());
  492. #elif defined(__OS2__)
  493.     GpiSavePS (_handle);
  494.     if (SupportsColor())
  495.         GpiSetColor (_handle, color.NativeForm());
  496.     POINTL pt;
  497.     pt.x = p.XCoord();
  498.     pt.y = DrawingArea().Height() - 1 - p.YCoord();
  499.     GpiSetPel (_handle, &pt);
  500.     GpiRestorePS (_handle, -1);
  501. #else
  502.     NotImplemented ("DrawPoint");
  503. #endif
  504. }
  505.  
  506.  
  507.  
  508.  
  509.  
  510. void UI_DrawingSurface::WriteString (const CL_String& str,
  511.                                      const UI_Rectangle& tRect,
  512.                                      UI_TextStyle tStyle)
  513. {
  514.     UI_Point p = tRect.Origin();
  515. #if defined(__MS_WINDOWS__)
  516.     long style;
  517.     TEXTMETRIC tm;
  518.     RECT rect = tRect.AsMSRect();
  519.     rect.right++; rect.bottom++; // Fix for off-by-one
  520.     GetTextMetrics (_handle, &tm);
  521.     style = (tStyle == UIText_Left) ? DT_LEFT
  522.         : (tStyle == UIText_Center ? DT_CENTER : DT_RIGHT);
  523.     int oldMode = SetBkMode (_handle, TRANSPARENT);
  524.     DrawText(_handle, (const char*)str, str.Size(), &rect, style);
  525.     SetBkMode (_handle, oldMode);
  526. #elif defined(__OS2__)
  527.     if (SupportsColor())
  528.         GpiSetColor (_handle, _pen->Color().NativeForm());
  529.     GpiSetBackMix (_handle, BM_LEAVEALONE);
  530.     UI_Font& fnt = Font();
  531.     short fontDescent = fnt.Descent(), fontAscent = fnt.Ascent();
  532.     long ht =  DrawingArea().Height();
  533.     // Now adjust for the base line of the font  
  534.     // (See p. 109 of Petzold's book)    
  535.     POINTL stringBase;
  536.     stringBase.x = tRect.Left();
  537.     stringBase.y = ht - tRect.Top() - fontAscent;
  538.     RECTL clipRect;
  539.     clipRect.xLeft   = tRect.Left();
  540.     if (tStyle == UIText_Center)
  541.         stringBase.x += (tRect.Width() - TextWidth (str))/2;
  542.     else if (tStyle == UIText_Right)
  543.         stringBase.x += tRect.Width() - TextWidth (str);
  544.     clipRect.yBottom = stringBase.y - fontDescent;
  545.     clipRect.xRight  = tRect.Right();
  546.     clipRect.yTop    = clipRect.yBottom + fontAscent + fontDescent - 1;
  547.     HRGN hrgn = GpiCreateRegion (_handle, 1, &clipRect);
  548.     HRGN oldRegion;
  549.     GpiSetClipRegion (_handle, hrgn, &oldRegion);
  550.     if (GpiCharStringAt (_handle, (PPOINTL) &stringBase, str.Length(),
  551.                          (char*) str.AsPtr()) == GPI_ERROR)
  552.         UI_Application::PMError();
  553.     GpiSetClipRegion (_handle, NULLHANDLE, &oldRegion);
  554. #else
  555.     NotImplemented ("WriteString");
  556. #endif
  557. }
  558.  
  559.  
  560.  
  561.  
  562. short UI_DrawingSurface::TextWidth (const char* strg) const
  563. {
  564. #if defined (__MS_WINDOWS__)
  565.     CL_String s (strg);
  566.     return GetTextExtent (_handle, s.AsPtr(), s.Size());
  567. #elif defined(__OS2__)
  568.     POINTL textBox[TXTBOX_COUNT];
  569.     GpiQueryTextBox (_handle, strlen (strg), (char*) strg,
  570.                      TXTBOX_COUNT, textBox);
  571.     return textBox[TXTBOX_CONCAT].x;
  572. #else
  573.     NotImplemented ("TextWidth"); // Implemented in DisplaySurface
  574.     return 0;
  575. #endif
  576. }
  577.  
  578.  
  579. bool UI_DrawingSurface::DrawBitmap (const UI_Bitmap& b, const UI_Point& p)
  580. {
  581. #if defined (__MS_WINDOWS__)
  582.     long bmpHandle = b.Handle();
  583.     if (!bmpHandle)
  584.         return FALSE;
  585.     HDC destdc = Handle ();
  586.     if (!destdc)
  587.         return FALSE;
  588.     HDC hdc = CreateCompatibleDC (destdc);
  589.     if (!hdc)
  590.         return FALSE;
  591.     HANDLE oldh = SelectObject (hdc, bmpHandle);
  592.     SetMapMode (hdc, GetMapMode (destdc));
  593.  
  594.     BITMAP bm;
  595.     POINT ptsize, ptorg;
  596.  
  597.     GetObject (bmpHandle, sizeof (BITMAP), (LPSTR) &bm);
  598.     ptsize.x = bm.bmWidth;
  599.     ptsize.y = bm.bmHeight;
  600.     DPtoLP (destdc, &ptsize, 1);
  601.  
  602.     ptorg.x = 0; ptorg.y = 0;
  603.     DPtoLP (hdc, &ptorg, 1);
  604.                 
  605.     BitBlt (destdc, p.XCoord(), p.YCoord(), b.Width(), b.Height(), hdc, 0, 0,
  606.             SRCCOPY);
  607.     SelectObject (hdc, oldh);
  608.     DeleteDC (hdc);
  609.     return TRUE;
  610.  
  611. #elif defined(__OS2__)
  612.     POINTL pt[4];
  613.     pt[0].x = p.XCoord();
  614.     pt[0].y = DrawingArea().Height() - p.YCoord() - b.Height() + 1;
  615.     pt[1].x = pt[0].x + b.Width ()  - 1;
  616.     pt[1].y = pt[0].y + b.Height () - 1;
  617.     pt[2].x = pt[2].y = 0;
  618.     pt[3].x = b.Width ()  - 1;
  619.     pt[3].y = b.Height () - 1;
  620.     GpiWCBitBlt (_handle, b.Handle(), 4, pt, ROP_SRCCOPY, BBO_AND);
  621.     return TRUE;
  622. #else
  623.     NotImplemented ("DrawBitmap");
  624.     return FALSE;
  625. #endif
  626. }
  627.  
  628. #if defined(__MS_WINDOWS__)
  629. HRGN getRectRgnMS (const UI_Rectangle& r)
  630. {
  631.     int x1,y1,x2,y2;
  632.     x1 = r.TopLeftX();
  633.     y1 = r.TopLeftY();
  634.     x2 = x1 + r.Width();
  635.     y2 = y1 + r.Height(); 
  636.     return CreateRectRgn(x1,y1,x2,y2);
  637. }
  638.  
  639. HRGN getPolyRgnMS (UI_Point p[], short numpts)
  640. {
  641.     HRGN h;
  642.     POINT  *msp;
  643.  
  644.     msp = new POINT[numpts];
  645.  
  646.     for (short i = 0; i < numpts; i++) {
  647.         msp[i].x = p[i].XCoord();
  648.         msp[i].y = p[i].YCoord();
  649.     }
  650.     h = CreatePolygonRgn (msp, numpts, WINDING);
  651.     delete msp;
  652.     return h;
  653.  
  654. }
  655.  
  656.  
  657. #endif    
  658.