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.
21 #include "TortoiseMerge.h"
22 #include "BrowseFolder.h"
24 #include "auto_buffer.h"
25 #include "CommonAppUtils.h"
30 IMPLEMENT_DYNAMIC(COpenDlg
, CStandAloneDialog
)
31 COpenDlg::COpenDlg(CWnd
* pParent
/*=NULL*/)
32 : CStandAloneDialog(COpenDlg::IDD
, pParent
)
34 , m_sTheirFile(_T(""))
36 , m_sUnifiedDiffFile(_T(""))
37 , m_sPatchDirectory(_T(""))
38 , m_bFromClipboard(FALSE
)
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
)
76 ON_BN_CLICKED(IDC_PATCHFROMCLIPBOARD
, &COpenDlg::OnBnClickedPatchfromclipboard
)
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()
125 void COpenDlg::OnBrowseForFile(CString
& filepath
, UINT nFileFilter
)
128 CCommonAppUtils::FileOpenSave(filepath
, NULL
, IDS_SELECTFILE
, nFileFilter
, true, this->m_hWnd
);
132 void COpenDlg::OnBnClickedDifffilebrowse()
134 OnBrowseForFile(m_sUnifiedDiffFile
, IDS_PATCHFILEFILTER
);
137 void COpenDlg::OnBnClickedDirectorybrowse()
139 CBrowseFolder folderBrowser
;
141 folderBrowser
.m_style
= BIF_EDITBOX
| BIF_NEWDIALOGSTYLE
| BIF_RETURNFSANCESTORS
| BIF_RETURNONLYFSDIRS
;
142 folderBrowser
.Show(GetSafeHwnd(), m_sPatchDirectory
, m_sPatchDirectory
);
146 void COpenDlg::OnBnClickedMergeradio()
148 GroupRadio(IDC_MERGERADIO
);
151 void COpenDlg::OnBnClickedApplyradio()
153 GroupRadio(IDC_APPLYRADIO
);
156 void COpenDlg::GroupRadio(UINT nID
)
159 BOOL bUnified
= FALSE
;
160 if (nID
== IDC_MERGERADIO
)
162 if (nID
== IDC_APPLYRADIO
)
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()
184 bool bUDiffOnClipboard
= false;
190 if (enumFormat
== m_cFormat
)
192 bUDiffOnClipboard
= true;
194 } while((enumFormat
= EnumClipboardFormats(enumFormat
))!=0);
198 if (GetDlgItem(IDC_BASEFILEEDIT
)->IsWindowEnabled())
200 m_sUnifiedDiffFile
.Empty();
201 m_sPatchDirectory
.Empty();
207 m_sTheirFile
.Empty();
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
))
220 if (!m_sYourFile
.IsEmpty())
221 if (!PathFileExists(m_sYourFile
))
223 if (!m_sTheirFile
.IsEmpty())
224 if (!PathFileExists(m_sTheirFile
))
225 sFile
= m_sTheirFile
;
227 if (bUDiffOnClipboard
&& m_bFromClipboard
)
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
);
242 size_t patchlen
= strlen(lpstr
);
243 _tfopen_s(&outFile
, sTempFile
, _T("wb"));
246 size_t size
= fwrite(lpstr
, sizeof(char), patchlen
, outFile
);
248 bUDiffOnClipboard
= false;
251 m_sUnifiedDiffFile
= sTempFile
;
263 if (!sFile
.IsEmpty())
266 sErr
.Format(IDS_ERR_PATCH_INVALIDPATCHFILE
, (LPCTSTR
)sFile
);
267 MessageBox(sErr
, NULL
, MB_ICONERROR
);
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
)
291 if (enumFormat
== m_cFormat
)
293 bUDiffOnClipboard
= true;
295 } while((enumFormat
= EnumClipboardFormats(enumFormat
))!=0);
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()
319 DialogEnableWindow(IDC_DIFFFILEEDIT
, !m_bFromClipboard
);
320 DialogEnableWindow(IDC_DIFFFILEBROWSE
, !m_bFromClipboard
);
323 void COpenDlg::AutoCompleteOn(int controlId
)
326 GetDlgItem(controlId
, &hwnd
);
328 SHAutoComplete(hwnd
, SHACF_AUTOSUGGEST_FORCE_ON
| SHACF_AUTOAPPEND_FORCE_ON
| SHACF_FILESYSTEM
);