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.

546 lines
19 KiB

  1. /*************************************************
  2. * srcproc.c *
  3. * *
  4. * Copyright (C) 1992-1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. #include "chnuconv.h"
  8. #include "resource.h"
  9. INT_PTR
  10. SourceTabProc(
  11. HWND hWnd,
  12. UINT message,
  13. WPARAM wParam,
  14. LPARAM lParam
  15. )
  16. {
  17. TCHAR szBuffer[50];
  18. int i;
  19. switch( message ) {
  20. case WM_INITDIALOG:
  21. {
  22. RECT rcTab;
  23. //GetTab window size
  24. // first get the size of the tab control
  25. if (GetWindowRect( hMainTabControl, &rcTab )){
  26. // adjust it to compensate for the tabs
  27. TabCtrl_AdjustRect( hMainTabControl, FALSE , &rcTab);
  28. // convert the screen coordinates to client coordinates
  29. MapWindowPoints( HWND_DESKTOP, GetParent(hMainTabControl),
  30. (LPPOINT)&rcTab, 2);
  31. }
  32. SetWindowPos(hWnd, HWND_TOP,
  33. rcTab.left,
  34. rcTab.top,
  35. rcTab.right - rcTab.left,
  36. rcTab.bottom - rcTab.top,
  37. SWP_NOACTIVATE );
  38. LoadString(_hModule, IDS_BUT_OPEN, szBuffer, EXTENSION_LENGTH);
  39. SetDlgItemText(hWnd,IDC_OPENORSAVEAS, szBuffer);
  40. LoadString(_hModule, IDS_BUT_FROMCLIPBOARD,szBuffer,EXTENSION_LENGTH);
  41. SetDlgItemText(hWnd, IDC_PASTEORCOPY, szBuffer);
  42. for (i=0;i<NumCodePage;i++)
  43. {
  44. LoadString(_hModule, IDS_CTLUNICODE+i,szBuffer,EXTENSION_LENGTH);
  45. ShowWindow(GetDlgItem(hWnd, IDC_RBUNICODE1+i), SW_SHOW);
  46. SetDlgItemText(hWnd, IDC_RBUNICODE1+i, szBuffer);
  47. }
  48. return(FALSE);
  49. }
  50. case WM_COMMAND:
  51. {
  52. switch (LOWORD(wParam)) {
  53. case IDC_OPENORSAVEAS : {
  54. HANDLE hFile;
  55. DWORD nBytesRead;
  56. TCHAR szFile[MAX_PATH],szFileTitle[MAX_PATH];
  57. /* First set up the structure for the GetOpenFileName
  58. * common dialog.
  59. */
  60. OPENFILENAME OpenFileName;
  61. /* buffers for the file names. */
  62. wsprintf (szFile, szBlank);
  63. wsprintf (szFileTitle, szBlank);
  64. OpenFileName.lStructSize = sizeof(OPENFILENAME);
  65. OpenFileName.hwndOwner = hWnd;
  66. OpenFileName.hInstance = (HANDLE) _hModule;
  67. OpenFileName.lpstrFilter = szFilter; // built in WM_CREATE
  68. OpenFileName.lpstrCustomFilter = NULL;
  69. OpenFileName.nMaxCustFilter = 0L;
  70. OpenFileName.nFilterIndex = 1L;
  71. OpenFileName.lpstrFile = szFile;
  72. OpenFileName.nMaxFile = MAX_PATH;
  73. OpenFileName.lpstrFileTitle = szFileTitle;
  74. OpenFileName.nMaxFileTitle = MAX_PATH;
  75. OpenFileName.lpstrInitialDir = NULL;
  76. LoadString(_hModule, IDS_OPENSOURCE, szBuffer, 50);
  77. OpenFileName.lpstrTitle = szBuffer;
  78. OpenFileName.nFileOffset = 0;
  79. OpenFileName.nFileExtension = 0;
  80. OpenFileName.lpstrDefExt = NULL;
  81. OpenFileName.lCustData = 0;
  82. OpenFileName.lpfnHook = NULL;
  83. OpenFileName.lpTemplateName = NULL;
  84. OpenFileName.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
  85. if (!GetOpenFileName(&OpenFileName)) return 0;
  86. SendMessage (hWnd, WM_COMMAND, IDC_CLEAR, 0);
  87. /* User has filled in the file information.
  88. * Try to open that file for reading.
  89. */
  90. hFile = CreateFile(szFile,
  91. GENERIC_READ,
  92. 0,
  93. NULL,
  94. OPEN_EXISTING,
  95. FILE_ATTRIBUTE_NORMAL,
  96. NULL);
  97. if (hFile == INVALID_HANDLE_VALUE) {
  98. LoadString(_hModule,IDS_OPENERROR,MBMessage,EXTENSION_LENGTH);
  99. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  100. return 0;
  101. }
  102. /* make sure file is not too big... i.e. > 2^32
  103. * If it is OK, write the file size in hwndSize0
  104. */
  105. {
  106. DWORD dwHigh;
  107. DWORD dwLow;
  108. dwLow = GetFileSize(hFile,&dwHigh);
  109. if ((dwHigh > 0) || (dwLow == 0xFFFFFFFF && GetLastError() != NO_ERROR))
  110. {
  111. LoadString(_hModule,IDS_FILETOOBIG,MBMessage,EXTENSION_LENGTH);
  112. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  113. CloseHandle (hFile);
  114. return 0;
  115. }
  116. nBytesSource= dwLow;
  117. }
  118. /* Allocate space for string, including potential UNICODE_NULL */
  119. pSourceData = ManageMemory (MMALLOC, MMSOURCE, nBytesSource +2, pSourceData);
  120. if (pSourceData == NULL) {
  121. CloseHandle (hFile);
  122. return 0;
  123. }
  124. /* first read two bytes and look for BOM */
  125. if (!ReadFile (hFile, pSourceData,SIZEOFBOM, &nBytesRead, NULL)) {
  126. LoadString(_hModule,IDS_READERROR,MBMessage,EXTENSION_LENGTH);
  127. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  128. CloseHandle (hFile);
  129. pSourceData = ManageMemory (MMFREE, MMSOURCE, 0, pSourceData);
  130. return 0;
  131. }
  132. /* If file begins with BOM, then we know the type,
  133. * we'll decrement the number of bytes by two,
  134. * and read the rest of the data.
  135. */
  136. if (IsBOM (pSourceData)) {
  137. gTypeSource = TYPEUNICODE;
  138. gTypeSourceID = IDC_RBUNICODE1-CODEPAGEBASE;
  139. gTypeDest = TYPECODEPAGE;
  140. gTypeDestID = giRBInit-CODEPAGEBASE;
  141. nBytesSource -=SIZEOFBOM;
  142. /* If file begins with Reverse BOM, then we know the type,
  143. * we'll decrement the number of bytes by two,
  144. * and read the rest of the data, and post a message so
  145. * that we know to swap the order later.
  146. */
  147. } else if (IsRBOM (pSourceData)) {
  148. gTypeSource = TYPEUNICODE;
  149. gTypeSourceID = IDC_RBUNICODE1-CODEPAGEBASE;
  150. gTypeDest = TYPECODEPAGE;
  151. gTypeDestID = giRBInit-CODEPAGEBASE;
  152. nBytesSource -=SIZEOFBOM;
  153. LoadString(_hModule,IDS_BYTEORDER,MBMessage,EXTENSION_LENGTH);
  154. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  155. SwapSource(TRUE);
  156. /* Oops, does not begin with BOM.
  157. * Reset file pointer, and read data.
  158. */
  159. } else {
  160. gTypeSource = TYPEUNKNOWN;
  161. SetFilePointer (hFile, -SIZEOFBOM, NULL, FILE_CURRENT);
  162. }
  163. /* try to read all of it into memory */
  164. if (!ReadFile (hFile, pSourceData,nBytesSource, &nBytesRead, NULL)) {
  165. LoadString(_hModule,IDS_READERROR,MBMessage,EXTENSION_LENGTH);
  166. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  167. CloseHandle (hFile);
  168. pSourceData = ManageMemory (MMFREE, MMSOURCE, 0, pSourceData);
  169. return 0;
  170. }
  171. CloseHandle (hFile);
  172. /* If we don't know the file type at this point,
  173. * try to determine if it is unicode.
  174. */
  175. if (gTypeSource == TYPEUNKNOWN) {
  176. gTypeSource = TYPECODEPAGE;
  177. gTypeSourceID = giRBInit-CODEPAGEBASE;
  178. gTypeDest = TYPEUNICODE;
  179. gTypeDestID = IDC_RBUNICODE1-CODEPAGEBASE;
  180. pSourceData[nBytesSource] = 0;
  181. }
  182. SendMessage (hWnd, WMU_ADJUSTFORNEWSOURCE, 0, (LPARAM)szFile);
  183. break;
  184. }
  185. /**********************************************************************\
  186. * WM_COMMAND, IDC_CLEAR
  187. *
  188. * Clear the SOURCE information. May cause data to be lost.
  189. \**********************************************************************/
  190. case IDC_CLEAR:
  191. {
  192. SetDlgItemText (hWnd, IDC_NAMETEXT, szBlank);
  193. SetDlgItemText (hWnd, IDC_SIZETEXT, szBlank);
  194. for(i=0;i<NumCodePage;i++){
  195. SendDlgItemMessage(hWnd,IDC_RBUNICODE1+i, BM_SETCHECK, 0,0);
  196. EnableControl(hWnd, IDC_RBUNICODE1+i, FALSE);
  197. SendDlgItemMessage(hWndTab[1],IDC_RBUNICODE1+i, BM_SETCHECK, 0,0);
  198. EnableControl(hWndTab[1], IDC_RBUNICODE1+i, FALSE);
  199. }
  200. pSourceData= ManageMemory (MMFREE, MMSOURCE, 0, pSourceData);
  201. pViewSourceData= ManageMemory (MMFREE, MMSOURCE, 0,pViewSourceData);
  202. nBytesSource=0;
  203. pTempData1= ManageMemory (MMFREE, MMDESTINATION, 0, pTempData1);
  204. EnableControl(hWnd, IDC_VIEW, FALSE);
  205. EnableControl(hWnd, IDC_CLEAR, FALSE);
  206. SendDlgItemMessage (hWnd,IDC_SWAPHIGHLOW, BM_SETCHECK, 0, 0);
  207. // set the gSourceSwapped as FALSE;
  208. gSourceSwapped = FALSE;
  209. EnableControl(hWnd, IDC_SWAPHIGHLOW, FALSE);
  210. EnableControl(_hWndMain, IDC_PUSH_CONVERT, FALSE);
  211. SendMessage (hWndTab[1], WM_COMMAND, IDC_CLEAR, 0);
  212. break;
  213. }
  214. /**********************************************************************\
  215. * WM_COMMAND, IDC_PASTEORCOPY
  216. *
  217. * Paste the clipboard's prefered data format into the source.
  218. * Fills pSourceData.
  219. \**********************************************************************/
  220. case IDC_PASTEORCOPY: {
  221. UINT iFormat;
  222. LPBYTE pData,pMyData;
  223. BOOL bClipBoardEmpty=TRUE;
  224. OpenClipboard (hWnd);
  225. iFormat = 0;
  226. while (iFormat = EnumClipboardFormats(iFormat))
  227. if ((iFormat == CF_UNICODETEXT) || (iFormat == CF_OEMTEXT) || (iFormat == CF_TEXT)) {
  228. bClipBoardEmpty=FALSE;
  229. pData = GetClipboardData (iFormat);
  230. pMyData = GlobalLock(pData);
  231. switch (iFormat) {
  232. case CF_UNICODETEXT:
  233. {
  234. int i=0;
  235. while( *((LPWORD)(pMyData)+i)!=0) i++;
  236. nBytesSource = i*2;
  237. }
  238. pSourceData= ManageMemory (MMALLOC, MMSOURCE, nBytesSource+2, pSourceData);
  239. lstrcpyW ((LPVOID)pSourceData, (LPVOID)pMyData);
  240. gTypeSource = TYPEUNICODE;
  241. gTypeSourceID = IDC_RBUNICODE1-CODEPAGEBASE;
  242. gTypeDest = TYPECODEPAGE;
  243. gTypeDestID = giRBInit-CODEPAGEBASE;
  244. break;
  245. case CF_OEMTEXT:
  246. {
  247. int i=0;
  248. while( *((char*)pMyData+i)!=0) i++;
  249. nBytesSource = i;
  250. }
  251. pSourceData= ManageMemory (MMALLOC, MMSOURCE, nBytesSource+1, pSourceData);
  252. lstrcpyA (pSourceData, pMyData);
  253. gTypeSource = TYPECODEPAGE;
  254. gTypeSourceID = giRBInit-CODEPAGEBASE;
  255. gTypeDest = TYPEUNICODE;
  256. gTypeDestID = IDC_RBUNICODE1-CODEPAGEBASE;
  257. break;
  258. case CF_TEXT:
  259. {
  260. int i=0;
  261. while( *((char*)pMyData+i)!=0) i++;
  262. nBytesSource = i;
  263. }
  264. pSourceData= ManageMemory (MMALLOC, MMSOURCE, nBytesSource+1, pSourceData);
  265. lstrcpyA (pSourceData, pMyData);
  266. gTypeSource = TYPECODEPAGE;
  267. gTypeSourceID = giRBInit-CODEPAGEBASE;
  268. gTypeDest = TYPEUNICODE;
  269. gTypeDestID = IDC_RBUNICODE1-CODEPAGEBASE;
  270. break;
  271. default: break; // shouldn't get here
  272. } /* end switch (iFormat) */
  273. GlobalUnlock(pData);
  274. break; /* break out of while loop. */
  275. } /* end if iFormat */
  276. if (!bClipBoardEmpty)
  277. {
  278. LoadString(_hModule, IDS_FROMCLIPBOARD, szBuffer, 50);
  279. SendMessage (hWnd, WMU_ADJUSTFORNEWSOURCE, 0, (LPARAM)szBuffer);
  280. }
  281. else
  282. {
  283. MessageBeep(0xFFFFFFFF);
  284. }
  285. CloseClipboard ();
  286. break;
  287. }
  288. case IDC_VIEW:
  289. {
  290. //See if user wants to swap source
  291. SwapSource(FALSE);
  292. if (gTypeSource == TYPEUNICODE)
  293. DialogBoxW (_hModule, MAKEINTRESOURCEW(IDD_SHOWTEXT), hWnd,
  294. ViewSourceProc);
  295. else
  296. DialogBoxA (_hModule, MAKEINTRESOURCEA(IDD_SHOWTEXT), hWnd,
  297. ViewSourceProc);
  298. break;
  299. }
  300. default:
  301. break;
  302. }
  303. break;
  304. }
  305. /**********************************************************************\
  306. * WMU_ADJUSTFORNEWSOURCE
  307. *
  308. * lParam - szName of source (file, clipboard, ...)
  309. *
  310. * global - nBytesSource
  311. *
  312. * "user message." Set the text of the Source windows
  313. \**********************************************************************/
  314. case WMU_ADJUSTFORNEWSOURCE:
  315. {
  316. LPVOID szName;
  317. szName = (LPVOID) lParam;
  318. // Set Window text appropriately
  319. wsprintf (szBuffer, szNBytes, nBytesSource);
  320. SetDlgItemText (hWnd, IDC_NAMETEXT, szName);
  321. SetDlgItemText (hWnd, IDC_SIZETEXT, szBuffer);
  322. // Clear the destination data if any to avoid user confusion.
  323. SendMessage (hWndTab[1], WM_COMMAND, IDC_CLEAR, 0);
  324. SendMessage (hWnd, WMU_SETCODEPAGEINFO, 0,0);
  325. EnableControl(hWnd, IDC_VIEW, TRUE);
  326. EnableControl(hWnd, IDC_SWAPHIGHLOW, TRUE);
  327. EnableControl(hWnd, IDC_CLEAR, TRUE);
  328. EnableControl(_hWndMain, IDC_PUSH_CONVERT, TRUE);
  329. break;
  330. }
  331. /**********************************************************************\
  332. * WMU_SETCODEPAGEINFO
  333. *
  334. * "user message." Set the text of the "type" windows to reflect
  335. * the state stored in gTypeSource and gi*CodePage.
  336. *
  337. \**********************************************************************/
  338. case WMU_SETCODEPAGEINFO:
  339. {
  340. for (i=0;i<NumCodePage;i++){
  341. EnableControl (hWnd,IDC_RBUNICODE1+i, TRUE);
  342. EnableControl (hWndTab[1],IDC_RBUNICODE1+i, TRUE);
  343. SendDlgItemMessage (hWnd,IDC_RBUNICODE1+i, BM_SETCHECK, 0, 0);
  344. }
  345. SendDlgItemMessage (hWnd, gTypeSourceID+CODEPAGEBASE,BM_SETCHECK, 1, 0);
  346. break;
  347. }
  348. }
  349. return DefWindowProc( hWnd, message, wParam, lParam );
  350. }
  351. /***************************************************************************\
  352. * FUNCTION: ViewSourceProc
  353. *
  354. * Fill Text, Name, and Type information into the dialog.
  355. * Set a proper font to display the text depending on what type it is.
  356. *
  357. \***************************************************************************/
  358. INT_PTR ViewSourceProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  359. {
  360. RECT rect;
  361. switch (message) {
  362. /******************************************************************\
  363. * WM_INITDIALOG
  364. \******************************************************************/
  365. case WM_INITDIALOG:
  366. {
  367. LOGFONTW logfont;
  368. HFONT hFont;
  369. GetSettings();
  370. /* Text is unicode... use *W() variants of functions. */
  371. if (gTypeSource == TYPEUNICODE) {
  372. WCHAR szBuffer[MAX_PATH];
  373. SetWindowTextW (GetDlgItem(hWnd, IDC_SHOWTEXT_EDIT), (LPCWSTR)pSourceData);
  374. GetDlgItemTextW (hWndTab[0], IDC_NAMETEXT, szBuffer, MAX_PATH);
  375. SetDlgItemTextW (hWnd, IDC_SHOWTEXT_NAME, szBuffer);
  376. LoadStringW(_hModule, gTypeSourceID+STRCODEPAGEBASE, szBuffer,EXTENSION_LENGTH);
  377. SetDlgItemTextW (hWnd, IDC_SHOWTEXT_TYPE, szBuffer);
  378. /* Text is codepage... use *A() variants of functions. */
  379. } else {
  380. char szBuffer[MAX_PATH];
  381. SetDlgItemTextA (hWnd, IDC_SHOWTEXT_EDIT, (LPCSTR)pSourceData);
  382. GetDlgItemTextA (hWndTab[0], IDC_NAMETEXT, szBuffer, MAX_PATH);
  383. SetDlgItemTextA (hWnd, IDC_SHOWTEXT_NAME, szBuffer);
  384. LoadStringA(_hModule, gTypeSourceID+STRCODEPAGEBASE, szBuffer,EXTENSION_LENGTH);
  385. SetDlgItemTextA (hWnd, IDC_SHOWTEXT_TYPE, szBuffer);
  386. }
  387. LoadString(_hModule, IDS_VIEWSOURCE, &gszExtensions[11][0], EXTENSION_LENGTH);
  388. SetWindowText (hWnd,&gszExtensions[11][0] );
  389. GetClientRect (hWnd, &rect);
  390. SendMessage (hWnd, WM_SIZE, 0,
  391. MAKELPARAM ((rect.right - rect.left), (rect.bottom - rect.top)));
  392. return TRUE;
  393. }
  394. case WM_SIZE: {
  395. HWND hwndText;
  396. HWND hwndNotice;
  397. RECT rc;
  398. POINT pt;
  399. hwndNotice = GetDlgItem (hWnd,IDC_SHOWTEXT_FONT);
  400. GetWindowRect(hwndNotice,&rc);
  401. pt.x = 0;
  402. pt.y = rc.bottom;
  403. ScreenToClient(hWnd, &pt);
  404. hwndText = GetDlgItem (hWnd, IDC_SHOWTEXT_EDIT);
  405. MoveWindow (hwndText, DLGBORDER, pt.y+5, (int) LOWORD(lParam) - 2*DLGBORDER,
  406. (int) HIWORD(lParam) - (pt.y+5) - DLGBORDER , TRUE);
  407. }
  408. break;
  409. case WM_COMMAND:
  410. switch (LOWORD (wParam)) {
  411. case IDCANCEL:
  412. case IDOK:
  413. EndDialog (hWnd, TRUE);
  414. }
  415. break; /* end WM_COMMAND */
  416. case WM_SYSCOMMAND:
  417. if (LOWORD (wParam) == SC_CLOSE)
  418. EndDialog (hWnd, FALSE);
  419. break;
  420. } /* end switch */
  421. return FALSE;
  422. }
  423. BOOL
  424. SwapSource(
  425. BOOL bForceSwap
  426. )
  427. {
  428. int i, end;
  429. BYTE temp;
  430. if (pSourceData == NULL) return FALSE;
  431. //Is the source already swapped?
  432. if (bForceSwap)
  433. SendDlgItemMessage(hWndTab[0], IDC_SWAPHIGHLOW,BM_GETCHECK,1,0);
  434. else if (SendDlgItemMessage(hWndTab[0], IDC_SWAPHIGHLOW,
  435. BM_GETCHECK,0,0) == gSourceSwapped)
  436. return FALSE;
  437. end = nBytesSource - 2;
  438. for (i = 0; i<= end; i+=2) {
  439. temp = pSourceData[i];
  440. pSourceData[i] = pSourceData[i+1];
  441. pSourceData[i+1] = temp;
  442. }
  443. //set flag that source has been swapped;
  444. gSourceSwapped = 1 - gSourceSwapped;
  445. return TRUE;
  446. }