Update documentation
[TortoiseGit.git] / src / TortoiseProc / PatchListCtrl.cpp
blob34ba42916d617e9b04c6ae777806580d1bbc3828
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015-2016 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // PathListCtrl.cpp : implementation file
22 #include "stdafx.h"
23 #include "TortoiseProc.h"
24 #include "PatchListCtrl.h"
25 #include "IconMenu.h"
26 #include "AppUtils.h"
27 #include "Git.h"
28 #include "AppUtils.h"
29 // CPatchListCtrl
31 IMPLEMENT_DYNAMIC(CPatchListCtrl, CListCtrl)
33 CPatchListCtrl::CPatchListCtrl()
34 : m_ContextMenuMask(0xFFFFFFFF)
36 HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
37 LOGFONT lf = {0};
38 GetObject(hFont, sizeof(LOGFONT), &lf);
39 lf.lfWeight = FW_BOLD;
40 m_boldFont.CreateFontIndirect(&lf);
43 CPatchListCtrl::~CPatchListCtrl()
48 BEGIN_MESSAGE_MAP(CPatchListCtrl, CListCtrl)
49 ON_NOTIFY_REFLECT(NM_DBLCLK, &CPatchListCtrl::OnNMDblclk)
50 ON_WM_CONTEXTMENU()
51 ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CPatchListCtrl::OnNMCustomdraw)
52 ON_WM_DROPFILES()
53 END_MESSAGE_MAP()
57 // CPatchListCtrl message handlers
61 void CPatchListCtrl::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
63 LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
65 CString path=GetItemText(pNMItemActivate->iItem,0);
66 CTGitPath gitpath;
67 gitpath.SetFromWin(path);
69 CAppUtils::StartUnifiedDiffViewer(path, gitpath.GetFilename(), 0, !!(GetAsyncKeyState(VK_SHIFT) & 0x8000));
71 *pResult = 0;
74 void CPatchListCtrl::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
76 int selected=this->GetSelectedCount();
77 int index=0;
78 POSITION pos=this->GetFirstSelectedItemPosition();
79 index=this->GetNextSelectedItem(pos);
81 CIconMenu popup;
82 if (popup.CreatePopupMenu())
84 if(selected == 1)
86 if( m_ContextMenuMask&GetMenuMask(MENU_VIEWPATCH))
87 popup.AppendMenuIcon(MENU_VIEWPATCH, IDS_MENU_VIEWPATCH, 0);
89 if( m_ContextMenuMask&GetMenuMask(MENU_VIEWWITHMERGE))
90 popup.AppendMenuIcon(MENU_VIEWWITHMERGE, IDS_MENU_VIEWWITHMERGE, 0);
92 popup.SetDefaultItem(MENU_VIEWPATCH, FALSE);
94 if(selected >= 1)
96 if( m_ContextMenuMask&GetMenuMask(MENU_SENDMAIL))
97 popup.AppendMenuIcon(MENU_SENDMAIL, IDS_MENU_SENDMAIL, IDI_MENUSENDMAIL);
99 if( m_ContextMenuMask&GetMenuMask(MENU_APPLY))
100 popup.AppendMenuIcon(MENU_APPLY, IDS_MENU_APPLY, 0);
103 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY, point.x, point.y, this);
105 switch (cmd)
107 case MENU_VIEWPATCH:
109 CString path=GetItemText(index,0);
110 CTGitPath gitpath;
111 gitpath.SetFromWin(path);
113 CAppUtils::StartUnifiedDiffViewer(path, gitpath.GetFilename(), 0, !!(GetAsyncKeyState(VK_SHIFT) & 0x8000));
114 break;
116 case MENU_VIEWWITHMERGE:
118 CString path=GetItemText(index,0);
119 CTGitPath gitpath;
120 gitpath.SetFromWin(path);
122 CTGitPath dir;
123 dir.SetFromGit(g_Git.m_CurrentDir);
125 CAppUtils::StartExtPatch(gitpath,dir);
126 break;
128 case MENU_SENDMAIL:
130 LaunchProc(L"sendmail");
131 break;
133 case MENU_APPLY:
135 LaunchProc(L"importpatch");
137 break;
139 default:
140 break;
145 int CPatchListCtrl::LaunchProc(const CString& command)
147 CString tempfile=GetTempFile();
148 POSITION pos=this->GetFirstSelectedItemPosition();
149 CFile file;
150 file.Open(tempfile,CFile::modeWrite|CFile::modeCreate);
152 while(pos)
154 int index = this->GetNextSelectedItem(pos);
155 CString one=this->GetItemText(index,0);
156 file.Write((LPCTSTR)one, sizeof(TCHAR) * one.GetLength());
157 file.Write(L"\n", sizeof(TCHAR) * 1);
160 file.Close();
162 CString cmd = L"/command:";
163 cmd += command;
164 cmd += L" /pathfile:\"";
165 cmd += tempfile;
166 cmd += L"\" /deletepathfile";
167 CAppUtils::RunTortoiseGitProc(cmd);
168 return 0;
171 void CPatchListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
173 NMLVCUSTOMDRAW *pNMCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
175 *pResult = 0;
177 switch (pNMCD->nmcd.dwDrawStage)
179 case CDDS_PREPAINT:
181 *pResult = CDRF_NOTIFYITEMDRAW;
182 return;
184 break;
185 case CDDS_ITEMPREPAINT:
187 // This is the prepaint stage for an item. Here's where we set the
188 // item's text color.
190 // Tell Windows to send draw notifications for each subitem.
191 *pResult = CDRF_NOTIFYSUBITEMDRAW;
193 DWORD_PTR data = this->GetItemData((int)pNMCD->nmcd.dwItemSpec);
194 if(data & (STATUS_APPLY_FAIL | STATUS_APPLY_SUCCESS | STATUS_APPLY_SKIP))
195 pNMCD->clrTextBk = RGB(200,200,200);
197 switch(data & STATUS_MASK)
199 case STATUS_APPLY_SUCCESS:
200 pNMCD->clrText = RGB(0,128,0);
201 break;
202 case STATUS_APPLY_FAIL:
203 pNMCD->clrText = RGB(255,0,0);
204 break;
205 case STATUS_APPLY_SKIP:
206 pNMCD->clrText = RGB(128,64,0);
207 break;
210 if(data & STATUS_APPLYING)
212 SelectObject(pNMCD->nmcd.hdc, m_boldFont.GetSafeHandle());
213 *pResult = CDRF_NOTIFYSUBITEMDRAW | CDRF_NEWFONT;
217 break;
219 *pResult = CDRF_DODEFAULT;
222 void CPatchListCtrl::OnDropFiles(HDROP hDropInfo)
224 UINT nNumFiles = DragQueryFile(hDropInfo, 0xFFFFFFFF, nullptr, 0);
225 for (UINT i = 0; i < nNumFiles; ++i)
227 CString file;
228 DragQueryFile(hDropInfo, i, CStrBuf(file, MAX_PATH), MAX_PATH);
229 if (PathIsDirectory(file))
230 continue;
232 // no duplicates
233 LVFINDINFO lvInfo;
234 lvInfo.flags = LVFI_STRING;
235 lvInfo.psz = file;
236 if (FindItem(&lvInfo, -1) != -1)
237 continue;
239 int index = InsertItem(GetItemCount(), file);
240 if (index >= 0)
241 SetCheck(index, true);
243 DragFinish(hDropInfo);
244 SetColumnWidth(0, LVSCW_AUTOSIZE);