OSDN Git Service

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