Fixed issue #4126: Capitalize the first letter in the Push dialog
[TortoiseGit.git] / src / TortoiseProc / PatchListCtrl.cpp
blobd4a61f700a2716c91949d736214d9652836016a1
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015-2016, 2018-2019, 2021-2023 - 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()
37 CPatchListCtrl::~CPatchListCtrl()
41 void CPatchListCtrl::PreSubclassWindow()
43 __super::PreSubclassWindow();
45 // use the default font, create a copy of it and
46 // change the copy to BOLD (leave the rest of the font
47 // the same)
48 LOGFONT lf = { 0 };
49 GetFont()->GetLogFont(&lf);
50 lf.lfWeight = FW_BOLD;
51 m_boldFont.CreateFontIndirect(&lf);
54 BEGIN_MESSAGE_MAP(CPatchListCtrl, CListCtrl)
55 ON_NOTIFY_REFLECT(NM_DBLCLK, &CPatchListCtrl::OnNMDblclk)
56 ON_WM_CONTEXTMENU()
57 ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CPatchListCtrl::OnNMCustomdraw)
58 ON_WM_DROPFILES()
59 END_MESSAGE_MAP()
63 // CPatchListCtrl message handlers
67 void CPatchListCtrl::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
69 LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
70 *pResult = 0;
72 CPoint point(pNMItemActivate->ptAction);
73 UINT uFlags = 0;
74 HitTest(point, &uFlags);
75 if (uFlags == LVHT_ONITEMSTATEICON)
76 return;
78 CString path=GetItemText(pNMItemActivate->iItem,0);
79 CTGitPath gitpath;
80 gitpath.SetFromWin(path);
82 CAppUtils::StartUnifiedDiffViewer(path, gitpath.GetFilename(), 0, !!(GetAsyncKeyState(VK_SHIFT) & 0x8000));
85 void CPatchListCtrl::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
87 int selected=this->GetSelectedCount();
88 POSITION pos=this->GetFirstSelectedItemPosition();
89 auto index = this->GetNextSelectedItem(pos);
91 CIconMenu popup;
92 if (popup.CreatePopupMenu())
94 if(selected == 1)
96 if( m_ContextMenuMask&GetMenuMask(MENU_VIEWPATCH))
97 popup.AppendMenuIcon(MENU_VIEWPATCH, IDS_MENU_VIEWPATCH, 0);
99 if( m_ContextMenuMask&GetMenuMask(MENU_VIEWWITHMERGE))
100 popup.AppendMenuIcon(MENU_VIEWWITHMERGE, IDS_MENU_VIEWWITHMERGE, 0);
102 popup.SetDefaultItem(MENU_VIEWPATCH, FALSE);
104 if(selected >= 1)
106 if( m_ContextMenuMask&GetMenuMask(MENU_SENDMAIL))
107 popup.AppendMenuIcon(MENU_SENDMAIL, IDS_MENU_SENDMAIL, IDI_MENUSENDMAIL);
109 if( m_ContextMenuMask&GetMenuMask(MENU_APPLY))
110 popup.AppendMenuIcon(MENU_APPLY, IDS_MENU_APPLY, 0);
113 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY, point.x, point.y, this);
115 switch (cmd)
117 case MENU_VIEWPATCH:
119 CString path=GetItemText(index,0);
120 CTGitPath gitpath;
121 gitpath.SetFromWin(path);
123 CAppUtils::StartUnifiedDiffViewer(path, gitpath.GetFilename(), 0, !!(GetAsyncKeyState(VK_SHIFT) & 0x8000));
124 break;
126 case MENU_VIEWWITHMERGE:
128 CString path=GetItemText(index,0);
129 CTGitPath gitpath;
130 gitpath.SetFromWin(path);
132 CTGitPath dir;
133 dir.SetFromGit(g_Git.m_CurrentDir);
135 CAppUtils::StartExtPatch(gitpath,dir);
136 break;
138 case MENU_SENDMAIL:
140 LaunchProc(L"sendmail");
141 break;
143 case MENU_APPLY:
145 LaunchProc(L"importpatch");
147 break;
149 default:
150 break;
155 int CPatchListCtrl::LaunchProc(const CString& command)
157 CString tempfile=GetTempFile();
158 if (tempfile.IsEmpty())
160 MessageBox(L"Could not create temp file.", L"TortoiseGit", MB_OK | MB_ICONERROR);
161 return -1;
164 CTGitPathList paths;
165 POSITION pos = GetFirstSelectedItemPosition();
166 while(pos)
168 int index = this->GetNextSelectedItem(pos);
169 paths.AddPath(GetItemText(index, 0));
171 if (!paths.WriteToFile(tempfile, false))
173 MessageBox(L"Could not write to temp file.", L"TortoiseGit", MB_OK | MB_ICONERROR);
174 return -1;
177 CString cmd = L"/command:";
178 cmd += command;
179 cmd += L" /pathfile:\"";
180 cmd += tempfile;
181 cmd += L"\" /deletepathfile";
182 if (!CAppUtils::RunTortoiseGitProc(cmd))
184 MessageBox(L"Could not start TortoiseGitProc.exe.", L"TortoiseGit", MB_ICONERROR);
185 return -1;
187 return 0;
190 void CPatchListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
192 NMLVCUSTOMDRAW *pNMCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
194 *pResult = 0;
196 switch (pNMCD->nmcd.dwDrawStage)
198 case CDDS_PREPAINT:
200 *pResult = CDRF_NOTIFYITEMDRAW;
201 return;
203 case CDDS_ITEMPREPAINT:
205 // This is the prepaint stage for an item. Here's where we set the
206 // item's text color.
208 // Tell Windows to send draw notifications for each subitem.
209 *pResult = CDRF_NOTIFYSUBITEMDRAW;
211 DWORD_PTR data = this->GetItemData(static_cast<int>(pNMCD->nmcd.dwItemSpec));
212 if(data & (STATUS_APPLY_FAIL | STATUS_APPLY_SUCCESS | STATUS_APPLY_SKIP))
213 pNMCD->clrTextBk = RGB(200,200,200);
215 switch(data & STATUS_MASK)
217 case STATUS_APPLY_SUCCESS:
218 pNMCD->clrText = RGB(0,128,0);
219 break;
220 case STATUS_APPLY_FAIL:
221 pNMCD->clrText = RGB(255,0,0);
222 break;
223 case STATUS_APPLY_SKIP:
224 pNMCD->clrText = RGB(128,64,0);
225 break;
228 if(data & STATUS_APPLYING)
230 SelectObject(pNMCD->nmcd.hdc, m_boldFont.GetSafeHandle());
231 *pResult = CDRF_NOTIFYSUBITEMDRAW | CDRF_NEWFONT;
235 break;
237 *pResult = CDRF_DODEFAULT;
240 void CPatchListCtrl::OnDropFiles(HDROP hDropInfo)
242 UINT nNumFiles = DragQueryFile(hDropInfo, 0xFFFFFFFF, nullptr, 0);
243 for (UINT i = 0; i < nNumFiles; ++i)
245 CString file;
246 DragQueryFile(hDropInfo, i, CStrBuf(file, MAX_PATH), MAX_PATH);
247 if (PathIsDirectory(file))
248 continue;
250 // no duplicates
251 LVFINDINFO lvInfo;
252 lvInfo.flags = LVFI_STRING;
253 lvInfo.psz = file;
254 if (FindItem(&lvInfo, -1) != -1)
255 continue;
257 int index = InsertItem(GetItemCount(), file);
258 if (index >= 0)
259 SetCheck(index, true);
261 DragFinish(hDropInfo);
262 SetColumnWidth(0, LVSCW_AUTOSIZE);