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.

513 lines
18 KiB

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