1
0

toolbarAddress.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. #include "main.h"
  2. #include "./toolbarAddress.h"
  3. #include "./toolbar.h"
  4. #include "./toolbarEditbox.h"
  5. #include "./resource.h"
  6. #include "./graphics.h"
  7. #include "./menu.h"
  8. #include "./ifc_imageloader.h"
  9. #include "./addressEncoder.h"
  10. #include <wininet.h>
  11. //#include <shlwapi.h>
  12. #include <strsafe.h>
  13. #define ADDRESSBAR_MINWIDTH 96
  14. UINT minWidth;
  15. HBITMAP bitmap;
  16. INT iconHeight;
  17. INT iconWidth;
  18. INT textHeight;
  19. INT textMargin;
  20. COLORREF rgbText;
  21. COLORREF rgbBk;
  22. HWND hEditor;
  23. ToolbarAddress::ToolbarAddress(LPCSTR pszName, UINT nStyle) :
  24. ToolbarItem(pszName, nStyle, ICON_NONE, NULL, NULL),
  25. minWidth(ADDRESSBAR_MINWIDTH), bitmap(NULL), iconHeight(0), iconWidth(0),
  26. textHeight(12), textMargin(0), rgbText(0x000000), rgbBk(0xFFFFFF), hEditor(NULL)
  27. {
  28. }
  29. ToolbarAddress::~ToolbarAddress()
  30. {
  31. if (NULL != bitmap)
  32. DeleteObject(bitmap);
  33. if (NULL != hEditor)
  34. DestroyWindow(hEditor);
  35. }
  36. ToolbarItem* CALLBACK ToolbarAddress::CreateInstance(ToolbarItem::Template *item)
  37. {
  38. if (NULL == item)
  39. return NULL;
  40. return new ToolbarAddress( (NULL != item->name) ? item->name : TOOLCLS_ADDRESSBAR,
  41. item->style);
  42. }
  43. BOOL ToolbarAddress::ActivateEditor(HWND hToolbar, const POINT *ppt)
  44. {
  45. if (NULL == hEditor)
  46. {
  47. UINT editorStyle = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_TABSTOP |
  48. ES_LEFT | ES_NOHIDESEL | ES_AUTOHSCROLL;
  49. if (0 != (styleAddressReadonly & style))
  50. editorStyle |= ES_READONLY;
  51. hEditor = CreateWindowEx(WS_EX_NOPARENTNOTIFY, L"Edit", text, editorStyle, 0, 0, 0, 0, hToolbar, NULL, NULL, 0);
  52. if (NULL == hEditor)
  53. return FALSE;
  54. ToolbarEditbox_AttachWindow(hEditor, this);
  55. HFONT hFont = (HFONT)SendMessage(hToolbar, WM_GETFONT, 0, 0L);
  56. SendMessage(hEditor, WM_SETFONT, (WPARAM)hFont, 0L);
  57. SendMessage(hEditor, EM_SETMARGINS, (WPARAM)(EC_LEFTMARGIN | EC_RIGHTMARGIN), MAKELPARAM(textMargin, textMargin));
  58. }
  59. UINT editorStyle = GetWindowStyle(hEditor);
  60. if (0 == (WS_VISIBLE & editorStyle))
  61. {
  62. RECT textRect;
  63. CopyRect(&textRect, &rect);
  64. InflateRect(&textRect, -((iconWidth -1)/2), -((iconHeight -1)/2));
  65. textRect.top = textRect.bottom - textHeight;
  66. SetWindowPos(hEditor, NULL, textRect.left, textRect.top,
  67. textRect.right - textRect.left, textRect.bottom - textRect.top,
  68. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
  69. UINT editorStyle = GetWindowStyle(hEditor);
  70. if (0 == (WS_VISIBLE & editorStyle))
  71. SetWindowLongPtr(hEditor, GWL_STYLE, editorStyle | WS_VISIBLE);
  72. }
  73. if (IsWindowVisible(hEditor))
  74. {
  75. if (hEditor != GetFocus())
  76. ::SetFocus(hEditor);
  77. if (NULL != ppt)
  78. {
  79. POINT editorPt = *ppt;
  80. MapWindowPoints(hToolbar, hEditor, &editorPt, 1);
  81. PostMessage(hEditor, WM_LBUTTONDOWN, (WPARAM)(MK_LBUTTON), MAKELPARAM(editorPt.x, editorPt.y));
  82. }
  83. else
  84. {
  85. SendMessage(hEditor, NTEBM_SELECTALL, 0, 0);
  86. }
  87. InvalidateRect(hEditor, NULL, FALSE);
  88. }
  89. InvalidateRect(hToolbar, &rect, FALSE);
  90. return TRUE;
  91. }
  92. UINT ToolbarAddress::GetStyle()
  93. {
  94. UINT filteredStyle = style;
  95. if (0 != (styleAddressReadonly & filteredStyle))
  96. filteredStyle &= ~styleTabstop;
  97. return filteredStyle;
  98. }
  99. void ToolbarAddress::SetStyle(HWND hToolbar, UINT newStyle, UINT styleMask)
  100. {
  101. ToolbarItem::SetStyle(hToolbar, newStyle, styleMask);
  102. if (NULL != hEditor)
  103. {
  104. if (0 != (stateHidden & styleMask) && 0 != (stateHidden & style))
  105. {
  106. if (GetFocus() == hEditor)
  107. EditboxNavigateNextCtrl(hEditor, TRUE);
  108. else
  109. DestroyWindow(hEditor);
  110. }
  111. if (0 != (styleAddressReadonly & styleMask))
  112. {
  113. UINT editorStyle = GetWindowStyle(hEditor);
  114. if (0 == (styleAddressReadonly & style) != 0 == (ES_READONLY & editorStyle))
  115. SendMessage(hEditor, EM_SETREADONLY, (WPARAM)(0 != (styleAddressReadonly & style)), 0L);
  116. }
  117. }
  118. }
  119. BOOL ToolbarAddress::SetRect(const RECT *prc)
  120. {
  121. BOOL result = ToolbarItem::SetRect(prc);
  122. if (FALSE == result) return result;
  123. if (NULL != hEditor)
  124. {
  125. UINT editorStyle = GetWindowStyle(hEditor);
  126. if (0 != (WS_VISIBLE & editorStyle))
  127. {
  128. RECT textRect;
  129. CopyRect(&textRect, &rect);
  130. InflateRect(&textRect, -((iconWidth -1)/2), -((iconHeight -1)/2));
  131. textRect.top = textRect.bottom - textHeight;
  132. SetWindowPos(hEditor, NULL, textRect.left, textRect.top,
  133. textRect.right - textRect.left, textRect.bottom - textRect.top,
  134. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
  135. }
  136. }
  137. return result;
  138. }
  139. BOOL ToolbarAddress::SetRectEmpty()
  140. {
  141. BOOL result = ToolbarItem::SetRectEmpty();
  142. if (FALSE == result) return result;
  143. if (NULL != hEditor)
  144. {
  145. if (GetFocus() == hEditor)
  146. EditboxNavigateNextCtrl(hEditor, TRUE);
  147. else
  148. DestroyWindow(hEditor);
  149. }
  150. return result;
  151. }
  152. BOOL ToolbarAddress::SetDescription(HWND hToolbar, LPCWSTR pszDescription)
  153. {
  154. if (NULL != pszDescription && FALSE == IS_INTRESOURCE(pszDescription))
  155. {
  156. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, 0, L"about:blank", -1, pszDescription, -1))
  157. pszDescription = NULL;
  158. }
  159. BOOL result = ToolbarItem::SetDescription(hToolbar, pszDescription);
  160. if (NULL != hToolbar)
  161. InvalidateRect(hToolbar, &rect, TRUE);
  162. return result;
  163. }
  164. BOOL ToolbarAddress::AdjustRect(HWND hToolbar, RECT *proposedRect)
  165. {
  166. if (0 == (styleFlexible & style) ||
  167. proposedRect->right < proposedRect->left ||
  168. ((UINT)(proposedRect->right - proposedRect->left)) < minWidth)
  169. {
  170. proposedRect->right = proposedRect->left + minWidth;
  171. }
  172. //INT iconCY = Toolbar_GetImageListHeight(hToolbar);
  173. INT height = textHeight + iconHeight;
  174. INT t = ((proposedRect->bottom - proposedRect->top) - height);
  175. proposedRect->top += (t/2 + t%2);
  176. proposedRect->bottom = proposedRect->top + height;
  177. return TRUE;
  178. }
  179. BOOL ToolbarAddress::Paint(HWND hToolbar, HDC hdc, const RECT *paintRect, UINT state)
  180. {
  181. if (NULL == bitmap)
  182. return FALSE;
  183. RECT blitRect;
  184. if (!IntersectRect(&blitRect, paintRect))
  185. return TRUE;
  186. BOOL success = FALSE;
  187. HDC hdcSrc = CreateCompatibleDC(hdc);
  188. UINT editorStyle = (NULL != hEditor) ? GetWindowStyle(hEditor) : 0;
  189. INT offsetY;
  190. if (0 != (stateDisabled & state) || 0 != (styleAddressReadonly & style))
  191. offsetY = 2* iconHeight;
  192. else if (WS_VISIBLE == ((WS_VISIBLE | ES_READONLY | WS_DISABLED) & editorStyle))
  193. offsetY = 1 * iconHeight;
  194. else
  195. offsetY = 0;
  196. INT partCY = (iconHeight - 1)/2;
  197. INT partCX = (iconWidth - 1)/2;
  198. RECT textRect;
  199. CopyRect(&textRect, &rect);
  200. InflateRect(&textRect, -partCX, -partCY);
  201. INT clipRgnCode = -1;
  202. HRGN clipRgn = CreateRectRgn(0, 0, 0, 0);
  203. if (NULL != clipRgn)
  204. {
  205. clipRgnCode = GetClipRgn(hdc, clipRgn);
  206. ExcludeClipRect(hdc, textRect.left, textRect.top, textRect.right, textRect.bottom);
  207. }
  208. if (NULL != hdcSrc)
  209. {
  210. success = TRUE;
  211. HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcSrc, bitmap);
  212. // left-top
  213. BitBlt(hdc, rect.left, rect.top, partCX, partCY, hdcSrc, 0, offsetY + 0, SRCCOPY);
  214. // right-top
  215. BitBlt(hdc, rect.right - partCX, rect.top, partCX, partCY, hdcSrc, iconWidth - partCX, offsetY + 0, SRCCOPY);
  216. // right-bottom
  217. BitBlt(hdc, rect.right - partCX, rect.bottom - partCY, partCX, partCY, hdcSrc, iconWidth - partCX, offsetY + (iconHeight - partCY), SRCCOPY);
  218. // left-bottom
  219. BitBlt(hdc, rect.left, rect.bottom - partCY, partCX, partCY, hdcSrc, 0, offsetY + (iconHeight - partCY), SRCCOPY);
  220. INT stretchMode = SetStretchBltMode(hdc, COLORONCOLOR);
  221. StretchBlt(hdc, rect.left, rect.top + partCY, partCX, (rect.bottom - rect.top) - 2*partCY, hdcSrc, 0, offsetY + partCY, partCX, 1, SRCCOPY);
  222. StretchBlt(hdc, rect.right - partCX, rect.top + partCY, partCX, (rect.bottom - rect.top) - 2*partCY, hdcSrc, iconWidth - partCX, offsetY + partCY, partCX, 1, SRCCOPY);
  223. StretchBlt(hdc, rect.left + partCX, rect.top, (rect.right - rect.left) - 2 * partCX, partCY, hdcSrc, partCX, offsetY + 0, 1, partCY, SRCCOPY);
  224. StretchBlt(hdc, rect.left + partCX, rect.bottom - partCY, (rect.right - rect.left) - 2 * partCX, partCY, hdcSrc, partCX, offsetY + (iconHeight - partCY), 1, partCY, SRCCOPY);
  225. if (COLORONCOLOR != stretchMode)
  226. SetStretchBltMode(hdc, stretchMode);
  227. SelectObject(hdcSrc, hbmpOld);
  228. DeleteDC(hdcSrc);
  229. }
  230. if (clipRgnCode >= 0)
  231. SelectClipRgn(hdc, ( 0 != clipRgnCode) ? (clipRgn ? clipRgn : NULL) : NULL);
  232. if (NULL != clipRgn)
  233. DeleteObject(clipRgn);
  234. COLORREF rgbTextOrig, rgbBkOrig;
  235. if (0 == (stateDisabled & state) && 0 == (styleAddressReadonly & style))
  236. {
  237. rgbTextOrig = SetTextColor(hdc, rgbText);
  238. rgbBkOrig = SetBkColor(hdc, rgbBk);
  239. }
  240. else
  241. {
  242. rgbTextOrig = GetTextColor(hdc);
  243. rgbBkOrig = GetBkColor(hdc);
  244. }
  245. LPCWSTR pszText;
  246. pszText = ( 0 == (styleAddressShowReal & style) && NULL != description) ? description : text;
  247. UINT cchText = (NULL != pszText && FALSE == IS_INTRESOURCE(pszText)) ? lstrlen(pszText) : 0;
  248. INT textAlign = SetTextAlign(hdc, TA_LEFT | TA_BOTTOM);
  249. ExtTextOut(hdc, textRect.left + textMargin, textRect.bottom, ETO_OPAQUE | ETO_CLIPPED, &textRect, pszText, cchText, NULL);
  250. if (textAlign != (TA_LEFT | TA_BOTTOM)) SetTextAlign(hdc, textAlign);
  251. if (rgbText != rgbTextOrig) SetTextColor(hdc, rgbTextOrig);
  252. if (rgbBk != rgbBkOrig) SetBkColor(hdc, rgbBkOrig);
  253. return success;
  254. }
  255. INT ToolbarAddress::GetTip(LPTSTR pszBuffer, INT cchBufferMax)
  256. {
  257. return 0;
  258. }
  259. HRESULT ToolbarAddress::GetText(LPWSTR pszBuffer, UINT cchBufferMax)
  260. {
  261. if (NULL == pszBuffer) return E_POINTER;
  262. if (0 == cchBufferMax) return E_OUTOFMEMORY;
  263. if (NULL == text)
  264. {
  265. *pszBuffer = L'\0';
  266. return S_OK;
  267. }
  268. if (IS_INTRESOURCE(text))
  269. return Plugin_CopyResString(pszBuffer, cchBufferMax, text);
  270. return AddressEncoder_EncodeString(text, pszBuffer,(size_t*)&cchBufferMax, ICU_BROWSER_MODE);
  271. }
  272. HRESULT ToolbarAddress::GetTextLength(size_t *pcchLength)
  273. {
  274. if (NULL == pcchLength)
  275. return E_POINTER;
  276. if (NULL == text)
  277. {
  278. *pcchLength = 0;
  279. return S_OK;
  280. }
  281. if (IS_INTRESOURCE(text))
  282. {
  283. WCHAR szBuffer[8192] = {0};
  284. HRESULT hr = Plugin_CopyResString(szBuffer, ARRAYSIZE(szBuffer), text);
  285. *pcchLength = (FAILED(hr)) ? 0 : lstrlen(szBuffer);
  286. return hr;
  287. }
  288. WCHAR szBuffer[1] = {0};
  289. size_t cchBufferMax = ARRAYSIZE(szBuffer);
  290. HRESULT hr = AddressEncoder_EncodeString(text, szBuffer, &cchBufferMax, ICU_BROWSER_MODE);
  291. if (SUCCEEDED(hr) || ENC_E_INSUFFICIENT_BUFFER == hr)
  292. {
  293. if (0 != cchBufferMax) cchBufferMax--;
  294. *pcchLength = cchBufferMax;
  295. hr = S_OK;
  296. }
  297. else
  298. {
  299. hr = E_FAIL;
  300. *pcchLength = 0;
  301. }
  302. return hr;
  303. }
  304. void ToolbarAddress::MouseMove(HWND hToolbar, UINT mouseFlags, POINT pt)
  305. {
  306. // UINT styleNew = (PtInItem(pt)) ? stateHighlighted : 0;
  307. // SetStyle(hToolbar, styleNew, stateHighlighted);
  308. }
  309. void ToolbarAddress::MouseLeave(HWND hToolbar)
  310. {
  311. if (0 != (stateHighlighted & style))
  312. {
  313. SetStyle(hToolbar, 0, stateHighlighted);
  314. }
  315. }
  316. void ToolbarAddress::LButtonDown(HWND hToolbar, UINT mouseFlags, POINT pt)
  317. {
  318. ActivateEditor(hToolbar, &pt);
  319. }
  320. void ToolbarAddress::UpdateSkin(HWND hToolbar)
  321. {
  322. if (NULL != bitmap)
  323. {
  324. DeleteObject(bitmap);
  325. bitmap = NULL;
  326. }
  327. iconHeight = 0;
  328. iconWidth = 0;
  329. COLORREF rgbToolBk = Toolbar_GetBkColor(hToolbar);
  330. rgbText = Toolbar_GetEditColor(hToolbar);
  331. rgbBk = Toolbar_GetEditBkColor(hToolbar);
  332. ifc_omimageloader *loader;
  333. if (SUCCEEDED(Plugin_QueryImageLoader(Plugin_GetInstance(), MAKEINTRESOURCE(IDR_TOOLBARADDRESS_IMAGE), TRUE, &loader)))
  334. {
  335. BITMAPINFOHEADER headerInfo;
  336. BYTE *pixelData;
  337. if (SUCCEEDED(loader->LoadBitmapEx(&bitmap, &headerInfo, (void**)&pixelData)))
  338. {
  339. if (headerInfo.biHeight < 0) headerInfo.biHeight = -headerInfo.biHeight;
  340. iconHeight = headerInfo.biHeight/3;
  341. iconWidth = headerInfo.biWidth;
  342. Image_Colorize(pixelData, headerInfo.biWidth, headerInfo.biHeight, headerInfo.biBitCount,
  343. rgbToolBk, Toolbar_GetFgColor(hToolbar), FALSE);
  344. // disabled
  345. Image_BlendOnColorEx(pixelData, headerInfo.biWidth, headerInfo.biHeight,
  346. 0, 0, headerInfo.biWidth, iconHeight, headerInfo.biBitCount, FALSE, rgbToolBk);
  347. // highlighted
  348. Image_BlendOnColorEx(pixelData, headerInfo.biWidth, headerInfo.biHeight,
  349. 0, 1*iconHeight, headerInfo.biWidth, iconHeight, headerInfo.biBitCount, FALSE, rgbBk);
  350. // normal
  351. Image_BlendOnColorEx(pixelData, headerInfo.biWidth, headerInfo.biHeight,
  352. 0, 2*iconHeight, headerInfo.biWidth, iconHeight, headerInfo.biBitCount, FALSE, rgbBk);
  353. }
  354. loader->Release();
  355. }
  356. textHeight = 12;
  357. HFONT toolbarFont = (HFONT)SendMessage(hToolbar, WM_GETFONT, 0, 0L);
  358. if (NULL != toolbarFont)
  359. {
  360. HDC hdc = GetDCEx(hToolbar, NULL, DCX_CACHE | DCX_NORESETATTRS);
  361. if (NULL != hdc)
  362. {
  363. HFONT originalFont = (HFONT)SelectObject(hdc, toolbarFont);
  364. TEXTMETRIC tm;
  365. if (GetTextMetrics(hdc, &tm))
  366. {
  367. textHeight = tm.tmHeight;
  368. textMargin = tm.tmAveCharWidth/2 + tm.tmAveCharWidth%2;
  369. }
  370. SelectObject(hdc, originalFont);
  371. ReleaseDC(hToolbar, hdc);
  372. }
  373. if (NULL != hEditor)
  374. {
  375. UINT editorStyle = GetWindowStyle(hEditor);
  376. if (0 != (WS_VISIBLE & editorStyle))
  377. {
  378. SendMessage(hEditor, WM_SETFONT, (WPARAM)toolbarFont, 0L);
  379. SendMessage(hEditor, EM_SETMARGINS, (WPARAM)(EC_LEFTMARGIN | EC_RIGHTMARGIN), MAKELPARAM(textMargin, textMargin));
  380. }
  381. }
  382. }
  383. }
  384. BOOL ToolbarAddress::FillMenuInfo(HWND hToolbar, MENUITEMINFO *pmii, LPWSTR pszBuffer, INT cchBufferMax)
  385. {
  386. return FALSE;
  387. }
  388. void ToolbarAddress::CommandSent(HWND hToolbar, INT commandId)
  389. {
  390. if (NULL != hEditor && ID_ADDRESSBAR_CHANGED != commandId)
  391. {
  392. if (GetFocus() == hEditor)
  393. EditboxNavigateNextCtrl(hEditor, TRUE);
  394. else
  395. DestroyWindow(hEditor);
  396. }
  397. }
  398. BOOL ToolbarAddress::SetValueStr(HWND hToolbar, LPCWSTR value)
  399. {
  400. Plugin_FreeResString(text);
  401. if (NULL != value)
  402. {
  403. if (FALSE != IS_INTRESOURCE(value) || FAILED(AddressEncoder_DecodeString(value, &text)))
  404. text = Plugin_DuplicateResString(value);
  405. }
  406. else
  407. text = NULL;
  408. RECT textRect;
  409. CopyRect(&textRect, &rect);
  410. InflateRect(&textRect, -((iconWidth -1)/2), -(iconHeight - 1)/2);
  411. InvalidateRect(hToolbar, &textRect, TRUE);
  412. return TRUE;
  413. }
  414. BOOL ToolbarAddress::SetClipboardText(HWND hToolbar, LPCWSTR pszText)
  415. {
  416. if(FALSE == OpenClipboard(hToolbar))
  417. return FALSE;
  418. EmptyClipboard();
  419. INT cchLen;
  420. HGLOBAL hMemory;
  421. cchLen = (NULL != pszText) ? lstrlenW(pszText) : 0;
  422. hMemory = (0 != cchLen) ? GlobalAlloc(GMEM_MOVEABLE, (cchLen + 1) * sizeof(WCHAR)) : NULL;
  423. if (NULL != hMemory)
  424. {
  425. LPWSTR pszDest = (LPWSTR)GlobalLock(hMemory);
  426. CopyMemory(pszDest, pszText, (cchLen + 1) * sizeof(WCHAR));
  427. GlobalUnlock(hMemory);
  428. }
  429. SetClipboardData(CF_UNICODETEXT, hMemory);
  430. LPSTR pszTextAnsi = (NULL != pszText) ? Plugin_WideCharToMultiByte(CP_ACP, 0, pszText, -1, NULL, NULL) : NULL;
  431. cchLen = (NULL != pszTextAnsi) ? lstrlenA(pszTextAnsi) : 0;
  432. hMemory = (0 != cchLen) ? GlobalAlloc(GMEM_MOVEABLE, (cchLen + 1) * sizeof(CHAR)) : NULL;
  433. if (NULL != hMemory)
  434. {
  435. LPWSTR pszDest = (LPWSTR)GlobalLock(hMemory);
  436. CopyMemory(pszDest, pszTextAnsi, (cchLen + 1) * sizeof(CHAR));
  437. GlobalUnlock(hMemory);
  438. }
  439. SetClipboardData(CF_TEXT, hMemory);
  440. CloseClipboard();
  441. return TRUE;
  442. }
  443. BOOL ToolbarAddress::DisplayContextMenu(HWND hToolbar, INT x, INT y)
  444. {
  445. HMENU hMenu = Menu_GetMenu(MENU_ADDRESSBAR, 0);
  446. if (NULL != hMenu)
  447. {
  448. UINT fEnable = MF_BYCOMMAND;
  449. if (0 != (styleAddressReadonly & style))
  450. fEnable |= (MF_DISABLED | MF_GRAYED);
  451. else
  452. fEnable |= MF_ENABLED;
  453. EnableMenuItem(hMenu, ID_ADDRESSBAR_EDITADDRESS, fEnable);
  454. INT commandId = Menu_TrackPopup(hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD, x, y, hToolbar, NULL);
  455. switch(commandId)
  456. {
  457. case ID_ADDRESSBAR_COPY:
  458. if (0 == (styleAddressShowReal & style) && NULL != description)
  459. {
  460. UINT cchTextMax = (IS_INTRESOURCE(description)) ? 2048 : (lstrlen(description) + 1);
  461. LPWSTR pszText = Plugin_MallocString(cchTextMax);
  462. if (NULL != pszText && FAILED(GetDescription(pszText, cchTextMax)))
  463. {
  464. Plugin_FreeString(pszText);
  465. pszText = NULL;
  466. }
  467. SetClipboardText(hToolbar, pszText);
  468. Plugin_FreeString(pszText);
  469. break;
  470. }
  471. // let it go
  472. case ID_ADDRESSBAR_COPYADDRESS:
  473. {
  474. size_t cchTextMax;
  475. if (SUCCEEDED(GetTextLength(&cchTextMax)))
  476. {
  477. cchTextMax++;
  478. LPWSTR pszText = Plugin_MallocString(cchTextMax);
  479. if (NULL != pszText && FAILED(GetText(pszText, (UINT)cchTextMax)))
  480. {
  481. Plugin_FreeString(pszText);
  482. pszText = NULL;
  483. }
  484. SetClipboardText(hToolbar, pszText);
  485. Plugin_FreeString(pszText);
  486. }
  487. }
  488. break;
  489. case ID_ADDRESSBAR_EDITADDRESS:
  490. ActivateEditor(hToolbar, NULL);
  491. break;
  492. }
  493. Menu_ReleaseMenu(hMenu, MENU_ADDRESSBAR);
  494. }
  495. return TRUE;
  496. }
  497. void ToolbarAddress::SetFocus(HWND hToolbar, ToolbarItem *focusItem, BOOL fSet)
  498. {
  499. ActivateEditor(hToolbar, NULL);
  500. }
  501. BOOL ToolbarAddress::SetCursor(HWND hToolbar, HWND hCursor, UINT hitTest, UINT messageId)
  502. {
  503. RECT textRect;
  504. CopyRect(&textRect, &rect);
  505. InflateRect(&textRect, -((iconWidth -1)/2), -(iconHeight - 1)/2);
  506. UINT messagePos = GetMessagePos();
  507. POINTS messagePts = MAKEPOINTS(messagePos);
  508. POINT messagePt;
  509. POINTSTOPOINT(messagePt, messagePts);
  510. MapWindowPoints(HWND_DESKTOP, hToolbar, &messagePt, 1);
  511. if (::PtInRect(&textRect, messagePt))
  512. {
  513. HCURSOR cursor = LoadCursor(NULL, IDC_IBEAM);
  514. if (NULL != cursor)
  515. {
  516. ::SetCursor(cursor);
  517. return TRUE;
  518. }
  519. }
  520. return FALSE;
  521. }
  522. void ToolbarAddress::EditboxDestroyed(HWND hwnd)
  523. {
  524. if (hEditor == hwnd)
  525. {
  526. hEditor = NULL;
  527. HWND hToolbar = GetParent(hwnd);
  528. if (NULL != hToolbar)
  529. {
  530. InvalidateRect(hToolbar, &rect, FALSE);
  531. Toolbar_CheckHide(hToolbar, TRUE);
  532. }
  533. }
  534. }
  535. BOOL ToolbarAddress::EditboxKillFocus(HWND hwnd, HWND hFocus)
  536. {
  537. DestroyWindow(hwnd);
  538. return TRUE;
  539. }
  540. void ToolbarAddress::EditboxResetText(HWND hwnd)
  541. {
  542. UINT editboxStyle = GetWindowStyle(hwnd);
  543. if (0 != (WS_VISIBLE & editboxStyle))
  544. SetWindowLongPtr(hwnd, GWL_STYLE, editboxStyle & ~WS_VISIBLE);
  545. SetWindowText(hwnd, text);
  546. SendMessage(hEditor, NTEBM_SELECTALL, 0, 0L);
  547. if (0 != (WS_VISIBLE & editboxStyle))
  548. {
  549. UINT editboxStyle = GetWindowStyle(hwnd);
  550. if (0 == (WS_VISIBLE & editboxStyle))
  551. SetWindowLongPtr(hwnd, GWL_STYLE, editboxStyle | WS_VISIBLE);
  552. InvalidateRect(hwnd, NULL, FALSE);
  553. }
  554. }
  555. void ToolbarAddress::EditboxNavigateNextCtrl(HWND hwnd, BOOL fForward)
  556. {
  557. HWND hToolbar = GetParent(hwnd);
  558. if (NULL != hToolbar)
  559. {
  560. INT itemIndex = Toolbar_GetNextTabItem(hToolbar, name, (FALSE == fForward));
  561. if (ITEM_ERR != itemIndex)
  562. {
  563. if (FALSE != Toolbar_NextItem(hToolbar, MAKEINTRESOURCE(itemIndex), TRUE))
  564. {
  565. ::SetFocus(hToolbar);
  566. return;
  567. }
  568. }
  569. HWND hParent = GetAncestor(hToolbar, GA_PARENT);
  570. while(NULL != hParent &&
  571. 0 != (WS_CHILD & GetWindowStyle(hParent)) &&
  572. 0 != (WS_EX_CONTROLPARENT & GetWindowStyleEx(hParent)))
  573. {
  574. HWND hTest = GetAncestor(hParent, GA_PARENT);
  575. if (NULL == hTest) break;
  576. hParent = hTest;
  577. }
  578. if (NULL != hParent)
  579. {
  580. HWND hFocus = GetNextDlgTabItem(hParent, hToolbar, (FALSE == fForward));
  581. if (NULL != hFocus) ::SetFocus(hFocus);
  582. }
  583. }
  584. }
  585. void ToolbarAddress::EditboxAcceptText(HWND hwnd)
  586. {
  587. UINT cchLenMax = GetWindowTextLength(hwnd);
  588. if (0 == cchLenMax) return;
  589. cchLenMax++;
  590. Plugin_FreeResString(text);
  591. text = Plugin_MallocString(cchLenMax);
  592. if (NULL != text)
  593. {
  594. INT cchText = GetWindowText(hwnd, text, cchLenMax);
  595. if (0 != cchText)
  596. {
  597. LPCWSTR begin, end;
  598. begin = text;
  599. end = text + cchText + 1;
  600. while (L' ' == *begin && begin != end) begin++;
  601. while (L' ' == *end && begin != end) end--;
  602. if (end <= begin) return;
  603. }
  604. }
  605. HWND hToolbar = GetParent(hwnd);
  606. if (NULL != hToolbar)
  607. {
  608. Toolbar_SendCommand(hToolbar, ID_ADDRESSBAR_CHANGED);
  609. }
  610. EditboxNavigateNextCtrl(hwnd, TRUE);
  611. }
  612. BOOL ToolbarAddress::EditboxKeyDown(HWND hwnd, UINT vKey, UINT state)
  613. {
  614. switch(vKey)
  615. {
  616. case VK_UP:
  617. case VK_DOWN:
  618. return TRUE;
  619. }
  620. return FALSE;
  621. }
  622. BOOL ToolbarAddress::EditboxKeyUp(HWND hwnd, UINT vKey, UINT state)
  623. {
  624. return FALSE;
  625. }
  626. BOOL ToolbarAddress::EditboxPreviewChar(HWND hwnd, UINT vKey, UINT state)
  627. {
  628. return FALSE;
  629. }