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 85 of file textbox.h.

Constructor & Destructor Documentation

◆ TextBox()

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

Constructor.

Definition at line 78 of file textbox.cpp.

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

78  :
79  Widget(widget),
80  MouseListener(),
81  KeyListener(),
82  mTextRows(),
83  mCaretColumn(0),
84  mCaretRow(0),
86  mEditable(true),
88 {
89  mAllowLogic = false;
90  setText("");
91  setFocusable(true);
92 
93  addMouseListener(this);
94  addKeyListener(this);
95  adjustSize();
96 
99  setFrameSize(0);
100 }
int mCaretColumn
Definition: textbox.h:309
int getWidth() const
Definition: widget.h:220
std::vector< std::string > mTextRows
Definition: textbox.h:304
int mMinWidth
Definition: textbox.h:316
Color mForegroundColor
Definition: widget.h:1085
void setFrameSize(const unsigned int frameSize)
Definition: widget.h:167
void adjustSize()
Definition: textbox.cpp:634
Widget(const Widget2 *const widget)
Definition: widget.cpp:84
bool mAllowLogic
Definition: widget.h:1159
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
const bool Opaque_false
Definition: opaque.h:29
bool mEditable
Definition: textbox.h:321
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
int mCaretRow
Definition: textbox.h:314
void setOpaque(const Opaque opaque)
Definition: textbox.h:273
void setFocusable(const bool focusable)
Definition: widget.cpp:191
const bool Opaque_true
Definition: opaque.h:29
Opaque mOpaque
Definition: textbox.h:326
void setText(const std::string &text)
Definition: textbox.cpp:226
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44

◆ ~TextBox()

TextBox::~TextBox ( )

Definition at line 102 of file textbox.cpp.

References gui, and Gui::removeDragged().

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

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 595 of file textbox.cpp.

References adjustSize(), and mTextRows.

Referenced by setEditable().

596 {
597  mTextRows.push_back(row);
598  adjustSize();
599 }
std::vector< std::string > mTextRows
Definition: textbox.h:304
void adjustSize()
Definition: textbox.cpp:634

◆ adjustSize()

void TextBox::adjustSize ( )
private

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

Definition at line 634 of file textbox.cpp.

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

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

635 {
636  int width = 0;
637  const Font *const font = getFont();
638  for (size_t i = 0, sz = mTextRows.size(); i < sz; ++i)
639  {
640  const int w = font->getWidth(mTextRows[i]);
641  if (width < w)
642  width = w;
643  }
644 
645  setWidth(width + 1);
646  setHeight(font->getHeight() * CAST_S32(mTextRows.size()));
647 }
Font * getFont() const
Definition: widget.cpp:330
void setWidth(const int width)
Definition: widget.cpp:132
std::vector< std::string > mTextRows
Definition: textbox.h:304
Definition: font.h:88
#define CAST_S32
Definition: cast.h:29
void setHeight(const int height)
Definition: widget.cpp:139
int getWidth(const std::string &text) const
Definition: font.cpp:333
int getHeight() const
Definition: font.cpp:360

◆ 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 445 of file textbox.cpp.

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

Referenced by getMinWidth(), and safeDraw().

446 {
447  BLOCK_START("TextBox::draw")
448  if (mOpaque == Opaque_true)
449  {
450  graphics->setColor(mBackgroundColor);
451  graphics->fillRectangle(Rect(0, 0, getWidth(), getHeight()));
452  }
453 
454  Font *const font = getFont();
455  if (isFocused() && isEditable())
456  {
457  drawCaret(graphics, font->getWidth(
458  mTextRows[mCaretRow].substr(0, mCaretColumn)),
459  mCaretRow * font->getHeight());
460  }
461 
462  const int fontHeight = font->getHeight();
463 
464  for (size_t i = 0, sz = mTextRows.size(); i < sz; i++)
465  {
466  // Move the text one pixel so we can have a caret before a letter.
467  font->drawString(graphics,
470  mTextRows[i], 1,
471  CAST_S32(i * CAST_SIZE(fontHeight)));
472  }
473  BLOCK_END("TextBox::draw")
474 }
Font * getFont() const
Definition: widget.cpp:330
int mCaretColumn
Definition: textbox.h:309
int getWidth() const
Definition: widget.h:220
std::vector< std::string > mTextRows
Definition: textbox.h:304
Definition: font.h:88
Definition: rect.h:72
#define BLOCK_START(name)
Definition: perfomance.h:78
void drawCaret(Graphics *const graphics, const int x, const int y) const
Definition: textbox.cpp:626
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:79
Color mForegroundColor
Definition: widget.h:1085
virtual bool isFocused() const
Definition: widget.cpp:183
Color mBackgroundColor
Definition: widget.h:1090
virtual void setColor(const Color &color)
Definition: graphics.h:319
#define CAST_S32
Definition: cast.h:29
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:253
int mCaretRow
Definition: textbox.h:314
virtual void fillRectangle(const Rect &rectangle)=0
Color mForegroundColor2
Definition: widget2.h:112
const bool Opaque_true
Definition: opaque.h:29
int getWidth(const std::string &text) const
Definition: font.cpp:333
Opaque mOpaque
Definition: textbox.h:326
int getHeight() const
Definition: widget.h:239
#define CAST_SIZE
Definition: cast.h:33
int getHeight() const
Definition: font.cpp:360
bool isEditable() const
Definition: textbox.h:236

◆ 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 626 of file textbox.cpp.

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

Referenced by draw(), and fontChanged().

629 {
630  graphics->setColor(mForegroundColor);
631  graphics->drawLine(x, getFont()->getHeight() + y, x, y);
632 }
Font * getFont() const
Definition: widget.cpp:330
Color mForegroundColor
Definition: widget.h:1085
virtual void setColor(const Color &color)
Definition: graphics.h:319
virtual void drawLine(int x1, int y1, int x2, int y2)=0
int getHeight() const
Definition: widget.h:239

◆ 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.

References A_NONNULL, adjustSize(), drawCaret(), mouseDragged(), mousePressed(), x, and y.

277  { adjustSize(); }
void adjustSize()
Definition: textbox.cpp:634

◆ 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

Referenced by getCaretRow().

◆ 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 555 of file textbox.cpp.

References CAST_S32, mCaretColumn, mCaretRow, mTextRows, and ServerCommandType::size.

Referenced by getNumberOfRows().

556 {
557  int pos = 0, row;
558 
559  for (row = 0; row < mCaretRow; row++)
560  pos += CAST_S32(mTextRows[row].size());
561 
562  return pos + mCaretColumn;
563 }
int mCaretColumn
Definition: textbox.h:309
std::vector< std::string > mTextRows
Definition: textbox.h:304
#define CAST_S32
Definition: cast.h:29
int mCaretRow
Definition: textbox.h:314

◆ 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 186 of file textbox.h.

References getCaretColumn(), mCaretRow, scrollToCaret(), setCaretColumn(), setCaretRow(), and setCaretRowColumn().

187  { return mCaretRow; }
int mCaretRow
Definition: textbox.h:314

◆ getMinWidth()

int TextBox::getMinWidth ( ) const
inline

Get the minimum text width for the text box.

Definition at line 107 of file textbox.h.

References A_NONNULL, draw(), getText(), keyPressed(), mMinWidth, safeDraw(), setForegroundColor(), setForegroundColorAll(), and setText().

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

108  { return mMinWidth; }
int mMinWidth
Definition: textbox.h:316

◆ 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 161 of file textbox.h.

References CAST_S32, getCaretPosition(), mTextRows, and setCaretPosition().

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

162  { return CAST_S32(mTextRows.size()); }
std::vector< std::string > mTextRows
Definition: textbox.h:304
#define CAST_S32
Definition: cast.h:29

◆ 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 494 of file textbox.cpp.

References CAST_S32, and mTextRows.

Referenced by getMinWidth().

495 {
496  if (mTextRows.empty())
497  return std::string();
498 
499  int i;
500  std::string text;
501 
502  const int sz = CAST_S32(mTextRows.size());
503  for (i = 0; i < sz - 1; ++ i)
504  text.append(mTextRows[i]).append("\n");
505  text.append(mTextRows[i]);
506 
507  return text;
508 }
std::vector< std::string > mTextRows
Definition: textbox.h:304
#define CAST_S32
Definition: cast.h:29

◆ 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 144 of file textbox.h.

References mTextRows, and setTextRow().

145  { return mTextRows[row]; }
std::vector< std::string > mTextRows
Definition: textbox.h:304

◆ 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 236 of file textbox.h.

References mEditable.

Referenced by draw().

237  { return mEditable; }
bool mEditable
Definition: textbox.h:321

◆ 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.

References mOpaque, and Opaque_true.

263  { return mOpaque == Opaque_true; }
const bool Opaque_true
Definition: opaque.h:29
Opaque mOpaque
Definition: textbox.h:326

◆ 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 257 of file textbox.cpp.

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 ServerCommandType::size.

Referenced by getMinWidth().

258 {
259  const Key &key = event.getKey();
260  const InputActionT action = event.getActionId();
261 
262  PRAGMA45(GCC diagnostic push)
263  PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
264  switch (action)
265  {
267  {
268  --mCaretColumn;
269  if (mCaretColumn < 0)
270  {
271  --mCaretRow;
272 
273  if (mCaretRow < 0)
274  {
275  mCaretRow = 0;
276  mCaretColumn = 0;
277  }
278  else
279  {
282  }
283  }
284  break;
285  }
286 
288  {
289  ++mCaretColumn;
291  {
292  ++ mCaretRow;
293 
294  const int sz = CAST_S32(mTextRows.size());
295  if (mCaretRow >= sz)
296  {
297  mCaretRow = sz - 1;
298  if (mCaretRow < 0)
299  mCaretRow = 0;
300 
303  }
304  else
305  {
306  mCaretColumn = 0;
307  }
308  }
309  break;
310  }
311 
313  {
314  setCaretRow(mCaretRow + 1);
315  break;
316  }
317  case InputAction::GUI_UP:
318  {
319  setCaretRow(mCaretRow - 1);
320  break;
321  }
323  {
324  mCaretColumn = 0;
325  break;
326  }
328  {
330  break;
331  }
332 
334  {
335  if (mEditable)
336  {
337  mTextRows.insert(mTextRows.begin() + mCaretRow + 1,
341  ++mCaretRow;
342  mCaretColumn = 0;
343  }
344  break;
345  }
346 
348  {
349  if (mCaretColumn != 0 && mEditable)
350  {
351  mTextRows[mCaretRow].erase(mCaretColumn - 1, 1);
352  --mCaretColumn;
353  }
354  else if (mCaretColumn == 0 && mCaretRow != 0 && mEditable)
355  {
357  mTextRows[mCaretRow - 1].size());
359  mTextRows.erase(mTextRows.begin() + mCaretRow);
360  --mCaretRow;
361  }
362  break;
363  }
364 
366  {
367  if (mCaretColumn < CAST_S32(
369  {
370  mTextRows[mCaretRow].erase(mCaretColumn, 1);
371  }
372  else if (mCaretColumn == CAST_S32(
373  mTextRows[mCaretRow].size()) &&
374  mCaretRow < (CAST_S32(mTextRows.size()) - 1) &&
375  mEditable)
376  {
378  mTextRows.erase(mTextRows.begin() + mCaretRow + 1);
379  }
380  break;
381  }
382 
384  {
385  Widget *const par = getParent();
386 
387  if (par != nullptr)
388  {
389  const int rowsPerPage = par->getChildrenArea().height
390  / getFont()->getHeight();
391  mCaretRow -= rowsPerPage;
392 
393  if (mCaretRow < 0)
394  mCaretRow = 0;
395  }
396  break;
397  }
398 
400  {
401  Widget *const par = getParent();
402 
403  if (par != nullptr)
404  {
405  const int rowsPerPage = par->getChildrenArea().height
406  / getFont()->getHeight();
407  mCaretRow += rowsPerPage;
408 
409  const int sz = CAST_S32(mTextRows.size());
410  if (mCaretRow >= sz)
411  mCaretRow = sz - 1;
412  }
413  break;
414  }
415 
417  {
418  if (mEditable)
419  {
420  mTextRows[mCaretRow].insert(mCaretColumn, std::string(" "));
421  mCaretColumn += 4;
422  }
423  break;
424  }
425 
426  default:
427  {
428  if (key.isCharacter() && mEditable)
429  {
431  std::string(1, CAST_S8(key.getValue())));
432  ++ mCaretColumn;
433  }
434  break;
435  }
436  }
437  PRAGMA45(GCC diagnostic pop)
438 
439  adjustSize();
440  scrollToCaret();
441 
442  event.consume();
443 }
Font * getFont() const
Definition: widget.cpp:330
int getValue() const
Definition: key.cpp:98
int mCaretColumn
Definition: textbox.h:309
std::vector< std::string > mTextRows
Definition: textbox.h:304
bool isCharacter() const
Definition: key.cpp:77
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wunused-result") int TestLauncher
#define CAST_S32
Definition: cast.h:29
void scrollToCaret()
Definition: textbox.cpp:583
InputAction ::T InputActionT
Definition: inputaction.h:714
void adjustSize()
Definition: textbox.cpp:634
#define CAST_S8
Definition: cast.h:25
int height
Definition: rect.h:223
bool mEditable
Definition: textbox.h:321
int mCaretRow
Definition: textbox.h:314
Widget * getParent() const
Definition: widget.h:201
Definition: widget.h:97
Definition: key.h:79
void setCaretRow(const int row)
Definition: textbox.cpp:541
int getHeight() const
Definition: font.cpp:360
virtual Rect getChildrenArea()
Definition: widget.cpp:450

◆ 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 621 of file textbox.cpp.

Referenced by fontChanged().

622 {
623  event.consume();
624 }

◆ 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 601 of file textbox.cpp.

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

Referenced by fontChanged().

602 {
603  if (event.getButton() == MouseButton::LEFT)
604  {
605  const int height = getFont()->getHeight();
606  if (height == 0)
607  return;
608 
609  event.consume();
610  mCaretRow = event.getY() / height;
611 
612  const int sz = CAST_S32(mTextRows.size());
613  if (mCaretRow >= sz)
614  mCaretRow = sz - 1;
615 
617  mTextRows[mCaretRow], event.getX());
618  }
619 }
Font * getFont() const
Definition: widget.cpp:330
int mCaretColumn
Definition: textbox.h:309
std::vector< std::string > mTextRows
Definition: textbox.h:304
MouseButtonT getButton() const
Definition: mouseevent.h:115
int getStringIndexAt(const std::string &text, const int x) const
Definition: font.cpp:411
#define CAST_S32
Definition: cast.h:29
int mCaretRow
Definition: textbox.h:314
int getX() const
Definition: mouseevent.h:126
int getHeight() const
Definition: font.cpp:360

◆ safeDraw()

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

Implements Widget.

Definition at line 476 of file textbox.cpp.

References draw().

Referenced by getMinWidth().

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

◆ 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 583 of file textbox.cpp.

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

Referenced by getCaretRow(), and keyPressed().

584 {
585  const Font *const font = getFont();
586  Rect scroll;
587  scroll.x = font->getWidth(mTextRows[mCaretRow].substr(0, mCaretColumn));
588  scroll.y = font->getHeight() * mCaretRow;
589  scroll.width = font->getWidth(" ");
590  // add 2 for some extra space
591  scroll.height = font->getHeight() + 2;
592  showPart(scroll);
593 }
Font * getFont() const
Definition: widget.cpp:330
int width
Definition: rect.h:218
int mCaretColumn
Definition: textbox.h:309
std::vector< std::string > mTextRows
Definition: textbox.h:304
Definition: font.h:88
Definition: rect.h:72
int x
Definition: rect.h:208
int y
Definition: rect.h:213
int height
Definition: rect.h:223
int mCaretRow
Definition: textbox.h:314
int getWidth(const std::string &text) const
Definition: font.cpp:333
virtual void showPart(const Rect &rectangle)
Definition: widget.cpp:510
int getHeight() const
Definition: font.cpp:360

◆ 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 565 of file textbox.cpp.

References CAST_S32, mCaretColumn, mCaretRow, mTextRows, and ServerCommandType::size.

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

566 {
567  mCaretColumn = column;
568 
569  const int sz = CAST_S32(mTextRows[mCaretRow].size());
570  if (mCaretColumn > sz)
571  mCaretColumn = sz;
572 
573  if (mCaretColumn < 0)
574  mCaretColumn = 0;
575 }
int mCaretColumn
Definition: textbox.h:309
std::vector< std::string > mTextRows
Definition: textbox.h:304
#define CAST_S32
Definition: cast.h:29
int mCaretRow
Definition: textbox.h:314

◆ 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 520 of file textbox.cpp.

References CAST_S32, mCaretColumn, mCaretRow, mTextRows, and ServerCommandType::size.

Referenced by getNumberOfRows().

521 {
522  for (int row = 0, fsz = CAST_S32(mTextRows.size());
523  row < fsz;
524  row ++)
525  {
526  if (position <= mTextRows[row].size())
527  {
528  mCaretRow = row;
529  mCaretColumn = position;
530  return; // we are done
531  }
532 
533  position--;
534  }
535 
536  // position beyond end of text
537  mCaretRow = CAST_S32(mTextRows.size() - 1);
539 }
int mCaretColumn
Definition: textbox.h:309
std::vector< std::string > mTextRows
Definition: textbox.h:304
#define CAST_S32
Definition: cast.h:29
int mCaretRow
Definition: textbox.h:314

◆ 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 541 of file textbox.cpp.

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

Referenced by getCaretRow(), keyPressed(), and setCaretRowColumn().

542 {
543  mCaretRow = row;
544 
545  const int sz = CAST_S32(mTextRows.size());
546  if (mCaretRow >= sz)
547  mCaretRow = sz - 1;
548 
549  if (mCaretRow < 0)
550  mCaretRow = 0;
551 
553 }
int mCaretColumn
Definition: textbox.h:309
std::vector< std::string > mTextRows
Definition: textbox.h:304
#define CAST_S32
Definition: cast.h:29
int mCaretRow
Definition: textbox.h:314
void setCaretColumn(const int column)
Definition: textbox.cpp:565

◆ 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 577 of file textbox.cpp.

References setCaretColumn(), and setCaretRow().

Referenced by getCaretRow().

578 {
579  setCaretRow(row);
580  setCaretColumn(column);
581 }
void setCaretRow(const int row)
Definition: textbox.cpp:541
void setCaretColumn(const int column)
Definition: textbox.cpp:565

◆ setEditable()

void TextBox::setEditable ( const bool  editable)
inline

Sets the text box to be editable or not.

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

Definition at line 244 of file textbox.h.

References addRow(), and mEditable.

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

245  { mEditable = editable; }
bool mEditable
Definition: textbox.h:321

◆ setForegroundColor()

void TextBox::setForegroundColor ( const Color color)

Definition at line 481 of file textbox.cpp.

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

Referenced by getMinWidth().

482 {
483  mForegroundColor = color;
484  mForegroundColor2 = color;
485 }
Color mForegroundColor
Definition: widget.h:1085
Color mForegroundColor2
Definition: widget2.h:112

◆ setForegroundColorAll()

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

Definition at line 487 of file textbox.cpp.

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

Referenced by getMinWidth(), ItemPopup::ItemPopup(), and SkillPopup::SkillPopup().

489 {
490  mForegroundColor = color1;
491  mForegroundColor2 = color2;
492 }
Color mForegroundColor
Definition: widget.h:1085
Color mForegroundColor2
Definition: widget2.h:112

◆ 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.

References mOpaque.

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

274  { mOpaque = opaque; }
Opaque mOpaque
Definition: textbox.h:326

◆ 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 226 of file textbox.cpp.

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

Referenced by getMinWidth(), setTextWrapped(), and TextBox().

227 {
228  mCaretColumn = 0;
229  mCaretRow = 0;
230 
231  mTextRows.clear();
232  if (text.empty())
233  {
234  adjustSize();
235  return;
236  }
237 
238  size_t pos;
239  size_t lastPos = 0;
240  int length;
241  do
242  {
243  pos = text.find('\n', lastPos);
244 
245  if (pos != std::string::npos)
246  length = CAST_S32(pos - lastPos);
247  else
248  length = CAST_S32(text.size() - lastPos);
249  std::string sub = text.substr(lastPos, length);
250  mTextRows.push_back(sub);
251  lastPos = pos + 1;
252  } while (pos != std::string::npos);
253 
254  adjustSize();
255 }
int mCaretColumn
Definition: textbox.h:309
std::vector< std::string > mTextRows
Definition: textbox.h:304
#define CAST_S32
Definition: cast.h:29
void adjustSize()
Definition: textbox.cpp:634
int mCaretRow
Definition: textbox.h:314

◆ 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 510 of file textbox.cpp.

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

Referenced by getTextRow().

511 {
512  mTextRows[row] = text;
513 
514  if (mCaretRow == row)
516 
517  adjustSize();
518 }
int mCaretColumn
Definition: textbox.h:309
std::vector< std::string > mTextRows
Definition: textbox.h:304
void adjustSize()
Definition: textbox.cpp:634
int mCaretRow
Definition: textbox.h:314
void setCaretColumn(const int column)
Definition: textbox.cpp:565

◆ 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 108 of file textbox.cpp.

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

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

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

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: