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
|
// 6 april 2015
#include "uipriv_windows.hpp"
HINSTANCE hInstance;
int nCmdShow;
HFONT hMessageFont;
// LONGTERM needed?
HBRUSH hollowBrush;
// the returned pointer is actually to the second character
// if the first character is - then free, otherwise don't
static const char *initerr(const char *message, const WCHAR *label, DWORD value)
{
WCHAR *sysmsg;
BOOL hassysmsg;
WCHAR *wmessage;
WCHAR *wout;
char *out;
hassysmsg = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, value, 0, (LPWSTR) (&sysmsg), 0, NULL) != 0;
if (!hassysmsg)
sysmsg = L"";
wmessage = toUTF16(message + 1);
wout = strf(L"-error initializing libui: %s; code %I32d (0x%08I32X) %s",
wmessage,
value, value,
sysmsg);
uiFree(wmessage);
if (hassysmsg)
LocalFree(sysmsg); // ignore error
out = toUTF8(wout);
uiFree(wout);
return out + 1;
}
#define ieLastErr(msg) initerr("=" msg, L"GetLastError() ==", GetLastError())
#define ieHRESULT(msg, hr) initerr("=" msg, L"HRESULT", (DWORD) hr)
// LONGTERM make common
uiInitOptions options;
#define wantedICCClasses ( \
ICC_STANDARD_CLASSES | /* user32.dll controls */ \
ICC_PROGRESS_CLASS | /* progress bars */ \
ICC_TAB_CLASSES | /* tabs */ \
ICC_LISTVIEW_CLASSES | /* table headers */ \
ICC_UPDOWN_CLASS | /* spinboxes */ \
ICC_BAR_CLASSES | /* trackbar */ \
ICC_DATE_CLASSES | /* date/time picker */ \
0)
const char *uiInit(uiInitOptions *o)
{
STARTUPINFOW si;
const char *ce;
HICON hDefaultIcon;
HCURSOR hDefaultCursor;
NONCLIENTMETRICSW ncm;
INITCOMMONCONTROLSEX icc;
HRESULT hr;
options = *o;
initAlloc();
nCmdShow = SW_SHOWDEFAULT;
GetStartupInfoW(&si);
if ((si.dwFlags & STARTF_USESHOWWINDOW) != 0)
nCmdShow = si.wShowWindow;
SetProcessDPIAware();
hDefaultIcon = LoadIconW(NULL, IDI_APPLICATION);
if (hDefaultIcon == NULL)
return ieLastErr("loading default icon for window classes");
hDefaultCursor = LoadCursorW(NULL, IDC_ARROW);
if (hDefaultCursor == NULL)
return ieLastErr("loading default cursor for window classes");
ce = initUtilWindow(hDefaultIcon, hDefaultCursor);
if (ce != NULL)
return initerr(ce, L"GetLastError() ==", GetLastError());
if (registerWindowClass(hDefaultIcon, hDefaultCursor) == 0)
return ieLastErr("registering uiWindow window class");
ZeroMemory(&ncm, sizeof (NONCLIENTMETRICSW));
ncm.cbSize = sizeof (NONCLIENTMETRICSW);
if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof (NONCLIENTMETRICSW), &ncm, sizeof (NONCLIENTMETRICSW)) == 0)
return ieLastErr("getting default fonts");
hMessageFont = CreateFontIndirectW(&(ncm.lfMessageFont));
if (hMessageFont == NULL)
return ieLastErr("loading default messagebox font; this is the default UI font");
if (initContainer(hDefaultIcon, hDefaultCursor) == 0)
return ieLastErr("initializing uiWindowsMakeContainer() window class");
hollowBrush = (HBRUSH) GetStockObject(HOLLOW_BRUSH);
if (hollowBrush == NULL)
return ieLastErr("getting hollow brush");
ZeroMemory(&icc, sizeof (INITCOMMONCONTROLSEX));
icc.dwSize = sizeof (INITCOMMONCONTROLSEX);
icc.dwICC = wantedICCClasses;
if (InitCommonControlsEx(&icc) == 0)
return ieLastErr("initializing Common Controls");
hr = CoInitialize(NULL);
if (hr != S_OK && hr != S_FALSE)
return ieHRESULT("initializing COM", hr);
// LONGTERM initialize COM security
// LONGTERM (windows vista) turn off COM exception handling
hr = initDraw();
if (hr != S_OK)
return ieHRESULT("initializing Direct2D", hr);
hr = initDrawText();
if (hr != S_OK)
return ieHRESULT("initializing DirectWrite", hr);
if (registerAreaClass(hDefaultIcon, hDefaultCursor) == 0)
return ieLastErr("registering uiArea window class");
if (registerMessageFilter() == 0)
return ieLastErr("registering libui message filter");
if (registerD2DScratchClass(hDefaultIcon, hDefaultCursor) == 0)
return ieLastErr("initializing D2D scratch window class");
return NULL;
}
void uiUninit(void)
{
uninitMenus();
unregisterD2DScratchClass();
unregisterMessageFilter();
unregisterArea();
uninitDrawText();
uninitDraw();
CoUninitialize();
if (DeleteObject(hollowBrush) == 0)
logLastError(L"error freeing hollow brush");
uninitContainer();
if (DeleteObject(hMessageFont) == 0)
logLastError(L"error deleting control font");
unregisterWindowClass();
// no need to delete the default icon or cursor; see http://stackoverflow.com/questions/30603077/
uninitUtilWindow();
uninitAlloc();
}
void uiFreeInitError(const char *err)
{
if (*(err - 1) == '-')
uiFree((void *) (err - 1));
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
hInstance = hinstDLL;
return TRUE;
}
|