shlmenu.c 24.2 KB
Newer Older
1
/*
2
 * see www.geocities.com/SiliconValley/4942/filemenu.html
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Copyright 1999, 2000 Juergen Schmied
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20

21
#include <stdarg.h>
22 23
#include <string.h>

24 25
#define COBJMACROS

26 27
#include "windef.h"
#include "winbase.h"
28
#include "winreg.h"
29 30
#include "wingdi.h"
#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 41 42 43 44 45
#ifdef FM_SEPARATOR
#undef FM_SEPARATOR
#endif
#define FM_SEPARATOR (LPCWSTR)1

static BOOL FileMenu_AppendItemW(HMENU hMenu, LPCWSTR lpText, UINT uID, int icon,
46
                                 HMENU hMenuPopup, int nItemHeight);
47 48

typedef struct
Juergen Schmied's avatar
Juergen Schmied committed
49 50 51
{
	BOOL		bInitialized;
	BOOL		bFixedItems;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	/* create */
	COLORREF	crBorderColor;
	int		nBorderWidth;
	HBITMAP		hBorderBmp;

	/* insert using pidl */
	LPITEMIDLIST	pidl;
	UINT		uID;
	UINT		uFlags;
	UINT		uEnumFlags;
	LPFNFMCALLBACK lpfnCallback;
} FMINFO, *LPFMINFO;

typedef struct
{	int	cchItemText;
	int	iIconIndex;
	HMENU	hMenu;
69
	WCHAR	szItemText[1];
70 71 72 73 74 75
} FMITEM, * LPFMITEM;

static BOOL bAbortInit;

#define	CCH_MAXITEMTEXT 256

76
WINE_DEFAULT_DEBUG_CHANNEL(shell);
77

Mike McCormack's avatar
Mike McCormack committed
78 79 80
static LPFMINFO FM_GetMenuInfo(HMENU hmenu)
{
	MENUINFO	MenuInfo;
81 82 83 84 85 86 87 88 89 90
	LPFMINFO	menudata;

	MenuInfo.cbSize = sizeof(MENUINFO);
	MenuInfo.fMask = MIM_MENUDATA;

	if (! GetMenuInfo(hmenu, &MenuInfo))
	  return NULL;

	menudata = (LPFMINFO)MenuInfo.dwMenuData;

Juergen Schmied's avatar
Juergen Schmied committed
91 92 93 94 95
	if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
	{
	  ERR("menudata corrupt: %p %lu\n", menudata, MenuInfo.cbSize);
	  return 0;
	}
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	return menudata;

}
/*************************************************************************
 * FM_SetMenuParameter				[internal]
 *
 */
static LPFMINFO FM_SetMenuParameter(
	HMENU hmenu,
	UINT uID,
	LPCITEMIDLIST pidl,
	UINT uFlags,
	UINT uEnumFlags,
	LPFNFMCALLBACK lpfnCallback)
{
	LPFMINFO	menudata;

114
	TRACE("\n");
115

116
	menudata = FM_GetMenuInfo(hmenu);
117

118 119 120
	if ( menudata->pidl)
	{ SHFree(menudata->pidl);
	}
121

122 123 124 125 126 127 128 129
	menudata->uID = uID;
	menudata->pidl = ILClone(pidl);
	menudata->uFlags = uFlags;
	menudata->uEnumFlags = uEnumFlags;
	menudata->lpfnCallback = lpfnCallback;

	return menudata;
}
130

131 132 133 134
/*************************************************************************
 * FM_InitMenuPopup				[internal]
 *
 */
135
static int FM_InitMenuPopup(HMENU hmenu, LPCITEMIDLIST pAlternatePidl)
136
{	IShellFolder	*lpsf, *lpsf2;
137
	ULONG		ulItemAttr = SFGAO_FOLDER;
138 139
	UINT		uID, uFlags, uEnumFlags;
	LPFNFMCALLBACK	lpfnCallback;
140
	LPCITEMIDLIST	pidl;
141
	WCHAR		sTemp[MAX_PATH];
142 143 144 145
	int		NumberOfItems = 0, iIcon;
	MENUINFO	MenuInfo;
	LPFMINFO	menudata;

146
	TRACE("%p %p\n", hmenu, pAlternatePidl);
147 148 149 150 151 152 153 154

	MenuInfo.cbSize = sizeof(MENUINFO);
	MenuInfo.fMask = MIM_MENUDATA;

	if (! GetMenuInfo(hmenu, &MenuInfo))
	  return FALSE;

	menudata = (LPFMINFO)MenuInfo.dwMenuData;
155

Juergen Schmied's avatar
Juergen Schmied committed
156 157 158 159 160
	if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
	{
	  ERR("menudata corrupt: %p %lu\n", menudata, MenuInfo.cbSize);
	  return 0;
	}
161

162 163
	if (menudata->bInitialized)
	  return 0;
164

165
	pidl = (pAlternatePidl? pAlternatePidl: menudata->pidl);
Juergen Schmied's avatar
Juergen Schmied committed
166 167 168 169
	if (!pidl)
	  return 0;

	uID = menudata->uID;
170 171 172 173
	uFlags = menudata->uFlags;
	uEnumFlags = menudata->uEnumFlags;
	lpfnCallback = menudata->lpfnCallback;
	menudata->bInitialized = FALSE;
Juergen Schmied's avatar
Juergen Schmied committed
174

175
	SetMenuInfo(hmenu, &MenuInfo);
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190
	if (SUCCEEDED (SHGetDesktopFolder(&lpsf)))
	{
	  if (SUCCEEDED(IShellFolder_BindToObject(lpsf, pidl,0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf2)))
	  {
	    IEnumIDList	*lpe = NULL;

	    if (SUCCEEDED (IShellFolder_EnumObjects(lpsf2, 0, uEnumFlags, &lpe )))
	    {

	      LPITEMIDLIST pidlTemp = NULL;
	      ULONG ulFetched;

	      while ((!bAbortInit) && (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched)))
	      {
191
		if (SUCCEEDED (IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulItemAttr)))
192
		{
193
		  ILGetDisplayNameExW(NULL, pidlTemp, sTemp, ILGDN_FORPARSING);
194
		  if (! (PidlToSicIndex(lpsf, pidlTemp, FALSE, 0, &iIcon)))
195 196 197 198 199 200
		    iIcon = FM_BLANK_ICON;
		  if ( SFGAO_FOLDER & ulItemAttr)
		  {
		    LPFMINFO lpFmMi;
		    MENUINFO MenuInfo;
		    HMENU hMenuPopup = CreatePopupMenu();
201

202
		    lpFmMi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FMINFO));
203 204 205 206 207 208 209 210 211

		    lpFmMi->pidl = ILCombine(pidl, pidlTemp);
		    lpFmMi->uEnumFlags = SHCONTF_FOLDERS | SHCONTF_NONFOLDERS;

		    MenuInfo.cbSize = sizeof(MENUINFO);
		    MenuInfo.fMask = MIM_MENUDATA;
		    MenuInfo.dwMenuData = (DWORD) lpFmMi;
		    SetMenuInfo (hMenuPopup, &MenuInfo);

212
		    FileMenu_AppendItemW (hmenu, sTemp, uID, iIcon, hMenuPopup, FM_DEFAULT_HEIGHT);
213 214 215
		  }
		  else
		  {
216 217 218 219
		    LPWSTR pExt = PathFindExtensionW(sTemp);
		    if (pExt)
		      *pExt = 0;
		    FileMenu_AppendItemW (hmenu, sTemp, uID, iIcon, 0, FM_DEFAULT_HEIGHT);
220 221 222 223 224
		  }
		}

		if (lpfnCallback)
		{
225
		  TRACE("enter callback\n");
226
		  lpfnCallback ( pidl, pidlTemp);
227
		  TRACE("leave callback\n");
228 229 230 231 232 233 234 235 236 237 238 239
		}

		NumberOfItems++;
	      }
	      IEnumIDList_Release (lpe);
	    }
	    IShellFolder_Release(lpsf2);
	  }
	  IShellFolder_Release(lpsf);
	}

	if ( GetMenuItemCount (hmenu) == 0 )
240 241 242
	{
          static const WCHAR szEmpty[] = { '(','e','m','p','t','y',')',0 };
	  FileMenu_AppendItemW (hmenu, szEmpty, uID, FM_BLANK_ICON, 0, FM_DEFAULT_HEIGHT);
Juergen Schmied's avatar
Juergen Schmied committed
243 244
	  NumberOfItems++;
	}
245 246 247 248 249 250

	menudata->bInitialized = TRUE;
	SetMenuInfo(hmenu, &MenuInfo);

	return NumberOfItems;
}
251 252 253
/*************************************************************************
 * FileMenu_Create				[SHELL32.114]
 *
Juergen Schmied's avatar
Juergen Schmied committed
254 255
 * NOTES
 *  for non-root menus values are
256
 *  (ffffffff,00000000,00000000,00000000,00000000)
257 258 259 260 261 262 263 264
 */
HMENU WINAPI FileMenu_Create (
	COLORREF crBorderColor,
	int nBorderWidth,
	HBITMAP hBorderBmp,
	int nSelHeight,
	UINT uFlags)
{
265 266
	MENUINFO	MenuInfo;
	LPFMINFO	menudata;
267

268
	HMENU hMenu = CreatePopupMenu();
269

270
	TRACE("0x%08lx 0x%08x %p 0x%08x 0x%08x  hMenu=%p\n",
271 272
	crBorderColor, nBorderWidth, hBorderBmp, nSelHeight, uFlags, hMenu);

273
	menudata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FMINFO));
274 275 276 277 278 279 280 281 282 283
	menudata->crBorderColor = crBorderColor;
	menudata->nBorderWidth = nBorderWidth;
	menudata->hBorderBmp = hBorderBmp;

	MenuInfo.cbSize = sizeof(MENUINFO);
	MenuInfo.fMask = MIM_MENUDATA;
	MenuInfo.dwMenuData = (DWORD) menudata;
	SetMenuInfo (hMenu, &MenuInfo);

	return hMenu;
284 285 286 287 288 289 290 291
}

/*************************************************************************
 * FileMenu_Destroy				[SHELL32.118]
 *
 * NOTES
 *  exported by name
 */
292
void WINAPI FileMenu_Destroy (HMENU hmenu)
293
{
294 295
	LPFMINFO	menudata;

296
	TRACE("%p\n", hmenu);
297 298

	FileMenu_DeleteAllItems (hmenu);
299

300 301 302 303 304 305 306 307
	menudata = FM_GetMenuInfo(hmenu);

	if ( menudata->pidl)
	{ SHFree( menudata->pidl);
	}
	HeapFree(GetProcessHeap(), 0, menudata);

	DestroyMenu (hmenu);
308 309 310
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
311
 * FileMenu_AppendItem			[SHELL32.115]
312 313
 *
 */
314
static BOOL FileMenu_AppendItemW(
315
	HMENU hMenu,
316
	LPCWSTR lpText,
317 318 319 320 321
	UINT uID,
	int icon,
	HMENU hMenuPopup,
	int nItemHeight)
{
322
	MENUITEMINFOW	mii;
Juergen Schmied's avatar
Juergen Schmied committed
323 324 325 326
	LPFMITEM	myItem;
	LPFMINFO	menudata;
	MENUINFO        MenuInfo;

327

328
	TRACE("%p %s 0x%08x 0x%08x %p 0x%08x\n",
329 330
	  hMenu, (lpText!=FM_SEPARATOR) ? debugstr_w(lpText) : NULL,
	  uID, icon, hMenuPopup, nItemHeight);
331

332
	ZeroMemory (&mii, sizeof(MENUITEMINFOW));
333

334
	mii.cbSize = sizeof(MENUITEMINFOW);
335 336

	if (lpText != FM_SEPARATOR)
337 338 339 340
	{
	  int len = strlenW (lpText);
	  myItem = (LPFMITEM) SHAlloc( sizeof(FMITEM) + len*sizeof(WCHAR));
	  strcpyW (myItem->szItemText, lpText);
341 342 343 344 345 346
	  myItem->cchItemText = len;
	  myItem->iIconIndex = icon;
	  myItem->hMenu = hMenu;
	  mii.fMask = MIIM_DATA;
	  mii.dwItemData = (DWORD) myItem;
	}
347

348 349
	if ( hMenuPopup )
	{ /* sub menu */
350 351
	  mii.fMask |= MIIM_TYPE | MIIM_SUBMENU;
	  mii.fType = MFT_OWNERDRAW;
352 353 354
	  mii.hSubMenu = hMenuPopup;
	}
	else if (lpText == FM_SEPARATOR )
355
	{ mii.fMask |= MIIM_ID | MIIM_TYPE;
356 357 358 359
	  mii.fType = MFT_SEPARATOR;
	}
	else
	{ /* normal item */
360
	  mii.fMask |= MIIM_ID | MIIM_TYPE | MIIM_STATE;
361
	  mii.fState = MFS_ENABLED | MFS_DEFAULT;
362
	  mii.fType = MFT_OWNERDRAW;
363 364 365
	}
	mii.wID = uID;

366
	InsertMenuItemW (hMenu, (UINT)-1, TRUE, &mii);
367

Juergen Schmied's avatar
Juergen Schmied committed
368 369 370 371 372 373 374 375
	/* set bFixedItems to true */
	MenuInfo.cbSize = sizeof(MENUINFO);
	MenuInfo.fMask = MIM_MENUDATA;

	if (! GetMenuInfo(hMenu, &MenuInfo))
	  return FALSE;

	menudata = (LPFMINFO)MenuInfo.dwMenuData;
Juergen Schmied's avatar
Juergen Schmied committed
376 377 378 379 380 381
	if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
	{
	  ERR("menudata corrupt: %p %lu\n", menudata, MenuInfo.cbSize);
	  return 0;
	}

Juergen Schmied's avatar
Juergen Schmied committed
382 383 384
	menudata->bFixedItems = TRUE;
	SetMenuInfo(hMenu, &MenuInfo);

385 386 387
	return TRUE;

}
388 389 390

/**********************************************************************/

391 392 393 394 395 396 397 398 399 400
BOOL WINAPI FileMenu_AppendItemAW(
	HMENU hMenu,
	LPCVOID lpText,
	UINT uID,
	int icon,
	HMENU hMenuPopup,
	int nItemHeight)
{
	BOOL ret;

401 402 403 404 405 406 407 408
	if ((SHELL_OsIsUnicode() && (lpText!=FM_SEPARATOR)) || (lpText == NULL))
	  ret = FileMenu_AppendItemW(hMenu, lpText, uID, icon, hMenuPopup, nItemHeight);
        else
	{
	  DWORD len = MultiByteToWideChar( CP_ACP, 0, lpText, -1, NULL, 0 );
	  LPWSTR lpszText = HeapAlloc ( GetProcessHeap(), 0, len*sizeof(WCHAR) );
	  MultiByteToWideChar( CP_ACP, 0, lpText, -1, lpszText, len );
	  ret = FileMenu_AppendItemW(hMenu, lpszText, uID, icon, hMenuPopup, nItemHeight);
409
	  HeapFree( GetProcessHeap(), 0, lpszText );
410
	}
411 412 413

	return ret;
}
Mike McCormack's avatar
Mike McCormack committed
414

415 416 417 418 419 420 421
/*************************************************************************
 * FileMenu_InsertUsingPidl			[SHELL32.110]
 *
 * NOTES
 *	uEnumFlags	any SHCONTF flag
 */
int WINAPI FileMenu_InsertUsingPidl (
422
	HMENU hmenu,
423 424 425 426 427
	UINT uID,
	LPCITEMIDLIST pidl,
	UINT uFlags,
	UINT uEnumFlags,
	LPFNFMCALLBACK lpfnCallback)
428
{
429
	TRACE("%p 0x%08x %p 0x%08x 0x%08x %p\n",
430
	hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
431 432 433

	pdump (pidl);

434
	bAbortInit = FALSE;
435

436
	FM_SetMenuParameter(hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
437 438

	return FM_InitMenuPopup(hmenu, NULL);
439 440 441 442 443
}

/*************************************************************************
 * FileMenu_ReplaceUsingPidl			[SHELL32.113]
 *
444
 * FIXME: the static items are deleted but won't be refreshed
445 446
 */
int WINAPI FileMenu_ReplaceUsingPidl(
447
	HMENU	hmenu,
448 449 450 451 452
	UINT	uID,
	LPCITEMIDLIST	pidl,
	UINT	uEnumFlags,
	LPFNFMCALLBACK lpfnCallback)
{
453
	TRACE("%p 0x%08x %p 0x%08x %p\n",
454
	hmenu, uID, pidl, uEnumFlags, lpfnCallback);
455

456 457
	FileMenu_DeleteAllItems (hmenu);

458
	FM_SetMenuParameter(hmenu, uID, pidl, 0, uEnumFlags, lpfnCallback);
459 460

	return FM_InitMenuPopup(hmenu, NULL);
461 462 463 464 465 466 467
}

/*************************************************************************
 * FileMenu_Invalidate			[SHELL32.111]
 */
void WINAPI FileMenu_Invalidate (HMENU hMenu)
{
468
	FIXME("%p\n",hMenu);
469 470 471 472 473 474 475 476 477
}

/*************************************************************************
 * FileMenu_FindSubMenuByPidl			[SHELL32.106]
 */
HMENU WINAPI FileMenu_FindSubMenuByPidl(
	HMENU	hMenu,
	LPCITEMIDLIST	pidl)
{
478
	FIXME("%p %p\n",hMenu, pidl);
479 480 481 482 483 484
	return 0;
}

/*************************************************************************
 * FileMenu_AppendFilesForPidl			[SHELL32.124]
 */
485
int WINAPI FileMenu_AppendFilesForPidl(
486
	HMENU	hmenu,
487 488 489
	LPCITEMIDLIST	pidl,
	BOOL	bAddSeperator)
{
490 491 492
	LPFMINFO	menudata;

	menudata = FM_GetMenuInfo(hmenu);
493

494
	menudata->bInitialized = FALSE;
495

496 497 498
	FM_InitMenuPopup(hmenu, pidl);

	if (bAddSeperator)
499
	  FileMenu_AppendItemW (hmenu, FM_SEPARATOR, 0, 0, 0, FM_DEFAULT_HEIGHT);
500

501
	TRACE("%p %p 0x%08x\n",hmenu, pidl,bAddSeperator);
502

503 504 505 506 507 508 509 510 511
	return 0;
}
/*************************************************************************
 * FileMenu_AddFilesForPidl			[SHELL32.125]
 *
 * NOTES
 *	uEnumFlags	any SHCONTF flag
 */
int WINAPI FileMenu_AddFilesForPidl (
512
	HMENU	hmenu,
513 514 515 516 517 518 519
	UINT	uReserved,
	UINT	uID,
	LPCITEMIDLIST	pidl,
	UINT	uFlags,
	UINT	uEnumFlags,
	LPFNFMCALLBACK	lpfnCallback)
{
520
	TRACE("%p 0x%08x 0x%08x %p 0x%08x 0x%08x %p\n",
521 522 523
	hmenu, uReserved, uID, pidl, uFlags, uEnumFlags, lpfnCallback);

	return FileMenu_InsertUsingPidl ( hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
524 525 526 527 528 529 530

}


/*************************************************************************
 * FileMenu_TrackPopupMenuEx			[SHELL32.116]
 */
531
BOOL WINAPI FileMenu_TrackPopupMenuEx (
532 533 534 535 536 537 538
	HMENU hMenu,
	UINT uFlags,
	int x,
	int y,
	HWND hWnd,
	LPTPMPARAMS lptpm)
{
539
	TRACE("%p 0x%08x 0x%x 0x%x %p %p\n",
540 541 542 543 544 545 546 547 548 549 550 551
	hMenu, uFlags, x, y, hWnd, lptpm);
	return TrackPopupMenuEx(hMenu, uFlags, x, y, hWnd, lptpm);
}

/*************************************************************************
 * FileMenu_GetLastSelectedItemPidls		[SHELL32.107]
 */
BOOL WINAPI FileMenu_GetLastSelectedItemPidls(
	UINT	uReserved,
	LPCITEMIDLIST	*ppidlFolder,
	LPCITEMIDLIST	*ppidlItem)
{
552
	FIXME("0x%08x %p %p\n",uReserved, ppidlFolder, ppidlItem);
553 554 555
	return 0;
}

556 557 558 559 560 561
#define FM_ICON_SIZE	16
#define FM_Y_SPACE	4
#define FM_SPACE1	4
#define FM_SPACE2	2
#define FM_LEFTBORDER	2
#define FM_RIGHTBORDER	8
562 563 564 565 566 567 568
/*************************************************************************
 * FileMenu_MeasureItem				[SHELL32.112]
 */
LRESULT WINAPI FileMenu_MeasureItem(
	HWND	hWnd,
	LPMEASUREITEMSTRUCT	lpmis)
{
569 570 571 572
	LPFMITEM pMyItem = (LPFMITEM)(lpmis->itemData);
	HDC hdc = GetDC(hWnd);
	SIZE size;
	LPFMINFO menuinfo;
573

574
	TRACE("%p %p %s\n", hWnd, lpmis, debugstr_w(pMyItem->szItemText));
575

576
	GetTextExtentPoint32W(hdc, pMyItem->szItemText, pMyItem->cchItemText, &size);
577

578 579 580 581 582
	lpmis->itemWidth = size.cx + FM_LEFTBORDER + FM_ICON_SIZE + FM_SPACE1 + FM_SPACE2 + FM_RIGHTBORDER;
	lpmis->itemHeight = (size.cy > (FM_ICON_SIZE + FM_Y_SPACE)) ? size.cy : (FM_ICON_SIZE + FM_Y_SPACE);

	/* add the menubitmap */
	menuinfo = FM_GetMenuInfo(pMyItem->hMenu);
Juergen Schmied's avatar
Juergen Schmied committed
583
	if (menuinfo->nBorderWidth)
584
	  lpmis->itemWidth += menuinfo->nBorderWidth;
585

586
	TRACE("-- 0x%04x 0x%04x\n", lpmis->itemWidth, lpmis->itemHeight);
587
	ReleaseDC (hWnd, hdc);
588 589 590 591 592 593 594 595 596
	return 0;
}
/*************************************************************************
 * FileMenu_DrawItem				[SHELL32.105]
 */
LRESULT WINAPI FileMenu_DrawItem(
	HWND			hWnd,
	LPDRAWITEMSTRUCT	lpdis)
{
597 598 599 600 601 602
	LPFMITEM pMyItem = (LPFMITEM)(lpdis->itemData);
	COLORREF clrPrevText, clrPrevBkgnd;
	int xi,yi,xt,yt;
	HIMAGELIST hImageList;
	RECT TextRect, BorderRect;
	LPFMINFO menuinfo;
603

604
	TRACE("%p %p %s\n", hWnd, lpdis, debugstr_w(pMyItem->szItemText));
605

606 607 608 609 610 611 612 613 614 615
	if (lpdis->itemState & ODS_SELECTED)
	{
	  clrPrevText = SetTextColor(lpdis->hDC, GetSysColor (COLOR_HIGHLIGHTTEXT));
	  clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor (COLOR_HIGHLIGHT));
	}
	else
	{
	  clrPrevText = SetTextColor(lpdis->hDC, GetSysColor (COLOR_MENUTEXT));
	  clrPrevBkgnd = SetBkColor(lpdis->hDC, GetSysColor (COLOR_MENU));
	}
616

617 618 619 620
	CopyRect(&TextRect, &(lpdis->rcItem));

	/* add the menubitmap */
	menuinfo = FM_GetMenuInfo(pMyItem->hMenu);
Juergen Schmied's avatar
Juergen Schmied committed
621
	if (menuinfo->nBorderWidth)
622
	  TextRect.left += menuinfo->nBorderWidth;
623

624 625 626 627 628 629 630 631 632 633 634
	BorderRect.right = menuinfo->nBorderWidth;
/*	FillRect(lpdis->hDC, &BorderRect, CreateSolidBrush( menuinfo->crBorderColor));
*/
	TextRect.left += FM_LEFTBORDER;
	xi = TextRect.left + FM_SPACE1;
	yi = TextRect.top + FM_Y_SPACE/2;
	TextRect.bottom -= FM_Y_SPACE/2;

	xt = xi + FM_ICON_SIZE + FM_SPACE2;
	yt = yi;

635
	ExtTextOutW (lpdis->hDC, xt , yt, ETO_OPAQUE, &TextRect, pMyItem->szItemText, pMyItem->cchItemText, NULL);
636

637
	Shell_GetImageList(0, &hImageList);
638
	ImageList_Draw(hImageList, pMyItem->iIconIndex, lpdis->hDC, xi, yi, ILD_NORMAL);
639

640
	TRACE("-- 0x%04lx 0x%04lx 0x%04lx 0x%04lx\n", TextRect.left, TextRect.top, TextRect.right, TextRect.bottom);
641

642 643 644 645
	SetTextColor(lpdis->hDC, clrPrevText);
	SetBkColor(lpdis->hDC, clrPrevBkgnd);

	return TRUE;
646 647 648 649 650 651
}

/*************************************************************************
 * FileMenu_InitMenuPopup			[SHELL32.109]
 *
 * NOTES
652
 *  The filemenu is an ownerdrawn menu. Call this function responding to
653 654 655
 *  WM_INITPOPUPMENU
 *
 */
656 657 658 659
BOOL WINAPI FileMenu_InitMenuPopup (HMENU hmenu)
{
	FM_InitMenuPopup(hmenu, NULL);
	return TRUE;
660 661 662 663 664 665 666 667 668
}

/*************************************************************************
 * FileMenu_HandleMenuChar			[SHELL32.108]
 */
LRESULT WINAPI FileMenu_HandleMenuChar(
	HMENU	hMenu,
	WPARAM	wParam)
{
669
	FIXME("%p 0x%08x\n",hMenu,wParam);
670 671 672 673 674 675 676 677 678
	return 0;
}

/*************************************************************************
 * FileMenu_DeleteAllItems			[SHELL32.104]
 *
 * NOTES
 *  exported by name
 */
679
BOOL WINAPI FileMenu_DeleteAllItems (HMENU hmenu)
680
{
681
	MENUITEMINFOW	mii;
682 683 684
	LPFMINFO	menudata;

	int i;
685

686
	TRACE("%p\n", hmenu);
687

688 689
	ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
	mii.cbSize = sizeof(MENUITEMINFOW);
690 691 692
	mii.fMask = MIIM_SUBMENU|MIIM_DATA;

	for (i = 0; i < GetMenuItemCount( hmenu ); i++)
693
	{ GetMenuItemInfoW(hmenu, i, TRUE, &mii );
694 695 696 697 698 699 700

	  if (mii.dwItemData)
	    SHFree((LPFMINFO)mii.dwItemData);

	  if (mii.hSubMenu)
	    FileMenu_Destroy(mii.hSubMenu);
	}
701

702 703 704
	while (DeleteMenu (hmenu, 0, MF_BYPOSITION)){};

	menudata = FM_GetMenuInfo(hmenu);
705

706
	menudata->bInitialized = FALSE;
707

708 709 710 711
	return TRUE;
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
712
 * FileMenu_DeleteItemByCmd 			[SHELL32.117]
713 714 715 716
 *
 */
BOOL WINAPI FileMenu_DeleteItemByCmd (HMENU hMenu, UINT uID)
{
717
	MENUITEMINFOW mii;
718

719
	TRACE("%p 0x%08x\n", hMenu, uID);
720

721 722
	ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
	mii.cbSize = sizeof(MENUITEMINFOW);
723 724
	mii.fMask = MIIM_SUBMENU;

725
	GetMenuItemInfoW(hMenu, uID, FALSE, &mii );
726 727 728 729
	if ( mii.hSubMenu )
	{
	  /* FIXME: Do what? */
	}
730 731 732 733 734 735 736 737 738 739

	DeleteMenu(hMenu, MF_BYCOMMAND, uID);
	return TRUE;
}

/*************************************************************************
 * FileMenu_DeleteItemByIndex			[SHELL32.140]
 */
BOOL WINAPI FileMenu_DeleteItemByIndex ( HMENU hMenu, UINT uPos)
{
740
	MENUITEMINFOW mii;
741

742
	TRACE("%p 0x%08x\n", hMenu, uPos);
743

744 745
	ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
	mii.cbSize = sizeof(MENUITEMINFOW);
746 747
	mii.fMask = MIIM_SUBMENU;

748
	GetMenuItemInfoW(hMenu, uPos, TRUE, &mii );
749 750 751 752
	if ( mii.hSubMenu )
	{
	  /* FIXME: Do what? */
	}
753

754 755 756 757 758 759 760 761 762 763 764
	DeleteMenu(hMenu, MF_BYPOSITION, uPos);
	return TRUE;
}

/*************************************************************************
 * FileMenu_DeleteItemByFirstID			[SHELL32.141]
 */
BOOL WINAPI FileMenu_DeleteItemByFirstID(
	HMENU	hMenu,
	UINT	uID)
{
765
	TRACE("%p 0x%08x\n", hMenu, uID);
766 767 768 769 770 771 772 773
	return 0;
}

/*************************************************************************
 * FileMenu_DeleteSeparator			[SHELL32.142]
 */
BOOL WINAPI FileMenu_DeleteSeparator(HMENU hMenu)
{
774
	TRACE("%p\n", hMenu);
775 776 777 778 779 780 781 782 783 784 785
	return 0;
}

/*************************************************************************
 * FileMenu_EnableItemByCmd			[SHELL32.143]
 */
BOOL WINAPI FileMenu_EnableItemByCmd(
	HMENU	hMenu,
	UINT	uID,
	BOOL	bEnable)
{
786
	TRACE("%p 0x%08x 0x%08x\n", hMenu, uID,bEnable);
787 788 789 790 791
	return 0;
}

/*************************************************************************
 * FileMenu_GetItemExtent			[SHELL32.144]
792
 *
793
 * NOTES
794
 *  if the menu is too big, entries are getting cut away!!
795 796 797
 */
DWORD WINAPI FileMenu_GetItemExtent (HMENU hMenu, UINT uPos)
{	RECT rect;
798

799
	FIXME("%p 0x%08x\n", hMenu, uPos);
800 801

	if (GetMenuItemRect(0, hMenu, uPos, &rect))
802
	{ FIXME("0x%04lx 0x%04lx 0x%04lx 0x%04lx\n",
803 804 805
	  rect.right, rect.left, rect.top, rect.bottom);
	  return ((rect.right-rect.left)<<16) + (rect.top-rect.bottom);
	}
806
	return 0x00100010; /*FIXME*/
807 808 809 810 811 812 813
}

/*************************************************************************
 * FileMenu_AbortInitMenu 			[SHELL32.120]
 *
 */
void WINAPI FileMenu_AbortInitMenu (void)
814
{	TRACE("\n");
815
	bAbortInit = TRUE;
816 817 818 819 820 821 822
}

/*************************************************************************
 * SHFind_InitMenuPopup				[SHELL32.149]
 *
 *
 * PARAMETERS
823
 *  hMenu		[in] handle of menu previously created
824
 *  hWndParent	[in] parent window
825 826 827 828 829 830
 *  w			[in] no pointer (0x209 over here) perhaps menu IDs ???
 *  x			[in] no pointer (0x226 over here)
 *
 * RETURNS
 *  LPXXXXX			 pointer to struct containing a func addr at offset 8
 *					 or NULL at failure.
831
 */
832
LPVOID WINAPI SHFind_InitMenuPopup (HMENU hMenu, HWND hWndParent, DWORD w, DWORD x)
Mike McCormack's avatar
Mike McCormack committed
833 834
{
	FIXME("hmenu=%p hwnd=%p 0x%08lx 0x%08lx stub\n",
835
		hMenu,hWndParent,w,x);
836
	return NULL; /* this is supposed to be a pointer */
837 838 839
}

/*************************************************************************
Mike McCormack's avatar
Mike McCormack committed
840
 * _SHIsMenuSeparator   (internal)
841
 */
Mike McCormack's avatar
Mike McCormack committed
842
static BOOL _SHIsMenuSeparator(HMENU hm, int i)
843
{
844
	MENUITEMINFOW mii;
845

846
	mii.cbSize = sizeof(MENUITEMINFOW);
847 848
	mii.fMask = MIIM_TYPE;
	mii.cch = 0;    /* WARNING: We MUST initialize it to 0*/
849
	if (!GetMenuItemInfoW(hm, i, TRUE, &mii))
850 851
	{
	  return(FALSE);
852 853 854
	}

	if (mii.fType & MFT_SEPARATOR)
855 856
	{
	  return(TRUE);
857 858
	}

859
	return(FALSE);
860
}
861

Mike McCormack's avatar
Mike McCormack committed
862 863 864
/*************************************************************************
 * Shell_MergeMenus				[SHELL32.67]
 */
865 866 867 868
HRESULT WINAPI Shell_MergeMenus (HMENU hmDst, HMENU hmSrc, UINT uInsert, UINT uIDAdjust, UINT uIDAdjustMax, ULONG uFlags)
{	int		nItem;
	HMENU		hmSubMenu;
	BOOL		bAlreadySeparated;
869 870
	MENUITEMINFOW	miiSrc;
	WCHAR		szName[256];
871 872
	UINT		uTemp, uIDMax = uIDAdjust;

873
	TRACE("hmenu1=%p hmenu2=%p 0x%04x 0x%04x 0x%04x  0x%04lx\n",
874 875 876 877 878 879 880
		 hmDst, hmSrc, uInsert, uIDAdjust, uIDAdjustMax, uFlags);

	if (!hmDst || !hmSrc)
	{ return uIDMax;
	}

	nItem = GetMenuItemCount(hmDst);
881 882 883 884

	if (uInsert >= (UINT)nItem)	/* insert position inside menu? */
	{
	  uInsert = (UINT)nItem;	/* append on the end */
885 886 887
	  bAlreadySeparated = TRUE;
	}
	else
888
	{
889
	  bAlreadySeparated = _SHIsMenuSeparator(hmDst, uInsert);
890
	}
891

892
	if ((uFlags & MM_ADDSEPARATOR) && !bAlreadySeparated)
893 894
	{
	  /* Add a separator between the menus */
895 896 897 898 899 900 901
	  InsertMenuA(hmDst, uInsert, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
	  bAlreadySeparated = TRUE;
	}


	/* Go through the menu items and clone them*/
	for (nItem = GetMenuItemCount(hmSrc) - 1; nItem >= 0; nItem--)
902
	{
903
	  miiSrc.cbSize = sizeof(MENUITEMINFOW);
904 905 906
	  miiSrc.fMask =  MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_CHECKMARKS | MIIM_TYPE | MIIM_DATA;

	  /* We need to reset this every time through the loop in case menus DON'T have IDs*/
907 908 909
	  miiSrc.fType = MFT_STRING;
	  miiSrc.dwTypeData = szName;
	  miiSrc.dwItemData = 0;
910
	  miiSrc.cch = sizeof(szName)/sizeof(WCHAR);
911

912
	  if (!GetMenuItemInfoW(hmSrc, nItem, TRUE, &miiSrc))
913 914
	  {
	    continue;
915
	  }
916 917 918

/*	  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);
*/
919
	  if (miiSrc.fType & MFT_SEPARATOR)
920 921
	  {
	    /* This is a separator; don't put two of them in a row */
922
	    if (bAlreadySeparated)
923 924
	      continue;

925 926 927
	    bAlreadySeparated = TRUE;
	  }
	  else if (miiSrc.hSubMenu)
928 929 930 931 932 933 934 935 936 937
	  {
	    if (uFlags & MM_SUBMENUSHAVEIDS)
	    {
	      miiSrc.wID += uIDAdjust;			/* add uIDAdjust to the ID */

	      if (miiSrc.wID > uIDAdjustMax)		/* skip ID's higher uIDAdjustMax */
	        continue;

	      if (uIDMax <= miiSrc.wID)			/* remember the highest ID */
	        uIDMax = miiSrc.wID + 1;
938 939
	    }
	    else
940 941
	    {
	      miiSrc.fMask &= ~MIIM_ID;			/* Don't set IDs for submenus that didn't have them already */
942 943
	    }
	    hmSubMenu = miiSrc.hSubMenu;
944

945
	    miiSrc.hSubMenu = CreatePopupMenu();
946 947 948 949 950

	    if (!miiSrc.hSubMenu) return(uIDMax);

	    uTemp = Shell_MergeMenus(miiSrc.hSubMenu, hmSubMenu, 0, uIDAdjust, uIDAdjustMax, uFlags & MM_SUBMENUSHAVEIDS);

951
	    if (uIDMax <= uTemp)
952 953
	      uIDMax = uTemp;

954 955
	    bAlreadySeparated = FALSE;
	  }
956 957 958 959 960 961 962 963 964 965
	  else						/* normal menu item */
	  {
	    miiSrc.wID += uIDAdjust;			/* add uIDAdjust to the ID */

	    if (miiSrc.wID > uIDAdjustMax)		/* skip ID's higher uIDAdjustMax */
	      continue;

	    if (uIDMax <= miiSrc.wID)			/* remember the highest ID */
	      uIDMax = miiSrc.wID + 1;

966 967
	    bAlreadySeparated = FALSE;
	  }
968 969 970

/*	  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);
*/
971
	  if (!InsertMenuItemW(hmDst, uInsert, TRUE, &miiSrc))
972 973
	  {
	    return(uIDMax);
974 975 976 977 978 979
	  }
	}

	/* Ensure the correct number of separators at the beginning of the
	inserted menu items*/
	if (uInsert == 0)
980 981 982 983
	{
	  if (bAlreadySeparated)
	  {
	    DeleteMenu(hmDst, uInsert, MF_BYPOSITION);
984 985 986
	  }
	}
	else
987 988 989 990 991 992
	{
	  if (_SHIsMenuSeparator(hmDst, uInsert-1))
	  {
	    if (bAlreadySeparated)
	    {
	      DeleteMenu(hmDst, uInsert, MF_BYPOSITION);
993 994 995
	    }
	  }
	  else
996 997 998 999
	  {
	    if ((uFlags & MM_ADDSEPARATOR) && !bAlreadySeparated)
	    {
	      /* Add a separator between the menus*/
1000
	      InsertMenuW(hmDst, uInsert, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
1001 1002 1003 1004 1005
	    }
	  }
	}
	return(uIDMax);
}