Source code of Windows XP (NT5)
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.

1078 lines
31 KiB

  1. /**************************************************************************\
  2. * chsuconv.c -- convert to/from unicode using
  3. * MulitByteToWideChar & WideCharToMulitByte
  4. *
  5. * Steve Firebaugh
  6. * Microsoft Developer Support
  7. * Copyright (C) 1992-1999 Microsoft Inc.
  8. *
  9. \**************************************************************************/
  10. #include "chnuconv.h"
  11. #include "resource.h"
  12. /**************************************************************************\
  13. * Global Data Items
  14. \**************************************************************************/
  15. /* Global variables storing the source and destination "type" information.
  16. *
  17. * used to communicate between main wnd proc, and *OptionsProc.
  18. *
  19. * gTypeSource - stores the type interpretation of the source data
  20. * (and implicitly the destination data.)
  21. * TYPEUNKNOWN: indeterminant... not set. Can not do conversion.
  22. * TYPEUNICODE: source unicode & destination giDestinationCodePage.
  23. * TYPECODEPAGE: source giSourceCodePage & destination unicode.
  24. *
  25. * giSourceCodePage stores valid source code page iff gTypeSource == TRUE
  26. * giDestinationCodePage stores valid destination code page iff gTypeSource == FALSE
  27. *
  28. */
  29. int gTypeSource;
  30. int gTypeSourceID;
  31. UINT giSourceCodePage;
  32. int gTypeDest;
  33. int gTypeDestID;
  34. UINT giDestinationCodePage;
  35. int giRBInit;
  36. TCHAR szDataOptionsDlg[40];
  37. /* Pointers to the source and destination data, and the
  38. * count of bytes in each of the buffers.
  39. */
  40. PBYTE pSourceData = NULL;
  41. PBYTE pDestinationData = NULL;
  42. PBYTE pTempData1 = NULL;
  43. int nBytesSource = NODATA;
  44. int nBytesDestination = NODATA;
  45. PBYTE pTempData = NULL;
  46. PBYTE pViewSourceData = NULL;
  47. int nBytesTemp = NODATA;
  48. BOOL gSourceSwapped=FALSE;
  49. BOOL gDestSwapped=FALSE;
  50. /* Conversion Options variables. */
  51. DWORD gMBFlags = MB_PRECOMPOSED;
  52. DWORD gWCFlags = 0;
  53. char glpDefaultChar[4] = "?"; // what is max size of multibyte character?
  54. BOOL gUsedDefaultChar = FALSE;
  55. DWORD gTCSCMapStatus = DONOTMAP; //simplified and traditional Chinese mapping
  56. DWORD gFWHWMapStatus = DONOTMAP; //full width and half width mapping
  57. /* Handling the Byte Order Mark (BOM).
  58. *
  59. * If the input file begins with a BOM, then we know it is unicode,
  60. * we skip over the BOM and decrement the size of data by SIZEOFBOM.
  61. *
  62. *
  63. * Before writing data that we know is unicode, write the szBOM string
  64. * to the file.
  65. *
  66. * Notice that this means that the file sizes we show in the window
  67. * do NOT include the BOM.
  68. */
  69. char szBOM[] = "\377\376"; // 0xFF, 0xFE // leave off TEXT() macro.
  70. char szRBOM[] = "\376\377"; // 0xFF, 0xFE // leave off TEXT() macro.
  71. UINT MBFlags = MB_OK | MB_ICONEXCLAMATION;
  72. TCHAR MBTitle[40];
  73. TCHAR MBMessage[EXTENSION_LENGTH];
  74. TCHAR szBlank[] = TEXT("");
  75. TCHAR szNBytes[40];
  76. TCHAR szFilter[MAX_PATH];
  77. TCHAR szHelpPathName[] = TEXT("chnuconv.chm");
  78. DLGTEMPLATE * dlgtSourceTab;
  79. DLGTEMPLATE * dlgtOptionTab;
  80. HGLOBAL hglbMem;
  81. PBYTE p;
  82. int NumCodePage;
  83. UINT uCodepage[]={ 0,//UNICODE
  84. 936, //GB
  85. 950,//BIG5
  86. 20000,//CNS
  87. 20001,//TCA
  88. 20002,//ETEN
  89. 20003,//IBM5
  90. 20004,//TELE
  91. 20005//WANG
  92. };
  93. //
  94. // Module handle.
  95. //
  96. HANDLE _hModule;
  97. //
  98. // Application's icon handle.
  99. //
  100. HANDLE _hIcon;
  101. //
  102. // Main window handle.
  103. //
  104. HANDLE _hWndMain;
  105. HANDLE hMainTabControl;
  106. HANDLE hWndDisplay;
  107. HANDLE hWndTab[NUMBER_OF_PAGES];
  108. //
  109. // Application's accelerator table handle.
  110. //
  111. HANDLE _hAccel;
  112. BOOL InitializeApplication( HINSTANCE hInstance, HINSTANCE hPrevInstance);
  113. BOOL MakeTabs( IN HWND hWnd, IN HWND hMainTabControl );
  114. VOID AdjustSelectTab ( IN HWND hWnd, IN HWND hMainTabControl );
  115. LRESULT APIENTRY MainWndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
  116. VOID CreateFilter(PTCHAR);
  117. int APIENTRY
  118. WinMain(
  119. HINSTANCE hInstance,
  120. HINSTANCE hPrevInstance,
  121. LPSTR lpCmdLine,
  122. int nCmdShow)
  123. /*++
  124. Routine Description:
  125. Main is the entry point for Code Converter. It initializes the application
  126. and manages the message pump. When the message pump quits, main performs
  127. some global cleanup.
  128. Arguments:
  129. None.
  130. Return Value:
  131. int - Returns the result of the PostQuitMessgae API or -1 if
  132. initialization failed.
  133. --*/
  134. {
  135. MSG msg;
  136. BOOL bHandled=FALSE;
  137. UNREFERENCED_PARAMETER( lpCmdLine );
  138. UNREFERENCED_PARAMETER( nCmdShow );
  139. if( InitializeApplication(hInstance, hPrevInstance)) {
  140. while( GetMessage( &msg, NULL, 0, 0 )) {
  141. bHandled = TranslateAccelerator( _hWndMain, _hAccel, &msg );
  142. if (bHandled==FALSE)
  143. {
  144. bHandled = TranslateAccelerator(hWndDisplay, _hAccel, &msg);
  145. if (bHandled == FALSE && IsDialogMessage(_hWndMain, &msg)==FALSE)
  146. {
  147. TranslateMessage(&msg); // Translates virtual key codes
  148. DispatchMessage(&msg); // Dispatches message to window
  149. }
  150. }
  151. }
  152. return (int)msg.wParam;
  153. }
  154. //
  155. // Initialization failed.
  156. //
  157. return -1;
  158. }
  159. BOOL
  160. InitializeApplication(
  161. HINSTANCE hInstance,
  162. HINSTANCE hPrevInstance
  163. )
  164. /*++
  165. Routine Description:
  166. InitializeApplication does just what its name implies. It initializes
  167. global varaibles, sets up global state (e.g. 3D-Controls), registers window
  168. classes and creates and shows the application's main window.
  169. Arguments:
  170. None.
  171. Return Value:
  172. BOOL - Returns TRUE if the application succesfully initialized.
  173. --*/
  174. {
  175. WNDCLASS Wc;
  176. TCHAR szBuffer[MAX_PATH];
  177. TCHAR szBuf[400];
  178. int i;
  179. //
  180. // Get the application's module (instance) handle.
  181. //
  182. InitCommonControls();
  183. //_hModule = hInstance;
  184. _hModule = GetModuleHandle( NULL );
  185. if( _hModule == NULL ) {
  186. return FALSE;
  187. }
  188. //
  189. // Load the application's main icon resource.
  190. //
  191. _hIcon = LoadIcon( _hModule, MAKEINTRESOURCE( IDI_CHNUCONV ));
  192. if( _hIcon == NULL ) {
  193. return FALSE;
  194. }
  195. //
  196. // Load the application's accelerator table.
  197. //
  198. _hAccel = LoadAccelerators( _hModule, MAKEINTRESOURCE( IDA_CHNUCONV ));
  199. if( _hAccel == NULL ) {
  200. return FALSE;
  201. }
  202. //
  203. // Register the window class for the application.
  204. //
  205. if (!hPrevInstance){
  206. Wc.style = CS_HREDRAW
  207. | CS_OWNDC
  208. | CS_SAVEBITS
  209. | CS_VREDRAW;
  210. Wc.lpfnWndProc = MainWndProc;
  211. Wc.cbClsExtra = 0;
  212. Wc.cbWndExtra = DLGWINDOWEXTRA;
  213. Wc.hInstance = _hModule;
  214. Wc.hIcon = _hIcon;
  215. Wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  216. Wc.hbrBackground = ( HBRUSH ) ( COLOR_BTNFACE + 1 );
  217. Wc.lpszMenuName = NULL;
  218. Wc.lpszClassName = L"Converter";
  219. if (!RegisterClass( &Wc ))
  220. return FALSE;
  221. }
  222. for(i=0;i<NumCodePage;i++)
  223. LoadString(_hModule,IDS_STRUNICODE+i,gszCodePage[i], EXTENSION_LENGTH);
  224. // For different locale we will have different default initial code page
  225. // and differnt source and dest. dialog box.
  226. switch(GetACP())
  227. {
  228. case 936:
  229. giRBInit=IDC_RBGB;
  230. NumCodePage=3;
  231. break;
  232. case 950:
  233. giRBInit=IDC_RBBIG5;
  234. NumCodePage=9;
  235. break;
  236. default:
  237. giRBInit=IDC_RBBIG5;
  238. NumCodePage=9;
  239. }
  240. dlgtSourceTab=DoLockDlgRes(MAKEINTRESOURCE(IDD_SOURCE_TAB));
  241. dlgtOptionTab=DoLockDlgRes(MAKEINTRESOURCE(IDD_OPTION_TAB));
  242. //
  243. // Create the main window.
  244. //
  245. _hWndMain = CreateDialog ( _hModule,
  246. MAKEINTRESOURCE( IDD_CHNUCONV ),
  247. NULL,
  248. (DLGPROC) MainWndProc,
  249. );
  250. if( _hWndMain == NULL ) {
  251. return FALSE;
  252. }
  253. //
  254. // Set the window title.
  255. //
  256. LoadString(_hModule, IDS_APPLICATION_NAME, szBuffer, EXTENSION_LENGTH);
  257. lstrcpy(MBTitle, szBuffer);
  258. LoadString(_hModule, IDS_NBYTES,szNBytes, EXTENSION_LENGTH);
  259. CreateFilter(szFilter);
  260. SetWindowText(_hWndMain, szBuffer);
  261. ShowWindow( _hWndMain, SW_SHOW );
  262. return TRUE;
  263. }
  264. LRESULT
  265. MainWndProc(
  266. HWND hWnd,
  267. UINT message,
  268. WPARAM wParam,
  269. LPARAM lParam
  270. )
  271. {
  272. TCHAR szBuffer[MAX_PATH];
  273. switch( message ) {
  274. case WM_PAINT:
  275. {
  276. PAINTSTRUCT ps;
  277. RECT rc;
  278. //
  279. // Don't waste our time if we're minimized
  280. //
  281. if (FALSE == IsIconic(hWnd))
  282. {
  283. BeginPaint(hWnd, &ps);
  284. GetClientRect(hWnd, &rc);
  285. //
  286. // Draw an edge just below menu bar
  287. //
  288. DrawEdge(ps.hdc, &rc, EDGE_ETCHED, BF_TOP);
  289. EndPaint(hWnd, &ps);
  290. }
  291. return TRUE;
  292. }
  293. case WM_CREATE:
  294. {
  295. int ScreenHeight;
  296. int ScreenWidth;
  297. //
  298. // Display the main window in the center of the screen.
  299. //
  300. ScreenWidth = GetSystemMetrics( SM_CXSCREEN );
  301. ScreenHeight = GetSystemMetrics( SM_CYSCREEN );
  302. if(SetWindowPos(
  303. hWnd,
  304. NULL,
  305. ( ScreenWidth - (( LPCREATESTRUCT ) lParam )->cx )/2,
  306. ( ScreenHeight - (( LPCREATESTRUCT ) lParam )->cy )/2,
  307. 0,
  308. 0,
  309. SWP_NOSIZE
  310. | SWP_NOREDRAW
  311. | SWP_NOZORDER
  312. ))
  313. return 0;
  314. }
  315. case WM_INITDIALOG:
  316. {
  317. //
  318. // Get the handle of the tab control
  319. //
  320. hMainTabControl = GetDlgItem( hWnd, IDC_MAIN_TAB );
  321. //
  322. // Fill out tab control with appropriate tabs
  323. //
  324. MakeTabs( hWnd, hMainTabControl );
  325. hWndTab[0] = CreateDialogIndirect(
  326. _hModule,
  327. dlgtSourceTab,
  328. hWnd,
  329. SourceTabProc
  330. );
  331. hWndTab[1]= CreateDialogIndirect(
  332. _hModule,
  333. dlgtSourceTab,
  334. hWnd,
  335. TargetTabProc
  336. );
  337. hWndTab[2] = CreateDialogIndirect(
  338. _hModule,
  339. dlgtOptionTab,
  340. hWnd,
  341. OptionTabProc
  342. );
  343. hWndDisplay=hWndTab[0];
  344. ShowWindow(hWndDisplay, SW_SHOW);
  345. return( TRUE );
  346. }
  347. case WM_COMMAND:
  348. {
  349. switch (LOWORD(wParam)) {
  350. /******************************************************************\
  351. * WM_COMMAND, IDC_PUSH_CONVERT
  352. *
  353. * This is where the conversion actually takes place.
  354. * In either case, make the call twice. Once to determine how
  355. * much memory is needed, allocate space, and then make the call again.
  356. *
  357. * If conversion succeeds, it fills pDestinationData.
  358. \******************************************************************/
  359. case IDC_PUSH_CONVERT: {
  360. int nBytesNeeded, nWCharNeeded, nWCharSource;
  361. TCHAR szSourceName[256];
  362. int ConfirmMap;
  363. int i;
  364. GetSettings();
  365. SwapSource(FALSE);
  366. if (nBytesSource == NODATA ) {
  367. LoadString(_hModule,IDS_NOTEXTCONVERT,MBMessage,EXTENSION_LENGTH);
  368. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  369. return 0;
  370. }
  371. /* Converting UNICODE -> giDestinationCodePage*/
  372. if (gTypeSource == TYPEUNICODE) {
  373. nWCharSource = nBytesSource/2;
  374. /* Allocate the required amount of space, including trailing NULL */
  375. pTempData1= ManageMemory (MMALLOC, MMDESTINATION,
  376. nBytesSource, pTempData1);
  377. // Map string if we need to do TC-SC conversion.
  378. if (gTCSCMapStatus != DONOTMAP){
  379. LCMapString (GetUserDefaultLCID(),gTCSCMapStatus,
  380. (LPWSTR)pSourceData,nWCharSource,
  381. (LPWSTR)pTempData1,nWCharSource);
  382. }else{
  383. // no conversion, just copy things over.
  384. memcpy(pTempData1,pSourceData,nBytesSource);
  385. }
  386. // Map string if we need to do FW-HW conversion.
  387. if (gFWHWMapStatus != DONOTMAP){
  388. pTempData= ManageMemory (MMALLOC, MMDESTINATION,
  389. nBytesSource, pTempData);
  390. LCMapString (GetUserDefaultLCID(),gFWHWMapStatus,
  391. (LPWSTR)pTempData1,nWCharSource,
  392. (LPWSTR)pTempData,nWCharSource);
  393. memcpy(pTempData1,pTempData,nBytesSource);
  394. }
  395. /*TS/SC mapping could happen between Unicode */
  396. if (gTypeDest == TYPEUNICODE){
  397. pDestinationData= ManageMemory (MMALLOC, MMDESTINATION,
  398. nBytesSource+2, pDestinationData);
  399. //put mapped unicode buffer to destination buffer.
  400. memcpy(pDestinationData,pTempData1,nBytesSource);
  401. nBytesDestination=nBytesSource;
  402. /* Unicode Null terminate string. */
  403. pDestinationData[nBytesDestination] = 0;
  404. pDestinationData[nBytesDestination+1] = 0;
  405. }else{
  406. giDestinationCodePage=uCodepage[gTypeDestID];
  407. /*Query the number of bytes required to store the Dest string */
  408. nBytesNeeded = WideCharToMultiByte(giDestinationCodePage,
  409. gWCFlags, (LPWSTR)pTempData1, nWCharSource,
  410. NULL, 0,
  411. glpDefaultChar, &gUsedDefaultChar);
  412. pDestinationData= ManageMemory (MMALLOC, MMDESTINATION,
  413. nBytesNeeded +1, pDestinationData);
  414. /* Do the conversion */
  415. nBytesDestination = WideCharToMultiByte(giDestinationCodePage,
  416. gWCFlags, (LPWSTR)pTempData1, nWCharSource,
  417. (LPSTR)pDestinationData, nBytesNeeded,
  418. glpDefaultChar, &gUsedDefaultChar);
  419. /* Null terminate string. */
  420. pDestinationData[nBytesDestination] = 0;
  421. }
  422. }
  423. /* converting giSourceCodePage -> UNICODE */
  424. else if (gTypeSource == TYPECODEPAGE && gTypeDest == TYPEUNICODE) {
  425. giSourceCodePage=uCodepage[gTypeSourceID];
  426. /* Query the number of WChar required to store the Dest string */
  427. nWCharNeeded = MultiByteToWideChar(giSourceCodePage, gMBFlags,
  428. pSourceData, nBytesSource, NULL, 0 );
  429. /* Allocate the required amount of space, including trailing NULL */
  430. pDestinationData= ManageMemory (MMALLOC, MMDESTINATION, (nWCharNeeded +1)*2, pDestinationData);
  431. /* Do the conversion */
  432. nWCharNeeded = MultiByteToWideChar(giSourceCodePage, gMBFlags,
  433. pSourceData, nBytesSource,
  434. (LPWSTR)pDestinationData, nWCharNeeded);
  435. /* MultiByteToWideChar returns # WCHAR, so multiply by 2 */
  436. nBytesDestination = 2*nWCharNeeded ;
  437. // Decide if we need to do TC-SC conversion.
  438. if (gTCSCMapStatus != DONOTMAP) {
  439. pTempData1= ManageMemory (MMALLOC, MMDESTINATION, nBytesDestination, pTempData1);
  440. LCMapString (GetUserDefaultLCID(),gTCSCMapStatus,
  441. (LPWSTR)pDestinationData,nWCharNeeded,
  442. (LPWSTR)pTempData1,nWCharNeeded);
  443. memcpy(pDestinationData,pTempData1,nBytesDestination);
  444. }
  445. if (gFWHWMapStatus != DONOTMAP) {
  446. pTempData1= ManageMemory (MMALLOC, MMDESTINATION, nBytesDestination, pTempData1);
  447. LCMapString (GetUserDefaultLCID(),gFWHWMapStatus,
  448. (LPWSTR)pDestinationData,nWCharNeeded,
  449. (LPWSTR)pTempData1,nWCharNeeded);
  450. memcpy(pDestinationData,pTempData1,nBytesDestination);
  451. }
  452. /* Null terminate string. */
  453. pDestinationData[nBytesDestination] = 0; // UNICODE_NULL
  454. pDestinationData[nBytesDestination+1] = 0;
  455. /* converting giSourceCodePage -> giDestinationCodePage */
  456. } else if(gTypeSourceID < NumCodePage && gTypeDestID < NumCodePage){
  457. giSourceCodePage=uCodepage[gTypeSourceID];
  458. /* Query the number of WChar required to store the Dest string */
  459. nWCharNeeded = MultiByteToWideChar(giSourceCodePage, gMBFlags,
  460. pSourceData, nBytesSource, NULL, 0 );
  461. /* Allocate the required amount of space, including trailing NULL */
  462. pTempData= ManageMemory (MMALLOC, MMDESTINATION, (nWCharNeeded +1)*2, pTempData);
  463. /* Do the conversion */
  464. nWCharNeeded = MultiByteToWideChar(giSourceCodePage, gMBFlags,
  465. pSourceData, nBytesSource,
  466. (LPWSTR)pTempData, nWCharNeeded);
  467. /* MultiByteToWideChar returns # WCHAR, so multiply by 2 */
  468. nBytesTemp = 2*nWCharNeeded ;
  469. // Decide if we need to do TC-SC conversion.
  470. if (gTCSCMapStatus != DONOTMAP) {
  471. pTempData1= ManageMemory (MMALLOC, MMDESTINATION, nBytesTemp, pTempData1);
  472. LCMapString (GetUserDefaultLCID(),gTCSCMapStatus,
  473. (LPWSTR)pTempData,nWCharNeeded,
  474. (LPWSTR)pTempData1,nWCharNeeded);
  475. memcpy(pTempData,pTempData1,nBytesTemp);
  476. }
  477. if (gFWHWMapStatus != DONOTMAP) {
  478. pTempData1= ManageMemory (MMALLOC, MMDESTINATION, nBytesTemp, pTempData1);
  479. LCMapString (GetUserDefaultLCID(),gFWHWMapStatus,
  480. (LPWSTR)pTempData,nWCharNeeded,
  481. (LPWSTR)pTempData1,nWCharNeeded);
  482. memcpy(pTempData,pTempData1,nBytesTemp);
  483. }
  484. /* Null terminate string. */
  485. pTempData[nBytesTemp] = 0; // UNICODE_NULL
  486. pTempData[nBytesTemp+1] = 0;
  487. giDestinationCodePage=uCodepage[gTypeDestID];
  488. nWCharSource = nBytesTemp/2;
  489. /* Query the number of bytes required to store the Dest string */
  490. nBytesNeeded = WideCharToMultiByte(giDestinationCodePage, gWCFlags,
  491. (LPWSTR)pTempData, nWCharSource,
  492. NULL, 0,
  493. glpDefaultChar, &gUsedDefaultChar);
  494. /* Allocate the required amount of space, including trailing NULL */
  495. pDestinationData= ManageMemory (MMALLOC, MMDESTINATION, nBytesNeeded +1, pDestinationData);
  496. /* Do the conversion */
  497. nBytesDestination = WideCharToMultiByte(giDestinationCodePage, gWCFlags,
  498. (LPWSTR)pTempData, nWCharSource,
  499. pDestinationData, nBytesNeeded, glpDefaultChar, &gUsedDefaultChar);
  500. /* Null terminate string. */
  501. pDestinationData[nBytesDestination] = 0;
  502. } else {
  503. LoadString(_hModule,IDS_STYPEUNKNOWN,MBMessage,EXTENSION_LENGTH);
  504. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  505. return 0;
  506. }
  507. /* code common to all conversions... */
  508. LoadString(_hModule, IDS_NOTSAVEYET, szBuffer, 50);
  509. TabCtrl_SetCurSel(hMainTabControl, 1);
  510. AdjustSelectTab(hWnd, hMainTabControl);
  511. SetDlgItemText (hWndTab[1], IDC_NAMETEXT, szBuffer);
  512. wsprintf (szBuffer, szNBytes, nBytesDestination);
  513. SetDlgItemText (hWndTab[1], IDC_SIZETEXT, szBuffer);
  514. EnableControl(hWndTab[1], IDC_OPENORSAVEAS, TRUE);
  515. EnableControl(hWndTab[1], IDC_VIEW, TRUE);
  516. EnableControl(hWndTab[1], IDC_PASTEORCOPY, TRUE);
  517. EnableControl(hWndTab[1], IDC_CLEAR, TRUE);
  518. EnableControl(hWndTab[1], IDC_SWAPHIGHLOW, TRUE);
  519. EnableMenuItem (GetMenu (_hWndMain),IDM_FILE_SAVEAS,MF_ENABLED);
  520. break; // end case IDC_PUSH_CONVERT
  521. }
  522. case IDM_HELP_CONTENT:
  523. case IDC_PUSH_HELP:
  524. {
  525. HtmlHelp(hWnd,szHelpPathName, HH_DISPLAY_TOPIC, (DWORD_PTR) NULL );
  526. break;
  527. }
  528. case IDM_HELP_ABOUT:
  529. {
  530. LoadString(_hModule, IDS_APPLICATION_NAME,szBuffer,
  531. EXTENSION_LENGTH);
  532. ShellAbout (hWnd, szBuffer, TEXT(""), _hIcon );
  533. break;
  534. }
  535. break;
  536. case IDM_FILE_OPEN:
  537. {
  538. SendMessage(hWndTab[0], WM_COMMAND, IDC_OPENORSAVEAS,0);
  539. break;
  540. }
  541. case IDM_FILE_SAVEAS:
  542. {
  543. SendMessage(hWndTab[1], WM_COMMAND, IDC_OPENORSAVEAS,0);
  544. break;
  545. }
  546. case IDM_FILE_EXIT:
  547. case IDOK:
  548. PostMessage(hWnd, WM_CLOSE, 0, 0L);
  549. break;
  550. default:
  551. return FALSE;
  552. }
  553. break;
  554. }
  555. case WM_NOTIFY:
  556. {
  557. static
  558. int nPreviousTab = 1;
  559. // switch on notification code
  560. switch ( ((LPNMHDR)lParam)->code ) {
  561. case TCN_SELCHANGE:
  562. {
  563. AdjustSelectTab(hWnd, hMainTabControl);
  564. return(TRUE);
  565. }
  566. }
  567. break;
  568. }
  569. case WM_CLOSE:
  570. {
  571. DestroyWindow( hWnd );
  572. break;
  573. }
  574. case WM_DESTROY:
  575. {
  576. // WinHelp( hwnd, szHelpPathName, (UINT) HELP_QUIT, (DWORD) NULL );
  577. ManageMemory (MMFREE, MMDESTINATION, 0, pTempData1);
  578. ManageMemory (MMFREE, MMSOURCE, 0, pSourceData);
  579. ManageMemory (MMFREE, MMDESTINATION, 0, pDestinationData);
  580. ManageMemory (MMFREE, MMSOURCE, 0, pViewSourceData);
  581. ManageMemory (MMFREE, MMDESTINATION, 0, pTempData);
  582. GlobalUnlock(hglbMem);
  583. GlobalFree(hglbMem);
  584. //
  585. // Destroy the application.
  586. //
  587. PostQuitMessage(0);
  588. return 0;
  589. }
  590. default:
  591. break;
  592. }
  593. return DefWindowProc( hWnd, message, wParam, lParam );
  594. }
  595. INT_PTR
  596. OptionTabProc(
  597. HWND hWnd,
  598. UINT message,
  599. WPARAM wParam,
  600. LPARAM lParam
  601. )
  602. {
  603. switch( message ) {
  604. case WM_INITDIALOG:
  605. {
  606. RECT rcTab;
  607. //GetTab window size
  608. // first get the size of the tab control
  609. if (GetWindowRect( hMainTabControl, &rcTab )){
  610. // adjust it to compensate for the tabs
  611. TabCtrl_AdjustRect( hMainTabControl, FALSE , &rcTab);
  612. // convert the screen coordinates to client coordinates
  613. MapWindowPoints( HWND_DESKTOP, GetParent(hMainTabControl),
  614. (LPPOINT)&rcTab, 2);
  615. }
  616. SetWindowPos(hWnd, HWND_TOP,
  617. rcTab.left,
  618. rcTab.top,
  619. rcTab.right - rcTab.left,
  620. rcTab.bottom - rcTab.top,
  621. SWP_NOACTIVATE );
  622. SendDlgItemMessage(hWnd, IDC_NOSCTCMAP, BM_SETCHECK, 1, 0);
  623. SendDlgItemMessage(hWnd, IDC_NOHWFWMAP, BM_SETCHECK, 1, 0);
  624. return(FALSE);
  625. }
  626. case WM_COMMAND:
  627. {
  628. switch (LOWORD(wParam))
  629. {
  630. case IDC_RESET :
  631. {
  632. SendDlgItemMessage(hWnd, IDC_NOSCTCMAP, BM_SETCHECK, 1, 0);
  633. SendDlgItemMessage(hWnd, IDC_SCTCMAP, BM_SETCHECK, 0, 0);
  634. SendDlgItemMessage(hWnd, IDC_TCSCMAP, BM_SETCHECK, 0, 0);
  635. SendDlgItemMessage(hWnd, IDC_NOHWFWMAP, BM_SETCHECK, 1, 0);
  636. SendDlgItemMessage(hWnd, IDC_HWFWMAP, BM_SETCHECK, 0, 0);
  637. SendDlgItemMessage(hWnd, IDC_FWHWMAP, BM_SETCHECK, 0, 0);
  638. }
  639. break;
  640. }
  641. }
  642. default:
  643. break;
  644. }
  645. return DefWindowProc( hWnd, message, wParam, lParam );
  646. }
  647. VOID
  648. GetSettings()
  649. {
  650. int i;
  651. gTypeSource = TYPEUNKNOWN;
  652. //Get source settings.
  653. for(i=0;i<NumCodePage;i++)
  654. if (SendDlgItemMessage(hWndTab[0],IDC_RBUNICODE1+i,
  655. BM_GETCHECK, 0,0))
  656. {
  657. gTypeSourceID = IDC_RBUNICODE1+i-CODEPAGEBASE;
  658. if(i==0)
  659. gTypeSource = TYPEUNICODE;
  660. else
  661. gTypeSource = TYPECODEPAGE;
  662. break;
  663. }
  664. //Get target settings.
  665. for(i=0;i<NumCodePage;i++)
  666. if (SendDlgItemMessage(hWndTab[1],IDC_RBUNICODE1+i,
  667. BM_GETCHECK, 0,0))
  668. {
  669. gTypeDestID = IDC_RBUNICODE1+i-CODEPAGEBASE;
  670. if(i==0)
  671. gTypeDest = TYPEUNICODE;
  672. else
  673. gTypeDest = TYPECODEPAGE;
  674. break;
  675. }
  676. //Get Option settings,
  677. for (i=0; i<= 2; i++)
  678. if (SendDlgItemMessage(hWndTab[2],IDC_NOSCTCMAP+i,
  679. BM_GETCHECK, 0,0))
  680. {
  681. switch (i) {
  682. case 1:
  683. gTCSCMapStatus = LCMAP_SIMPLIFIED_CHINESE;
  684. break;
  685. case 2:
  686. gTCSCMapStatus = LCMAP_TRADITIONAL_CHINESE;
  687. break;
  688. default:
  689. gTCSCMapStatus = DONOTMAP;
  690. break;
  691. }
  692. break;
  693. }
  694. for (i=0; i<= 2; i++)
  695. if (SendDlgItemMessage(hWndTab[2],IDC_NOHWFWMAP+i,
  696. BM_GETCHECK, 0,0))
  697. {
  698. switch (i) {
  699. case 1:
  700. gFWHWMapStatus = LCMAP_FULLWIDTH;
  701. break;
  702. case 2:
  703. gFWHWMapStatus = LCMAP_HALFWIDTH;
  704. break;
  705. default:
  706. gFWHWMapStatus = DONOTMAP;
  707. break;
  708. }
  709. break;
  710. }
  711. if(gTypeSourceID==gTypeDestID &&
  712. gTCSCMapStatus==DONOTMAP &&
  713. gFWHWMapStatus==DONOTMAP)
  714. if(gTypeSource == TYPEUNICODE){
  715. gTypeDest = TYPECODEPAGE;
  716. gTypeDestID = giRBInit-CODEPAGEBASE;
  717. }else{
  718. gTypeDest = TYPEUNICODE;
  719. gTypeDestID = IDC_RBUNICODE1-CODEPAGEBASE;
  720. }
  721. }
  722. VOID
  723. AdjustSelectTab(
  724. HWND hWnd,
  725. HWND hMainTabControl
  726. )
  727. {
  728. TC_ITEM tci;
  729. int iSel;
  730. iSel = TabCtrl_GetCurSel( hMainTabControl );
  731. //
  732. //get the proper index to the appropriate procs
  733. //that were set in MakeTabs
  734. //
  735. tci.mask = TCIF_PARAM;
  736. TabCtrl_GetItem(hMainTabControl, iSel, &tci);
  737. // Create the new child dialog box.
  738. ShowWindow(hWndDisplay, SW_HIDE);
  739. ShowWindow(hWndTab[iSel], SW_SHOW);
  740. if (iSel==1)
  741. AdjustTargetTab();
  742. hWndDisplay=hWndTab[iSel];
  743. }
  744. BOOL
  745. MakeTabs(
  746. HWND hWnd,
  747. HWND hMainTabControl
  748. )
  749. /*++
  750. Routine Description:
  751. MakeTabs fills out the Main Tab Control with appropriate tabs
  752. Arguments:
  753. HWND hWnd - handle of main window
  754. HWND hMainTabControl - handle to the tab control
  755. Return Value:
  756. BOOL - Returns TRUE if successful.
  757. --*/
  758. {
  759. TC_ITEM tci;
  760. TCHAR pszTabText[30];
  761. int i;
  762. tci.mask = TCIF_TEXT | TCIF_PARAM;
  763. tci.pszText = pszTabText;
  764. tci.cchTextMax = sizeof( pszTabText );
  765. for (i = 0; i < NUMBER_OF_PAGES; i++) {
  766. // Get the Tab title, the current index + the strind ID
  767. LoadString(_hModule, i + IDS_FIRST_TAB, tci.pszText, EXTENSION_LENGTH);
  768. // store the index to the procs
  769. tci.lParam = i;
  770. // insert the tab
  771. TabCtrl_InsertItem( hMainTabControl, i + 1, &tci );
  772. }
  773. return(TRUE);
  774. }
  775. DLGTEMPLATE * WINAPI
  776. DoLockDlgRes(LPWSTR lpszResName)
  777. /*++
  778. Routine Description:
  779. DoLockDlgRes - loads and locks a dialog template resource.
  780. Arguments:
  781. lpszResName - name of the resource
  782. Return Value:
  783. Returns a pointer to the locked resource.
  784. --*/
  785. {
  786. HRSRC hrsrc = FindResource(NULL, lpszResName, RT_DIALOG);
  787. HGLOBAL hglb = LoadResource(_hModule, hrsrc);
  788. return (DLGTEMPLATE *) LockResource(hglb);
  789. }
  790. /**************************************************************************\
  791. *
  792. * function: IsUnicode()
  793. *
  794. * HACK... eventually use a proper function for IsUnicode
  795. * Use function from unipad?
  796. *
  797. \**************************************************************************/
  798. BOOL IsUnicode (PBYTE pb)
  799. {
  800. return (IsBOM (pb));
  801. }
  802. /**************************************************************************\
  803. *
  804. * function: IsBOM()
  805. *
  806. * true iff pb points to a Byte Order Mark.
  807. *
  808. \**************************************************************************/
  809. BOOL IsBOM (PBYTE pb)
  810. {
  811. if ((*pb == 0xFF) & (*(pb+1) == 0xFE)) // BOM
  812. return TRUE;
  813. else
  814. return FALSE;
  815. }
  816. /**************************************************************************\
  817. *
  818. * function: IsRBOM()
  819. *
  820. * true iff pb points to a reversed Byte Order Mark.
  821. *
  822. \**************************************************************************/
  823. BOOL IsRBOM (PBYTE pb)
  824. {
  825. if ((*pb == 0xFE) & (*(pb+1) == 0xFF)) // RBOM
  826. return TRUE;
  827. else
  828. return FALSE;
  829. }
  830. VOID CreateFilter(PTCHAR szFilterSpec )
  831. {
  832. PTCHAR pszFilterSpec;
  833. TCHAR szAnsiText[EXTENSION_LENGTH], szAllFiles[EXTENSION_LENGTH];
  834. /* construct default filter string in the required format for
  835. * the new FileOpen and FileSaveAs dialogs
  836. * if you add to this, make sure CCHFILTERMAX is large enough.
  837. */
  838. LoadString(_hModule, IDS_ANSITEXT, szAnsiText, EXTENSION_LENGTH);
  839. LoadString(_hModule, IDS_ALLFILES, szAllFiles, EXTENSION_LENGTH);
  840. // .txt first for compatibility
  841. pszFilterSpec= szFilterSpec;
  842. lstrcpy( pszFilterSpec, szAnsiText );
  843. pszFilterSpec += lstrlen( pszFilterSpec ) + 1;
  844. lstrcpy( pszFilterSpec, TEXT("*.txt"));
  845. pszFilterSpec += lstrlen( pszFilterSpec ) + 1;
  846. // and last, all files
  847. lstrcpy( pszFilterSpec, szAllFiles );
  848. pszFilterSpec += lstrlen( pszFilterSpec ) + 1;
  849. lstrcpy(pszFilterSpec, TEXT("*.*") );
  850. pszFilterSpec += lstrlen( pszFilterSpec ) + 1;
  851. *pszFilterSpec = TEXT('\0');
  852. }
  853. LPVOID ManageMemory (UINT message, UINT sourcedestination, DWORD nBytes, LPVOID p)
  854. {
  855. switch (message) {
  856. case MMFREE :
  857. if (p != NULL) GlobalFree (GlobalHandle (p));
  858. return NULL;
  859. break;
  860. case MMALLOC :
  861. if (p != NULL) GlobalFree (GlobalHandle (p));
  862. p = (LPVOID) GlobalAlloc (GPTR, nBytes);
  863. return p;
  864. break;
  865. } /* end switch (message) */
  866. return NULL;
  867. }