Assorted spelling fixes.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 LPFMINFO FM_GetMenuInfo(HMENU hmenu)
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 %lu\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         if ( menudata->pidl)
118         { SHFree(menudata->pidl);
119         }
120
121         menudata->uID = uID;
122         menudata->pidl = ILClone(pidl);
123         menudata->uFlags = uFlags;
124         menudata->uEnumFlags = uEnumFlags;
125         menudata->lpfnCallback = lpfnCallback;
126
127         return menudata;
128 }
129
130 /*************************************************************************
131  * FM_InitMenuPopup                             [internal]
132  *
133  */
134 static int FM_InitMenuPopup(HMENU hmenu, LPCITEMIDLIST pAlternatePidl)
135 {       IShellFolder    *lpsf, *lpsf2;
136         ULONG           ulItemAttr = SFGAO_FOLDER;
137         UINT            uID, uFlags, uEnumFlags;
138         LPFNFMCALLBACK  lpfnCallback;
139         LPCITEMIDLIST   pidl;
140         WCHAR           sTemp[MAX_PATH];
141         int             NumberOfItems = 0, iIcon;
142         MENUINFO        MenuInfo;
143         LPFMINFO        menudata;
144
145         TRACE("%p %p\n", hmenu, pAlternatePidl);
146
147         MenuInfo.cbSize = sizeof(MENUINFO);
148         MenuInfo.fMask = MIM_MENUDATA;
149
150         if (! GetMenuInfo(hmenu, &MenuInfo))
151           return FALSE;
152
153         menudata = (LPFMINFO)MenuInfo.dwMenuData;
154
155         if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
156         {
157           ERR("menudata corrupt: %p %lu\n", menudata, MenuInfo.cbSize);
158           return 0;
159         }
160
161         if (menudata->bInitialized)
162           return 0;
163
164         pidl = (pAlternatePidl? pAlternatePidl: menudata->pidl);
165         if (!pidl)
166           return 0;
167
168         uID = menudata->uID;
169         uFlags = menudata->uFlags;
170         uEnumFlags = menudata->uEnumFlags;
171         lpfnCallback = menudata->lpfnCallback;
172         menudata->bInitialized = FALSE;
173
174         SetMenuInfo(hmenu, &MenuInfo);
175
176         if (SUCCEEDED (SHGetDesktopFolder(&lpsf)))
177         {
178           if (SUCCEEDED(IShellFolder_BindToObject(lpsf, pidl,0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf2)))
179           {
180             IEnumIDList *lpe = NULL;
181
182             if (SUCCEEDED (IShellFolder_EnumObjects(lpsf2, 0, uEnumFlags, &lpe )))
183             {
184
185               LPITEMIDLIST pidlTemp = NULL;
186               ULONG ulFetched;
187
188               while ((!bAbortInit) && (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched)))
189               {
190                 if (SUCCEEDED (IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulItemAttr)))
191                 {
192                   ILGetDisplayNameExW(NULL, pidlTemp, sTemp, ILGDN_FORPARSING);
193                   if (! (PidlToSicIndex(lpsf, pidlTemp, FALSE, 0, &iIcon)))
194                     iIcon = FM_BLANK_ICON;
195                   if ( SFGAO_FOLDER & ulItemAttr)
196                   {
197                     LPFMINFO lpFmMi;
198                     MENUINFO MenuInfo;
199                     HMENU hMenuPopup = CreatePopupMenu();
200
201                     lpFmMi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FMINFO));
202
203                     lpFmMi->pidl = ILCombine(pidl, pidlTemp);
204                     lpFmMi->uEnumFlags = SHCONTF_FOLDERS | SHCONTF_NONFOLDERS;
205
206                     MenuInfo.cbSize = sizeof(MENUINFO);
207                     MenuInfo.fMask = MIM_MENUDATA;
208                     MenuInfo.dwMenuData = (DWORD) lpFmMi;
209                     SetMenuInfo (hMenuPopup, &MenuInfo);
210
211                     FileMenu_AppendItemW (hmenu, sTemp, uID, iIcon, hMenuPopup, FM_DEFAULT_HEIGHT);
212                   }
213                   else
214                   {
215                     LPWSTR pExt = PathFindExtensionW(sTemp);
216                     if (pExt)
217                       *pExt = 0;
218                     FileMenu_AppendItemW (hmenu, sTemp, uID, iIcon, 0, FM_DEFAULT_HEIGHT);
219                   }
220                 }
221
222                 if (lpfnCallback)
223                 {
224                   TRACE("enter callback\n");
225                   lpfnCallback ( pidl, pidlTemp);
226                   TRACE("leave callback\n");
227                 }
228
229                 NumberOfItems++;
230               }
231               IEnumIDList_Release (lpe);
232             }
233             IShellFolder_Release(lpsf2);
234           }
235           IShellFolder_Release(lpsf);
236         }
237
238         if ( GetMenuItemCount (hmenu) == 0 )
239         {
240           static const WCHAR szEmpty[] = { '(','e','m','p','t','y',')',0 };
241           FileMenu_AppendItemW (hmenu, szEmpty, uID, FM_BLANK_ICON, 0, FM_DEFAULT_HEIGHT);
242           NumberOfItems++;
243         }
244
245         menudata->bInitialized = TRUE;
246         SetMenuInfo(hmenu, &MenuInfo);
247
248         return NumberOfItems;
249 }
250 /*************************************************************************
251  * FileMenu_Create                              [SHELL32.114]
252  *
253  * NOTES
254  *  for non-root menus values are
255  *  (ffffffff,00000000,00000000,00000000,00000000)
256  */
257 HMENU WINAPI FileMenu_Create (
258         COLORREF crBorderColor,
259         int nBorderWidth,
260         HBITMAP hBorderBmp,
261         int nSelHeight,
262         UINT uFlags)
263 {
264         MENUINFO        MenuInfo;
265         LPFMINFO        menudata;
266
267         HMENU hMenu = CreatePopupMenu();
268
269         TRACE("0x%08lx 0x%08x %p 0x%08x 0x%08x  hMenu=%p\n",
270         crBorderColor, nBorderWidth, hBorderBmp, nSelHeight, uFlags, hMenu);
271
272         menudata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FMINFO));
273         menudata->crBorderColor = crBorderColor;
274         menudata->nBorderWidth = nBorderWidth;
275         menudata->hBorderBmp = hBorderBmp;
276
277         MenuInfo.cbSize = sizeof(MENUINFO);
278         MenuInfo.fMask = MIM_MENUDATA;
279         MenuInfo.dwMenuData = (DWORD) menudata;
280         SetMenuInfo (hMenu, &MenuInfo);
281
282         return hMenu;
283 }
284
285 /*************************************************************************
286  * FileMenu_Destroy                             [SHELL32.118]
287  *
288  * NOTES
289  *  exported by name
290  */
291 void WINAPI FileMenu_Destroy (HMENU hmenu)
292 {
293         LPFMINFO        menudata;
294
295         TRACE("%p\n", hmenu);
296
297         FileMenu_DeleteAllItems (hmenu);
298
299         menudata = FM_GetMenuInfo(hmenu);
300
301         if ( menudata->pidl)
302         { SHFree( menudata->pidl);
303         }
304         HeapFree(GetProcessHeap(), 0, menudata);
305
306         DestroyMenu (hmenu);
307 }
308
309 /*************************************************************************
310  * FileMenu_AppendItem                  [SHELL32.115]
311  *
312  */
313 static BOOL FileMenu_AppendItemW(
314         HMENU hMenu,
315         LPCWSTR lpText,
316         UINT uID,
317         int icon,
318         HMENU hMenuPopup,
319         int nItemHeight)
320 {
321         MENUITEMINFOW   mii;
322         LPFMITEM        myItem;
323         LPFMINFO        menudata;
324         MENUINFO        MenuInfo;
325
326
327         TRACE("%p %s 0x%08x 0x%08x %p 0x%08x\n",
328           hMenu, (lpText!=FM_SEPARATOR) ? debugstr_w(lpText) : NULL,
329           uID, icon, hMenuPopup, nItemHeight);
330
331         ZeroMemory (&mii, sizeof(MENUITEMINFOW));
332
333         mii.cbSize = sizeof(MENUITEMINFOW);
334
335         if (lpText != FM_SEPARATOR)
336         {
337           int len = strlenW (lpText);
338           myItem = (LPFMITEM) SHAlloc( sizeof(FMITEM) + len*sizeof(WCHAR));
339           strcpyW (myItem->szItemText, lpText);
340           myItem->cchItemText = len;
341           myItem->iIconIndex = icon;
342           myItem->hMenu = hMenu;
343           mii.fMask = MIIM_DATA;
344           mii.dwItemData = (DWORD) myItem;
345         }
346
347         if ( hMenuPopup )
348         { /* sub menu */
349           mii.fMask |= MIIM_TYPE | MIIM_SUBMENU;
350           mii.fType = MFT_OWNERDRAW;
351           mii.hSubMenu = hMenuPopup;
352         }
353         else if (lpText == FM_SEPARATOR )
354         { mii.fMask |= MIIM_ID | MIIM_TYPE;
355           mii.fType = MFT_SEPARATOR;
356         }
357         else
358         { /* normal item */
359           mii.fMask |= MIIM_ID | MIIM_TYPE | MIIM_STATE;
360           mii.fState = MFS_ENABLED | MFS_DEFAULT;
361           mii.fType = MFT_OWNERDRAW;
362         }
363         mii.wID = uID;
364
365         InsertMenuItemW (hMenu, (UINT)-1, TRUE, &mii);
366
367         /* set bFixedItems to true */
368         MenuInfo.cbSize = sizeof(MENUINFO);
369         MenuInfo.fMask = MIM_MENUDATA;
370
371         if (! GetMenuInfo(hMenu, &MenuInfo))
372           return FALSE;
373
374         menudata = (LPFMINFO)MenuInfo.dwMenuData;
375         if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
376         {
377           ERR("menudata corrupt: %p %lu\n", menudata, MenuInfo.cbSize);
378           return 0;
379         }
380
381         menudata->bFixedItems = TRUE;
382         SetMenuInfo(hMenu, &MenuInfo);
383
384         return TRUE;
385
386 }
387
388 /**********************************************************************/
389
390 BOOL WINAPI FileMenu_AppendItemAW(
391         HMENU hMenu,
392         LPCVOID lpText,
393         UINT uID,
394         int icon,
395         HMENU hMenuPopup,
396         int nItemHeight)
397 {
398         BOOL ret;
399
400         if ((SHELL_OsIsUnicode() && (lpText!=FM_SEPARATOR)) || (lpText == NULL))
401           ret = FileMenu_AppendItemW(hMenu, lpText, uID, icon, hMenuPopup, nItemHeight);
402         else
403         {
404           DWORD len = MultiByteToWideChar( CP_ACP, 0, lpText, -1, NULL, 0 );
405           LPWSTR lpszText = HeapAlloc ( GetProcessHeap(), 0, len*sizeof(WCHAR) );
406           MultiByteToWideChar( CP_ACP, 0, lpText, -1, lpszText, len );
407           ret = FileMenu_AppendItemW(hMenu, lpszText, uID, icon, hMenuPopup, nItemHeight);
408           HeapFree( GetProcessHeap(), 0, lpszText );
409         }
410
411         return ret;
412 }
413 /*************************************************************************
414  * FileMenu_InsertUsingPidl                     [SHELL32.110]
415  *
416  * NOTES
417  *      uEnumFlags      any SHCONTF flag
418  */
419 int WINAPI FileMenu_InsertUsingPidl (
420         HMENU hmenu,
421         UINT uID,
422         LPCITEMIDLIST pidl,
423         UINT uFlags,
424         UINT uEnumFlags,
425         LPFNFMCALLBACK lpfnCallback)
426 {
427         TRACE("%p 0x%08x %p 0x%08x 0x%08x %p\n",
428         hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
429
430         pdump (pidl);
431
432         bAbortInit = FALSE;
433
434         FM_SetMenuParameter(hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
435
436         return FM_InitMenuPopup(hmenu, NULL);
437 }
438
439 /*************************************************************************
440  * FileMenu_ReplaceUsingPidl                    [SHELL32.113]
441  *
442  * FIXME: the static items are deleted but won't be refreshed
443  */
444 int WINAPI FileMenu_ReplaceUsingPidl(
445         HMENU   hmenu,
446         UINT    uID,
447         LPCITEMIDLIST   pidl,
448         UINT    uEnumFlags,
449         LPFNFMCALLBACK lpfnCallback)
450 {
451         TRACE("%p 0x%08x %p 0x%08x %p\n",
452         hmenu, uID, pidl, uEnumFlags, lpfnCallback);
453
454         FileMenu_DeleteAllItems (hmenu);
455
456         FM_SetMenuParameter(hmenu, uID, pidl, 0, uEnumFlags, lpfnCallback);
457
458         return FM_InitMenuPopup(hmenu, NULL);
459 }
460
461 /*************************************************************************
462  * FileMenu_Invalidate                  [SHELL32.111]
463  */
464 void WINAPI FileMenu_Invalidate (HMENU hMenu)
465 {
466         FIXME("%p\n",hMenu);
467 }
468
469 /*************************************************************************
470  * FileMenu_FindSubMenuByPidl                   [SHELL32.106]
471  */
472 HMENU WINAPI FileMenu_FindSubMenuByPidl(
473         HMENU   hMenu,
474         LPCITEMIDLIST   pidl)
475 {
476         FIXME("%p %p\n",hMenu, pidl);
477         return 0;
478 }
479
480 /*************************************************************************
481  * FileMenu_AppendFilesForPidl                  [SHELL32.124]
482  */
483 int WINAPI FileMenu_AppendFilesForPidl(
484         HMENU   hmenu,
485         LPCITEMIDLIST   pidl,
486         BOOL    bAddSeperator)
487 {
488         LPFMINFO        menudata;
489
490         menudata = FM_GetMenuInfo(hmenu);
491
492         menudata->bInitialized = FALSE;
493
494         FM_InitMenuPopup(hmenu, pidl);
495
496         if (bAddSeperator)
497           FileMenu_AppendItemW (hmenu, FM_SEPARATOR, 0, 0, 0, FM_DEFAULT_HEIGHT);
498
499         TRACE("%p %p 0x%08x\n",hmenu, pidl,bAddSeperator);
500
501         return 0;
502 }
503 /*************************************************************************
504  * FileMenu_AddFilesForPidl                     [SHELL32.125]
505  *
506  * NOTES
507  *      uEnumFlags      any SHCONTF flag
508  */
509 int WINAPI FileMenu_AddFilesForPidl (
510         HMENU   hmenu,
511         UINT    uReserved,
512         UINT    uID,
513         LPCITEMIDLIST   pidl,
514         UINT    uFlags,
515         UINT    uEnumFlags,
516         LPFNFMCALLBACK  lpfnCallback)
517 {
518         TRACE("%p 0x%08x 0x%08x %p 0x%08x 0x%08x %p\n",
519         hmenu, uReserved, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
520
521         return FileMenu_InsertUsingPidl ( hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
522
523 }
524
525
526 /*************************************************************************
527  * FileMenu_TrackPopupMenuEx                    [SHELL32.116]
528  */
529 BOOL WINAPI FileMenu_TrackPopupMenuEx (
530         HMENU hMenu,
531         UINT uFlags,
532         int x,
533         int y,
534         HWND hWnd,
535         LPTPMPARAMS lptpm)
536 {
537         TRACE("%p 0x%08x 0x%x 0x%x %p %p\n",
538         hMenu, uFlags, x, y, hWnd, lptpm);
539         return TrackPopupMenuEx(hMenu, uFlags, x, y, hWnd, lptpm);
540 }
541
542 /*************************************************************************
543  * FileMenu_GetLastSelectedItemPidls            [SHELL32.107]
544  */
545 BOOL WINAPI FileMenu_GetLastSelectedItemPidls(
546         UINT    uReserved,
547         LPCITEMIDLIST   *ppidlFolder,
548         LPCITEMIDLIST   *ppidlItem)
549 {
550         FIXME("0x%08x %p %p\n",uReserved, ppidlFolder, ppidlItem);
551         return 0;
552 }
553
554 #define FM_ICON_SIZE    16
555 #define FM_Y_SPACE      4
556 #define FM_SPACE1       4
557 #define FM_SPACE2       2
558 #define FM_LEFTBORDER   2
559 #define FM_RIGHTBORDER  8
560 /*************************************************************************
561  * FileMenu_MeasureItem                         [SHELL32.112]
562  */
563 LRESULT WINAPI FileMenu_MeasureItem(
564         HWND    hWnd,
565         LPMEASUREITEMSTRUCT     lpmis)
566 {
567         LPFMITEM pMyItem = (LPFMITEM)(lpmis->itemData);
568         HDC hdc = GetDC(hWnd);
569         SIZE size;
570         LPFMINFO menuinfo;
571
572         TRACE("%p %p %s\n", hWnd, lpmis, debugstr_w(pMyItem->szItemText));
573
574         GetTextExtentPoint32W(hdc, pMyItem->szItemText, pMyItem->cchItemText, &size);
575
576         lpmis->itemWidth = size.cx + FM_LEFTBORDER + FM_ICON_SIZE + FM_SPACE1 + FM_SPACE2 + FM_RIGHTBORDER;
577         lpmis->itemHeight = (size.cy > (FM_ICON_SIZE + FM_Y_SPACE)) ? size.cy : (FM_ICON_SIZE + FM_Y_SPACE);
578
579         /* add the menubitmap */
580         menuinfo = FM_GetMenuInfo(pMyItem->hMenu);
581         if (menuinfo->nBorderWidth)
582           lpmis->itemWidth += menuinfo->nBorderWidth;
583
584         TRACE("-- 0x%04x 0x%04x\n", lpmis->itemWidth, lpmis->itemHeight);
585         ReleaseDC (hWnd, hdc);
586         return 0;
587 }
588 /*************************************************************************
589  * FileMenu_DrawItem                            [SHELL32.105]
590  */
591 LRESULT WINAPI FileMenu_DrawItem(
592         HWND                    hWnd,
593         LPDRAWITEMSTRUCT        lpdis)
594 {
595         LPFMITEM pMyItem = (LPFMITEM)(lpdis->itemData);
596         COLORREF clrPrevText, clrPrevBkgnd;
597         int xi,yi,xt,yt;
598         HIMAGELIST hImageList;
599         RECT TextRect, BorderRect;
600         LPFMINFO menuinfo;
601
602         TRACE("%p %p %s\n", hWnd, lpdis, debugstr_w(pMyItem->szItemText));
603
604         if (lpdis->itemState & ODS_SELECTED)
605         {
606           clrPrevText = SetTextColor(lpdis->hDC, GetSysColor (COLOR_HIGHLIGHTTEXT));
607           clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor (COLOR_HIGHLIGHT));
608         }
609         else
610         {
611           clrPrevText = SetTextColor(lpdis->hDC, GetSysColor (COLOR_MENUTEXT));
612           clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor (COLOR_MENU));
613         }
614
615         CopyRect(&TextRect, &(lpdis->rcItem));
616
617         /* add the menubitmap */
618         menuinfo = FM_GetMenuInfo(pMyItem->hMenu);
619         if (menuinfo->nBorderWidth)
620           TextRect.left += menuinfo->nBorderWidth;
621
622         BorderRect.right = menuinfo->nBorderWidth;
623 /*      FillRect(lpdis->hDC, &BorderRect, CreateSolidBrush( menuinfo->crBorderColor));
624 */
625         TextRect.left += FM_LEFTBORDER;
626         xi = TextRect.left + FM_SPACE1;
627         yi = TextRect.top + FM_Y_SPACE/2;
628         TextRect.bottom -= FM_Y_SPACE/2;
629
630         xt = xi + FM_ICON_SIZE + FM_SPACE2;
631         yt = yi;
632
633         ExtTextOutW (lpdis->hDC, xt , yt, ETO_OPAQUE, &TextRect, pMyItem->szItemText, pMyItem->cchItemText, NULL);
634
635         Shell_GetImageList(0, &hImageList);
636         ImageList_Draw(hImageList, pMyItem->iIconIndex, lpdis->hDC, xi, yi, ILD_NORMAL);
637
638         TRACE("-- 0x%04lx 0x%04lx 0x%04lx 0x%04lx\n", TextRect.left, TextRect.top, TextRect.right, TextRect.bottom);
639
640         SetTextColor(lpdis->hDC, clrPrevText);
641         SetBkColor(lpdis->hDC, clrPrevBkgnd);
642
643         return TRUE;
644 }
645
646 /*************************************************************************
647  * FileMenu_InitMenuPopup                       [SHELL32.109]
648  *
649  * NOTES
650  *  The filemenu is an ownerdrawn menu. Call this function responding to
651  *  WM_INITPOPUPMENU
652  *
653  */
654 BOOL WINAPI FileMenu_InitMenuPopup (HMENU hmenu)
655 {
656         FM_InitMenuPopup(hmenu, NULL);
657         return TRUE;
658 }
659
660 /*************************************************************************
661  * FileMenu_HandleMenuChar                      [SHELL32.108]
662  */
663 LRESULT WINAPI FileMenu_HandleMenuChar(
664         HMENU   hMenu,
665         WPARAM  wParam)
666 {
667         FIXME("%p 0x%08x\n",hMenu,wParam);
668         return 0;
669 }
670
671 /*************************************************************************
672  * FileMenu_DeleteAllItems                      [SHELL32.104]
673  *
674  * NOTES
675  *  exported by name
676  */
677 BOOL WINAPI FileMenu_DeleteAllItems (HMENU hmenu)
678 {
679         MENUITEMINFOW   mii;
680         LPFMINFO        menudata;
681
682         int i;
683
684         TRACE("%p\n", hmenu);
685
686         ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
687         mii.cbSize = sizeof(MENUITEMINFOW);
688         mii.fMask = MIIM_SUBMENU|MIIM_DATA;
689
690         for (i = 0; i < GetMenuItemCount( hmenu ); i++)
691         { GetMenuItemInfoW(hmenu, i, TRUE, &mii );
692
693           if (mii.dwItemData)
694             SHFree((LPFMINFO)mii.dwItemData);
695
696           if (mii.hSubMenu)
697             FileMenu_Destroy(mii.hSubMenu);
698         }
699
700         while (DeleteMenu (hmenu, 0, MF_BYPOSITION)){};
701
702         menudata = FM_GetMenuInfo(hmenu);
703
704         menudata->bInitialized = FALSE;
705
706         return TRUE;
707 }
708
709 /*************************************************************************
710  * FileMenu_DeleteItemByCmd                     [SHELL32.117]
711  *
712  */
713 BOOL WINAPI FileMenu_DeleteItemByCmd (HMENU hMenu, UINT uID)
714 {
715         MENUITEMINFOW mii;
716
717         TRACE("%p 0x%08x\n", hMenu, uID);
718
719         ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
720         mii.cbSize = sizeof(MENUITEMINFOW);
721         mii.fMask = MIIM_SUBMENU;
722
723         GetMenuItemInfoW(hMenu, uID, FALSE, &mii );
724         if ( mii.hSubMenu )
725         {
726           /* FIXME: Do what? */
727         }
728
729         DeleteMenu(hMenu, MF_BYCOMMAND, uID);
730         return TRUE;
731 }
732
733 /*************************************************************************
734  * FileMenu_DeleteItemByIndex                   [SHELL32.140]
735  */
736 BOOL WINAPI FileMenu_DeleteItemByIndex ( HMENU hMenu, UINT uPos)
737 {
738         MENUITEMINFOW mii;
739
740         TRACE("%p 0x%08x\n", hMenu, uPos);
741
742         ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
743         mii.cbSize = sizeof(MENUITEMINFOW);
744         mii.fMask = MIIM_SUBMENU;
745
746         GetMenuItemInfoW(hMenu, uPos, TRUE, &mii );
747         if ( mii.hSubMenu )
748         {
749           /* FIXME: Do what? */
750         }
751
752         DeleteMenu(hMenu, MF_BYPOSITION, uPos);
753         return TRUE;
754 }
755
756 /*************************************************************************
757  * FileMenu_DeleteItemByFirstID                 [SHELL32.141]
758  */
759 BOOL WINAPI FileMenu_DeleteItemByFirstID(
760         HMENU   hMenu,
761         UINT    uID)
762 {
763         TRACE("%p 0x%08x\n", hMenu, uID);
764         return 0;
765 }
766
767 /*************************************************************************
768  * FileMenu_DeleteSeparator                     [SHELL32.142]
769  */
770 BOOL WINAPI FileMenu_DeleteSeparator(HMENU hMenu)
771 {
772         TRACE("%p\n", hMenu);
773         return 0;
774 }
775
776 /*************************************************************************
777  * FileMenu_EnableItemByCmd                     [SHELL32.143]
778  */
779 BOOL WINAPI FileMenu_EnableItemByCmd(
780         HMENU   hMenu,
781         UINT    uID,
782         BOOL    bEnable)
783 {
784         TRACE("%p 0x%08x 0x%08x\n", hMenu, uID,bEnable);
785         return 0;
786 }
787
788 /*************************************************************************
789  * FileMenu_GetItemExtent                       [SHELL32.144]
790  *
791  * NOTES
792  *  if the menu is too big, entries are getting cut away!!
793  */
794 DWORD WINAPI FileMenu_GetItemExtent (HMENU hMenu, UINT uPos)
795 {       RECT rect;
796
797         FIXME("%p 0x%08x\n", hMenu, uPos);
798
799         if (GetMenuItemRect(0, hMenu, uPos, &rect))
800         { FIXME("0x%04lx 0x%04lx 0x%04lx 0x%04lx\n",
801           rect.right, rect.left, rect.top, rect.bottom);
802           return ((rect.right-rect.left)<<16) + (rect.top-rect.bottom);
803         }
804         return 0x00100010; /*FIXME*/
805 }
806
807 /*************************************************************************
808  * FileMenu_AbortInitMenu                       [SHELL32.120]
809  *
810  */
811 void WINAPI FileMenu_AbortInitMenu (void)
812 {       TRACE("\n");
813         bAbortInit = TRUE;
814 }
815
816 /*************************************************************************
817  * SHFind_InitMenuPopup                         [SHELL32.149]
818  *
819  *
820  * PARAMETERS
821  *  hMenu               [in] handle of menu previously created
822  *  hWndParent  [in] parent window
823  *  w                   [in] no pointer (0x209 over here) perhaps menu IDs ???
824  *  x                   [in] no pointer (0x226 over here)
825  *
826  * RETURNS
827  *  LPXXXXX                      pointer to struct containing a func addr at offset 8
828  *                                       or NULL at failure.
829  */
830 LPVOID WINAPI SHFind_InitMenuPopup (HMENU hMenu, HWND hWndParent, DWORD w, DWORD x)
831 {       FIXME("hmenu=%p hwnd=%p 0x%08lx 0x%08lx stub\n",
832                 hMenu,hWndParent,w,x);
833         return NULL; /* this is supposed to be a pointer */
834 }
835
836 /*************************************************************************
837  * Shell_MergeMenus                             [SHELL32.67]
838  *
839  */
840 BOOL _SHIsMenuSeparator(HMENU hm, int i)
841 {
842         MENUITEMINFOW mii;
843
844         mii.cbSize = sizeof(MENUITEMINFOW);
845         mii.fMask = MIIM_TYPE;
846         mii.cch = 0;    /* WARNING: We MUST initialize it to 0*/
847         if (!GetMenuItemInfoW(hm, i, TRUE, &mii))
848         {
849           return(FALSE);
850         }
851
852         if (mii.fType & MFT_SEPARATOR)
853         {
854           return(TRUE);
855         }
856
857         return(FALSE);
858 }
859
860 /**********************************************************************/
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%04lx\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 }