ManaPlus
tabstrip.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 
22 #include "gui/widgets/tabstrip.h"
23 
24 #include "gui/widgets/button.h"
25 
26 #include "utils/foreach.h"
27 
28 #include "debug.h"
29 
30 TabStrip::TabStrip(const Widget2 *const widget,
31  const std::string &group,
32  const int height,
33  const int spacing) :
34  WidgetGroup(widget, group, height, spacing),
35  mPressFirst(true)
36 {
37  mAllowLogic = false;
38 }
39 
40 TabStrip::TabStrip(const Widget2 *const widget,
41  const int height,
42  const int spacing) :
43  WidgetGroup(widget, "", height, spacing),
44  mPressFirst(true)
45 {
46  mAllowLogic = false;
47 }
48 
49 Widget *TabStrip::createWidget(const std::string &text,
50  const bool pressed) const
51 {
52  Button *const widget = new Button(this,
53  BUTTON_SKIN);
54  widget->setStick(true);
55  widget->setCaption(text);
56  widget->adjustSize();
57  if (((mCount == 0) && mPressFirst) || pressed)
58  widget->setPressed(true);
59  widget->setTag(CAST_S32(mWidgets.size()));
60  return widget;
61 }
62 
63 void TabStrip::action(const ActionEvent &event)
64 {
65  WidgetGroup::action(event);
66  if (event.getSource() != nullptr)
67  {
68  Widget *const widget = event.getSource();
69  Button *const button = static_cast<Button*>(widget);
70  if (button == nullptr)
71  return;
72  if (button->isPressed2())
73  {
75  {
76  if (*iter != widget)
77  {
78  Button *const button2 = static_cast<Button*>(*iter);
79  button2->setPressed(false);
80  }
81  }
82  }
83  else
84  {
85  button->setPressed(true);
86  }
87  }
88 }
89 
91 {
93  {
94  Button *button = static_cast<Button*>(*iter);
95  if (button->isPressed2())
96  {
97  button->setPressed(false);
98  ++iter;
99  if (iter == mWidgets.end())
100  iter = mWidgets.begin();
101  button = static_cast<Button*>(*iter);
102  action(ActionEvent(button, button->getActionEventId()));
103  return;
104  }
105  }
106 }
107 
109 {
111  {
112  Button *button = static_cast<Button*>(*iter);
113  if (button->isPressed2())
114  {
115  button->setPressed(false);
116  if (iter == mWidgets.begin())
117  iter = mWidgets.end();
118  if (iter == mWidgets.begin())
119  return;
120  --iter;
121  button = static_cast<Button*>(*iter);
122  action(ActionEvent(button, button->getActionEventId()));
123  return;
124  }
125  }
126 }
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
WidgetList mWidgets
WidgetList::const_iterator WidgetListConstIterator
Definition: button.h:102
void setCaption(const std::string &caption)
Definition: button.h:214
void setTag(int tag)
Definition: button.h:174
void setStick(bool b)
Definition: button.h:180
void adjustSize()
Definition: button.cpp:799
bool isPressed2() const
Definition: button.cpp:856
void setPressed(bool b)
Definition: button.h:183
Widget * getSource() const
Definition: event.h:104
void nextTab()
Definition: tabstrip.cpp:90
bool mPressFirst
Definition: tabstrip.h:55
void prevTab()
Definition: tabstrip.cpp:108
Widget * createWidget(const std::string &name, const bool pressed) const
Definition: tabstrip.cpp:49
TabStrip(const Widget2 *const widget, const std::string &group, const int height, const int spacing)
Definition: tabstrip.cpp:30
void action(const ActionEvent &event)
Definition: tabstrip.cpp:63
void action(const ActionEvent &event)
Definition: widgetgroup.cpp:58
Definition: widget.h:99
bool mAllowLogic
Definition: widget.h:1160
const std::string & getActionEventId() const
Definition: widget.h:605
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25