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
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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
	if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
	{
93
	  ERR("menudata corrupt: %p %u\n", menudata, MenuInfo.cbSize);
Juergen Schmied's avatar
Juergen Schmied committed
94 95
	  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
	SHFree(menudata->pidl);
119

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

	return menudata;
}
128

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

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

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

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

	menudata = (LPFMINFO)MenuInfo.dwMenuData;
153

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

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

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

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

173
	SetMenuInfo(hmenu, &MenuInfo);
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188
	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)))
	      {
189
		if (SUCCEEDED (IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulItemAttr)))
190
		{
191
		  ILGetDisplayNameExW(NULL, pidlTemp, sTemp, ILGDN_FORPARSING);
192
		  if (! (PidlToSicIndex(lpsf, pidlTemp, FALSE, 0, &iIcon)))
193 194 195 196 197 198
		    iIcon = FM_BLANK_ICON;
		  if ( SFGAO_FOLDER & ulItemAttr)
		  {
		    LPFMINFO lpFmMi;
		    MENUINFO MenuInfo;
		    HMENU hMenuPopup = CreatePopupMenu();
199

200
		    lpFmMi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FMINFO));
201 202 203 204 205 206

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

		    MenuInfo.cbSize = sizeof(MENUINFO);
		    MenuInfo.fMask = MIM_MENUDATA;
Kevin Koltzau's avatar
Kevin Koltzau committed
207
		    MenuInfo.dwMenuData = (ULONG_PTR) lpFmMi;
208 209
		    SetMenuInfo (hMenuPopup, &MenuInfo);

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

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

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

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

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

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

266
	HMENU hMenu = CreatePopupMenu();
267

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

271
	menudata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FMINFO));
272 273 274 275 276 277
	menudata->crBorderColor = crBorderColor;
	menudata->nBorderWidth = nBorderWidth;
	menudata->hBorderBmp = hBorderBmp;

	MenuInfo.cbSize = sizeof(MENUINFO);
	MenuInfo.fMask = MIM_MENUDATA;
Kevin Koltzau's avatar
Kevin Koltzau committed
278
	MenuInfo.dwMenuData = (ULONG_PTR) menudata;
279 280 281
	SetMenuInfo (hMenu, &MenuInfo);

	return hMenu;
282 283 284 285 286 287 288 289
}

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

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

	FileMenu_DeleteAllItems (hmenu);
297

298 299
	menudata = FM_GetMenuInfo(hmenu);

300
	SHFree( menudata->pidl);
301 302 303
	HeapFree(GetProcessHeap(), 0, menudata);

	DestroyMenu (hmenu);
304 305 306
}

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

323

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

328
	ZeroMemory (&mii, sizeof(MENUITEMINFOW));
329

330
	mii.cbSize = sizeof(MENUITEMINFOW);
331 332

	if (lpText != FM_SEPARATOR)
333 334 335 336
	{
	  int len = strlenW (lpText);
	  myItem = (LPFMITEM) SHAlloc( sizeof(FMITEM) + len*sizeof(WCHAR));
	  strcpyW (myItem->szItemText, lpText);
337 338 339 340
	  myItem->cchItemText = len;
	  myItem->iIconIndex = icon;
	  myItem->hMenu = hMenu;
	  mii.fMask = MIIM_DATA;
Kevin Koltzau's avatar
Kevin Koltzau committed
341
	  mii.dwItemData = (ULONG_PTR) myItem;
342
	}
343

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

362
	InsertMenuItemW (hMenu, (UINT)-1, TRUE, &mii);
363

Juergen Schmied's avatar
Juergen Schmied committed
364 365 366 367 368 369 370 371
	/* 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
372 373
	if ((menudata == 0) || (MenuInfo.cbSize != sizeof(MENUINFO)))
	{
374
	  ERR("menudata corrupt: %p %u\n", menudata, MenuInfo.cbSize);
Juergen Schmied's avatar
Juergen Schmied committed
375 376 377
	  return 0;
	}

Juergen Schmied's avatar
Juergen Schmied committed
378 379 380
	menudata->bFixedItems = TRUE;
	SetMenuInfo(hMenu, &MenuInfo);

381 382 383
	return TRUE;

}
384 385 386

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

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

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

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

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

	pdump (pidl);

430
	bAbortInit = FALSE;
431

432
	FM_SetMenuParameter(hmenu, uID, pidl, uFlags, uEnumFlags, lpfnCallback);
433 434

	return FM_InitMenuPopup(hmenu, NULL);
435 436 437 438 439
}

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

452 453
	FileMenu_DeleteAllItems (hmenu);

454
	FM_SetMenuParameter(hmenu, uID, pidl, 0, uEnumFlags, lpfnCallback);
455 456

	return FM_InitMenuPopup(hmenu, NULL);
457 458 459 460 461 462 463
}

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

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

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

	menudata = FM_GetMenuInfo(hmenu);
489

490
	menudata->bInitialized = FALSE;
491

492 493
	FM_InitMenuPopup(hmenu, pidl);

494
	if (bAddSeparator)
495
	  FileMenu_AppendItemW (hmenu, FM_SEPARATOR, 0, 0, 0, FM_DEFAULT_HEIGHT);
496

497
	TRACE("%p %p 0x%08x\n",hmenu, pidl,bAddSeparator);
498

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

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

}


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

552 553 554 555 556 557
#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
558 559 560 561 562 563 564
/*************************************************************************
 * FileMenu_MeasureItem				[SHELL32.112]
 */
LRESULT WINAPI FileMenu_MeasureItem(
	HWND	hWnd,
	LPMEASUREITEMSTRUCT	lpmis)
{
565 566 567 568
	LPFMITEM pMyItem = (LPFMITEM)(lpmis->itemData);
	HDC hdc = GetDC(hWnd);
	SIZE size;
	LPFMINFO menuinfo;
569

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

572
	GetTextExtentPoint32W(hdc, pMyItem->szItemText, pMyItem->cchItemText, &size);
573

574 575 576 577 578
	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
579
	if (menuinfo->nBorderWidth)
580
	  lpmis->itemWidth += menuinfo->nBorderWidth;
581

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

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

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

613 614 615 616
	CopyRect(&TextRect, &(lpdis->rcItem));

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

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

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

633
	Shell_GetImageList(0, &hImageList);
634
	ImageList_Draw(hImageList, pMyItem->iIconIndex, lpdis->hDC, xi, yi, ILD_NORMAL);
635

636
	TRACE("-- 0x%04x 0x%04x 0x%04x 0x%04x\n", TextRect.left, TextRect.top, TextRect.right, TextRect.bottom);
637

638 639 640 641
	SetTextColor(lpdis->hDC, clrPrevText);
	SetBkColor(lpdis->hDC, clrPrevBkgnd);

	return TRUE;
642 643 644 645 646 647
}

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

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

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

	int i;
681

682
	TRACE("%p\n", hmenu);
683

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

	for (i = 0; i < GetMenuItemCount( hmenu ); i++)
689
	{ GetMenuItemInfoW(hmenu, i, TRUE, &mii );
690

691
	  SHFree((LPFMINFO)mii.dwItemData);
692 693 694 695

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

697 698 699
	while (DeleteMenu (hmenu, 0, MF_BYPOSITION)){};

	menudata = FM_GetMenuInfo(hmenu);
700

701
	menudata->bInitialized = FALSE;
702

703 704 705 706
	return TRUE;
}

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

714
	TRACE("%p 0x%08x\n", hMenu, uID);
715

716 717
	ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
	mii.cbSize = sizeof(MENUITEMINFOW);
718 719
	mii.fMask = MIIM_SUBMENU;

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

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

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

737
	TRACE("%p 0x%08x\n", hMenu, uPos);
738

739 740
	ZeroMemory ( &mii, sizeof(MENUITEMINFOW));
	mii.cbSize = sizeof(MENUITEMINFOW);
741 742
	mii.fMask = MIIM_SUBMENU;

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

749 750 751 752 753 754 755 756 757 758 759
	DeleteMenu(hMenu, MF_BYPOSITION, uPos);
	return TRUE;
}

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

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

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

/*************************************************************************
 * FileMenu_GetItemExtent			[SHELL32.144]
787
 *
788
 * NOTES
789
 *  if the menu is too big, entries are getting cut away!!
790 791 792
 */
DWORD WINAPI FileMenu_GetItemExtent (HMENU hMenu, UINT uPos)
{	RECT rect;
793

794
	FIXME("%p 0x%08x\n", hMenu, uPos);
795 796

	if (GetMenuItemRect(0, hMenu, uPos, &rect))
797
	{ FIXME("0x%04x 0x%04x 0x%04x 0x%04x\n",
798 799 800
	  rect.right, rect.left, rect.top, rect.bottom);
	  return ((rect.right-rect.left)<<16) + (rect.top-rect.bottom);
	}
801
	return 0x00100010; /*FIXME*/
802 803 804 805 806 807 808
}

/*************************************************************************
 * FileMenu_AbortInitMenu 			[SHELL32.120]
 *
 */
void WINAPI FileMenu_AbortInitMenu (void)
809
{	TRACE("\n");
810
	bAbortInit = TRUE;
811 812 813 814 815
}

/*************************************************************************
 * SHFind_InitMenuPopup				[SHELL32.149]
 *
816 817
 * Get the IContextMenu instance for the submenu of options displayed
 * for the Search entry in the Classic style Start menu.
818 819
 *
 * PARAMETERS
820
 *  hMenu		[in] handle of menu previously created
821
 *  hWndParent	[in] parent window
822 823 824 825 826 827
 *  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.
828
 */
829
LPVOID WINAPI SHFind_InitMenuPopup (HMENU hMenu, HWND hWndParent, DWORD w, DWORD x)
Mike McCormack's avatar
Mike McCormack committed
830
{
831
	FIXME("hmenu=%p hwnd=%p 0x%08x 0x%08x stub\n",
832
		hMenu,hWndParent,w,x);
833
	return NULL; /* this is supposed to be a pointer */
834 835 836
}

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

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

	if (mii.fType & MFT_SEPARATOR)
852 853
	{
	  return(TRUE);
854 855
	}

856
	return(FALSE);
857
}
858

Mike McCormack's avatar
Mike McCormack committed
859 860 861
/*************************************************************************
 * Shell_MergeMenus				[SHELL32.67]
 */
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%04x\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);
}