OSDN Git Service

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