Prevent the recycle bin from even getting monitored
[TortoiseGit.git] / src / Utils / CommonAppUtils.cpp
blobd9565e8f7d41beccd5eae7d80784d3fe82756481
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.
20 #include "StdAfx.h"
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"
28 extern CString sOrigCWD;
30 bool CCommonAppUtils::LaunchApplication(const CString& sCommandLine, UINT idErrMessageFormat, bool bWaitForStartup, CString *cwd)
32 STARTUPINFO startup;
33 PROCESS_INFORMATION process;
34 memset(&startup, 0, sizeof(startup));
35 startup.cb = sizeof(startup);
36 memset(&process, 0, sizeof(process));
38 CString cleanCommandLine(sCommandLine);
40 CString theCWD = sOrigCWD;
41 if (cwd != NULL)
42 theCWD = *cwd;
44 if (CreateProcess(NULL, const_cast<TCHAR*>((LPCTSTR)cleanCommandLine), NULL, NULL, FALSE, 0, 0, theCWD, &startup, &process)==0)
46 if(idErrMessageFormat != 0)
48 CString temp;
49 temp.Format(idErrMessageFormat, CFormatMessageWrapper());
50 MessageBox(NULL, temp, _T("TortoiseGit"), MB_OK | MB_ICONINFORMATION);
52 return false;
55 if (bWaitForStartup)
57 WaitForInputIdle(process.hProcess, 10000);
60 CloseHandle(process.hThread);
61 CloseHandle(process.hProcess);
62 return true;
65 bool CCommonAppUtils::RunTortoiseProc(const CString& sCommandLine)
67 CString pathToExecutable = CPathUtils::GetAppDirectory() + _T("TortoiseProc.exe");
68 CString sCmd;
69 sCmd.Format(_T("\"%s\" %s"), (LPCTSTR)pathToExecutable, (LPCTSTR)sCommandLine);
70 if (AfxGetMainWnd()->GetSafeHwnd() && (sCommandLine.Find(L"/hwnd:") < 0))
72 CString sCmdLine;
73 sCmdLine.Format(L"%s /hwnd:%ld", (LPCTSTR)sCommandLine, AfxGetMainWnd()->GetSafeHwnd());
74 sCmd.Format(_T("\"%s\" %s"), (LPCTSTR)pathToExecutable, (LPCTSTR)sCmdLine);
77 return LaunchApplication(sCmd, NULL, false);
80 bool CCommonAppUtils::SetListCtrlBackgroundImage(HWND hListCtrl, UINT nID, int width /* = 128 */, int height /* = 128 */)
82 ListView_SetTextBkColor(hListCtrl, CLR_NONE);
83 COLORREF bkColor = ListView_GetBkColor(hListCtrl);
84 // create a bitmap from the icon
85 HICON hIcon = (HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(nID), IMAGE_ICON, width, height, LR_DEFAULTCOLOR);
86 if (!hIcon)
87 return false;
89 RECT rect = {0};
90 rect.right = width;
91 rect.bottom = height;
92 HBITMAP bmp = NULL;
94 HWND desktop = ::GetDesktopWindow();
95 if (desktop)
97 HDC screen_dev = ::GetDC(desktop);
98 if (screen_dev)
100 // Create a compatible DC
101 HDC dst_hdc = ::CreateCompatibleDC(screen_dev);
102 if (dst_hdc)
104 // Create a new bitmap of icon size
105 bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);
106 if (bmp)
108 // Select it into the compatible DC
109 HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);
110 // Fill the background of the compatible DC with the given color
111 ::SetBkColor(dst_hdc, bkColor);
112 ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
114 // Draw the icon into the compatible DC
115 ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);
116 ::SelectObject(dst_hdc, old_dst_bmp);
118 ::DeleteDC(dst_hdc);
121 ::ReleaseDC(desktop, screen_dev);
124 // Restore settings
125 DestroyIcon(hIcon);
127 if (bmp == NULL)
128 return false;
130 LVBKIMAGE lv;
131 lv.ulFlags = LVBKIF_TYPE_WATERMARK;
132 lv.hbm = bmp;
133 lv.xOffsetPercent = 100;
134 lv.yOffsetPercent = 100;
135 ListView_SetBkImage(hListCtrl, &lv);
136 return true;
139 bool CCommonAppUtils::FileOpenSave(CString& path, int * filterindex, UINT title, UINT filter, bool bOpen, HWND hwndOwner)
141 OPENFILENAME ofn = {0}; // common dialog box structure
142 TCHAR szFile[MAX_PATH] = {0}; // buffer for file name. Explorer can't handle paths longer than MAX_PATH.
143 ofn.lStructSize = sizeof(OPENFILENAME);
144 ofn.hwndOwner = hwndOwner;
145 _tcscpy_s(szFile, MAX_PATH, (LPCTSTR)path);
146 ofn.lpstrFile = szFile;
147 ofn.nMaxFile = _countof(szFile);
148 CString sFilter;
149 TCHAR * pszFilters = NULL;
150 if (filter)
152 sFilter.LoadString(filter);
153 pszFilters = new TCHAR[sFilter.GetLength()+4];
154 _tcscpy_s (pszFilters, sFilter.GetLength()+4, sFilter);
155 // Replace '|' delimiters with '\0's
156 TCHAR *ptr = pszFilters + _tcslen(pszFilters); //set ptr at the NULL
157 while (ptr != pszFilters)
159 if (*ptr == '|')
160 *ptr = '\0';
161 ptr--;
163 ofn.lpstrFilter = pszFilters;
165 ofn.nFilterIndex = 1;
166 ofn.lpstrFileTitle = NULL;
167 ofn.nMaxFileTitle = 0;
168 ofn.lpstrInitialDir = NULL;
169 CString temp;
170 if (title)
172 temp.LoadString(title);
173 CStringUtils::RemoveAccelerators(temp);
175 ofn.lpstrTitle = temp;
176 if (bOpen)
177 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER;
178 else
179 ofn.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER;
182 // Display the Open dialog box.
183 bool bRet = false;
184 if (bOpen)
186 bRet = !!GetOpenFileName(&ofn);
188 else
190 bRet = !!GetSaveFileName(&ofn);
192 SetCurrentDirectory(sOrigCWD.GetBuffer());
193 sOrigCWD.ReleaseBuffer();
194 if (bRet)
196 if (pszFilters)
197 delete [] pszFilters;
198 path = CString(ofn.lpstrFile);
199 if (filterindex)
200 *filterindex = ofn.nFilterIndex;
201 return true;
203 if (pszFilters)
204 delete [] pszFilters;
205 return false;