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
= (PBYTE
)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
)
141 if (!SHGetFileInfoW(pwszExt
, FILE_ATTRIBUTE_NORMAL
, &fi
, sizeof(fi
), SHGFI_USEFILEATTRIBUTES
| SHGFI_TYPENAME
| SHGFI_ICON
| SHGFI_SMALLICON
))
147 /* Create new item */
148 SHELLNEW_ITEM
*pNewItem
= static_cast<SHELLNEW_ITEM
*>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SHELLNEW_ITEM
)));
155 TRACE("new item %ls\n", fi
.szTypeName
);
156 pNewItem
->Type
= Types
[i
].Type
;
157 pNewItem
->pData
= pData
;
158 pNewItem
->cbData
= pData
? cbData
: 0;
159 pNewItem
->pwszExt
= _wcsdup(pwszExt
);
160 pNewItem
->pwszDesc
= _wcsdup(fi
.szTypeName
);
162 pNewItem
->hIcon
= fi
.hIcon
;
168 CNewMenu::CacheItems()
175 WCHAR wszName
[MAX_PATH
];
176 SHELLNEW_ITEM
*pNewItem
;
177 SHELLNEW_ITEM
*pCurItem
= NULL
;
179 /* Enumerate all extensions */
180 while (RegEnumKeyW(HKEY_CLASSES_ROOT
, dwIndex
++, wszName
, _countof(wszName
)) == ERROR_SUCCESS
)
182 if (wszName
[0] != L
'.')
185 pNewItem
= LoadItem(wszName
);
188 dwSize
+= wcslen(wszName
) + 1;
189 if (!m_pLinkItem
&& wcsicmp(pNewItem
->pwszExt
, L
".lnk") == 0)
191 /* The unique link handler */
192 m_pLinkItem
= pNewItem
;
195 /* Add at the end of the list */
198 pCurItem
->pNext
= pNewItem
;
203 pCurItem
= m_pItems
= pNewItem
;
210 lpValues
= (LPWSTR
) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwSize
* sizeof(WCHAR
));
214 for (pCurItem
= m_pItems
, lpValue
= lpValues
; pCurItem
; pCurItem
= pCurItem
->pNext
)
216 memcpy(lpValue
, pCurItem
->pwszExt
, (wcslen(pCurItem
->pwszExt
) + 1) * sizeof(WCHAR
));
217 lpValue
+= wcslen(pCurItem
->pwszExt
) + 1;
220 if (RegCreateKeyEx(HKEY_CURRENT_USER
, ShellNewKey
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, NULL
) != ERROR_SUCCESS
)
222 HeapFree(GetProcessHeap(), 0, lpValues
);
226 if (RegSetValueExW(hKey
, L
"Classes", NULL
, REG_MULTI_SZ
, (LPBYTE
)lpValues
, dwSize
* sizeof(WCHAR
)) != ERROR_SUCCESS
)
228 HeapFree(GetProcessHeap(), 0, lpValues
);
233 HeapFree(GetProcessHeap(), 0, lpValues
);
240 CNewMenu::LoadCachedItems()
246 SHELLNEW_ITEM
*pNewItem
;
247 SHELLNEW_ITEM
*pCurItem
= NULL
;
249 if (RegOpenKeyExW(HKEY_CURRENT_USER
, ShellNewKey
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
252 if (RegQueryValueExW(hKey
, L
"Classes", NULL
, NULL
, NULL
, &dwSize
) != ERROR_SUCCESS
)
255 lpValues
= (LPWSTR
) HeapAlloc(GetProcessHeap(), 0, dwSize
);
259 if (RegQueryValueExW(hKey
, L
"Classes", NULL
, NULL
, (LPBYTE
)lpValues
, &dwSize
) != ERROR_SUCCESS
)
261 HeapFree(GetProcessHeap(), 0, lpValues
);
267 for (; *wszName
!= '\0'; wszName
+= wcslen(wszName
) + 1)
269 pNewItem
= LoadItem(wszName
);
272 if (!m_pLinkItem
&& wcsicmp(pNewItem
->pwszExt
, L
".lnk") == 0)
274 /* The unique link handler */
275 m_pLinkItem
= pNewItem
;
278 /* Add at the end of the list */
281 pCurItem
->pNext
= pNewItem
;
286 pCurItem
= m_pItems
= pNewItem
;
291 HeapFree(GetProcessHeap(), 0, lpValues
);
298 CNewMenu::LoadAllItems()
300 /* If there are any unload them */
303 if (!LoadCachedItems())
308 return (m_pItems
!= NULL
);
312 CNewMenu::InsertShellNewItems(HMENU hMenu
, UINT idCmdFirst
, UINT Pos
)
315 UINT idCmd
= idCmdFirst
;
318 if (m_pItems
== NULL
)
324 ZeroMemory(&mii
, sizeof(mii
));
325 mii
.cbSize
= sizeof(mii
);
327 /* Insert the new folder action */
328 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEWFOLDER
, wszBuf
, _countof(wszBuf
)))
330 mii
.fMask
= MIIM_ID
| MIIM_BITMAP
| MIIM_STRING
;
331 mii
.dwTypeData
= wszBuf
;
332 mii
.cch
= wcslen(mii
.dwTypeData
);
334 mii
.hbmpItem
= HBMMENU_CALLBACK
;
335 if (InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
))
338 /* Insert the new shortcut action */
341 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEWLINK
, wszBuf
, _countof(wszBuf
)))
343 mii
.dwTypeData
= wszBuf
;
344 mii
.cch
= wcslen(mii
.dwTypeData
);
346 if (InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
))
350 /* Insert a seperator for the custom new action */
351 mii
.fMask
= MIIM_TYPE
| MIIM_ID
;
352 mii
.fType
= MFT_SEPARATOR
;
354 InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
);
356 /* Insert the rest of the items */
357 mii
.fMask
= MIIM_ID
| MIIM_BITMAP
| MIIM_STRING
;
360 for (SHELLNEW_ITEM
*pCurItem
= m_pItems
; pCurItem
; pCurItem
= pCurItem
->pNext
)
362 /* Skip shortcut item */
363 if (pCurItem
== m_pLinkItem
)
366 TRACE("szDesc %s\n", debugstr_w(pCurItem
->pwszDesc
));
367 mii
.dwTypeData
= pCurItem
->pwszDesc
;
368 mii
.cch
= wcslen(mii
.dwTypeData
);
370 if (InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
))
374 return idCmd
- idCmdFirst
;
377 CNewMenu::SHELLNEW_ITEM
*CNewMenu::FindItemFromIdOffset(UINT IdOffset
)
380 return NULL
; /* Folder */
383 return m_pLinkItem
; /* shortcut */
385 /* Find shell new item */
386 SHELLNEW_ITEM
*pItem
= m_pItems
;
387 for (UINT i
= 2; pItem
; ++i
)
392 pItem
= pItem
->pNext
;
398 HRESULT
CNewMenu::SelectNewItem(LONG wEventId
, UINT uFlags
, LPWSTR pszName
, BOOL bRename
)
400 CComPtr
<IShellBrowser
> lpSB
;
401 CComPtr
<IShellView
> lpSV
;
404 PITEMID_CHILD pidlNewItem
;
407 dwSelectFlags
= SVSI_DESELECTOTHERS
| SVSI_ENSUREVISIBLE
| SVSI_FOCUSED
| SVSI_SELECT
;
409 dwSelectFlags
|= SVSI_EDIT
;
411 /* Notify the view object about the new item */
412 SHChangeNotify(wEventId
, uFlags
, (LPCVOID
) pszName
, NULL
);
417 /* Get a pointer to the shell view */
418 hr
= IUnknown_QueryService(m_pSite
, SID_IFolderView
, IID_PPV_ARG(IShellView
, &lpSV
));
419 if (FAILED_UNEXPECTEDLY(hr
))
422 /* Attempt to get the pidl of the new item */
423 hr
= SHILCreateFromPathW(pszName
, &pidl
, NULL
);
424 if (FAILED_UNEXPECTEDLY(hr
))
427 pidlNewItem
= ILFindLastID(pidl
);
429 hr
= lpSV
->SelectItem(pidlNewItem
, dwSelectFlags
);
436 // Code is duplicated in CDefaultContextMenu
437 HRESULT
CNewMenu::CreateNewFolder(LPCMINVOKECOMMANDINFO lpici
)
439 WCHAR wszPath
[MAX_PATH
];
440 WCHAR wszName
[MAX_PATH
];
441 WCHAR wszNewFolder
[25];
444 /* Get folder path */
445 hr
= SHGetPathFromIDListW(m_pidlFolder
, wszPath
);
446 if (FAILED_UNEXPECTEDLY(hr
))
449 if (!LoadStringW(shell32_hInstance
, IDS_NEWFOLDER
, wszNewFolder
, _countof(wszNewFolder
)))
452 /* Create the name of the new directory */
453 if (!PathYetAnotherMakeUniqueName(wszName
, wszPath
, NULL
, wszNewFolder
))
456 /* Create the new directory and show the appropriate dialog in case of error */
457 if (SHCreateDirectory(lpici
->hwnd
, wszName
) != ERROR_SUCCESS
)
460 /* Show and select the new item in the def view */
461 SelectNewItem(SHCNE_MKDIR
, SHCNF_PATHW
, wszName
, TRUE
);
466 HRESULT
CNewMenu::NewItemByCommand(SHELLNEW_ITEM
*pItem
, LPCWSTR wszPath
)
468 WCHAR wszBuf
[MAX_PATH
];
470 WCHAR wszTemp
[MAX_PATH
];
472 PROCESS_INFORMATION pi
;
474 if (!ExpandEnvironmentStringsW((LPWSTR
)pItem
->pData
, wszBuf
, _countof(wszBuf
)))
476 TRACE("ExpandEnvironmentStrings failed\n");
480 /* Expand command parameter, FIXME: there can be more modifiers */
481 Ptr
= wcsstr(wszBuf
, L
"%1");
485 StringCbPrintfW(wszTemp
, sizeof(wszTemp
), wszBuf
, wszPath
);
494 ZeroMemory(&si
, sizeof(si
));
496 if (CreateProcessW(NULL
, pwszCmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
498 CloseHandle(pi
.hProcess
);
499 CloseHandle(pi
.hThread
);
504 ERR("Failed to create process\n");
509 HRESULT
CNewMenu::NewItemByNonCommand(SHELLNEW_ITEM
*pItem
, LPWSTR wszName
,
510 DWORD cchNameMax
, LPCWSTR wszPath
)
512 WCHAR wszBuf
[MAX_PATH
];
513 WCHAR wszNewFile
[MAX_PATH
];
514 BOOL bSuccess
= TRUE
;
516 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEW
, wszBuf
, _countof(wszBuf
)))
519 StringCchPrintfW(wszNewFile
, _countof(wszNewFile
), L
"%s %s%s", wszBuf
, pItem
->pwszDesc
, pItem
->pwszExt
);
521 /* Create the name of the new file */
522 if (!PathYetAnotherMakeUniqueName(wszName
, wszPath
, NULL
, wszNewFile
))
525 /* Create new file */
526 HANDLE hFile
= CreateFileW(wszName
, GENERIC_WRITE
, 0, NULL
, CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, NULL
);
527 if (hFile
!= INVALID_HANDLE_VALUE
)
529 if (pItem
->Type
== SHELLNEW_TYPE_DATA
)
531 /* Write a content */
533 WriteFile(hFile
, pItem
->pData
, pItem
->cbData
, &cbWritten
, NULL
);
544 if (pItem
->Type
== SHELLNEW_TYPE_FILENAME
)
547 if (!CopyFileW((LPWSTR
)pItem
->pData
, wszName
, FALSE
))
548 ERR("Copy file failed: %ls\n", (LPWSTR
)pItem
->pData
);
551 /* Show message if we failed */
554 TRACE("Notifying fs %s\n", debugstr_w(wszName
));
555 SelectNewItem(SHCNE_CREATE
, SHCNF_PATHW
, wszName
, pItem
!= m_pLinkItem
);
559 StringCbPrintfW(wszBuf
, sizeof(wszBuf
), L
"Cannot create file: %s", wszName
);
560 MessageBoxW(NULL
, wszBuf
, L
"Cannot create file", MB_OK
| MB_ICONERROR
); // FIXME load localized error msg
566 HRESULT
CNewMenu::CreateNewItem(SHELLNEW_ITEM
*pItem
, LPCMINVOKECOMMANDINFO lpcmi
)
569 WCHAR wszPath
[MAX_PATH
], wszName
[MAX_PATH
];
571 /* Get folder path */
572 hr
= SHGetPathFromIDListW(m_pidlFolder
, wszPath
);
573 if (FAILED_UNEXPECTEDLY(hr
))
576 if (pItem
== m_pLinkItem
)
578 NewItemByNonCommand(pItem
, wszName
, _countof(wszName
), wszPath
);
579 NewItemByCommand(pItem
, wszName
);
585 case SHELLNEW_TYPE_COMMAND
:
586 NewItemByCommand(pItem
, wszPath
);
589 case SHELLNEW_TYPE_DATA
:
590 case SHELLNEW_TYPE_FILENAME
:
591 case SHELLNEW_TYPE_NULLFILE
:
592 NewItemByNonCommand(pItem
, wszName
, _countof(wszName
), wszPath
);
595 case SHELLNEW_TYPE_INVALID
:
596 ERR("Invalid type\n");
603 HRESULT STDMETHODCALLTYPE
CNewMenu::SetSite(IUnknown
*pUnkSite
)
609 HRESULT STDMETHODCALLTYPE
CNewMenu::GetSite(REFIID riid
, void **ppvSite
)
611 return m_pSite
->QueryInterface(riid
, ppvSite
);
616 CNewMenu::QueryContextMenu(HMENU hMenu
,
626 TRACE("%p %p %u %u %u %u\n", this,
627 hMenu
, indexMenu
, idCmdFirst
, idCmdLast
, uFlags
);
629 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEW
, wszNew
, _countof(wszNew
)))
632 m_hSubMenu
= CreateMenu();
636 cItems
= InsertShellNewItems(m_hSubMenu
, idCmdFirst
, 0);
638 ZeroMemory(&mii
, sizeof(mii
));
639 mii
.cbSize
= sizeof(mii
);
640 mii
.fMask
= MIIM_TYPE
| MIIM_ID
| MIIM_STATE
| MIIM_SUBMENU
;
641 mii
.fType
= MFT_STRING
;
643 mii
.dwTypeData
= wszNew
;
644 mii
.cch
= wcslen(mii
.dwTypeData
);
645 mii
.fState
= MFS_ENABLED
;
646 mii
.hSubMenu
= m_hSubMenu
;
648 if (!InsertMenuItemW(hMenu
, indexMenu
, TRUE
, &mii
))
651 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, cItems
);
656 CNewMenu::InvokeCommand(LPCMINVOKECOMMANDINFO lpici
)
660 if (LOWORD(lpici
->lpVerb
) == 0)
662 hr
= CreateNewFolder(lpici
);
666 SHELLNEW_ITEM
*pItem
= FindItemFromIdOffset(LOWORD(lpici
->lpVerb
));
668 hr
= CreateNewItem(pItem
, lpici
);
671 TRACE("CNewMenu::InvokeCommand %x\n", hr
);
677 CNewMenu::GetCommandString(UINT_PTR idCmd
,
683 FIXME("%p %lu %u %p %p %u\n", this,
684 idCmd
, uType
, pwReserved
, pszName
, cchMax
);
691 CNewMenu::HandleMenuMsg(UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
698 CNewMenu::HandleMenuMsg2(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*plResult
)
704 MEASUREITEMSTRUCT
* lpmis
= reinterpret_cast<MEASUREITEMSTRUCT
*>(lParam
);
705 if (!lpmis
|| lpmis
->CtlType
!= ODT_MENU
)
708 if (lpmis
->itemWidth
< (UINT
)GetSystemMetrics(SM_CXMENUCHECK
))
709 lpmis
->itemWidth
= GetSystemMetrics(SM_CXMENUCHECK
);
710 if (lpmis
->itemHeight
< 16)
711 lpmis
->itemHeight
= 16;
719 DRAWITEMSTRUCT
* lpdis
= reinterpret_cast<DRAWITEMSTRUCT
*>(lParam
);
720 if (!lpdis
|| lpdis
->CtlType
!= ODT_MENU
)
723 DWORD id
= LOWORD(lpdis
->itemID
);
727 hIcon
= m_hIconFolder
;
735 SHELLNEW_ITEM
*pItem
= FindItemFromIdOffset(id
);
737 hIcon
= pItem
->hIcon
;
743 DrawIconEx(lpdis
->hDC
,
745 lpdis
->rcItem
.top
+ (lpdis
->rcItem
.bottom
- lpdis
->rcItem
.top
- 16) / 2,
760 CNewMenu::Initialize(LPCITEMIDLIST pidlFolder
,
761 IDataObject
*pdtobj
, HKEY hkeyProgID
)
763 m_pidlFolder
= ILClone(pidlFolder
);
765 /* Load folder and shortcut icons */
766 m_hIconFolder
= (HICON
)LoadImage(shell32_hInstance
, MAKEINTRESOURCE(IDI_SHELL_FOLDER
), IMAGE_ICON
, 16, 16, LR_SHARED
);
767 m_hIconLink
= (HICON
)LoadImage(shell32_hInstance
, MAKEINTRESOURCE(IDI_SHELL_SHORTCUT
), IMAGE_ICON
, 16, 16, LR_SHARED
);