ManaPlus
Public Member Functions | Private Member Functions | Private Attributes
TextBox Class Reference

#include <textbox.h>

Inheritance diagram for TextBox:
Widget MouseListener KeyListener Widget2

Public Member Functions

 TextBox (const Widget2 *const widget)
 
 ~TextBox ()
 
void setTextWrapped (const std::string &text, const int minDimension)
 
int getMinWidth () const
 
void keyPressed (KeyEvent &event)
 
void draw (Graphics *const graphics)
 
void safeDraw (Graphics *const graphics)
 
void setForegroundColor (const Color &color)
 
void setForegroundColorAll (const Color &color1, const Color &color2)
 
void setText (const std::string &text)
 
std::string getText () const
 
const std::string & getTextRow (const int row) const
 
void setTextRow (const int row, const std::string &text)
 
unsigned int getNumberOfRows () const
 
unsigned int getCaretPosition () const
 
void setCaretPosition (unsigned int position)
 
unsigned int getCaretRow () const
 
void setCaretRow (const int row)
 
unsigned int getCaretColumn () const
 
void setCaretColumn (const int column)
 
void setCaretRowColumn (const int row, const int column)
 
void scrollToCaret ()
 
bool isEditable () const
 
void setEditable (const bool editable)
 
void addRow (const std::string &row)
 
bool isOpaque () const
 
void setOpaque (const Opaque opaque)
 
void fontChanged ()
 
void mousePressed (MouseEvent &event)
 
void mouseDragged (MouseEvent &event)
 
- Public Member Functions inherited from Widget
 Widget (const Widget2 *const widget)
 
 ~Widget ()
 
virtual void drawFrame (Graphics *graphics)
 
virtual void safeDrawFrame (Graphics *graphics)
 
void setFrameSize (const unsigned int frameSize)
 
unsigned int getFrameSize () const
 
virtual void logic ()
 
WidgetgetParent () const
 
void setWidth (const int width)
 
int getWidth () const
 
void setHeight (const int height)
 
int getHeight () const
 
void setSize (const int width, const int height)
 
void setX (const int x)
 
int getX () const
 
void setY (const int y)
 
int getY () const
 
void setPosition (const int x, const int y)
 
void setDimension (const Rect &dimension)
 
const RectgetDimension () const
 
void setFocusable (const bool focusable)
 
bool isFocusable () const
 
virtual bool isFocused () const
 
void setEnabled (const bool enabled)
 
bool isEnabled () const
 
void setVisible (Visible visible)
 
bool isVisible () const
 
void setBaseColor (const Color &color)
 
const ColorgetBaseColor () const
 
void setForegroundColor (const Color &color)
 
const ColorgetForegroundColor () const
 
void setBackgroundColor (const Color &color)
 
const ColorgetBackgroundColor () const
 
virtual void requestFocus ()
 
virtual void requestMoveToTop ()
 
virtual void requestMoveToBottom ()
 
virtual void setFocusHandler (FocusHandler *const focusHandler)
 
FocusHandlergetFocusHandler ()
 
void addActionListener (ActionListener *const actionListener)
 
void removeActionListener (ActionListener *const actionListener)
 
void addDeathListener (WidgetDeathListener *const deathListener)
 
void removeDeathListener (WidgetDeathListener *const deathListener)
 
void addMouseListener (MouseListener *const mouseListener)
 
void removeMouseListener (MouseListener *const mouseListener)
 
void addKeyListener (KeyListener *const keyListener)
 
void removeKeyListener (KeyListener *const keyListener)
 
void addFocusListener (FocusListener *const focusListener)
 
void removeFocusListener (FocusListener *const focusListener)
 
void addWidgetListener (WidgetListener *const widgetListener)
 
void removeWidgetListener (WidgetListener *const widgetListener)
 
void setActionEventId (const std::string &actionEventId)
 
const std::string & getActionEventId () const
 
virtual void getAbsolutePosition (int &x, int &y) const
 
virtual void setParent (Widget *parent)
 
FontgetFont () const
 
void setFont (Font *const font)
 
bool isTabInEnabled () const
 
void setTabInEnabled (const bool enabled)
 
bool isTabOutEnabled () const
 
void setTabOutEnabled (const bool enabled)
 
void requestModalFocus ()
 
virtual void requestModalMouseInputFocus ()
 
virtual void releaseModalFocus ()
 
virtual void releaseModalMouseInputFocus ()
 
virtual bool isModalFocused () const
 
virtual bool isModalMouseInputFocused () const
 
virtual WidgetgetWidgetAt (int x, int y)
 
const std::list< MouseListener * > & getMouseListeners () const A_CONST
 
const std::list< KeyListener * > & getKeyListeners () const A_CONST
 
const std::list< FocusListener * > & getFocusListeners () const A_CONST
 
virtual Rect getChildrenArea ()
 
virtual FocusHandlergetInternalFocusHandler ()
 
void setInternalFocusHandler (FocusHandler *const internalFocusHandler)
 
virtual void moveToTop (Widget *widget)
 
virtual void moveToBottom (Widget *widget)
 
virtual void focusNext ()
 
virtual void focusPrevious ()
 
virtual void showWidgetPart (Widget *const widget, const Rect &area)
 
void setId (const std::string &id)
 
const std::string & getId () const
 
virtual void showPart (const Rect &rectangle)
 
bool isAllowLogic () const
 
void setMouseConsume (const bool b)
 
bool isMouseConsume () const
 
void setRedraw (const bool b)
 
virtual bool isSelectable () const
 
void setSelectable (const bool selectable)
 
void windowResized ()
 
virtual void postInit ()
 
- Public Member Functions inherited from Widget2
virtual ~Widget2 ()
 
const ColorgetThemeColor (const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
 
const ColorgetThemeCharColor (const signed char c, bool &valid) const A_INLINE
 
virtual void setWidget2 (const Widget2 *const widget)
 
void setPalette (int palette)
 
void checkPalette ()
 
void setForegroundColor2 (const Color &color)
 
WidgetgetWindow () const
 
virtual void setWindow (Widget *const window)
 
- Public Member Functions inherited from MouseListener
virtual ~MouseListener ()
 
virtual void mouseEntered (MouseEvent &event)
 
virtual void mouseExited (MouseEvent &event)
 
virtual void mouseReleased (MouseEvent &event)
 
virtual void mouseClicked (MouseEvent &event)
 
virtual void mouseWheelMovedUp (MouseEvent &event)
 
virtual void mouseWheelMovedDown (MouseEvent &event)
 
virtual void mouseMoved (MouseEvent &event)
 
- Public Member Functions inherited from KeyListener
virtual ~KeyListener ()
 
virtual void keyReleased (KeyEvent &event)
 

Private Member Functions

void drawCaret (Graphics *const graphics, const int x, const int y) const
 
void adjustSize ()
 

Private Attributes

std::vector< std::string > mTextRows
 
int mCaretColumn
 
int mCaretRow
 
int mMinWidth
 
bool mEditable
 
Opaque mOpaque
 

Additional Inherited Members

- Static Public Member Functions inherited from Widget
static void setGlobalFont (Font *const font)
 
static FontgetGloablFont ()
 
static void cleanGlobalFont ()
 
static bool widgetExists (const Widget *const widget)
 
static void distributeWindowResizeEvent ()
 
static WidgetcallPostInit (Widget *const widget)
 
- Data Fields inherited from Widget
Visible mVisible
 
- Protected Types inherited from Widget
typedef std::list< MouseListener * > MouseListenerList
 
typedef MouseListenerList::iterator MouseListenerIterator
 
typedef std::list< KeyListener * > KeyListenerList
 
typedef KeyListenerList::iterator KeyListenerIterator
 
typedef std::list< ActionListener * > ActionListenerList
 
typedef ActionListenerList::iterator ActionListenerIterator
 
typedef std::list< WidgetDeathListener * > WidgetDeathListenerList
 
typedef WidgetDeathListenerList::iterator WidgetDeathListenerIterator
 
typedef std::list< FocusListener * > FocusListenerList
 
typedef FocusListenerList::iterator FocusListenerIterator
 
typedef std::list< WidgetListener * > WidgetListenerList
 
typedef WidgetListenerList::iterator WidgetListenerIterator
 
- Protected Member Functions inherited from Widget
void distributeActionEvent ()
 
void distributeResizedEvent ()
 
void distributeMovedEvent ()
 
void distributeHiddenEvent ()
 
void distributeShownEvent ()
 
- Protected Member Functions inherited from Widget2
 Widget2 (const Widget2 *const widget)
 
- Protected Member Functions inherited from MouseListener
 MouseListener ()
 
- Protected Member Functions inherited from KeyListener
 KeyListener ()
 
- Protected Attributes inherited from Widget
MouseListenerList mMouseListeners
 
KeyListenerList mKeyListeners
 
ActionListenerList mActionListeners
 
WidgetDeathListenerList mDeathListeners
 
FocusListenerList mFocusListeners
 
WidgetListenerList mWidgetListeners
 
Color mForegroundColor
 
Color mBackgroundColor
 
Color mBaseColor
 
Rect mDimension
 
std::string mActionEventId
 
std::string mId
 
FocusHandlermFocusHandler
 
FocusHandlermInternalFocusHandler
 
WidgetmParent
 
FontmCurrentFont
 
unsigned int mFrameSize
 
bool mFocusable
 
bool mTabIn
 
bool mTabOut
 
bool mEnabled
 
bool mAllowLogic
 
bool mMouseConsume
 
bool mRedraw
 
bool mSelectable
 
- Protected Attributes inherited from Widget2
int mPaletteOffset
 
WidgetmWindow
 
Color mForegroundColor2
 
- Static Protected Attributes inherited from Widget
static FontmGlobalFont = 0
 

Detailed Description

A text box, meant to be used inside a scroll area. Same as the Guichan text box except this one doesn't have a background or border, instead completely relying on the scroll area.

Definition at line 86 of file textbox.h.

Constructor & Destructor Documentation

◆ TextBox()

TextBox::TextBox ( const Widget2 *const  widget)
explicit

Constructor.

Definition at line 79 of file textbox.cpp.

79  :
80  Widget(widget),
81  MouseListener(),
82  KeyListener(),
83  mTextRows(),
84  mCaretColumn(0),
85  mCaretRow(0),
87  mEditable(true),
89 {
90  mAllowLogic = false;
91  setText("");
92  setFocusable(true);
93 
94  addMouseListener(this);
95  addKeyListener(this);
96  adjustSize();
97 
98  mForegroundColor = getThemeColor(ThemeColorId::TEXTBOX, 255U);
100  setFrameSize(0);
101 }
int mMinWidth
Definition: textbox.h:316
std::vector< std::string > mTextRows
Definition: textbox.h:304
void setOpaque(const Opaque opaque)
Definition: textbox.h:273
void adjustSize()
Definition: textbox.cpp:635
int mCaretRow
Definition: textbox.h:314
void setText(const std::string &text)
Definition: textbox.cpp:227
Opaque mOpaque
Definition: textbox.h:326
bool mEditable
Definition: textbox.h:321
int mCaretColumn
Definition: textbox.h:309
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
Color mForegroundColor
Definition: widget.h:1086
void setFrameSize(const unsigned int frameSize)
Definition: widget.h:168
void setFocusable(const bool focusable)
Definition: widget.cpp:192
Widget(const Widget2 *const widget)
Definition: widget.cpp:85
bool mAllowLogic
Definition: widget.h:1160
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:272
int getWidth() const
Definition: widget.h:221
const bool Opaque_false
Definition: opaque.h:30
const bool Opaque_true
Definition: opaque.h:30

References Widget::addKeyListener(), Widget::addMouseListener(), adjustSize(), Widget2::getThemeColor(), Widget::mAllowLogic, Widget::mForegroundColor, Opaque_false, Widget::setFocusable(), Widget::setFrameSize(), setOpaque(), and setText().

◆ ~TextBox()

TextBox::~TextBox ( )

Definition at line 103 of file textbox.cpp.

104 {
105  if (gui != nullptr)
106  gui->removeDragged(this);
107 }
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1162
Gui * gui
Definition: gui.cpp:111

References gui, and Gui::removeDragged().

Member Function Documentation

◆ addRow()

void TextBox::addRow ( const std::string &  row)

Adds a row of text to the end of the text.

Parameters
rowThe row to add.

Definition at line 596 of file textbox.cpp.

597 {
598  mTextRows.push_back(row);
599  adjustSize();
600 }

References adjustSize(), and mTextRows.

◆ adjustSize()

void TextBox::adjustSize ( )
private

Adjusts the text box's size to fit the text.

Definition at line 635 of file textbox.cpp.

636 {
637  int width = 0;
638  const Font *const font = getFont();
639  for (size_t i = 0, sz = mTextRows.size(); i < sz; ++i)
640  {
641  const int w = font->getWidth(mTextRows[i]);
642  if (width < w)
643  width = w;
644  }
645 
646  setWidth(width + 1);
647  setHeight(font->getHeight() * CAST_S32(mTextRows.size()));
648 }
#define CAST_S32
Definition: cast.h:30
Definition: font.h:90
int getHeight() const
Definition: font.cpp:362
int getWidth(const std::string &text) const
Definition: font.cpp:334
void setWidth(const int width)
Definition: widget.cpp:133
void setHeight(const int height)
Definition: widget.cpp:140
Font * getFont() const
Definition: widget.cpp:331

References CAST_S32, Widget::getFont(), Font::getHeight(), Font::getWidth(), mTextRows, Widget::setHeight(), and Widget::setWidth().

Referenced by addRow(), fontChanged(), keyPressed(), setText(), setTextRow(), and TextBox().

◆ draw()

void TextBox::draw ( Graphics *const  graphics)
virtual

Draws the widget. It is called by the parent widget when it is time for the widget to draw itself. The graphics object is set up so that all drawing is relative to the widget, i.e coordinate (0,0) is the top left corner of the widget. It is not possible to draw outside of a widget's dimension.

Parameters
graphicsaA graphics object to draw with.

Implements Widget.

Definition at line 446 of file textbox.cpp.

447 {
448  BLOCK_START("TextBox::draw")
449  if (mOpaque == Opaque_true)
450  {
451  graphics->setColor(mBackgroundColor);
452  graphics->fillRectangle(Rect(0, 0, getWidth(), getHeight()));
453  }
454 
455  Font *const font = getFont();
456  if (isFocused() && isEditable())
457  {
458  drawCaret(graphics, font->getWidth(
459  mTextRows[mCaretRow].substr(0, mCaretColumn)),
460  mCaretRow * font->getHeight());
461  }
462 
463  const int fontHeight = font->getHeight();
464 
465  for (size_t i = 0, sz = mTextRows.size(); i < sz; i++)
466  {
467  // Move the text one pixel so we can have a caret before a letter.
468  font->drawString(graphics,
471  mTextRows[i], 1,
472  CAST_S32(i * CAST_SIZE(fontHeight)));
473  }
474  BLOCK_END("TextBox::draw")
475 }
#define CAST_SIZE
Definition: cast.h:34
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:254
virtual void fillRectangle(const Rect &rectangle)=0
virtual void setColor(const Color &color)
Definition: graphics.h:320
Definition: rect.h:74
void drawCaret(Graphics *const graphics, const int x, const int y) const
Definition: textbox.cpp:627
bool isEditable() const
Definition: textbox.h:237
Color mForegroundColor2
Definition: widget2.h:113
Color mBackgroundColor
Definition: widget.h:1091
int getHeight() const
Definition: widget.h:240
virtual bool isFocused() const
Definition: widget.cpp:184
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79

References BLOCK_END, BLOCK_START, CAST_S32, CAST_SIZE, drawCaret(), Font::drawString(), Graphics::fillRectangle(), Widget::getFont(), Font::getHeight(), Widget::getHeight(), Widget::getWidth(), Font::getWidth(), isEditable(), Widget::isFocused(), Widget::mBackgroundColor, mCaretColumn, mCaretRow, Widget::mForegroundColor, Widget2::mForegroundColor2, mOpaque, mTextRows, Opaque_true, and Graphics::setColor().

Referenced by safeDraw().

◆ drawCaret()

void TextBox::drawCaret ( Graphics *const  graphics,
const int  x,
const int  y 
) const
private

Draws the caret. Overloaded this method if you want to change the style of the caret.

Parameters
graphicsa Graphics object to draw with.
xthe x position.
ythe y position.

Definition at line 627 of file textbox.cpp.

630 {
631  graphics->setColor(mForegroundColor);
632  graphics->drawLine(x, getFont()->getHeight() + y, x, y);
633 }
virtual void drawLine(int x1, int y1, int x2, int y2)=0

References Graphics::drawLine(), Widget::getFont(), Widget::getHeight(), Widget::mForegroundColor, Graphics::setColor(), x, and y.

Referenced by draw().

◆ fontChanged()

void TextBox::fontChanged ( )
inlinevirtual

Called when the font has changed. If the change is global, this function will only be called if the widget doesn't have a font already set.

Reimplemented from Widget.

Definition at line 276 of file textbox.h.

277  { adjustSize(); }

References adjustSize().

◆ getCaretColumn()

unsigned int TextBox::getCaretColumn ( ) const

Gets the column where the caret is currently located.

Returns
The column where the caret is currently located.
See also
setCaretColumn

◆ getCaretPosition()

unsigned int TextBox::getCaretPosition ( ) const

Gets the caret position in the text.

Returns
The caret position in the text.
See also
setCaretPosition

Definition at line 556 of file textbox.cpp.

557 {
558  int pos = 0;
559 
560  for (int row = 0; row < mCaretRow; row++)
561  pos += CAST_S32(mTextRows[row].size());
562 
563  return pos + mCaretColumn;
564 }
int size()
Definition: emotedb.cpp:306

References CAST_S32, mCaretColumn, mCaretRow, mTextRows, and EmoteDB::size().

◆ getCaretRow()

unsigned int TextBox::getCaretRow ( ) const
inline

Gets the row number where the caret is currently located.

Returns
The row number where the caret is currently located.
See also
setCaretRow

Definition at line 187 of file textbox.h.

188  { return mCaretRow; }

References mCaretRow.

◆ getMinWidth()

int TextBox::getMinWidth ( ) const
inline

Get the minimum text width for the text box.

Definition at line 108 of file textbox.h.

109  { return mMinWidth; }

References mMinWidth.

Referenced by OkDialog::OkDialog(), ConfirmDialog::postInit(), ItemPopup::setItem(), and SkillPopup::show().

◆ getNumberOfRows()

unsigned int TextBox::getNumberOfRows ( ) const
inline

Gets the number of rows in the text.

Returns
The number of rows in the text.

Definition at line 162 of file textbox.h.

163  { return CAST_S32(mTextRows.size()); }

References CAST_S32, and mTextRows.

Referenced by ConfirmDialog::postInit(), ItemPopup::setItem(), and SkillPopup::show().

◆ getText()

std::string TextBox::getText ( ) const

Gets the text of the text box.

Returns
The text of the text box.
See also
setText

Definition at line 495 of file textbox.cpp.

496 {
497  if (mTextRows.empty())
498  return std::string();
499 
500  int i;
501  std::string text;
502 
503  const int sz = CAST_S32(mTextRows.size());
504  for (i = 0; i < sz - 1; ++ i)
505  text.append(mTextRows[i]).append("\n");
506  text.append(mTextRows[i]);
507 
508  return text;
509 }

References CAST_S32, and mTextRows.

◆ getTextRow()

const std::string& TextBox::getTextRow ( const int  row) const
inline

Gets a certain row from the text.

Parameters
rowThe number of the row to get from the text.
Returns
A row from the text of the text box.
See also
setTextRow

Definition at line 145 of file textbox.h.

146  { return mTextRows[row]; }

References mTextRows.

◆ isEditable()

bool TextBox::isEditable ( ) const
inline

Checks if the text box is editable.

Returns
True it the text box is editable, false otherwise.
See also
setEditable

Definition at line 237 of file textbox.h.

238  { return mEditable; }

References mEditable.

Referenced by draw().

◆ isOpaque()

bool TextBox::isOpaque ( ) const
inline

Checks if the text box is opaque. An opaque text box will draw it's background and it's text. A non opaque text box only draw it's text making it transparent.

Returns
True if the text box is opaque, false otherwise.
See also
setOpaque

Definition at line 262 of file textbox.h.

263  { return mOpaque == Opaque_true; }

References mOpaque, and Opaque_true.

◆ keyPressed()

void TextBox::keyPressed ( KeyEvent event)
virtual

Called if a key is pressed when the widget has keyboard focus. If a key is held down the widget will generate multiple key presses.

Parameters
eventDiscribes the event.

Reimplemented from KeyListener.

Definition at line 258 of file textbox.cpp.

259 {
260  const Key &key = event.getKey();
261  const InputActionT action = event.getActionId();
262 
263  PRAGMA45(GCC diagnostic push)
264  PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
265  switch (action)
266  {
268  {
269  --mCaretColumn;
270  if (mCaretColumn < 0)
271  {
272  --mCaretRow;
273 
274  if (mCaretRow < 0)
275  {
276  mCaretRow = 0;
277  mCaretColumn = 0;
278  }
279  else
280  {
283  }
284  }
285  break;
286  }
287 
289  {
290  ++mCaretColumn;
292  {
293  ++ mCaretRow;
294 
295  const int sz = CAST_S32(mTextRows.size());
296  if (mCaretRow >= sz)
297  {
298  mCaretRow = sz - 1;
299  if (mCaretRow < 0)
300  mCaretRow = 0;
301 
304  }
305  else
306  {
307  mCaretColumn = 0;
308  }
309  }
310  break;
311  }
312 
314  {
315  setCaretRow(mCaretRow + 1);
316  break;
317  }
318  case InputAction::GUI_UP:
319  {
320  setCaretRow(mCaretRow - 1);
321  break;
322  }
324  {
325  mCaretColumn = 0;
326  break;
327  }
329  {
331  break;
332  }
333 
335  {
336  if (mEditable)
337  {
338  mTextRows.insert(mTextRows.begin() + mCaretRow + 1,
340  mTextRows[mCaretRow].size() - mCaretColumn));
342  ++mCaretRow;
343  mCaretColumn = 0;
344  }
345  break;
346  }
347 
349  {
350  if (mCaretColumn != 0 && mEditable)
351  {
352  mTextRows[mCaretRow].erase(mCaretColumn - 1, 1);
353  --mCaretColumn;
354  }
355  else if (mCaretColumn == 0 && mCaretRow != 0 && mEditable)
356  {
358  mTextRows[mCaretRow - 1].size());
360  mTextRows.erase(mTextRows.begin() + mCaretRow);
361  --mCaretRow;
362  }
363  break;
364  }
365 
367  {
368  if (mCaretColumn < CAST_S32(
370  {
371  mTextRows[mCaretRow].erase(mCaretColumn, 1);
372  }
373  else if (mCaretColumn == CAST_S32(
374  mTextRows[mCaretRow].size()) &&
375  mCaretRow < (CAST_S32(mTextRows.size()) - 1) &&
376  mEditable)
377  {
379  mTextRows.erase(mTextRows.begin() + mCaretRow + 1);
380  }
381  break;
382  }
383 
385  {
386  Widget *const par = getParent();
387 
388  if (par != nullptr)
389  {
390  const int rowsPerPage = par->getChildrenArea().height
391  / getFont()->getHeight();
392  mCaretRow -= rowsPerPage;
393 
394  if (mCaretRow < 0)
395  mCaretRow = 0;
396  }
397  break;
398  }
399 
401  {
402  Widget *const par = getParent();
403 
404  if (par != nullptr)
405  {
406  const int rowsPerPage = par->getChildrenArea().height
407  / getFont()->getHeight();
408  mCaretRow += rowsPerPage;
409 
410  const int sz = CAST_S32(mTextRows.size());
411  if (mCaretRow >= sz)
412  mCaretRow = sz - 1;
413  }
414  break;
415  }
416 
418  {
419  if (mEditable)
420  {
421  mTextRows[mCaretRow].insert(mCaretColumn, std::string(" "));
422  mCaretColumn += 4;
423  }
424  break;
425  }
426 
427  default:
428  {
429  if (key.isCharacter() && mEditable)
430  {
432  std::string(1, CAST_S8(key.getValue())));
433  ++ mCaretColumn;
434  }
435  break;
436  }
437  }
438  PRAGMA45(GCC diagnostic pop)
439 
440  adjustSize();
441  scrollToCaret();
442 
443  event.consume();
444 }
#define CAST_S8
Definition: cast.h:26
Definition: key.h:81
bool isCharacter() const
Definition: key.cpp:78
int getValue() const
Definition: key.cpp:99
int height
Definition: rect.h:224
void setCaretRow(const int row)
Definition: textbox.cpp:542
void scrollToCaret()
Definition: textbox.cpp:584
Definition: widget.h:99
virtual Rect getChildrenArea()
Definition: widget.cpp:451
Widget * getParent() const
Definition: widget.h:202
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wredundant-decls") PRAGMA45(GCC diagnostic pop) class TestMain
InputAction ::T InputActionT
Definition: inputaction.h:717

References adjustSize(), CAST_S32, CAST_S8, Widget::getChildrenArea(), Widget::getFont(), Font::getHeight(), Widget::getParent(), Key::getValue(), InputAction::GUI_BACKSPACE, InputAction::GUI_DELETE, InputAction::GUI_DOWN, InputAction::GUI_END, InputAction::GUI_HOME, InputAction::GUI_LEFT, InputAction::GUI_PAGE_DOWN, InputAction::GUI_PAGE_UP, InputAction::GUI_RIGHT, InputAction::GUI_SELECT2, InputAction::GUI_TAB, InputAction::GUI_UP, Rect::height, Key::isCharacter(), mCaretColumn, mCaretRow, mEditable, mTextRows, PRAGMA45(), scrollToCaret(), setCaretRow(), and EmoteDB::size().

◆ mouseDragged()

void TextBox::mouseDragged ( MouseEvent event)
virtual

Called when the mouse has moved and the mouse has previously been pressed on the widget.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 622 of file textbox.cpp.

623 {
624  event.consume();
625 }

◆ mousePressed()

void TextBox::mousePressed ( MouseEvent event)
virtual

Called when a mouse button has been pressed on the widget area.

NOTE: A mouse press is NOT equal to a mouse click. Use mouseClickMessage to check for mouse clicks.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 602 of file textbox.cpp.

603 {
604  if (event.getButton() == MouseButton::LEFT)
605  {
606  const int height = getFont()->getHeight();
607  if (height == 0)
608  return;
609 
610  event.consume();
611  mCaretRow = event.getY() / height;
612 
613  const int sz = CAST_S32(mTextRows.size());
614  if (mCaretRow >= sz)
615  mCaretRow = sz - 1;
616 
618  mTextRows[mCaretRow], event.getX());
619  }
620 }
int getStringIndexAt(const std::string &text, const int x) const
Definition: font.cpp:413
MouseButtonT getButton() const
Definition: mouseevent.h:116
int getX() const
Definition: mouseevent.h:127

References CAST_S32, MouseEvent::getButton(), Widget::getFont(), Font::getHeight(), Font::getStringIndexAt(), MouseEvent::getX(), MouseButton::LEFT, mCaretColumn, mCaretRow, and mTextRows.

◆ safeDraw()

void TextBox::safeDraw ( Graphics *const  graphics)
virtual

Implements Widget.

Definition at line 477 of file textbox.cpp.

478 {
479  TextBox::draw(graphics);
480 }
void draw(Graphics *const graphics)
Definition: textbox.cpp:446

References draw().

◆ scrollToCaret()

void TextBox::scrollToCaret ( )

Scrolls the text to the caret if the text box is in a scroll area.

See also
ScrollArea

Definition at line 584 of file textbox.cpp.

585 {
586  const Font *const font = getFont();
587  Rect scroll;
588  scroll.x = font->getWidth(mTextRows[mCaretRow].substr(0, mCaretColumn));
589  scroll.y = font->getHeight() * mCaretRow;
590  scroll.width = font->getWidth(" ");
591  // add 2 for some extra space
592  scroll.height = font->getHeight() + 2;
593  showPart(scroll);
594 }
int y
Definition: rect.h:214
int width
Definition: rect.h:219
int x
Definition: rect.h:209
virtual void showPart(const Rect &rectangle)
Definition: widget.cpp:511

References Widget::getFont(), Font::getHeight(), Font::getWidth(), Rect::height, mCaretColumn, mCaretRow, mTextRows, Widget::showPart(), Rect::width, Rect::x, and Rect::y.

Referenced by keyPressed().

◆ setCaretColumn()

void TextBox::setCaretColumn ( const int  column)

Sets the column where the caret should be currently located.

Parameters
Thecolumn where the caret should be currently located.
See also
getCaretColumn

Definition at line 566 of file textbox.cpp.

567 {
568  mCaretColumn = column;
569 
570  const int sz = CAST_S32(mTextRows[mCaretRow].size());
571  if (mCaretColumn > sz)
572  mCaretColumn = sz;
573 
574  if (mCaretColumn < 0)
575  mCaretColumn = 0;
576 }

References CAST_S32, mCaretColumn, mCaretRow, mTextRows, and EmoteDB::size().

Referenced by setCaretRow(), setCaretRowColumn(), and setTextRow().

◆ setCaretPosition()

void TextBox::setCaretPosition ( unsigned int  position)

Sets the position of the caret in the text.

Parameters
positionthe positon of the caret.
See also
getCaretPosition

Definition at line 521 of file textbox.cpp.

522 {
523  for (int row = 0, fsz = CAST_S32(mTextRows.size());
524  row < fsz;
525  row ++)
526  {
527  if (position <= mTextRows[row].size())
528  {
529  mCaretRow = row;
530  mCaretColumn = position;
531  return; // we are done
532  }
533 
534  position--;
535  }
536 
537  // position beyond end of text
538  mCaretRow = CAST_S32(mTextRows.size() - 1);
540 }

References CAST_S32, mCaretColumn, mCaretRow, mTextRows, and EmoteDB::size().

◆ setCaretRow()

void TextBox::setCaretRow ( const int  row)

Sets the row where the caret should be currently located.

Parameters
Therow where the caret should be currently located.
See also
getCaretRow

Definition at line 542 of file textbox.cpp.

543 {
544  mCaretRow = row;
545 
546  const int sz = CAST_S32(mTextRows.size());
547  if (mCaretRow >= sz)
548  mCaretRow = sz - 1;
549 
550  if (mCaretRow < 0)
551  mCaretRow = 0;
552 
554 }
void setCaretColumn(const int column)
Definition: textbox.cpp:566

References CAST_S32, mCaretColumn, mCaretRow, mTextRows, and setCaretColumn().

Referenced by keyPressed(), and setCaretRowColumn().

◆ setCaretRowColumn()

void TextBox::setCaretRowColumn ( const int  row,
const int  column 
)

Sets the row and the column where the caret should be curretly located.

Parameters
rowThe row where the caret should be currently located.
columnThe column where the caret should be currently located.
See also
getCaretRow, getCaretColumn

Definition at line 578 of file textbox.cpp.

579 {
580  setCaretRow(row);
581  setCaretColumn(column);
582 }

References setCaretColumn(), and setCaretRow().

◆ setEditable()

void TextBox::setEditable ( const bool  editable)

Sets the text box to be editable or not.

Parameters
editableTrue if the text box should be editable, false otherwise.

Definition at line 650 of file textbox.cpp.

651 {
652  mEditable = editable;
653  mSelectable = editable;
654 }
bool mSelectable
Definition: widget.h:1166

References mEditable, and Widget::mSelectable.

Referenced by ConfirmDialog::ConfirmDialog(), ItemPopup::ItemPopup(), OkDialog::OkDialog(), TextBoxPopup::postInit(), and SkillPopup::SkillPopup().

◆ setForegroundColor()

void TextBox::setForegroundColor ( const Color color)

Definition at line 482 of file textbox.cpp.

483 {
484  mForegroundColor = color;
485  mForegroundColor2 = color;
486 }

References Widget::mForegroundColor, and Widget2::mForegroundColor2.

◆ setForegroundColorAll()

void TextBox::setForegroundColorAll ( const Color color1,
const Color color2 
)

Definition at line 488 of file textbox.cpp.

490 {
491  mForegroundColor = color1;
492  mForegroundColor2 = color2;
493 }

References Widget::mForegroundColor, and Widget2::mForegroundColor2.

Referenced by ItemPopup::ItemPopup(), TextBoxPopup::postInit(), and SkillPopup::SkillPopup().

◆ setOpaque()

void TextBox::setOpaque ( const Opaque  opaque)
inline

Sets the text box to be opaque or not. An opaque text box will draw it's background and it's text. A non opaque text box only draw it's text making it transparent.

Parameters
opaqueTrue if the text box should be opaque, false otherwise.
See also
isOpaque

Definition at line 273 of file textbox.h.

274  { mOpaque = opaque; }

References mOpaque.

Referenced by ConfirmDialog::ConfirmDialog(), OkDialog::OkDialog(), TextBoxPopup::postInit(), and TextBox().

◆ setText()

void TextBox::setText ( const std::string &  text)

Sets the text of the text box.

Parameters
textThe text of the text box.
See also
getText

Definition at line 227 of file textbox.cpp.

228 {
229  mCaretColumn = 0;
230  mCaretRow = 0;
231 
232  mTextRows.clear();
233  if (text.empty())
234  {
235  adjustSize();
236  return;
237  }
238 
239  size_t pos;
240  size_t lastPos = 0;
241  int length;
242  do
243  {
244  pos = text.find('\n', lastPos);
245 
246  if (pos != std::string::npos)
247  length = CAST_S32(pos - lastPos);
248  else
249  length = CAST_S32(text.size() - lastPos);
250  std::string sub = text.substr(lastPos, length);
251  mTextRows.push_back(sub);
252  lastPos = pos + 1;
253  } while (pos != std::string::npos);
254 
255  adjustSize();
256 }

References adjustSize(), CAST_S32, mCaretColumn, mCaretRow, and mTextRows.

Referenced by setTextWrapped(), and TextBox().

◆ setTextRow()

void TextBox::setTextRow ( const int  row,
const std::string &  text 
)

Sets the text of a certain row of the text.

Parameters
rowThe number of the row to set in the text.
textThe text to set in the given row number.
See also
getTextRow

Definition at line 511 of file textbox.cpp.

512 {
513  mTextRows[row] = text;
514 
515  if (mCaretRow == row)
517 
518  adjustSize();
519 }

References adjustSize(), mCaretColumn, mCaretRow, mTextRows, and setCaretColumn().

◆ setTextWrapped()

void TextBox::setTextWrapped ( const std::string &  text,
const int  minDimension 
)

Sets the text after wrapping it to the current width of the widget.

Definition at line 109 of file textbox.cpp.

110 {
111  // Make sure parent scroll area sets width of this widget
112  if (getParent() != nullptr)
113  getParent()->logic();
114 
115  // Take the supplied minimum dimension as a starting
116  // point and try to beat it
117  mMinWidth = minDimension;
118 
119  const size_t textSize = text.size();
120  size_t spacePos = text.rfind(' ', textSize);
121 
122  if (spacePos != std::string::npos)
123  {
124  const std::string word = text.substr(spacePos + 1);
125  const int length = getFont()->getWidth(word);
126 
127  if (length > mMinWidth)
128  mMinWidth = length;
129  }
130 
131  std::stringstream wrappedStream;
132  size_t newlinePos;
133  size_t lastNewlinePos = 0;
134  int minWidth = 0;
135  int xpos;
136 
137  do
138  {
139  // Determine next piece of string to wrap
140  newlinePos = text.find('\n', lastNewlinePos);
141 
142  if (newlinePos == std::string::npos)
143  newlinePos = textSize;
144 
145  std::string line =
146  text.substr(lastNewlinePos, newlinePos - lastNewlinePos);
147  size_t lastSpacePos = 0;
148  xpos = 0;
149  const Font *const font = getFont();
150  const int spaceWidth = font->getWidth(" ");
151  size_t sz = line.size();
152 
153  do
154  {
155  spacePos = line.find(' ', lastSpacePos);
156 
157  if (spacePos == std::string::npos)
158  spacePos = sz;
159 
160  const std::string word =
161  line.substr(lastSpacePos, spacePos - lastSpacePos);
162 
163  const int width = font->getWidth(word);
164 
165  if (xpos == 0 && width > mMinWidth)
166  {
167  mMinWidth = width;
168  xpos = width;
169  wrappedStream << word;
170  }
171  else if (xpos != 0 && xpos + spaceWidth + width <=
172  mMinWidth)
173  {
174  xpos += spaceWidth + width;
175  wrappedStream << " " << word;
176  }
177  else if (lastSpacePos == 0)
178  {
179  xpos += width;
180  wrappedStream << word;
181  }
182  else
183  {
184  if (xpos > minWidth)
185  minWidth = xpos;
186 
187  // The window wasn't big enough. Resize it and try again.
188  if (minWidth > mMinWidth)
189  {
190  mMinWidth = minWidth;
191  wrappedStream.clear();
192  wrappedStream.str("");
193  lastNewlinePos = 0;
194  newlinePos = text.find('\n', lastNewlinePos);
195  if (newlinePos == std::string::npos)
196  newlinePos = textSize;
197  line = text.substr(lastNewlinePos, newlinePos -
198  lastNewlinePos);
199  sz = line.size();
200  break;
201  }
202  else
203  {
204  wrappedStream << "\n" << word;
205  }
206  xpos = width;
207  }
208  lastSpacePos = spacePos + 1;
209  }
210  while (spacePos != sz);
211 
212  if (text.find('\n', lastNewlinePos) != std::string::npos)
213  wrappedStream << "\n";
214 
215  lastNewlinePos = newlinePos + 1;
216  }
217  while (newlinePos != textSize);
218 
219  if (xpos > minWidth)
220  minWidth = xpos;
221 
222  mMinWidth = minWidth;
223 
224  setText(wrappedStream.str());
225 }
virtual void logic()
Definition: widget.h:193

References Widget::getFont(), Widget::getParent(), Font::getWidth(), Widget::logic(), mMinWidth, and setText().

Referenced by ConfirmDialog::ConfirmDialog(), OkDialog::OkDialog(), ItemPopup::setItem(), TextBoxPopup::show(), and SkillPopup::show().

Field Documentation

◆ mCaretColumn

int TextBox::mCaretColumn
private

Holds the current column of the caret.

Definition at line 309 of file textbox.h.

Referenced by draw(), getCaretPosition(), keyPressed(), mousePressed(), scrollToCaret(), setCaretColumn(), setCaretPosition(), setCaretRow(), setText(), and setTextRow().

◆ mCaretRow

int TextBox::mCaretRow
private

Holds the current row of the caret.

Definition at line 314 of file textbox.h.

Referenced by draw(), getCaretPosition(), getCaretRow(), keyPressed(), mousePressed(), scrollToCaret(), setCaretColumn(), setCaretPosition(), setCaretRow(), setText(), and setTextRow().

◆ mEditable

bool TextBox::mEditable
private

True if the text box is editable, false otherwise.

Definition at line 321 of file textbox.h.

Referenced by isEditable(), keyPressed(), and setEditable().

◆ mMinWidth

int TextBox::mMinWidth
private

Definition at line 316 of file textbox.h.

Referenced by getMinWidth(), and setTextWrapped().

◆ mOpaque

Opaque TextBox::mOpaque
private

True if the text box is editable, false otherwise.

Definition at line 326 of file textbox.h.

Referenced by draw(), isOpaque(), and setOpaque().

◆ mTextRows

std::vector<std::string> TextBox::mTextRows
private

The documentation for this class was generated from the following files: