TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTOPHER BUTLER
from: DARIN MCBRIDE
date: 1998-04-03 21:33:00
subject: Help!

 CB> How would I go about writing an EditBox class?
Hm?  You mean a _text_ editbox?  Interesting idea!
 CB> I'm using DJGPP, and would also like to be able to use 
 CB> it on other versions of GCC (ie Linux, etc), so I'd 
 CB> prefer not to have many OS-specific things :)
Well, as soon as you start manipulating text like that, you're getting fairly 
OS-specific.  One good way around this, however, is to use a library, such as 
VidMgr (in snippets, I believe) to handle the actual updating of the screen.  
After that, creating an editbox should be relatively trivial.
 CB> I'd like to be able to make it simply a class that has 
 CB> "events" (keypresses) passed to a handling function, so 
 CB> that I can have more than one thing on the screen at 
 CB> once, with one of them having focus.
Unless you wrote an entire TUI (text user interface) library that would do 
all your message passing, this would still be manual... I can see it being 
trivial with the correct design, though.
Sample (sometimes pseudo) code - note that I wouldn't normally do this 
ne.
class EditBox
{
  // ...
private:
  string data;
  size_t curpos;
//size_t endpos; // if you want to try to handle shift-key to "select"
                 // don't recommend trying at first 
  size_t max_strlen;
public:
  const size_t NO_MAXIMUM = (size_t)-1;
public:
  string GetString() const { return data; }
  void   SetString(const string& s) { data = s; Repaint(); }
  size_t GetCursorPosition() const { return curpos; }
  void   SetCursorPosition(size_t pos) 
  {
    if (pos < data.length())
    {
      curpos = pos;
      // if you use endpos...
      //if (endpos < pos) endpos = curpos;
    }
    else
      SetCursorPosition(data.length() - 1);
  }
  size_t GetMaxLength() const { return max_strlen; }
  void   SetMaxLength(size_t max)
  {
    max_strlen = max;
    if (data.length() > max)
    {
      data = string(data, max);
      Repaint();
    }
  }
  void HandleKey(int nKeyScanCode)
  {
    if (KeyPressed(nKeyScanCode))
    {              
      if (nKeyScanCode == VK_BACKSPACE)
      {
        data.remove(curpos, endpos); // remove all from beginning to end
      }
      else if (isprint(nKeyScanCode)) // can we insert it (printable)?
      {
        if (max_strlen == NO_MAXIMUM ||
            data.length() == max_strlen())
        {
          beep(); // ignore it - too long
        }
        else if (GlobalIsInsertOn())
        {
          data.insert(curpos, nKeyScanCode);
        }
        else
        {
          data[curpose] = (char)nKeyScanCode;
        }
      }
      Repaint();
    }
  }
  // the following function is the one to derive from.
  // It should return false if the key should be ignored
  // Otherwise, return true.  Note that it can change the key as
  // required - perhaps uppercasing it, etc.
  virtual bool KeyPressed(int& nKeyScanCode)
  { return true; } // default of not changing the scan code and not killing 

  virtual void Repaint()
  // no code here for ya ... you have to repaint the string to the screen.
  // you may want to put _'s after the end of the string (past 
ata.length()),
  // for example.  You will need data members to tell you where you are on
  // the screen, how much space you have, ...
};
I hope this gives you a good start... :-)
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.