OSDN Git Service

Add Rebase menu to explore contextmenu.
[tortoisegit/TortoiseGitJp.git] / src / TortoiseShell / ContextMenu.cpp
1 // TortoiseGit - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2008 - TortoiseGit\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "stdafx.h"\r
20 #include "ShellExt.h"\r
21 #include "ItemIDList.h"\r
22 #include "PreserveChdir.h"\r
23 #include "UnicodeUtils.h"\r
24 //#include "GitProperties.h"\r
25 #include "GitStatus.h"\r
26 #include "TGitPath.h"\r
27 \r
28 #define GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])\r
29 #define GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])\r
30 \r
31 int g_shellidlist=RegisterClipboardFormat(CFSTR_SHELLIDLIST);\r
32 \r
33 CShellExt::MenuInfo CShellExt::menuInfo[] =\r
34 {       \r
35         { ShellMenuClone,                                               MENUCLONE,                      IDI_CLONE,                              IDS_MENUCLONE,                  IDS_MENUDESCCHECKOUT,\r
36         ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },\r
37 \r
38         { ShellMenuPull,                                                MENUPULL,                       IDI_PULL,                               IDS_MENUPULL,                   IDS_MENUPULL,\r
39         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
40 \r
41         { ShellMenuFetch,                                               MENUFETCH,                      IDI_PULL,                               IDS_MENUFETCH,                  IDS_MENUFETCH,\r
42         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
43 \r
44         { ShellMenuPush,                                                MENUPUSH,                       IDI_PUSH,                               IDS_MENUPUSH,                   IDS_MENUPULL,\r
45         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
46 \r
47 //      { ShellMenuCheckout,                                    MENUCHECKOUT,           IDI_CHECKOUT,                   IDS_MENUCHECKOUT,                       IDS_MENUDESCCHECKOUT,\r
48 //      ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },\r
49 \r
50 //      { ShellMenuUpdate,                                              MENUUPDATE,                     IDI_UPDATE,                             IDS_MENUUPDATE,                         IDS_MENUDESCUPDATE,                             \r
51 //      ITEMIS_INSVN,   ITEMIS_ADDED, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
52 \r
53         { ShellMenuCommit,                                              MENUCOMMIT,                     IDI_COMMIT,                             IDS_MENUCOMMIT,                         IDS_MENUDESCCOMMIT,\r
54         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
55 \r
56         { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},\r
57 \r
58         { ShellMenuDiff,                                                MENUDIFF,                       IDI_DIFF,                               IDS_MENUDIFF,                           IDS_MENUDESCDIFF,\r
59         ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_FOLDER|ITEMIS_NORMAL, ITEMIS_TWO, 0, 0, 0, 0, 0 },\r
60 \r
61         { ShellMenuPrevDiff,                                    MENUPREVDIFF,                   IDI_DIFF,                               IDS_MENUPREVDIFF,                       IDS_MENUDESCPREVDIFF,\r
62         ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },\r
63 \r
64 //      { ShellMenuUrlDiff,                                             MENUURLDIFF,            IDI_DIFF,                               IDS_MENUURLDIFF,                        IDS_MENUDESCURLDIFF,\r
65 //      ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, 0, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },\r
66 \r
67         { ShellMenuLog,                                                 MENULOG,                        IDI_LOG,                                IDS_MENULOG,                            IDS_MENUDESCLOG,\r
68         ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, 0, 0 },\r
69 \r
70 //      { ShellMenuRepoBrowse,                                  MENUREPOBROWSE,         IDI_REPOBROWSE,                 IDS_MENUREPOBROWSE,                     IDS_MENUDESCREPOBROWSE,\r
71 //      ITEMIS_ONLYONE, 0, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },\r
72 \r
73         { ShellMenuShowChanged,                                 MENUSHOWCHANGED,        IDI_SHOWCHANGED,                IDS_MENUSHOWCHANGED,            IDS_MENUDESCSHOWCHANGED,\r
74         ITEMIS_INSVN|ITEMIS_ONLYONE, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0},\r
75 \r
76         { ShellMenuRebase,                                          MENUREBASE,                 IDI_SHOWCHANGED,                IDS_MENUREBASE,                         IDS_MENUREBASE,\r
77         ITEMIS_INSVN|ITEMIS_ONLYONE, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0},\r
78 \r
79 //      { ShellMenuRevisionGraph,                               MENUREVISIONGRAPH,      IDI_REVISIONGRAPH,              IDS_MENUREVISIONGRAPH,          IDS_MENUDESCREVISIONGRAPH,\r
80 //      ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, 0, 0, 0, 0},\r
81 \r
82         { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},\r
83 \r
84         { ShellMenuConflictEditor,                              MENUCONFLICTEDITOR,     IDI_CONFLICT,                   IDS_MENUCONFLICT,                       IDS_MENUDESCCONFLICT,\r
85         ITEMIS_INSVN|ITEMIS_CONFLICTED, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },\r
86 \r
87 //      { ShellMenuResolve,                                             MENURESOLVE,            IDI_RESOLVE,                    IDS_MENURESOLVE,                        IDS_MENUDESCRESOLVE,\r
88 //      ITEMIS_INSVN|ITEMIS_CONFLICTED, 0, ITEMIS_INSVN|ITEMIS_FOLDER, 0, ITEMIS_FOLDERINSVN, 0, 0, 0 },\r
89 \r
90 //      { ShellMenuUpdateExt,                                   MENUUPDATEEXT,          IDI_UPDATE,                             IDS_MENUUPDATEEXT,                      IDS_MENUDESCUPDATEEXT,\r
91 //      ITEMIS_INSVN, ITEMIS_ADDED, ITEMIS_FOLDERINSVN, ITEMIS_ADDED, 0, 0, 0, 0 },\r
92 \r
93         { ShellMenuRename,                                              MENURENAME,                     IDI_RENAME,                             IDS_MENURENAME,                         IDS_MENUDESCRENAME,\r
94         ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_INVERSIONEDFOLDER, 0, 0, 0, 0, 0, 0, 0 },\r
95 \r
96         { ShellMenuRemove,                                              MENUREMOVE,                     IDI_DELETE,                             IDS_MENUREMOVE,                         IDS_MENUDESCREMOVE,\r
97         ITEMIS_INSVN|ITEMIS_INVERSIONEDFOLDER, ITEMIS_ADDED, 0, 0, 0, 0, 0, 0 },\r
98 \r
99         { ShellMenuRemoveKeep,                                  MENUREMOVE,                     IDI_DELETE,                             IDS_MENUREMOVEKEEP,                     IDS_MENUDESCREMOVEKEEP,\r
100         ITEMIS_INSVN|ITEMIS_INVERSIONEDFOLDER|ITEMIS_EXTENDED, ITEMIS_ADDED, 0, 0, 0, 0, 0, 0 },\r
101 \r
102         { ShellMenuRevert,                                              MENUREVERT,                     IDI_REVERT,                             IDS_MENUREVERT,                         IDS_MENUDESCREVERT,\r
103         ITEMIS_INSVN, ITEMIS_NORMAL|ITEMIS_ADDED, ITEMIS_FOLDERINSVN, ITEMIS_ADDED, 0, 0, 0, 0 },\r
104 \r
105         { ShellMenuRevert,                                              MENUREVERT,                     IDI_REVERT,                             IDS_MENUUNDOADD,                        IDS_MENUDESCUNDOADD,\r
106         ITEMIS_ADDED, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN|ITEMIS_ADDED, 0, 0, 0, 0, 0 },\r
107 \r
108         { ShellMenuDelUnversioned,                              MENUDELUNVERSIONED,     IDI_DELUNVERSIONED,             IDS_MENUDELUNVERSIONED,         IDS_MENUDESCDELUNVERSIONED,\r
109         ITEMIS_FOLDER|ITEMIS_INSVN|ITEMIS_EXTENDED, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },\r
110 \r
111         { ShellMenuCleanup,                                             MENUCLEANUP,            IDI_CLEANUP,                    IDS_MENUCLEANUP,                        IDS_MENUDESCCLEANUP,\r
112         ITEMIS_INSVN|ITEMIS_FOLDER, 0, ITEMIS_FOLDERINSVN|ITEMIS_FOLDER, 0, 0, 0, 0, 0 },\r
113 \r
114 //      { ShellMenuLock,                                                MENULOCK,                       IDI_LOCK,                               IDS_MENU_LOCK,                          IDS_MENUDESC_LOCK,\r
115 //      ITEMIS_INSVN, ITEMIS_LOCKED|ITEMIS_ADDED, ITEMIS_FOLDERINSVN, ITEMIS_LOCKED|ITEMIS_ADDED, 0, 0, 0, 0 },\r
116 \r
117 //      { ShellMenuUnlock,                                              MENUUNLOCK,                     IDI_UNLOCK,                             IDS_MENU_UNLOCK,                        IDS_MENUDESC_UNLOCK,\r
118 //      ITEMIS_INSVN|ITEMIS_LOCKED, 0, ITEMIS_FOLDER|ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0 },\r
119 \r
120 //      { ShellMenuUnlockForce,                                 MENUUNLOCK,                     IDI_UNLOCK,                             IDS_MENU_UNLOCKFORCE,           IDS_MENUDESC_UNLOCKFORCE,\r
121 //      ITEMIS_INSVN|ITEMIS_LOCKED, 0, ITEMIS_FOLDER|ITEMIS_INSVN|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },\r
122 \r
123         { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},\r
124 \r
125 //      { ShellMenuCopy,                                                MENUCOPY,                       IDI_COPY,                               IDS_MENUBRANCH,                         IDS_MENUDESCCOPY,\r
126 //      ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },\r
127 \r
128         { ShellMenuSwitch,                                              MENUSWITCH,                     IDI_SWITCH,                             IDS_MENUSWITCH,                         IDS_MENUDESCSWITCH,\r
129         ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },\r
130 \r
131         { ShellMenuMerge,                                               MENUMERGE,                      IDI_MERGE,                              IDS_MENUMERGE,                          IDS_MENUDESCMERGE,\r
132         ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },\r
133         { ShellMenuMergeAll,                                    MENUMERGEALL,           IDI_MERGE,                              IDS_MENUMERGEALL,                       IDS_MENUDESCMERGEALL,\r
134         ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, 0, 0, 0, 0, 0 },\r
135 \r
136         { ShellMenuBranch,                                              MENUCOPY,                       IDI_COPY,                               IDS_MENUBRANCH,                         IDS_MENUDESCCOPY,\r
137         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
138         { ShellMenuTag,                                                 MENUTAG,                        IDI_TAG,                                IDS_MENUTAG,                            IDS_MENUDESCCOPY,\r
139         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
140 \r
141         { ShellMenuExport,                                              MENUEXPORT,                     IDI_EXPORT,                             IDS_MENUEXPORT,                         IDS_MENUDESCEXPORT,\r
142         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
143 \r
144 //      { ShellMenuRelocate,                                    MENURELOCATE,           IDI_RELOCATE,                   IDS_MENURELOCATE,                       IDS_MENUDESCRELOCATE,\r
145 //      ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },\r
146 \r
147         { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},\r
148 \r
149         { ShellMenuCreateRepos,                                 MENUCREATEREPOS,        IDI_CREATEREPOS,                IDS_MENUCREATEREPOS,            IDS_MENUDESCCREATEREPOS,\r
150         ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },\r
151 \r
152         { ShellMenuAdd,                                                 MENUADD,                        IDI_ADD,                                IDS_MENUADD,                            IDS_MENUDESCADD,\r
153         ITEMIS_INVERSIONEDFOLDER, ITEMIS_INSVN, ITEMIS_INSVN|ITEMIS_FOLDER, 0, ITEMIS_IGNORED, 0, ITEMIS_DELETED, ITEMIS_FOLDER|ITEMIS_ONLYONE },\r
154 \r
155 //      { ShellMenuAddAsReplacement,                    MENUADD,                        IDI_ADD,                                IDS_MENUADDASREPLACEMENT,       IDS_MENUADDASREPLACEMENT,\r
156 //      ITEMIS_DELETED|ITEMIS_ONLYONE, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },\r
157 \r
158 //      { ShellMenuImport,                                              MENUIMPORT,                     IDI_IMPORT,                             IDS_MENUIMPORT,                         IDS_MENUDESCIMPORT,\r
159 //      ITEMIS_FOLDER, ITEMIS_INSVN, 0, 0, 0, 0, 0, 0 },\r
160 \r
161         { ShellMenuBlame,                                               MENUBLAME,                      IDI_BLAME,                              IDS_MENUBLAME,                          IDS_MENUDESCBLAME,\r
162         ITEMIS_NORMAL|ITEMIS_ONLYONE, ITEMIS_FOLDER|ITEMIS_ADDED, 0, 0, 0, 0, 0, 0 },\r
163         // TODO: original code is ITEMIS_INSVN|ITEMIS_ONLYONE, makes sense to only allow blaming of versioned files\r
164         //       why was this changed, is this related to GitStatus?\r
165 \r
166         { ShellMenuIgnoreSub,                                   MENUIGNORE,                     IDI_IGNORE,                             IDS_MENUIGNORE,                         IDS_MENUDESCIGNORE,\r
167         ITEMIS_INVERSIONEDFOLDER, ITEMIS_IGNORED|ITEMIS_INSVN, 0, 0, 0, 0, 0, 0 },\r
168 \r
169         { ShellMenuUnIgnoreSub,                                 MENUIGNORE,                     IDI_IGNORE,                             IDS_MENUUNIGNORE,                       IDS_MENUDESCUNIGNORE,\r
170         ITEMIS_IGNORED, 0, 0, 0, 0, 0, 0, 0 },\r
171 \r
172         { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},\r
173 \r
174 //      { ShellMenuCherryPick,                                  MENUCHERRYPICK,         IDI_CREATEPATCH,                IDS_MENUCHERRYPICK,             IDS_MENUDESCCREATEPATCH,\r
175 //      ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
176 \r
177         { ShellMenuFormatPatch,                                 MENUFORMATPATCH,        IDI_CREATEPATCH,                IDS_MENUFORMATPATCH,            IDS_MENUDESCCREATEPATCH,\r
178         ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
179 \r
180         { ShellMenuImportPatch,                                 MENUIMPORTPATCH,        IDI_PATCH,                              IDS_MENUIMPORTPATCH,            IDS_MENUDESCCREATEPATCH,\r
181         ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
182 \r
183 \r
184         { ShellMenuCreatePatch,                                 MENUCREATEPATCH,        IDI_CREATEPATCH,                IDS_MENUCREATEPATCH,            IDS_MENUDESCCREATEPATCH,\r
185         ITEMIS_INSVN, ITEMIS_NORMAL, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
186 \r
187         { ShellMenuApplyPatch,                                  MENUAPPLYPATCH,         IDI_PATCH,                              IDS_MENUAPPLYPATCH,                     IDS_MENUDESCAPPLYPATCH,\r
188         ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_FOLDERINSVN, ITEMIS_ADDED, ITEMIS_ONLYONE|ITEMIS_PATCHFILE, 0, ITEMIS_FOLDERINSVN, ITEMIS_ADDED, 0, 0 },\r
189 \r
190         { ShellMenuProperties,                                  MENUPROPERTIES,         IDI_PROPERTIES,                 IDS_MENUPROPERTIES,                     IDS_MENUDESCPROPERTIES,\r
191         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
192 \r
193         { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},\r
194 //      { ShellMenuClipPaste,                                   MENUCLIPPASTE,          IDI_CLIPPASTE,                  IDS_MENUCLIPPASTE,                      IDS_MENUDESCCLIPPASTE,\r
195 //      ITEMIS_INSVN|ITEMIS_FOLDER|ITEMIS_PATHINCLIPBOARD, 0, 0, 0, 0, 0, 0, 0 },\r
196 \r
197         { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},\r
198 \r
199         { ShellMenuSettings,                                    MENUSETTINGS,           IDI_SETTINGS,                   IDS_MENUSETTINGS,                       IDS_MENUDESCSETTINGS,\r
200         ITEMIS_FOLDER, 0, 0, ITEMIS_FOLDER, 0, 0, 0, 0 },\r
201         { ShellMenuHelp,                                                MENUHELP,                       IDI_HELP,                               IDS_MENUHELP,                           IDS_MENUDESCHELP,\r
202         ITEMIS_FOLDER, 0, 0, ITEMIS_FOLDER, 0, 0, 0, 0 },\r
203         { ShellMenuAbout,                                               MENUABOUT,                      IDI_ABOUT,                              IDS_MENUABOUT,                          IDS_MENUDESCABOUT,\r
204         ITEMIS_FOLDER, 0, 0, ITEMIS_FOLDER, 0, 0, 0, 0 },\r
205 \r
206         // the sub menus - they're not added like the the commands, therefore the menu ID is zero\r
207         // but they still need to be in here, because we use the icon and string information anyway.\r
208         { ShellSubMenu,                                                 NULL,                           IDI_APP,                                IDS_MENUSUBMENU,                        0,\r
209         0, 0, 0, 0, 0, 0, 0, 0 },\r
210         { ShellSubMenuFile,                                             NULL,                           IDI_MENUFILE,                   IDS_MENUSUBMENU,                        0,\r
211         0, 0, 0, 0, 0, 0, 0, 0 },\r
212         { ShellSubMenuFolder,                                   NULL,                           IDI_MENUFOLDER,                 IDS_MENUSUBMENU,                        0,\r
213         0, 0, 0, 0, 0, 0, 0, 0 },\r
214         { ShellSubMenuLink,                                             NULL,                           IDI_MENULINK,                   IDS_MENUSUBMENU,                        0,\r
215         0, 0, 0, 0, 0, 0, 0, 0 },\r
216         { ShellSubMenuMultiple,                                 NULL,                           IDI_MENUMULTIPLE,               IDS_MENUSUBMENU,                        0,\r
217         0, 0, 0, 0, 0, 0, 0, 0 },\r
218         // mark the last entry to tell the loop where to stop iterating over this array\r
219         { ShellMenuLastEntry,                                   0,                                      0,                                              0,                                                      0,\r
220         0, 0, 0, 0, 0, 0, 0, 0 },\r
221 };\r
222 \r
223 \r
224 STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST pIDFolder,\r
225                                    LPDATAOBJECT pDataObj,\r
226                                    HKEY /* hRegKey */)\r
227 {\r
228 \r
229         ATLTRACE("Shell :: Initialize\n");\r
230         PreserveChdir preserveChdir;\r
231         files_.clear();\r
232         folder_.erase();\r
233         uuidSource.erase();\r
234         uuidTarget.erase();\r
235         itemStates = 0;\r
236         itemStatesFolder = 0;\r
237         stdstring statuspath;\r
238         git_wc_status_kind fetchedstatus = git_wc_status_none;\r
239         // get selected files/folders\r
240         if (pDataObj)\r
241         {\r
242                 STGMEDIUM medium;\r
243                 FORMATETC fmte = {(CLIPFORMAT)g_shellidlist,\r
244                         (DVTARGETDEVICE FAR *)NULL, \r
245                         DVASPECT_CONTENT, \r
246                         -1, \r
247                         TYMED_HGLOBAL};\r
248                 HRESULT hres = pDataObj->GetData(&fmte, &medium);\r
249 \r
250                 if (SUCCEEDED(hres) && medium.hGlobal)\r
251                 {\r
252                         if (m_State == FileStateDropHandler)\r
253                         {\r
254 \r
255                                 FORMATETC etc = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };\r
256                                 STGMEDIUM stg = { TYMED_HGLOBAL };\r
257                                 if ( FAILED( pDataObj->GetData ( &etc, &stg )))\r
258                                 {\r
259                                         ReleaseStgMedium ( &medium );\r
260                                         return E_INVALIDARG;\r
261                                 }\r
262 \r
263 \r
264                                 HDROP drop = (HDROP)GlobalLock(stg.hGlobal);\r
265                                 if ( NULL == drop )\r
266                                 {\r
267                                         ReleaseStgMedium ( &stg );\r
268                                         ReleaseStgMedium ( &medium );\r
269                                         return E_INVALIDARG;\r
270                                 }\r
271 \r
272                                 int count = DragQueryFile(drop, (UINT)-1, NULL, 0);\r
273                                 if (count == 1)\r
274                                         itemStates |= ITEMIS_ONLYONE;\r
275                                 for (int i = 0; i < count; i++)\r
276                                 {\r
277                                         // find the path length in chars\r
278                                         UINT len = DragQueryFile(drop, i, NULL, 0);\r
279                                         if (len == 0)\r
280                                                 continue;\r
281                                         TCHAR * szFileName = new TCHAR[len+1];\r
282                                         if (0 == DragQueryFile(drop, i, szFileName, len+1))\r
283                                         {\r
284                                                 delete [] szFileName;\r
285                                                 continue;\r
286                                         }\r
287                                         stdstring str = stdstring(szFileName);\r
288                                         delete [] szFileName;\r
289                                         if ((str.empty() == false)&&(g_ShellCache.IsContextPathAllowed(szFileName)))\r
290                                         {\r
291                                                 if (itemStates & ITEMIS_ONLYONE)\r
292                                                 {\r
293                                                         CTGitPath strpath;\r
294                                                         strpath.SetFromWin(str.c_str());\r
295                                                         itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".diff"))==0) ? ITEMIS_PATCHFILE : 0;\r
296                                                         itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".patch"))==0) ? ITEMIS_PATCHFILE : 0;\r
297                                                 }\r
298                                                 files_.push_back(str);\r
299                                                 if (i == 0)\r
300                                                 {\r
301                                                         //get the Subversion status of the item\r
302                                                         git_wc_status_kind status = git_wc_status_none;\r
303                                                         CTGitPath askedpath;\r
304                                                         askedpath.SetFromWin(str.c_str());\r
305                                                         try\r
306                                                         {\r
307                                                                 GitStatus stat;\r
308                                                                 stat.GetStatus(CTGitPath(str.c_str()), false, true, true);\r
309                                                                 if (stat.status)\r
310                                                                 {\r
311                                                                         statuspath = str;\r
312                                                                         status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);\r
313                                                                         fetchedstatus = status;\r
314                                                                         //if ((stat.status->entry)&&(stat.status->entry->lock_token))\r
315                                                                         //      itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;\r
316                                                                         if ( askedpath.IsDirectory() )//if ((stat.status->entry)&&(stat.status->entry->kind == git_node_dir))\r
317                                                                         {\r
318                                                                                 itemStates |= ITEMIS_FOLDER;\r
319                                                                                 if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
320                                                                                         itemStates |= ITEMIS_FOLDERINSVN;\r
321                                                                         }\r
322                                                                         //if ((stat.status->entry)&&(stat.status->entry->present_props))\r
323                                                                         //{\r
324                                                                         //      if (strstr(stat.status->entry->present_props, "svn:needs-lock"))\r
325                                                                         //              itemStates |= ITEMIS_NEEDSLOCK;\r
326                                                                         //}\r
327                                                                         //if ((stat.status->entry)&&(stat.status->entry->uuid))\r
328                                                                         //      uuidSource = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);\r
329                                                                 }\r
330                                                                 else\r
331                                                                 {\r
332                                                                         // sometimes, git_client_status() returns with an error.\r
333                                                                         // in that case, we have to check if the working copy is versioned\r
334                                                                         // anyway to show the 'correct' context menu\r
335                                                                         if (askedpath.HasAdminDir())\r
336                                                                                 status = git_wc_status_normal;\r
337                                                                 }\r
338                                                         }\r
339                                                         catch ( ... )\r
340                                                         {\r
341                                                                 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));\r
342                                                         }\r
343 \r
344                                                         // TODO: should we really assume any sub-directory to be versioned\r
345                                                         //       or only if it contains versioned files\r
346                                                         if ( askedpath.IsDirectory() )\r
347                                                         {\r
348                                                                 if (askedpath.HasAdminDir())\r
349                                                                         itemStates |= ITEMIS_INSVN;\r
350                                                         }\r
351                                                         if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
352                                                                 itemStates |= ITEMIS_INSVN;\r
353                                                         if (status == git_wc_status_ignored)\r
354                                                                 itemStates |= ITEMIS_IGNORED;\r
355                                                         if (status == git_wc_status_normal)\r
356                                                                 itemStates |= ITEMIS_NORMAL;\r
357                                                         if (status == git_wc_status_conflicted)\r
358                                                                 itemStates |= ITEMIS_CONFLICTED;\r
359                                                         if (status == git_wc_status_added)\r
360                                                                 itemStates |= ITEMIS_ADDED;\r
361                                                         if (status == git_wc_status_deleted)\r
362                                                                 itemStates |= ITEMIS_DELETED;\r
363                                                 }\r
364                                         }\r
365                                 } // for (int i = 0; i < count; i++)\r
366                                 GlobalUnlock ( drop );\r
367                                 ReleaseStgMedium ( &stg );\r
368 \r
369                         } // if (m_State == FileStateDropHandler) \r
370                         else\r
371                         {\r
372 \r
373                                 //Enumerate PIDLs which the user has selected\r
374                                 CIDA* cida = (CIDA*)GlobalLock(medium.hGlobal);\r
375                                 ItemIDList parent( GetPIDLFolder (cida));\r
376 \r
377                                 int count = cida->cidl;\r
378                                 BOOL statfetched = FALSE;\r
379                                 for (int i = 0; i < count; ++i)\r
380                                 {\r
381                                         ItemIDList child (GetPIDLItem (cida, i), &parent);\r
382                                         stdstring str = child.toString();\r
383                                         if ((str.empty() == false)&&(g_ShellCache.IsContextPathAllowed(str.c_str())))\r
384                                         {\r
385                                                 //check if our menu is requested for a subversion admin directory\r
386                                                 if (g_GitAdminDir.IsAdminDirPath(str.c_str()))\r
387                                                         continue;\r
388 \r
389                                                 files_.push_back(str);\r
390                                                 CTGitPath strpath;\r
391                                                 strpath.SetFromWin(str.c_str());\r
392                                                 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".diff"))==0) ? ITEMIS_PATCHFILE : 0;\r
393                                                 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".patch"))==0) ? ITEMIS_PATCHFILE : 0;\r
394                                                 if (!statfetched)\r
395                                                 {\r
396                                                         //get the Subversion status of the item\r
397                                                         git_wc_status_kind status = git_wc_status_none;\r
398                                                         if ((g_ShellCache.IsSimpleContext())&&(strpath.IsDirectory()))\r
399                                                         {\r
400                                                                 if (strpath.HasAdminDir())\r
401                                                                         status = git_wc_status_normal;\r
402                                                         }\r
403                                                         else\r
404                                                         {\r
405                                                                 try\r
406                                                                 {\r
407                                                                         GitStatus stat;\r
408                                                                         if (strpath.HasAdminDir())\r
409                                                                                 stat.GetStatus(strpath, false, true, true);\r
410                                                                         statuspath = str;\r
411                                                                         if (stat.status)\r
412                                                                         {\r
413                                                                                 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);\r
414                                                                                 fetchedstatus = status;\r
415                                                                                 //if ((stat.status->entry)&&(stat.status->entry->lock_token))\r
416                                                                                 //      itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;\r
417                                                                                 if ( strpath.IsDirectory() )//if ((stat.status->entry)&&(stat.status->entry->kind == git_node_dir))\r
418                                                                                 {\r
419                                                                                         itemStates |= ITEMIS_FOLDER;\r
420                                                                                         if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
421                                                                                                 itemStates |= ITEMIS_FOLDERINSVN;\r
422                                                                                 }\r
423                                                                                 // TODO: do we need to check that it's not a dir? does conflict options makes sense for dir in git?\r
424                                                                                 if (status == git_wc_status_conflicted)//if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))\r
425                                                                                         itemStates |= ITEMIS_CONFLICTED;\r
426                                                                                 //if ((stat.status->entry)&&(stat.status->entry->present_props))\r
427                                                                                 //{\r
428                                                                                 //      if (strstr(stat.status->entry->present_props, "svn:needs-lock"))\r
429                                                                                 //              itemStates |= ITEMIS_NEEDSLOCK;\r
430                                                                                 //}\r
431                                                                                 //if ((stat.status->entry)&&(stat.status->entry->uuid))\r
432                                                                                 //      uuidSource = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);\r
433                                                                         }       \r
434                                                                         else\r
435                                                                         {\r
436                                                                                 // sometimes, git_client_status() returns with an error.\r
437                                                                                 // in that case, we have to check if the working copy is versioned\r
438                                                                                 // anyway to show the 'correct' context menu\r
439                                                                                 if (strpath.HasAdminDir())\r
440                                                                                 {\r
441                                                                                         status = git_wc_status_normal;\r
442                                                                                         fetchedstatus = status;\r
443                                                                                 }\r
444                                                                         }\r
445                                                                         statfetched = TRUE;\r
446                                                                 }\r
447                                                                 catch ( ... )\r
448                                                                 {\r
449                                                                         ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));\r
450                                                                 }\r
451                                                         }\r
452 \r
453                                                         // TODO: should we really assume any sub-directory to be versioned\r
454                                                         //       or only if it contains versioned files\r
455                                                         if ( strpath.IsDirectory() )\r
456                                                         {\r
457                                                                 if (strpath.HasAdminDir())\r
458                                                                         itemStates |= ITEMIS_INSVN;\r
459                                                         }\r
460                                                         if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
461                                                                 itemStates |= ITEMIS_INSVN;\r
462                                                         if (status == git_wc_status_ignored)\r
463                                                         {\r
464                                                                 itemStates |= ITEMIS_IGNORED;\r
465                                                                 // the item is ignored. Get the svn:ignored properties so we can (maybe) later\r
466                                                                 // offer a 'remove from ignored list' entry\r
467 //                                                              GitProperties props(strpath.GetContainingDirectory(), false);\r
468 //                                                              ignoredprops.empty();\r
469 //                                                              for (int p=0; p<props.GetCount(); ++p)\r
470 //                                                              {\r
471 //                                                                      if (props.GetItemName(p).compare(stdstring(_T("svn:ignore")))==0)\r
472 //                                                                      {\r
473 //                                                                              std::string st = props.GetItemValue(p);\r
474 //                                                                              ignoredprops = MultibyteToWide(st.c_str());\r
475 //                                                                              // remove all escape chars ('\\')\r
476 //                                                                              std::remove(ignoredprops.begin(), ignoredprops.end(), '\\');\r
477 //                                                                              break;\r
478 //                                                                      }\r
479 //                                                              }\r
480                                                         }\r
481                                                         if (status == git_wc_status_normal)\r
482                                                                 itemStates |= ITEMIS_NORMAL;\r
483                                                         if (status == git_wc_status_conflicted)\r
484                                                                 itemStates |= ITEMIS_CONFLICTED;\r
485                                                         if (status == git_wc_status_added)\r
486                                                                 itemStates |= ITEMIS_ADDED;\r
487                                                         if (status == git_wc_status_deleted)\r
488                                                                 itemStates |= ITEMIS_DELETED;\r
489                                                 }\r
490                                         }\r
491                                 } // for (int i = 0; i < count; ++i)\r
492                                 ItemIDList child (GetPIDLItem (cida, 0), &parent);\r
493                                 if (g_ShellCache.HasSVNAdminDir(child.toString().c_str(), FALSE))\r
494                                         itemStates |= ITEMIS_INVERSIONEDFOLDER;\r
495                                 GlobalUnlock(medium.hGlobal);\r
496 \r
497                                 // if the item is a versioned folder, check if there's a patch file\r
498                                 // in the clipboard to be used in "Apply Patch"\r
499                                 UINT cFormatDiff = RegisterClipboardFormat(_T("TGIT_UNIFIEDDIFF"));\r
500                                 if (cFormatDiff)\r
501                                 {\r
502                                         if (IsClipboardFormatAvailable(cFormatDiff)) \r
503                                                 itemStates |= ITEMIS_PATCHINCLIPBOARD;\r
504                                 }\r
505                                 if (IsClipboardFormatAvailable(CF_HDROP)) \r
506                                         itemStates |= ITEMIS_PATHINCLIPBOARD;\r
507 \r
508                         }\r
509 \r
510                         ReleaseStgMedium ( &medium );\r
511                         if (medium.pUnkForRelease)\r
512                         {\r
513                                 IUnknown* relInterface = (IUnknown*)medium.pUnkForRelease;\r
514                                 relInterface->Release();\r
515                         }\r
516                 }\r
517         }\r
518 \r
519         // get folder background\r
520         if (pIDFolder)\r
521         {\r
522 \r
523                 ItemIDList list(pIDFolder);\r
524                 folder_ = list.toString();\r
525                 git_wc_status_kind status = git_wc_status_none;\r
526                 if (IsClipboardFormatAvailable(CF_HDROP)) \r
527                         itemStatesFolder |= ITEMIS_PATHINCLIPBOARD;\r
528                 \r
529                 CTGitPath askedpath;\r
530                 askedpath.SetFromWin(folder_.c_str());\r
531 \r
532                 if ((folder_.compare(statuspath)!=0)&&(g_ShellCache.IsContextPathAllowed(folder_.c_str())))\r
533                 {\r
534                         \r
535                         try\r
536                         {\r
537                                 GitStatus stat;\r
538                                 stat.GetStatus(CTGitPath(folder_.c_str()), false, true, true);\r
539                                 if (stat.status)\r
540                                 {\r
541                                         status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);\r
542 //                                      if ((stat.status->entry)&&(stat.status->entry->lock_token))\r
543 //                                              itemStatesFolder |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;\r
544 //                                      if ((stat.status->entry)&&(stat.status->entry->present_props))\r
545 //                                      {\r
546 //                                              if (strstr(stat.status->entry->present_props, "svn:needs-lock"))\r
547 //                                                      itemStatesFolder |= ITEMIS_NEEDSLOCK;\r
548 //                                      }\r
549 //                                      if ((stat.status->entry)&&(stat.status->entry->uuid))\r
550 //                                              uuidTarget = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);\r
551                                 \r
552                                 }\r
553                                 else\r
554                                 {\r
555                                         // sometimes, git_client_status() returns with an error.\r
556                                         // in that case, we have to check if the working copy is versioned\r
557                                         // anyway to show the 'correct' context menu\r
558                                         if (askedpath.HasAdminDir())\r
559                                                 status = git_wc_status_normal;\r
560                                 }\r
561                                 \r
562                                 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
563                                 if (askedpath.HasAdminDir())\r
564                                         itemStatesFolder |= ITEMIS_INSVN;\r
565                                 if (status == git_wc_status_normal)\r
566                                         itemStatesFolder |= ITEMIS_NORMAL;\r
567                                 if (status == git_wc_status_conflicted)\r
568                                         itemStatesFolder |= ITEMIS_CONFLICTED;\r
569                                 if (status == git_wc_status_added)\r
570                                         itemStatesFolder |= ITEMIS_ADDED;\r
571                                 if (status == git_wc_status_deleted)\r
572                                         itemStatesFolder |= ITEMIS_DELETED;\r
573 \r
574                         }\r
575                         catch ( ... )\r
576                         {\r
577                                 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));\r
578                         }\r
579                 }\r
580                 else\r
581                 {\r
582                         status = fetchedstatus;\r
583                 }\r
584                 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
585                 if (askedpath.HasAdminDir())\r
586                 {\r
587                         itemStatesFolder |= ITEMIS_FOLDERINSVN;\r
588                 }\r
589                 if (status == git_wc_status_ignored)\r
590                         itemStatesFolder |= ITEMIS_IGNORED;\r
591                 itemStatesFolder |= ITEMIS_FOLDER;\r
592                 if (files_.size() == 0)\r
593                         itemStates |= ITEMIS_ONLYONE;\r
594                 if (m_State != FileStateDropHandler)\r
595                         itemStates |= itemStatesFolder;\r
596 \r
597         }\r
598         if (files_.size() == 2)\r
599                 itemStates |= ITEMIS_TWO;\r
600         if ((files_.size() == 1)&&(g_ShellCache.IsContextPathAllowed(files_.front().c_str())))\r
601         {\r
602 \r
603                 itemStates |= ITEMIS_ONLYONE;\r
604                 if (m_State != FileStateDropHandler)\r
605                 {\r
606                         if (PathIsDirectory(files_.front().c_str()))\r
607                         {\r
608                                 folder_ = files_.front();\r
609                                 git_wc_status_kind status = git_wc_status_none;\r
610                                 CTGitPath askedpath;\r
611                                 askedpath.SetFromWin(folder_.c_str());\r
612 \r
613                                 if (folder_.compare(statuspath)!=0)\r
614                                 {                               \r
615                                         try\r
616                                         {\r
617                                                 GitStatus stat;\r
618                                                 stat.GetStatus(CTGitPath(folder_.c_str()), false, true, true);\r
619                                                 if (stat.status)\r
620                                                 {\r
621                                                         status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);\r
622 //                                                      if ((stat.status->entry)&&(stat.status->entry->lock_token))\r
623 //                                                              itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;\r
624 //                                                      if ((stat.status->entry)&&(stat.status->entry->present_props))\r
625 //                                                      {\r
626 //                                                              if (strstr(stat.status->entry->present_props, "svn:needs-lock"))\r
627 //                                                                      itemStates |= ITEMIS_NEEDSLOCK;\r
628 //                                                      }\r
629 //                                                      if ((stat.status->entry)&&(stat.status->entry->uuid))\r
630 //                                                              uuidTarget = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);\r
631                                                 }\r
632                                         }\r
633                                         catch ( ... )\r
634                                         {\r
635                                                 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));\r
636                                         }\r
637                                 }\r
638                                 else\r
639                                 {\r
640                                         status = fetchedstatus;\r
641                                 }\r
642                                 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
643                                 if (askedpath.HasAdminDir())\r
644                                         itemStates |= ITEMIS_FOLDERINSVN;\r
645                                 if (status == git_wc_status_ignored)\r
646                                         itemStates |= ITEMIS_IGNORED;\r
647                                 itemStates |= ITEMIS_FOLDER;\r
648                                 if (status == git_wc_status_added)\r
649                                         itemStates |= ITEMIS_ADDED;\r
650                                 if (status == git_wc_status_deleted)\r
651                                         itemStates |= ITEMIS_DELETED;\r
652                         }\r
653                 }\r
654         \r
655         }\r
656         \r
657         return NOERROR;\r
658 }\r
659 \r
660 void CShellExt::InsertGitMenu(BOOL istop, HMENU menu, UINT pos, UINT_PTR id, UINT stringid, UINT icon, UINT idCmdFirst, GitCommands com, UINT uFlags)\r
661 {\r
662         TCHAR menutextbuffer[255] = {0};\r
663         TCHAR verbsbuffer[255] = {0};\r
664         MAKESTRING(stringid);\r
665 \r
666         if (istop)\r
667         {\r
668                 //menu entry for the top context menu, so append an "Git " before\r
669                 //the menu text to indicate where the entry comes from\r
670                 _tcscpy_s(menutextbuffer, 255, _T("Git "));\r
671         }\r
672         _tcscat_s(menutextbuffer, 255, stringtablebuffer);\r
673         if ((fullver < 0x500)||(fullver == 0x500 && !uFlags))\r
674         {\r
675                 InsertMenu(menu, pos, MF_BYPOSITION | MF_STRING , id, menutextbuffer);\r
676                 if (fullver >= 0x500)\r
677                 {\r
678                         // on win2k, the context menu does not work properly if we use\r
679                         // icon bitmaps. At least the menu text is empty in the context menu\r
680                         // for folder backgrounds (seems like a win2k bug).\r
681                         HBITMAP bmp = IconToBitmap(icon); \r
682                         SetMenuItemBitmaps(menu, pos, MF_BYPOSITION, bmp, bmp);\r
683                 }\r
684         }\r
685         else\r
686         {\r
687                 MENUITEMINFO menuiteminfo = {0};\r
688                 menuiteminfo.cbSize = sizeof(menuiteminfo);\r
689                 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_BITMAP | MIIM_STRING;\r
690                 menuiteminfo.fType = MFT_STRING;\r
691                 menuiteminfo.dwTypeData = menutextbuffer;\r
692                 if (icon)\r
693                         menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(icon) : HBMMENU_CALLBACK;\r
694                 menuiteminfo.wID = id;\r
695                 InsertMenuItem(menu, pos, TRUE, &menuiteminfo);\r
696         }\r
697 \r
698         if (istop)\r
699         {\r
700                 //menu entry for the top context menu, so append an "Git " before\r
701                 //the menu text to indicate where the entry comes from\r
702                 _tcscpy_s(menutextbuffer, 255, _T("Git "));\r
703         }\r
704         LoadString(g_hResInst, stringid, verbsbuffer, sizeof(verbsbuffer));\r
705         _tcscat_s(menutextbuffer, 255, verbsbuffer);\r
706         stdstring verb = stdstring(menutextbuffer);\r
707         if (verb.find('&') != -1)\r
708         {\r
709                 verb.erase(verb.find('&'),1);\r
710         }\r
711         myVerbsMap[verb] = id - idCmdFirst;\r
712         myVerbsMap[verb] = id;\r
713         myVerbsIDMap[id - idCmdFirst] = verb;\r
714         myVerbsIDMap[id] = verb;\r
715         // We store the relative and absolute diameter\r
716         // (drawitem callback uses absolute, others relative)\r
717         myIDMap[id - idCmdFirst] = com;\r
718         myIDMap[id] = com;\r
719         if (!istop)\r
720                 mySubMenuMap[pos] = com;\r
721 }\r
722 \r
723 HBITMAP CShellExt::IconToBitmap(UINT uIcon)\r
724 {\r
725         std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);\r
726         if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)\r
727                 return bitmap_it->second;\r
728 \r
729         HICON hIcon = (HICON)LoadImage(g_hResInst, MAKEINTRESOURCE(uIcon), IMAGE_ICON, 12, 12, LR_DEFAULTCOLOR);\r
730         if (!hIcon)\r
731                 return NULL;\r
732 \r
733         RECT rect;\r
734 \r
735         rect.right = ::GetSystemMetrics(SM_CXMENUCHECK);\r
736         rect.bottom = ::GetSystemMetrics(SM_CYMENUCHECK);\r
737 \r
738         rect.left = rect.top = 0;\r
739 \r
740         HWND desktop = ::GetDesktopWindow();\r
741         if (desktop == NULL)\r
742         {\r
743                 DestroyIcon(hIcon);\r
744                 return NULL;\r
745         }\r
746 \r
747         HDC screen_dev = ::GetDC(desktop);\r
748         if (screen_dev == NULL)\r
749         {\r
750                 DestroyIcon(hIcon);\r
751                 return NULL;\r
752         }\r
753 \r
754         // Create a compatible DC\r
755         HDC dst_hdc = ::CreateCompatibleDC(screen_dev);\r
756         if (dst_hdc == NULL)\r
757         {\r
758                 DestroyIcon(hIcon);\r
759                 ::ReleaseDC(desktop, screen_dev); \r
760                 return NULL;\r
761         }\r
762 \r
763         // Create a new bitmap of icon size\r
764         HBITMAP bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);\r
765         if (bmp == NULL)\r
766         {\r
767                 DestroyIcon(hIcon);\r
768                 ::DeleteDC(dst_hdc);\r
769                 ::ReleaseDC(desktop, screen_dev); \r
770                 return NULL;\r
771         }\r
772 \r
773         // Select it into the compatible DC\r
774         HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);\r
775         if (old_dst_bmp == NULL)\r
776         {\r
777                 DestroyIcon(hIcon);\r
778                 return NULL;\r
779         }\r
780 \r
781         // Fill the background of the compatible DC with the white color\r
782         // that is taken by menu routines as transparent\r
783         ::SetBkColor(dst_hdc, RGB(255, 255, 255));\r
784         ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);\r
785 \r
786         // Draw the icon into the compatible DC\r
787         ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);\r
788 \r
789         // Restore settings\r
790         ::SelectObject(dst_hdc, old_dst_bmp);\r
791         ::DeleteDC(dst_hdc);\r
792         ::ReleaseDC(desktop, screen_dev); \r
793         DestroyIcon(hIcon);\r
794         if (bmp)\r
795                 bitmaps.insert(bitmap_it, std::make_pair(uIcon, bmp));\r
796         return bmp;\r
797 }\r
798 \r
799 bool CShellExt::WriteClipboardPathsToTempFile(stdstring& tempfile)\r
800 {\r
801         bool bRet = true;\r
802         tempfile = stdstring();\r
803         //write all selected files and paths to a temporary file\r
804         //for TortoiseProc.exe to read out again.\r
805         DWORD written = 0;\r
806         DWORD pathlength = GetTempPath(0, NULL);\r
807         TCHAR * path = new TCHAR[pathlength+1];\r
808         TCHAR * tempFile = new TCHAR[pathlength + 100];\r
809         GetTempPath (pathlength+1, path);\r
810         GetTempFileName (path, _T("git"), 0, tempFile);\r
811         tempfile = stdstring(tempFile);\r
812 \r
813         HANDLE file = ::CreateFile (tempFile,\r
814                 GENERIC_WRITE, \r
815                 FILE_SHARE_READ, \r
816                 0, \r
817                 CREATE_ALWAYS, \r
818                 FILE_ATTRIBUTE_TEMPORARY,\r
819                 0);\r
820 \r
821         delete [] path;\r
822         delete [] tempFile;\r
823         if (file == INVALID_HANDLE_VALUE)\r
824                 return false;\r
825 \r
826         if (!IsClipboardFormatAvailable(CF_HDROP))\r
827                 return false;\r
828         if (!OpenClipboard(NULL))\r
829                 return false;\r
830 \r
831         stdstring sClipboardText;\r
832         HGLOBAL hglb = GetClipboardData(CF_HDROP);\r
833         HDROP hDrop = (HDROP)GlobalLock(hglb);\r
834         if(hDrop != NULL)\r
835         {\r
836                 TCHAR szFileName[MAX_PATH];\r
837                 UINT cFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); \r
838                 for(UINT i = 0; i < cFiles; ++i)\r
839                 {\r
840                         DragQueryFile(hDrop, i, szFileName, sizeof(szFileName));\r
841                         stdstring filename = szFileName;\r
842                         ::WriteFile (file, filename.c_str(), filename.size()*sizeof(TCHAR), &written, 0);\r
843                         ::WriteFile (file, _T("\n"), 2, &written, 0);\r
844                 }\r
845                 GlobalUnlock(hDrop);\r
846         }\r
847         else bRet = false;\r
848         GlobalUnlock(hglb);\r
849 \r
850         CloseClipboard();\r
851         ::CloseHandle(file);\r
852 \r
853         return bRet;\r
854 }\r
855 \r
856 stdstring CShellExt::WriteFileListToTempFile()\r
857 {\r
858         //write all selected files and paths to a temporary file\r
859         //for TortoiseProc.exe to read out again.\r
860         DWORD pathlength = GetTempPath(0, NULL);\r
861         TCHAR * path = new TCHAR[pathlength+1];\r
862         TCHAR * tempFile = new TCHAR[pathlength + 100];\r
863         GetTempPath (pathlength+1, path);\r
864         GetTempFileName (path, _T("git"), 0, tempFile);\r
865         stdstring retFilePath = stdstring(tempFile);\r
866         \r
867         HANDLE file = ::CreateFile (tempFile,\r
868                                                                 GENERIC_WRITE, \r
869                                                                 FILE_SHARE_READ, \r
870                                                                 0, \r
871                                                                 CREATE_ALWAYS, \r
872                                                                 FILE_ATTRIBUTE_TEMPORARY,\r
873                                                                 0);\r
874 \r
875         delete [] path;\r
876         delete [] tempFile;\r
877         if (file == INVALID_HANDLE_VALUE)\r
878                 return stdstring();\r
879                 \r
880         DWORD written = 0;\r
881         if (files_.empty())\r
882         {\r
883                 ::WriteFile (file, folder_.c_str(), folder_.size()*sizeof(TCHAR), &written, 0);\r
884                 ::WriteFile (file, _T("\n"), 2, &written, 0);\r
885         }\r
886 \r
887         for (std::vector<stdstring>::iterator I = files_.begin(); I != files_.end(); ++I)\r
888         {\r
889                 ::WriteFile (file, I->c_str(), I->size()*sizeof(TCHAR), &written, 0);\r
890                 ::WriteFile (file, _T("\n"), 2, &written, 0);\r
891         }\r
892         ::CloseHandle(file);\r
893         return retFilePath;\r
894 }\r
895 \r
896 STDMETHODIMP CShellExt::QueryDropContext(UINT uFlags, UINT idCmdFirst, HMENU hMenu, UINT &indexMenu)\r
897 {\r
898         PreserveChdir preserveChdir;\r
899         LoadLangDll();\r
900 \r
901         if ((uFlags & CMF_DEFAULTONLY)!=0)\r
902                 return NOERROR;                                 //we don't change the default action\r
903 \r
904         if ((files_.size() == 0)||(folder_.size() == 0))\r
905                 return NOERROR;\r
906 \r
907         if (((uFlags & 0x000f)!=CMF_NORMAL)&&(!(uFlags & CMF_EXPLORE))&&(!(uFlags & CMF_VERBSONLY)))\r
908                 return NOERROR;\r
909 \r
910         bool bSourceAndTargetFromSameRepository = (uuidSource.compare(uuidTarget) == 0) || uuidSource.empty() || uuidTarget.empty();\r
911 \r
912         //the drop handler only has eight commands, but not all are visible at the same time:\r
913         //if the source file(s) are under version control then those files can be moved\r
914         //to the new location or they can be moved with a rename, \r
915         //if they are unversioned then they can be added to the working copy\r
916         //if they are versioned, they also can be exported to an unversioned location\r
917         UINT idCmd = idCmdFirst;\r
918 \r
919         // Git move here\r
920         // available if source is versioned but not added, target is versioned, source and target from same repository or target folder is added\r
921         if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&((itemStates & ITEMIS_INSVN)&&((~itemStates) & ITEMIS_ADDED)))\r
922                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPMOVEMENU, 0, idCmdFirst, ShellMenuDropMove, uFlags);\r
923 \r
924         // Git move and rename here\r
925         // 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\r
926         if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_ONLYONE)&&((~itemStates) & ITEMIS_ADDED))\r
927                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPMOVERENAMEMENU, 0, idCmdFirst, ShellMenuDropMoveRename, uFlags);\r
928 \r
929         // Git copy here\r
930         // available if source is versioned but not added, target is versioned, source and target from same repository or target folder is added\r
931         if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&((~itemStates) & ITEMIS_ADDED))\r
932                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYMENU, 0, idCmdFirst, ShellMenuDropCopy, uFlags);\r
933 \r
934         // Git copy and rename here, source and target from same repository\r
935         // available if source is a single, versioned but not added item, target is versioned or target folder is added\r
936         if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_ONLYONE)&&((~itemStates) & ITEMIS_ADDED))\r
937                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYRENAMEMENU, 0, idCmdFirst, ShellMenuDropCopyRename, uFlags);\r
938 \r
939         // Git add here\r
940         // available if target is versioned and source is either unversioned or from another repository\r
941         if ((itemStatesFolder & ITEMIS_FOLDERINSVN)&&(((~itemStates) & ITEMIS_INSVN)||!bSourceAndTargetFromSameRepository))\r
942                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYADDMENU, 0, idCmdFirst, ShellMenuDropCopyAdd, uFlags);\r
943 \r
944         // Git export here\r
945         // available if source is versioned and a folder\r
946         if ((itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_FOLDER))\r
947                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPEXPORTMENU, 0, idCmdFirst, ShellMenuDropExport, uFlags);\r
948 \r
949         // Git export all here\r
950         // available if source is versioned and a folder\r
951         if ((itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_FOLDER))\r
952                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPEXPORTEXTENDEDMENU, 0, idCmdFirst, ShellMenuDropExportExtended, uFlags);\r
953 \r
954         // apply patch\r
955         // available if source is a patchfile\r
956         if (itemStates & ITEMIS_PATCHFILE)\r
957                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_MENUAPPLYPATCH, 0, idCmdFirst, ShellMenuApplyPatch, uFlags);\r
958 \r
959         // separator\r
960         if (idCmd != idCmdFirst)\r
961                 InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); \r
962 \r
963         return ResultFromScode(MAKE_SCODE(SEVERITY_SUCCESS, 0, (USHORT)(idCmd - idCmdFirst)));\r
964 }\r
965 \r
966 STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu,\r
967                                          UINT indexMenu,\r
968                                          UINT idCmdFirst,\r
969                                          UINT /*idCmdLast*/,\r
970                                          UINT uFlags)\r
971 {\r
972         ATLTRACE("Shell :: QueryContextMenu\n");\r
973         PreserveChdir preserveChdir;\r
974         \r
975         //first check if our drop handler is called\r
976         //and then (if true) provide the context menu for the\r
977         //drop handler\r
978         if (m_State == FileStateDropHandler)\r
979         {\r
980                 return QueryDropContext(uFlags, idCmdFirst, hMenu, indexMenu);\r
981         }\r
982 \r
983         if ((uFlags & CMF_DEFAULTONLY)!=0)\r
984                 return NOERROR;                                 //we don't change the default action\r
985 \r
986         if ((files_.size() == 0)&&(folder_.size() == 0))\r
987                 return NOERROR;\r
988 \r
989         if (((uFlags & 0x000f)!=CMF_NORMAL)&&(!(uFlags & CMF_EXPLORE))&&(!(uFlags & CMF_VERBSONLY)))\r
990                 return NOERROR;\r
991 \r
992         int csidlarray[] = \r
993         {\r
994                 CSIDL_BITBUCKET,\r
995                 CSIDL_CDBURN_AREA,\r
996                 CSIDL_COMMON_FAVORITES,\r
997                 CSIDL_COMMON_STARTMENU,\r
998                 CSIDL_COMPUTERSNEARME,\r
999                 CSIDL_CONNECTIONS,\r
1000                 CSIDL_CONTROLS,\r
1001                 CSIDL_COOKIES,\r
1002                 CSIDL_FAVORITES,\r
1003                 CSIDL_FONTS,\r
1004                 CSIDL_HISTORY,\r
1005                 CSIDL_INTERNET,\r
1006                 CSIDL_INTERNET_CACHE,\r
1007                 CSIDL_NETHOOD,\r
1008                 CSIDL_NETWORK,\r
1009                 CSIDL_PRINTERS,\r
1010                 CSIDL_PRINTHOOD,\r
1011                 CSIDL_RECENT,\r
1012                 CSIDL_SENDTO,\r
1013                 CSIDL_STARTMENU,\r
1014                 0\r
1015         };\r
1016         if (IsIllegalFolder(folder_, csidlarray))\r
1017                 return NOERROR;\r
1018 \r
1019         if (folder_.empty())\r
1020         {\r
1021                 // folder is empty, but maybe files are selected\r
1022                 if (files_.size() == 0)\r
1023                         return NOERROR; // nothing selected - we don't have a menu to show\r
1024                 // check whether a selected entry is an UID - those are namespace extensions\r
1025                 // which we can't handle\r
1026                 for (std::vector<stdstring>::const_iterator it = files_.begin(); it != files_.end(); ++it)\r
1027                 {\r
1028                         if (_tcsncmp(it->c_str(), _T("::{"), 3)==0)\r
1029                                 return NOERROR;\r
1030                 }\r
1031         }\r
1032 \r
1033         //check if our menu is requested for a subversion admin directory\r
1034         if (g_GitAdminDir.IsAdminDirPath(folder_.c_str()))\r
1035                 return NOERROR;\r
1036 \r
1037         if (uFlags & CMF_EXTENDEDVERBS)\r
1038                 itemStates |= ITEMIS_EXTENDED;\r
1039 \r
1040         const BOOL bShortcut = !!(uFlags & CMF_VERBSONLY);\r
1041         if ( bShortcut && (files_.size()==1))\r
1042         {\r
1043                 // Don't show the context menu for a link if the\r
1044                 // destination is not part of a working copy.\r
1045                 // It would only show the standard menu items\r
1046                 // which are already shown for the lnk-file.\r
1047                 CString path = files_.front().c_str();\r
1048                 if ( !g_GitAdminDir.HasAdminDir(path) )\r
1049                 {\r
1050                         return NOERROR;\r
1051                 }\r
1052         }\r
1053 \r
1054         //check if we already added our menu entry for a folder.\r
1055         //we check that by iterating through all menu entries and check if \r
1056         //the dwItemData member points to our global ID string. That string is set\r
1057         //by our shell extension when the folder menu is inserted.\r
1058         TCHAR menubuf[MAX_PATH];\r
1059         int count = GetMenuItemCount(hMenu);\r
1060         for (int i=0; i<count; ++i)\r
1061         {\r
1062                 MENUITEMINFO miif;\r
1063                 SecureZeroMemory(&miif, sizeof(MENUITEMINFO));\r
1064                 miif.cbSize = sizeof(MENUITEMINFO);\r
1065                 miif.fMask = MIIM_DATA;\r
1066                 miif.dwTypeData = menubuf;\r
1067                 miif.cch = sizeof(menubuf)/sizeof(TCHAR);\r
1068                 GetMenuItemInfo(hMenu, i, TRUE, &miif);\r
1069                 if (miif.dwItemData == (ULONG_PTR)g_MenuIDString)\r
1070                         return NOERROR;\r
1071         }\r
1072 \r
1073         LoadLangDll();\r
1074         UINT idCmd = idCmdFirst;\r
1075 \r
1076         //create the sub menu\r
1077         HMENU subMenu = CreateMenu();\r
1078         int indexSubMenu = 0;\r
1079 \r
1080         unsigned __int64 topmenu = g_ShellCache.GetMenuLayout();\r
1081         unsigned __int64 menumask = g_ShellCache.GetMenuMask();\r
1082 \r
1083         int menuIndex = 0;\r
1084         bool bAddSeparator = false;\r
1085         bool bMenuEntryAdded = false;\r
1086         bool bMenuEmpty = true;\r
1087         // insert separator at start\r
1088         InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); idCmd++;\r
1089         bool bShowIcons = !!DWORD(CRegStdWORD(_T("Software\\TortoiseGit\\ShowContextMenuIcons"), TRUE));\r
1090         // ?? TortoiseSVN had this as (fullver <= 0x0500) this disabled icons in win2k, but icons work fine in win2k\r
1091         if (fullver < 0x0500)\r
1092                 bShowIcons = false;\r
1093         while (menuInfo[menuIndex].command != ShellMenuLastEntry)\r
1094         {\r
1095                 if (menuInfo[menuIndex].command == ShellSeparator)\r
1096                 {\r
1097                         // we don't add a separator immediately. Because there might not be\r
1098                         // another 'normal' menu entry after we insert a separator.\r
1099                         // we simply set a flag here, indicating that before the next\r
1100                         // 'normal' menu entry, a separator should be added.\r
1101                         if (!bMenuEmpty)\r
1102                                 bAddSeparator = true;\r
1103                 }\r
1104                 else\r
1105                 {\r
1106                         // check the conditions whether to show the menu entry or not\r
1107                         bool bInsertMenu = false;\r
1108 \r
1109                         if (menuInfo[menuIndex].firstyes && menuInfo[menuIndex].firstno)\r
1110                         {\r
1111                                 if (((menuInfo[menuIndex].firstyes & itemStates) == menuInfo[menuIndex].firstyes)\r
1112                                         &&\r
1113                                         ((menuInfo[menuIndex].firstno & (~itemStates)) == menuInfo[menuIndex].firstno))\r
1114                                         bInsertMenu = true;\r
1115                         }\r
1116                         else if ((menuInfo[menuIndex].firstyes)&&((menuInfo[menuIndex].firstyes & itemStates) == menuInfo[menuIndex].firstyes))\r
1117                                 bInsertMenu = true;\r
1118                         else if ((menuInfo[menuIndex].firstno)&&((menuInfo[menuIndex].firstno & (~itemStates)) == menuInfo[menuIndex].firstno))\r
1119                                 bInsertMenu = true;\r
1120 \r
1121                         if (menuInfo[menuIndex].secondyes && menuInfo[menuIndex].secondno)\r
1122                         {\r
1123                                 if (((menuInfo[menuIndex].secondyes & itemStates) == menuInfo[menuIndex].secondyes)\r
1124                                         &&\r
1125                                         ((menuInfo[menuIndex].secondno & (~itemStates)) == menuInfo[menuIndex].secondno))\r
1126                                         bInsertMenu = true;\r
1127                         }\r
1128                         else if ((menuInfo[menuIndex].secondyes)&&((menuInfo[menuIndex].secondyes & itemStates) == menuInfo[menuIndex].secondyes))\r
1129                                 bInsertMenu = true;\r
1130                         else if ((menuInfo[menuIndex].secondno)&&((menuInfo[menuIndex].secondno & (~itemStates)) == menuInfo[menuIndex].secondno))\r
1131                                 bInsertMenu = true;\r
1132 \r
1133                         if (menuInfo[menuIndex].thirdyes && menuInfo[menuIndex].thirdno)\r
1134                         {\r
1135                                 if (((menuInfo[menuIndex].thirdyes & itemStates) == menuInfo[menuIndex].thirdyes)\r
1136                                         &&\r
1137                                         ((menuInfo[menuIndex].thirdno & (~itemStates)) == menuInfo[menuIndex].thirdno))\r
1138                                         bInsertMenu = true;\r
1139                         }\r
1140                         else if ((menuInfo[menuIndex].thirdyes)&&((menuInfo[menuIndex].thirdyes & itemStates) == menuInfo[menuIndex].thirdyes))\r
1141                                 bInsertMenu = true;\r
1142                         else if ((menuInfo[menuIndex].thirdno)&&((menuInfo[menuIndex].thirdno & (~itemStates)) == menuInfo[menuIndex].thirdno))\r
1143                                 bInsertMenu = true;\r
1144 \r
1145                         if (menuInfo[menuIndex].fourthyes && menuInfo[menuIndex].fourthno)\r
1146                         {\r
1147                                 if (((menuInfo[menuIndex].fourthyes & itemStates) == menuInfo[menuIndex].fourthyes)\r
1148                                         &&\r
1149                                         ((menuInfo[menuIndex].fourthno & (~itemStates)) == menuInfo[menuIndex].fourthno))\r
1150                                         bInsertMenu = true;\r
1151                         }\r
1152                         else if ((menuInfo[menuIndex].fourthyes)&&((menuInfo[menuIndex].fourthyes & itemStates) == menuInfo[menuIndex].fourthyes))\r
1153                                 bInsertMenu = true;\r
1154                         else if ((menuInfo[menuIndex].fourthno)&&((menuInfo[menuIndex].fourthno & (~itemStates)) == menuInfo[menuIndex].fourthno))\r
1155                                 bInsertMenu = true;\r
1156 \r
1157                         if (menuInfo[menuIndex].menuID & (~menumask))\r
1158                         {\r
1159                                 if (bInsertMenu)\r
1160                                 {\r
1161                                         // insert a separator\r
1162                                         if ((bMenuEntryAdded)&&(bAddSeparator))\r
1163                                         {\r
1164                                                 bAddSeparator = false;\r
1165                                                 bMenuEntryAdded = false;\r
1166                                                 InsertMenu(subMenu, indexSubMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); \r
1167                                                 idCmd++;\r
1168                                         }\r
1169                                         \r
1170                                         // handle special cases (sub menus)\r
1171                                         if ((menuInfo[menuIndex].command == ShellMenuIgnoreSub)||(menuInfo[menuIndex].command == ShellMenuUnIgnoreSub))\r
1172                                         {\r
1173                                                 InsertIgnoreSubmenus(idCmd, idCmdFirst, hMenu, subMenu, indexMenu, indexSubMenu, topmenu, bShowIcons);\r
1174                                                 bMenuEntryAdded = true;\r
1175                                                 bMenuEmpty = false;\r
1176                                         }\r
1177                                         else\r
1178                                         {\r
1179                                                 bool bIsTop = ((topmenu & menuInfo[menuIndex].menuID) != 0);\r
1180 \r
1181                                                 // insert the menu entry\r
1182                                                 InsertGitMenu(  bIsTop,\r
1183                                                                                 bIsTop ? hMenu : subMenu,\r
1184                                                                                 bIsTop ? indexMenu++ : indexSubMenu++,\r
1185                                                                                 idCmd++,\r
1186                                                                                 menuInfo[menuIndex].menuTextID,\r
1187                                                                                 bShowIcons ? menuInfo[menuIndex].iconID : 0,\r
1188                                                                                 idCmdFirst,\r
1189                                                                                 menuInfo[menuIndex].command,\r
1190                                                                                 uFlags);\r
1191                                                 if (!bIsTop)\r
1192                                                 {\r
1193                                                         bMenuEntryAdded = true;\r
1194                                                         bMenuEmpty = false;\r
1195                                                 }\r
1196                                         }\r
1197                                 }\r
1198                         }\r
1199                 }\r
1200                 menuIndex++;\r
1201         }\r
1202 \r
1203         //add sub menu to main context menu\r
1204         //don't use InsertMenu because this will lead to multiple menu entries in the explorer file menu.\r
1205         //see http://support.microsoft.com/default.aspx?scid=kb;en-us;214477 for details of that.\r
1206         MAKESTRING(IDS_MENUSUBMENU);\r
1207         MENUITEMINFO menuiteminfo;\r
1208         SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));\r
1209         menuiteminfo.cbSize = sizeof(menuiteminfo);\r
1210         menuiteminfo.fType = MFT_STRING;\r
1211         menuiteminfo.dwTypeData = stringtablebuffer;\r
1212 \r
1213         UINT uIcon = bShowIcons ? IDI_APP : 0;\r
1214         if (folder_.size())\r
1215         {\r
1216                 uIcon = bShowIcons ? IDI_MENUFOLDER : 0;\r
1217                 myIDMap[idCmd - idCmdFirst] = ShellSubMenuFolder;\r
1218                 myIDMap[idCmd] = ShellSubMenuFolder;\r
1219                 menuiteminfo.dwItemData = (ULONG_PTR)g_MenuIDString;\r
1220         }\r
1221         else if (!bShortcut && (files_.size()==1))\r
1222         {\r
1223                 uIcon = bShowIcons ? IDI_MENUFILE : 0;\r
1224                 myIDMap[idCmd - idCmdFirst] = ShellSubMenuFile;\r
1225                 myIDMap[idCmd] = ShellSubMenuFile;\r
1226         }\r
1227         else if (bShortcut && (files_.size()==1))\r
1228         {\r
1229                 uIcon = bShowIcons ? IDI_MENULINK : 0;\r
1230                 myIDMap[idCmd - idCmdFirst] = ShellSubMenuLink;\r
1231                 myIDMap[idCmd] = ShellSubMenuLink;\r
1232         }\r
1233         else if (files_.size() > 1)\r
1234         {\r
1235                 uIcon = bShowIcons ? IDI_MENUMULTIPLE : 0;\r
1236                 myIDMap[idCmd - idCmdFirst] = ShellSubMenuMultiple;\r
1237                 myIDMap[idCmd] = ShellSubMenuMultiple;\r
1238         }\r
1239         else\r
1240         {\r
1241                 myIDMap[idCmd - idCmdFirst] = ShellSubMenu;\r
1242                 myIDMap[idCmd] = ShellSubMenu;\r
1243         }\r
1244         HBITMAP bmp = NULL;\r
1245         if ((fullver < 0x500)||(fullver == 0x500 && !uFlags))\r
1246         {\r
1247                 bmp = IconToBitmap(uIcon);\r
1248                 menuiteminfo.fMask = MIIM_STRING | MIIM_ID | MIIM_SUBMENU | MIIM_CHECKMARKS | MIIM_DATA;\r
1249         }\r
1250         else\r
1251         {\r
1252                 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_SUBMENU | MIIM_DATA | MIIM_BITMAP | MIIM_STRING;\r
1253                 if (bShowIcons)\r
1254                         menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(uIcon) : HBMMENU_CALLBACK;\r
1255         }\r
1256         menuiteminfo.hbmpChecked = bmp;\r
1257         menuiteminfo.hbmpUnchecked = bmp;\r
1258         menuiteminfo.hSubMenu = subMenu;\r
1259         menuiteminfo.wID = idCmd++;\r
1260         InsertMenuItem(hMenu, indexMenu++, TRUE, &menuiteminfo);\r
1261 \r
1262         //separator after\r
1263         InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); idCmd++;\r
1264 \r
1265         //return number of menu items added\r
1266         return ResultFromScode(MAKE_SCODE(SEVERITY_SUCCESS, 0, (USHORT)(idCmd - idCmdFirst)));\r
1267 }\r
1268 \r
1269 \r
1270 // This is called when you invoke a command on the menu:\r
1271 STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)\r
1272 {\r
1273         PreserveChdir preserveChdir;\r
1274         HRESULT hr = E_INVALIDARG;\r
1275         if (lpcmi == NULL)\r
1276                 return hr;\r
1277 \r
1278         std::string command;\r
1279         std::string parent;\r
1280         std::string file;\r
1281 \r
1282         if ((files_.size() > 0)||(folder_.size() > 0))\r
1283         {\r
1284                 UINT idCmd = LOWORD(lpcmi->lpVerb);\r
1285 \r
1286                 if (HIWORD(lpcmi->lpVerb))\r
1287                 {\r
1288                         stdstring verb = stdstring(MultibyteToWide(lpcmi->lpVerb));\r
1289                         std::map<stdstring, UINT_PTR>::const_iterator verb_it = myVerbsMap.lower_bound(verb);\r
1290                         if (verb_it != myVerbsMap.end() && verb_it->first == verb)\r
1291                                 idCmd = verb_it->second;\r
1292                         else\r
1293                                 return hr;\r
1294                 }\r
1295 \r
1296                 // See if we have a handler interface for this id\r
1297                 std::map<UINT_PTR, UINT_PTR>::const_iterator id_it = myIDMap.lower_bound(idCmd);\r
1298                 if (id_it != myIDMap.end() && id_it->first == idCmd)\r
1299                 {\r
1300                         STARTUPINFO startup;\r
1301                         PROCESS_INFORMATION process;\r
1302                         memset(&startup, 0, sizeof(startup));\r
1303                         startup.cb = sizeof(startup);\r
1304                         memset(&process, 0, sizeof(process));\r
1305                         CRegStdString tortoiseProcPath(_T("Software\\TortoiseGit\\ProcPath"), _T("TortoiseProc.exe"), false, HKEY_LOCAL_MACHINE);\r
1306                         CRegStdString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T("TortoiseMerge.exe"), false, HKEY_LOCAL_MACHINE);\r
1307 \r
1308                         //TortoiseProc expects a command line of the form:\r
1309                         //"/command:<commandname> /pathfile:<path> /startrev:<startrevision> /endrev:<endrevision> /deletepathfile\r
1310                         // or\r
1311                         //"/command:<commandname> /path:<path> /startrev:<startrevision> /endrev:<endrevision>\r
1312                         //\r
1313                         //* path is a path to a single file/directory for commands which only act on single items (log, checkout, ...)\r
1314                         //* pathfile is a path to a temporary file which contains a list of file paths\r
1315                         stdstring svnCmd = _T(" /command:");\r
1316                         stdstring tempfile;\r
1317                         switch (id_it->second)\r
1318                         {\r
1319                                 //#region case\r
1320                         case ShellMenuCheckout:\r
1321                                 svnCmd += _T("checkout /path:\"");\r
1322                                 svnCmd += folder_;\r
1323                                 svnCmd += _T("\"");\r
1324                                 break;\r
1325                         case ShellMenuUpdate:\r
1326                                 tempfile = WriteFileListToTempFile();\r
1327                                 svnCmd += _T("update /pathfile:\"");\r
1328                                 svnCmd += tempfile;\r
1329                                 svnCmd += _T("\"");\r
1330                                 svnCmd += _T(" /deletepathfile");\r
1331                                 break;\r
1332                         case ShellMenuUpdateExt:\r
1333                                 tempfile = WriteFileListToTempFile();\r
1334                                 svnCmd += _T("update /pathfile:\"");\r
1335                                 svnCmd += tempfile;\r
1336                                 svnCmd += _T("\"");\r
1337                                 svnCmd += _T(" /deletepathfile");\r
1338                                 svnCmd += _T(" /rev");\r
1339                                 break;\r
1340                         case ShellMenuCommit:\r
1341                                 tempfile = WriteFileListToTempFile();\r
1342                                 svnCmd += _T("commit /pathfile:\"");\r
1343                                 svnCmd += tempfile;\r
1344                                 svnCmd += _T("\"");\r
1345                                 svnCmd += _T(" /deletepathfile");\r
1346                                 break;\r
1347                         case ShellMenuAdd:\r
1348                         case ShellMenuAddAsReplacement:\r
1349                                 tempfile = WriteFileListToTempFile();\r
1350                                 svnCmd += _T("add /pathfile:\"");\r
1351                                 svnCmd += tempfile;\r
1352                                 svnCmd += _T("\"");\r
1353                                 svnCmd += _T(" /deletepathfile");\r
1354                                 break;\r
1355                         case ShellMenuIgnore:\r
1356                                 tempfile = WriteFileListToTempFile();\r
1357                                 svnCmd += _T("ignore /pathfile:\"");\r
1358                                 svnCmd += tempfile;\r
1359                                 svnCmd += _T("\"");\r
1360                                 svnCmd += _T(" /deletepathfile");\r
1361                                 break;\r
1362                         case ShellMenuIgnoreCaseSensitive:\r
1363                                 tempfile = WriteFileListToTempFile();\r
1364                                 svnCmd += _T("ignore /pathfile:\"");\r
1365                                 svnCmd += tempfile;\r
1366                                 svnCmd += _T("\"");\r
1367                                 svnCmd += _T(" /deletepathfile");\r
1368                                 svnCmd += _T(" /onlymask");\r
1369                                 break;\r
1370                         case ShellMenuUnIgnore:\r
1371                                 tempfile = WriteFileListToTempFile();\r
1372                                 svnCmd += _T("unignore /pathfile:\"");\r
1373                                 svnCmd += tempfile;\r
1374                                 svnCmd += _T("\"");\r
1375                                 svnCmd += _T(" /deletepathfile");\r
1376                                 break;\r
1377                         case ShellMenuUnIgnoreCaseSensitive:\r
1378                                 tempfile = WriteFileListToTempFile();\r
1379                                 svnCmd += _T("unignore /pathfile:\"");\r
1380                                 svnCmd += tempfile;\r
1381                                 svnCmd += _T("\"");\r
1382                                 svnCmd += _T(" /deletepathfile");\r
1383                                 svnCmd += _T(" /onlymask");\r
1384                                 break;\r
1385                         case ShellMenuRevert:\r
1386                                 tempfile = WriteFileListToTempFile();\r
1387                                 svnCmd += _T("revert /pathfile:\"");\r
1388                                 svnCmd += tempfile;\r
1389                                 svnCmd += _T("\"");\r
1390                                 svnCmd += _T(" /deletepathfile");\r
1391                                 break;\r
1392                         case ShellMenuDelUnversioned:\r
1393                                 svnCmd += _T("delunversioned /path:\"");\r
1394                                 svnCmd += folder_;\r
1395                                 svnCmd += _T("\"");\r
1396                                 break;\r
1397                         case ShellMenuCleanup:\r
1398                                 tempfile = WriteFileListToTempFile();\r
1399                                 svnCmd += _T("cleanup /pathfile:\"");\r
1400                                 svnCmd += tempfile;\r
1401                                 svnCmd += _T("\"");\r
1402                                 svnCmd += _T(" /deletepathfile");\r
1403                                 break;\r
1404                         case ShellMenuResolve:\r
1405                                 tempfile = WriteFileListToTempFile();\r
1406                                 svnCmd += _T("resolve /pathfile:\"");\r
1407                                 svnCmd += tempfile;\r
1408                                 svnCmd += _T("\"");\r
1409                                 svnCmd += _T(" /deletepathfile");\r
1410                                 break;\r
1411                         case ShellMenuSwitch:\r
1412                                 svnCmd += _T("switch /path:\"");\r
1413                                 if (files_.size() > 0)\r
1414                                         svnCmd += files_.front();\r
1415                                 else\r
1416                                         svnCmd += folder_;\r
1417                                 svnCmd += _T("\"");\r
1418                                 break;\r
1419                         case ShellMenuImport:\r
1420                                 svnCmd += _T("import /path:\"");\r
1421                                 svnCmd += folder_;\r
1422                                 svnCmd += _T("\"");\r
1423                                 break;\r
1424                         case ShellMenuExport:\r
1425                                 svnCmd += _T("export /path:\"");\r
1426                                 svnCmd += folder_;\r
1427                                 svnCmd += _T("\"");\r
1428                                 break;\r
1429                         case ShellMenuAbout:\r
1430                                 svnCmd += _T("about");\r
1431                                 break;\r
1432                         case ShellMenuCreateRepos:\r
1433                                 svnCmd += _T("repocreate /path:\"");\r
1434                                 svnCmd += folder_;\r
1435                                 svnCmd += _T("\"");\r
1436                                 break;\r
1437                         case ShellMenuMerge:\r
1438                                 svnCmd += _T("merge /path:\"");\r
1439                                 if (files_.size() > 0)\r
1440                                         svnCmd += files_.front();\r
1441                                 else\r
1442                                         svnCmd += folder_;\r
1443                                 svnCmd += _T("\"");\r
1444                                 break;\r
1445                         case ShellMenuMergeAll:\r
1446                                 svnCmd += _T("mergeall /path:\"");\r
1447                                 if (files_.size() > 0)\r
1448                                         svnCmd += files_.front();\r
1449                                 else\r
1450                                         svnCmd += folder_;\r
1451                                 svnCmd += _T("\"");\r
1452                                 break;\r
1453                         case ShellMenuCopy:\r
1454                                 svnCmd += _T("copy /path:\"");\r
1455                                 if (files_.size() > 0)\r
1456                                         svnCmd += files_.front();\r
1457                                 else\r
1458                                         svnCmd += folder_;\r
1459                                 svnCmd += _T("\"");\r
1460                                 break;\r
1461                         case ShellMenuSettings:\r
1462                                 svnCmd += _T("settings");\r
1463                                 break;\r
1464                         case ShellMenuHelp:\r
1465                                 svnCmd += _T("help");\r
1466                                 break;\r
1467                         case ShellMenuRename:\r
1468                                 svnCmd += _T("rename /path:\"");\r
1469                                 if (files_.size() > 0)\r
1470                                         svnCmd += files_.front();\r
1471                                 else\r
1472                                         svnCmd += folder_;\r
1473                                 svnCmd += _T("\"");\r
1474                                 break;\r
1475                         case ShellMenuRemove:\r
1476                                 tempfile = WriteFileListToTempFile();\r
1477                                 svnCmd += _T("remove /pathfile:\"");\r
1478                                 svnCmd += tempfile;\r
1479                                 svnCmd += _T("\"");\r
1480                                 svnCmd += _T(" /deletepathfile");\r
1481                                 break;\r
1482                         case ShellMenuRemoveKeep:\r
1483                                 tempfile = WriteFileListToTempFile();\r
1484                                 svnCmd += _T("remove /pathfile:\"");\r
1485                                 svnCmd += tempfile;\r
1486                                 svnCmd += _T("\"");\r
1487                                 svnCmd += _T(" /deletepathfile");\r
1488                                 svnCmd += _T(" /keep");\r
1489                                 break;\r
1490                         case ShellMenuDiff:\r
1491                                 svnCmd += _T("diff /path:\"");\r
1492                                 if (files_.size() == 1)\r
1493                                         svnCmd += files_.front();\r
1494                                 else if (files_.size() == 2)\r
1495                                 {\r
1496                                         std::vector<stdstring>::iterator I = files_.begin();\r
1497                                         svnCmd += *I;\r
1498                                         I++;\r
1499                                         svnCmd += _T("\" /path2:\"");\r
1500                                         svnCmd += *I;\r
1501                                 }\r
1502                                 else\r
1503                                         svnCmd += folder_;\r
1504                                 svnCmd += _T("\"");\r
1505                                 if (GetAsyncKeyState(VK_SHIFT) & 0x8000)\r
1506                                         svnCmd += _T(" /alternative");\r
1507                                 break;\r
1508                         case ShellMenuPrevDiff:\r
1509                                 svnCmd += _T("prevdiff /path:\"");\r
1510                                 if (files_.size() == 1)\r
1511                                         svnCmd += files_.front();\r
1512                                 else\r
1513                                         svnCmd += folder_;\r
1514                                 svnCmd += _T("\"");\r
1515                                 if (GetAsyncKeyState(VK_SHIFT) & 0x8000)\r
1516                                         svnCmd += _T(" /alternative");\r
1517                                 break;\r
1518                         case ShellMenuUrlDiff:\r
1519                                 svnCmd += _T("urldiff /path:\"");\r
1520                                 if (files_.size() == 1)\r
1521                                         svnCmd += files_.front();\r
1522                                 else\r
1523                                         svnCmd += folder_;\r
1524                                 svnCmd += _T("\"");\r
1525                                 break;\r
1526                         case ShellMenuDropCopyAdd:\r
1527                                 tempfile = WriteFileListToTempFile();\r
1528                                 svnCmd += _T("dropcopyadd /pathfile:\"");\r
1529                                 svnCmd += tempfile;\r
1530                                 svnCmd += _T("\"");\r
1531                                 svnCmd += _T(" /deletepathfile");\r
1532                                 svnCmd += _T(" /droptarget:\"");\r
1533                                 svnCmd += folder_;\r
1534                                 svnCmd += _T("\"";)\r
1535                                         break;\r
1536                         case ShellMenuDropCopy:\r
1537                                 tempfile = WriteFileListToTempFile();\r
1538                                 svnCmd += _T("dropcopy /pathfile:\"");\r
1539                                 svnCmd += tempfile;\r
1540                                 svnCmd += _T("\"");\r
1541                                 svnCmd += _T(" /deletepathfile");\r
1542                                 svnCmd += _T(" /droptarget:\"");\r
1543                                 svnCmd += folder_;\r
1544                                 svnCmd += _T("\"";)\r
1545                                         break;\r
1546                         case ShellMenuDropCopyRename:\r
1547                                 tempfile = WriteFileListToTempFile();\r
1548                                 svnCmd += _T("dropcopy /pathfile:\"");\r
1549                                 svnCmd += tempfile;\r
1550                                 svnCmd += _T("\"");\r
1551                                 svnCmd += _T(" /deletepathfile");\r
1552                                 svnCmd += _T(" /droptarget:\"");\r
1553                                 svnCmd += folder_;\r
1554                                 svnCmd += _T("\" /rename";)\r
1555                                         break;\r
1556                         case ShellMenuDropMove:\r
1557                                 tempfile = WriteFileListToTempFile();\r
1558                                 svnCmd += _T("dropmove /pathfile:\"");\r
1559                                 svnCmd += tempfile;\r
1560                                 svnCmd += _T("\"");\r
1561                                 svnCmd += _T(" /deletepathfile");\r
1562                                 svnCmd += _T(" /droptarget:\"");\r
1563                                 svnCmd += folder_;\r
1564                                 svnCmd += _T("\"");\r
1565                                 break;\r
1566                         case ShellMenuDropMoveRename:\r
1567                                 tempfile = WriteFileListToTempFile();\r
1568                                 svnCmd += _T("dropmove /pathfile:\"");\r
1569                                 svnCmd += tempfile;\r
1570                                 svnCmd += _T("\"");\r
1571                                 svnCmd += _T(" /deletepathfile");\r
1572                                 svnCmd += _T(" /droptarget:\"");\r
1573                                 svnCmd += folder_;\r
1574                                 svnCmd += _T("\" /rename";)\r
1575                                 break;\r
1576                         case ShellMenuDropExport:\r
1577                                 tempfile = WriteFileListToTempFile();\r
1578                                 svnCmd += _T("dropexport /pathfile:\"");\r
1579                                 svnCmd += tempfile;\r
1580                                 svnCmd += _T("\"");\r
1581                                 svnCmd += _T(" /deletepathfile");\r
1582                                 svnCmd += _T(" /droptarget:\"");\r
1583                                 svnCmd += folder_;\r
1584                                 svnCmd += _T("\"");\r
1585                                 break;\r
1586                         case ShellMenuDropExportExtended:\r
1587                                 tempfile = WriteFileListToTempFile();\r
1588                                 svnCmd += _T("dropexport /pathfile:\"");\r
1589                                 svnCmd += tempfile;\r
1590                                 svnCmd += _T("\"");\r
1591                                 svnCmd += _T(" /deletepathfile");\r
1592                                 svnCmd += _T(" /droptarget:\"");\r
1593                                 svnCmd += folder_;\r
1594                                 svnCmd += _T("\"");\r
1595                                 svnCmd += _T(" /extended");\r
1596                                 break;\r
1597                         case ShellMenuLog:\r
1598                                 svnCmd += _T("log /path:\"");\r
1599                                 if (files_.size() > 0)\r
1600                                         svnCmd += files_.front();\r
1601                                 else\r
1602                                         svnCmd += folder_;\r
1603                                 svnCmd += _T("\"");\r
1604                                 break;\r
1605                         case ShellMenuConflictEditor:\r
1606                                 svnCmd += _T("conflicteditor /path:\"");\r
1607                                 if (files_.size() > 0)\r
1608                                         svnCmd += files_.front();\r
1609                                 else\r
1610                                         svnCmd += folder_;\r
1611                                 svnCmd += _T("\"");\r
1612                                 break;\r
1613                         case ShellMenuRelocate:\r
1614                                 svnCmd += _T("relocate /path:\"");\r
1615                                 if (files_.size() > 0)\r
1616                                         svnCmd += files_.front();\r
1617                                 else\r
1618                                         svnCmd += folder_;\r
1619                                 svnCmd += _T("\"");\r
1620                                 break;\r
1621                         case ShellMenuRebase:\r
1622                                 svnCmd += _T("rebase /path:\"");\r
1623                                 if (files_.size() > 0)\r
1624                                         svnCmd += files_.front();\r
1625                                 else\r
1626                                         svnCmd += folder_;\r
1627                                 svnCmd += _T("\"");\r
1628                                 break;\r
1629                         case ShellMenuShowChanged:\r
1630                                 if (files_.size() > 1)\r
1631                 {\r
1632                                     tempfile = WriteFileListToTempFile();\r
1633                                     svnCmd += _T("repostatus /pathfile:\"");\r
1634                                     svnCmd += tempfile;\r
1635                                 svnCmd += _T("\"");\r
1636                                 svnCmd += _T(" /deletepathfile");\r
1637                 }\r
1638                 else\r
1639                 {\r
1640                     svnCmd += _T("repostatus /path:\"");\r
1641                                     if (files_.size() > 0)\r
1642                                             svnCmd += files_.front();\r
1643                                     else\r
1644                                             svnCmd += folder_;\r
1645                                 svnCmd += _T("\"");\r
1646                 }\r
1647                                 break;\r
1648                         case ShellMenuRepoBrowse:\r
1649                                 svnCmd += _T("repobrowser /path:\"");\r
1650                                 if (files_.size() > 0)\r
1651                                         svnCmd += files_.front();\r
1652                                 else\r
1653                                         svnCmd += folder_;\r
1654                                 svnCmd += _T("\"");\r
1655                                 break;\r
1656                         case ShellMenuBlame:\r
1657                                 svnCmd += _T("blame /path:\"");\r
1658                                 if (files_.size() > 0)\r
1659                                         svnCmd += files_.front();\r
1660                                 else\r
1661                                         svnCmd += folder_;\r
1662                                 svnCmd += _T("\"");\r
1663                                 break;\r
1664                         case ShellMenuCreatePatch:\r
1665                                 tempfile = WriteFileListToTempFile();\r
1666                                 svnCmd += _T("createpatch /pathfile:\"");\r
1667                                 svnCmd += tempfile;\r
1668                                 svnCmd += _T("\"");\r
1669                                 svnCmd += _T(" /deletepathfile");\r
1670                                 break;\r
1671                         case ShellMenuApplyPatch:\r
1672                                 if ((itemStates & ITEMIS_PATCHINCLIPBOARD) && ((~itemStates) & ITEMIS_PATCHFILE))\r
1673                                 {\r
1674                                         // if there's a patch file in the clipboard, we save it\r
1675                                         // to a temporary file and tell TortoiseMerge to use that one\r
1676                                         UINT cFormat = RegisterClipboardFormat(_T("TGIT_UNIFIEDDIFF"));\r
1677                                         if ((cFormat)&&(OpenClipboard(NULL)))\r
1678                                         { \r
1679                                                 HGLOBAL hglb = GetClipboardData(cFormat); \r
1680                                                 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb); \r
1681 \r
1682                                                 DWORD len = GetTempPath(0, NULL);\r
1683                                                 TCHAR * path = new TCHAR[len+1];\r
1684                                                 TCHAR * tempF = new TCHAR[len+100];\r
1685                                                 GetTempPath (len+1, path);\r
1686                                                 GetTempFileName (path, TEXT("git"), 0, tempF);\r
1687                                                 std::wstring sTempFile = std::wstring(tempF);\r
1688                                                 delete [] path;\r
1689                                                 delete [] tempF;\r
1690 \r
1691                                                 FILE * outFile;\r
1692                                                 size_t patchlen = strlen(lpstr);\r
1693                                                 _tfopen_s(&outFile, sTempFile.c_str(), _T("wb"));\r
1694                                                 if(outFile)\r
1695                                                 {\r
1696                                                         size_t size = fwrite(lpstr, sizeof(char), patchlen, outFile);\r
1697                                                         if (size == patchlen)\r
1698                                                         {\r
1699                                                                 itemStates |= ITEMIS_PATCHFILE;\r
1700                                                                 files_.clear();\r
1701                                                                 files_.push_back(sTempFile);\r
1702                                                         }\r
1703                                                         fclose(outFile);\r
1704                                                 }\r
1705                                                 GlobalUnlock(hglb); \r
1706                                                 CloseClipboard(); \r
1707                                         } \r
1708                                 }\r
1709                                 if (itemStates & ITEMIS_PATCHFILE)\r
1710                                 {\r
1711                                         svnCmd = _T(" /diff:\"");\r
1712                                         if (files_.size() > 0)\r
1713                                         {\r
1714                                                 svnCmd += files_.front();\r
1715                                                 if (itemStatesFolder & ITEMIS_FOLDERINSVN)\r
1716                                                 {\r
1717                                                         svnCmd += _T("\" /patchpath:\"");\r
1718                                                         svnCmd += folder_;\r
1719                                                 }\r
1720                                         }\r
1721                                         else\r
1722                                                 svnCmd += folder_;\r
1723                                         if (itemStates & ITEMIS_INVERSIONEDFOLDER)\r
1724                                                 svnCmd += _T("\" /wc");\r
1725                                         else\r
1726                                                 svnCmd += _T("\"");\r
1727                                 }\r
1728                                 else\r
1729                                 {\r
1730                                         svnCmd = _T(" /patchpath:\"");\r
1731                                         if (files_.size() > 0)\r
1732                                                 svnCmd += files_.front();\r
1733                                         else\r
1734                                                 svnCmd += folder_;\r
1735                                         svnCmd += _T("\"");\r
1736                                 }\r
1737                                 myIDMap.clear();\r
1738                                 myVerbsIDMap.clear();\r
1739                                 myVerbsMap.clear();\r
1740                                 if (CreateProcess(((stdstring)tortoiseMergePath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)\r
1741                                 {\r
1742                                         LPVOID lpMsgBuf;\r
1743                                         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
1744                                                 FORMAT_MESSAGE_FROM_SYSTEM | \r
1745                                                 FORMAT_MESSAGE_IGNORE_INSERTS,\r
1746                                                 NULL,\r
1747                                                 GetLastError(),\r
1748                                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language\r
1749                                                 (LPTSTR) &lpMsgBuf,\r
1750                                                 0,\r
1751                                                 NULL \r
1752                                                 );\r
1753                                         MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseMerge launch failed"), MB_OK | MB_ICONINFORMATION );\r
1754                                         LocalFree( lpMsgBuf );\r
1755                                 }\r
1756                                 CloseHandle(process.hThread);\r
1757                                 CloseHandle(process.hProcess);\r
1758                                 return NOERROR;\r
1759                                 break;\r
1760                         case ShellMenuRevisionGraph:\r
1761                                 svnCmd += _T("revisiongraph /path:\"");\r
1762                                 if (files_.size() > 0)\r
1763                                         svnCmd += files_.front();\r
1764                                 else\r
1765                                         svnCmd += folder_;\r
1766                                 svnCmd += _T("\"");\r
1767                                 break;\r
1768                         case ShellMenuProperties:\r
1769                                 tempfile = WriteFileListToTempFile();\r
1770                                 svnCmd += _T("properties /pathfile:\"");\r
1771                                 svnCmd += tempfile;\r
1772                                 svnCmd += _T("\"");\r
1773                                 svnCmd += _T(" /deletepathfile");\r
1774                                 break;\r
1775                         case ShellMenuClipPaste:\r
1776                                 if (WriteClipboardPathsToTempFile(tempfile))\r
1777                                 {\r
1778                                         bool bCopy = true;\r
1779                                         UINT cPrefDropFormat = RegisterClipboardFormat(_T("Preferred DropEffect"));\r
1780                                         if (cPrefDropFormat)\r
1781                                         {\r
1782                                                 if (OpenClipboard(lpcmi->hwnd))\r
1783                                                 {\r
1784                                                         HGLOBAL hglb = GetClipboardData(cPrefDropFormat);\r
1785                                                         if (hglb)\r
1786                                                         {\r
1787                                                                 DWORD* effect = (DWORD*) GlobalLock(hglb);\r
1788                                                                 if (*effect == DROPEFFECT_MOVE)\r
1789                                                                         bCopy = false;\r
1790                                                                 GlobalUnlock(hglb);\r
1791                                                         }\r
1792                                                         CloseClipboard();\r
1793                                                 }\r
1794                                         }\r
1795 \r
1796                                         if (bCopy)\r
1797                                                 svnCmd += _T("pastecopy /pathfile:\"");\r
1798                                         else\r
1799                                                 svnCmd += _T("pastemove /pathfile:\"");\r
1800                                         svnCmd += tempfile;\r
1801                                         svnCmd += _T("\"");\r
1802                                         svnCmd += _T(" /deletepathfile");\r
1803                                         svnCmd += _T(" /droptarget:\"");\r
1804                                         svnCmd += folder_;\r
1805                                         svnCmd += _T("\"");\r
1806                                 }\r
1807                                 else return NOERROR;\r
1808                                 break;\r
1809                         case ShellMenuClone:\r
1810                                 svnCmd += _T("clone /path:\"");\r
1811                                 svnCmd += folder_;\r
1812                                 svnCmd += _T("\"");\r
1813                                 break;\r
1814                         case ShellMenuPull:\r
1815                                 svnCmd += _T("pull /path:\"");\r
1816                                 if (files_.size() > 0)\r
1817                                         svnCmd += files_.front();\r
1818                                 else\r
1819                                         svnCmd += folder_;\r
1820                                 svnCmd += _T("\"");\r
1821                                 break;\r
1822                         case ShellMenuPush:\r
1823                                 svnCmd += _T("push /path:\"");\r
1824                                 if (files_.size() > 0)\r
1825                                         svnCmd += files_.front();\r
1826                                 else\r
1827                                         svnCmd += folder_;\r
1828                                 svnCmd += _T("\"");\r
1829                                 break;\r
1830                         case ShellMenuBranch:\r
1831                                 svnCmd += _T("branch /path:\"");\r
1832                                 if (files_.size() > 0)\r
1833                                         svnCmd += files_.front();\r
1834                                 else\r
1835                                         svnCmd += folder_;\r
1836                                 svnCmd += _T("\"");\r
1837                                 break;\r
1838                         \r
1839                         case ShellMenuTag:\r
1840                                 svnCmd += _T("tag /path:\"");\r
1841                                 if (files_.size() > 0)\r
1842                                         svnCmd += files_.front();\r
1843                                 else\r
1844                                         svnCmd += folder_;\r
1845                                 svnCmd += _T("\"");\r
1846                                 break;\r
1847 \r
1848                         case ShellMenuFormatPatch:\r
1849                                 svnCmd += _T("formatpatch /path:\"");\r
1850                                 if (files_.size() > 0)\r
1851                                         svnCmd += files_.front();\r
1852                                 else\r
1853                                         svnCmd += folder_;\r
1854                                 svnCmd += _T("\"");\r
1855                                 break;\r
1856 \r
1857                         case ShellMenuImportPatch:\r
1858                                 svnCmd += _T("importpatch /path:\"");\r
1859                                 if (files_.size() > 0)\r
1860                                         svnCmd += files_.front();\r
1861                                 else\r
1862                                         svnCmd += folder_;\r
1863                                 svnCmd += _T("\"");\r
1864                                 break;\r
1865 \r
1866                         case ShellMenuCherryPick:\r
1867                                 svnCmd += _T("cherrypick /path:\"");\r
1868                                 if (files_.size() > 0)\r
1869                                         svnCmd += files_.front();\r
1870                                 else\r
1871                                         svnCmd += folder_;\r
1872                                 svnCmd += _T("\"");\r
1873                                 break;\r
1874                         case ShellMenuFetch:\r
1875                                 svnCmd += _T("fetch /path:\"");\r
1876                                 if (files_.size() > 0)\r
1877                                         svnCmd += files_.front();\r
1878                                 else\r
1879                                         svnCmd += folder_;\r
1880                                 svnCmd += _T("\"");\r
1881                                 break;\r
1882 \r
1883                         default:\r
1884                                 break;\r
1885                                 //#endregion\r
1886                         } // switch (id_it->second) \r
1887                         svnCmd += _T(" /hwnd:");\r
1888                         TCHAR buf[30];\r
1889                         _stprintf_s(buf, 30, _T("%d"), lpcmi->hwnd);\r
1890                         svnCmd += buf;\r
1891                         myIDMap.clear();\r
1892                         myVerbsIDMap.clear();\r
1893                         myVerbsMap.clear();\r
1894                         if (CreateProcess(((stdstring)tortoiseProcPath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)\r
1895                         {\r
1896                                 LPVOID lpMsgBuf;\r
1897                                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
1898                                         FORMAT_MESSAGE_FROM_SYSTEM | \r
1899                                         FORMAT_MESSAGE_IGNORE_INSERTS,\r
1900                                         NULL,\r
1901                                         GetLastError(),\r
1902                                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language\r
1903                                         (LPTSTR) &lpMsgBuf,\r
1904                                         0,\r
1905                                         NULL \r
1906                                         );\r
1907                                 MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseProc Launch failed"), MB_OK | MB_ICONINFORMATION );\r
1908                                 LocalFree( lpMsgBuf );\r
1909                         }\r
1910                         CloseHandle(process.hThread);\r
1911                         CloseHandle(process.hProcess);\r
1912                         hr = NOERROR;\r
1913                 } // if (id_it != myIDMap.end() && id_it->first == idCmd) \r
1914         } // if ((files_.size() > 0)||(folder_.size() > 0)) \r
1915         return hr;\r
1916 \r
1917 }\r
1918 \r
1919 // This is for the status bar and things like that:\r
1920 STDMETHODIMP CShellExt::GetCommandString(UINT_PTR idCmd,\r
1921                                          UINT uFlags,\r
1922                                          UINT FAR * /*reserved*/,\r
1923                                          LPSTR pszName,\r
1924                                          UINT cchMax)\r
1925 {\r
1926         PreserveChdir preserveChdir;\r
1927         //do we know the id?\r
1928         std::map<UINT_PTR, UINT_PTR>::const_iterator id_it = myIDMap.lower_bound(idCmd);\r
1929         if (id_it == myIDMap.end() || id_it->first != idCmd)\r
1930         {\r
1931                 return E_INVALIDARG;            //no, we don't\r
1932         }\r
1933 \r
1934         LoadLangDll();\r
1935         HRESULT hr = E_INVALIDARG;\r
1936 \r
1937         MAKESTRING(IDS_MENUDESCDEFAULT);\r
1938         int menuIndex = 0;\r
1939         while (menuInfo[menuIndex].command != ShellMenuLastEntry)\r
1940         {\r
1941                 if (menuInfo[menuIndex].command == (GitCommands)id_it->second)\r
1942                 {\r
1943                         MAKESTRING(menuInfo[menuIndex].menuDescID);\r
1944                         break;\r
1945                 }\r
1946                 menuIndex++;\r
1947         }\r
1948 \r
1949         const TCHAR * desc = stringtablebuffer;\r
1950         switch(uFlags)\r
1951         {\r
1952         case GCS_HELPTEXTA:\r
1953                 {\r
1954                         std::string help = WideToMultibyte(desc);\r
1955                         lstrcpynA(pszName, help.c_str(), cchMax);\r
1956                         hr = S_OK;\r
1957                         break; \r
1958                 }\r
1959         case GCS_HELPTEXTW: \r
1960                 {\r
1961                         wide_string help = desc;\r
1962                         lstrcpynW((LPWSTR)pszName, help.c_str(), cchMax); \r
1963                         hr = S_OK;\r
1964                         break; \r
1965                 }\r
1966         case GCS_VERBA:\r
1967                 {\r
1968                         std::map<UINT_PTR, stdstring>::const_iterator verb_id_it = myVerbsIDMap.lower_bound(idCmd);\r
1969                         if (verb_id_it != myVerbsIDMap.end() && verb_id_it->first == idCmd)\r
1970                         {\r
1971                                 std::string help = WideToMultibyte(verb_id_it->second);\r
1972                                 lstrcpynA(pszName, help.c_str(), cchMax);\r
1973                                 hr = S_OK;\r
1974                         }\r
1975                 }\r
1976                 break;\r
1977         case GCS_VERBW:\r
1978                 {\r
1979                         std::map<UINT_PTR, stdstring>::const_iterator verb_id_it = myVerbsIDMap.lower_bound(idCmd);\r
1980                         if (verb_id_it != myVerbsIDMap.end() && verb_id_it->first == idCmd)\r
1981                         {\r
1982                                 wide_string help = verb_id_it->second;\r
1983                                 ATLTRACE("verb : %ws\n", help.c_str());\r
1984                                 lstrcpynW((LPWSTR)pszName, help.c_str(), cchMax); \r
1985                                 hr = S_OK;\r
1986                         }\r
1987                 }\r
1988                 break;\r
1989         }\r
1990         return hr;\r
1991 }\r
1992 \r
1993 STDMETHODIMP CShellExt::HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
1994 {\r
1995         LRESULT res;\r
1996         return HandleMenuMsg2(uMsg, wParam, lParam, &res);\r
1997 }\r
1998 \r
1999 STDMETHODIMP CShellExt::HandleMenuMsg2(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *pResult)\r
2000 {\r
2001         PreserveChdir preserveChdir;\r
2002 \r
2003         LRESULT res;\r
2004         if (pResult == NULL)\r
2005                 pResult = &res;\r
2006         *pResult = FALSE;\r
2007 \r
2008         LoadLangDll();\r
2009         switch (uMsg)\r
2010         {\r
2011         case WM_MEASUREITEM:\r
2012                 {\r
2013                         MEASUREITEMSTRUCT* lpmis = (MEASUREITEMSTRUCT*)lParam;\r
2014                         if (lpmis==NULL)\r
2015                                 break;\r
2016                         lpmis->itemWidth += 2;\r
2017                         if (lpmis->itemHeight < 16)\r
2018                                 lpmis->itemHeight = 16;\r
2019                         *pResult = TRUE;\r
2020                 }\r
2021                 break;\r
2022         case WM_DRAWITEM:\r
2023                 {\r
2024                         LPCTSTR resource;\r
2025                         DRAWITEMSTRUCT* lpdis = (DRAWITEMSTRUCT*)lParam;\r
2026                         if ((lpdis==NULL)||(lpdis->CtlType != ODT_MENU))\r
2027                                 return S_OK;            //not for a menu\r
2028                         resource = GetMenuTextFromResource(myIDMap[lpdis->itemID]);\r
2029                         if (resource == NULL)\r
2030                                 return S_OK;\r
2031                         HICON hIcon = (HICON)LoadImage(g_hResInst, resource, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);\r
2032                         if (hIcon == NULL)\r
2033                                 return S_OK;\r
2034                         DrawIconEx(lpdis->hDC,\r
2035                                 lpdis->rcItem.left - 16,\r
2036                                 lpdis->rcItem.top + (lpdis->rcItem.bottom - lpdis->rcItem.top - 16) / 2,\r
2037                                 hIcon, 16, 16,\r
2038                                 0, NULL, DI_NORMAL);\r
2039                         DestroyIcon(hIcon);\r
2040                         *pResult = TRUE;\r
2041                 }\r
2042                 break;\r
2043         case WM_MENUCHAR:\r
2044                 {\r
2045                         LPCTSTR resource;\r
2046                         TCHAR *szItem;\r
2047                         if (HIWORD(wParam) != MF_POPUP)\r
2048                                 return NOERROR;\r
2049                         int nChar = LOWORD(wParam);\r
2050                         if (_istascii((wint_t)nChar) && _istupper((wint_t)nChar))\r
2051                                 nChar = tolower(nChar);\r
2052                         // we have the char the user pressed, now search that char in all our\r
2053                         // menu items\r
2054                         std::vector<int> accmenus;\r
2055                         for (std::map<UINT_PTR, UINT_PTR>::iterator It = mySubMenuMap.begin(); It != mySubMenuMap.end(); ++It)\r
2056                         {\r
2057                                 resource = GetMenuTextFromResource(mySubMenuMap[It->first]);\r
2058                                 if (resource == NULL)\r
2059                                         continue;\r
2060                                 szItem = stringtablebuffer;\r
2061                                 TCHAR * amp = _tcschr(szItem, '&');\r
2062                                 if (amp == NULL)\r
2063                                         continue;\r
2064                                 amp++;\r
2065                                 int ampChar = LOWORD(*amp);\r
2066                                 if (_istascii((wint_t)ampChar) && _istupper((wint_t)ampChar))\r
2067                                         ampChar = tolower(ampChar);\r
2068                                 if (ampChar == nChar)\r
2069                                 {\r
2070                                         // yep, we found a menu which has the pressed key\r
2071                                         // as an accelerator. Add that menu to the list to\r
2072                                         // process later.\r
2073                                         accmenus.push_back(It->first);\r
2074                                 }\r
2075                         }\r
2076                         if (accmenus.size() == 0)\r
2077                         {\r
2078                                 // no menu with that accelerator key.\r
2079                                 *pResult = MAKELONG(0, MNC_IGNORE);\r
2080                                 return NOERROR;\r
2081                         }\r
2082                         if (accmenus.size() == 1)\r
2083                         {\r
2084                                 // Only one menu with that accelerator key. We're lucky!\r
2085                                 // So just execute that menu entry.\r
2086                                 *pResult = MAKELONG(accmenus[0], MNC_EXECUTE);\r
2087                                 return NOERROR;\r
2088                         }\r
2089                         if (accmenus.size() > 1)\r
2090                         {\r
2091                                 // we have more than one menu item with this accelerator key!\r
2092                                 MENUITEMINFO mif;\r
2093                                 mif.cbSize = sizeof(MENUITEMINFO);\r
2094                                 mif.fMask = MIIM_STATE;\r
2095                                 for (std::vector<int>::iterator it = accmenus.begin(); it != accmenus.end(); ++it)\r
2096                                 {\r
2097                                         GetMenuItemInfo((HMENU)lParam, *it, TRUE, &mif);\r
2098                                         if (mif.fState == MFS_HILITE)\r
2099                                         {\r
2100                                                 // this is the selected item, so select the next one\r
2101                                                 ++it;\r
2102                                                 if (it == accmenus.end())\r
2103                                                         *pResult = MAKELONG(accmenus[0], MNC_SELECT);\r
2104                                                 else\r
2105                                                         *pResult = MAKELONG(*it, MNC_SELECT);\r
2106                                                 return NOERROR;\r
2107                                         }\r
2108                                 }\r
2109                                 *pResult = MAKELONG(accmenus[0], MNC_SELECT);\r
2110                         }\r
2111                 }\r
2112                 break;\r
2113         default:\r
2114                 return NOERROR;\r
2115         }\r
2116 \r
2117         return NOERROR;\r
2118 }\r
2119 \r
2120 LPCTSTR CShellExt::GetMenuTextFromResource(int id)\r
2121 {\r
2122         TCHAR textbuf[255];\r
2123         LPCTSTR resource = NULL;\r
2124         unsigned __int64 layout = g_ShellCache.GetMenuLayout();\r
2125         space = 6;\r
2126 \r
2127         int menuIndex = 0;\r
2128         while (menuInfo[menuIndex].command != ShellMenuLastEntry)\r
2129         {\r
2130                 if (menuInfo[menuIndex].command == id)\r
2131                 {\r
2132                         MAKESTRING(menuInfo[menuIndex].menuTextID);\r
2133                         resource = MAKEINTRESOURCE(menuInfo[menuIndex].iconID);\r
2134                         switch (id)\r
2135                         {\r
2136                         case ShellSubMenuMultiple:\r
2137                         case ShellSubMenuLink:\r
2138                         case ShellSubMenuFolder:\r
2139                         case ShellSubMenuFile:\r
2140                         case ShellSubMenu:\r
2141                                 space = 0;\r
2142                                 break;\r
2143                         default:\r
2144                                 space = layout & menuInfo[menuIndex].menuID ? 0 : 6;\r
2145                                 if (layout & (menuInfo[menuIndex].menuID)) \r
2146                                 {\r
2147                                         _tcscpy_s(textbuf, 255, _T("Git "));\r
2148                                         _tcscat_s(textbuf, 255, stringtablebuffer);\r
2149                                         _tcscpy_s(stringtablebuffer, 255, textbuf);\r
2150                                 }\r
2151                                 break;\r
2152                         }\r
2153                         return resource;\r
2154                 }\r
2155                 menuIndex++;\r
2156         }\r
2157         return NULL;\r
2158 }\r
2159 \r
2160 bool CShellExt::IsIllegalFolder(std::wstring folder, int * cslidarray)\r
2161 {\r
2162         int i=0;\r
2163         TCHAR buf[MAX_PATH];    //MAX_PATH ok, since SHGetSpecialFolderPath doesn't return the required buffer length!\r
2164         LPITEMIDLIST pidl = NULL;\r
2165         while (cslidarray[i])\r
2166         {\r
2167                 ++i;\r
2168                 pidl = NULL;\r
2169                 if (SHGetFolderLocation(NULL, cslidarray[i-1], NULL, 0, &pidl)!=S_OK)\r
2170                         continue;\r
2171                 if (!SHGetPathFromIDList(pidl, buf))\r
2172                 {\r
2173                         // not a file system path, definitely illegal for our use\r
2174                         CoTaskMemFree(pidl);\r
2175                         continue;\r
2176                 }\r
2177                 CoTaskMemFree(pidl);\r
2178                 if (_tcslen(buf)==0)\r
2179                         continue;\r
2180                 if (_tcscmp(buf, folder.c_str())==0)\r
2181                         return true;\r
2182         }\r
2183         return false;\r
2184 }\r
2185 \r
2186 void CShellExt::InsertIgnoreSubmenus(UINT &idCmd, UINT idCmdFirst, HMENU hMenu, HMENU subMenu, UINT &indexMenu, int &indexSubMenu, unsigned __int64 topmenu, bool bShowIcons)\r
2187 {\r
2188         HMENU ignoresubmenu = NULL;\r
2189         int indexignoresub = 0;\r
2190         bool bShowIgnoreMenu = false;\r
2191         TCHAR maskbuf[MAX_PATH];                // MAX_PATH is ok, since this only holds a filename\r
2192         TCHAR ignorepath[MAX_PATH];             // MAX_PATH is ok, since this only holds a filename\r
2193         if (files_.size() == 0)\r
2194                 return;\r
2195         UINT icon = bShowIcons ? IDI_IGNORE : 0;\r
2196 \r
2197         std::vector<stdstring>::iterator I = files_.begin();\r
2198         if (_tcsrchr(I->c_str(), '\\'))\r
2199                 _tcscpy_s(ignorepath, MAX_PATH, _tcsrchr(I->c_str(), '\\')+1);\r
2200         else\r
2201                 _tcscpy_s(ignorepath, MAX_PATH, I->c_str());\r
2202         if ((itemStates & ITEMIS_IGNORED)&&(ignoredprops.size() > 0))\r
2203         {\r
2204                 // check if the item name is ignored or the mask\r
2205                 size_t p = 0;\r
2206                 while ( (p=ignoredprops.find( ignorepath,p )) != -1 )\r
2207                 {\r
2208                         if ( (p==0 || ignoredprops[p-1]==TCHAR('\n'))\r
2209                                 && (p+_tcslen(ignorepath)==ignoredprops.length() || ignoredprops[p+_tcslen(ignorepath)+1]==TCHAR('\n')) )\r
2210                         {\r
2211                                 break;\r
2212                         }\r
2213                         p++;\r
2214                 }\r
2215                 if (p!=-1)\r
2216                 {\r
2217                         ignoresubmenu = CreateMenu();\r
2218                         InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);\r
2219                         stdstring verb = stdstring(ignorepath);\r
2220                         myVerbsMap[verb] = idCmd - idCmdFirst;\r
2221                         myVerbsMap[verb] = idCmd;\r
2222                         myVerbsIDMap[idCmd - idCmdFirst] = verb;\r
2223                         myVerbsIDMap[idCmd] = verb;\r
2224                         myIDMap[idCmd - idCmdFirst] = ShellMenuUnIgnore;\r
2225                         myIDMap[idCmd++] = ShellMenuUnIgnore;\r
2226                         bShowIgnoreMenu = true;\r
2227                 }\r
2228                 _tcscpy_s(maskbuf, MAX_PATH, _T("*"));\r
2229                 if (_tcsrchr(ignorepath, '.'))\r
2230                 {\r
2231                         _tcscat_s(maskbuf, MAX_PATH, _tcsrchr(ignorepath, '.'));\r
2232                         p = ignoredprops.find(maskbuf);\r
2233                         if ((p!=-1) &&\r
2234                                 ((ignoredprops.compare(maskbuf)==0) || (ignoredprops.find('\n', p)==p+_tcslen(maskbuf)+1) || (ignoredprops.rfind('\n', p)==p-1)))\r
2235                         {\r
2236                                 if (ignoresubmenu==NULL)\r
2237                                         ignoresubmenu = CreateMenu();\r
2238 \r
2239                                 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, maskbuf);\r
2240                                 stdstring verb = stdstring(maskbuf);\r
2241                                 myVerbsMap[verb] = idCmd - idCmdFirst;\r
2242                                 myVerbsMap[verb] = idCmd;\r
2243                                 myVerbsIDMap[idCmd - idCmdFirst] = verb;\r
2244                                 myVerbsIDMap[idCmd] = verb;\r
2245                                 myIDMap[idCmd - idCmdFirst] = ShellMenuUnIgnoreCaseSensitive;\r
2246                                 myIDMap[idCmd++] = ShellMenuUnIgnoreCaseSensitive;\r
2247                                 bShowIgnoreMenu = true;\r
2248                         }\r
2249                 }\r
2250         }\r
2251         else if ((itemStates & ITEMIS_IGNORED) == 0)\r
2252         {\r
2253                 bShowIgnoreMenu = true;\r
2254                 ignoresubmenu = CreateMenu();\r
2255                 if (itemStates & ITEMIS_ONLYONE)\r
2256                 {\r
2257                         InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);\r
2258                         myIDMap[idCmd - idCmdFirst] = ShellMenuIgnore;\r
2259                         myIDMap[idCmd++] = ShellMenuIgnore;\r
2260 \r
2261                         _tcscpy_s(maskbuf, MAX_PATH, _T("*"));\r
2262                         if (_tcsrchr(ignorepath, '.'))\r
2263                         {\r
2264                                 _tcscat_s(maskbuf, MAX_PATH, _tcsrchr(ignorepath, '.'));\r
2265                                 InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, maskbuf);\r
2266                                 stdstring verb = stdstring(maskbuf);\r
2267                                 myVerbsMap[verb] = idCmd - idCmdFirst;\r
2268                                 myVerbsMap[verb] = idCmd;\r
2269                                 myVerbsIDMap[idCmd - idCmdFirst] = verb;\r
2270                                 myVerbsIDMap[idCmd] = verb;\r
2271                                 myIDMap[idCmd - idCmdFirst] = ShellMenuIgnoreCaseSensitive;\r
2272                                 myIDMap[idCmd++] = ShellMenuIgnoreCaseSensitive;\r
2273                         }\r
2274                 }\r
2275                 else\r
2276                 {\r
2277                         MAKESTRING(IDS_MENUIGNOREMULTIPLE);\r
2278                         _stprintf_s(ignorepath, MAX_PATH, stringtablebuffer, files_.size());\r
2279                         InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);\r
2280                         stdstring verb = stdstring(ignorepath);\r
2281                         myVerbsMap[verb] = idCmd - idCmdFirst;\r
2282                         myVerbsMap[verb] = idCmd;\r
2283                         myVerbsIDMap[idCmd - idCmdFirst] = verb;\r
2284                         myVerbsIDMap[idCmd] = verb;\r
2285                         myIDMap[idCmd - idCmdFirst] = ShellMenuIgnore;\r
2286                         myIDMap[idCmd++] = ShellMenuIgnore;\r
2287 \r
2288                         MAKESTRING(IDS_MENUIGNOREMULTIPLEMASK);\r
2289                         _stprintf_s(ignorepath, MAX_PATH, stringtablebuffer, files_.size());\r
2290                         InsertMenu(ignoresubmenu, indexignoresub++, MF_BYPOSITION | MF_STRING , idCmd, ignorepath);\r
2291                         verb = stdstring(ignorepath);\r
2292                         myVerbsMap[verb] = idCmd - idCmdFirst;\r
2293                         myVerbsMap[verb] = idCmd;\r
2294                         myVerbsIDMap[idCmd - idCmdFirst] = verb;\r
2295                         myVerbsIDMap[idCmd] = verb;\r
2296                         myIDMap[idCmd - idCmdFirst] = ShellMenuIgnoreCaseSensitive;\r
2297                         myIDMap[idCmd++] = ShellMenuIgnoreCaseSensitive;\r
2298                 }\r
2299         }\r
2300 \r
2301         if (bShowIgnoreMenu)\r
2302         {\r
2303                 MENUITEMINFO menuiteminfo;\r
2304                 SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));\r
2305                 menuiteminfo.cbSize = sizeof(menuiteminfo);\r
2306                 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_SUBMENU | MIIM_DATA | MIIM_BITMAP | MIIM_STRING;\r
2307                 menuiteminfo.fType = MFT_STRING;\r
2308                 HBITMAP bmp = (fullver >= 0x600) ? IconToBitmapPARGB32(icon) : IconToBitmap(icon);\r
2309                 if (icon)\r
2310                         menuiteminfo.hbmpItem = (fullver >= 0x600) ? bmp : HBMMENU_CALLBACK;\r
2311                 menuiteminfo.hbmpChecked = bmp;\r
2312                 menuiteminfo.hbmpUnchecked = bmp;\r
2313                 menuiteminfo.hSubMenu = ignoresubmenu;\r
2314                 menuiteminfo.wID = idCmd;\r
2315                 SecureZeroMemory(stringtablebuffer, sizeof(stringtablebuffer));\r
2316                 if (itemStates & ITEMIS_IGNORED)\r
2317                         GetMenuTextFromResource(ShellMenuUnIgnoreSub);\r
2318                 else\r
2319                         GetMenuTextFromResource(ShellMenuIgnoreSub);\r
2320                 menuiteminfo.dwTypeData = stringtablebuffer;\r
2321                 menuiteminfo.cch = (UINT)min(_tcslen(menuiteminfo.dwTypeData), UINT_MAX);\r
2322 \r
2323                 InsertMenuItem((topmenu & MENUIGNORE) ? hMenu : subMenu, (topmenu & MENUIGNORE) ? indexMenu++ : indexSubMenu++, TRUE, &menuiteminfo);\r
2324                 if (itemStates & ITEMIS_IGNORED)\r
2325                 {\r
2326                         myIDMap[idCmd - idCmdFirst] = ShellMenuUnIgnoreSub;\r
2327                         myIDMap[idCmd++] = ShellMenuUnIgnoreSub;\r
2328                 }\r
2329                 else\r
2330                 {\r
2331                         myIDMap[idCmd - idCmdFirst] = ShellMenuIgnoreSub;\r
2332                         myIDMap[idCmd++] = ShellMenuIgnoreSub;\r
2333                 }\r
2334         }\r
2335 }\r
2336 \r
2337 HBITMAP CShellExt::IconToBitmapPARGB32(UINT uIcon)\r
2338 {\r
2339         std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);\r
2340         if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)\r
2341                 return bitmap_it->second;\r
2342 \r
2343         HICON hIcon = (HICON)LoadImage(g_hResInst, MAKEINTRESOURCE(uIcon), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);\r
2344         if (!hIcon)\r
2345                 return NULL;\r
2346 \r
2347         if (pfnBeginBufferedPaint == NULL || pfnEndBufferedPaint == NULL || pfnGetBufferedPaintBits == NULL)\r
2348                 return NULL;\r
2349 \r
2350         SIZE sizIcon;\r
2351         sizIcon.cx = GetSystemMetrics(SM_CXSMICON);\r
2352         sizIcon.cy = GetSystemMetrics(SM_CYSMICON);\r
2353 \r
2354         RECT rcIcon;\r
2355         SetRect(&rcIcon, 0, 0, sizIcon.cx, sizIcon.cy);\r
2356         HBITMAP hBmp = NULL;\r
2357 \r
2358         HDC hdcDest = CreateCompatibleDC(NULL);\r
2359         if (hdcDest)\r
2360         {\r
2361                 if (SUCCEEDED(Create32BitHBITMAP(hdcDest, &sizIcon, NULL, &hBmp)))\r
2362                 {\r
2363                         HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcDest, hBmp);\r
2364                         if (hbmpOld)\r
2365                         {\r
2366                                 BLENDFUNCTION bfAlpha = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };\r
2367                                 BP_PAINTPARAMS paintParams = {0};\r
2368                                 paintParams.cbSize = sizeof(paintParams);\r
2369                                 paintParams.dwFlags = BPPF_ERASE;\r
2370                                 paintParams.pBlendFunction = &bfAlpha;\r
2371 \r
2372                                 HDC hdcBuffer;\r
2373                                 HPAINTBUFFER hPaintBuffer = pfnBeginBufferedPaint(hdcDest, &rcIcon, BPBF_DIB, &paintParams, &hdcBuffer);\r
2374                                 if (hPaintBuffer)\r
2375                                 {\r
2376                                         if (DrawIconEx(hdcBuffer, 0, 0, hIcon, sizIcon.cx, sizIcon.cy, 0, NULL, DI_NORMAL))\r
2377                                         {\r
2378                                                 // If icon did not have an alpha channel we need to convert buffer to PARGB\r
2379                                                 ConvertBufferToPARGB32(hPaintBuffer, hdcDest, hIcon, sizIcon);\r
2380                                         }\r
2381 \r
2382                                         // This will write the buffer contents to the destination bitmap\r
2383                                         pfnEndBufferedPaint(hPaintBuffer, TRUE);\r
2384                                 }\r
2385 \r
2386                                 SelectObject(hdcDest, hbmpOld);\r
2387                         }\r
2388                 }\r
2389 \r
2390                 DeleteDC(hdcDest);\r
2391         }\r
2392 \r
2393         DestroyIcon(hIcon);\r
2394 \r
2395         if(hBmp)\r
2396                 bitmaps.insert(bitmap_it, std::make_pair(uIcon, hBmp));\r
2397         return hBmp;\r
2398 }\r
2399 \r
2400 HRESULT CShellExt::Create32BitHBITMAP(HDC hdc, const SIZE *psize, __deref_opt_out void **ppvBits, __out HBITMAP* phBmp)\r
2401 {\r
2402         *phBmp = NULL;\r
2403 \r
2404         BITMAPINFO bmi;\r
2405         ZeroMemory(&bmi, sizeof(bmi));\r