Fix TGitCache RunTime 4->1 check fail problem. Add submodule status at shell extension
[TortoiseGit.git] / src / TortoiseShell / ContextMenu.cpp
blob8a9668618c569c545b4e9f0f82409b7022cdea11
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - 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 #include "stdafx.h"
20 #include "ShellExt.h"
21 #include "ItemIDList.h"
22 #include "PreserveChdir.h"
23 #include "UnicodeUtils.h"
24 //#include "GitProperties.h"
25 #include "GitStatus.h"
26 #include "TGitPath.h"
28 #define GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
29 #define GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
31 int g_shellidlist=RegisterClipboardFormat(CFSTR_SHELLIDLIST);
33 CShellExt::MenuInfo CShellExt::menuInfo[] =
35 { ShellMenuClone, MENUCLONE, IDI_CLONE, IDS_MENUCLONE, IDS_MENUDESCCHECKOUT,
36 ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },
38 { ShellMenuPull, MENUPULL, IDI_PULL, IDS_MENUPULL, IDS_MENUPULL,
39 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
41 { ShellMenuFetch, MENUFETCH, IDI_PULL, IDS_MENUFETCH, IDS_MENUFETCH,
42 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
44 { ShellMenuPush, MENUPUSH, IDI_PUSH, IDS_MENUPUSH, IDS_MENUPULL,
45 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
47 // { ShellMenuCheckout, MENUCHECKOUT, IDI_CHECKOUT, IDS_MENUCHECKOUT, IDS_MENUDESCCHECKOUT,
48 // ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },
50 // { ShellMenuUpdate, MENUSUBUPDATE, IDI_UPDATE, IDS_MENUUPDATE, IDS_MENUDESCUPDATE,
51 // ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
54 { ShellMenuCommit, MENUCOMMIT, IDI_COMMIT, IDS_MENUCOMMIT, IDS_MENUDESCCOMMIT,
55 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
57 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
59 { ShellMenuDiff, MENUDIFF, IDI_DIFF, IDS_MENUDIFF, IDS_MENUDESCDIFF,
60 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_FOLDER|ITEMIS_NORMAL, ITEMIS_TWO, 0, 0, 0, 0, 0 },
62 { ShellMenuPrevDiff, MENUPREVDIFF, IDI_DIFF, IDS_MENUPREVDIFF, IDS_MENUDESCPREVDIFF,
63 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },
65 // { ShellMenuUrlDiff, MENUURLDIFF, IDI_DIFF, IDS_MENUURLDIFF, IDS_MENUDESCURLDIFF,
66 // ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, 0, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
68 { ShellMenuLog, MENULOG, IDI_LOG, IDS_MENULOG, IDS_MENUDESCLOG,
69 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, 0, 0 },
71 { ShellMenuRefLog, MENUREFLOG, IDI_LOG, IDS_MENUREFLOG, IDS_MENUDESCREFLOG,
72 ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, ITEMIS_ADDED, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, ITEMIS_ADDED, 0, 0 },
74 // { ShellMenuRepoBrowse, MENUREPOBROWSE, IDI_REPOBROWSE, IDS_MENUREPOBROWSE, IDS_MENUDESCREPOBROWSE,
75 // ITEMIS_ONLYONE, 0, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
77 { ShellMenuShowChanged, MENUSHOWCHANGED, IDI_SHOWCHANGED, IDS_MENUSHOWCHANGED, IDS_MENUDESCSHOWCHANGED,
78 ITEMIS_INSVN|ITEMIS_ONLYONE, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0},
80 { ShellMenuRebase, MENUREBASE, IDI_REBASE, IDS_MENUREBASE, IDS_MENUREBASE,
81 ITEMIS_INSVN|ITEMIS_ONLYONE, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0},
83 // { ShellMenuRevisionGraph, MENUREVISIONGRAPH, IDI_REVISIONGRAPH, IDS_MENUREVISIONGRAPH, IDS_MENUDESCREVISIONGRAPH,
84 // ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, 0, 0, 0, 0},
86 { ShellMenuStashSave, MENUSTASHSAVE, IDI_COMMIT, IDS_MENUSTASHSAVE, IDS_MENUSTASHSAVE,
87 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
88 { ShellMenuStashApply, MENUSTASHAPPLY, IDI_RELOCATE, IDS_MENUSTASHAPPLY, IDS_MENUSTASHAPPLY,
89 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
90 { ShellMenuStashList, MENUSTASHAPPLY, IDI_LOG, IDS_MENUSTASHLIST, IDS_MENUSTASHLIST,
91 ITEMIS_INSVN|ITEMIS_EXTENDED, 0, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
94 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
96 { ShellMenuConflictEditor, MENUCONFLICTEDITOR, IDI_CONFLICT, IDS_MENUCONFLICT, IDS_MENUDESCCONFLICT,
97 ITEMIS_INSVN|ITEMIS_CONFLICTED, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },
99 { ShellMenuResolve, MENURESOLVE, IDI_RESOLVE, IDS_MENURESOLVE, IDS_MENUDESCRESOLVE,
100 ITEMIS_INSVN|ITEMIS_CONFLICTED, 0, ITEMIS_INSVN|ITEMIS_FOLDER, 0, ITEMIS_FOLDERINSVN, 0, 0, 0 },
103 { ShellMenuRename, MENURENAME, IDI_RENAME, IDS_MENURENAME, IDS_MENUDESCRENAME,
104 ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_INVERSIONEDFOLDER, 0, 0, 0, 0, 0, 0, 0 },
106 { ShellMenuRemove, MENUREMOVE, IDI_DELETE, IDS_MENUREMOVE, IDS_MENUDESCREMOVE,
107 ITEMIS_INSVN|ITEMIS_INVERSIONEDFOLDER, ITEMIS_ADDED, 0, 0, 0, 0, 0, 0 },
109 { ShellMenuRemoveKeep, MENUREMOVE, IDI_DELETE, IDS_MENUREMOVEKEEP, IDS_MENUDESCREMOVEKEEP,
110 ITEMIS_INSVN|ITEMIS_INVERSIONEDFOLDER|ITEMIS_EXTENDED, ITEMIS_ADDED, 0, 0, 0, 0, 0, 0 },
112 { ShellMenuRevert, MENUREVERT, IDI_REVERT, IDS_MENUREVERT, IDS_MENUDESCREVERT,
113 ITEMIS_INSVN, ITEMIS_NORMAL|ITEMIS_ADDED, ITEMIS_FOLDERINSVN, ITEMIS_ADDED, 0, 0, 0, 0 },
115 { ShellMenuRevert, MENUREVERT, IDI_REVERT, IDS_MENUUNDOADD, IDS_MENUDESCUNDOADD,
116 ITEMIS_ADDED, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN|ITEMIS_ADDED, 0, 0, 0, 0, 0 },
118 { ShellMenuDelUnversioned, MENUDELUNVERSIONED, IDI_DELUNVERSIONED, IDS_MENUDELUNVERSIONED, IDS_MENUDESCDELUNVERSIONED,
119 ITEMIS_FOLDER|ITEMIS_INSVN|ITEMIS_EXTENDED, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
121 { ShellMenuCleanup, MENUCLEANUP, IDI_CLEANUP, IDS_MENUCLEANUP, IDS_MENUDESCCLEANUP,
122 ITEMIS_INSVN|ITEMIS_FOLDER, 0, ITEMIS_FOLDERINSVN|ITEMIS_FOLDER, 0, 0, 0, 0, 0 },
124 // { ShellMenuLock, MENULOCK, IDI_LOCK, IDS_MENU_LOCK, IDS_MENUDESC_LOCK,
125 // ITEMIS_INSVN, ITEMIS_LOCKED|ITEMIS_ADDED, ITEMIS_FOLDERINSVN, ITEMIS_LOCKED|ITEMIS_ADDED, 0, 0, 0, 0 },
127 // { ShellMenuUnlock, MENUUNLOCK, IDI_UNLOCK, IDS_MENU_UNLOCK, IDS_MENUDESC_UNLOCK,
128 // ITEMIS_INSVN|ITEMIS_LOCKED, 0, ITEMIS_FOLDER|ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0 },
130 // { ShellMenuUnlockForce, MENUUNLOCK, IDI_UNLOCK, IDS_MENU_UNLOCKFORCE, IDS_MENUDESC_UNLOCKFORCE,
131 // ITEMIS_INSVN|ITEMIS_LOCKED, 0, ITEMIS_FOLDER|ITEMIS_INSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
133 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
135 // { ShellMenuCopy, MENUCOPY, IDI_COPY, IDS_MENUBRANCH, IDS_MENUDESCCOPY,
136 // ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
138 { ShellMenuSwitch, MENUSWITCH, IDI_SWITCH, IDS_MENUSWITCH, IDS_MENUDESCSWITCH,
139 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
141 { ShellMenuMerge, MENUMERGE, IDI_MERGE, IDS_MENUMERGE, IDS_MENUDESCMERGE,
142 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
143 // { ShellMenuMergeAll, MENUMERGEALL, IDI_MERGE, IDS_MENUMERGEALL, IDS_MENUDESCMERGEALL,
144 // ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
146 { ShellMenuBranch, MENUCOPY, IDI_COPY, IDS_MENUBRANCH, IDS_MENUDESCCOPY,
147 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
148 { ShellMenuTag, MENUTAG, IDI_TAG, IDS_MENUTAG, IDS_MENUDESCCOPY,
149 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
151 { ShellMenuExport, MENUEXPORT, IDI_EXPORT, IDS_MENUEXPORT, IDS_MENUDESCEXPORT,
152 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
154 // { ShellMenuRelocate, MENURELOCATE, IDI_RELOCATE, IDS_MENURELOCATE, IDS_MENUDESCRELOCATE,
155 // ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
157 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
159 { ShellMenuCreateRepos, MENUCREATEREPOS, IDI_CREATEREPOS, IDS_MENUCREATEREPOS, IDS_MENUDESCCREATEREPOS,
160 ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },
162 { ShellMenuAdd, MENUADD, IDI_ADD, IDS_MENUADD, IDS_MENUDESCADD,
163 ITEMIS_INVERSIONEDFOLDER, ITEMIS_INSVN, ITEMIS_INSVN|ITEMIS_FOLDER, 0, ITEMIS_IGNORED, 0, ITEMIS_DELETED, ITEMIS_FOLDER|ITEMIS_ONLYONE },
165 // { ShellMenuAddAsReplacement, MENUADD, IDI_ADD, IDS_MENUADDASREPLACEMENT, IDS_MENUADDASREPLACEMENT,
166 // ITEMIS_DELETED|ITEMIS_ONLYONE, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },
168 // { ShellMenuImport, MENUIMPORT, IDI_IMPORT, IDS_MENUIMPORT, IDS_MENUDESCIMPORT,
169 // ITEMIS_FOLDER, ITEMIS_INSVN, 0, 0, 0, 0, 0, 0 },
171 { ShellMenuBlame, MENUBLAME, IDI_BLAME, IDS_MENUBLAME, IDS_MENUDESCBLAME,
172 ITEMIS_NORMAL|ITEMIS_ONLYONE, ITEMIS_FOLDER|ITEMIS_ADDED, 0, 0, 0, 0, 0, 0 },
173 // TODO: original code is ITEMIS_INSVN|ITEMIS_ONLYONE, makes sense to only allow blaming of versioned files
174 // why was this changed, is this related to GitStatus?
176 { ShellMenuIgnoreSub, MENUIGNORE, IDI_IGNORE, IDS_MENUIGNORE, IDS_MENUDESCIGNORE,
177 ITEMIS_INVERSIONEDFOLDER, ITEMIS_IGNORED|ITEMIS_INSVN, 0, 0, 0, 0, 0, 0 },
179 { ShellMenuUnIgnoreSub, MENUIGNORE, IDI_IGNORE, IDS_MENUUNIGNORE, IDS_MENUDESCUNIGNORE,
180 ITEMIS_IGNORED, 0, 0, 0, 0, 0, 0, 0 },
182 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
184 { ShellMenuSubAdd, MENUSUBADD, IDI_ADD, IDS_MENUSUBADD, IDS_MENUSUBADD,
185 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
187 { ShellMenuUpdateExt, MENUUPDATEEXT, IDI_UPDATE, IDS_MENUUPDATEEXT, IDS_MENUDESCUPDATEEXT,
188 ITEMIS_FOLDERINSVN|ITEMIS_SUBMODULE, 0, 0, 0, 0, 0, 0, 0 },
191 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
193 // { ShellMenuCherryPick, MENUCHERRYPICK, IDI_CREATEPATCH, IDS_MENUCHERRYPICK, IDS_MENUDESCCREATEPATCH,
194 // ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
196 { ShellMenuFormatPatch, MENUFORMATPATCH, IDI_CREATEPATCH, IDS_MENUFORMATPATCH, IDS_MENUDESCCREATEPATCH,
197 ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
199 { ShellMenuImportPatch, MENUIMPORTPATCH, IDI_PATCH, IDS_MENUIMPORTPATCH, IDS_MENUDESCCREATEPATCH,
200 ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
203 { ShellMenuCreatePatch, MENUCREATEPATCH, IDI_CREATEPATCH, IDS_MENUCREATEPATCH, IDS_MENUDESCCREATEPATCH,
204 ITEMIS_INSVN|ITEMIS_EXTENDED, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
206 { ShellMenuApplyPatch, MENUAPPLYPATCH, IDI_PATCH, IDS_MENUAPPLYPATCH, IDS_MENUDESCAPPLYPATCH,
207 ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, ITEMIS_ADDED, ITEMIS_ONLYONE|ITEMIS_PATCHFILE|ITEMIS_EXTENDED, 0, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, ITEMIS_ADDED, 0, 0 },
209 // { ShellMenuProperties, MENUPROPERTIES, IDI_PROPERTIES, IDS_MENUPROPERTIES, IDS_MENUDESCPROPERTIES,
210 // ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
212 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
213 // { ShellMenuClipPaste, MENUCLIPPASTE, IDI_CLIPPASTE, IDS_MENUCLIPPASTE, IDS_MENUDESCCLIPPASTE,
214 // ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_PATHINCLIPBOARD, 0, 0, 0, 0, 0, 0, 0 },
216 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
218 { ShellMenuSettings, MENUSETTINGS, IDI_SETTINGS, IDS_MENUSETTINGS, IDS_MENUDESCSETTINGS,
219 ITEMIS_FOLDER, 0, 0, ITEMIS_FOLDER, 0, 0, 0, 0 },
220 { ShellMenuHelp, MENUHELP, IDI_HELP, IDS_MENUHELP, IDS_MENUDESCHELP,
221 ITEMIS_FOLDER, 0, 0, ITEMIS_FOLDER, 0, 0, 0, 0 },
222 { ShellMenuAbout, MENUABOUT, IDI_ABOUT, IDS_MENUABOUT, IDS_MENUDESCABOUT,
223 ITEMIS_FOLDER, 0, 0, ITEMIS_FOLDER, 0, 0, 0, 0 },
225 // the sub menus - they're not added like the the commands, therefore the menu ID is zero
226 // but they still need to be in here, because we use the icon and string information anyway.
227 { ShellSubMenu, NULL, IDI_APP, IDS_MENUSUBMENU, 0,
228 0, 0, 0, 0, 0, 0, 0, 0 },
229 { ShellSubMenuFile, NULL, IDI_MENUFILE, IDS_MENUSUBMENU, 0,
230 0, 0, 0, 0, 0, 0, 0, 0 },
231 { ShellSubMenuFolder, NULL, IDI_MENUFOLDER, IDS_MENUSUBMENU, 0,
232 0, 0, 0, 0, 0, 0, 0, 0 },
233 { ShellSubMenuLink, NULL, IDI_MENULINK, IDS_MENUSUBMENU, 0,
234 0, 0, 0, 0, 0, 0, 0, 0 },
235 { ShellSubMenuMultiple, NULL, IDI_MENUMULTIPLE, IDS_MENUSUBMENU, 0,
236 0, 0, 0, 0, 0, 0, 0, 0 },
237 // mark the last entry to tell the loop where to stop iterating over this array
238 { ShellMenuLastEntry, 0, 0, 0, 0,
239 0, 0, 0, 0, 0, 0, 0, 0 },
243 STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST pIDFolder,
244 LPDATAOBJECT pDataObj,
245 HKEY /* hRegKey */)
248 ATLTRACE("Shell :: Initialize\n");
249 PreserveChdir preserveChdir;
250 files_.clear();
251 folder_.erase();
252 uuidSource.erase();
253 uuidTarget.erase();
254 itemStates = 0;
255 itemStatesFolder = 0;
256 stdstring statuspath;
257 git_wc_status_kind fetchedstatus = git_wc_status_none;
258 // get selected files/folders
259 if (pDataObj)
261 STGMEDIUM medium;
262 FORMATETC fmte = {(CLIPFORMAT)g_shellidlist,
263 (DVTARGETDEVICE FAR *)NULL,
264 DVASPECT_CONTENT,
265 -1,
266 TYMED_HGLOBAL};
267 HRESULT hres = pDataObj->GetData(&fmte, &medium);
269 if (SUCCEEDED(hres) && medium.hGlobal)
271 if (m_State == FileStateDropHandler)
274 FORMATETC etc = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
275 STGMEDIUM stg = { TYMED_HGLOBAL };
276 if ( FAILED( pDataObj->GetData ( &etc, &stg )))
278 ReleaseStgMedium ( &medium );
279 return E_INVALIDARG;
283 HDROP drop = (HDROP)GlobalLock(stg.hGlobal);
284 if ( NULL == drop )
286 ReleaseStgMedium ( &stg );
287 ReleaseStgMedium ( &medium );
288 return E_INVALIDARG;
291 int count = DragQueryFile(drop, (UINT)-1, NULL, 0);
292 if (count == 1)
293 itemStates |= ITEMIS_ONLYONE;
294 for (int i = 0; i < count; i++)
296 // find the path length in chars
297 UINT len = DragQueryFile(drop, i, NULL, 0);
298 if (len == 0)
299 continue;
300 TCHAR * szFileName = new TCHAR[len+1];
301 if (0 == DragQueryFile(drop, i, szFileName, len+1))
303 delete [] szFileName;
304 continue;
306 stdstring str = stdstring(szFileName);
307 delete [] szFileName;
308 if ((str.empty() == false)&&(g_ShellCache.IsContextPathAllowed(szFileName)))
310 if (itemStates & ITEMIS_ONLYONE)
312 CTGitPath strpath;
313 strpath.SetFromWin(str.c_str());
314 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".diff"))==0) ? ITEMIS_PATCHFILE : 0;
315 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".patch"))==0) ? ITEMIS_PATCHFILE : 0;
317 files_.push_back(str);
318 if (i == 0)
320 //get the Subversion status of the item
321 git_wc_status_kind status = git_wc_status_none;
322 CTGitPath askedpath;
323 askedpath.SetFromWin(str.c_str());
326 GitStatus stat;
327 stat.GetStatus(CTGitPath(str.c_str()), false, true, true);
328 if (stat.status)
330 statuspath = str;
331 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);
332 fetchedstatus = status;
333 //if ((stat.status->entry)&&(stat.status->entry->lock_token))
334 // itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;
335 if ( askedpath.IsDirectory() )//if ((stat.status->entry)&&(stat.status->entry->kind == git_node_dir))
337 itemStates |= ITEMIS_FOLDER;
338 if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
339 itemStates |= ITEMIS_FOLDERINSVN;
341 //if ((stat.status->entry)&&(stat.status->entry->present_props))
343 // if (strstr(stat.status->entry->present_props, "svn:needs-lock"))
344 // itemStates |= ITEMIS_NEEDSLOCK;
346 //if ((stat.status->entry)&&(stat.status->entry->uuid))
347 // uuidSource = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);
349 else
351 // sometimes, git_client_status() returns with an error.
352 // in that case, we have to check if the working copy is versioned
353 // anyway to show the 'correct' context menu
354 if (askedpath.HasAdminDir())
355 status = git_wc_status_normal;
358 catch ( ... )
360 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));
363 // TODO: should we really assume any sub-directory to be versioned
364 // or only if it contains versioned files
365 if ( askedpath.IsDirectory() )
367 if (askedpath.HasAdminDir())
368 itemStates |= ITEMIS_INSVN;
370 if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
371 itemStates |= ITEMIS_INSVN;
372 if (status == git_wc_status_ignored)
373 itemStates |= ITEMIS_IGNORED;
374 if (status == git_wc_status_normal)
375 itemStates |= ITEMIS_NORMAL;
376 if (status == git_wc_status_conflicted)
377 itemStates |= ITEMIS_CONFLICTED;
378 if (status == git_wc_status_added)
379 itemStates |= ITEMIS_ADDED;
380 if (status == git_wc_status_deleted)
381 itemStates |= ITEMIS_DELETED;
384 } // for (int i = 0; i < count; i++)
385 GlobalUnlock ( drop );
386 ReleaseStgMedium ( &stg );
388 } // if (m_State == FileStateDropHandler)
389 else
392 //Enumerate PIDLs which the user has selected
393 CIDA* cida = (CIDA*)GlobalLock(medium.hGlobal);
394 ItemIDList parent( GetPIDLFolder (cida));
396 int count = cida->cidl;
397 BOOL statfetched = FALSE;
398 for (int i = 0; i < count; ++i)
400 ItemIDList child (GetPIDLItem (cida, i), &parent);
401 stdstring str = child.toString();
402 if ((str.empty() == false)&&(g_ShellCache.IsContextPathAllowed(str.c_str())))
404 //check if our menu is requested for a subversion admin directory
405 if (g_GitAdminDir.IsAdminDirPath(str.c_str()))
406 continue;
408 files_.push_back(str);
409 CTGitPath strpath;
410 strpath.SetFromWin(str.c_str());
411 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".diff"))==0) ? ITEMIS_PATCHFILE : 0;
412 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".patch"))==0) ? ITEMIS_PATCHFILE : 0;
413 if (!statfetched)
415 //get the Subversion status of the item
416 git_wc_status_kind status = git_wc_status_none;
417 if ((g_ShellCache.IsSimpleContext())&&(strpath.IsDirectory()))
419 if (strpath.HasAdminDir())
420 status = git_wc_status_normal;
422 else
426 GitStatus stat;
427 if (strpath.HasAdminDir())
428 stat.GetStatus(strpath, false, true, true);
429 statuspath = str;
430 if (stat.status)
432 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);
433 fetchedstatus = status;
434 //if ((stat.status->entry)&&(stat.status->entry->lock_token))
435 // itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;
436 if ( strpath.IsDirectory() )//if ((stat.status->entry)&&(stat.status->entry->kind == git_node_dir))
438 itemStates |= ITEMIS_FOLDER;
439 if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
440 itemStates |= ITEMIS_FOLDERINSVN;
442 // TODO: do we need to check that it's not a dir? does conflict options makes sense for dir in git?
443 if (status == git_wc_status_conflicted)//if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))
444 itemStates |= ITEMIS_CONFLICTED;
445 //if ((stat.status->entry)&&(stat.status->entry->present_props))
447 // if (strstr(stat.status->entry->present_props, "svn:needs-lock"))
448 // itemStates |= ITEMIS_NEEDSLOCK;
450 //if ((stat.status->entry)&&(stat.status->entry->uuid))
451 // uuidSource = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);
453 else
455 // sometimes, git_client_status() returns with an error.
456 // in that case, we have to check if the working copy is versioned
457 // anyway to show the 'correct' context menu
458 if (strpath.HasAdminDir())
460 status = git_wc_status_normal;
461 fetchedstatus = status;
464 statfetched = TRUE;
466 catch ( ... )
468 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));
472 // TODO: should we really assume any sub-directory to be versioned
473 // or only if it contains versioned files
474 if ( strpath.IsDirectory() )
476 if (strpath.HasAdminDir())
477 itemStates |= ITEMIS_INSVN;
479 if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
480 itemStates |= ITEMIS_INSVN;
481 if (status == git_wc_status_ignored)
483 itemStates |= ITEMIS_IGNORED;
484 // the item is ignored. Get the svn:ignored properties so we can (maybe) later
485 // offer a 'remove from ignored list' entry
486 // GitProperties props(strpath.GetContainingDirectory(), false);
487 // ignoredprops.empty();
488 // for (int p=0; p<props.GetCount(); ++p)
489 // {
490 // if (props.GetItemName(p).compare(stdstring(_T("svn:ignore")))==0)
491 // {
492 // std::string st = props.GetItemValue(p);
493 // ignoredprops = MultibyteToWide(st.c_str());
494 // // remove all escape chars ('\\')
495 // std::remove(ignoredprops.begin(), ignoredprops.end(), '\\');
496 // break;
497 // }
498 // }
500 if (status == git_wc_status_normal)
501 itemStates |= ITEMIS_NORMAL;
502 if (status == git_wc_status_conflicted)
503 itemStates |= ITEMIS_CONFLICTED;
504 if (status == git_wc_status_added)
505 itemStates |= ITEMIS_ADDED;
506 if (status == git_wc_status_deleted)
507 itemStates |= ITEMIS_DELETED;
510 } // for (int i = 0; i < count; ++i)
511 ItemIDList child (GetPIDLItem (cida, 0), &parent);
512 if (g_ShellCache.HasSVNAdminDir(child.toString().c_str(), FALSE))
513 itemStates |= ITEMIS_INVERSIONEDFOLDER;
514 GlobalUnlock(medium.hGlobal);
516 // if the item is a versioned folder, check if there's a patch file
517 // in the clipboard to be used in "Apply Patch"
518 UINT cFormatDiff = RegisterClipboardFormat(_T("TGIT_UNIFIEDDIFF"));
519 if (cFormatDiff)
521 if (IsClipboardFormatAvailable(cFormatDiff))
522 itemStates |= ITEMIS_PATCHINCLIPBOARD;
524 if (IsClipboardFormatAvailable(CF_HDROP))
525 itemStates |= ITEMIS_PATHINCLIPBOARD;
529 ReleaseStgMedium ( &medium );
530 if (medium.pUnkForRelease)
532 IUnknown* relInterface = (IUnknown*)medium.pUnkForRelease;
533 relInterface->Release();
538 // get folder background
539 if (pIDFolder)
542 ItemIDList list(pIDFolder);
543 folder_ = list.toString();
544 git_wc_status_kind status = git_wc_status_none;
545 if (IsClipboardFormatAvailable(CF_HDROP))
546 itemStatesFolder |= ITEMIS_PATHINCLIPBOARD;
548 CTGitPath askedpath;
549 askedpath.SetFromWin(folder_.c_str());
551 if ((folder_.compare(statuspath)!=0)&&(g_ShellCache.IsContextPathAllowed(folder_.c_str())))
556 GitStatus stat;
557 stat.GetStatus(CTGitPath(folder_.c_str()), false, true, true);
558 if (stat.status)
560 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);
561 // if ((stat.status->entry)&&(stat.status->entry->lock_token))
562 // itemStatesFolder |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;
563 // if ((stat.status->entry)&&(stat.status->entry->present_props))
564 // {
565 // if (strstr(stat.status->entry->present_props, "svn:needs-lock"))
566 // itemStatesFolder |= ITEMIS_NEEDSLOCK;
567 // }
568 // if ((stat.status->entry)&&(stat.status->entry->uuid))
569 // uuidTarget = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);
572 else
574 // sometimes, git_client_status() returns with an error.
575 // in that case, we have to check if the working copy is versioned
576 // anyway to show the 'correct' context menu
577 if (askedpath.HasAdminDir())
578 status = git_wc_status_normal;
581 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
582 if (askedpath.HasAdminDir())
583 itemStatesFolder |= ITEMIS_INSVN;
584 if (status == git_wc_status_normal)
585 itemStatesFolder |= ITEMIS_NORMAL;
586 if (status == git_wc_status_conflicted)
587 itemStatesFolder |= ITEMIS_CONFLICTED;
588 if (status == git_wc_status_added)
589 itemStatesFolder |= ITEMIS_ADDED;
590 if (status == git_wc_status_deleted)
591 itemStatesFolder |= ITEMIS_DELETED;
594 catch ( ... )
596 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));
599 else
601 status = fetchedstatus;
603 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
604 if (askedpath.HasAdminDir())
606 itemStatesFolder |= ITEMIS_FOLDERINSVN;
608 if (askedpath.HasSubmodules())
610 itemStatesFolder |= ITEMIS_SUBMODULE;
612 if (status == git_wc_status_ignored)
613 itemStatesFolder |= ITEMIS_IGNORED;
614 itemStatesFolder |= ITEMIS_FOLDER;
615 if (files_.size() == 0)
616 itemStates |= ITEMIS_ONLYONE;
617 if (m_State != FileStateDropHandler)
618 itemStates |= itemStatesFolder;
622 if (files_.size() == 2)
623 itemStates |= ITEMIS_TWO;
624 if ((files_.size() == 1)&&(g_ShellCache.IsContextPathAllowed(files_.front().c_str())))
627 itemStates |= ITEMIS_ONLYONE;
628 if (m_State != FileStateDropHandler)
630 if (PathIsDirectory(files_.front().c_str()))
632 folder_ = files_.front();
633 git_wc_status_kind status = git_wc_status_none;
634 CTGitPath askedpath;
635 askedpath.SetFromWin(folder_.c_str());
637 if (folder_.compare(statuspath)!=0)
641 GitStatus stat;
642 stat.GetStatus(CTGitPath(folder_.c_str()), false, true, true);
643 if (stat.status)
645 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);
646 // if ((stat.status->entry)&&(stat.status->entry->lock_token))
647 // itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;
648 // if ((stat.status->entry)&&(stat.status->entry->present_props))
649 // {
650 // if (strstr(stat.status->entry->present_props, "svn:needs-lock"))
651 // itemStates |= ITEMIS_NEEDSLOCK;
652 // }
653 // if ((stat.status->entry)&&(stat.status->entry->uuid))
654 // uuidTarget = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);
657 catch ( ... )
659 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));
662 else
664 status = fetchedstatus;
666 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
667 if (askedpath.HasAdminDir())
668 itemStates |= ITEMIS_FOLDERINSVN;
669 if (status == git_wc_status_ignored)
670 itemStates |= ITEMIS_IGNORED;
671 itemStates |= ITEMIS_FOLDER;
672 if (status == git_wc_status_added)
673 itemStates |= ITEMIS_ADDED;
674 if (status == git_wc_status_deleted)
675 itemStates |= ITEMIS_DELETED;
681 return NOERROR;
684 void CShellExt::InsertGitMenu(BOOL istop, HMENU menu, UINT pos, UINT_PTR id, UINT stringid, UINT icon, UINT idCmdFirst, GitCommands com, UINT uFlags)
686 TCHAR menutextbuffer[512] = {0};
687 TCHAR verbsbuffer[255] = {0};
688 MAKESTRING(stringid);
690 if (istop)
692 //menu entry for the top context menu, so append an "Git " before
693 //the menu text to indicate where the entry comes from
694 _tcscpy_s(menutextbuffer, 255, _T("Git "));
696 _tcscat_s(menutextbuffer, 255, stringtablebuffer);
697 #if 1
698 // insert branch name into "Git Commit..." entry, so it looks like "Git Commit "master"..."
699 // so we have an easy and fast way to check the current branch
700 // (the other alternative is using a separate disabled menu entry, the code is already done but commented out)
701 if (com == ShellMenuCommit)
703 // get branch name
704 CTGitPath path(folder_.empty() ? files_.front().c_str() : folder_.c_str());
705 CString sProjectRoot;
706 CString sBranchName;
708 if (path.HasAdminDir(&sProjectRoot) && !g_Git.GetCurrentBranchFromFile(sProjectRoot, sBranchName))
710 if (sBranchName.GetLength() == 40)
712 // if SHA1 only show 4 first bytes
713 BOOL bIsSha1 = TRUE;
714 for (int i=0; i<40; i++)
715 if ( !iswxdigit(sBranchName[i]) )
717 bIsSha1 = FALSE;
718 break;
720 if (bIsSha1)
721 sBranchName = sBranchName.Left(8) + _T("....");
724 // sanity check
725 if (sBranchName.GetLength() > 64)
726 sBranchName = sBranchName.Left(64) + _T("...");
728 // scan to before "..."
729 LPTSTR s = menutextbuffer + _tcslen(menutextbuffer)-1;
730 if (s > menutextbuffer)
732 while (s > menutextbuffer)
734 if (*s != _T('.'))
736 s++;
737 break;
739 s--;
742 else
744 s = menutextbuffer;
747 // append branch name and end with ...
748 _tcscpy(s, _T(" -> \"") + sBranchName + _T("\"..."));
751 #endif
752 if ((fullver < 0x500)||(fullver == 0x500 && !(uFlags&~(CMF_RESERVED|CMF_EXPLORE))))
754 // on win2k, the context menu does not work properly if we use
755 // icon bitmaps. At least the menu text is empty in the context menu
756 // for folder backgrounds (seems like a win2k bug).
757 // the workaround is to use the check/unchecked bitmaps, which are drawn
758 // with AND raster op, but it's better than nothing at all
759 InsertMenu(menu, pos, MF_BYPOSITION | MF_STRING , id, menutextbuffer);
760 if (icon)
762 HBITMAP bmp = IconToBitmap(icon);
763 SetMenuItemBitmaps(menu, pos, MF_BYPOSITION, bmp, bmp);
766 else
768 MENUITEMINFO menuiteminfo;
769 SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));
770 menuiteminfo.cbSize = sizeof(menuiteminfo);
771 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
772 menuiteminfo.fType = MFT_STRING;
773 menuiteminfo.dwTypeData = menutextbuffer;
774 if (icon)
776 menuiteminfo.fMask |= MIIM_BITMAP;
777 menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(icon) : HBMMENU_CALLBACK;
779 menuiteminfo.wID = id;
780 InsertMenuItem(menu, pos, TRUE, &menuiteminfo);
783 if (istop)
785 //menu entry for the top context menu, so append an "Git " before
786 //the menu text to indicate where the entry comes from
787 _tcscpy_s(menutextbuffer, 255, _T("Git "));
789 LoadString(g_hResInst, stringid, verbsbuffer, sizeof(verbsbuffer));
790 _tcscat_s(menutextbuffer, 255, verbsbuffer);
791 stdstring verb = stdstring(menutextbuffer);
792 if (verb.find('&') != -1)
794 verb.erase(verb.find('&'),1);
796 myVerbsMap[verb] = id - idCmdFirst;
797 myVerbsMap[verb] = id;
798 myVerbsIDMap[id - idCmdFirst] = verb;
799 myVerbsIDMap[id] = verb;
800 // We store the relative and absolute diameter
801 // (drawitem callback uses absolute, others relative)
802 myIDMap[id - idCmdFirst] = com;
803 myIDMap[id] = com;
804 if (!istop)
805 mySubMenuMap[pos] = com;
808 HBITMAP CShellExt::IconToBitmap(UINT uIcon)
810 std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);
811 if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)
812 return bitmap_it->second;
814 HICON hIcon = (HICON)LoadImage(g_hResInst, MAKEINTRESOURCE(uIcon), IMAGE_ICON, 12, 12, LR_DEFAULTCOLOR);
815 if (!hIcon)
816 return NULL;
818 RECT rect;
820 rect.right = ::GetSystemMetrics(SM_CXMENUCHECK);
821 rect.bottom = ::GetSystemMetrics(SM_CYMENUCHECK);
823 rect.left = rect.top = 0;
825 HWND desktop = ::GetDesktopWindow();
826 if (desktop == NULL)
828 DestroyIcon(hIcon);
829 return NULL;
832 HDC screen_dev = ::GetDC(desktop);
833 if (screen_dev == NULL)
835 DestroyIcon(hIcon);
836 return NULL;
839 // Create a compatible DC
840 HDC dst_hdc = ::CreateCompatibleDC(screen_dev);
841 if (dst_hdc == NULL)
843 DestroyIcon(hIcon);
844 ::ReleaseDC(desktop, screen_dev);
845 return NULL;
848 // Create a new bitmap of icon size
849 HBITMAP bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);
850 if (bmp == NULL)
852 DestroyIcon(hIcon);
853 ::DeleteDC(dst_hdc);
854 ::ReleaseDC(desktop, screen_dev);
855 return NULL;
858 // Select it into the compatible DC
859 HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);
860 if (old_dst_bmp == NULL)
862 DestroyIcon(hIcon);
863 return NULL;
866 // Fill the background of the compatible DC with the white color
867 // that is taken by menu routines as transparent
868 ::SetBkColor(dst_hdc, RGB(255, 255, 255));
869 ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
871 // Draw the icon into the compatible DC
872 ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);
874 // Restore settings
875 ::SelectObject(dst_hdc, old_dst_bmp);
876 ::DeleteDC(dst_hdc);
877 ::ReleaseDC(desktop, screen_dev);
878 DestroyIcon(hIcon);
879 if (bmp)
880 bitmaps.insert(bitmap_it, std::make_pair(uIcon, bmp));
881 return bmp;
884 bool CShellExt::WriteClipboardPathsToTempFile(stdstring& tempfile)
886 bool bRet = true;
887 tempfile = stdstring();
888 //write all selected files and paths to a temporary file
889 //for TortoiseProc.exe to read out again.
890 DWORD written = 0;
891 DWORD pathlength = GetTempPath(0, NULL);
892 TCHAR * path = new TCHAR[pathlength+1];
893 TCHAR * tempFile = new TCHAR[pathlength + 100];
894 GetTempPath (pathlength+1, path);
895 GetTempFileName (path, _T("git"), 0, tempFile);
896 tempfile = stdstring(tempFile);
898 HANDLE file = ::CreateFile (tempFile,
899 GENERIC_WRITE,
900 FILE_SHARE_READ,
902 CREATE_ALWAYS,
903 FILE_ATTRIBUTE_TEMPORARY,
906 delete [] path;
907 delete [] tempFile;
908 if (file == INVALID_HANDLE_VALUE)
909 return false;
911 if (!IsClipboardFormatAvailable(CF_HDROP))
912 return false;
913 if (!OpenClipboard(NULL))
914 return false;
916 stdstring sClipboardText;
917 HGLOBAL hglb = GetClipboardData(CF_HDROP);
918 HDROP hDrop = (HDROP)GlobalLock(hglb);
919 if(hDrop != NULL)
921 TCHAR szFileName[MAX_PATH];
922 UINT cFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
923 for(UINT i = 0; i < cFiles; ++i)
925 DragQueryFile(hDrop, i, szFileName, sizeof(szFileName));
926 stdstring filename = szFileName;
927 ::WriteFile (file, filename.c_str(), filename.size()*sizeof(TCHAR), &written, 0);
928 ::WriteFile (file, _T("\n"), 2, &written, 0);
930 GlobalUnlock(hDrop);
932 else bRet = false;
933 GlobalUnlock(hglb);
935 CloseClipboard();
936 ::CloseHandle(file);
938 return bRet;
941 stdstring CShellExt::WriteFileListToTempFile()
943 //write all selected files and paths to a temporary file
944 //for TortoiseProc.exe to read out again.
945 DWORD pathlength = GetTempPath(0, NULL);
946 TCHAR * path = new TCHAR[pathlength+1];
947 TCHAR * tempFile = new TCHAR[pathlength + 100];
948 GetTempPath (pathlength+1, path);
949 GetTempFileName (path, _T("git"), 0, tempFile);
950 stdstring retFilePath = stdstring(tempFile);
952 HANDLE file = ::CreateFile (tempFile,
953 GENERIC_WRITE,
954 FILE_SHARE_READ,
956 CREATE_ALWAYS,
957 FILE_ATTRIBUTE_TEMPORARY,
960 delete [] path;
961 delete [] tempFile;
962 if (file == INVALID_HANDLE_VALUE)
963 return stdstring();
965 DWORD written = 0;
966 if (files_.empty())
968 ::WriteFile (file, folder_.c_str(), folder_.size()*sizeof(TCHAR), &written, 0);
969 ::WriteFile (file, _T("\n"), 2, &written, 0);
972 for (std::vector<stdstring>::iterator I = files_.begin(); I != files_.end(); ++I)
974 ::WriteFile (file, I->c_str(), I->size()*sizeof(TCHAR), &written, 0);
975 ::WriteFile (file, _T("\n"), 2, &written, 0);
977 ::CloseHandle(file);
978 return retFilePath;
981 STDMETHODIMP CShellExt::QueryDropContext(UINT uFlags, UINT idCmdFirst, HMENU hMenu, UINT &indexMenu)
983 PreserveChdir preserveChdir;
984 LoadLangDll();
986 if ((uFlags & CMF_DEFAULTONLY)!=0)
987 return NOERROR; //we don't change the default action
989 if ((files_.size() == 0)||(folder_.size() == 0))
990 return NOERROR;
992 if (((uFlags & 0x000f)!=CMF_NORMAL)&&(!(uFlags & CMF_EXPLORE))&&(!(uFlags & CMF_VERBSONLY)))
993 return NOERROR;
995 bool bSourceAndTargetFromSameRepository = (uuidSource.compare(uuidTarget) == 0) || uuidSource.empty() || uuidTarget.empty();
997 //the drop handler only has eight commands, but not all are visible at the same time:
998 //if the source file(s) are under version control then those files can be moved
999 //to the new location or they can be moved with a rename,
1000 //if they are unversioned then they can be added to the working copy
1001 //if they are versioned, they also can be exported to an unversioned location
1002 UINT idCmd = idCmdFirst;
1004 // Git move here
1005 // available if source is versioned but not added, target is versioned, source and target from same repository or target folder is added
1006 if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&((itemStates & ITEMIS_INSVN)&&((~itemStates) & ITEMIS_ADDED)))
1007 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPMOVEMENU, 0, idCmdFirst, ShellMenuDropMove, uFlags);
1009 // Git move and rename here
1010 // available if source is a single, versioned but not added item, target is versioned, source and target from same repository or target folder is added
1011 if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_ONLYONE)&&((~itemStates) & ITEMIS_ADDED))
1012 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPMOVERENAMEMENU, 0, idCmdFirst, ShellMenuDropMoveRename, uFlags);
1014 // Git copy here
1015 // available if source is versioned but not added, target is versioned, source and target from same repository or target folder is added
1016 if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&((~itemStates) & ITEMIS_ADDED))
1017 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYMENU, 0, idCmdFirst, ShellMenuDropCopy, uFlags);
1019 // Git copy and rename here, source and target from same repository
1020 // available if source is a single, versioned but not added item, target is versioned or target folder is added
1021 if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_ONLYONE)&&((~itemStates) & ITEMIS_ADDED))
1022 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYRENAMEMENU, 0, idCmdFirst, ShellMenuDropCopyRename, uFlags);
1024 // Git add here
1025 // available if target is versioned and source is either unversioned or from another repository
1026 if ((itemStatesFolder & ITEMIS_FOLDERINSVN)&&(((~itemStates) & ITEMIS_INSVN)||!bSourceAndTargetFromSameRepository))
1027 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYADDMENU, 0, idCmdFirst, ShellMenuDropCopyAdd, uFlags);
1029 // Git export here
1030 // available if source is versioned and a folder
1031 if ((itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_FOLDER))
1032 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPEXPORTMENU, 0, idCmdFirst, ShellMenuDropExport, uFlags);
1034 // Git export all here
1035 // available if source is versioned and a folder
1036 if ((itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_FOLDER))
1037 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPEXPORTEXTENDEDMENU, 0, idCmdFirst, ShellMenuDropExportExtended, uFlags);
1039 // apply patch
1040 // available if source is a patchfile
1041 if (itemStates & ITEMIS_PATCHFILE)
1042 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_MENUAPPLYPATCH, 0, idCmdFirst, ShellMenuApplyPatch, uFlags);
1044 // separator
1045 if (idCmd != idCmdFirst)
1046 InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
1048 return ResultFromScode(MAKE_SCODE(SEVERITY_SUCCESS, 0, (USHORT)(idCmd - idCmdFirst)));
1051 STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu,
1052 UINT indexMenu,
1053 UINT idCmdFirst,
1054 UINT /*idCmdLast*/,
1055 UINT uFlags)
1057 ATLTRACE("Shell :: QueryContextMenu\n");
1058 PreserveChdir preserveChdir;
1060 //first check if our drop handler is called
1061 //and then (if true) provide the context menu for the
1062 //drop handler
1063 if (m_State == FileStateDropHandler)
1065 return QueryDropContext(uFlags, idCmdFirst, hMenu, indexMenu);
1068 if ((uFlags & CMF_DEFAULTONLY)!=0)
1069 return NOERROR; //we don't change the default action
1071 if ((files_.size() == 0)&&(folder_.size() == 0))
1072 return NOERROR;
1074 if (((uFlags & 0x000f)!=CMF_NORMAL)&&(!(uFlags & CMF_EXPLORE))&&(!(uFlags & CMF_VERBSONLY)))
1075 return NOERROR;
1077 int csidlarray[] =
1079 CSIDL_BITBUCKET,
1080 CSIDL_CDBURN_AREA,
1081 CSIDL_COMMON_FAVORITES,
1082 CSIDL_COMMON_STARTMENU,
1083 CSIDL_COMPUTERSNEARME,
1084 CSIDL_CONNECTIONS,
1085 CSIDL_CONTROLS,
1086 CSIDL_COOKIES,
1087 CSIDL_FAVORITES,
1088 CSIDL_FONTS,
1089 CSIDL_HISTORY,
1090 CSIDL_INTERNET,
1091 CSIDL_INTERNET_CACHE,
1092 CSIDL_NETHOOD,
1093 CSIDL_NETWORK,
1094 CSIDL_PRINTERS,
1095 CSIDL_PRINTHOOD,
1096 CSIDL_RECENT,
1097 CSIDL_SENDTO,
1098 CSIDL_STARTMENU,
1101 if (IsIllegalFolder(folder_, csidlarray))
1102 return NOERROR;
1104 if (folder_.empty())
1106 // folder is empty, but maybe files are selected
1107 if (files_.size() == 0)
1108 return NOERROR; // nothing selected - we don't have a menu to show
1109 // check whether a selected entry is an UID - those are namespace extensions
1110 // which we can't handle
1111 for (std::vector<stdstring>::const_iterator it = files_.begin(); it != files_.end(); ++it)
1113 if (_tcsncmp(it->c_str(), _T("::{"), 3)==0)
1114 return NOERROR;
1118 //check if our menu is requested for a subversion admin directory
1119 if (g_GitAdminDir.IsAdminDirPath(folder_.c_str()))
1120 return NOERROR;
1122 if (uFlags & CMF_EXTENDEDVERBS)
1123 itemStates |= ITEMIS_EXTENDED;
1125 const BOOL bShortcut = !!(uFlags & CMF_VERBSONLY);
1126 if ( bShortcut && (files_.size()==1))
1128 // Don't show the context menu for a link if the
1129 // destination is not part of a working copy.
1130 // It would only show the standard menu items
1131 // which are already shown for the lnk-file.
1132 CString path = files_.front().c_str();
1133 if ( !g_GitAdminDir.HasAdminDir(path) )
1135 return NOERROR;
1139 //check if we already added our menu entry for a folder.
1140 //we check that by iterating through all menu entries and check if
1141 //the dwItemData member points to our global ID string. That string is set
1142 //by our shell extension when the folder menu is inserted.
1143 TCHAR menubuf[MAX_PATH];
1144 int count = GetMenuItemCount(hMenu);
1145 for (int i=0; i<count; ++i)
1147 MENUITEMINFO miif;
1148 SecureZeroMemory(&miif, sizeof(MENUITEMINFO));
1149 miif.cbSize = sizeof(MENUITEMINFO);
1150 miif.fMask = MIIM_DATA;
1151 miif.dwTypeData = menubuf;
1152 miif.cch = sizeof(menubuf)/sizeof(TCHAR);
1153 GetMenuItemInfo(hMenu, i, TRUE, &miif);
1154 if (miif.dwItemData == (ULONG_PTR)g_MenuIDString)
1155 return NOERROR;
1158 LoadLangDll();
1159 UINT idCmd = idCmdFirst;
1161 //create the sub menu
1162 HMENU subMenu = CreateMenu();
1163 int indexSubMenu = 0;
1165 unsigned __int64 topmenu = g_ShellCache.GetMenuLayout();
1166 unsigned __int64 menumask = g_ShellCache.GetMenuMask();
1168 int menuIndex = 0;
1169 bool bAddSeparator = false;
1170 bool bMenuEntryAdded = false;
1171 bool bMenuEmpty = true;
1172 // insert separator at start
1173 InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); idCmd++;
1174 bool bShowIcons = !!DWORD(CRegStdWORD(_T("Software\\TortoiseGit\\ShowContextMenuIcons"), TRUE));
1175 // ?? TSV disabled icons for win2k and earlier, but they work for win2k and should work for win95 and up
1176 /*if (fullver <= 0x500)
1177 bShowIcons = false;*/
1179 #if 0
1180 if (itemStates & (ITEMIS_INSVN|ITEMIS_FOLDERINSVN))
1182 // show current branch name (as a "read-only" menu entry)
1184 CTGitPath path(folder_.empty() ? files_.front().c_str() : folder_.c_str());
1185 CString sProjectRoot;
1186 CString sBranchName;
1188 if (path.HasAdminDir(&sProjectRoot) && !g_Git.GetCurrentBranchFromFile(sProjectRoot, sBranchName))
1190 if (sBranchName.GetLength() == 40)
1192 // if SHA1 only show 4 first bytes
1193 BOOL bIsSha1 = TRUE;
1194 for (int i=0; i<40; i++)
1195 if ( !iswxdigit(sBranchName[i]) )
1197 bIsSha1 = FALSE;
1198 break;
1200 if (bIsSha1)
1201 sBranchName = sBranchName.Left(8) + _T("....");
1204 sBranchName = _T('"') + sBranchName + _T('"');
1206 const int icon = IDI_COPY;
1207 const int pos = indexMenu++;
1208 const int id = idCmd++;
1210 if ((fullver < 0x500)||(fullver == 0x500 && !(uFlags&~(CMF_RESERVED|CMF_EXPLORE))))
1212 InsertMenu(hMenu, pos, MF_DISABLED|MF_GRAYED|MF_BYPOSITION|MF_STRING, id, sBranchName);
1213 HBITMAP bmp = IconToBitmap(icon);
1214 SetMenuItemBitmaps(hMenu, pos, MF_BYPOSITION, bmp, bmp);
1216 else
1218 MENUITEMINFO menuiteminfo;
1219 SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));
1220 menuiteminfo.cbSize = sizeof(menuiteminfo);
1221 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_STATE;
1222 menuiteminfo.fState = MFS_DISABLED;
1223 menuiteminfo.fType = MFT_STRING;
1224 menuiteminfo.dwTypeData = (LPWSTR)sBranchName.GetString();
1225 if (icon)
1227 menuiteminfo.fMask |= MIIM_BITMAP;
1228 menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(icon) : HBMMENU_CALLBACK;
1230 if (menuiteminfo.hbmpItem == HBMMENU_CALLBACK)
1232 // WM_DRAWITEM uses myIDMap to get icon, we use the same icon as create branch
1233 myIDMap[id - idCmdFirst] = ShellMenuBranch;
1234 myIDMap[id] = ShellMenuBranch;
1237 menuiteminfo.wID = id;
1238 InsertMenuItem(hMenu, pos, TRUE, &menuiteminfo);
1242 #endif
1244 while (menuInfo[menuIndex].command != ShellMenuLastEntry)
1246 if (menuInfo[menuIndex].command == ShellSeparator)
1248 // we don't add a separator immediately. Because there might not be
1249 // another 'normal' menu entry after we insert a separator.
1250 // we simply set a flag here, indicating that before the next
1251 // 'normal' menu entry, a separator should be added.
1252 if (!bMenuEmpty)
1253 bAddSeparator = true;
1255 else
1257 // check the conditions whether to show the menu entry or not
1258 bool bInsertMenu = false;
1260 if (menuInfo[menuIndex].firstyes && menuInfo[menuIndex].firstno)
1262 if (((menuInfo[menuIndex].firstyes & itemStates) == menuInfo[menuIndex].firstyes)
1264 ((menuInfo[menuIndex].firstno & (~itemStates)) == menuInfo[menuIndex].firstno))
1265 bInsertMenu = true;
1267 else if ((menuInfo[menuIndex].firstyes)&&((menuInfo[menuIndex].firstyes & itemStates) == menuInfo[menuIndex].firstyes))
1268 bInsertMenu = true;
1269 else if ((menuInfo[menuIndex].firstno)&&((menuInfo[menuIndex].firstno & (~itemStates)) == menuInfo[menuIndex].firstno))
1270 bInsertMenu = true;
1272 if (menuInfo[menuIndex].secondyes && menuInfo[menuIndex].secondno)
1274 if (((menuInfo[menuIndex].secondyes & itemStates) == menuInfo[menuIndex].secondyes)
1276 ((menuInfo[menuIndex].secondno & (~itemStates)) == menuInfo[menuIndex].secondno))
1277 bInsertMenu = true;
1279 else if ((menuInfo[menuIndex].secondyes)&&((menuInfo[menuIndex].secondyes & itemStates) == menuInfo[menuIndex].secondyes))
1280 bInsertMenu = true;
1281 else if ((menuInfo[menuIndex].secondno)&&((menuInfo[menuIndex].secondno & (~itemStates)) == menuInfo[menuIndex].secondno))
1282 bInsertMenu = true;
1284 if (menuInfo[menuIndex].thirdyes && menuInfo[menuIndex].thirdno)
1286 if (((menuInfo[menuIndex].thirdyes & itemStates) == menuInfo[menuIndex].thirdyes)
1288 ((menuInfo[menuIndex].thirdno & (~itemStates)) == menuInfo[menuIndex].thirdno))
1289 bInsertMenu = true;
1291 else if ((menuInfo[menuIndex].thirdyes)&&((menuInfo[menuIndex].thirdyes & itemStates) == menuInfo[menuIndex].thirdyes))
1292 bInsertMenu = true;
1293 else if ((menuInfo[menuIndex].thirdno)&&((menuInfo[menuIndex].thirdno & (~itemStates)) == menuInfo[menuIndex].thirdno))
1294 bInsertMenu = true;
1296 if (menuInfo[menuIndex].fourthyes && menuInfo[menuIndex].fourthno)
1298 if (((menuInfo[menuIndex].fourthyes & itemStates) == menuInfo[menuIndex].fourthyes)
1300 ((menuInfo[menuIndex].fourthno & (~itemStates)) == menuInfo[menuIndex].fourthno))
1301 bInsertMenu = true;
1303 else if ((menuInfo[menuIndex].fourthyes)&&((menuInfo[menuIndex].fourthyes & itemStates) == menuInfo[menuIndex].fourthyes))
1304 bInsertMenu = true;
1305 else if ((menuInfo[menuIndex].fourthno)&&((menuInfo[menuIndex].fourthno & (~itemStates)) == menuInfo[menuIndex].fourthno))
1306 bInsertMenu = true;
1308 if (menuInfo[menuIndex].menuID & (~menumask))
1310 if (bInsertMenu)
1312 // insert a separator
1313 if ((bMenuEntryAdded)&&(bAddSeparator))
1315 bAddSeparator = false;
1316 bMenuEntryAdded = false;
1317 InsertMenu(subMenu, indexSubMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
1318 idCmd++;
1321 // handle special cases (sub menus)
1322 if ((menuInfo[menuIndex].command == ShellMenuIgnoreSub)||(menuInfo[menuIndex].command == ShellMenuUnIgnoreSub))
1324 InsertIgnoreSubmenus(idCmd, idCmdFirst, hMenu, subMenu, indexMenu, indexSubMenu, topmenu, bShowIcons, uFlags);
1325 bMenuEntryAdded = true;
1326 bMenuEmpty = false;
1328 else
1330 bool bIsTop = ((topmenu & menuInfo[menuIndex].menuID) != 0);
1332 // insert the menu entry
1333 InsertGitMenu( bIsTop,
1334 bIsTop ? hMenu : subMenu,
1335 bIsTop ? indexMenu++ : indexSubMenu++,
1336 idCmd++,
1337 menuInfo[menuIndex].menuTextID,
1338 bShowIcons ? menuInfo[menuIndex].iconID : 0,
1339 idCmdFirst,
1340 menuInfo[menuIndex].command,
1341 uFlags);
1342 if (!bIsTop)
1344 bMenuEntryAdded = true;
1345 bMenuEmpty = false;
1351 menuIndex++;
1354 //add sub menu to main context menu
1355 //don't use InsertMenu because this will lead to multiple menu entries in the explorer file menu.
1356 //see http://support.microsoft.com/default.aspx?scid=kb;en-us;214477 for details of that.
1357 MAKESTRING(IDS_MENUSUBMENU);
1358 MENUITEMINFO menuiteminfo;
1359 SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));
1360 menuiteminfo.cbSize = sizeof(menuiteminfo);
1361 menuiteminfo.fType = MFT_STRING;
1362 menuiteminfo.dwTypeData = stringtablebuffer;
1364 UINT uIcon = bShowIcons ? IDI_APP : 0;
1365 if (folder_.size())
1367 uIcon = bShowIcons ? IDI_MENUFOLDER : 0;
1368 myIDMap[idCmd - idCmdFirst] = ShellSubMenuFolder;
1369 myIDMap[idCmd] = ShellSubMenuFolder;
1370 menuiteminfo.dwItemData = (ULONG_PTR)g_MenuIDString;
1372 else if (!bShortcut && (files_.size()==1))
1374 uIcon = bShowIcons ? IDI_MENUFILE : 0;
1375 myIDMap[idCmd - idCmdFirst] = ShellSubMenuFile;
1376 myIDMap[idCmd] = ShellSubMenuFile;
1378 else if (bShortcut && (files_.size()==1))
1380 uIcon = bShowIcons ? IDI_MENULINK : 0;
1381 myIDMap[idCmd - idCmdFirst] = ShellSubMenuLink;
1382 myIDMap[idCmd] = ShellSubMenuLink;
1384 else if (files_.size() > 1)
1386 uIcon = bShowIcons ? IDI_MENUMULTIPLE : 0;
1387 myIDMap[idCmd - idCmdFirst] = ShellSubMenuMultiple;
1388 myIDMap[idCmd] = ShellSubMenuMultiple;
1390 else
1392 myIDMap[idCmd - idCmdFirst] = ShellSubMenu;
1393 myIDMap[idCmd] = ShellSubMenu;
1395 HBITMAP bmp = NULL;
1396 if ((fullver < 0x500)||(fullver == 0x500 && !(uFlags&~(CMF_RESERVED|CMF_EXPLORE))))
1398 menuiteminfo.fMask = MIIM_STRING | MIIM_ID | MIIM_SUBMENU | MIIM_DATA;
1399 if (uIcon)
1401 menuiteminfo.fMask |= MIIM_CHECKMARKS;
1402 bmp = IconToBitmap(uIcon);
1403 menuiteminfo.hbmpChecked = bmp;
1404 menuiteminfo.hbmpUnchecked = bmp;
1407 else
1409 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_SUBMENU | MIIM_DATA | MIIM_STRING;
1410 if (uIcon)
1412 menuiteminfo.fMask |= MIIM_BITMAP;
1413 menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(uIcon) : HBMMENU_CALLBACK;
1416 menuiteminfo.hSubMenu = subMenu;
1417 menuiteminfo.wID = idCmd++;
1418 InsertMenuItem(hMenu, indexMenu++, TRUE, &menuiteminfo);
1420 //separator after
1421 InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); idCmd++;
1423 //return number of menu items added
1424 return ResultFromScode(MAKE_SCODE(SEVERITY_SUCCESS, 0, (USHORT)(idCmd - idCmdFirst)));
1428 // This is called when you invoke a command on the menu:
1429 STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
1431 PreserveChdir preserveChdir;
1432 HRESULT hr = E_INVALIDARG;
1433 if (lpcmi == NULL)
1434 return hr;
1436 std::string command;
1437 std::string parent;
1438 std::string file;
1440 if ((files_.size() > 0)||(folder_.size() > 0))
1442 UINT idCmd = LOWORD(lpcmi->lpVerb);
1444 if (HIWORD(lpcmi->lpVerb))
1446 stdstring verb = stdstring(MultibyteToWide(lpcmi->lpVerb));
1447 std::map<stdstring, UINT_PTR>::const_iterator verb_it = myVerbsMap.lower_bound(verb);
1448 if (verb_it != myVerbsMap.end() && verb_it->first == verb)
1449 idCmd = verb_it->second;
1450 else
1451 return hr;
1454 // See if we have a handler interface for this id
1455 std::map<UINT_PTR, UINT_PTR>::const_iterator id_it = myIDMap.lower_bound(idCmd);
1456 if (id_it != myIDMap.end() && id_it->first == idCmd)
1458 STARTUPINFO startup;
1459 PROCESS_INFORMATION process;
1460 memset(&startup, 0, sizeof(startup));
1461 startup.cb = sizeof(startup);
1462 memset(&process, 0, sizeof(process));
1463 CRegStdString tortoiseProcPath(_T("Software\\TortoiseGit\\ProcPath"), _T("TortoiseProc.exe"), false, HKEY_LOCAL_MACHINE);
1464 CRegStdString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T("TortoiseMerge.exe"), false, HKEY_LOCAL_MACHINE);
1466 //TortoiseProc expects a command line of the form:
1467 //"/command:<commandname> /pathfile:<path> /startrev:<startrevision> /endrev:<endrevision> /deletepathfile
1468 // or
1469 //"/command:<commandname> /path:<path> /startrev:<startrevision> /endrev:<endrevision>
1471 //* path is a path to a single file/directory for commands which only act on single items (log, checkout, ...)
1472 //* pathfile is a path to a temporary file which contains a list of file paths
1473 stdstring svnCmd = _T(" /command:");
1474 stdstring tempfile;
1475 switch (id_it->second)
1477 //#region case
1478 case ShellMenuCheckout:
1479 svnCmd += _T("checkout /path:\"");
1480 svnCmd += folder_;
1481 svnCmd += _T("\"");
1482 break;
1483 case ShellMenuUpdate:
1484 tempfile = WriteFileListToTempFile();
1485 svnCmd += _T("update /pathfile:\"");
1486 svnCmd += tempfile;
1487 svnCmd += _T("\"");
1488 svnCmd += _T(" /deletepathfile");
1489 break;
1490 case ShellMenuUpdateExt:
1491 tempfile = WriteFileListToTempFile();
1492 svnCmd += _T("subupdate /pathfile:\"");
1493 svnCmd += tempfile;
1494 svnCmd += _T("\"");
1495 svnCmd += _T(" /deletepathfile");
1496 svnCmd += _T(" /bkpath:");
1497 svnCmd += folder_;
1498 break;
1499 case ShellMenuCommit:
1500 tempfile = WriteFileListToTempFile();
1501 svnCmd += _T("commit /pathfile:\"");
1502 svnCmd += tempfile;
1503 svnCmd += _T("\"");
1504 svnCmd += _T(" /deletepathfile");
1505 break;
1506 case ShellMenuAdd:
1507 case ShellMenuAddAsReplacement:
1508 tempfile = WriteFileListToTempFile();
1509 svnCmd += _T("add /pathfile:\"");
1510 svnCmd += tempfile;
1511 svnCmd += _T("\"");
1512 svnCmd += _T(" /deletepathfile");
1513 break;
1514 case ShellMenuIgnore:
1515 tempfile = WriteFileListToTempFile();
1516 svnCmd += _T("ignore /pathfile:\"");
1517 svnCmd += tempfile;
1518 svnCmd += _T("\"");
1519 svnCmd += _T(" /deletepathfile");
1520 break;
1521 case ShellMenuIgnoreCaseSensitive:
1522 tempfile = WriteFileListToTempFile();
1523 svnCmd += _T("ignore /pathfile:\"");
1524 svnCmd += tempfile;
1525 svnCmd += _T("\"");
1526 svnCmd += _T(" /deletepathfile");
1527 svnCmd += _T(" /onlymask");
1528 break;
1529 case ShellMenuUnIgnore:
1530 tempfile = WriteFileListToTempFile();
1531 svnCmd += _T("unignore /pathfile:\"");
1532 svnCmd += tempfile;
1533 svnCmd += _T("\"");
1534 svnCmd += _T(" /deletepathfile");
1535 break;
1536 case ShellMenuUnIgnoreCaseSensitive:
1537 tempfile = WriteFileListToTempFile();
1538 svnCmd += _T("unignore /pathfile:\"");
1539 svnCmd += tempfile;
1540 svnCmd += _T("\"");
1541 svnCmd += _T(" /deletepathfile");
1542 svnCmd += _T(" /onlymask");
1543 break;
1544 case ShellMenuRevert:
1545 tempfile = WriteFileListToTempFile();
1546 svnCmd += _T("revert /pathfile:\"");
1547 svnCmd += tempfile;
1548 svnCmd += _T("\"");
1549 svnCmd += _T(" /deletepathfile");
1550 break;
1551 case ShellMenuDelUnversioned:
1552 svnCmd += _T("delunversioned /path:\"");
1553 svnCmd += folder_;
1554 svnCmd += _T("\"");
1555 break;
1556 case ShellMenuCleanup:
1557 tempfile = WriteFileListToTempFile();
1558 svnCmd += _T("cleanup /pathfile:\"");
1559 svnCmd += tempfile;
1560 svnCmd += _T("\"");
1561 svnCmd += _T(" /deletepathfile");
1562 break;
1563 case ShellMenuResolve:
1564 tempfile = WriteFileListToTempFile();
1565 svnCmd += _T("resolve /pathfile:\"");
1566 svnCmd += tempfile;
1567 svnCmd += _T("\"");
1568 svnCmd += _T(" /deletepathfile");
1569 break;
1570 case ShellMenuSwitch:
1571 svnCmd += _T("switch /path:\"");
1572 if (files_.size() > 0)
1573 svnCmd += files_.front();
1574 else
1575 svnCmd += folder_;
1576 svnCmd += _T("\"");
1577 break;
1578 case ShellMenuImport:
1579 svnCmd += _T("import /path:\"");
1580 svnCmd += folder_;
1581 svnCmd += _T("\"");
1582 break;
1583 case ShellMenuExport:
1584 svnCmd += _T("export /path:\"");
1585 svnCmd += folder_;
1586 svnCmd += _T("\"");
1587 break;
1588 case ShellMenuAbout:
1589 svnCmd += _T("about");
1590 break;
1591 case ShellMenuCreateRepos:
1592 svnCmd += _T("repocreate /path:\"");
1593 svnCmd += folder_;
1594 svnCmd += _T("\"");
1595 break;
1596 case ShellMenuMerge:
1597 svnCmd += _T("merge /path:\"");
1598 if (files_.size() > 0)
1599 svnCmd += files_.front();
1600 else
1601 svnCmd += folder_;
1602 svnCmd += _T("\"");
1603 break;
1604 case ShellMenuMergeAll:
1605 svnCmd += _T("mergeall /path:\"");
1606 if (files_.size() > 0)
1607 svnCmd += files_.front();
1608 else
1609 svnCmd += folder_;
1610 svnCmd += _T("\"");
1611 break;
1612 case ShellMenuCopy:
1613 svnCmd += _T("copy /path:\"");
1614 if (files_.size() > 0)
1615 svnCmd += files_.front();
1616 else
1617 svnCmd += folder_;
1618 svnCmd += _T("\"");
1619 break;
1620 case ShellMenuSettings:
1621 svnCmd += _T("settings");
1622 break;
1623 case ShellMenuHelp:
1624 svnCmd += _T("help");
1625 break;
1626 case ShellMenuRename:
1627 svnCmd += _T("rename /path:\"");
1628 if (files_.size() > 0)
1629 svnCmd += files_.front();
1630 else
1631 svnCmd += folder_;
1632 svnCmd += _T("\"");
1633 break;
1634 case ShellMenuRemove:
1635 tempfile = WriteFileListToTempFile();
1636 svnCmd += _T("remove /pathfile:\"");
1637 svnCmd += tempfile;
1638 svnCmd += _T("\"");
1639 svnCmd += _T(" /deletepathfile");
1640 break;
1641 case ShellMenuRemoveKeep:
1642 tempfile = WriteFileListToTempFile();
1643 svnCmd += _T("remove /pathfile:\"");
1644 svnCmd += tempfile;
1645 svnCmd += _T("\"");
1646 svnCmd += _T(" /deletepathfile");
1647 svnCmd += _T(" /keep");
1648 break;
1649 case ShellMenuDiff:
1650 svnCmd += _T("diff /path:\"");
1651 if (files_.size() == 1)
1652 svnCmd += files_.front();
1653 else if (files_.size() == 2)
1655 std::vector<stdstring>::iterator I = files_.begin();
1656 svnCmd += *I;
1657 I++;
1658 svnCmd += _T("\" /path2:\"");
1659 svnCmd += *I;
1661 else
1662 svnCmd += folder_;
1663 svnCmd += _T("\"");
1664 if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
1665 svnCmd += _T(" /alternative");
1666 break;
1667 case ShellMenuPrevDiff:
1668 svnCmd += _T("prevdiff /path:\"");
1669 if (files_.size() == 1)
1670 svnCmd += files_.front();
1671 else
1672 svnCmd += folder_;
1673 svnCmd += _T("\"");
1674 if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
1675 svnCmd += _T(" /alternative");
1676 break;
1677 case ShellMenuUrlDiff:
1678 svnCmd += _T("urldiff /path:\"");
1679 if (files_.size() == 1)
1680 svnCmd += files_.front();
1681 else
1682 svnCmd += folder_;
1683 svnCmd += _T("\"");
1684 break;
1685 case ShellMenuDropCopyAdd:
1686 tempfile = WriteFileListToTempFile();
1687 svnCmd += _T("dropcopyadd /pathfile:\"");
1688 svnCmd += tempfile;
1689 svnCmd += _T("\"");
1690 svnCmd += _T(" /deletepathfile");
1691 svnCmd += _T(" /droptarget:\"");
1692 svnCmd += folder_;
1693 svnCmd += _T("\"";)
1694 break;
1695 case ShellMenuDropCopy:
1696 tempfile = WriteFileListToTempFile();
1697 svnCmd += _T("dropcopy /pathfile:\"");
1698 svnCmd += tempfile;
1699 svnCmd += _T("\"");
1700 svnCmd += _T(" /deletepathfile");
1701 svnCmd += _T(" /droptarget:\"");
1702 svnCmd += folder_;
1703 svnCmd += _T("\"";)
1704 break;
1705 case ShellMenuDropCopyRename:
1706 tempfile = WriteFileListToTempFile();
1707 svnCmd += _T("dropcopy /pathfile:\"");
1708 svnCmd += tempfile;
1709 svnCmd += _T("\"");
1710 svnCmd += _T(" /deletepathfile");
1711 svnCmd += _T(" /droptarget:\"");
1712 svnCmd += folder_;
1713 svnCmd += _T("\" /rename";)
1714 break;
1715 case ShellMenuDropMove:
1716 tempfile = WriteFileListToTempFile();
1717 svnCmd += _T("dropmove /pathfile:\"");
1718 svnCmd += tempfile;
1719 svnCmd += _T("\"");
1720 svnCmd += _T(" /deletepathfile");
1721 svnCmd += _T(" /droptarget:\"");
1722 svnCmd += folder_;
1723 svnCmd += _T("\"");
1724 break;
1725 case ShellMenuDropMoveRename:
1726 tempfile = WriteFileListToTempFile();
1727 svnCmd += _T("dropmove /pathfile:\"");
1728 svnCmd += tempfile;
1729 svnCmd += _T("\"");
1730 svnCmd += _T(" /deletepathfile");
1731 svnCmd += _T(" /droptarget:\"");
1732 svnCmd += folder_;
1733 svnCmd += _T("\" /rename";)
1734 break;
1735 case ShellMenuDropExport:
1736 tempfile = WriteFileListToTempFile();
1737 svnCmd += _T("dropexport /pathfile:\"");
1738 svnCmd += tempfile;
1739 svnCmd += _T("\"");
1740 svnCmd += _T(" /deletepathfile");
1741 svnCmd += _T(" /droptarget:\"");
1742 svnCmd += folder_;
1743 svnCmd += _T("\"");
1744 break;
1745 case ShellMenuDropExportExtended:
1746 tempfile = WriteFileListToTempFile();
1747 svnCmd += _T("dropexport /pathfile:\"");
1748 svnCmd += tempfile;
1749 svnCmd += _T("\"");
1750 svnCmd += _T(" /deletepathfile");
1751 svnCmd += _T(" /droptarget:\"");
1752 svnCmd += folder_;
1753 svnCmd += _T("\"");
1754 svnCmd += _T(" /extended");
1755 break;
1756 case ShellMenuLog:
1757 svnCmd += _T("log /path:\"");
1758 if (files_.size() > 0)
1759 svnCmd += files_.front();
1760 else
1761 svnCmd += folder_;
1762 svnCmd += _T("\"");
1763 break;
1764 case ShellMenuConflictEditor:
1765 svnCmd += _T("conflicteditor /path:\"");
1766 if (files_.size() > 0)
1767 svnCmd += files_.front();
1768 else
1769 svnCmd += folder_;
1770 svnCmd += _T("\"");
1771 break;
1772 case ShellMenuRelocate:
1773 svnCmd += _T("relocate /path:\"");
1774 if (files_.size() > 0)
1775 svnCmd += files_.front();
1776 else
1777 svnCmd += folder_;
1778 svnCmd += _T("\"");
1779 break;
1780 case ShellMenuRebase:
1781 svnCmd += _T("rebase /path:\"");
1782 if (files_.size() > 0)
1783 svnCmd += files_.front();
1784 else
1785 svnCmd += folder_;
1786 svnCmd += _T("\"");
1787 break;
1788 case ShellMenuShowChanged:
1789 if (files_.size() > 1)
1791 tempfile = WriteFileListToTempFile();
1792 svnCmd += _T("repostatus /pathfile:\"");
1793 svnCmd += tempfile;
1794 svnCmd += _T("\"");
1795 svnCmd += _T(" /deletepathfile");
1797 else
1799 svnCmd += _T("repostatus /path:\"");
1800 if (files_.size() > 0)
1801 svnCmd += files_.front();
1802 else
1803 svnCmd += folder_;
1804 svnCmd += _T("\"");
1806 break;
1807 case ShellMenuRefLog:
1808 svnCmd += _T("reflog /path:\"");
1809 if (files_.size() > 0)
1810 svnCmd += files_.front();
1811 else
1812 svnCmd += folder_;
1813 svnCmd += _T("\"");
1814 break;
1816 case ShellMenuStashSave:
1817 svnCmd += _T("stashsave /path:\"");
1818 if (files_.size() > 0)
1819 svnCmd += files_.front();
1820 else
1821 svnCmd += folder_;
1822 svnCmd += _T("\"");
1823 break;
1825 case ShellMenuStashApply:
1826 svnCmd += _T("stashapply /path:\"");
1827 if (files_.size() > 0)
1828 svnCmd += files_.front();
1829 else
1830 svnCmd += folder_;
1831 svnCmd += _T("\"");
1832 break;
1834 case ShellMenuStashList:
1835 svnCmd += _T("reflog /path:\"");
1836 if (files_.size() > 0)
1837 svnCmd += files_.front();
1838 else
1839 svnCmd += folder_;
1840 svnCmd += _T("\" /ref:refs/stash");
1841 break;
1843 case ShellMenuSubAdd:
1844 svnCmd += _T("subadd /path:\"");
1845 if (files_.size() > 0)
1846 svnCmd += files_.front();
1847 else
1848 svnCmd += folder_;
1849 svnCmd += _T("\"");
1850 break;
1852 case ShellMenuBlame:
1853 svnCmd += _T("blame /path:\"");
1854 if (files_.size() > 0)
1855 svnCmd += files_.front();
1856 else
1857 svnCmd += folder_;
1858 svnCmd += _T("\"");
1859 break;
1860 case ShellMenuCreatePatch:
1861 tempfile = WriteFileListToTempFile();
1862 svnCmd += _T("createpatch /pathfile:\"");
1863 svnCmd += tempfile;
1864 svnCmd += _T("\"");
1865 svnCmd += _T(" /deletepathfile");
1866 break;
1867 case ShellMenuApplyPatch:
1868 if ((itemStates & ITEMIS_PATCHINCLIPBOARD) && ((~itemStates) & ITEMIS_PATCHFILE))
1870 // if there's a patch file in the clipboard, we save it
1871 // to a temporary file and tell TortoiseMerge to use that one
1872 UINT cFormat = RegisterClipboardFormat(_T("TGIT_UNIFIEDDIFF"));
1873 if ((cFormat)&&(OpenClipboard(NULL)))
1875 HGLOBAL hglb = GetClipboardData(cFormat);
1876 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
1878 DWORD len = GetTempPath(0, NULL);
1879 TCHAR * path = new TCHAR[len+1];
1880 TCHAR * tempF = new TCHAR[len+100];
1881 GetTempPath (len+1, path);
1882 GetTempFileName (path, TEXT("git"), 0, tempF);
1883 std::wstring sTempFile = std::wstring(tempF);
1884 delete [] path;
1885 delete [] tempF;
1887 FILE * outFile;
1888 size_t patchlen = strlen(lpstr);
1889 _tfopen_s(&outFile, sTempFile.c_str(), _T("wb"));
1890 if(outFile)
1892 size_t size = fwrite(lpstr, sizeof(char), patchlen, outFile);
1893 if (size == patchlen)
1895 itemStates |= ITEMIS_PATCHFILE;
1896 files_.clear();
1897 files_.push_back(sTempFile);
1899 fclose(outFile);
1901 GlobalUnlock(hglb);
1902 CloseClipboard();
1905 if (itemStates & ITEMIS_PATCHFILE)
1907 svnCmd = _T(" /diff:\"");
1908 if (files_.size() > 0)
1910 svnCmd += files_.front();
1911 if (itemStatesFolder & ITEMIS_FOLDERINSVN)
1913 svnCmd += _T("\" /patchpath:\"");
1914 svnCmd += folder_;
1917 else
1918 svnCmd += folder_;
1919 if (itemStates & ITEMIS_INVERSIONEDFOLDER)
1920 svnCmd += _T("\" /wc");
1921 else
1922 svnCmd += _T("\"");
1924 else
1926 svnCmd = _T(" /patchpath:\"");
1927 if (files_.size() > 0)
1928 svnCmd += files_.front();
1929 else
1930 svnCmd += folder_;
1931 svnCmd += _T("\"");
1933 myIDMap.clear();
1934 myVerbsIDMap.clear();
1935 myVerbsMap.clear();
1936 if (CreateProcess(((stdstring)tortoiseMergePath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)
1938 LPVOID lpMsgBuf;
1939 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1940 FORMAT_MESSAGE_FROM_SYSTEM |
1941 FORMAT_MESSAGE_IGNORE_INSERTS,
1942 NULL,
1943 GetLastError(),
1944 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
1945 (LPTSTR) &lpMsgBuf,
1947 NULL
1949 MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseMerge launch failed"), MB_OK | MB_ICONINFORMATION );
1950 LocalFree( lpMsgBuf );
1952 CloseHandle(process.hThread);
1953 CloseHandle(process.hProcess);
1954 return NOERROR;
1955 break;
1956 case ShellMenuRevisionGraph:
1957 svnCmd += _T("revisiongraph /path:\"");
1958 if (files_.size() > 0)
1959 svnCmd += files_.front();
1960 else
1961 svnCmd += folder_;
1962 svnCmd += _T("\"");
1963 break;
1964 case ShellMenuProperties:
1965 tempfile = WriteFileListToTempFile();
1966 svnCmd += _T("properties /pathfile:\"");
1967 svnCmd += tempfile;
1968 svnCmd += _T("\"");
1969 svnCmd += _T(" /deletepathfile");
1970 break;
1971 case ShellMenuClipPaste:
1972 if (WriteClipboardPathsToTempFile(tempfile))
1974 bool bCopy = true;
1975 UINT cPrefDropFormat = RegisterClipboardFormat(_T("Preferred DropEffect"));
1976 if (cPrefDropFormat)
1978 if (OpenClipboard(lpcmi->hwnd))
1980 HGLOBAL hglb = GetClipboardData(cPrefDropFormat);
1981 if (hglb)
1983 DWORD* effect = (DWORD*) GlobalLock(hglb);
1984 if (*effect == DROPEFFECT_MOVE)
1985 bCopy = false;
1986 GlobalUnlock(hglb);
1988 CloseClipboard();
1992 if (bCopy)
1993 svnCmd += _T("pastecopy /pathfile:\"");
1994 else
1995 svnCmd += _T("pastemove /pathfile:\"");
1996 svnCmd += tempfile;
1997 svnCmd += _T("\"");
1998 svnCmd += _T(" /deletepathfile");
1999 svnCmd += _T(" /droptarget:\"");
2000 svnCmd += folder_;
2001 svnCmd += _T("\"");
2003 else return NOERROR;
2004 break;
2005 case ShellMenuClone:
2006 svnCmd += _T("clone /path:\"");
2007 svnCmd += folder_;
2008 svnCmd += _T("\"");
2009 break;
2010 case ShellMenuPull:
2011 svnCmd += _T("pull /path:\"");
2012 if (files_.size() > 0)
2013 svnCmd += files_.front();
2014 else
2015 svnCmd += folder_;
2016 svnCmd += _T("\"");
2017 break;
2018 case ShellMenuPush:
2019 svnCmd += _T("push /path:\"");
2020 if (files_.size() > 0)
2021 svnCmd += files_.front();
2022 else
2023 svnCmd += folder_;
2024 svnCmd += _T("\"");
2025 break;
2026 case ShellMenuBranch:
2027 svnCmd += _T("branch /path:\"");
2028 if (files_.size() > 0)
2029 svnCmd += files_.front();
2030 else
2031 svnCmd += folder_;
2032 svnCmd += _T("\"");
2033 break;
2035 case ShellMenuTag:
2036 svnCmd += _T("tag /path:\"");
2037 if (files_.size() > 0)
2038 svnCmd += files_.front();
2039 else
2040 svnCmd += folder_;
2041 svnCmd += _T("\"");
2042 break;
2044 case ShellMenuFormatPatch:
2045 svnCmd += _T("formatpatch /path:\"");
2046 if (files_.size() > 0)
2047 svnCmd += files_.front();
2048 else
2049 svnCmd += folder_;
2050 svnCmd += _T("\"");
2051 break;
2053 case ShellMenuImportPatch:
2054 svnCmd += _T("importpatch /path:\"");
2055 if (files_.size() > 0)
2056 svnCmd += files_.front();
2057 else
2058 svnCmd += folder_;
2059 svnCmd += _T("\"");
2060 break;
2062 case ShellMenuCherryPick:
2063 svnCmd += _T("cherrypick /path:\"");
2064 if (files_.size() > 0)
2065 svnCmd += files_.front();
2066 else
2067 svnCmd += folder_;
2068 svnCmd += _T("\"");
2069 break;
2070 case ShellMenuFetch:
2071 svnCmd += _T("fetch /path:\"");
2072 if (files_.size() > 0)
2073 svnCmd += files_.front();
2074 else
2075 svnCmd += folder_;
2076 svnCmd += _T("\"");
2077 break;
2079 default:
2080 break;
2081 //#endregion
2082 } // switch (id_it->second)
2083 svnCmd += _T(" /hwnd:");
2084 TCHAR buf[30];
2085 _stprintf_s(buf, 30, _T("%d"), lpcmi->hwnd);
2086 svnCmd += buf;
2087 myIDMap.clear();
2088 myVerbsIDMap.clear();
2089 myVerbsMap.clear();
2090 if (CreateProcess(((stdstring)tortoiseProcPath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)
2092 LPVOID lpMsgBuf;
2093 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
2094 FORMAT_MESSAGE_FROM_SYSTEM |
2095 FORMAT_MESSAGE_IGNORE_INSERTS,
2096 NULL,
2097 GetLastError(),
2098 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
2099 (LPTSTR) &lpMsgBuf,
2101 NULL
2103 MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseProc Launch failed"), MB_OK | MB_ICONINFORMATION );
2104 LocalFree( lpMsgBuf );
2106 CloseHandle(process.hThread);
2107 CloseHandle(process.hProcess);
2108 hr = NOERROR;
2109 } // if (id_it != myIDMap.end() && id_it->first == idCmd)
2110 } // if ((files_.size() > 0)||(folder_.size() > 0))
2111 return hr;
2115 // This is for the status bar and things like that:
2116 STDMETHODIMP CShellExt::GetCommandString(UINT_PTR idCmd,
2117 UINT uFlags,
2118 UINT FAR * /*reserved*/,
2119 LPSTR pszName,
2120 UINT cchMax)
2122 PreserveChdir preserveChdir;
2123 //do we know the id?
2124 std::map<UINT_PTR, UINT_PTR>::const_iterator id_it = myIDMap.lower_bound(idCmd);
2125 if (id_it == myIDMap.end() || id_it->first != idCmd)
2127 return E_INVALIDARG; //no, we don't
2130 LoadLangDll();
2131 HRESULT hr = E_INVALIDARG;
2133 MAKESTRING(IDS_MENUDESCDEFAULT);
2134 int menuIndex = 0;
2135 while (menuInfo[menuIndex].command != ShellMenuLastEntry)
2137 if (menuInfo[menuIndex].command == (GitCommands)id_it->second)
2139 MAKESTRING(menuInfo[menuIndex].menuDescID);
2140 break;
2142 menuIndex++;
2145 const TCHAR * desc = stringtablebuffer;
2146 switch(uFlags)
2148 case GCS_HELPTEXTA:
2150 std::string help = WideToMultibyte(desc);
2151 lstrcpynA(pszName, help.c_str(), cchMax);
2152 hr = S_OK;
2153 break;
2155 case GCS_HELPTEXTW:
2157 wide_string help = desc;
2158 lstrcpynW((LPWSTR)pszName, help.c_str(), cchMax);
2159 hr = S_OK;
2160 break;
2162 case GCS_VERBA:
2164 std::map<UINT_PTR, stdstring>::const_iterator verb_id_it = myVerbsIDMap.lower_bound(idCmd);
2165 if (verb_id_it != myVerbsIDMap.end() && verb_id_it->first == idCmd)
2167 std::string help = WideToMultibyte(verb_id_it->second);
2168 lstrcpynA(pszName, help.c_str(), cchMax);
2169 hr = S_OK;
2172 break;
2173 case GCS_VERBW:
2175 std::map<UINT_PTR, stdstring>::const_iterator verb_id_it = myVerbsIDMap.lower_bound(idCmd);
2176 if (verb_id_it != myVerbsIDMap.end() && verb_id_it->first == idCmd)
2178 wide_string help = verb_id_it->second;
2179 ATLTRACE("verb : %ws\n", help.c_str());
2180 lstrcpynW((LPWSTR)pszName, help.c_str(), cchMax);
2181 hr = S_OK;
2184 break;
2186 return hr;
2189 STDMETHODIMP CShellExt::HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)
2191 LRESULT res;
2192 return HandleMenuMsg2(uMsg, wParam, lParam, &res);
2195 STDMETHODIMP CShellExt::HandleMenuMsg2(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *pResult)
2197 PreserveChdir preserveChdir;
2199 LRESULT res;
2200 if (pResult == NULL)
2201 pResult = &res;
2202 *pResult = FALSE;
2204 LoadLangDll();
2205 switch (uMsg)
2207 case WM_MEASUREITEM:
2209 MEASUREITEMSTRUCT* lpmis = (MEASUREITEMSTRUCT*)lParam;
2210 if (lpmis==NULL||lpmis->CtlType!=ODT_MENU)
2211 break;
2212 lpmis->itemWidth += 2;
2213 if (lpmis->itemHeight < 16)
2214 lpmis->itemHeight = 16;
2215 *pResult = TRUE;
2217 break;
2218 case WM_DRAWITEM:
2220 LPCTSTR resource;
2221 DRAWITEMSTRUCT* lpdis = (DRAWITEMSTRUCT*)lParam;
2222 if ((lpdis==NULL)||(lpdis->CtlType != ODT_MENU))
2223 return S_OK; //not for a menu
2224 resource = GetMenuTextFromResource(myIDMap[lpdis->itemID]);
2225 if (resource == NULL)
2226 return S_OK;
2227 HICON hIcon = (HICON)LoadImage(g_hResInst, resource, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
2228 if (hIcon == NULL)
2229 return S_OK;
2230 DrawIconEx(lpdis->hDC,
2231 lpdis->rcItem.left - 16,
2232 lpdis->rcItem.top + (lpdis->rcItem.bottom - lpdis->rcItem.top - 16) / 2,
2233 hIcon, 16, 16,
2234 0, NULL, DI_NORMAL);
2235 DestroyIcon(hIcon);
2236 *pResult = TRUE;
2238 break;
2239 case WM_MENUCHAR:
2241 LPCTSTR resource;
2242 TCHAR *szItem;
2243 if (HIWORD(wParam) != MF_POPUP)
2244 return NOERROR;
2245 int nChar = LOWORD(wParam);
2246 if (_istascii((wint_t)nChar) && _istupper((wint_t)nChar))
2247 nChar = tolower(nChar);
2248 // we have the char the user pressed, now search that char in all our
2249 // menu items
2250 std::vector<int> accmenus;
2251 for (std::map<UINT_PTR, UINT_PTR>::iterator It = mySubMenuMap.begin(); It != mySubMenuMap.end(); ++It)
2253 resource = GetMenuTextFromResource(mySubMenuMap[It->first]);
2254 if (resource == NULL)
2255 continue;
2256 szItem = stringtablebuffer;
2257 TCHAR * amp = _tcschr(szItem, '&');
2258 if (amp == NULL)
2259 continue;
2260 amp++;
2261 int ampChar = LOWORD(*amp);
2262 if (_istascii((wint_t)ampChar) && _istupper((wint_t)ampChar))
2263 ampChar = tolower(ampChar);
2264 if (ampChar == nChar)
2266 // yep, we found a menu which has the pressed key
2267 // as an accelerator. Add that menu to the list to
2268 // process later.
2269 accmenus.push_back(It->first);
2272 if (accmenus.size() == 0)
2274 // no menu with that accelerator key.
2275 *pResult = MAKELONG(0, MNC_IGNORE);
2276 return NOERROR;
2278 if (accmenus.size() == 1)
2280 // Only one menu with that accelerator key. We're lucky!
2281 // So just execute that menu entry.
2282 *pResult = MAKELONG(accmenus[0], MNC_EXECUTE);
2283 return NOERROR;
2285 if (accmenus.size() > 1)
2287 // we have more than one menu item with this accelerator key!
2288 MENUITEMINFO mif;
2289 mif.cbSize = sizeof(MENUITEMINFO);
2290 mif.fMask = MIIM_STATE;
2291 for (std::vector<int>::iterator it = accmenus.begin(); it != accmenus.end(); ++it)
2293 GetMenuItemInfo((HMENU)lParam, *it, TRUE, &mif);
2294 if (mif.fState == MFS_HILITE)
2296 // this is the selected item, so select the next one
2297 ++it;
2298 if (it == accmenus.end())
2299 *pResult = MAKELONG(accmenus[0], MNC_SELECT);
2300 else
2301 *pResult = MAKELONG(*it, MNC_SELECT);
2302 return NOERROR;
2305 *pResult = MAKELONG(accmenus[0], MNC_SELECT);
2308 break;
2309 default:
2310 return NOERROR;
2313 return NOERROR;
2316 LPCTSTR CShellExt::GetMenuTextFromResource(int id)
2318 TCHAR textbuf[255];
2319 LPCTSTR resource = NULL;
2320 unsigned __int64 layout = g_ShellCache.GetMenuLayout();
2321 space = 6;
2323 int menuIndex = 0;
2324 while (menuInfo[menuIndex].command != ShellMenuLastEntry)
2326 if (menuInfo[menuIndex].command == id)
2328 MAKESTRING(menuInfo[menuIndex].menuTextID);
2329 resource = MAKEINTRESOURCE(menuInfo[menuIndex].iconID);
2330 switch (id)
2332 case ShellSubMenuMultiple:
2333 case ShellSubMenuLink:
2334 case ShellSubMenuFolder:
2335 case ShellSubMenuFile:
2336 case ShellSubMenu:
2337 space = 0;
2338 break;
2339 default:
2340 space = layout & menuInfo[menuIndex].menuID ? 0 : 6;
2341 if (layout & (menuInfo[menuIndex].menuID))
2343 _tcscpy_s(textbuf, 255, _T("Git "));
2344 _tcscat_s(textbuf, 255, stringtablebuffer);
2345 _tcscpy_s(stringtablebuffer, 255, textbuf);
2347 break;
2349 return resource;
2351 menuIndex++;
2353 return NULL;
2356 bool CShellExt::IsIllegalFolder(std::wstring folder, int * cslidarray)
2358 int i=0;
2359 TCHAR buf[MAX_PATH]; //MAX_PATH ok, since SHGetSpecialFolderPath doesn't return the required buffer length!
2360 LPITEMIDLIST pidl = NULL;
2361 while (cslidarray[i])
2363 ++i;
2364 pidl = NULL;
2365 if (SHGetFolderLocation(NULL, cslidarray[i-1], NULL, 0, &pidl)!=S_OK)
2366 continue;
2367 if (!SHGetPathFromIDList(pidl, buf))
2369 // not a file system path, definitely illegal for our use
2370 CoTaskMemFree(pidl);
2371 continue;
2373 CoTaskMemFree(pidl);
2374 if (_tcslen(buf)==0)
2375 continue;
2376 if (_tcscmp(buf, folder.c_str())==0)
2377 return true;
2379 return false;
2382 void CShellExt::InsertIgnoreSubmenus(UINT &idCmd, UINT idCmdFirst, HMENU hMenu, HMENU subMenu, UINT &indexMenu, int &indexSubMenu, unsigned __int64 topmenu, bool bShowIcons, UINT uFlags)
2384 HMENU ignoresubmenu = NULL;
2385 int indexignoresub = 0;
2386 bool bShowIgnoreMenu = false;
2387 TCHAR maskbuf[MAX_PATH]; // MAX_PATH is ok, since this only holds a filename
2388 TCHAR ignorepath[MAX_PATH]; // MAX_PATH is ok, since this only holds a filename
2389 if (files_.size() == 0)
2390 return;
2391 UINT icon = bShowIcons ? IDI_IGNORE : 0;
2393 std::vector<stdstring>::iterator I = files_.begin();
2394 if (_tcsrchr(I->c_str(), '\\'))
2395 _tcscpy_s(ignorepath, MAX_PATH, _tcsrchr(I->c_str(), '\\')+1);
2396 else
2397 _tcscpy_s(ignorepath, MAX_PATH, I->c_str());
2398 if ((itemStates & ITEMIS_IGNORED)&&(ignoredprops.size() > 0))
2400 // check if the item name is ignored or the mask
2401 size_t p = 0;
2402 while ( (p=ignoredprops.find( ignorepath,p )) != -1 )
2404 if ( (p==0 || ignoredprops[p-1]==TCHAR('\n'))
2405 && (p+_tcslen(ignorepath)==ignoredprops.length() || ignoredprops[p+_tcslen(ignorepath)+1]==TCHAR('\n')) )
2407 break;
2409 p++;
2411 if (p!=-1)
2413 ignoresubmenu = CreateMenu();
2414 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);
2415 stdstring verb = stdstring(ignorepath);
2416 myVerbsMap[verb] = idCmd - idCmdFirst;
2417 myVerbsMap[verb] = idCmd;
2418 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2419 myVerbsIDMap[idCmd] = verb;
2420 myIDMap[idCmd - idCmdFirst] = ShellMenuUnIgnore;
2421 myIDMap[idCmd++] = ShellMenuUnIgnore;
2422 bShowIgnoreMenu = true;
2424 _tcscpy_s(maskbuf, MAX_PATH, _T("*"));
2425 if (_tcsrchr(ignorepath, '.'))
2427 _tcscat_s(maskbuf, MAX_PATH, _tcsrchr(ignorepath, '.'));
2428 p = ignoredprops.find(maskbuf);
2429 if ((p!=-1) &&
2430 ((ignoredprops.compare(maskbuf)==0) || (ignoredprops.find('\n', p)==p+_tcslen(maskbuf)+1) || (ignoredprops.rfind('\n', p)==p-1)))
2432 if (ignoresubmenu==NULL)
2433 ignoresubmenu = CreateMenu();
2435 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, maskbuf);
2436 stdstring verb = stdstring(maskbuf);
2437 myVerbsMap[verb] = idCmd - idCmdFirst;
2438 myVerbsMap[verb] = idCmd;
2439 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2440 myVerbsIDMap[idCmd] = verb;
2441 myIDMap[idCmd - idCmdFirst] = ShellMenuUnIgnoreCaseSensitive;
2442 myIDMap[idCmd++] = ShellMenuUnIgnoreCaseSensitive;
2443 bShowIgnoreMenu = true;
2447 else if ((itemStates & ITEMIS_IGNORED) == 0)
2449 bShowIgnoreMenu = true;
2450 ignoresubmenu = CreateMenu();
2451 if (itemStates & ITEMIS_ONLYONE)
2453 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);
2454 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnore;
2455 myIDMap[idCmd++] = ShellMenuIgnore;
2457 _tcscpy_s(maskbuf, MAX_PATH, _T("*"));
2458 if (_tcsrchr(ignorepath, '.'))
2460 _tcscat_s(maskbuf, MAX_PATH, _tcsrchr(ignorepath, '.'));
2461 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, maskbuf);
2462 stdstring verb = stdstring(maskbuf);
2463 myVerbsMap[verb] = idCmd - idCmdFirst;
2464 myVerbsMap[verb] = idCmd;
2465 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2466 myVerbsIDMap[idCmd] = verb;
2467 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnoreCaseSensitive;
2468 myIDMap[idCmd++] = ShellMenuIgnoreCaseSensitive;
2471 else
2473 MAKESTRING(IDS_MENUIGNOREMULTIPLE);
2474 _stprintf_s(ignorepath, MAX_PATH, stringtablebuffer, files_.size());
2475 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);
2476 stdstring verb = stdstring(ignorepath);
2477 myVerbsMap[verb] = idCmd - idCmdFirst;
2478 myVerbsMap[verb] = idCmd;
2479 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2480 myVerbsIDMap[idCmd] = verb;
2481 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnore;
2482 myIDMap[idCmd++] = ShellMenuIgnore;
2484 MAKESTRING(IDS_MENUIGNOREMULTIPLEMASK);
2485 _stprintf_s(ignorepath, MAX_PATH, stringtablebuffer, files_.size());
2486 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);
2487 verb = stdstring(ignorepath);
2488 myVerbsMap[verb] = idCmd - idCmdFirst;
2489 myVerbsMap[verb] = idCmd;
2490 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2491 myVerbsIDMap[idCmd] = verb;
2492 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnoreCaseSensitive;
2493 myIDMap[idCmd++] = ShellMenuIgnoreCaseSensitive;
2497 if (bShowIgnoreMenu)
2499 MENUITEMINFO menuiteminfo;
2500 SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));
2501 menuiteminfo.cbSize = sizeof(menuiteminfo);
2502 if (fullver < 0x500 || (fullver == 0x500 && !(uFlags&~(CMF_RESERVED|CMF_EXPLORE))))
2504 menuiteminfo.fMask = MIIM_STRING | MIIM_ID | MIIM_SUBMENU | MIIM_DATA;
2505 if (icon)
2507 HBITMAP bmp = IconToBitmap(icon);
2508 menuiteminfo.fMask |= MIIM_CHECKMARKS;
2509 menuiteminfo.hbmpChecked = bmp;
2510 menuiteminfo.hbmpUnchecked = bmp;
2513 else
2515 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_SUBMENU | MIIM_DATA | MIIM_STRING;
2516 if (icon)
2518 menuiteminfo.fMask |= MIIM_BITMAP;
2519 menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(icon) : HBMMENU_CALLBACK;
2522 menuiteminfo.fType = MFT_STRING;
2523 menuiteminfo.hSubMenu = ignoresubmenu;
2524 menuiteminfo.wID = idCmd;
2525 SecureZeroMemory(stringtablebuffer, sizeof(stringtablebuffer));
2526 if (itemStates & ITEMIS_IGNORED)
2527 GetMenuTextFromResource(ShellMenuUnIgnoreSub);
2528 else
2529 GetMenuTextFromResource(ShellMenuIgnoreSub);
2530 menuiteminfo.dwTypeData = stringtablebuffer;
2531 menuiteminfo.cch = (UINT)min(_tcslen(menuiteminfo.dwTypeData), UINT_MAX);
2533 InsertMenuItem((topmenu & MENUIGNORE) ? hMenu : subMenu, (topmenu & MENUIGNORE) ? indexMenu++ : indexSubMenu++, TRUE, &menuiteminfo);
2534 if (itemStates & ITEMIS_IGNORED)
2536 myIDMap[idCmd - idCmdFirst] = ShellMenuUnIgnoreSub;
2537 myIDMap[idCmd++] = ShellMenuUnIgnoreSub;
2539 else
2541 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnoreSub;
2542 myIDMap[idCmd++] = ShellMenuIgnoreSub;
2547 HBITMAP CShellExt::IconToBitmapPARGB32(UINT uIcon)
2549 std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);
2550 if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)
2551 return bitmap_it->second;
2553 HICON hIcon = (HICON)LoadImage(g_hResInst, MAKEINTRESOURCE(uIcon), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
2554 if (!hIcon)
2555 return NULL;
2557 if (pfnBeginBufferedPaint == NULL || pfnEndBufferedPaint == NULL || pfnGetBufferedPaintBits == NULL)
2558 return NULL;
2560 SIZE sizIcon;
2561 sizIcon.cx = GetSystemMetrics(SM_CXSMICON);
2562 sizIcon.cy = GetSystemMetrics(SM_CYSMICON);
2564 RECT rcIcon;
2565 SetRect(&rcIcon, 0, 0, sizIcon.cx, sizIcon.cy);
2566 HBITMAP hBmp = NULL;
2568 HDC hdcDest = CreateCompatibleDC(NULL);
2569 if (hdcDest)
2571 if (SUCCEEDED(Create32BitHBITMAP(hdcDest, &sizIcon, NULL, &hBmp)))
2573 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcDest, hBmp);
2574 if (hbmpOld)
2576 BLENDFUNCTION bfAlpha = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2577 BP_PAINTPARAMS paintParams = {0};
2578 paintParams.cbSize = sizeof(paintParams);
2579 paintParams.dwFlags = BPPF_ERASE;
2580 paintParams.pBlendFunction = &bfAlpha;
2582 HDC hdcBuffer;
2583 HPAINTBUFFER hPaintBuffer = pfnBeginBufferedPaint(hdcDest, &rcIcon, BPBF_DIB, &paintParams, &hdcBuffer);
2584 if (hPaintBuffer)
2586 if (DrawIconEx(hdcBuffer, 0, 0, hIcon, sizIcon.cx, sizIcon.cy, 0, NULL, DI_NORMAL))
2588 // If icon did not have an alpha channel we need to convert buffer to PARGB
2589 ConvertBufferToPARGB32(hPaintBuffer, hdcDest, hIcon, sizIcon);
2592 // This will write the buffer contents to the destination bitmap
2593 pfnEndBufferedPaint(hPaintBuffer, TRUE);
2596 SelectObject(hdcDest, hbmpOld);
2600 DeleteDC(hdcDest);
2603 DestroyIcon(hIcon);
2605 if(hBmp)
2606 bitmaps.insert(bitmap_it, std::make_pair(uIcon, hBmp));
2607 return hBmp;
2610 HRESULT CShellExt::Create32BitHBITMAP(HDC hdc, const SIZE *psize, __deref_opt_out void **ppvBits, __out HBITMAP* phBmp)
2612 *phBmp = NULL;
2614 BITMAPINFO bmi;
2615 ZeroMemory(&bmi, sizeof(bmi));
2616 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2617 bmi.bmiHeader.biPlanes = 1;
2618 bmi.bmiHeader.biCompression = BI_RGB;
2620 bmi.bmiHeader.biWidth = psize->cx;
2621 bmi.bmiHeader.biHeight = psize->cy;
2622 bmi.bmiHeader.biBitCount = 32;
2624 HDC hdcUsed = hdc ? hdc : GetDC(NULL);
2625 if (hdcUsed)
2627 *phBmp = CreateDIBSection(hdcUsed, &bmi, DIB_RGB_COLORS, ppvBits, NULL, 0);
2628 if (hdc != hdcUsed)
2630 ReleaseDC(NULL, hdcUsed);
2633 return (NULL == *phBmp) ? E_OUTOFMEMORY : S_OK;
2636 HRESULT CShellExt::ConvertBufferToPARGB32(HPAINTBUFFER hPaintBuffer, HDC hdc, HICON hicon, SIZE& sizIcon)
2638 RGBQUAD *prgbQuad;
2639 int cxRow;
2640 HRESULT hr = pfnGetBufferedPaintBits(hPaintBuffer, &prgbQuad, &cxRow);
2641 if (SUCCEEDED(hr))
2643 ARGB *pargb = reinterpret_cast<ARGB *>(prgbQuad);
2644 if (!HasAlpha(pargb, sizIcon, cxRow))
2646 ICONINFO info;
2647 if (GetIconInfo(hicon, &info))
2649 if (info.hbmMask)
2651 hr = ConvertToPARGB32(hdc, pargb, info.hbmMask, sizIcon, cxRow);
2654 DeleteObject(info.hbmColor);
2655 DeleteObject(info.hbmMask);
2660 return hr;
2663 bool CShellExt::HasAlpha(__in ARGB *pargb, SIZE& sizImage, int cxRow)
2665 ULONG cxDelta = cxRow - sizImage.cx;
2666 for (ULONG y = sizImage.cy; y; --y)
2668 for (ULONG x = sizImage.cx; x; --x)
2670 if (*pargb++ & 0xFF000000)
2672 return true;
2676 pargb += cxDelta;
2679 return false;
2682 HRESULT CShellExt::ConvertToPARGB32(HDC hdc, __inout ARGB *pargb, HBITMAP hbmp, SIZE& sizImage, int cxRow)
2684 BITMAPINFO bmi;
2685 ZeroMemory(&bmi, sizeof(bmi));
2686 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2687 bmi.bmiHeader.biPlanes = 1;
2688 bmi.bmiHeader.biCompression = BI_RGB;
2690 bmi.bmiHeader.biWidth = sizImage.cx;
2691 bmi.bmiHeader.biHeight = sizImage.cy;
2692 bmi.bmiHeader.biBitCount = 32;
2694 HRESULT hr = E_OUTOFMEMORY;
2695 HANDLE hHeap = GetProcessHeap();
2696 void *pvBits = HeapAlloc(hHeap, 0, bmi.bmiHeader.biWidth * 4 * bmi.bmiHeader.biHeight);
2697 if (pvBits)
2699 hr = E_UNEXPECTED;
2700 if (GetDIBits(hdc, hbmp, 0, bmi.bmiHeader.biHeight, pvBits, &bmi, DIB_RGB_COLORS) == bmi.bmiHeader.biHeight)
2702 ULONG cxDelta = cxRow - bmi.bmiHeader.biWidth;
2703 ARGB *pargbMask = static_cast<ARGB *>(pvBits);
2705 for (ULONG y = bmi.bmiHeader.biHeight; y; --y)
2707 for (ULONG x = bmi.bmiHeader.biWidth; x; --x)
2709 if (*pargbMask++)
2711 // transparent pixel
2712 *pargb++ = 0;
2714 else
2716 // opaque pixel
2717 *pargb++ |= 0xFF000000;
2721 pargb += cxDelta;
2724 hr = S_OK;
2727 HeapFree(hHeap, 0, pvBits);
2730 return hr;