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-2018 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 
23 
24 #include "debug.h"
25 
27  const int boxWidth,
28  const int boxHeight) :
29  Container(widget),
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 
44 {
45  if (getWidth() < mBoxWidth)
46  {
48  return;
49  }
50 
51  const int itemCount = CAST_S32(mWidgets.size());
52 
53  if (mBoxWidth == 0)
54  mGridWidth = getWidth();
55  else
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 }
void setSize(const int width, const int height)
Definition: widget.cpp:366
int getWidth() const
Definition: widget.h:220
void setWidth(const int width)
Definition: widget.cpp:132
FlowContainer(const Widget2 *const widget, const int boxWidth, const int boxHeight)
void add(Widget *const widget)
#define CAST_S32
Definition: cast.h:29
virtual void add(Widget *const widget)
void setHeight(const int height)
Definition: widget.cpp:139
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
#define A_UNUSED
Definition: localconsts.h:159
Definition: widget.h:97
Definition: event.h:77
int getHeight() const
Definition: widget.h:239
WidgetList mWidgets
void widgetResized(const Event &event)