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
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
27 CNewMenu::CNewMenu() :
46 void CNewMenu::UnloadItem(SHELLNEW_ITEM
*pItem
)
48 /* Note: free allows NULL as argument */
50 free(pItem
->pwszDesc
);
54 DestroyIcon(pItem
->hIcon
);
56 HeapFree(GetProcessHeap(), 0, pItem
);
59 void CNewMenu::UnloadAllItems()
61 SHELLNEW_ITEM
*pCurItem
;
63 /* Unload normal items */
67 m_pItems
= m_pItems
->pNext
;
72 /* Unload link item */
74 UnloadItem(m_pLinkItem
);
78 CNewMenu::SHELLNEW_ITEM
*CNewMenu::LoadItem(LPCWSTR pwszExt
)
81 WCHAR wszBuf
[MAX_PATH
];
85 StringCbPrintfW(wszBuf
, sizeof(wszBuf
), L
"%s\\ShellNew", pwszExt
);
87 TRACE("LoadItem Keyname %s Name %s\n", debugstr_w(pwszExt
), debugstr_w(wszBuf
));
89 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszBuf
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
91 TRACE("Failed to open key\n");
95 /* Find first valid value */
103 {L
"FileName", SHELLNEW_TYPE_FILENAME
, TRUE
, TRUE
},
104 {L
"Command", SHELLNEW_TYPE_COMMAND
, TRUE
, TRUE
},
105 {L
"Data", SHELLNEW_TYPE_DATA
, TRUE
, FALSE
},
106 {L
"NullFile", SHELLNEW_TYPE_NULLFILE
, FALSE
},
111 for (i
= 0; Types
[i
].pszName
; ++i
)
113 /* Note: We are using ANSI function because strings can be treated as data */
115 DWORD dwFlags
= Types
[i
].bStr
? RRF_RT_REG_SZ
: RRF_RT_ANY
;
117 if (RegGetValueW(hKey
, NULL
, Types
[i
].pszName
, dwFlags
, NULL
, NULL
, &cbData
) == ERROR_SUCCESS
)
119 if (Types
[i
].bNeedData
&& cbData
> 0)
121 pData
= (BYTE
*)malloc(cbData
);
122 RegGetValueW(hKey
, NULL
, Types
[i
].pszName
, dwFlags
, &dwType
, pData
, &cbData
);
123 if (!Types
[i
].bStr
&& (dwType
== REG_SZ
|| dwType
== REG_EXPAND_SZ
))
125 PBYTE pData2
= (PBYTE
)malloc(cbData
);
126 cbData
= WideCharToMultiByte(CP_ACP
, 0, (LPWSTR
)pData
, -1, (LPSTR
)pData2
, cbData
, NULL
, NULL
);
136 /* Was any key found? */
137 if (!Types
[i
].pszName
)
141 if (!SHGetFileInfoW(pwszExt
, FILE_ATTRIBUTE_NORMAL
, &fi
, sizeof(fi
), SHGFI_USEFILEATTRIBUTES
|SHGFI_TYPENAME
|SHGFI_ICON
|SHGFI_SMALLICON
))
144 /* Create new item */
145 SHELLNEW_ITEM
*pNewItem
= static_cast<SHELLNEW_ITEM
*>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SHELLNEW_ITEM
)));
152 TRACE("new item %ls\n", fi
.szTypeName
);
153 pNewItem
->Type
= Types
[i
].Type
;
154 pNewItem
->pData
= pData
;
155 pNewItem
->cbData
= pData
? cbData
: 0;
156 pNewItem
->pwszExt
= _wcsdup(pwszExt
);
157 pNewItem
->pwszDesc
= _wcsdup(fi
.szTypeName
);
159 pNewItem
->hIcon
= fi
.hIcon
;
165 CNewMenu::CacheItems()
172 WCHAR wszName
[MAX_PATH
];
173 SHELLNEW_ITEM
*pNewItem
;
174 SHELLNEW_ITEM
*pCurItem
= NULL
;
176 /* Enumerate all extensions */
177 while (RegEnumKeyW(HKEY_CLASSES_ROOT
, dwIndex
++, wszName
, _countof(wszName
)) == ERROR_SUCCESS
)
179 if (wszName
[0] != L
'.')
182 pNewItem
= LoadItem(wszName
);
185 dwSize
+= wcslen(wszName
) + 1;
186 if (wcsicmp(pNewItem
->pwszExt
, L
".lnk") == 0)
189 m_pLinkItem
= pNewItem
;
193 /* Add at the end of list */
196 pCurItem
->pNext
= pNewItem
;
200 pCurItem
= m_pItems
= pNewItem
;
207 lpValues
= (LPWSTR
) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwSize
* sizeof(WCHAR
));
215 memcpy(lpValue
, pCurItem
->pwszExt
, (wcslen(pCurItem
->pwszExt
) + 1) * sizeof(WCHAR
));
216 lpValue
+= wcslen(pCurItem
->pwszExt
) + 1;
217 pCurItem
= pCurItem
->pNext
;
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 (; '\0' != *wszName
; wszName
+= wcslen(wszName
) + 1)
269 pNewItem
= LoadItem(wszName
);
272 if (wcsicmp(pNewItem
->pwszExt
, L
".lnk") == 0)
275 m_pLinkItem
= pNewItem
;
279 /* Add at the end of list */
282 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())
310 m_pLinkItem
= static_cast<SHELLNEW_ITEM
*>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SHELLNEW_ITEM
)));
313 m_pLinkItem
->Type
= SHELLNEW_TYPE_NULLFILE
;
314 m_pLinkItem
->pwszDesc
= _wcsdup(L
"Link");
315 m_pLinkItem
->pwszExt
= _wcsdup(L
".lnk");
319 if (m_pItems
== NULL
)
326 CNewMenu::InsertShellNewItems(HMENU hMenu
, UINT idCmdFirst
, UINT Pos
)
330 UINT idCmd
= idCmdFirst
;
332 if (m_pItems
== NULL
)
338 ZeroMemory(&mii
, sizeof(mii
));
339 mii
.cbSize
= sizeof(mii
);
341 /* Insert new folder action */
342 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEWFOLDER
, wszBuf
, _countof(wszBuf
)))
344 mii
.fMask
= MIIM_ID
| MIIM_BITMAP
| MIIM_STRING
;
345 mii
.dwTypeData
= wszBuf
;
346 mii
.cch
= wcslen(mii
.dwTypeData
);
348 mii
.hbmpItem
= HBMMENU_CALLBACK
;
349 if (InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
))
352 /* Insert new shortcut action */
353 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEWLINK
, wszBuf
, _countof(wszBuf
)))
355 mii
.dwTypeData
= wszBuf
;
356 mii
.cch
= wcslen(mii
.dwTypeData
);
358 if (InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
))
361 /* Insert seperator for custom new action */
362 mii
.fMask
= MIIM_TYPE
| MIIM_ID
;
363 mii
.fType
= MFT_SEPARATOR
;
365 InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
);
367 /* Insert rest of items */
368 mii
.fMask
= MIIM_ID
| MIIM_BITMAP
| MIIM_STRING
;
371 SHELLNEW_ITEM
*pCurItem
= m_pItems
;
374 TRACE("szDesc %s\n", debugstr_w(pCurItem
->pwszDesc
));
375 mii
.dwTypeData
= pCurItem
->pwszDesc
;
376 mii
.cch
= wcslen(mii
.dwTypeData
);
378 if (InsertMenuItemW(hMenu
, Pos
++, TRUE
, &mii
))
380 pCurItem
= pCurItem
->pNext
;
383 return idCmd
- idCmdFirst
;
386 CNewMenu::SHELLNEW_ITEM
*CNewMenu::FindItemFromIdOffset(UINT IdOffset
)
389 return NULL
; /* Folder */
392 return m_pLinkItem
; /* shortcut */
394 /* Find shell new item */
395 SHELLNEW_ITEM
*pItem
= m_pItems
;
396 for (UINT i
= 2; pItem
; ++i
)
401 pItem
= pItem
->pNext
;
407 HRESULT
CNewMenu::SelectNewItem(LPCMINVOKECOMMANDINFO lpici
, LONG wEventId
, UINT uFlags
, LPWSTR pszName
)
409 CComPtr
<IShellBrowser
> lpSB
;
410 CComPtr
<IShellView
> lpSV
;
413 PITEMID_CHILD pidlNewItem
;
415 /* Notify the view object about the new item */
416 SHChangeNotify(wEventId
, uFlags
, (LPCVOID
) pszName
, NULL
);
418 /* FIXME: I think that this can be implemented using callbacks to the shell folder */
420 /* Note: CWM_GETISHELLBROWSER returns shell browser without adding reference */
421 lpSB
= (LPSHELLBROWSER
)SendMessageA(lpici
->hwnd
, CWM_GETISHELLBROWSER
, 0, 0);
425 hr
= lpSB
->QueryActiveShellView(&lpSV
);
429 /* Attempt to get the pidl of the new item */
430 hr
= SHILCreateFromPathW(pszName
, &pidl
, NULL
);
431 if (FAILED_UNEXPECTEDLY(hr
))
434 pidlNewItem
= ILFindLastID(pidl
);
436 hr
= lpSV
->SelectItem(pidlNewItem
, SVSI_DESELECTOTHERS
| SVSI_EDIT
| SVSI_ENSUREVISIBLE
|
437 SVSI_FOCUSED
| SVSI_SELECT
);
444 // Code is duplicated in CDefaultContextMenu
445 HRESULT
CNewMenu::CreateNewFolder(LPCMINVOKECOMMANDINFO lpici
)
447 WCHAR wszPath
[MAX_PATH
];
448 WCHAR wszName
[MAX_PATH
];
449 WCHAR wszNewFolder
[25];
452 /* Get folder path */
453 hr
= SHGetPathFromIDListW(m_pidlFolder
, wszPath
);
454 if (FAILED_UNEXPECTEDLY(hr
))
457 if (!LoadStringW(shell32_hInstance
, IDS_NEWFOLDER
, wszNewFolder
, _countof(wszNewFolder
)))
460 /* Create the name of the new directory */
461 if (!PathYetAnotherMakeUniqueName(wszName
, wszPath
, NULL
, wszNewFolder
))
464 /* Create the new directory and show the appropriate dialog in case of error */
465 if (SHCreateDirectory(lpici
->hwnd
, wszName
) != ERROR_SUCCESS
)
468 /* Show and select the new item in the def view */
469 SelectNewItem(lpici
, SHCNE_MKDIR
, SHCNF_PATHW
, wszName
);
474 HRESULT
CNewMenu::CreateNewItem(SHELLNEW_ITEM
*pItem
, LPCMINVOKECOMMANDINFO lpcmi
)
476 WCHAR wszBuf
[MAX_PATH
];
477 WCHAR wszPath
[MAX_PATH
];
480 /* Get folder path */
481 hr
= SHGetPathFromIDListW(m_pidlFolder
, wszPath
);
482 if (FAILED_UNEXPECTEDLY(hr
))
487 case SHELLNEW_TYPE_COMMAND
:
490 WCHAR wszTemp
[MAX_PATH
];
492 PROCESS_INFORMATION pi
;
494 if (!ExpandEnvironmentStringsW((LPWSTR
)pItem
->pData
, wszBuf
, _countof(wszBuf
)))
496 TRACE("ExpandEnvironmentStrings failed\n");
500 /* Expand command parameter, FIXME: there can be more modifiers */
501 Ptr
= wcsstr(wszBuf
, L
"%1");
505 StringCbPrintfW(wszTemp
, sizeof(wszTemp
), wszBuf
, wszPath
);
514 ZeroMemory(&si
, sizeof(si
));
516 if (CreateProcessW(NULL
, pwszCmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
518 CloseHandle(pi
.hProcess
);
519 CloseHandle(pi
.hThread
);
523 ERR("Failed to create process\n");
528 case SHELLNEW_TYPE_DATA
:
529 case SHELLNEW_TYPE_FILENAME
:
530 case SHELLNEW_TYPE_NULLFILE
:
532 BOOL bSuccess
= TRUE
;
533 WCHAR wszName
[MAX_PATH
];
534 WCHAR wszNewFile
[MAX_PATH
];
536 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEW
, wszBuf
, _countof(wszBuf
)))
539 StringCchPrintfW(wszNewFile
, _countof(wszNewFile
), L
"%s %s%s", wszBuf
, pItem
->pwszDesc
, pItem
->pwszExt
);
541 /* Create the name of the new file */
542 if (!PathYetAnotherMakeUniqueName(wszName
, wszPath
, NULL
, wszNewFile
))
545 /* Create new file */
546 HANDLE hFile
= CreateFileW(wszName
, GENERIC_WRITE
, 0, NULL
, CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, NULL
);
547 if (hFile
!= INVALID_HANDLE_VALUE
)
549 if (pItem
->Type
== SHELLNEW_TYPE_DATA
)
551 /* Write a content */
553 WriteFile(hFile
, pItem
->pData
, pItem
->cbData
, &cbWritten
, NULL
);
564 if (pItem
->Type
== SHELLNEW_TYPE_FILENAME
)
567 if (!CopyFileW((LPWSTR
)pItem
->pData
, wszName
, FALSE
))
568 ERR("Copy file failed: %ls\n", (LPWSTR
)pItem
->pData
);
571 /* Show message if we failed */
574 TRACE("Notifying fs %s\n", debugstr_w(wszName
));
575 SelectNewItem(lpcmi
, SHCNE_CREATE
, SHCNF_PATHW
, wszName
);
579 StringCbPrintfW(wszBuf
, sizeof(wszBuf
), L
"Cannot create file: %s", wszName
);
580 MessageBoxW(NULL
, wszBuf
, L
"Cannot create file", MB_OK
|MB_ICONERROR
); // FIXME load localized error msg
585 case SHELLNEW_TYPE_INVALID
:
586 ERR("Invalid type\n");
593 HRESULT STDMETHODCALLTYPE
CNewMenu::SetSite(IUnknown
*pUnkSite
)
599 HRESULT STDMETHODCALLTYPE
CNewMenu::GetSite(REFIID riid
, void **ppvSite
)
601 return m_pSite
->QueryInterface(riid
, ppvSite
);
606 CNewMenu::QueryContextMenu(HMENU hMenu
,
616 TRACE("%p %p %u %u %u %u\n", this,
617 hMenu
, indexMenu
, idCmdFirst
, idCmdLast
, uFlags
);
619 if (!LoadStringW(shell32_hInstance
, FCIDM_SHVIEW_NEW
, wszNew
, _countof(wszNew
)))
622 m_hSubMenu
= CreateMenu();
626 cItems
= InsertShellNewItems(m_hSubMenu
, idCmdFirst
, 0);
628 memset(&mii
, 0, sizeof(mii
));
629 mii
.cbSize
= sizeof(mii
);
630 mii
.fMask
= MIIM_TYPE
| MIIM_ID
| MIIM_STATE
| MIIM_SUBMENU
;
631 mii
.fType
= MFT_STRING
;
633 mii
.dwTypeData
= wszNew
;
634 mii
.cch
= wcslen(mii
.dwTypeData
);
635 mii
.fState
= MFS_ENABLED
;
636 mii
.hSubMenu
= m_hSubMenu
;
638 if (!InsertMenuItemW(hMenu
, indexMenu
, TRUE
, &mii
))
641 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, cItems
);
646 CNewMenu::InvokeCommand(LPCMINVOKECOMMANDINFO lpici
)
650 if (LOWORD(lpici
->lpVerb
) == 0)
651 hr
= CreateNewFolder(lpici
);
654 SHELLNEW_ITEM
*pItem
= FindItemFromIdOffset(LOWORD(lpici
->lpVerb
));
656 hr
= CreateNewItem(pItem
, lpici
);
659 TRACE("CNewMenu::InvokeCommand %x\n", hr
);
665 CNewMenu::GetCommandString(UINT_PTR idCmd
,
671 FIXME("%p %lu %u %p %p %u\n", this,
672 idCmd
, uType
, pwReserved
, pszName
, cchMax
);
679 CNewMenu::HandleMenuMsg(UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
686 CNewMenu::HandleMenuMsg2(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*plResult
)
692 MEASUREITEMSTRUCT
* lpmis
= reinterpret_cast<MEASUREITEMSTRUCT
*>(lParam
);
693 if (!lpmis
|| lpmis
->CtlType
!= ODT_MENU
)
696 if (lpmis
->itemWidth
< (UINT
)GetSystemMetrics(SM_CXMENUCHECK
))
697 lpmis
->itemWidth
= GetSystemMetrics(SM_CXMENUCHECK
);
698 if (lpmis
->itemHeight
< 16)
699 lpmis
->itemHeight
= 16;
707 DRAWITEMSTRUCT
* lpdis
= reinterpret_cast<DRAWITEMSTRUCT
*>(lParam
);
708 if (!lpdis
|| lpdis
->CtlType
!= ODT_MENU
)
711 DWORD id
= LOWORD(lpdis
->itemID
);
714 hIcon
= m_hiconFolder
;
719 SHELLNEW_ITEM
*pItem
= FindItemFromIdOffset(id
);
721 hIcon
= pItem
->hIcon
;
727 DrawIconEx(lpdis
->hDC
,
729 lpdis
->rcItem
.top
+ (lpdis
->rcItem
.bottom
- lpdis
->rcItem
.top
- 16) / 2,
744 CNewMenu::Initialize(LPCITEMIDLIST pidlFolder
,
745 IDataObject
*pdtobj
, HKEY hkeyProgID
)
747 m_pidlFolder
= ILClone(pidlFolder
);
749 /* Load folder and shortcut icons */
750 m_hiconFolder
= (HICON
)LoadImage(shell32_hInstance
, MAKEINTRESOURCE(IDI_SHELL_FOLDER
), IMAGE_ICON
, 16, 16, LR_SHARED
);
751 m_hiconLink
= (HICON
)LoadImage(shell32_hInstance
, MAKEINTRESOURCE(IDI_SHELL_SHORTCUT
), IMAGE_ICON
, 16, 16, LR_SHARED
);