GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/serverslistbox.h Lines: 48 53 90.6 %
Date: 2017-11-29 Branches: 12 20 60.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifndef GUI_WIDGETS_SERVERSLISTBOX_H
24
#define GUI_WIDGETS_SERVERSLISTBOX_H
25
26
#include "gui/widgets/listbox.h"
27
28
#include "gui/models/serverslistmodel.h"
29
30
#include "localconsts.h"
31
32
4
class ServersListBox final : public ListBox
33
{
34
    public:
35
4
        ServersListBox(const Widget2 *const widget,
36
4
                       ServersListModel *const model) :
37
            ListBox(widget, model, "serverslistbox.xml"),
38
            mNotSupportedColor(getThemeColor(
39
8
                ThemeColorId::SERVER_VERSION_NOT_SUPPORTED)),
40
            mNotSupportedColor2(getThemeColor(
41

24
                ThemeColorId::SERVER_VERSION_NOT_SUPPORTED_OUTLINE))
42
        {
43
8
            mHighlightColor = getThemeColor(ThemeColorId::HIGHLIGHT);
44
4
        }
45
46
        A_DELETE_COPY(ServersListBox)
47
48
4
        void draw(Graphics *const graphics) override final A_NONNULL(2)
49
        {
50
4
            if (mListModel == nullptr)
51
                return;
52
53
4
            ServersListModel *const model
54
                = static_cast<ServersListModel *>(mListModel);
55
56
4
            updateAlpha();
57
58
4
            mHighlightColor.a = CAST_S32(mAlpha * 255.0F);
59
4
            graphics->setColor(mHighlightColor);
60
61
4
            const int height = getRowHeight();
62
4
            mNotSupportedColor.a = CAST_S32(mAlpha * 255.0F);
63
64
            // Draw filled rectangle around the selected list element
65
4
            if (mSelected >= 0)
66
            {
67
12
                graphics->fillRectangle(Rect(mPadding,
68
4
                    height * mSelected + mPadding,
69
8
                    getWidth() - 2 * mPadding,
70
4
                    height));
71
            }
72
73
4
            Font *const font1 = boldFont;
74
4
            Font *const font2 = getFont();
75
4
            const int fontHeight = font1->getHeight();
76
4
            const int pad1 = fontHeight + mPadding;
77
4
            const int pad2 = height / 4 + mPadding;
78
8
            const int width = getWidth();
79
            // Draw the list elements
80
60
            for (int i = 0, y = 0; i < model->getNumberOfElements();
81
28
                 ++i, y += height)
82
            {
83
28
                const ServerInfo &info = model->getServer(i);
84
85
                const Color *color1;
86
                const Color *color2;
87
28
                if (mSelected == i)
88
                {
89
4
                    color1 = &mForegroundSelectedColor;
90
4
                    color2 = &mForegroundSelectedColor2;
91
                }
92
                else
93
                {
94
24
                    color1 = &mForegroundColor;
95
24
                    color2 = &mForegroundColor2;
96
                }
97
98
                int top;
99
28
                int x = mPadding;
100
101
56
                if (!info.name.empty())
102
                {
103
28
                    x += font1->getWidth(info.name) + 15;
104
28
                    font1->drawString(graphics,
105
                        *color1,
106
                        *color2,
107
                        info.name,
108
                        mPadding,
109
28
                        y + mPadding);
110
28
                    top = y + pad1;
111
                }
112
                else
113
                {
114
                    top = y + pad2;
115
                }
116
117
56
                if (!info.description.empty())
118
                {
119
28
                    font2->drawString(graphics,
120
                        *color1,
121
                        *color2,
122
                        info.description,
123
                        x,
124
28
                        y + mPadding);
125
                }
126
56
                font2->drawString(graphics,
127
                    *color1,
128
                    *color2,
129
56
                    model->getElementAt(i),
130
                    mPadding,
131
                    top);
132
133
28
                if (info.version.first > 0)
134
                {
135
                    font2->drawString(graphics,
136
                        mNotSupportedColor,
137
                        mNotSupportedColor2,
138
                        info.version.second,
139
                        width - info.version.first - mPadding,
140
                        top);
141
                }
142
            }
143
        }
144
145
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2)
146
        {
147
            ServersListBox::draw(graphics);
148
        }
149
150
24
        unsigned int getRowHeight() const override final
151
        {
152
24
            return 2 * getFont()->getHeight() + 5;
153
        }
154
    private:
155
        Color mNotSupportedColor;
156
        Color mNotSupportedColor2;
157
};
158
159
#endif  // GUI_WIDGETS_SERVERSLISTBOX_H