OSDN Git Service

fix some build error
[tortoisegit/TortoiseGitJp.git] / src / TortoiseMerge / OpenDlg.cpp
1 // TortoiseMerge - a Diff/Patch program\r
2 \r
3 // Copyright (C) 2006-2008 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "stdafx.h"\r
20 #include "TortoiseMerge.h"\r
21 #include "BrowseFolder.h"\r
22 #include ".\opendlg.h"\r
23 \r
24 \r
25 // COpenDlg dialog\r
26 \r
27 IMPLEMENT_DYNAMIC(COpenDlg, CDialog)\r
28 COpenDlg::COpenDlg(CWnd* pParent /*=NULL*/)\r
29         : CDialog(COpenDlg::IDD, pParent)\r
30         , m_sBaseFile(_T(""))\r
31         , m_sTheirFile(_T(""))\r
32         , m_sYourFile(_T(""))\r
33         , m_sUnifiedDiffFile(_T(""))\r
34         , m_sPatchDirectory(_T(""))\r
35         , m_bFromClipboard(FALSE)\r
36         , m_cFormat(0)\r
37         , m_nextViewer(NULL)\r
38 {\r
39 }\r
40 \r
41 COpenDlg::~COpenDlg()\r
42 {\r
43 }\r
44 \r
45 void COpenDlg::DoDataExchange(CDataExchange* pDX)\r
46 {\r
47         CDialog::DoDataExchange(pDX);\r
48         DDX_Text(pDX, IDC_BASEFILEEDIT, m_sBaseFile);\r
49         DDX_Text(pDX, IDC_THEIRFILEEDIT, m_sTheirFile);\r
50         DDX_Text(pDX, IDC_YOURFILEEDIT, m_sYourFile);\r
51         DDX_Text(pDX, IDC_DIFFFILEEDIT, m_sUnifiedDiffFile);\r
52         DDX_Text(pDX, IDC_DIRECTORYEDIT, m_sPatchDirectory);\r
53         DDX_Control(pDX, IDC_BASEFILEEDIT, m_cBaseFileEdit);\r
54         DDX_Control(pDX, IDC_THEIRFILEEDIT, m_cTheirFileEdit);\r
55         DDX_Control(pDX, IDC_YOURFILEEDIT, m_cYourFileEdit);\r
56         DDX_Control(pDX, IDC_DIFFFILEEDIT, m_cDiffFileEdit);\r
57         DDX_Control(pDX, IDC_DIRECTORYEDIT, m_cDirEdit);\r
58         DDX_Check(pDX, IDC_PATCHFROMCLIPBOARD, m_bFromClipboard);\r
59 }\r
60 \r
61 BEGIN_MESSAGE_MAP(COpenDlg, CDialog)\r
62         ON_BN_CLICKED(IDC_BASEFILEBROWSE, OnBnClickedBasefilebrowse)\r
63         ON_BN_CLICKED(IDC_THEIRFILEBROWSE, OnBnClickedTheirfilebrowse)\r
64         ON_BN_CLICKED(IDC_YOURFILEBROWSE, OnBnClickedYourfilebrowse)\r
65         ON_BN_CLICKED(IDC_HELPBUTTON, OnBnClickedHelp)\r
66         ON_BN_CLICKED(IDC_DIFFFILEBROWSE, OnBnClickedDifffilebrowse)\r
67         ON_BN_CLICKED(IDC_DIRECTORYBROWSE, OnBnClickedDirectorybrowse)\r
68         ON_BN_CLICKED(IDC_MERGERADIO, OnBnClickedMergeradio)\r
69         ON_BN_CLICKED(IDC_APPLYRADIO, OnBnClickedApplyradio)\r
70         ON_WM_CHANGECBCHAIN()\r
71         ON_WM_DRAWCLIPBOARD()\r
72         ON_WM_DESTROY()\r
73         ON_BN_CLICKED(IDC_PATCHFROMCLIPBOARD, &COpenDlg::OnBnClickedPatchfromclipboard)\r
74 END_MESSAGE_MAP()\r
75 \r
76 BOOL COpenDlg::OnInitDialog()\r
77 {\r
78         CDialog::OnInitDialog();\r
79 \r
80         GroupRadio(IDC_MERGERADIO);\r
81 \r
82         CheckRadioButton(IDC_MERGERADIO, IDC_APPLYRADIO, IDC_MERGERADIO);\r
83 \r
84         // turn on auto completion for the edit controls\r
85         HWND hwndEdit;\r
86         GetDlgItem(IDC_BASEFILEEDIT, &hwndEdit);\r
87         if (hwndEdit)\r
88                 SHAutoComplete(hwndEdit, SHACF_AUTOSUGGEST_FORCE_ON | SHACF_AUTOAPPEND_FORCE_ON | SHACF_FILESYSTEM);\r
89         GetDlgItem(IDC_THEIRFILEEDIT, &hwndEdit);\r
90         if (hwndEdit)\r
91                 SHAutoComplete(hwndEdit, SHACF_AUTOSUGGEST_FORCE_ON | SHACF_AUTOAPPEND_FORCE_ON | SHACF_FILESYSTEM);\r
92         GetDlgItem(IDC_YOURFILEEDIT, &hwndEdit);\r
93         if (hwndEdit)\r
94                 SHAutoComplete(hwndEdit, SHACF_AUTOSUGGEST_FORCE_ON | SHACF_AUTOAPPEND_FORCE_ON | SHACF_FILESYSTEM);\r
95         GetDlgItem(IDC_DIFFFILEEDIT, &hwndEdit);\r
96         if (hwndEdit)\r
97                 SHAutoComplete(hwndEdit, SHACF_AUTOSUGGEST_FORCE_ON | SHACF_AUTOAPPEND_FORCE_ON | SHACF_FILESYSTEM);\r
98         GetDlgItem(IDC_DIRECTORYEDIT, &hwndEdit);\r
99         if (hwndEdit)\r
100                 SHAutoComplete(hwndEdit, SHACF_AUTOSUGGEST_FORCE_ON | SHACF_AUTOAPPEND_FORCE_ON | SHACF_FILESYSTEM);\r
101 \r
102         m_cFormat = RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));\r
103         m_nextViewer = SetClipboardViewer();\r
104 \r
105         return TRUE;  // return TRUE unless you set the focus to a control\r
106         // EXCEPTION: OCX Property Pages should return FALSE\r
107 }\r
108 \r
109 // COpenDlg message handlers\r
110 \r
111 void COpenDlg::OnBnClickedBasefilebrowse()\r
112 {\r
113         CString temp;\r
114         UpdateData();\r
115         temp.LoadString(IDS_SELECTFILE);\r
116         BrowseForFile(m_sBaseFile, temp);\r
117         UpdateData(FALSE);\r
118 }\r
119 \r
120 void COpenDlg::OnBnClickedTheirfilebrowse()\r
121 {\r
122         CString temp;\r
123         UpdateData();\r
124         temp.LoadString(IDS_SELECTFILE);\r
125         BrowseForFile(m_sTheirFile, temp);\r
126         UpdateData(FALSE);\r
127 }\r
128 \r
129 void COpenDlg::OnBnClickedYourfilebrowse()\r
130 {\r
131         CString temp;\r
132         UpdateData();\r
133         temp.LoadString(IDS_SELECTFILE);\r
134         BrowseForFile(m_sYourFile, temp);\r
135         UpdateData(FALSE);\r
136 }\r
137 \r
138 void COpenDlg::OnBnClickedHelp()\r
139 {\r
140         this->OnHelp();\r
141 }\r
142 \r
143 BOOL COpenDlg::BrowseForFile(CString& filepath, CString title, UINT nFileFilter)\r
144 {\r
145         OPENFILENAME ofn = {0};                 // common dialog box structure\r
146         TCHAR szFile[MAX_PATH] = {0};   // buffer for file name\r
147         if (!filepath.IsEmpty())\r
148         {\r
149                 _tcscpy_s(szFile, filepath);\r
150         }\r
151         // Initialize OPENFILENAME\r
152         ofn.lStructSize = sizeof(OPENFILENAME);\r
153         ofn.hwndOwner = this->m_hWnd;\r
154         ofn.lpstrFile = szFile;\r
155         ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);\r
156         CString sFilter;\r
157         sFilter.LoadString(nFileFilter);\r
158         TCHAR * pszFilters = new TCHAR[sFilter.GetLength()+4];\r
159         _tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);\r
160         // Replace '|' delimiters with '\0's\r
161         TCHAR *ptr = pszFilters + _tcslen(pszFilters);  //set ptr at the NULL\r
162         while (ptr != pszFilters)\r
163         {\r
164                 if (*ptr == '|')\r
165                         *ptr = '\0';\r
166                 ptr--;\r
167         }\r
168         ofn.lpstrFilter = pszFilters;\r
169         ofn.nFilterIndex = 1;\r
170         ofn.lpstrFileTitle = NULL;\r
171         ofn.nMaxFileTitle = 0;\r
172         ofn.lpstrInitialDir = NULL;\r
173         ofn.lpstrTitle = title;\r
174         ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;\r
175 \r
176         // Display the Open dialog box. \r
177 \r
178         if (GetOpenFileName(&ofn)==TRUE)\r
179         {\r
180                 filepath = CString(ofn.lpstrFile);\r
181                 delete [] pszFilters;\r
182                 return TRUE;\r
183         }\r
184         delete [] pszFilters;\r
185         return FALSE;                   //user canceled the dialog\r
186 }\r
187 \r
188 void COpenDlg::OnBnClickedDifffilebrowse()\r
189 {\r
190         CString temp;\r
191         UpdateData();\r
192         temp.LoadString(IDS_SELECTFILE);\r
193         BrowseForFile(m_sUnifiedDiffFile, temp, IDS_PATCHFILEFILTER);\r
194         UpdateData(FALSE);\r
195 }\r
196 \r
197 void COpenDlg::OnBnClickedDirectorybrowse()\r
198 {\r
199         CBrowseFolder folderBrowser;\r
200         UpdateData();\r
201         folderBrowser.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;\r
202         folderBrowser.Show(GetSafeHwnd(), m_sPatchDirectory, m_sPatchDirectory);\r
203         UpdateData(FALSE);\r
204 }\r
205 \r
206 void COpenDlg::OnBnClickedMergeradio()\r
207 {\r
208         GroupRadio(IDC_MERGERADIO);\r
209 }\r
210 \r
211 void COpenDlg::OnBnClickedApplyradio()\r
212 {\r
213         GroupRadio(IDC_APPLYRADIO);\r
214 }\r
215 \r
216 void COpenDlg::GroupRadio(UINT nID)\r
217 {\r
218         BOOL bMerge = FALSE;\r
219         BOOL bUnified = FALSE;\r
220         if (nID == IDC_MERGERADIO)\r
221                 bMerge = TRUE;\r
222         if (nID == IDC_APPLYRADIO)\r
223                 bUnified = TRUE;\r
224 \r
225         GetDlgItem(IDC_BASEFILEEDIT)->EnableWindow(bMerge);\r
226         GetDlgItem(IDC_BASEFILEBROWSE)->EnableWindow(bMerge);\r
227         GetDlgItem(IDC_THEIRFILEEDIT)->EnableWindow(bMerge);\r
228         GetDlgItem(IDC_THEIRFILEBROWSE)->EnableWindow(bMerge);\r
229         GetDlgItem(IDC_YOURFILEEDIT)->EnableWindow(bMerge);\r
230         GetDlgItem(IDC_YOURFILEBROWSE)->EnableWindow(bMerge);\r
231 \r
232         GetDlgItem(IDC_DIFFFILEEDIT)->EnableWindow(bUnified);\r
233         GetDlgItem(IDC_DIFFFILEBROWSE)->EnableWindow(bUnified);\r
234         GetDlgItem(IDC_DIRECTORYEDIT)->EnableWindow(bUnified);\r
235         GetDlgItem(IDC_DIRECTORYBROWSE)->EnableWindow(bUnified);\r
236 \r
237         CheckAndEnableClipboardChecker();\r
238 }\r
239 \r
240 void COpenDlg::OnOK()\r
241 {\r
242         UpdateData(TRUE);\r
243 \r
244         bool bUDiffOnClipboard = false;\r
245         if (OpenClipboard())\r
246         {\r
247                 UINT enumFormat = 0;\r
248                 do \r
249                 {\r
250                         if (enumFormat == m_cFormat)\r
251                         {\r
252                                 bUDiffOnClipboard = true;\r
253                         }\r
254                 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);\r
255                 CloseClipboard();\r
256         }\r
257 \r
258         if (GetDlgItem(IDC_BASEFILEEDIT)->IsWindowEnabled())\r
259         {\r
260                 m_sUnifiedDiffFile.Empty();\r
261                 m_sPatchDirectory.Empty();\r
262         }\r
263         else\r
264         {\r
265                 m_sBaseFile.Empty();\r
266                 m_sYourFile.Empty();\r
267                 m_sTheirFile.Empty();\r
268         }\r
269         UpdateData(FALSE);\r
270         CString sFile;\r
271         if (!m_sUnifiedDiffFile.IsEmpty())\r
272                 if (!PathFileExists(m_sUnifiedDiffFile))\r
273                         sFile = m_sUnifiedDiffFile;\r
274         if (!m_sPatchDirectory.IsEmpty())\r
275                 if (!PathFileExists(m_sPatchDirectory))\r
276                         sFile = m_sPatchDirectory;\r
277         if (!m_sBaseFile.IsEmpty())\r
278                 if (!PathFileExists(m_sBaseFile))\r
279                         sFile = m_sBaseFile;\r
280         if (!m_sYourFile.IsEmpty())\r
281                 if (!PathFileExists(m_sYourFile))\r
282                         sFile = m_sYourFile;\r
283         if (!m_sTheirFile.IsEmpty())\r
284                 if (!PathFileExists(m_sTheirFile))\r
285                         sFile = m_sTheirFile;\r
286 \r
287         if (bUDiffOnClipboard && m_bFromClipboard)\r
288         {\r
289                 if (OpenClipboard()) \r
290                 { \r
291                         HGLOBAL hglb = GetClipboardData(m_cFormat); \r
292                         LPCSTR lpstr = (LPCSTR)GlobalLock(hglb); \r
293 \r
294                         DWORD len = GetTempPath(0, NULL);\r
295                         TCHAR * path = new TCHAR[len+1];\r
296                         TCHAR * tempF = new TCHAR[len+100];\r
297                         GetTempPath (len+1, path);\r
298                         GetTempFileName (path, TEXT("tsm"), 0, tempF);\r
299                         CString sTempFile = CString(tempF);\r
300                         delete [] path;\r
301                         delete [] tempF;\r
302 \r
303                         FILE * outFile;\r
304                         size_t patchlen = strlen(lpstr);\r
305                         _tfopen_s(&outFile, sTempFile, _T("wb"));\r
306                         if(outFile)\r
307                         {\r
308                                 size_t size = fwrite(lpstr, sizeof(char), patchlen, outFile);\r
309                                 if (size < patchlen)\r
310                                         bUDiffOnClipboard = false;\r
311                                 else\r
312                                 {\r
313                                         m_sUnifiedDiffFile = sTempFile;\r
314                                         UpdateData(FALSE);\r
315                                         sFile.Empty();\r
316                                 }\r
317                                 fclose(outFile);\r
318                         }\r
319                         GlobalUnlock(hglb); \r
320                         CloseClipboard(); \r
321                 } \r
322 \r
323         }\r
324 \r
325         if (!sFile.IsEmpty())\r
326         {\r
327                 CString sErr;\r
328                 sErr.Format(IDS_ERR_PATCH_INVALIDPATCHFILE, (LPCTSTR)sFile);\r
329                 MessageBox(sErr, NULL, MB_ICONERROR);\r
330                 return;\r
331         }\r
332         CDialog::OnOK();\r
333 }\r
334 \r
335 void COpenDlg::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)\r
336 {\r
337         CDialog::OnChangeCbChain(hWndRemove, hWndAfter);\r
338 }\r
339 \r
340 bool COpenDlg::CheckAndEnableClipboardChecker()\r
341 {\r
342         int radio = GetCheckedRadioButton(IDC_MERGERADIO, IDC_APPLYRADIO);\r
343         bool bUDiffOnClipboard = false;\r
344         if (radio == IDC_APPLYRADIO)\r
345         {\r
346                 if (OpenClipboard())\r
347                 {\r
348                         UINT enumFormat = 0;\r
349                         do \r
350                         {\r
351                                 if (enumFormat == m_cFormat)\r
352                                 {\r
353                                         bUDiffOnClipboard = true;\r
354                                 }\r
355                         } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);\r
356                         CloseClipboard();\r
357                 }\r
358         }\r
359 \r
360         DialogEnableWindow(IDC_PATCHFROMCLIPBOARD, bUDiffOnClipboard);\r
361         return bUDiffOnClipboard;\r
362 }\r
363 \r
364 void COpenDlg::OnDrawClipboard()\r
365 {\r
366         CheckAndEnableClipboardChecker();\r
367         CDialog::OnDrawClipboard();\r
368 }\r
369 \r
370 void COpenDlg::OnDestroy()\r
371 {\r
372         ChangeClipboardChain(m_nextViewer);\r
373         CDialog::OnDestroy();\r
374 }\r
375 \r
376 BOOL COpenDlg::DialogEnableWindow(UINT nID, BOOL bEnable)\r
377 {\r
378         CWnd * pwndDlgItem = GetDlgItem(nID);\r
379         if (pwndDlgItem == NULL)\r
380                 return FALSE;\r
381         if (bEnable)\r
382                 return pwndDlgItem->EnableWindow(bEnable);\r
383         if (GetFocus() == pwndDlgItem)\r
384         {\r
385                 SendMessage(WM_NEXTDLGCTL, 0, FALSE);\r
386         }\r
387         return pwndDlgItem->EnableWindow(bEnable);\r
388 }\r
389 \r
390 void COpenDlg::OnBnClickedPatchfromclipboard()\r
391 {\r
392         UpdateData();\r
393         DialogEnableWindow(IDC_DIFFFILEEDIT, !m_bFromClipboard);\r
394         DialogEnableWindow(IDC_DIFFFILEBROWSE, !m_bFromClipboard);\r
395 }\r