SyncDlg: Disable reflist if libgit2 is disabled
[TortoiseGit.git] / src / TortoiseMerge / FilePatchesDlg.cpp
blobdc651ef238953b840ce3453a9d20b7ec3af60603
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2006, 2008, 2010-2012 - TortoiseSVN
4 // Copyright (C) 2012 - Sven Strickroth <email@cs-ware.de>
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 "FilePatchesDlg.h"
23 #include "GitPatch.h"
24 #include "AppUtils.h"
25 #include "PathUtils.h"
26 #include "SysProgressDlg.h"
27 #include "MessageBox.h"
29 IMPLEMENT_DYNAMIC(CFilePatchesDlg, CResizableStandAloneDialog)
30 CFilePatchesDlg::CFilePatchesDlg(CWnd* pParent /*=NULL*/)
31 : CResizableStandAloneDialog(CFilePatchesDlg::IDD, pParent)
32 , m_ShownIndex(-1)
33 , m_bMinimized(FALSE)
34 , m_pPatch(NULL)
35 , m_pCallBack(NULL)
36 , m_nWindowHeight(-1)
37 , m_pMainFrame(NULL)
38 , m_boldFont(NULL)
40 m_ImgList.Create(16, 16, ILC_COLOR16 | ILC_MASK, 4, 1);
43 CFilePatchesDlg::~CFilePatchesDlg()
45 if (m_boldFont)
46 DeleteObject(m_boldFont);
49 void CFilePatchesDlg::DoDataExchange(CDataExchange* pDX)
51 CResizableStandAloneDialog::DoDataExchange(pDX);
52 DDX_Control(pDX, IDC_FILELIST, m_cFileList);
55 BOOL CFilePatchesDlg::SetFileStatusAsPatched(CString sPath)
57 for (int i=0; i<m_arFileStates.GetCount(); i++)
59 if (sPath.CompareNoCase(GetFullPath(i))==0)
61 m_arFileStates.SetAt(i, (DWORD)FPDLG_FILESTATE_PATCHED);
62 Invalidate();
63 return TRUE;
66 return FALSE;
69 CString CFilePatchesDlg::GetFullPath(int nIndex)
71 CString temp = m_pPatch->GetStrippedPath(nIndex);
72 temp.Replace('/', '\\');
73 //temp = temp.Mid(temp.Find('\\')+1);
74 if (PathIsRelative(temp))
75 temp = m_sPath + temp;
76 return temp;
79 BOOL CFilePatchesDlg::OnInitDialog()
81 CResizableStandAloneDialog::OnInitDialog();
83 // hide the grip since it would overlap with the "path all" button
84 #if 0
85 HideGrip();
86 #endif
88 HFONT hFont = (HFONT)m_cFileList.SendMessage(WM_GETFONT);
89 LOGFONT lf = {0};
90 GetObject(hFont, sizeof(LOGFONT), &lf);
91 lf.lfWeight = FW_BOLD;
92 m_boldFont = CreateFontIndirect(&lf);
94 AddAnchor(IDC_FILELIST, TOP_LEFT, BOTTOM_RIGHT);
95 AddAnchor(IDC_PATCHSELECTEDBUTTON, BOTTOM_LEFT, BOTTOM_RIGHT);
96 AddAnchor(IDC_PATCHALLBUTTON, BOTTOM_LEFT, BOTTOM_RIGHT);
98 return TRUE;
101 BOOL CFilePatchesDlg::Init(GitPatch * pPatch, CPatchFilesDlgCallBack * pCallBack, CString sPath, CWnd * pParent)
103 if ((pCallBack==NULL)||(pPatch==NULL))
105 m_cFileList.DeleteAllItems();
106 return FALSE;
108 m_arFileStates.RemoveAll();
109 m_pPatch = pPatch;
110 m_pCallBack = pCallBack;
111 m_sPath = sPath;
112 if (m_sPath.IsEmpty())
114 CString title(MAKEINTRESOURCE(IDS_DIFF_TITLE));
115 SetWindowText(title);
117 else
119 CRect rect;
120 GetClientRect(&rect);
121 SetTitleWithPath(rect.Width());
122 if (m_sPath.Right(1).Compare(_T("\\"))==0)
123 m_sPath = m_sPath.Left(m_sPath.GetLength()-1);
125 m_sPath = m_sPath + _T("\\");
126 for (int i=m_ImgList.GetImageCount();i>0;i--)
128 m_ImgList.Remove(0);
132 m_cFileList.SetExtendedStyle(LVS_EX_INFOTIP | LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER);
133 m_cFileList.DeleteAllItems();
134 int c = ((CHeaderCtrl*)(m_cFileList.GetDlgItem(0)))->GetItemCount()-1;
135 while (c>=0)
136 m_cFileList.DeleteColumn(c--);
137 m_cFileList.InsertColumn(0, _T(""));
139 m_cFileList.SetRedraw(false);
141 for(int i=0; i<m_pPatch->GetNumberOfFiles(); i++)
143 CString sFile = CPathUtils::GetFileNameFromPath(m_pPatch->GetStrippedPath(i));
145 int state;
146 if (m_sPath.IsEmpty())
147 state = 0;
148 else
150 state = m_pPatch->GetFailedHunks(i);
152 if (m_pPatch->GetHasConflict(i))
153 state = FPDLG_FILESTATE_CONFLICT;
154 if (state > 0)
155 state = FPDLG_FILESTATE_ERROR;
156 m_arFileStates.Add(state);
157 CString sFileName = GetFullPath(i);
158 sFileName = CPathUtils::GetFileNameFromPath(sFileName);
159 SHFILEINFO sfi = {0};
160 SHGetFileInfo(
161 sFileName,
162 FILE_ATTRIBUTE_NORMAL,
163 &sfi,
164 sizeof(SHFILEINFO),
165 SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
166 m_cFileList.InsertItem(i, sFile, m_ImgList.Add(sfi.hIcon));
169 int mincol = 0;
170 int maxcol = ((CHeaderCtrl*)(m_cFileList.GetDlgItem(0)))->GetItemCount()-1;
171 int col;
172 for (col = mincol; col <= maxcol; col++)
174 m_cFileList.SetColumnWidth(col,LVSCW_AUTOSIZE_USEHEADER);
177 m_cFileList.SetImageList(&m_ImgList, LVSIL_SMALL);
178 m_cFileList.SetRedraw(true);
180 RECT parentrect;
181 pParent->GetWindowRect(&parentrect);
182 RECT windowrect;
183 GetWindowRect(&windowrect);
185 int width = windowrect.right - windowrect.left;
186 int height = windowrect.bottom - windowrect.top;
187 windowrect.right = parentrect.left;
188 windowrect.left = windowrect.right - width;
189 if (windowrect.left < 0)
191 windowrect.left = 0;
192 windowrect.right = width;
194 windowrect.top = parentrect.top;
195 windowrect.bottom = windowrect.top + height;
197 SetWindowPos(NULL, windowrect.left, windowrect.top, width, height, SWP_NOACTIVATE | SWP_NOZORDER);
199 m_nWindowHeight = windowrect.bottom - windowrect.top;
200 m_pMainFrame = pParent;
201 return TRUE;
204 BEGIN_MESSAGE_MAP(CFilePatchesDlg, CResizableStandAloneDialog)
205 ON_WM_SIZE()
206 ON_NOTIFY(LVN_GETINFOTIP, IDC_FILELIST, OnLvnGetInfoTipFilelist)
207 ON_NOTIFY(NM_DBLCLK, IDC_FILELIST, OnNMDblclkFilelist)
208 ON_NOTIFY(NM_CUSTOMDRAW, IDC_FILELIST, OnNMCustomdrawFilelist)
209 ON_NOTIFY(NM_RCLICK, IDC_FILELIST, OnNMRclickFilelist)
210 ON_WM_NCLBUTTONDBLCLK()
211 ON_WM_MOVING()
212 ON_BN_CLICKED(IDC_PATCHSELECTEDBUTTON, &CFilePatchesDlg::OnBnClickedPatchselectedbutton)
213 ON_BN_CLICKED(IDC_PATCHALLBUTTON, &CFilePatchesDlg::OnBnClickedPatchallbutton)
214 ON_NOTIFY(LVN_ITEMCHANGED, IDC_FILELIST, &CFilePatchesDlg::OnLvnItemchangedFilelist)
215 END_MESSAGE_MAP()
217 void CFilePatchesDlg::OnSize(UINT nType, int cx, int cy)
219 CResizableStandAloneDialog::OnSize(nType, cx, cy);
220 if (this->IsWindowVisible())
222 m_cFileList.SetColumnWidth(0, LVSCW_AUTOSIZE);
224 SetTitleWithPath(cx);
227 void CFilePatchesDlg::OnLvnGetInfoTipFilelist(NMHDR *pNMHDR, LRESULT *pResult)
229 LPNMLVGETINFOTIP pGetInfoTip = reinterpret_cast<LPNMLVGETINFOTIP>(pNMHDR);
231 if (m_arFileStates.GetCount() > pGetInfoTip->iItem)
233 CString temp;
234 if (m_arFileStates.GetAt(pGetInfoTip->iItem) == 0)
235 temp = GetFullPath(pGetInfoTip->iItem);
236 else
237 temp.Format(IDS_PATCH_ITEMTT, (LPCTSTR)GetFullPath(pGetInfoTip->iItem), m_arFileStates.GetAt(pGetInfoTip->iItem));
238 _tcsncpy_s(pGetInfoTip->pszText, pGetInfoTip->cchTextMax, temp, pGetInfoTip->cchTextMax - 1);
240 else
241 pGetInfoTip->pszText[0] = 0;
243 *pResult = 0;
246 void CFilePatchesDlg::OnNMDblclkFilelist(NMHDR *pNMHDR, LRESULT *pResult)
248 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
249 *pResult = 0;
250 if ((pNMLV->iItem < 0) || (pNMLV->iItem >= m_arFileStates.GetCount()))
251 return;
252 if (m_pCallBack==NULL)
253 return;
255 if (m_arFileStates.GetAt(pNMLV->iItem) == FPDLG_FILESTATE_ERROR)
257 MessageBox(m_pPatch->GetPatchRejects(pNMLV->iItem), NULL, MB_ICONERROR);
258 return;
261 if (m_sPath.IsEmpty())
263 m_pCallBack->DiffFiles(GetFullPath(pNMLV->iItem), _T(""),
264 _T(""), _T(""));
265 m_ShownIndex = pNMLV->iItem;
266 m_cFileList.Invalidate();
268 else
270 if (m_arFileStates.GetAt(pNMLV->iItem)!=FPDLG_FILESTATE_PATCHED)
272 m_pCallBack->PatchFile(m_pPatch->GetStrippedPath(pNMLV->iItem), m_pPatch->GetContentMods(pNMLV->iItem), m_pPatch->GetPropMods(pNMLV->iItem), _T(""));
273 m_ShownIndex = pNMLV->iItem;
274 m_cFileList.Invalidate();
279 void CFilePatchesDlg::OnNMCustomdrawFilelist(NMHDR *pNMHDR, LRESULT *pResult)
281 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
283 // Take the default processing unless we set this to something else below.
284 *pResult = CDRF_DODEFAULT;
286 // First thing - check the draw stage. If it's the control's prepaint
287 // stage, then tell Windows we want messages for every item.
289 if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
291 *pResult = CDRF_NOTIFYITEMDRAW;
293 else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
295 // This is the prepaint stage for an item. Here's where we set the
296 // item's text color. Our return value will tell Windows to draw the
297 // item itself, but it will use the new color we set here.
299 COLORREF crText = ::GetSysColor(COLOR_WINDOWTEXT);
301 if (m_arFileStates.GetCount() > (INT_PTR)pLVCD->nmcd.dwItemSpec)
303 if (m_arFileStates.GetAt(pLVCD->nmcd.dwItemSpec) == FPDLG_FILESTATE_CONFLICT)
305 crText = RGB(255, 200, 100); // orange
307 if (m_arFileStates.GetAt(pLVCD->nmcd.dwItemSpec)==FPDLG_FILESTATE_ERROR)
309 crText = RGB(200, 0, 0);
311 if (m_arFileStates.GetAt(pLVCD->nmcd.dwItemSpec)>0)
313 crText = RGB(100, 0, 0);
315 if (m_arFileStates.GetAt(pLVCD->nmcd.dwItemSpec)==FPDLG_FILESTATE_PATCHED)
317 crText = ::GetSysColor(COLOR_GRAYTEXT);
319 // Store the color back in the NMLVCUSTOMDRAW struct.
320 pLVCD->clrText = crText;
321 if (m_ShownIndex == (int)pLVCD->nmcd.dwItemSpec)
323 SelectObject(pLVCD->nmcd.hdc, m_boldFont);
324 // We changed the font, so we're returning CDRF_NEWFONT. This
325 // tells the control to recalculate the extent of the text.
326 *pResult = CDRF_NOTIFYSUBITEMDRAW | CDRF_NEWFONT;
332 void CFilePatchesDlg::OnNMRclickFilelist(NMHDR * /*pNMHDR*/, LRESULT *pResult)
334 *pResult = 0;
335 if (m_sPath.IsEmpty())
336 return;
337 CString temp;
338 CMenu popup;
339 POINT point;
340 DWORD ptW = GetMessagePos();
341 point.x = GET_X_LPARAM(ptW);
342 point.y = GET_Y_LPARAM(ptW);
343 if (!popup.CreatePopupMenu())
344 return;
346 UINT nFlags = MF_STRING | (m_cFileList.GetSelectedCount()==1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED);
347 temp.LoadString(IDS_PATCH_PREVIEW);
348 popup.AppendMenu(nFlags, ID_PATCHPREVIEW, temp);
349 popup.SetDefaultItem(ID_PATCHPREVIEW, FALSE);
351 temp.LoadString(IDS_PATCH_ALL);
352 popup.AppendMenu(MF_STRING | MF_ENABLED, ID_PATCHALL, temp);
354 nFlags = MF_STRING | (m_cFileList.GetSelectedCount() > 0 ? MF_ENABLED : MF_DISABLED | MF_GRAYED);
355 temp.LoadString(IDS_PATCH_SELECTED);
356 popup.AppendMenu(nFlags, ID_PATCHSELECTED, temp);
358 // if the context menu is invoked through the keyboard, we have to use
359 // a calculated position on where to anchor the menu on
360 if ((point.x == -1) && (point.y == -1))
362 CRect rect;
363 GetWindowRect(&rect);
364 point = rect.CenterPoint();
367 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON, point.x, point.y, this, 0);
368 switch (cmd)
370 case ID_PATCHPREVIEW:
371 if (m_pCallBack)
373 int nIndex = m_cFileList.GetSelectionMark();
374 if (m_arFileStates.GetAt(nIndex) == FPDLG_FILESTATE_ERROR)
376 MessageBox(m_pPatch->GetPatchRejects(nIndex), NULL, MB_ICONERROR);
378 else if ( m_arFileStates.GetAt(nIndex)!=FPDLG_FILESTATE_PATCHED)
380 m_pCallBack->PatchFile(m_pPatch->GetStrippedPath(nIndex), m_pPatch->GetContentMods(nIndex), m_pPatch->GetPropMods(nIndex), _T(""));
381 m_ShownIndex = nIndex;
382 m_cFileList.Invalidate();
385 break;
386 case ID_PATCHALL:
387 PatchAll();
388 break;
389 case ID_PATCHSELECTED:
390 PatchSelected();
391 break;
392 default:
393 break;
397 void CFilePatchesDlg::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
399 if (!m_bMinimized)
401 RECT windowrect;
402 RECT clientrect;
403 GetWindowRect(&windowrect);
404 GetClientRect(&clientrect);
405 m_nWindowHeight = windowrect.bottom - windowrect.top;
406 MoveWindow(windowrect.left, windowrect.top,
407 windowrect.right - windowrect.left,
408 m_nWindowHeight - (clientrect.bottom - clientrect.top));
410 else
412 RECT windowrect;
413 GetWindowRect(&windowrect);
414 MoveWindow(windowrect.left, windowrect.top, windowrect.right - windowrect.left, m_nWindowHeight);
416 m_bMinimized = !m_bMinimized;
417 CResizableStandAloneDialog::OnNcLButtonDblClk(nHitTest, point);
420 void CFilePatchesDlg::OnMoving(UINT fwSide, LPRECT pRect)
422 RECT parentRect;
423 m_pMainFrame->GetWindowRect(&parentRect);
424 const int stickySize = 5;
425 if (abs(parentRect.left - pRect->right) < stickySize)
427 int width = pRect->right - pRect->left;
428 pRect->right = parentRect.left;
429 pRect->left = pRect->right - width;
431 CResizableStandAloneDialog::OnMoving(fwSide, pRect);
434 void CFilePatchesDlg::OnOK()
436 return;
439 void CFilePatchesDlg::SetTitleWithPath(int width)
441 CString title;
442 title.LoadString(IDS_PATCH_TITLE);
443 title += _T(" ") + m_sPath;
444 title = title.Left(MAX_PATH-1);
445 CDC * pDC = GetDC();
446 if (pDC)
448 PathCompactPath(pDC->GetSafeHdc(), title.GetBuffer(), width);
449 ReleaseDC(pDC);
450 title.ReleaseBuffer();
452 SetWindowText(title);
455 void CFilePatchesDlg::OnBnClickedPatchselectedbutton()
457 PatchSelected();
460 void CFilePatchesDlg::OnBnClickedPatchallbutton()
462 PatchAll();
465 void CFilePatchesDlg::PatchAll()
467 if (m_pCallBack)
469 CSysProgressDlg progDlg;
470 progDlg.SetTitle(IDR_MAINFRAME);
471 progDlg.SetShowProgressBar(true);
472 progDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PATCH_ALL)));
473 progDlg.ShowModeless(m_hWnd);
474 for (int i=0; i<m_arFileStates.GetCount() && !progDlg.HasUserCancelled(); i++)
476 if (m_arFileStates.GetAt(i) == FPDLG_FILESTATE_ERROR)
477 MessageBox(m_pPatch->GetPatchRejects(i), NULL, MB_ICONERROR);
478 else if (m_arFileStates.GetAt(i) != FPDLG_FILESTATE_PATCHED)
480 progDlg.SetLine(2, GetFullPath(i), true);
481 m_pCallBack->PatchFile(m_pPatch->GetStrippedPath(i), m_pPatch->GetContentMods(i), m_pPatch->GetPropMods(i), _T(""), TRUE);
482 m_ShownIndex = i;
483 m_cFileList.Invalidate();
485 progDlg.SetProgress64(i, m_arFileStates.GetCount());
487 progDlg.Stop();
491 void CFilePatchesDlg::PatchSelected()
493 if (m_pCallBack)
495 CSysProgressDlg progDlg;
496 progDlg.SetTitle(IDR_MAINFRAME);
497 progDlg.SetShowProgressBar(true);
498 progDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PATCH_SELECTED)));
499 progDlg.ShowModeless(m_hWnd);
500 // The list cannot be sorted by user, so the order of the
501 // items in the list is identical to the order in the array
502 // m_arFileStates.
503 int selCount = m_cFileList.GetSelectedCount();
504 int count = 1;
505 POSITION pos = m_cFileList.GetFirstSelectedItemPosition();
506 int index;
507 while (((index = m_cFileList.GetNextSelectedItem(pos)) >= 0) && (!progDlg.HasUserCancelled()))
509 if (m_arFileStates.GetAt(index) == FPDLG_FILESTATE_ERROR)
510 MessageBox(m_pPatch->GetPatchRejects(index), NULL, MB_ICONERROR);
511 else if (m_arFileStates.GetAt(index) != FPDLG_FILESTATE_PATCHED)
513 progDlg.SetLine(2, GetFullPath(index), true);
514 m_pCallBack->PatchFile(m_pPatch->GetStrippedPath(index), m_pPatch->GetContentMods(index), m_pPatch->GetPropMods(index), _T(""), TRUE);
515 m_ShownIndex = index;
516 m_cFileList.Invalidate();
518 progDlg.SetProgress64(count++, selCount);
520 progDlg.Stop();
524 void CFilePatchesDlg::OnLvnItemchangedFilelist(NMHDR * /*pNMHDR*/, LRESULT *pResult)
526 DialogEnableWindow(IDC_PATCHSELECTEDBUTTON, m_cFileList.GetSelectedCount() > 0);
528 *pResult = 0;