aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8.1/tools/GUIEditor/CGUIPanel.cpp
blob: 4135b7a890bb27a3ac8d4300d30286aa52eb8263 (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2006-2012 Asger Feldthaus
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

/*
	Originally Klasker's but I've messed around with it lots - Gaz
*/

#include "CGUIPanel.h"
#include "IGUIEnvironment.h"
#include "IGUIScrollBar.h"
#include "IGUITabControl.h"
#include "IVideoDriver.h"

const int SCROLL_BAR_SIZE = 16; // Scroll bars are 16 pixels wide
const int BORDER_WIDTH = 2;

namespace irr
{
namespace gui
{

CGUIPanel::CGUIPanel(IGUIEnvironment* environment, IGUIElement* parent, s32 id, const core::rect<s32>& rectangle,
			bool border, E_SCROLL_BAR_MODE vMode, E_SCROLL_BAR_MODE hMode)
	: IGUIElement(EGUIET_ELEMENT, environment, parent, id, rectangle),
	VScrollBar(0), HScrollBar(0), ClipPane(0), InnerPane(0),
	VScrollBarMode(vMode), HScrollBarMode(hMode), NeedsUpdate(true), Border(border)
{
	#ifdef _DEBUG
	setDebugName("CGUIPanel");
	#endif

	s32 width = rectangle.getWidth();
	s32 height = rectangle.getHeight();

	core::rect<s32> rct = core::rect<s32>(width - SCROLL_BAR_SIZE,0, width, height);

	VScrollBar = environment->addScrollBar(false, rct, 0, id);
	VScrollBar->setSubElement(true);
	VScrollBar->setTabStop(false);
	VScrollBar->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
	VScrollBar->grab();
	IGUIElement::addChild(VScrollBar);

	rct = core::rect<s32>(0, height - SCROLL_BAR_SIZE, width - SCROLL_BAR_SIZE,height );

	HScrollBar = environment->addScrollBar(true, rct, 0, id);
	HScrollBar->setSubElement(true);
	HScrollBar->setTabStop(false);
	HScrollBar->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT);
	HScrollBar->grab();
	IGUIElement::addChild(HScrollBar);

	rct = core::rect<s32>(0,0, width - SCROLL_BAR_SIZE, height - SCROLL_BAR_SIZE);

	ClipPane = environment->addTab( rct, 0, -1);
	ClipPane->setSubElement(true);
	ClipPane->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
	ClipPane->grab();
	IGUIElement::addChild(ClipPane);

	InnerPane = environment->addTab(rct, ClipPane, -1);
	InnerPane->setSubElement(true);
	InnerPane->grab();

	calculateClientArea();
	resizeInnerPane();
}

CGUIPanel::~CGUIPanel()
{
	// because the inner pane has the list of children, we need to remove the outer ones manually
	IGUIElement::removeChild(VScrollBar);
	IGUIElement::removeChild(HScrollBar);
	IGUIElement::removeChild(ClipPane);

	// now we can drop the others
	VScrollBar->drop();
	HScrollBar->drop();
	ClipPane->drop();
	InnerPane->drop();
}


void CGUIPanel::draw()
{
	if (NeedsUpdate)
	{
		calculateClientArea();
		resizeInnerPane();
		NeedsUpdate = false;
	}

	IGUISkin* skin = Environment->getSkin();
	if (Border && skin)
	{
		skin->draw3DSunkenPane( this, skin->getColor( EGDC_APP_WORKSPACE), false, true, AbsoluteRect, &AbsoluteClippingRect );
	}

	IGUIElement::draw();
}

void CGUIPanel::addChild(IGUIElement *child)
{
	// add the child to the inner pane
	InnerPane->addChild(child);

	NeedsUpdate = true;
}

void CGUIPanel::removeChild(IGUIElement *child)
{
	InnerPane->removeChild(child);

	NeedsUpdate = true;
}

//! returns children of the inner pane
const core::list<IGUIElement*>& CGUIPanel::getChildren()
{
	return InnerPane->getChildren();
}

bool CGUIPanel::hasBorder() const
{
	return Border;
}

void CGUIPanel::setBorder( bool enabled )
{
	Border = enabled;
}

IGUIScrollBar* CGUIPanel::getVScrollBar() const
{
	return VScrollBar;
}

IGUIScrollBar* CGUIPanel::getHScrollBar() const
{
	return HScrollBar;
}

E_SCROLL_BAR_MODE CGUIPanel::getVScrollBarMode() const
{
	return VScrollBarMode;
}

void CGUIPanel::setVScrollBarMode( E_SCROLL_BAR_MODE mode )
{
	VScrollBarMode = mode;
	NeedsUpdate = true;
}

E_SCROLL_BAR_MODE CGUIPanel::getHScrollBarMode() const
{
	return HScrollBarMode;
}

void CGUIPanel::setHScrollBarMode(E_SCROLL_BAR_MODE mode)
{
	HScrollBarMode = mode;
	NeedsUpdate = true;
}

bool CGUIPanel::OnEvent(const SEvent &event)
{
	// Redirect mouse wheel to scrollbar
	if (event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
	{
		if (VScrollBar->isVisible())
		{
			Environment->setFocus(VScrollBar);
			VScrollBar->OnEvent(event);
			return true;
		}
		else if (VScrollBar->isVisible())
		{
			Environment->setFocus(HScrollBar);
			HScrollBar->OnEvent(event);
			return true;
		}
	}
	else
	{
		if (event.EventType == EET_GUI_EVENT && event.GUIEvent.EventType == EGET_SCROLL_BAR_CHANGED &&
			(event.GUIEvent.Caller == HScrollBar || event.GUIEvent.Caller == VScrollBar) )
		{
			moveInnerPane();

			return true;
		}
	}

	return IGUIElement::OnEvent(event);
}

void CGUIPanel::moveInnerPane()
{
	core::dimension2d<s32> dim = InnerPane->getAbsolutePosition().getSize();
	core::position2d<s32> newpos(HScrollBar->isVisible() ? -HScrollBar->getPos() : 0 , VScrollBar->isVisible() ? -VScrollBar->getPos() : 0);
	core::rect<s32> r(newpos, newpos + dim);
	InnerPane->setRelativePosition(r);
}


void CGUIPanel::updateAbsolutePosition()
{
	IGUIElement::updateAbsolutePosition();
	calculateClientArea();
	resizeInnerPane();
}


void CGUIPanel::resizeInnerPane()
{
	if (!HScrollBar || !VScrollBar || !InnerPane || !ClipPane)
		return;

	// get outer pane size
	core::rect<s32> outerRect = ClipPane->getRelativePosition();

	// resize flexible children depending on outer pane
	InnerPane->setRelativePosition(outerRect);

	// get desired size (total size of all children)
	core::rect<s32> totalRect(0, 0, 0, 0);

	core::list<IGUIElement*>::ConstIterator it;

	for (it  = InnerPane->getChildren().begin();
         it != InnerPane->getChildren().end(); ++it)
	{
		core::rect<s32> rct = (*it)->getRelativePosition();
		totalRect.addInternalPoint(rct.UpperLeftCorner);
		totalRect.addInternalPoint(rct.LowerRightCorner);
	}

	// move children if pane needs to grow
	core::position2di adjustedMovement(0,0);

	if (totalRect.UpperLeftCorner.X < 0)
		adjustedMovement.X = -totalRect.UpperLeftCorner.X;
	if (totalRect.UpperLeftCorner.Y < 0)
		adjustedMovement.Y = -totalRect.UpperLeftCorner.Y;

	if (adjustedMovement.X > 0 || adjustedMovement.Y > 0)
	{
		totalRect += adjustedMovement;

		for (it = InnerPane->getChildren().begin();
			it != InnerPane->getChildren().end(); ++it )
		{
			(*it)->move(adjustedMovement);
		}
	}

	// make sure the inner pane is at least as big as the outer
	if (totalRect.getWidth() < outerRect.getWidth())
	{
		totalRect.UpperLeftCorner.X = 0;
		totalRect.LowerRightCorner.X = outerRect.getWidth();
	}
	if (totalRect.getHeight() < outerRect.getHeight())
	{
		totalRect.UpperLeftCorner.Y = 0;
		totalRect.LowerRightCorner.Y = outerRect.getHeight();
	}

	InnerPane->setRelativePosition(totalRect);

	// scrollbars
	if ( HScrollBarMode != ESBM_ALWAYS_INVISIBLE &&
		(totalRect.getWidth() > outerRect.getWidth() || HScrollBarMode == ESBM_ALWAYS_VISIBLE) )
	{
		HScrollBar->setVisible(true);
		HScrollBar->setMax(totalRect.getWidth() - outerRect.getWidth());
		bringToFront(HScrollBar);
	}
	else
		HScrollBar->setVisible(false);

	if ( VScrollBarMode != ESBM_ALWAYS_INVISIBLE &&
		(totalRect.getHeight() > outerRect.getHeight() || VScrollBarMode == ESBM_ALWAYS_VISIBLE) )
	{
		VScrollBar->setVisible(true);
		VScrollBar->setMax(totalRect.getHeight() - outerRect.getHeight());
		bringToFront(VScrollBar);
	}
	else
		VScrollBar->setVisible(false);

	// move to adjust for scrollbar pos
	moveInnerPane();
}

void CGUIPanel::calculateClientArea()
{
	core::rect<s32> ClientArea(0,0, AbsoluteRect.getWidth(),AbsoluteRect.getHeight());

	if (VScrollBar->isVisible())
		ClientArea.LowerRightCorner.X -= VScrollBar->getRelativePosition().getWidth();

	if (HScrollBar->isVisible())
		ClientArea.LowerRightCorner.Y -= HScrollBar->getRelativePosition().getHeight();

	if (Border)
	{
		ClientArea.UpperLeftCorner += core::position2d<s32>( BORDER_WIDTH, BORDER_WIDTH );
		ClientArea.LowerRightCorner -= core::position2d<s32>( BORDER_WIDTH, BORDER_WIDTH );
	}

	ClipPane->setRelativePosition(ClientArea);
}

core::rect<s32> CGUIPanel::getClientArea() const
{
	return ClipPane->getRelativePosition();
}

void CGUIPanel::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options)
{
	IGUIElement::serializeAttributes(out, options);

	out->addBool("border", Border);
	out->addEnum("horizontalScrollBar", HScrollBarMode, GUIScrollBarModeNames );
	out->addEnum("verticalScrollBar", VScrollBarMode, GUIScrollBarModeNames );
}

void CGUIPanel::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
	IGUIElement::deserializeAttributes(in, options);

	setBorder(in->getAttributeAsBool("border"));
	setHScrollBarMode((E_SCROLL_BAR_MODE)in->getAttributeAsEnumeration("horizontalScrollBar", GUIScrollBarModeNames));
	setVScrollBarMode((E_SCROLL_BAR_MODE)in->getAttributeAsEnumeration("verticalScrollBar", GUIScrollBarModeNames));
}

} // namespace gui
} // namespace irr