GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/extendedlistbox.cpp Lines: 14 128 10.9 %
Date: 2021-03-17 Branches: 11 104 10.6 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/widgets/extendedlistbox.h"
23
24
#include "gui/models/extendedlistmodel.h"
25
26
#include "gui/skin.h"
27
28
#include "gui/fonts/font.h"
29
30
#include "render/graphics.h"
31
32
#include "debug.h"
33
34
5
ExtendedListBox::ExtendedListBox(const Widget2 *const widget,
35
                                 ListModel *const listModel,
36
                                 const std::string &skin,
37
5
                                 const unsigned int rowHeight) :
38
    ListBox(widget, listModel, skin),
39


20
    mImagePadding(mSkin != nullptr ? mSkin->getOption("imagePadding") : 0),
40


20
    mSpacing(mSkin != nullptr ? mSkin->getOption("spacing") : 0),
41
    mHeight(0),
42
    mListItems(),
43
30
    mSelectedItems()
44
{
45
5
    if (rowHeight != 0U)
46
2
        mRowHeight = rowHeight;
47
5
}
48
49
10
ExtendedListBox::~ExtendedListBox()
50
{
51
10
}
52
53
void ExtendedListBox::draw(Graphics *const graphics)
54
{
55
    if (mListModel == nullptr)
56
        return;
57
58
    BLOCK_START("ExtendedListBox::draw")
59
    ExtendedListModel *const model = static_cast<ExtendedListModel *>(
60
        mListModel);
61
62
    updateAlpha();
63
    Font *const font = getFont();
64
65
    const int height = CAST_S32(mRowHeight);
66
    const int pad2 = 2 + mPadding;
67
    const int width = mDimension.width;
68
    int textPos = (height - font->getHeight()) / 2 + mPadding;
69
    if (textPos < 0)
70
        textPos = 0;
71
72
    const int sz = mListModel->getNumberOfElements();
73
    mListItems.clear();
74
    mSelectedItems.clear();
75
    int y = 0;
76
    const int insideWidth = width - pad2;
77
    for (int f = 0; f < sz; f ++)
78
    {
79
        int row = f;
80
        bool useImage = true;
81
        std::string str = mListModel->getElementAt(f);
82
        int strWidth = font->getWidth(str) + 8;
83
84
        const Image *const image = model->getImageAt(row);
85
        if (image != nullptr)
86
            strWidth += image->getWidth() + mImagePadding;
87
88
        STD_VECTOR<ExtendedListBoxItem> &list =
89
            row == mSelected ? mSelectedItems : mListItems;
90
91
        if (insideWidth < strWidth)
92
        {
93
            const size_t strSize = str.size();
94
            size_t divPos = strSize / 2;
95
            if (divPos > 0 && CAST_U8(
96
                str[divPos - 1]) >= 0xc0)
97
            {
98
                divPos --;
99
            }
100
            for (size_t d = divPos; d > 10; d --)
101
            {
102
                if (str[d] == 32)
103
                {
104
                    divPos = d + 1;
105
                    break;
106
                }
107
            }
108
            list.push_back(ExtendedListBoxItem(row,
109
                str.substr(0, divPos), useImage, y));
110
            str = str.substr(divPos);
111
            y += height;
112
            useImage = false;
113
        }
114
        list.push_back(ExtendedListBoxItem(row, str, useImage, y));
115
116
        y += height;
117
    }
118
    mHeight = y + height;
119
120
    const size_t itemsSz = mListItems.size();
121
    const size_t selSz = mSelectedItems.size();
122
    int minY = -1;
123
    int maxY = -1;
124
    for (size_t f = 0; f < selSz; f ++)
125
    {
126
        const ExtendedListBoxItem &item = mSelectedItems[f];
127
        const int y1 = item.y;
128
        if (minY == -1)
129
            minY = y1;
130
        if (maxY < y1)
131
            maxY = y1;
132
    }
133
134
    if (minY != -1)
135
    {
136
        mHighlightColor.a = CAST_U32(mAlpha * 255.0F);
137
        graphics->setColor(mHighlightColor);
138
        graphics->fillRectangle(Rect(mPadding, minY + mPadding,
139
            width - pad2, maxY - minY + height));
140
    }
141
142
    for (size_t f = 0; f < itemsSz; ++f)
143
    {
144
        const ExtendedListBoxItem &item = mListItems[f];
145
        if (item.image)
146
        {
147
            const int row1 = item.row;
148
            const Image *const image = model->getImageAt(row1);
149
            if (image != nullptr)
150
            {
151
                graphics->drawImage(image,
152
                    mImagePadding,
153
                    item.y + (height - image->getHeight()) / 2 + mPadding);
154
            }
155
        }
156
    }
157
158
    for (size_t f = 0; f < itemsSz; ++f)
159
    {
160
        const ExtendedListBoxItem &item = mListItems[f];
161
        const int row1 = item.row;
162
        const int y1 = item.y;
163
        const Image *const image = model->getImageAt(row1);
164
        if ((image == nullptr) || !item.image)
165
        {
166
            font->drawString(graphics,
167
                mForegroundColor, mForegroundColor2,
168
                item.text,
169
                mPadding, y1 + textPos);
170
        }
171
        else
172
        {
173
            font->drawString(graphics,
174
                mForegroundColor, mForegroundColor2,
175
                item.text,
176
                image->getWidth() + mImagePadding + mSpacing, y1 + textPos);
177
        }
178
    }
179
180
    for (size_t f = 0; f < selSz; ++f)
181
    {
182
        const ExtendedListBoxItem &item = mSelectedItems[f];
183
        const int row1 = item.row;
184
        const int y1 = item.y;
185
        const Image *const image = model->getImageAt(row1);
186
        if ((image == nullptr) || !item.image)
187
        {
188
            font->drawString(graphics,
189
                mForegroundSelectedColor, mForegroundSelectedColor2,
190
                item.text,
191
                mPadding, y1 + textPos);
192
        }
193
        else
194
        {
195
            font->drawString(graphics,
196
                mForegroundSelectedColor, mForegroundSelectedColor2,
197
                item.text,
198
                image->getWidth() + mImagePadding + mSpacing, y1 + textPos);
199
        }
200
    }
201
202
    for (size_t f = 0; f < selSz; ++f)
203
    {
204
        const ExtendedListBoxItem &item = mSelectedItems[f];
205
        if (item.image)
206
        {
207
            const int row1 = item.row;
208
            const Image *const image = model->getImageAt(row1);
209
            if (image != nullptr)
210
            {
211
                graphics->drawImage(image,
212
                    mImagePadding,
213
                    item.y + (height - image->getHeight()) / 2 + mPadding);
214
            }
215
        }
216
    }
217
218
    BLOCK_END("ExtendedListBox::draw")
219
}
220
221
void ExtendedListBox::safeDraw(Graphics *const graphics)
222
{
223
    ExtendedListBox::draw(graphics);
224
}
225
226
13
void ExtendedListBox::adjustSize()
227
{
228
13
    if (mHeight != 0)
229
        setHeight(mHeight + 2 * mPadding);
230
    else
231
13
        ListBox::adjustSize();
232
13
}
233
234
int ExtendedListBox::getSelectionByMouse(const int y) const
235
{
236
    if (mListItems.empty() && mSelectedItems.empty())
237
        return ListBox::getSelectionByMouse(y);
238
239
    const int height = CAST_S32(mRowHeight);
240
    const size_t itemsSz = mListItems.size();
241
    for (size_t f = 0; f < itemsSz; f ++)
242
    {
243
        const ExtendedListBoxItem &item = mListItems[f];
244
        const int y2 = item.y + mPadding;
245
        if (y2 <= y && y2 + height > y)
246
            return item.row;
247
    }
248
249
    const size_t selSz = mSelectedItems.size();
250
    for (size_t f = 0; f < selSz; f ++)
251
    {
252
        const ExtendedListBoxItem &item = mSelectedItems[f];
253
        const int y2 = item.y + mPadding;
254
        if (y2 <= y && y2 + height > y)
255
            return item.row;
256
    }
257
    return 0;
258
}