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.

463 lines
15 KiB

  1. /*************************************************
  2. * tgtproc.c *
  3. * *
  4. * Copyright (C) 1992-1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. #include "chnuconv.h"
  8. #include "resource.h"
  9. INT_PTR
  10. TargetTabProc(
  11. HWND hWnd,
  12. UINT message,
  13. WPARAM wParam,
  14. LPARAM lParam
  15. )
  16. {
  17. TCHAR szBuffer[MAX_PATH];
  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_SAVEAS, szBuffer, EXTENSION_LENGTH);
  39. SetDlgItemText(hWnd,IDC_OPENORSAVEAS, szBuffer);
  40. LoadString(_hModule, IDS_BUT_TOCLIPBOARD,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. SendMessage(hWnd, WM_COMMAND, IDC_CLEAR, 0);
  49. return(FALSE);
  50. }
  51. case WM_COMMAND:
  52. {
  53. switch(wParam){
  54. /******************************************************************\
  55. * WM_COMMAND, IDC_OPENORSAVEAS
  56. *
  57. * Put up common dialog, try to open file, and write data to it.
  58. \******************************************************************/
  59. case IDC_OPENORSAVEAS:
  60. {
  61. HANDLE hFile;
  62. DWORD nBytesRead;
  63. TCHAR szFile[MAX_PATH],szFileTitle[MAX_PATH];
  64. TCHAR szDefExt[4];
  65. OPENFILENAME OpenFileName;
  66. /* buffers for the file names. */
  67. if (nBytesDestination == NODATA ) {
  68. LoadString(_hModule,IDS_NOTEXTSAVE,MBMessage,EXTENSION_LENGTH);
  69. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  70. return 0;
  71. }
  72. //see if user selects swap destination
  73. SwapDest(FALSE);
  74. /* Set up the structure for the GetSaveFileName
  75. * common dialog.
  76. */
  77. lstrcpy(szDefExt, TEXT("txt") );
  78. wsprintf (szFile, szBlank);
  79. wsprintf (szFileTitle, szBlank);
  80. OpenFileName.lStructSize = sizeof(OPENFILENAME);
  81. OpenFileName.hwndOwner = hWnd;
  82. OpenFileName.hInstance = (HANDLE) _hModule;
  83. OpenFileName.lpstrFilter = szFilter;
  84. OpenFileName.lpstrCustomFilter = NULL;
  85. OpenFileName.nMaxCustFilter = 0L;
  86. OpenFileName.nFilterIndex = 1L;
  87. OpenFileName.lpstrFile = szFile;
  88. OpenFileName.nMaxFile = MAX_PATH;
  89. OpenFileName.lpstrFileTitle = szFileTitle;
  90. OpenFileName.nMaxFileTitle = MAX_PATH;
  91. OpenFileName.lpstrInitialDir = NULL;
  92. LoadString(_hModule, IDS_SAVEAS, szBuffer, 50);
  93. OpenFileName.lpstrTitle = szBuffer;
  94. OpenFileName.nFileOffset = 0;
  95. OpenFileName.nFileExtension = 0;
  96. OpenFileName.lpstrDefExt = szDefExt;
  97. OpenFileName.lCustData = 0;
  98. OpenFileName.lpfnHook = NULL;
  99. OpenFileName.lpTemplateName = NULL;
  100. OpenFileName.Flags = OFN_HIDEREADONLY;
  101. if (!GetSaveFileName(&OpenFileName)) return 0;
  102. /* User has filled in the file information.
  103. * Try to open that file for writing.
  104. */
  105. hFile = CreateFile(szFile,
  106. GENERIC_WRITE,
  107. 0,
  108. NULL,
  109. CREATE_ALWAYS,
  110. FILE_ATTRIBUTE_NORMAL,
  111. NULL);
  112. if (hFile == INVALID_HANDLE_VALUE) {
  113. LoadString(_hModule,IDS_CREATEERROR,MBMessage,EXTENSION_LENGTH);
  114. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  115. return 0;
  116. }
  117. /* if destination is unicode, try to write BOM first.
  118. * unless the bytes have been swapped
  119. * (criterion: hwndByteOrder contains text)
  120. * in which case, write a Reverse Byte Order Mark.
  121. */
  122. if (gTypeDest == TYPEUNICODE)
  123. if (!SendDlgItemMessage(hWndTab[1],IDC_SWAPHIGHLOW,
  124. BM_GETCHECK, 0, 0)){
  125. if (!WriteFile (hFile, szBOM, SIZEOFBOM, &nBytesRead, NULL)) {
  126. LoadString(_hModule,IDS_WRITEERROR,MBMessage,EXTENSION_LENGTH);
  127. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  128. CloseHandle (hFile);
  129. return 0;
  130. }
  131. }
  132. else
  133. {
  134. if (!WriteFile (hFile, szRBOM, SIZEOFBOM, &nBytesRead, NULL)) {
  135. LoadString(_hModule,IDS_WRITEERROR,MBMessage,EXTENSION_LENGTH);
  136. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  137. CloseHandle (hFile);
  138. return 0;
  139. }
  140. }
  141. /* try to write all of it into memory */
  142. if (!WriteFile (hFile, pDestinationData,nBytesDestination, &nBytesRead, NULL)) {
  143. LoadString(_hModule,IDS_WRITEERROR,MBMessage,EXTENSION_LENGTH);
  144. MessageBox (hWnd, MBMessage,MBTitle, MBFlags);
  145. CloseHandle (hFile);
  146. return 0;
  147. }
  148. SetDlgItemText (hWnd,IDC_NAMETEXT,szFile);
  149. CloseHandle (hFile);
  150. break;
  151. }
  152. case IDC_VIEW:
  153. {
  154. SwapDest(FALSE);
  155. if (gTypeDest == TYPEUNICODE)
  156. DialogBoxW (_hModule, MAKEINTRESOURCEW(IDD_SHOWTEXT), hWnd,
  157. ViewTargetProc);
  158. else
  159. DialogBoxA (_hModule, MAKEINTRESOURCEA(IDD_SHOWTEXT), hWnd,
  160. ViewTargetProc);
  161. break;
  162. }
  163. /**********************************************************************\
  164. * WM_COMMAND, IDC_CLEAR
  165. *
  166. * Clear the SOURCE information. May cause data to be lost.
  167. \**********************************************************************/
  168. case IDC_CLEAR:
  169. {
  170. int i;
  171. SetDlgItemText (hWnd, IDC_NAMETEXT, szBlank);
  172. SetDlgItemText (hWnd, IDC_SIZETEXT, szBlank);
  173. for(i=0;i<NumCodePage;i++)
  174. SendDlgItemMessage(hWnd,IDC_RBUNICODE1+i, BM_SETCHECK, 0,0);
  175. pDestinationData= ManageMemory (MMFREE, MMSOURCE, 0, pDestinationData);
  176. nBytesDestination=0;
  177. pTempData1= ManageMemory (MMFREE, MMDESTINATION, 0, pTempData1);
  178. EnableControl(hWnd, IDC_VIEW, FALSE);
  179. EnableControl(hWnd, IDC_PASTEORCOPY, FALSE);
  180. EnableControl(hWnd, IDC_OPENORSAVEAS, FALSE);
  181. EnableControl(hWnd, IDC_CLEAR, FALSE);
  182. EnableControl(hWnd, IDC_SWAPHIGHLOW, FALSE);
  183. SendDlgItemMessage (hWnd,IDC_SWAPHIGHLOW, BM_SETCHECK, 0, 0);
  184. // set gDestSwapped as FALSE
  185. gDestSwapped = FALSE;
  186. EnableMenuItem (GetMenu (_hWndMain),IDM_FILE_SAVEAS,MF_GRAYED);
  187. break;
  188. }
  189. /**********************************************************************\
  190. * WM_COMMAND, IDC_PASTEORCOPY
  191. *
  192. * Copy destination data to the clipboard.
  193. \**********************************************************************/
  194. case IDC_PASTEORCOPY:
  195. {
  196. int i;
  197. if (pDestinationData == NULL) return FALSE;
  198. //see if user selects swap hi-low byte order
  199. SwapDest(FALSE);
  200. OpenClipboard (hWnd);
  201. if( EmptyClipboard() ) {
  202. GlobalUnlock(hglbMem);
  203. GlobalFree(hglbMem);
  204. hglbMem = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE,
  205. nBytesDestination+40);
  206. p=(PBYTE)GlobalLock(hglbMem);
  207. for(i=0;i<nBytesDestination;i++)
  208. *(p+i)=*(pDestinationData+i);
  209. *(p+i)=0;
  210. //if source NOT unicode, destination is, else look at dest CP
  211. if (gTypeDest == TYPEUNICODE) {
  212. *(p+i+1)=0;
  213. GlobalUnlock(hglbMem);
  214. SetClipboardData (CF_UNICODETEXT, hglbMem);
  215. }
  216. else {
  217. GlobalUnlock(hglbMem);
  218. if (gTypeDestID == giRBInit-CODEPAGEBASE)
  219. SetClipboardData (CF_TEXT, hglbMem);
  220. else
  221. SetClipboardData (CF_OEMTEXT, hglbMem);
  222. }
  223. }
  224. CloseClipboard ();
  225. }
  226. break;
  227. }
  228. }
  229. default:
  230. break;
  231. }
  232. return DefWindowProc( hWnd, message, wParam, lParam );
  233. }
  234. /***************************************************************************\
  235. * FUNCTION: ViewTargetProc
  236. *
  237. * Fill Text, Name, and Type information into the dialog.
  238. * Set a proper font to display the text depending on what type it is.
  239. *
  240. \***************************************************************************/
  241. INT_PTR
  242. ViewTargetProc(
  243. HWND hWnd,
  244. UINT message,
  245. WPARAM wParam,
  246. LPARAM lParam
  247. )
  248. {
  249. RECT rect;
  250. switch (message) {
  251. /******************************************************************\
  252. * WM_INITDIALOG
  253. \******************************************************************/
  254. case WM_INITDIALOG:
  255. {
  256. GetSettings();
  257. // Text is unicode... use *W() variants of functions.
  258. if (gTypeDest == TYPEUNICODE) {
  259. WCHAR szBuffer[MAX_PATH];
  260. SetDlgItemTextW (hWnd, IDC_SHOWTEXT_EDIT, (LPCWSTR)pDestinationData);
  261. GetDlgItemTextW (hWndTab[1], IDC_NAMETEXT, szBuffer, MAX_PATH);
  262. SetDlgItemTextW (hWnd, IDC_SHOWTEXT_NAME, szBuffer);
  263. LoadStringW(_hModule, gTypeDestID+STRCODEPAGEBASE, szBuffer,EXTENSION_LENGTH);
  264. SetDlgItemTextW (hWnd, IDC_SHOWTEXT_TYPE, szBuffer);
  265. // Text is codepage... use *A() variants of functions.
  266. } else {
  267. char szBuffer[MAX_PATH];
  268. SetDlgItemTextA (hWnd, IDC_SHOWTEXT_EDIT, (LPCSTR)pDestinationData);
  269. GetDlgItemTextA (hWndTab[1], IDC_NAMETEXT, szBuffer, MAX_PATH);
  270. SetDlgItemTextA (hWnd, IDC_SHOWTEXT_NAME, szBuffer);
  271. LoadStringA(_hModule, gTypeDestID+STRCODEPAGEBASE, szBuffer,EXTENSION_LENGTH);
  272. SetDlgItemTextA (hWnd, IDC_SHOWTEXT_TYPE, szBuffer);
  273. }
  274. LoadString(_hModule, IDS_VIEWTARGET, &gszExtensions[11][0], EXTENSION_LENGTH);
  275. SetWindowText (hWnd,&gszExtensions[11][0] );
  276. GetClientRect (hWnd, &rect);
  277. SendMessage (hWnd, WM_SIZE, 0,
  278. MAKELPARAM ((rect.right - rect.left), (rect.bottom - rect.top)));
  279. return TRUE;
  280. }
  281. case WM_SIZE: {
  282. HWND hwndText;
  283. HWND hwndNotice;
  284. RECT rc;
  285. POINT pt;
  286. hwndNotice = GetDlgItem (hWnd,IDC_SHOWTEXT_FONT);
  287. GetWindowRect(hwndNotice,&rc);
  288. pt.x = 0;
  289. pt.y = rc.bottom;
  290. ScreenToClient(hWnd, &pt);
  291. hwndText = GetDlgItem (hWnd, IDC_SHOWTEXT_EDIT);
  292. MoveWindow (hwndText, DLGBORDER, pt.y+5, (int) LOWORD(lParam) - 2*DLGBORDER,
  293. (int) HIWORD(lParam) - (pt.y+5) - DLGBORDER , TRUE);
  294. }
  295. break;
  296. case WM_COMMAND:
  297. switch (LOWORD (wParam)) {
  298. case IDCANCEL:
  299. case IDOK:
  300. EndDialog (hWnd, TRUE);
  301. }
  302. break; /* end WM_COMMAND */
  303. case WM_SYSCOMMAND:
  304. if (LOWORD (wParam) == SC_CLOSE)
  305. EndDialog (hWnd, FALSE);
  306. break;
  307. } /* end switch */
  308. return FALSE;
  309. }
  310. BOOL
  311. SwapDest(
  312. BOOL bForceSwap
  313. )
  314. {
  315. int i, end;
  316. BYTE temp;
  317. if (pDestinationData == NULL) return FALSE;
  318. //Is the Destination already swapped?
  319. if (SendDlgItemMessage(hWndTab[1], IDC_SWAPHIGHLOW,
  320. BM_GETCHECK,0,0) == gDestSwapped && !bForceSwap)
  321. return FALSE;
  322. end = nBytesDestination - 2;
  323. for (i = 0; i<= end; i+=2) {
  324. temp = pDestinationData[i];
  325. pDestinationData[i] = pDestinationData[i+1];
  326. pDestinationData[i+1] = temp;
  327. }
  328. //set flag that source has been swapped;
  329. gDestSwapped = 1 - gDestSwapped;
  330. return TRUE;
  331. }
  332. VOID
  333. AdjustTargetTab()
  334. {
  335. int i;
  336. GetSettings();
  337. //if user change swap option in source tab, we'd better reset dest.
  338. if (SendDlgItemMessage(hWndTab[0], IDC_SWAPHIGHLOW,
  339. BM_GETCHECK, 0,0) != gSourceSwapped)
  340. SendMessage(hWndTab[1], WM_COMMAND, IDC_CLEAR,0);
  341. //Do we have a source yet?
  342. if(IsWindowEnabled(GetDlgItem(hWndTab[0], IDC_VIEW))){
  343. for (i=0;i<NumCodePage;i++){
  344. EnableControl(hWndTab[1], i+CODEPAGEBASE, TRUE);
  345. SendDlgItemMessage (hWndTab[1],i+CODEPAGEBASE,
  346. BM_SETCHECK, 0, 0);
  347. }
  348. if (gTCSCMapStatus==DONOTMAP && gFWHWMapStatus==DONOTMAP)
  349. EnableControl(hWndTab[1], gTypeSourceID+CODEPAGEBASE,FALSE);
  350. SendDlgItemMessage (hWndTab[1],gTypeDestID+CODEPAGEBASE,
  351. BM_SETCHECK, 1, 0);
  352. }
  353. }
  354. BOOL
  355. EnableControl(
  356. IN HWND hWnd,
  357. IN int ControlId,
  358. IN BOOL Enable
  359. )
  360. /*++
  361. Routine Description:
  362. Enable or diable the specified control based on the supplied flag.
  363. Arguments:
  364. hWnd - Supplies the window (dialog box) handle that contains the
  365. control.
  366. ControlId - Supplies the control id.
  367. Enable - Supplies a flag which if TRUE causes the control to be enabled
  368. and disables the control if FALSE.
  369. Return Value:
  370. BOOL - Returns TRUE if the control is succesfully enabled / disabled.
  371. --*/
  372. {
  373. HWND hWndControl;
  374. BOOL Success;
  375. hWndControl = GetDlgItem( hWnd, ControlId );
  376. if( hWndControl == NULL ) {
  377. return FALSE;
  378. }
  379. if( Enable == IsWindowEnabled( hWndControl )) {
  380. return TRUE;
  381. }
  382. Success = EnableWindow( hWndControl, Enable );
  383. return TRUE;
  384. }