ManaPlus
textboxpopup.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008 The Legend of Mazzeroth Development Team
4  * Copyright (C) 2008-2009 The Mana World Development Team
5  * Copyright (C) 2009-2010 The Mana Developers
6  * Copyright (C) 2011-2019 The ManaPlus Developers
7  * Copyright (C) 2019-2021 Andrei Karas
8  *
9  * This file is part of The ManaPlus Client.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
26 
27 #include "gui/widgets/textbox.h"
28 
29 #include "gui/fonts/font.h"
30 
31 #include "render/graphics.h"
32 
33 #include "debug.h"
34 
36 
38  Popup("TextBoxPopup", "textboxpopup.xml"),
39  mTextBox(new TextBox(this))
40 {
41 }
42 
44 {
46  const int fontHeight = getFont()->getHeight();
47  setMinHeight(fontHeight);
48  mTextBox->setEditable(false);
51  getThemeColor(ThemeColorId::POPUP, 255U),
52  getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U));
53  add(mTextBox);
54  addMouseListener(this);
55 }
56 
58 {
59 }
60 
61 void TextBoxPopup::show(const int x, const int y, const std::string &str)
62 {
63  mTextBox->setTextWrapped(str, 190);
65  const int distance = 20;
66 
67  const Rect &rect = mDimension;
68  int posX = std::max(0, x - rect.width / 2);
69  int posY = y + distance;
70 
71  if (posX + rect.width > mainGraphics->mWidth)
72  posX = mainGraphics->mWidth - rect.width;
73  if (posY + rect.height > mainGraphics->mHeight)
74  posY = y - rect.height - distance;
75 
76  setPosition(posX, posY);
79 }
80 
82 {
83  Popup::mouseMoved(event);
84 
85  // When the mouse moved on top of the popup, hide it
87 }
virtual void add(Widget *const widget)
int getHeight() const
Definition: font.cpp:362
int mWidth
Definition: graphics.h:484
int mHeight
Definition: graphics.h:485
Definition: popup.h:52
void setMinHeight(const int height)
Definition: popup.cpp:207
void setContentSize(int width, int height)
Definition: popup.cpp:155
void mouseMoved(MouseEvent &event)
Definition: popup.cpp:255
void postInit()
Definition: popup.h:177
Definition: rect.h:74
int width
Definition: rect.h:219
int height
Definition: rect.h:224
void show(const int x, const int y, const std::string &str)
TextBox * mTextBox
Definition: textboxpopup.h:60
void mouseMoved(MouseEvent &event)
void setEditable(const bool editable)
Definition: textbox.cpp:650
void setOpaque(const Opaque opaque)
Definition: textbox.h:273
void setForegroundColorAll(const Color &color1, const Color &color2)
Definition: textbox.cpp:488
void setTextWrapped(const std::string &text, const int minDimension)
Definition: textbox.cpp:109
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
void setVisible(Visible visible)
Definition: widget.cpp:225
virtual void requestMoveToTop()
Definition: widget.cpp:213
Rect mDimension
Definition: widget.h:1101
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
Font * getFont() const
Definition: widget.cpp:331
void setPosition(const int x, const int y)
Definition: widget.cpp:161
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
#define new
Definition: debug_new.h:147
Graphics * mainGraphics
Definition: graphics.cpp:109
const bool Opaque_false
Definition: opaque.h:30
TextBoxPopup * textBoxPopup
const bool Visible_false
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30