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"
28 extern CString sOrigCWD
;
30 bool CCommonAppUtils::LaunchApplication(const CString
& sCommandLine
, UINT idErrMessageFormat
, bool bWaitForStartup
, CString
*cwd
)
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
;
44 if (CreateProcess(NULL
, const_cast<TCHAR
*>((LPCTSTR
)cleanCommandLine
), NULL
, NULL
, FALSE
, 0, 0, theCWD
, &startup
, &process
)==0)
46 if(idErrMessageFormat
!= 0)
49 temp
.Format(idErrMessageFormat
, CFormatMessageWrapper());
50 MessageBox(NULL
, temp
, _T("TortoiseGit"), MB_OK
| MB_ICONINFORMATION
);
57 WaitForInputIdle(process
.hProcess
, 10000);
60 CloseHandle(process
.hThread
);
61 CloseHandle(process
.hProcess
);
65 bool CCommonAppUtils::RunTortoiseProc(const CString
& sCommandLine
)
67 CString pathToExecutable
= CPathUtils::GetAppDirectory() + _T("TortoiseProc.exe");
69 sCmd
.Format(_T("\"%s\" %s"), (LPCTSTR
)pathToExecutable
, (LPCTSTR
)sCommandLine
);
70 if (AfxGetMainWnd()->GetSafeHwnd() && (sCommandLine
.Find(L
"/hwnd:") < 0))
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
);
94 HWND desktop
= ::GetDesktopWindow();
97 HDC screen_dev
= ::GetDC(desktop
);
100 // Create a compatible DC
101 HDC dst_hdc
= ::CreateCompatibleDC(screen_dev
);
104 // Create a new bitmap of icon size
105 bmp
= ::CreateCompatibleBitmap(screen_dev
, rect
.right
, rect
.bottom
);
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
);
121 ::ReleaseDC(desktop
, screen_dev
);
131 lv
.ulFlags
= LVBKIF_TYPE_WATERMARK
;
133 lv
.xOffsetPercent
= 100;
134 lv
.yOffsetPercent
= 100;
135 ListView_SetBkImage(hListCtrl
, &lv
);
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
);
149 TCHAR
* pszFilters
= NULL
;
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
)
163 ofn
.lpstrFilter
= pszFilters
;
165 ofn
.nFilterIndex
= 1;
166 ofn
.lpstrFileTitle
= NULL
;
167 ofn
.nMaxFileTitle
= 0;
168 ofn
.lpstrInitialDir
= NULL
;
172 temp
.LoadString(title
);
173 CStringUtils::RemoveAccelerators(temp
);
175 ofn
.lpstrTitle
= temp
;
177 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
| OFN_EXPLORER
;
179 ofn
.Flags
= OFN_OVERWRITEPROMPT
| OFN_EXPLORER
;
182 // Display the Open dialog box.
186 bRet
= !!GetOpenFileName(&ofn
);
190 bRet
= !!GetSaveFileName(&ofn
);
192 SetCurrentDirectory(sOrigCWD
.GetBuffer());
193 sOrigCWD
.ReleaseBuffer();
197 delete [] pszFilters
;
198 path
= CString(ofn
.lpstrFile
);
200 *filterindex
= ofn
.nFilterIndex
;
204 delete [] pszFilters
;