home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / DC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  10.1 KB  |  434 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of TDC
  6. //----------------------------------------------------------------------------
  7. #include <owl/owlpch.h>
  8. #include <owl/dc.h>
  9.  
  10. void
  11. TDC::Init()
  12. {
  13.   OrgBrush = 0;
  14.   OrgPen = 0;
  15.   OrgFont = 0;
  16.   OrgPalette = 0;
  17. #if defined(BI_PLAT_WIN32)
  18.   OrgTextBrush = 0;
  19. #endif
  20. }
  21.  
  22. TDC::TDC(HDC handle)
  23. :
  24.   TGdiBase(handle, NoAutoDelete)
  25. {
  26.   Init();
  27. }
  28.  
  29. //
  30. // Following two ctors are for use by derived classes only
  31. //
  32. TDC::TDC()
  33. {
  34.   Init();
  35. }
  36.  
  37. TDC::TDC(HDC handle, TAutoDelete autoDelete)
  38. :
  39.   TGdiBase(handle, autoDelete)
  40. {
  41.   Init();
  42. }
  43.  
  44. //
  45. // Default dtor does not delete Handle
  46. //
  47. TDC::~TDC()
  48. {
  49.   RestoreObjects();
  50. }
  51.  
  52. HDC
  53. TDC::GetAttributeHDC() const
  54. {
  55.   return HDC(Handle);
  56. }
  57.  
  58. void
  59. TDC::SelectObject(const TPen& pen)
  60. {
  61.   HPEN oldPen = (HPEN)::SelectObject(GetHDC(), pen);
  62.   if (oldPen) {
  63.     OBJ_REF_INC(pen);
  64.     if ((bool)oldPen > 1)
  65.       if (!OrgPen)
  66.         OrgPen = oldPen;
  67.       else
  68.         OBJ_REF_DEC(oldPen, false);
  69.   }
  70. }
  71.  
  72. void
  73. TDC::SelectObject(const TBrush& brush)
  74. {
  75.   HBRUSH oldBrush = (HBRUSH)::SelectObject(GetHDC(), brush);
  76.   if (oldBrush) {
  77.     OBJ_REF_INC(brush);
  78.     if ((bool)oldBrush > 1)
  79.       if (!OrgBrush)
  80.         OrgBrush = oldBrush;
  81.       else
  82.         OBJ_REF_DEC(oldBrush, false);
  83.   }
  84. }
  85.  
  86. void
  87. TDC::SelectObject(const TFont& font)
  88. {
  89.   HFONT oldFont = (HFONT)::SelectObject(GetHDC(), font);
  90.   if (oldFont) {
  91.     OBJ_REF_INC(font);
  92.     if ((bool)oldFont > 1)
  93.       if (!OrgFont)
  94.         OrgFont = oldFont;
  95.       else
  96.         OBJ_REF_DEC(oldFont, false);
  97.   }
  98. }
  99.  
  100. void
  101. TDC::SelectObject(const TPalette& palette, bool forceBackground)
  102. {
  103.   HPALETTE oldPalette = ::SelectPalette(GetHDC(), palette, forceBackground);
  104.   if (oldPalette) {
  105.     OBJ_REF_INC(palette);
  106.     if ((bool)oldPalette > 1)
  107.       if (!OrgPalette)
  108.         OrgPalette = oldPalette;
  109.       else
  110.         OBJ_REF_DEC(oldPalette, false);
  111.   }
  112. }
  113.  
  114. void
  115. TDC::SelectStockObject(int index)
  116. {
  117.   PRECONDITION(::GetStockObject(index));
  118.   HANDLE oldObj = ::SelectObject(GetHDC(), ::GetStockObject(index));
  119.   if ((bool)oldObj > 1)
  120.       OBJ_REF_DEC(oldObj, false);
  121. }
  122.  
  123. void
  124. TDC::RestorePen()
  125. {
  126.   if (OrgPen) {
  127.     OBJ_REF_DEC(::SelectObject(GetHDC(), OrgPen), false);
  128.     OrgPen = 0;
  129.   }
  130. }
  131.  
  132. void
  133. TDC::RestoreBrush()
  134. {
  135.   if (OrgBrush) {
  136.     OBJ_REF_DEC(::SelectObject(GetHDC(), OrgBrush), false);
  137.     OrgBrush = 0;
  138.   }
  139. }
  140.  
  141. void
  142. TDC::RestoreFont()
  143. {
  144.   if (OrgFont) {
  145.     OBJ_REF_DEC(::SelectObject(GetHDC(), OrgFont), false);
  146.     OrgFont = 0;
  147.   }
  148. }
  149.  
  150. void
  151. TDC::RestorePalette()
  152. {
  153.   if (OrgPalette) {
  154.     OBJ_REF_DEC(::SelectPalette(GetHDC(), OrgPalette, false), false);
  155.     OrgPalette = 0;
  156.   }
  157. }
  158.  
  159. #if defined(BI_PLAT_WIN32)
  160. void
  161. TDC::RestoreTextBrush()
  162. {
  163.   if (OrgTextBrush) {
  164.     OBJ_REF_DEC(::SelectObject(GetHDC(), OrgTextBrush), false);
  165.     OrgTextBrush = 0;
  166.   }
  167. }
  168. #endif
  169.  
  170. void
  171. TDC::RestoreObjects()
  172. {
  173.   if (Handle) {
  174.     RestorePen();
  175.     RestoreBrush();
  176.     RestoreFont();
  177.     RestorePalette();
  178.   #if defined(BI_PLAT_WIN32)
  179.     RestoreTextBrush();
  180.   #endif
  181.   }
  182. }
  183.  
  184. //
  185. // implement subset of Win32 GetCurrentObject for Win16 (& Win32s!)
  186. //
  187. HANDLE
  188. TDC::GetCurrentObject(uint objectType) const
  189. {
  190.   HGDIOBJ curObj;
  191.  
  192. #if defined(BI_PLAT_WIN32)
  193.   static bool notImplemented = false;
  194.  
  195.   //
  196.   // Try the Win32 GetCurrentObject() function, will fail under Win32s
  197.   //
  198.   if (!notImplemented) {
  199.     curObj = ::GetCurrentObject(GetHDC(), objectType);
  200.     if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
  201.       return curObj;
  202.     notImplemented = true;
  203.   }
  204. #endif
  205.  
  206.   curObj = 0;
  207.   switch (objectType) {
  208.     case OBJ_PEN:
  209.       curObj = ::SelectObject(GetHDC(), ::GetStockObject(BLACK_PEN));
  210.       break;
  211.  
  212.     case OBJ_BRUSH:
  213.       curObj = ::SelectObject(GetHDC(), ::GetStockObject(BLACK_BRUSH));
  214.       break;
  215.  
  216.     case OBJ_PAL:
  217.       curObj = ::SelectObject(GetHDC(), ::GetStockObject(DEFAULT_PALETTE));
  218.       break;
  219.  
  220.     case OBJ_FONT:
  221.       curObj = ::SelectObject(GetHDC(), ::GetStockObject(DEVICE_DEFAULT_FONT));
  222.       break;
  223.  
  224.     default:     // throw an exception for unsupported objtypes
  225.       CHECK(curObj);
  226.   }
  227.  
  228.   if (curObj)
  229.     ::SelectObject(GetHDC(), curObj);
  230.   return curObj;
  231. }
  232.  
  233. //
  234. // Compile this in to get this function for 16bit
  235. //
  236. #if !defined(BI_PLAT_WIN32)
  237.  
  238. //
  239. // Use undocumented GDI GetClipRgn(HDC) to get the actual handle, and
  240. // make a copy of it. Note that the Win32 GetClipRgn(HDC) does the copying
  241. // itself, while this win16 version requires us to do it.
  242. //
  243. extern "C" HRGN FAR PASCAL ::GetClipRgn(HDC);  // GDI.173
  244.  
  245. bool
  246. TDC::GetClipRgn(TRegion& region) const
  247. {
  248.   HRGN hRgn = ::GetClipRgn(GetHDC());
  249.   if (!hRgn)
  250.     return false;
  251.   region = TRegion(hRgn);   // make a copy of the region using assignment
  252.   return true;
  253. }
  254. #endif
  255.  
  256. extern "C" bool FAR PASCAL FastWindowFrame(HDC, LPRECT, int xWidth, int yWidth, long rop);
  257.  
  258. void
  259. TDC::OWLFastWindowFrame(TBrush& brush, TRect& r, int xWidth, int yWidth)
  260. {
  261.   SelectObject(brush);
  262.  
  263. #if !defined(BI_PLAT_WIN32)
  264.   if (!FastWindowFrame(GetHDC(), &r, xWidth, yWidth, PATCOPY))
  265. #endif
  266.   {
  267.     int  width = r.Width() - xWidth;
  268.     int  height = r.Height() - yWidth;
  269.  
  270.     PatBlt(r.left, r.top, xWidth, height, PATCOPY);  // left
  271.     PatBlt(xWidth, r.top, width, yWidth, PATCOPY);   // top
  272.     PatBlt(r.left, height, width, yWidth, PATCOPY);  // bottom
  273.     PatBlt(width, yWidth, xWidth, height, PATCOPY);  // right
  274.   }
  275.   RestoreBrush();
  276. }
  277.  
  278. int
  279. TDC::SaveDC() const
  280. {
  281.   return ::SaveDC(GetHDC());
  282. }
  283.  
  284. bool
  285. TDC::RestoreDC(int savedIndex)
  286. {
  287.   return ::RestoreDC(GetHDC(), savedIndex);
  288. }
  289.  
  290. int
  291. TDC::GetDeviceCaps(int index) const
  292. {
  293.   return ::GetDeviceCaps(GetAttributeHDC(), index);
  294. }
  295.  
  296. bool
  297. TDC::ResetDC(DEVMODE far& devMode)
  298. {
  299.   return ::ResetDC(GetHDC(), &devMode) != 0;
  300. }
  301.  
  302. TColor
  303. TDC::SetBkColor(TColor color)
  304. {
  305.   if (GetHDC() != GetAttributeHDC())
  306.     ::SetBkColor(GetHDC(), color);
  307.   return ::SetBkColor(GetAttributeHDC(), color);
  308. }
  309.  
  310. TColor
  311. TDC::SetTextColor(TColor color)
  312. {
  313.   if (GetHDC() != GetAttributeHDC())
  314.     ::SetTextColor(GetHDC(), color);
  315.   return ::SetTextColor(GetAttributeHDC(), color);
  316. }
  317.  
  318. int
  319. TDC::SetMapMode(int mode)
  320. {
  321.   if (GetHDC() != GetAttributeHDC())
  322.     ::SetMapMode(GetHDC(), mode);
  323.   return ::SetMapMode(GetAttributeHDC(), mode);
  324. }
  325.  
  326. bool
  327. TDC::SetViewportOrg(const TPoint& point, TPoint far* oldOrg)
  328. {
  329.   if (GetHDC() != GetAttributeHDC())
  330.     ::SetViewportOrgEx(GetHDC(), point.x, point.y, oldOrg);
  331.   return ::SetViewportOrgEx(GetAttributeHDC(), point.x, point.y, oldOrg);
  332. }
  333.  
  334. bool
  335. TDC::OffsetViewportOrg(const TPoint& delta, TPoint far* oldOrg)
  336. {
  337.   if (GetHDC() != GetAttributeHDC())
  338.     ::OffsetViewportOrgEx(GetHDC(), delta.x, delta.y, oldOrg);
  339.   return ::OffsetViewportOrgEx(GetAttributeHDC(), delta.x, delta.y, oldOrg);
  340. }
  341.  
  342. bool
  343. TDC::SetViewportExt(const TSize& extent, TSize far* oldExtent)
  344. {
  345.   if (GetHDC() != GetAttributeHDC())
  346.     ::SetViewportExtEx(GetHDC(), extent.cx, extent.cy, oldExtent);
  347.   return ::SetViewportExtEx(GetAttributeHDC(), extent.cx, extent.cy, oldExtent);
  348. }
  349.  
  350. bool
  351. TDC::ScaleViewportExt(int xNum, int xDenom, int yNum, int yDenom,
  352.                       TSize far* oldExtent)
  353. {
  354.   if (GetHDC() != GetAttributeHDC())
  355.     ::ScaleViewportExtEx(GetHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
  356.   return ::ScaleViewportExtEx(GetAttributeHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
  357. }
  358.  
  359. bool
  360. TDC::SetWindowOrg(const TPoint& point, TPoint far* oldOrg)
  361. {
  362.   if (GetHDC() != GetAttributeHDC())
  363.     ::SetWindowOrgEx(GetHDC(), point.x, point.y, oldOrg);
  364.   return ::SetWindowOrgEx(GetAttributeHDC(), point.x, point.y, oldOrg);
  365. }
  366.  
  367. bool
  368. TDC::OffsetWindowOrg(const TPoint& delta, TPoint far* oldOrg)
  369. {
  370.   if (GetHDC() != GetAttributeHDC())
  371.     ::OffsetWindowOrgEx(GetHDC(), delta.x, delta.y, oldOrg);
  372.   return ::OffsetWindowOrgEx(GetAttributeHDC(), delta.x, delta.y, oldOrg);
  373. }
  374.  
  375. bool
  376. TDC::SetWindowExt(const TSize& extent, TSize far* oldExtent)
  377. {
  378.   if (GetHDC() != GetAttributeHDC())
  379.     ::SetWindowExtEx(GetHDC(), extent.cx, extent.cy, oldExtent);
  380.   return ::SetWindowExtEx(GetAttributeHDC(), extent.cx, extent.cy, oldExtent);
  381. }
  382.  
  383. bool
  384. TDC::ScaleWindowExt(int xNum, int xDenom, int yNum, int yDenom, TSize far* oldExtent)
  385. {
  386.   if (GetHDC() != GetAttributeHDC())
  387.     ::ScaleWindowExtEx(GetHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
  388.   return ::ScaleWindowExtEx(GetAttributeHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
  389. }
  390.  
  391. bool
  392. TDC::TextOut(int x, int y, const char far* str, int count)
  393. {
  394.   return ::TextOut(GetHDC(), x, y, str, count>=0 ? count : strlen(str));
  395. }
  396.  
  397. bool
  398. TDC::ExtTextOut(int x, int y, uint16 options, const TRect* rect,
  399.                 const char far* str, int count, const int far* dx)
  400. {
  401.   return ::ExtTextOut(GetHDC(), x, y, options, rect, str,
  402.                       count>=0 ? count : strlen(str), (int far*)dx);
  403.                                                       // API typecast
  404. }
  405.  
  406. bool
  407. TDC::TabbedTextOut(const TPoint& p, const char far* str, int count,
  408.                    int numPositions, const int far* positions,
  409.                    int tabOrigin, TSize& size) {
  410.   size = (uint32)::TabbedTextOut(GetHDC(), p.x, p.y, str,
  411.                                  count>=0 ? count : strlen(str),
  412.                                  numPositions, (int far*)positions,
  413.                                  tabOrigin);
  414.                                                 // API typecast
  415.   return true;
  416. }
  417.  
  418. int
  419. TDC::DrawText(const char far* str, int count, const TRect& rect, uint16 format)
  420. {
  421.   return ::DrawText(GetHDC(), str, count,  // uses -1 to signify autocount
  422.                     (RECT*)&rect, format);
  423.                     // API typecast
  424. }
  425.  
  426. bool
  427. TDC::GrayString(const TBrush& brush, GRAYSTRINGPROC outputFunc,
  428.                 const char far* str, int count, const TRect& rect)
  429. {
  430.   return ::GrayString(GetHDC(), brush, outputFunc, (uint32)str,
  431.                       count>=0 ? count : 0,  // uses 0 to signify autocount
  432.                       rect.left, rect.top, rect.Width(), rect.Height());
  433. }
  434.