aboutsummaryrefslogtreecommitdiff
path: root/Src/omBrowser/toolbarAddress.cpp
blob: 4f3bc35b3dad252e3b8ff5d3012b2a512a7f213d (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
761
#include "main.h"
#include "./toolbarAddress.h"
#include "./toolbar.h"
#include "./toolbarEditbox.h"
#include "./resource.h"
#include "./graphics.h"
#include "./menu.h"
#include "./ifc_imageloader.h"
#include "./addressEncoder.h"


#include <wininet.h>
//#include <shlwapi.h>
#include <strsafe.h>

#define ADDRESSBAR_MINWIDTH		96

	UINT minWidth;
	HBITMAP bitmap;
	INT iconHeight;
	INT iconWidth;
	INT textHeight;
	INT textMargin;
	COLORREF rgbText;
	COLORREF rgbBk;
	HWND hEditor;

ToolbarAddress::ToolbarAddress(LPCSTR pszName, UINT nStyle) :
	ToolbarItem(pszName, nStyle, ICON_NONE, NULL, NULL), 
	minWidth(ADDRESSBAR_MINWIDTH), bitmap(NULL), iconHeight(0), iconWidth(0),
	textHeight(12), textMargin(0), rgbText(0x000000), rgbBk(0xFFFFFF), hEditor(NULL)
{
}

ToolbarAddress::~ToolbarAddress()
{
	if (NULL != bitmap)
		DeleteObject(bitmap);
	if (NULL != hEditor)
		DestroyWindow(hEditor);
}

ToolbarItem* CALLBACK ToolbarAddress::CreateInstance(ToolbarItem::Template *item)
{
	if (NULL == item) 
		return NULL;

	return new ToolbarAddress( (NULL != item->name) ? item->name : TOOLCLS_ADDRESSBAR,
								item->style);
}

BOOL ToolbarAddress::ActivateEditor(HWND hToolbar, const POINT *ppt)
{
	if (NULL == hEditor)
	{
		UINT editorStyle = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_TABSTOP |
							ES_LEFT | ES_NOHIDESEL | ES_AUTOHSCROLL;
		
		if (0 != (styleAddressReadonly & style))
			editorStyle |= ES_READONLY;
	
		hEditor = CreateWindowEx(WS_EX_NOPARENTNOTIFY, L"Edit", text, editorStyle, 0, 0, 0, 0, hToolbar, NULL, NULL, 0);

		if (NULL == hEditor)
			return FALSE;
		
		ToolbarEditbox_AttachWindow(hEditor, this);

		HFONT hFont = (HFONT)SendMessage(hToolbar, WM_GETFONT, 0, 0L);
		SendMessage(hEditor, WM_SETFONT, (WPARAM)hFont, 0L);
		SendMessage(hEditor, EM_SETMARGINS, (WPARAM)(EC_LEFTMARGIN | EC_RIGHTMARGIN), MAKELPARAM(textMargin, textMargin));
	}

	UINT editorStyle = GetWindowStyle(hEditor);
	if (0 == (WS_VISIBLE & editorStyle))
	{
		RECT textRect;
		CopyRect(&textRect, &rect);
		InflateRect(&textRect, -((iconWidth -1)/2), -((iconHeight -1)/2));
		textRect.top = textRect.bottom - textHeight;

		SetWindowPos(hEditor, NULL, textRect.left, textRect.top, 
				textRect.right - textRect.left, textRect.bottom - textRect.top, 
				SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
		
		UINT editorStyle = GetWindowStyle(hEditor);
		if (0 == (WS_VISIBLE & editorStyle))
			SetWindowLongPtr(hEditor, GWL_STYLE, editorStyle | WS_VISIBLE);
		
	}

	if (IsWindowVisible(hEditor))
	{		
		if (hEditor != GetFocus())
			::SetFocus(hEditor);

		if (NULL != ppt)
		{
			POINT editorPt = *ppt;
			MapWindowPoints(hToolbar, hEditor, &editorPt, 1);
			PostMessage(hEditor, WM_LBUTTONDOWN, (WPARAM)(MK_LBUTTON), MAKELPARAM(editorPt.x, editorPt.y));
		}
		else
		{
			SendMessage(hEditor, NTEBM_SELECTALL, 0, 0);
		}
		
		InvalidateRect(hEditor, NULL, FALSE);
	}

	InvalidateRect(hToolbar, &rect, FALSE);
	return TRUE;
}


UINT ToolbarAddress::GetStyle()
{
	UINT filteredStyle = style;
	if (0 != (styleAddressReadonly & filteredStyle))
		filteredStyle &= ~styleTabstop;

	return filteredStyle;
}

void ToolbarAddress::SetStyle(HWND hToolbar, UINT newStyle, UINT styleMask)
{
	ToolbarItem::SetStyle(hToolbar, newStyle, styleMask);

	if (NULL != hEditor)
	{
		if (0 != (stateHidden & styleMask) && 0 != (stateHidden & style))
		{
			if (GetFocus() == hEditor)
				EditboxNavigateNextCtrl(hEditor, TRUE);
			else
				DestroyWindow(hEditor);
		}
	
		if (0 != (styleAddressReadonly & styleMask))
		{
			UINT editorStyle = GetWindowStyle(hEditor);
			if (0 == (styleAddressReadonly & style) != 0 == (ES_READONLY & editorStyle))
				SendMessage(hEditor, EM_SETREADONLY, (WPARAM)(0 != (styleAddressReadonly & style)), 0L);
		}
	}
}

BOOL ToolbarAddress::SetRect(const RECT *prc)
{
	BOOL result = ToolbarItem::SetRect(prc);
	if (FALSE == result) return result;
	
	if (NULL != hEditor)
	{
		UINT editorStyle = GetWindowStyle(hEditor);
		if (0 != (WS_VISIBLE & editorStyle))
		{
			RECT textRect;
			CopyRect(&textRect, &rect);
			InflateRect(&textRect, -((iconWidth -1)/2), -((iconHeight -1)/2));
			textRect.top = textRect.bottom - textHeight;
			
			SetWindowPos(hEditor, NULL, textRect.left, textRect.top, 
					textRect.right - textRect.left, textRect.bottom - textRect.top, 
					SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
		}
	}

	return result;
}

BOOL ToolbarAddress::SetRectEmpty()
{
	BOOL result = ToolbarItem::SetRectEmpty();
	if (FALSE == result) return result;


	if (NULL != hEditor)
	{
		if (GetFocus() == hEditor)
			EditboxNavigateNextCtrl(hEditor, TRUE);
		else
			DestroyWindow(hEditor);
	}

	return result;
}

BOOL ToolbarAddress::SetDescription(HWND hToolbar, LPCWSTR pszDescription)
{
	if (NULL != pszDescription && FALSE == IS_INTRESOURCE(pszDescription))
	{
		if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, 0, L"about:blank", -1, pszDescription, -1))
			pszDescription = NULL;
	}
	
	BOOL result = ToolbarItem::SetDescription(hToolbar, pszDescription);
	if (NULL != hToolbar)
		InvalidateRect(hToolbar, &rect, TRUE);
	return result;
}

BOOL ToolbarAddress::AdjustRect(HWND hToolbar, RECT *proposedRect)
{	
	if (0 == (styleFlexible & style) || 
		proposedRect->right < proposedRect->left ||
		((UINT)(proposedRect->right - proposedRect->left)) < minWidth)
	{
		proposedRect->right = proposedRect->left + minWidth;
	}
	//INT iconCY = Toolbar_GetImageListHeight(hToolbar);
	INT height = textHeight + iconHeight;
	INT t = ((proposedRect->bottom - proposedRect->top) - height);
	proposedRect->top += (t/2 + t%2);
	proposedRect->bottom = proposedRect->top + height;

	
	return TRUE;
}

BOOL ToolbarAddress::Paint(HWND hToolbar, HDC hdc, const RECT *paintRect, UINT state)
{
	
	if (NULL == bitmap)
		return FALSE;
		
	RECT blitRect;
	if (!IntersectRect(&blitRect, paintRect))
		return TRUE;
	
	BOOL success = FALSE;
	HDC hdcSrc = CreateCompatibleDC(hdc);

	UINT editorStyle = (NULL != hEditor) ? GetWindowStyle(hEditor) : 0;
	INT offsetY;
	if (0 != (stateDisabled & state) || 0 != (styleAddressReadonly & style))
		offsetY = 2* iconHeight;
	else if (WS_VISIBLE == ((WS_VISIBLE | ES_READONLY | WS_DISABLED) & editorStyle))
		offsetY = 1 * iconHeight;
	else
		offsetY = 0;

	INT partCY = (iconHeight - 1)/2;
	INT partCX = (iconWidth - 1)/2;

	RECT textRect;
	CopyRect(&textRect, &rect);
	InflateRect(&textRect, -partCX, -partCY);

	INT clipRgnCode = -1;
	HRGN clipRgn = CreateRectRgn(0, 0, 0, 0);
	if (NULL != clipRgn)
	{
		clipRgnCode = GetClipRgn(hdc, clipRgn);
		ExcludeClipRect(hdc, textRect.left, textRect.top, textRect.right, textRect.bottom);
	}

	if (NULL != hdcSrc)
	{
		success = TRUE;
		HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcSrc, bitmap);

		// left-top
		BitBlt(hdc, rect.left, rect.top, partCX, partCY, hdcSrc, 0, offsetY + 0, SRCCOPY);
		// right-top
		BitBlt(hdc, rect.right - partCX, rect.top, partCX, partCY, hdcSrc, iconWidth - partCX, offsetY + 0, SRCCOPY);
		// right-bottom
		BitBlt(hdc, rect.right - partCX, rect.bottom - partCY, partCX, partCY, hdcSrc, iconWidth - partCX, offsetY + (iconHeight - partCY), SRCCOPY);
		// left-bottom
		BitBlt(hdc, rect.left, rect.bottom - partCY, partCX, partCY, hdcSrc, 0, offsetY + (iconHeight - partCY), SRCCOPY);
		
		INT stretchMode = SetStretchBltMode(hdc, COLORONCOLOR);

		StretchBlt(hdc, rect.left, rect.top + partCY, partCX, (rect.bottom - rect.top) - 2*partCY, hdcSrc, 0, offsetY + partCY, partCX, 1, SRCCOPY);
		StretchBlt(hdc, rect.right - partCX, rect.top + partCY, partCX, (rect.bottom - rect.top) - 2*partCY, hdcSrc, iconWidth - partCX, offsetY + partCY, partCX, 1, SRCCOPY);
		StretchBlt(hdc, rect.left + partCX, rect.top, (rect.right - rect.left) - 2 * partCX, partCY, hdcSrc, partCX, offsetY + 0, 1, partCY, SRCCOPY);
		StretchBlt(hdc, rect.left + partCX, rect.bottom - partCY, (rect.right - rect.left) - 2 * partCX, partCY, hdcSrc, partCX, offsetY + (iconHeight - partCY), 1, partCY, SRCCOPY);
		

		if (COLORONCOLOR != stretchMode)
				SetStretchBltMode(hdc, stretchMode);


		SelectObject(hdcSrc, hbmpOld);
		DeleteDC(hdcSrc);
	}
	
	if (clipRgnCode >= 0)
		SelectClipRgn(hdc, ( 0 != clipRgnCode) ? (clipRgn ? clipRgn : NULL) : NULL);
	
	if (NULL != clipRgn)
		DeleteObject(clipRgn);

	COLORREF rgbTextOrig, rgbBkOrig;
	
	if (0 == (stateDisabled & state) && 0 == (styleAddressReadonly & style))
	{
		rgbTextOrig = SetTextColor(hdc, rgbText);
		rgbBkOrig = SetBkColor(hdc, rgbBk);
	}
	else
	{
		rgbTextOrig = GetTextColor(hdc);
		rgbBkOrig = GetBkColor(hdc);
	}

	LPCWSTR pszText;
	pszText = ( 0 == (styleAddressShowReal & style) && NULL != description) ? description : text;

	
	UINT cchText = (NULL != pszText && FALSE == IS_INTRESOURCE(pszText)) ? lstrlen(pszText) : 0;
	INT textAlign = SetTextAlign(hdc, TA_LEFT | TA_BOTTOM);
	ExtTextOut(hdc, textRect.left + textMargin, textRect.bottom, ETO_OPAQUE | ETO_CLIPPED, &textRect, pszText, cchText, NULL);

	if (textAlign != (TA_LEFT | TA_BOTTOM)) SetTextAlign(hdc, textAlign);
	if (rgbText != rgbTextOrig) SetTextColor(hdc, rgbTextOrig);
	if (rgbBk != rgbBkOrig) SetBkColor(hdc, rgbBkOrig);

	
	
	return success;
}

INT ToolbarAddress::GetTip(LPTSTR pszBuffer, INT cchBufferMax)
{
	return 0;
}

HRESULT ToolbarAddress::GetText(LPWSTR pszBuffer, UINT cchBufferMax)
{
	if (NULL == pszBuffer) return E_POINTER;
	if (0 == cchBufferMax) return E_OUTOFMEMORY;
	if (NULL == text)
	{
		*pszBuffer = L'\0';
		return S_OK;
	}

	if (IS_INTRESOURCE(text))
		return Plugin_CopyResString(pszBuffer, cchBufferMax, text);
	
	return AddressEncoder_EncodeString(text, pszBuffer,(size_t*)&cchBufferMax, ICU_BROWSER_MODE);
}

HRESULT ToolbarAddress::GetTextLength(size_t *pcchLength)
{
	if (NULL == pcchLength)
		return E_POINTER;

	if (NULL == text)
	{
		*pcchLength = 0;
		return S_OK;
	}

	if (IS_INTRESOURCE(text))
	{
		WCHAR szBuffer[8192] = {0};
		HRESULT hr = Plugin_CopyResString(szBuffer, ARRAYSIZE(szBuffer), text);
		*pcchLength = (FAILED(hr)) ? 0 : lstrlen(szBuffer);
		return hr;
	}

	WCHAR szBuffer[1] = {0};
	size_t cchBufferMax = ARRAYSIZE(szBuffer);
	HRESULT hr = AddressEncoder_EncodeString(text, szBuffer, &cchBufferMax, ICU_BROWSER_MODE);
	if (SUCCEEDED(hr) || ENC_E_INSUFFICIENT_BUFFER == hr)
	{
		if (0 != cchBufferMax) cchBufferMax--;
		*pcchLength = cchBufferMax;
		hr = S_OK;
	}
	else
	{
		hr = E_FAIL;
		*pcchLength = 0;

	}

	return hr;

}
void ToolbarAddress::MouseMove(HWND hToolbar, UINT mouseFlags, POINT pt)
{
//	UINT styleNew = (PtInItem(pt)) ? stateHighlighted  : 0;
//	SetStyle(hToolbar, styleNew, stateHighlighted);
	
}

void ToolbarAddress::MouseLeave(HWND hToolbar)
{
	if (0 != (stateHighlighted & style))
	{
		SetStyle(hToolbar, 0, stateHighlighted);
	}
}

void ToolbarAddress::LButtonDown(HWND hToolbar, UINT mouseFlags, POINT pt)
{
	ActivateEditor(hToolbar, &pt);
}

void ToolbarAddress::UpdateSkin(HWND hToolbar)
{
	if (NULL != bitmap)
	{
		DeleteObject(bitmap);
		bitmap = NULL;
	}
			
	iconHeight = 0;
	iconWidth = 0;

	COLORREF rgbToolBk = Toolbar_GetBkColor(hToolbar);
	rgbText = Toolbar_GetEditColor(hToolbar);
	rgbBk = Toolbar_GetEditBkColor(hToolbar);

	ifc_omimageloader *loader;
	if (SUCCEEDED(Plugin_QueryImageLoader(Plugin_GetInstance(), MAKEINTRESOURCE(IDR_TOOLBARADDRESS_IMAGE), TRUE, &loader)))
	{
		BITMAPINFOHEADER headerInfo;
		BYTE *pixelData;
		if (SUCCEEDED(loader->LoadBitmapEx(&bitmap, &headerInfo, (void**)&pixelData)))
		{
			if (headerInfo.biHeight < 0) headerInfo.biHeight = -headerInfo.biHeight;
			iconHeight = headerInfo.biHeight/3;
			iconWidth  = headerInfo.biWidth;
			
			Image_Colorize(pixelData, headerInfo.biWidth, headerInfo.biHeight, headerInfo.biBitCount, 
				rgbToolBk, Toolbar_GetFgColor(hToolbar), FALSE);
			
			// disabled
			Image_BlendOnColorEx(pixelData, headerInfo.biWidth, headerInfo.biHeight, 
					0, 0, headerInfo.biWidth, iconHeight, headerInfo.biBitCount, FALSE, rgbToolBk);
			// highlighted
			Image_BlendOnColorEx(pixelData, headerInfo.biWidth, headerInfo.biHeight, 
					0, 1*iconHeight, headerInfo.biWidth, iconHeight, headerInfo.biBitCount, FALSE, rgbBk);
			// normal
			Image_BlendOnColorEx(pixelData, headerInfo.biWidth, headerInfo.biHeight, 
					0, 2*iconHeight, headerInfo.biWidth, iconHeight, headerInfo.biBitCount, FALSE, rgbBk);

		}
		loader->Release();
	}

	textHeight = 12;
	HFONT toolbarFont = (HFONT)SendMessage(hToolbar, WM_GETFONT, 0, 0L);

	if (NULL != toolbarFont)
	{
		HDC hdc = GetDCEx(hToolbar, NULL, DCX_CACHE | DCX_NORESETATTRS);
		if (NULL != hdc)
		{	
			HFONT originalFont = (HFONT)SelectObject(hdc, toolbarFont);

			TEXTMETRIC tm;
			if (GetTextMetrics(hdc, &tm))
			{
				textHeight = tm.tmHeight;
				textMargin = tm.tmAveCharWidth/2 + tm.tmAveCharWidth%2;
			}
			
			SelectObject(hdc, originalFont);
			ReleaseDC(hToolbar, hdc);
		}

		if (NULL != hEditor)
		{
			UINT editorStyle = GetWindowStyle(hEditor);
			if (0 != (WS_VISIBLE & editorStyle))
			{
				SendMessage(hEditor, WM_SETFONT, (WPARAM)toolbarFont, 0L);
				SendMessage(hEditor, EM_SETMARGINS, (WPARAM)(EC_LEFTMARGIN | EC_RIGHTMARGIN), MAKELPARAM(textMargin, textMargin));
			}
		}
	}
}

BOOL ToolbarAddress::FillMenuInfo(HWND hToolbar, MENUITEMINFO *pmii, LPWSTR pszBuffer, INT cchBufferMax)
{
	return FALSE;
}
void ToolbarAddress::CommandSent(HWND hToolbar, INT commandId)
{
	if (NULL != hEditor && ID_ADDRESSBAR_CHANGED != commandId)
	{
		if (GetFocus() == hEditor)
			EditboxNavigateNextCtrl(hEditor, TRUE);
		else
			DestroyWindow(hEditor);
	}
}



BOOL ToolbarAddress::SetValueStr(HWND hToolbar, LPCWSTR value)
{
	Plugin_FreeResString(text);
	
	if (NULL != value)
	{
		if (FALSE != IS_INTRESOURCE(value) || FAILED(AddressEncoder_DecodeString(value, &text)))
			text = Plugin_DuplicateResString(value);
	}
	else
		text = NULL;
	
	RECT textRect;
	CopyRect(&textRect, &rect);
	InflateRect(&textRect, -((iconWidth -1)/2), -(iconHeight - 1)/2);

	InvalidateRect(hToolbar, &textRect, TRUE);

	return TRUE;
}

BOOL ToolbarAddress::SetClipboardText(HWND hToolbar, LPCWSTR pszText)
{
	if(FALSE == OpenClipboard(hToolbar))
		return FALSE;

	EmptyClipboard(); 
	
	INT cchLen;
	HGLOBAL hMemory;
	
	cchLen = (NULL != pszText) ? lstrlenW(pszText) : 0; 
	hMemory = (0 != cchLen) ? GlobalAlloc(GMEM_MOVEABLE, (cchLen + 1) * sizeof(WCHAR)) : NULL; 
	if (NULL != hMemory) 
	{ 
		LPWSTR pszDest = (LPWSTR)GlobalLock(hMemory); 
		CopyMemory(pszDest, pszText, (cchLen + 1) * sizeof(WCHAR));
		GlobalUnlock(hMemory); 	
	} 
	SetClipboardData(CF_UNICODETEXT, hMemory); 
	

	LPSTR pszTextAnsi = (NULL != pszText) ? Plugin_WideCharToMultiByte(CP_ACP, 0, pszText, -1, NULL, NULL) : NULL;
	cchLen = (NULL != pszTextAnsi) ? lstrlenA(pszTextAnsi) : 0; 
	hMemory = (0 != cchLen) ? GlobalAlloc(GMEM_MOVEABLE, (cchLen + 1) * sizeof(CHAR)) : NULL; 
	if (NULL != hMemory) 
	{ 
		LPWSTR pszDest = (LPWSTR)GlobalLock(hMemory); 
		CopyMemory(pszDest, pszTextAnsi, (cchLen + 1) * sizeof(CHAR));
		GlobalUnlock(hMemory); 	
	} 
	SetClipboardData(CF_TEXT, hMemory); 

	CloseClipboard(); 
	return TRUE;
}

BOOL ToolbarAddress::DisplayContextMenu(HWND hToolbar, INT x, INT y)
{
	HMENU hMenu = Menu_GetMenu(MENU_ADDRESSBAR, 0);
	if (NULL != hMenu)
	{	
		UINT fEnable = MF_BYCOMMAND;
		if (0 != (styleAddressReadonly & style))
			fEnable |= (MF_DISABLED | MF_GRAYED);
		else
			fEnable |= MF_ENABLED;

		EnableMenuItem(hMenu, ID_ADDRESSBAR_EDITADDRESS, fEnable);

		INT commandId = Menu_TrackPopup(hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD, x, y, hToolbar, NULL);
		switch(commandId)
		{
			case ID_ADDRESSBAR_COPY:
				if (0 == (styleAddressShowReal & style) && NULL != description)
				{
					UINT cchTextMax = (IS_INTRESOURCE(description)) ? 2048 : (lstrlen(description) + 1);
					LPWSTR pszText = Plugin_MallocString(cchTextMax);
					if (NULL != pszText && FAILED(GetDescription(pszText, cchTextMax)))
					{
						Plugin_FreeString(pszText);
						pszText = NULL;
					}
					SetClipboardText(hToolbar, pszText);
					Plugin_FreeString(pszText);
					break;
				}
				// let it go
			case ID_ADDRESSBAR_COPYADDRESS:
				{
					size_t cchTextMax;
					if (SUCCEEDED(GetTextLength(&cchTextMax)))
					{
						cchTextMax++;
						LPWSTR pszText = Plugin_MallocString(cchTextMax);
						if (NULL != pszText && FAILED(GetText(pszText, (UINT)cchTextMax)))
						{
							Plugin_FreeString(pszText);
							pszText = NULL;
						}
							
						SetClipboardText(hToolbar, pszText);
						Plugin_FreeString(pszText);
					}
				}
				break;
			case ID_ADDRESSBAR_EDITADDRESS:
				ActivateEditor(hToolbar,  NULL);
				break;
		}

		Menu_ReleaseMenu(hMenu, MENU_ADDRESSBAR);
	}
	return TRUE;
}

void ToolbarAddress::SetFocus(HWND hToolbar, ToolbarItem *focusItem, BOOL fSet)
{
	ActivateEditor(hToolbar,  NULL);
}

BOOL ToolbarAddress::SetCursor(HWND hToolbar, HWND hCursor, UINT hitTest, UINT messageId)
{
	RECT textRect;
	CopyRect(&textRect, &rect);
	InflateRect(&textRect, -((iconWidth -1)/2), -(iconHeight - 1)/2);
	UINT messagePos = GetMessagePos();
	POINTS messagePts = MAKEPOINTS(messagePos);
	POINT messagePt;
	POINTSTOPOINT(messagePt, messagePts);
	MapWindowPoints(HWND_DESKTOP, hToolbar, &messagePt, 1);
	if (::PtInRect(&textRect, messagePt))
	{
		HCURSOR cursor = LoadCursor(NULL, IDC_IBEAM);
		if (NULL != cursor)
		{
			::SetCursor(cursor);
			return TRUE;
		}
	}
	return FALSE;
}

void ToolbarAddress::EditboxDestroyed(HWND hwnd)
{
	if (hEditor == hwnd)
	{
		hEditor = NULL;
	
		HWND hToolbar = GetParent(hwnd);
		if (NULL != hToolbar) 
		{
			InvalidateRect(hToolbar, &rect, FALSE);
			Toolbar_CheckHide(hToolbar, TRUE);
		}
	}
}

BOOL ToolbarAddress::EditboxKillFocus(HWND hwnd, HWND hFocus)
{
	DestroyWindow(hwnd);
	return TRUE;
}

void ToolbarAddress::EditboxResetText(HWND hwnd)
{
	UINT editboxStyle = GetWindowStyle(hwnd);
	if (0 != (WS_VISIBLE & editboxStyle))
		SetWindowLongPtr(hwnd, GWL_STYLE, editboxStyle & ~WS_VISIBLE);

	SetWindowText(hwnd, text);
	SendMessage(hEditor, NTEBM_SELECTALL, 0, 0L);
	
	if (0 != (WS_VISIBLE & editboxStyle))
	{
		UINT editboxStyle = GetWindowStyle(hwnd);
		if (0 == (WS_VISIBLE & editboxStyle))
			SetWindowLongPtr(hwnd, GWL_STYLE, editboxStyle | WS_VISIBLE);
		
		InvalidateRect(hwnd, NULL, FALSE);
	}

}

void ToolbarAddress::EditboxNavigateNextCtrl(HWND hwnd, BOOL fForward)
{
	HWND hToolbar = GetParent(hwnd);
	if (NULL != hToolbar) 
	{
		INT itemIndex = Toolbar_GetNextTabItem(hToolbar, name, (FALSE == fForward));
		if (ITEM_ERR != itemIndex)
		{
			if (FALSE != Toolbar_NextItem(hToolbar, MAKEINTRESOURCE(itemIndex), TRUE))
			{
				::SetFocus(hToolbar);
				return;
			}
		}

		HWND hParent = GetAncestor(hToolbar, GA_PARENT);
		while(NULL != hParent &&
				0 != (WS_CHILD & GetWindowStyle(hParent)) &&
				0 != (WS_EX_CONTROLPARENT & GetWindowStyleEx(hParent)))
		{
			HWND hTest = GetAncestor(hParent, GA_PARENT);
			if (NULL == hTest) break;
			hParent = hTest;
		}
		
		if (NULL != hParent)
		{
			HWND hFocus = GetNextDlgTabItem(hParent, hToolbar, (FALSE == fForward));
			if (NULL != hFocus) ::SetFocus(hFocus);
		}
	}
}

void ToolbarAddress::EditboxAcceptText(HWND hwnd)
{
	UINT cchLenMax = GetWindowTextLength(hwnd);
	if (0 == cchLenMax) return;

	cchLenMax++;
	Plugin_FreeResString(text);
	text = Plugin_MallocString(cchLenMax);
	if (NULL != text)
	{
		INT cchText = GetWindowText(hwnd, text, cchLenMax);
		if (0 != cchText)
		{
			LPCWSTR begin, end;
			begin = text;
			end = text + cchText + 1;
			while (L' ' == *begin && begin != end) begin++;
			while (L' ' == *end && begin != end) end--;
			if (end <= begin) return;
		}
	}
	
	HWND hToolbar = GetParent(hwnd);

	if (NULL != hToolbar) 
	{
		Toolbar_SendCommand(hToolbar, ID_ADDRESSBAR_CHANGED);
	}

	EditboxNavigateNextCtrl(hwnd, TRUE);
}
BOOL ToolbarAddress::EditboxKeyDown(HWND hwnd, UINT vKey, UINT state)
{
	switch(vKey)
	{
		case VK_UP:
		case VK_DOWN:
			return TRUE;
	}
	return FALSE;
}
BOOL ToolbarAddress::EditboxKeyUp(HWND hwnd, UINT vKey, UINT state)
{
	return FALSE;
}
BOOL ToolbarAddress::EditboxPreviewChar(HWND hwnd, UINT vKey, UINT state)
{
	return FALSE;
}