GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/flowcontainer.cpp Lines: 0 37 0.0 %
Date: 2021-03-17 Branches: 0 24 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009-2010  The Mana Developers
4
 *  Copyright (C) 2011-2019  The ManaPlus Developers
5
 *  Copyright (C) 2019-2021  Andrei Karas
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
#include "gui/widgets/flowcontainer.h"
24
25
#include "debug.h"
26
27
FlowContainer::FlowContainer(const Widget2 *const widget,
28
                             const int boxWidth,
29
                             const int boxHeight) :
30
    Container(widget),
31
    WidgetListener(),
32
    mBoxWidth(boxWidth),
33
    mBoxHeight(boxHeight),
34
    mGridWidth(1),
35
    mGridHeight(1)
36
{
37
    addWidgetListener(this);
38
    if (mBoxWidth == 0)
39
        mBoxWidth = 1;
40
    if (mBoxHeight == 0)
41
        mBoxHeight = 1;
42
}
43
44
void FlowContainer::widgetResized(const Event &event A_UNUSED)
45
{
46
    if (getWidth() < mBoxWidth)
47
    {
48
        setWidth(mBoxWidth);
49
        return;
50
    }
51
52
    const int itemCount = CAST_S32(mWidgets.size());
53
54
    if (mBoxWidth == 0)
55
        mGridWidth = getWidth();
56
    else
57
        mGridWidth = getWidth() / mBoxWidth;
58
59
    if (mGridWidth < 1)
60
        mGridWidth = 1;
61
62
    mGridHeight = itemCount / mGridWidth;
63
64
    if (itemCount % mGridWidth != 0 || mGridHeight < 1)
65
        ++mGridHeight;
66
67
    int height = mGridHeight * mBoxHeight;
68
69
    if (getHeight() != height)
70
    {
71
        setHeight(height);
72
        return;
73
    }
74
75
    int i = 0;
76
    height = 0;
77
    for (WidgetList::const_iterator it = mWidgets.begin();
78
         it != mWidgets.end(); ++it)
79
    {
80
        const int x = i % mGridWidth * mBoxWidth;
81
        (*it)->setPosition(x, height);
82
83
        i++;
84
85
        if (i % mGridWidth == 0)
86
            height += mBoxHeight;
87
    }
88
}
89
90
void FlowContainer::add(Widget *const widget)
91
{
92
    if (widget == nullptr)
93
        return;
94
95
    Container::add(widget);
96
    widget->setSize(mBoxWidth, mBoxHeight);
97
    widgetResized(Event(nullptr));
98
}