Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

582 lines
17 KiB

  1. //////////////////////////////////////////////////////////////////
  2. // File : exres.cpp
  3. // Owner : ToshiaK
  4. // Purpose : Wrapper function for Gettting resource with Specified
  5. // language ID.
  6. // In WinNT, GetThreadLocale() SetThreadLocale() works
  7. // and before getting resource, change LangId temporary,
  8. // call normal API for getting resource,
  9. // and reset LangID to previous one.
  10. // In Win95, SetThreadLocale() does NOT work.
  11. // in this case, Load resource directory and
  12. // find spcified language resource.
  13. //
  14. // Copyright(c) 1991-1997, Microsoft Corp. All rights reserved
  15. //
  16. //////////////////////////////////////////////////////////////////
  17. #define WIN32_LEAN_AND_MEAN
  18. #include <windows.h>
  19. #include <windowsx.h>
  20. #include "exres.h"
  21. //----------------------------------------------------------------
  22. // Internal memory Allocate Free function.
  23. //----------------------------------------------------------------
  24. inline LPVOID ExMemAlloc(INT size)
  25. {
  26. return (LPVOID)GlobalAllocPtr(GHND, (size));
  27. }
  28. inline BOOL ExMemFree(LPVOID lp)
  29. {
  30. #ifndef UNDER_CE
  31. return GlobalFreePtr((lp));
  32. #else // UNDER_CE
  33. return (BOOL)GlobalFreePtr((lp));
  34. #endif // UNDER_CE
  35. }
  36. inline Min(INT a, INT b)
  37. {
  38. return ((a)<(b)?(a):(b)) ;
  39. }
  40. //----------------------------------------------------------------
  41. // Function for Getting OS version
  42. //----------------------------------------------------------------
  43. inline static POSVERSIONINFO ExGetOSVersion(VOID)
  44. {
  45. static BOOL fFirst = TRUE;
  46. static OSVERSIONINFO os;
  47. if ( fFirst ) {
  48. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  49. if (GetVersionEx( &os ) ) {
  50. fFirst = FALSE;
  51. }
  52. }
  53. return &os;
  54. }
  55. inline static BOOL ExIsWin95(VOID)
  56. {
  57. BOOL fBool;
  58. fBool = (ExGetOSVersion()->dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
  59. (ExGetOSVersion()->dwMajorVersion >= 4) &&
  60. (ExGetOSVersion()->dwMinorVersion < 10);
  61. return fBool;
  62. }
  63. inline static BOOL ExIsWin98(VOID)
  64. {
  65. BOOL fBool;
  66. fBool = (ExGetOSVersion()->dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
  67. (ExGetOSVersion()->dwMajorVersion >= 4) &&
  68. (ExGetOSVersion()->dwMinorVersion >= 10);
  69. return fBool;
  70. }
  71. inline static BOOL ExIsWinNT4(VOID)
  72. {
  73. BOOL fBool;
  74. fBool = (ExGetOSVersion()->dwPlatformId == VER_PLATFORM_WIN32_NT) &&
  75. (ExGetOSVersion()->dwMajorVersion >= 4) &&
  76. (ExGetOSVersion()->dwMinorVersion >= 0);
  77. return fBool;
  78. }
  79. inline static BOOL ExIsWinNT5(VOID)
  80. {
  81. BOOL fBool;
  82. fBool = (ExGetOSVersion()->dwPlatformId == VER_PLATFORM_WIN32_NT) &&
  83. (ExGetOSVersion()->dwMajorVersion >= 5) &&
  84. (ExGetOSVersion()->dwMinorVersion >= 0);
  85. return fBool;
  86. }
  87. inline static BOOL ExIsWinNT(VOID)
  88. {
  89. return (ExIsWinNT4() || ExIsWinNT5());
  90. }
  91. //----------------------------------------------------------------
  92. // Resource API open to public
  93. //----------------------------------------------------------------
  94. //////////////////////////////////////////////////////////////////
  95. // Function : ExLoadStringW
  96. // Type : INT
  97. // Purpose : Wrapper of LoadStrinW() API.
  98. // Load Unicode string with specified Language
  99. // in any platform.
  100. // Args :
  101. // : LANGID lgid
  102. // : HINSTANCE hInst
  103. // : UINT uID
  104. // : LPWSTR lpBuffer
  105. // : INT nBufferMax
  106. // Return :
  107. // DATE : 971028
  108. //////////////////////////////////////////////////////////////////
  109. INT WINAPI ExLoadStringW(LANGID lgid, HINSTANCE hInst, UINT uID, LPWSTR lpBuffer, INT nBufferMax)
  110. {
  111. if(!hInst) {
  112. return 0;
  113. }
  114. if(!lpBuffer) {
  115. return 0;
  116. }
  117. #if 0
  118. if(ExIsWinNT()) {
  119. LCID lcidOrig = GetThreadLocale();
  120. SetThreadLocale(MAKELCID(lgid, SORT_DEFAULT));
  121. INT ret = LoadStringW(hInst, uID, lpBuffer, nBufferMax);
  122. SetThreadLocale(lcidOrig);
  123. return ret;
  124. }
  125. #endif
  126. INT len = 0;
  127. UINT block, num;
  128. block = (uID >>4)+1;
  129. num = uID & 0xf;
  130. HRSRC hres;
  131. hres = FindResourceEx(hInst,
  132. RT_STRING,
  133. MAKEINTRESOURCE(block),
  134. (WORD)lgid);
  135. //Dbg(("hres[0x%08x]\n", hres));
  136. if(!hres) {
  137. goto Error;
  138. }
  139. HGLOBAL hgbl;
  140. hgbl = LoadResource(hInst, hres);
  141. if(!hres) {
  142. goto Error;
  143. }
  144. //Dbg(("hgbl[0x%08x]\n", hgbl));
  145. LPWSTR lpwstr;
  146. lpwstr = (LPWSTR)LockResource(hgbl);
  147. if(!lpwstr) {
  148. goto Error;
  149. }
  150. UINT i;
  151. for(i = 0; i < num; i++) {
  152. lpwstr += *lpwstr + 1;
  153. }
  154. len = *lpwstr;
  155. CopyMemory(lpBuffer, lpwstr+1, Min(len, nBufferMax-1) * sizeof(WCHAR));
  156. lpBuffer[Min(len, nBufferMax-1)]= (WCHAR)0x0000;
  157. Error:
  158. return len;
  159. }
  160. //////////////////////////////////////////////////////////////////
  161. // Function : ExLoadStringA
  162. // Type : INT
  163. // Purpose : Wrapper of LoadStringA().
  164. // Args :
  165. // : LANGID lgid
  166. // : HINSTANCE hInst
  167. // : INT uID
  168. // : LPSTR lpBuffer
  169. // : INT nBufferMax
  170. // Return :
  171. // DATE :
  172. //////////////////////////////////////////////////////////////////
  173. INT WINAPI ExLoadStringA(LANGID lgid, HINSTANCE hInst, INT uID, LPSTR lpBuffer, INT nBufferMax)
  174. {
  175. if(!hInst) {
  176. return 0;
  177. }
  178. if(!lpBuffer) {
  179. return 0;
  180. }
  181. #if 0
  182. if(ExIsWinNT()) {
  183. LCID lcidOrig = GetThreadLocale();
  184. SetThreadLocale(MAKELCID(lgid, SORT_DEFAULT));
  185. INT len = LoadStringA(hInst, uID, lpBuffer, nBufferMax);
  186. SetThreadLocale(lcidOrig);
  187. return len;
  188. }
  189. #endif
  190. LPWSTR lpwstr = (LPWSTR)ExMemAlloc(nBufferMax*sizeof(WCHAR));
  191. if(!lpwstr) {
  192. return 0;
  193. }
  194. INT len = ExLoadStringW(lgid, hInst, uID, lpwstr, nBufferMax);
  195. len = WideCharToMultiByte(932,
  196. WC_COMPOSITECHECK,
  197. lpwstr, -1,
  198. lpBuffer, nBufferMax,
  199. NULL, NULL);
  200. if( len ) {
  201. len --; // remove NULL char
  202. }
  203. ExMemFree(lpwstr);
  204. return len;
  205. }
  206. //////////////////////////////////////////////////////////////////
  207. // Function : ExDialogBoxParamA
  208. // Type : int
  209. // Purpose :
  210. // Args :
  211. // : LANGID lgid
  212. // : HINSTANCE hInstance // handle to application instance
  213. // : LPCTSTR lpTemplateName // identifies dialog box template
  214. // : HWND hWndParent // handle to owner window
  215. // : DLGPROC lpDialogFunc // pointer to dialog box procedure
  216. // : LPARAM dwInitParam // initialization value
  217. // Return :
  218. // DATE :
  219. //////////////////////////////////////////////////////////////////
  220. int WINAPI ExDialogBoxParamA(LANGID lgid,
  221. HINSTANCE hInstance,
  222. LPCTSTR lpTemplateName,
  223. HWND hWndParent,
  224. DLGPROC lpDialogFunc,
  225. LPARAM dwInitParam)
  226. {
  227. DLGTEMPLATE*pDlgTmpl;
  228. pDlgTmpl = ExLoadDialogTemplate(lgid, hInstance, lpTemplateName);
  229. if(ExIsWinNT5()) {
  230. return (INT)DialogBoxIndirectParamW(hInstance,
  231. pDlgTmpl,
  232. hWndParent,
  233. lpDialogFunc,
  234. dwInitParam);
  235. }
  236. else {
  237. return (INT)DialogBoxIndirectParamA(hInstance,
  238. pDlgTmpl,
  239. hWndParent,
  240. lpDialogFunc,
  241. dwInitParam);
  242. }
  243. }
  244. //////////////////////////////////////////////////////////////////
  245. // Function : ExDialogBoxParamW
  246. // Type : int
  247. // Purpose :
  248. // Args :
  249. // : LANGID lgid
  250. // : HINSTANCE hInstance // handle to application instance
  251. // : LPCWSTR lpTemplateName // identifies dialog box template
  252. // : HWND hWndParent // handle to owner window
  253. // : DLGPROC lpDialogFunc // pointer to dialog box procedure
  254. // : LPARAM dwInitParam // initialization value
  255. // Return :
  256. // DATE :
  257. //////////////////////////////////////////////////////////////////
  258. int WINAPI ExDialogBoxParamW(LANGID lgid,
  259. HINSTANCE hInstance,
  260. LPCWSTR lpTemplateName,
  261. HWND hWndParent,
  262. DLGPROC lpDialogFunc,
  263. LPARAM dwInitParam)
  264. {
  265. DLGTEMPLATE*pDlgTmpl;
  266. #ifndef UNDER_CE
  267. pDlgTmpl = ExLoadDialogTemplate(lgid, hInstance, MAKEINTRESOURCEA(lpTemplateName));
  268. #else // UNDER_CE
  269. pDlgTmpl = ExLoadDialogTemplate(lgid, hInstance, MAKEINTRESOURCE(lpTemplateName));
  270. #endif // UNDER_CE
  271. return (INT)DialogBoxIndirectParamW(hInstance,
  272. pDlgTmpl,
  273. hWndParent,
  274. lpDialogFunc,
  275. dwInitParam);
  276. }
  277. //////////////////////////////////////////////////////////////////
  278. // Function : ExCreateDialogParamA
  279. // Type : HWND
  280. // Purpose :
  281. // Args :
  282. // : LANGID lgid
  283. // : HINSTANCE hInstance // handle to application instance
  284. // : LPCTSTR lpTemplateName // identifies dialog box template
  285. // : HWND hWndParent // handle to owner window
  286. // : DLGPROC lpDialogFunc // pointer to dialog box procedure
  287. // : LPARAM dwInitParam // initialization value
  288. // Return :
  289. // DATE :
  290. //////////////////////////////////////////////////////////////////
  291. HWND WINAPI ExCreateDialogParamA(LANGID lgid,
  292. HINSTANCE hInstance,
  293. LPCTSTR lpTemplateName,
  294. HWND hWndParent,
  295. DLGPROC lpDialogFunc,
  296. LPARAM dwInitParam)
  297. {
  298. DLGTEMPLATE*pDlgTmpl;
  299. pDlgTmpl = ExLoadDialogTemplate(lgid, hInstance, lpTemplateName);
  300. if(ExIsWinNT5()) {
  301. return CreateDialogIndirectParamW( hInstance, pDlgTmpl, hWndParent, lpDialogFunc, dwInitParam);
  302. }
  303. else {
  304. return CreateDialogIndirectParamA( hInstance, pDlgTmpl, hWndParent, lpDialogFunc, dwInitParam);
  305. }
  306. }
  307. //////////////////////////////////////////////////////////////////
  308. // Function : ExCreateDialogParamW
  309. // Type : HWND
  310. // Purpose :
  311. // Args :
  312. // : LANGID lgid
  313. // : HINSTANCE hInstance // handle to application instance
  314. // : LPCTSTR lpTemplateName // identifies dialog box template
  315. // : HWND hWndParent // handle to owner window
  316. // : DLGPROC lpDialogFunc // pointer to dialog box procedure
  317. // : LPARAM dwInitParam // initialization value
  318. // Return :
  319. // DATE :
  320. //////////////////////////////////////////////////////////////////
  321. HWND WINAPI ExCreateDialogParamW(LANGID lgid,
  322. HINSTANCE hInstance,
  323. LPCWSTR lpTemplateName,
  324. HWND hWndParent,
  325. DLGPROC lpDialogFunc,
  326. LPARAM dwInitParam)
  327. {
  328. DLGTEMPLATE*pDlgTmpl;
  329. #ifndef UNDER_CE
  330. pDlgTmpl = ExLoadDialogTemplate(lgid, hInstance, MAKEINTRESOURCEA(lpTemplateName));
  331. #else // UNDER_CE
  332. pDlgTmpl = ExLoadDialogTemplate(lgid, hInstance, MAKEINTRESOURCE(lpTemplateName));
  333. #endif // UNDER_CE
  334. return CreateDialogIndirectParamW( hInstance, pDlgTmpl, hWndParent, lpDialogFunc, dwInitParam);
  335. }
  336. //////////////////////////////////////////////////////////////////
  337. // Function : ExLoadDialogTemplate
  338. // Type : DLGTEMPLATE *
  339. // Purpose :
  340. // Args :
  341. // : LANGID lgid
  342. // : HINSTANCE hInstance
  343. // : LPCSTR pchTemplate
  344. // Return :
  345. // DATE :
  346. //////////////////////////////////////////////////////////////////
  347. DLGTEMPLATE * WINAPI ExLoadDialogTemplate(LANGID lgid,
  348. HINSTANCE hInstance,
  349. #ifndef UNDER_CE
  350. LPCSTR pchTemplate)
  351. #else // UNDER_CE
  352. LPCTSTR pchTemplate)
  353. #endif // UNDER_CE
  354. {
  355. HRSRC hResDlg;
  356. HANDLE hDlgTmpl;
  357. #ifndef UNDER_CE
  358. hResDlg = FindResourceExA( hInstance, RT_DIALOG, pchTemplate, lgid);
  359. #else // UNDER_CE
  360. hResDlg = FindResourceEx(hInstance, RT_DIALOG, pchTemplate, lgid);
  361. #endif // UNDER_CE
  362. if((hResDlg == NULL) && (lgid != MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL))) {
  363. #ifndef UNDER_CE
  364. hResDlg = FindResourceExA(hInstance,
  365. RT_DIALOG,
  366. pchTemplate,
  367. MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL));
  368. #else // UNDER_CE
  369. hResDlg = FindResourceEx(hInstance,
  370. RT_DIALOG,
  371. pchTemplate,
  372. MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL));
  373. #endif // UNDER_CE
  374. }
  375. if (hResDlg == NULL) {
  376. return NULL;
  377. }
  378. hDlgTmpl = LoadResource( hInstance, hResDlg );
  379. if(hDlgTmpl == NULL) {
  380. return NULL; /* failed */
  381. }
  382. return (DLGTEMPLATE *)LockResource( hDlgTmpl );
  383. }
  384. //////////////////////////////////////////////////////////////////
  385. // Function : ExLoadMenuTemplate
  386. // Type : MENUTEMPLATE *
  387. // Purpose :
  388. // Args :
  389. // : LANGID lgid
  390. // : HINSTANCE hInstance
  391. // : LPCSTR pchTemplate
  392. // Return :
  393. // DATE :
  394. //////////////////////////////////////////////////////////////////
  395. static MENUTEMPLATE* ExLoadMenuTemplate(LANGID lgid,
  396. HINSTANCE hInstance,
  397. #ifndef UNDER_CE
  398. LPCSTR pchTemplate)
  399. #else // UNDER_CE
  400. LPCTSTR pchTemplate)
  401. #endif // UNDER_CE
  402. {
  403. HRSRC hResMenu;
  404. HANDLE hMenuTmpl;
  405. hResMenu = FindResourceEx( hInstance, RT_MENU, pchTemplate, lgid);
  406. if((hResMenu == NULL) && (lgid != MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL))) {
  407. hResMenu = FindResourceEx(hInstance,
  408. RT_MENU,
  409. pchTemplate,
  410. MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL));
  411. }
  412. if (hResMenu == NULL) {
  413. return NULL;
  414. }
  415. hMenuTmpl = LoadResource( hInstance, hResMenu );
  416. if(hMenuTmpl == NULL) {
  417. return NULL; /* failed */
  418. }
  419. return (MENUTEMPLATE *)LockResource( hMenuTmpl );
  420. }
  421. //////////////////////////////////////////////////////////////////
  422. // Function : ExLoadMenu
  423. // Type : HMENU
  424. // Purpose :
  425. // Args :
  426. // : LANGID lgid
  427. // : HINSTANCE hInstance // handle to application instance
  428. // : LPCTSTR lpMenuName // identifies menu template
  429. // Return :
  430. // DATE :
  431. //////////////////////////////////////////////////////////////////
  432. HMENU WINAPI ExLoadMenu (LANGID lgid,
  433. HINSTANCE hInstance,
  434. LPCTSTR lpMenuName )
  435. {
  436. #ifndef UNDER_CE // not support LoadMenuIndirect
  437. MENUTEMPLATE* pMenuTmpl;
  438. pMenuTmpl = ExLoadMenuTemplate(lgid, hInstance, lpMenuName);
  439. return LoadMenuIndirect( pMenuTmpl );
  440. #else // UNDER_CE
  441. return ::LoadMenu(hInstance, lpMenuName);
  442. #endif // UNDER_CE
  443. }
  444. //////////////////////////////////////////////////////////////////
  445. // Function : SetDefaultGUIFont
  446. // Type : static INT
  447. // Purpose : Searh All children window and Call SendMessage()
  448. // with WM_SETFONT.
  449. // It is called recursively.
  450. // Args :
  451. // : HWND hwndParent
  452. // Return :
  453. // DATE :
  454. //////////////////////////////////////////////////////////////////
  455. static INT SetDefaultGUIFont(HWND hwndParent)
  456. {
  457. HWND hwndChild;
  458. if(!hwndParent) {
  459. return 0;
  460. }
  461. SendMessage(hwndParent,
  462. WM_SETFONT,
  463. (WPARAM)(HFONT)GetStockObject(DEFAULT_GUI_FONT),
  464. MAKELPARAM(TRUE, 0));
  465. for(hwndChild = GetWindow(hwndParent, GW_CHILD);
  466. hwndChild != NULL;
  467. hwndChild = GetWindow(hwndChild, GW_HWNDNEXT)) {
  468. SetDefaultGUIFont(hwndChild);
  469. }
  470. return 0;
  471. }
  472. //////////////////////////////////////////////////////////////////
  473. // Function : SetDefaultGUIFontEx
  474. // Type : static INT
  475. // Purpose : Searh All children window and Call SendMessage()
  476. // with WM_SETFONT.
  477. // It is called recursively.
  478. // Args :
  479. // : HWND hwndParent
  480. // : HFONT hFont
  481. // Return :
  482. // DATE :
  483. //////////////////////////////////////////////////////////////////
  484. static INT SetDefaultGUIFontEx(HWND hwndParent, HFONT hFont)
  485. {
  486. HWND hwndChild;
  487. if(!hwndParent) {
  488. return 0;
  489. }
  490. SendMessage(hwndParent,
  491. WM_SETFONT,
  492. (WPARAM)hFont,
  493. MAKELPARAM(TRUE, 0));
  494. for(hwndChild = GetWindow(hwndParent, GW_CHILD);
  495. hwndChild != NULL;
  496. hwndChild = GetWindow(hwndChild, GW_HWNDNEXT)) {
  497. SetDefaultGUIFontEx(hwndChild, hFont);
  498. }
  499. return 0;
  500. }
  501. //////////////////////////////////////////////////////////////////
  502. // Function : WINAPI ExSetDefaultGUIFont
  503. // Type : VOID
  504. // Purpose : Change GUI font as DEFAULT_GUI_FONT
  505. // In Win95, WinNT4, DEFAULT_GUI_FONT is "�l�r �o �S�V�b�N"
  506. // In Memphis, WinNT5.0 DEFAULT_GUI_FONT is "MS UI Gothic"
  507. // IME98's Dialog resource uses "MS UI Gothic" as their font.
  508. // if IME98 works in Win95 or WinNT40, This API Call SendMessage() with WM_SETFONT
  509. // to all children window.
  510. // It should be called in WM_INITDIALOG. If you are creating new child window,
  511. // You have to call it after new window was created.
  512. // Args :
  513. // : HWND hwndDlg: Set the Dialog window handle to change font.
  514. // Return : none
  515. // DATE :
  516. //////////////////////////////////////////////////////////////////
  517. VOID WINAPI ExSetDefaultGUIFont(HWND hwndDlg)
  518. {
  519. //It is Valid only if platform is WinNT4.0 or Win95
  520. //if(ExIsWinNT5() || ExIsWin98()) {
  521. SetDefaultGUIFont(hwndDlg);
  522. UpdateWindow(hwndDlg);
  523. //}
  524. return;
  525. }
  526. //////////////////////////////////////////////////////////////////
  527. // Function : WINAPI ExSetDefaultGUIFontEx
  528. // Type : VOID
  529. // Purpose : Change GUI font to given font.
  530. // It should be called in WM_INITDIALOG. If you are creating new child window,
  531. // you have to call it after new window was created.
  532. // If hFont is NULL, it will call ExSetDefaultGUIFont
  533. // Args :
  534. // : HWND hwndDlg: Set the Dialog window handle to change font.
  535. // : HFONT hFont : Font handle which will be applied to.
  536. // Return : none
  537. // DATE :
  538. //////////////////////////////////////////////////////////////////
  539. VOID WINAPI ExSetDefaultGUIFontEx(HWND hwndDlg, HFONT hFont)
  540. {
  541. if(NULL == hFont){
  542. ExSetDefaultGUIFont(hwndDlg);
  543. }else{
  544. SetDefaultGUIFontEx(hwndDlg, hFont);
  545. UpdateWindow(hwndDlg);
  546. }
  547. return;
  548. }