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