GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/textboxpopup.cpp Lines: 14 31 45.2 %
Date: 2017-11-29 Branches: 5 14 35.7 %

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-2017  The ManaPlus Developers
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/popups/textboxpopup.h"
25
26
#include "gui/widgets/textbox.h"
27
28
#include "gui/fonts/font.h"
29
30
#include "render/graphics.h"
31
32
#include "debug.h"
33
34
TextBoxPopup *textBoxPopup = nullptr;
35
36
4
TextBoxPopup::TextBoxPopup() :
37
    Popup("TextBoxPopup", "textboxpopup.xml"),
38


28
    mTextBox(new TextBox(this))
39
{
40
4
}
41
42
4
void TextBoxPopup::postInit()
43
{
44
8
    Popup::postInit();
45
4
    const int fontHeight = getFont()->getHeight();
46
4
    setMinHeight(fontHeight);
47
8
    mTextBox->setEditable(false);
48
8
    mTextBox->setOpaque(Opaque_false);
49
4
    add(mTextBox);
50
4
    addMouseListener(this);
51
4
}
52
53
4
TextBoxPopup::~TextBoxPopup()
54
{
55
4
}
56
57
void TextBoxPopup::show(const int x, const int y, const std::string &str)
58
{
59
    mTextBox->setTextWrapped(str, 190);
60
    setContentSize(mTextBox->getWidth(), mTextBox->getHeight());
61
    const int distance = 20;
62
63
    const Rect &rect = mDimension;
64
    int posX = std::max(0, x - rect.width / 2);
65
    int posY = y + distance;
66
67
    if (posX + rect.width > mainGraphics->mWidth)
68
        posX = mainGraphics->mWidth - rect.width;
69
    if (posY + rect.height > mainGraphics->mHeight)
70
        posY = y - rect.height - distance;
71
72
    setPosition(posX, posY);
73
    setVisible(Visible_true);
74
    requestMoveToTop();
75
}
76
77
void TextBoxPopup::mouseMoved(MouseEvent &event)
78
{
79
    Popup::mouseMoved(event);
80
81
    // When the mouse moved on top of the popup, hide it
82
    setVisible(Visible_false);
83
}