1
0

options.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "main.h"
  2. #include "./options.h"
  3. #include "./optionsHook.h"
  4. #include "./resource.h"
  5. #include "./ifc_omconfig.h"
  6. #include "./ifc_ombrowserconfig.h"
  7. #include "./ifc_omtoolbarconfig.h"
  8. #include "./ifc_omstatusbarconfig.h"
  9. #include "../winamp/wa_ipc.h"
  10. #include <windows.h>
  11. #include <commctrl.h>
  12. #include <shlwapi.h>
  13. #include <strsafe.h>
  14. HWND CALLBACK OptionsUI_CreatePage(HWND hParent, UINT style);
  15. HWND CALLBACK OptionsDebug_CreatePage(HWND hParent, UINT style);
  16. HWND CALLBACK OptionsInfo_CreatePage(HWND hParent, UINT style);
  17. static const OPTIONSPAGECREATOR szPageList[] =
  18. {
  19. OptionsUI_CreatePage,
  20. OptionsDebug_CreatePage,
  21. OptionsInfo_CreatePage,
  22. };
  23. typedef struct __OPTIONSDATA
  24. {
  25. obj_ombrowser *browserManager;
  26. BROWSEROPTIONSCALLBACK callback;
  27. ULONG_PTR user;
  28. UINT configCookie;
  29. UINT style;
  30. } OPTIONSDATA;
  31. #define OPTIONS_PROP L"OptionsDataProp"
  32. #define GetOptions(__hwnd) ((OPTIONSDATA*)GetProp(__hwnd, OPTIONS_PROP))
  33. #define IDC_ACTIVEPAGE 10001
  34. static INT_PTR CALLBACK OptionsFrame_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  35. HRESULT BrowserOptions_ShowDialog(obj_ombrowser *browserManager, HWND hOwner, UINT style, BROWSEROPTIONSCALLBACK callback, ULONG_PTR user)
  36. {
  37. OPTIONSDATA optionsData = {0};
  38. optionsData.browserManager = browserManager;
  39. optionsData.callback = callback;
  40. optionsData.user = user;
  41. optionsData.style = style;
  42. INT_PTR result = Plugin_DialogBoxParam(MAKEINTRESOURCE(IDD_BROWSER_OPTIONS), hOwner,
  43. OptionsFrame_DialogProc, (LPARAM)&optionsData);
  44. return (0 == result) ? S_OK : E_FAIL;
  45. }
  46. BOOL Options_SetCheckbox(HWND hwnd, UINT controlId, HRESULT checkedState)
  47. {
  48. HWND hControl = GetDlgItem(hwnd, controlId);
  49. if (NULL == hControl) return FALSE;
  50. if (FAILED(checkedState))
  51. {
  52. EnableWindow(hControl, FALSE);
  53. }
  54. else
  55. {
  56. WPARAM wParam = (S_OK == checkedState) ? BST_CHECKED : BST_UNCHECKED;
  57. SendMessage(hControl, BM_SETCHECK, wParam, 0L);
  58. EnableWindow(hControl, TRUE);
  59. }
  60. return TRUE;
  61. }
  62. static void OptionsFrame_NotifyTabSelected(HWND hwnd)
  63. {
  64. HWND hFrame = GetDlgItem(hwnd, IDC_TABFRAME);
  65. if (NULL == hFrame) return;
  66. NMHDR nmh = {0};
  67. nmh.code = TCN_SELCHANGE;
  68. nmh.hwndFrom = hFrame;
  69. nmh.idFrom = GetDlgCtrlID(hFrame);
  70. SendNotifyMessage(hwnd, WM_NOTIFY, (WPARAM)nmh.idFrom, (LPARAM)&nmh);
  71. }
  72. static void OptionsFrame_InitializePages(HWND hwnd, UINT style)
  73. {
  74. HWND hFrame = GetDlgItem(hwnd, IDC_TABFRAME);
  75. if (NULL == hFrame) return;
  76. HWND hWinamp = NULL;
  77. if (FAILED(Plugin_GetWinampWnd(&hWinamp)))
  78. hWinamp = NULL;
  79. INT pageCount = 0;
  80. WCHAR szBuffer[256] = {0};
  81. TCITEM tabItem = {0};
  82. tabItem.mask = TCIF_TEXT | TCIF_PARAM;
  83. for (size_t i = 0; i < ARRAYSIZE(szPageList); i++)
  84. {
  85. HWND hPage = szPageList[i](hwnd, style);
  86. if (NULL == hPage) continue;
  87. if (NULL != hWinamp && 0 == SENDWAIPC(hWinamp, IPC_USE_UXTHEME_FUNC, IPC_ISWINTHEMEPRESENT))
  88. SENDWAIPC(hWinamp, IPC_USE_UXTHEME_FUNC, hPage);
  89. if (0 == GetWindowText(hPage, szBuffer, ARRAYSIZE(szBuffer)))
  90. StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), L"Page %d", pageCount + 1);
  91. tabItem.lParam = (LPARAM)hPage;
  92. tabItem.pszText = szBuffer;
  93. tabItem.iImage = 0;
  94. if (-1 != TabCtrl_InsertItem(hFrame, pageCount, &tabItem))
  95. {
  96. pageCount++;
  97. }
  98. }
  99. TabCtrl_SetCurSel(hFrame, 0);
  100. }
  101. void getViewport(RECT *r, HWND wnd, int full, RECT *sr)
  102. {
  103. POINT *p = NULL;
  104. if (p || sr || wnd)
  105. {
  106. HMONITOR hm = NULL;
  107. if (sr)
  108. hm = MonitorFromRect(sr, MONITOR_DEFAULTTONEAREST);
  109. else if (wnd)
  110. hm = MonitorFromWindow(wnd, MONITOR_DEFAULTTONEAREST);
  111. else if (p)
  112. hm = MonitorFromPoint(*p, MONITOR_DEFAULTTONEAREST);
  113. if (hm)
  114. {
  115. MONITORINFOEXW mi;
  116. memset(&mi, 0, sizeof(mi));
  117. mi.cbSize = sizeof(mi);
  118. if (GetMonitorInfoW(hm, &mi))
  119. {
  120. if (!full)
  121. *r = mi.rcWork;
  122. else
  123. *r = mi.rcMonitor;
  124. return ;
  125. }
  126. }
  127. }
  128. if (full)
  129. { // this might be borked =)
  130. r->top = r->left = 0;
  131. r->right = GetSystemMetrics(SM_CXSCREEN);
  132. r->bottom = GetSystemMetrics(SM_CYSCREEN);
  133. }
  134. else
  135. {
  136. SystemParametersInfoW(SPI_GETWORKAREA, 0, r, 0);
  137. }
  138. }
  139. BOOL windowOffScreen(HWND hwnd, POINT pt)
  140. {
  141. RECT r = {0}, wnd = {0}, sr = {0};
  142. GetWindowRect(hwnd, &wnd);
  143. sr.left = pt.x;
  144. sr.top = pt.y;
  145. sr.right = sr.left + (wnd.right - wnd.left);
  146. sr.bottom = sr.top + (wnd.bottom - wnd.top);
  147. getViewport(&r, hwnd, 0, &sr);
  148. return !PtInRect(&r, pt);
  149. }
  150. static INT_PTR OptionsFrame_OnInitDialog(HWND hwnd, HWND hFocus, LPARAM param)
  151. {
  152. HWND hWinamp = NULL;
  153. if (FAILED(Plugin_GetWinampWnd(&hWinamp)))
  154. hWinamp = NULL;
  155. OPTIONSDATA *optionsData = (OPTIONSDATA*)param;
  156. SetProp(hwnd, OPTIONS_PROP, optionsData);
  157. OptionsConfigHook *configHook;
  158. if (SUCCEEDED(OptionsConfigHook::CreateInstance(hwnd, &configHook)))
  159. {
  160. ifc_omconfig *config;
  161. if (NULL != optionsData->browserManager && SUCCEEDED(optionsData->browserManager->GetConfig(NULL, (void**)&config)))
  162. {
  163. config->RegisterCallback(configHook, &optionsData->configCookie);
  164. config->Release();
  165. }
  166. configHook->Release();
  167. }
  168. HWND hFrame = GetDlgItem(hwnd, IDC_TABFRAME);
  169. if (NULL != hFrame)
  170. {
  171. if (NULL != hWinamp && 0 == SENDWAIPC(hWinamp, IPC_USE_UXTHEME_FUNC, IPC_ISWINTHEMEPRESENT))
  172. SENDWAIPC(hWinamp, IPC_USE_UXTHEME_FUNC, hFrame);
  173. TabCtrl_SetMinTabWidth(hFrame, 60);
  174. OptionsFrame_InitializePages(hwnd, optionsData->style);
  175. OptionsFrame_NotifyTabSelected(hwnd);
  176. UINT style = GetWindowStyle(hFrame);
  177. style |= TCS_HOTTRACK | TCS_TABS | TCS_MULTILINE | TCS_RIGHTJUSTIFY;
  178. SetWindowLongPtr(hFrame, GWL_STYLE, style);
  179. }
  180. PostMessage(hwnd, WM_CHANGEUISTATE, MAKEWPARAM(UIS_INITIALIZE, UISF_HIDEACCEL | UISF_HIDEFOCUS), 0L);
  181. if (0 != optionsData->callback)
  182. optionsData->callback(hwnd, BOCALLBACK_INIT, optionsData->user);
  183. // show config window and restore last position as applicable
  184. obj_ombrowser *browserManager;
  185. if (FALSE != SendMessage(hwnd, BOM_GETBROWSER, 0, (LPARAM)&browserManager))
  186. {
  187. ifc_ombrowserconfig *browserConfig;
  188. if (SUCCEEDED(browserManager->GetConfig(&IFC_OmBrowserConfig, (void**)&browserConfig)))
  189. {
  190. POINT pt = {-1, -1};
  191. pt.x = browserConfig->GetX();
  192. pt.y = browserConfig->GetY();
  193. browserConfig->Release();
  194. if (!windowOffScreen(hwnd, pt))
  195. SetWindowPos(hwnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOSENDCHANGING);
  196. }
  197. browserManager->Release();
  198. }
  199. return 0;
  200. }
  201. static void OptionsFrame_OnDestroy(HWND hwnd)
  202. {
  203. obj_ombrowser *browserManager;
  204. if (FALSE != SendMessage(hwnd, BOM_GETBROWSER, 0, (LPARAM)&browserManager))
  205. {
  206. ifc_ombrowserconfig *browserConfig;
  207. if (SUCCEEDED(browserManager->GetConfig(&IFC_OmBrowserConfig, (void**)&browserConfig)))
  208. {
  209. RECT rect = {0};
  210. GetWindowRect(hwnd, &rect);
  211. browserConfig->SetX(rect.left);
  212. browserConfig->SetY(rect.top);
  213. browserConfig->Release();
  214. }
  215. browserManager->Release();
  216. }
  217. OPTIONSDATA *optionsData = GetOptions(hwnd);
  218. RemoveProp(hwnd, OPTIONS_PROP);
  219. if (NULL == optionsData)
  220. return;
  221. if (0 != optionsData->configCookie)
  222. {
  223. ifc_omconfig *config;
  224. if (NULL != optionsData->browserManager && SUCCEEDED(optionsData->browserManager->GetConfig(NULL, (void**)&config)))
  225. {
  226. config->UnregisterCallback(optionsData->configCookie);
  227. config->Release();
  228. }
  229. optionsData->configCookie = 0;
  230. }
  231. }
  232. static void OptionsFrame_OnWindowPosChanged(HWND hwnd, WINDOWPOS *pwp)
  233. {
  234. }
  235. static void OptionsFrame_OnTabSelected(HWND hwnd, HWND hFrame)
  236. {
  237. if (NULL == hFrame) return;
  238. HWND hPageOld = GetDlgItem(hwnd, IDC_ACTIVEPAGE);
  239. if (NULL != hPageOld)
  240. SetWindowLongPtr(hPageOld, GWLP_ID, 0);
  241. INT iSelected = TabCtrl_GetCurSel(hFrame);
  242. TCITEM tabItem;
  243. tabItem.mask = TCIF_PARAM;
  244. HWND hPage = (TRUE == TabCtrl_GetItem(hFrame, iSelected, &tabItem)) ? (HWND)tabItem.lParam : NULL;
  245. if (NULL != hPage)
  246. {
  247. SetWindowLongPtr(hPage, GWLP_ID, IDC_ACTIVEPAGE);
  248. RECT rcTab;
  249. GetClientRect(hFrame, &rcTab);
  250. MapWindowPoints(hFrame, hwnd, (POINT*)&rcTab, 2);
  251. TabCtrl_AdjustRect(hFrame, FALSE, &rcTab);
  252. SetWindowPos(hPage, HWND_TOP, rcTab.left, rcTab.top,
  253. rcTab.right - rcTab.left, rcTab.bottom - rcTab.top,
  254. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOREDRAW);
  255. }
  256. if (NULL != hPage)
  257. {
  258. ShowWindow(hPage, SW_SHOWNA);
  259. }
  260. if (NULL != hPageOld)
  261. {
  262. ShowWindow(hPageOld, SW_HIDE);
  263. if (NULL != hPage)
  264. RedrawWindow(hPage, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN | RDW_NOINTERNALPAINT | RDW_NOERASE);
  265. }
  266. }
  267. static void OptionsFrame_OnCommand(HWND hwnd, INT commandId, INT eventId, HWND hControl)
  268. {
  269. switch(commandId)
  270. {
  271. case IDOK:
  272. case IDCANCEL:
  273. EndDialog(hwnd, 0);
  274. break;
  275. }
  276. }
  277. static LRESULT OptionsFrame_OnNotify(HWND hwnd, INT controlId, NMHDR *pnmh)
  278. {
  279. switch(controlId)
  280. {
  281. case IDC_TABFRAME:
  282. switch(pnmh->code)
  283. {
  284. case TCN_SELCHANGE:
  285. OptionsFrame_OnTabSelected(hwnd, pnmh->hwndFrom);
  286. break;
  287. }
  288. break;
  289. }
  290. return 0;
  291. }
  292. static LRESULT OptionsFrame_OnGetBrowser(HWND hwnd, obj_ombrowser **ppBrowser)
  293. {
  294. if (NULL == ppBrowser) return FALSE;
  295. OPTIONSDATA *optionsData = GetOptions(hwnd);
  296. if (NULL == optionsData || NULL == optionsData->browserManager)
  297. return FALSE;
  298. *ppBrowser = optionsData->browserManager;
  299. (*ppBrowser)->AddRef();
  300. return TRUE;
  301. }
  302. static void OptionsFrame_OnConfigChanged(HWND hwnd, BOMCONFIGCHANGED *configData)
  303. {
  304. HWND hControl = GetDlgItem(hwnd, IDC_TABFRAME);
  305. if (NULL == hControl) return;
  306. TCITEM item = {0};
  307. item.mask = TCIF_PARAM;
  308. INT count = (INT)SendMessage(hControl, TCM_GETITEMCOUNT, 0, 0L);
  309. for(INT i = 0; i < count; i++)
  310. {
  311. if (FALSE != (BOOL)SendMessage(hControl, TCM_GETITEM, (WPARAM)i, (LPARAM)&item) &&
  312. NULL != item.lParam)
  313. {
  314. SendMessage((HWND)item.lParam, BOM_CONFIGCHANGED, 0, (LPARAM)configData);
  315. }
  316. }
  317. }
  318. static INT_PTR CALLBACK OptionsFrame_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  319. {
  320. switch(uMsg)
  321. {
  322. case WM_INITDIALOG: return OptionsFrame_OnInitDialog(hwnd, (HWND)wParam, lParam);
  323. case WM_DESTROY: OptionsFrame_OnDestroy(hwnd); return 0;
  324. case WM_WINDOWPOSCHANGED: OptionsFrame_OnWindowPosChanged(hwnd, (WINDOWPOS*)lParam); return TRUE;
  325. case WM_COMMAND: OptionsFrame_OnCommand(hwnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); return TRUE;
  326. case WM_NOTIFY: MSGRESULT(hwnd, OptionsFrame_OnNotify(hwnd, (INT)wParam, (NMHDR*)lParam));
  327. case BOM_GETBROWSER: MSGRESULT(hwnd, OptionsFrame_OnGetBrowser(hwnd, (obj_ombrowser**)lParam));
  328. case BOM_CONFIGCHANGED: OptionsFrame_OnConfigChanged(hwnd, (BOMCONFIGCHANGED*)lParam); return TRUE;
  329. }
  330. return 0;
  331. }