2 * provides new shell item service
4 * Copyright 2007 Johannes Anderwald (johannes.anderwald@reactos.org)
5 * Copyright 2009 Andrew Hill
6 * Copyright 2012 Rafal Harabien
7 * Copyright 2019 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
28 CNewMenu::CNewMenu() :
47 void CNewMenu::UnloadItem(SHELLNEW_ITEM
*pItem
)
49 /* Note: free allows NULL as argument */
51 free(pItem
->pwszDesc
);
55 DestroyIcon(pItem
->hIcon
);
57 HeapFree(GetProcessHeap(), 0, pItem
);
60 void CNewMenu::UnloadAllItems()
62 SHELLNEW_ITEM
*pCurItem
;
64 /* Unload the handler items, including the link item */
68 m_pItems
= m_pItems
->pNext
;
75 CNewMenu::SHELLNEW_ITEM
*CNewMenu::LoadItem(LPCWSTR pwszExt
)
78 WCHAR wszBuf
[MAX_PATH
];
82 StringCbPrintfW(wszBuf
, sizeof(wszBuf
), L
"%s\\ShellNew", pwszExt
);
84 TRACE("LoadItem Keyname %s Name %s\n", debugstr_w(pwszExt
), debugstr_w(wszBuf
));
86 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszBuf
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
88 TRACE("Failed to open key\n");
92 /* Find first valid value */
100 {L
"FileName", SHELLNEW_TYPE_FILENAME
, TRUE
, TRUE
},
101 {L
"Command", SHELLNEW_TYPE_COMMAND
, TRUE
, TRUE
},
102 {L
"Data", SHELLNEW_TYPE_DATA
, TRUE
, FALSE
},
103 {L
"NullFile", SHELLNEW_TYPE_NULLFILE
, FALSE
},
108 for (i
= 0; Types
[i
].pszName
; ++i
)
110 /* Note: We are using ANSI function because strings can be treated as data */
112 DWORD dwFlags
= Types
[i
].bStr
? RRF_RT_REG_SZ
: RRF_RT_ANY
;
114 if (RegGetValueW(hKey
, NULL
, Types
[i
].pszName
, dwFlags
, NULL
, NULL
, &cbData
) == ERROR_SUCCESS
)
116 if (Types
[i
].bNeedData
&& cbData
> 0)
118 pData
= (BYTE
*)malloc(cbData
);
119 RegGetValueW(hKey
, NULL
, Types
[i
].pszName
, dwFlags
, &dwType
, pData
, &cbData
);
120 if (!Types
[i
].bStr
&& (dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
))
122 PBYTE pData2
= (PBYTE
)malloc(cbData
);
123 cbData
= WideCharToMultiByte(CP_ACP
, 0, (LPWSTR
)pData
, -1, (LPSTR
)pData2
, cbData
, NULL
, NULL
);
133 /* Was any key found? */
134 if (!Types
[i
].pszName
)
138 if (!SHGetFileInfoW(pwszExt
, FILE_ATTRIBUTE_NORMAL
, &fi
, sizeof(fi
), SHGFI_USEFILEATTRIBUTES
|SHGFI_TYPENAME
|SHGFI_ICON
|SHGFI_SMALLICON
))
141 /* Create new item */
142 SHELLNEW_ITEM
*pNewItem
= static_cast<SHELLNEW_ITEM
*>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SHELLNEW_ITEM
)));
149 TRACE("new item %ls\n", fi
.szTypeName
);
150 pNewItem
->Type
= Types
[i
].Type
;
151 pNewItem
->pData
= pData
;
152 pNewItem
->cbData
= pData
? cbData
: 0;
153 pNewItem
->pwszExt
= _wcsdup(pwszExt
);
154 pNewItem
->pwszDesc
= _wcsdup(fi
.szTypeName
);
156 pNewItem
->hIcon
= fi
.hIcon
;
162 CNewMenu::CacheItems()
169 WCHAR wszName
[MAX_PATH
];
170 SHELLNEW_ITEM
*pNewItem
;
171 SHELLNEW_ITEM
*pCurItem
= NULL
;
173 /* Enumerate all extensions */
174 while (RegEnumKeyW(HKEY_CLASSES_ROOT
, dwIndex
++, wszName
, _countof(wszName
)) == ERROR_SUCCESS
)
176 if (wszName
[0] != L
'.')
179 pNewItem
= LoadItem(wszName
);
182 dwSize
+= wcslen(wszName
) + 1;
183 if (!m_pLinkItem
&& wcsicmp(pNewItem
->pwszExt
, L
".lnk") == 0)
185 /* The unique link handler */
186 m_pLinkItem
= pNewItem
;
189 /* Add at the end of the list */
192 pCurItem
->pNext
= pNewItem
;
197 pCurItem
= m_pItems
= pNewItem
;
204 lpValues
= (LPWSTR
) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwSize
* sizeof(WCHAR
));
208 for (pCurItem
= m_pItems
, lpValue
= lpValues
; pCurItem
; pCurItem
= pCurItem
->pNext
)
210 memcpy(lpValue
, pCurItem
->pwszExt
, (wcslen(pCurItem
->pwszExt
) + 1) * sizeof(WCHAR
));
211 lpValue
+= wcslen(pCurItem
->pwszExt
) + 1;
214 if (RegCreateKeyEx(HKEY_CURRENT_USER
, ShellNewKey
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, NULL
) != ERROR_SUCCESS
)
216 HeapFree(GetProcessHeap(), 0, lpValues
);
220 if (RegSetValueExW(hKey
, L
"Classes", NULL
, REG_MULTI_SZ
, (LPBYTE
)lpValues
, dwSize
* sizeof(WCHAR
)) != ERROR_SUCCESS
)
222 HeapFree(GetProcessHeap(), 0, lpValues
);
227 HeapFree(GetProcessHeap(), 0, lpValues
);
234 CNewMenu::LoadCachedItems()
240 SHELLNEW_ITEM
*pNewItem
;
241 SHELLNEW_ITEM
*pCurItem
= NULL
;
243 if (RegOpenKeyExW(HKEY_CURRENT_USER
, ShellNewKey
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
246 if (RegQueryValueExW(hKey
, L
"Classes", NULL
, NULL
, NULL
, &dwSize
) != ERROR_SUCCESS
)
249 lpValues
= (LPWSTR
) HeapAlloc(GetProcessHeap(), 0, dwSize
);
253 if (RegQueryValueExW(hKey
, L
"Classes", NULL
, NULL
, (LPBYTE
)lpValues
, &dwSize
) != ERROR_SUCCESS
)
255 HeapFree(GetProcessHeap(), 0, lpValues
);
261 for (; '\0' != *wszName
; wszName
+= wcslen(wszName
) + 1)
263 pNewItem
= LoadItem(wszName
);
266 if (!m_pLinkItem
&& wcsicmp(pNewItem
->pwszExt
, L
".lnk") == 0)
268 /* The unique link handler */
269 m_pLinkItem
= pNewItem
;
272 /* Add at the end of the list */
275 pCurItem
->pNext
= pNewItem
;
280 pCurItem
= m_pItems
= pNewItem
;
285 HeapFree(GetProcessHeap(), 0, lpValues
);
292 CNewMenu::LoadAllItems()
294 /* If there are any unload them */
297 if (!LoadCachedItems())
302 return (m_pItems
!= NULL
);
306 CNewMenu::InsertShellNewItems(HMENU hMenu
, UINT idCmdFirst
, UINT Pos
)
310 UINT idCmd
= idCmdFirst
;
312 if (m_pItems
== NULL
)
318 ZeroMemory(&mii
, sizeof(mii
));
319 mii
.cbSize
= sizeof(mii
);
321 /* Insert new folder action */
322 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEWFOLDER
, wszBuf
, _countof(wszBuf
)))
324 mii
.fMask
= MIIM_ID
| MIIM_BITMAP
| MIIM_STRING
;
325 mii
.dwTypeData
= wszBuf
;
326 mii
.cch
= wcslen(mii
.dwTypeData
);
328 mii
.hbmpItem
= HBMMENU_CALLBACK
;
329 if (InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
))
332 /* Insert the new shortcut action */
335 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEWLINK
, wszBuf
, _countof(wszBuf
)))
337 mii
.dwTypeData
= wszBuf
;
338 mii
.cch
= wcslen(mii
.dwTypeData
);
340 if (InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
))
344 /* Insert seperator for custom new action */
345 mii
.fMask
= MIIM_TYPE
| MIIM_ID
;
346 mii
.fType
= MFT_SEPARATOR
;
348 InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
);
350 /* Insert rest of items */
351 mii
.fMask
= MIIM_ID
| MIIM_BITMAP
| MIIM_STRING
;
354 SHELLNEW_ITEM
*pCurItem
= m_pItems
;
357 /* Skip shortcut item */
358 if (pCurItem
== m_pLinkItem
)
361 TRACE("szDesc %s\n", debugstr_w(pCurItem
->pwszDesc
));
362 mii
.dwTypeData
= pCurItem
->pwszDesc
;
363 mii
.cch
= wcslen(mii
.dwTypeData
);
365 if (InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
))
367 pCurItem
= pCurItem
->pNext
;
370 return idCmd
- idCmdFirst
;
373 CNewMenu::SHELLNEW_ITEM
*CNewMenu::FindItemFromIdOffset(UINT IdOffset
)
376 return NULL
; /* Folder */
379 return m_pLinkItem
; /* shortcut */
381 /* Find shell new item */
382 SHELLNEW_ITEM
*pItem
= m_pItems
;
383 for (UINT i
= 2; pItem
; ++i
)
388 pItem
= pItem
->pNext
;
394 HRESULT
CNewMenu::SelectNewItem(LONG wEventId
, UINT uFlags
, LPWSTR pszName
, BOOL bRename
)
396 CComPtr
<IShellBrowser
> lpSB
;
397 CComPtr
<IShellView
> lpSV
;
400 PITEMID_CHILD pidlNewItem
;
403 dwSelectFlags
= SVSI_DESELECTOTHERS
| SVSI_ENSUREVISIBLE
| SVSI_FOCUSED
| SVSI_SELECT
;
405 dwSelectFlags
|= SVSI_EDIT
;
407 /* Notify the view object about the new item */
408 SHChangeNotify(wEventId
, uFlags
, (LPCVOID
) pszName
, NULL
);
413 /* Get a pointer to the shell view */
414 hr
= IUnknown_QueryService(m_pSite
, SID_IFolderView
, IID_PPV_ARG(IShellView
, &lpSV
));
415 if (FAILED_UNEXPECTEDLY(hr
))
418 /* Attempt to get the pidl of the new item */
419 hr
= SHILCreateFromPathW(pszName
, &pidl
, NULL
);
420 if (FAILED_UNEXPECTEDLY(hr
))
423 pidlNewItem
= ILFindLastID(pidl
);
425 hr
= lpSV
->SelectItem(pidlNewItem
, dwSelectFlags
);
432 // Code is duplicated in CDefaultContextMenu
433 HRESULT
CNewMenu::CreateNewFolder(LPCMINVOKECOMMANDINFO lpici
)
435 WCHAR wszPath
[MAX_PATH
];
436 WCHAR wszName
[MAX_PATH
];
437 WCHAR wszNewFolder
[25];
440 /* Get folder path */
441 hr
= SHGetPathFromIDListW(m_pidlFolder
, wszPath
);
442 if (FAILED_UNEXPECTEDLY(hr
))
445 if (!LoadStringW(shell32_hInstance
, IDS_NEWFOLDER
, wszNewFolder
, _countof(wszNewFolder
)))
448 /* Create the name of the new directory */
449 if (!PathYetAnotherMakeUniqueName(wszName
, wszPath
, NULL
, wszNewFolder
))
452 /* Create the new directory and show the appropriate dialog in case of error */
453 if (SHCreateDirectory(lpici
->hwnd
, wszName
) != ERROR_SUCCESS
)
456 /* Show and select the new item in the def view */
457 SelectNewItem(SHCNE_MKDIR
, SHCNF_PATHW
, wszName
, TRUE
);
462 HRESULT
CNewMenu::NewItemByCommand(SHELLNEW_ITEM
*pItem
, LPCWSTR wszPath
)
464 WCHAR wszBuf
[MAX_PATH
];
466 WCHAR wszTemp
[MAX_PATH
];
468 PROCESS_INFORMATION pi
;
470 if (!ExpandEnvironmentStringsW((LPWSTR
)pItem
->pData
, wszBuf
, _countof(wszBuf
)))
472 TRACE("ExpandEnvironmentStrings failed\n");
476 /* Expand command parameter, FIXME: there can be more modifiers */
477 Ptr
= wcsstr(wszBuf
, L
"%1");
481 StringCbPrintfW(wszTemp
, sizeof(wszTemp
), wszBuf
, wszPath
);
490 ZeroMemory(&si
, sizeof(si
));
492 if (CreateProcessW(NULL
, pwszCmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
494 CloseHandle(pi
.hProcess
);
495 CloseHandle(pi
.hThread
);
500 ERR("Failed to create process\n");
505 HRESULT
CNewMenu::NewItemByNonCommand(SHELLNEW_ITEM
*pItem
, LPWSTR wszName
,
506 DWORD cchNameMax
, LPCWSTR wszPath
)
508 WCHAR wszBuf
[MAX_PATH
];
509 WCHAR wszNewFile
[MAX_PATH
];
510 BOOL bSuccess
= TRUE
;
512 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEW
, wszBuf
, _countof(wszBuf
)))
515 StringCchPrintfW(wszNewFile
, _countof(wszNewFile
), L
"%s %s%s", wszBuf
, pItem
->pwszDesc
, pItem
->pwszExt
);
517 /* Create the name of the new file */
518 if (!PathYetAnotherMakeUniqueName(wszName
, wszPath
, NULL
, wszNewFile
))
521 /* Create new file */
522 HANDLE hFile
= CreateFileW(wszName
, GENERIC_WRITE
, 0, NULL
, CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, NULL
);
523 if (hFile
!= INVALID_HANDLE_VALUE
)
525 if (pItem
->Type
== SHELLNEW_TYPE_DATA
)
527 /* Write a content */
529 WriteFile(hFile
, pItem
->pData
, pItem
->cbData
, &cbWritten
, NULL
);
540 if (pItem
->Type
== SHELLNEW_TYPE_FILENAME
)
543 if (!CopyFileW((LPWSTR
)pItem
->pData
, wszName
, FALSE
))
544 ERR("Copy file failed: %ls\n", (LPWSTR
)pItem
->pData
);
547 /* Show message if we failed */
550 TRACE("Notifying fs %s\n", debugstr_w(wszName
));
551 SelectNewItem(SHCNE_CREATE
, SHCNF_PATHW
, wszName
, pItem
!= m_pLinkItem
);
555 StringCbPrintfW(wszBuf
, sizeof(wszBuf
), L
"Cannot create file: %s", wszName
);
556 MessageBoxW(NULL
, wszBuf
, L
"Cannot create file", MB_OK
|MB_ICONERROR
); // FIXME load localized error msg
562 HRESULT
CNewMenu::CreateNewItem(SHELLNEW_ITEM
*pItem
, LPCMINVOKECOMMANDINFO lpcmi
)
565 WCHAR wszPath
[MAX_PATH
], wszName
[MAX_PATH
];
567 /* Get folder path */
568 hr
= SHGetPathFromIDListW(m_pidlFolder
, wszPath
);
569 if (FAILED_UNEXPECTEDLY(hr
))
572 if (pItem
== m_pLinkItem
)
574 NewItemByNonCommand(pItem
, wszName
, _countof(wszName
), wszPath
);
575 NewItemByCommand(pItem
, wszName
);
581 case SHELLNEW_TYPE_COMMAND
:
582 NewItemByCommand(pItem
, wszPath
);
585 case SHELLNEW_TYPE_DATA
:
586 case SHELLNEW_TYPE_FILENAME
:
587 case SHELLNEW_TYPE_NULLFILE
:
588 NewItemByNonCommand(pItem
, wszName
, _countof(wszName
), wszPath
);
591 case SHELLNEW_TYPE_INVALID
:
592 ERR("Invalid type\n");
599 HRESULT STDMETHODCALLTYPE
CNewMenu::SetSite(IUnknown
*pUnkSite
)
605 HRESULT STDMETHODCALLTYPE
CNewMenu::GetSite(REFIID riid
, void **ppvSite
)
607 return m_pSite
->QueryInterface(riid
, ppvSite
);
612 CNewMenu::QueryContextMenu(HMENU hMenu
,
622 TRACE("%p %p %u %u %u %u\n", this,
623 hMenu
, indexMenu
, idCmdFirst
, idCmdLast
, uFlags
);
625 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEW
, wszNew
, _countof(wszNew
)))
628 m_hSubMenu
= CreateMenu();
632 cItems
= InsertShellNewItems(m_hSubMenu
, idCmdFirst
, 0);
634 memset(&mii
, 0, sizeof(mii
));
635 mii
.cbSize
= sizeof(mii
);
636 mii
.fMask
= MIIM_TYPE
| MIIM_ID
| MIIM_STATE
| MIIM_SUBMENU
;
637 mii
.fType
= MFT_STRING
;
639 mii
.dwTypeData
= wszNew
;
640 mii
.cch
= wcslen(mii
.dwTypeData
);
641 mii
.fState
= MFS_ENABLED
;
642 mii
.hSubMenu
= m_hSubMenu
;
644 if (!InsertMenuItemW(hMenu
, indexMenu
, TRUE
, &mii
))
647 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, cItems
);
652 CNewMenu::InvokeCommand(LPCMINVOKECOMMANDINFO lpici
)
656 if (LOWORD(lpici
->lpVerb
) == 0)
657 hr
= CreateNewFolder(lpici
);
660 SHELLNEW_ITEM
*pItem
= FindItemFromIdOffset(LOWORD(lpici
->lpVerb
));
662 hr
= CreateNewItem(pItem
, lpici
);
665 TRACE("CNewMenu::InvokeCommand %x\n", hr
);
671 CNewMenu::GetCommandString(UINT_PTR idCmd
,
677 FIXME("%p %lu %u %p %p %u\n", this,
678 idCmd
, uType
, pwReserved
, pszName
, cchMax
);
685 CNewMenu::HandleMenuMsg(UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
692 CNewMenu::HandleMenuMsg2(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*plResult
)
698 MEASUREITEMSTRUCT
* lpmis
= reinterpret_cast<MEASUREITEMSTRUCT
*>(lParam
);
699 if (!lpmis
|| lpmis
->CtlType
!= ODT_MENU
)
702 if (lpmis
->itemWidth
< (UINT
)GetSystemMetrics(SM_CXMENUCHECK
))
703 lpmis
->itemWidth
= GetSystemMetrics(SM_CXMENUCHECK
);
704 if (lpmis
->itemHeight
< 16)
705 lpmis
->itemHeight
= 16;
713 DRAWITEMSTRUCT
* lpdis
= reinterpret_cast<DRAWITEMSTRUCT
*>(lParam
);
714 if (!lpdis
|| lpdis
->CtlType
!= ODT_MENU
)
717 DWORD id
= LOWORD(lpdis
->itemID
);
720 hIcon
= m_hiconFolder
;
725 SHELLNEW_ITEM
*pItem
= FindItemFromIdOffset(id
);
727 hIcon
= pItem
->hIcon
;
733 DrawIconEx(lpdis
->hDC
,
735 lpdis
->rcItem
.top
+ (lpdis
->rcItem
.bottom
- lpdis
->rcItem
.top
- 16) / 2,
750 CNewMenu::Initialize(LPCITEMIDLIST pidlFolder
,
751 IDataObject
*pdtobj
, HKEY hkeyProgID
)
753 m_pidlFolder
= ILClone(pidlFolder
);
755 /* Load folder and shortcut icons */
756 m_hiconFolder
= (HICON
)LoadImage(shell32_hInstance
, MAKEINTRESOURCE(IDI_SHELL_FOLDER
), IMAGE_ICON
, 16, 16, LR_SHARED
);
757 m_hiconLink
= (HICON
)LoadImage(shell32_hInstance
, MAKEINTRESOURCE(IDI_SHELL_SHORTCUT
), IMAGE_ICON
, 16, 16, LR_SHARED
);