ManaPlus
widgetgroup.cpp
Go to the documentation of this file.
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 
23 
24 #include "debug.h"
25 
26 WidgetGroup::WidgetGroup(const Widget2 *const widget,
27  const std::string &group,
28  const int height,
29  const int spacing) :
30  Container(widget),
33  mSpacing(spacing),
34  mCount(0),
35  mGroup(group),
36  mLastX(spacing)
37 {
38  setHeight(height);
39  addWidgetListener(this);
40 }
41 
42 void WidgetGroup::addButton(const std::string &restrict text,
43  const std::string &restrict tag,
44  const bool pressed)
45 {
46  if (text.empty() || tag.empty())
47  return;
48 
49  Widget *const widget = createWidget(text, pressed);
50  if (widget != nullptr)
51  {
52  widget->setActionEventId(mActionEventId + tag);
53  widget->addActionListener(this);
54  addWidget(widget, mSpacing);
55  }
56 }
57 
58 void WidgetGroup::action(const ActionEvent &event)
59 {
60  for (ActionListenerIterator iter = mActionListeners.begin();
61  iter != mActionListeners.end(); ++iter)
62  {
63  (*iter)->action(event);
64  }
65 }
66 
67 void WidgetGroup::addWidget(Widget *const widget,
68  const int spacing)
69 {
70  if (widget == nullptr)
71  return;
72 
73  Container::add(widget);
74  widget->setPosition(mLastX, spacing);
75  mCount++;
76  mLastX += widget->getWidth() + 2 * mSpacing;
77 }
78 
80 {
82 
83  mCount = 0;
84 }
85 
87 {
88 }
virtual void add(Widget *const widget)
virtual void clear()
Definition: event.h:79
virtual void addWidget(Widget *const widget, const int spacing)
Definition: widgetgroup.cpp:67
virtual void addButton(const std::string &text, const std::string &tag, const bool pressed)
Definition: widgetgroup.cpp:42
WidgetGroup(const Widget2 *const widget, const std::string &group, const int height, const int spacing)
Definition: widgetgroup.cpp:26
void clear()
Definition: widgetgroup.cpp:79
virtual Widget * createWidget(const std::string &name, const bool pressed) const =0
void widgetResized(const Event &event)
Definition: widgetgroup.cpp:86
void action(const ActionEvent &event)
Definition: widgetgroup.cpp:58
Definition: widget.h:99
ActionListenerList mActionListeners
Definition: widget.h:1034
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:302
void setHeight(const int height)
Definition: widget.cpp:140
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
std::string mActionEventId
Definition: widget.h:1106
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
void setPosition(const int x, const int y)
Definition: widget.cpp:161
ActionListenerList::iterator ActionListenerIterator
Definition: widget.h:1039
int getWidth() const
Definition: widget.h:221
#define restrict
Definition: localconsts.h:165
#define A_UNUSED
Definition: localconsts.h:160