File: listwidget.h

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (261 lines) | stat: -rw-r--r-- 7,414 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
	This file is part of Warzone 2100.
	Copyright (C) 1999-2004  Eidos Interactive
	Copyright (C) 2005-2020  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*!
 * \file listwidget.h
 * \brief A list widget. Useful for displaying lists of buttons and stuff.
 */

#ifndef THISISALISTWIDGET_H
#define THISISALISTWIDGET_H

#include "lib/ivis_opengl/ivisdef.h"

#include "widget.h"
#include <functional>
#include <algorithm>

enum class TabAlignment
{
	LeftAligned,
	RightAligned
};

struct TabSelectionStyle
{
	TabSelectionStyle() {}
	TabSelectionStyle(AtlasImage tab, AtlasImage tabDown, AtlasImage tabHighlight, AtlasImage prev, AtlasImage prevDown, AtlasImage prevHighlight, AtlasImage next, AtlasImage nextDown, AtlasImage nextHighlight, int gap, TabAlignment tabAlignment = TabAlignment::LeftAligned);

	WzSize tabSize;
	WzSize scrollTabSize;
	AtlasImage tabImage, tabImageDown, tabImageHighlight;
	AtlasImage prevScrollTabImage, prevScrollTabImageDown, prevScrollTabImageHighlight;
	AtlasImage nextScrollTabImage, nextScrollTabImageDown, nextScrollTabImageHighlight;
	int tabGap;
	TabAlignment tabAlignment = TabAlignment::LeftAligned;
};

class ListWidget;

class TabSelectionWidget : public WIDGET
{
protected:
	TabSelectionWidget(): WIDGET(), currentTab(0), tabsAtOnce(1) {}
	virtual void initialize();

public:
	static std::shared_ptr<TabSelectionWidget> make()
	{
		class make_shared_enabler: public TabSelectionWidget {};
		auto widget = std::make_shared<make_shared_enabler>();
		widget->initialize();
		return widget;
	}

	void setHeight(int height);
	void addStyle(TabSelectionStyle const &tabStyle);

	size_t tabs() const
	{
		return tabButtons.size();
	}

	/* The optional "onTabChanged" callback function */
	typedef std::function<void (TabSelectionWidget& widget, size_t currentTab)> W_TABSELECTION_ON_TAB_CHANGED_FUNC;

	void addOnTabChangedHandler(const W_TABSELECTION_ON_TAB_CHANGED_FUNC& onTabChangedFunc);

public:
	void setTab(size_t tab);
	void setNumberOfTabs(size_t tabs);

private:
	void prevTabPage();
	void nextTabPage();

protected:
	void geometryChanged() override;

protected:
	friend class ListWidget;
	void doLayoutAll();
private:

	std::vector<TabSelectionStyle> styles;
	size_t currentTab;
	size_t tabsAtOnce;
	std::vector<std::shared_ptr<W_BUTTON>> tabButtons;
	std::shared_ptr<W_BUTTON> prevTabPageButton;
	std::shared_ptr<W_BUTTON> nextTabPageButton;
	std::vector<W_TABSELECTION_ON_TAB_CHANGED_FUNC> onTabChangedHandlers;
};

class ListWidget : public WIDGET
{

public:
	enum Order {RightThenDown, DownThenRight};

	ListWidget();

	void widgetLost(WIDGET *widget) override;

	void setChildSize(int width, int height);  ///< Sets the size of all child widgets (applied by calling addWidgetToLayout).
	void setChildSpacing(int width, int height);  ///< Sets the distance between child widgets (applied by calling addWidgetToLayout).
	void setOrder(Order order);  ///< Sets whether subsequent child widgets are placed in horizontal or vertical lines (applied by calling addWidgetToLayout).
	void addWidgetToLayout(const std::shared_ptr<WIDGET> &widget);  ///< Manages the geometry of widget, and shows/hides it when changing tabs.

	size_t currentPage() const
	{
		return currentPage_;
	}
	size_t pages() const
	{
		return numberOfPages;
	}
	size_t childrenSize() const
	{
		return myChildren.size();
	}
	size_t firstWidgetShownIndex() const;
	size_t lastWidgetShownIndex() const;
	std::shared_ptr<WIDGET> getWidgetAtIndex(size_t index) const;

	/* The optional "onCurrentPageChanged" callback function */
	typedef std::function<void (ListWidget& psWidget, size_t currentPage)> W_LISTWIDGET_ON_CURRENTPAGECHANGED_FUNC;

	/* The optional "onNumberOfPagesChanged" callback function */
	typedef std::function<void (ListWidget& psWidget, size_t numberOfPages)> W_LISTWIDGET_ON_NUMBEROFPAGESCHANGED_FUNC;

	void addOnCurrentPageChangedHandler(const W_LISTWIDGET_ON_CURRENTPAGECHANGED_FUNC& handlerFunc);
	void addOnNumberOfPagesChangedHandler(const W_LISTWIDGET_ON_NUMBEROFPAGESCHANGED_FUNC& handlerFunc);

protected:
	void geometryChanged() override;

public:
	void setCurrentPage(size_t page);

	size_t widgetsPerPage() const
	{
		return widgetsPerRow() * widgetsPerColumn();
	}

private:
	void doLayoutAll();
	void doLayout(size_t num);

	size_t widgetsPerRow() const
	{
		return static_cast<size_t>(std::max((width() + spacing.width()) / widgetSkipX(), 1));
	}
	size_t widgetsPerColumn() const
	{
		return static_cast<size_t>(std::max((height() + spacing.height()) / widgetSkipY(), 1));
	}
	int widgetSkipX() const
	{
		return childSize.width() + spacing.width();
	}
	int widgetSkipY() const
	{
		return childSize.height() + spacing.height();
	}
	void updateNumberOfPages();

	size_t numberOfPages = 1;
	WzSize childSize;
	WzSize spacing;
	size_t currentPage_;
	std::vector<std::shared_ptr<WIDGET>> myChildren;
	Order order;
	std::vector<W_LISTWIDGET_ON_CURRENTPAGECHANGED_FUNC> onCurrentPageChangedHandlers;
	std::vector<W_LISTWIDGET_ON_NUMBEROFPAGESCHANGED_FUNC> onNumberOfPagesChangedHandlers;
};

class ListTabWidget : public WIDGET
{
protected:
	ListTabWidget(): WIDGET(), tabPos(Top) {}
	virtual void initialize();

public:
	enum TabPosition {Top, Bottom};

	void geometryChanged() override;

	void setTitle(const WzString& string);

	void setChildSize(int width, int height)
	{
		widgets->setChildSize(width, height);    ///< Sets the size of all child widgets (applied by calling addWidgetToLayout).
	}
	void setChildSpacing(int width, int height)
	{
		widgets->setChildSpacing(width, height);    ///< Sets the distance between child widgets (applied by calling addWidgetToLayout).
	}
	void setOrder(ListWidget::Order order)
	{
		widgets->setOrder(order);    ///< Sets whether subsequent child widgets are placed in horizontal or vertical lines (applied by calling addWidgetToLayout).
	}
	void addWidgetToLayout(const std::shared_ptr<WIDGET> &widget);  ///< Manages the geometry of widget, and shows/hides it when changing tabs.
	bool setCurrentPage(size_t page)
	{
		widgets->setCurrentPage(page);
		return widgets->currentPage() == page;
	}
	size_t currentPage() const
	{
		return widgets->currentPage();
	}
	size_t pages() const
	{
		return widgets->pages();
	}
	size_t childrenSize() const
	{
		return widgets->childrenSize();
	}

	void setTabPosition(TabPosition pos);

	TabSelectionWidget *tabWidget()
	{
		return tabs.get();
	}
	ListWidget *listWidget()
	{
		return widgets.get();
	}

	int32_t heightOfTabsLabel() const;

	void goToChildPage(size_t childIndex)
	{
		setCurrentPage(childIndex / widgets->widgetsPerPage());
	}

private:
	std::shared_ptr<TabSelectionWidget> tabs;
	std::shared_ptr<ListWidget> widgets;
	std::shared_ptr<W_LABEL> label;
	TabPosition tabPos;
};

#endif //THISISALISTWIDGET_H