ManaPlus
flowcontainer.cpp
Go to the documentation of this file.
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 
24 
25 #include "debug.h"
26 
28  const int boxWidth,
29  const int boxHeight) :
30  Container(widget),
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 
45 {
46  if (getWidth() < mBoxWidth)
47  {
49  return;
50  }
51 
52  const int itemCount = CAST_S32(mWidgets.size());
53 
54  if (mBoxWidth == 0)
55  mGridWidth = getWidth();
56  else
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 }
#define CAST_S32
Definition: cast.h:30
virtual void add(Widget *const widget)
WidgetList mWidgets
Definition: event.h:79
FlowContainer(const Widget2 *const widget, const int boxWidth, const int boxHeight)
void add(Widget *const widget)
void widgetResized(const Event &event)
Definition: widget.h:99
void setWidth(const int width)
Definition: widget.cpp:133
void setSize(const int width, const int height)
Definition: widget.cpp:367
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:302
void setHeight(const int height)
Definition: widget.cpp:140
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
#define A_UNUSED
Definition: localconsts.h:160