1 // TortoiseMerge - a Diff/Patch program
\r
3 // Copyright (C) 2006, 2008 - Stefan Kueng
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
20 #include "TortoiseMerge.h"
\r
21 #include "FilePatchesDlg.h"
\r
23 #include "AppUtils.h"
\r
24 #include "PathUtils.h"
\r
25 #include "ProgressDlg.h"
\r
28 IMPLEMENT_DYNAMIC(CFilePatchesDlg, CDialog)
\r
29 CFilePatchesDlg::CFilePatchesDlg(CWnd* pParent /*=NULL*/)
\r
30 : CDialog(CFilePatchesDlg::IDD, pParent)
\r
32 m_ImgList.Create(16, 16, ILC_COLOR16 | ILC_MASK, 4, 1);
\r
33 m_bMinimized = FALSE;
\r
36 CFilePatchesDlg::~CFilePatchesDlg()
\r
40 void CFilePatchesDlg::DoDataExchange(CDataExchange* pDX)
\r
42 CDialog::DoDataExchange(pDX);
\r
43 DDX_Control(pDX, IDC_FILELIST, m_cFileList);
\r
46 BOOL CFilePatchesDlg::SetFileStatusAsPatched(CString sPath)
\r
48 for (int i=0; i<m_arFileStates.GetCount(); i++)
\r
50 if (sPath.CompareNoCase(GetFullPath(i))==0)
\r
52 m_arFileStates.SetAt(i, FPDLG_FILESTATE_PATCHED);
\r
60 CString CFilePatchesDlg::GetFullPath(int nIndex)
\r
62 CString temp = m_pPatch->GetFilename(nIndex);
\r
63 temp.Replace('/', '\\');
\r
64 //temp = temp.Mid(temp.Find('\\')+1);
\r
65 if (PathIsRelative(temp))
\r
66 temp = m_sPath + temp;
\r
70 BOOL CFilePatchesDlg::Init(CPatch * pPatch, CPatchFilesDlgCallBack * pCallBack, CString sPath, CWnd * pParent)
\r
72 if ((pCallBack==NULL)||(pPatch==NULL))
\r
74 m_cFileList.DeleteAllItems();
\r
77 m_arFileStates.RemoveAll();
\r
79 m_pCallBack = pCallBack;
\r
81 if (m_sPath.IsEmpty())
\r
83 CString title(MAKEINTRESOURCE(IDS_DIFF_TITLE));
\r
84 SetWindowText(title);
\r
89 title.LoadString(IDS_PATCH_TITLE);
\r
90 title += _T(" ") + m_sPath;
\r
92 GetClientRect(&rect);
\r
93 PathCompactPath(GetDC()->m_hDC, title.GetBuffer(), rect.Width());
\r
94 title.ReleaseBuffer();
\r
95 SetWindowText(title);
\r
96 if (m_sPath.Right(1).Compare(_T("\\"))==0)
\r
97 m_sPath = m_sPath.Left(m_sPath.GetLength()-1);
\r
99 m_sPath = m_sPath + _T("\\");
\r
100 for (int i=m_ImgList.GetImageCount();i>0;i--)
\r
102 m_ImgList.Remove(0);
\r
106 m_cFileList.SetExtendedStyle(LVS_EX_INFOTIP | LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER);
\r
107 m_cFileList.DeleteAllItems();
\r
108 int c = ((CHeaderCtrl*)(m_cFileList.GetDlgItem(0)))->GetItemCount()-1;
\r
110 m_cFileList.DeleteColumn(c--);
\r
111 m_cFileList.InsertColumn(0, _T(""));
\r
113 m_cFileList.SetRedraw(false);
\r
115 for(int i=0; i<m_pPatch->GetNumberOfFiles(); i++)
\r
117 CString sFile = CPathUtils::GetFileNameFromPath(m_pPatch->GetFilename(i));
\r
119 if (m_sPath.IsEmpty())
\r
120 state = FPDLG_FILESTATE_GOOD;
\r
123 if (m_pPatch->PatchFile(GetFullPath(i)))
\r
124 state = FPDLG_FILESTATE_GOOD;
\r
126 state = FPDLG_FILESTATE_CONFLICTED;
\r
128 m_arFileStates.Add(state);
\r
132 FILE_ATTRIBUTE_NORMAL,
\r
134 sizeof(SHFILEINFO),
\r
135 SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
\r
136 m_cFileList.InsertItem(i, sFile, m_ImgList.Add(sfi.hIcon));
\r
140 int maxcol = ((CHeaderCtrl*)(m_cFileList.GetDlgItem(0)))->GetItemCount()-1;
\r
142 for (col = mincol; col <= maxcol; col++)
\r
144 m_cFileList.SetColumnWidth(col,LVSCW_AUTOSIZE_USEHEADER);
\r
147 m_cFileList.SetImageList(&m_ImgList, LVSIL_SMALL);
\r
148 m_cFileList.SetRedraw(true);
\r
151 pParent->GetWindowRect(&parentrect);
\r
153 GetWindowRect(&windowrect);
\r
155 int width = windowrect.right - windowrect.left;
\r
156 int height = windowrect.bottom - windowrect.top;
\r
157 windowrect.right = parentrect.left;
\r
158 windowrect.left = windowrect.right - width;
\r
159 if (windowrect.left < 0)
\r
161 windowrect.left = 0;
\r
162 windowrect.right = width;
\r
164 windowrect.top = parentrect.top;
\r
165 windowrect.bottom = windowrect.top + height;
\r
167 SetWindowPos(NULL, windowrect.left, windowrect.top, width, height, SWP_NOACTIVATE | SWP_NOZORDER);
\r
169 m_nWindowHeight = windowrect.bottom - windowrect.top;
\r
170 m_pMainFrame = pParent;
\r
174 BEGIN_MESSAGE_MAP(CFilePatchesDlg, CDialog)
\r
176 ON_NOTIFY(LVN_GETINFOTIP, IDC_FILELIST, OnLvnGetInfoTipFilelist)
\r
177 ON_NOTIFY(NM_DBLCLK, IDC_FILELIST, OnNMDblclkFilelist)
\r
178 ON_NOTIFY(NM_CUSTOMDRAW, IDC_FILELIST, OnNMCustomdrawFilelist)
\r
179 ON_NOTIFY(NM_RCLICK, IDC_FILELIST, OnNMRclickFilelist)
\r
180 ON_WM_NCLBUTTONDBLCLK()
\r
184 void CFilePatchesDlg::OnSize(UINT nType, int cx, int cy)
\r
186 CDialog::OnSize(nType, cx, cy);
\r
187 if (this->IsWindowVisible())
\r
190 GetClientRect(rect);
\r
191 GetDlgItem(IDC_FILELIST)->MoveWindow(rect.left, rect.top, cx, cy);
\r
192 m_cFileList.SetColumnWidth(0, cx);
\r
195 title.LoadString(IDS_PATCH_TITLE);
\r
196 title += _T(" ") + m_sPath;
\r
197 PathCompactPath(GetDC()->m_hDC, title.GetBuffer(), cx);
\r
198 title.ReleaseBuffer();
\r
199 SetWindowText(title);
\r
202 void CFilePatchesDlg::OnLvnGetInfoTipFilelist(NMHDR *pNMHDR, LRESULT *pResult)
\r
204 LPNMLVGETINFOTIP pGetInfoTip = reinterpret_cast<LPNMLVGETINFOTIP>(pNMHDR);
\r
206 CString temp = GetFullPath(pGetInfoTip->iItem);
\r
207 _tcsncpy_s(pGetInfoTip->pszText, pGetInfoTip->cchTextMax, temp, pGetInfoTip->cchTextMax);
\r
211 void CFilePatchesDlg::OnNMDblclkFilelist(NMHDR *pNMHDR, LRESULT *pResult)
\r
213 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
\r
215 if ((pNMLV->iItem < 0) || (pNMLV->iItem >= m_arFileStates.GetCount()))
\r
217 if (m_pCallBack==NULL)
\r
219 if (m_sPath.IsEmpty())
\r
221 m_pCallBack->DiffFiles(GetFullPath(pNMLV->iItem), m_pPatch->GetRevision(pNMLV->iItem),
\r
222 m_pPatch->GetFilename2(pNMLV->iItem), m_pPatch->GetRevision2(pNMLV->iItem));
\r
226 if (m_arFileStates.GetAt(pNMLV->iItem)!=FPDLG_FILESTATE_PATCHED)
\r
228 m_pCallBack->PatchFile(GetFullPath(pNMLV->iItem), m_pPatch->GetRevision(pNMLV->iItem));
\r
233 void CFilePatchesDlg::OnNMCustomdrawFilelist(NMHDR *pNMHDR, LRESULT *pResult)
\r
235 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
\r
237 // Take the default processing unless we set this to something else below.
\r
238 *pResult = CDRF_DODEFAULT;
\r
240 // First thing - check the draw stage. If it's the control's prepaint
\r
241 // stage, then tell Windows we want messages for every item.
\r
243 if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
\r
245 *pResult = CDRF_NOTIFYITEMDRAW;
\r
247 else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
\r
249 // This is the prepaint stage for an item. Here's where we set the
\r
250 // item's text color. Our return value will tell Windows to draw the
\r
251 // item itself, but it will use the new color we set here.
\r
253 COLORREF crText = ::GetSysColor(COLOR_WINDOWTEXT);
\r
255 if (m_arFileStates.GetCount() > (INT_PTR)pLVCD->nmcd.dwItemSpec)
\r
257 if (m_arFileStates.GetAt(pLVCD->nmcd.dwItemSpec)==FPDLG_FILESTATE_CONFLICTED)
\r
259 crText = RGB(200, 0, 0);
\r
261 if (m_arFileStates.GetAt(pLVCD->nmcd.dwItemSpec)==FPDLG_FILESTATE_PATCHED)
\r
263 crText = ::GetSysColor(COLOR_GRAYTEXT);
\r
265 // Store the color back in the NMLVCUSTOMDRAW struct.
\r
266 pLVCD->clrText = crText;
\r
269 // Tell Windows to paint the control itself.
\r
270 *pResult = CDRF_DODEFAULT;
\r
274 void CFilePatchesDlg::OnNMRclickFilelist(NMHDR * /*pNMHDR*/, LRESULT *pResult)
\r
277 if (m_sPath.IsEmpty())
\r
282 DWORD ptW = GetMessagePos();
\r
283 point.x = GET_X_LPARAM(ptW);
\r
284 point.y = GET_Y_LPARAM(ptW);
\r
285 if (popup.CreatePopupMenu())
\r
289 nFlags = MF_STRING | (m_cFileList.GetSelectedCount()==1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED);
\r
290 temp.LoadString(IDS_PATCH_PREVIEW);
\r
291 popup.AppendMenu(nFlags, ID_PATCHPREVIEW, temp);
\r
292 popup.SetDefaultItem(ID_PATCHPREVIEW, FALSE);
\r
294 temp.LoadString(IDS_PATCH_ALL);
\r
295 popup.AppendMenu(MF_STRING | MF_ENABLED, ID_PATCHALL, temp);
\r
297 nFlags = MF_STRING | (m_cFileList.GetSelectedCount()>0 ? MF_ENABLED : MF_DISABLED | MF_GRAYED);
\r
298 temp.LoadString(IDS_PATCH_SELECTED);
\r
299 popup.AppendMenu(nFlags, ID_PATCHSELECTED, temp);
\r
301 // if the context menu is invoked through the keyboard, we have to use
\r
302 // a calculated position on where to anchor the menu on
\r
303 if ((point.x == -1) && (point.y == -1))
\r
306 GetWindowRect(&rect);
\r
307 point = rect.CenterPoint();
\r
310 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY, point.x, point.y, this, 0);
\r
313 case ID_PATCHPREVIEW:
\r
317 int nIndex = m_cFileList.GetSelectionMark();
\r
318 if ( m_arFileStates.GetAt(nIndex)!=FPDLG_FILESTATE_PATCHED)
\r
320 m_pCallBack->PatchFile(GetFullPath(nIndex), m_pPatch->GetRevision(nIndex));
\r
329 CProgressDlg progDlg;
\r
330 progDlg.SetTitle(IDR_MAINFRAME);
\r
331 progDlg.SetShowProgressBar(true);
\r
332 progDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PATCH_ALL)));
\r
333 progDlg.ShowModeless(m_hWnd);
\r
335 for (int i=0; i<m_arFileStates.GetCount() && !progDlg.HasUserCancelled(); i++)
\r
337 if (m_arFileStates.GetAt(i)!= FPDLG_FILESTATE_PATCHED)
\r
339 progDlg.SetLine(2, GetFullPath(i), true);
\r
340 m_pCallBack->PatchFile(GetFullPath(i), m_pPatch->GetRevision(i), TRUE);
\r
342 progDlg.SetProgress64(i, m_arFileStates.GetCount());
\r
348 case ID_PATCHSELECTED:
\r
352 CProgressDlg progDlg;
\r
353 progDlg.SetTitle(IDR_MAINFRAME);
\r
354 progDlg.SetShowProgressBar(true);
\r
355 progDlg.SetLine(1, CString(MAKEINTRESOURCE(IDS_PATCH_SELECTED)));
\r
356 progDlg.ShowModeless(m_hWnd);
\r
358 // The list cannot be sorted by user, so the order of the
\r
359 // items in the list is identical to the order in the array
\r
361 int selCount = m_cFileList.GetSelectedCount();
\r
363 POSITION pos = m_cFileList.GetFirstSelectedItemPosition();
\r
365 while (((index = m_cFileList.GetNextSelectedItem(pos)) >= 0) && (!progDlg.HasUserCancelled()))
\r
367 if (m_arFileStates.GetAt(index)!= FPDLG_FILESTATE_PATCHED)
\r
369 progDlg.SetLine(2, GetFullPath(index), true);
\r
370 m_pCallBack->PatchFile(GetFullPath(index), m_pPatch->GetRevision(index), TRUE);
\r
372 progDlg.SetProgress64(count++, selCount);
\r
384 void CFilePatchesDlg::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
\r
390 GetWindowRect(&windowrect);
\r
391 GetClientRect(&clientrect);
\r
392 m_nWindowHeight = windowrect.bottom - windowrect.top;
\r
393 MoveWindow(windowrect.left, windowrect.top,
\r
394 windowrect.right - windowrect.left,
\r
395 m_nWindowHeight - (clientrect.bottom - clientrect.top));
\r
400 GetWindowRect(&windowrect);
\r
401 MoveWindow(windowrect.left, windowrect.top, windowrect.right - windowrect.left, m_nWindowHeight);
\r
403 m_bMinimized = !m_bMinimized;
\r
404 CDialog::OnNcLButtonDblClk(nHitTest, point);
\r
407 void CFilePatchesDlg::OnMoving(UINT fwSide, LPRECT pRect)
\r
409 #define STICKYSIZE 5
\r
411 m_pMainFrame->GetWindowRect(&parentRect);
\r
412 if (abs(parentRect.left - pRect->right) < STICKYSIZE)
\r
414 int width = pRect->right - pRect->left;
\r
415 pRect->right = parentRect.left;
\r
416 pRect->left = pRect->right - width;
\r
418 CDialog::OnMoving(fwSide, pRect);
\r
421 void CFilePatchesDlg::OnOK()
\r