folderborwser_listbox.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include "./folderbrowser.h"
  2. #include "./folderbrowser_internal.h"
  3. #include "../nu/CGlobalAtom.h"
  4. static CGlobalAtom NAVMGR_FBLISTBOXW(L"FBLISTBOX");
  5. extern size_t FolderBrowser_GetListBoxColumn(HWND hwndList);
  6. static LRESULT CALLBACK FBListBox_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  7. typedef struct _FBMULTISELECT
  8. {
  9. HWND hOwner;
  10. INT nStart;
  11. INT nTrack;
  12. POINTS ptStart;
  13. } FBMULTISELECT;
  14. static HWND g_hDragSource = NULL;
  15. static FBMULTISELECT g_MultiSelect = {NULL, };
  16. BOOL FolderBrowser_CustomizeListBox(HWND hwndListbox)
  17. {
  18. LONG_PTR oldProc = GetWindowLongPtrW(hwndListbox, GWLP_WNDPROC);
  19. if (!oldProc || !SetPropW(hwndListbox, NAVMGR_FBLISTBOXW, (HANDLE)oldProc)) return FALSE;
  20. SetWindowLongPtrW(hwndListbox, GWLP_WNDPROC, (LONGX86)(LONG_PTR)FBListBox_WindowProc);
  21. return TRUE;
  22. }
  23. static void ResetMultiSelect(FBMULTISELECT *pms)
  24. {
  25. pms->hOwner = NULL;
  26. pms->nTrack = -1;
  27. pms->nStart = -1;
  28. pms->ptStart.x = -1;
  29. pms->ptStart.y = -1;
  30. }
  31. static void EnsureFocused(HWND hwnd)
  32. {
  33. HWND hFocus = GetFocus();
  34. if (hwnd != hFocus)
  35. {
  36. SetFocus(hwnd);
  37. HWND hParent = GetParent(hwnd);
  38. if (NULL != hParent)
  39. SendMessageW(hParent, WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(hwnd), LBN_SETFOCUS), (LPARAM)hwnd);
  40. }
  41. }
  42. static void EmulateSelectionChanged(HWND hwnd, INT caretIndex, INT ctrlId)
  43. {
  44. HWND hParent = GetParent(hwnd);
  45. SendMessageW(hwnd, LB_SETCARETINDEX, (WPARAM)caretIndex, TRUE);
  46. if (NULL != hParent)
  47. SendMessageW(hParent, WM_COMMAND, MAKEWPARAM(ctrlId, LBN_SELCHANGE), (LPARAM)hwnd);
  48. }
  49. static BOOL FBListBox_OnLButtonDown(HWND hwnd, UINT uFlags, POINTS pts)
  50. {
  51. g_hDragSource = NULL;
  52. ResetMultiSelect(&g_MultiSelect);
  53. INT count = (INT)SendMessageW(hwnd, LB_GETCOUNT, 0, 0L);
  54. if (count > 0)
  55. {
  56. DWORD item = (DWORD)SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, *((LPARAM*)&pts));
  57. if (HIWORD(item))
  58. {
  59. if (0 == (MK_SHIFT & uFlags))
  60. {
  61. EnsureFocused(hwnd);
  62. SendMessageW(hwnd, LB_SETSEL, FALSE, (LPARAM)-1);
  63. EmulateSelectionChanged(hwnd, (INT)SendMessageW(hwnd, LB_GETCARETINDEX, 0, 0L), GetDlgCtrlID(hwnd));
  64. }
  65. g_MultiSelect.hOwner = hwnd;
  66. g_MultiSelect.nStart = LOWORD(item);
  67. g_MultiSelect.nTrack = item;
  68. g_MultiSelect.ptStart = pts;
  69. SetCapture(hwnd);
  70. return TRUE;
  71. }
  72. item = LOWORD(item);
  73. INT sel = (INT)SendMessageW(hwnd, LB_GETSEL, item, 0L);
  74. if (sel > 0)
  75. {
  76. EnsureFocused(hwnd);
  77. g_hDragSource = hwnd;
  78. return TRUE;
  79. }
  80. RECT rc;
  81. if (SendMessageW(hwnd, LB_GETITEMRECT, (WPARAM)item, (LPARAM)&rc))
  82. {
  83. MEASUREITEMSTRUCT mis;
  84. ZeroMemory(&mis, sizeof(MEASUREITEMSTRUCT));
  85. mis.CtlID = GetDlgCtrlID(hwnd);
  86. mis.CtlType = ODT_LISTBOX;
  87. mis.itemID = item;
  88. mis.itemData = (DWORD)SendMessageW(hwnd, LB_GETITEMDATA, (WPARAM)item, 0L);
  89. if (SendMessageW(GetParent(hwnd), WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis))
  90. {
  91. rc.right = rc.left + mis.itemWidth;
  92. POINT pt;
  93. POINTSTOPOINT(pt, pts);
  94. if (PtInRect(&rc, pt))
  95. {
  96. EnsureFocused(hwnd);
  97. if (0 == ((MK_CONTROL | MK_SHIFT) & uFlags)) SendMessageW(hwnd, LB_SETSEL, FALSE, (LPARAM)-1);
  98. if (MK_SHIFT & uFlags)
  99. {
  100. INT anchor = (INT)SendMessageW(hwnd, LB_GETANCHORINDEX, 0, 0L);
  101. INT start = (anchor < (INT)item) ? anchor : item;
  102. INT stop = (start == anchor) ? item : anchor;
  103. SendMessageW(hwnd, LB_SETSEL, FALSE, (LPARAM)-1);
  104. SendMessageW(hwnd, LB_SELITEMRANGEEX, (WPARAM)start, (LPARAM)stop);
  105. SendMessageW(hwnd, LB_SETANCHORINDEX, (WPARAM)anchor, 0L);
  106. }
  107. else
  108. {
  109. SendMessageW(hwnd, LB_SETSEL, (0 == (MK_CONTROL & uFlags) || 0 == sel), (LPARAM)item);
  110. }
  111. EmulateSelectionChanged(hwnd, item, mis.CtlID);
  112. return TRUE;
  113. }
  114. }
  115. }
  116. }
  117. else
  118. {
  119. EnsureFocused(hwnd);
  120. return TRUE;
  121. }
  122. return FALSE;
  123. }
  124. static BOOL FBListBox_OnLButtonUp(HWND hwnd, UINT uFlags, POINTS pts)
  125. {
  126. ResetMultiSelect(&g_MultiSelect);
  127. if (hwnd == GetCapture())
  128. ReleaseCapture();
  129. INT count = (INT)SendMessageW(hwnd, LB_GETCOUNT, 0, 0L);
  130. if (count > 0)
  131. {
  132. DWORD item = (DWORD)SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, *((LPARAM*)&pts));
  133. if (HIWORD(item)) return FALSE;
  134. item = LOWORD(item);
  135. INT sel = (INT)SendMessageW(hwnd, LB_GETSEL, item, 0L);
  136. if (sel > 0 && hwnd == g_hDragSource)
  137. {
  138. if ((INT)SendMessageW(hwnd, LB_GETSELCOUNT, item, 0L) > 0)
  139. {
  140. SendMessageW(hwnd, LB_SETSEL, FALSE, (LPARAM)-1);
  141. SendMessageW(hwnd, LB_SETSEL, TRUE, (LPARAM)item);
  142. EmulateSelectionChanged(hwnd, item, GetDlgCtrlID(hwnd));
  143. }
  144. }
  145. }
  146. g_hDragSource = NULL;
  147. return FALSE;
  148. }
  149. static BOOL FBListBox_OnMouseMove(HWND hwnd, UINT uFlags, POINTS pts)
  150. {
  151. if (g_hDragSource == hwnd) return TRUE;
  152. if (g_MultiSelect.hOwner == hwnd)
  153. {
  154. RECT rc;
  155. if (GetClientRect(hwnd, &rc))
  156. pts.x = (SHORT)(rc.left + (rc.right - rc.left)/2);
  157. DWORD item = (DWORD)SendMessageW(hwnd, LB_ITEMFROMPOINT, 0, *((LPARAM*)&pts));
  158. BOOL bNotify = FALSE;
  159. if (item != g_MultiSelect.nTrack)
  160. {
  161. INT nCurrent = LOWORD(item);
  162. INT nTrack = LOWORD(g_MultiSelect.nTrack);
  163. if (abs(nCurrent - g_MultiSelect.nStart) < abs(nTrack - g_MultiSelect.nStart))
  164. {
  165. INT first = (nTrack < g_MultiSelect.nStart) ? g_MultiSelect.nStart : nTrack;
  166. INT last = (first == nTrack) ? g_MultiSelect.nStart : nTrack;
  167. LRESULT r = SendMessageW(hwnd, LB_SELITEMRANGEEX, (WPARAM)first, (LPARAM)last);
  168. if (!bNotify) bNotify = (LB_ERR != r);
  169. }
  170. if (nCurrent != g_MultiSelect.nStart)
  171. {
  172. INT first = (nCurrent > g_MultiSelect.nStart) ? g_MultiSelect.nStart : nCurrent;
  173. INT last = (first == nCurrent) ? g_MultiSelect.nStart : nCurrent;
  174. LRESULT r = SendMessageW(hwnd, LB_SELITEMRANGEEX, (WPARAM)first, (LPARAM)last);
  175. if (!bNotify) bNotify = (LB_ERR != r);
  176. }
  177. else if (0 == HIWORD(item))
  178. {
  179. LRESULT r = SendMessageW(hwnd, LB_SETSEL, TRUE, (LPARAM)nCurrent);
  180. if (!bNotify) bNotify = (LB_ERR != r);
  181. }
  182. g_MultiSelect.nTrack = (INT)item;
  183. }
  184. else if (0 != HIWORD(g_MultiSelect.nTrack) && 0 != HIWORD(item) &&
  185. LOWORD(item) == g_MultiSelect.nStart &&
  186. (INT)SendMessageW(hwnd, LB_GETSEL, (WPARAM)g_MultiSelect.nStart, 0L) > 0)
  187. {
  188. BOOL bReset = TRUE;
  189. if (1 == (INT)SendMessageW(hwnd, LB_GETCOUNT, 0, 0L))
  190. {
  191. RECT ri;
  192. if (LB_ERR != SendMessageW(hwnd, LB_GETITEMRECT, (WPARAM)g_MultiSelect.nStart, (LPARAM)&ri))
  193. {
  194. LONG t = (pts.y < g_MultiSelect.ptStart.y) ? pts.y : g_MultiSelect.ptStart.y;
  195. LONG b = (t != pts.y) ? pts.y : g_MultiSelect.ptStart.y;
  196. bReset = !(t < ri.bottom && b > ri.top);
  197. }
  198. }
  199. if (bReset)
  200. {
  201. LRESULT r = SendMessageW(hwnd, LB_SETSEL, FALSE, (LPARAM)g_MultiSelect.nStart);
  202. if (!bNotify) bNotify = (LB_ERR != r);
  203. }
  204. }
  205. if (bNotify)
  206. {
  207. HWND hParent = GetParent(hwnd);
  208. if (NULL != hParent)
  209. SendMessageW(hParent, WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(hwnd), LBN_SELCHANGE), (LPARAM)hwnd);
  210. }
  211. }
  212. return FALSE;
  213. }
  214. static BOOL FBListBox_OnMouseWheel(HWND hwnd, UINT vkCode, INT delta, POINTS pts)
  215. {
  216. if (MK_CONTROL == vkCode)
  217. {
  218. PostMessageW(GetParent(hwnd), WM_MOUSEWHEEL, MAKEWPARAM(vkCode, delta), MAKELPARAM(pts.x, pts.y));
  219. return TRUE;
  220. }
  221. return FALSE;
  222. }
  223. static BOOL FBListBox_OnKeyDown(HWND hwnd, UINT vkCode, UINT flags)
  224. {
  225. UINT uiState = 0;
  226. switch(vkCode)
  227. {
  228. case VK_CONTROL:
  229. case VK_SHIFT:
  230. return FALSE;
  231. case VK_MENU:
  232. uiState = (UINT)SendMessageW(hwnd, WM_QUERYUISTATE, 0, 0L);
  233. if (UISF_HIDEACCEL & uiState)
  234. SendMessageW(hwnd, WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR, UISF_HIDEACCEL), 0L);
  235. return FALSE;
  236. }
  237. uiState = (UINT)SendMessageW(hwnd, WM_QUERYUISTATE, 0, 0L);
  238. if (UISF_HIDEFOCUS & uiState)
  239. {
  240. SendMessageW(hwnd, WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR, UISF_HIDEFOCUS), 0L);
  241. }
  242. if (0 != (0x80000000 & GetAsyncKeyState(VK_CONTROL)))
  243. {
  244. switch(vkCode)
  245. {
  246. case VK_UP:
  247. case VK_DOWN:
  248. case VK_PRIOR:
  249. case VK_NEXT:
  250. case VK_END:
  251. case VK_HOME:
  252. {
  253. INT count = (INT)SendMessageW(hwnd, LB_GETCOUNT, 0, 0L);
  254. INT caret = (INT)SendMessageW(hwnd, LB_GETCARETINDEX, 0, 0L);
  255. INT caretNew = caret;
  256. INT page = 0;
  257. if (count > 0)
  258. {
  259. RECT rc;
  260. INT h = (INT)SendMessageW(hwnd, LB_GETITEMHEIGHT, 0, 0L);
  261. if (h != -1 && GetClientRect(hwnd, &rc))
  262. {
  263. page = (rc.bottom - rc.top)/(h) - 1;
  264. if (page < 1) page = 1;
  265. }
  266. }
  267. switch(vkCode)
  268. {
  269. case VK_UP: caretNew = caret - 1; break;
  270. case VK_DOWN: caretNew = caret + 1; break;
  271. case VK_PRIOR: caretNew = caret - page; break;
  272. case VK_NEXT: caretNew = caret + page; break;
  273. case VK_END: caretNew = count - 1; break;
  274. case VK_HOME: caretNew = 0; break;
  275. }
  276. if (caretNew < 0) caretNew = 0;
  277. if (caretNew >= count) caretNew = count - 1;
  278. if (caret != caretNew && -1 != caretNew)
  279. SendMessageW(hwnd, LB_SETCARETINDEX, (WPARAM)caretNew, FALSE);
  280. }
  281. return TRUE;
  282. case VK_SPACE:
  283. {
  284. INT caret = (INT)SendMessageW(hwnd, LB_GETCARETINDEX, 0, 0L);
  285. if (-1 != caret)
  286. {
  287. INT selected = (INT)SendMessageW(hwnd, LB_GETSEL, caret, 0L);
  288. SendMessageW(hwnd, LB_SETSEL, (0 == selected), (LPARAM)caret);
  289. HWND hParent = GetParent(hwnd);
  290. if (NULL != hParent)
  291. SendMessageW(hParent, WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(hwnd), LBN_SELCHANGE), (LPARAM)hwnd);
  292. }
  293. }
  294. return TRUE;
  295. }
  296. }
  297. return FALSE;
  298. }
  299. static BOOL FBListBox_OnNcHitTest(HWND hwnd, POINTS pts, LRESULT *pResult)
  300. {
  301. if (SIZER_OVERLAP_LEFT > 0 || SIZER_OVERLAP_RIGHT > 0)
  302. {
  303. RECT rw;
  304. if (GetWindowRect(hwnd, &rw))
  305. {
  306. if ((SIZER_OVERLAP_RIGHT > 0 && pts.x >= rw.left && pts.x <= (rw.left + SIZER_OVERLAP_RIGHT)) ||
  307. (SIZER_OVERLAP_LEFT > 0 && pts.x <= rw.right && pts.x >= (rw.right - SIZER_OVERLAP_LEFT)))
  308. {
  309. *pResult = HTTRANSPARENT;
  310. return TRUE;
  311. }
  312. }
  313. }
  314. return FALSE;
  315. }
  316. static LRESULT CALLBACK FBListBox_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  317. {
  318. WNDPROC fnOriginalProc = (WNDPROC)GetPropW(hwnd, NAVMGR_FBLISTBOXW);
  319. if (!fnOriginalProc) return DefWindowProcW(hwnd, uMsg, wParam, lParam);
  320. switch(uMsg)
  321. {
  322. case WM_NCDESTROY:
  323. SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONGX86)(LONG_PTR)fnOriginalProc);
  324. RemovePropW(hwnd, NAVMGR_FBLISTBOXW);
  325. return CallWindowProcW(fnOriginalProc, hwnd, uMsg, wParam, lParam);
  326. case WM_LBUTTONDOWN:
  327. if (FBListBox_OnLButtonDown(hwnd, (UINT)wParam, MAKEPOINTS(lParam))) return 0;
  328. break;
  329. case WM_LBUTTONUP:
  330. if (FBListBox_OnLButtonUp(hwnd, (UINT)wParam, MAKEPOINTS(lParam))) return 0;
  331. break;
  332. case WM_MOUSEWHEEL:
  333. if (FBListBox_OnMouseWheel(hwnd, GET_KEYSTATE_WPARAM(wParam), GET_WHEEL_DELTA_WPARAM(wParam), MAKEPOINTS(lParam))) return 0;
  334. break;
  335. case WM_MOUSEMOVE:
  336. if (FBListBox_OnMouseMove(hwnd, (UINT)wParam, MAKEPOINTS(lParam))) return 0;
  337. break;
  338. case WM_KEYDOWN:
  339. if (FBListBox_OnKeyDown(hwnd, (UINT)wParam, (UINT)lParam)) return 0;
  340. break;
  341. case WM_UPDATEUISTATE:
  342. InvalidateRect(hwnd, NULL, FALSE);
  343. break;
  344. case WM_NCHITTEST:
  345. {
  346. LRESULT lResult;
  347. if (FBListBox_OnNcHitTest(hwnd, MAKEPOINTS(lParam), &lResult))
  348. return lResult;
  349. }
  350. break;
  351. }
  352. return CallWindowProcW(fnOriginalProc, hwnd, uMsg, wParam, lParam);
  353. }