aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_devices/widgetHost.cpp
blob: 0b8e2ae9789f7f6c44cb0c5e780d508d150231d3 (plain) (blame)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "main.h"
#include "./widgetHost.h"


#define WIDGETHOST_WINDOW_CLASS		L"NullsoftDevicesWidgetHost"
#define WIDGETHOST_WIDGET_ID		1000

typedef 
enum WidgetHostState
{
	WIDGETHOST_STATE_FROZEN_UI = (1 << 0),
} WidgetHostState;
DEFINE_ENUM_FLAG_OPERATORS(WidgetHostState);

typedef struct WidgetHost
{
	WidgetHostState state;
	WidgetStyle widgetStyle;
	HFONT font;
	HRGN updateRegion;
	POINT updateOffset;
} WidgetHost;

typedef struct WidgetHostCreateParam
{
	WidgetCreateProc widgetCreate;
	void *widgetCreateParam;
} WidgetHostCreateParam;

#define WIDGETHOST(_hwnd) ((WidgetHost*)(LONGX86)GetWindowLongPtrW((_hwnd), 0))
#define WIDGETHOST_RET_VOID(_self, _hwnd) {(_self) = WIDGETHOST((_hwnd)); if (NULL == (_self)) return;}
#define WIDGETHOST_RET_VAL(_self, _hwnd, _error) {(_self) = WIDGETHOST((_hwnd)); if (NULL == (_self)) return (_error);}

#define WIDGETHOST_WIDGET(_hostWindow)	(GetDlgItem((_hostWindow), WIDGETHOST_WIDGET_ID))

#define WIDGETHOST_IS_FROZEN(_self) (0 != (WIDGETHOST_STATE_FROZEN_UI & (_self)->state))
#define WIDGETHOST_FREEZE(_self) (((_self)->state) |= WIDGETHOST_STATE_FROZEN_UI)
#define WIDGETHOST_THAW(_self) (((_self)->state) &= ~WIDGETHOST_STATE_FROZEN_UI)


static LRESULT CALLBACK 
WidgetHost_WindowProc(HWND hwnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam);

static ATOM 
WidgetHost_GetClassAtom(HINSTANCE instance)
{
	WNDCLASSEXW klass;
	ATOM klassAtom;

	klassAtom = (ATOM)GetClassInfoExW(instance, WIDGETHOST_WINDOW_CLASS, &klass);
	if (0 != klassAtom)
		return klassAtom;

	memset(&klass, 0, sizeof(klass));
	klass.cbSize = sizeof(klass);
	klass.style = CS_DBLCLKS;
	klass.lpfnWndProc = WidgetHost_WindowProc;
	klass.cbClsExtra = 0;
	klass.cbWndExtra = sizeof(WidgetHost*);
	klass.hInstance = instance;
	klass.hIcon = NULL;
	klass.hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW);
	klass.hbrBackground = NULL;
	klass.lpszMenuName = NULL;
	klass.lpszClassName = WIDGETHOST_WINDOW_CLASS;
	klass.hIconSm = NULL;
	klassAtom = RegisterClassExW(&klass);
	
	return klassAtom;
}


HWND 
WidgetHost_Create(unsigned int windowStyle, int x, int y, int width, int height, 
				  HWND parentWindow, WidgetCreateProc createProc, void *createParam)
{
	HINSTANCE instance;
	ATOM klassAtom;
	HWND hwnd;
	WidgetHostCreateParam hostParam;
		

	if (NULL == createProc)
		return NULL;

	instance = GetModuleHandleW(NULL);
	klassAtom = WidgetHost_GetClassAtom(instance);
	if (0 == klassAtom)
		return NULL;
	
	hostParam.widgetCreate = createProc;
	hostParam.widgetCreateParam = createParam;
	

	hwnd = CreateWindowExW(WS_EX_NOPARENTNOTIFY |WS_EX_CONTROLPARENT, 
							(LPCWSTR)MAKEINTATOM(klassAtom), NULL,
							WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | windowStyle,
							x, y, width, height, 
							parentWindow, NULL, instance, &hostParam);

	return hwnd;
}

static void 
WidgetHost_Layout(HWND hwnd, BOOL redraw)
{
	WidgetHost *self;
	RECT rect;
	WIDGETHOST_RET_VOID(self, hwnd);

	if (FALSE == GetClientRect(hwnd, &rect))
		return;

	HWND widgetWindow = WIDGETHOST_WIDGET(hwnd);
	if (NULL != widgetWindow)
	{
		unsigned int flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER;
		if (FALSE == redraw)
			flags |= SWP_NOREDRAW;

		SetWindowPos(widgetWindow, NULL, 0, 0, RECTWIDTH(rect), RECTHEIGHT(rect), flags);
	}
}

static void
WidgetHost_Paint(HWND hwnd, HDC hdc, const RECT *paintRect, BOOL erase)
{
	if (FALSE != erase)
	{
		COLORREF backColor, prevBackColor;
		backColor = Graphics_GetSkinColor(WADLG_WNDBG);
		prevBackColor = SetBkColor(hdc, backColor);

		ExtTextOut(hdc, 0, 0, ETO_OPAQUE, paintRect, NULL, 0, NULL);
		SetBkColor(hdc, prevBackColor);
	}
}

static void
WidgetHost_UpdateSkin(HWND hwnd)
{
	WidgetHost *self;
	WIDGETHOST_RET_VOID(self, hwnd);
	BOOL styleChanged = FALSE;

	if (FALSE != WidgetStyle_UpdateDefaultColors(&self->widgetStyle))
		 styleChanged = TRUE;

	if (FALSE != styleChanged)
	{
		HWND widgetWindow = WIDGETHOST_WIDGET(hwnd);
		if (NULL != widgetWindow)
		{
			WIDGET_STYLE_COLOR_CHANGED(widgetWindow);
			InvalidateRect(widgetWindow, NULL, TRUE);
		}
	}

}

static void
WidgetHost_UpdateFont(HWND hwnd, BOOL redraw)
{
	WidgetHost *self;
	BOOL styleChanged = FALSE;
	long unitWidth, unitHeight;

	WIDGETHOST_RET_VOID(self, hwnd);

	if (FALSE == Graphics_GetWindowBaseUnits(hwnd, &unitWidth, &unitHeight))
	{
		unitWidth = 6;
		unitHeight = 13;
	}

	if (FALSE != WidgetStyle_UpdateDefaultFonts(&self->widgetStyle, self->font, unitWidth, unitHeight))
		styleChanged = TRUE;

	if (FALSE != styleChanged)
	{
		HWND widgetWindow = WIDGETHOST_WIDGET(hwnd);
		if (NULL != widgetWindow)
		{
			WIDGET_STYLE_COLOR_CHANGED(widgetWindow);
			InvalidateRect(widgetWindow, NULL, TRUE);
		}
	}

}

static LRESULT
WidgetHost_OnCreate(HWND hwnd, CREATESTRUCT *createStruct)
{	
	WidgetHost *self;
	HWND widgetWindow;
	WidgetHostCreateParam *createParam;
	
	if (NULL == createStruct)
		return -1;

	createParam = (WidgetHostCreateParam*)createStruct->lpCreateParams;
	if (NULL == createParam)
		return -1;

	self = (WidgetHost*)malloc(sizeof(WidgetHost));
	if (NULL == self)
		return -1;

	SetLastError(ERROR_SUCCESS);
	if (!SetWindowLongPtr(hwnd, 0, (LONGX86)self) && ERROR_SUCCESS != GetLastError())
		return -1;

	memset(self, 0, sizeof(WidgetHost));

	WIDGETHOST_FREEZE(self);

	MLSkinWindow2(Plugin_GetLibraryWindow(), hwnd, SKINNEDWND_TYPE_WINDOW, 
					SWS_USESKINFONT | SWS_USESKINCOLORS | SWS_USESKINCURSORS);

	WidgetHost_UpdateFont(hwnd, FALSE);
	WidgetHost_UpdateSkin(hwnd);

	widgetWindow = NULL;
	if (NULL != createParam->widgetCreate)
		widgetWindow = createParam->widgetCreate(hwnd, createParam->widgetCreateParam);

	if (NULL == widgetWindow)
		return  -1;

	SetWindowLongPtrW(widgetWindow, GWLP_ID, WIDGETHOST_WIDGET_ID);

	WIDGET_SET_STYLE(widgetWindow, &self->widgetStyle);

	SetWindowPos(widgetWindow, NULL, 0, 0, 0, 0, 
		SWP_NOSIZE  | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW | SWP_FRAMECHANGED);
	
	ShowWindow(widgetWindow, SW_SHOWNA);

	SetWindowPos(widgetWindow, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
	
	WIDGETHOST_THAW(self);

	SetWindowPos(hwnd, NULL, 0, 0, 0, 0, 
		SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED);

	return 0;
}

static void
WidgetHost_OnDestroy(HWND hwnd)
{
	WidgetHost *self;

	self = WIDGETHOST(hwnd);
	SetWindowLongPtr(hwnd, 0, 0);
	
	if (NULL == self)
		return;

	WIDGETHOST_FREEZE(self);

	WidgetStyle_Free(&self->widgetStyle);
	free(self);
}

static void
WidgetHost_OnPaint(HWND hwnd)
{
	PAINTSTRUCT ps;
	
	if (NULL != BeginPaint(hwnd, &ps))
	{		
		WidgetHost_Paint(hwnd, ps.hdc, &ps.rcPaint, ps.fErase);
		EndPaint(hwnd, &ps);
	}
}

static void 
WidgetHost_OnPrintClient(HWND hwnd, HDC hdc, UINT options)
{	
	RECT clientRect;
	if (GetClientRect(hwnd, &clientRect))
	{
		WidgetHost_Paint(hwnd, hdc, &clientRect, TRUE);
	}
}

static void
WidgetHost_OnWindowPosChanged(HWND hwnd, WINDOWPOS *windowPos)
{
	if ((SWP_NOSIZE | SWP_NOMOVE) != ((SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED) & windowPos->flags))
	{
		WidgetHost *self;
		WIDGETHOST_RET_VOID(self, hwnd);

		if (FALSE != WIDGETHOST_IS_FROZEN(self))
			return;

		WidgetHost_Layout(hwnd, 0 == (SWP_NOREDRAW & windowPos->flags));
	}
}

static void
WidgetHost_OnDisplayChanged(HWND hwnd, INT bpp, INT dpi_x, INT dpi_y)
{
	WidgetHost *self;
	WIDGETHOST_RET_VOID(self, hwnd);

	if (FALSE != WIDGETHOST_IS_FROZEN(self))
		return;
	
	WidgetHost_UpdateSkin(hwnd);
	InvalidateRect(hwnd, NULL, TRUE);
}

static void
WidgetHost_OnSetFont(HWND hwnd, HFONT font, BOOL redraw)
{
	WidgetHost *self;
	LOGFONTW prevFont, newFont;
	
	WIDGETHOST_RET_VOID(self, hwnd);
	
	if (NULL == self->font || 
		sizeof(LOGFONTW) != GetObjectW(self->font, sizeof(prevFont), &prevFont))
	{
		ZeroMemory(&prevFont, sizeof(prevFont));
	}

	self->font = font;


	if (NULL == self->font || 
		sizeof(newFont) != GetObjectW(self->font, sizeof(newFont), &newFont))
	{
		ZeroMemory(&newFont, sizeof(newFont));
	}

	if (0 != memcmp(&prevFont, &newFont, sizeof(prevFont)) &&
		FALSE == WIDGETHOST_IS_FROZEN(self))
	{
		WidgetHost_UpdateFont(hwnd, redraw);
	}
}

static HFONT
WidgetHost_OnGetFont(HWND hwnd)
{
	WidgetHost *self;
	WIDGETHOST_RET_VAL(self, hwnd, NULL);
	
	return self->font;
}

static void
WidgetHost_OnSetUpdateRegion(HWND hwnd, HRGN updateRegion, POINTS regionOffset)
{
	WidgetHost *self;
	WIDGETHOST_RET_VOID(self, hwnd);

	self->updateRegion = updateRegion;
	self->updateOffset.x = regionOffset.x;
	self->updateOffset.y = regionOffset.y;
}


static LRESULT CALLBACK 
WidgetHost_WindowProc(HWND hwnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
		case WM_CREATE:				return WidgetHost_OnCreate(hwnd, (CREATESTRUCT*)lParam);
		case WM_DESTROY:			WidgetHost_OnDestroy(hwnd); return 0;
		case WM_PAINT:				WidgetHost_OnPaint(hwnd); return 0;
		case WM_PRINTCLIENT:		WidgetHost_OnPrintClient(hwnd, (HDC)wParam, (UINT)lParam); return 0;
		case WM_PRINT:				return 0;
		case WM_ERASEBKGND:			return 0;
		case WM_WINDOWPOSCHANGED:	WidgetHost_OnWindowPosChanged(hwnd, (WINDOWPOS*)lParam); return 0;
		case WM_SIZE:				return 0;
		case WM_MOVE:				return 0;
		case WM_DISPLAYCHANGE:		WidgetHost_OnDisplayChanged(hwnd, (INT)wParam, LOWORD(lParam), HIWORD(lParam)); return 0;
		case WM_SETFONT:			WidgetHost_OnSetFont(hwnd, (HFONT)wParam, LOWORD(lParam)); return 0;
		case WM_GETFONT:			return (LRESULT)WidgetHost_OnGetFont(hwnd);

		// gen_ml flickerless drawing
		case WM_USER + 0x200:		return 1;
		case WM_USER + 0x201:		WidgetHost_OnSetUpdateRegion(hwnd, (HRGN)lParam, MAKEPOINTS(wParam)); return 0;

	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}