aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_devices/welcomeWidget.cpp
blob: a4ea327b8023057366d5fc2643cf414870f89b71 (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#include "main.h"
#include "./welcomeWidget.h"
#include "../winamp/commandLink.h"

#include <strsafe.h>

#define WELCOMEWIDGET_OFFSET_LEFT_DLU			4
#define WELCOMEWIDGET_OFFSET_TOP_DLU			2
#define WELCOMEWIDGET_OFFSET_RIGHT_DLU			4
#define WELCOMEWIDGET_OFFSET_BOTTOM_DLU			2

#define WELCOMEWIDGET_IMAGE_MIN_WIDTH			280
#define WELCOMEWIDGET_IMAGE_MIN_HEIGHT			(424 - 200)

#define WELCOMEWIDGET_TITLE_MAX_HEIGHT			168
#define WELCOMEWIDGET_TITLE_MIN_WIDTH			WELCOMEWIDGET_IMAGE_MIN_WIDTH

#define WELCOMEWIDGET_TEXT_MAX_HEIGHT			66
#define WELCOMEWIDGET_TEXT_MIN_WIDTH			WELCOMEWIDGET_IMAGE_MIN_WIDTH

#define WELCOMEWIDGET_HELP_LINK					1000

typedef struct WelcomeWidget
{
	HDC context;
	long contextWidth;
	long contextHeight;
	HBITMAP previousBitmap;
	HFONT helpFont;
} WelcomeWidget;

static BOOL
WelcomeWidget_GetBestTextRect(HDC hdc, const wchar_t *text, unsigned int length, 
							  RECT *rect, long maxWidth, unsigned int format)
{
	RECT testRect;
	long right;

	if (NULL == rect)
		return FALSE;

	format |= DT_CALCRECT;

	if (length < 0)
		length = (NULL != text) ? lstrlen(text) : 0;
	
	if (0 == length || NULL == text)
	{
		rect->right = rect->left;
		rect->bottom = rect->top;
		return TRUE;
	}


	SetRect(&testRect, rect->left, rect->top, rect->right, rect->top);
	maxWidth += rect->left;
	right = rect->right;

	for(;;)
	{
		if (FALSE == DrawText(hdc, text, length, &testRect, format))
			return FALSE;

		if (testRect.bottom <= rect->bottom || right >= maxWidth)
		{
			CopyRect(rect, &testRect);
			break;
		}

		long increment = maxWidth - right;
		if (increment > 60)
			increment = increment / 4;
		else if (increment > 4)
			increment = increment / 2;

		right += increment;
		testRect.right = right;
		testRect.bottom = rect->top;
	}

	return TRUE;
}

static COLORREF 
WelcomeWidget_GetTextColor(WidgetStyle *style, COLORREF *shadowColor, BOOL titleMode)
{
	COLORREF frontColor, backColor;
	WORD backHue, backLuma, backSat, frontHue, frontLuma, frontSat;

	backColor = WIDGETSTYLE_BACK_COLOR(style);
	frontColor = WIDGETSTYLE_TEXT_COLOR(style);
	
	ColorRGBToHLS(backColor, &backHue, &backLuma, &backSat);
	ColorRGBToHLS(frontColor, &frontHue, &frontLuma, &frontSat);

	if(frontLuma > backLuma)
	{
		if (NULL != shadowColor)
		{
			if (FALSE != titleMode || backLuma > 180)
				*shadowColor = Graphics_BlendColors(0x000000, backColor, 102);
			else
				*shadowColor = backColor;
		}
		
		return Graphics_BlendColors((FALSE != titleMode) ? 0xBBBBBB : 0xEEEEEE, frontColor, 230);
	}

	if (NULL != shadowColor)
	{
		if (FALSE != titleMode || backLuma < 60)
			*shadowColor = Graphics_BlendColors(0xFFFFFF, backColor, 102);
		else
			*shadowColor = backColor;
	}
	
	return Graphics_BlendColors((FALSE != titleMode) ? 0x333333 : 0x111111, frontColor, 230);
}

static HFONT
WelcomeWidget_CreateTitleFont(WidgetStyle *style, HDC hdc)
{
	LOGFONT lf;
	HFONT textFont;
	long height, pixelsY;

	pixelsY = GetDeviceCaps(hdc, LOGPIXELSY);
	height = MulDiv(16, pixelsY, 96);

	textFont = WIDGETSTYLE_TITLE_FONT(style);
	if (NULL != textFont)
	{
		HFONT prevFont;
		TEXTMETRIC tm;

		prevFont = SelectFont(hdc, textFont);
		if (FALSE != GetTextMetricsW(hdc, &tm))
		{
			long test = MulDiv(tm.tmHeight - tm.tmInternalLeading, 96, pixelsY);
			test = MulDiv((test + 2), pixelsY, 72);
			if (test > height)
				height = test;
		}
		SelectFont(hdc, prevFont);
	}

	lf.lfHeight = -height;
	lf.lfWidth = 0;
	lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfWeight = FW_DONTCARE;
	lf.lfItalic = FALSE;
	lf.lfUnderline = FALSE;
	lf.lfStrikeOut = FALSE;
	lf.lfCharSet = DEFAULT_CHARSET;
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	lf.lfQuality = Graphics_GetSysFontQuality();
	lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
	StringCchCopy(lf.lfFaceName, ARRAYSIZE(lf.lfFaceName), L"Arial");

	return CreateFontIndirect(&lf);
}

static BOOL
WelcomeWidget_FillContext(HDC targetDC, long width, long height, HDC sourceDC, 
							  HBITMAP backBitmap, long backWidth, long backHeight, WidgetStyle *style)
{
	RECT rect, partRect;
	wchar_t buffer[4096] = {0};
	int bufferLength;
	COLORREF textColor, shadowColor;
	HFONT prevFont;

	if (NULL == targetDC || 
		NULL == sourceDC || 
		NULL == style)
	{
		return FALSE;
	}

	SetRect(&rect, 0, 0, width, height);

	FillRect(targetDC, &rect, WIDGETSTYLE_BACK_BRUSH(style));
	

	if (NULL != backBitmap)
	{
		Image_AlphaBlend(targetDC, &rect, sourceDC, NULL, 255, backBitmap, NULL, 
			AlphaBlend_AlignCenter | AlphaBlend_AlignVCenter, NULL);
	}

	SetBkMode(targetDC, TRANSPARENT);
	
	
	WASABI_API_LNGSTRINGW_BUF(IDS_WELCOMEWIDGET_TITLE, buffer, ARRAYSIZE(buffer));
	if (L'\0' != buffer[0])
	{
		BOOL textMode;

		textMode = TRUE;

		if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, buffer, -1, L"WINAMP DEVICE MANAGER", -1))
		{
			HBITMAP titleBitmap;
			titleBitmap = Image_LoadEx(Plugin_GetInstance(), MAKEINTRESOURCE(IDR_WELCOME_WIDGET_TITLE_IMAGE), 
										SRC_TYPE_PNG, ISF_PREMULTIPLY, 0, 0);
			if (NULL != titleBitmap)
			{
				SetRect(&partRect, rect.left, rect.top, rect.right, rect.top + WELCOMEWIDGET_TITLE_MAX_HEIGHT);

				if (FALSE != Image_AlphaBlend(targetDC, &partRect, sourceDC, NULL, 255, titleBitmap, NULL, 
									AlphaBlend_AlignCenter | AlphaBlend_AlignVCenter, NULL))
				{
					textMode = FALSE;
				}

				DeleteObject(titleBitmap);
			}
		}

		if (FALSE != textMode)
		{
			HFONT titleFont;

			bufferLength = lstrlen(buffer);
			
			titleFont = WelcomeWidget_CreateTitleFont(style, targetDC);

			textColor = WelcomeWidget_GetTextColor(style, &shadowColor, TRUE);
			prevFont = SelectFont(targetDC, (NULL != titleFont) ? titleFont : WIDGETSTYLE_TITLE_FONT(style));

			SetRect(&partRect, 0, 0, width, 0);

			if (FALSE != DrawText(targetDC, buffer, bufferLength, &partRect, 
						DT_CALCRECT | DT_LEFT | DT_TOP | DT_WORDBREAK | DT_NOPREFIX | DT_EDITCONTROL | DT_END_ELLIPSIS))
			{
				if (partRect.bottom > WELCOMEWIDGET_TITLE_MAX_HEIGHT)
				{
					TEXTMETRIC textMetrics;
					if (FALSE != GetTextMetrics(targetDC, &textMetrics))
						partRect.bottom = (WELCOMEWIDGET_TITLE_MAX_HEIGHT/textMetrics.tmHeight)*textMetrics.tmHeight;
				}

				OffsetRect(&partRect, 
							rect.left + (RECTWIDTH(rect) - RECTWIDTH(partRect))/2, 
							rect.top  + (WELCOMEWIDGET_TITLE_MAX_HEIGHT - RECTHEIGHT(partRect))/2);
		
				OffsetRect(&partRect, 0, 2);
				SetTextColor(targetDC, shadowColor);
				DrawText(targetDC, buffer, bufferLength, &partRect, 
						DT_CENTER | DT_TOP | DT_WORDBREAK | DT_NOPREFIX | DT_EDITCONTROL | DT_END_ELLIPSIS);

				OffsetRect(&partRect, 0, -2);
				
				SetTextColor(targetDC, textColor);
				DrawText(targetDC, buffer, bufferLength, &partRect, 
						DT_CENTER | DT_TOP | DT_WORDBREAK | DT_NOPREFIX | DT_EDITCONTROL | DT_END_ELLIPSIS);
			}

			SelectFont(targetDC, prevFont);
			if (NULL != titleFont)
				DeleteObject(titleFont);
		}
	}

	WASABI_API_LNGSTRINGW_BUF(IDS_WELCOMEWIDGET_TEXT, buffer, ARRAYSIZE(buffer));
	if (L'\0' != buffer[0])
	{
		bufferLength = lstrlen(buffer);

		textColor = WelcomeWidget_GetTextColor(style, &shadowColor, FALSE);
		prevFont = SelectFont(targetDC, WIDGETSTYLE_TEXT_FONT(style));

		SetRect(&partRect, 0, 0, WELCOMEWIDGET_TEXT_MIN_WIDTH, WELCOMEWIDGET_TEXT_MAX_HEIGHT);

		if (FALSE != WelcomeWidget_GetBestTextRect(targetDC, buffer, bufferLength, &partRect, width, 
					DT_LEFT | DT_TOP | DT_WORDBREAK | DT_NOPREFIX | DT_EDITCONTROL | DT_END_ELLIPSIS))
		{
			if (partRect.bottom > WELCOMEWIDGET_TEXT_MAX_HEIGHT)
			{
				TEXTMETRIC textMetrics;
				if (FALSE != GetTextMetrics(targetDC, &textMetrics))
					partRect.bottom = (WELCOMEWIDGET_TEXT_MAX_HEIGHT/textMetrics.tmHeight)*textMetrics.tmHeight;
			}

			OffsetRect(&partRect, 
						rect.left + (RECTWIDTH(rect) - RECTWIDTH(partRect))/2, 
						rect.bottom - WELCOMEWIDGET_TEXT_MAX_HEIGHT);
	
			OffsetRect(&partRect, 0, 1);
			SetTextColor(targetDC, shadowColor);
			DrawText(targetDC, buffer, bufferLength, &partRect, 
					DT_LEFT | DT_TOP | DT_WORDBREAK | DT_NOPREFIX | DT_EDITCONTROL | DT_END_ELLIPSIS);

			OffsetRect(&partRect, 0, -1);
			
			SetTextColor(targetDC, textColor);
			DrawText(targetDC, buffer, bufferLength, &partRect, 
					DT_LEFT | DT_TOP | DT_WORDBREAK | DT_NOPREFIX | DT_EDITCONTROL | DT_END_ELLIPSIS);
		}

		SelectFont(targetDC, prevFont);
	}

	return TRUE;
}

static HDC
WelcomeWidget_CreateContext(HWND hwnd, WidgetStyle *style, HBITMAP *prevBitmapOut, long *widthOut, long *heightOut)
{
	
	HDC windowDC, targetDC, sourceDC;
	HBITMAP backBitmap, targetBitmap;
	HBITMAP prevTargetBitmap;
	long width, height, backWidth, backHeight;

	if (NULL == prevBitmapOut || NULL == style)
		return NULL;

	windowDC = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
	if (NULL == windowDC)
		return NULL;

	backBitmap = Image_LoadEx(Plugin_GetInstance(), MAKEINTRESOURCE(IDR_WELCOME_WIDGET_IMAGE), 
							SRC_TYPE_PNG, ISF_PREMULTIPLY, 0, 0);

	width = WELCOMEWIDGET_IMAGE_MIN_WIDTH;
	height = WELCOMEWIDGET_IMAGE_MIN_HEIGHT;
	backWidth = 0;
	backHeight = 0;

	if (NULL != backBitmap)
	{
		BITMAP bi;
		if (sizeof(bi) == GetObject(backBitmap, sizeof(bi), &bi))
		{
			backWidth = bi.bmWidth;
			if (backWidth > width)
				width = backWidth;

			backHeight = ABS(bi.bmHeight);
			if (backHeight > height)
				height = backHeight;
		}
	}

	targetDC = CreateCompatibleDC(windowDC);
	sourceDC = CreateCompatibleDC(windowDC);
	targetBitmap = CreateCompatibleBitmap(windowDC, width, height);
	prevTargetBitmap = NULL;

	ReleaseDC(hwnd, windowDC);

	if (NULL != targetDC && 
		NULL != sourceDC && 
		NULL != targetBitmap)
	{

		HFONT prevTargetFont;
		prevTargetBitmap = SelectBitmap(targetDC, targetBitmap);
		prevTargetFont = GetCurrentFont(targetDC); 
		HBITMAP prevSourceBitmap = GetCurrentBitmap(sourceDC);

		if (FALSE == WelcomeWidget_FillContext(targetDC, width, height, sourceDC, 
											backBitmap, backWidth, backHeight, style))
		{
			SelectBitmap(targetDC, prevTargetBitmap);
			prevTargetBitmap = NULL;
			DeleteObject(targetBitmap);
			targetBitmap = NULL;
		}
		
		SelectFont(targetDC, prevTargetFont);
		SelectBitmap(sourceDC, prevSourceBitmap);
	}

	if (NULL != sourceDC)
		DeleteDC(sourceDC);

	if (NULL != targetDC && NULL == targetBitmap)
	{
		DeleteDC(targetDC);
		targetDC = NULL;
		prevTargetBitmap = NULL;
	}

	if (NULL != backBitmap)
		DeleteObject(backBitmap);

	*prevBitmapOut = prevTargetBitmap;
	if (NULL != widthOut)
		*widthOut = width;
	if (NULL != heightOut)
		*heightOut = height;

	return targetDC;
}

static void
WelcomeWidget_ResetContext(WelcomeWidget * self)
{
	if (NULL == self ||
		NULL == self->context)
	{
		return;
	}

	if (NULL != self->previousBitmap)
	{
		HBITMAP bitmap = SelectBitmap(self->context, self->previousBitmap);
		if (NULL != bitmap)
			DeleteObject(bitmap);
	}

	DeleteDC(self->context);
	self->context = NULL;
	self->previousBitmap = NULL;
	self->contextWidth = 0;
	self->contextHeight = 0;
}

static BOOL
WelcomeWidget_GetViewOrigin(HWND hwnd, POINT *pt)
{
	SCROLLINFO scrollInfo;

	if (NULL == pt)
		return FALSE;

	scrollInfo.cbSize = sizeof(scrollInfo);
	scrollInfo.fMask = SIF_POS;
		
	if (FALSE == GetScrollInfo(hwnd, SB_HORZ, &scrollInfo))
		return FALSE;
	pt->x = -scrollInfo.nPos;

	if (FALSE == GetScrollInfo(hwnd, SB_VERT, &scrollInfo))
		return FALSE;
	pt->y = -scrollInfo.nPos;

	return TRUE;
}

static BOOL
WelcomeWidget_UpdateHelpPosition(WelcomeWidget *self, HWND hwnd, const RECT *clientRect, BOOL redraw)
{
	HWND elementWindow;
	RECT elementRect;
	unsigned int positionFlags;
	long left, top;
	POINT origin;

	if (NULL == self || NULL == hwnd || NULL == clientRect)
		return FALSE;

	elementWindow = GetDlgItem(hwnd, WELCOMEWIDGET_HELP_LINK);
	if (NULL == elementWindow)
		return FALSE;
	

	if (FALSE == WelcomeWidget_GetViewOrigin(hwnd, &origin))
	{
		origin.x = 0;
		origin.y = 0;
	}

	if (FALSE == GetWindowRect(elementWindow, &elementRect))
		return FALSE;

	positionFlags = SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE;
	if (FALSE == redraw)
		positionFlags |= SWP_NOREDRAW;
		
	left = clientRect->right;
	left -= (RECTWIDTH(elementRect) + 2);
	if (left < clientRect->left)
		left = clientRect->left;

	top = clientRect->top;
	top += 2;

	return SetWindowPos(elementWindow, NULL, left, origin.y + top, 0, 0, positionFlags);
}

static BOOL
WelcomeWidget_EnsureHelpVisible(WelcomeWidget *self, HWND hwnd)
{
	HWND elementWindow;
	POINT origin;

	if (NULL == self || NULL == hwnd)
		return FALSE;

	elementWindow = GetDlgItem(hwnd, WELCOMEWIDGET_HELP_LINK);
	if (NULL == elementWindow)
		return FALSE;
	
	if (FALSE != WelcomeWidget_GetViewOrigin(hwnd, &origin) && 
		0 != origin.y)
	{
		return WIDGET_SCROLL(hwnd, 0, origin.y, TRUE);
	}

	return FALSE;
}

static BOOL
WelcomeWidget_GetHelpUrl(wchar_t *buffer, size_t bufferMax)
{
	if (NULL == buffer)
		return FALSE;

	WASABI_API_LNGSTRINGW_BUF(IDS_WELCOMEWIDGET_HELP_URL, buffer, bufferMax);
	return (L'\0' != buffer[0]);
}

static BOOL 
WelcomeWidget_InitCb(HWND hwnd, void **object, void *param)
{
	wchar_t buffer[64] = {0};
	WelcomeWidget *self = (WelcomeWidget*)malloc(sizeof(WelcomeWidget));

	if (NULL == self)
		return FALSE;

	ZeroMemory(self, sizeof(WelcomeWidget));
	WIDGET_ENABLE_CHILDREN_SCROLL(hwnd, FALSE);


	WASABI_API_LNGSTRINGW_BUF(IDS_HELP_LINK, buffer, ARRAYSIZE(buffer));
	CommandLink_CreateWindow(WS_EX_NOPARENTNOTIFY, 
							buffer, 
							WS_CHILD | WS_VISIBLE | WS_TABSTOP |
							/*CLS_ALWAYSUNDERLINE |*/ CLS_HOTTRACK | CLS_HIGHLIGHTCOLOR,
							0, 0, 0, 0, hwnd, WELCOMEWIDGET_HELP_LINK);

	*object = self;

	return TRUE;
}

static void 
WelcomeWidget_DestroyCb(WelcomeWidget *self, HWND hwnd)
{
	if (NULL == self)
		return;

	WelcomeWidget_ResetContext(self);
	if (NULL != self->helpFont)
		DeleteObject(self->helpFont);

	free(self);
}


static void
WelcomeWidget_LayoutCb(WelcomeWidget *self, HWND hwnd, WidgetStyle *style, 
					 const RECT *clientRect, SIZE *viewSize, BOOL redraw)
{
	if (NULL == self || NULL == style)
		return;

	if (NULL == self->context)
	{
		self->context = WelcomeWidget_CreateContext(hwnd, style, &self->previousBitmap, &self->contextWidth, &self->contextHeight);
		if (NULL == self->context)
			return;
	}
	
	viewSize->cx = self->contextWidth + 
					WIDGETSTYLE_DLU_TO_HORZ_PX(style, WELCOMEWIDGET_OFFSET_LEFT_DLU);
	
	viewSize->cy = self->contextHeight + 
					WIDGETSTYLE_DLU_TO_VERT_PX(style, WELCOMEWIDGET_OFFSET_TOP_DLU);
	

	WelcomeWidget_UpdateHelpPosition(self, hwnd, clientRect, redraw);
}


static BOOL 
WelcomeWidget_PaintCb(WelcomeWidget *self, HWND hwnd, WidgetStyle *style, HDC hdc, const RECT *paintRect, BOOL erase)
{
	FillRegion fillRegion;

	FillRegion_Init(&fillRegion, paintRect);
	
	if (NULL != self->context)
	{
		RECT partRect, rect;
		long offsetX, offsetY;

		GetClientRect(hwnd, &rect);

		SetRect(&partRect, 0, 0, self->contextWidth, self->contextHeight);
	
		offsetX = rect.left + WIDGETSTYLE_DLU_TO_HORZ_PX(style, WELCOMEWIDGET_OFFSET_LEFT_DLU);
		offsetY = WIDGETSTYLE_DLU_TO_HORZ_PX(style, WELCOMEWIDGET_OFFSET_RIGHT_DLU);
		if ((offsetX + partRect.right + offsetY) < rect.right)
			offsetX += (rect.right - (offsetX + partRect.right + offsetY))/2;

		offsetY = rect.top + WIDGETSTYLE_DLU_TO_VERT_PX(style, WELCOMEWIDGET_OFFSET_TOP_DLU);

		OffsetRect(&partRect, offsetX, offsetY);
		
		if (FALSE != IntersectRect(&rect, &partRect, paintRect) && 
			FALSE != BitBlt(hdc, rect.left, rect.top, RECTWIDTH(rect), RECTHEIGHT(rect), 
							self->context, rect.left - partRect.left, rect.top - partRect.top, SRCCOPY))
		{
			FillRegion_ExcludeRect(&fillRegion, &rect);
		}

	}


	if (FALSE != erase)
		FillRegion_BrushFill(&fillRegion, hdc, WIDGETSTYLE_BACK_BRUSH(style));

	FillRegion_Uninit(&fillRegion);

	return TRUE;
}

static void
WelcomeWidget_StyleColorChangedCb(WelcomeWidget *self, HWND hwnd, WidgetStyle *style)
{
	HWND elementWindow;

	if (NULL == self)
		return;

	WelcomeWidget_ResetContext(self);
	self->context = WelcomeWidget_CreateContext(hwnd, style, &self->previousBitmap, &self->contextWidth, &self->contextHeight);

	elementWindow = GetDlgItem(hwnd, WELCOMEWIDGET_HELP_LINK);
	if (NULL != elementWindow)
	{
		CommandLink_SetBackColor(elementWindow, WIDGETSTYLE_BACK_COLOR(style));
		CommandLink_SetTextColor(elementWindow, WelcomeWidget_GetTextColor(style, NULL, TRUE));
		CommandLink_SetHighlightColor(elementWindow, WelcomeWidget_GetTextColor(style, NULL, FALSE));
	}
}

static void
WelcomeWidget_StyleFontChangedCb(WelcomeWidget *self, HWND hwnd, WidgetStyle *style)
{
	HWND elementWindow;

	if (NULL == self)
		return;

	WelcomeWidget_ResetContext(self);
	self->context = WelcomeWidget_CreateContext(hwnd, style, &self->previousBitmap, &self->contextWidth, &self->contextHeight);

	elementWindow = GetDlgItem(hwnd, WELCOMEWIDGET_HELP_LINK);
	if (NULL != elementWindow)
	{
		SIZE elementSize;

		if (NULL != self->helpFont)
			DeleteObject(self->helpFont);

		self->helpFont = Graphics_DuplicateFont(WIDGETSTYLE_TEXT_FONT(style), 0, TRUE, TRUE);

		SendMessage(elementWindow, WM_SETFONT, (WPARAM)self->helpFont, 0L);
		if (FALSE != CommandLink_GetIdealSize(elementWindow, &elementSize))
		{
			SetWindowPos(elementWindow, NULL, 0, 0, elementSize.cx, elementSize.cy, 
				SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
		}
	}
}

static void
WelcomeWidget_ScrollCb(WelcomeWidget *self, HWND hwnd, int *dx, int *dy)
{	
	RECT clientRect;
	if (FALSE != GetClientRect(hwnd, &clientRect))
		WelcomeWidget_UpdateHelpPosition(self, hwnd, &clientRect, TRUE);
	
}

static void 
WelcomeWidget_HelpLinkCb(WelcomeWidget *self, HWND hwnd, NMHDR *pnmh, LRESULT *result)
{
	switch(pnmh->code)
	{
		case NM_CLICK:
			{
				wchar_t buffer[4096] = {0};
				if (FALSE != WelcomeWidget_GetHelpUrl(buffer, ARRAYSIZE(buffer)))
					MediaLibrary_ShowHelp(Plugin_GetLibraryWindow(), buffer);
			}
			break;
		case NM_SETFOCUS:
			WelcomeWidget_EnsureHelpVisible(self, hwnd);
			break;

	}
}

static BOOL
WelcomeWidget_NotifyCb(WelcomeWidget *self, HWND hwnd, NMHDR *pnmh, LRESULT *result)
{
	switch(pnmh->idFrom)
	{
		case WELCOMEWIDGET_HELP_LINK:
			WelcomeWidget_HelpLinkCb(self, hwnd, pnmh, result);
			return TRUE;
	}

	return FALSE;
}

static BOOL
WelcomeWidget_HelpCb(WelcomeWidget *self, HWND hwnd, wchar_t *buffer, size_t bufferMax)
{
	return WelcomeWidget_GetHelpUrl(buffer, bufferMax);
}

HWND WelcomeWidget_CreateWindow(HWND parentWindow, int x, int y, int width, int height, BOOL border, unsigned int controlId)
{
	const static WidgetInterface welcomeWidgetInterface =
	{
		(WidgetInitCallback)WelcomeWidget_InitCb,
		(WidgetDestroyCallback)WelcomeWidget_DestroyCb,
		(WidgetLayoutCallback)WelcomeWidget_LayoutCb,
		(WidgetPaintCallback)WelcomeWidget_PaintCb,
		(WidgetStyleCallback)WelcomeWidget_StyleColorChangedCb,
		(WidgetStyleCallback)WelcomeWidget_StyleFontChangedCb,
		NULL /*mouseMove*/,
		NULL /*leftButtonDown*/,
		NULL /*leftButtonUp*/,
		NULL /*leftButtonDblClk*/,
		NULL /*rightButtonDown*/,
		NULL /*rightButtonUp*/,
		NULL /*keyDown*/,
		NULL /*keyUp*/,
		NULL /*character*/,
		NULL /*inputRequest*/,
		NULL /*focusChanged*/,
		NULL /*contextMenu*/,
		NULL /*zoomChanging*/,
		NULL /*scrollBefore*/,
		(WidgetScrollCallback)WelcomeWidget_ScrollCb,
		(WidgetNotifyCallback)WelcomeWidget_NotifyCb,
		(WidgetHelpCallback)WelcomeWidget_HelpCb,

	};

	return Widget_CreateWindow(WIDGET_TYPE_WELCOME,
								&welcomeWidgetInterface, 
								NULL, 
								((FALSE != border) ? WS_EX_CLIENTEDGE : 0) | WS_EX_CONTROLPARENT, 
								WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
								x, y, width, height, 
								parentWindow, 
								controlId, 0L);
}