Fixed issue #151: need "stash pop" function and clear HasAdminDir HasGitSVN HashSubmo...
[TortoiseGit.git] / src / TortoiseShell / ContextMenu.cpp
blob3ce2c55e3efee536f1194695d65b65b0480be22a
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_MENUPUSH,
45 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
47 { ShellMenuSync, MENUSYNC, IDI_RELOCATE, IDS_MENUSYNC, IDS_MENUDESCSYNC,
48 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
51 // { ShellMenuCheckout, MENUCHECKOUT, IDI_CHECKOUT, IDS_MENUCHECKOUT, IDS_MENUDESCCHECKOUT,
52 // ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },
54 // { ShellMenuUpdate, MENUSUBUPDATE, IDI_UPDATE, IDS_MENUUPDATE, IDS_MENUDESCUPDATE,
55 // ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
57 { ShellSeparator, ITEMIS_GITSVN, 0, 0, 0, 0, 0, 0, 0},
59 { ShellMenuCommit, MENUCOMMIT, IDI_COMMIT, IDS_MENUCOMMIT, IDS_MENUDESCCOMMIT,
60 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
62 { ShellMenuGitSVNDCommit, MENUSVNDCOMMIT, IDI_COMMIT, IDS_MENUSVNDCOMMIT, IDS_MENUSVNDCOMMIT_DESC,
63 ITEMIS_INSVN|ITEMIS_GITSVN, 0, ITEMIS_FOLDERINSVN|ITEMIS_GITSVN, 0, 0, 0, 0, 0 },
65 { ShellMenuGitSVNRebase, MENUSVNREBASE, IDI_REBASE, IDS_MENUSVNREBASE, IDS_MENUSVNREBASE_DESC,
66 ITEMIS_INSVN|ITEMIS_GITSVN, 0, ITEMIS_FOLDERINSVN|ITEMIS_GITSVN, 0, 0, 0, 0, 0},
68 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
70 { ShellMenuDiff, MENUDIFF, IDI_DIFF, IDS_MENUDIFF, IDS_MENUDESCDIFF,
71 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_FOLDER|ITEMIS_NORMAL, ITEMIS_TWO, 0, 0, 0, 0, 0 },
73 { ShellMenuPrevDiff, MENUPREVDIFF, IDI_DIFF, IDS_MENUPREVDIFF, IDS_MENUDESCPREVDIFF,
74 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },
76 // { ShellMenuUrlDiff, MENUURLDIFF, IDI_DIFF, IDS_MENUURLDIFF, IDS_MENUDESCURLDIFF,
77 // ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, 0, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
79 { ShellMenuLog, MENULOG, IDI_LOG, IDS_MENULOG, IDS_MENUDESCLOG,
80 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, 0, 0 },
82 { ShellMenuRefLog, MENUREFLOG, IDI_LOG, IDS_MENUREFLOG, IDS_MENUDESCREFLOG,
83 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 },
85 { ShellMenuRefBrowse, MENUREFBROWSE, IDI_REPOBROWSE, IDS_MENUREFBROWSE, IDS_MENUDESCREFBROWSE,
86 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 },
89 // { ShellMenuRepoBrowse, MENUREPOBROWSE, IDI_REPOBROWSE, IDS_MENUREPOBROWSE, IDS_MENUDESCREPOBROWSE,
90 // ITEMIS_ONLYONE, 0, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
92 { ShellMenuShowChanged, MENUSHOWCHANGED, IDI_SHOWCHANGED, IDS_MENUSHOWCHANGED, IDS_MENUDESCSHOWCHANGED,
93 ITEMIS_INSVN|ITEMIS_ONLYONE, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0},
95 { ShellMenuRebase, MENUREBASE, IDI_REBASE, IDS_MENUREBASE, IDS_MENUREBASE,
96 ITEMIS_INSVN, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0},
98 // { ShellMenuRevisionGraph, MENUREVISIONGRAPH, IDI_REVISIONGRAPH, IDS_MENUREVISIONGRAPH, IDS_MENUDESCREVISIONGRAPH,
99 // ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, 0, 0, 0, 0},
101 { ShellMenuStashSave, MENUSTASHSAVE, IDI_COMMIT, IDS_MENUSTASHSAVE, IDS_MENUSTASHSAVE,
102 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
103 { ShellMenuStashApply, MENUSTASHAPPLY, IDI_RELOCATE, IDS_MENUSTASHAPPLY, IDS_MENUSTASHAPPLY,
104 ITEMIS_INSVN|ITEMIS_STASH, 0, ITEMIS_FOLDERINSVN|ITEMIS_STASH, 0, 0, 0, 0, 0 },
105 { ShellMenuStashPop, MENUSTASHPOP, IDI_RELOCATE, IDS_MENUSTASHPOP, IDS_MENUSTASHPOP,
106 ITEMIS_INSVN|ITEMIS_STASH, 0, ITEMIS_FOLDERINSVN|ITEMIS_STASH, 0, 0, 0, 0, 0 },
107 { ShellMenuStashList, MENUSTASHAPPLY, IDI_LOG, IDS_MENUSTASHLIST, IDS_MENUSTASHLIST,
108 ITEMIS_INSVN|ITEMIS_EXTENDED, 0, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
111 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
113 { ShellMenuConflictEditor, MENUCONFLICTEDITOR, IDI_CONFLICT, IDS_MENUCONFLICT, IDS_MENUDESCCONFLICT,
114 ITEMIS_INSVN|ITEMIS_CONFLICTED, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },
116 { ShellMenuResolve, MENURESOLVE, IDI_RESOLVE, IDS_MENURESOLVE, IDS_MENUDESCRESOLVE,
117 ITEMIS_INSVN|ITEMIS_CONFLICTED, 0, ITEMIS_INSVN|ITEMIS_FOLDER, 0, ITEMIS_FOLDERINSVN, 0, 0, 0 },
120 { ShellMenuRename, MENURENAME, IDI_RENAME, IDS_MENURENAME, IDS_MENUDESCRENAME,
121 ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_INVERSIONEDFOLDER, 0, 0, 0, 0, 0, 0, 0 },
123 { ShellMenuRemove, MENUREMOVE, IDI_DELETE, IDS_MENUREMOVE, IDS_MENUDESCREMOVE,
124 ITEMIS_INSVN|ITEMIS_INVERSIONEDFOLDER, ITEMIS_ADDED, 0, 0, 0, 0, 0, 0 },
126 { ShellMenuRemoveKeep, MENUREMOVE, IDI_DELETE, IDS_MENUREMOVEKEEP, IDS_MENUDESCREMOVEKEEP,
127 ITEMIS_INSVN|ITEMIS_INVERSIONEDFOLDER|ITEMIS_EXTENDED, ITEMIS_ADDED, 0, 0, 0, 0, 0, 0 },
129 { ShellMenuRevert, MENUREVERT, IDI_REVERT, IDS_MENUREVERT, IDS_MENUDESCREVERT,
130 ITEMIS_INSVN, ITEMIS_NORMAL|ITEMIS_ADDED, ITEMIS_FOLDERINSVN, ITEMIS_ADDED, 0, 0, 0, 0 },
132 { ShellMenuRevert, MENUREVERT, IDI_REVERT, IDS_MENUUNDOADD, IDS_MENUDESCUNDOADD,
133 ITEMIS_ADDED, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN|ITEMIS_ADDED, 0, 0, 0, 0, 0 },
135 { ShellMenuDelUnversioned, MENUDELUNVERSIONED, IDI_DELUNVERSIONED, IDS_MENUDELUNVERSIONED, IDS_MENUDESCDELUNVERSIONED,
136 ITEMIS_FOLDER|ITEMIS_INSVN|ITEMIS_EXTENDED, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
138 { ShellMenuCleanup, MENUCLEANUP, IDI_CLEANUP, IDS_MENUCLEANUP, IDS_MENUDESCCLEANUP,
139 ITEMIS_INSVN|ITEMIS_FOLDER, 0, ITEMIS_FOLDERINSVN|ITEMIS_FOLDER, 0, 0, 0, 0, 0 },
141 // { ShellMenuLock, MENULOCK, IDI_LOCK, IDS_MENU_LOCK, IDS_MENUDESC_LOCK,
142 // ITEMIS_INSVN, ITEMIS_LOCKED|ITEMIS_ADDED, ITEMIS_FOLDERINSVN, ITEMIS_LOCKED|ITEMIS_ADDED, 0, 0, 0, 0 },
144 // { ShellMenuUnlock, MENUUNLOCK, IDI_UNLOCK, IDS_MENU_UNLOCK, IDS_MENUDESC_UNLOCK,
145 // ITEMIS_INSVN|ITEMIS_LOCKED, 0, ITEMIS_FOLDER|ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0 },
147 // { ShellMenuUnlockForce, MENUUNLOCK, IDI_UNLOCK, IDS_MENU_UNLOCKFORCE, IDS_MENUDESC_UNLOCKFORCE,
148 // ITEMIS_INSVN|ITEMIS_LOCKED, 0, ITEMIS_FOLDER|ITEMIS_INSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
150 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
152 // { ShellMenuCopy, MENUCOPY, IDI_COPY, IDS_MENUBRANCH, IDS_MENUDESCCOPY,
153 // ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
155 { ShellMenuSwitch, MENUSWITCH, IDI_SWITCH, IDS_MENUSWITCH, IDS_MENUDESCSWITCH,
156 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
158 { ShellMenuMerge, MENUMERGE, IDI_MERGE, IDS_MENUMERGE, IDS_MENUDESCMERGE,
159 ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
160 // { ShellMenuMergeAll, MENUMERGEALL, IDI_MERGE, IDS_MENUMERGEALL, IDS_MENUDESCMERGEALL,
161 // ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
163 { ShellMenuBranch, MENUCOPY, IDI_COPY, IDS_MENUBRANCH, IDS_MENUDESCCOPY,
164 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
165 { ShellMenuTag, MENUTAG, IDI_TAG, IDS_MENUTAG, IDS_MENUDESCCOPY,
166 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
168 { ShellMenuExport, MENUEXPORT, IDI_EXPORT, IDS_MENUEXPORT, IDS_MENUDESCEXPORT,
169 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
171 // { ShellMenuRelocate, MENURELOCATE, IDI_RELOCATE, IDS_MENURELOCATE, IDS_MENUDESCRELOCATE,
172 // ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },
174 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
176 { ShellMenuCreateRepos, MENUCREATEREPOS, IDI_CREATEREPOS, IDS_MENUCREATEREPOS, IDS_MENUDESCCREATEREPOS,
177 ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },
179 { ShellMenuAdd, MENUADD, IDI_ADD, IDS_MENUADD, IDS_MENUDESCADD,
180 ITEMIS_INVERSIONEDFOLDER, ITEMIS_INSVN, ITEMIS_INSVN|ITEMIS_FOLDER, 0, ITEMIS_IGNORED, 0, ITEMIS_DELETED, ITEMIS_FOLDER|ITEMIS_ONLYONE },
182 // { ShellMenuAddAsReplacement, MENUADD, IDI_ADD, IDS_MENUADDASREPLACEMENT, IDS_MENUADDASREPLACEMENT,
183 // ITEMIS_DELETED|ITEMIS_ONLYONE, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },
185 // { ShellMenuImport, MENUIMPORT, IDI_IMPORT, IDS_MENUIMPORT, IDS_MENUDESCIMPORT,
186 // ITEMIS_FOLDER, ITEMIS_INSVN, 0, 0, 0, 0, 0, 0 },
188 { ShellMenuBlame, MENUBLAME, IDI_BLAME, IDS_MENUBLAME, IDS_MENUDESCBLAME,
189 ITEMIS_NORMAL|ITEMIS_ONLYONE, ITEMIS_FOLDER|ITEMIS_ADDED, 0, 0, 0, 0, 0, 0 },
190 // TODO: original code is ITEMIS_INSVN|ITEMIS_ONLYONE, makes sense to only allow blaming of versioned files
191 // why was this changed, is this related to GitStatus?
193 { ShellMenuIgnoreSub, MENUIGNORE, IDI_IGNORE, IDS_MENUIGNORE, IDS_MENUDESCIGNORE,
194 ITEMIS_INVERSIONEDFOLDER, ITEMIS_IGNORED|ITEMIS_INSVN, 0, 0, 0, 0, 0, 0 },
196 { ShellMenuUnIgnoreSub, MENUIGNORE, IDI_IGNORE, IDS_MENUUNIGNORE, IDS_MENUDESCUNIGNORE,
197 ITEMIS_IGNORED, 0, 0, 0, 0, 0, 0, 0 },
199 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
201 { ShellMenuSubAdd, MENUSUBADD, IDI_ADD, IDS_MENUSUBADD, IDS_MENUSUBADD,
202 ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
204 { ShellMenuUpdateExt, MENUUPDATEEXT, IDI_UPDATE, IDS_MENUUPDATEEXT, IDS_MENUDESCUPDATEEXT,
205 ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_SUBMODULE, 0, 0, 0, 0, 0, 0, 0 },
207 { ShellMenuSubSync, MENUSUBSYNC, IDI_MENUSYNC, IDS_MENUSUBSYNC, IDS_MENUSUBSYNC,
208 ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_SUBMODULE|ITEMIS_EXTENDED, 0, 0, 0, 0, 0, 0, 0 },
212 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
214 // { ShellMenuCherryPick, MENUCHERRYPICK, IDI_CREATEPATCH, IDS_MENUCHERRYPICK, IDS_MENUDESCCREATEPATCH,
215 // ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
217 { ShellMenuFormatPatch, MENUFORMATPATCH, IDI_CREATEPATCH, IDS_MENUFORMATPATCH, IDS_MENUDESCCREATEPATCH,
218 ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
220 { ShellMenuImportPatch, MENUIMPORTPATCH, IDI_PATCH, IDS_MENUIMPORTPATCH, IDS_MENUDESCCREATEPATCH,
221 ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, ITEMIS_PATCHFILE, 0, 0, 0 },
224 { ShellMenuCreatePatch, MENUCREATEPATCH, IDI_CREATEPATCH, IDS_MENUCREATEPATCH, IDS_MENUDESCCREATEPATCH,
225 ITEMIS_INSVN|ITEMIS_EXTENDED, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },
227 { ShellMenuApplyPatch, MENUAPPLYPATCH, IDI_PATCH, IDS_MENUAPPLYPATCH, IDS_MENUDESCAPPLYPATCH,
228 ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, ITEMIS_ADDED, ITEMIS_ONLYONE|ITEMIS_PATCHFILE, 0, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, ITEMIS_ADDED, 0, 0 },
230 { ShellMenuSendMail, MENUSENDMAIL, IDI_MENUSENDMAIL, IDS_MENUSENDMAIL, IDS_MENUDESSENDMAIL,
231 ITEMIS_PATCHFILE, 0, 0, 0, 0, 0, 0, 0 },
233 // { ShellMenuProperties, MENUPROPERTIES, IDI_PROPERTIES, IDS_MENUPROPERTIES, IDS_MENUDESCPROPERTIES,
234 // ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },
236 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
237 // { ShellMenuClipPaste, MENUCLIPPASTE, IDI_CLIPPASTE, IDS_MENUCLIPPASTE, IDS_MENUDESCCLIPPASTE,
238 // ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_PATHINCLIPBOARD, 0, 0, 0, 0, 0, 0, 0 },
240 { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},
242 { ShellMenuSettings, MENUSETTINGS, IDI_SETTINGS, IDS_MENUSETTINGS, IDS_MENUDESCSETTINGS,
243 ITEMIS_FOLDER, 0, 0, ITEMIS_FOLDER, 0, 0, 0, 0 },
244 { ShellMenuHelp, MENUHELP, IDI_HELP, IDS_MENUHELP, IDS_MENUDESCHELP,
245 ITEMIS_FOLDER, 0, 0, ITEMIS_FOLDER, 0, 0, 0, 0 },
246 { ShellMenuAbout, MENUABOUT, IDI_ABOUT, IDS_MENUABOUT, IDS_MENUDESCABOUT,
247 ITEMIS_FOLDER, 0, 0, ITEMIS_FOLDER, 0, 0, 0, 0 },
249 // the sub menus - they're not added like the the commands, therefore the menu ID is zero
250 // but they still need to be in here, because we use the icon and string information anyway.
251 { ShellSubMenu, NULL, IDI_APP, IDS_MENUSUBMENU, 0,
252 0, 0, 0, 0, 0, 0, 0, 0 },
253 { ShellSubMenuFile, NULL, IDI_MENUFILE, IDS_MENUSUBMENU, 0,
254 0, 0, 0, 0, 0, 0, 0, 0 },
255 { ShellSubMenuFolder, NULL, IDI_MENUFOLDER, IDS_MENUSUBMENU, 0,
256 0, 0, 0, 0, 0, 0, 0, 0 },
257 { ShellSubMenuLink, NULL, IDI_MENULINK, IDS_MENUSUBMENU, 0,
258 0, 0, 0, 0, 0, 0, 0, 0 },
259 { ShellSubMenuMultiple, NULL, IDI_MENUMULTIPLE, IDS_MENUSUBMENU, 0,
260 0, 0, 0, 0, 0, 0, 0, 0 },
261 // mark the last entry to tell the loop where to stop iterating over this array
262 { ShellMenuLastEntry, 0, 0, 0, 0,
263 0, 0, 0, 0, 0, 0, 0, 0 },
267 STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST pIDFolder,
268 LPDATAOBJECT pDataObj,
269 HKEY /* hRegKey */)
272 ATLTRACE("Shell :: Initialize\n");
273 PreserveChdir preserveChdir;
274 files_.clear();
275 folder_.erase();
276 uuidSource.erase();
277 uuidTarget.erase();
278 itemStates = 0;
279 itemStatesFolder = 0;
280 stdstring statuspath;
281 git_wc_status_kind fetchedstatus = git_wc_status_none;
282 // get selected files/folders
283 if (pDataObj)
285 STGMEDIUM medium;
286 FORMATETC fmte = {(CLIPFORMAT)g_shellidlist,
287 (DVTARGETDEVICE FAR *)NULL,
288 DVASPECT_CONTENT,
289 -1,
290 TYMED_HGLOBAL};
291 HRESULT hres = pDataObj->GetData(&fmte, &medium);
293 if (SUCCEEDED(hres) && medium.hGlobal)
295 if (m_State == FileStateDropHandler)
298 FORMATETC etc = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
299 STGMEDIUM stg = { TYMED_HGLOBAL };
300 if ( FAILED( pDataObj->GetData ( &etc, &stg )))
302 ReleaseStgMedium ( &medium );
303 return E_INVALIDARG;
307 HDROP drop = (HDROP)GlobalLock(stg.hGlobal);
308 if ( NULL == drop )
310 ReleaseStgMedium ( &stg );
311 ReleaseStgMedium ( &medium );
312 return E_INVALIDARG;
315 int count = DragQueryFile(drop, (UINT)-1, NULL, 0);
316 if (count == 1)
317 itemStates |= ITEMIS_ONLYONE;
318 for (int i = 0; i < count; i++)
320 // find the path length in chars
321 UINT len = DragQueryFile(drop, i, NULL, 0);
322 if (len == 0)
323 continue;
324 TCHAR * szFileName = new TCHAR[len+1];
325 if (0 == DragQueryFile(drop, i, szFileName, len+1))
327 delete [] szFileName;
328 continue;
330 stdstring str = stdstring(szFileName);
331 delete [] szFileName;
332 if ((str.empty() == false)&&(g_ShellCache.IsContextPathAllowed(szFileName)))
334 if (itemStates & ITEMIS_ONLYONE)
336 CTGitPath strpath;
337 strpath.SetFromWin(str.c_str());
338 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".diff"))==0) ? ITEMIS_PATCHFILE : 0;
339 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".patch"))==0) ? ITEMIS_PATCHFILE : 0;
341 files_.push_back(str);
342 if (i == 0)
344 //get the Subversion status of the item
345 git_wc_status_kind status = git_wc_status_none;
346 CTGitPath askedpath;
347 askedpath.SetFromWin(str.c_str());
350 GitStatus stat;
351 stat.GetStatus(CTGitPath(str.c_str()), false, true, true);
352 if (stat.status)
354 statuspath = str;
355 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);
356 fetchedstatus = status;
357 //if ((stat.status->entry)&&(stat.status->entry->lock_token))
358 // itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;
359 if ( askedpath.IsDirectory() )//if ((stat.status->entry)&&(stat.status->entry->kind == git_node_dir))
361 itemStates |= ITEMIS_FOLDER;
362 if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
363 itemStates |= ITEMIS_FOLDERINSVN;
365 //if ((stat.status->entry)&&(stat.status->entry->present_props))
367 // if (strstr(stat.status->entry->present_props, "svn:needs-lock"))
368 // itemStates |= ITEMIS_NEEDSLOCK;
370 //if ((stat.status->entry)&&(stat.status->entry->uuid))
371 // uuidSource = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);
373 else
375 // sometimes, git_client_status() returns with an error.
376 // in that case, we have to check if the working copy is versioned
377 // anyway to show the 'correct' context menu
378 if (askedpath.HasAdminDir())
379 status = git_wc_status_normal;
382 catch ( ... )
384 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));
387 // TODO: should we really assume any sub-directory to be versioned
388 // or only if it contains versioned files
389 itemStates |= askedpath.GetAdminDirMask();
391 if ((status == git_wc_status_unversioned) || (status == git_wc_status_ignored) || (status == git_wc_status_none))
392 itemStates &= ~ITEMIS_INSVN;
394 if (status == git_wc_status_ignored)
395 itemStates |= ITEMIS_IGNORED;
396 if (status == git_wc_status_normal)
397 itemStates |= ITEMIS_NORMAL;
398 if (status == git_wc_status_conflicted)
399 itemStates |= ITEMIS_CONFLICTED;
400 if (status == git_wc_status_added)
401 itemStates |= ITEMIS_ADDED;
402 if (status == git_wc_status_deleted)
403 itemStates |= ITEMIS_DELETED;
406 } // for (int i = 0; i < count; i++)
407 GlobalUnlock ( drop );
408 ReleaseStgMedium ( &stg );
410 } // if (m_State == FileStateDropHandler)
411 else
414 //Enumerate PIDLs which the user has selected
415 CIDA* cida = (CIDA*)GlobalLock(medium.hGlobal);
416 ItemIDList parent( GetPIDLFolder (cida));
418 int count = cida->cidl;
419 BOOL statfetched = FALSE;
420 for (int i = 0; i < count; ++i)
422 ItemIDList child (GetPIDLItem (cida, i), &parent);
423 stdstring str = child.toString();
424 if ((str.empty() == false)&&(g_ShellCache.IsContextPathAllowed(str.c_str())))
426 //check if our menu is requested for a subversion admin directory
427 if (g_GitAdminDir.IsAdminDirPath(str.c_str()))
428 continue;
430 files_.push_back(str);
431 CTGitPath strpath;
432 strpath.SetFromWin(str.c_str());
433 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".diff"))==0) ? ITEMIS_PATCHFILE : 0;
434 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".patch"))==0) ? ITEMIS_PATCHFILE : 0;
435 if (!statfetched)
437 //get the Subversion status of the item
438 git_wc_status_kind status = git_wc_status_none;
439 if ((g_ShellCache.IsSimpleContext())&&(strpath.IsDirectory()))
441 if (strpath.HasAdminDir())
442 status = git_wc_status_normal;
444 else
448 GitStatus stat;
449 if (strpath.HasAdminDir())
450 stat.GetStatus(strpath, false, true, true);
451 statuspath = str;
452 if (stat.status)
454 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);
455 fetchedstatus = status;
456 //if ((stat.status->entry)&&(stat.status->entry->lock_token))
457 // itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;
458 if ( strpath.IsDirectory() )//if ((stat.status->entry)&&(stat.status->entry->kind == git_node_dir))
460 itemStates |= ITEMIS_FOLDER;
461 if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
462 itemStates |= ITEMIS_FOLDERINSVN;
464 // TODO: do we need to check that it's not a dir? does conflict options makes sense for dir in git?
465 if (status == git_wc_status_conflicted)//if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))
466 itemStates |= ITEMIS_CONFLICTED;
467 //if ((stat.status->entry)&&(stat.status->entry->present_props))
469 // if (strstr(stat.status->entry->present_props, "svn:needs-lock"))
470 // itemStates |= ITEMIS_NEEDSLOCK;
472 //if ((stat.status->entry)&&(stat.status->entry->uuid))
473 // uuidSource = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);
475 else
477 // sometimes, git_client_status() returns with an error.
478 // in that case, we have to check if the working copy is versioned
479 // anyway to show the 'correct' context menu
480 if (strpath.HasAdminDir())
482 status = git_wc_status_normal;
483 fetchedstatus = status;
486 statfetched = TRUE;
488 catch ( ... )
490 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));
494 itemStates |= strpath.GetAdminDirMask();
496 if ((status == git_wc_status_unversioned)||(status == git_wc_status_ignored)||(status == git_wc_status_none))
497 itemStates &= ~ITEMIS_INSVN;
498 if (status == git_wc_status_ignored)
500 itemStates |= ITEMIS_IGNORED;
501 // the item is ignored. Get the svn:ignored properties so we can (maybe) later
502 // offer a 'remove from ignored list' entry
503 // GitProperties props(strpath.GetContainingDirectory(), false);
504 // ignoredprops.empty();
505 // for (int p=0; p<props.GetCount(); ++p)
506 // {
507 // if (props.GetItemName(p).compare(stdstring(_T("svn:ignore")))==0)
508 // {
509 // std::string st = props.GetItemValue(p);
510 // ignoredprops = MultibyteToWide(st.c_str());
511 // // remove all escape chars ('\\')
512 // std::remove(ignoredprops.begin(), ignoredprops.end(), '\\');
513 // break;
514 // }
515 // }
518 if (status == git_wc_status_normal)
519 itemStates |= ITEMIS_NORMAL;
520 if (status == git_wc_status_conflicted)
521 itemStates |= ITEMIS_CONFLICTED;
522 if (status == git_wc_status_added)
523 itemStates |= ITEMIS_ADDED;
524 if (status == git_wc_status_deleted)
525 itemStates |= ITEMIS_DELETED;
528 } // for (int i = 0; i < count; ++i)
529 ItemIDList child (GetPIDLItem (cida, 0), &parent);
530 if (g_ShellCache.HasSVNAdminDir(child.toString().c_str(), FALSE))
531 itemStates |= ITEMIS_INVERSIONEDFOLDER;
532 GlobalUnlock(medium.hGlobal);
534 // if the item is a versioned folder, check if there's a patch file
535 // in the clipboard to be used in "Apply Patch"
536 UINT cFormatDiff = RegisterClipboardFormat(_T("TGIT_UNIFIEDDIFF"));
537 if (cFormatDiff)
539 if (IsClipboardFormatAvailable(cFormatDiff))
540 itemStates |= ITEMIS_PATCHINCLIPBOARD;
542 if (IsClipboardFormatAvailable(CF_HDROP))
543 itemStates |= ITEMIS_PATHINCLIPBOARD;
547 ReleaseStgMedium ( &medium );
548 if (medium.pUnkForRelease)
550 IUnknown* relInterface = (IUnknown*)medium.pUnkForRelease;
551 relInterface->Release();
556 // get folder background
557 if (pIDFolder)
560 ItemIDList list(pIDFolder);
561 folder_ = list.toString();
562 git_wc_status_kind status = git_wc_status_none;
563 if (IsClipboardFormatAvailable(CF_HDROP))
564 itemStatesFolder |= ITEMIS_PATHINCLIPBOARD;
566 CTGitPath askedpath;
567 askedpath.SetFromWin(folder_.c_str());
569 if ((folder_.compare(statuspath)!=0)&&(g_ShellCache.IsContextPathAllowed(folder_.c_str())))
574 GitStatus stat;
575 stat.GetStatus(CTGitPath(folder_.c_str()), false, true, true);
576 if (stat.status)
578 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);
579 // if ((stat.status->entry)&&(stat.status->entry->lock_token))
580 // itemStatesFolder |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;
581 // if ((stat.status->entry)&&(stat.status->entry->present_props))
582 // {
583 // if (strstr(stat.status->entry->present_props, "svn:needs-lock"))
584 // itemStatesFolder |= ITEMIS_NEEDSLOCK;
585 // }
586 // if ((stat.status->entry)&&(stat.status->entry->uuid))
587 // uuidTarget = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);
590 else
592 // sometimes, git_client_status() returns with an error.
593 // in that case, we have to check if the working copy is versioned
594 // anyway to show the 'correct' context menu
595 if (askedpath.HasAdminDir())
596 status = git_wc_status_normal;
599 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
600 itemStatesFolder |= askedpath.GetAdminDirMask();
602 if (status == git_wc_status_normal)
603 itemStatesFolder |= ITEMIS_NORMAL;
604 if (status == git_wc_status_conflicted)
605 itemStatesFolder |= ITEMIS_CONFLICTED;
606 if (status == git_wc_status_added)
607 itemStatesFolder |= ITEMIS_ADDED;
608 if (status == git_wc_status_deleted)
609 itemStatesFolder |= ITEMIS_DELETED;
612 catch ( ... )
614 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));
617 else
619 status = fetchedstatus;
621 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
622 itemStatesFolder |= askedpath.GetAdminDirMask();
624 if (status == git_wc_status_ignored)
625 itemStatesFolder |= ITEMIS_IGNORED;
626 itemStatesFolder |= ITEMIS_FOLDER;
627 if (files_.size() == 0)
628 itemStates |= ITEMIS_ONLYONE;
629 if (m_State != FileStateDropHandler)
630 itemStates |= itemStatesFolder;
634 if (files_.size() == 2)
635 itemStates |= ITEMIS_TWO;
636 if ((files_.size() == 1)&&(g_ShellCache.IsContextPathAllowed(files_.front().c_str())))
639 itemStates |= ITEMIS_ONLYONE;
640 if (m_State != FileStateDropHandler)
642 if (PathIsDirectory(files_.front().c_str()))
644 folder_ = files_.front();
645 git_wc_status_kind status = git_wc_status_none;
646 CTGitPath askedpath;
647 askedpath.SetFromWin(folder_.c_str());
649 if (folder_.compare(statuspath)!=0)
653 GitStatus stat;
654 stat.GetStatus(CTGitPath(folder_.c_str()), false, true, true);
655 if (stat.status)
657 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);
658 // if ((stat.status->entry)&&(stat.status->entry->lock_token))
659 // itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;
660 // if ((stat.status->entry)&&(stat.status->entry->present_props))
661 // {
662 // if (strstr(stat.status->entry->present_props, "svn:needs-lock"))
663 // itemStates |= ITEMIS_NEEDSLOCK;
664 // }
665 // if ((stat.status->entry)&&(stat.status->entry->uuid))
666 // uuidTarget = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);
669 catch ( ... )
671 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));
674 else
676 status = fetchedstatus;
678 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))
679 itemStates |= askedpath.GetAdminDirMask();
681 if (status == git_wc_status_ignored)
682 itemStates |= ITEMIS_IGNORED;
683 itemStates |= ITEMIS_FOLDER;
684 if (status == git_wc_status_added)
685 itemStates |= ITEMIS_ADDED;
686 if (status == git_wc_status_deleted)
687 itemStates |= ITEMIS_DELETED;
694 return NOERROR;
697 void CShellExt::InsertGitMenu(BOOL istop, HMENU menu, UINT pos, UINT_PTR id, UINT stringid, UINT icon, UINT idCmdFirst, GitCommands com, UINT uFlags)
699 TCHAR menutextbuffer[512] = {0};
700 TCHAR verbsbuffer[255] = {0};
701 MAKESTRING(stringid);
703 if (istop)
705 //menu entry for the top context menu, so append an "Git " before
706 //the menu text to indicate where the entry comes from
707 _tcscpy_s(menutextbuffer, 255, _T("Git "));
709 _tcscat_s(menutextbuffer, 255, stringtablebuffer);
710 #if 1
711 // insert branch name into "Git Commit..." entry, so it looks like "Git Commit "master"..."
712 // so we have an easy and fast way to check the current branch
713 // (the other alternative is using a separate disabled menu entry, the code is already done but commented out)
714 if (com == ShellMenuCommit)
716 // get branch name
717 CTGitPath path(folder_.empty() ? files_.front().c_str() : folder_.c_str());
718 CString sProjectRoot;
719 CString sBranchName;
721 if (path.HasAdminDir(&sProjectRoot) && !g_Git.GetCurrentBranchFromFile(sProjectRoot, sBranchName))
723 if (sBranchName.GetLength() == 40)
725 // if SHA1 only show 4 first bytes
726 BOOL bIsSha1 = TRUE;
727 for (int i=0; i<40; i++)
728 if ( !iswxdigit(sBranchName[i]) )
730 bIsSha1 = FALSE;
731 break;
733 if (bIsSha1)
734 sBranchName = sBranchName.Left(8) + _T("....");
737 // sanity check
738 if (sBranchName.GetLength() > 64)
739 sBranchName = sBranchName.Left(64) + _T("...");
741 // scan to before "..."
742 LPTSTR s = menutextbuffer + _tcslen(menutextbuffer)-1;
743 if (s > menutextbuffer)
745 while (s > menutextbuffer)
747 if (*s != _T('.'))
749 s++;
750 break;
752 s--;
755 else
757 s = menutextbuffer;
760 // append branch name and end with ...
761 _tcscpy(s, _T(" -> \"") + sBranchName + _T("\"..."));
764 #endif
765 if ((fullver < 0x500)||(fullver == 0x500 && !(uFlags&~(CMF_RESERVED|CMF_EXPLORE|CMF_EXTENDEDVERBS))))
767 // on win2k, the context menu does not work properly if we use
768 // icon bitmaps. At least the menu text is empty in the context menu
769 // for folder backgrounds (seems like a win2k bug).
770 // the workaround is to use the check/unchecked bitmaps, which are drawn
771 // with AND raster op, but it's better than nothing at all
772 InsertMenu(menu, pos, MF_BYPOSITION | MF_STRING , id, menutextbuffer);
773 if (icon)
775 HBITMAP bmp = IconToBitmap(icon);
776 SetMenuItemBitmaps(menu, pos, MF_BYPOSITION, bmp, bmp);
779 else
781 MENUITEMINFO menuiteminfo;
782 SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));
783 menuiteminfo.cbSize = sizeof(menuiteminfo);
784 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
785 menuiteminfo.fType = MFT_STRING;
786 menuiteminfo.dwTypeData = menutextbuffer;
787 if (icon)
789 menuiteminfo.fMask |= MIIM_BITMAP;
790 menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(icon) : HBMMENU_CALLBACK;
792 menuiteminfo.wID = id;
793 InsertMenuItem(menu, pos, TRUE, &menuiteminfo);
796 if (istop)
798 //menu entry for the top context menu, so append an "Git " before
799 //the menu text to indicate where the entry comes from
800 _tcscpy_s(menutextbuffer, 255, _T("Git "));
802 LoadString(g_hResInst, stringid, verbsbuffer, sizeof(verbsbuffer));
803 _tcscat_s(menutextbuffer, 255, verbsbuffer);
804 stdstring verb = stdstring(menutextbuffer);
805 if (verb.find('&') != -1)
807 verb.erase(verb.find('&'),1);
809 myVerbsMap[verb] = id - idCmdFirst;
810 myVerbsMap[verb] = id;
811 myVerbsIDMap[id - idCmdFirst] = verb;
812 myVerbsIDMap[id] = verb;
813 // We store the relative and absolute diameter
814 // (drawitem callback uses absolute, others relative)
815 myIDMap[id - idCmdFirst] = com;
816 myIDMap[id] = com;
817 if (!istop)
818 mySubMenuMap[pos] = com;
821 HBITMAP CShellExt::IconToBitmap(UINT uIcon)
823 std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);
824 if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)
825 return bitmap_it->second;
827 HICON hIcon = (HICON)LoadImage(g_hResInst, MAKEINTRESOURCE(uIcon), IMAGE_ICON, 12, 12, LR_DEFAULTCOLOR);
828 if (!hIcon)
829 return NULL;
831 RECT rect;
833 rect.right = ::GetSystemMetrics(SM_CXMENUCHECK);
834 rect.bottom = ::GetSystemMetrics(SM_CYMENUCHECK);
836 rect.left = rect.top = 0;
838 HWND desktop = ::GetDesktopWindow();
839 if (desktop == NULL)
841 DestroyIcon(hIcon);
842 return NULL;
845 HDC screen_dev = ::GetDC(desktop);
846 if (screen_dev == NULL)
848 DestroyIcon(hIcon);
849 return NULL;
852 // Create a compatible DC
853 HDC dst_hdc = ::CreateCompatibleDC(screen_dev);
854 if (dst_hdc == NULL)
856 DestroyIcon(hIcon);
857 ::ReleaseDC(desktop, screen_dev);
858 return NULL;
861 // Create a new bitmap of icon size
862 HBITMAP bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);
863 if (bmp == NULL)
865 DestroyIcon(hIcon);
866 ::DeleteDC(dst_hdc);
867 ::ReleaseDC(desktop, screen_dev);
868 return NULL;
871 // Select it into the compatible DC
872 HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);
873 if (old_dst_bmp == NULL)
875 DestroyIcon(hIcon);
876 return NULL;
879 // Fill the background of the compatible DC with the white color
880 // that is taken by menu routines as transparent
881 ::SetBkColor(dst_hdc, RGB(255, 255, 255));
882 ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
884 // Draw the icon into the compatible DC
885 ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);
887 // Restore settings
888 ::SelectObject(dst_hdc, old_dst_bmp);
889 ::DeleteDC(dst_hdc);
890 ::ReleaseDC(desktop, screen_dev);
891 DestroyIcon(hIcon);
892 if (bmp)
893 bitmaps.insert(bitmap_it, std::make_pair(uIcon, bmp));
894 return bmp;
897 bool CShellExt::WriteClipboardPathsToTempFile(stdstring& tempfile)
899 bool bRet = true;
900 tempfile = stdstring();
901 //write all selected files and paths to a temporary file
902 //for TortoiseProc.exe to read out again.
903 DWORD written = 0;
904 DWORD pathlength = GetTempPath(0, NULL);
905 TCHAR * path = new TCHAR[pathlength+1];
906 TCHAR * tempFile = new TCHAR[pathlength + 100];
907 GetTempPath (pathlength+1, path);
908 GetTempFileName (path, _T("git"), 0, tempFile);
909 tempfile = stdstring(tempFile);
911 HANDLE file = ::CreateFile (tempFile,
912 GENERIC_WRITE,
913 FILE_SHARE_READ,
915 CREATE_ALWAYS,
916 FILE_ATTRIBUTE_TEMPORARY,
919 delete [] path;
920 delete [] tempFile;
921 if (file == INVALID_HANDLE_VALUE)
922 return false;
924 if (!IsClipboardFormatAvailable(CF_HDROP))
925 return false;
926 if (!OpenClipboard(NULL))
927 return false;
929 stdstring sClipboardText;
930 HGLOBAL hglb = GetClipboardData(CF_HDROP);
931 HDROP hDrop = (HDROP)GlobalLock(hglb);
932 if(hDrop != NULL)
934 TCHAR szFileName[MAX_PATH];
935 UINT cFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
936 for(UINT i = 0; i < cFiles; ++i)
938 DragQueryFile(hDrop, i, szFileName, sizeof(szFileName));
939 stdstring filename = szFileName;
940 ::WriteFile (file, filename.c_str(), filename.size()*sizeof(TCHAR), &written, 0);
941 ::WriteFile (file, _T("\n"), 2, &written, 0);
943 GlobalUnlock(hDrop);
945 else bRet = false;
946 GlobalUnlock(hglb);
948 CloseClipboard();
949 ::CloseHandle(file);
951 return bRet;
954 stdstring CShellExt::WriteFileListToTempFile()
956 //write all selected files and paths to a temporary file
957 //for TortoiseProc.exe to read out again.
958 DWORD pathlength = GetTempPath(0, NULL);
959 TCHAR * path = new TCHAR[pathlength+1];
960 TCHAR * tempFile = new TCHAR[pathlength + 100];
961 GetTempPath (pathlength+1, path);
962 GetTempFileName (path, _T("git"), 0, tempFile);
963 stdstring retFilePath = stdstring(tempFile);
965 HANDLE file = ::CreateFile (tempFile,
966 GENERIC_WRITE,
967 FILE_SHARE_READ,
969 CREATE_ALWAYS,
970 FILE_ATTRIBUTE_TEMPORARY,
973 delete [] path;
974 delete [] tempFile;
975 if (file == INVALID_HANDLE_VALUE)
976 return stdstring();
978 DWORD written = 0;
979 if (files_.empty())
981 ::WriteFile (file, folder_.c_str(), folder_.size()*sizeof(TCHAR), &written, 0);
982 ::WriteFile (file, _T("\n"), 2, &written, 0);
985 for (std::vector<stdstring>::iterator I = files_.begin(); I != files_.end(); ++I)
987 ::WriteFile (file, I->c_str(), I->size()*sizeof(TCHAR), &written, 0);
988 ::WriteFile (file, _T("\n"), 2, &written, 0);
990 ::CloseHandle(file);
991 return retFilePath;
994 STDMETHODIMP CShellExt::QueryDropContext(UINT uFlags, UINT idCmdFirst, HMENU hMenu, UINT &indexMenu)
996 PreserveChdir preserveChdir;
997 LoadLangDll();
999 if ((uFlags & CMF_DEFAULTONLY)!=0)
1000 return NOERROR; //we don't change the default action
1002 if ((files_.size() == 0)||(folder_.size() == 0))
1003 return NOERROR;
1005 if (((uFlags & 0x000f)!=CMF_NORMAL)&&(!(uFlags & CMF_EXPLORE))&&(!(uFlags & CMF_VERBSONLY)))
1006 return NOERROR;
1008 bool bSourceAndTargetFromSameRepository = (uuidSource.compare(uuidTarget) == 0) || uuidSource.empty() || uuidTarget.empty();
1010 //the drop handler only has eight commands, but not all are visible at the same time:
1011 //if the source file(s) are under version control then those files can be moved
1012 //to the new location or they can be moved with a rename,
1013 //if they are unversioned then they can be added to the working copy
1014 //if they are versioned, they also can be exported to an unversioned location
1015 UINT idCmd = idCmdFirst;
1017 // Git move here
1018 // available if source is versioned but not added, target is versioned, source and target from same repository or target folder is added
1019 if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&((itemStates & ITEMIS_INSVN)&&((~itemStates) & ITEMIS_ADDED)))
1020 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPMOVEMENU, 0, idCmdFirst, ShellMenuDropMove, uFlags);
1022 // Git move and rename here
1023 // 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
1024 if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_ONLYONE)&&((~itemStates) & ITEMIS_ADDED))
1025 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPMOVERENAMEMENU, 0, idCmdFirst, ShellMenuDropMoveRename, uFlags);
1027 // Git copy here
1028 // available if source is versioned but not added, target is versioned, source and target from same repository or target folder is added
1029 if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&((~itemStates) & ITEMIS_ADDED))
1030 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYMENU, 0, idCmdFirst, ShellMenuDropCopy, uFlags);
1032 // Git copy and rename here, source and target from same repository
1033 // available if source is a single, versioned but not added item, target is versioned or target folder is added
1034 if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_ONLYONE)&&((~itemStates) & ITEMIS_ADDED))
1035 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYRENAMEMENU, 0, idCmdFirst, ShellMenuDropCopyRename, uFlags);
1037 // Git add here
1038 // available if target is versioned and source is either unversioned or from another repository
1039 if ((itemStatesFolder & ITEMIS_FOLDERINSVN)&&(((~itemStates) & ITEMIS_INSVN)||!bSourceAndTargetFromSameRepository))
1040 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYADDMENU, 0, idCmdFirst, ShellMenuDropCopyAdd, uFlags);
1042 // Git export here
1043 // available if source is versioned and a folder
1044 if ((itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_FOLDER))
1045 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPEXPORTMENU, 0, idCmdFirst, ShellMenuDropExport, uFlags);
1047 // Git export all here
1048 // available if source is versioned and a folder
1049 if ((itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_FOLDER))
1050 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPEXPORTEXTENDEDMENU, 0, idCmdFirst, ShellMenuDropExportExtended, uFlags);
1052 // apply patch
1053 // available if source is a patchfile
1054 if (itemStates & ITEMIS_PATCHFILE)
1055 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_MENUAPPLYPATCH, 0, idCmdFirst, ShellMenuApplyPatch, uFlags);
1057 // separator
1058 if (idCmd != idCmdFirst)
1059 InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
1061 return ResultFromScode(MAKE_SCODE(SEVERITY_SUCCESS, 0, (USHORT)(idCmd - idCmdFirst)));
1064 STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu,
1065 UINT indexMenu,
1066 UINT idCmdFirst,
1067 UINT /*idCmdLast*/,
1068 UINT uFlags)
1070 ATLTRACE("Shell :: QueryContextMenu\n");
1071 PreserveChdir preserveChdir;
1073 //first check if our drop handler is called
1074 //and then (if true) provide the context menu for the
1075 //drop handler
1076 if (m_State == FileStateDropHandler)
1078 return QueryDropContext(uFlags, idCmdFirst, hMenu, indexMenu);
1081 if ((uFlags & CMF_DEFAULTONLY)!=0)
1082 return NOERROR; //we don't change the default action
1084 if ((files_.size() == 0)&&(folder_.size() == 0))
1085 return NOERROR;
1087 if (((uFlags & 0x000f)!=CMF_NORMAL)&&(!(uFlags & CMF_EXPLORE))&&(!(uFlags & CMF_VERBSONLY)))
1088 return NOERROR;
1090 int csidlarray[] =
1092 CSIDL_BITBUCKET,
1093 CSIDL_CDBURN_AREA,
1094 CSIDL_COMMON_FAVORITES,
1095 CSIDL_COMMON_STARTMENU,
1096 CSIDL_COMPUTERSNEARME,
1097 CSIDL_CONNECTIONS,
1098 CSIDL_CONTROLS,
1099 CSIDL_COOKIES,
1100 CSIDL_FAVORITES,
1101 CSIDL_FONTS,
1102 CSIDL_HISTORY,
1103 CSIDL_INTERNET,
1104 CSIDL_INTERNET_CACHE,
1105 CSIDL_NETHOOD,
1106 CSIDL_NETWORK,
1107 CSIDL_PRINTERS,
1108 CSIDL_PRINTHOOD,
1109 CSIDL_RECENT,
1110 CSIDL_SENDTO,
1111 CSIDL_STARTMENU,
1114 if (IsIllegalFolder(folder_, csidlarray))
1115 return NOERROR;
1117 if (folder_.empty())
1119 // folder is empty, but maybe files are selected
1120 if (files_.size() == 0)
1121 return NOERROR; // nothing selected - we don't have a menu to show
1122 // check whether a selected entry is an UID - those are namespace extensions
1123 // which we can't handle
1124 for (std::vector<stdstring>::const_iterator it = files_.begin(); it != files_.end(); ++it)
1126 if (_tcsncmp(it->c_str(), _T("::{"), 3)==0)
1127 return NOERROR;
1131 //check if our menu is requested for a subversion admin directory
1132 if (g_GitAdminDir.IsAdminDirPath(folder_.c_str()))
1133 return NOERROR;
1135 if (uFlags & CMF_EXTENDEDVERBS)
1136 itemStates |= ITEMIS_EXTENDED;
1138 const BOOL bShortcut = !!(uFlags & CMF_VERBSONLY);
1139 if ( bShortcut && (files_.size()==1))
1141 // Don't show the context menu for a link if the
1142 // destination is not part of a working copy.
1143 // It would only show the standard menu items
1144 // which are already shown for the lnk-file.
1145 CString path = files_.front().c_str();
1146 if ( !g_GitAdminDir.HasAdminDir(path) )
1148 return NOERROR;
1152 //check if we already added our menu entry for a folder.
1153 //we check that by iterating through all menu entries and check if
1154 //the dwItemData member points to our global ID string. That string is set
1155 //by our shell extension when the folder menu is inserted.
1156 TCHAR menubuf[MAX_PATH];
1157 int count = GetMenuItemCount(hMenu);
1158 for (int i=0; i<count; ++i)
1160 MENUITEMINFO miif;
1161 SecureZeroMemory(&miif, sizeof(MENUITEMINFO));
1162 miif.cbSize = sizeof(MENUITEMINFO);
1163 miif.fMask = MIIM_DATA;
1164 miif.dwTypeData = menubuf;
1165 miif.cch = sizeof(menubuf)/sizeof(TCHAR);
1166 GetMenuItemInfo(hMenu, i, TRUE, &miif);
1167 if (miif.dwItemData == (ULONG_PTR)g_MenuIDString)
1168 return NOERROR;
1171 LoadLangDll();
1172 UINT idCmd = idCmdFirst;
1174 //create the sub menu
1175 HMENU subMenu = CreateMenu();
1176 int indexSubMenu = 0;
1178 unsigned __int64 topmenu = g_ShellCache.GetMenuLayout();
1179 unsigned __int64 menumask = g_ShellCache.GetMenuMask();
1181 int menuIndex = 0;
1182 bool bAddSeparator = false;
1183 bool bMenuEntryAdded = false;
1184 bool bMenuEmpty = true;
1185 // insert separator at start
1186 InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); idCmd++;
1187 bool bShowIcons = !!DWORD(CRegStdWORD(_T("Software\\TortoiseGit\\ShowContextMenuIcons"), TRUE));
1188 // ?? TSV disabled icons for win2k and earlier, but they work for win2k and should work for win95 and up
1189 /*if (fullver <= 0x500)
1190 bShowIcons = false;*/
1192 #if 0
1193 if (itemStates & (ITEMIS_INSVN|ITEMIS_FOLDERINSVN))
1195 // show current branch name (as a "read-only" menu entry)
1197 CTGitPath path(folder_.empty() ? files_.front().c_str() : folder_.c_str());
1198 CString sProjectRoot;
1199 CString sBranchName;
1201 if (path.HasAdminDir(&sProjectRoot) && !g_Git.GetCurrentBranchFromFile(sProjectRoot, sBranchName))
1203 if (sBranchName.GetLength() == 40)
1205 // if SHA1 only show 4 first bytes
1206 BOOL bIsSha1 = TRUE;
1207 for (int i=0; i<40; i++)
1208 if ( !iswxdigit(sBranchName[i]) )
1210 bIsSha1 = FALSE;
1211 break;
1213 if (bIsSha1)
1214 sBranchName = sBranchName.Left(8) + _T("....");
1217 sBranchName = _T('"') + sBranchName + _T('"');
1219 const int icon = IDI_COPY;
1220 const int pos = indexMenu++;
1221 const int id = idCmd++;
1223 if ((fullver < 0x500)||(fullver == 0x500 && !(uFlags&~(CMF_RESERVED|CMF_EXPLORE|CMF_EXTENDEDVERBS))))
1225 InsertMenu(hMenu, pos, MF_DISABLED|MF_GRAYED|MF_BYPOSITION|MF_STRING, id, sBranchName);
1226 HBITMAP bmp = IconToBitmap(icon);
1227 SetMenuItemBitmaps(hMenu, pos, MF_BYPOSITION, bmp, bmp);
1229 else
1231 MENUITEMINFO menuiteminfo;
1232 SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));
1233 menuiteminfo.cbSize = sizeof(menuiteminfo);
1234 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_STATE;
1235 menuiteminfo.fState = MFS_DISABLED;
1236 menuiteminfo.fType = MFT_STRING;
1237 menuiteminfo.dwTypeData = (LPWSTR)sBranchName.GetString();
1238 if (icon)
1240 menuiteminfo.fMask |= MIIM_BITMAP;
1241 menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(icon) : HBMMENU_CALLBACK;
1243 if (menuiteminfo.hbmpItem == HBMMENU_CALLBACK)
1245 // WM_DRAWITEM uses myIDMap to get icon, we use the same icon as create branch
1246 myIDMap[id - idCmdFirst] = ShellMenuBranch;
1247 myIDMap[id] = ShellMenuBranch;
1250 menuiteminfo.wID = id;
1251 InsertMenuItem(hMenu, pos, TRUE, &menuiteminfo);
1255 #endif
1257 while (menuInfo[menuIndex].command != ShellMenuLastEntry)
1259 if (menuInfo[menuIndex].command == ShellSeparator)
1261 // we don't add a separator immediately. Because there might not be
1262 // another 'normal' menu entry after we insert a separator.
1263 // we simply set a flag here, indicating that before the next
1264 // 'normal' menu entry, a separator should be added.
1265 if (!bMenuEmpty)
1266 bAddSeparator = true;
1268 else
1270 // check the conditions whether to show the menu entry or not
1271 bool bInsertMenu = false;
1273 if (menuInfo[menuIndex].firstyes && menuInfo[menuIndex].firstno)
1275 if (((menuInfo[menuIndex].firstyes & itemStates) == menuInfo[menuIndex].firstyes)
1277 ((menuInfo[menuIndex].firstno & (~itemStates)) == menuInfo[menuIndex].firstno))
1278 bInsertMenu = true;
1280 else if ((menuInfo[menuIndex].firstyes)&&((menuInfo[menuIndex].firstyes & itemStates) == menuInfo[menuIndex].firstyes))
1281 bInsertMenu = true;
1282 else if ((menuInfo[menuIndex].firstno)&&((menuInfo[menuIndex].firstno & (~itemStates)) == menuInfo[menuIndex].firstno))
1283 bInsertMenu = true;
1285 if (menuInfo[menuIndex].secondyes && menuInfo[menuIndex].secondno)
1287 if (((menuInfo[menuIndex].secondyes & itemStates) == menuInfo[menuIndex].secondyes)
1289 ((menuInfo[menuIndex].secondno & (~itemStates)) == menuInfo[menuIndex].secondno))
1290 bInsertMenu = true;
1292 else if ((menuInfo[menuIndex].secondyes)&&((menuInfo[menuIndex].secondyes & itemStates) == menuInfo[menuIndex].secondyes))
1293 bInsertMenu = true;
1294 else if ((menuInfo[menuIndex].secondno)&&((menuInfo[menuIndex].secondno & (~itemStates)) == menuInfo[menuIndex].secondno))
1295 bInsertMenu = true;
1297 if (menuInfo[menuIndex].thirdyes && menuInfo[menuIndex].thirdno)
1299 if (((menuInfo[menuIndex].thirdyes & itemStates) == menuInfo[menuIndex].thirdyes)
1301 ((menuInfo[menuIndex].thirdno & (~itemStates)) == menuInfo[menuIndex].thirdno))
1302 bInsertMenu = true;
1304 else if ((menuInfo[menuIndex].thirdyes)&&((menuInfo[menuIndex].thirdyes & itemStates) == menuInfo[menuIndex].thirdyes))
1305 bInsertMenu = true;
1306 else if ((menuInfo[menuIndex].thirdno)&&((menuInfo[menuIndex].thirdno & (~itemStates)) == menuInfo[menuIndex].thirdno))
1307 bInsertMenu = true;
1309 if (menuInfo[menuIndex].fourthyes && menuInfo[menuIndex].fourthno)
1311 if (((menuInfo[menuIndex].fourthyes & itemStates) == menuInfo[menuIndex].fourthyes)
1313 ((menuInfo[menuIndex].fourthno & (~itemStates)) == menuInfo[menuIndex].fourthno))
1314 bInsertMenu = true;
1316 else if ((menuInfo[menuIndex].fourthyes)&&((menuInfo[menuIndex].fourthyes & itemStates) == menuInfo[menuIndex].fourthyes))
1317 bInsertMenu = true;
1318 else if ((menuInfo[menuIndex].fourthno)&&((menuInfo[menuIndex].fourthno & (~itemStates)) == menuInfo[menuIndex].fourthno))
1319 bInsertMenu = true;
1321 if (menuInfo[menuIndex].menuID & (~menumask))
1323 if (bInsertMenu)
1325 // insert a separator
1326 if ((bMenuEntryAdded)&&(bAddSeparator))
1328 bAddSeparator = false;
1329 bMenuEntryAdded = false;
1330 InsertMenu(subMenu, indexSubMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
1331 idCmd++;
1334 // handle special cases (sub menus)
1335 if ((menuInfo[menuIndex].command == ShellMenuIgnoreSub)||(menuInfo[menuIndex].command == ShellMenuUnIgnoreSub))
1337 InsertIgnoreSubmenus(idCmd, idCmdFirst, hMenu, subMenu, indexMenu, indexSubMenu, topmenu, bShowIcons, uFlags);
1338 bMenuEntryAdded = true;
1339 bMenuEmpty = false;
1341 else
1343 bool bIsTop = ((topmenu & menuInfo[menuIndex].menuID) != 0);
1345 // insert the menu entry
1346 InsertGitMenu( bIsTop,
1347 bIsTop ? hMenu : subMenu,
1348 bIsTop ? indexMenu++ : indexSubMenu++,
1349 idCmd++,
1350 menuInfo[menuIndex].menuTextID,
1351 bShowIcons ? menuInfo[menuIndex].iconID : 0,
1352 idCmdFirst,
1353 menuInfo[menuIndex].command,
1354 uFlags);
1355 if (!bIsTop)
1357 bMenuEntryAdded = true;
1358 bMenuEmpty = false;
1364 menuIndex++;
1367 //add sub menu to main context menu
1368 //don't use InsertMenu because this will lead to multiple menu entries in the explorer file menu.
1369 //see http://support.microsoft.com/default.aspx?scid=kb;en-us;214477 for details of that.
1370 MAKESTRING(IDS_MENUSUBMENU);
1371 MENUITEMINFO menuiteminfo;
1372 SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));
1373 menuiteminfo.cbSize = sizeof(menuiteminfo);
1374 menuiteminfo.fType = MFT_STRING;
1375 menuiteminfo.dwTypeData = stringtablebuffer;
1377 UINT uIcon = bShowIcons ? IDI_APP : 0;
1378 if (folder_.size())
1380 uIcon = bShowIcons ? IDI_MENUFOLDER : 0;
1381 myIDMap[idCmd - idCmdFirst] = ShellSubMenuFolder;
1382 myIDMap[idCmd] = ShellSubMenuFolder;
1383 menuiteminfo.dwItemData = (ULONG_PTR)g_MenuIDString;
1385 else if (!bShortcut && (files_.size()==1))
1387 uIcon = bShowIcons ? IDI_MENUFILE : 0;
1388 myIDMap[idCmd - idCmdFirst] = ShellSubMenuFile;
1389 myIDMap[idCmd] = ShellSubMenuFile;
1391 else if (bShortcut && (files_.size()==1))
1393 uIcon = bShowIcons ? IDI_MENULINK : 0;
1394 myIDMap[idCmd - idCmdFirst] = ShellSubMenuLink;
1395 myIDMap[idCmd] = ShellSubMenuLink;
1397 else if (files_.size() > 1)
1399 uIcon = bShowIcons ? IDI_MENUMULTIPLE : 0;
1400 myIDMap[idCmd - idCmdFirst] = ShellSubMenuMultiple;
1401 myIDMap[idCmd] = ShellSubMenuMultiple;
1403 else
1405 myIDMap[idCmd - idCmdFirst] = ShellSubMenu;
1406 myIDMap[idCmd] = ShellSubMenu;
1408 HBITMAP bmp = NULL;
1409 if ((fullver < 0x500)||(fullver == 0x500 && !(uFlags&~(CMF_RESERVED|CMF_EXPLORE|CMF_EXTENDEDVERBS))))
1411 menuiteminfo.fMask = MIIM_STRING | MIIM_ID | MIIM_SUBMENU | MIIM_DATA;
1412 if (uIcon)
1414 menuiteminfo.fMask |= MIIM_CHECKMARKS;
1415 bmp = IconToBitmap(uIcon);
1416 menuiteminfo.hbmpChecked = bmp;
1417 menuiteminfo.hbmpUnchecked = bmp;
1420 else
1422 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_SUBMENU | MIIM_DATA | MIIM_STRING;
1423 if (uIcon)
1425 menuiteminfo.fMask |= MIIM_BITMAP;
1426 menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(uIcon) : HBMMENU_CALLBACK;
1429 menuiteminfo.hSubMenu = subMenu;
1430 menuiteminfo.wID = idCmd++;
1431 InsertMenuItem(hMenu, indexMenu++, TRUE, &menuiteminfo);
1433 //separator after
1434 InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); idCmd++;
1436 //return number of menu items added
1437 return ResultFromScode(MAKE_SCODE(SEVERITY_SUCCESS, 0, (USHORT)(idCmd - idCmdFirst)));
1441 // This is called when you invoke a command on the menu:
1442 STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
1444 PreserveChdir preserveChdir;
1445 HRESULT hr = E_INVALIDARG;
1446 if (lpcmi == NULL)
1447 return hr;
1449 std::string command;
1450 std::string parent;
1451 std::string file;
1453 if ((files_.size() > 0)||(folder_.size() > 0))
1455 UINT idCmd = LOWORD(lpcmi->lpVerb);
1457 if (HIWORD(lpcmi->lpVerb))
1459 stdstring verb = stdstring(MultibyteToWide(lpcmi->lpVerb));
1460 std::map<stdstring, UINT_PTR>::const_iterator verb_it = myVerbsMap.lower_bound(verb);
1461 if (verb_it != myVerbsMap.end() && verb_it->first == verb)
1462 idCmd = verb_it->second;
1463 else
1464 return hr;
1467 // See if we have a handler interface for this id
1468 std::map<UINT_PTR, UINT_PTR>::const_iterator id_it = myIDMap.lower_bound(idCmd);
1469 if (id_it != myIDMap.end() && id_it->first == idCmd)
1471 STARTUPINFO startup;
1472 PROCESS_INFORMATION process;
1473 memset(&startup, 0, sizeof(startup));
1474 startup.cb = sizeof(startup);
1475 memset(&process, 0, sizeof(process));
1476 CRegStdString tortoiseProcPath(_T("Software\\TortoiseGit\\ProcPath"), _T("TortoiseProc.exe"), false, HKEY_LOCAL_MACHINE);
1477 CRegStdString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T("TortoiseMerge.exe"), false, HKEY_LOCAL_MACHINE);
1479 //TortoiseProc expects a command line of the form:
1480 //"/command:<commandname> /pathfile:<path> /startrev:<startrevision> /endrev:<endrevision> /deletepathfile
1481 // or
1482 //"/command:<commandname> /path:<path> /startrev:<startrevision> /endrev:<endrevision>
1484 //* path is a path to a single file/directory for commands which only act on single items (log, checkout, ...)
1485 //* pathfile is a path to a temporary file which contains a list of file paths
1486 stdstring svnCmd = _T(" /command:");
1487 stdstring tempfile;
1488 switch (id_it->second)
1490 //#region case
1491 case ShellMenuSync:
1492 svnCmd += _T("sync /path:\"");
1493 svnCmd += folder_;
1494 svnCmd += _T("\"");
1495 break;
1496 case ShellMenuCheckout:
1497 svnCmd += _T("checkout /path:\"");
1498 svnCmd += folder_;
1499 svnCmd += _T("\"");
1500 break;
1501 case ShellMenuUpdate:
1502 tempfile = WriteFileListToTempFile();
1503 svnCmd += _T("update /pathfile:\"");
1504 svnCmd += tempfile;
1505 svnCmd += _T("\"");
1506 svnCmd += _T(" /deletepathfile");
1507 break;
1508 case ShellMenuSubSync:
1509 tempfile = WriteFileListToTempFile();
1510 svnCmd += _T("subsync /pathfile:\"");
1511 svnCmd += tempfile;
1512 svnCmd += _T("\"");
1513 svnCmd += _T(" /deletepathfile");
1514 if(itemStatesFolder&ITEMIS_SUBMODULE)
1516 svnCmd += _T(" /bkpath:");
1517 svnCmd += folder_;
1519 break;
1520 case ShellMenuUpdateExt:
1521 tempfile = WriteFileListToTempFile();
1522 svnCmd += _T("subupdate /pathfile:\"");
1523 svnCmd += tempfile;
1524 svnCmd += _T("\"");
1525 svnCmd += _T(" /deletepathfile");
1526 if(itemStatesFolder&ITEMIS_SUBMODULE)
1528 svnCmd += _T(" /bkpath:");
1529 svnCmd += folder_;
1531 break;
1532 case ShellMenuCommit:
1533 tempfile = WriteFileListToTempFile();
1534 svnCmd += _T("commit /pathfile:\"");
1535 svnCmd += tempfile;
1536 svnCmd += _T("\"");
1537 svnCmd += _T(" /deletepathfile");
1538 break;
1539 case ShellMenuAdd:
1540 case ShellMenuAddAsReplacement:
1541 tempfile = WriteFileListToTempFile();
1542 svnCmd += _T("add /pathfile:\"");
1543 svnCmd += tempfile;
1544 svnCmd += _T("\"");
1545 svnCmd += _T(" /deletepathfile");
1546 break;
1547 case ShellMenuIgnore:
1548 tempfile = WriteFileListToTempFile();
1549 svnCmd += _T("ignore /pathfile:\"");
1550 svnCmd += tempfile;
1551 svnCmd += _T("\"");
1552 svnCmd += _T(" /deletepathfile");
1553 break;
1554 case ShellMenuIgnoreCaseSensitive:
1555 tempfile = WriteFileListToTempFile();
1556 svnCmd += _T("ignore /pathfile:\"");
1557 svnCmd += tempfile;
1558 svnCmd += _T("\"");
1559 svnCmd += _T(" /deletepathfile");
1560 svnCmd += _T(" /onlymask");
1561 break;
1562 case ShellMenuUnIgnore:
1563 tempfile = WriteFileListToTempFile();
1564 svnCmd += _T("unignore /pathfile:\"");
1565 svnCmd += tempfile;
1566 svnCmd += _T("\"");
1567 svnCmd += _T(" /deletepathfile");
1568 break;
1569 case ShellMenuUnIgnoreCaseSensitive:
1570 tempfile = WriteFileListToTempFile();
1571 svnCmd += _T("unignore /pathfile:\"");
1572 svnCmd += tempfile;
1573 svnCmd += _T("\"");
1574 svnCmd += _T(" /deletepathfile");
1575 svnCmd += _T(" /onlymask");
1576 break;
1577 case ShellMenuRevert:
1578 tempfile = WriteFileListToTempFile();
1579 svnCmd += _T("revert /pathfile:\"");
1580 svnCmd += tempfile;
1581 svnCmd += _T("\"");
1582 svnCmd += _T(" /deletepathfile");
1583 break;
1584 case ShellMenuDelUnversioned:
1585 svnCmd += _T("delunversioned /path:\"");
1586 svnCmd += folder_;
1587 svnCmd += _T("\"");
1588 break;
1589 case ShellMenuCleanup:
1590 tempfile = WriteFileListToTempFile();
1591 svnCmd += _T("cleanup /pathfile:\"");
1592 svnCmd += tempfile;
1593 svnCmd += _T("\"");
1594 svnCmd += _T(" /deletepathfile");
1595 break;
1596 case ShellMenuSendMail:
1597 tempfile = WriteFileListToTempFile();
1598 svnCmd += _T("sendmail /pathfile:\"");
1599 svnCmd += tempfile;
1600 svnCmd += _T("\"");
1601 svnCmd += _T(" /deletepathfile");
1602 break;
1603 case ShellMenuResolve:
1604 tempfile = WriteFileListToTempFile();
1605 svnCmd += _T("resolve /pathfile:\"");
1606 svnCmd += tempfile;
1607 svnCmd += _T("\"");
1608 svnCmd += _T(" /deletepathfile");
1609 break;
1610 case ShellMenuSwitch:
1611 svnCmd += _T("switch /path:\"");
1612 if (files_.size() > 0)
1613 svnCmd += files_.front();
1614 else
1615 svnCmd += folder_;
1616 svnCmd += _T("\"");
1617 break;
1618 case ShellMenuImport:
1619 svnCmd += _T("import /path:\"");
1620 svnCmd += folder_;
1621 svnCmd += _T("\"");
1622 break;
1623 case ShellMenuExport:
1624 svnCmd += _T("export /path:\"");
1625 svnCmd += folder_;
1626 svnCmd += _T("\"");
1627 break;
1628 case ShellMenuAbout:
1629 svnCmd += _T("about");
1630 break;
1631 case ShellMenuCreateRepos:
1632 svnCmd += _T("repocreate /path:\"");
1633 svnCmd += folder_;
1634 svnCmd += _T("\"");
1635 break;
1636 case ShellMenuMerge:
1637 svnCmd += _T("merge /path:\"");
1638 if (files_.size() > 0)
1639 svnCmd += files_.front();
1640 else
1641 svnCmd += folder_;
1642 svnCmd += _T("\"");
1643 break;
1644 case ShellMenuMergeAll:
1645 svnCmd += _T("mergeall /path:\"");
1646 if (files_.size() > 0)
1647 svnCmd += files_.front();
1648 else
1649 svnCmd += folder_;
1650 svnCmd += _T("\"");
1651 break;
1652 case ShellMenuCopy:
1653 svnCmd += _T("copy /path:\"");
1654 if (files_.size() > 0)
1655 svnCmd += files_.front();
1656 else
1657 svnCmd += folder_;
1658 svnCmd += _T("\"");
1659 break;
1660 case ShellMenuSettings:
1661 svnCmd += _T("settings /path:\"");
1662 if (files_.size() > 0)
1663 svnCmd += files_.front();
1664 else
1665 svnCmd += folder_;
1666 svnCmd += _T("\"");
1667 break;
1668 case ShellMenuHelp:
1669 svnCmd += _T("help");
1670 break;
1671 case ShellMenuRename:
1672 svnCmd += _T("rename /path:\"");
1673 if (files_.size() > 0)
1674 svnCmd += files_.front();
1675 else
1676 svnCmd += folder_;
1677 svnCmd += _T("\"");
1678 break;
1679 case ShellMenuRemove:
1680 tempfile = WriteFileListToTempFile();
1681 svnCmd += _T("remove /pathfile:\"");
1682 svnCmd += tempfile;
1683 svnCmd += _T("\"");
1684 svnCmd += _T(" /deletepathfile");
1685 break;
1686 case ShellMenuRemoveKeep:
1687 tempfile = WriteFileListToTempFile();
1688 svnCmd += _T("remove /pathfile:\"");
1689 svnCmd += tempfile;
1690 svnCmd += _T("\"");
1691 svnCmd += _T(" /deletepathfile");
1692 svnCmd += _T(" /keep");
1693 break;
1694 case ShellMenuDiff:
1695 svnCmd += _T("diff /path:\"");
1696 if (files_.size() == 1)
1697 svnCmd += files_.front();
1698 else if (files_.size() == 2)
1700 std::vector<stdstring>::iterator I = files_.begin();
1701 svnCmd += *I;
1702 I++;
1703 svnCmd += _T("\" /path2:\"");
1704 svnCmd += *I;
1706 else
1707 svnCmd += folder_;
1708 svnCmd += _T("\"");
1709 if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
1710 svnCmd += _T(" /alternative");
1711 break;
1712 case ShellMenuPrevDiff:
1713 svnCmd += _T("prevdiff /path:\"");
1714 if (files_.size() == 1)
1715 svnCmd += files_.front();
1716 else
1717 svnCmd += folder_;
1718 svnCmd += _T("\"");
1719 if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
1720 svnCmd += _T(" /alternative");
1721 break;
1722 case ShellMenuUrlDiff:
1723 svnCmd += _T("urldiff /path:\"");
1724 if (files_.size() == 1)
1725 svnCmd += files_.front();
1726 else
1727 svnCmd += folder_;
1728 svnCmd += _T("\"");
1729 break;
1730 case ShellMenuDropCopyAdd:
1731 tempfile = WriteFileListToTempFile();
1732 svnCmd += _T("dropcopyadd /pathfile:\"");
1733 svnCmd += tempfile;
1734 svnCmd += _T("\"");
1735 svnCmd += _T(" /deletepathfile");
1736 svnCmd += _T(" /droptarget:\"");
1737 svnCmd += folder_;
1738 svnCmd += _T("\"";)
1739 break;
1740 case ShellMenuDropCopy:
1741 tempfile = WriteFileListToTempFile();
1742 svnCmd += _T("dropcopy /pathfile:\"");
1743 svnCmd += tempfile;
1744 svnCmd += _T("\"");
1745 svnCmd += _T(" /deletepathfile");
1746 svnCmd += _T(" /droptarget:\"");
1747 svnCmd += folder_;
1748 svnCmd += _T("\"";)
1749 break;
1750 case ShellMenuDropCopyRename:
1751 tempfile = WriteFileListToTempFile();
1752 svnCmd += _T("dropcopy /pathfile:\"");
1753 svnCmd += tempfile;
1754 svnCmd += _T("\"");
1755 svnCmd += _T(" /deletepathfile");
1756 svnCmd += _T(" /droptarget:\"");
1757 svnCmd += folder_;
1758 svnCmd += _T("\" /rename";)
1759 break;
1760 case ShellMenuDropMove:
1761 tempfile = WriteFileListToTempFile();
1762 svnCmd += _T("dropmove /pathfile:\"");
1763 svnCmd += tempfile;
1764 svnCmd += _T("\"");
1765 svnCmd += _T(" /deletepathfile");
1766 svnCmd += _T(" /droptarget:\"");
1767 svnCmd += folder_;
1768 svnCmd += _T("\"");
1769 break;
1770 case ShellMenuDropMoveRename:
1771 tempfile = WriteFileListToTempFile();
1772 svnCmd += _T("dropmove /pathfile:\"");
1773 svnCmd += tempfile;
1774 svnCmd += _T("\"");
1775 svnCmd += _T(" /deletepathfile");
1776 svnCmd += _T(" /droptarget:\"");
1777 svnCmd += folder_;
1778 svnCmd += _T("\" /rename";)
1779 break;
1780 case ShellMenuDropExport:
1781 tempfile = WriteFileListToTempFile();
1782 svnCmd += _T("dropexport /pathfile:\"");
1783 svnCmd += tempfile;
1784 svnCmd += _T("\"");
1785 svnCmd += _T(" /deletepathfile");
1786 svnCmd += _T(" /droptarget:\"");
1787 svnCmd += folder_;
1788 svnCmd += _T("\"");
1789 break;
1790 case ShellMenuDropExportExtended:
1791 tempfile = WriteFileListToTempFile();
1792 svnCmd += _T("dropexport /pathfile:\"");
1793 svnCmd += tempfile;
1794 svnCmd += _T("\"");
1795 svnCmd += _T(" /deletepathfile");
1796 svnCmd += _T(" /droptarget:\"");
1797 svnCmd += folder_;
1798 svnCmd += _T("\"");
1799 svnCmd += _T(" /extended");
1800 break;
1801 case ShellMenuLog:
1802 svnCmd += _T("log /path:\"");
1803 if (files_.size() > 0)
1804 svnCmd += files_.front();
1805 else
1806 svnCmd += folder_;
1807 svnCmd += _T("\"");
1808 break;
1809 case ShellMenuConflictEditor:
1810 svnCmd += _T("conflicteditor /path:\"");
1811 if (files_.size() > 0)
1812 svnCmd += files_.front();
1813 else
1814 svnCmd += folder_;
1815 svnCmd += _T("\"");
1816 break;
1817 case ShellMenuRelocate:
1818 svnCmd += _T("relocate /path:\"");
1819 if (files_.size() > 0)
1820 svnCmd += files_.front();
1821 else
1822 svnCmd += folder_;
1823 svnCmd += _T("\"");
1824 break;
1825 case ShellMenuGitSVNRebase:
1826 svnCmd += _T("svnrebase /path:\"");
1827 if (files_.size() > 0)
1828 svnCmd += files_.front();
1829 else
1830 svnCmd += folder_;
1831 svnCmd += _T("\"");
1832 break;
1833 case ShellMenuGitSVNDCommit:
1834 svnCmd += _T("svndcommit /path:\"");
1835 if (files_.size() > 0)
1836 svnCmd += files_.front();
1837 else
1838 svnCmd += folder_;
1839 svnCmd += _T("\"");
1840 break;
1841 case ShellMenuRebase:
1842 svnCmd += _T("rebase /path:\"");
1843 if (files_.size() > 0)
1844 svnCmd += files_.front();
1845 else
1846 svnCmd += folder_;
1847 svnCmd += _T("\"");
1848 break;
1849 case ShellMenuShowChanged:
1850 if (files_.size() > 1)
1852 tempfile = WriteFileListToTempFile();
1853 svnCmd += _T("repostatus /pathfile:\"");
1854 svnCmd += tempfile;
1855 svnCmd += _T("\"");
1856 svnCmd += _T(" /deletepathfile");
1858 else
1860 svnCmd += _T("repostatus /path:\"");
1861 if (files_.size() > 0)
1862 svnCmd += files_.front();
1863 else
1864 svnCmd += folder_;
1865 svnCmd += _T("\"");
1867 break;
1868 case ShellMenuRefBrowse:
1869 svnCmd += _T("refbrowse /path:\"");
1870 if (files_.size() > 0)
1871 svnCmd += files_.front();
1872 else
1873 svnCmd += folder_;
1874 svnCmd += _T("\"");
1875 break;
1876 case ShellMenuRefLog:
1877 svnCmd += _T("reflog /path:\"");
1878 if (files_.size() > 0)
1879 svnCmd += files_.front();
1880 else
1881 svnCmd += folder_;
1882 svnCmd += _T("\"");
1883 break;
1885 case ShellMenuStashSave:
1886 svnCmd += _T("stashsave /path:\"");
1887 if (files_.size() > 0)
1888 svnCmd += files_.front();
1889 else
1890 svnCmd += folder_;
1891 svnCmd += _T("\"");
1892 break;
1894 case ShellMenuStashApply:
1895 svnCmd += _T("stashapply /path:\"");
1896 if (files_.size() > 0)
1897 svnCmd += files_.front();
1898 else
1899 svnCmd += folder_;
1900 svnCmd += _T("\"");
1901 break;
1903 case ShellMenuStashPop:
1904 svnCmd += _T("stashpop /path:\"");
1905 if (files_.size() > 0)
1906 svnCmd += files_.front();
1907 else
1908 svnCmd += folder_;
1909 svnCmd += _T("\"");
1910 break;
1913 case ShellMenuStashList:
1914 svnCmd += _T("reflog /path:\"");
1915 if (files_.size() > 0)
1916 svnCmd += files_.front();
1917 else
1918 svnCmd += folder_;
1919 svnCmd += _T("\" /ref:refs/stash");
1920 break;
1922 case ShellMenuSubAdd:
1923 svnCmd += _T("subadd /path:\"");
1924 if (files_.size() > 0)
1925 svnCmd += files_.front();
1926 else
1927 svnCmd += folder_;
1928 svnCmd += _T("\"");
1929 break;
1931 case ShellMenuBlame:
1932 svnCmd += _T("blame /path:\"");
1933 if (files_.size() > 0)
1934 svnCmd += files_.front();
1935 else
1936 svnCmd += folder_;
1937 svnCmd += _T("\"");
1938 break;
1939 case ShellMenuCreatePatch:
1940 tempfile = WriteFileListToTempFile();
1941 svnCmd += _T("createpatch /pathfile:\"");
1942 svnCmd += tempfile;
1943 svnCmd += _T("\"");
1944 svnCmd += _T(" /deletepathfile");
1945 break;
1946 case ShellMenuApplyPatch:
1947 if ((itemStates & ITEMIS_PATCHINCLIPBOARD) && ((~itemStates) & ITEMIS_PATCHFILE))
1949 // if there's a patch file in the clipboard, we save it
1950 // to a temporary file and tell TortoiseMerge to use that one
1951 UINT cFormat = RegisterClipboardFormat(_T("TGIT_UNIFIEDDIFF"));
1952 if ((cFormat)&&(OpenClipboard(NULL)))
1954 HGLOBAL hglb = GetClipboardData(cFormat);
1955 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
1957 DWORD len = GetTempPath(0, NULL);
1958 TCHAR * path = new TCHAR[len+1];
1959 TCHAR * tempF = new TCHAR[len+100];
1960 GetTempPath (len+1, path);
1961 GetTempFileName (path, TEXT("git"), 0, tempF);
1962 std::wstring sTempFile = std::wstring(tempF);
1963 delete [] path;
1964 delete [] tempF;
1966 FILE * outFile;
1967 size_t patchlen = strlen(lpstr);
1968 _tfopen_s(&outFile, sTempFile.c_str(), _T("wb"));
1969 if(outFile)
1971 size_t size = fwrite(lpstr, sizeof(char), patchlen, outFile);
1972 if (size == patchlen)
1974 itemStates |= ITEMIS_PATCHFILE;
1975 files_.clear();
1976 files_.push_back(sTempFile);
1978 fclose(outFile);
1980 GlobalUnlock(hglb);
1981 CloseClipboard();
1984 if (itemStates & ITEMIS_PATCHFILE)
1986 svnCmd = _T(" /diff:\"");
1987 if (files_.size() > 0)
1989 svnCmd += files_.front();
1990 if (itemStatesFolder & ITEMIS_FOLDERINSVN)
1992 svnCmd += _T("\" /patchpath:\"");
1993 svnCmd += folder_;
1996 else
1997 svnCmd += folder_;
1998 if (itemStates & ITEMIS_INVERSIONEDFOLDER)
1999 svnCmd += _T("\" /wc");
2000 else
2001 svnCmd += _T("\"");
2003 else
2005 svnCmd = _T(" /patchpath:\"");
2006 if (files_.size() > 0)
2007 svnCmd += files_.front();
2008 else
2009 svnCmd += folder_;
2010 svnCmd += _T("\"");
2012 myIDMap.clear();
2013 myVerbsIDMap.clear();
2014 myVerbsMap.clear();
2015 if (CreateProcess(((stdstring)tortoiseMergePath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)
2017 LPVOID lpMsgBuf;
2018 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
2019 FORMAT_MESSAGE_FROM_SYSTEM |
2020 FORMAT_MESSAGE_IGNORE_INSERTS,
2021 NULL,
2022 GetLastError(),
2023 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
2024 (LPTSTR) &lpMsgBuf,
2026 NULL
2028 MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseMerge launch failed"), MB_OK | MB_ICONINFORMATION );
2029 LocalFree( lpMsgBuf );
2031 CloseHandle(process.hThread);
2032 CloseHandle(process.hProcess);
2033 return NOERROR;
2034 break;
2035 case ShellMenuRevisionGraph:
2036 svnCmd += _T("revisiongraph /path:\"");
2037 if (files_.size() > 0)
2038 svnCmd += files_.front();
2039 else
2040 svnCmd += folder_;
2041 svnCmd += _T("\"");
2042 break;
2043 case ShellMenuProperties:
2044 tempfile = WriteFileListToTempFile();
2045 svnCmd += _T("properties /pathfile:\"");
2046 svnCmd += tempfile;
2047 svnCmd += _T("\"");
2048 svnCmd += _T(" /deletepathfile");
2049 break;
2050 case ShellMenuClipPaste:
2051 if (WriteClipboardPathsToTempFile(tempfile))
2053 bool bCopy = true;
2054 UINT cPrefDropFormat = RegisterClipboardFormat(_T("Preferred DropEffect"));
2055 if (cPrefDropFormat)
2057 if (OpenClipboard(lpcmi->hwnd))
2059 HGLOBAL hglb = GetClipboardData(cPrefDropFormat);
2060 if (hglb)
2062 DWORD* effect = (DWORD*) GlobalLock(hglb);
2063 if (*effect == DROPEFFECT_MOVE)
2064 bCopy = false;
2065 GlobalUnlock(hglb);
2067 CloseClipboard();
2071 if (bCopy)
2072 svnCmd += _T("pastecopy /pathfile:\"");
2073 else
2074 svnCmd += _T("pastemove /pathfile:\"");
2075 svnCmd += tempfile;
2076 svnCmd += _T("\"");
2077 svnCmd += _T(" /deletepathfile");
2078 svnCmd += _T(" /droptarget:\"");
2079 svnCmd += folder_;
2080 svnCmd += _T("\"");
2082 else return NOERROR;
2083 break;
2084 case ShellMenuClone:
2085 svnCmd += _T("clone /path:\"");
2086 svnCmd += folder_;
2087 svnCmd += _T("\"");
2088 break;
2089 case ShellMenuPull:
2090 svnCmd += _T("pull /path:\"");
2091 if (files_.size() > 0)
2092 svnCmd += files_.front();
2093 else
2094 svnCmd += folder_;
2095 svnCmd += _T("\"");
2096 break;
2097 case ShellMenuPush:
2098 svnCmd += _T("push /path:\"");
2099 if (files_.size() > 0)
2100 svnCmd += files_.front();
2101 else
2102 svnCmd += folder_;
2103 svnCmd += _T("\"");
2104 break;
2105 case ShellMenuBranch:
2106 svnCmd += _T("branch /path:\"");
2107 if (files_.size() > 0)
2108 svnCmd += files_.front();
2109 else
2110 svnCmd += folder_;
2111 svnCmd += _T("\"");
2112 break;
2114 case ShellMenuTag:
2115 svnCmd += _T("tag /path:\"");
2116 if (files_.size() > 0)
2117 svnCmd += files_.front();
2118 else
2119 svnCmd += folder_;
2120 svnCmd += _T("\"");
2121 break;
2123 case ShellMenuFormatPatch:
2124 svnCmd += _T("formatpatch /path:\"");
2125 if (files_.size() > 0)
2126 svnCmd += files_.front();
2127 else
2128 svnCmd += folder_;
2129 svnCmd += _T("\"");
2130 break;
2132 case ShellMenuImportPatch:
2133 tempfile = WriteFileListToTempFile();
2134 svnCmd += _T("importpatch /pathfile:\"");
2135 svnCmd += tempfile;
2136 svnCmd += _T("\"");
2137 svnCmd += _T(" /deletepathfile");
2138 break;
2140 case ShellMenuCherryPick:
2141 svnCmd += _T("cherrypick /path:\"");
2142 if (files_.size() > 0)
2143 svnCmd += files_.front();
2144 else
2145 svnCmd += folder_;
2146 svnCmd += _T("\"");
2147 break;
2148 case ShellMenuFetch:
2149 svnCmd += _T("fetch /path:\"");
2150 if (files_.size() > 0)
2151 svnCmd += files_.front();
2152 else
2153 svnCmd += folder_;
2154 svnCmd += _T("\"");
2155 break;
2157 default:
2158 break;
2159 //#endregion
2160 } // switch (id_it->second)
2161 svnCmd += _T(" /hwnd:");
2162 TCHAR buf[30];
2163 _stprintf_s(buf, 30, _T("%d"), lpcmi->hwnd);
2164 svnCmd += buf;
2165 myIDMap.clear();
2166 myVerbsIDMap.clear();
2167 myVerbsMap.clear();
2168 if (CreateProcess(((stdstring)tortoiseProcPath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)
2170 LPVOID lpMsgBuf;
2171 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
2172 FORMAT_MESSAGE_FROM_SYSTEM |
2173 FORMAT_MESSAGE_IGNORE_INSERTS,
2174 NULL,
2175 GetLastError(),
2176 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
2177 (LPTSTR) &lpMsgBuf,
2179 NULL
2181 MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseProc Launch failed"), MB_OK | MB_ICONINFORMATION );
2182 LocalFree( lpMsgBuf );
2184 CloseHandle(process.hThread);
2185 CloseHandle(process.hProcess);
2186 hr = NOERROR;
2187 } // if (id_it != myIDMap.end() && id_it->first == idCmd)
2188 } // if ((files_.size() > 0)||(folder_.size() > 0))
2189 return hr;
2193 // This is for the status bar and things like that:
2194 STDMETHODIMP CShellExt::GetCommandString(UINT_PTR idCmd,
2195 UINT uFlags,
2196 UINT FAR * /*reserved*/,
2197 LPSTR pszName,
2198 UINT cchMax)
2200 PreserveChdir preserveChdir;
2201 //do we know the id?
2202 std::map<UINT_PTR, UINT_PTR>::const_iterator id_it = myIDMap.lower_bound(idCmd);
2203 if (id_it == myIDMap.end() || id_it->first != idCmd)
2205 return E_INVALIDARG; //no, we don't
2208 LoadLangDll();
2209 HRESULT hr = E_INVALIDARG;
2211 MAKESTRING(IDS_MENUDESCDEFAULT);
2212 int menuIndex = 0;
2213 while (menuInfo[menuIndex].command != ShellMenuLastEntry)
2215 if (menuInfo[menuIndex].command == (GitCommands)id_it->second)
2217 MAKESTRING(menuInfo[menuIndex].menuDescID);
2218 break;
2220 menuIndex++;
2223 const TCHAR * desc = stringtablebuffer;
2224 switch(uFlags)
2226 case GCS_HELPTEXTA:
2228 std::string help = WideToMultibyte(desc);
2229 lstrcpynA(pszName, help.c_str(), cchMax);
2230 hr = S_OK;
2231 break;
2233 case GCS_HELPTEXTW:
2235 wide_string help = desc;
2236 lstrcpynW((LPWSTR)pszName, help.c_str(), cchMax);
2237 hr = S_OK;
2238 break;
2240 case GCS_VERBA:
2242 std::map<UINT_PTR, stdstring>::const_iterator verb_id_it = myVerbsIDMap.lower_bound(idCmd);
2243 if (verb_id_it != myVerbsIDMap.end() && verb_id_it->first == idCmd)
2245 std::string help = WideToMultibyte(verb_id_it->second);
2246 lstrcpynA(pszName, help.c_str(), cchMax);
2247 hr = S_OK;
2250 break;
2251 case GCS_VERBW:
2253 std::map<UINT_PTR, stdstring>::const_iterator verb_id_it = myVerbsIDMap.lower_bound(idCmd);
2254 if (verb_id_it != myVerbsIDMap.end() && verb_id_it->first == idCmd)
2256 wide_string help = verb_id_it->second;
2257 ATLTRACE("verb : %ws\n", help.c_str());
2258 lstrcpynW((LPWSTR)pszName, help.c_str(), cchMax);
2259 hr = S_OK;
2262 break;
2264 return hr;
2267 STDMETHODIMP CShellExt::HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)
2269 LRESULT res;
2270 return HandleMenuMsg2(uMsg, wParam, lParam, &res);
2273 STDMETHODIMP CShellExt::HandleMenuMsg2(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *pResult)
2275 PreserveChdir preserveChdir;
2277 LRESULT res;
2278 if (pResult == NULL)
2279 pResult = &res;
2280 *pResult = FALSE;
2282 LoadLangDll();
2283 switch (uMsg)
2285 case WM_MEASUREITEM:
2287 MEASUREITEMSTRUCT* lpmis = (MEASUREITEMSTRUCT*)lParam;
2288 if (lpmis==NULL||lpmis->CtlType!=ODT_MENU)
2289 break;
2290 lpmis->itemWidth += 2;
2291 if (lpmis->itemHeight < 16)
2292 lpmis->itemHeight = 16;
2293 *pResult = TRUE;
2295 break;
2296 case WM_DRAWITEM:
2298 LPCTSTR resource;
2299 DRAWITEMSTRUCT* lpdis = (DRAWITEMSTRUCT*)lParam;
2300 if ((lpdis==NULL)||(lpdis->CtlType != ODT_MENU))
2301 return S_OK; //not for a menu
2302 resource = GetMenuTextFromResource(myIDMap[lpdis->itemID]);
2303 if (resource == NULL)
2304 return S_OK;
2305 HICON hIcon = (HICON)LoadImage(g_hResInst, resource, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
2306 if (hIcon == NULL)
2307 return S_OK;
2308 DrawIconEx(lpdis->hDC,
2309 lpdis->rcItem.left - 16,
2310 lpdis->rcItem.top + (lpdis->rcItem.bottom - lpdis->rcItem.top - 16) / 2,
2311 hIcon, 16, 16,
2312 0, NULL, DI_NORMAL);
2313 DestroyIcon(hIcon);
2314 *pResult = TRUE;
2316 break;
2317 case WM_MENUCHAR:
2319 LPCTSTR resource;
2320 TCHAR *szItem;
2321 if (HIWORD(wParam) != MF_POPUP)
2322 return NOERROR;
2323 int nChar = LOWORD(wParam);
2324 if (_istascii((wint_t)nChar) && _istupper((wint_t)nChar))
2325 nChar = tolower(nChar);
2326 // we have the char the user pressed, now search that char in all our
2327 // menu items
2328 std::vector<int> accmenus;
2329 for (std::map<UINT_PTR, UINT_PTR>::iterator It = mySubMenuMap.begin(); It != mySubMenuMap.end(); ++It)
2331 resource = GetMenuTextFromResource(mySubMenuMap[It->first]);
2332 if (resource == NULL)
2333 continue;
2334 szItem = stringtablebuffer;
2335 TCHAR * amp = _tcschr(szItem, '&');
2336 if (amp == NULL)
2337 continue;
2338 amp++;
2339 int ampChar = LOWORD(*amp);
2340 if (_istascii((wint_t)ampChar) && _istupper((wint_t)ampChar))
2341 ampChar = tolower(ampChar);
2342 if (ampChar == nChar)
2344 // yep, we found a menu which has the pressed key
2345 // as an accelerator. Add that menu to the list to
2346 // process later.
2347 accmenus.push_back(It->first);
2350 if (accmenus.size() == 0)
2352 // no menu with that accelerator key.
2353 *pResult = MAKELONG(0, MNC_IGNORE);
2354 return NOERROR;
2356 if (accmenus.size() == 1)
2358 // Only one menu with that accelerator key. We're lucky!
2359 // So just execute that menu entry.
2360 *pResult = MAKELONG(accmenus[0], MNC_EXECUTE);
2361 return NOERROR;
2363 if (accmenus.size() > 1)
2365 // we have more than one menu item with this accelerator key!
2366 MENUITEMINFO mif;
2367 mif.cbSize = sizeof(MENUITEMINFO);
2368 mif.fMask = MIIM_STATE;
2369 for (std::vector<int>::iterator it = accmenus.begin(); it != accmenus.end(); ++it)
2371 GetMenuItemInfo((HMENU)lParam, *it, TRUE, &mif);
2372 if (mif.fState == MFS_HILITE)
2374 // this is the selected item, so select the next one
2375 ++it;
2376 if (it == accmenus.end())
2377 *pResult = MAKELONG(accmenus[0], MNC_SELECT);
2378 else
2379 *pResult = MAKELONG(*it, MNC_SELECT);
2380 return NOERROR;
2383 *pResult = MAKELONG(accmenus[0], MNC_SELECT);
2386 break;
2387 default:
2388 return NOERROR;
2391 return NOERROR;
2394 LPCTSTR CShellExt::GetMenuTextFromResource(int id)
2396 TCHAR textbuf[255];
2397 LPCTSTR resource = NULL;
2398 unsigned __int64 layout = g_ShellCache.GetMenuLayout();
2399 space = 6;
2401 int menuIndex = 0;
2402 while (menuInfo[menuIndex].command != ShellMenuLastEntry)
2404 if (menuInfo[menuIndex].command == id)
2406 MAKESTRING(menuInfo[menuIndex].menuTextID);
2407 resource = MAKEINTRESOURCE(menuInfo[menuIndex].iconID);
2408 switch (id)
2410 case ShellSubMenuMultiple:
2411 case ShellSubMenuLink:
2412 case ShellSubMenuFolder:
2413 case ShellSubMenuFile:
2414 case ShellSubMenu:
2415 space = 0;
2416 break;
2417 default:
2418 space = layout & menuInfo[menuIndex].menuID ? 0 : 6;
2419 if (layout & (menuInfo[menuIndex].menuID))
2421 _tcscpy_s(textbuf, 255, _T("Git "));
2422 _tcscat_s(textbuf, 255, stringtablebuffer);
2423 _tcscpy_s(stringtablebuffer, 255, textbuf);
2425 break;
2427 return resource;
2429 menuIndex++;
2431 return NULL;
2434 bool CShellExt::IsIllegalFolder(std::wstring folder, int * cslidarray)
2436 int i=0;
2437 TCHAR buf[MAX_PATH]; //MAX_PATH ok, since SHGetSpecialFolderPath doesn't return the required buffer length!
2438 LPITEMIDLIST pidl = NULL;
2439 while (cslidarray[i])
2441 ++i;
2442 pidl = NULL;
2443 if (SHGetFolderLocation(NULL, cslidarray[i-1], NULL, 0, &pidl)!=S_OK)
2444 continue;
2445 if (!SHGetPathFromIDList(pidl, buf))
2447 // not a file system path, definitely illegal for our use
2448 CoTaskMemFree(pidl);
2449 continue;
2451 CoTaskMemFree(pidl);
2452 if (_tcslen(buf)==0)
2453 continue;
2454 if (_tcscmp(buf, folder.c_str())==0)
2455 return true;
2457 return false;
2460 void CShellExt::InsertIgnoreSubmenus(UINT &idCmd, UINT idCmdFirst, HMENU hMenu, HMENU subMenu, UINT &indexMenu, int &indexSubMenu, unsigned __int64 topmenu, bool bShowIcons, UINT uFlags)
2462 HMENU ignoresubmenu = NULL;
2463 int indexignoresub = 0;
2464 bool bShowIgnoreMenu = false;
2465 TCHAR maskbuf[MAX_PATH]; // MAX_PATH is ok, since this only holds a filename
2466 TCHAR ignorepath[MAX_PATH]; // MAX_PATH is ok, since this only holds a filename
2467 if (files_.size() == 0)
2468 return;
2469 UINT icon = bShowIcons ? IDI_IGNORE : 0;
2471 std::vector<stdstring>::iterator I = files_.begin();
2472 if (_tcsrchr(I->c_str(), '\\'))
2473 _tcscpy_s(ignorepath, MAX_PATH, _tcsrchr(I->c_str(), '\\')+1);
2474 else
2475 _tcscpy_s(ignorepath, MAX_PATH, I->c_str());
2476 if ((itemStates & ITEMIS_IGNORED)&&(ignoredprops.size() > 0))
2478 // check if the item name is ignored or the mask
2479 size_t p = 0;
2480 while ( (p=ignoredprops.find( ignorepath,p )) != -1 )
2482 if ( (p==0 || ignoredprops[p-1]==TCHAR('\n'))
2483 && (p+_tcslen(ignorepath)==ignoredprops.length() || ignoredprops[p+_tcslen(ignorepath)+1]==TCHAR('\n')) )
2485 break;
2487 p++;
2489 if (p!=-1)
2491 ignoresubmenu = CreateMenu();
2492 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);
2493 stdstring verb = stdstring(ignorepath);
2494 myVerbsMap[verb] = idCmd - idCmdFirst;
2495 myVerbsMap[verb] = idCmd;
2496 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2497 myVerbsIDMap[idCmd] = verb;
2498 myIDMap[idCmd - idCmdFirst] = ShellMenuUnIgnore;
2499 myIDMap[idCmd++] = ShellMenuUnIgnore;
2500 bShowIgnoreMenu = true;
2502 _tcscpy_s(maskbuf, MAX_PATH, _T("*"));
2503 if (_tcsrchr(ignorepath, '.'))
2505 _tcscat_s(maskbuf, MAX_PATH, _tcsrchr(ignorepath, '.'));
2506 p = ignoredprops.find(maskbuf);
2507 if ((p!=-1) &&
2508 ((ignoredprops.compare(maskbuf)==0) || (ignoredprops.find('\n', p)==p+_tcslen(maskbuf)+1) || (ignoredprops.rfind('\n', p)==p-1)))
2510 if (ignoresubmenu==NULL)
2511 ignoresubmenu = CreateMenu();
2513 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, maskbuf);
2514 stdstring verb = stdstring(maskbuf);
2515 myVerbsMap[verb] = idCmd - idCmdFirst;
2516 myVerbsMap[verb] = idCmd;
2517 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2518 myVerbsIDMap[idCmd] = verb;
2519 myIDMap[idCmd - idCmdFirst] = ShellMenuUnIgnoreCaseSensitive;
2520 myIDMap[idCmd++] = ShellMenuUnIgnoreCaseSensitive;
2521 bShowIgnoreMenu = true;
2525 else if ((itemStates & ITEMIS_IGNORED) == 0)
2527 bShowIgnoreMenu = true;
2528 ignoresubmenu = CreateMenu();
2529 if (itemStates & ITEMIS_ONLYONE)
2531 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);
2532 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnore;
2533 myIDMap[idCmd++] = ShellMenuIgnore;
2535 _tcscpy_s(maskbuf, MAX_PATH, _T("*"));
2536 if (_tcsrchr(ignorepath, '.'))
2538 _tcscat_s(maskbuf, MAX_PATH, _tcsrchr(ignorepath, '.'));
2539 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, maskbuf);
2540 stdstring verb = stdstring(maskbuf);
2541 myVerbsMap[verb] = idCmd - idCmdFirst;
2542 myVerbsMap[verb] = idCmd;
2543 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2544 myVerbsIDMap[idCmd] = verb;
2545 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnoreCaseSensitive;
2546 myIDMap[idCmd++] = ShellMenuIgnoreCaseSensitive;
2549 else
2551 MAKESTRING(IDS_MENUIGNOREMULTIPLE);
2552 _stprintf_s(ignorepath, MAX_PATH, stringtablebuffer, files_.size());
2553 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);
2554 stdstring verb = stdstring(ignorepath);
2555 myVerbsMap[verb] = idCmd - idCmdFirst;
2556 myVerbsMap[verb] = idCmd;
2557 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2558 myVerbsIDMap[idCmd] = verb;
2559 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnore;
2560 myIDMap[idCmd++] = ShellMenuIgnore;
2562 MAKESTRING(IDS_MENUIGNOREMULTIPLEMASK);
2563 _stprintf_s(ignorepath, MAX_PATH, stringtablebuffer, files_.size());
2564 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);
2565 verb = stdstring(ignorepath);
2566 myVerbsMap[verb] = idCmd - idCmdFirst;
2567 myVerbsMap[verb] = idCmd;
2568 myVerbsIDMap[idCmd - idCmdFirst] = verb;
2569 myVerbsIDMap[idCmd] = verb;
2570 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnoreCaseSensitive;
2571 myIDMap[idCmd++] = ShellMenuIgnoreCaseSensitive;
2575 if (bShowIgnoreMenu)
2577 MENUITEMINFO menuiteminfo;
2578 SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));
2579 menuiteminfo.cbSize = sizeof(menuiteminfo);
2580 if (fullver < 0x500 || (fullver == 0x500 && !(uFlags&~(CMF_RESERVED|CMF_EXPLORE|CMF_EXTENDEDVERBS))))
2582 menuiteminfo.fMask = MIIM_STRING | MIIM_ID | MIIM_SUBMENU | MIIM_DATA;
2583 if (icon)
2585 HBITMAP bmp = IconToBitmap(icon);
2586 menuiteminfo.fMask |= MIIM_CHECKMARKS;
2587 menuiteminfo.hbmpChecked = bmp;
2588 menuiteminfo.hbmpUnchecked = bmp;
2591 else
2593 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_SUBMENU | MIIM_DATA | MIIM_STRING;
2594 if (icon)
2596 menuiteminfo.fMask |= MIIM_BITMAP;
2597 menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(icon) : HBMMENU_CALLBACK;
2600 menuiteminfo.fType = MFT_STRING;
2601 menuiteminfo.hSubMenu = ignoresubmenu;
2602 menuiteminfo.wID = idCmd;
2603 SecureZeroMemory(stringtablebuffer, sizeof(stringtablebuffer));
2604 if (itemStates & ITEMIS_IGNORED)
2605 GetMenuTextFromResource(ShellMenuUnIgnoreSub);
2606 else
2607 GetMenuTextFromResource(ShellMenuIgnoreSub);
2608 menuiteminfo.dwTypeData = stringtablebuffer;
2609 menuiteminfo.cch = (UINT)min(_tcslen(menuiteminfo.dwTypeData), UINT_MAX);
2611 InsertMenuItem((topmenu & MENUIGNORE) ? hMenu : subMenu, (topmenu & MENUIGNORE) ? indexMenu++ : indexSubMenu++, TRUE, &menuiteminfo);
2612 if (itemStates & ITEMIS_IGNORED)
2614 myIDMap[idCmd - idCmdFirst] = ShellMenuUnIgnoreSub;
2615 myIDMap[idCmd++] = ShellMenuUnIgnoreSub;
2617 else
2619 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnoreSub;
2620 myIDMap[idCmd++] = ShellMenuIgnoreSub;
2625 HBITMAP CShellExt::IconToBitmapPARGB32(UINT uIcon)
2627 std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);
2628 if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)
2629 return bitmap_it->second;
2631 HICON hIcon = (HICON)LoadImage(g_hResInst, MAKEINTRESOURCE(uIcon), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
2632 if (!hIcon)
2633 return NULL;
2635 if (pfnBeginBufferedPaint == NULL || pfnEndBufferedPaint == NULL || pfnGetBufferedPaintBits == NULL)
2636 return NULL;
2638 SIZE sizIcon;
2639 sizIcon.cx = GetSystemMetrics(SM_CXSMICON);
2640 sizIcon.cy = GetSystemMetrics(SM_CYSMICON);
2642 RECT rcIcon;
2643 SetRect(&rcIcon, 0, 0, sizIcon.cx, sizIcon.cy);
2644 HBITMAP hBmp = NULL;
2646 HDC hdcDest = CreateCompatibleDC(NULL);
2647 if (hdcDest)
2649 if (SUCCEEDED(Create32BitHBITMAP(hdcDest, &sizIcon, NULL, &hBmp)))
2651 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcDest, hBmp);
2652 if (hbmpOld)
2654 BLENDFUNCTION bfAlpha = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2655 BP_PAINTPARAMS paintParams = {0};
2656 paintParams.cbSize = sizeof(paintParams);
2657 paintParams.dwFlags = BPPF_ERASE;
2658 paintParams.pBlendFunction = &bfAlpha;
2660 HDC hdcBuffer;
2661 HPAINTBUFFER hPaintBuffer = pfnBeginBufferedPaint(hdcDest, &rcIcon, BPBF_DIB, &paintParams, &hdcBuffer);
2662 if (hPaintBuffer)
2664 if (DrawIconEx(hdcBuffer, 0, 0, hIcon, sizIcon.cx, sizIcon.cy, 0, NULL, DI_NORMAL))
2666 // If icon did not have an alpha channel we need to convert buffer to PARGB
2667 ConvertBufferToPARGB32(hPaintBuffer, hdcDest, hIcon, sizIcon);
2670 // This will write the buffer contents to the destination bitmap
2671 pfnEndBufferedPaint(hPaintBuffer, TRUE);
2674 SelectObject(hdcDest, hbmpOld);
2678 DeleteDC(hdcDest);
2681 DestroyIcon(hIcon);
2683 if(hBmp)
2684 bitmaps.insert(bitmap_it, std::make_pair(uIcon, hBmp));
2685 return hBmp;
2688 HRESULT CShellExt::Create32BitHBITMAP(HDC hdc, const SIZE *psize, __deref_opt_out void **ppvBits, __out HBITMAP* phBmp)
2690 *phBmp = NULL;
2692 BITMAPINFO bmi;
2693 ZeroMemory(&bmi, sizeof(bmi));
2694 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2695 bmi.bmiHeader.biPlanes = 1;
2696 bmi.bmiHeader.biCompression = BI_RGB;
2698 bmi.bmiHeader.biWidth = psize->cx;
2699 bmi.bmiHeader.biHeight = psize->cy;
2700 bmi.bmiHeader.biBitCount = 32;
2702 HDC hdcUsed = hdc ? hdc : GetDC(NULL);
2703 if (hdcUsed)
2705 *phBmp = CreateDIBSection(hdcUsed, &bmi, DIB_RGB_COLORS, ppvBits, NULL, 0);
2706 if (hdc != hdcUsed)
2708 ReleaseDC(NULL, hdcUsed);
2711 return (NULL == *phBmp) ? E_OUTOFMEMORY : S_OK;
2714 HRESULT CShellExt::ConvertBufferToPARGB32(HPAINTBUFFER hPaintBuffer, HDC hdc, HICON hicon, SIZE& sizIcon)
2716 RGBQUAD *prgbQuad;
2717 int cxRow;
2718 HRESULT hr = pfnGetBufferedPaintBits(hPaintBuffer, &prgbQuad, &cxRow);
2719 if (SUCCEEDED(hr))
2721 ARGB *pargb = reinterpret_cast<ARGB *>(prgbQuad);
2722 if (!HasAlpha(pargb, sizIcon, cxRow))
2724 ICONINFO info;
2725 if (GetIconInfo(hicon, &info))
2727 if (info.hbmMask)
2729 hr = ConvertToPARGB32(hdc, pargb, info.hbmMask, sizIcon, cxRow);
2732 DeleteObject(info.hbmColor);
2733 DeleteObject(info.hbmMask);
2738 return hr;
2741 bool CShellExt::HasAlpha(__in ARGB *pargb, SIZE& sizImage, int cxRow)
2743 ULONG cxDelta = cxRow - sizImage.cx;
2744 for (ULONG y = sizImage.cy; y; --y)
2746 for (ULONG x = sizImage.cx; x; --x)
2748 if (*pargb++ & 0xFF000000)
2750 return true;
2754 pargb += cxDelta;
2757 return false;
2760 HRESULT CShellExt::ConvertToPARGB32(HDC hdc, __inout ARGB *pargb, HBITMAP hbmp, SIZE& sizImage, int cxRow)
2762 BITMAPINFO bmi;
2763 ZeroMemory(&bmi, sizeof(bmi));
2764 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2765 bmi.bmiHeader.biPlanes = 1;
2766 bmi.bmiHeader.biCompression = BI_RGB;
2768 bmi.bmiHeader.biWidth = sizImage.cx;
2769 bmi.bmiHeader.biHeight = sizImage.cy;
2770 bmi.bmiHeader.biBitCount = 32;
2772 HRESULT hr = E_OUTOFMEMORY;
2773 HANDLE hHeap = GetProcessHeap();
2774 void *pvBits = HeapAlloc(hHeap, 0, bmi.bmiHeader.biWidth * 4 * bmi.bmiHeader.biHeight);
2775 if (pvBits)
2777 hr = E_UNEXPECTED;
2778 if (GetDIBits(hdc, hbmp, 0, bmi.bmiHeader.biHeight, pvBits, &bmi, DIB_RGB_COLORS) == bmi.bmiHeader.biHeight)
2780 ULONG cxDelta = cxRow - bmi.bmiHeader.biWidth;
2781 ARGB *pargbMask = static_cast<ARGB *>(pvBits);
2783 for (ULONG y = bmi.bmiHeader.biHeight; y; --y)
2785 for (ULONG x = bmi.bmiHeader.biWidth; x; --x)
2787 if (*pargbMask++)
2789 // transparent pixel
2790 *pargb++ = 0;
2792 else
2794 // opaque pixel
2795 *pargb++ |= 0xFF000000;
2799 pargb += cxDelta;
2802 hr = S_OK;
2805 HeapFree(hHeap, 0, pvBits);
2808 return hr;