File: window.c

package info (click to toggle)
golang-golang-x-exp 0.0~git20150826.1.eb7c1fa-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,000 kB
  • ctags: 2,884
  • sloc: objc: 239; ansic: 195; sh: 124; asm: 24; makefile: 8
file content (107 lines) | stat: -rw-r--r-- 2,681 bytes parent folder | download
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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

#include "_cgo_export.h"
#include "windriver.h"

#define windowClass L"shiny_Window"

static LRESULT CALLBACK windowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	WINDOWPOS *wp = (WINDOWPOS *) lParam;
	RECT r;
	HDC dc;

	switch (uMsg) {
	case WM_PAINT:
		handlePaint(hwnd);
		break; // explicitly defer to DefWindowProc; it will handle validation for us
	case WM_WINDOWPOSCHANGED:
		if ((wp->flags & SWP_NOSIZE) != 0) {
			break;
		}
		if (GetClientRect(hwnd, &r) == 0) {
			/* TODO(andlabs) */;
		}
		sendSizeEvent(hwnd, &r);
		return 0;
	case WM_MOUSEMOVE:
	case WM_LBUTTONDOWN:
		// TODO(andlabs): call SetFocus()?
	case WM_LBUTTONUP:
	case WM_MBUTTONDOWN:
	case WM_MBUTTONUP:
	case WM_RBUTTONDOWN:
	case WM_RBUTTONUP:
		sendMouseEvent(hwnd, uMsg,
			GET_X_LPARAM(lParam),
			GET_Y_LPARAM(lParam));
		return 0;
	case WM_KEYDOWN:
	case WM_KEYUP:
	case WM_SYSKEYDOWN:
	case WM_SYSKEYUP:
		// TODO
		break;
	case msgFillSrc:
		// TODO error checks
		dc = GetDC(hwnd);
		fillSrc(dc, (RECT *) lParam, (COLORREF) wParam);
		ReleaseDC(hwnd, dc);
		break;
	case msgFillOver:
		// TODO error checks
		dc = GetDC(hwnd);
		fillOver(dc, (RECT *) lParam, (COLORREF) wParam);
		ReleaseDC(hwnd, dc);
		break;
	}
	return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}

HRESULT initWindowClass(void) {
	WNDCLASSW wc;

	ZeroMemory(&wc, sizeof (WNDCLASSW));
	wc.lpszClassName = windowClass;
	wc.lpfnWndProc = windowWndProc;
	wc.hInstance = thishInstance;
	wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
	if (wc.hIcon == NULL) {
		return lastErrorToHRESULT();
	}
	wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
	if (wc.hCursor == NULL) {
		return lastErrorToHRESULT();
	}
	// TODO(andlabs): change this to something else? NULL? the hollow brush?
	wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
	if (RegisterClassW(&wc) == 0) {
		return lastErrorToHRESULT();
	}
	return S_OK;
}

HRESULT createWindow(HWND *phwnd) {
	*phwnd = CreateWindowExW(0,
		windowClass, L"Shiny Window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, thishInstance, NULL);
	if (*phwnd == NULL) {
		return lastErrorToLRESULT();
	}
	// TODO(andlabs): use proper nCmdShow
	ShowWindow(*phwnd, SW_SHOWDEFAULT);
	// TODO(andlabs): call UpdateWindow()
	return lS_OK;
}

void sendFill(HWND hwnd, UINT uMsg, RECT r, COLORREF color) {
	// Note: this SendMessageW won't return until after the fill
	// completes, so using &r is safe.
	SendMessageW(hwnd, uMsg, (WPARAM) color, (LPARAM) (&r));
}