File: Container.cpp

package info (click to toggle)
storm-lang 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 51,984 kB
  • sloc: ansic: 261,420; cpp: 140,270; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (277 lines) | stat: -rw-r--r-- 6,805 bytes parent folder | download | duplicates (3)
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
#include "stdafx.h"
#include "Container.h"
#include "Exception.h"
#include "RadioButton.h"
#include <limits>

namespace gui {

	ContainerBase::ContainerBase() {}

	RadioGroup *ContainerBase::radioGroup(Nat id) {
		if (!radioGroups)
			radioGroups = new (this) Map<Nat, RadioGroup *>();

		Map<Nat, RadioGroup *>::Iter i = radioGroups->find(id);
		if (i != radioGroups->end())
			return i.v();

		RadioGroup *g = new (this) RadioGroup();
		radioGroups->put(id, g);
		return g;
	}

#ifdef GUI_GTK
	void ContainerBase::addChild(GtkWidget *child, Rect pos) {
		throw new (this) InternalError(S("Need to implement 'addChild' for custom containers!"));
	}

	void ContainerBase::moveChild(GtkWidget *child, Rect pos) {
		throw new (this) InternalError(S("Need to implement 'moveChild' for custom containers!"));
	}
#endif

#ifdef GUI_WIN32
	// Reserved ids.
	static const Nat reservedTo = IDCANCEL;
#else
	static const Nat reservedTo = 0;
#endif

	Container::Container() {
		ids = new (this) IdMap();
		windows = new (this) WinMap();
		lastId = reservedTo;
	}

	void Container::add(Window *child) {
		if (child->parent())
			throw new (this) GuiError(S("Can not attach a child multiple times or to multiple parents."));

		nat id = allocate(child);
		child->attachParent(this);

		if (created())
			child->parentCreated(id);
	}

	void Container::remove(Window *child) {
		if (!windows->has(child))
			return;

		child->detachParent();
		Nat id = windows->get(child);
		ids->remove(id);
		windows->remove(child);
	}

	Array<Window *> *Container::children() const {
		Array<Window *> *result = new (this) Array<Window *>();
		result->reserve(ids->count());
		for (IdMap::Iter i = ids->begin(), end = ids->end(); i != end; ++i) {
			result->push(i.v());
		}
		return result;
	}

	void Container::parentCreated(Nat id) {
		Window::parentCreated(id);

		// Late creation, create any remaining children!
		for (IdMap::Iter i = ids->begin(), end = ids->end(); i != end; ++i) {
			Window *w = i.v();
			if (!w->created())
				w->parentCreated(i.k());
		}
	}

	void Container::windowDestroyed() {
		Window::windowDestroyed();

		for (IdMap::Iter i = ids->begin(), end = ids->end(); i != end; ++i) {
			Window *w = i.v();
			if (w->created())
				w->handle(invalid);
		}
	}

	Nat Container::allocate(Window *window) {
		Nat firstFree;
		// Fallback in rare cases with a lot of creation/destruction.
		if (lastId == std::numeric_limits<Nat>::max()) {
			WARNING(L"Wrapping. Tab order will not be as expected!");
			// Wrap around and find something not used... We will most likely not have 2^32 controls
			// in a window, so we should find an empty id somewhere!
			for (firstFree = reservedTo + 1; ids->has(firstFree); firstFree++)
				;
		} else {
			firstFree = ++lastId;
		}

		ids->put(firstFree, window);
		windows->put(window, firstFree);
		return firstFree;
	}

	void Container::allocate(Window *window, Nat id) {
		if (id == 0)
			return;

		if (ids->has(id))
			throw new (this) GuiError(TO_S(this, S("The id ") << id << S(" is already in use.")));

		ids->put(id, window);
		windows->put(window, id);
	}

#ifdef GUI_WIN32

	bool Container::create(ContainerBase *parent, nat id) {
		// Make sure it is a control parent to make tab and control focus work properly.
		return Window::createEx(NULL, childFlags, WS_EX_CONTROLPARENT, parent->handle().hwnd(), id);
	}

	MsgResult Container::onMessage(const Message &msg) {
		switch (msg.msg) {
		case WM_COMMAND:
			if (onCommand(msg))
				return msgResult(0);
			break;
		case WM_NOTIFY:
			if (onNotify(msg))
				return msgResult(0);
			break;
		}

		return Window::onMessage(msg);
	}

	bool Container::onCommand(const Message &msg) {
		nat type = HIWORD(msg.wParam);
		nat id = LOWORD(msg.wParam);

		// Menus and accelerators. Not implemented yet.
		if (msg.lParam == 0 || msg.lParam == 1)
			return false;

		IdMap::Iter i = ids->find(id);
		if (i == ids->end())
			return false;

		return i.v()->onCommand(type);
	}

	bool Container::onNotify(const Message &msg) {
		NMHDR *data = (NMHDR *)msg.lParam;
		nat id = nat(data->idFrom);

		IdMap::Iter i = ids->find(id);
		if (i == ids->end())
			return false;

		return i.v()->onNotify(data);
	}

	void Container::updateDpi(Bool move) {
		Window::updateDpi(move);

		for (IdMap::Iter i = ids->begin(), end = ids->end(); i != end; ++i) {
			Window *w = i.v();
			w->updateDpi(true);
		}
	}

#endif
#ifdef GUI_GTK

	bool Container::create(ContainerBase *parent, nat id) {
		// We need an event box to catch events.
		GtkWidget *box = gtk_event_box_new();

		GtkWidget *container = basic_new();
		gtk_container_add(GTK_CONTAINER(box), container);
		gtk_widget_show(container);

		initWidget(parent, box);
		return true;
	}

	void Container::addChild(GtkWidget *child, Rect pos) {
		// There is a BASIC layout inside the frame.
		Basic *basic = BASIC(gtk_bin_get_child(GTK_BIN(handle().widget())));
		Size s = pos.size();
		basic_put(basic, child, pos.p0.x, pos.p0.y, s.w, s.h);
	}

	void Container::moveChild(GtkWidget *child, Rect pos) {
		// There is a BASIC layout inside the frame.
		Basic *basic = BASIC(gtk_bin_get_child(GTK_BIN(handle().widget())));
		Size s = pos.size();
		basic_move(basic, child, pos.p0.x, pos.p0.y, s.w, s.h);
	}

	void Container::updateGtkMinSize() {
		Size min = minSize();
		Basic *basic = BASIC(gtk_bin_get_child(GTK_BIN(handle().widget())));
		basic_set_min_size(basic, gint(min.w), gint(min.h));
	}

#endif


	SingleChildContainer::SingleChildContainer() {}

	SingleChildContainer::SingleChildContainer(Window *child) {
		content(child);
	}

	void SingleChildContainer::content(Window *child) {
		if (this->child && this->child->created()) {
			this->child->detachParent();
		}

		if (child->parent())
			throw new (this) GuiError(S("Can not attach a child multiple times or to multiple parents."));
		child->attachParent(this);
		this->child = child;

		if (created())
			child->parentCreated(1);
	}

	void SingleChildContainer::parentCreated(nat id) {
		Window::parentCreated(id);

		if (child)
			child->parentCreated(1);
	}

	void SingleChildContainer::windowDestroyed() {
		ContainerBase::windowDestroyed();

		if (child && child->created())
			child->handle(invalid);
	}

#ifdef GUI_GTK

	void SingleChildContainer::addChild(GtkWidget *child, Rect pos) {
		// Remove any child already inside the scroll window (a Scrollable is added automatically)
		GtkWidget *content = gtk_bin_get_child(GTK_BIN(handle().widget()));
		if (content)
			gtk_container_remove(GTK_CONTAINER(handle().widget()), content);

		// Now we can add the new child.
		gtk_container_add(GTK_CONTAINER(handle().widget()), child);
	}

	void SingleChildContainer::moveChild(GtkWidget *child, Rect pos) {}

	void SingleChildContainer::updateGtkMinSize() {
		if (child)
			child->updateGtkMinSize();
	}

#endif

}