GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/textpopup.cpp Lines: 18 52 34.6 %
Date: 2017-11-29 Branches: 8 24 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-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/textpopup.h"
25
26
#include "gui/widgets/label.h"
27
28
#include "gui/fonts/font.h"
29
30
#include "debug.h"
31
32
TextPopup *textPopup = nullptr;
33
34
4
TextPopup::TextPopup() :
35
    Popup("TextPopup", "textpopup.xml"),
36


28
    mText()
37
{
38
4
}
39
40
4
void TextPopup::postInit()
41
{
42
8
    Popup::postInit();
43
4
    const int fontHeight = getFont()->getHeight();
44
4
    int y = 0;
45
16
    for (int f = 0; f < TEXTPOPUPCOUNT; f ++)
46
    {
47
12
        Label *const label = new Label(this);
48
12
        mText[f] = label;
49
12
        label->setPosition(0, y);
50
36
        label->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
51
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
52
12
        add(label);
53
12
        y += fontHeight;
54
    }
55
4
    addMouseListener(this);
56
4
}
57
58
4
TextPopup::~TextPopup()
59
{
60
4
}
61
62
void TextPopup::show(const int x, const int y, const std::string &str1,
63
                     const std::string &str2, const std::string &str3)
64
{
65
    mText[0]->setCaption(str1);
66
    mText[1]->setCaption(str2);
67
    mText[2]->setCaption(str3);
68
69
    int minWidth = 0;
70
    for (int f = 0; f < TEXTPOPUPCOUNT; f ++)
71
    {
72
        Label *const label = mText[f];
73
        label->adjustSize();
74
        const int width = label->getWidth();
75
        if (width > minWidth)
76
            minWidth = width;
77
    }
78
79
    const int pad2 = 2 * mPadding;
80
    minWidth += pad2;
81
    setWidth(minWidth);
82
83
    int cnt = 1;
84
    if (!str2.empty())
85
        cnt ++;
86
    if (!str3.empty())
87
        cnt ++;
88
89
    setHeight(pad2 + mText[0]->getFont()->getHeight() * cnt);
90
    const int distance = 20;
91
92
    const Rect &rect = mDimension;
93
    int posX = std::max(0, x - rect.width / 2);
94
    int posY = y + distance;
95
96
    if (posX + rect.width > mainGraphics->mWidth)
97
        posX = mainGraphics->mWidth - rect.width;
98
    if (posY + rect.height > mainGraphics->mHeight)
99
        posY = y - rect.height - distance;
100
101
    setPosition(posX, posY);
102
    setVisible(Visible_true);
103
    requestMoveToTop();
104
}
105
106
void TextPopup::mouseMoved(MouseEvent &event)
107
{
108
    Popup::mouseMoved(event);
109
110
    // When the mouse moved on top of the popup, hide it
111
    setVisible(Visible_false);
112
}