ManaPlus
widgetgroup.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2012-2018 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
22 
23 #include "debug.h"
24 
25 WidgetGroup::WidgetGroup(const Widget2 *const widget,
26  const std::string &group,
27  const int height,
28  const int spacing) :
29  Container(widget),
32  mSpacing(spacing),
33  mCount(0),
34  mGroup(group),
35  mLastX(spacing)
36 {
37  setHeight(height);
38  addWidgetListener(this);
39 }
40 
41 void WidgetGroup::addButton(const std::string &restrict text,
42  const std::string &restrict tag,
43  const bool pressed)
44 {
45  if (text.empty() || tag.empty())
46  return;
47 
48  Widget *const widget = createWidget(text, pressed);
49  if (widget != nullptr)
50  {
51  widget->setActionEventId(mActionEventId + tag);
52  widget->addActionListener(this);
53  addWidget(widget, mSpacing);
54  }
55 }
56 
57 void WidgetGroup::action(const ActionEvent &event)
58 {
59  for (ActionListenerIterator iter = mActionListeners.begin();
60  iter != mActionListeners.end(); ++iter)
61  {
62  (*iter)->action(event);
63  }
64 }
65 
66 void WidgetGroup::addWidget(Widget *const widget,
67  const int spacing)
68 {
69  if (widget == nullptr)
70  return;
71 
72  Container::add(widget);
73  widget->setPosition(mLastX, spacing);
74  mCount++;
75  mLastX += widget->getWidth() + 2 * mSpacing;
76 }
77 
79 {
81 
82  mCount = 0;
83 }
84 
86 {
87 }
int getWidth() const
Definition: widget.h:220
virtual void addWidget(Widget *const widget, const int spacing)
Definition: widgetgroup.cpp:66
virtual void addButton(const std::string &text, const std::string &tag, const bool pressed)
Definition: widgetgroup.cpp:41
void clear()
Definition: widgetgroup.cpp:78
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
void widgetResized(const Event &event)
Definition: widgetgroup.cpp:85
void action(const ActionEvent &event)
Definition: widgetgroup.cpp:57
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
WidgetGroup(const Widget2 *const widget, const std::string &group, const int height, const int spacing)
Definition: widgetgroup.cpp:25
virtual void clear()
ActionListenerList mActionListeners
Definition: widget.h:1033
virtual void add(Widget *const widget)
void setPosition(const int x, const int y)
Definition: widget.cpp:160
ActionListenerList::iterator ActionListenerIterator
Definition: widget.h:1038
void setHeight(const int height)
Definition: widget.cpp:139
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
#define A_UNUSED
Definition: localconsts.h:171
Definition: widget.h:97
virtual Widget * createWidget(const std::string &name, const bool pressed) const =0
Definition: event.h:77
#define restrict
Definition: localconsts.h:176
std::string mActionEventId
Definition: widget.h:1105