summaryrefslogtreecommitdiff
path: root/main.go
blob: 9a425b63be6062e5e8d844b3bcfd64dadd663b80 (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
// 7 february 2014
package main

import (
	"fmt"
	"os"
	"runtime"
)

func fatalf(format string, args ...interface{}) {
	s := fmt.Sprintf(format, args...)
	_, err := MessageBox(NULL,
		"An internal error has occured:\n" + s,
		os.Args[0],
		MB_OK | MB_ICONERROR)
	if err == nil {
		os.Exit(1)
	}
	panic(fmt.Sprintf("error trying to warn user of internal error: %v\ninternal error:\n%s", err, s))
}

const (
	IDC_BUTTON = 100 + iota
	IDC_VARCOMBO
	IDC_FIXCOMBO
	IDC_EDIT
	IDC_LIST
	IDC_LABEL
	IDC_CHECK
)

var varCombo, fixCombo, edit, list HWND

func wndProc(hwnd HWND, msg uint32, wParam WPARAM, lParam LPARAM) LRESULT {
	switch msg {
	case WM_COMMAND:
		if wParam.LOWORD() == IDC_BUTTON {
			buttonclick := "neither clicked nor double clicked (somehow)"
			if wParam.HIWORD() == BN_CLICKED {
				buttonclick = "clicked"
			} else if wParam.HIWORD() == BN_DOUBLECLICKED {
				buttonclick = "double clicked"
			}

			varText, err := getText(varCombo)
			if err != nil {
				fatalf("error getting variable combo box text: %v", err)
			}

			fixTextWM, err := getText(fixCombo)
			if err != nil {
				fatalf("error getting fixed combo box text with WM_GETTEXT: %v", err)
			}

			fixTextIndex, err := SendMessage(fixCombo, CB_GETCURSEL, 0, 0)
			if err != nil {
				fatalf("error getting fixed combo box current selection: %v", err)
			}
			// TODO get text from index

			editText, err := getText(edit)
			if err != nil {
				fatalf("error getting edit field text: %v", err)
			}

			listIndex, err := SendMessage(list, LB_GETCURSEL, 0, 0)
			if err != nil {
				fatalf("error getting fixed list box current selection: %v", err)
			}
			// TODO get text from index

			checkState, err := IsDlgButtonChecked(hwnd, IDC_CHECK)
			if err != nil {
				fatalf("error getting checkbox check state: %v", err)
			}

			MessageBox(hwnd,
				fmt.Sprintf("button state: %s\n" +
					"variable combo box text: %s\n" +
					"fixed combo box text with WM_GETTEXT: %s\n" +
					"fixed combo box current index: %d\n" +
					"edit field text: %s\n" +
					"list box current index: %d\n" +
					"check box checked: %v\n",
					buttonclick, varText, fixTextWM, fixTextIndex, editText, listIndex, checkState == BST_CHECKED),
				"note",
				MB_OK)
		}
		return 0
	case WM_CLOSE:
		err := DestroyWindow(hwnd)
		if err != nil {
			fatalf("error destroying window: %v", err)
		}
		return 0
	case WM_DESTROY:
		err := PostQuitMessage(0)
		if err != nil {
			fatalf("error posting quit message: %v", err)
		}
		return 0
	default:
		return DefWindowProc(hwnd, msg, wParam, lParam)
	}
	fatalf("major bug: forgot a return on wndProc for message %d", msg)
	panic("unreachable")
}

const className = "mainwin"

func main() {
	runtime.LockOSThread()

	hInstance, err := getWinMainhInstance()
	if err != nil {
		fatalf("error getting WinMain hInstance: %v", err)
	}
	nCmdShow, err := getWinMainnCmdShow()
	if err != nil {
		fatalf("error getting WinMain nCmdShow: %v", err)
	}

	icon, err := LoadIcon_ResourceID(NULL, IDI_APPLICATION)
	if err != nil {
		fatalf("error getting window icon: %v", err)
	}
	cursor, err := LoadCursor_ResourceID(NULL, IDC_ARROW)
	if err != nil {
		fatalf("error getting window cursor: %v", err)
	}

	wc := &WNDCLASS{
		LpszClassName:	className,
		LpfnWndProc:		wndProc,
		HInstance:		hInstance,
		HIcon:			icon,
		HCursor:			cursor,
		HbrBackground:	HBRUSH(COLOR_WINDOW + 1),
	}
	_, err = RegisterClass(wc)
	if err != nil {
		fatalf("error registering window class: %v", err)
	}

	hwnd, err := CreateWindowEx(
		WS_EX_OVERLAPPEDWINDOW,
		className, "Main Window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
		NULL, NULL, hInstance, NULL)
	if err != nil {
		fatalf("error creating window: %v", err)
	}

	const controlStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP

	_, err = CreateWindowEx(
		0,
		"BUTTON", "Click Me",
		BS_PUSHBUTTON | controlStyle,
		20, 20, 100, 20,
		hwnd, HMENU(IDC_BUTTON), hInstance, NULL)
	if err != nil {
		fatalf("error creating button: %v", err)
	}

	varCombo, err = CreateWindowEx(
		0,
		"COMBOBOX", "",
		CBS_DROPDOWN | CBS_AUTOHSCROLL | controlStyle,
		140, 20, 100, 20,
		hwnd, HMENU(IDC_VARCOMBO), hInstance, NULL)
	if err != nil {
		fatalf("error creating variable combo box: %v", err)
	}
	vcItems := []string{"a", "b", "c", "d"}
	for _, v := range vcItems {
		_, err := SendMessage(varCombo, CB_ADDSTRING, 0,
			LPARAMFromString(v))
		if err != nil {
			fatalf("error adding %q to variable combo box: %v", v, err)
		}
	}

	fixCombo, err = CreateWindowEx(
		0,
		"COMBOBOX", "",
		CBS_DROPDOWNLIST | controlStyle,
		140, 50, 100, 20,
		hwnd, HMENU(IDC_FIXCOMBO), hInstance, NULL)
	if err != nil {
		fatalf("error creating fixed combo box: %v", err)
	}
	fcItems := []string{"e", "f", "g", "h"}
	for _, v := range fcItems {
		_, err := SendMessage(fixCombo, CB_ADDSTRING, 0,
			LPARAMFromString(v))
		if err != nil {
			fatalf("error adding %q to fixed combo box: %v", v, err)
		}
	}

	edit, err = CreateWindowEx(
		0,
		"EDIT", "",
		ES_AUTOHSCROLL | ES_NOHIDESEL | WS_BORDER | controlStyle,
		20, 50, 100, 20,
		hwnd, HMENU(IDC_EDIT), hInstance, NULL)
	if err != nil {
		fatalf("error creating edit field: %v", err)
	}

	list, err = CreateWindowEx(
		0,
		"LISTBOX", "",
		LBS_STANDARD | controlStyle,
		20, 80, 100, 100,
		hwnd, HMENU(IDC_FIXCOMBO), hInstance, NULL)
	if err != nil {
		fatalf("error creating list box: %v", err)
	}
	lItems := []string{"i", "j", "k", "l"}
	for _, v := range lItems {
		_, err := SendMessage(list, LB_ADDSTRING, 0,
			LPARAMFromString(v))
		if err != nil {
			fatalf("error adding %q to list box: %v", v, err)
		}
		// TODO check actual return value as THAT indicates an error
	}

	_, err = CreateWindowEx(
		0,
		"STATIC", "Label",
		SS_NOPREFIX | controlStyle,
		140, 80, 100, 20,
		hwnd, HMENU(IDC_FIXCOMBO), hInstance, NULL)
	if err != nil {
		fatalf("error creating label: %v", err)
	}

	_, err = CreateWindowEx(
		0,
		"BUTTON", "Checkbox",
		BS_AUTOCHECKBOX | controlStyle,
		140, 110, 100, 20,
		hwnd, HMENU(IDC_CHECK), hInstance, NULL)
	if err != nil {
		fatalf("error creating checkbox: %v", err)
	}

	_, err = ShowWindow(hwnd, nCmdShow)
	if err != nil {
		fatalf("error showing window: %v", err)
	}
	err = UpdateWindow(hwnd)
	if err != nil {
		fatalf("error updating window: %v", err)
	}

	for {
		msg, done, err := GetMessage(NULL, 0, 0)
		if err != nil {
			fatalf("error getting message: %v", err)
		}
		if done {
			break
		}
		_, err = TranslateMessage(msg)
		if err != nil {
			fatalf("error translating message: %v", err)
		}
		_, err = DispatchMessage(msg)
		if err != nil {
			fatalf("error dispatching message: %v", err)
		}
	}
}