Configdlg.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include "ConfigDlg.h"
  2. #include "gen_hotkeys.h"
  3. #include "accelBlock.h"
  4. #define ListView_InsertColumnW(hwnd, iCol, pcol) \
  5. (int)SNDMSG((hwnd), LVM_INSERTCOLUMNW, (WPARAM)(int)(iCol), (LPARAM)(const LV_COLUMNW *)(pcol))
  6. #define ListView_InsertItemW(hwnd, pitem) \
  7. (int)SNDMSG((hwnd), LVM_INSERTITEMW, 0, (LPARAM)(const LV_ITEMW *)(pitem))
  8. #define ListView_SetItemTextW(hwndLV, i, iSubItem_, pszText_) \
  9. { LV_ITEMW _macro_lvi;\
  10. _macro_lvi.iSubItem = (iSubItem_);\
  11. _macro_lvi.pszText = (pszText_);\
  12. SNDMSG((hwndLV), LVM_SETITEMTEXTW, (WPARAM)(i), (LPARAM)(LV_ITEMW *)&_macro_lvi);\
  13. }
  14. #define ListView_SetItemW(hwnd, pitem) \
  15. (BOOL)SNDMSG((hwnd), LVM_SETITEMW, 0, (LPARAM)(LV_ITEMW *)(pitem))
  16. void SetListItem(HWND hwHKList, HWND hwHK, HOTKEY_DATA *hk_data, int idx = -1, int failed = 0);
  17. static AcceleratorBlocker *pAccelBlock = NULL;
  18. static bool changed = false;
  19. static int setCheckedStuff(HWND hwndDlg)
  20. {
  21. int checked = SendMessage(GetDlgItem(hwndDlg, IDC_ENABLED), BM_GETCHECK, 0, 0) == BST_CHECKED;
  22. EnableWindow(GetDlgItem(hwndDlg,IDC_HKLIST),checked);
  23. EnableWindow(GetDlgItem(hwndDlg,IDC_COMBO),checked);
  24. EnableWindow(GetDlgItem(hwndDlg,IDC_HOTKEY),checked);
  25. EnableWindow(GetDlgItem(hwndDlg,IDC_ADD),checked);
  26. if (!checked)
  27. {
  28. EnableWindow(GetDlgItem(hwndDlg,IDC_SAVE),0);
  29. EnableWindow(GetDlgItem(hwndDlg,IDC_REMOVE),0);
  30. }
  31. EnableWindow(GetDlgItem(hwndDlg,IDC_DEFAULT),checked);
  32. return checked;
  33. }
  34. int ResizeComboBoxDropDown(HWND hwndDlg, UINT id, const char* str, int width){
  35. SIZE size = {0};
  36. HWND control = GetDlgItem(hwndDlg, id);
  37. HDC hdc = GetDC(control);
  38. // get and select parent dialog's font so that it'll calculate things correctly
  39. HFONT font = (HFONT)SendMessage(hwndDlg,WM_GETFONT,0,0), oldfont = (HFONT)SelectObject(hdc,font);
  40. GetTextExtentPoint32(hdc, str, lstrlen(str)+1, &size);
  41. int ret = width;
  42. if(size.cx > width)
  43. {
  44. SendDlgItemMessageW(hwndDlg, id, CB_SETDROPPEDWIDTH, size.cx, 0);
  45. ret = size.cx;
  46. }
  47. SelectObject(hdc, oldfont);
  48. ReleaseDC(control, hdc);
  49. return ret;
  50. }
  51. int ResizeComboBoxDropDownW(HWND hwndDlg, UINT id, const wchar_t *str, int width){
  52. SIZE size = {0};
  53. HWND control = GetDlgItem(hwndDlg, id);
  54. HDC hdc = GetDC(control);
  55. // get and select parent dialog's font so that it'll calculate things correctly
  56. HFONT font = (HFONT)SendMessage(hwndDlg,WM_GETFONT,0,0), oldfont = (HFONT)SelectObject(hdc,font);
  57. GetTextExtentPoint32W(hdc, str, (int)wcslen(str)+1, &size);
  58. int ret = width;
  59. if(size.cx > width)
  60. {
  61. SendDlgItemMessageW(hwndDlg, id, CB_SETDROPPEDWIDTH, size.cx, 0);
  62. ret = size.cx;
  63. }
  64. SelectObject(hdc, oldfont);
  65. ReleaseDC(control, hdc);
  66. return ret;
  67. }
  68. BOOL CALLBACK ConfigProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  69. {
  70. HWND hwHKList = GetDlgItem(hwndDlg, IDC_HKLIST);
  71. HWND hwCombo = GetDlgItem(hwndDlg, IDC_COMBO);
  72. HWND hwHK = GetDlgItem(hwndDlg, IDC_HOTKEY);
  73. switch (uMsg)
  74. {
  75. case WM_INITDIALOG:
  76. {
  77. int colwidth1 = -1, colwidth2 = -1;
  78. if (hwHKList && hwHK)
  79. {
  80. RECT r;
  81. SendMessage(hwHKList, WM_SETREDRAW, FALSE, 0);
  82. ListView_SetExtendedListViewStyle(hwHKList, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
  83. GetClientRect(hwHKList, &r);
  84. colwidth2 = GetPrivateProfileIntW(L"gen_hotkeys", L"col2", -1, g_iniFile);
  85. LVCOLUMNW lc = {LVCF_TEXT | LVCF_WIDTH, 0,
  86. (colwidth2==-1?((r.right / 2) - GetSystemMetrics(SM_CYHSCROLL)):colwidth2),
  87. WASABI_API_LNGSTRINGW(IDS_GHK_HOTKEY_COLUMN), 0, 0
  88. };
  89. ListView_InsertColumnW(hwHKList, 0, &lc);
  90. lc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
  91. colwidth1 = GetPrivateProfileIntW(L"gen_hotkeys", L"col1", -1, g_iniFile);
  92. lc.cx = (colwidth1==-1?(r.right - lc.cx - GetSystemMetrics(SM_CYHSCROLL)):colwidth1);
  93. lc.pszText = WASABI_API_LNGSTRINGW(IDS_GHK_ACTION_COLUMN);
  94. lc.iSubItem = 1;
  95. ListView_InsertColumnW(hwHKList, 0, &lc);
  96. }
  97. changed = false;
  98. SubclassEditBox(hwHK);
  99. if (NULL != hwHK && NULL == pAccelBlock)
  100. pAccelBlock = new AcceleratorBlocker(hwHK);
  101. HBITMAP hBitmap = LoadBitmap(psPlugin.hDllInstance, MAKEINTRESOURCE(IDB_LVSTATE));
  102. HIMAGELIST hImageList = ImageList_Create(14, 14, ILC_COLOR8|ILC_MASK, 1, 0);
  103. ImageList_AddMasked(hImageList, hBitmap, RGB(252, 254, 252));
  104. ListView_SetImageList(hwHKList, hImageList, LVSIL_STATE);
  105. DeleteObject(hBitmap);
  106. ListView_SetItemCount(hwHKList, g_dwHotkeys);
  107. // We don't want our hot keys while in the config
  108. for (size_t i = 0; i < g_dwHotkeys; i++)
  109. {
  110. SetLastError(0);
  111. UnregisterHotkey(g_hotkeys + i);
  112. SetListItem(hwHKList, hwHK, &g_hotkeys[i].hkd, -1, g_hotkeys[i].failed);
  113. }
  114. hotkeysClear();
  115. if(colwidth1==-1)
  116. ListView_SetColumnWidth(hwHKList, 0, LVSCW_AUTOSIZE);
  117. if(colwidth2==-1)
  118. ListView_SetColumnWidth(hwHKList, 1, LVSCW_AUTOSIZE);
  119. // clear
  120. SendMessage(hwHK, wmHKCtlSet, 0, 0);
  121. ListView_SetItemState(hwHKList, -1, 0, LVIS_SELECTED);
  122. SendMessage(hwHKList, WM_SETREDRAW, TRUE, 0);
  123. int width = 0;
  124. for (size_t i = 0; i < GetCommandsNum(); i++)
  125. {
  126. bool unicode = 0;
  127. char *cmdname = GetCommandName((unsigned int)i, &unicode);
  128. if (*cmdname)
  129. {
  130. LRESULT pos;
  131. if (unicode)
  132. {
  133. pos = SendMessageW(hwCombo, CB_ADDSTRING, 0, (LPARAM) cmdname);
  134. width = ResizeComboBoxDropDownW(hwndDlg, IDC_COMBO, (wchar_t*)cmdname, width);
  135. }
  136. else
  137. {
  138. pos = SendMessageA(hwCombo, CB_ADDSTRING, 0, (LPARAM) cmdname);
  139. width = ResizeComboBoxDropDown(hwndDlg, IDC_COMBO, cmdname, width);
  140. }
  141. SendMessage(hwCombo, CB_SETITEMDATA, pos, i);
  142. }
  143. }
  144. // Set the enabled checkbox
  145. CheckDlgButton(hwndDlg,IDC_ENABLED,GetPrivateProfileIntW(L"gen_hotkeys", L"enabled", 0, g_iniFile));
  146. CheckDlgButton(hwndDlg,IDC_ENABLED_WM_APPCOMMAND,GetPrivateProfileIntW(L"gen_hotkeys", L"appcommand", 0, g_iniFile));
  147. setCheckedStuff(hwndDlg);
  148. if (NULL != WASABI_API_APP)
  149. WASABI_API_APP->DirectMouseWheel_EnableConvertToMouseWheel(hwHKList, TRUE);
  150. break;
  151. }
  152. case WM_COMMAND:
  153. {
  154. // we don't want accelerator keys working when the hotkey window is focused
  155. // that could be a problem if someone wants to bind Alt+D for example...
  156. if (GetFocus() == hwHK)
  157. break;
  158. switch (LOWORD(wParam))
  159. {
  160. case IDC_ADD:
  161. {
  162. HOTKEY_DATA hkd;
  163. DWORD dwHotKey = (unsigned long)SendMessage(hwHK, wmHKCtlGet, 0, 0);
  164. UINT iCommand = (unsigned int)SendMessage(hwCombo, CB_GETITEMDATA, SendMessage(hwCombo, CB_GETCURSEL, 0, 0), 0);
  165. if (!dwHotKey || iCommand == CB_ERR)
  166. break;
  167. hkd.dwHotKey = dwHotKey;
  168. hkd.iCommand = iCommand;
  169. changed = true;
  170. SetListItem(hwHKList, hwHK, &hkd);
  171. break;
  172. }
  173. case IDC_REMOVE:
  174. {
  175. int i = ListView_GetNextItem(hwHKList, -1, LVNI_SELECTED);
  176. while (i != -1)
  177. {
  178. LVITEM lvi = {LVIF_PARAM, i};
  179. ListView_GetItem(hwHKList, &lvi);
  180. HOTKEY_DATA *hkd = (HOTKEY_DATA *) lvi.lParam;
  181. delete hkd;
  182. changed = true;
  183. ListView_DeleteItem(hwHKList, i);
  184. i = ListView_GetNextItem(hwHKList, -1, LVNI_SELECTED);
  185. }
  186. EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE), 0);
  187. break;
  188. }
  189. case IDC_SAVE:
  190. {
  191. if (ListView_GetSelectedCount(hwHKList) != 1)
  192. break;
  193. int iSel = ListView_GetNextItem(hwHKList, -1, LVNI_SELECTED);
  194. LVITEMW lvi = {LVIF_PARAM, iSel};
  195. ListView_GetItem(hwHKList, &lvi);
  196. HOTKEY_DATA *hkd = (HOTKEY_DATA *) lvi.lParam;
  197. if (hkd)
  198. {
  199. DWORD dwHotKey = (unsigned long)SendMessage(hwHK, wmHKCtlGet, 0, 0);
  200. UINT iCommand = (unsigned int)SendMessage(hwCombo, CB_GETITEMDATA, SendMessage(hwCombo, CB_GETCURSEL, 0, 0), 0);
  201. if (!dwHotKey || iCommand == CB_ERR)
  202. break;
  203. hkd->dwHotKey = dwHotKey;
  204. hkd->iCommand = iCommand;
  205. changed = true;
  206. SetListItem(hwHKList, hwHK, hkd, iSel);
  207. }
  208. break;
  209. }
  210. case IDC_ENABLED:
  211. if (setCheckedStuff(hwndDlg))
  212. {
  213. DWORD dwSelected = ListView_GetSelectedCount(hwHKList);
  214. EnableWindow(GetDlgItem(hwndDlg, IDC_SAVE), dwSelected == 1);
  215. EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE), dwSelected);
  216. changed = true;
  217. }
  218. break;
  219. case IDC_DEFAULT:
  220. {
  221. DWORD i;
  222. SendMessage(hwndDlg, WM_SETREDRAW, FALSE, 0);
  223. DWORD dwCount = (DWORD)ListView_GetItemCount(hwHKList);
  224. if (dwCount)
  225. {
  226. for (i = 0; i < dwCount; i++)
  227. {
  228. LVITEM lvi = {LVIF_PARAM,(int)i};
  229. ListView_GetItem(hwHKList, &lvi);
  230. HOTKEY_DATA *hkd = (HOTKEY_DATA *) lvi.lParam;
  231. delete hkd;
  232. }
  233. }
  234. changed = true;
  235. ListView_DeleteAllItems(hwHKList);
  236. for (i = 0; i < DEFHKDS_NUM; i++)
  237. {
  238. SetListItem(hwHKList, hwHK, g_defhkds + i);
  239. }
  240. // restore the size of the columns on a reset as well
  241. ListView_SetColumnWidth(hwHKList, 0, LVSCW_AUTOSIZE);
  242. ListView_SetColumnWidth(hwHKList, 1, LVSCW_AUTOSIZE);
  243. ListView_SetItemState(hwHKList, -1, 0, LVIS_SELECTED);
  244. SendMessage(hwndDlg, WM_SETREDRAW, TRUE, 0);
  245. InvalidateRect(hwndDlg, 0, 0);
  246. break;
  247. }
  248. }
  249. break;
  250. }
  251. case WM_NOTIFY:
  252. {
  253. LPNMHDR nmhdr = (LPNMHDR) lParam;
  254. if (nmhdr && nmhdr->idFrom == IDC_HKLIST)
  255. {
  256. if (nmhdr->code == LVN_ITEMCHANGED)
  257. {
  258. LPNMLISTVIEW pnmv = (LPNMLISTVIEW) lParam;
  259. DWORD dwSelected = ListView_GetSelectedCount(hwHKList);
  260. EnableWindow(GetDlgItem(hwndDlg, IDC_SAVE), dwSelected == 1);
  261. EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE), dwSelected);
  262. SendMessage(hwHK, wmHKCtlSet, 0, 0);
  263. SendMessage(hwCombo, CB_SETCURSEL, -1, 0);
  264. if (dwSelected == 1 && (pnmv->uNewState & LVIS_SELECTED))
  265. {
  266. HOTKEY_DATA *hkd = (HOTKEY_DATA *) pnmv->lParam;
  267. if (hkd)
  268. {
  269. SendMessage(hwHK, wmHKCtlSet, hkd->dwHotKey, 0);
  270. bool unicode = 0;
  271. char *cmd = GetCommandName(hkd->iCommand, &unicode);
  272. if (*cmd)
  273. {
  274. if(unicode) SendMessageW(hwCombo, CB_SELECTSTRING, -1, (LPARAM) cmd);
  275. else SendMessageA(hwCombo, CB_SELECTSTRING, -1, (LPARAM) cmd);
  276. }
  277. }
  278. }
  279. }
  280. else if(nmhdr->code == LVN_KEYDOWN)
  281. {
  282. LPNMLVKEYDOWN pnmv = (LPNMLVKEYDOWN) lParam;
  283. if(pnmv->wVKey == VK_DELETE)
  284. {
  285. changed = true;
  286. SendMessage(hwndDlg,WM_COMMAND,MAKEWPARAM(IDC_REMOVE,0),0);
  287. }
  288. else if(pnmv->wVKey == 'A' && GetAsyncKeyState(VK_CONTROL))
  289. {
  290. for(int i = 0; i < ListView_GetItemCount(hwHKList); i++)
  291. {
  292. ListView_SetItemState(hwHKList,i,LVIS_SELECTED,LVIS_SELECTED);
  293. }
  294. }
  295. }
  296. }
  297. }
  298. break;
  299. case WM_DESTROY:
  300. {
  301. int checked = (SendMessage(GetDlgItem(hwndDlg, IDC_ENABLED), BM_GETCHECK, 0, 0) == BST_CHECKED);
  302. writePrivateProfileInt(L"enabled", checked);
  303. writePrivateProfileInt(L"appcommand",
  304. (SendMessage(GetDlgItem(hwndDlg, IDC_ENABLED_WM_APPCOMMAND), BM_GETCHECK, 0, 0) == BST_CHECKED));
  305. hotkeysClear();
  306. int iCount = ListView_GetItemCount(hwHKList);
  307. HOTKEY_DATA *hkds = NULL;
  308. if (iCount)
  309. {
  310. hkds = new HOTKEY_DATA[iCount]; // TODO: could alloca this
  311. memset(hkds, 0, iCount * sizeof(HOTKEY_DATA));
  312. }
  313. if (hkds || !iCount)
  314. {
  315. for (size_t i = 0; i < (unsigned int)iCount; i++)
  316. {
  317. LVITEM lvi = {LVIF_PARAM, (int)i};
  318. ListView_GetItem(hwHKList, &lvi);
  319. HOTKEY_DATA *hkd = (HOTKEY_DATA *) lvi.lParam;
  320. if (hkd)
  321. {
  322. hkds[i] = *hkd;
  323. delete hkd;
  324. }
  325. }
  326. hotkeysLoad(hkds, iCount, checked);
  327. if (changed) hotkeysSave(hkds, iCount);
  328. delete [] hkds;
  329. }
  330. writePrivateProfileInt(L"col1", ListView_GetColumnWidth(hwHKList,0));
  331. writePrivateProfileInt(L"col2", ListView_GetColumnWidth(hwHKList,1));
  332. RegisterShellHookWindow(psPlugin.hwndParent);
  333. if (NULL != pAccelBlock && hwHK == pAccelBlock->GetHwnd())
  334. {
  335. delete(pAccelBlock);
  336. pAccelBlock = NULL;
  337. }
  338. if (NULL != WASABI_API_APP)
  339. WASABI_API_APP->DirectMouseWheel_EnableConvertToMouseWheel(hwHKList, FALSE);
  340. }
  341. break;
  342. }
  343. return FALSE;
  344. }
  345. // idx = -1 for insertion
  346. void SetListItem(HWND hwHKList, HWND hwHK, HOTKEY_DATA *hk_data, int idx, int failed)
  347. {
  348. wchar_t szHK[1024] = {L""};
  349. SendMessage(hwHK, wmHKCtlSet, hk_data->dwHotKey, 0);
  350. GetWindowTextW(hwHK, szHK, sizeof(szHK));
  351. HOTKEY_DATA *hk_data_allocated = 0;
  352. if (idx < 0)
  353. hk_data_allocated = new HOTKEY_DATA;
  354. if (idx >= 0 || hk_data_allocated)
  355. {
  356. if (idx < 0)
  357. *hk_data_allocated = *hk_data;
  358. int pos = ListView_GetItemCount(hwHKList);
  359. if (idx >= 0)
  360. pos = idx;
  361. // Add item to the list
  362. bool unicode = 0;
  363. LVITEM lvi = {
  364. LVIF_PARAM | LVIF_TEXT | LVIF_STATE,
  365. pos,
  366. 0,
  367. LVIS_SELECTED | INDEXTOSTATEIMAGEMASK(failed ? (UINT)1 : 0) | LVIS_FOCUSED,
  368. LVIS_SELECTED | LVIS_STATEIMAGEMASK | LVIS_FOCUSED,
  369. GetCommandName(hk_data->iCommand, &unicode),
  370. 0,
  371. 0,
  372. (LPARAM) hk_data_allocated
  373. };
  374. char tmp[1024] = {0};
  375. if (!lvi.pszText || !*lvi.pszText)
  376. {
  377. if (hk_data->szCommand)
  378. {
  379. StringCchPrintf(tmp, 1024, "[%s]", hk_data->szCommand);
  380. lvi.pszText = tmp;
  381. }
  382. }
  383. if (!lvi.pszText || !*lvi.pszText)
  384. lvi.pszText = WASABI_API_LNGSTRING(IDS_GHK_ACTION_NOT_FOUND);
  385. if (idx >= 0)
  386. {
  387. lvi.mask ^= LVIF_PARAM;
  388. if(unicode) ListView_SetItemW(hwHKList, &lvi);
  389. else ListView_SetItem(hwHKList, &lvi);
  390. }
  391. else
  392. {
  393. ListView_SetItemState(hwHKList, -1, 0, LVIS_SELECTED | LVIS_FOCUSED);
  394. if(unicode) ListView_InsertItemW(hwHKList, &lvi);
  395. else ListView_InsertItem(hwHKList, &lvi);
  396. }
  397. ListView_SetItemTextW(hwHKList, pos, 1, szHK);
  398. }
  399. }