File: TabView.cpp

package info (click to toggle)
storm-lang 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (262 lines) | stat: -rw-r--r-- 6,541 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
262
#include "stdafx.h"
#include "TabView.h"
#include "Win32Dpi.h"
#include "MnemonicStr.h"

namespace gui {

	TabView::TabView() {
		children = new (this) Array<Window *>();
		titles = new (this) Array<Str *>();
	}

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

		for (Nat i = 0; i < children->count(); i++) {
			Window *child = children->at(i);
			if (child->created())
				child->handle(invalid);
		}

#ifdef GUI_WIN32
		if (backgroundBrush != Handle())
			DeleteObject(backgroundBrush.brush());
#endif
	}

	void TabView::add(Str *title, Window *child) {
		children->push(child);
		titles->push(title);

		child->attachParent(this);

		if (created()) {
			// We need to tell the child to create itself before we add the tab. Otherwise the Gtk
			// code will not work.
			child->parentCreated(children->count());

			tabAdded(child, title, children->count() - 1);
		}
	}

	void TabView::tab(Str *title, Window *child) {
		add(title, child);
	}

#ifdef GUI_WIN32

	void TabView::selected(Nat tab) {
		if (created()) {
			if (currentSelected < children->count())
				children->at(currentSelected)->visible(false);
			if (tab < children->count())
				children->at(tab)->visible(true);
		}

		currentSelected = tab;
	}

	Size TabView::minSize() {
		Size result;
		for (Nat i = 0; i < children->count(); i++)
			result = result.max(children->at(i)->minSize());

		if (created()) {
			// We can just use the Win32 API to adjust the size.
			Nat dpi = currentDpi();
			Size px = dpiToPx(dpi, result);
			RECT r = convert(Rect(Point(), px));
			TabCtrl_AdjustRect(handle().hwnd(), TRUE, &r);
			result = convert(r).size();
		} else {
			// We could try to do this manually, but many controls don't report an accurate size
			// until they are created, so this is fine.
		}
		return result;
	}

	void TabView::parentCreated(nat id) {
		ContainerBase::parentCreated(id);

		// Create children with ID from 1 and upwards.
		for (Nat i = 0; i < children->count(); i++)
			children->at(i)->parentCreated(i + 1);

		// Update UI?
		resized(pos().size());
	}

	void TabView::pos(Rect r) {
		ContainerBase::pos(r);

		// We need this since we won't get the messages sent to the control in general (at least not
		// the ones we send ourselves).
		resized(r.size());
	}

	void TabView::resized(Size size) {
		if (created()) {
			Nat dpi = currentDpi();
			dpiToPx(dpi, size);

			RECT position = {
				0, 0,
				LONG(size.w), LONG(size.h)
			};
			TabCtrl_AdjustRect(handle().hwnd(), FALSE, &position);

			for (Nat i = 0; i < children->count(); i++) {
				Window *child = children->at(i);
				if (child->created()) {
					// Call movewindow directly so we don't have to convert back and forward through DPI.
					MoveWindow(child->handle().hwnd(),
							position.left, position.top,
							position.right - position.left, position.bottom - position.top, TRUE);
				}
			}
		}
	}

	bool TabView::onNotify(NMHDR *header) {
		if (header->code == TCN_SELCHANGE) {
			selected(TabCtrl_GetCurSel(handle().hwnd()));
		}
		return true;
	}

	bool TabView::create(ContainerBase *parent, nat id) {
		DWORD flags = controlFlags | WS_CLIPSIBLINGS;
		if (!createEx(WC_TABCONTROL, flags, WS_EX_CONTROLPARENT, parent->handle().hwnd(), id, cWin32Subclass))
			return false;

		for (Nat i = 0; i < children->count(); i++)
			tabAdded(children->at(i), titles->at(i), i);

		if (currentSelected < children->count())
			children->at(currentSelected)->visible(true);

		return true;
	}

	void TabView::windowBackground(HBRUSH &brush, COLORREF &color) {
		if (backgroundBrush != Handle()) {
			brush = backgroundBrush.brush();
			color = COLORREF(backgroundColor);
		} else {
			// Sensible default, even if not 100% correct.
			// Should only be used briefly, before things are drawn.
			brush = GetSysColorBrush(COLOR_WINDOW);
			color = GetSysColor(COLOR_WINDOW);
		}
	}

	MsgResult TabView::onMessage(const Message &msg) {
		switch (msg.msg) {
		case WM_PAINT:
			return noResultNotify();
		}
		return ContainerBase::onMessage(msg);
	}

	void TabView::afterMessage(const Message &msg) {
		if (msg.msg == WM_PAINT && backgroundBrush == Handle()) {
			HWND wnd = handle().hwnd();
			RECT inspect;
			GetClientRect(wnd, &inspect);

			TabCtrl_AdjustRect(wnd, FALSE, &inspect);

			HDC dc = GetDC(handle().hwnd());
			COLORREF color = GetPixel(dc, inspect.left, inspect.top);
			ReleaseDC(wnd, dc);

			backgroundBrush = Handle(CreateSolidBrush(color));
			backgroundColor = size_t(color);
		}
	}

	void TabView::updateDpi(Bool move) {
		for (Nat i = 0; i < children->count(); i++)
			children->at(i)->updateDpi(true);
		ContainerBase::updateDpi(move);
	}

	void TabView::tabAdded(Window *child, Str *title, Nat id) {
		child->visible(false);

		TCITEM item;
		item.mask = TCIF_TEXT | TCIF_PARAM;
		item.pszText = (LPWSTR)(MnemonicStr(title).win32Mnemonic())->c_str();
		item.lParam = id;
		TabCtrl_InsertItem(handle().hwnd(), id, &item);
	}

#endif
#ifdef GUI_GTK

	Size TabView::minSize() {
		gint w = 0, h = 0;

		if (created()) {
			for (Nat i = 0; i < children->count(); i++)
				children->at(i)->updateGtkMinSize();

			gtk_widget_get_preferred_width(handle().widget(), &w, NULL);
			gtk_widget_get_preferred_height(handle().widget(), &h, NULL);
		}

		return Size(Float(w), Float(h));
	}

	void TabView::parentCreated(nat id) {
		// Tell our children to create themselves first. This makes it possible for us to add them
		// directly later on.
		for (Nat i = 0; i < children->count(); i++)
			children->at(i)->parentCreated(i + 1);

		Window::parentCreated(id);
		// Handle size?
	}

	bool TabView::create(ContainerBase *parent, nat id) {
		GtkWidget *widget = gtk_notebook_new();

		initWidget(parent, widget);

		for (Nat i = 0; i < children->count(); i++)
			tabAdded(children->at(i), titles->at(i), i);

		return true;
	}

	void TabView::moveChild(GtkWidget *child, Rect pos) {
		// We don't honor requests from children.
	}

	void TabView::selected(Nat tab) {
		currentSelected = tab;
	}

	void TabView::addChild(GtkWidget *child, Rect pos) {
		// We don't do anything here.
	}

	void TabView::tabAdded(Window *child, Str *title, Nat id) {
		GtkNotebook *notebook = GTK_NOTEBOOK(handle().widget());
		gtk_notebook_append_page(notebook, child->handle().widget(), gtk_label_new(title->utf8_str()));
	}

	void TabView::pos(Rect r) {
		// We don't need to do anything special on Gtk.
		ContainerBase::pos(r);
	}

	void TabView::resized(Size size) {
		// We don't need to do anything special on Gtk.
		ContainerBase::resized(size);
	}

#endif

}