Fix compilation warnings
[TortoiseGit.git] / src / TortoiseMerge / FilePatchesDlg.cpp
blob32e8cb10a45c05e6f44679886baab059e3b1f6db
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 (state > 0)
153 state = FPDLG_FILESTATE_ERROR;
154 m_arFileStates.Add(state);
155 SHFILEINFO sfi;
156 SHGetFileInfo(
157 GetFullPath(i),
158 FILE_ATTRIBUTE_NORMAL,
159 &sfi,
160 sizeof(SHFILEINFO),
161 SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
162 m_cFileList.InsertItem(i, sFile, m_ImgList.Add(sfi.hIcon));
165 int mincol = 0;
166 int maxcol = ((CHeaderCtrl*)(m_cFileList.GetDlgItem(0)))->GetItemCount()-1;
167 int col;
168 for (col = mincol; col <= maxcol; col++)
170 m_cFileList.SetColumnWidth(col,LVSCW_AUTOSIZE_USEHEADER);
173 m_cFileList.SetImageList(&m_ImgList, LVSIL_SMALL);
174 m_cFileList.SetRedraw(true);
176 RECT parentrect;
177 pParent->GetWindowRect(&parentrect);
178 RECT windowrect;
179 GetWindowRect(&windowrect);
181 int width = windowrect.right - windowrect.left;
182 int height = windowrect.bottom - windowrect.top;
183 windowrect.right = parentrect.left;
184 windowrect.left = windowrect.right - width;
185 if (windowrect.left < 0)
187 windowrect.left = 0;
188 windowrect.right = width;
190 windowrect.top = parentrect.top;
191 windowrect.bottom = windowrect.top + height;
193 SetWindowPos(NULL, windowrect.left, windowrect.top, width, height, SWP_NOACTIVATE | SWP_NOZORDER);
195 m_nWindowHeight = windowrect.bottom - windowrect.top;
196 m_pMainFrame = pParent;
197 return TRUE;
200 BEGIN_MESSAGE_MAP(CFilePatchesDlg, CResizableStandAloneDialog)
201 ON_WM_SIZE()
202 ON_NOTIFY(LVN_GETINFOTIP, IDC_FILELIST, OnLvnGetInfoTipFilelist)
203 ON_NOTIFY(NM_DBLCLK, IDC_FILELIST, OnNMDblclkFilelist)
204 ON_NOTIFY(NM_CUSTOMDRAW, IDC_FILELIST, OnNMCustomdrawFilelist)
205 ON_NOTIFY(NM_RCLICK, IDC_FILELIST, OnNMRclickFilelist)
206 ON_WM_NCLBUTTONDBLCLK()
207 ON_WM_MOVING()
208 ON_BN_CLICKED(IDC_PATCHSELECTEDBUTTON, &CFilePatchesDlg::OnBnClickedPatchselectedbutton)
209 ON_BN_CLICKED(IDC_PATCHALLBUTTON, &CFilePatchesDlg::OnBnClickedPatchallbutton)
210 ON_NOTIFY(LVN_ITEMCHANGED, IDC_FILELIST, &CFilePatchesDlg::OnLvnItemchangedFilelist)
211 END_MESSAGE_MAP()
213 void CFilePatchesDlg::OnSize(UINT nType, int cx, int cy)
215 CResizableStandAloneDialog::OnSize(nType, cx, cy);
216 if (this->IsWindowVisible())
218 m_cFileList.SetColumnWidth(0, LVSCW_AUTOSIZE);
220 SetTitleWithPath(cx);
223 void CFilePatchesDlg::OnLvnGetInfoTipFilelist(NMHDR *pNMHDR, LRESULT *pResult)
225 LPNMLVGETINFOTIP pGetInfoTip = reinterpret_cast<LPNMLVGETINFOTIP>(pNMHDR);
227 if (m_arFileStates.GetCount() > pGetInfoTip->iItem)
229 CString temp;
230 if (m_arFileStates.GetAt(pGetInfoTip->iItem) == 0)
231 temp = GetFullPath(pGetInfoTip->iItem);
232 else
233 temp.Format(IDS_PATCH_ITEMTT, (LPCTSTR)GetFullPath(pGetInfoTip->iItem), m_arFileStates.GetAt(pGetInfoTip->iItem));
234 _tcsncpy_s(pGetInfoTip->pszText, pGetInfoTip->cchTextMax, temp, pGetInfoTip->cchTextMax);
236 else
237 pGetInfoTip->pszText[0] = 0;
239 *pResult = 0;
242 void CFilePatchesDlg::OnNMDblclkFilelist(NMHDR *pNMHDR, LRESULT *pResult)
244 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
245 *pResult = 0;
246 if ((pNMLV->iItem < 0) || (pNMLV->iItem >= m_arFileStates.GetCount()))
247 return;
248 if (m_pCallBack==NULL)
249 return;
251 if (m_arFileStates.GetAt(pNMLV->iItem) == FPDLG_FILESTATE_ERROR)
253 MessageBox(m_pPatch->GetPatchRejects(pNMLV->iItem), NULL, MB_ICONERROR);
254 return;
257 if (m_sPath.IsEmpty())
259 m_pCallBack->DiffFiles(GetFullPath(pNMLV->iItem), _T(""),
260 _T(""), _T(""));
261 m_ShownIndex = pNMLV->iItem;
262 m_cFileList.Invalidate();
264 else
266 if (m_arFileStates.GetAt(pNMLV->iItem)!=FPDLG_FILESTATE_PATCHED)
268 m_pCallBack->PatchFile(m_pPatch->GetStrippedPath(pNMLV->iItem), m_pPatch->GetContentMods(pNMLV->iItem), m_pPatch->GetPropMods(pNMLV->iItem), _T(""));
269 m_ShownIndex = pNMLV->iItem;
270 m_cFileList.Invalidate();
275 void CFilePatchesDlg::OnNMCustomdrawFilelist(NMHDR *pNMHDR, LRESULT *pResult)
277 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
279 // Take the default processing unless we set this to something else below.
280 *pResult = CDRF_DODEFAULT;
282 // First thing - check the draw stage. If it's the control's prepaint
283 // stage, then tell Windows we want messages for every item.
285 if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
287 *pResult = CDRF_NOTIFYITEMDRAW;
289 else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
291 // This is the prepaint stage for an item. Here's where we set the
292 // item's text color. Our return value will tell Windows to draw the
293 // item itself, but it will use the new color we set here.
295 COLORREF crText = ::GetSysColor(COLOR_WINDOWTEXT);
297 if (m_arFileStates.GetCount() > (INT_PTR)pLVCD->nmcd.dwItemSpec)
299 if (m_arFileStates.GetAt(pLVCD->nmcd.dwItemSpec)==FPDLG_FILESTATE_ERROR)
301 crText = RGB(200, 0, 0);
303 if (m_arFileStates.GetAt(pLVCD->nmcd.dwItemSpec)>0)
305 crText = RGB(100, 0, 0);
307 if (m_arFileStates.GetAt(pLVCD->nmcd.dwItemSpec)==FPDLG_FILESTATE_PATCHED)
309 crText = ::GetSysColor(COLOR_GRAYTEXT);
311 // Store the color back in the NMLVCUSTOMDRAW struct.
312 pLVCD->clrText = crText;
313 if (m_ShownIndex == (int)pLVCD->nmcd.dwItemSpec)
315 SelectObject(pLVCD->nmcd.hdc, m_boldFont);
316 // We changed the font, so we're returning CDRF_NEWFONT. This
317 // tells the control to recalculate the extent of the text.
318 *pResult = CDRF_NOTIFYSUBITEMDRAW | CDRF_NEWFONT;
324 void CFilePatchesDlg::OnNMRclickFilelist(NMHDR * /*pNMHDR*/, LRESULT *pResult)
326 *pResult = 0;
327 if (m_sPath.IsEmpty())
328 return;
329 CString temp;
330 CMenu popup;
331 POINT point;
332 DWORD ptW = GetMessagePos();
333 point.x = GET_X_LPARAM(ptW);
334 point.y = GET_Y_LPARAM(ptW);
335 if (!popup.CreatePopupMenu())
336 return;
338 UINT nFlags = MF_STRING | (m_cFileList.GetSelectedCount()==1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED);
339 temp.LoadString(IDS_PATCH_PREVIEW);
340 popup.AppendMenu(nFlags, ID_PATCHPREVIEW, temp);
341 popup.SetDefaultItem(ID_PATCHPREVIEW, FALSE);
343 temp.LoadString(IDS_PATCH_ALL);
344 popup.AppendMenu(MF_STRING | MF_ENABLED, ID_PATCHALL, temp);
346 nFlags = MF_STRING | (m_cFileList.GetSelectedCount() > 0 ? MF_ENABLED : MF_DISABLED | MF_GRAYED);
347 temp.LoadString(IDS_PATCH_SELECTED);
348 popup.AppendMenu(nFlags, ID_PATCHSELECTED, temp);
350 // if the context menu is invoked through the keyboard, we have to use
351 // a calculated position on where to anchor the menu on
352 if ((point.x == -1) && (point.y == -1))
354 CRect rect;
355 GetWindowRect(&rect);
356 point = rect.CenterPoint();
359 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON, point.x, point.y, this, 0);
360 switch (cmd)
362 case ID_PATCHPREVIEW:
363 if (m_pCallBack)
365 int nIndex = m_cFileList.GetSelectionMark();
366 if (m_arFileStates.GetAt(nIndex) == FPDLG_FILESTATE_ERROR)
368 MessageBox(m_pPatch->GetPatchRejects(nIndex), NULL, MB_ICONERROR);
370 else if ( m_arFileStates.GetAt(nIndex)!=FPDLG_FILESTATE_PATCHED)
372 m_pCallBack->PatchFile(m_pPatch->GetStrippedPath(nIndex), m_pPatch->GetContentMods(nIndex), m_pPatch->GetPropMods(nIndex), _T(""));
373 m_ShownIndex = nIndex;
374 m_cFileList.Invalidate();
377 break;
378 case ID_PATCHALL:
379 PatchAll();
380 break;
381 case ID_PATCHSELECTED:
382 PatchSelected();
383 break;
384 default:
385 break;
389 void CFilePatchesDlg::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
391 if (!m_bMinimized)
393 RECT windowrect;
394 RECT clientrect;
395 GetWindowRect(&windowrect);
396 GetClientRect(&clientrect);
397 m_nWindowHeight = windowrect.bottom - windowrect.top;
398 MoveWindow(windowrect.left, windowrect.top,
399 windowrect.right - windowrect.left,
400 m_nWindowHeight - (clientrect.bottom - clientrect.top));
402 else
404 RECT windowrect;
405 GetWindowRect(&windowrect);
406 MoveWindow(windowrect.left, windowrect.top, windowrect.right - windowrect.left, m_nWindowHeight);
408 m_bMinimized = !m_bMinimized;
409 CResizableStandAloneDialog::OnNcLButtonDblClk(nHitTest, point);
412 void CFilePatchesDlg::OnMoving(UINT fwSide, LPRECT pRect)
414 RECT parentRect;
415 m_pMainFrame->GetWindowRect(&parentRect);
416 const int stickySize = 5;
417 if (abs(parentRect.left - pRect->right) < stickySize)
419 int width = pRect->right - pRect->left;
420 pRect->right = parentRect.left;
421 pRect->left = pRect->right - width;
423 CResizableStandAloneDialog::OnMoving(fwSide, pRect);
426 void CFilePatchesDlg::OnOK()
428 return;
431 void CFilePatchesDlg::SetTitleWithPath(int width)
433 CString title;
434 title.LoadString(IDS_PATCH_TITLE);
435 title += _T(" ") + m_sPath;
436 title = title.Left(MAX_PATH-1);
437 CDC * pDC = GetDC();
438 if (pDC)
440 PathCompactPath(pDC->GetSafeHdc(), title.GetBuffer(), width);
441 ReleaseDC(pDC);
443 title.ReleaseBuffer();
444 SetWindowText(title);
447 void CFilePatchesDlg::OnBnClickedPatchselectedbutton()
449 PatchSelected();
452 void CFilePatchesDlg::OnBnClickedPatchallbutton()
454 PatchAll();
457 void CFilePatchesDlg::PatchAll()
459 if (m_pCallBack)
461 CSysProgressDlg progDlg;
462 progDlg.SetTitle(IDR_MAINFRAME);
463 progDlg.SetShowProgressBar(true);
464 progDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PATCH_ALL)));
465 progDlg.ShowModeless(m_hWnd);
466 for (int i=0; i<m_arFileStates.GetCount() && !progDlg.HasUserCancelled(); i++)
468 if (m_arFileStates.GetAt(i) == FPDLG_FILESTATE_ERROR)
469 MessageBox(m_pPatch->GetPatchRejects(i), NULL, MB_ICONERROR);
470 else if (m_arFileStates.GetAt(i) != FPDLG_FILESTATE_PATCHED)
472 progDlg.SetLine(2, GetFullPath(i), true);
473 m_pCallBack->PatchFile(m_pPatch->GetStrippedPath(i), m_pPatch->GetContentMods(i), m_pPatch->GetPropMods(i), _T(""), TRUE);
474 m_ShownIndex = i;
475 m_cFileList.Invalidate();
477 progDlg.SetProgress64(i, m_arFileStates.GetCount());
479 progDlg.Stop();
483 void CFilePatchesDlg::PatchSelected()
485 if (m_pCallBack)
487 CSysProgressDlg progDlg;
488 progDlg.SetTitle(IDR_MAINFRAME);
489 progDlg.SetShowProgressBar(true);
490 progDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PATCH_SELECTED)));
491 progDlg.ShowModeless(m_hWnd);
492 // The list cannot be sorted by user, so the order of the
493 // items in the list is identical to the order in the array
494 // m_arFileStates.
495 int selCount = m_cFileList.GetSelectedCount();
496 int count = 1;
497 POSITION pos = m_cFileList.GetFirstSelectedItemPosition();
498 int index;
499 while (((index = m_cFileList.GetNextSelectedItem(pos)) >= 0) && (!progDlg.HasUserCancelled()))
501 if (m_arFileStates.GetAt(index) == FPDLG_FILESTATE_ERROR)
502 MessageBox(m_pPatch->GetPatchRejects(index), NULL, MB_ICONERROR);
503 else if (m_arFileStates.GetAt(index) != FPDLG_FILESTATE_PATCHED)
505 progDlg.SetLine(2, GetFullPath(index), true);
506 m_pCallBack->PatchFile(m_pPatch->GetStrippedPath(index), m_pPatch->GetContentMods(index), m_pPatch->GetPropMods(index), _T(""), TRUE);
507 m_ShownIndex = index;
508 m_cFileList.Invalidate();
510 progDlg.SetProgress64(count++, selCount);
512 progDlg.Stop();
516 void CFilePatchesDlg::OnLvnItemchangedFilelist(NMHDR * /*pNMHDR*/, LRESULT *pResult)
518 DialogEnableWindow(IDC_PATCHSELECTEDBUTTON, m_cFileList.GetSelectedCount() > 0);
520 *pResult = 0;