home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / FONT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.1 KB  |  217 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.11  $
  6. //
  7. // Implementation of TFont, an encapsulation of the GDI Font object
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_GDIOBJEC_H)
  11. # include <owl/gdiobjec.h>
  12. #endif
  13. #if !defined(WINSYS_UIMETRIC_H)
  14. # include <winsys/uimetric.h>
  15. #endif
  16. #if !defined(DEFAULT_GUI_FONT)      // May not be defined by 16-bit headers
  17. # define  DEFAULT_GUI_FONT  17
  18. #endif
  19.  
  20. OWL_DIAGINFO;
  21. DIAG_DECLARE_GROUP(OwlGDI);          // General GDI diagnostic group
  22. DIAG_DECLARE_GROUP(OwlGDIOrphan);    // Oprhan control tracing group
  23.  
  24. //
  25. // Constructors
  26. //
  27. TFont::TFont(HFONT handle, TAutoDelete autoDelete)
  28. :
  29.   TGdiObject(handle, autoDelete)
  30. {
  31.   if (ShouldDelete)
  32.     RefAdd(Handle, Font);
  33. }
  34.  
  35. //
  36. //
  37. //
  38. TFont::TFont(const char far* facename,
  39.              int height, int width, int escapement, int orientation,
  40.              int weight,
  41.              uint8 pitchAndFamily,
  42.              uint8 italic, uint8 underline, uint8 strikeout, uint8 charSet,
  43.              uint8 outputPrecision, uint8 clipPrecision, uint8 quality)
  44. {
  45.   Handle = ::CreateFont(height, width, escapement, orientation, weight,
  46.                         italic, underline, strikeout, charSet,
  47.                         outputPrecision, clipPrecision, quality,
  48.                         pitchAndFamily, facename);
  49.   WARNX(OwlGDI, !Handle, 0, "Cannot create TFont (" << TResId(facename) <<
  50.         " " << height << "pt)");
  51.   CheckValid();
  52.   RefAdd(Handle, Font);
  53. }
  54.  
  55. //
  56. //
  57. //
  58. TFont::TFont(int height, int width, int escapement, int orientation,
  59.              int weight,
  60.              uint8 italic, uint8 underline, uint8 strikeout,
  61.              uint8 charSet,
  62.              uint8 outputPrecision,
  63.              uint8 clipPrecision,
  64.              uint8 quality,
  65.              uint8 pitchAndFamily,
  66.              const char far* facename)
  67. {
  68.   Handle = ::CreateFont(height, width, escapement, orientation, weight,
  69.                         italic, underline, strikeout, charSet,
  70.                         outputPrecision, clipPrecision, quality,
  71.                         pitchAndFamily, facename);
  72.   WARNX(OwlGDI, !Handle, 0, "Cannot create TFont (" << TResId(facename) <<
  73.         " " << height << "pt)");
  74.   CheckValid();
  75.   RefAdd(Handle, Font);
  76. }
  77.  
  78. //
  79. //
  80. //
  81. TFont::TFont(const LOGFONT far* logFont)
  82. {
  83.   PRECONDITION(logFont);
  84.   Handle = ::CreateFontIndirect((LPLOGFONT)logFont);  // API typecast
  85.   WARNX(OwlGDI, !Handle, 0, "Cannot create TFont from logfont @" <<
  86.         hex << uint32(LPVOID(logFont)));
  87.   CheckValid();
  88.   RefAdd(Handle, Font);
  89. }
  90.  
  91. //
  92. //
  93. //
  94. TFont::TFont(const TFont& src)
  95. {
  96. #if !defined(NO_GDI_SHARE_HANDLES)
  97.   Handle = src.Handle;
  98.   RefAdd(Handle, Font);
  99. #else
  100.   LOGFONT logFont;
  101.   src.GetObject(logFont);
  102.   Handle = ::CreateFontIndirect(&logFont);
  103.   WARNX(OwlGDI, !Handle, 0, "Cannot create TFont from TFont @" <<
  104.         hex << uint32(LPVOID(&src)));
  105.   CheckValid();
  106.   RefAdd(Handle, Font);
  107. #endif
  108. }
  109.  
  110. //
  111. // Retrieves information about this font when it is selected in the
  112. // specified Device Context.
  113. //
  114. void
  115. TFont::GetTextMetrics(TEXTMETRIC& tm, TDC& dc) const
  116. {
  117.   dc.SelectObject(*this);
  118.   dc.GetTextMetrics(tm);
  119.   dc.RestoreFont();
  120. }
  121.  
  122. //
  123. // Returns information about this font when it is selected in the
  124. // specified Device Context.
  125. //
  126. TEXTMETRIC
  127. TFont::GetTextMetrics(TDC& dc) const
  128. {
  129.   TEXTMETRIC tm;
  130.   GetTextMetrics(tm, dc);
  131.   return tm;
  132. }
  133.  
  134. //
  135. // Retrieves information about this font when it is selected in
  136. // a Screen Device Context.
  137. //
  138. void
  139. TFont::GetTextMetrics(TEXTMETRIC& tm) const
  140. {
  141.   TScreenDC dc;
  142.   GetTextMetrics(tm, dc);
  143. }
  144.  
  145. //
  146. // Returns information about this font when it is selected in
  147. // a Screen Device Context.
  148. //
  149. TEXTMETRIC
  150. TFont::GetTextMetrics() const
  151. {
  152.   TEXTMETRIC tm;
  153.   TScreenDC dc;
  154.   GetTextMetrics(tm, dc);
  155.   return tm;
  156. }
  157.  
  158. //
  159. // Return the extent of a given string using this particular font.
  160. //
  161. TSize
  162. TFont::GetTextExtent(const char far* text) const
  163. {
  164.   TScreenDC dc;
  165.   return GetTextExtent(dc, text);
  166. }
  167.  
  168. //
  169. // Return the extent of a given string using this particular font in a
  170. // particular DC.
  171. //
  172. TSize
  173. TFont::GetTextExtent(TDC& dc, const char far* text) const
  174. {
  175.   dc.SelectObject(*this);
  176.   TSize size = dc.GetTextExtent(text, strlen(text));
  177.   dc.RestoreFont();
  178.   return size;
  179. }
  180.  
  181. //
  182. //  Return the font's name
  183. //
  184. const string&
  185. TFont::GetFaceName() const
  186. {
  187.   LOGFONT lf;
  188.   static string name;
  189.   if (name.is_null())
  190.   {
  191.     GetObject(lf);
  192.     name = string(lf.lfFaceName);
  193.   }
  194.   return name;
  195. }
  196.  
  197. //
  198. //  Return the pitch and family flags for this logical font
  199. //
  200. uint8
  201. TFont::GetPitchAndFamily() const
  202. {
  203.   LOGFONT lf;
  204.   GetObject(lf);
  205.   return lf.lfPitchAndFamily;
  206. }
  207.  
  208. //
  209. // Create a wrapper for the default GUI font.
  210. //
  211. TDefaultGUIFont::TDefaultGUIFont()
  212. :
  213.   TFont(TSystem::Has3dUI() ? HFONT(::GetStockObject(DEFAULT_GUI_FONT)) :
  214.                              HFONT(::GetStockObject(SYSTEM_FONT)))
  215. {
  216. }
  217.