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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Implementation of TTextGadget.
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_TEXTGADG_H)
  11. # include <owl/textgadg.h>
  12. #endif
  13. #if !defined(OWL_GADGETWI_H)
  14. # include <owl/gadgetwi.h>
  15. #endif
  16. #if !defined(WINSYS_UIMETRIC_H)
  17. # include <winsys/uimetric.h>
  18. #endif
  19.  
  20. OWL_DIAGINFO;
  21.  
  22. //
  23. // Construct a text gadget
  24. //
  25. TTextGadget::TTextGadget(int          id,
  26.                          TBorderStyle borderStyle,
  27.                          TAlign       align,
  28.                          uint         numChars,
  29.                          const char far* text,
  30.                          TFont*       font)
  31. :
  32.   TGadget(id, borderStyle),
  33.   Font(font)
  34. {
  35.   Margins.Left = Margins.Right = TUIMetric::CxFixedFrame;
  36.   Align = align;
  37.   NumChars = numChars;
  38.   Text = 0;
  39.   TextLen = 0;
  40.  
  41.   SetShrinkWrap(false, true);
  42.   SetText(text);
  43. }
  44.  
  45. //
  46. // Destruct this text gadget and delete the text it is holding
  47. //
  48. TTextGadget::~TTextGadget()
  49. {
  50.   delete[] Text;
  51.   delete Font;
  52. }
  53.  
  54. //
  55. // Invalidate the working portion of this gadget--in this case just the
  56. // InnerRect
  57. //
  58. void
  59. TTextGadget::Invalidate()
  60. {
  61.   TRect  innerRect;
  62.  
  63.   GetInnerRect(innerRect);
  64.   InvalidateRect(innerRect, false);
  65. }
  66.  
  67. //
  68. // Set the text for this gadget
  69. //
  70. void
  71. TTextGadget::SetText(const char far* text)
  72. {
  73.   // Skip processing if new text is the same as current
  74.   //
  75.   if (text && Text && strcmp(text, Text) == 0)
  76.     return;
  77.  
  78.   delete[] Text;
  79.  
  80.   if (text) {
  81.     Text = strnewdup(text);
  82.     TextLen = strlen(Text);
  83.   }
  84.   else {
  85.     Text = 0;
  86.     TextLen = 0;
  87.   }
  88.  
  89.   if (Window)
  90.     Window->GadgetChangedSize(*this);
  91.  
  92.   Invalidate();
  93. }
  94.  
  95. //
  96. // Respond to the virtual call to let this gadget's Window know how big this
  97. // text gadget wants to be based on the text size.
  98. //
  99. void
  100. TTextGadget::GetDesiredSize(TSize& size)
  101. {
  102.   TGadget::GetDesiredSize(size);
  103.   TFont* font = Font;
  104.   if (font == 0)
  105.     font = &(Window->GetFont());
  106.  
  107.   if (font == 0)
  108.     return;
  109.  
  110.   if (ShrinkWrapWidth)
  111.     size.cx += font->GetTextExtent(Text).cx;
  112.   else {
  113.     int  left, right, top, bottom;
  114.     GetOuterSizes(left, right, top, bottom);
  115.  
  116.     int newW = font->GetMaxWidth() * NumChars;
  117.     size.cx += newW + left + right - Bounds.Width();  // Old bounds already considered
  118.   }
  119.  
  120.   if (ShrinkWrapHeight)
  121.     size.cy += font->GetHeight() + 2;
  122. }
  123.  
  124. //
  125. // Paint the text gadget by painting gadget borders, & then painting text in
  126. // the InnerRect. Empty or 0 text blanks the gadget.
  127. //
  128. void
  129. TTextGadget::Paint(TDC& dc)
  130. {
  131.   PaintBorder(dc);
  132.  
  133.   TRect  innerRect;
  134.   GetInnerRect(innerRect);
  135.   int x = innerRect.left;
  136.   int y = innerRect.top;
  137.  
  138.   if (Font == 0)
  139.     dc.SelectObject(Window->GetFont());
  140.   else
  141.     dc.SelectObject(*Font);
  142.  
  143.   // Adjust the horizontal position for centered or right justified text
  144.   //
  145.   if (Text && TextLen && Align != Left) {
  146.     TSize  extent = dc.GetTextExtent(Text, TextLen);
  147.  
  148.     if (extent.cx < innerRect.Width()) {
  149.       int  diff = innerRect.Width() - extent.cx;
  150.  
  151.       if (Align == Center)
  152.         diff /= 2;
  153.  
  154.       x += diff;
  155.     }
  156.   }
  157.  
  158.   // Use ExtTextOut() to paint the text & background rectangle
  159.   //
  160.   dc.SetBkColor(TColor::Sys3dFace);
  161.   dc.SetTextColor(GetEnabled() ? TColor::SysBtnText : TColor::SysGrayText);
  162.   dc.ExtTextOut(x, y, ETO_CLIPPED | ETO_OPAQUE, &innerRect, Text, TextLen);
  163.   dc.RestoreFont();
  164. }
  165.