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.

330 lines
8.2 KiB

  1. // convertDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "convert.h"
  5. #include "convertDlg.h"
  6. #include "FileConv.h"
  7. #include "Msg.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19. // Dialog Data
  20. //{{AFX_DATA(CAboutDlg)
  21. enum { IDD = IDD_ABOUTBOX };
  22. //}}AFX_DATA
  23. // ClassWizard generated virtual function overrides
  24. //{{AFX_VIRTUAL(CAboutDlg)
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  27. //}}AFX_VIRTUAL
  28. // Implementation
  29. protected:
  30. //{{AFX_MSG(CAboutDlg)
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CAboutDlg)
  37. //}}AFX_DATA_INIT
  38. }
  39. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CAboutDlg)
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  46. //{{AFX_MSG_MAP(CAboutDlg)
  47. // No message handlers
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CConvertDlg dialog
  52. CConvertDlg::CConvertDlg(CWnd* pParent /*=NULL*/)
  53. : CDialog(CConvertDlg::IDD, pParent)
  54. {
  55. //{{AFX_DATA_INIT(CConvertDlg)
  56. m_strSourceFileName = _T("");
  57. m_strTargetFileName = _T("");
  58. m_ToUnicodeOrAnsi = 0;
  59. //}}AFX_DATA_INIT
  60. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  61. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  62. }
  63. void CConvertDlg::DoDataExchange(CDataExchange* pDX)
  64. {
  65. CDialog::DoDataExchange(pDX);
  66. //{{AFX_DATA_MAP(CConvertDlg)
  67. DDX_Control(pDX, IDC_CONVERT, m_cBtnConvert);
  68. DDX_Text(pDX, IDC_SOURCEFILENAME, m_strSourceFileName);
  69. DDV_MaxChars(pDX, m_strSourceFileName, MAX_PATH-1);
  70. DDX_Text(pDX, IDC_TARGETFILENAME, m_strTargetFileName);
  71. DDV_MaxChars(pDX, m_strTargetFileName, MAX_PATH-1);
  72. DDX_Radio(pDX, IDC_GBTOUNICODE, m_ToUnicodeOrAnsi);
  73. //}}AFX_DATA_MAP
  74. }
  75. BEGIN_MESSAGE_MAP(CConvertDlg, CDialog)
  76. //{{AFX_MSG_MAP(CConvertDlg)
  77. ON_WM_SYSCOMMAND()
  78. ON_WM_PAINT()
  79. ON_WM_QUERYDRAGICON()
  80. ON_BN_CLICKED(IDC_OPENSOURCEFILE, OnOpensourcefile)
  81. ON_BN_CLICKED(IDC_ABOUT, OnAbout)
  82. ON_EN_CHANGE(IDC_TARGETFILENAME, OnChangeTargetfilename)
  83. ON_BN_CLICKED(IDC_GBTOUNICODE, OnGbtounicode)
  84. ON_BN_CLICKED(IDC_UNICODETOGB, OnUnicodetogb)
  85. ON_BN_CLICKED(IDC_CONVERT, OnConvert)
  86. //}}AFX_MSG_MAP
  87. END_MESSAGE_MAP()
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CConvertDlg message handlers
  90. BOOL CConvertDlg::OnInitDialog()
  91. {
  92. CDialog::OnInitDialog();
  93. // Add "About..." menu item to system menu.
  94. // IDM_ABOUTBOX must be in the system command range.
  95. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  96. ASSERT(IDM_ABOUTBOX < 0xF000);
  97. CMenu* pSysMenu = GetSystemMenu(FALSE);
  98. if (pSysMenu != NULL)
  99. {
  100. CString strAboutMenu;
  101. strAboutMenu.LoadString(IDS_ABOUTBOX);
  102. if (!strAboutMenu.IsEmpty())
  103. {
  104. pSysMenu->AppendMenu(MF_SEPARATOR);
  105. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  106. }
  107. }
  108. // Set the icon for this dialog. The framework does this automatically
  109. // when the application's main window is not a dialog
  110. SetIcon(m_hIcon, TRUE); // Set big icon
  111. SetIcon(m_hIcon, FALSE); // Set small icon
  112. m_fTargetFileNameChanged = TRUE;
  113. return TRUE; // return TRUE unless you set the focus to a control
  114. }
  115. void CConvertDlg::OnSysCommand(UINT nID, LPARAM lParam)
  116. {
  117. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  118. {
  119. CAboutDlg dlgAbout;
  120. dlgAbout.DoModal();
  121. }
  122. else
  123. {
  124. CDialog::OnSysCommand(nID, lParam);
  125. }
  126. }
  127. // If you add a minimize button to your dialog, you will need the code below
  128. // to draw the icon. For MFC applications using the document/view model,
  129. // this is automatically done for you by the framework.
  130. void CConvertDlg::OnPaint()
  131. {
  132. if (IsIconic())
  133. {
  134. CPaintDC dc(this); // device context for painting
  135. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  136. // Center icon in client rectangle
  137. int cxIcon = GetSystemMetrics(SM_CXICON);
  138. int cyIcon = GetSystemMetrics(SM_CYICON);
  139. CRect rect;
  140. GetClientRect(&rect);
  141. int x = (rect.Width() - cxIcon + 1) / 2;
  142. int y = (rect.Height() - cyIcon + 1) / 2;
  143. // Draw the icon
  144. dc.DrawIcon(x, y, m_hIcon);
  145. }
  146. else
  147. {
  148. CDialog::OnPaint();
  149. }
  150. }
  151. // The system calls this to obtain the cursor to display while the user drags
  152. // the minimized window.
  153. HCURSOR CConvertDlg::OnQueryDragIcon()
  154. {
  155. return (HCURSOR) m_hIcon;
  156. }
  157. void CConvertDlg::OnOpensourcefile()
  158. {
  159. // BOOL fRet = FALSE;
  160. OPENFILENAME ofn;
  161. // TCHAR* tszFileName = new TCHAR[MAX_PATH];
  162. // if (tszFileName) {
  163. // goto Exit;
  164. // }
  165. #ifdef RTF_SUPPORT
  166. #ifdef XML_SUPPORT
  167. // RTF & XML
  168. TCHAR tszFilter[] = _T(
  169. "Text file (.txt)|*.txt|"\
  170. "Rich text format file (.rtf)|*.rtf|"\
  171. "Html file (.html;.htm)|*.html;*.htm|"\
  172. "Xml file (.xml)|*.xml|"\
  173. "All files (*.*)|*.*|");
  174. #else
  175. // RTF & !XML
  176. TCHAR tszFilter[] = _T(
  177. "Text file (.txt)|*.txt|"\
  178. "Rich text format file (.rtf)|*.rtf|"\
  179. "Html file (.html;.htm)|*.html;*.htm|"\
  180. "All files (*.*)|*.*|");
  181. #endif
  182. #else
  183. #ifdef XM_SUPPORT
  184. // !RTF & XML
  185. TCHAR tszFilter[] = _T(
  186. "Text file (.txt)|*.txt|"\
  187. "Html file (.html;.htm)|*.html;*.htm|"\
  188. "Xml file (.xml)|*.xml|"\
  189. "All files (*.*)|*.*|");
  190. #else
  191. // !RTF & !XML
  192. TCHAR tszFilter[] = _T(
  193. "Text file (.txt)|*.txt|"\
  194. "Html file (.html;.htm)|*.html;*.htm|"\
  195. "All files (*.*)|*.*|");
  196. #endif
  197. #endif
  198. TCHAR tszFileName[MAX_PATH];
  199. *tszFileName = NULL;
  200. int nLen = lstrlen (tszFilter);
  201. for (int i = 0; i < nLen; i++) {
  202. if (tszFilter[i] == '|') { tszFilter[i] = 0; }
  203. }
  204. ofn.lStructSize = sizeof(OPENFILENAME);
  205. ofn.hwndOwner = m_hWnd;
  206. ofn.lpstrFilter = tszFilter;
  207. ofn.lpstrCustomFilter = NULL;
  208. ofn.nMaxCustFilter = 0;
  209. ofn.nFilterIndex = 1;
  210. ofn.lpstrFile = tszFileName;
  211. ofn.nMaxFile = MAX_PATH;
  212. ofn.lpstrFileTitle = NULL;
  213. ofn.nMaxFileTitle = 0;
  214. ofn.lpstrInitialDir = NULL;
  215. ofn.lpstrTitle = NULL;
  216. ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY|OFN_PATHMUSTEXIST|OFN_LONGNAMES;
  217. ofn.lpstrDefExt = NULL;//TEXT("txt");
  218. if (GetOpenFileName(&ofn)) {
  219. UpdateData(TRUE);
  220. try {
  221. m_strSourceFileName = tszFileName;
  222. if (!GenerateTargetFileName(tszFileName, &m_strTargetFileName,
  223. m_ToUnicodeOrAnsi == 0 ? TRUE:FALSE)) {
  224. throw 0;
  225. }
  226. m_fTargetFileNameChanged = FALSE;
  227. UpdateData(FALSE);
  228. }
  229. catch (...) {
  230. MsgOverflow();
  231. }
  232. }
  233. return;
  234. }
  235. void CConvertDlg::OnConvert()
  236. {
  237. UpdateData(TRUE);
  238. m_cBtnConvert.EnableWindow(FALSE);
  239. BOOL fOk = Convert((PCTCH)m_strSourceFileName, (PCTCH)m_strTargetFileName,
  240. m_ToUnicodeOrAnsi == 0 ? TRUE:FALSE);
  241. if (fOk) {
  242. MsgConvertFinish();
  243. } else {
  244. MsgConvertFail();
  245. }
  246. m_cBtnConvert.EnableWindow(TRUE);
  247. }
  248. void CConvertDlg::OnAbout()
  249. {
  250. CAboutDlg dlgAbout;
  251. dlgAbout.DoModal();
  252. }
  253. void CConvertDlg::OnChangeTargetfilename()
  254. {
  255. m_fTargetFileNameChanged = TRUE;
  256. }
  257. void CConvertDlg::OnGbtounicode()
  258. {
  259. if (!m_fTargetFileNameChanged) {
  260. UpdateData(TRUE);
  261. if (GenerateTargetFileName(m_strSourceFileName, &m_strTargetFileName,
  262. m_ToUnicodeOrAnsi == 0 ? TRUE:FALSE)) {
  263. UpdateData(FALSE);
  264. } else {
  265. MsgOverflow();
  266. }
  267. }
  268. }
  269. void CConvertDlg::OnUnicodetogb()
  270. {
  271. if (!m_fTargetFileNameChanged) {
  272. UpdateData(TRUE);
  273. if (GenerateTargetFileName(m_strSourceFileName, &m_strTargetFileName,
  274. m_ToUnicodeOrAnsi == 0 ? TRUE:FALSE)) {
  275. UpdateData(FALSE);
  276. } else {
  277. MsgOverflow();
  278. }
  279. }
  280. }