ebmDevMag.com home
ebm Developer's Magazine
links

developer's mag
main page

article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
part 8
part 9


6 - Text Resizing

ResizeText() is the key to our new features. It creates our truncated text, based on the valid width (if positive), and the current font:
  void paLabel::ResizeText(void)
  {
    if ( NULL!=m_origText )
    {
      strcpy(m_dispText,m_origText);
      if ( m_width>=0 ) // do we truncate?
      {
        unsigned int len=GUI_TruncateText(m_font,m_dispText,m_width,false);
        if (len<strlen(m_dispText))
          m_dispText[len]='\0';
      }
      CLabel::SetLabel(m_dispText); // resize/redisplay
    }
  }
Now we see why we don't set our label in the constructor. Each time ResizeText() is called, it checks if truncation is desired (a positive width value), and creates a second string copy which has its pointer passed to CLabel. It is our parent CLabel class that deals with drawing, but always with the special truncated string of ours. So with very little effort we have a size-limiting text class that maintains our string for us.

The sample code for this column includes this class, and an example of both a CLabel and a paLabel in action. Drawn between two black shapes onscreen, the paLabel is clipped to avoid overwriting the right rectangle. On the other hand, the CLabel immediately overwrites when the button is pressed. For clipping occasions like this, a child override of CLabel can be very handy. While not perfect, this class can be a springboard to further modifications, such as allowing color, justification, and so forth.

Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice