OSDN Git Service

Add workaround show corrent ContextMenu!
[tortoisegit/TortoiseGitJp.git] / src / TortoiseShell / ContextMenu.cpp
1 // TortoiseGit - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2008 - TortoiseGit\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "stdafx.h"\r
20 #include "ShellExt.h"\r
21 #include "ItemIDList.h"\r
22 #include "PreserveChdir.h"\r
23 #include "UnicodeUtils.h"\r
24 //#include "GitProperties.h"\r
25 #include "GitStatus.h"\r
26 #include "TGitPath.h"\r
27 \r
28 #define GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])\r
29 #define GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])\r
30 \r
31 int g_shellidlist=RegisterClipboardFormat(CFSTR_SHELLIDLIST);\r
32 \r
33 CShellExt::MenuInfo CShellExt::menuInfo[] =\r
34 {       \r
35         { ShellMenuClone,                                               MENUCLONE,                      IDI_CLONE,                              IDS_MENUCLONE,                  IDS_MENUDESCCHECKOUT,\r
36         ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },\r
37 \r
38         { ShellMenuPull,                                                MENUPULL,                       IDI_PULL,                               IDS_MENUPULL,                   IDS_MENUPULL,\r
39         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
40 \r
41         { ShellMenuFetch,                                               MENUFETCH,                      IDI_PULL,                               IDS_MENUFETCH,                  IDS_MENUFETCH,\r
42         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
43 \r
44         { ShellMenuPush,                                                MENUPUSH,                       IDI_PUSH,                               IDS_MENUPUSH,                   IDS_MENUPULL,\r
45         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
46 \r
47 //      { ShellMenuCheckout,                                    MENUCHECKOUT,           IDI_CHECKOUT,                   IDS_MENUCHECKOUT,                       IDS_MENUDESCCHECKOUT,\r
48 //      ITEMIS_FOLDER, ITEMIS_INSVN|ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0, 0 },\r
49 \r
50 //      { ShellMenuUpdate,                                              MENUUPDATE,                     IDI_UPDATE,                             IDS_MENUUPDATE,                         IDS_MENUDESCUPDATE,                             \r
51 //      ITEMIS_INSVN,   ITEMIS_ADDED, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
52 \r
53         { ShellMenuCommit,                                              MENUCOMMIT,                     IDI_COMMIT,                             IDS_MENUCOMMIT,                         IDS_MENUDESCCOMMIT,\r
54         ITEMIS_INSVN, 0, ITEMIS_FOLDERINSVN, 0, 0, 0, 0, 0 },\r
55 \r
56         { ShellSeparator, 0, 0, 0, 0, 0, 0, 0, 0},\r
57 \r
58         { ShellMenuDiff,                                                MENUDIFF,                       IDI_DIFF,                               IDS_MENUDIFF,                           IDS_MENUDESCDIFF,\r
59         ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_FOLDER|ITEMIS_NORMAL, ITEMIS_TWO, 0, 0, 0, 0, 0 },\r
60 \r
61         { ShellMenuPrevDiff,                                    MENUPREVDIFF,                   IDI_DIFF,                               IDS_MENUPREVDIFF,                       IDS_MENUDESCPREVDIFF,\r
62         ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_FOLDER, 0, 0, 0, 0, 0, 0 },\r
63 \r
64 //      { ShellMenuUrlDiff,                                             MENUURLDIFF,            IDI_DIFF,                               IDS_MENUURLDIFF,                        IDS_MENUDESCURLDIFF,\r
65 //      ITEMIS_INSVN|ITEMIS_ONLYONE|ITEMIS_EXTENDED, 0, ITEMIS_FOLDERINSVN|ITEMIS_EXTENDED|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },\r
66 \r
67         { ShellMenuLog,                                                 MENULOG,                        IDI_LOG,                                IDS_MENULOG,                            IDS_MENUDESCLOG,\r
68         ITEMIS_INSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, ITEMIS_ADDED, 0, 0 },\r
69 \r
70 //      { ShellMenuRepoBrowse,                                  MENUREPOBROWSE,         IDI_REPOBROWSE,                 IDS_MENUREPOBROWSE,                     IDS_MENUDESCREPOBROWSE,\r
71 //      ITEMIS_ONLYONE, 0, ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0 },\r
72 \r
73         { ShellMenuShowChanged,                                 MENUSHOWCHANGED,        IDI_SHOWCHANGED,                IDS_MENUSHOWCHANGED,            IDS_MENUDESCSHOWCHANGED,\r
74         ITEMIS_INSVN|ITEMIS_ONLYONE, 0, ITEMIS_FOLDER|ITEMIS_FOLDERINSVN|ITEMIS_ONLYONE, 0, 0, 0, 0, 0},\r
75 \r
76 //      { 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                                                         if (askedpath.HasAdminDir())\r
340                                                                 itemStates |= ITEMIS_INSVN;\r
341                                                         if (status == git_wc_status_ignored)\r
342                                                                 itemStates |= ITEMIS_IGNORED;\r
343                                                         if (status == git_wc_status_normal)\r
344                                                                 itemStates |= ITEMIS_NORMAL;\r
345                                                         if (status == git_wc_status_conflicted)\r
346                                                                 itemStates |= ITEMIS_CONFLICTED;\r
347                                                         if (status == git_wc_status_added)\r
348                                                                 itemStates |= ITEMIS_ADDED;\r
349                                                         if (status == git_wc_status_deleted)\r
350                                                                 itemStates |= ITEMIS_DELETED;\r
351                                                 }\r
352                                         }\r
353                                 } // for (int i = 0; i < count; i++)\r
354                                 GlobalUnlock ( drop );\r
355                                 ReleaseStgMedium ( &stg );\r
356 \r
357                         } // if (m_State == FileStateDropHandler) \r
358                         else\r
359                         {\r
360 \r
361                                 //Enumerate PIDLs which the user has selected\r
362                                 CIDA* cida = (CIDA*)GlobalLock(medium.hGlobal);\r
363                                 ItemIDList parent( GetPIDLFolder (cida));\r
364 \r
365                                 int count = cida->cidl;\r
366                                 BOOL statfetched = FALSE;\r
367                                 for (int i = 0; i < count; ++i)\r
368                                 {\r
369                                         ItemIDList child (GetPIDLItem (cida, i), &parent);\r
370                                         stdstring str = child.toString();\r
371                                         if ((str.empty() == false)&&(g_ShellCache.IsContextPathAllowed(str.c_str())))\r
372                                         {\r
373                                                 //check if our menu is requested for a subversion admin directory\r
374                                                 if (g_GitAdminDir.IsAdminDirPath(str.c_str()))\r
375                                                         continue;\r
376 \r
377                                                 files_.push_back(str);\r
378                                                 CTGitPath strpath;\r
379                                                 strpath.SetFromWin(str.c_str());\r
380                                                 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".diff"))==0) ? ITEMIS_PATCHFILE : 0;\r
381                                                 itemStates |= (strpath.GetFileExtension().CompareNoCase(_T(".patch"))==0) ? ITEMIS_PATCHFILE : 0;\r
382                                                 if (!statfetched)\r
383                                                 {\r
384                                                         //get the Subversion status of the item\r
385                                                         git_wc_status_kind status = git_wc_status_none;\r
386                                                         if ((g_ShellCache.IsSimpleContext())&&(strpath.IsDirectory()))\r
387                                                         {\r
388                                                                 if (strpath.HasAdminDir())\r
389                                                                         status = git_wc_status_normal;\r
390                                                         }\r
391                                                         else\r
392                                                         {\r
393                                                                 try\r
394                                                                 {\r
395                                                                         GitStatus stat;\r
396                                                                         if (strpath.HasAdminDir())\r
397                                                                                 stat.GetStatus(strpath, false, true, true);\r
398                                                                         statuspath = str;\r
399                                                                         if (stat.status)\r
400                                                                         {\r
401                                                                                 status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);\r
402                                                                                 fetchedstatus = status;\r
403                                                                                 //if ((stat.status->entry)&&(stat.status->entry->lock_token))\r
404                                                                                 //      itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;\r
405                                                                                 //if ((stat.status->entry)&&(stat.status->entry->kind == git_node_dir))\r
406                                                                                 //{\r
407                                                                                 //      itemStates |= ITEMIS_FOLDER;\r
408                                                                                 //      if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
409                                                                                 //              itemStates |= ITEMIS_FOLDERINGit;\r
410                                                                                 //}\r
411                                                                                 //if ((stat.status->entry)&&(stat.status->entry->conflict_wrk))\r
412                                                                                 //      itemStates |= ITEMIS_CONFLICTED;\r
413                                                                                 //if ((stat.status->entry)&&(stat.status->entry->present_props))\r
414                                                                                 //{\r
415                                                                                 //      if (strstr(stat.status->entry->present_props, "svn:needs-lock"))\r
416                                                                                 //              itemStates |= ITEMIS_NEEDSLOCK;\r
417                                                                                 //}\r
418                                                                                 //if ((stat.status->entry)&&(stat.status->entry->uuid))\r
419                                                                                 //      uuidSource = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);\r
420                                                                         }       \r
421                                                                         else\r
422                                                                         {\r
423                                                                                 // sometimes, git_client_status() returns with an error.\r
424                                                                                 // in that case, we have to check if the working copy is versioned\r
425                                                                                 // anyway to show the 'correct' context menu\r
426                                                                                 if (strpath.HasAdminDir())\r
427                                                                                 {\r
428                                                                                         status = git_wc_status_normal;\r
429                                                                                         fetchedstatus = status;\r
430                                                                                 }\r
431                                                                         }\r
432                                                                         statfetched = TRUE;\r
433                                                                 }\r
434                                                                 catch ( ... )\r
435                                                                 {\r
436                                                                         ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));\r
437                                                                 }\r
438                                                         }\r
439                                                         //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
440                                                         if (strpath.HasAdminDir())\r
441                                                                 itemStates |= ITEMIS_INSVN;\r
442                                                         if (status == git_wc_status_ignored)\r
443                                                         {\r
444                                                                 itemStates |= ITEMIS_IGNORED;\r
445                                                                 // the item is ignored. Get the svn:ignored properties so we can (maybe) later\r
446                                                                 // offer a 'remove from ignored list' entry\r
447 //                                                              GitProperties props(strpath.GetContainingDirectory(), false);\r
448 //                                                              ignoredprops.empty();\r
449 //                                                              for (int p=0; p<props.GetCount(); ++p)\r
450 //                                                              {\r
451 //                                                                      if (props.GetItemName(p).compare(stdstring(_T("svn:ignore")))==0)\r
452 //                                                                      {\r
453 //                                                                              std::string st = props.GetItemValue(p);\r
454 //                                                                              ignoredprops = MultibyteToWide(st.c_str());\r
455 //                                                                              // remove all escape chars ('\\')\r
456 //                                                                              std::remove(ignoredprops.begin(), ignoredprops.end(), '\\');\r
457 //                                                                              break;\r
458 //                                                                      }\r
459 //                                                              }\r
460                                                         }\r
461                                                         if (status == git_wc_status_normal)\r
462                                                                 itemStates |= ITEMIS_NORMAL;\r
463                                                         if (status == git_wc_status_conflicted)\r
464                                                                 itemStates |= ITEMIS_CONFLICTED;\r
465                                                         if (status == git_wc_status_added)\r
466                                                                 itemStates |= ITEMIS_ADDED;\r
467                                                         if (status == git_wc_status_deleted)\r
468                                                                 itemStates |= ITEMIS_DELETED;\r
469                                                 }\r
470                                         }\r
471                                 } // for (int i = 0; i < count; ++i)\r
472                                 ItemIDList child (GetPIDLItem (cida, 0), &parent);\r
473 // ? was this disabled because the /wc option does not work safely with Git? or because the code below didn't compile before?\r
474 //                              if (g_ShellCache.HasSVNAdminDir(child.toString().c_str(), FALSE))\r
475 //                                      itemStates |= ITEMIS_INVERSIONEDFOLDER;\r
476                                 GlobalUnlock(medium.hGlobal);\r
477 \r
478                                 // if the item is a versioned folder, check if there's a patch file\r
479                                 // in the clipboard to be used in "Apply Patch"\r
480                                 UINT cFormatDiff = RegisterClipboardFormat(_T("TGIT_UNIFIEDDIFF"));\r
481                                 if (cFormatDiff)\r
482                                 {\r
483                                         if (IsClipboardFormatAvailable(cFormatDiff)) \r
484                                                 itemStates |= ITEMIS_PATCHINCLIPBOARD;\r
485                                 }\r
486                                 if (IsClipboardFormatAvailable(CF_HDROP)) \r
487                                         itemStates |= ITEMIS_PATHINCLIPBOARD;\r
488 \r
489                         }\r
490 \r
491                         ReleaseStgMedium ( &medium );\r
492                         if (medium.pUnkForRelease)\r
493                         {\r
494                                 IUnknown* relInterface = (IUnknown*)medium.pUnkForRelease;\r
495                                 relInterface->Release();\r
496                         }\r
497                 }\r
498         }\r
499 \r
500         // get folder background\r
501         if (pIDFolder)\r
502         {\r
503 \r
504                 ItemIDList list(pIDFolder);\r
505                 folder_ = list.toString();\r
506                 git_wc_status_kind status = git_wc_status_none;\r
507                 if (IsClipboardFormatAvailable(CF_HDROP)) \r
508                         itemStatesFolder |= ITEMIS_PATHINCLIPBOARD;\r
509                 \r
510                 CTGitPath askedpath;\r
511                 askedpath.SetFromWin(folder_.c_str());\r
512 \r
513                 if ((folder_.compare(statuspath)!=0)&&(g_ShellCache.IsContextPathAllowed(folder_.c_str())))\r
514                 {\r
515                         \r
516                         try\r
517                         {\r
518                                 GitStatus stat;\r
519                                 stat.GetStatus(CTGitPath(folder_.c_str()), false, true, true);\r
520                                 if (stat.status)\r
521                                 {\r
522                                         status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);\r
523 //                                      if ((stat.status->entry)&&(stat.status->entry->lock_token))\r
524 //                                              itemStatesFolder |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;\r
525 //                                      if ((stat.status->entry)&&(stat.status->entry->present_props))\r
526 //                                      {\r
527 //                                              if (strstr(stat.status->entry->present_props, "svn:needs-lock"))\r
528 //                                                      itemStatesFolder |= ITEMIS_NEEDSLOCK;\r
529 //                                      }\r
530 //                                      if ((stat.status->entry)&&(stat.status->entry->uuid))\r
531 //                                              uuidTarget = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);\r
532                                 \r
533                                 }\r
534                                 else\r
535                                 {\r
536                                         // sometimes, git_client_status() returns with an error.\r
537                                         // in that case, we have to check if the working copy is versioned\r
538                                         // anyway to show the 'correct' context menu\r
539                                         if (askedpath.HasAdminDir())\r
540                                                 status = git_wc_status_normal;\r
541                                 }\r
542                                 \r
543                                 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
544                                 if (askedpath.HasAdminDir())\r
545                                         itemStatesFolder |= ITEMIS_INSVN;\r
546                                 if (status == git_wc_status_normal)\r
547                                         itemStatesFolder |= ITEMIS_NORMAL;\r
548                                 if (status == git_wc_status_conflicted)\r
549                                         itemStatesFolder |= ITEMIS_CONFLICTED;\r
550                                 if (status == git_wc_status_added)\r
551                                         itemStatesFolder |= ITEMIS_ADDED;\r
552                                 if (status == git_wc_status_deleted)\r
553                                         itemStatesFolder |= ITEMIS_DELETED;\r
554 \r
555                         }\r
556                         catch ( ... )\r
557                         {\r
558                                 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));\r
559                         }\r
560                 }\r
561                 else\r
562                 {\r
563                         status = fetchedstatus;\r
564                 }\r
565                 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
566                 if (askedpath.HasAdminDir())\r
567                 {\r
568                         itemStatesFolder |= ITEMIS_FOLDERINSVN;\r
569                 }\r
570                 if (status == git_wc_status_ignored)\r
571                         itemStatesFolder |= ITEMIS_IGNORED;\r
572                 itemStatesFolder |= ITEMIS_FOLDER;\r
573                 if (files_.size() == 0)\r
574                         itemStates |= ITEMIS_ONLYONE;\r
575                 if (m_State != FileStateDropHandler)\r
576                         itemStates |= itemStatesFolder;\r
577 \r
578         }\r
579         if (files_.size() == 2)\r
580                 itemStates |= ITEMIS_TWO;\r
581         if ((files_.size() == 1)&&(g_ShellCache.IsContextPathAllowed(files_.front().c_str())))\r
582         {\r
583 \r
584                 itemStates |= ITEMIS_ONLYONE;\r
585                 if (m_State != FileStateDropHandler)\r
586                 {\r
587                         if (PathIsDirectory(files_.front().c_str()))\r
588                         {\r
589                                 folder_ = files_.front();\r
590                                 git_wc_status_kind status = git_wc_status_none;\r
591                                 CTGitPath askedpath;\r
592                                 askedpath.SetFromWin(folder_.c_str());\r
593 \r
594                                 if (folder_.compare(statuspath)!=0)\r
595                                 {                               \r
596                                         try\r
597                                         {\r
598                                                 GitStatus stat;\r
599                                                 stat.GetStatus(CTGitPath(folder_.c_str()), false, true, true);\r
600                                                 if (stat.status)\r
601                                                 {\r
602                                                         status = GitStatus::GetMoreImportant(stat.status->text_status, stat.status->prop_status);\r
603 //                                                      if ((stat.status->entry)&&(stat.status->entry->lock_token))\r
604 //                                                              itemStates |= (stat.status->entry->lock_token[0] != 0) ? ITEMIS_LOCKED : 0;\r
605 //                                                      if ((stat.status->entry)&&(stat.status->entry->present_props))\r
606 //                                                      {\r
607 //                                                              if (strstr(stat.status->entry->present_props, "svn:needs-lock"))\r
608 //                                                                      itemStates |= ITEMIS_NEEDSLOCK;\r
609 //                                                      }\r
610 //                                                      if ((stat.status->entry)&&(stat.status->entry->uuid))\r
611 //                                                              uuidTarget = CUnicodeUtils::StdGetUnicode(stat.status->entry->uuid);\r
612                                                 }\r
613                                         }\r
614                                         catch ( ... )\r
615                                         {\r
616                                                 ATLTRACE2(_T("Exception in GitStatus::GetStatus()\n"));\r
617                                         }\r
618                                 }\r
619                                 else\r
620                                 {\r
621                                         status = fetchedstatus;\r
622                                 }\r
623                                 //if ((status != git_wc_status_unversioned)&&(status != git_wc_status_ignored)&&(status != git_wc_status_none))\r
624                                 if (askedpath.HasAdminDir())\r
625                                         itemStates |= ITEMIS_FOLDERINSVN;\r
626                                 if (status == git_wc_status_ignored)\r
627                                         itemStates |= ITEMIS_IGNORED;\r
628                                 itemStates |= ITEMIS_FOLDER;\r
629                                 if (status == git_wc_status_added)\r
630                                         itemStates |= ITEMIS_ADDED;\r
631                                 if (status == git_wc_status_deleted)\r
632                                         itemStates |= ITEMIS_DELETED;\r
633                         }\r
634                 }\r
635         \r
636         }\r
637         \r
638         return NOERROR;\r
639 }\r
640 \r
641 void CShellExt::InsertGitMenu(BOOL istop, HMENU menu, UINT pos, UINT_PTR id, UINT stringid, UINT icon, UINT idCmdFirst, GitCommands com, UINT uFlags)\r
642 {\r
643         TCHAR menutextbuffer[255] = {0};\r
644         TCHAR verbsbuffer[255] = {0};\r
645         MAKESTRING(stringid);\r
646 \r
647         if (istop)\r
648         {\r
649                 //menu entry for the top context menu, so append an "Git " before\r
650                 //the menu text to indicate where the entry comes from\r
651                 _tcscpy_s(menutextbuffer, 255, _T("Git "));\r
652         }\r
653         _tcscat_s(menutextbuffer, 255, stringtablebuffer);\r
654         if ((fullver < 0x500)||(fullver == 0x500 && !uFlags))\r
655         {\r
656                 InsertMenu(menu, pos, MF_BYPOSITION | MF_STRING , id, menutextbuffer);\r
657                 if (fullver >= 0x500)\r
658                 {\r
659                         // on win2k, the context menu does not work properly if we use\r
660                         // icon bitmaps. At least the menu text is empty in the context menu\r
661                         // for folder backgrounds (seems like a win2k bug).\r
662                         HBITMAP bmp = IconToBitmap(icon); \r
663                         SetMenuItemBitmaps(menu, pos, MF_BYPOSITION, bmp, bmp);\r
664                 }\r
665         }\r
666         else\r
667         {\r
668                 MENUITEMINFO menuiteminfo = {0};\r
669                 menuiteminfo.cbSize = sizeof(menuiteminfo);\r
670                 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_BITMAP | MIIM_STRING;\r
671                 menuiteminfo.fType = MFT_STRING;\r
672                 menuiteminfo.dwTypeData = menutextbuffer;\r
673                 if (icon)\r
674                         menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(icon) : HBMMENU_CALLBACK;\r
675                 menuiteminfo.wID = id;\r
676                 InsertMenuItem(menu, pos, TRUE, &menuiteminfo);\r
677         }\r
678 \r
679         if (istop)\r
680         {\r
681                 //menu entry for the top context menu, so append an "Git " before\r
682                 //the menu text to indicate where the entry comes from\r
683                 _tcscpy_s(menutextbuffer, 255, _T("Git "));\r
684         }\r
685         LoadString(g_hResInst, stringid, verbsbuffer, sizeof(verbsbuffer));\r
686         _tcscat_s(menutextbuffer, 255, verbsbuffer);\r
687         stdstring verb = stdstring(menutextbuffer);\r
688         if (verb.find('&') != -1)\r
689         {\r
690                 verb.erase(verb.find('&'),1);\r
691         }\r
692         myVerbsMap[verb] = id - idCmdFirst;\r
693         myVerbsMap[verb] = id;\r
694         myVerbsIDMap[id - idCmdFirst] = verb;\r
695         myVerbsIDMap[id] = verb;\r
696         // We store the relative and absolute diameter\r
697         // (drawitem callback uses absolute, others relative)\r
698         myIDMap[id - idCmdFirst] = com;\r
699         myIDMap[id] = com;\r
700         if (!istop)\r
701                 mySubMenuMap[pos] = com;\r
702 }\r
703 \r
704 HBITMAP CShellExt::IconToBitmap(UINT uIcon)\r
705 {\r
706         std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);\r
707         if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)\r
708                 return bitmap_it->second;\r
709 \r
710         HICON hIcon = (HICON)LoadImage(g_hResInst, MAKEINTRESOURCE(uIcon), IMAGE_ICON, 12, 12, LR_DEFAULTCOLOR);\r
711         if (!hIcon)\r
712                 return NULL;\r
713 \r
714         RECT rect;\r
715 \r
716         rect.right = ::GetSystemMetrics(SM_CXMENUCHECK);\r
717         rect.bottom = ::GetSystemMetrics(SM_CYMENUCHECK);\r
718 \r
719         rect.left = rect.top = 0;\r
720 \r
721         HWND desktop = ::GetDesktopWindow();\r
722         if (desktop == NULL)\r
723         {\r
724                 DestroyIcon(hIcon);\r
725                 return NULL;\r
726         }\r
727 \r
728         HDC screen_dev = ::GetDC(desktop);\r
729         if (screen_dev == NULL)\r
730         {\r
731                 DestroyIcon(hIcon);\r
732                 return NULL;\r
733         }\r
734 \r
735         // Create a compatible DC\r
736         HDC dst_hdc = ::CreateCompatibleDC(screen_dev);\r
737         if (dst_hdc == NULL)\r
738         {\r
739                 DestroyIcon(hIcon);\r
740                 ::ReleaseDC(desktop, screen_dev); \r
741                 return NULL;\r
742         }\r
743 \r
744         // Create a new bitmap of icon size\r
745         HBITMAP bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);\r
746         if (bmp == NULL)\r
747         {\r
748                 DestroyIcon(hIcon);\r
749                 ::DeleteDC(dst_hdc);\r
750                 ::ReleaseDC(desktop, screen_dev); \r
751                 return NULL;\r
752         }\r
753 \r
754         // Select it into the compatible DC\r
755         HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);\r
756         if (old_dst_bmp == NULL)\r
757         {\r
758                 DestroyIcon(hIcon);\r
759                 return NULL;\r
760         }\r
761 \r
762         // Fill the background of the compatible DC with the white color\r
763         // that is taken by menu routines as transparent\r
764         ::SetBkColor(dst_hdc, RGB(255, 255, 255));\r
765         ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);\r
766 \r
767         // Draw the icon into the compatible DC\r
768         ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);\r
769 \r
770         // Restore settings\r
771         ::SelectObject(dst_hdc, old_dst_bmp);\r
772         ::DeleteDC(dst_hdc);\r
773         ::ReleaseDC(desktop, screen_dev); \r
774         DestroyIcon(hIcon);\r
775         if (bmp)\r
776                 bitmaps.insert(bitmap_it, std::make_pair(uIcon, bmp));\r
777         return bmp;\r
778 }\r
779 \r
780 bool CShellExt::WriteClipboardPathsToTempFile(stdstring& tempfile)\r
781 {\r
782         bool bRet = true;\r
783         tempfile = stdstring();\r
784         //write all selected files and paths to a temporary file\r
785         //for TortoiseProc.exe to read out again.\r
786         DWORD written = 0;\r
787         DWORD pathlength = GetTempPath(0, NULL);\r
788         TCHAR * path = new TCHAR[pathlength+1];\r
789         TCHAR * tempFile = new TCHAR[pathlength + 100];\r
790         GetTempPath (pathlength+1, path);\r
791         GetTempFileName (path, _T("git"), 0, tempFile);\r
792         tempfile = stdstring(tempFile);\r
793 \r
794         HANDLE file = ::CreateFile (tempFile,\r
795                 GENERIC_WRITE, \r
796                 FILE_SHARE_READ, \r
797                 0, \r
798                 CREATE_ALWAYS, \r
799                 FILE_ATTRIBUTE_TEMPORARY,\r
800                 0);\r
801 \r
802         delete [] path;\r
803         delete [] tempFile;\r
804         if (file == INVALID_HANDLE_VALUE)\r
805                 return false;\r
806 \r
807         if (!IsClipboardFormatAvailable(CF_HDROP))\r
808                 return false;\r
809         if (!OpenClipboard(NULL))\r
810                 return false;\r
811 \r
812         stdstring sClipboardText;\r
813         HGLOBAL hglb = GetClipboardData(CF_HDROP);\r
814         HDROP hDrop = (HDROP)GlobalLock(hglb);\r
815         if(hDrop != NULL)\r
816         {\r
817                 TCHAR szFileName[MAX_PATH];\r
818                 UINT cFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); \r
819                 for(UINT i = 0; i < cFiles; ++i)\r
820                 {\r
821                         DragQueryFile(hDrop, i, szFileName, sizeof(szFileName));\r
822                         stdstring filename = szFileName;\r
823                         ::WriteFile (file, filename.c_str(), filename.size()*sizeof(TCHAR), &written, 0);\r
824                         ::WriteFile (file, _T("\n"), 2, &written, 0);\r
825                 }\r
826                 GlobalUnlock(hDrop);\r
827         }\r
828         else bRet = false;\r
829         GlobalUnlock(hglb);\r
830 \r
831         CloseClipboard();\r
832         ::CloseHandle(file);\r
833 \r
834         return bRet;\r
835 }\r
836 \r
837 stdstring CShellExt::WriteFileListToTempFile()\r
838 {\r
839         //write all selected files and paths to a temporary file\r
840         //for TortoiseProc.exe to read out again.\r
841         DWORD pathlength = GetTempPath(0, NULL);\r
842         TCHAR * path = new TCHAR[pathlength+1];\r
843         TCHAR * tempFile = new TCHAR[pathlength + 100];\r
844         GetTempPath (pathlength+1, path);\r
845         GetTempFileName (path, _T("git"), 0, tempFile);\r
846         stdstring retFilePath = stdstring(tempFile);\r
847         \r
848         HANDLE file = ::CreateFile (tempFile,\r
849                                                                 GENERIC_WRITE, \r
850                                                                 FILE_SHARE_READ, \r
851                                                                 0, \r
852                                                                 CREATE_ALWAYS, \r
853                                                                 FILE_ATTRIBUTE_TEMPORARY,\r
854                                                                 0);\r
855 \r
856         delete [] path;\r
857         delete [] tempFile;\r
858         if (file == INVALID_HANDLE_VALUE)\r
859                 return stdstring();\r
860                 \r
861         DWORD written = 0;\r
862         if (files_.empty())\r
863         {\r
864                 ::WriteFile (file, folder_.c_str(), folder_.size()*sizeof(TCHAR), &written, 0);\r
865                 ::WriteFile (file, _T("\n"), 2, &written, 0);\r
866         }\r
867 \r
868         for (std::vector<stdstring>::iterator I = files_.begin(); I != files_.end(); ++I)\r
869         {\r
870                 ::WriteFile (file, I->c_str(), I->size()*sizeof(TCHAR), &written, 0);\r
871                 ::WriteFile (file, _T("\n"), 2, &written, 0);\r
872         }\r
873         ::CloseHandle(file);\r
874         return retFilePath;\r
875 }\r
876 \r
877 STDMETHODIMP CShellExt::QueryDropContext(UINT uFlags, UINT idCmdFirst, HMENU hMenu, UINT &indexMenu)\r
878 {\r
879         PreserveChdir preserveChdir;\r
880         LoadLangDll();\r
881 \r
882         if ((uFlags & CMF_DEFAULTONLY)!=0)\r
883                 return NOERROR;                                 //we don't change the default action\r
884 \r
885         if ((files_.size() == 0)||(folder_.size() == 0))\r
886                 return NOERROR;\r
887 \r
888         if (((uFlags & 0x000f)!=CMF_NORMAL)&&(!(uFlags & CMF_EXPLORE))&&(!(uFlags & CMF_VERBSONLY)))\r
889                 return NOERROR;\r
890 \r
891         bool bSourceAndTargetFromSameRepository = (uuidSource.compare(uuidTarget) == 0) || uuidSource.empty() || uuidTarget.empty();\r
892 \r
893         //the drop handler only has eight commands, but not all are visible at the same time:\r
894         //if the source file(s) are under version control then those files can be moved\r
895         //to the new location or they can be moved with a rename, \r
896         //if they are unversioned then they can be added to the working copy\r
897         //if they are versioned, they also can be exported to an unversioned location\r
898         UINT idCmd = idCmdFirst;\r
899 \r
900         // Git move here\r
901         // available if source is versioned but not added, target is versioned, source and target from same repository or target folder is added\r
902         if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&((itemStates & ITEMIS_INSVN)&&((~itemStates) & ITEMIS_ADDED)))\r
903                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPMOVEMENU, 0, idCmdFirst, ShellMenuDropMove, uFlags);\r
904 \r
905         // Git move and rename here\r
906         // 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
907         if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_ONLYONE)&&((~itemStates) & ITEMIS_ADDED))\r
908                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPMOVERENAMEMENU, 0, idCmdFirst, ShellMenuDropMoveRename, uFlags);\r
909 \r
910         // Git copy here\r
911         // available if source is versioned but not added, target is versioned, source and target from same repository or target folder is added\r
912         if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&((~itemStates) & ITEMIS_ADDED))\r
913                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYMENU, 0, idCmdFirst, ShellMenuDropCopy, uFlags);\r
914 \r
915         // Git copy and rename here, source and target from same repository\r
916         // available if source is a single, versioned but not added item, target is versioned or target folder is added\r
917         if ((bSourceAndTargetFromSameRepository||(itemStatesFolder & ITEMIS_ADDED))&&(itemStatesFolder & ITEMIS_FOLDERINSVN)&&(itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_ONLYONE)&&((~itemStates) & ITEMIS_ADDED))\r
918                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYRENAMEMENU, 0, idCmdFirst, ShellMenuDropCopyRename, uFlags);\r
919 \r
920         // Git add here\r
921         // available if target is versioned and source is either unversioned or from another repository\r
922         if ((itemStatesFolder & ITEMIS_FOLDERINSVN)&&(((~itemStates) & ITEMIS_INSVN)||!bSourceAndTargetFromSameRepository))\r
923                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPCOPYADDMENU, 0, idCmdFirst, ShellMenuDropCopyAdd, uFlags);\r
924 \r
925         // Git export here\r
926         // available if source is versioned and a folder\r
927         if ((itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_FOLDER))\r
928                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPEXPORTMENU, 0, idCmdFirst, ShellMenuDropExport, uFlags);\r
929 \r
930         // Git export all here\r
931         // available if source is versioned and a folder\r
932         if ((itemStates & ITEMIS_INSVN)&&(itemStates & ITEMIS_FOLDER))\r
933                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_DROPEXPORTEXTENDEDMENU, 0, idCmdFirst, ShellMenuDropExportExtended, uFlags);\r
934 \r
935         // apply patch\r
936         // available if source is a patchfile\r
937         if (itemStates & ITEMIS_PATCHFILE)\r
938                 InsertGitMenu(FALSE, hMenu, indexMenu++, idCmd++, IDS_MENUAPPLYPATCH, 0, idCmdFirst, ShellMenuApplyPatch, uFlags);\r
939 \r
940         // separator\r
941         if (idCmd != idCmdFirst)\r
942                 InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); \r
943 \r
944         return ResultFromScode(MAKE_SCODE(SEVERITY_SUCCESS, 0, (USHORT)(idCmd - idCmdFirst)));\r
945 }\r
946 \r
947 STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu,\r
948                                          UINT indexMenu,\r
949                                          UINT idCmdFirst,\r
950                                          UINT /*idCmdLast*/,\r
951                                          UINT uFlags)\r
952 {\r
953         ATLTRACE("Shell :: QueryContextMenu\n");\r
954         PreserveChdir preserveChdir;\r
955         \r
956         //first check if our drop handler is called\r
957         //and then (if true) provide the context menu for the\r
958         //drop handler\r
959         if (m_State == FileStateDropHandler)\r
960         {\r
961                 return QueryDropContext(uFlags, idCmdFirst, hMenu, indexMenu);\r
962         }\r
963 \r
964         if ((uFlags & CMF_DEFAULTONLY)!=0)\r
965                 return NOERROR;                                 //we don't change the default action\r
966 \r
967         if ((files_.size() == 0)&&(folder_.size() == 0))\r
968                 return NOERROR;\r
969 \r
970         if (((uFlags & 0x000f)!=CMF_NORMAL)&&(!(uFlags & CMF_EXPLORE))&&(!(uFlags & CMF_VERBSONLY)))\r
971                 return NOERROR;\r
972 \r
973         int csidlarray[] = \r
974         {\r
975                 CSIDL_BITBUCKET,\r
976                 CSIDL_CDBURN_AREA,\r
977                 CSIDL_COMMON_FAVORITES,\r
978                 CSIDL_COMMON_STARTMENU,\r
979                 CSIDL_COMPUTERSNEARME,\r
980                 CSIDL_CONNECTIONS,\r
981                 CSIDL_CONTROLS,\r
982                 CSIDL_COOKIES,\r
983                 CSIDL_FAVORITES,\r
984                 CSIDL_FONTS,\r
985                 CSIDL_HISTORY,\r
986                 CSIDL_INTERNET,\r
987                 CSIDL_INTERNET_CACHE,\r
988                 CSIDL_NETHOOD,\r
989                 CSIDL_NETWORK,\r
990                 CSIDL_PRINTERS,\r
991                 CSIDL_PRINTHOOD,\r
992                 CSIDL_RECENT,\r
993                 CSIDL_SENDTO,\r
994                 CSIDL_STARTMENU,\r
995                 0\r
996         };\r
997         if (IsIllegalFolder(folder_, csidlarray))\r
998                 return NOERROR;\r
999 \r
1000         if (folder_.empty())\r
1001         {\r
1002                 // folder is empty, but maybe files are selected\r
1003                 if (files_.size() == 0)\r
1004                         return NOERROR; // nothing selected - we don't have a menu to show\r
1005                 // check whether a selected entry is an UID - those are namespace extensions\r
1006                 // which we can't handle\r
1007                 for (std::vector<stdstring>::const_iterator it = files_.begin(); it != files_.end(); ++it)\r
1008                 {\r
1009                         if (_tcsncmp(it->c_str(), _T("::{"), 3)==0)\r
1010                                 return NOERROR;\r
1011                 }\r
1012         }\r
1013 \r
1014         //check if our menu is requested for a subversion admin directory\r
1015         if (g_GitAdminDir.IsAdminDirPath(folder_.c_str()))\r
1016                 return NOERROR;\r
1017 \r
1018         if (uFlags & CMF_EXTENDEDVERBS)\r
1019                 itemStates |= ITEMIS_EXTENDED;\r
1020 \r
1021         const BOOL bShortcut = !!(uFlags & CMF_VERBSONLY);\r
1022         if ( bShortcut && (files_.size()==1))\r
1023         {\r
1024                 // Don't show the context menu for a link if the\r
1025                 // destination is not part of a working copy.\r
1026                 // It would only show the standard menu items\r
1027                 // which are already shown for the lnk-file.\r
1028                 CString path = files_.front().c_str();\r
1029                 if ( !g_GitAdminDir.HasAdminDir(path) )\r
1030                 {\r
1031                         return NOERROR;\r
1032                 }\r
1033         }\r
1034 \r
1035         //check if we already added our menu entry for a folder.\r
1036         //we check that by iterating through all menu entries and check if \r
1037         //the dwItemData member points to our global ID string. That string is set\r
1038         //by our shell extension when the folder menu is inserted.\r
1039         TCHAR menubuf[MAX_PATH];\r
1040         int count = GetMenuItemCount(hMenu);\r
1041         for (int i=0; i<count; ++i)\r
1042         {\r
1043                 MENUITEMINFO miif;\r
1044                 SecureZeroMemory(&miif, sizeof(MENUITEMINFO));\r
1045                 miif.cbSize = sizeof(MENUITEMINFO);\r
1046                 miif.fMask = MIIM_DATA;\r
1047                 miif.dwTypeData = menubuf;\r
1048                 miif.cch = sizeof(menubuf)/sizeof(TCHAR);\r
1049                 GetMenuItemInfo(hMenu, i, TRUE, &miif);\r
1050                 if (miif.dwItemData == (ULONG_PTR)g_MenuIDString)\r
1051                         return NOERROR;\r
1052         }\r
1053 \r
1054         LoadLangDll();\r
1055         UINT idCmd = idCmdFirst;\r
1056 \r
1057         //create the sub menu\r
1058         HMENU subMenu = CreateMenu();\r
1059         int indexSubMenu = 0;\r
1060 \r
1061         unsigned __int64 topmenu = g_ShellCache.GetMenuLayout();\r
1062         unsigned __int64 menumask = g_ShellCache.GetMenuMask();\r
1063 \r
1064         int menuIndex = 0;\r
1065         bool bAddSeparator = false;\r
1066         bool bMenuEntryAdded = false;\r
1067         // insert separator at start\r
1068         InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); idCmd++;\r
1069         bool bShowIcons = !!DWORD(CRegStdWORD(_T("Software\\TortoiseGit\\ShowContextMenuIcons"), TRUE));\r
1070         if (fullver <= 0x0500)\r
1071                 bShowIcons = false;\r
1072         while (menuInfo[menuIndex].command != ShellMenuLastEntry)\r
1073         {\r
1074                 if (menuInfo[menuIndex].command == ShellSeparator)\r
1075                 {\r
1076                         // we don't add a separator immediately. Because there might not be\r
1077                         // another 'normal' menu entry after we insert a separator.\r
1078                         // we simply set a flag here, indicating that before the next\r
1079                         // 'normal' menu entry, a separator should be added.\r
1080                         bAddSeparator = true;\r
1081                 }\r
1082                 else\r
1083                 {\r
1084                         // check the conditions whether to show the menu entry or not\r
1085                         bool bInsertMenu = false;\r
1086 \r
1087                         if (menuInfo[menuIndex].firstyes && menuInfo[menuIndex].firstno)\r
1088                         {\r
1089                                 if (((menuInfo[menuIndex].firstyes & itemStates) == menuInfo[menuIndex].firstyes)\r
1090                                         &&\r
1091                                         ((menuInfo[menuIndex].firstno & (~itemStates)) == menuInfo[menuIndex].firstno))\r
1092                                         bInsertMenu = true;\r
1093                         }\r
1094                         else if ((menuInfo[menuIndex].firstyes)&&((menuInfo[menuIndex].firstyes & itemStates) == menuInfo[menuIndex].firstyes))\r
1095                                 bInsertMenu = true;\r
1096                         else if ((menuInfo[menuIndex].firstno)&&((menuInfo[menuIndex].firstno & (~itemStates)) == menuInfo[menuIndex].firstno))\r
1097                                 bInsertMenu = true;\r
1098 \r
1099                         if (menuInfo[menuIndex].secondyes && menuInfo[menuIndex].secondno)\r
1100                         {\r
1101                                 if (((menuInfo[menuIndex].secondyes & itemStates) == menuInfo[menuIndex].secondyes)\r
1102                                         &&\r
1103                                         ((menuInfo[menuIndex].secondno & (~itemStates)) == menuInfo[menuIndex].secondno))\r
1104                                         bInsertMenu = true;\r
1105                         }\r
1106                         else if ((menuInfo[menuIndex].secondyes)&&((menuInfo[menuIndex].secondyes & itemStates) == menuInfo[menuIndex].secondyes))\r
1107                                 bInsertMenu = true;\r
1108                         else if ((menuInfo[menuIndex].secondno)&&((menuInfo[menuIndex].secondno & (~itemStates)) == menuInfo[menuIndex].secondno))\r
1109                                 bInsertMenu = true;\r
1110 \r
1111                         if (menuInfo[menuIndex].thirdyes && menuInfo[menuIndex].thirdno)\r
1112                         {\r
1113                                 if (((menuInfo[menuIndex].thirdyes & itemStates) == menuInfo[menuIndex].thirdyes)\r
1114                                         &&\r
1115                                         ((menuInfo[menuIndex].thirdno & (~itemStates)) == menuInfo[menuIndex].thirdno))\r
1116                                         bInsertMenu = true;\r
1117                         }\r
1118                         else if ((menuInfo[menuIndex].thirdyes)&&((menuInfo[menuIndex].thirdyes & itemStates) == menuInfo[menuIndex].thirdyes))\r
1119                                 bInsertMenu = true;\r
1120                         else if ((menuInfo[menuIndex].thirdno)&&((menuInfo[menuIndex].thirdno & (~itemStates)) == menuInfo[menuIndex].thirdno))\r
1121                                 bInsertMenu = true;\r
1122 \r
1123                         if (menuInfo[menuIndex].fourthyes && menuInfo[menuIndex].fourthno)\r
1124                         {\r
1125                                 if (((menuInfo[menuIndex].fourthyes & itemStates) == menuInfo[menuIndex].fourthyes)\r
1126                                         &&\r
1127                                         ((menuInfo[menuIndex].fourthno & (~itemStates)) == menuInfo[menuIndex].fourthno))\r
1128                                         bInsertMenu = true;\r
1129                         }\r
1130                         else if ((menuInfo[menuIndex].fourthyes)&&((menuInfo[menuIndex].fourthyes & itemStates) == menuInfo[menuIndex].fourthyes))\r
1131                                 bInsertMenu = true;\r
1132                         else if ((menuInfo[menuIndex].fourthno)&&((menuInfo[menuIndex].fourthno & (~itemStates)) == menuInfo[menuIndex].fourthno))\r
1133                                 bInsertMenu = true;\r
1134 \r
1135                         if (menuInfo[menuIndex].menuID & (~menumask))\r
1136                         {\r
1137                                 if (bInsertMenu)\r
1138                                 {\r
1139                                         // insert a separator\r
1140                                         if ((bMenuEntryAdded)&&(bAddSeparator))\r
1141                                         {\r
1142                                                 bAddSeparator = false;\r
1143                                                 bMenuEntryAdded = false;\r
1144                                                 InsertMenu(subMenu, indexSubMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); \r
1145                                                 idCmd++;\r
1146                                         }\r
1147                                         \r
1148                                         // handle special cases (sub menus)\r
1149                                         if ((menuInfo[menuIndex].command == ShellMenuIgnoreSub)||(menuInfo[menuIndex].command == ShellMenuUnIgnoreSub))\r
1150                                         {\r
1151                                                 InsertIgnoreSubmenus(idCmd, idCmdFirst, hMenu, subMenu, indexMenu, indexSubMenu, topmenu, bShowIcons);\r
1152                                                 bMenuEntryAdded = true;\r
1153                                         }\r
1154                                         else\r
1155                                         {\r
1156                                                 // the 'get lock' command is special\r
1157                                                 bool bIsTop = ((topmenu & menuInfo[menuIndex].menuID) != 0);\r
1158                                                 if (menuInfo[menuIndex].command == ShellMenuLock)\r
1159                                                 {\r
1160                                                         if ((itemStates & ITEMIS_NEEDSLOCK) && g_ShellCache.IsGetLockTop())\r
1161                                                                 bIsTop = true;\r
1162                                                 }\r
1163                                                 // insert the menu entry\r
1164                                                 InsertGitMenu(  bIsTop,\r
1165                                                                                 bIsTop ? hMenu : subMenu,\r
1166                                                                                 bIsTop ? indexMenu++ : indexSubMenu++,\r
1167                                                                                 idCmd++,\r
1168                                                                                 menuInfo[menuIndex].menuTextID,\r
1169                                                                                 bShowIcons ? menuInfo[menuIndex].iconID : 0,\r
1170                                                                                 idCmdFirst,\r
1171                                                                                 menuInfo[menuIndex].command,\r
1172                                                                                 uFlags);\r
1173                                                 if (!bIsTop)\r
1174                                                         bMenuEntryAdded = true;\r
1175                                         }\r
1176                                 }\r
1177                         }\r
1178                 }\r
1179                 menuIndex++;\r
1180         }\r
1181 \r
1182         //add sub menu to main context menu\r
1183         //don't use InsertMenu because this will lead to multiple menu entries in the explorer file menu.\r
1184         //see http://support.microsoft.com/default.aspx?scid=kb;en-us;214477 for details of that.\r
1185         MAKESTRING(IDS_MENUSUBMENU);\r
1186         MENUITEMINFO menuiteminfo;\r
1187         SecureZeroMemory(&menuiteminfo, sizeof(menuiteminfo));\r
1188         menuiteminfo.cbSize = sizeof(menuiteminfo);\r
1189         menuiteminfo.fType = MFT_STRING;\r
1190         menuiteminfo.dwTypeData = stringtablebuffer;\r
1191 \r
1192         UINT uIcon = bShowIcons ? IDI_APP : 0;\r
1193         if (folder_.size())\r
1194         {\r
1195                 uIcon = bShowIcons ? IDI_MENUFOLDER : 0;\r
1196                 myIDMap[idCmd - idCmdFirst] = ShellSubMenuFolder;\r
1197                 myIDMap[idCmd] = ShellSubMenuFolder;\r
1198                 menuiteminfo.dwItemData = (ULONG_PTR)g_MenuIDString;\r
1199         }\r
1200         else if (!bShortcut && (files_.size()==1))\r
1201         {\r
1202                 uIcon = bShowIcons ? IDI_MENUFILE : 0;\r
1203                 myIDMap[idCmd - idCmdFirst] = ShellSubMenuFile;\r
1204                 myIDMap[idCmd] = ShellSubMenuFile;\r
1205         }\r
1206         else if (bShortcut && (files_.size()==1))\r
1207         {\r
1208                 uIcon = bShowIcons ? IDI_MENULINK : 0;\r
1209                 myIDMap[idCmd - idCmdFirst] = ShellSubMenuLink;\r
1210                 myIDMap[idCmd] = ShellSubMenuLink;\r
1211         }\r
1212         else if (files_.size() > 1)\r
1213         {\r
1214                 uIcon = bShowIcons ? IDI_MENUMULTIPLE : 0;\r
1215                 myIDMap[idCmd - idCmdFirst] = ShellSubMenuMultiple;\r
1216                 myIDMap[idCmd] = ShellSubMenuMultiple;\r
1217         }\r
1218         else\r
1219         {\r
1220                 myIDMap[idCmd - idCmdFirst] = ShellSubMenu;\r
1221                 myIDMap[idCmd] = ShellSubMenu;\r
1222         }\r
1223         HBITMAP bmp = NULL;\r
1224         if ((fullver < 0x500)||(fullver == 0x500 && !uFlags))\r
1225         {\r
1226                 bmp = IconToBitmap(uIcon);\r
1227                 menuiteminfo.fMask = MIIM_STRING | MIIM_ID | MIIM_SUBMENU | MIIM_CHECKMARKS | MIIM_DATA;\r
1228         }\r
1229         else\r
1230         {\r
1231                 menuiteminfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_SUBMENU | MIIM_DATA | MIIM_BITMAP | MIIM_STRING;\r
1232                 if (bShowIcons)\r
1233                         menuiteminfo.hbmpItem = (fullver >= 0x600) ? IconToBitmapPARGB32(uIcon) : HBMMENU_CALLBACK;\r
1234         }\r
1235         menuiteminfo.hbmpChecked = bmp;\r
1236         menuiteminfo.hbmpUnchecked = bmp;\r
1237         menuiteminfo.hSubMenu = subMenu;\r
1238         menuiteminfo.wID = idCmd++;\r
1239         InsertMenuItem(hMenu, indexMenu++, TRUE, &menuiteminfo);\r
1240 \r
1241         //separator after\r
1242         InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL); idCmd++;\r
1243 \r
1244         //return number of menu items added\r
1245         return ResultFromScode(MAKE_SCODE(SEVERITY_SUCCESS, 0, (USHORT)(idCmd - idCmdFirst)));\r
1246 }\r
1247 \r
1248 \r
1249 // This is called when you invoke a command on the menu:\r
1250 STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)\r
1251 {\r
1252         PreserveChdir preserveChdir;\r
1253         HRESULT hr = E_INVALIDARG;\r
1254         if (lpcmi == NULL)\r
1255                 return hr;\r
1256 \r
1257         std::string command;\r
1258         std::string parent;\r
1259         std::string file;\r
1260 \r
1261         if ((files_.size() > 0)||(folder_.size() > 0))\r
1262         {\r
1263                 UINT idCmd = LOWORD(lpcmi->lpVerb);\r
1264 \r
1265                 if (HIWORD(lpcmi->lpVerb))\r
1266                 {\r
1267                         stdstring verb = stdstring(MultibyteToWide(lpcmi->lpVerb));\r
1268                         std::map<stdstring, UINT_PTR>::const_iterator verb_it = myVerbsMap.lower_bound(verb);\r
1269                         if (verb_it != myVerbsMap.end() && verb_it->first == verb)\r
1270                                 idCmd = verb_it->second;\r
1271                         else\r
1272                                 return hr;\r
1273                 }\r
1274 \r
1275                 // See if we have a handler interface for this id\r
1276                 std::map<UINT_PTR, UINT_PTR>::const_iterator id_it = myIDMap.lower_bound(idCmd);\r
1277                 if (id_it != myIDMap.end() && id_it->first == idCmd)\r
1278                 {\r
1279                         STARTUPINFO startup;\r
1280                         PROCESS_INFORMATION process;\r
1281                         memset(&startup, 0, sizeof(startup));\r
1282                         startup.cb = sizeof(startup);\r
1283                         memset(&process, 0, sizeof(process));\r
1284                         CRegStdString tortoiseProcPath(_T("Software\\TortoiseGit\\ProcPath"), _T("TortoiseProc.exe"), false, HKEY_LOCAL_MACHINE);\r
1285                         CRegStdString tortoiseMergePath(_T("Software\\TortoiseGit\\TMergePath"), _T("TortoiseMerge.exe"), false, HKEY_LOCAL_MACHINE);\r
1286 \r
1287                         //TortoiseProc expects a command line of the form:\r
1288                         //"/command:<commandname> /pathfile:<path> /startrev:<startrevision> /endrev:<endrevision> /deletepathfile\r
1289                         // or\r
1290                         //"/command:<commandname> /path:<path> /startrev:<startrevision> /endrev:<endrevision>\r
1291                         //\r
1292                         //* path is a path to a single file/directory for commands which only act on single items (log, checkout, ...)\r
1293                         //* pathfile is a path to a temporary file which contains a list of file paths\r
1294                         stdstring svnCmd = _T(" /command:");\r
1295                         stdstring tempfile;\r
1296                         switch (id_it->second)\r
1297                         {\r
1298                                 //#region case\r
1299                         case ShellMenuCheckout:\r
1300                                 svnCmd += _T("checkout /path:\"");\r
1301                                 svnCmd += folder_;\r
1302                                 svnCmd += _T("\"");\r
1303                                 break;\r
1304                         case ShellMenuUpdate:\r
1305                                 tempfile = WriteFileListToTempFile();\r
1306                                 svnCmd += _T("update /pathfile:\"");\r
1307                                 svnCmd += tempfile;\r
1308                                 svnCmd += _T("\"");\r
1309                                 svnCmd += _T(" /deletepathfile");\r
1310                                 break;\r
1311                         case ShellMenuUpdateExt:\r
1312                                 tempfile = WriteFileListToTempFile();\r
1313                                 svnCmd += _T("update /pathfile:\"");\r
1314                                 svnCmd += tempfile;\r
1315                                 svnCmd += _T("\"");\r
1316                                 svnCmd += _T(" /deletepathfile");\r
1317                                 svnCmd += _T(" /rev");\r
1318                                 break;\r
1319                         case ShellMenuCommit:\r
1320                                 tempfile = WriteFileListToTempFile();\r
1321                                 svnCmd += _T("commit /pathfile:\"");\r
1322                                 svnCmd += tempfile;\r
1323                                 svnCmd += _T("\"");\r
1324                                 svnCmd += _T(" /deletepathfile");\r
1325                                 break;\r
1326                         case ShellMenuAdd:\r
1327                         case ShellMenuAddAsReplacement:\r
1328                                 tempfile = WriteFileListToTempFile();\r
1329                                 svnCmd += _T("add /pathfile:\"");\r
1330                                 svnCmd += tempfile;\r
1331                                 svnCmd += _T("\"");\r
1332                                 svnCmd += _T(" /deletepathfile");\r
1333                                 break;\r
1334                         case ShellMenuIgnore:\r
1335                                 tempfile = WriteFileListToTempFile();\r
1336                                 svnCmd += _T("ignore /pathfile:\"");\r
1337                                 svnCmd += tempfile;\r
1338                                 svnCmd += _T("\"");\r
1339                                 svnCmd += _T(" /deletepathfile");\r
1340                                 break;\r
1341                         case ShellMenuIgnoreCaseSensitive:\r
1342                                 tempfile = WriteFileListToTempFile();\r
1343                                 svnCmd += _T("ignore /pathfile:\"");\r
1344                                 svnCmd += tempfile;\r
1345                                 svnCmd += _T("\"");\r
1346                                 svnCmd += _T(" /deletepathfile");\r
1347                                 svnCmd += _T(" /onlymask");\r
1348                                 break;\r
1349                         case ShellMenuUnIgnore:\r
1350                                 tempfile = WriteFileListToTempFile();\r
1351                                 svnCmd += _T("unignore /pathfile:\"");\r
1352                                 svnCmd += tempfile;\r
1353                                 svnCmd += _T("\"");\r
1354                                 svnCmd += _T(" /deletepathfile");\r
1355                                 break;\r
1356                         case ShellMenuUnIgnoreCaseSensitive:\r
1357                                 tempfile = WriteFileListToTempFile();\r
1358                                 svnCmd += _T("unignore /pathfile:\"");\r
1359                                 svnCmd += tempfile;\r
1360                                 svnCmd += _T("\"");\r
1361                                 svnCmd += _T(" /deletepathfile");\r
1362                                 svnCmd += _T(" /onlymask");\r
1363                                 break;\r
1364                         case ShellMenuRevert:\r
1365                                 tempfile = WriteFileListToTempFile();\r
1366                                 svnCmd += _T("revert /pathfile:\"");\r
1367                                 svnCmd += tempfile;\r
1368                                 svnCmd += _T("\"");\r
1369                                 svnCmd += _T(" /deletepathfile");\r
1370                                 break;\r
1371                         case ShellMenuDelUnversioned:\r
1372                                 svnCmd += _T("delunversioned /path:\"");\r
1373                                 svnCmd += folder_;\r
1374                                 svnCmd += _T("\"");\r
1375                                 break;\r
1376                         case ShellMenuCleanup:\r
1377                                 tempfile = WriteFileListToTempFile();\r
1378                                 svnCmd += _T("cleanup /pathfile:\"");\r
1379                                 svnCmd += tempfile;\r
1380                                 svnCmd += _T("\"");\r
1381                                 svnCmd += _T(" /deletepathfile");\r
1382                                 break;\r
1383                         case ShellMenuResolve:\r
1384                                 tempfile = WriteFileListToTempFile();\r
1385                                 svnCmd += _T("resolve /pathfile:\"");\r
1386                                 svnCmd += tempfile;\r
1387                                 svnCmd += _T("\"");\r
1388                                 svnCmd += _T(" /deletepathfile");\r
1389                                 break;\r
1390                         case ShellMenuSwitch:\r
1391                                 svnCmd += _T("switch /path:\"");\r
1392                                 if (files_.size() > 0)\r
1393                                         svnCmd += files_.front();\r
1394                                 else\r
1395                                         svnCmd += folder_;\r
1396                                 svnCmd += _T("\"");\r
1397                                 break;\r
1398                         case ShellMenuImport:\r
1399                                 svnCmd += _T("import /path:\"");\r
1400                                 svnCmd += folder_;\r
1401                                 svnCmd += _T("\"");\r
1402                                 break;\r
1403                         case ShellMenuExport:\r
1404                                 svnCmd += _T("export /path:\"");\r
1405                                 svnCmd += folder_;\r
1406                                 svnCmd += _T("\"");\r
1407                                 break;\r
1408                         case ShellMenuAbout:\r
1409                                 svnCmd += _T("about");\r
1410                                 break;\r
1411                         case ShellMenuCreateRepos:\r
1412                                 svnCmd += _T("repocreate /path:\"");\r
1413                                 svnCmd += folder_;\r
1414                                 svnCmd += _T("\"");\r
1415                                 break;\r
1416                         case ShellMenuMerge:\r
1417                                 svnCmd += _T("merge /path:\"");\r
1418                                 if (files_.size() > 0)\r
1419                                         svnCmd += files_.front();\r
1420                                 else\r
1421                                         svnCmd += folder_;\r
1422                                 svnCmd += _T("\"");\r
1423                                 break;\r
1424                         case ShellMenuMergeAll:\r
1425                                 svnCmd += _T("mergeall /path:\"");\r
1426                                 if (files_.size() > 0)\r
1427                                         svnCmd += files_.front();\r
1428                                 else\r
1429                                         svnCmd += folder_;\r
1430                                 svnCmd += _T("\"");\r
1431                                 break;\r
1432                         case ShellMenuCopy:\r
1433                                 svnCmd += _T("copy /path:\"");\r
1434                                 if (files_.size() > 0)\r
1435                                         svnCmd += files_.front();\r
1436                                 else\r
1437                                         svnCmd += folder_;\r
1438                                 svnCmd += _T("\"");\r
1439                                 break;\r
1440                         case ShellMenuSettings:\r
1441                                 svnCmd += _T("settings");\r
1442                                 break;\r
1443                         case ShellMenuHelp:\r
1444                                 svnCmd += _T("help");\r
1445                                 break;\r
1446                         case ShellMenuRename:\r
1447                                 svnCmd += _T("rename /path:\"");\r
1448                                 if (files_.size() > 0)\r
1449                                         svnCmd += files_.front();\r
1450                                 else\r
1451                                         svnCmd += folder_;\r
1452                                 svnCmd += _T("\"");\r
1453                                 break;\r
1454                         case ShellMenuRemove:\r
1455                                 tempfile = WriteFileListToTempFile();\r
1456                                 svnCmd += _T("remove /pathfile:\"");\r
1457                                 svnCmd += tempfile;\r
1458                                 svnCmd += _T("\"");\r
1459                                 svnCmd += _T(" /deletepathfile");\r
1460                                 break;\r
1461                         case ShellMenuRemoveKeep:\r
1462                                 tempfile = WriteFileListToTempFile();\r
1463                                 svnCmd += _T("remove /pathfile:\"");\r
1464                                 svnCmd += tempfile;\r
1465                                 svnCmd += _T("\"");\r
1466                                 svnCmd += _T(" /deletepathfile");\r
1467                                 svnCmd += _T(" /keep");\r
1468                                 break;\r
1469                         case ShellMenuDiff:\r
1470                                 svnCmd += _T("diff /path:\"");\r
1471                                 if (files_.size() == 1)\r
1472                                         svnCmd += files_.front();\r
1473                                 else if (files_.size() == 2)\r
1474                                 {\r
1475                                         std::vector<stdstring>::iterator I = files_.begin();\r
1476                                         svnCmd += *I;\r
1477                                         I++;\r
1478                                         svnCmd += _T("\" /path2:\"");\r
1479                                         svnCmd += *I;\r
1480                                 }\r
1481                                 else\r
1482                                         svnCmd += folder_;\r
1483                                 svnCmd += _T("\"");\r
1484                                 if (GetAsyncKeyState(VK_SHIFT) & 0x8000)\r
1485                                         svnCmd += _T(" /alternative");\r
1486                                 break;\r
1487                         case ShellMenuPrevDiff:\r
1488                                 svnCmd += _T("prevdiff /path:\"");\r
1489                                 if (files_.size() == 1)\r
1490                                         svnCmd += files_.front();\r
1491                                 else\r
1492                                         svnCmd += folder_;\r
1493                                 svnCmd += _T("\"");\r
1494                                 if (GetAsyncKeyState(VK_SHIFT) & 0x8000)\r
1495                                         svnCmd += _T(" /alternative");\r
1496                                 break;\r
1497                         case ShellMenuUrlDiff:\r
1498                                 svnCmd += _T("urldiff /path:\"");\r
1499                                 if (files_.size() == 1)\r
1500                                         svnCmd += files_.front();\r
1501                                 else\r
1502                                         svnCmd += folder_;\r
1503                                 svnCmd += _T("\"");\r
1504                                 break;\r
1505                         case ShellMenuDropCopyAdd:\r
1506                                 tempfile = WriteFileListToTempFile();\r
1507                                 svnCmd += _T("dropcopyadd /pathfile:\"");\r
1508                                 svnCmd += tempfile;\r
1509                                 svnCmd += _T("\"");\r
1510                                 svnCmd += _T(" /deletepathfile");\r
1511                                 svnCmd += _T(" /droptarget:\"");\r
1512                                 svnCmd += folder_;\r
1513                                 svnCmd += _T("\"";)\r
1514                                         break;\r
1515                         case ShellMenuDropCopy:\r
1516                                 tempfile = WriteFileListToTempFile();\r
1517                                 svnCmd += _T("dropcopy /pathfile:\"");\r
1518                                 svnCmd += tempfile;\r
1519                                 svnCmd += _T("\"");\r
1520                                 svnCmd += _T(" /deletepathfile");\r
1521                                 svnCmd += _T(" /droptarget:\"");\r
1522                                 svnCmd += folder_;\r
1523                                 svnCmd += _T("\"";)\r
1524                                         break;\r
1525                         case ShellMenuDropCopyRename:\r
1526                                 tempfile = WriteFileListToTempFile();\r
1527                                 svnCmd += _T("dropcopy /pathfile:\"");\r
1528                                 svnCmd += tempfile;\r
1529                                 svnCmd += _T("\"");\r
1530                                 svnCmd += _T(" /deletepathfile");\r
1531                                 svnCmd += _T(" /droptarget:\"");\r
1532                                 svnCmd += folder_;\r
1533                                 svnCmd += _T("\" /rename";)\r
1534                                         break;\r
1535                         case ShellMenuDropMove:\r
1536                                 tempfile = WriteFileListToTempFile();\r
1537                                 svnCmd += _T("dropmove /pathfile:\"");\r
1538                                 svnCmd += tempfile;\r
1539                                 svnCmd += _T("\"");\r
1540                                 svnCmd += _T(" /deletepathfile");\r
1541                                 svnCmd += _T(" /droptarget:\"");\r
1542                                 svnCmd += folder_;\r
1543                                 svnCmd += _T("\"");\r
1544                                 break;\r
1545                         case ShellMenuDropMoveRename:\r
1546                                 tempfile = WriteFileListToTempFile();\r
1547                                 svnCmd += _T("dropmove /pathfile:\"");\r
1548                                 svnCmd += tempfile;\r
1549                                 svnCmd += _T("\"");\r
1550                                 svnCmd += _T(" /deletepathfile");\r
1551                                 svnCmd += _T(" /droptarget:\"");\r
1552                                 svnCmd += folder_;\r
1553                                 svnCmd += _T("\" /rename";)\r
1554                                 break;\r
1555                         case ShellMenuDropExport:\r
1556                                 tempfile = WriteFileListToTempFile();\r
1557                                 svnCmd += _T("dropexport /pathfile:\"");\r
1558                                 svnCmd += tempfile;\r
1559                                 svnCmd += _T("\"");\r
1560                                 svnCmd += _T(" /deletepathfile");\r
1561                                 svnCmd += _T(" /droptarget:\"");\r
1562                                 svnCmd += folder_;\r
1563                                 svnCmd += _T("\"");\r
1564                                 break;\r
1565                         case ShellMenuDropExportExtended:\r
1566                                 tempfile = WriteFileListToTempFile();\r
1567                                 svnCmd += _T("dropexport /pathfile:\"");\r
1568                                 svnCmd += tempfile;\r
1569                                 svnCmd += _T("\"");\r
1570                                 svnCmd += _T(" /deletepathfile");\r
1571                                 svnCmd += _T(" /droptarget:\"");\r
1572                                 svnCmd += folder_;\r
1573                                 svnCmd += _T("\"");\r
1574                                 svnCmd += _T(" /extended");\r
1575                                 break;\r
1576                         case ShellMenuLog:\r
1577                                 svnCmd += _T("log /path:\"");\r
1578                                 if (files_.size() > 0)\r
1579                                         svnCmd += files_.front();\r
1580                                 else\r
1581                                         svnCmd += folder_;\r
1582                                 svnCmd += _T("\"");\r
1583                                 break;\r
1584                         case ShellMenuConflictEditor:\r
1585                                 svnCmd += _T("conflicteditor /path:\"");\r
1586                                 if (files_.size() > 0)\r
1587                                         svnCmd += files_.front();\r
1588                                 else\r
1589                                         svnCmd += folder_;\r
1590                                 svnCmd += _T("\"");\r
1591                                 break;\r
1592                         case ShellMenuRelocate:\r
1593                                 svnCmd += _T("relocate /path:\"");\r
1594                                 if (files_.size() > 0)\r
1595                                         svnCmd += files_.front();\r
1596                                 else\r
1597                                         svnCmd += folder_;\r
1598                                 svnCmd += _T("\"");\r
1599                                 break;\r
1600                         case ShellMenuShowChanged:\r
1601                                 if (files_.size() > 1)\r
1602                 {\r
1603                                     tempfile = WriteFileListToTempFile();\r
1604                                     svnCmd += _T("repostatus /pathfile:\"");\r
1605                                     svnCmd += tempfile;\r
1606                                 svnCmd += _T("\"");\r
1607                                 svnCmd += _T(" /deletepathfile");\r
1608                 }\r
1609                 else\r
1610                 {\r
1611                     svnCmd += _T("repostatus /path:\"");\r
1612                                     if (files_.size() > 0)\r
1613                                             svnCmd += files_.front();\r
1614                                     else\r
1615                                             svnCmd += folder_;\r
1616                                 svnCmd += _T("\"");\r
1617                 }\r
1618                                 break;\r
1619                         case ShellMenuRepoBrowse:\r
1620                                 svnCmd += _T("repobrowser /path:\"");\r
1621                                 if (files_.size() > 0)\r
1622                                         svnCmd += files_.front();\r
1623                                 else\r
1624                                         svnCmd += folder_;\r
1625                                 svnCmd += _T("\"");\r
1626                                 break;\r
1627                         case ShellMenuBlame:\r
1628                                 svnCmd += _T("blame /path:\"");\r
1629                                 if (files_.size() > 0)\r
1630                                         svnCmd += files_.front();\r
1631                                 else\r
1632                                         svnCmd += folder_;\r
1633                                 svnCmd += _T("\"");\r
1634                                 break;\r
1635                         case ShellMenuCreatePatch:\r
1636                                 tempfile = WriteFileListToTempFile();\r
1637                                 svnCmd += _T("createpatch /pathfile:\"");\r
1638                                 svnCmd += tempfile;\r
1639                                 svnCmd += _T("\"");\r
1640                                 svnCmd += _T(" /deletepathfile");\r
1641                                 break;\r
1642                         case ShellMenuApplyPatch:\r
1643                                 if ((itemStates & ITEMIS_PATCHINCLIPBOARD) && ((~itemStates) & ITEMIS_PATCHFILE))\r
1644                                 {\r
1645                                         // if there's a patch file in the clipboard, we save it\r
1646                                         // to a temporary file and tell TortoiseMerge to use that one\r
1647                                         UINT cFormat = RegisterClipboardFormat(_T("TGIT_UNIFIEDDIFF"));\r
1648                                         if ((cFormat)&&(OpenClipboard(NULL)))\r
1649                                         { \r
1650                                                 HGLOBAL hglb = GetClipboardData(cFormat); \r
1651                                                 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb); \r
1652 \r
1653                                                 DWORD len = GetTempPath(0, NULL);\r
1654                                                 TCHAR * path = new TCHAR[len+1];\r
1655                                                 TCHAR * tempF = new TCHAR[len+100];\r
1656                                                 GetTempPath (len+1, path);\r
1657                                                 GetTempFileName (path, TEXT("git"), 0, tempF);\r
1658                                                 std::wstring sTempFile = std::wstring(tempF);\r
1659                                                 delete [] path;\r
1660                                                 delete [] tempF;\r
1661 \r
1662                                                 FILE * outFile;\r
1663                                                 size_t patchlen = strlen(lpstr);\r
1664                                                 _tfopen_s(&outFile, sTempFile.c_str(), _T("wb"));\r
1665                                                 if(outFile)\r
1666                                                 {\r
1667                                                         size_t size = fwrite(lpstr, sizeof(char), patchlen, outFile);\r
1668                                                         if (size == patchlen)\r
1669                                                         {\r
1670                                                                 itemStates |= ITEMIS_PATCHFILE;\r
1671                                                                 files_.clear();\r
1672                                                                 files_.push_back(sTempFile);\r
1673                                                         }\r
1674                                                         fclose(outFile);\r
1675                                                 }\r
1676                                                 GlobalUnlock(hglb); \r
1677                                                 CloseClipboard(); \r
1678                                         } \r
1679                                 }\r
1680                                 if (itemStates & ITEMIS_PATCHFILE)\r
1681                                 {\r
1682                                         svnCmd = _T(" /diff:\"");\r
1683                                         if (files_.size() > 0)\r
1684                                         {\r
1685                                                 svnCmd += files_.front();\r
1686                                                 if (itemStatesFolder & ITEMIS_FOLDERINSVN)\r
1687                                                 {\r
1688                                                         svnCmd += _T("\" /patchpath:\"");\r
1689                                                         svnCmd += folder_;\r
1690                                                 }\r
1691                                         }\r
1692                                         else\r
1693                                                 svnCmd += folder_;\r
1694                                         if (itemStates & ITEMIS_INVERSIONEDFOLDER)\r
1695                                                 svnCmd += _T("\" /wc");\r
1696                                         else\r
1697                                                 svnCmd += _T("\"");\r
1698                                 }\r
1699                                 else\r
1700                                 {\r
1701                                         svnCmd = _T(" /patchpath:\"");\r
1702                                         if (files_.size() > 0)\r
1703                                                 svnCmd += files_.front();\r
1704                                         else\r
1705                                                 svnCmd += folder_;\r
1706                                         svnCmd += _T("\"");\r
1707                                 }\r
1708                                 myIDMap.clear();\r
1709                                 myVerbsIDMap.clear();\r
1710                                 myVerbsMap.clear();\r
1711                                 if (CreateProcess(((stdstring)tortoiseMergePath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)\r
1712                                 {\r
1713                                         LPVOID lpMsgBuf;\r
1714                                         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
1715                                                 FORMAT_MESSAGE_FROM_SYSTEM | \r
1716                                                 FORMAT_MESSAGE_IGNORE_INSERTS,\r
1717                                                 NULL,\r
1718                                                 GetLastError(),\r
1719                                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language\r
1720                                                 (LPTSTR) &lpMsgBuf,\r
1721                                                 0,\r
1722                                                 NULL \r
1723                                                 );\r
1724                                         MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseMerge launch failed"), MB_OK | MB_ICONINFORMATION );\r
1725                                         LocalFree( lpMsgBuf );\r
1726                                 }\r
1727                                 CloseHandle(process.hThread);\r
1728                                 CloseHandle(process.hProcess);\r
1729                                 return NOERROR;\r
1730                                 break;\r
1731                         case ShellMenuRevisionGraph:\r
1732                                 svnCmd += _T("revisiongraph /path:\"");\r
1733                                 if (files_.size() > 0)\r
1734                                         svnCmd += files_.front();\r
1735                                 else\r
1736                                         svnCmd += folder_;\r
1737                                 svnCmd += _T("\"");\r
1738                                 break;\r
1739                         case ShellMenuLock:\r
1740                                 tempfile = WriteFileListToTempFile();\r
1741                                 svnCmd += _T("lock /pathfile:\"");\r
1742                                 svnCmd += tempfile;\r
1743                                 svnCmd += _T("\"");\r
1744                                 svnCmd += _T(" /deletepathfile");\r
1745                                 break;\r
1746                         case ShellMenuUnlock:\r
1747                                 tempfile = WriteFileListToTempFile();\r
1748                                 svnCmd += _T("unlock /pathfile:\"");\r
1749                                 svnCmd += tempfile;\r
1750                                 svnCmd += _T("\"");\r
1751                                 svnCmd += _T(" /deletepathfile");\r
1752                                 break;\r
1753                         case ShellMenuUnlockForce:\r
1754                                 tempfile = WriteFileListToTempFile();\r
1755                                 svnCmd += _T("unlock /pathfile:\"");\r
1756                                 svnCmd += tempfile;\r
1757                                 svnCmd += _T("\"");\r
1758                                 svnCmd += _T(" /deletepathfile");\r
1759                                 svnCmd += _T(" /force");\r
1760                                 break;\r
1761                         case ShellMenuProperties:\r
1762                                 tempfile = WriteFileListToTempFile();\r
1763                                 svnCmd += _T("properties /pathfile:\"");\r
1764                                 svnCmd += tempfile;\r
1765                                 svnCmd += _T("\"");\r
1766                                 svnCmd += _T(" /deletepathfile");\r
1767                                 break;\r
1768                         case ShellMenuClipPaste:\r
1769                                 if (WriteClipboardPathsToTempFile(tempfile))\r
1770                                 {\r
1771                                         bool bCopy = true;\r
1772                                         UINT cPrefDropFormat = RegisterClipboardFormat(_T("Preferred DropEffect"));\r
1773                                         if (cPrefDropFormat)\r
1774                                         {\r
1775                                                 if (OpenClipboard(lpcmi->hwnd))\r
1776                                                 {\r
1777                                                         HGLOBAL hglb = GetClipboardData(cPrefDropFormat);\r
1778                                                         if (hglb)\r
1779                                                         {\r
1780                                                                 DWORD* effect = (DWORD*) GlobalLock(hglb);\r
1781                                                                 if (*effect == DROPEFFECT_MOVE)\r
1782                                                                         bCopy = false;\r
1783                                                                 GlobalUnlock(hglb);\r
1784                                                         }\r
1785                                                         CloseClipboard();\r
1786                                                 }\r
1787                                         }\r
1788 \r
1789                                         if (bCopy)\r
1790                                                 svnCmd += _T("pastecopy /pathfile:\"");\r
1791                                         else\r
1792                                                 svnCmd += _T("pastemove /pathfile:\"");\r
1793                                         svnCmd += tempfile;\r
1794                                         svnCmd += _T("\"");\r
1795                                         svnCmd += _T(" /deletepathfile");\r
1796                                         svnCmd += _T(" /droptarget:\"");\r
1797                                         svnCmd += folder_;\r
1798                                         svnCmd += _T("\"");\r
1799                                 }\r
1800                                 else return NOERROR;\r
1801                                 break;\r
1802                         case ShellMenuClone:\r
1803                                 svnCmd += _T("clone /path:\"");\r
1804                                 svnCmd += folder_;\r
1805                                 svnCmd += _T("\"");\r
1806                                 break;\r
1807                         case ShellMenuPull:\r
1808                                 svnCmd += _T("pull /path:\"");\r
1809                                 if (files_.size() > 0)\r
1810                                         svnCmd += files_.front();\r
1811                                 else\r
1812                                         svnCmd += folder_;\r
1813                                 svnCmd += _T("\"");\r
1814                                 break;\r
1815                         case ShellMenuPush:\r
1816                                 svnCmd += _T("push /path:\"");\r
1817                                 if (files_.size() > 0)\r
1818                                         svnCmd += files_.front();\r
1819                                 else\r
1820                                         svnCmd += folder_;\r
1821                                 svnCmd += _T("\"");\r
1822                                 break;\r
1823                         case ShellMenuBranch:\r
1824                                 svnCmd += _T("branch /path:\"");\r
1825                                 if (files_.size() > 0)\r
1826                                         svnCmd += files_.front();\r
1827                                 else\r
1828                                         svnCmd += folder_;\r
1829                                 svnCmd += _T("\"");\r
1830                                 break;\r
1831                         \r
1832                         case ShellMenuTag:\r
1833                                 svnCmd += _T("tag /path:\"");\r
1834                                 if (files_.size() > 0)\r
1835                                         svnCmd += files_.front();\r
1836                                 else\r
1837                                         svnCmd += folder_;\r
1838                                 svnCmd += _T("\"");\r
1839                                 break;\r
1840 \r
1841                         case ShellMenuFormatPatch:\r
1842                                 svnCmd += _T("formatpatch /path:\"");\r
1843                                 if (files_.size() > 0)\r
1844                                         svnCmd += files_.front();\r
1845                                 else\r
1846                                         svnCmd += folder_;\r
1847                                 svnCmd += _T("\"");\r
1848                                 break;\r
1849 \r
1850                         case ShellMenuImportPatch:\r
1851                                 svnCmd += _T("importpatch /path:\"");\r
1852                                 if (files_.size() > 0)\r
1853                                         svnCmd += files_.front();\r
1854                                 else\r
1855                                         svnCmd += folder_;\r
1856                                 svnCmd += _T("\"");\r
1857                                 break;\r
1858 \r
1859                         case ShellMenuCherryPick:\r
1860                                 svnCmd += _T("cherrypick /path:\"");\r
1861                                 if (files_.size() > 0)\r
1862                                         svnCmd += files_.front();\r
1863                                 else\r
1864                                         svnCmd += folder_;\r
1865                                 svnCmd += _T("\"");\r
1866                                 break;\r
1867                         case ShellMenuFetch:\r
1868                                 svnCmd += _T("fetch /path:\"");\r
1869                                 if (files_.size() > 0)\r
1870                                         svnCmd += files_.front();\r
1871                                 else\r
1872                                         svnCmd += folder_;\r
1873                                 svnCmd += _T("\"");\r
1874                                 break;\r
1875 \r
1876                         default:\r
1877                                 break;\r
1878                                 //#endregion\r
1879                         } // switch (id_it->second) \r
1880                         svnCmd += _T(" /hwnd:");\r
1881                         TCHAR buf[30];\r
1882                         _stprintf_s(buf, 30, _T("%d"), lpcmi->hwnd);\r
1883                         svnCmd += buf;\r
1884                         myIDMap.clear();\r
1885                         myVerbsIDMap.clear();\r
1886                         myVerbsMap.clear();\r
1887                         if (CreateProcess(((stdstring)tortoiseProcPath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)\r
1888                         {\r
1889                                 LPVOID lpMsgBuf;\r
1890                                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
1891                                         FORMAT_MESSAGE_FROM_SYSTEM | \r
1892                                         FORMAT_MESSAGE_IGNORE_INSERTS,\r
1893                                         NULL,\r
1894                                         GetLastError(),\r
1895                                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language\r
1896                                         (LPTSTR) &lpMsgBuf,\r
1897                                         0,\r
1898                                         NULL \r
1899                                         );\r
1900                                 MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("TortoiseProc Launch failed"), MB_OK | MB_ICONINFORMATION );\r
1901                                 LocalFree( lpMsgBuf );\r
1902                         }\r
1903                         CloseHandle(process.hThread);\r
1904                         CloseHandle(process.hProcess);\r
1905                         hr = NOERROR;\r
1906                 } // if (id_it != myIDMap.end() && id_it->first == idCmd) \r
1907         } // if ((files_.size() > 0)||(folder_.size() > 0)) \r
1908         return hr;\r
1909 \r
1910 }\r
1911 \r
1912 // This is for the status bar and things like that:\r
1913 STDMETHODIMP CShellExt::GetCommandString(UINT_PTR idCmd,\r
1914                                          UINT uFlags,\r
1915                                          UINT FAR * /*reserved*/,\r
1916                                          LPSTR pszName,\r
1917                                          UINT cchMax)\r
1918 {\r
1919         PreserveChdir preserveChdir;\r
1920         //do we know the id?\r
1921         std::map<UINT_PTR, UINT_PTR>::const_iterator id_it = myIDMap.lower_bound(idCmd);\r
1922         if (id_it == myIDMap.end() || id_it->first != idCmd)\r
1923         {\r
1924                 return E_INVALIDARG;            //no, we don't\r
1925         }\r
1926 \r
1927         LoadLangDll();\r
1928         HRESULT hr = E_INVALIDARG;\r
1929 \r
1930         MAKESTRING(IDS_MENUDESCDEFAULT);\r
1931         int menuIndex = 0;\r
1932         while (menuInfo[menuIndex].command != ShellMenuLastEntry)\r
1933         {\r
1934                 if (menuInfo[menuIndex].command == (GitCommands)id_it->second)\r
1935                 {\r
1936                         MAKESTRING(menuInfo[menuIndex].menuDescID);\r
1937                         break;\r
1938                 }\r
1939                 menuIndex++;\r
1940         }\r
1941 \r
1942         const TCHAR * desc = stringtablebuffer;\r
1943         switch(uFlags)\r
1944         {\r
1945         case GCS_HELPTEXTA:\r
1946                 {\r
1947                         std::string help = WideToMultibyte(desc);\r
1948                         lstrcpynA(pszName, help.c_str(), cchMax);\r
1949                         hr = S_OK;\r
1950                         break; \r
1951                 }\r
1952         case GCS_HELPTEXTW: \r
1953                 {\r
1954                         wide_string help = desc;\r
1955                         lstrcpynW((LPWSTR)pszName, help.c_str(), cchMax); \r
1956                         hr = S_OK;\r
1957                         break; \r
1958                 }\r
1959         case GCS_VERBA:\r
1960                 {\r
1961                         std::map<UINT_PTR, stdstring>::const_iterator verb_id_it = myVerbsIDMap.lower_bound(idCmd);\r
1962                         if (verb_id_it != myVerbsIDMap.end() && verb_id_it->first == idCmd)\r
1963                         {\r
1964                                 std::string help = WideToMultibyte(verb_id_it->second);\r
1965                                 lstrcpynA(pszName, help.c_str(), cchMax);\r
1966                                 hr = S_OK;\r
1967                         }\r
1968                 }\r
1969                 break;\r
1970         case GCS_VERBW:\r
1971                 {\r
1972                         std::map<UINT_PTR, stdstring>::const_iterator verb_id_it = myVerbsIDMap.lower_bound(idCmd);\r
1973                         if (verb_id_it != myVerbsIDMap.end() && verb_id_it->first == idCmd)\r
1974                         {\r
1975                                 wide_string help = verb_id_it->second;\r
1976                                 ATLTRACE("verb : %ws\n", help.c_str());\r
1977                                 lstrcpynW((LPWSTR)pszName, help.c_str(), cchMax); \r
1978                                 hr = S_OK;\r
1979                         }\r
1980                 }\r
1981                 break;\r
1982         }\r
1983         return hr;\r
1984 }\r
1985 \r
1986 STDMETHODIMP CShellExt::HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
1987 {\r
1988         LRESULT res;\r
1989         return HandleMenuMsg2(uMsg, wParam, lParam, &res);\r
1990 }\r
1991 \r
1992 STDMETHODIMP CShellExt::HandleMenuMsg2(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *pResult)\r
1993 {\r
1994         PreserveChdir preserveChdir;\r
1995 \r
1996         LRESULT res;\r
1997         if (pResult == NULL)\r
1998                 pResult = &res;\r
1999         *pResult = FALSE;\r
2000 \r
2001         LoadLangDll();\r
2002         switch (uMsg)\r
2003         {\r
2004         case WM_MEASUREITEM:\r
2005                 {\r
2006                         MEASUREITEMSTRUCT* lpmis = (MEASUREITEMSTRUCT*)lParam;\r
2007                         if (lpmis==NULL)\r
2008                                 break;\r
2009                         lpmis->itemWidth += 2;\r
2010                         if (lpmis->itemHeight < 16)\r
2011                                 lpmis->itemHeight = 16;\r
2012                         *pResult = TRUE;\r
2013                 }\r
2014                 break;\r
2015         case WM_DRAWITEM:\r
2016                 {\r
2017                         LPCTSTR resource;\r
2018                         DRAWITEMSTRUCT* lpdis = (DRAWITEMSTRUCT*)lParam;\r
2019                         if ((lpdis==NULL)||(lpdis->CtlType != ODT_MENU))\r
2020                                 return S_OK;            //not for a menu\r
2021                         resource = GetMenuTextFromResource(myIDMap[lpdis->itemID]);\r
2022                         if (resource == NULL)\r
2023                                 return S_OK;\r
2024                         HICON hIcon = (HICON)LoadImage(g_hResInst, resource, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);\r
2025                         if (hIcon == NULL)\r
2026                                 return S_OK;\r
2027                         DrawIconEx(lpdis->hDC,\r
2028                                 lpdis->rcItem.left - 16,\r
2029                                 lpdis->rcItem.top + (lpdis->rcItem.bottom - lpdis->rcItem.top - 16) / 2,\r
2030                                 hIcon, 16, 16,\r
2031                                 0, NULL, DI_NORMAL);\r
2032                         DestroyIcon(hIcon);\r
2033                         *pResult = TRUE;\r
2034                 }\r
2035                 break;\r
2036         case WM_MENUCHAR:\r
2037                 {\r
2038                         LPCTSTR resource;\r
2039                         TCHAR *szItem;\r
2040                         if (HIWORD(wParam) != MF_POPUP)\r
2041                                 return NOERROR;\r
2042                         int nChar = LOWORD(wParam);\r
2043                         if (_istascii((wint_t)nChar) && _istupper((wint_t)nChar))\r
2044                                 nChar = tolower(nChar);\r
2045                         // we have the char the user pressed, now search that char in all our\r
2046                         // menu items\r
2047                         std::vector<int> accmenus;\r
2048                         for (std::map<UINT_PTR, UINT_PTR>::iterator It = mySubMenuMap.begin(); It != mySubMenuMap.end(); ++It)\r
2049                         {\r
2050                                 resource = GetMenuTextFromResource(mySubMenuMap[It->first]);\r
2051                                 if (resource == NULL)\r
2052                                         continue;\r
2053                                 szItem = stringtablebuffer;\r
2054                                 TCHAR * amp = _tcschr(szItem, '&');\r
2055                                 if (amp == NULL)\r
2056                                         continue;\r
2057                                 amp++;\r
2058                                 int ampChar = LOWORD(*amp);\r
2059                                 if (_istascii((wint_t)ampChar) && _istupper((wint_t)ampChar))\r
2060                                         ampChar = tolower(ampChar);\r
2061                                 if (ampChar == nChar)\r
2062                                 {\r
2063                                         // yep, we found a menu which has the pressed key\r
2064                                         // as an accelerator. Add that menu to the list to\r
2065                                         // process later.\r
2066                                         accmenus.push_back(It->first);\r
2067                                 }\r
2068                         }\r
2069                         if (accmenus.size() == 0)\r
2070                         {\r
2071                                 // no menu with that accelerator key.\r
2072                                 *pResult = MAKELONG(0, MNC_IGNORE);\r
2073                                 return NOERROR;\r
2074                         }\r
2075                         if (accmenus.size() == 1)\r
2076                         {\r
2077                                 // Only one menu with that accelerator key. We're lucky!\r
2078                                 // So just execute that menu entry.\r
2079                                 *pResult = MAKELONG(accmenus[0], MNC_EXECUTE);\r
2080                                 return NOERROR;\r
2081                         }\r
2082                         if (accmenus.size() > 1)\r
2083                         {\r
2084                                 // we have more than one menu item with this accelerator key!\r
2085                                 MENUITEMINFO mif;\r
2086                                 mif.cbSize = sizeof(MENUITEMINFO);\r
2087                                 mif.fMask = MIIM_STATE;\r
2088                                 for (std::vector<int>::iterator it = accmenus.begin(); it != accmenus.end(); ++it)\r
2089                                 {\r
2090                                         GetMenuItemInfo((HMENU)lParam, *it, TRUE, &mif);\r
2091                                         if (mif.fState == MFS_HILITE)\r
2092                                         {\r
2093                                                 // this is the selected item, so select the next one\r
2094                                                 ++it;\r
2095                                                 if (it == accmenus.end())\r
2096                                                         *pResult = MAKELONG(accmenus[0], MNC_SELECT);\r
2097                                                 else\r
2098                                                         *pResult = MAKELONG(*it, MNC_SELECT);\r
2099                                                 return NOERROR;\r
2100                                         }\r
2101                                 }\r
2102                                 *pResult = MAKELONG(accmenus[0], MNC_SELECT);\r
2103                         }\r
2104                 }\r
2105                 break;\r
2106         default:\r
2107                 return NOERROR;\r
2108         }\r
2109 \r
2110         return NOERROR;\r
2111 }\r
2112 \r
2113 LPCTSTR CShellExt::GetMenuTextFromResource(int id)\r
2114 {\r
2115         TCHAR textbuf[255];\r
2116         LPCTSTR resource = NULL;\r
2117         unsigned __int64 layout = g_ShellCache.GetMenuLayout();\r
2118         space = 6;\r
2119 \r
2120         int menuIndex = 0;\r
2121         while (menuInfo[menuIndex].command != ShellMenuLastEntry)\r
2122         {\r
2123                 if (menuInfo[menuIndex].command == id)\r
2124                 {\r
2125                         MAKESTRING(menuInfo[menuIndex].menuTextID);\r
2126                         resource = MAKEINTRESOURCE(menuInfo[menuIndex].iconID);\r
2127                         switch (id)\r
2128                         {\r
2129                         case ShellMenuLock:\r
2130                                 // menu lock is special because it can be set to the top\r
2131                                 // with a separate option in the registry\r
2132                                 space = ((layout & MENULOCK) || ((itemStates & ITEMIS_NEEDSLOCK) && g_ShellCache.IsGetLockTop())) ? 0 : 6;\r
2133                                 if ((layout & MENULOCK) || ((itemStates & ITEMIS_NEEDSLOCK) && g_ShellCache.IsGetLockTop()))\r
2134                                 {\r
2135                                         _tcscpy_s(textbuf, 255, _T("Git "));\r
2136                                         _tcscat_s(textbuf, 255, stringtablebuffer);\r
2137                                         _tcscpy_s(stringtablebuffer, 255, textbuf);\r
2138                                 }\r
2139                                 break;\r
2140                                 // the sub menu entries are special because they're *always* on the top level menu\r
2141                         case ShellSubMenuMultiple:\r
2142                         case ShellSubMenuLink:\r
2143                         case ShellSubMenuFolder:\r
2144                         case ShellSubMenuFile:\r
2145                         case ShellSubMenu:\r
2146                                 space = 0;\r
2147                                 break;\r
2148                         default:\r
2149                                 space = layout & menuInfo[menuIndex].menuID ? 0 : 6;\r
2150                                 if (layout & (menuInfo[menuIndex].menuID)) \r
2151                                 {\r
2152                                         _tcscpy_s(textbuf, 255, _T("Git "));\r
2153                                         _tcscat_s(textbuf, 255, stringtablebuffer);\r
2154                                         _tcscpy_s(stringtablebuffer, 255, textbuf);\r
2155                                 }\r
2156                                 break;\r
2157                         }\r
2158                         return resource;\r
2159                 }\r
2160                 menuIndex++;\r