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