OSDN Git Service

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