[SHELL32] Addendum to r71980, RegSetValueEx expects a size in bytes, not chars. CORE...
[reactos.git] / reactos / dll / win32 / shell32 / CNewMenu.cpp
1 /*
2 * provides new shell item service
3 *
4 * Copyright 2007 Johannes Anderwald (johannes.anderwald@reactos.org)
5 * Copyright 2009 Andrew Hill
6 * Copyright 2012 Rafal Harabien
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "precomp.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(shell);
26
27 CNewMenu::CNewMenu() :
28 m_pidlFolder(NULL),
29 m_wszPath(NULL),
30 m_pItems(NULL),
31 m_pLinkItem(NULL),
32 m_pSite(NULL),
33 m_hiconFolder(NULL),
34 m_hiconLink(NULL),
35 m_idCmdFirst(0)
36 {
37 }
38
39 CNewMenu::~CNewMenu()
40 {
41 UnloadAllItems();
42
43 if (m_pidlFolder)
44 ILFree(m_pidlFolder);
45 }
46
47 void CNewMenu::UnloadItem(SHELLNEW_ITEM *pItem)
48 {
49 /* Note: free allows NULL as argument */
50 free(pItem->pData);
51 free(pItem->pwszDesc);
52 free(pItem->pwszExt);
53
54 if (pItem->hIcon)
55 DestroyIcon(pItem->hIcon);
56
57 HeapFree(GetProcessHeap(), 0, pItem);
58 }
59
60 void CNewMenu::UnloadAllItems()
61 {
62 SHELLNEW_ITEM *pCurItem;
63
64 /* Unload normal items */
65 while (m_pItems)
66 {
67 pCurItem = m_pItems;
68 m_pItems = m_pItems->pNext;
69
70 UnloadItem(pCurItem);
71 }
72
73 /* Unload link item */
74 if (m_pLinkItem)
75 UnloadItem(m_pLinkItem);
76 m_pLinkItem = NULL;
77 }
78
79 CNewMenu::SHELLNEW_ITEM *CNewMenu::LoadItem(LPCWSTR pwszExt)
80 {
81 HKEY hKey;
82 WCHAR wszBuf[MAX_PATH];
83 BYTE *pData = NULL;
84 DWORD cbData;
85
86 StringCbPrintfW(wszBuf, sizeof(wszBuf), L"%s\\ShellNew", pwszExt);
87
88 TRACE("LoadItem Keyname %s Name %s\n", debugstr_w(pwszExt), debugstr_w(wszBuf));
89
90 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszBuf, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
91 {
92 TRACE("Failed to open key\n");
93 return NULL;
94 }
95
96 /* Find first valid value */
97 struct
98 {
99 LPCWSTR pszName;
100 SHELLNEW_TYPE Type;
101 BOOL bNeedData;
102 BOOL bStr;
103 } Types[] = {
104 {L"FileName", SHELLNEW_TYPE_FILENAME, TRUE, TRUE},
105 {L"Command", SHELLNEW_TYPE_COMMAND, TRUE, TRUE},
106 {L"Data", SHELLNEW_TYPE_DATA, TRUE, FALSE},
107 {L"NullFile", SHELLNEW_TYPE_NULLFILE, FALSE},
108 {NULL}
109 };
110 UINT i;
111
112 for (i = 0; Types[i].pszName; ++i)
113 {
114 /* Note: We are using ANSI function because strings can be treated as data */
115 cbData = 0;
116 DWORD dwFlags = Types[i].bStr ? RRF_RT_REG_SZ : RRF_RT_ANY;
117 DWORD dwType;
118 if (RegGetValueW(hKey, NULL, Types[i].pszName, dwFlags, NULL, NULL, &cbData) == ERROR_SUCCESS)
119 {
120 if (Types[i].bNeedData && cbData > 0)
121 {
122 pData = (BYTE*)malloc(cbData);
123 RegGetValueW(hKey, NULL, Types[i].pszName, dwFlags, &dwType, pData, &cbData);
124 if (!Types[i].bStr && (dwType == REG_SZ || dwType == REG_EXPAND_SZ))
125 {
126 PBYTE pData2 = (PBYTE)malloc(cbData);
127 cbData = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)pData, -1, (LPSTR)pData2, cbData, NULL, NULL);
128 free(pData);
129 pData = pData2;
130 }
131 }
132 break;
133 }
134 }
135 RegCloseKey(hKey);
136
137 /* Was any key found? */
138 if (!Types[i].pszName)
139 return NULL;
140
141 SHFILEINFO fi;
142 if (!SHGetFileInfoW(pwszExt, FILE_ATTRIBUTE_NORMAL, &fi, sizeof(fi), SHGFI_USEFILEATTRIBUTES|SHGFI_TYPENAME|SHGFI_ICON|SHGFI_SMALLICON))
143 return NULL;
144
145 /* Create new item */
146 SHELLNEW_ITEM *pNewItem = static_cast<SHELLNEW_ITEM *>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SHELLNEW_ITEM)));
147 if (!pNewItem)
148 {
149 free(pData);
150 return NULL;
151 }
152
153 TRACE("new item %ls\n", fi.szTypeName);
154 pNewItem->Type = Types[i].Type;
155 pNewItem->pData = pData;
156 pNewItem->cbData = pData ? cbData : 0;
157 pNewItem->pwszExt = _wcsdup(pwszExt);
158 pNewItem->pwszDesc = _wcsdup(fi.szTypeName);
159 if (fi.hIcon)
160 pNewItem->hIcon = fi.hIcon;
161
162 return pNewItem;
163 }
164
165 BOOL
166 CNewMenu::CacheItems()
167 {
168 HKEY hKey;
169 DWORD dwSize = 0;
170 DWORD dwIndex = 0;
171 LPWSTR lpValue;
172 LPWSTR lpValues;
173 WCHAR wszName[MAX_PATH];
174 SHELLNEW_ITEM *pNewItem;
175 SHELLNEW_ITEM *pCurItem = NULL;
176
177 /* Enumerate all extensions */
178 while (RegEnumKeyW(HKEY_CLASSES_ROOT, dwIndex++, wszName, _countof(wszName)) == ERROR_SUCCESS)
179 {
180 if (wszName[0] != L'.')
181 continue;
182
183 pNewItem = LoadItem(wszName);
184 if (pNewItem)
185 {
186 dwSize += wcslen(wszName) + 1;
187 if (wcsicmp(pNewItem->pwszExt, L".lnk") == 0)
188 {
189 /* Link handler */
190 m_pLinkItem = pNewItem;
191 }
192 else
193 {
194 /* Add at the end of list */
195 if (pCurItem)
196 {
197 pCurItem->pNext = pNewItem;
198 pCurItem = pNewItem;
199 }
200 else
201 pCurItem = m_pItems = pNewItem;
202 }
203 }
204 }
205
206 dwSize++;
207
208 lpValues = (LPWSTR) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize * sizeof(WCHAR));
209 if (!lpValues)
210 return FALSE;
211
212 lpValue = lpValues;
213 pCurItem = m_pItems;
214 while (pCurItem)
215 {
216 memcpy(lpValue, pCurItem->pwszExt, (wcslen(pCurItem->pwszExt) + 1) * sizeof(WCHAR));
217 lpValue += wcslen(pCurItem->pwszExt) + 1;
218 pCurItem = pCurItem->pNext;
219 }
220
221 if (RegCreateKeyEx(HKEY_CURRENT_USER, ShellNewKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS)
222 {
223 HeapFree(GetProcessHeap(), 0, lpValues);
224 return FALSE;
225 }
226
227 if (RegSetValueExW(hKey, L"Classes", NULL, REG_MULTI_SZ, (LPBYTE)lpValues, dwSize * sizeof(WCHAR)) != ERROR_SUCCESS)
228 {
229 HeapFree(GetProcessHeap(), 0, lpValues);
230 return FALSE;
231 }
232
233 HeapFree(GetProcessHeap(), 0, lpValues);
234 RegCloseKey(hKey);
235
236 return TRUE;
237 }
238
239 BOOL
240 CNewMenu::LoadCachedItems()
241 {
242 LPWSTR wszName;
243 LPWSTR lpValues;
244 DWORD dwSize;
245 HKEY hKey;
246 SHELLNEW_ITEM *pNewItem;
247 SHELLNEW_ITEM *pCurItem = NULL;
248
249 if (RegOpenKeyExW(HKEY_CURRENT_USER, ShellNewKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
250 return FALSE;
251
252 if (RegQueryValueExW(hKey, L"Classes", NULL, NULL, NULL, &dwSize) != ERROR_SUCCESS)
253 return FALSE;
254
255 lpValues = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, dwSize);
256 if (!lpValues)
257 return FALSE;
258
259 if (RegQueryValueExW(hKey, L"Classes", NULL, NULL, (LPBYTE)lpValues, &dwSize) != ERROR_SUCCESS)
260 {
261 HeapFree(GetProcessHeap(), 0, lpValues);
262 return FALSE;
263 }
264
265 wszName = lpValues;
266
267 for (; '\0' != *wszName; wszName += wcslen(wszName) + 1)
268 {
269 pNewItem = LoadItem(wszName);
270 if (pNewItem)
271 {
272 if (wcsicmp(pNewItem->pwszExt, L".lnk") == 0)
273 {
274 /* Link handler */
275 m_pLinkItem = pNewItem;
276 }
277 else
278 {
279 /* Add at the end of list */
280 if (pCurItem)
281 {
282 pCurItem->pNext = pNewItem;
283 pCurItem = pNewItem;
284 }
285 else
286 pCurItem = m_pItems = pNewItem;
287 }
288 }
289 }
290
291 HeapFree(GetProcessHeap(), 0, lpValues);
292 RegCloseKey(hKey);
293
294 return TRUE;
295 }
296
297 BOOL
298 CNewMenu::LoadAllItems()
299 {
300 /* If there are any unload them */
301 UnloadAllItems();
302
303 if (!LoadCachedItems())
304 {
305 CacheItems();
306 }
307
308 if (!m_pLinkItem)
309 {
310 m_pLinkItem = static_cast<SHELLNEW_ITEM *>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SHELLNEW_ITEM)));
311 if (m_pLinkItem)
312 {
313 m_pLinkItem->Type = SHELLNEW_TYPE_NULLFILE;
314 m_pLinkItem->pwszDesc = _wcsdup(L"Link");
315 m_pLinkItem->pwszExt = _wcsdup(L".lnk");
316 }
317 }
318
319 if (m_pItems == NULL)
320 return FALSE;
321 else
322 return TRUE;
323 }
324
325 UINT
326 CNewMenu::InsertShellNewItems(HMENU hMenu, UINT idCmdFirst, UINT Pos)
327 {
328 MENUITEMINFOW mii;
329 WCHAR wszBuf[256];
330 UINT idCmd = idCmdFirst;
331
332 if (m_pItems == NULL)
333 {
334 if (!LoadAllItems())
335 return 0;
336 }
337
338 ZeroMemory(&mii, sizeof(mii));
339 mii.cbSize = sizeof(mii);
340
341 /* Insert new folder action */
342 if (!LoadStringW(shell32_hInstance, FCIDM_SHVIEW_NEWFOLDER, wszBuf, _countof(wszBuf)))
343 wszBuf[0] = 0;
344 mii.fMask = MIIM_ID | MIIM_BITMAP | MIIM_STRING;
345 mii.dwTypeData = wszBuf;
346 mii.cch = wcslen(mii.dwTypeData);
347 mii.wID = idCmd;
348 mii.hbmpItem = HBMMENU_CALLBACK;
349 if (InsertMenuItemW(hMenu, Pos++, TRUE, &mii))
350 ++idCmd;
351
352 /* Insert new shortcut action */
353 if (!LoadStringW(shell32_hInstance, FCIDM_SHVIEW_NEWLINK, wszBuf, _countof(wszBuf)))
354 wszBuf[0] = 0;
355 mii.dwTypeData = wszBuf;
356 mii.cch = wcslen(mii.dwTypeData);
357 mii.wID = idCmd;
358 if (InsertMenuItemW(hMenu, Pos++, TRUE, &mii))
359 ++idCmd;
360
361 /* Insert seperator for custom new action */
362 mii.fMask = MIIM_TYPE | MIIM_ID;
363 mii.fType = MFT_SEPARATOR;
364 mii.wID = -1;
365 InsertMenuItemW(hMenu, Pos++, TRUE, &mii);
366
367 /* Insert rest of items */
368 mii.fMask = MIIM_ID | MIIM_BITMAP | MIIM_STRING;
369 mii.fType = 0;
370
371 SHELLNEW_ITEM *pCurItem = m_pItems;
372 while (pCurItem)
373 {
374 TRACE("szDesc %s\n", debugstr_w(pCurItem->pwszDesc));
375 mii.dwTypeData = pCurItem->pwszDesc;
376 mii.cch = wcslen(mii.dwTypeData);
377 mii.wID = idCmd;
378 if (InsertMenuItemW(hMenu, Pos++, TRUE, &mii))
379 ++idCmd;
380 pCurItem = pCurItem->pNext;
381 }
382
383 return idCmd - idCmdFirst;
384 }
385
386 CNewMenu::SHELLNEW_ITEM *CNewMenu::FindItemFromIdOffset(UINT IdOffset)
387 {
388 if (IdOffset == 0)
389 return NULL; /* Folder */
390
391 if (IdOffset == 1)
392 return m_pLinkItem; /* shortcut */
393
394 /* Find shell new item */
395 SHELLNEW_ITEM *pItem = m_pItems;
396 for (UINT i = 2; pItem; ++i)
397 {
398 if (i == IdOffset)
399 break;
400
401 pItem = pItem->pNext;
402 }
403
404 return pItem;
405 }
406
407 HRESULT CNewMenu::SelectNewItem(LPCMINVOKECOMMANDINFO lpici, LONG wEventId, UINT uFlags, LPWSTR pszName)
408 {
409 CComPtr<IShellBrowser> lpSB;
410 CComPtr<IShellView> lpSV;
411 HRESULT hr = E_FAIL;
412 LPITEMIDLIST pidl;
413 PITEMID_CHILD pidlNewItem;
414
415 /* Notify the view object about the new item */
416 SHChangeNotify(wEventId, uFlags, (LPCVOID) pszName, NULL);
417
418 /* FIXME: I think that this can be implemented using callbacks to the shell folder */
419
420 /* Note: CWM_GETISHELLBROWSER returns shell browser without adding reference */
421 lpSB = (LPSHELLBROWSER)SendMessageA(lpici->hwnd, CWM_GETISHELLBROWSER, 0, 0);
422 if (!lpSB)
423 return E_FAIL;
424
425 hr = lpSB->QueryActiveShellView(&lpSV);
426 if (FAILED(hr))
427 return hr;
428
429 /* Attempt to get the pidl of the new item */
430 hr = SHILCreateFromPathW(pszName, &pidl, NULL);
431 if (FAILED_UNEXPECTEDLY(hr))
432 return hr;
433
434 pidlNewItem = ILFindLastID(pidl);
435
436 hr = lpSV->SelectItem(pidlNewItem, SVSI_DESELECTOTHERS | SVSI_EDIT | SVSI_ENSUREVISIBLE |
437 SVSI_FOCUSED | SVSI_SELECT);
438
439 SHFree(pidl);
440
441 return hr;
442 }
443
444 // Code is duplicated in CDefaultContextMenu
445 HRESULT CNewMenu::CreateNewFolder(LPCMINVOKECOMMANDINFO lpici)
446 {
447 WCHAR wszPath[MAX_PATH];
448 WCHAR wszName[MAX_PATH];
449 WCHAR wszNewFolder[25];
450 HRESULT hr;
451
452 /* Get folder path */
453 hr = SHGetPathFromIDListW(m_pidlFolder, wszPath);
454 if (FAILED_UNEXPECTEDLY(hr))
455 return hr;
456
457 if (!LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, _countof(wszNewFolder)))
458 return E_FAIL;
459
460 /* Create the name of the new directory */
461 if (!PathYetAnotherMakeUniqueName(wszName, wszPath, NULL, wszNewFolder))
462 return E_FAIL;
463
464 /* Create the new directory and show the appropriate dialog in case of error */
465 if (SHCreateDirectory (lpici->hwnd, wszName) != ERROR_SUCCESS)
466 return E_FAIL;
467
468 /* Show and select the new item in the def view */
469 SelectNewItem(lpici, SHCNE_MKDIR, SHCNF_PATHW, wszName);
470
471 return S_OK;
472 }
473
474 HRESULT CNewMenu::CreateNewItem(SHELLNEW_ITEM *pItem, LPCMINVOKECOMMANDINFO lpcmi)
475 {
476 WCHAR wszBuf[MAX_PATH];
477 WCHAR wszPath[MAX_PATH];
478 HRESULT hr;
479
480 /* Get folder path */
481 hr = SHGetPathFromIDListW(m_pidlFolder, wszPath);
482 if (FAILED_UNEXPECTEDLY(hr))
483 return hr;
484
485 switch (pItem->Type)
486 {
487 case SHELLNEW_TYPE_COMMAND:
488 {
489 LPWSTR Ptr, pwszCmd;
490 WCHAR wszTemp[MAX_PATH];
491 STARTUPINFOW si;
492 PROCESS_INFORMATION pi;
493
494 if (!ExpandEnvironmentStringsW((LPWSTR)pItem->pData, wszBuf, MAX_PATH))
495 {
496 TRACE("ExpandEnvironmentStrings failed\n");
497 break;
498 }
499
500 /* Expand command parameter, FIXME: there can be more modifiers */
501 Ptr = wcsstr(wszBuf, L"%1");
502 if (Ptr)
503 {
504 Ptr[1] = 's';
505 StringCbPrintfW(wszTemp, sizeof(wszTemp), wszBuf, wszPath);
506 pwszCmd = wszTemp;
507 }
508 else
509 pwszCmd = wszBuf;
510
511 /* Create process */
512 ZeroMemory(&si, sizeof(si));
513 si.cb = sizeof(si);
514 if (CreateProcessW(NULL, pwszCmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
515 {
516 CloseHandle(pi.hProcess);
517 CloseHandle(pi.hThread);
518 } else
519 ERR("Failed to create process\n");
520 break;
521 }
522 case SHELLNEW_TYPE_DATA:
523 case SHELLNEW_TYPE_FILENAME:
524 case SHELLNEW_TYPE_NULLFILE:
525 {
526 BOOL bSuccess = TRUE;
527 LPWSTR pwszFilename = NULL;
528 size_t cchFilenameMax = 0;
529
530 /* Build new file name */
531 LoadStringW(shell32_hInstance, FCIDM_SHVIEW_NEW, wszBuf, _countof(wszBuf));
532 StringCchCatExW(wszPath, _countof(wszPath), L"\\", &pwszFilename, &cchFilenameMax, 0);
533 StringCchPrintfW(pwszFilename, cchFilenameMax, L"%s %s%s", wszBuf, pItem->pwszDesc, pItem->pwszExt);
534
535 /* Find unique name */
536 for (UINT i = 2; PathFileExistsW(wszPath); ++i)
537 {
538 StringCchPrintfW(pwszFilename, cchFilenameMax, L"%s %s (%u)%s", wszBuf, pItem->pwszDesc, i, pItem->pwszExt);
539 TRACE("New Filename %ls\n", pwszFilename);
540 }
541
542 /* Create new file */
543 HANDLE hFile = CreateFileW(wszPath, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
544 if (hFile != INVALID_HANDLE_VALUE)
545 {
546 if (pItem->Type == SHELLNEW_TYPE_DATA)
547 {
548 /* Write a content */
549 DWORD cbWritten;
550 WriteFile(hFile, pItem->pData, pItem->cbData, &cbWritten, NULL);
551 }
552
553 /* Close file now */
554 CloseHandle(hFile);
555 } else
556 bSuccess = FALSE;
557
558 if (pItem->Type == SHELLNEW_TYPE_FILENAME)
559 {
560 /* Copy file */
561 if (!CopyFileW((LPWSTR)pItem->pData, wszPath, FALSE))
562 ERR("Copy file failed: %ls\n", (LPWSTR)pItem->pData);
563 }
564
565 /* Show message if we failed */
566 if (bSuccess)
567 {
568 TRACE("Notifying fs %s\n", debugstr_w(wszPath));
569 SelectNewItem(lpcmi, SHCNE_CREATE, SHCNF_PATHW, wszPath);
570 }
571 else
572 {
573 StringCbPrintfW(wszBuf, sizeof(wszBuf), L"Cannot create file: %s", pwszFilename);
574 MessageBoxW(NULL, wszBuf, L"Cannot create file", MB_OK|MB_ICONERROR); // FIXME
575 }
576 break;
577 }
578 case SHELLNEW_TYPE_INVALID:
579 ERR("Invalid type\n");
580 break;
581 }
582
583 return S_OK;
584 }
585
586 HRESULT STDMETHODCALLTYPE CNewMenu::SetSite(IUnknown *pUnkSite)
587 {
588 m_pSite = pUnkSite;
589 return S_OK;
590 }
591
592 HRESULT STDMETHODCALLTYPE CNewMenu::GetSite(REFIID riid, void **ppvSite)
593 {
594 return m_pSite->QueryInterface(riid, ppvSite);
595 }
596
597 HRESULT
598 WINAPI
599 CNewMenu::QueryContextMenu(HMENU hMenu,
600 UINT indexMenu,
601 UINT idCmdFirst,
602 UINT idCmdLast,
603 UINT uFlags)
604 {
605 WCHAR wszNew[200];
606 MENUITEMINFOW mii;
607 UINT cItems = 0;
608
609 m_idCmdFirst = idCmdFirst;
610
611 TRACE("%p %p %u %u %u %u\n", this,
612 hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
613
614 if (!LoadStringW(shell32_hInstance, FCIDM_SHVIEW_NEW, wszNew, _countof(wszNew)))
615 return E_FAIL;
616
617 m_hSubMenu = CreateMenu();
618 if (!m_hSubMenu)
619 return E_FAIL;
620
621 cItems = InsertShellNewItems(m_hSubMenu, idCmdFirst, 0);
622
623 memset(&mii, 0, sizeof(mii));
624 mii.cbSize = sizeof(mii);
625 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE | MIIM_SUBMENU;
626 mii.fType = MFT_STRING;
627 mii.wID = -1;
628 mii.dwTypeData = wszNew;
629 mii.cch = wcslen(mii.dwTypeData);
630 mii.fState = MFS_ENABLED;
631 mii.hSubMenu = m_hSubMenu;
632
633 if (!InsertMenuItemW(hMenu, indexMenu, TRUE, &mii))
634 return E_FAIL;
635
636 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, cItems);
637 }
638
639 HRESULT
640 WINAPI
641 CNewMenu::InvokeCommand(LPCMINVOKECOMMANDINFO lpici)
642 {
643 HRESULT hr = E_FAIL;
644
645 if (LOWORD(lpici->lpVerb) == 0)
646 hr = CreateNewFolder(lpici);
647 else
648 {
649 SHELLNEW_ITEM *pItem = FindItemFromIdOffset(LOWORD(lpici->lpVerb));
650 if (pItem)
651 hr = CreateNewItem(pItem, lpici);
652 }
653
654 TRACE("CNewMenu::InvokeCommand %x\n", hr);
655 return hr;
656 }
657
658 HRESULT
659 WINAPI
660 CNewMenu::GetCommandString(UINT_PTR idCmd,
661 UINT uType,
662 UINT *pwReserved,
663 LPSTR pszName,
664 UINT cchMax)
665 {
666 FIXME("%p %lu %u %p %p %u\n", this,
667 idCmd, uType, pwReserved, pszName, cchMax );
668
669 return E_NOTIMPL;
670 }
671
672 HRESULT
673 WINAPI
674 CNewMenu::HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)
675 {
676 return S_OK;
677 }
678
679 HRESULT
680 WINAPI
681 CNewMenu::HandleMenuMsg2(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *plResult)
682 {
683 switch (uMsg)
684 {
685 case WM_MEASUREITEM:
686 {
687 MEASUREITEMSTRUCT* lpmis = reinterpret_cast<MEASUREITEMSTRUCT*>(lParam);
688 if (!lpmis || lpmis->CtlType != ODT_MENU)
689 break;
690
691 if (lpmis->itemWidth < (UINT)GetSystemMetrics(SM_CXMENUCHECK))
692 lpmis->itemWidth = GetSystemMetrics(SM_CXMENUCHECK);
693 if (lpmis->itemHeight < 16)
694 lpmis->itemHeight = 16;
695
696 if (plResult)
697 *plResult = TRUE;
698 break;
699 }
700 case WM_DRAWITEM:
701 {
702 DRAWITEMSTRUCT* lpdis = reinterpret_cast<DRAWITEMSTRUCT*>(lParam);
703 if (!lpdis || lpdis->CtlType != ODT_MENU)
704 break;
705
706 DWORD id = LOWORD(lpdis->itemID) - m_idCmdFirst;
707 HICON hIcon = 0;
708 if (id == 0)
709 hIcon = m_hiconFolder;
710 else if (id == 1)
711 hIcon = m_hiconLink;
712 else
713 {
714 SHELLNEW_ITEM *pItem = FindItemFromIdOffset(id);
715 if (pItem)
716 hIcon = pItem->hIcon;
717 }
718
719 if (!hIcon)
720 break;
721
722 DrawIconEx(lpdis->hDC,
723 2,
724 lpdis->rcItem.top + (lpdis->rcItem.bottom - lpdis->rcItem.top - 16) / 2,
725 hIcon,
726 16,
727 16,
728 0, NULL, DI_NORMAL);
729
730 if(plResult)
731 *plResult = TRUE;
732 }
733 }
734
735 return S_OK;
736 }
737
738 HRESULT WINAPI
739 CNewMenu::Initialize(LPCITEMIDLIST pidlFolder,
740 IDataObject *pdtobj, HKEY hkeyProgID)
741 {
742 m_pidlFolder = ILClone(pidlFolder);
743
744 /* Load folder and shortcut icons */
745 m_hiconFolder = (HICON)LoadImage(shell32_hInstance, MAKEINTRESOURCE(IDI_SHELL_FOLDER), IMAGE_ICON, 16, 16, LR_SHARED);
746 m_hiconLink = (HICON)LoadImage(shell32_hInstance, MAKEINTRESOURCE(IDI_SHELL_SHORTCUT), IMAGE_ICON, 16, 16, LR_SHARED);
747 return S_OK;
748 }