Make sure buffer is large enough for the nul terminator
[TortoiseGit.git] / src / TortoiseProc / PatchListCtrl.cpp
blob8a0529f1bbab40c4c2aa1b4be1015c9b6ebdbf67
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015 - 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()
35 m_ContextMenuMask=0xFFFFFFFF;
37 HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
38 LOGFONT lf = {0};
39 GetObject(hFont, sizeof(LOGFONT), &lf);
40 lf.lfWeight = FW_BOLD;
41 m_boldFont = CreateFontIndirect(&lf);
45 CPatchListCtrl::~CPatchListCtrl()
47 if (m_boldFont)
48 DeleteObject(m_boldFont);
52 BEGIN_MESSAGE_MAP(CPatchListCtrl, CListCtrl)
53 ON_NOTIFY_REFLECT(NM_DBLCLK, &CPatchListCtrl::OnNMDblclk)
54 ON_WM_CONTEXTMENU()
55 ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CPatchListCtrl::OnNMCustomdraw)
56 ON_WM_DROPFILES()
57 END_MESSAGE_MAP()
61 // CPatchListCtrl message handlers
65 void CPatchListCtrl::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
67 LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
69 CString path=GetItemText(pNMItemActivate->iItem,0);
70 CTGitPath gitpath;
71 gitpath.SetFromWin(path);
73 CAppUtils::StartUnifiedDiffViewer(path,gitpath.GetFilename());
75 *pResult = 0;
78 void CPatchListCtrl::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
80 int selected=this->GetSelectedCount();
81 int index=0;
82 POSITION pos=this->GetFirstSelectedItemPosition();
83 index=this->GetNextSelectedItem(pos);
85 CIconMenu popup;
86 if (popup.CreatePopupMenu())
88 if(selected == 1)
90 if( m_ContextMenuMask&GetMenuMask(MENU_VIEWPATCH))
91 popup.AppendMenuIcon(MENU_VIEWPATCH, IDS_MENU_VIEWPATCH, 0);
93 if( m_ContextMenuMask&GetMenuMask(MENU_VIEWWITHMERGE))
94 popup.AppendMenuIcon(MENU_VIEWWITHMERGE, IDS_MENU_VIEWWITHMERGE, 0);
96 popup.SetDefaultItem(MENU_VIEWPATCH, FALSE);
98 if(selected >= 1)
100 if( m_ContextMenuMask&GetMenuMask(MENU_SENDMAIL))
101 popup.AppendMenuIcon(MENU_SENDMAIL, IDS_MENU_SENDMAIL, IDI_MENUSENDMAIL);
103 if( m_ContextMenuMask&GetMenuMask(MENU_APPLY))
104 popup.AppendMenuIcon(MENU_APPLY, IDS_MENU_APPLY, 0);
107 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY, point.x, point.y, this, 0);
109 switch (cmd)
111 case MENU_VIEWPATCH:
114 CString path=GetItemText(index,0);
115 CTGitPath gitpath;
116 gitpath.SetFromWin(path);
118 CAppUtils::StartUnifiedDiffViewer(path,gitpath.GetFilename());
119 break;
121 case MENU_VIEWWITHMERGE:
123 CString path=GetItemText(index,0);
124 CTGitPath gitpath;
125 gitpath.SetFromWin(path);
127 CTGitPath dir;
128 dir.SetFromGit(g_Git.m_CurrentDir);
130 CAppUtils::StartExtPatch(gitpath,dir);
131 break;
133 case MENU_SENDMAIL:
135 LaunchProc(_T("sendmail"));
136 break;
138 case MENU_APPLY:
140 LaunchProc(_T("importpatch"));
142 break;
144 default:
145 break;
150 int CPatchListCtrl::LaunchProc(const CString& command)
152 CString tempfile=GetTempFile();
153 POSITION pos=this->GetFirstSelectedItemPosition();
154 CFile file;
155 file.Open(tempfile,CFile::modeWrite|CFile::modeCreate);
157 while(pos)
159 int index = this->GetNextSelectedItem(pos);
160 CString one=this->GetItemText(index,0);
161 file.Write((LPCTSTR)one, sizeof(TCHAR) * one.GetLength());
162 file.Write(_T("\n"),sizeof(TCHAR)*1);
165 file.Close();
167 CString cmd = command;
168 cmd +=_T(" /pathfile:\"");
169 cmd += tempfile;
170 cmd += _T("\" /deletepathfile");
171 CAppUtils::RunTortoiseGitProc(cmd);
172 return 0;
175 void CPatchListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
177 NMLVCUSTOMDRAW *pNMCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
179 *pResult = 0;
182 switch (pNMCD->nmcd.dwDrawStage)
184 case CDDS_PREPAINT:
186 *pResult = CDRF_NOTIFYITEMDRAW;
187 return;
189 break;
190 case CDDS_ITEMPREPAINT:
192 // This is the prepaint stage for an item. Here's where we set the
193 // item's text color.
195 // Tell Windows to send draw notifications for each subitem.
196 *pResult = CDRF_NOTIFYSUBITEMDRAW;
198 DWORD_PTR data = this->GetItemData((int)pNMCD->nmcd.dwItemSpec);
199 if(data & (STATUS_APPLY_FAIL | STATUS_APPLY_SUCCESS | STATUS_APPLY_SKIP))
201 pNMCD->clrTextBk = RGB(200,200,200);
204 switch(data & STATUS_MASK)
206 case STATUS_APPLY_SUCCESS:
207 pNMCD->clrText = RGB(0,128,0);
208 break;
209 case STATUS_APPLY_FAIL:
210 pNMCD->clrText = RGB(255,0,0);
211 break;
212 case STATUS_APPLY_SKIP:
213 pNMCD->clrText = RGB(128,64,0);
214 break;
217 if(data & STATUS_APPLYING)
219 SelectObject(pNMCD->nmcd.hdc, m_boldFont);
220 *pResult = CDRF_NOTIFYSUBITEMDRAW | CDRF_NEWFONT;
224 break;
226 *pResult = CDRF_DODEFAULT;
229 void CPatchListCtrl::OnDropFiles(HDROP hDropInfo)
231 UINT nNumFiles = DragQueryFile(hDropInfo, 0xFFFFFFFF, nullptr, 0);
232 for (UINT i = 0; i < nNumFiles; ++i)
234 CString file;
235 DragQueryFile(hDropInfo, i, file.GetBufferSetLength(MAX_PATH), MAX_PATH);
236 file.ReleaseBuffer();
237 if (PathIsDirectory(file))
238 continue;
240 // no duplicates
241 LVFINDINFO lvInfo;
242 lvInfo.flags = LVFI_STRING;
243 lvInfo.psz = file;
244 if (FindItem(&lvInfo, -1) != -1)
245 continue;
247 int index = InsertItem(GetItemCount(), file);
248 if (index >= 0)
249 SetCheck(index, true);
251 DragFinish(hDropInfo);
252 SetColumnWidth(0, LVSCW_AUTOSIZE);