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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1992, 1993 by Borland International
  3. //   source\owl\textwidg.cpp
  4. //   Defines class TTextGadget.
  5. //----------------------------------------------------------------------------
  6. #include <owl\owlpch.h>
  7. #include <owl\textgadg.h>
  8. #include <owl\gadgetwi.h>
  9.  
  10. TTextGadget::TTextGadget(int          id,
  11.                          TBorderStyle borderStyle,
  12.                          TAlign       align,
  13.                          UINT         numChars,
  14.                          const char*  text)
  15.   : TGadget(id, borderStyle)
  16. {
  17.   Margins.Left = Margins.Right = 2;
  18.   Align = align;
  19.   NumChars = numChars;
  20.   Text = 0;
  21.   TextLen = 0;
  22.   if (text)
  23.     SetText(text);
  24. }
  25.  
  26. TTextGadget::~TTextGadget()
  27. {
  28.   delete Text;
  29. }
  30.  
  31. //
  32. // Invalidate the working portion of this gadget--the InnerRect
  33. //
  34. void
  35. TTextGadget::Invalidate()
  36. {
  37.   TRect  innerRect;
  38.  
  39.   GetInnerRect(innerRect);
  40.   InvalidateRect(innerRect, FALSE);
  41. }
  42.  
  43. //
  44. // Set the text for this gadget
  45. //
  46. void
  47. TTextGadget::SetText(const char* text)
  48. {
  49.   if (text != Text) {
  50.     delete Text;
  51.  
  52.     if (text) {
  53.       Text = strnewdup(text);
  54.       TextLen = strlen(Text);
  55.  
  56.     } else {
  57.       Text = 0;
  58.       TextLen = 0;
  59.     }
  60.  
  61.     Invalidate();
  62.   }
  63. }
  64.  
  65. UINT
  66. TTextGadget::GetMaxCharWidth()
  67. {
  68.   TScreenDC   dc;
  69.   TEXTMETRIC  metrics;
  70.  
  71.   dc.SelectObject(Window->GetFont());
  72.   dc.GetTextMetrics(metrics);
  73.   return (metrics.tmMaxCharWidth + metrics.tmAveCharWidth) / 2;
  74. }
  75.  
  76. void
  77. TTextGadget::GetDesiredSize(TSize& size)
  78. {
  79.   TGadget::GetDesiredSize(size);
  80.  
  81.   if (ShrinkWrapWidth)
  82.     size.cx += GetMaxCharWidth() * NumChars;
  83.  
  84.   if (ShrinkWrapHeight)
  85.     size.cy += Window->GetFontHeight();
  86. }
  87.  
  88. //
  89. // Paint the text gadget by painting gadget borders, & then painting text in
  90. // the InnerRect. Empty or 0 text blanks the gadget.
  91. //
  92. void
  93. TTextGadget::Paint(TDC& dc)
  94. {
  95.   PaintBorder(dc);
  96.  
  97.   TRect  innerRect;
  98.   GetInnerRect(innerRect);
  99.   int x = innerRect.left;
  100.   int y = innerRect.top;
  101.  
  102.   //
  103.   // adjust the horizontal position for centered or right justified text
  104.   //
  105.   if (Text && TextLen && Align != Left) {
  106.     TSize  extent = dc.GetTextExtent(Text, TextLen);
  107.  
  108.     if (extent.cx < innerRect.Width()) {
  109.       int  diff = innerRect.Width() - extent.cx;
  110.  
  111.       if (Align == Center)
  112.         diff /= 2;
  113.  
  114.       x += diff;
  115.     }
  116.   }
  117.  
  118.   //
  119.   // use ExtTextOut() to paint the text & background rectangle
  120.   //
  121.   dc.SetBkColor(GetSysColor(COLOR_BTNFACE));
  122.   dc.SetTextColor(GetSysColor(COLOR_BTNTEXT));
  123.   dc.ExtTextOut(x, y, ETO_CLIPPED | ETO_OPAQUE, &innerRect, Text, TextLen);
  124. }
  125.