GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/textboxpopup.cpp Lines: 16 33 48.5 %
Date: 2021-03-17 Branches: 4 12 33.3 %

Line Branch Exec Source
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
25
#include "gui/popups/textboxpopup.h"
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
35
TextBoxPopup *textBoxPopup = nullptr;
36
37
2
TextBoxPopup::TextBoxPopup() :
38
    Popup("TextBoxPopup", "textboxpopup.xml"),
39


14
    mTextBox(new TextBox(this))
40
{
41
2
}
42
43
2
void TextBoxPopup::postInit()
44
{
45
4
    Popup::postInit();
46
2
    const int fontHeight = getFont()->getHeight();
47
2
    setMinHeight(fontHeight);
48
2
    mTextBox->setEditable(false);
49
4
    mTextBox->setOpaque(Opaque_false);
50
6
    mTextBox->setForegroundColorAll(
51
        getThemeColor(ThemeColorId::POPUP, 255U),
52
2
        getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U));
53
2
    add(mTextBox);
54
2
    addMouseListener(this);
55
2
}
56
57
2
TextBoxPopup::~TextBoxPopup()
58
{
59
2
}
60
61
void TextBoxPopup::show(const int x, const int y, const std::string &str)
62
{
63
    mTextBox->setTextWrapped(str, 190);
64
    setContentSize(mTextBox->getWidth(), mTextBox->getHeight());
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);
77
    setVisible(Visible_true);
78
    requestMoveToTop();
79
}
80
81
void TextBoxPopup::mouseMoved(MouseEvent &event)
82
{
83
    Popup::mouseMoved(event);
84
85
    // When the mouse moved on top of the popup, hide it
86
    setVisible(Visible_false);
87
}