spage_lang.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #define APSTUDIO_READONLY_SYMBOLS
  2. #include "main.h"
  3. #include "./spage_lang.h"
  4. #include "./setup_resource.h"
  5. #include "../nu/ns_wc.h"
  6. #include "./langutil.h"
  7. typedef struct _LANGREC
  8. {
  9. LPWSTR pszFileName;
  10. INT nType;
  11. } LANGREC;
  12. static BOOL CALLBACK AddLangToListBox(ENUMLANG *pel, LPVOID pUser);
  13. static INT ListBox_FindLangFileIndex(HWND hwndLB, LPCWSTR pszLangPath);
  14. static LPCWSTR ListBox_GetSelectedLang(HWND hwndLB, LPWSTR pszLangPath, INT cchLen);
  15. setup_page_lang::setup_page_lang() : ref(1), hwnd(NULL)
  16. {
  17. *szSelectionPath = 0x00;
  18. }
  19. setup_page_lang::~setup_page_lang()
  20. {
  21. }
  22. size_t setup_page_lang::AddRef()
  23. {
  24. return ++ref;
  25. }
  26. size_t setup_page_lang::Release()
  27. {
  28. if (1 == ref)
  29. {
  30. delete(this);
  31. return 0;
  32. }
  33. return --ref;
  34. }
  35. HRESULT setup_page_lang::GetName(bool bShort, const wchar_t **pszName)
  36. {
  37. static wchar_t szName[64] = {0};
  38. *pszName = (*szName) ? szName : getStringW(IDS_PAGE_LANGUAGE, szName, sizeof(szName)/sizeof(wchar_t));
  39. return S_OK;
  40. }
  41. HRESULT setup_page_lang::Save(HWND hwndText)
  42. {
  43. if (S_FALSE == IsDirty()) return S_OK;
  44. if (!*szSelectionPath) *config_langpack = 0x00;
  45. else
  46. {
  47. StringCbCopyW(config_langpack, sizeof(config_langpack), szSelectionPath);
  48. }
  49. config_save_langpack_var();
  50. return S_OK;
  51. }
  52. HRESULT setup_page_lang::Revert(void)
  53. {
  54. HRESULT hr(S_OK);
  55. if (*config_langpack)
  56. {
  57. StringCbCopyW(config_langpack, sizeof(szSelectionPath), config_langpack);
  58. }
  59. else szSelectionPath[0] = 0x00;
  60. if (hwnd)
  61. {
  62. HWND hwndLB = GetDlgItem(hwnd, IDC_LB_LANG);
  63. INT index = ListBox_FindLangFileIndex(hwndLB, szSelectionPath);
  64. if (LB_ERR == index && *szSelectionPath) index = ListBox_FindLangFileIndex(hwndLB, NULL); // find default embeded lang
  65. SendMessageW(hwndLB, LB_SETCURSEL, (LB_ERR != index) ? index : 0, 0L);
  66. }
  67. return hr;
  68. }
  69. HRESULT setup_page_lang::IsDirty(void)
  70. {
  71. INT cr;
  72. cr = ComparePath(config_langpack, szSelectionPath, LANGDIR);
  73. if (!cr) return E_UNEXPECTED;
  74. return (CSTR_EQUAL != cr) ? S_OK : S_FALSE;
  75. }
  76. HRESULT setup_page_lang::Validate(void)
  77. {
  78. return S_OK;
  79. }
  80. HRESULT setup_page_lang::CreateView(HWND hwndParent, HWND *phwnd)
  81. {
  82. *phwnd = WACreateDialogParam(MAKEINTRESOURCEW(IDD_SETUP_PAGE_LANG), hwndParent, ::DialogProc, (LPARAM)this);
  83. return S_OK;
  84. }
  85. void setup_page_lang::ListBox_OnSelChange(HWND hwndCtrl)
  86. {
  87. ListBox_GetSelectedLang(hwndCtrl, szSelectionPath, sizeof(szSelectionPath)/sizeof(wchar_t));
  88. }
  89. void setup_page_lang::ListBox_OnItemDelete(DELETEITEMSTRUCT *pdis)
  90. {
  91. LANGREC *pr = (LANGREC*)pdis->itemData;
  92. if (pr)
  93. {
  94. if (pr->pszFileName) free(pr->pszFileName);
  95. free(pr);
  96. }
  97. }
  98. INT_PTR setup_page_lang::OnInitDialog(HWND hwndFocus, LPARAM lParam)
  99. {
  100. INT index;
  101. HWND hwndLB;
  102. hwndLB = GetDlgItem(hwnd, IDC_LB_LANG);
  103. EnumerateLanguages(AddLangToListBox, hwndLB);
  104. index = ListBox_FindLangFileIndex(hwndLB, szSelectionPath);
  105. if (LB_ERR == index && *szSelectionPath) index = ListBox_FindLangFileIndex(hwndLB, NULL); // find default embeded lang
  106. SendMessageW(hwndLB, LB_SETCURSEL, (LB_ERR != index) ? index : 0, 0L);
  107. PostMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_LB_LANG, LBN_SELCHANGE), (LPARAM)hwndLB);
  108. return 0;
  109. }
  110. void setup_page_lang::OnSize(UINT nType, INT cx, INT cy)
  111. {
  112. HWND hwndCtrl;
  113. RECT rw;
  114. hwndCtrl = GetDlgItem(hwnd, IDC_LBL_HEADER);
  115. if (hwndCtrl)
  116. {
  117. GetWindowRect(hwndCtrl, &rw);
  118. MapWindowPoints(HWND_DESKTOP, hwnd, (POINT*)&rw, 2);
  119. SetWindowPos(hwndCtrl, NULL, 0, 0, cx - rw.left*2, rw.bottom - rw.top, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
  120. }
  121. hwndCtrl = GetDlgItem(hwnd, IDC_LB_LANG);
  122. if (hwndCtrl)
  123. {
  124. GetWindowRect(hwndCtrl, &rw);
  125. MapWindowPoints(HWND_DESKTOP, hwnd, (POINT*)&rw, 2);
  126. SetWindowPos(hwndCtrl, NULL, max(0, (cx - (rw.right - rw.left))/2), rw.top, rw.right - rw.left, cy - rw.top - 16, SWP_NOACTIVATE | SWP_NOZORDER);
  127. }
  128. }
  129. void setup_page_lang::OnCommand(INT nCtrlID, INT nEvntID, HWND hwndCtrl)
  130. {
  131. switch(nCtrlID)
  132. {
  133. case IDC_LB_LANG:
  134. switch(nEvntID)
  135. {
  136. case LBN_SELCHANGE: ListBox_OnSelChange(hwndCtrl); break;
  137. }
  138. break;
  139. }
  140. }
  141. INT_PTR setup_page_lang::OnDeleteItem(INT nCtrlID, DELETEITEMSTRUCT *pdis)
  142. {
  143. switch(nCtrlID)
  144. {
  145. case IDC_LB_LANG: ListBox_OnItemDelete(pdis); return TRUE;
  146. }
  147. return 0;
  148. }
  149. INT_PTR setup_page_lang::PageDlgProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  150. {
  151. switch(uMsg)
  152. {
  153. case WM_INITDIALOG: return OnInitDialog((HWND)wParam, lParam);
  154. case WM_DESTROY: break;
  155. case WM_SIZE: OnSize((UINT)wParam, LOWORD(lParam), HIWORD(lParam)); break;
  156. case WM_COMMAND: OnCommand(LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break;
  157. }
  158. return 0;
  159. }
  160. static INT_PTR WINAPI DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  161. {
  162. setup_page_lang *pInst = (setup_page_lang*)GetPropW(hwnd, L"SETUPPAGE");
  163. switch(uMsg)
  164. {
  165. case WM_INITDIALOG:
  166. pInst = (setup_page_lang*)lParam;
  167. if (pInst)
  168. {
  169. pInst->hwnd = hwnd;
  170. SetPropW(hwnd, L"SETUPPAGE", pInst);
  171. }
  172. break;
  173. case WM_DESTROY:
  174. if (pInst)
  175. {
  176. pInst->PageDlgProc(uMsg, wParam, lParam);
  177. RemovePropW(hwnd, L"SETUPPAGE");
  178. pInst = NULL;
  179. }
  180. break;
  181. }
  182. return (pInst) ? pInst->PageDlgProc(uMsg, wParam, lParam) : 0;
  183. }
  184. static BOOL CALLBACK AddLangToListBox(ENUMLANG *pel, LPVOID pUser)
  185. {
  186. INT index;
  187. LANGREC *pr;
  188. HWND hwndLB = (HWND)pUser;
  189. if (!hwndLB) return FALSE;
  190. pr = (LANGREC*)calloc(1, sizeof(LANGREC));
  191. if (!pr) return FALSE;
  192. pr->nType = pel->nType;
  193. if (pel->pszFileName) pr->pszFileName = _wcsdup(pel->pszFileName);
  194. index = (INT)SendMessageW(hwndLB, LB_ADDSTRING, 0, (LPARAM)pel->pszName);
  195. if (LB_ERR != index) SendMessageW(hwndLB, LB_SETITEMDATA, index, (LPARAM)pr);
  196. else
  197. {
  198. if (pr->pszFileName) free(pr->pszFileName);
  199. free(pr);
  200. }
  201. return TRUE;
  202. }
  203. static INT ListBox_FindLangFileIndex(HWND hwndLB, LPCWSTR pszLangPath)
  204. {
  205. int index, count;
  206. if (!hwndLB) return LB_ERR;
  207. index = LB_ERR;
  208. count = (INT)SendMessageW(hwndLB, LB_GETCOUNT, 0, 0L);
  209. for (index = 0; index < count; index++)
  210. {
  211. LANGREC *pr = (LANGREC*) SendMessageW(hwndLB, LB_GETITEMDATA, index, 0L);
  212. if (!pr || LB_ERR == (INT)(INT_PTR)pr) continue;
  213. if (!pszLangPath || !*pszLangPath)
  214. {
  215. if (LANG_FILETYPE_EMBED == pr->nType && (!pr->pszFileName || !*pr->pszFileName)) break;
  216. }
  217. else
  218. {
  219. if (CSTR_EQUAL == ComparePath(pszLangPath, pr->pszFileName, LANGDIR)) break;
  220. }
  221. }
  222. return (count == index) ? LB_ERR : index;
  223. }
  224. static LPCWSTR ListBox_GetSelectedLang(HWND hwndLB, LPWSTR pszLangPath, INT cchLen)
  225. {
  226. INT index;
  227. LANGREC *pr;
  228. if (!hwndLB || !pszLangPath) return NULL;
  229. index = (INT)SendMessageW(hwndLB, LB_GETCURSEL, 0, 0L);
  230. if (LB_ERR == index) return NULL;
  231. pr = (LANGREC*)SendMessageW(hwndLB, LB_GETITEMDATA, index, 0L);
  232. if (!pr || LB_ERR == (INT)(INT_PTR)pr) return NULL;
  233. if (!pr->pszFileName || !*pr->pszFileName) pszLangPath[0] = 0x00;
  234. else if (S_OK != StringCchCopyW(pszLangPath, cchLen, pr->pszFileName)) return NULL;
  235. return pszLangPath;
  236. }
  237. #ifdef CBCLASS
  238. #undef CBCLASS
  239. #endif
  240. #define CBCLASS setup_page_lang
  241. START_DISPATCH
  242. CB(ADDREF, AddRef)
  243. CB(RELEASE, Release)
  244. CB(API_SETUPPAGE_GET_NAME, GetName)
  245. CB(API_SETUPPAGE_CREATEVIEW, CreateView)
  246. CB(API_SETUPPAGE_SAVE, Save)
  247. CB(API_SETUPPAGE_REVERT, Revert)
  248. CB(API_SETUPPAGE_ISDIRTY, IsDirty)
  249. CB(API_SETUPPAGE_VALIDATE, Validate)
  250. END_DISPATCH
  251. #undef CBCLASS