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

78 79 80 81 82 83 84 85 86 87 88 89
LPFMINFO FM_GetMenuInfo(HMENU hmenu)
{	MENUINFO	MenuInfo;
	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
90 91 92 93 94
	if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
	{
	  ERR("menudata corrupt: %p %lu\n", menudata, MenuInfo.cbSize);
	  return 0;
	}
95

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

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

113
	TRACE("\n");
114

115
	menudata = FM_GetMenuInfo(hmenu);
116

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

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

	return menudata;
}
129

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

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

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

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

	menudata = (LPFMINFO)MenuInfo.dwMenuData;
154

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

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

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

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

174
	SetMenuInfo(hmenu, &MenuInfo);
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189
	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)))
	      {
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 195 196 197 198 199
		    iIcon = FM_BLANK_ICON;
		  if ( SFGAO_FOLDER & ulItemAttr)
		  {
		    LPFMINFO lpFmMi;
		    MENUINFO MenuInfo;
		    HMENU hMenuPopup = CreatePopupMenu();
200

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

		    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);

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

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

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

	if ( GetMenuItemCount (hmenu) == 0 )
239 240 241
	{
          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
242 243
	  NumberOfItems++;
	}
244 245 246 247 248 249

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

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

267
	HMENU hMenu = CreatePopupMenu();
268

269
	TRACE("0x%08lx 0x%08x %p 0x%08x 0x%08x  hMenu=%p\n",
270 271 272 273 274 275 276 277 278 279 280 281 282
	crBorderColor, nBorderWidth, hBorderBmp, nSelHeight, uFlags, hMenu);

	menudata = (LPFMINFO)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FMINFO));
	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;
283 284 285 286 287 288 289 290
}

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

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

	FileMenu_DeleteAllItems (hmenu);
298

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

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

	DestroyMenu (hmenu);
307 308 309
}

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

326

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

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

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

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

347 348
	if ( hMenuPopup )
	{ /* sub menu */
349 350
	  mii.fMask |= MIIM_TYPE | MIIM_SUBMENU;
	  mii.fType = MFT_OWNERDRAW;
351 352 353
	  mii.hSubMenu = hMenuPopup;
	}
	else if (lpText == FM_SEPARATOR )
354
	{ mii.fMask |= MIIM_ID | MIIM_TYPE;
355 356 357 358
	  mii.fType = MFT_SEPARATOR;
	}
	else
	{ /* 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 364
	}
	mii.wID = uID;

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

Juergen Schmied's avatar
Juergen Schmied committed
367 368 369 370 371 372 373 374
	/* 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
375 376 377 378 379 380
	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
381 382 383
	menudata->bFixedItems = TRUE;
	SetMenuInfo(hMenu, &MenuInfo);

384 385 386
	return TRUE;

}
387 388 389

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

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

400 401 402 403 404 405 406 407
	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);
408
	  HeapFree( GetProcessHeap(), 0, lpszText );
409
	}
410 411 412

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

	pdump (pidl);

432
	bAbortInit = FALSE;
433

434
	FM_SetMenuParameter(hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
435 436

	return FM_InitMenuPopup(hmenu, NULL);
437 438 439 440 441
}

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

454 455
	FileMenu_DeleteAllItems (hmenu);

456
	FM_SetMenuParameter(hmenu, uID, pidl, 0, uEnumFlags, lpfnCallback);
457 458

	return FM_InitMenuPopup(hmenu, NULL);
459 460 461 462 463 464 465
}

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

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

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

	menudata = FM_GetMenuInfo(hmenu);
491

492
	menudata->bInitialized = FALSE;
493

494 495 496
	FM_InitMenuPopup(hmenu, pidl);

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

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

}


/*************************************************************************
 * FileMenu_TrackPopupMenuEx			[SHELL32.116]
 */
529
BOOL WINAPI FileMenu_TrackPopupMenuEx (
530 531 532 533 534 535 536
	HMENU hMenu,
	UINT uFlags,
	int x,
	int y,
	HWND hWnd,
	LPTPMPARAMS lptpm)
{
537
	TRACE("%p 0x%08x 0x%x 0x%x %p %p\n",
538 539 540 541 542 543 544 545 546 547 548 549
	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)
{
550
	FIXME("0x%08x %p %p\n",uReserved, ppidlFolder, ppidlItem);
551 552 553
	return 0;
}

554 555 556 557 558 559
#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
560 561 562 563 564 565 566
/*************************************************************************
 * FileMenu_MeasureItem				[SHELL32.112]
 */
LRESULT WINAPI FileMenu_MeasureItem(
	HWND	hWnd,
	LPMEASUREITEMSTRUCT	lpmis)
{
567 568 569 570
	LPFMITEM pMyItem = (LPFMITEM)(lpmis->itemData);
	HDC hdc = GetDC(hWnd);
	SIZE size;
	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 577 578 579 580
	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
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 587 588 589 590 591 592 593 594
	return 0;
}
/*************************************************************************
 * FileMenu_DrawItem				[SHELL32.105]
 */
LRESULT WINAPI FileMenu_DrawItem(
	HWND			hWnd,
	LPDRAWITEMSTRUCT	lpdis)
{
595 596 597 598 599 600
	LPFMITEM pMyItem = (LPFMITEM)(lpdis->itemData);
	COLORREF clrPrevText, clrPrevBkgnd;
	int xi,yi,xt,yt;
	HIMAGELIST hImageList;
	RECT TextRect, BorderRect;
	LPFMINFO menuinfo;
601

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

604 605 606 607 608 609 610 611 612 613
	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));
	}
614

615 616 617 618
	CopyRect(&TextRect, &(lpdis->rcItem));

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

622 623 624 625 626 627 628 629 630 631 632
	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;

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 641 642 643
	SetTextColor(lpdis->hDC, clrPrevText);
	SetBkColor(lpdis->hDC, clrPrevBkgnd);

	return TRUE;
644 645 646 647 648 649
}

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

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

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

	int i;
683

684
	TRACE("%p\n", hmenu);
685

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

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

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

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

700 701 702
	while (DeleteMenu (hmenu, 0, MF_BYPOSITION)){};

	menudata = FM_GetMenuInfo(hmenu);
703

704
	menudata->bInitialized = FALSE;
705

706 707 708 709
	return TRUE;
}

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

717
	TRACE("%p 0x%08x\n", hMenu, uID);
718

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

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

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

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

740
	TRACE("%p 0x%08x\n", hMenu, uPos);
741

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

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

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

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

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

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

/*************************************************************************
 * FileMenu_GetItemExtent			[SHELL32.144]
790
 *
791
 * NOTES
792
 *  if the menu is too big, entries are getting cut away!!
793 794 795
 */
DWORD WINAPI FileMenu_GetItemExtent (HMENU hMenu, UINT uPos)
{	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 802 803
	  rect.right, rect.left, rect.top, rect.bottom);
	  return ((rect.right-rect.left)<<16) + (rect.top-rect.bottom);
	}
804
	return 0x00100010; /*FIXME*/
805 806 807 808 809 810 811
}

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

/*************************************************************************
 * SHFind_InitMenuPopup				[SHELL32.149]
 *
 *
 * PARAMETERS
821
 *  hMenu		[in] handle of menu previously created
822
 *  hWndParent	[in] parent window
823 824 825 826 827 828
 *  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.
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 838 839 840 841
}

/*************************************************************************
 * Shell_MergeMenus				[SHELL32.67]
 *
 */
BOOL _SHIsMenuSeparator(HMENU hm, int i)
{
842
	MENUITEMINFOW mii;
843

844
	mii.cbSize = sizeof(MENUITEMINFOW);
845 846
	mii.fMask = MIIM_TYPE;
	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 863 864 865
HRESULT WINAPI Shell_MergeMenus (HMENU hmDst, HMENU hmSrc, UINT uInsert, UINT uIDAdjust, UINT uIDAdjustMax, ULONG uFlags)
{	int		nItem;
	HMENU		hmSubMenu;
	BOOL		bAlreadySeparated;
866 867
	MENUITEMINFOW	miiSrc;
	WCHAR		szName[256];
868 869
	UINT		uTemp, uIDMax = uIDAdjust;

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

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

	nItem = GetMenuItemCount(hmDst);
878 879 880 881

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

889
	if ((uFlags & MM_ADDSEPARATOR) && !bAlreadySeparated)
890 891
	{
	  /* Add a separator between the menus */
892 893 894 895 896 897 898
	  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--)
899
	{
900
	  miiSrc.cbSize = sizeof(MENUITEMINFOW);
901 902 903
	  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*/
904 905 906
	  miiSrc.fType = MFT_STRING;
	  miiSrc.dwTypeData = szName;
	  miiSrc.dwItemData = 0;
907
	  miiSrc.cch = sizeof(szName)/sizeof(WCHAR);
908

909
	  if (!GetMenuItemInfoW(hmSrc, nItem, TRUE, &miiSrc))
910 911
	  {
	    continue;
912
	  }
913 914 915

/*	  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);
*/
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 921
	      continue;

922 923 924
	    bAlreadySeparated = TRUE;
	  }
	  else if (miiSrc.hSubMenu)
925 926 927 928 929 930 931 932 933 934
	  {
	    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;
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 945 946 947

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

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

948
	    if (uIDMax <= uTemp)
949 950
	      uIDMax = uTemp;

951 952
	    bAlreadySeparated = FALSE;
	  }
953 954 955 956 957 958 959 960 961 962
	  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;

963 964
	    bAlreadySeparated = FALSE;
	  }
965 966 967

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

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