toolbarEditbox.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. #define OEMRESOURCE
  2. #include "main.h"
  3. #include "./toolbarEditbox.h"
  4. #include "./menu.h"
  5. #include "./ifc_skinhelper.h"
  6. #include "../Plugins/General/gen_ml/ml_ipc_0313.h"
  7. #include <richedit.h>
  8. #include <strsafe.h>
  9. #define NTEF_USERFLAGSMASK 0x00FFFFFF
  10. #define NTEF_UNICODE 0x01000000
  11. #define NTEF_CLICKEDONCE 0x02000000
  12. #define NTEF_SHOWCURSOR 0x04000000
  13. #define NTEF_KEYDOWN 0x08000000
  14. #define NTEF_MENULOOP 0x10000000
  15. #define NTEF_SHOWCARET 0x20000000
  16. typedef struct __TOOLBAREDITBOX
  17. {
  18. WNDPROC originalProc;
  19. UINT flags;
  20. ToolbarEditboxHost *host;
  21. DWORD dblclkTime;
  22. } TOOLBAREDITBOX;
  23. static ATOM TOOLBAREDITBOX_PROP = 0;
  24. static HHOOK mouseHook = NULL;
  25. #define GetEditbox(__hwnd) ((TOOLBAREDITBOX*)GetProp((__hwnd), MAKEINTATOM(TOOLBAREDITBOX_PROP)))
  26. static LRESULT CALLBACK ToolbarEditbox_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  27. static INT CALLBACK ToolbarEditbox_WordBreakProc(LPWSTR pszText, INT iCurrent, INT cchLen, INT code);
  28. static INT CALLBACK ToolbarEditbox_WordBreakProc2(LPWSTR pszText, INT iCurrent, INT cchLen, INT code);
  29. static void CALLBACK ToolbarEditbox_Uninitialize()
  30. {
  31. if (0 != TOOLBAREDITBOX_PROP)
  32. {
  33. GlobalDeleteAtom(TOOLBAREDITBOX_PROP);
  34. TOOLBAREDITBOX_PROP = 0;
  35. }
  36. }
  37. BOOL ToolbarEditbox_AttachWindow(HWND hEditbox, ToolbarEditboxHost *host)
  38. {
  39. if (!IsWindow(hEditbox))
  40. return FALSE;
  41. if (0 == TOOLBAREDITBOX_PROP)
  42. {
  43. TOOLBAREDITBOX_PROP = GlobalAddAtom(L"NullsoftToolbarEditBox");
  44. if (0 == TOOLBAREDITBOX_PROP) return FALSE;
  45. Plugin_RegisterUnloadCallback(ToolbarEditbox_Uninitialize);
  46. }
  47. TOOLBAREDITBOX *editbox = (TOOLBAREDITBOX*)GetProp(hEditbox, MAKEINTATOM(TOOLBAREDITBOX_PROP));
  48. if (NULL != editbox) return TRUE;
  49. editbox = (TOOLBAREDITBOX*)calloc(1, sizeof(TOOLBAREDITBOX));
  50. if (NULL == editbox) return FALSE;
  51. ifc_skinhelper *skinHelper = NULL;
  52. if (SUCCEEDED(Plugin_GetSkinHelper(&skinHelper)))
  53. {
  54. // skinHelper->SkinControl(hEditbox, SKINNEDWND_TYPE_EDIT, SWS_USESKINCOLORS | SWS_USESKINCURSORS | SWES_BOTTOM);
  55. skinHelper->Release();
  56. }
  57. ZeroMemory(editbox, sizeof(TOOLBAREDITBOX));
  58. if (IsWindowUnicode(hEditbox))
  59. editbox->flags |= NTEF_UNICODE;
  60. editbox->host = host;
  61. editbox->originalProc = (WNDPROC)(LONG_PTR)((0 != (NTEF_UNICODE & editbox->flags)) ?
  62. SetWindowLongPtrW(hEditbox, GWLP_WNDPROC, (LONGX86)(LONG_PTR)ToolbarEditbox_WindowProc) :
  63. SetWindowLongPtrA(hEditbox, GWLP_WNDPROC, (LONGX86)(LONG_PTR)ToolbarEditbox_WindowProc));
  64. if (NULL == editbox->originalProc || !SetProp(hEditbox, MAKEINTATOM(TOOLBAREDITBOX_PROP), editbox))
  65. {
  66. if (NULL != editbox->originalProc)
  67. {
  68. if (0 != (NTEF_UNICODE & editbox->flags))
  69. SetWindowLongPtrW(hEditbox, GWLP_WNDPROC, (LONGX86)(LONG_PTR)editbox->originalProc);
  70. else
  71. SetWindowLongPtrA(hEditbox, GWLP_WNDPROC, (LONGX86)(LONG_PTR)editbox->originalProc);
  72. }
  73. free(editbox);
  74. return FALSE;
  75. }
  76. SendMessage(hEditbox, EM_SETWORDBREAKPROC, 0, (LPARAM)ToolbarEditbox_WordBreakProc);
  77. return TRUE;
  78. }
  79. static void ToolbarEditbox_PatchCursor()
  80. {
  81. ShowCursor(FALSE);
  82. SetCursor(LoadCursor(NULL, IDC_ARROW));
  83. ShowCursor(TRUE);
  84. CURSORINFO cursorInfo;
  85. cursorInfo.cbSize = sizeof(CURSORINFO);
  86. if (GetCursorInfo(&cursorInfo) &&
  87. 0 != (CURSOR_SHOWING & cursorInfo.flags))
  88. {
  89. POINT pt;
  90. GetCursorPos(&pt);
  91. HWND hTarget= WindowFromPoint(pt);
  92. if (NULL != hTarget)
  93. {
  94. UINT hitTest = (UINT)SendMessage(hTarget, WM_NCHITTEST, 0, MAKELPARAM(pt.x, pt.y));
  95. UINT uMsg = (HTCLIENT == hitTest) ? WM_MOUSEMOVE : WM_NCMOUSEMOVE;
  96. SendMessage(hTarget, WM_SETCURSOR, (WPARAM)hTarget, MAKELPARAM(hitTest, uMsg));
  97. }
  98. }
  99. }
  100. static void ToolbarEditbox_Detach(HWND hwnd)
  101. {
  102. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  103. RemoveProp(hwnd, MAKEINTATOM(TOOLBAREDITBOX_PROP));
  104. ToolbarEditbox_PatchCursor();
  105. if (NULL != editbox && 0 != (NTEF_SHOWCURSOR & editbox->flags))
  106. {
  107. ShowCursor(TRUE);
  108. editbox->flags &= ~NTEF_SHOWCURSOR;
  109. }
  110. if (NULL != mouseHook)
  111. {
  112. UnhookWindowsHookEx(mouseHook);
  113. mouseHook = NULL;
  114. }
  115. if (NULL == editbox)
  116. return;
  117. if (NULL != editbox->originalProc)
  118. {
  119. if (0 != (NTEF_UNICODE & editbox->flags))
  120. SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONGX86)(LONG_PTR)editbox->originalProc);
  121. else
  122. SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONGX86)(LONG_PTR)editbox->originalProc);
  123. }
  124. free(editbox);
  125. }
  126. static LRESULT ToolbarEditbox_CallOrigWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  127. {
  128. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  129. if (NULL == editbox || NULL == editbox->originalProc)
  130. {
  131. return (0 != (NTEF_UNICODE & editbox->flags)) ?
  132. DefWindowProcW(hwnd, uMsg, wParam, lParam) :
  133. DefWindowProcA(hwnd, uMsg, wParam, lParam);
  134. }
  135. return (0 != (NTEF_UNICODE & editbox->flags)) ?
  136. CallWindowProcW(editbox->originalProc, hwnd, uMsg, wParam, lParam) :
  137. CallWindowProcA(editbox->originalProc, hwnd, uMsg, wParam, lParam);
  138. }
  139. static void ToolbarEditbox_ResetText(HWND hwnd)
  140. {
  141. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  142. if (NULL != editbox && NULL != editbox->host)
  143. editbox->host->EditboxResetText(hwnd);
  144. }
  145. static void ToolbarEditbox_NavigateNextCtrl(HWND hwnd, BOOL fForward)
  146. {
  147. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  148. if (NULL != editbox && NULL != editbox->host)
  149. editbox->host->EditboxNavigateNextCtrl(hwnd, fForward);
  150. }
  151. static void ToolbarEditbox_AcceptText(HWND hwnd)
  152. {
  153. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  154. if (NULL != editbox && NULL != editbox->host)
  155. editbox->host->EditboxAcceptText(hwnd);
  156. }
  157. static BOOL ToolbarEditbox_IsDelimiterChar(WCHAR testChar)
  158. {
  159. WORD info;
  160. if (FALSE == GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &testChar, 1, &info))
  161. return 0;
  162. return (0 != ((C1_SPACE | C1_PUNCT | C1_CNTRL | C1_BLANK) & info));
  163. }
  164. static INT ToolbarEditbox_FindLeft(LPCWSTR pszText, INT iCurrent, INT cchLen)
  165. {
  166. if (iCurrent <= 0)
  167. return 0;
  168. LPCWSTR pszCursor = &pszText[iCurrent];
  169. BOOL charDelim = ToolbarEditbox_IsDelimiterChar(*pszCursor);
  170. for(;;)
  171. {
  172. pszCursor = CharPrev(pszText, pszCursor);
  173. if (charDelim != ToolbarEditbox_IsDelimiterChar(*pszCursor))
  174. return (INT)(INT_PTR)(CharNext(pszCursor) - pszText);
  175. if (pszCursor == pszText)
  176. break;
  177. }
  178. return 0;
  179. }
  180. static INT ToolbarEditbox_FindRight(LPCWSTR pszText, INT iCurrent, INT cchLen)
  181. {
  182. if (iCurrent >= cchLen)
  183. return cchLen;
  184. LPCWSTR pszEnd = &pszText[cchLen];
  185. LPCWSTR pszCursor = &pszText[iCurrent];
  186. if (iCurrent > 0)
  187. pszCursor = CharNext(pszCursor);
  188. BOOL charDelim = ToolbarEditbox_IsDelimiterChar(*pszCursor);
  189. for(;;)
  190. {
  191. pszCursor = CharNext(pszCursor);
  192. if (pszCursor >= pszEnd)
  193. break;
  194. if (charDelim != ToolbarEditbox_IsDelimiterChar(*pszCursor))
  195. return (INT)(INT_PTR)(pszCursor - pszText);
  196. }
  197. return cchLen;
  198. }
  199. static INT ToolbarEditbox_FindWordLeft(LPCWSTR pszText, INT iCurrent, INT cchLen, BOOL fRightCtrl)
  200. {
  201. if (iCurrent < 2)
  202. return 0;
  203. LPCWSTR pszCursor = &pszText[iCurrent];
  204. if (FALSE == fRightCtrl)
  205. pszCursor = CharPrev(pszText, pszCursor);
  206. BOOL prevCharDelim = ToolbarEditbox_IsDelimiterChar(*pszCursor);
  207. for(;;)
  208. {
  209. pszCursor = CharPrev(pszText, pszCursor);
  210. if (TRUE == ToolbarEditbox_IsDelimiterChar(*pszCursor))
  211. {
  212. if (FALSE == prevCharDelim)
  213. return (INT)(INT_PTR)(CharNext(pszCursor) - pszText);
  214. prevCharDelim = TRUE;
  215. }
  216. else
  217. prevCharDelim = FALSE;
  218. if (pszCursor == pszText)
  219. break;
  220. }
  221. return 0;
  222. }
  223. static INT ToolbarEditbox_FindWordRight(LPCWSTR pszText, INT iCurrent, INT cchLen)
  224. {
  225. if ( iCurrent >= (cchLen - 1))
  226. return cchLen;
  227. LPCWSTR pszEnd = &pszText[cchLen];
  228. LPCWSTR pszCursor = &pszText[iCurrent];
  229. BOOL prevCharDelim = ToolbarEditbox_IsDelimiterChar(*pszCursor);
  230. for(;;)
  231. {
  232. pszCursor = CharNext(pszCursor);
  233. if (pszCursor >= pszEnd)
  234. break;
  235. if (prevCharDelim != ToolbarEditbox_IsDelimiterChar(*pszCursor))
  236. {
  237. prevCharDelim = TRUE;
  238. return (INT)(INT_PTR)(pszCursor - pszText);
  239. }
  240. else
  241. prevCharDelim = FALSE;
  242. }
  243. return cchLen;
  244. }
  245. static INT CALLBACK ToolbarEditbox_WordBreakProc(LPWSTR pszText, INT iCurrent, INT cchLen, INT code)
  246. {
  247. switch(code)
  248. {
  249. case WB_ISDELIMITER: return (iCurrent < 0) ? 0 : ((iCurrent > cchLen) ? (cchLen + 1) : ToolbarEditbox_IsDelimiterChar(pszText[iCurrent]));
  250. case WB_LEFT: return ToolbarEditbox_FindLeft(pszText, iCurrent, cchLen);
  251. case WB_RIGHT: return ToolbarEditbox_FindRight(pszText, iCurrent, cchLen);
  252. case WB_MOVEWORDLEFT: return ToolbarEditbox_FindWordLeft(pszText, iCurrent, cchLen, FALSE);
  253. case WB_MOVEWORDRIGHT: return ToolbarEditbox_FindWordRight(pszText, iCurrent, cchLen);
  254. }
  255. return 0;
  256. }
  257. static INT CALLBACK ToolbarEditbox_WordBreakProcOverrideLeft(LPWSTR pszText, INT iCurrent, INT cchLen, INT code)
  258. {
  259. switch(code)
  260. {
  261. case WB_LEFT: return ToolbarEditbox_FindWordLeft(pszText, iCurrent, cchLen, FALSE);
  262. case WB_RIGHT: return ToolbarEditbox_FindWordRight(pszText, iCurrent, cchLen);
  263. }
  264. return ToolbarEditbox_WordBreakProc(pszText, iCurrent, cchLen, code);
  265. }
  266. static INT CALLBACK ToolbarEditbox_WordBreakProcOverrideRight(LPWSTR pszText, INT iCurrent, INT cchLen, INT code)
  267. {
  268. switch(code)
  269. {
  270. case WB_LEFT: return ToolbarEditbox_FindWordLeft(pszText, iCurrent, cchLen, TRUE);
  271. case WB_RIGHT: return ToolbarEditbox_FindWordRight(pszText, iCurrent, cchLen);
  272. }
  273. return ToolbarEditbox_WordBreakProc(pszText, iCurrent, cchLen, code);
  274. }
  275. static void ToolbarEditbox_OnDestroy(HWND hwnd)
  276. {
  277. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  278. if (NULL != editbox && NULL != editbox->host)
  279. editbox->host->EditboxDestroyed(hwnd);
  280. WNDPROC originalProc = (editbox ? editbox->originalProc : NULL);
  281. BOOL fUnicode = (0 != (NTEF_UNICODE & editbox->flags));
  282. ToolbarEditbox_Detach(hwnd);
  283. if (NULL != originalProc)
  284. {
  285. if (FALSE != fUnicode)
  286. CallWindowProcW(originalProc, hwnd, WM_DESTROY, 0, 0L);
  287. else
  288. CallWindowProcA(originalProc, hwnd, WM_DESTROY, 0, 0L);
  289. }
  290. }
  291. static void ToolbarEditbox_OnSetFocus(HWND hwnd, HWND hFocus)
  292. {
  293. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  294. if (NULL != editbox)
  295. {
  296. editbox->flags &= ~NTEF_CLICKEDONCE;
  297. }
  298. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hFocus, 0L);
  299. }
  300. static void ToolbarEditbox_OnKillFocus(HWND hwnd, HWND hFocus)
  301. {
  302. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  303. if (NULL != editbox && NULL != editbox->host )
  304. {
  305. if (FALSE != editbox->host->EditboxKillFocus(hwnd, hFocus))
  306. return;
  307. }
  308. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hFocus, 0L);
  309. }
  310. static LRESULT ToolbarEditbox_OnGetDlgCode(HWND hwnd, INT vKey, MSG* pMsg)
  311. {
  312. LRESULT result = ToolbarEditbox_CallOrigWindowProc(hwnd, WM_GETDLGCODE, (WPARAM)vKey, (LPARAM)pMsg);
  313. result |= DLGC_WANTALLKEYS;
  314. result &= ~DLGC_HASSETSEL;
  315. return result;
  316. }
  317. static LRESULT ToolbarEditbox_OnSetCursor(HWND hwnd, HWND hOwner, INT hitTest, INT messageId)
  318. {
  319. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  320. if (HTCLIENT == hitTest &&
  321. NULL != editbox && 0 == (NTEF_MENULOOP & editbox->flags))
  322. {
  323. HCURSOR hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(OCR_IBEAM), IMAGE_CURSOR,
  324. 0, 0, LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE);
  325. if (NULL != hCursor)
  326. {
  327. SetCursor(hCursor);
  328. return TRUE;
  329. }
  330. }
  331. HWND hParent = GetParent(hwnd);
  332. if (NULL != hParent &&
  333. FALSE != (BOOL)SendMessage(hParent, WM_SETCURSOR, (WPARAM)hOwner, MAKELPARAM(hitTest, messageId)))
  334. {
  335. return TRUE;
  336. }
  337. return ToolbarEditbox_CallOrigWindowProc(hwnd, WM_SETCURSOR, (WPARAM)hOwner, MAKELPARAM(hitTest, messageId));
  338. }
  339. static void ToolbarEditbox_OnContextMenu(HWND hwnd, HWND hTarget, POINTS pts)
  340. {
  341. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  342. if (NULL != editbox) editbox->flags |= NTEF_MENULOOP;
  343. // SendMessage(hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELPARAM(HTCLIENT, WM_MOUSEMOVE));
  344. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_CONTEXTMENU, (WPARAM)hTarget, *((LPARAM*)&pts));
  345. if (NULL != editbox) editbox->flags &= ~NTEF_MENULOOP;
  346. }
  347. static void ToolbarEditbox_OnLButtonDown(HWND hwnd, UINT vKey, POINTS pts)
  348. {
  349. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  350. if (NULL != editbox)
  351. {
  352. if (0 != (NTEF_SHOWCARET & editbox->flags))
  353. {
  354. editbox->flags &= ~NTEF_SHOWCARET;
  355. ShowCaret(hwnd);
  356. }
  357. DWORD clickTime = GetTickCount();
  358. if (clickTime >= editbox->dblclkTime && clickTime <= (editbox->dblclkTime + GetDoubleClickTime()))
  359. {
  360. SendMessage(hwnd, NTEBM_SELECTALL, 0, 0L);
  361. return;
  362. }
  363. UINT start, end;
  364. SendMessage(hwnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
  365. if (start != end)
  366. {
  367. editbox->flags |= NTEF_CLICKEDONCE;
  368. }
  369. }
  370. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_LBUTTONDOWN, (WPARAM)vKey, *((LPARAM*)&pts));
  371. }
  372. static void ToolbarEditbox_OnLButtonUp(HWND hwnd, UINT vKey, POINTS pts)
  373. {
  374. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_LBUTTONUP, (WPARAM)vKey, *((LPARAM*)&pts));
  375. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  376. if (NULL != editbox && 0 == (NTEF_CLICKEDONCE & editbox->flags) && hwnd == GetFocus())
  377. {
  378. editbox->flags |= NTEF_CLICKEDONCE;
  379. UINT start, end;
  380. SendMessage(hwnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
  381. if (start == end)
  382. {
  383. SendMessage(hwnd, NTEBM_SELECTALL, 0, 0L);
  384. }
  385. }
  386. }
  387. static void ToolbarEditbox_OnLButtonDblClk(HWND hwnd, UINT vKey, POINTS pts)
  388. {
  389. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  390. if (NULL == editbox) return;
  391. DWORD clickTime = GetTickCount();
  392. if (clickTime >= editbox->dblclkTime && clickTime <= (editbox->dblclkTime + 2*GetDoubleClickTime()))
  393. {
  394. INT r = (INT)SendMessage(hwnd, EM_CHARFROMPOS, 0, *(LPARAM*)&pts);
  395. r = LOWORD(r);
  396. SendMessage(hwnd, EM_SETSEL, (WPARAM)r, (LPARAM)r);
  397. editbox->dblclkTime = 0;
  398. }
  399. else
  400. {
  401. editbox->dblclkTime = clickTime;
  402. }
  403. INT f, l;
  404. SendMessage(hwnd, EM_GETSEL, (WPARAM)&f, (LPARAM)&l);
  405. if (f != l) return;
  406. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_LBUTTONDBLCLK, (WPARAM)vKey, *((LPARAM*)&pts));
  407. }
  408. static void ToolbarEditbox_OnMouseMove(HWND hwnd, UINT vKey, POINTS pts)
  409. {
  410. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_MOUSEMOVE, (WPARAM)vKey, *((LPARAM*)&pts));
  411. if (0 != (MK_LBUTTON & vKey))
  412. {
  413. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  414. if (NULL != editbox && 0 == (NTEF_SHOWCARET & editbox->flags) &&
  415. hwnd == GetFocus())
  416. {
  417. UINT start, end;
  418. SendMessage(hwnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
  419. if (start != end)
  420. {
  421. if (FALSE != HideCaret(hwnd))
  422. editbox->flags |= NTEF_SHOWCARET;
  423. }
  424. }
  425. }
  426. }
  427. static LRESULT CALLBACK ToolbarEditbox_MouseHook(int nCode, WPARAM wParam, LPARAM lParam)
  428. {
  429. ToolbarEditbox_PatchCursor();
  430. TOOLBAREDITBOX *editbox = GetEditbox(GetFocus());
  431. if (NULL != editbox && 0 != (NTEF_SHOWCURSOR & editbox->flags))
  432. {
  433. ShowCursor(TRUE);
  434. editbox->flags &= ~NTEF_SHOWCURSOR;
  435. }
  436. LRESULT result = CallNextHookEx(mouseHook, nCode, wParam, lParam);
  437. UnhookWindowsHookEx(mouseHook);
  438. mouseHook = NULL;
  439. return result;
  440. }
  441. static void ToolbarEditbox_DeleteWord(HWND hwnd, UINT vKey, UINT state)
  442. {
  443. BOOL resetVisible = FALSE;
  444. INT first, last;
  445. SendMessage(hwnd, EM_GETSEL, (WPARAM)&first, (LPARAM)&last);
  446. if (first == last)
  447. {
  448. UINT windowStyle = GetWindowStyle(hwnd);
  449. if (0 != (WS_VISIBLE & windowStyle))
  450. {
  451. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle & ~WS_VISIBLE);
  452. resetVisible = TRUE;
  453. }
  454. SendMessage(hwnd, WM_KEYDOWN, (WPARAM)vKey, (LPARAM)state);
  455. INT newFirst, newLast;
  456. SendMessage(hwnd, EM_GETSEL, (WPARAM)&newFirst, (LPARAM)&newLast);
  457. if (newFirst != first || newLast != last)
  458. SendMessage(hwnd, EM_SETSEL, (WPARAM)first, (LPARAM)newLast);
  459. }
  460. SendMessage(hwnd, EM_REPLACESEL, TRUE, NULL);
  461. if (FALSE != resetVisible)
  462. {
  463. UINT windowStyle = GetWindowStyle(hwnd);
  464. if (0 == (WS_VISIBLE & windowStyle))
  465. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle | WS_VISIBLE);
  466. InvalidateRect(hwnd, NULL, FALSE);
  467. }
  468. }
  469. static void ToolbarEditbox_OnKeyDown(HWND hwnd, UINT vKey, UINT state)
  470. {
  471. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  472. if (NULL != editbox)
  473. {
  474. if (0 != (NTEF_SHOWCARET & editbox->flags))
  475. {
  476. editbox->flags &= ~NTEF_SHOWCARET;
  477. ShowCaret(hwnd);
  478. }
  479. editbox->flags |= NTEF_KEYDOWN;
  480. }
  481. if (NULL == editbox || NULL == editbox->host ||
  482. FALSE == editbox->host->EditboxKeyDown(hwnd, vKey, state))
  483. {
  484. EDITWORDBREAKPROC fnOrigBreak = NULL;
  485. if(0 != (0x8000 & GetAsyncKeyState(VK_CONTROL)))
  486. {
  487. switch(vKey)
  488. {
  489. case VK_LEFT:
  490. case VK_RIGHT:
  491. fnOrigBreak = (EDITWORDBREAKPROC)SendMessage(hwnd, EM_GETWORDBREAKPROC, 0, 0L);
  492. if (ToolbarEditbox_WordBreakProc == fnOrigBreak)
  493. {
  494. EDITWORDBREAKPROC fnOverride = (VK_LEFT == vKey) ?
  495. ToolbarEditbox_WordBreakProcOverrideLeft : ToolbarEditbox_WordBreakProcOverrideRight;
  496. SendMessage(hwnd, EM_SETWORDBREAKPROC, 0, (LPARAM)fnOverride);
  497. }
  498. break;
  499. case VK_DELETE:
  500. ToolbarEditbox_DeleteWord(hwnd, VK_RIGHT, state);
  501. return;
  502. case VK_BACK:
  503. ToolbarEditbox_DeleteWord(hwnd, VK_LEFT, state);
  504. return;
  505. }
  506. }
  507. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_KEYDOWN, (WPARAM)vKey, (LPARAM)state);
  508. if (NULL != fnOrigBreak)
  509. SendMessage(hwnd, EM_SETWORDBREAKPROC, 0, (LPARAM)fnOrigBreak);
  510. }
  511. if (('0' <= vKey && vKey <= '9') ||
  512. ('A' <= vKey && vKey <= 'Z') ||
  513. (VK_NUMPAD0 <= vKey && vKey <= VK_DIVIDE) ||
  514. (VK_OEM_1 <= vKey && vKey <= VK_OEM_3) ||
  515. (VK_OEM_4 <= vKey && vKey <= VK_OEM_8) ||
  516. VK_OEM_NEC_EQUAL == vKey ||
  517. VK_OEM_102 == vKey)
  518. {
  519. if (0 == (0x8000 & GetAsyncKeyState(VK_CONTROL)) &&
  520. 0 == (0x8000 & GetAsyncKeyState(VK_MENU)))
  521. {
  522. PostMessage(hwnd, NTEBM_UPDATECURSOR, 0, 0L);
  523. }
  524. }
  525. }
  526. static void ToolbarEditbox_OnKeyUp(HWND hwnd, UINT vKey, UINT state)
  527. {
  528. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  529. if (NULL == editbox || NULL == editbox->host ||
  530. FALSE == editbox->host->EditboxKeyUp(hwnd, vKey, state))
  531. {
  532. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_KEYUP, (WPARAM)vKey, (LPARAM)state);
  533. }
  534. if (NULL != editbox)
  535. editbox->flags &= ~NTEF_KEYDOWN;
  536. if (NULL == mouseHook)
  537. {
  538. mouseHook = SetWindowsHookEx(WH_MOUSE, ToolbarEditbox_MouseHook, NULL, GetCurrentThreadId());
  539. }
  540. }
  541. static void ToolbarEditbox_OnChar(HWND hwnd, UINT vKey, UINT state)
  542. {
  543. BOOL fControl = FALSE;
  544. if (0 != (0x8000 & GetAsyncKeyState(VK_CONTROL)))
  545. {
  546. fControl = TRUE;
  547. UINT scanCode = (HIWORD(state) & 0x00FF);
  548. vKey = MapVirtualKey(scanCode, MAPVK_VSC_TO_VK);
  549. }
  550. switch(vKey)
  551. {
  552. case VK_ESCAPE: ToolbarEditbox_ResetText(hwnd); return;
  553. case VK_TAB: ToolbarEditbox_NavigateNextCtrl(hwnd, 0 == (0x8000 & GetAsyncKeyState(VK_SHIFT))); return;
  554. case VK_RETURN: ToolbarEditbox_AcceptText(hwnd); return;
  555. }
  556. if (FALSE != fControl)
  557. {
  558. switch(vKey)
  559. {
  560. case 'A': SendMessage(hwnd, NTEBM_SELECTALL, 0, 0L); return;
  561. case 'Z': SendMessage(hwnd, EM_UNDO, 0, 0L); return;
  562. case 'X': SendMessage(hwnd, WM_CUT, 0, 0L); return;
  563. case 'C': SendMessage(hwnd, WM_COPY, 0, 0L); return;
  564. case 'V': SendMessage(hwnd, WM_PASTE, 0, 0L); return;
  565. }
  566. return;
  567. }
  568. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  569. if (NULL != editbox && NULL != editbox->host &&
  570. FALSE != editbox->host->EditboxPreviewChar(hwnd, vKey, state))
  571. {
  572. return;
  573. }
  574. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_CHAR, (WPARAM)vKey, (LPARAM)state);
  575. }
  576. static void ToolbarEditbox_OnUpdateCursor(HWND hwnd)
  577. {
  578. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  579. if (NULL != editbox && 0 != (NTEF_KEYDOWN & editbox->flags))
  580. {
  581. if (NULL != mouseHook)
  582. {
  583. UnhookWindowsHookEx(mouseHook);
  584. mouseHook = NULL;
  585. }
  586. if (0 == (NTEF_SHOWCURSOR & editbox->flags))
  587. {
  588. CURSORINFO cursorInfo;
  589. cursorInfo.cbSize = sizeof(CURSORINFO);
  590. if (GetCursorInfo(&cursorInfo) && 0 != (CURSOR_SHOWING & cursorInfo.flags))
  591. {
  592. ShowCursor(FALSE);
  593. editbox->flags |= NTEF_SHOWCURSOR;
  594. }
  595. }
  596. }
  597. }
  598. static void ToolbarEditbox_OnEnterMenuLoop(HWND hwnd, BOOL fContext)
  599. {
  600. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  601. if (NULL != editbox) editbox->flags |= NTEF_MENULOOP;
  602. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_ENTERMENULOOP, (WPARAM)fContext, 0L);
  603. }
  604. static void ToolbarEditbox_OnExitMenuLoop(HWND hwnd, BOOL fContext)
  605. {
  606. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  607. if (NULL != editbox) editbox->flags &= ~NTEF_MENULOOP;
  608. ToolbarEditbox_CallOrigWindowProc(hwnd, WM_EXITMENULOOP, (WPARAM)fContext, 0L);
  609. }
  610. static BOOL ToolbarEditbox_RemoveBadChars(LPCWSTR pszText, LPWSTR *bufferOut)
  611. {
  612. LPWSTR buffer = NULL;
  613. if (NULL == pszText) return FALSE;
  614. const WCHAR szBadChars[] = { L'\r', L'\n', L'\t', L'\0'};
  615. BOOL fDetected = FALSE;
  616. for (LPCWSTR p = pszText; L'\0' != *p && FALSE == fDetected; p++)
  617. {
  618. for (LPCWSTR b = szBadChars; L'\0' != *b; b++)
  619. {
  620. if (*p == *b)
  621. {
  622. fDetected = TRUE;
  623. break;
  624. }
  625. }
  626. }
  627. if (FALSE == fDetected)
  628. return FALSE;
  629. if (NULL == bufferOut)
  630. return TRUE;
  631. INT cchText = lstrlen(pszText);
  632. buffer = Plugin_MallocString(cchText + 1);
  633. if (NULL == buffer) return FALSE;
  634. LPCWSTR s = pszText;
  635. LPWSTR d = buffer;
  636. LPCWSTR b;
  637. for(;;)
  638. {
  639. for (b = szBadChars; L'\0' != *b && *s != *b; b++);
  640. if(L'\0' != *b)
  641. {
  642. if (L'\t' == *b)
  643. {
  644. *d = L' ';
  645. d++;
  646. }
  647. }
  648. else
  649. {
  650. *d = *s;
  651. d++;
  652. }
  653. if (L'\0' == *s)
  654. break;
  655. s++;
  656. }
  657. *bufferOut = buffer;
  658. return TRUE;
  659. }
  660. static LRESULT ToolbarEditbox_OnSetText(HWND hwnd, LPCWSTR pszText)
  661. {
  662. LPWSTR buffer;
  663. if (FALSE == ToolbarEditbox_RemoveBadChars(pszText, &buffer))
  664. buffer = NULL;
  665. else
  666. pszText = buffer;
  667. LRESULT result = ToolbarEditbox_CallOrigWindowProc(hwnd, WM_SETTEXT, 0, (LPARAM)pszText);
  668. if (NULL != buffer)
  669. Plugin_FreeString(buffer);
  670. return result;
  671. }
  672. static LRESULT ToolbarEditbox_OnReplaceSel(HWND hwnd, BOOL fUndo, LPCWSTR pszText)
  673. {
  674. LPWSTR buffer;
  675. if (FALSE == ToolbarEditbox_RemoveBadChars(pszText, &buffer))
  676. buffer = NULL;
  677. else
  678. pszText = buffer;
  679. LRESULT result = ToolbarEditbox_CallOrigWindowProc(hwnd, EM_REPLACESEL, (WPARAM)fUndo, (LPARAM)pszText);
  680. if (NULL != buffer)
  681. Plugin_FreeString(buffer);
  682. return result;
  683. }
  684. static void ToolbarEditbox_ReplaceText(HWND hwnd, LPCWSTR pszText, BOOL fUndo, BOOL fScrollCaret)
  685. {
  686. UINT windowStyle = GetWindowStyle(hwnd);
  687. if (0 != (WS_VISIBLE & windowStyle))
  688. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle & ~WS_VISIBLE);
  689. SendMessage(hwnd, EM_REPLACESEL, (WPARAM)fUndo, (LPARAM)pszText);
  690. if (FALSE != fScrollCaret)
  691. {
  692. INT f, l;
  693. SendMessage(hwnd, EM_GETSEL, (WPARAM)&f, (LPARAM)&l);
  694. SendMessage(hwnd, EM_SETSEL, (WPARAM)f, (LPARAM)l);
  695. }
  696. if (0 != (WS_VISIBLE & windowStyle))
  697. {
  698. windowStyle = GetWindowStyle(hwnd);
  699. if (0 == (WS_VISIBLE & windowStyle))
  700. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle | WS_VISIBLE);
  701. InvalidateRect(hwnd, NULL, FALSE);
  702. }
  703. }
  704. static void ToolbarEditbox_OnPaste(HWND hwnd)
  705. {
  706. IDataObject *pObject;
  707. HRESULT hr = OleGetClipboard(&pObject);
  708. if (SUCCEEDED(hr))
  709. {
  710. FORMATETC fmt = {CF_UNICODETEXT, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  711. STGMEDIUM stgm;
  712. hr = pObject->GetData(&fmt, &stgm);
  713. if(S_OK == hr)
  714. {
  715. LPCWSTR pClipboard = (LPCWSTR)GlobalLock(stgm.hGlobal);
  716. ToolbarEditbox_ReplaceText(hwnd, pClipboard, TRUE, TRUE);
  717. GlobalUnlock(stgm.hGlobal);
  718. ReleaseStgMedium(&stgm);
  719. }
  720. else
  721. {
  722. fmt.cfFormat = CF_TEXT;
  723. hr = pObject->GetData(&fmt, &stgm);
  724. if(S_OK == hr)
  725. {
  726. LPCSTR pClipboardAnsi = (LPCSTR)GlobalLock(stgm.hGlobal);
  727. LPWSTR pClipboard = Plugin_MultiByteToWideChar(CP_ACP, 0, pClipboardAnsi, -1);
  728. ToolbarEditbox_ReplaceText(hwnd, pClipboard, TRUE, TRUE);
  729. Plugin_FreeString(pClipboard);
  730. GlobalUnlock(stgm.hGlobal);
  731. ReleaseStgMedium(&stgm);
  732. }
  733. }
  734. pObject->Release();
  735. }
  736. }
  737. static void ToolbarEditbox_OnSelectAll(HWND hwnd)
  738. {
  739. UINT windowStyle = GetWindowStyle(hwnd);
  740. INT f, l, tl;
  741. tl = GetWindowTextLength(hwnd);
  742. SendMessage(hwnd, EM_GETSEL, (WPARAM)&f, (LPARAM)&l);
  743. if (f == 0 && tl == l)
  744. return;
  745. if (0 != (WS_VISIBLE & windowStyle))
  746. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle & ~WS_VISIBLE);
  747. RECT rc;
  748. GetWindowRect(hwnd, &rc);
  749. SetWindowPos(hwnd, NULL, 0, 0, 0xFFFFF, rc.bottom - rc.top,
  750. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSENDCHANGING | SWP_NOCOPYBITS);
  751. SendMessage(hwnd, EM_SETSEL, 0, -1);
  752. SetWindowPos(hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top,
  753. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSENDCHANGING | SWP_NOCOPYBITS);
  754. if (0 != (WS_VISIBLE & windowStyle))
  755. {
  756. UINT windowStyle = GetWindowStyle(hwnd);
  757. if (0 == (WS_VISIBLE & windowStyle))
  758. {
  759. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle | WS_VISIBLE);
  760. InvalidateRect(hwnd, NULL, FALSE);
  761. }
  762. }
  763. TOOLBAREDITBOX *editbox = GetEditbox(hwnd);
  764. if (NULL != editbox &&
  765. 0 == (NTEF_SHOWCARET & editbox->flags) &&
  766. FALSE != HideCaret(hwnd))
  767. {
  768. editbox->flags |= NTEF_SHOWCARET;
  769. }
  770. }
  771. static LRESULT ToolbarEditbox_OnFindWordBreak(HWND hwnd, INT code, INT start)
  772. {
  773. EDITWORDBREAKPROC fnBreak = (EDITWORDBREAKPROC)SendMessage(hwnd, EM_GETWORDBREAKPROC, 0, 0L);
  774. if (NULL == fnBreak) return 0;
  775. UINT cchText = GetWindowTextLength(hwnd);
  776. if (0 == cchText) return 0;
  777. LPWSTR pszText = Plugin_MallocString(cchText + 1);
  778. if (NULL == pszText) return 0;
  779. LRESULT result = 0;
  780. cchText = GetWindowText(hwnd, pszText, cchText + 1);
  781. if (0 != cchText)
  782. {
  783. result = fnBreak(pszText, start, cchText, code);
  784. }
  785. Plugin_FreeString(pszText);
  786. return result;
  787. }
  788. static LRESULT CALLBACK ToolbarEditbox_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  789. {
  790. switch(uMsg)
  791. {
  792. case WM_DESTROY: ToolbarEditbox_OnDestroy(hwnd); return 0;
  793. case WM_SETFOCUS: ToolbarEditbox_OnSetFocus(hwnd, (HWND)wParam); return 0;
  794. case WM_KILLFOCUS: ToolbarEditbox_OnKillFocus(hwnd, (HWND)wParam); return 0;
  795. case WM_GETDLGCODE: return ToolbarEditbox_OnGetDlgCode(hwnd, (INT)wParam, (MSG*)lParam);
  796. case WM_SETCURSOR: return ToolbarEditbox_OnSetCursor(hwnd, (HWND)wParam, (INT)LOWORD(lParam), (INT)HIWORD(lParam));
  797. case WM_CONTEXTMENU: ToolbarEditbox_OnContextMenu(hwnd, (HWND)wParam, MAKEPOINTS(lParam)); return 0;
  798. case WM_LBUTTONDOWN: ToolbarEditbox_OnLButtonDown(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); return 0;
  799. case WM_LBUTTONUP: ToolbarEditbox_OnLButtonUp(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); return 0;
  800. case WM_LBUTTONDBLCLK: ToolbarEditbox_OnLButtonDblClk(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); return 0;
  801. case WM_MOUSEMOVE: ToolbarEditbox_OnMouseMove(hwnd, (UINT)wParam, MAKEPOINTS(lParam)); return 0;
  802. case WM_KEYDOWN: ToolbarEditbox_OnKeyDown(hwnd, (UINT)wParam, (UINT)lParam); return 0;
  803. case WM_KEYUP: ToolbarEditbox_OnKeyUp(hwnd, (UINT)wParam, (UINT)lParam); return 0;
  804. case WM_CHAR: ToolbarEditbox_OnChar(hwnd, (UINT)wParam, (UINT)lParam); return 0;
  805. case WM_ENTERMENULOOP: ToolbarEditbox_OnEnterMenuLoop(hwnd, (BOOL)wParam); return 0;
  806. case WM_EXITMENULOOP: ToolbarEditbox_OnExitMenuLoop(hwnd, (BOOL)wParam); return 0;
  807. case WM_SETTEXT: return ToolbarEditbox_OnSetText(hwnd, (LPCWSTR)lParam);
  808. case WM_PASTE: ToolbarEditbox_OnPaste(hwnd); return 1;
  809. case EM_REPLACESEL: ToolbarEditbox_OnReplaceSel(hwnd, (BOOL)wParam, (LPCWSTR)lParam); return 0;
  810. case EM_FINDWORDBREAK: return ToolbarEditbox_OnFindWordBreak(hwnd, (INT)wParam, (INT)lParam);
  811. case NTEBM_UPDATECURSOR: ToolbarEditbox_OnUpdateCursor(hwnd); return 0;
  812. case NTEBM_SELECTALL: ToolbarEditbox_OnSelectAll(hwnd); return 0;
  813. }
  814. return ToolbarEditbox_CallOrigWindowProc(hwnd, uMsg, wParam, lParam);
  815. }