Update editorconfig
[TortoiseGit.git] / src / TortoiseMerge / OpenDlg.cpp
blob1b3147e25a909c545ab6250313cb103c015de1c7
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2012 - TortoiseGit
4 // Copyright (C) 2006-2010, 2012-2013 - 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 "CommonAppUtils.h"
25 #include "registry.h"
27 // COpenDlg dialog
29 IMPLEMENT_DYNAMIC(COpenDlg, CStandAloneDialog)
30 COpenDlg::COpenDlg(CWnd* pParent /*=NULL*/)
31 : CStandAloneDialog(COpenDlg::IDD, pParent)
32 , m_bFromClipboard(FALSE)
33 , m_cFormat(0)
34 , m_nextViewer(NULL)
38 COpenDlg::~COpenDlg()
42 void COpenDlg::DoDataExchange(CDataExchange* pDX)
44 CStandAloneDialog::DoDataExchange(pDX);
45 DDX_Text(pDX, IDC_BASEFILEEDIT, m_sBaseFile);
46 DDX_Text(pDX, IDC_THEIRFILEEDIT, m_sTheirFile);
47 DDX_Text(pDX, IDC_YOURFILEEDIT, m_sYourFile);
48 DDX_Text(pDX, IDC_DIFFFILEEDIT, m_sUnifiedDiffFile);
49 DDX_Text(pDX, IDC_DIRECTORYEDIT, m_sPatchDirectory);
50 DDX_Control(pDX, IDC_BASEFILEEDIT, m_cBaseFileEdit);
51 DDX_Control(pDX, IDC_THEIRFILEEDIT, m_cTheirFileEdit);
52 DDX_Control(pDX, IDC_YOURFILEEDIT, m_cYourFileEdit);
53 DDX_Control(pDX, IDC_DIFFFILEEDIT, m_cDiffFileEdit);
54 DDX_Control(pDX, IDC_DIRECTORYEDIT, m_cDirEdit);
55 DDX_Check(pDX, IDC_PATCHFROMCLIPBOARD, m_bFromClipboard);
58 BEGIN_MESSAGE_MAP(COpenDlg, CStandAloneDialog)
59 ON_BN_CLICKED(IDC_BASEFILEBROWSE, OnBnClickedBasefilebrowse)
60 ON_BN_CLICKED(IDC_THEIRFILEBROWSE, OnBnClickedTheirfilebrowse)
61 ON_BN_CLICKED(IDC_YOURFILEBROWSE, OnBnClickedYourfilebrowse)
62 ON_BN_CLICKED(IDHELP, OnBnClickedHelp)
63 ON_BN_CLICKED(IDC_DIFFFILEBROWSE, OnBnClickedDifffilebrowse)
64 ON_BN_CLICKED(IDC_DIRECTORYBROWSE, OnBnClickedDirectorybrowse)
65 ON_BN_CLICKED(IDC_MERGERADIO, OnBnClickedMergeradio)
66 ON_BN_CLICKED(IDC_APPLYRADIO, OnBnClickedApplyradio)
67 ON_WM_CHANGECBCHAIN()
68 ON_WM_DRAWCLIPBOARD()
69 ON_WM_DESTROY()
70 ON_BN_CLICKED(IDC_PATCHFROMCLIPBOARD, &COpenDlg::OnBnClickedPatchfromclipboard)
71 END_MESSAGE_MAP()
73 BOOL COpenDlg::OnInitDialog()
75 CStandAloneDialog::OnInitDialog();
77 CRegDWORD lastRadioButton(_T("Software\\TortoiseGitMerge\\OpenRadio"), IDC_MERGERADIO);
78 if (((DWORD)lastRadioButton != IDC_MERGERADIO)&&((DWORD)lastRadioButton != IDC_APPLYRADIO))
79 lastRadioButton = IDC_MERGERADIO;
80 GroupRadio((DWORD)lastRadioButton);
81 CheckRadioButton(IDC_MERGERADIO, IDC_APPLYRADIO, (DWORD)lastRadioButton);
83 // turn on auto completion for the edit controls
84 AutoCompleteOn(IDC_BASEFILEEDIT);
85 AutoCompleteOn(IDC_THEIRFILEEDIT);
86 AutoCompleteOn(IDC_YOURFILEEDIT);
87 AutoCompleteOn(IDC_DIFFFILEEDIT);
88 AutoCompleteOn(IDC_DIRECTORYEDIT);
90 m_cFormat = RegisterClipboardFormat(_T("TGIT_UNIFIEDDIFF"));
91 m_nextViewer = SetClipboardViewer();
93 return TRUE; // return TRUE unless you set the focus to a control
94 // EXCEPTION: OCX Property Pages should return FALSE
97 // COpenDlg message handlers
99 void COpenDlg::OnBnClickedBasefilebrowse()
101 OnBrowseForFile(m_sBaseFile);
104 void COpenDlg::OnBnClickedTheirfilebrowse()
106 OnBrowseForFile(m_sTheirFile);
109 void COpenDlg::OnBnClickedYourfilebrowse()
111 OnBrowseForFile(m_sYourFile);
114 void COpenDlg::OnBnClickedHelp()
116 this->OnHelp();
119 void COpenDlg::OnBrowseForFile(CString& filepath, UINT nFileFilter)
121 UpdateData();
122 CCommonAppUtils::FileOpenSave(filepath, NULL, IDS_SELECTFILE, nFileFilter, true, m_hWnd);
123 UpdateData(FALSE);
126 void COpenDlg::OnBnClickedDifffilebrowse()
128 OnBrowseForFile(m_sUnifiedDiffFile, IDS_PATCHFILEFILTER);
131 void COpenDlg::OnBnClickedDirectorybrowse()
133 CBrowseFolder folderBrowser;
134 UpdateData();
135 folderBrowser.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
136 folderBrowser.Show(GetSafeHwnd(), m_sPatchDirectory, m_sPatchDirectory);
137 UpdateData(FALSE);
140 void COpenDlg::OnBnClickedMergeradio()
142 GroupRadio(IDC_MERGERADIO);
145 void COpenDlg::OnBnClickedApplyradio()
147 GroupRadio(IDC_APPLYRADIO);
150 void COpenDlg::GroupRadio(UINT nID)
152 BOOL bMerge = FALSE;
153 BOOL bUnified = FALSE;
154 if (nID == IDC_MERGERADIO)
155 bMerge = TRUE;
156 if (nID == IDC_APPLYRADIO)
157 bUnified = TRUE;
159 GetDlgItem(IDC_BASEFILEEDIT)->EnableWindow(bMerge);
160 GetDlgItem(IDC_BASEFILEBROWSE)->EnableWindow(bMerge);
161 GetDlgItem(IDC_THEIRFILEEDIT)->EnableWindow(bMerge);
162 GetDlgItem(IDC_THEIRFILEBROWSE)->EnableWindow(bMerge);
163 GetDlgItem(IDC_YOURFILEEDIT)->EnableWindow(bMerge);
164 GetDlgItem(IDC_YOURFILEBROWSE)->EnableWindow(bMerge);
166 GetDlgItem(IDC_DIFFFILEEDIT)->EnableWindow(bUnified);
167 GetDlgItem(IDC_DIFFFILEBROWSE)->EnableWindow(bUnified);
168 GetDlgItem(IDC_DIRECTORYEDIT)->EnableWindow(bUnified);
169 GetDlgItem(IDC_DIRECTORYBROWSE)->EnableWindow(bUnified);
171 CheckAndEnableClipboardChecker();
174 void COpenDlg::OnOK()
176 UpdateData(TRUE);
178 bool bUDiffOnClipboard = false;
179 if (OpenClipboard())
181 UINT enumFormat = 0;
184 if (enumFormat == m_cFormat)
186 bUDiffOnClipboard = true;
188 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);
189 CloseClipboard();
192 if (GetDlgItem(IDC_BASEFILEEDIT)->IsWindowEnabled())
194 m_sUnifiedDiffFile.Empty();
195 m_sPatchDirectory.Empty();
197 else
199 m_sBaseFile.Empty();
200 m_sYourFile.Empty();
201 m_sTheirFile.Empty();
203 UpdateData(FALSE);
204 CString sFile;
205 if (!m_sUnifiedDiffFile.IsEmpty())
206 if (!PathFileExists(m_sUnifiedDiffFile))
207 sFile = m_sUnifiedDiffFile;
208 if (!m_sPatchDirectory.IsEmpty())
209 if (!PathFileExists(m_sPatchDirectory))
210 sFile = m_sPatchDirectory;
211 if (!m_sBaseFile.IsEmpty())
212 if (!PathFileExists(m_sBaseFile))
213 sFile = m_sBaseFile;
214 if (!m_sYourFile.IsEmpty())
215 if (!PathFileExists(m_sYourFile))
216 sFile = m_sYourFile;
217 if (!m_sTheirFile.IsEmpty())
218 if (!PathFileExists(m_sTheirFile))
219 sFile = m_sTheirFile;
221 if (bUDiffOnClipboard && m_bFromClipboard)
223 if (OpenClipboard())
225 HGLOBAL hglb = GetClipboardData(m_cFormat);
226 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
228 DWORD len = GetTempPath(0, NULL);
229 auto path = std::make_unique<TCHAR[]>(len + 1);
230 auto tempF = std::make_unique<TCHAR[]>(len + 100);
231 GetTempPath (len+1, path.get());
232 GetTempFileName (path.get(), _T("tsm"), 0, tempF.get());
233 CString sTempFile = CString(tempF.get());
235 FILE * outFile;
236 size_t patchlen = strlen(lpstr);
237 _tfopen_s(&outFile, sTempFile, _T("wb"));
238 if(outFile)
240 size_t size = fwrite(lpstr, sizeof(char), patchlen, outFile);
241 if (size < patchlen)
242 bUDiffOnClipboard = false;
243 else
245 m_sUnifiedDiffFile = sTempFile;
246 UpdateData(FALSE);
247 sFile.Empty();
249 fclose(outFile);
251 GlobalUnlock(hglb);
252 CloseClipboard();
257 if (!sFile.IsEmpty())
259 CString sErr;
260 sErr.Format(IDS_ERR_PATCH_INVALIDPATCHFILE, (LPCTSTR)sFile);
261 MessageBox(sErr, NULL, MB_ICONERROR);
262 return;
264 CRegDWORD lastRadioButton(_T("Software\\TortoiseGitMerge\\OpenRadio"), IDC_MERGERADIO);
265 lastRadioButton = GetCheckedRadioButton(IDC_MERGERADIO, IDC_APPLYRADIO);
266 CStandAloneDialog::OnOK();
269 void COpenDlg::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
271 CStandAloneDialog::OnChangeCbChain(hWndRemove, hWndAfter);
274 bool COpenDlg::CheckAndEnableClipboardChecker()
276 int radio = GetCheckedRadioButton(IDC_MERGERADIO, IDC_APPLYRADIO);
277 bool bUDiffOnClipboard = false;
278 if (radio == IDC_APPLYRADIO)
280 if (OpenClipboard())
282 UINT enumFormat = 0;
285 if (enumFormat == m_cFormat)
287 bUDiffOnClipboard = true;
289 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);
290 CloseClipboard();
294 DialogEnableWindow(IDC_PATCHFROMCLIPBOARD, bUDiffOnClipboard);
295 return bUDiffOnClipboard;
298 void COpenDlg::OnDrawClipboard()
300 CheckAndEnableClipboardChecker();
301 CStandAloneDialog::OnDrawClipboard();
304 void COpenDlg::OnDestroy()
306 ChangeClipboardChain(m_nextViewer);
307 CStandAloneDialog::OnDestroy();
310 void COpenDlg::OnBnClickedPatchfromclipboard()
312 UpdateData();
313 DialogEnableWindow(IDC_DIFFFILEEDIT, !m_bFromClipboard);
314 DialogEnableWindow(IDC_DIFFFILEBROWSE, !m_bFromClipboard);
317 void COpenDlg::AutoCompleteOn(int controlId)
319 HWND hwnd;
320 GetDlgItem(controlId, &hwnd);
321 if (hwnd)
322 SHAutoComplete(hwnd, SHACF_AUTOSUGGEST_FORCE_ON | SHACF_AUTOAPPEND_FORCE_ON | SHACF_FILESYSTEM);