1
// TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - TortoiseGit
4 // Copyright (C) 2003-2008,2010 - TortoiseSVN
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.
21 #include "..\Resources\LoglistCommonResource.h"
22 #include "CommonAppUtils.h"
23 #include "PathUtils.h"
24 #include "StringUtils.h"
25 #include "CreateProcessHelper.h"
26 #include "FormatMessageWrapper.h"
29 extern CString sOrigCWD
;
31 bool CCommonAppUtils::LaunchApplication(const CString
& sCommandLine
, UINT idErrMessageFormat
, bool bWaitForStartup
, CString
*cwd
, bool uac
)
33 CString theCWD
= sOrigCWD
;
40 SHELLEXECUTEINFO shellinfo
;
41 memset(&shellinfo
, 0, sizeof(shellinfo
));
42 shellinfo
.cbSize
= sizeof(shellinfo
);
43 shellinfo
.hwnd
= NULL
;
44 shellinfo
.lpVerb
= _T("runas");
45 shellinfo
.nShow
= SW_SHOWNORMAL
;
46 shellinfo
.fMask
= SEE_MASK_NOCLOSEPROCESS
;
47 shellinfo
.lpDirectory
= theCWD
;
49 int pos
= sCommandLine
.Find('"');
52 pos
= sCommandLine
.Find('"', 2);
55 file
= sCommandLine
.Mid(1, pos
- 1);
56 param
= sCommandLine
.Mid(pos
+ 1);
60 if (idErrMessageFormat
!= 0)
63 temp
.Format(idErrMessageFormat
, CFormatMessageWrapper());
64 MessageBox(NULL
, temp
, _T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
71 pos
= sCommandLine
.Find(' ', 1);
74 file
= sCommandLine
.Mid(0, pos
);
75 param
= sCommandLine
.Mid(pos
+ 1);
81 shellinfo
.lpFile
= file
;
82 shellinfo
.lpParameters
= param
;
84 if (!ShellExecuteEx(&shellinfo
))
86 if (idErrMessageFormat
!= 0)
89 temp
.Format(idErrMessageFormat
, CFormatMessageWrapper());
90 MessageBox(NULL
, temp
, _T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
97 WaitForInputIdle(shellinfo
.hProcess
, 10000);
100 CloseHandle(shellinfo
.hProcess
);
105 PROCESS_INFORMATION process
;
106 memset(&startup
, 0, sizeof(startup
));
107 startup
.cb
= sizeof(startup
);
108 memset(&process
, 0, sizeof(process
));
110 CString
cleanCommandLine(sCommandLine
);
112 if (CreateProcess(NULL
, const_cast<TCHAR
*>((LPCTSTR
)cleanCommandLine
), NULL
, NULL
, FALSE
, 0, 0, theCWD
, &startup
, &process
)==0)
114 if(idErrMessageFormat
!= 0)
117 temp
.Format(idErrMessageFormat
, CFormatMessageWrapper());
118 MessageBox(NULL
, temp
, _T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
125 WaitForInputIdle(process
.hProcess
, 10000);
128 CloseHandle(process
.hThread
);
129 CloseHandle(process
.hProcess
);
134 bool CCommonAppUtils::RunTortoiseGitProc(const CString
& sCommandLine
)
136 CString pathToExecutable
= CPathUtils::GetAppDirectory() + _T("TortoiseGitProc.exe");
138 sCmd
.Format(_T("\"%s\" %s"), (LPCTSTR
)pathToExecutable
, (LPCTSTR
)sCommandLine
);
139 if (AfxGetMainWnd()->GetSafeHwnd() && (sCommandLine
.Find(L
"/hwnd:") < 0))
142 sCmdLine
.Format(L
"%s /hwnd:%ld", (LPCTSTR
)sCommandLine
, AfxGetMainWnd()->GetSafeHwnd());
143 sCmd
.Format(_T("\"%s\" %s"), (LPCTSTR
)pathToExecutable
, (LPCTSTR
)sCmdLine
);
146 return LaunchApplication(sCmd
, NULL
, false);
149 bool CCommonAppUtils::SetListCtrlBackgroundImage(HWND hListCtrl
, UINT nID
, int width
/* = 128 */, int height
/* = 128 */)
151 if ((((DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\ShowListBackgroundImage"), TRUE
)) == FALSE
))
153 ListView_SetTextBkColor(hListCtrl
, CLR_NONE
);
154 COLORREF bkColor
= ListView_GetBkColor(hListCtrl
);
155 // create a bitmap from the icon
156 HICON hIcon
= (HICON
)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(nID
), IMAGE_ICON
, width
, height
, LR_DEFAULTCOLOR
);
162 rect
.bottom
= height
;
165 HWND desktop
= ::GetDesktopWindow();
168 HDC screen_dev
= ::GetDC(desktop
);
171 // Create a compatible DC
172 HDC dst_hdc
= ::CreateCompatibleDC(screen_dev
);
175 // Create a new bitmap of icon size
176 bmp
= ::CreateCompatibleBitmap(screen_dev
, rect
.right
, rect
.bottom
);
179 // Select it into the compatible DC
180 HBITMAP old_dst_bmp
= (HBITMAP
)::SelectObject(dst_hdc
, bmp
);
181 // Fill the background of the compatible DC with the given color
182 ::SetBkColor(dst_hdc
, bkColor
);
183 ::ExtTextOut(dst_hdc
, 0, 0, ETO_OPAQUE
, &rect
, NULL
, 0, NULL
);
185 // Draw the icon into the compatible DC
186 ::DrawIconEx(dst_hdc
, 0, 0, hIcon
, rect
.right
, rect
.bottom
, 0, NULL
, DI_NORMAL
);
187 ::SelectObject(dst_hdc
, old_dst_bmp
);
192 ::ReleaseDC(desktop
, screen_dev
);
202 lv
.ulFlags
= LVBKIF_TYPE_WATERMARK
;
204 lv
.xOffsetPercent
= 100;
205 lv
.yOffsetPercent
= 100;
206 ListView_SetBkImage(hListCtrl
, &lv
);
210 bool CCommonAppUtils::FileOpenSave(CString
& path
, int * filterindex
, UINT title
, UINT filter
, bool bOpen
, HWND hwndOwner
)
212 OPENFILENAME ofn
= {0}; // common dialog box structure
213 TCHAR szFile
[MAX_PATH
] = {0}; // buffer for file name. Explorer can't handle paths longer than MAX_PATH.
214 ofn
.lStructSize
= sizeof(OPENFILENAME
);
215 ofn
.hwndOwner
= hwndOwner
;
216 _tcscpy_s(szFile
, MAX_PATH
, (LPCTSTR
)path
);
217 ofn
.lpstrFile
= szFile
;
218 ofn
.nMaxFile
= _countof(szFile
);
220 TCHAR
* pszFilters
= NULL
;
223 sFilter
.LoadString(filter
);
224 pszFilters
= new TCHAR
[sFilter
.GetLength()+4];
225 _tcscpy_s (pszFilters
, sFilter
.GetLength()+4, sFilter
);
226 // Replace '|' delimiters with '\0's
227 TCHAR
*ptr
= pszFilters
+ _tcslen(pszFilters
); //set ptr at the NULL
228 while (ptr
!= pszFilters
)
234 ofn
.lpstrFilter
= pszFilters
;
236 ofn
.nFilterIndex
= 1;
237 ofn
.lpstrFileTitle
= NULL
;
238 ofn
.nMaxFileTitle
= 0;
239 ofn
.lpstrInitialDir
= NULL
;
243 temp
.LoadString(title
);
244 CStringUtils::RemoveAccelerators(temp
);
246 ofn
.lpstrTitle
= temp
;
248 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
| OFN_EXPLORER
;
250 ofn
.Flags
= OFN_OVERWRITEPROMPT
| OFN_EXPLORER
;
253 // Display the Open dialog box.
257 bRet
= !!GetOpenFileName(&ofn
);
261 bRet
= !!GetSaveFileName(&ofn
);
263 SetCurrentDirectory(sOrigCWD
.GetBuffer());
264 sOrigCWD
.ReleaseBuffer();
268 delete [] pszFilters
;
269 path
= CString(ofn
.lpstrFile
);
271 *filterindex
= ofn
.nFilterIndex
;
275 delete [] pszFilters
;
279 void CCommonAppUtils::SetCharFormat(CWnd
* window
, DWORD mask
, DWORD effects
, const std::vector
<CHARRANGE
>& positions
)
282 SecureZeroMemory(&format
, sizeof(CHARFORMAT2
));
283 format
.cbSize
= sizeof(CHARFORMAT2
);
284 format
.dwMask
= mask
;
285 format
.dwEffects
= effects
;
286 format
.crTextColor
= effects
;
288 for (std::vector
<CHARRANGE
>::const_iterator iter
= positions
.begin(), end
= positions
.end(); iter
!= end
; ++iter
)
290 CHARRANGE range
= *iter
;
291 window
->SendMessage(EM_EXSETSEL
, NULL
, (LPARAM
)&range
);
292 window
->SendMessage(EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&format
);
296 void CCommonAppUtils::SetCharFormat(CWnd
* window
, DWORD mask
, DWORD effects
)
299 SecureZeroMemory(&format
, sizeof(CHARFORMAT2
));
300 format
.cbSize
= sizeof(CHARFORMAT2
);
301 format
.dwMask
= mask
;
302 format
.dwEffects
= effects
;
303 window
->SendMessage(EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&format
);