mshtml: Implement MarkupServices_ParseString.
[wine.git] / dlls / shell32 / shlmenu.c
blob85ca417a6235d77afb87f8ee958e3076bc5a2ecb
1 /*
2 * see www.geocities.com/SiliconValley/4942/filemenu.html
4 * Copyright 1999, 2000 Juergen Schmied
5 * Copyright 2011 Jay Yang
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <string.h>
25 #define COBJMACROS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "shlobj.h"
33 #include "shlwapi.h"
34 #include "shell32_main.h"
36 #include "pidl.h"
37 #include "wine/debug.h"
38 #include "debughlp.h"
40 /* FileMenu_Create nSelHeight constants */
41 #define FM_DEFAULT_SELHEIGHT -1
42 #define FM_FULL_SELHEIGHT 0
44 /* FileMenu_Create flags */
45 #define FMF_SMALL_ICONS 0x00
46 #define FMF_LARGE_ICONS 0x08
47 #define FMF_NO_COLUMN_BREAK 0x10
49 /* FileMenu_AppendItem constants */
50 #define FM_SEPARATOR ((const WCHAR *)1)
51 #define FM_BLANK_ICON -1
52 #define FM_DEFAULT_HEIGHT 0
54 /* FileMenu_InsertUsingPidl flags */
55 #define FMF_NO_EMPTY_ITEM 0x01
56 #define FMF_NO_PROGRAM_GROUPS 0x04
58 /* FileMenu_InsertUsingPidl callback function */
59 typedef void (CALLBACK *LPFNFMCALLBACK)(LPCITEMIDLIST pidlFolder, LPCITEMIDLIST pidlFile);
61 static BOOL FileMenu_AppendItemW(HMENU hMenu, LPCWSTR lpText, UINT uID, int icon,
62 HMENU hMenuPopup, int nItemHeight);
63 BOOL WINAPI FileMenu_DeleteAllItems(HMENU hMenu);
65 typedef struct
67 BOOL bInitialized;
68 BOOL bFixedItems;
69 /* create */
70 COLORREF crBorderColor;
71 int nBorderWidth;
72 HBITMAP hBorderBmp;
74 /* insert using pidl */
75 LPITEMIDLIST pidl;
76 UINT uID;
77 UINT uFlags;
78 UINT uEnumFlags;
79 LPFNFMCALLBACK lpfnCallback;
80 } FMINFO, *LPFMINFO;
82 typedef struct
83 { int cchItemText;
84 int iIconIndex;
85 HMENU hMenu;
86 WCHAR szItemText[1];
87 } FMITEM, * LPFMITEM;
89 static BOOL bAbortInit;
91 #define CCH_MAXITEMTEXT 256
93 WINE_DEFAULT_DEBUG_CHANNEL(shell);
95 static LPFMINFO FM_GetMenuInfo(HMENU hmenu)
97 MENUINFO MenuInfo;
98 LPFMINFO menudata;
100 MenuInfo.cbSize = sizeof(MENUINFO);
101 MenuInfo.fMask = MIM_MENUDATA;
103 if (! GetMenuInfo(hmenu, &MenuInfo))
104 return NULL;
106 menudata = (LPFMINFO)MenuInfo.dwMenuData;
108 if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
110 ERR("menudata corrupt: %p %lu\n", menudata, MenuInfo.cbSize);
111 return 0;
114 return menudata;
117 /*************************************************************************
118 * FM_SetMenuParameter [internal]
121 static LPFMINFO FM_SetMenuParameter(
122 HMENU hmenu,
123 UINT uID,
124 LPCITEMIDLIST pidl,
125 UINT uFlags,
126 UINT uEnumFlags,
127 LPFNFMCALLBACK lpfnCallback)
129 LPFMINFO menudata;
131 TRACE("\n");
133 menudata = FM_GetMenuInfo(hmenu);
135 SHFree(menudata->pidl);
137 menudata->uID = uID;
138 menudata->pidl = ILClone(pidl);
139 menudata->uFlags = uFlags;
140 menudata->uEnumFlags = uEnumFlags;
141 menudata->lpfnCallback = lpfnCallback;
143 return menudata;
146 /*************************************************************************
147 * FM_InitMenuPopup [internal]
150 static int FM_InitMenuPopup(HMENU hmenu, LPCITEMIDLIST pAlternatePidl)
151 { IShellFolder *lpsf, *lpsf2;
152 ULONG ulItemAttr = SFGAO_FOLDER;
153 UINT uID, uEnumFlags;
154 LPFNFMCALLBACK lpfnCallback;
155 LPCITEMIDLIST pidl;
156 WCHAR sTemp[MAX_PATH];
157 int NumberOfItems = 0, iIcon;
158 MENUINFO MenuInfo;
159 LPFMINFO menudata;
161 TRACE("%p %p\n", hmenu, pAlternatePidl);
163 MenuInfo.cbSize = sizeof(MENUINFO);
164 MenuInfo.fMask = MIM_MENUDATA;
166 if (! GetMenuInfo(hmenu, &MenuInfo))
167 return FALSE;
169 menudata = (LPFMINFO)MenuInfo.dwMenuData;
171 if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
173 ERR("menudata corrupt: %p %lu\n", menudata, MenuInfo.cbSize);
174 return 0;
177 if (menudata->bInitialized)
178 return 0;
180 pidl = (pAlternatePidl? pAlternatePidl: menudata->pidl);
181 if (!pidl)
182 return 0;
184 uID = menudata->uID;
185 uEnumFlags = menudata->uEnumFlags;
186 lpfnCallback = menudata->lpfnCallback;
187 menudata->bInitialized = FALSE;
189 SetMenuInfo(hmenu, &MenuInfo);
191 if (SUCCEEDED (SHGetDesktopFolder(&lpsf)))
193 if (SUCCEEDED(IShellFolder_BindToObject(lpsf, pidl,0,&IID_IShellFolder,(LPVOID *)&lpsf2)))
195 IEnumIDList *lpe = NULL;
197 if (SUCCEEDED (IShellFolder_EnumObjects(lpsf2, 0, uEnumFlags, &lpe )))
200 LPITEMIDLIST pidlTemp = NULL;
201 ULONG ulFetched;
203 while ((!bAbortInit) && (S_OK == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched)))
205 if (SUCCEEDED (IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulItemAttr)))
207 ILGetDisplayNameExW(NULL, pidlTemp, sTemp, ILGDN_FORPARSING);
208 if (! (PidlToSicIndex(lpsf, pidlTemp, FALSE, 0, &iIcon)))
209 iIcon = FM_BLANK_ICON;
210 if ( SFGAO_FOLDER & ulItemAttr)
212 LPFMINFO lpFmMi;
213 MENUINFO MenuInfo;
214 HMENU hMenuPopup = CreatePopupMenu();
216 lpFmMi = calloc(1, sizeof(*lpFmMi));
218 lpFmMi->pidl = ILCombine(pidl, pidlTemp);
219 lpFmMi->uEnumFlags = SHCONTF_FOLDERS | SHCONTF_NONFOLDERS;
221 MenuInfo.cbSize = sizeof(MENUINFO);
222 MenuInfo.fMask = MIM_MENUDATA;
223 MenuInfo.dwMenuData = (ULONG_PTR) lpFmMi;
224 SetMenuInfo (hMenuPopup, &MenuInfo);
226 FileMenu_AppendItemW (hmenu, sTemp, uID, iIcon, hMenuPopup, FM_DEFAULT_HEIGHT);
228 else
230 LPWSTR pExt = PathFindExtensionW(sTemp);
231 if (pExt)
232 *pExt = 0;
233 FileMenu_AppendItemW (hmenu, sTemp, uID, iIcon, 0, FM_DEFAULT_HEIGHT);
237 if (lpfnCallback)
239 TRACE("enter callback\n");
240 lpfnCallback ( pidl, pidlTemp);
241 TRACE("leave callback\n");
244 NumberOfItems++;
246 IEnumIDList_Release (lpe);
248 IShellFolder_Release(lpsf2);
250 IShellFolder_Release(lpsf);
253 if ( GetMenuItemCount (hmenu) == 0 )
255 FileMenu_AppendItemW (hmenu, L"(empty)", uID, FM_BLANK_ICON, 0, FM_DEFAULT_HEIGHT);
256 NumberOfItems++;
259 menudata->bInitialized = TRUE;
260 SetMenuInfo(hmenu, &MenuInfo);
262 return NumberOfItems;
264 /*************************************************************************
265 * FileMenu_Create [SHELL32.114]
267 * NOTES
268 * for non-root menus values are
269 * (ffffffff,00000000,00000000,00000000,00000000)
271 HMENU WINAPI FileMenu_Create (
272 COLORREF crBorderColor,
273 int nBorderWidth,
274 HBITMAP hBorderBmp,
275 int nSelHeight,
276 UINT uFlags)
278 MENUINFO MenuInfo;
279 LPFMINFO menudata;
281 HMENU hMenu = CreatePopupMenu();
283 TRACE("0x%08lx 0x%08x %p 0x%08x 0x%08x hMenu=%p\n",
284 crBorderColor, nBorderWidth, hBorderBmp, nSelHeight, uFlags, hMenu);
286 menudata = calloc(1, sizeof(*menudata));
287 menudata->crBorderColor = crBorderColor;
288 menudata->nBorderWidth = nBorderWidth;
289 menudata->hBorderBmp = hBorderBmp;
291 MenuInfo.cbSize = sizeof(MENUINFO);
292 MenuInfo.fMask = MIM_MENUDATA;
293 MenuInfo.dwMenuData = (ULONG_PTR) menudata;
294 SetMenuInfo (hMenu, &MenuInfo);
296 return hMenu;
299 /*************************************************************************
300 * FileMenu_Destroy [SHELL32.118]
302 * NOTES
303 * exported by name
305 void WINAPI FileMenu_Destroy (HMENU hmenu)
307 LPFMINFO menudata;
309 TRACE("%p\n", hmenu);
311 FileMenu_DeleteAllItems (hmenu);
313 menudata = FM_GetMenuInfo(hmenu);
315 SHFree( menudata->pidl);
316 free(menudata);
318 DestroyMenu (hmenu);
321 /*************************************************************************
322 * FileMenu_AppendItem [SHELL32.115]
325 static BOOL FileMenu_AppendItemW(
326 HMENU hMenu,
327 LPCWSTR lpText,
328 UINT uID,
329 int icon,
330 HMENU hMenuPopup,
331 int nItemHeight)
333 MENUITEMINFOW mii;
334 LPFMITEM myItem;
335 LPFMINFO menudata;
336 MENUINFO MenuInfo;
339 TRACE("%p %s 0x%08x 0x%08x %p 0x%08x\n",
340 hMenu, (lpText!=FM_SEPARATOR) ? debugstr_w(lpText) : NULL,
341 uID, icon, hMenuPopup, nItemHeight);
343 ZeroMemory (&mii, sizeof(MENUITEMINFOW));
345 mii.cbSize = sizeof(MENUITEMINFOW);
347 if (lpText != FM_SEPARATOR)
349 int len = lstrlenW (lpText);
350 myItem = SHAlloc(sizeof(FMITEM) + len*sizeof(WCHAR));
351 lstrcpyW (myItem->szItemText, lpText);
352 myItem->cchItemText = len;
353 myItem->iIconIndex = icon;
354 myItem->hMenu = hMenu;
355 mii.fMask = MIIM_DATA;
356 mii.dwItemData = (ULONG_PTR) myItem;
359 if ( hMenuPopup )
360 { /* sub menu */
361 mii.fMask |= MIIM_TYPE | MIIM_SUBMENU;
362 mii.fType = MFT_OWNERDRAW;
363 mii.hSubMenu = hMenuPopup;
365 else if (lpText == FM_SEPARATOR )
366 { mii.fMask |= MIIM_ID | MIIM_TYPE;
367 mii.fType = MFT_SEPARATOR;
369 else
370 { /* normal item */
371 mii.fMask |= MIIM_ID | MIIM_TYPE | MIIM_STATE;
372 mii.fState = MFS_ENABLED | MFS_DEFAULT;
373 mii.fType = MFT_OWNERDRAW;
375 mii.wID = uID;
377 InsertMenuItemW (hMenu, (UINT)-1, TRUE, &mii);
379 /* set bFixedItems to true */
380 MenuInfo.cbSize = sizeof(MENUINFO);
381 MenuInfo.fMask = MIM_MENUDATA;
383 if (! GetMenuInfo(hMenu, &MenuInfo))
384 return FALSE;
386 menudata = (LPFMINFO)MenuInfo.dwMenuData;
387 if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
389 ERR("menudata corrupt: %p %lu\n", menudata, MenuInfo.cbSize);
390 return FALSE;
393 menudata->bFixedItems = TRUE;
394 SetMenuInfo(hMenu, &MenuInfo);
396 return TRUE;
400 /**********************************************************************/
402 BOOL WINAPI FileMenu_AppendItemAW(
403 HMENU hMenu,
404 LPCVOID lpText,
405 UINT uID,
406 int icon,
407 HMENU hMenuPopup,
408 int nItemHeight)
410 BOOL ret;
412 if (!lpText) return FALSE;
414 if (SHELL_OsIsUnicode() || lpText == FM_SEPARATOR)
415 ret = FileMenu_AppendItemW(hMenu, lpText, uID, icon, hMenuPopup, nItemHeight);
416 else
418 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpText, -1, NULL, 0 );
419 WCHAR *lpszText = malloc(len * sizeof(WCHAR));
420 if (!lpszText) return FALSE;
421 MultiByteToWideChar( CP_ACP, 0, lpText, -1, lpszText, len );
422 ret = FileMenu_AppendItemW(hMenu, lpszText, uID, icon, hMenuPopup, nItemHeight);
423 free(lpszText);
426 return ret;
429 /*************************************************************************
430 * FileMenu_InsertUsingPidl [SHELL32.110]
432 * NOTES
433 * uEnumFlags any SHCONTF flag
435 int WINAPI FileMenu_InsertUsingPidl (
436 HMENU hmenu,
437 UINT uID,
438 LPCITEMIDLIST pidl,
439 UINT uFlags,
440 UINT uEnumFlags,
441 LPFNFMCALLBACK lpfnCallback)
443 TRACE("%p 0x%08x %p 0x%08x 0x%08x %p\n",
444 hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
446 pdump (pidl);
448 bAbortInit = FALSE;
450 FM_SetMenuParameter(hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
452 return FM_InitMenuPopup(hmenu, NULL);
455 /*************************************************************************
456 * FileMenu_ReplaceUsingPidl [SHELL32.113]
458 * FIXME: the static items are deleted but won't be refreshed
460 int WINAPI FileMenu_ReplaceUsingPidl(
461 HMENU hmenu,
462 UINT uID,
463 LPCITEMIDLIST pidl,
464 UINT uEnumFlags,
465 LPFNFMCALLBACK lpfnCallback)
467 TRACE("%p 0x%08x %p 0x%08x %p\n",
468 hmenu, uID, pidl, uEnumFlags, lpfnCallback);
470 FileMenu_DeleteAllItems (hmenu);
472 FM_SetMenuParameter(hmenu, uID, pidl, 0, uEnumFlags, lpfnCallback);
474 return FM_InitMenuPopup(hmenu, NULL);
477 /*************************************************************************
478 * FileMenu_Invalidate [SHELL32.111]
480 void WINAPI FileMenu_Invalidate (HMENU hMenu)
482 FIXME("%p\n",hMenu);
485 /*************************************************************************
486 * FileMenu_FindSubMenuByPidl [SHELL32.106]
488 HMENU WINAPI FileMenu_FindSubMenuByPidl(
489 HMENU hMenu,
490 LPCITEMIDLIST pidl)
492 FIXME("%p %p\n",hMenu, pidl);
493 return 0;
496 /*************************************************************************
497 * FileMenu_AppendFilesForPidl [SHELL32.124]
499 int WINAPI FileMenu_AppendFilesForPidl(
500 HMENU hmenu,
501 LPCITEMIDLIST pidl,
502 BOOL bAddSeparator)
504 LPFMINFO menudata;
506 menudata = FM_GetMenuInfo(hmenu);
508 menudata->bInitialized = FALSE;
510 FM_InitMenuPopup(hmenu, pidl);
512 if (bAddSeparator)
513 FileMenu_AppendItemW (hmenu, FM_SEPARATOR, 0, 0, 0, FM_DEFAULT_HEIGHT);
515 TRACE("%p %p 0x%08x\n",hmenu, pidl,bAddSeparator);
517 return 0;
519 /*************************************************************************
520 * FileMenu_AddFilesForPidl [SHELL32.125]
522 * NOTES
523 * uEnumFlags any SHCONTF flag
525 int WINAPI FileMenu_AddFilesForPidl (
526 HMENU hmenu,
527 UINT uReserved,
528 UINT uID,
529 LPCITEMIDLIST pidl,
530 UINT uFlags,
531 UINT uEnumFlags,
532 LPFNFMCALLBACK lpfnCallback)
534 TRACE("%p 0x%08x 0x%08x %p 0x%08x 0x%08x %p\n",
535 hmenu, uReserved, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
537 return FileMenu_InsertUsingPidl ( hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
542 /*************************************************************************
543 * FileMenu_TrackPopupMenuEx [SHELL32.116]
545 BOOL WINAPI FileMenu_TrackPopupMenuEx (
546 HMENU hMenu,
547 UINT uFlags,
548 int x,
549 int y,
550 HWND hWnd,
551 LPTPMPARAMS lptpm)
553 TRACE("%p 0x%08x 0x%x 0x%x %p %p\n",
554 hMenu, uFlags, x, y, hWnd, lptpm);
555 return TrackPopupMenuEx(hMenu, uFlags, x, y, hWnd, lptpm);
558 /*************************************************************************
559 * FileMenu_GetLastSelectedItemPidls [SHELL32.107]
561 BOOL WINAPI FileMenu_GetLastSelectedItemPidls(
562 UINT uReserved,
563 LPCITEMIDLIST *ppidlFolder,
564 LPCITEMIDLIST *ppidlItem)
566 FIXME("0x%08x %p %p\n",uReserved, ppidlFolder, ppidlItem);
567 return FALSE;
570 #define FM_ICON_SIZE 16
571 #define FM_Y_SPACE 4
572 #define FM_SPACE1 4
573 #define FM_SPACE2 2
574 #define FM_LEFTBORDER 2
575 #define FM_RIGHTBORDER 8
576 /*************************************************************************
577 * FileMenu_MeasureItem [SHELL32.112]
579 LRESULT WINAPI FileMenu_MeasureItem(
580 HWND hWnd,
581 LPMEASUREITEMSTRUCT lpmis)
583 LPFMITEM pMyItem = (LPFMITEM)(lpmis->itemData);
584 HDC hdc = GetDC(hWnd);
585 SIZE size;
586 LPFMINFO menuinfo;
588 TRACE("%p %p %s\n", hWnd, lpmis, debugstr_w(pMyItem->szItemText));
590 GetTextExtentPoint32W(hdc, pMyItem->szItemText, pMyItem->cchItemText, &size);
592 lpmis->itemWidth = size.cx + FM_LEFTBORDER + FM_ICON_SIZE + FM_SPACE1 + FM_SPACE2 + FM_RIGHTBORDER;
593 lpmis->itemHeight = (size.cy > (FM_ICON_SIZE + FM_Y_SPACE)) ? size.cy : (FM_ICON_SIZE + FM_Y_SPACE);
595 /* add the menubitmap */
596 menuinfo = FM_GetMenuInfo(pMyItem->hMenu);
597 if (menuinfo->nBorderWidth)
598 lpmis->itemWidth += menuinfo->nBorderWidth;
600 TRACE("-- 0x%04x 0x%04x\n", lpmis->itemWidth, lpmis->itemHeight);
601 ReleaseDC (hWnd, hdc);
602 return 0;
604 /*************************************************************************
605 * FileMenu_DrawItem [SHELL32.105]
607 LRESULT WINAPI FileMenu_DrawItem(
608 HWND hWnd,
609 LPDRAWITEMSTRUCT lpdis)
611 LPFMITEM pMyItem = (LPFMITEM)(lpdis->itemData);
612 COLORREF clrPrevText, clrPrevBkgnd;
613 int xi,yi,xt,yt;
614 HIMAGELIST hImageList;
615 RECT TextRect;
616 LPFMINFO menuinfo;
618 TRACE("%p %p %s\n", hWnd, lpdis, debugstr_w(pMyItem->szItemText));
620 if (lpdis->itemState & ODS_SELECTED)
622 clrPrevText = SetTextColor(lpdis->hDC, GetSysColor (COLOR_HIGHLIGHTTEXT));
623 clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor (COLOR_HIGHLIGHT));
625 else
627 clrPrevText = SetTextColor(lpdis->hDC, GetSysColor (COLOR_MENUTEXT));
628 clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor (COLOR_MENU));
631 TextRect = lpdis->rcItem;
633 /* add the menubitmap */
634 menuinfo = FM_GetMenuInfo(pMyItem->hMenu);
635 if (menuinfo->nBorderWidth)
636 TextRect.left += menuinfo->nBorderWidth;
638 TextRect.left += FM_LEFTBORDER;
639 xi = TextRect.left + FM_SPACE1;
640 yi = TextRect.top + FM_Y_SPACE/2;
641 TextRect.bottom -= FM_Y_SPACE/2;
643 xt = xi + FM_ICON_SIZE + FM_SPACE2;
644 yt = yi;
646 ExtTextOutW (lpdis->hDC, xt , yt, ETO_OPAQUE, &TextRect, pMyItem->szItemText, pMyItem->cchItemText, NULL);
648 Shell_GetImageLists(0, &hImageList);
649 ImageList_Draw(hImageList, pMyItem->iIconIndex, lpdis->hDC, xi, yi, ILD_NORMAL);
651 TRACE("-- %s\n", wine_dbgstr_rect(&TextRect));
653 SetTextColor(lpdis->hDC, clrPrevText);
654 SetBkColor(lpdis->hDC, clrPrevBkgnd);
656 return TRUE;
659 /*************************************************************************
660 * FileMenu_InitMenuPopup [SHELL32.109]
662 * NOTES
663 * The filemenu is an ownerdrawn menu. Call this function responding to
664 * WM_INITPOPUPMENU
667 BOOL WINAPI FileMenu_InitMenuPopup (HMENU hmenu)
669 FM_InitMenuPopup(hmenu, NULL);
670 return TRUE;
673 /*************************************************************************
674 * FileMenu_HandleMenuChar [SHELL32.108]
676 LRESULT WINAPI FileMenu_HandleMenuChar(
677 HMENU hMenu,
678 WPARAM wParam)
680 FIXME("%p 0x%08Ix\n",hMenu,wParam);
681 return 0;
684 /*************************************************************************
685 * FileMenu_DeleteAllItems [SHELL32.104]
687 * NOTES
688 * exported by name
690 BOOL WINAPI FileMenu_DeleteAllItems (HMENU hmenu)
692 MENUITEMINFOW mii;
693 LPFMINFO menudata;
695 int i;
697 TRACE("%p\n", hmenu);
699 ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
700 mii.cbSize = sizeof(MENUITEMINFOW);
701 mii.fMask = MIIM_SUBMENU|MIIM_DATA;
703 for (i = 0; i < GetMenuItemCount( hmenu ); i++)
704 { GetMenuItemInfoW(hmenu, i, TRUE, &mii );
706 SHFree((LPFMINFO)mii.dwItemData);
708 if (mii.hSubMenu)
709 FileMenu_Destroy(mii.hSubMenu);
712 while (DeleteMenu (hmenu, 0, MF_BYPOSITION)){};
714 menudata = FM_GetMenuInfo(hmenu);
716 menudata->bInitialized = FALSE;
718 return TRUE;
721 /*************************************************************************
722 * FileMenu_DeleteItemByCmd [SHELL32.117]
725 BOOL WINAPI FileMenu_DeleteItemByCmd (HMENU hMenu, UINT uID)
727 MENUITEMINFOW mii;
729 TRACE("%p 0x%08x\n", hMenu, uID);
731 ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
732 mii.cbSize = sizeof(MENUITEMINFOW);
733 mii.fMask = MIIM_SUBMENU;
735 GetMenuItemInfoW(hMenu, uID, FALSE, &mii );
736 if ( mii.hSubMenu )
738 /* FIXME: Do what? */
741 DeleteMenu(hMenu, MF_BYCOMMAND, uID);
742 return TRUE;
745 /*************************************************************************
746 * FileMenu_DeleteItemByIndex [SHELL32.140]
748 BOOL WINAPI FileMenu_DeleteItemByIndex ( HMENU hMenu, UINT uPos)
750 MENUITEMINFOW mii;
752 TRACE("%p 0x%08x\n", hMenu, uPos);
754 ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
755 mii.cbSize = sizeof(MENUITEMINFOW);
756 mii.fMask = MIIM_SUBMENU;
758 GetMenuItemInfoW(hMenu, uPos, TRUE, &mii );
759 if ( mii.hSubMenu )
761 /* FIXME: Do what? */
764 DeleteMenu(hMenu, MF_BYPOSITION, uPos);
765 return TRUE;
768 /*************************************************************************
769 * FileMenu_DeleteItemByFirstID [SHELL32.141]
771 BOOL WINAPI FileMenu_DeleteItemByFirstID(
772 HMENU hMenu,
773 UINT uID)
775 TRACE("%p 0x%08x\n", hMenu, uID);
776 return FALSE;
779 /*************************************************************************
780 * FileMenu_DeleteSeparator [SHELL32.142]
782 BOOL WINAPI FileMenu_DeleteSeparator(HMENU hMenu)
784 TRACE("%p\n", hMenu);
785 return FALSE;
788 /*************************************************************************
789 * FileMenu_EnableItemByCmd [SHELL32.143]
791 BOOL WINAPI FileMenu_EnableItemByCmd(
792 HMENU hMenu,
793 UINT uID,
794 BOOL bEnable)
796 TRACE("%p 0x%08x 0x%08x\n", hMenu, uID,bEnable);
797 return FALSE;
800 /*************************************************************************
801 * FileMenu_GetItemExtent [SHELL32.144]
803 * NOTES
804 * if the menu is too big, entries are getting cut away!!
806 DWORD WINAPI FileMenu_GetItemExtent (HMENU hMenu, UINT uPos)
807 { RECT rect;
809 FIXME("%p 0x%08x\n", hMenu, uPos);
811 if (GetMenuItemRect(0, hMenu, uPos, &rect))
813 FIXME("%s\n", wine_dbgstr_rect(&rect));
814 return ((rect.right-rect.left)<<16) + (rect.top-rect.bottom);
816 return 0x00100010; /*FIXME*/
819 /*************************************************************************
820 * FileMenu_AbortInitMenu [SHELL32.120]
823 void WINAPI FileMenu_AbortInitMenu (void)
824 { TRACE("\n");
825 bAbortInit = TRUE;
828 /*************************************************************************
829 * SHFind_InitMenuPopup [SHELL32.149]
831 * Get the IContextMenu instance for the submenu of options displayed
832 * for the Search entry in the Classic style Start menu.
834 * PARAMETERS
835 * hMenu [in] handle of menu previously created
836 * hWndParent [in] parent window
837 * w [in] no pointer (0x209 over here) perhaps menu IDs ???
838 * x [in] no pointer (0x226 over here)
840 * RETURNS
841 * LPXXXXX pointer to struct containing a func addr at offset 8
842 * or NULL at failure.
844 LPVOID WINAPI SHFind_InitMenuPopup (HMENU hMenu, HWND hWndParent, DWORD w, DWORD x)
846 FIXME("hmenu=%p hwnd=%p 0x%08lx 0x%08lx stub\n",
847 hMenu,hWndParent,w,x);
848 return NULL; /* this is supposed to be a pointer */
851 /*************************************************************************
852 * _SHIsMenuSeparator (internal)
854 static BOOL _SHIsMenuSeparator(HMENU hm, int i)
856 MENUITEMINFOW mii;
858 mii.cbSize = sizeof(MENUITEMINFOW);
859 mii.fMask = MIIM_TYPE;
860 mii.cch = 0; /* WARNING: We MUST initialize it to 0*/
861 if (!GetMenuItemInfoW(hm, i, TRUE, &mii))
863 return(FALSE);
866 if (mii.fType & MFT_SEPARATOR)
868 return(TRUE);
871 return(FALSE);
874 /*************************************************************************
875 * Shell_MergeMenus [SHELL32.67]
877 UINT WINAPI Shell_MergeMenus (HMENU hmDst, HMENU hmSrc, UINT uInsert, UINT uIDAdjust, UINT uIDAdjustMax, ULONG uFlags)
878 { int nItem;
879 HMENU hmSubMenu;
880 BOOL bAlreadySeparated;
881 MENUITEMINFOW miiSrc;
882 WCHAR szName[256];
883 UINT uTemp, uIDMax = uIDAdjust;
885 TRACE("hmenu1=%p hmenu2=%p 0x%04x 0x%04x 0x%04x 0x%04lx\n",
886 hmDst, hmSrc, uInsert, uIDAdjust, uIDAdjustMax, uFlags);
888 if (!hmDst || !hmSrc)
889 return uIDMax;
891 nItem = GetMenuItemCount(hmDst);
892 if (nItem == -1)
893 return uIDMax;
895 if (uInsert >= (UINT)nItem) /* insert position inside menu? */
897 uInsert = (UINT)nItem; /* append on the end */
898 bAlreadySeparated = TRUE;
900 else
902 bAlreadySeparated = _SHIsMenuSeparator(hmDst, uInsert);
905 if ((uFlags & MM_ADDSEPARATOR) && !bAlreadySeparated)
907 /* Add a separator between the menus */
908 InsertMenuA(hmDst, uInsert, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
909 bAlreadySeparated = TRUE;
913 /* Go through the menu items and clone them*/
914 for (nItem = GetMenuItemCount(hmSrc) - 1; nItem >= 0; nItem--)
916 miiSrc.cbSize = sizeof(MENUITEMINFOW);
917 miiSrc.fMask = MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_CHECKMARKS | MIIM_TYPE | MIIM_DATA;
919 /* We need to reset this every time through the loop in case menus DON'T have IDs*/
920 miiSrc.fType = MFT_STRING;
921 miiSrc.dwTypeData = szName;
922 miiSrc.dwItemData = 0;
923 miiSrc.cch = ARRAY_SIZE(szName);
925 if (!GetMenuItemInfoW(hmSrc, nItem, TRUE, &miiSrc))
927 continue;
930 /* TRACE("found menu=0x%04x %s id=0x%04x mask=0x%08x smenu=0x%04x\n", hmSrc, debugstr_a(miiSrc.dwTypeData), miiSrc.wID, miiSrc.fMask, miiSrc.hSubMenu);
932 if (miiSrc.fType & MFT_SEPARATOR)
934 /* This is a separator; don't put two of them in a row */
935 if (bAlreadySeparated)
936 continue;
938 bAlreadySeparated = TRUE;
940 else if (miiSrc.hSubMenu)
942 if (uFlags & MM_SUBMENUSHAVEIDS)
944 miiSrc.wID += uIDAdjust; /* add uIDAdjust to the ID */
946 if (miiSrc.wID > uIDAdjustMax) /* skip IDs higher than uIDAdjustMax */
947 continue;
949 if (uIDMax <= miiSrc.wID) /* remember the highest ID */
950 uIDMax = miiSrc.wID + 1;
952 else
954 miiSrc.fMask &= ~MIIM_ID; /* Don't set IDs for submenus that didn't have them already */
956 hmSubMenu = miiSrc.hSubMenu;
958 miiSrc.hSubMenu = CreatePopupMenu();
960 if (!miiSrc.hSubMenu) return(uIDMax);
962 uTemp = Shell_MergeMenus(miiSrc.hSubMenu, hmSubMenu, 0, uIDAdjust, uIDAdjustMax, uFlags & MM_SUBMENUSHAVEIDS);
964 if (uIDMax <= uTemp)
965 uIDMax = uTemp;
967 bAlreadySeparated = FALSE;
969 else /* normal menu item */
971 miiSrc.wID += uIDAdjust; /* add uIDAdjust to the ID */
973 if (miiSrc.wID > uIDAdjustMax) /* skip IDs higher than uIDAdjustMax */
974 continue;
976 if (uIDMax <= miiSrc.wID) /* remember the highest ID */
977 uIDMax = miiSrc.wID + 1;
979 bAlreadySeparated = FALSE;
982 /* TRACE("inserting menu=0x%04x %s id=0x%04x mask=0x%08x smenu=0x%04x\n", hmDst, debugstr_a(miiSrc.dwTypeData), miiSrc.wID, miiSrc.fMask, miiSrc.hSubMenu);
984 if (!InsertMenuItemW(hmDst, uInsert, TRUE, &miiSrc))
986 return(uIDMax);
990 /* Ensure the correct number of separators at the beginning of the
991 inserted menu items*/
992 if (uInsert == 0)
994 if (bAlreadySeparated)
996 DeleteMenu(hmDst, uInsert, MF_BYPOSITION);
999 else
1001 if (_SHIsMenuSeparator(hmDst, uInsert-1))
1003 if (bAlreadySeparated)
1005 DeleteMenu(hmDst, uInsert, MF_BYPOSITION);
1008 else
1010 if ((uFlags & MM_ADDSEPARATOR) && !bAlreadySeparated)
1012 /* Add a separator between the menus*/
1013 InsertMenuW(hmDst, uInsert, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
1017 return(uIDMax);
1020 typedef struct
1022 IContextMenu3 IContextMenu3_iface;
1023 IContextMenu **menus;
1024 UINT *offsets;
1025 UINT menu_count;
1026 ULONG refCount;
1027 }CompositeCMenu;
1029 static const IContextMenu3Vtbl CompositeCMenuVtbl;
1031 static CompositeCMenu* impl_from_IContextMenu3(IContextMenu3* iface)
1033 return CONTAINING_RECORD(iface, CompositeCMenu, IContextMenu3_iface);
1036 static HRESULT CompositeCMenu_Constructor(IContextMenu **menus,UINT menu_count, REFIID riid, void **ppv)
1038 CompositeCMenu *ret;
1039 UINT i;
1041 TRACE("(%p,%u,%s,%p)\n",menus,menu_count,shdebugstr_guid(riid),ppv);
1043 ret = malloc(sizeof(*ret));
1044 if(!ret)
1045 return E_OUTOFMEMORY;
1046 ret->IContextMenu3_iface.lpVtbl = &CompositeCMenuVtbl;
1047 ret->menu_count = menu_count;
1048 ret->menus = malloc(menu_count * sizeof(IContextMenu*));
1049 if(!ret->menus)
1051 free(ret);
1052 return E_OUTOFMEMORY;
1054 ret->offsets = calloc(menu_count, sizeof(UINT));
1055 if(!ret->offsets)
1057 free(ret->menus);
1058 free(ret);
1059 return E_OUTOFMEMORY;
1061 ret->refCount=0;
1062 memcpy(ret->menus,menus,menu_count*sizeof(IContextMenu*));
1063 for(i=0;i<menu_count;i++)
1064 IContextMenu_AddRef(menus[i]);
1065 return IContextMenu3_QueryInterface(&(ret->IContextMenu3_iface),riid,ppv);
1068 static void CompositeCMenu_Destroy(CompositeCMenu *This)
1070 UINT i;
1071 for(i=0;i<This->menu_count;i++)
1072 IContextMenu_Release(This->menus[i]);
1073 free(This->menus);
1074 free(This->offsets);
1075 free(This);
1078 static HRESULT WINAPI CompositeCMenu_QueryInterface(IContextMenu3 *iface, REFIID riid, void **ppv)
1080 TRACE("(%p)->(%s,%p)\n",iface,shdebugstr_guid(riid),ppv);
1081 if(!ppv)
1082 return E_INVALIDARG;
1083 if(IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IContextMenu) ||
1084 IsEqualIID(riid,&IID_IContextMenu2) || IsEqualIID(riid,&IID_IContextMenu3))
1085 *ppv=iface;
1086 else
1087 return E_NOINTERFACE;
1088 IContextMenu3_AddRef(iface);
1089 return S_OK;
1092 static ULONG WINAPI CompositeCMenu_AddRef(IContextMenu3 *iface)
1094 CompositeCMenu *This = impl_from_IContextMenu3(iface);
1095 TRACE("(%p)->()\n",iface);
1096 return ++This->refCount;
1099 static ULONG WINAPI CompositeCMenu_Release(IContextMenu3 *iface)
1101 CompositeCMenu *This = impl_from_IContextMenu3(iface);
1102 TRACE("(%p)->()\n",iface);
1103 if(--This->refCount)
1104 return This->refCount;
1105 CompositeCMenu_Destroy(This);
1106 return 0;
1109 static UINT CompositeCMenu_GetIndexForCommandId(CompositeCMenu *This,UINT id)
1111 UINT low=0;
1112 UINT high=This->menu_count;
1113 while(high-low!=1)
1115 UINT i=(high+low)/2;
1116 if(This->offsets[i]<=id)
1117 low=i;
1118 else
1119 high=i;
1121 return low;
1124 static HRESULT WINAPI CompositeCMenu_GetCommandString(IContextMenu3* iface, UINT_PTR idCmd, UINT uFlags, UINT *pwReserved, LPSTR pszName, UINT cchMax)
1126 CompositeCMenu *This = impl_from_IContextMenu3(iface);
1127 UINT index = CompositeCMenu_GetIndexForCommandId(This,idCmd);
1128 TRACE("(%p)->(%Ix,%x,%p,%s,%u)\n",iface,idCmd,uFlags,pwReserved,pszName,cchMax);
1129 return IContextMenu_GetCommandString(This->menus[index],idCmd,uFlags,pwReserved,pszName,cchMax);
1132 static HRESULT WINAPI CompositeCMenu_InvokeCommand(IContextMenu3* iface,LPCMINVOKECOMMANDINFO pici)
1134 CompositeCMenu *This = impl_from_IContextMenu3(iface);
1136 TRACE("(%p)->(%p)\n", iface, pici);
1138 if (IS_INTRESOURCE(pici->lpVerb))
1140 UINT id = (UINT_PTR)pici->lpVerb;
1141 UINT index = CompositeCMenu_GetIndexForCommandId(This, id);
1142 return IContextMenu_InvokeCommand(This->menus[index], pici);
1144 else
1146 /*call each handler until one of them succeeds*/
1147 UINT i;
1149 for (i = 0; i < This->menu_count; i++)
1151 HRESULT hres;
1152 if (SUCCEEDED(hres = IContextMenu_InvokeCommand(This->menus[i], pici)))
1153 return hres;
1155 return E_FAIL;
1159 static HRESULT WINAPI CompositeCMenu_QueryContextMenu(IContextMenu3 *iface, HMENU hmenu,UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
1161 CompositeCMenu *This = impl_from_IContextMenu3(iface);
1162 UINT i=0;
1163 UINT id_offset=idCmdFirst;
1164 TRACE("(%p)->(%p,%u,%u,%u,%x)\n",iface,hmenu,indexMenu,idCmdFirst,idCmdLast,uFlags);
1165 for(;i<This->menu_count;i++)
1167 HRESULT hres;
1168 This->offsets[i]=id_offset;
1169 hres = IContextMenu_QueryContextMenu(This->menus[i],hmenu,indexMenu,id_offset,idCmdLast,uFlags);
1170 if(SUCCEEDED(hres))
1171 id_offset+=hres;
1173 return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, id_offset-idCmdFirst);
1176 static HRESULT WINAPI CompositeCMenu_HandleMenuMsg(IContextMenu3 *iface, UINT uMsg, WPARAM wParam, LPARAM lParam)
1178 CompositeCMenu *This = impl_from_IContextMenu3(iface);
1179 HMENU menu;
1180 UINT id;
1181 UINT index;
1182 IContextMenu2 *handler;
1183 HRESULT hres;
1184 TRACE("(%p)->(%x,%Ix,%Ix)\n",iface,uMsg,wParam,lParam);
1185 switch(uMsg)
1187 case WM_INITMENUPOPUP:
1188 menu = (HMENU)wParam;
1189 id = GetMenuItemID(menu,LOWORD(lParam));
1190 break;
1191 case WM_DRAWITEM:
1192 id = ((DRAWITEMSTRUCT*)lParam)->itemID;
1193 break;
1194 case WM_MEASUREITEM:
1195 id = ((MEASUREITEMSTRUCT*)lParam)->itemID;
1196 break;
1197 default:
1198 WARN("Unimplemented uMsg: 0x%x\n",uMsg);
1199 return E_NOTIMPL;
1201 index = CompositeCMenu_GetIndexForCommandId(This,id);
1202 hres = IContextMenu_QueryInterface(This->menus[index],&IID_IContextMenu2,
1203 (void**)&handler);
1204 if(SUCCEEDED(hres))
1205 return IContextMenu2_HandleMenuMsg(handler,uMsg,wParam,lParam);
1206 return S_OK;
1209 static HRESULT WINAPI CompositeCMenu_HandleMenuMsg2(IContextMenu3 *iface, UINT uMsg, WPARAM wParam, LPARAM lParam,LRESULT *plResult)
1211 CompositeCMenu *This = impl_from_IContextMenu3(iface);
1212 HMENU menu;
1213 UINT id;
1214 UINT index;
1215 IContextMenu3 *handler;
1216 HRESULT hres;
1217 LRESULT lres;
1218 TRACE("(%p)->(%x,%Ix,%Ix,%p)\n",iface,uMsg,wParam,lParam,plResult);
1219 if(!plResult)
1220 plResult=&lres;
1221 switch(uMsg)
1223 case WM_INITMENUPOPUP:
1224 menu = (HMENU)wParam;
1225 id = GetMenuItemID(menu,LOWORD(lParam));
1226 break;
1227 case WM_DRAWITEM:
1228 id = ((DRAWITEMSTRUCT*)lParam)->itemID;
1229 break;
1230 case WM_MEASUREITEM:
1231 id = ((MEASUREITEMSTRUCT*)lParam)->itemID;
1232 break;
1233 case WM_MENUCHAR:
1235 UINT i=0;
1236 for(;i<This->menu_count;i++)
1238 hres = IContextMenu_QueryInterface(This->menus[i],&IID_IContextMenu3,(void**)&handler);
1239 if(SUCCEEDED(hres))
1241 hres = IContextMenu3_HandleMenuMsg2(handler,uMsg,wParam,lParam,plResult);
1242 if(SUCCEEDED(hres) && HIWORD(*plResult))
1243 return hres;
1247 default:
1248 WARN("Unimplemented uMsg: 0x%x\n",uMsg);
1249 return E_NOTIMPL;
1251 index = CompositeCMenu_GetIndexForCommandId(This,id);
1252 hres = IContextMenu_QueryInterface(This->menus[index],&IID_IContextMenu3,(void**)&handler);
1253 if(SUCCEEDED(hres))
1254 return IContextMenu3_HandleMenuMsg2(handler,uMsg,wParam,lParam,plResult);
1255 return S_OK;
1258 static const IContextMenu3Vtbl CompositeCMenuVtbl=
1260 CompositeCMenu_QueryInterface,
1261 CompositeCMenu_AddRef,
1262 CompositeCMenu_Release,
1263 CompositeCMenu_QueryContextMenu,
1264 CompositeCMenu_InvokeCommand,
1265 CompositeCMenu_GetCommandString,
1266 CompositeCMenu_HandleMenuMsg,
1267 CompositeCMenu_HandleMenuMsg2
1270 static HRESULT SHELL_CreateContextMenu(HWND hwnd, IContextMenu* system_menu,
1271 IShellFolder *folder, LPCITEMIDLIST folder_pidl,
1272 LPCITEMIDLIST *apidl, UINT cidl, const HKEY *aKeys,
1273 UINT cKeys,REFIID riid, void** ppv)
1275 HRESULT ret;
1276 TRACE("(%p,%p,%p,%p,%p,%u,%p,%u,%s,%p)\n",hwnd,system_menu,folder,folder_pidl,apidl,cidl,aKeys,cKeys,shdebugstr_guid(riid),ppv);
1277 ret = CompositeCMenu_Constructor(&system_menu,1,riid,ppv);
1278 return ret;
1281 HRESULT WINAPI CDefFolderMenu_Create2(LPCITEMIDLIST pidlFolder, HWND hwnd, UINT cidl,
1282 LPCITEMIDLIST *apidl, IShellFolder *psf,
1283 LPFNDFMCALLBACK lpfn, UINT nKeys, const HKEY *ahkeys,
1284 IContextMenu **ppcm)
1286 IContextMenu *system_menu;
1287 HRESULT hres;
1288 LPITEMIDLIST folder_pidl;
1289 TRACE("(%p,%p,%u,%p,%p,%u,%p,%p)\n",pidlFolder,hwnd,cidl,apidl,psf,nKeys,ahkeys,ppcm);
1290 if(!pidlFolder)
1292 IPersistFolder2 *persist;
1293 IShellFolder_QueryInterface(psf,&IID_IPersistFolder2,(void**)&persist);
1294 IPersistFolder2_GetCurFolder(persist,&folder_pidl);
1295 IPersistFolder2_Release(persist);
1297 else
1298 folder_pidl=ILClone(pidlFolder);
1300 ItemMenu_Constructor(psf, folder_pidl, (const LPCITEMIDLIST*)apidl, cidl, &IID_IContextMenu, (void**)&system_menu);
1301 hres= SHELL_CreateContextMenu(hwnd,system_menu,psf,folder_pidl,apidl,cidl,ahkeys,nKeys,&IID_IContextMenu,(void**)ppcm);
1302 IContextMenu_Release(system_menu);
1303 ILFree(folder_pidl);
1304 return hres;
1307 HRESULT WINAPI SHCreateDefaultContextMenu(const DEFCONTEXTMENU *pdcm, REFIID riid, void **ppv)
1309 IShellFolder *folder=pdcm->psf;
1310 LPITEMIDLIST folder_pidl;
1311 HRESULT ret;
1312 IContextMenu *system_menu;
1313 TRACE("(%p,%s,%p)\n",pdcm,shdebugstr_guid(riid),ppv);
1314 if(!pdcm->pidlFolder)
1316 IPersistFolder2 *persist;
1317 IShellFolder_QueryInterface(folder,&IID_IPersistFolder2,(void**)&persist);
1318 IPersistFolder2_GetCurFolder(persist,&folder_pidl);
1319 IPersistFolder2_Release(persist);
1321 else
1322 folder_pidl=ILClone(pdcm->pidlFolder);
1323 if(pdcm->cKeys==0)
1324 FIXME("Loading shell extensions using IQueryAssociations not yet supported\n");
1326 ItemMenu_Constructor(folder, folder_pidl, (const LPCITEMIDLIST*)pdcm->apidl, pdcm->cidl, &IID_IContextMenu, (void**)&system_menu);
1327 ret = SHELL_CreateContextMenu(pdcm->hwnd,system_menu,folder,folder_pidl,(LPCITEMIDLIST*)pdcm->apidl,pdcm->cidl,pdcm->aKeys,pdcm->cKeys,riid,ppv);
1328 IContextMenu_Release(system_menu);
1329 ILFree(folder_pidl);
1330 return ret;