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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of TTextGadget.
  6. //----------------------------------------------------------------------------
  7. #include <owl/owlpch.h>
  8. #include <owl/textgadg.h>
  9. #include <owl/gadgetwi.h>
  10.  
  11. TTextGadget::TTextGadget(int          id,
  12.                          TBorderStyle borderStyle,
  13.                          TAlign       align,
  14.                          uint         numChars,
  15.                          const char*  text)
  16. :
  17.   TGadget(id, borderStyle)
  18. {
  19.   Margins.Left = Margins.Right = 2;
  20.   Align = align;
  21.   NumChars = numChars;
  22.   Text = 0;
  23.   TextLen = 0;
  24.   if (text)
  25.     SetText(text);
  26. }
  27.  
  28. TTextGadget::~TTextGadget()
  29. {
  30.   delete [] Text;
  31. }
  32.  
  33. //
  34. // Invalidate the working portion of this gadget--the InnerRect
  35. //
  36. void
  37. TTextGadget::Invalidate()
  38. {
  39.   TRect  innerRect;
  40.  
  41.   GetInnerRect(innerRect);
  42.   InvalidateRect(innerRect, false);
  43. }
  44.  
  45. //
  46. // Set the text for this gadget
  47. //
  48. void
  49. TTextGadget::SetText(const char* text)
  50. {
  51.   //
  52.   // Skip processing if new text is similar to current
  53.   //
  54.   if (text && Text && !strcmp(text, Text))
  55.     return;
  56.  
  57.   //
  58.   // Skip processing if matching pointers [eg. g->SetText(g->GetText()),
  59.   // or g->SetText(0) when current text is already null].
  60.   // NOTE: This is safe as long as 'Text' is always a strnewduped string
  61.   //       and not simply a pointer to a user supplied buffer. Derived 
  62.   //       classes must follow the convention of always setting
  63.   //       'Text' via SetText.
  64.   //
  65.   if (text == Text) 
  66.     return;
  67.  
  68.   delete [] Text;
  69.   Text = 0;
  70.   TextLen = 0;
  71.  
  72.   if (text) {
  73.     Text = strnewdup(text);
  74.     TextLen = strlen(Text);
  75.   }
  76.  
  77.   Invalidate();
  78. }
  79.  
  80. uint
  81. TTextGadget::GetMaxCharWidth()
  82. {
  83.   TScreenDC   dc;
  84.   TEXTMETRIC  metrics;
  85.  
  86.   dc.SelectObject(Window->GetFont());
  87.   dc.GetTextMetrics(metrics);
  88.   return (metrics.tmMaxCharWidth + metrics.tmAveCharWidth) / 2;
  89. }
  90.  
  91. void
  92. TTextGadget::GetDesiredSize(TSize& size)
  93. {
  94.   TGadget::GetDesiredSize(size);
  95.  
  96.   if (ShrinkWrapWidth)
  97.     size.cx += GetMaxCharWidth() * NumChars;
  98.  
  99.   if (ShrinkWrapHeight)
  100.     size.cy += Window->GetFontHeight();
  101. }
  102.  
  103. //
  104. // Paint the text gadget by painting gadget borders, & then painting text in
  105. // the InnerRect. Empty or 0 text blanks the gadget.
  106. //
  107. void
  108. TTextGadget::Paint(TDC& dc)
  109. {
  110.   PaintBorder(dc);
  111.  
  112.   TRect  innerRect;
  113.   GetInnerRect(innerRect);
  114.   int x = innerRect.left;
  115.   int y = innerRect.top;
  116.  
  117.   //
  118.   // adjust the horizontal position for centered or right justified text
  119.   //
  120.   if (Text && TextLen && Align != Left) {
  121.     TSize  extent = dc.GetTextExtent(Text, TextLen);
  122.  
  123.     if (extent.cx < innerRect.Width()) {
  124.       int  diff = innerRect.Width() - extent.cx;
  125.  
  126.       if (Align == Center)
  127.         diff /= 2;
  128.  
  129.       x += diff;
  130.     }
  131.   }
  132.  
  133.   //
  134.   // use ExtTextOut() to paint the text & background rectangle
  135.   //
  136.   dc.SetBkColor(GetSysColor(COLOR_BTNFACE));
  137.   dc.SetTextColor(GetSysColor(COLOR_BTNTEXT));
  138.   dc.ExtTextOut(x, y, ETO_CLIPPED | ETO_OPAQUE, &innerRect, Text, TextLen);
  139. }
  140.