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
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2002-2006 The ScummVM project
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*/
#include "common/stdafx.h"
#include "common/util.h"
#include "gui/TabWidget.h"
#include "gui/dialog.h"
#include "gui/newgui.h"
#include "gui/eval.h"
namespace GUI {
enum {
kCmdLeft = 'LEFT',
kCmdRight = 'RGHT'
};
TabWidget::TabWidget(GuiObject *boss, int x, int y, int w, int h)
: Widget(boss, x, y, w, h) {
init();
}
TabWidget::TabWidget(GuiObject *boss, const String &name)
: Widget(boss, name) {
init();
}
void TabWidget::init() {
_tabOffset = 0; // TODO
_tabSpacing = g_gui.theme()->getTabSpacing();
_tabPadding = g_gui.theme()->getTabPadding();
_flags = WIDGET_ENABLED;
_type = kTabWidget;
_activeTab = -1;
_firstVisibleTab = 0;
_tabWidth = g_gui.evaluator()->getVar("TabWidget.tabWidth");
_tabHeight = g_gui.evaluator()->getVar("TabWidget.tabHeight");
_titleVPad = g_gui.evaluator()->getVar("TabWidget.titleVPad");
_butRP = g_gui.evaluator()->getVar("TabWidget.navButtonRightPad", 0);
_butTP = g_gui.evaluator()->getVar("TabWidget.navButtonTopPad", 0);
_butW = g_gui.evaluator()->getVar("TabWidget.navButtonW", 10);
_butH = g_gui.evaluator()->getVar("TabWidget.navButtonH", 10);
int x = _w - _butRP - _butW * 2 - 2;
int y = _butTP - _tabHeight;
_navLeft = new ButtonWidget(this, x, y, _butW, _butH, "<", kCmdLeft, 0);
_navLeft->setHints(THEME_HINT_NO_BACKGROUND_RESTORE);
_navRight = new ButtonWidget(this, x + _butW + 2, y, _butW, _butH, ">", kCmdRight, 0);
_navRight->setHints(THEME_HINT_NO_BACKGROUND_RESTORE);
}
TabWidget::~TabWidget() {
for (uint i = 0; i < _tabs.size(); ++i) {
delete _tabs[i].firstWidget;
_tabs[i].firstWidget = 0;
}
_tabs.clear();
}
int16 TabWidget::getChildY() const {
return getAbsY() + _tabHeight;
}
int TabWidget::addTab(const String &title) {
// Add a new tab page
Tab newTab;
newTab.title = title;
newTab.firstWidget = 0;
_tabs.push_back(newTab);
int numTabs = _tabs.size();
if (g_gui.evaluator()->getVar("TabWidget.tabWidth") == 0) {
if (_tabWidth == 0)
_tabWidth = 40;
// Determine the new tab width
int newWidth = g_gui.getStringWidth(title) + 2 * 3;
if (_tabWidth < newWidth)
_tabWidth = newWidth;
int maxWidth = _w / numTabs;
if (_tabWidth > maxWidth)
_tabWidth = maxWidth;
}
// Activate the new tab
setActiveTab(numTabs - 1);
return _activeTab;
}
void TabWidget::setActiveTab(int tabID) {
assert(0 <= tabID && tabID < (int)_tabs.size());
if (_activeTab != tabID) {
// Exchange the widget lists, and switch to the new tab
if (_activeTab != -1) {
_tabs[_activeTab].firstWidget = _firstWidget;
releaseFocus();
}
_activeTab = tabID;
_firstWidget = _tabs[tabID].firstWidget;
_boss->draw();
}
}
void TabWidget::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
Widget::handleCommand(sender, cmd, data);
switch (cmd) {
case kCmdLeft:
if (_firstVisibleTab) {
_firstVisibleTab--;
draw();
}
break;
case kCmdRight:
if (_firstVisibleTab + _w / _tabWidth < (int)_tabs.size()) {
_firstVisibleTab++;
draw();
}
break;
}
}
void TabWidget::handleMouseDown(int x, int y, int button, int clickCount) {
assert(y < _tabHeight);
// Determine which tab was clicked
int tabID = -1;
if (x >= 0 && x % (_tabWidth + _tabSpacing) < _tabWidth) {
tabID = x / (_tabWidth + _tabSpacing);
if (tabID >= (int)_tabs.size())
tabID = -1;
}
// If a tab was clicked, switch to that pane
if (tabID >= 0 && tabID + _firstVisibleTab < (int)_tabs.size()) {
setActiveTab(tabID + _firstVisibleTab);
}
}
bool TabWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
// TODO: maybe there should be a way to switch between tabs
// using the keyboard? E.g. Alt-Shift-Left/Right-Arrow or something
// like that.
return Widget::handleKeyDown(ascii, keycode, modifiers);
}
void TabWidget::reflowLayout() {
Widget::reflowLayout();
for (uint i = 0; i < _tabs.size(); ++i) {
Widget *w = _tabs[i].firstWidget;
while (w) {
w->reflowLayout();
w = w->next();
}
}
_tabHeight = g_gui.evaluator()->getVar("TabWidget.tabHeight");
_tabWidth = g_gui.evaluator()->getVar("TabWidget.tabWidth");
_titleVPad = g_gui.evaluator()->getVar("TabWidget.titleVPad");
if (_tabWidth == 0) {
_tabWidth = 40;
int maxWidth = _w / _tabs.size();
for (uint i = 0; i < _tabs.size(); ++i) {
// Determine the new tab width
int newWidth = g_gui.getStringWidth(_tabs[i].title) + 2 * 3;
if (_tabWidth < newWidth)
_tabWidth = newWidth;
if (_tabWidth > maxWidth)
_tabWidth = maxWidth;
}
}
_butRP = g_gui.evaluator()->getVar("TabWidget.navButtonRightPad", 0);
_butTP = g_gui.evaluator()->getVar("TabWidget.navButtonTopPad", 0);
_butW = g_gui.evaluator()->getVar("TabWidget.navButtonW", 10);
_butH = g_gui.evaluator()->getVar("TabWidget.navButtonH", 10);
int x = _w - _butRP - _butW * 2 - 2;
int y = _butTP - _tabHeight;
_navLeft->resize(x, y, _butW, _butH);
_navRight->resize(x + _butW + 2, y, _butW, _butH);
_tabOffset = 0; // TODO
_tabSpacing = g_gui.theme()->getTabSpacing();
_tabPadding = g_gui.theme()->getTabPadding();
}
void TabWidget::drawWidget(bool hilite) {
Common::Array<Common::String> tabs;
for (int i = _firstVisibleTab; i < (int)_tabs.size(); ++i) {
tabs.push_back(_tabs[i].title);
}
g_gui.theme()->drawTab(Common::Rect(_x, _y, _x+_w, _y+_h), _tabHeight, _tabWidth, tabs, _activeTab - _firstVisibleTab, _hints, _titleVPad);
}
void TabWidget::draw() {
Widget::draw();
if (_tabWidth * _tabs.size() > _w) {
_navLeft->draw();
_navRight->draw();
}
}
Widget *TabWidget::findWidget(int x, int y) {
if (y < _tabHeight) {
if (_tabWidth * _tabs.size() > _w) {
if (y >= _butTP && y < _butTP + _butH) {
if (x >= _w - _butRP - _butW * 2 - 2 && x < _w - _butRP - _butW - 2)
return _navLeft;
if (x >= _w - _butRP - _butW && x < _w - _butRP)
return _navRight;
}
}
// Click was in the tab area
return this;
} else {
// Iterate over all child widgets and find the one which was clicked
return Widget::findWidgetInChain(_firstWidget, x, y - _tabHeight);
}
}
} // End of namespace GUI
|