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


4 - A Few Tweaks

Although labels are useful, they do suffer from some limitations. As already mentioned, the label does not keep a copy of its data - for changing labels, this can mean extra memory management. In addition, once label text is changed, the label display is resized to display as much as can fit onscreen. This can wreak havoc with displays, when the text overwrites another object because it's too long.

To fix some of these issues, we'll look at a class that adds some features to CLabel, called paLabel. First off, we'll have paLabel manage text passed to it by making an internal copy. Additionally, it needs to be aware of its location, clipping the text if there is not enough room to display it entirely. The current CLabel does quite a bit of useful work, so we are best to reuse it by inheriting from it rather than creating our own from scratch:
  class paLabel : public CLabel
  {
    private:

      int  m_width;
      char*m_origText, *m_dispText;
      const FONT *m_font;

      void ResizeText(void);

    public:

      paLabel(U16 uid,const char *label,const FONT *font,int width=-1);
      ~paLabel(void);
      const char *GetLabel(void)
      { return m_origText; }
      int Width(void)
      { return m_width; }
      void Width(int width);
      void SetFont(const FONT *font);
      void SetLabel(const char *label);
  };
Our class inherits from the CLabel publicly, meaning that its functions are available in this child class as well. Without adding extra code, this class would work virtually the same as the original CLabel. However, we need to adjust some routines to add our features, hiding the parent's versions with our own.

First off, we add variables to manage the object's width and font. Since we will be clipping, we'll keep a copy of both the original string and a clipped version. We'll also overload some functions so the class becomes aware of our new features.

Since we have memory to allocate and free up, we need both a constructor and destructor. We create a new GetLabel(), as well as SetLabel() and SetFont(). Finally, we have Width() for both setting and getting the clipping width of the label in pixels. The constructor also allows us to set the width, but an additional function is easy to add, and may be beneficial later on.

The final function ResizeText() is an internal helper function we'll look at in detail shortly. With that, our class is specified, and we can now look at implementation.

Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice