summaryrefslogtreecommitdiff
path: root/new/init_windows.c
blob: c484ed3c46c118d31d422c123c6d79cd73ca883a (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
// 6 april 2015
#include "uipriv_windows.h"

HINSTANCE hInstance;
int nCmdShow;

HFONT hMessageFont;

HWND initialParent;

struct uiInitError {
	char *msg;
	char failbuf[256];
};

static uiInitError *loadLastError(uiInitError *err, const char *message)
{
	DWORD le;

	le = GetLastError();
	// TODO FormatMessageW() it
	// TODO make sure argument is right; _snprintf_s() isn't supported on Windows XP
	snprintf(err->failbuf, 256, "error %s (last error %I32u)", message, le);
	err->msg = err->failbuf;
	return err;
}

uiInitError *uiInit(uiInitOptions *o)
{
	uiInitError *err;
	STARTUPINFOW si;
	const char *ce;
	HICON hDefaultIcon;
	HCURSOR hDefaultCursor;
	NONCLIENTMETRICSW ncm;

	err = uiNew(uiInitError);

	hInstance = GetModuleHandle(NULL);
	if (hInstance == NULL)
		return loadLastError(err, "getting program HINSTANCE");

	nCmdShow = SW_SHOWDEFAULT;
	GetStartupInfoW(&si);
	if ((si.dwFlags & STARTF_USESHOWWINDOW) != 0)
		nCmdShow = si.wShowWindow;

	// TODO add "in initCommonControls()" to each of the messages this returns
	ce = initCommonControls();
	if (ce != NULL)
		return loadLastError(err, ce);

	hDefaultIcon = LoadIconW(NULL, IDI_APPLICATION);
	if (hDefaultIcon == NULL)
		return loadLastError(err, "loading default icon for window classes");
	hDefaultCursor = LoadCursorW(NULL, IDC_ARROW);
	if (hDefaultCursor == NULL)
		return loadLastError(err, "loading default cursor for window classes");

	if (registerWindowClass(hDefaultIcon, hDefaultCursor) == 0)
		return loadLastError(err, "registering uiWindow window class");

	ZeroMemory(&ncm, sizeof (NONCLIENTMETRICSW));
	ncm.cbSize = sizeof (NONCLIENTMETRICSW);
	if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof (NONCLIENTMETRICSW), &ncm, sizeof (NONCLIENTMETRICSW)) == 0)
		return loadLastError(err, "getting default fonts");
	hMessageFont = CreateFontIndirectW(&(ncm.lfMessageFont));
	if (hMessageFont == NULL)
		return loadLastError(err, "loading default messagebox font; this is the default UI font");

	// give each control a reasonable initial parent
	// don't free the initial parent!
	// TODO tune this better; it shouldn't be closed, for instance
	initialParent = (HWND) uiWindowHandle(uiNewWindow("", 0, 0));

	uiFree(err);
	return NULL;
}

const char *uiInitErrorMessage(uiInitError *err)
{
	return err->msg;
}

void uiInitErrorFree(uiInitError *err)
{
	if (err->msg != err->failbuf)
		uiFree(err->msg);
	uiFree(err);
}