home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / DC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  9.8 KB  |  427 lines

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