kernel32: Invalidate local copy of console input event handle while reallocating...
[wine] / dlls / shell32 / shlmenu.c
1 /*
2  * see www.geocities.com/SiliconValley/4942/filemenu.html
3  *
4  * Copyright 1999, 2000 Juergen Schmied
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "shlobj.h"
32 #include "undocshell.h"
33 #include "shlwapi.h"
34 #include "shell32_main.h"
35
36 #include "pidl.h"
37 #include "wine/debug.h"
38
39 #ifdef FM_SEPARATOR
40 #undef FM_SEPARATOR
41 #endif
42 #define FM_SEPARATOR (LPCWSTR)1
43
44 static BOOL FileMenu_AppendItemW(HMENU hMenu, LPCWSTR lpText, UINT uID, int icon,
45                                  HMENU hMenuPopup, int nItemHeight);
46
47 typedef struct
48 {
49         BOOL            bInitialized;
50         BOOL            bFixedItems;
51         /* create */
52         COLORREF        crBorderColor;
53         int             nBorderWidth;
54         HBITMAP         hBorderBmp;
55
56         /* insert using pidl */
57         LPITEMIDLIST    pidl;
58         UINT            uID;
59         UINT            uFlags;
60         UINT            uEnumFlags;
61         LPFNFMCALLBACK lpfnCallback;
62 } FMINFO, *LPFMINFO;
63
64 typedef struct
65 {       int     cchItemText;
66         int     iIconIndex;
67         HMENU   hMenu;
68         WCHAR   szItemText[1];
69 } FMITEM, * LPFMITEM;
70
71 static BOOL bAbortInit;
72
73 #define CCH_MAXITEMTEXT 256
74
75 WINE_DEFAULT_DEBUG_CHANNEL(shell);
76
77 static LPFMINFO FM_GetMenuInfo(HMENU hmenu)
78 {
79         MENUINFO        MenuInfo;
80         LPFMINFO        menudata;
81
82         MenuInfo.cbSize = sizeof(MENUINFO);
83         MenuInfo.fMask = MIM_MENUDATA;
84
85         if (! GetMenuInfo(hmenu, &MenuInfo))
86           return NULL;
87
88         menudata = (LPFMINFO)MenuInfo.dwMenuData;
89
90         if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
91         {
92           ERR("menudata corrupt: %p %u\n", menudata, MenuInfo.cbSize);
93           return 0;
94         }
95
96         return menudata;
97
98 }
99 /*************************************************************************
100  * FM_SetMenuParameter                          [internal]
101  *
102  */
103 static LPFMINFO FM_SetMenuParameter(
104         HMENU hmenu,
105         UINT uID,
106         LPCITEMIDLIST pidl,
107         UINT uFlags,
108         UINT uEnumFlags,
109         LPFNFMCALLBACK lpfnCallback)
110 {
111         LPFMINFO        menudata;
112
113         TRACE("\n");
114
115         menudata = FM_GetMenuInfo(hmenu);
116
117         SHFree(menudata->pidl);
118
119         menudata->uID = uID;
120         menudata->pidl = ILClone(pidl);
121         menudata->uFlags = uFlags;
122         menudata->uEnumFlags = uEnumFlags;
123         menudata->lpfnCallback = lpfnCallback;
124
125         return menudata;
126 }
127
128 /*************************************************************************
129  * FM_InitMenuPopup                             [internal]
130  *
131  */
132 static int FM_InitMenuPopup(HMENU hmenu, LPCITEMIDLIST pAlternatePidl)
133 {       IShellFolder    *lpsf, *lpsf2;
134         ULONG           ulItemAttr = SFGAO_FOLDER;
135         UINT            uID, uFlags, uEnumFlags;
136         LPFNFMCALLBACK  lpfnCallback;
137         LPCITEMIDLIST   pidl;
138         WCHAR           sTemp[MAX_PATH];
139         int             NumberOfItems = 0, iIcon;
140         MENUINFO        MenuInfo;
141         LPFMINFO        menudata;
142
143         TRACE("%p %p\n", hmenu, pAlternatePidl);
144
145         MenuInfo.cbSize = sizeof(MENUINFO);
146         MenuInfo.fMask = MIM_MENUDATA;
147
148         if (! GetMenuInfo(hmenu, &MenuInfo))
149           return FALSE;
150
151         menudata = (LPFMINFO)MenuInfo.dwMenuData;
152
153         if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
154         {
155           ERR("menudata corrupt: %p %u\n", menudata, MenuInfo.cbSize);
156           return 0;
157         }
158
159         if (menudata->bInitialized)
160           return 0;
161
162         pidl = (pAlternatePidl? pAlternatePidl: menudata->pidl);
163         if (!pidl)
164           return 0;
165
166         uID = menudata->uID;
167         uFlags = menudata->uFlags;
168         uEnumFlags = menudata->uEnumFlags;
169         lpfnCallback = menudata->lpfnCallback;
170         menudata->bInitialized = FALSE;
171
172         SetMenuInfo(hmenu, &MenuInfo);
173
174         if (SUCCEEDED (SHGetDesktopFolder(&lpsf)))
175         {
176           if (SUCCEEDED(IShellFolder_BindToObject(lpsf, pidl,0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf2)))
177           {
178             IEnumIDList *lpe = NULL;
179
180             if (SUCCEEDED (IShellFolder_EnumObjects(lpsf2, 0, uEnumFlags, &lpe )))
181             {
182
183               LPITEMIDLIST pidlTemp = NULL;
184               ULONG ulFetched;
185
186               while ((!bAbortInit) && (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched)))
187               {
188                 if (SUCCEEDED (IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulItemAttr)))
189                 {
190                   ILGetDisplayNameExW(NULL, pidlTemp, sTemp, ILGDN_FORPARSING);
191                   if (! (PidlToSicIndex(lpsf, pidlTemp, FALSE, 0, &iIcon)))
192                     iIcon = FM_BLANK_ICON;
193                   if ( SFGAO_FOLDER & ulItemAttr)
194                   {
195                     LPFMINFO lpFmMi;
196                     MENUINFO MenuInfo;
197                     HMENU hMenuPopup = CreatePopupMenu();
198
199                     lpFmMi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FMINFO));
200
201                     lpFmMi->pidl = ILCombine(pidl, pidlTemp);
202                     lpFmMi->uEnumFlags = SHCONTF_FOLDERS | SHCONTF_NONFOLDERS;
203
204                     MenuInfo.cbSize = sizeof(MENUINFO);
205                     MenuInfo.fMask = MIM_MENUDATA;
206                     MenuInfo.dwMenuData = (ULONG_PTR) lpFmMi;
207                     SetMenuInfo (hMenuPopup, &MenuInfo);
208
209                     FileMenu_AppendItemW (hmenu, sTemp, uID, iIcon, hMenuPopup, FM_DEFAULT_HEIGHT);
210                   }
211                   else
212                   {
213                     LPWSTR pExt = PathFindExtensionW(sTemp);
214                     if (pExt)
215                       *pExt = 0;
216                     FileMenu_AppendItemW (hmenu, sTemp, uID, iIcon, 0, FM_DEFAULT_HEIGHT);
217                   }
218                 }
219
220                 if (lpfnCallback)
221                 {
222                   TRACE("enter callback\n");
223                   lpfnCallback ( pidl, pidlTemp);
224                   TRACE("leave callback\n");
225                 }
226
227                 NumberOfItems++;
228               }
229               IEnumIDList_Release (lpe);
230             }
231             IShellFolder_Release(lpsf2);
232           }
233           IShellFolder_Release(lpsf);
234         }
235
236         if ( GetMenuItemCount (hmenu) == 0 )
237         {
238           static const WCHAR szEmpty[] = { '(','e','m','p','t','y',')',0 };
239           FileMenu_AppendItemW (hmenu, szEmpty, uID, FM_BLANK_ICON, 0, FM_DEFAULT_HEIGHT);
240           NumberOfItems++;
241         }
242
243         menudata->bInitialized = TRUE;
244         SetMenuInfo(hmenu, &MenuInfo);
245
246         return NumberOfItems;
247 }
248 /*************************************************************************
249  * FileMenu_Create                              [SHELL32.114]
250  *
251  * NOTES
252  *  for non-root menus values are
253  *  (ffffffff,00000000,00000000,00000000,00000000)
254  */
255 HMENU WINAPI FileMenu_Create (
256         COLORREF crBorderColor,
257         int nBorderWidth,
258         HBITMAP hBorderBmp,
259         int nSelHeight,
260         UINT uFlags)
261 {
262         MENUINFO        MenuInfo;
263         LPFMINFO        menudata;
264
265         HMENU hMenu = CreatePopupMenu();
266
267         TRACE("0x%08x 0x%08x %p 0x%08x 0x%08x  hMenu=%p\n",
268         crBorderColor, nBorderWidth, hBorderBmp, nSelHeight, uFlags, hMenu);
269
270         menudata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FMINFO));
271         menudata->crBorderColor = crBorderColor;
272         menudata->nBorderWidth = nBorderWidth;
273         menudata->hBorderBmp = hBorderBmp;
274
275         MenuInfo.cbSize = sizeof(MENUINFO);
276         MenuInfo.fMask = MIM_MENUDATA;
277         MenuInfo.dwMenuData = (ULONG_PTR) menudata;
278         SetMenuInfo (hMenu, &MenuInfo);
279
280         return hMenu;
281 }
282
283 /*************************************************************************
284  * FileMenu_Destroy                             [SHELL32.118]
285  *
286  * NOTES
287  *  exported by name
288  */
289 void WINAPI FileMenu_Destroy (HMENU hmenu)
290 {
291         LPFMINFO        menudata;
292
293         TRACE("%p\n", hmenu);
294
295         FileMenu_DeleteAllItems (hmenu);
296
297         menudata = FM_GetMenuInfo(hmenu);
298
299         SHFree( menudata->pidl);
300         HeapFree(GetProcessHeap(), 0, menudata);
301
302         DestroyMenu (hmenu);
303 }
304
305 /*************************************************************************
306  * FileMenu_AppendItem                  [SHELL32.115]
307  *
308  */
309 static BOOL FileMenu_AppendItemW(
310         HMENU hMenu,
311         LPCWSTR lpText,
312         UINT uID,
313         int icon,
314         HMENU hMenuPopup,
315         int nItemHeight)
316 {
317         MENUITEMINFOW   mii;
318         LPFMITEM        myItem;
319         LPFMINFO        menudata;
320         MENUINFO        MenuInfo;
321
322
323         TRACE("%p %s 0x%08x 0x%08x %p 0x%08x\n",
324           hMenu, (lpText!=FM_SEPARATOR) ? debugstr_w(lpText) : NULL,
325           uID, icon, hMenuPopup, nItemHeight);
326
327         ZeroMemory (&mii, sizeof(MENUITEMINFOW));
328
329         mii.cbSize = sizeof(MENUITEMINFOW);
330
331         if (lpText != FM_SEPARATOR)
332         {
333           int len = strlenW (lpText);
334           myItem = (LPFMITEM) SHAlloc( sizeof(FMITEM) + len*sizeof(WCHAR));
335           strcpyW (myItem->szItemText, lpText);
336           myItem->cchItemText = len;
337           myItem->iIconIndex = icon;
338           myItem->hMenu = hMenu;
339           mii.fMask = MIIM_DATA;
340           mii.dwItemData = (ULONG_PTR) myItem;
341         }
342
343         if ( hMenuPopup )
344         { /* sub menu */
345           mii.fMask |= MIIM_TYPE | MIIM_SUBMENU;
346           mii.fType = MFT_OWNERDRAW;
347           mii.hSubMenu = hMenuPopup;
348         }
349         else if (lpText == FM_SEPARATOR )
350         { mii.fMask |= MIIM_ID | MIIM_TYPE;
351           mii.fType = MFT_SEPARATOR;
352         }
353         else
354         { /* normal item */
355           mii.fMask |= MIIM_ID | MIIM_TYPE | MIIM_STATE;
356           mii.fState = MFS_ENABLED | MFS_DEFAULT;
357           mii.fType = MFT_OWNERDRAW;
358         }
359         mii.wID = uID;
360
361         InsertMenuItemW (hMenu, (UINT)-1, TRUE, &mii);
362
363         /* set bFixedItems to true */
364         MenuInfo.cbSize = sizeof(MENUINFO);
365         MenuInfo.fMask = MIM_MENUDATA;
366
367         if (! GetMenuInfo(hMenu, &MenuInfo))
368           return FALSE;
369
370         menudata = (LPFMINFO)MenuInfo.dwMenuData;
371         if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
372         {
373           ERR("menudata corrupt: %p %u\n", menudata, MenuInfo.cbSize);
374           return 0;
375         }
376
377         menudata->bFixedItems = TRUE;
378         SetMenuInfo(hMenu, &MenuInfo);
379
380         return TRUE;
381
382 }
383
384 /**********************************************************************/
385
386 BOOL WINAPI FileMenu_AppendItemAW(
387         HMENU hMenu,
388         LPCVOID lpText,
389         UINT uID,
390         int icon,
391         HMENU hMenuPopup,
392         int nItemHeight)
393 {
394         BOOL ret;
395
396         if ((SHELL_OsIsUnicode() && (lpText!=FM_SEPARATOR)) || (lpText == NULL))
397           ret = FileMenu_AppendItemW(hMenu, lpText, uID, icon, hMenuPopup, nItemHeight);
398         else
399         {
400           DWORD len = MultiByteToWideChar( CP_ACP, 0, lpText, -1, NULL, 0 );
401           LPWSTR lpszText = HeapAlloc ( GetProcessHeap(), 0, len*sizeof(WCHAR) );
402           MultiByteToWideChar( CP_ACP, 0, lpText, -1, lpszText, len );
403           ret = FileMenu_AppendItemW(hMenu, lpszText, uID, icon, hMenuPopup, nItemHeight);
404           HeapFree( GetProcessHeap(), 0, lpszText );
405         }
406
407         return ret;
408 }
409
410 /*************************************************************************
411  * FileMenu_InsertUsingPidl                     [SHELL32.110]
412  *
413  * NOTES
414  *      uEnumFlags      any SHCONTF flag
415  */
416 int WINAPI FileMenu_InsertUsingPidl (
417         HMENU hmenu,
418         UINT uID,
419         LPCITEMIDLIST pidl,
420         UINT uFlags,
421         UINT uEnumFlags,
422         LPFNFMCALLBACK lpfnCallback)
423 {
424         TRACE("%p 0x%08x %p 0x%08x 0x%08x %p\n",
425         hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
426
427         pdump (pidl);
428
429         bAbortInit = FALSE;
430
431         FM_SetMenuParameter(hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
432
433         return FM_InitMenuPopup(hmenu, NULL);
434 }
435
436 /*************************************************************************
437  * FileMenu_ReplaceUsingPidl                    [SHELL32.113]
438  *
439  * FIXME: the static items are deleted but won't be refreshed
440  */
441 int WINAPI FileMenu_ReplaceUsingPidl(
442         HMENU   hmenu,
443         UINT    uID,
444         LPCITEMIDLIST   pidl,
445         UINT    uEnumFlags,
446         LPFNFMCALLBACK lpfnCallback)
447 {
448         TRACE("%p 0x%08x %p 0x%08x %p\n",
449         hmenu, uID, pidl, uEnumFlags, lpfnCallback);
450
451         FileMenu_DeleteAllItems (hmenu);
452
453         FM_SetMenuParameter(hmenu, uID, pidl, 0, uEnumFlags, lpfnCallback);
454
455         return FM_InitMenuPopup(hmenu, NULL);
456 }
457
458 /*************************************************************************
459  * FileMenu_Invalidate                  [SHELL32.111]
460  */
461 void WINAPI FileMenu_Invalidate (HMENU hMenu)
462 {
463         FIXME("%p\n",hMenu);
464 }
465
466 /*************************************************************************
467  * FileMenu_FindSubMenuByPidl                   [SHELL32.106]
468  */
469 HMENU WINAPI FileMenu_FindSubMenuByPidl(
470         HMENU   hMenu,
471         LPCITEMIDLIST   pidl)
472 {
473         FIXME("%p %p\n",hMenu, pidl);
474         return 0;
475 }
476
477 /*************************************************************************
478  * FileMenu_AppendFilesForPidl                  [SHELL32.124]
479  */
480 int WINAPI FileMenu_AppendFilesForPidl(
481         HMENU   hmenu,
482         LPCITEMIDLIST   pidl,
483         BOOL    bAddSeparator)
484 {
485         LPFMINFO        menudata;
486
487         menudata = FM_GetMenuInfo(hmenu);
488
489         menudata->bInitialized = FALSE;
490
491         FM_InitMenuPopup(hmenu, pidl);
492
493         if (bAddSeparator)
494           FileMenu_AppendItemW (hmenu, FM_SEPARATOR, 0, 0, 0, FM_DEFAULT_HEIGHT);
495
496         TRACE("%p %p 0x%08x\n",hmenu, pidl,bAddSeparator);
497
498         return 0;
499 }
500 /*************************************************************************
501  * FileMenu_AddFilesForPidl                     [SHELL32.125]
502  *
503  * NOTES
504  *      uEnumFlags      any SHCONTF flag
505  */
506 int WINAPI FileMenu_AddFilesForPidl (
507         HMENU   hmenu,
508         UINT    uReserved,
509         UINT    uID,
510         LPCITEMIDLIST   pidl,
511         UINT    uFlags,
512         UINT    uEnumFlags,
513         LPFNFMCALLBACK  lpfnCallback)
514 {
515         TRACE("%p 0x%08x 0x%08x %p 0x%08x 0x%08x %p\n",
516         hmenu, uReserved, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
517
518         return FileMenu_InsertUsingPidl ( hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
519
520 }
521
522
523 /*************************************************************************
524  * FileMenu_TrackPopupMenuEx                    [SHELL32.116]
525  */
526 BOOL WINAPI FileMenu_TrackPopupMenuEx (
527         HMENU hMenu,
528         UINT uFlags,
529         int x,
530         int y,
531         HWND hWnd,
532         LPTPMPARAMS lptpm)
533 {
534         TRACE("%p 0x%08x 0x%x 0x%x %p %p\n",
535         hMenu, uFlags, x, y, hWnd, lptpm);
536         return TrackPopupMenuEx(hMenu, uFlags, x, y, hWnd, lptpm);
537 }
538
539 /*************************************************************************
540  * FileMenu_GetLastSelectedItemPidls            [SHELL32.107]
541  */
542 BOOL WINAPI FileMenu_GetLastSelectedItemPidls(
543         UINT    uReserved,
544         LPCITEMIDLIST   *ppidlFolder,
545         LPCITEMIDLIST   *ppidlItem)
546 {
547         FIXME("0x%08x %p %p\n",uReserved, ppidlFolder, ppidlItem);
548         return 0;
549 }
550
551 #define FM_ICON_SIZE    16
552 #define FM_Y_SPACE      4
553 #define FM_SPACE1       4
554 #define FM_SPACE2       2
555 #define FM_LEFTBORDER   2
556 #define FM_RIGHTBORDER  8
557 /*************************************************************************
558  * FileMenu_MeasureItem                         [SHELL32.112]
559  */
560 LRESULT WINAPI FileMenu_MeasureItem(
561         HWND    hWnd,
562         LPMEASUREITEMSTRUCT     lpmis)
563 {
564         LPFMITEM pMyItem = (LPFMITEM)(lpmis->itemData);
565         HDC hdc = GetDC(hWnd);
566         SIZE size;
567         LPFMINFO menuinfo;
568
569         TRACE("%p %p %s\n", hWnd, lpmis, debugstr_w(pMyItem->szItemText));
570
571         GetTextExtentPoint32W(hdc, pMyItem->szItemText, pMyItem->cchItemText, &size);
572
573         lpmis->itemWidth = size.cx + FM_LEFTBORDER + FM_ICON_SIZE + FM_SPACE1 + FM_SPACE2 + FM_RIGHTBORDER;
574         lpmis->itemHeight = (size.cy > (FM_ICON_SIZE + FM_Y_SPACE)) ? size.cy : (FM_ICON_SIZE + FM_Y_SPACE);
575
576         /* add the menubitmap */
577         menuinfo = FM_GetMenuInfo(pMyItem->hMenu);
578         if (menuinfo->nBorderWidth)
579           lpmis->itemWidth += menuinfo->nBorderWidth;
580
581         TRACE("-- 0x%04x 0x%04x\n", lpmis->itemWidth, lpmis->itemHeight);
582         ReleaseDC (hWnd, hdc);
583         return 0;
584 }
585 /*************************************************************************
586  * FileMenu_DrawItem                            [SHELL32.105]
587  */
588 LRESULT WINAPI FileMenu_DrawItem(
589         HWND                    hWnd,
590         LPDRAWITEMSTRUCT        lpdis)
591 {
592         LPFMITEM pMyItem = (LPFMITEM)(lpdis->itemData);
593         COLORREF clrPrevText, clrPrevBkgnd;
594         int xi,yi,xt,yt;
595         HIMAGELIST hImageList;
596         RECT TextRect, BorderRect;
597         LPFMINFO menuinfo;
598
599         TRACE("%p %p %s\n", hWnd, lpdis, debugstr_w(pMyItem->szItemText));
600
601         if (lpdis->itemState & ODS_SELECTED)
602         {
603           clrPrevText = SetTextColor(lpdis->hDC, GetSysColor (COLOR_HIGHLIGHTTEXT));
604           clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor (COLOR_HIGHLIGHT));
605         }
606         else
607         {
608           clrPrevText = SetTextColor(lpdis->hDC, GetSysColor (COLOR_MENUTEXT));
609           clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor (COLOR_MENU));
610         }
611
612         CopyRect(&TextRect, &(lpdis->rcItem));
613
614         /* add the menubitmap */
615         menuinfo = FM_GetMenuInfo(pMyItem->hMenu);
616         if (menuinfo->nBorderWidth)
617           TextRect.left += menuinfo->nBorderWidth;
618
619         BorderRect.right = menuinfo->nBorderWidth;
620 /*      FillRect(lpdis->hDC, &BorderRect, CreateSolidBrush( menuinfo->crBorderColor));
621 */
622         TextRect.left += FM_LEFTBORDER;
623         xi = TextRect.left + FM_SPACE1;
624         yi = TextRect.top + FM_Y_SPACE/2;
625         TextRect.bottom -= FM_Y_SPACE/2;
626
627         xt = xi + FM_ICON_SIZE + FM_SPACE2;
628         yt = yi;
629
630         ExtTextOutW (lpdis->hDC, xt , yt, ETO_OPAQUE, &TextRect, pMyItem->szItemText, pMyItem->cchItemText, NULL);
631
632         Shell_GetImageList(0, &hImageList);
633         ImageList_Draw(hImageList, pMyItem->iIconIndex, lpdis->hDC, xi, yi, ILD_NORMAL);
634
635         TRACE("-- 0x%04x 0x%04x 0x%04x 0x%04x\n", TextRect.left, TextRect.top, TextRect.right, TextRect.bottom);
636
637         SetTextColor(lpdis->hDC, clrPrevText);
638         SetBkColor(lpdis->hDC, clrPrevBkgnd);
639
640         return TRUE;
641 }
642
643 /*************************************************************************
644  * FileMenu_InitMenuPopup                       [SHELL32.109]
645  *
646  * NOTES
647  *  The filemenu is an ownerdrawn menu. Call this function responding to
648  *  WM_INITPOPUPMENU
649  *
650  */
651 BOOL WINAPI FileMenu_InitMenuPopup (HMENU hmenu)
652 {
653         FM_InitMenuPopup(hmenu, NULL);
654         return TRUE;
655 }
656
657 /*************************************************************************
658  * FileMenu_HandleMenuChar                      [SHELL32.108]
659  */
660 LRESULT WINAPI FileMenu_HandleMenuChar(
661         HMENU   hMenu,
662         WPARAM  wParam)
663 {
664         FIXME("%p 0x%08lx\n",hMenu,wParam);
665         return 0;
666 }
667
668 /*************************************************************************
669  * FileMenu_DeleteAllItems                      [SHELL32.104]
670  *
671  * NOTES
672  *  exported by name
673  */
674 BOOL WINAPI FileMenu_DeleteAllItems (HMENU hmenu)
675 {
676         MENUITEMINFOW   mii;
677         LPFMINFO        menudata;
678
679         int i;
680
681         TRACE("%p\n", hmenu);
682
683         ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
684         mii.cbSize = sizeof(MENUITEMINFOW);
685         mii.fMask = MIIM_SUBMENU|MIIM_DATA;
686
687         for (i = 0; i < GetMenuItemCount( hmenu ); i++)
688         { GetMenuItemInfoW(hmenu, i, TRUE, &mii );
689
690           SHFree((LPFMINFO)mii.dwItemData);
691
692           if (mii.hSubMenu)
693             FileMenu_Destroy(mii.hSubMenu);
694         }
695
696         while (DeleteMenu (hmenu, 0, MF_BYPOSITION)){};
697
698         menudata = FM_GetMenuInfo(hmenu);
699
700         menudata->bInitialized = FALSE;
701
702         return TRUE;
703 }
704
705 /*************************************************************************
706  * FileMenu_DeleteItemByCmd                     [SHELL32.117]
707  *
708  */
709 BOOL WINAPI FileMenu_DeleteItemByCmd (HMENU hMenu, UINT uID)
710 {
711         MENUITEMINFOW mii;
712
713         TRACE("%p 0x%08x\n", hMenu, uID);
714
715         ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
716         mii.cbSize = sizeof(MENUITEMINFOW);
717         mii.fMask = MIIM_SUBMENU;
718
719         GetMenuItemInfoW(hMenu, uID, FALSE, &mii );
720         if ( mii.hSubMenu )
721         {
722           /* FIXME: Do what? */
723         }
724
725         DeleteMenu(hMenu, MF_BYCOMMAND, uID);
726         return TRUE;
727 }
728
729 /*************************************************************************
730  * FileMenu_DeleteItemByIndex                   [SHELL32.140]
731  */
732 BOOL WINAPI FileMenu_DeleteItemByIndex ( HMENU hMenu, UINT uPos)
733 {
734         MENUITEMINFOW mii;
735
736         TRACE("%p 0x%08x\n", hMenu, uPos);
737
738         ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
739         mii.cbSize = sizeof(MENUITEMINFOW);
740         mii.fMask = MIIM_SUBMENU;
741
742         GetMenuItemInfoW(hMenu, uPos, TRUE, &mii );
743         if ( mii.hSubMenu )
744         {
745           /* FIXME: Do what? */
746         }
747
748         DeleteMenu(hMenu, MF_BYPOSITION, uPos);
749         return TRUE;
750 }
751
752 /*************************************************************************
753  * FileMenu_DeleteItemByFirstID                 [SHELL32.141]
754  */
755 BOOL WINAPI FileMenu_DeleteItemByFirstID(
756         HMENU   hMenu,
757         UINT    uID)
758 {
759         TRACE("%p 0x%08x\n", hMenu, uID);
760         return 0;
761 }
762
763 /*************************************************************************
764  * FileMenu_DeleteSeparator                     [SHELL32.142]
765  */
766 BOOL WINAPI FileMenu_DeleteSeparator(HMENU hMenu)
767 {
768         TRACE("%p\n", hMenu);
769         return 0;
770 }
771
772 /*************************************************************************
773  * FileMenu_EnableItemByCmd                     [SHELL32.143]
774  */
775 BOOL WINAPI FileMenu_EnableItemByCmd(
776         HMENU   hMenu,
777         UINT    uID,
778         BOOL    bEnable)
779 {
780         TRACE("%p 0x%08x 0x%08x\n", hMenu, uID,bEnable);
781         return 0;
782 }
783
784 /*************************************************************************
785  * FileMenu_GetItemExtent                       [SHELL32.144]
786  *
787  * NOTES
788  *  if the menu is too big, entries are getting cut away!!
789  */
790 DWORD WINAPI FileMenu_GetItemExtent (HMENU hMenu, UINT uPos)
791 {       RECT rect;
792
793         FIXME("%p 0x%08x\n", hMenu, uPos);
794
795         if (GetMenuItemRect(0, hMenu, uPos, &rect))
796         { FIXME("0x%04x 0x%04x 0x%04x 0x%04x\n",
797           rect.right, rect.left, rect.top, rect.bottom);
798           return ((rect.right-rect.left)<<16) + (rect.top-rect.bottom);
799         }
800         return 0x00100010; /*FIXME*/
801 }
802
803 /*************************************************************************
804  * FileMenu_AbortInitMenu                       [SHELL32.120]
805  *
806  */
807 void WINAPI FileMenu_AbortInitMenu (void)
808 {       TRACE("\n");
809         bAbortInit = TRUE;
810 }
811
812 /*************************************************************************
813  * SHFind_InitMenuPopup                         [SHELL32.149]
814  *
815  * Get the IContextMenu instance for the submenu of options displayed
816  * for the Search entry in the Classic style Start menu.
817  *
818  * PARAMETERS
819  *  hMenu               [in] handle of menu previously created
820  *  hWndParent  [in] parent window
821  *  w                   [in] no pointer (0x209 over here) perhaps menu IDs ???
822  *  x                   [in] no pointer (0x226 over here)
823  *
824  * RETURNS
825  *  LPXXXXX                      pointer to struct containing a func addr at offset 8
826  *                                       or NULL at failure.
827  */
828 LPVOID WINAPI SHFind_InitMenuPopup (HMENU hMenu, HWND hWndParent, DWORD w, DWORD x)
829 {
830         FIXME("hmenu=%p hwnd=%p 0x%08x 0x%08x stub\n",
831                 hMenu,hWndParent,w,x);
832         return NULL; /* this is supposed to be a pointer */
833 }
834
835 /*************************************************************************
836  * _SHIsMenuSeparator   (internal)
837  */
838 static BOOL _SHIsMenuSeparator(HMENU hm, int i)
839 {
840         MENUITEMINFOW mii;
841
842         mii.cbSize = sizeof(MENUITEMINFOW);
843         mii.fMask = MIIM_TYPE;
844         mii.cch = 0;    /* WARNING: We MUST initialize it to 0*/
845         if (!GetMenuItemInfoW(hm, i, TRUE, &mii))
846         {
847           return(FALSE);
848         }
849
850         if (mii.fType & MFT_SEPARATOR)
851         {
852           return(TRUE);
853         }
854
855         return(FALSE);
856 }
857
858 /*************************************************************************
859  * Shell_MergeMenus                             [SHELL32.67]
860  */
861 HRESULT WINAPI Shell_MergeMenus (HMENU hmDst, HMENU hmSrc, UINT uInsert, UINT uIDAdjust, UINT uIDAdjustMax, ULONG uFlags)
862 {       int             nItem;
863         HMENU           hmSubMenu;
864         BOOL            bAlreadySeparated;
865         MENUITEMINFOW   miiSrc;
866         WCHAR           szName[256];
867         UINT            uTemp, uIDMax = uIDAdjust;
868
869         TRACE("hmenu1=%p hmenu2=%p 0x%04x 0x%04x 0x%04x  0x%04x\n",
870                  hmDst, hmSrc, uInsert, uIDAdjust, uIDAdjustMax, uFlags);
871
872         if (!hmDst || !hmSrc)
873         { return uIDMax;
874         }
875
876         nItem = GetMenuItemCount(hmDst);
877
878         if (uInsert >= (UINT)nItem)     /* insert position inside menu? */
879         {
880           uInsert = (UINT)nItem;        /* append on the end */
881           bAlreadySeparated = TRUE;
882         }
883         else
884         {
885           bAlreadySeparated = _SHIsMenuSeparator(hmDst, uInsert);
886         }
887
888         if ((uFlags & MM_ADDSEPARATOR) && !bAlreadySeparated)
889         {
890           /* Add a separator between the menus */
891           InsertMenuA(hmDst, uInsert, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
892           bAlreadySeparated = TRUE;
893         }
894
895
896         /* Go through the menu items and clone them*/
897         for (nItem = GetMenuItemCount(hmSrc) - 1; nItem >= 0; nItem--)
898         {
899           miiSrc.cbSize = sizeof(MENUITEMINFOW);
900           miiSrc.fMask =  MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_CHECKMARKS | MIIM_TYPE | MIIM_DATA;
901
902           /* We need to reset this every time through the loop in case menus DON'T have IDs*/
903           miiSrc.fType = MFT_STRING;
904           miiSrc.dwTypeData = szName;
905           miiSrc.dwItemData = 0;
906           miiSrc.cch = sizeof(szName)/sizeof(WCHAR);
907
908           if (!GetMenuItemInfoW(hmSrc, nItem, TRUE, &miiSrc))
909           {
910             continue;
911           }
912
913 /*        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);
914 */
915           if (miiSrc.fType & MFT_SEPARATOR)
916           {
917             /* This is a separator; don't put two of them in a row */
918             if (bAlreadySeparated)
919               continue;
920
921             bAlreadySeparated = TRUE;
922           }
923           else if (miiSrc.hSubMenu)
924           {
925             if (uFlags & MM_SUBMENUSHAVEIDS)
926             {
927               miiSrc.wID += uIDAdjust;                  /* add uIDAdjust to the ID */
928
929               if (miiSrc.wID > uIDAdjustMax)            /* skip ID's higher uIDAdjustMax */
930                 continue;
931
932               if (uIDMax <= miiSrc.wID)                 /* remember the highest ID */
933                 uIDMax = miiSrc.wID + 1;
934             }
935             else
936             {
937               miiSrc.fMask &= ~MIIM_ID;                 /* Don't set IDs for submenus that didn't have them already */
938             }
939             hmSubMenu = miiSrc.hSubMenu;
940
941             miiSrc.hSubMenu = CreatePopupMenu();
942
943             if (!miiSrc.hSubMenu) return(uIDMax);
944
945             uTemp = Shell_MergeMenus(miiSrc.hSubMenu, hmSubMenu, 0, uIDAdjust, uIDAdjustMax, uFlags & MM_SUBMENUSHAVEIDS);
946
947             if (uIDMax <= uTemp)
948               uIDMax = uTemp;
949
950             bAlreadySeparated = FALSE;
951           }
952           else                                          /* normal menu item */
953           {
954             miiSrc.wID += uIDAdjust;                    /* add uIDAdjust to the ID */
955
956             if (miiSrc.wID > uIDAdjustMax)              /* skip ID's higher uIDAdjustMax */
957               continue;
958
959             if (uIDMax <= miiSrc.wID)                   /* remember the highest ID */
960               uIDMax = miiSrc.wID + 1;
961
962             bAlreadySeparated = FALSE;
963           }
964
965 /*        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);
966 */
967           if (!InsertMenuItemW(hmDst, uInsert, TRUE, &miiSrc))
968           {
969             return(uIDMax);
970           }
971         }
972
973         /* Ensure the correct number of separators at the beginning of the
974         inserted menu items*/
975         if (uInsert == 0)
976         {
977           if (bAlreadySeparated)
978           {
979             DeleteMenu(hmDst, uInsert, MF_BYPOSITION);
980           }
981         }
982         else
983         {
984           if (_SHIsMenuSeparator(hmDst, uInsert-1))
985           {
986             if (bAlreadySeparated)
987             {
988               DeleteMenu(hmDst, uInsert, MF_BYPOSITION);
989             }
990           }
991           else
992           {
993             if ((uFlags & MM_ADDSEPARATOR) && !bAlreadySeparated)
994             {
995               /* Add a separator between the menus*/
996               InsertMenuW(hmDst, uInsert, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
997             }
998           }
999         }
1000         return(uIDMax);
1001 }