Fixed issue #1507: Submodule Diff Dialog should show dirty state only on working...
[TortoiseGit.git] / src / TortoiseMerge / OpenDlg.cpp
blob5373d6cc12cb6a4faa20ab61134c69c6109700db
1 // TortoiseMerge - a Diff/Patch program
3 // Copyright (C) 2012 - TortoiseGit
4 // Copyright (C) 2006-2010 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "TortoiseMerge.h"
22 #include "BrowseFolder.h"
23 #include "opendlg.h"
24 #include "auto_buffer.h"
25 #include "CommonAppUtils.h"
26 #include "registry.h"
28 // COpenDlg dialog
30 IMPLEMENT_DYNAMIC(COpenDlg, CStandAloneDialog)
31 COpenDlg::COpenDlg(CWnd* pParent /*=NULL*/)
32 : CStandAloneDialog(COpenDlg::IDD, pParent)
33 , m_sBaseFile(_T(""))
34 , m_sTheirFile(_T(""))
35 , m_sYourFile(_T(""))
36 , m_sUnifiedDiffFile(_T(""))
37 , m_sPatchDirectory(_T(""))
38 , m_bFromClipboard(FALSE)
39 , m_cFormat(0)
40 , m_nextViewer(NULL)
44 COpenDlg::~COpenDlg()
48 void COpenDlg::DoDataExchange(CDataExchange* pDX)
50 CStandAloneDialog::DoDataExchange(pDX);
51 DDX_Text(pDX, IDC_BASEFILEEDIT, m_sBaseFile);
52 DDX_Text(pDX, IDC_THEIRFILEEDIT, m_sTheirFile);
53 DDX_Text(pDX, IDC_YOURFILEEDIT, m_sYourFile);
54 DDX_Text(pDX, IDC_DIFFFILEEDIT, m_sUnifiedDiffFile);
55 DDX_Text(pDX, IDC_DIRECTORYEDIT, m_sPatchDirectory);
56 DDX_Control(pDX, IDC_BASEFILEEDIT, m_cBaseFileEdit);
57 DDX_Control(pDX, IDC_THEIRFILEEDIT, m_cTheirFileEdit);
58 DDX_Control(pDX, IDC_YOURFILEEDIT, m_cYourFileEdit);
59 DDX_Control(pDX, IDC_DIFFFILEEDIT, m_cDiffFileEdit);
60 DDX_Control(pDX, IDC_DIRECTORYEDIT, m_cDirEdit);
61 DDX_Check(pDX, IDC_PATCHFROMCLIPBOARD, m_bFromClipboard);
64 BEGIN_MESSAGE_MAP(COpenDlg, CStandAloneDialog)
65 ON_BN_CLICKED(IDC_BASEFILEBROWSE, OnBnClickedBasefilebrowse)
66 ON_BN_CLICKED(IDC_THEIRFILEBROWSE, OnBnClickedTheirfilebrowse)
67 ON_BN_CLICKED(IDC_YOURFILEBROWSE, OnBnClickedYourfilebrowse)
68 ON_BN_CLICKED(IDHELP, OnBnClickedHelp)
69 ON_BN_CLICKED(IDC_DIFFFILEBROWSE, OnBnClickedDifffilebrowse)
70 ON_BN_CLICKED(IDC_DIRECTORYBROWSE, OnBnClickedDirectorybrowse)
71 ON_BN_CLICKED(IDC_MERGERADIO, OnBnClickedMergeradio)
72 ON_BN_CLICKED(IDC_APPLYRADIO, OnBnClickedApplyradio)
73 ON_WM_CHANGECBCHAIN()
74 ON_WM_DRAWCLIPBOARD()
75 ON_WM_DESTROY()
76 ON_BN_CLICKED(IDC_PATCHFROMCLIPBOARD, &COpenDlg::OnBnClickedPatchfromclipboard)
77 END_MESSAGE_MAP()
79 BOOL COpenDlg::OnInitDialog()
81 CStandAloneDialog::OnInitDialog();
83 CRegDWORD lastRadioButton(_T("Software\\TortoiseMerge\\OpenRadio"), IDC_MERGERADIO);
84 if (((DWORD)lastRadioButton != IDC_MERGERADIO)&&((DWORD)lastRadioButton != IDC_APPLYRADIO))
85 lastRadioButton = IDC_MERGERADIO;
86 GroupRadio((DWORD)lastRadioButton);
87 CheckRadioButton(IDC_MERGERADIO, IDC_APPLYRADIO, (DWORD)lastRadioButton);
89 // turn on auto completion for the edit controls
90 AutoCompleteOn(IDC_BASEFILEEDIT);
91 AutoCompleteOn(IDC_THEIRFILEEDIT);
92 AutoCompleteOn(IDC_YOURFILEEDIT);
93 AutoCompleteOn(IDC_DIFFFILEEDIT);
94 AutoCompleteOn(IDC_DIRECTORYEDIT);
96 m_cFormat = RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
97 m_nextViewer = SetClipboardViewer();
99 return TRUE; // return TRUE unless you set the focus to a control
100 // EXCEPTION: OCX Property Pages should return FALSE
103 // COpenDlg message handlers
105 void COpenDlg::OnBnClickedBasefilebrowse()
107 OnBrowseForFile(m_sBaseFile);
110 void COpenDlg::OnBnClickedTheirfilebrowse()
112 OnBrowseForFile(m_sTheirFile);
115 void COpenDlg::OnBnClickedYourfilebrowse()
117 OnBrowseForFile(m_sYourFile);
120 void COpenDlg::OnBnClickedHelp()
122 this->OnHelp();
125 void COpenDlg::OnBrowseForFile(CString& filepath, UINT nFileFilter)
127 UpdateData();
128 CCommonAppUtils::FileOpenSave(filepath, NULL, IDS_SELECTFILE, nFileFilter, true, this->m_hWnd);
129 UpdateData(FALSE);
132 void COpenDlg::OnBnClickedDifffilebrowse()
134 OnBrowseForFile(m_sUnifiedDiffFile, IDS_PATCHFILEFILTER);
137 void COpenDlg::OnBnClickedDirectorybrowse()
139 CBrowseFolder folderBrowser;
140 UpdateData();
141 folderBrowser.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
142 folderBrowser.Show(GetSafeHwnd(), m_sPatchDirectory, m_sPatchDirectory);
143 UpdateData(FALSE);
146 void COpenDlg::OnBnClickedMergeradio()
148 GroupRadio(IDC_MERGERADIO);
151 void COpenDlg::OnBnClickedApplyradio()
153 GroupRadio(IDC_APPLYRADIO);
156 void COpenDlg::GroupRadio(UINT nID)
158 BOOL bMerge = FALSE;
159 BOOL bUnified = FALSE;
160 if (nID == IDC_MERGERADIO)
161 bMerge = TRUE;
162 if (nID == IDC_APPLYRADIO)
163 bUnified = TRUE;
165 GetDlgItem(IDC_BASEFILEEDIT)->EnableWindow(bMerge);
166 GetDlgItem(IDC_BASEFILEBROWSE)->EnableWindow(bMerge);
167 GetDlgItem(IDC_THEIRFILEEDIT)->EnableWindow(bMerge);
168 GetDlgItem(IDC_THEIRFILEBROWSE)->EnableWindow(bMerge);
169 GetDlgItem(IDC_YOURFILEEDIT)->EnableWindow(bMerge);
170 GetDlgItem(IDC_YOURFILEBROWSE)->EnableWindow(bMerge);
172 GetDlgItem(IDC_DIFFFILEEDIT)->EnableWindow(bUnified);
173 GetDlgItem(IDC_DIFFFILEBROWSE)->EnableWindow(bUnified);
174 GetDlgItem(IDC_DIRECTORYEDIT)->EnableWindow(bUnified);
175 GetDlgItem(IDC_DIRECTORYBROWSE)->EnableWindow(bUnified);
177 CheckAndEnableClipboardChecker();
180 void COpenDlg::OnOK()
182 UpdateData(TRUE);
184 bool bUDiffOnClipboard = false;
185 if (OpenClipboard())
187 UINT enumFormat = 0;
190 if (enumFormat == m_cFormat)
192 bUDiffOnClipboard = true;
194 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);
195 CloseClipboard();
198 if (GetDlgItem(IDC_BASEFILEEDIT)->IsWindowEnabled())
200 m_sUnifiedDiffFile.Empty();
201 m_sPatchDirectory.Empty();
203 else
205 m_sBaseFile.Empty();
206 m_sYourFile.Empty();
207 m_sTheirFile.Empty();
209 UpdateData(FALSE);
210 CString sFile;
211 if (!m_sUnifiedDiffFile.IsEmpty())
212 if (!PathFileExists(m_sUnifiedDiffFile))
213 sFile = m_sUnifiedDiffFile;
214 if (!m_sPatchDirectory.IsEmpty())
215 if (!PathFileExists(m_sPatchDirectory))
216 sFile = m_sPatchDirectory;
217 if (!m_sBaseFile.IsEmpty())
218 if (!PathFileExists(m_sBaseFile))
219 sFile = m_sBaseFile;
220 if (!m_sYourFile.IsEmpty())
221 if (!PathFileExists(m_sYourFile))
222 sFile = m_sYourFile;
223 if (!m_sTheirFile.IsEmpty())
224 if (!PathFileExists(m_sTheirFile))
225 sFile = m_sTheirFile;
227 if (bUDiffOnClipboard && m_bFromClipboard)
229 if (OpenClipboard())
231 HGLOBAL hglb = GetClipboardData(m_cFormat);
232 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
234 DWORD len = GetTempPath(0, NULL);
235 auto_buffer<TCHAR> path(len+1);
236 auto_buffer<TCHAR> tempF(len+100);
237 GetTempPath (len+1, path);
238 GetTempFileName (path, TEXT("tsm"), 0, tempF);
239 CString sTempFile = CString(tempF);
241 FILE * outFile;
242 size_t patchlen = strlen(lpstr);
243 _tfopen_s(&outFile, sTempFile, _T("wb"));
244 if(outFile)
246 size_t size = fwrite(lpstr, sizeof(char), patchlen, outFile);
247 if (size < patchlen)
248 bUDiffOnClipboard = false;
249 else
251 m_sUnifiedDiffFile = sTempFile;
252 UpdateData(FALSE);
253 sFile.Empty();
255 fclose(outFile);
257 GlobalUnlock(hglb);
258 CloseClipboard();
263 if (!sFile.IsEmpty())
265 CString sErr;
266 sErr.Format(IDS_ERR_PATCH_INVALIDPATCHFILE, (LPCTSTR)sFile);
267 MessageBox(sErr, NULL, MB_ICONERROR);
268 return;
270 CRegDWORD lastRadioButton(_T("Software\\TortoiseMerge\\OpenRadio"), IDC_MERGERADIO);
271 lastRadioButton = GetCheckedRadioButton(IDC_MERGERADIO, IDC_APPLYRADIO);
272 CStandAloneDialog::OnOK();
275 void COpenDlg::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
277 CStandAloneDialog::OnChangeCbChain(hWndRemove, hWndAfter);
280 bool COpenDlg::CheckAndEnableClipboardChecker()
282 int radio = GetCheckedRadioButton(IDC_MERGERADIO, IDC_APPLYRADIO);
283 bool bUDiffOnClipboard = false;
284 if (radio == IDC_APPLYRADIO)
286 if (OpenClipboard())
288 UINT enumFormat = 0;
291 if (enumFormat == m_cFormat)
293 bUDiffOnClipboard = true;
295 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);
296 CloseClipboard();
300 DialogEnableWindow(IDC_PATCHFROMCLIPBOARD, bUDiffOnClipboard);
301 return bUDiffOnClipboard;
304 void COpenDlg::OnDrawClipboard()
306 CheckAndEnableClipboardChecker();
307 CStandAloneDialog::OnDrawClipboard();
310 void COpenDlg::OnDestroy()
312 ChangeClipboardChain(m_nextViewer);
313 CStandAloneDialog::OnDestroy();
316 void COpenDlg::OnBnClickedPatchfromclipboard()
318 UpdateData();
319 DialogEnableWindow(IDC_DIFFFILEEDIT, !m_bFromClipboard);
320 DialogEnableWindow(IDC_DIFFFILEBROWSE, !m_bFromClipboard);
323 void COpenDlg::AutoCompleteOn(int controlId)
325 HWND hwnd;
326 GetDlgItem(controlId, &hwnd);
327 if (hwnd)
328 SHAutoComplete(hwnd, SHACF_AUTOSUGGEST_FORCE_ON | SHACF_AUTOAPPEND_FORCE_ON | SHACF_FILESYSTEM);