
user: remove menu item pointers from function interfaces


Thomas Kho

---

 dlls/user/menu.c |  160 +++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 116 insertions(+), 44 deletions(-)

diff --git a/dlls/user/menu.c b/dlls/user/menu.c
index 20af392..7780bd5 100644
--- a/dlls/user/menu.c
+++ b/dlls/user/menu.c
@@ -152,6 +152,7 @@ #define MENU_BOTTOM_MARGIN 2
 
   /* (other menu->FocusedItem values give the position of the focused item) */
 #define NO_SELECTED_ITEM  0xffff
+#define ITEM_NOT_FOUND    0xffff
 
 #define MENU_ITEM_TYPE(flags) \
   ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
@@ -665,20 +666,20 @@ static UINT  MENU_GetStartOfPrevColumn(
  * Find a menu item. Return a pointer on the item, and modifies *hmenu
  * in case the item was in a sub-menu.
  */
-static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
+static UINT MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
 {
     POPUPMENU tmpmenu;
     POPUPMENU *menu = &tmpmenu;
     MENUITEM *fallback = NULL;
-    UINT fallback_pos = 0;
+    UINT fallback_pos = ITEM_NOT_FOUND;
     UINT i;
 
     if ((*hmenu == (HMENU)0xffff) || (MENU_GetMenu(*hmenu, menu)))
-        return NULL;
+        return ITEM_NOT_FOUND;
     if (wFlags & MF_BYPOSITION)
     {
-	if (*nPos >= menu->nItems) return NULL;
-	return &menu->items[*nPos];
+	if (*nPos >= menu->nItems) return ITEM_NOT_FOUND;
+	return *nPos;
     }
     else
     {
@@ -688,8 +689,8 @@ static MENUITEM *MENU_FindItem( HMENU *h
 	    if (item->fType & MF_POPUP)
 	    {
 		HMENU hsubmenu = item->hSubMenu;
-		MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
-		if (subitem)
+		UINT subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
+		if (subitem != ITEM_NOT_FOUND)
 		{
 		    *hmenu = hsubmenu;
 		    return subitem;
@@ -704,7 +705,7 @@ static MENUITEM *MENU_FindItem( HMENU *h
 	    else if (item->wID == *nPos)
 	    {
 		*nPos = i;
-		return item;
+		return i;
 	    }
 	}
     }
@@ -712,7 +713,7 @@ static MENUITEM *MENU_FindItem( HMENU *h
     if (fallback)
         *nPos = fallback_pos;
 
-    return fallback;
+    return fallback_pos;
 }
 
 /***********************************************************************
@@ -2220,7 +2221,7 @@ static MENUITEM *MENU_InsertItem( HMENU 
         if (pos > menu->nItems)
             pos = menu->nItems;
     } else {
-        if (!MENU_FindItem( &hMenu, &pos, flags ))
+        if (MENU_FindItem( &hMenu, &pos, flags ) == ITEM_NOT_FOUND)
             pos = menu->nItems;
         else {
             if (MENU_GetMenu(hMenu, menu))
@@ -3682,10 +3683,15 @@ BOOL WINAPI ChangeMenuW( HMENU hMenu, UI
 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
 {
     MENUITEM *item;
+    POPUPMENU menu;
     DWORD ret;
+    UINT pos;
 
     TRACE("menu=%p id=%04x flags=%04x\n", hMenu, id, flags );
-    if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
+    if ((pos = MENU_FindItem( &hMenu, &id, flags )) == ITEM_NOT_FOUND)
+        return -1;
+    MENU_GetMenu(hMenu, &menu);
+    item = &menu.items[pos];
     ret = item->fState & MF_CHECKED;
     if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
     else item->fState &= ~MF_CHECKED;
@@ -3702,6 +3708,7 @@ BOOL WINAPI EnableMenuItem( HMENU hMenu,
     MENUITEM *item;
     POPUPMENU tmpmenu;
     POPUPMENU *menu = &tmpmenu;
+    UINT pos;
 
     TRACE("(%p, %04x, %04x) !\n", hMenu, wItemID, wFlags);
 
@@ -3709,8 +3716,10 @@ BOOL WINAPI EnableMenuItem( HMENU hMenu,
     if (MENU_GetMenu(hMenu, menu))
 	return (UINT)-1;
 
-    if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
+    if ((pos = MENU_FindItem( &hMenu, &wItemID, wFlags )) == ITEM_NOT_FOUND)
 	return (UINT)-1;
+    MENU_GetMenu(hMenu, menu);
+    item = &menu->items[pos];
 
     oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
     item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
@@ -3751,13 +3760,17 @@ INT WINAPI GetMenuStringA(
 	UINT wFlags	/* [in] MF_ flags */
 ) {
     MENUITEM *item;
+    POPUPMENU menu;
+    UINT pos;
 
     TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
     if (str && nMaxSiz) str[0] = '\0';
-    if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
+    if ((pos = MENU_FindItem( &hMenu, &wItemID, wFlags )) == ITEM_NOT_FOUND) {
         SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
         return 0;
     }
+    MENU_GetMenu(hMenu, &menu);
+    item = &menu.items[pos];
     if (!item->text) return 0;
     if (!str || !nMaxSiz) return strlenW(item->text);
     if (!WideCharToMultiByte( CP_ACP, 0, item->text, -1, str, nMaxSiz, NULL, NULL ))
@@ -3774,13 +3787,17 @@ INT WINAPI GetMenuStringW( HMENU hMenu, 
                                LPWSTR str, INT nMaxSiz, UINT wFlags )
 {
     MENUITEM *item;
+    POPUPMENU menu;
+    UINT pos;
 
     TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
     if (str && nMaxSiz) str[0] = '\0';
-    if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
+    if ((pos = MENU_FindItem( &hMenu, &wItemID, wFlags )) == ITEM_NOT_FOUND) {
         SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
         return 0;
     }
+    MENU_GetMenu(hMenu, &menu);
+    item = &menu.items[pos];
     if (!str || !nMaxSiz) return item->text ? strlenW(item->text) : 0;
     if( !(item->text)) {
         str[0] = 0;
@@ -3800,7 +3817,8 @@ BOOL WINAPI HiliteMenuItem( HWND hWnd, H
     POPUPMENU tmpmenu;
     LPPOPUPMENU menu = &tmpmenu;
     TRACE("(%p, %p, %04x, %04x);\n", hWnd, hMenu, wItemID, wHilite);
-    if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
+    if (MENU_FindItem( &hMenu, &wItemID, wHilite ) == ITEM_NOT_FOUND)
+        return FALSE;
     if (MENU_GetMenu(hMenu, menu)) return FALSE;
     if (MENU_GetLocalMenu(hMenu)->FocusedItem == wItemID) return TRUE;
     MENU_HideSubPopups( hWnd, hMenu, FALSE );
@@ -3815,8 +3833,14 @@ BOOL WINAPI HiliteMenuItem( HWND hWnd, H
 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
 {
     MENUITEM *item;
+    UINT pos;
+    POPUPMENU menu;
     TRACE("(menu=%p, id=%04x, flags=%04x);\n", hMenu, wItemID, wFlags);
-    if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
+    if ((pos = MENU_FindItem( &hMenu, &wItemID, wFlags )) == ITEM_NOT_FOUND)
+        return -1;
+    MENU_GetMenu(hMenu, &menu);
+    item = &menu.items[pos];
+
     debug_print_menuitem ("  item: ", item, "");
     if (item->fType & MF_POPUP)
     {
@@ -3854,8 +3878,13 @@ INT WINAPI GetMenuItemCount( HMENU hMenu
 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
 {
     MENUITEM * lpmi;
+    POPUPMENU menu;
+    UINT pos;
 
-    if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return -1;
+    if ((pos = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))
+        == ITEM_NOT_FOUND) return -1;
+    MENU_GetMenu(hMenu, &menu);
+    lpmi = &menu.items[pos];
     if (lpmi->fType & MF_POPUP) return -1;
     return lpmi->wID;
 
@@ -3950,10 +3979,13 @@ BOOL WINAPI RemoveMenu( HMENU hMenu, UIN
     POPUPMENU tmpmenu;
     LPPOPUPMENU menu = &tmpmenu;
     MENUITEM *item;
+    UINT pos;
 
     TRACE("(menu=%p pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
-    if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
+    if ((pos = MENU_FindItem( &hMenu, &nPos, wFlags )) == ITEM_NOT_FOUND)
+        return FALSE;
     if (MENU_GetMenu(hMenu, menu)) return FALSE;
+    item = &menu->items[pos];
 
       /* Remove item */
 
@@ -3985,8 +4017,13 @@ BOOL WINAPI RemoveMenu( HMENU hMenu, UIN
  */
 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
 {
-    MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
-    if (!item) return FALSE;
+    MENUITEM *item;
+    POPUPMENU menu;
+    UINT pos;
+    if ((pos = MENU_FindItem( &hMenu, &nPos, wFlags )) == ITEM_NOT_FOUND)
+        return FALSE;
+    MENU_GetMenu(hMenu, &menu);
+    item = &menu.items[pos];
     if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
       /* nPos is now the position of the item */
     RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
@@ -4003,14 +4040,17 @@ BOOL WINAPI ModifyMenuW( HMENU hMenu, UI
     MENUITEM *item;
     POPUPMENU tmpmenu;
     POPUPMENU *menu = &tmpmenu;
+    UINT i;
 
     if (IS_STRING_ITEM(flags))
         TRACE("%p %d %04x %04x %s\n", hMenu, pos, flags, id, debugstr_w(str) );
     else
         TRACE("%p %d %04x %04x %p\n", hMenu, pos, flags, id, str );
 
-    if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
+    if ((i = MENU_FindItem( &hMenu, &pos, flags )) == ITEM_NOT_FOUND)
+        return FALSE;
     MENU_GetMenu(hMenu, menu);
+    item = &menu->items[i];
     menu->Height = 0; /* force size recalculate */
     MENU_UpdateMenu(hMenu, menu, SET_MI_HEIGHT);
     return MENU_SetItemData( item, flags, id, str );
@@ -4076,9 +4116,14 @@ BOOL WINAPI SetMenuItemBitmaps( HMENU hM
                                     HBITMAP hNewUnCheck, HBITMAP hNewCheck)
 {
     MENUITEM *item;
+    POPUPMENU menu;
+    UINT pos;
     TRACE("(%p, %04x, %04x, %p, %p)\n",
           hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
-    if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
+    if ((pos = MENU_FindItem( &hMenu, &nPos, wFlags )) == ITEM_NOT_FOUND)
+        return FALSE;
+    MENU_GetMenu(hMenu, &menu);
+    item = &menu.items[pos];
 
     if (!hNewCheck && !hNewUnCheck)
     {
@@ -4310,8 +4355,13 @@ BOOL WINAPI SetMenu( HWND hWnd, HMENU hM
 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
 {
     MENUITEM * lpmi;
+    POPUPMENU menu;
+    UINT pos;
 
-    if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
+    if ((pos = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))
+        == ITEM_NOT_FOUND) return 0;
+    MENU_GetMenu(hMenu, &menu);
+    lpmi = &menu.items[pos];
     if (!(lpmi->fType & MF_POPUP)) return 0;
     return lpmi->hSubMenu;
 }
@@ -4433,7 +4483,8 @@ HMENU16 WINAPI LookupMenuHandle16( HMENU
 {
     HMENU hmenu32 = HMENU_32(hmenu);
     UINT id32 = id;
-    if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
+    if (MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND ) == ITEM_NOT_FOUND)
+        return 0;
     else return HMENU_16(hmenu32);
 }
 
@@ -4582,7 +4633,13 @@ BOOL WINAPI IsMenu(HMENU hmenu)
 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
 					LPMENUITEMINFOW lpmii, BOOL unicode)
 {
-    MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
+    MENUITEM *menu;
+    POPUPMENU pm;
+    UINT pos;
+    
+    pos = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
+    MENU_GetMenu(hmenu, &pm);
+    menu = (pos == ITEM_NOT_FOUND) ? NULL : &pm.items[pos];
 
     debug_print_menuitem("GetMenuItemInfo_common: ", menu, "");
 
@@ -4845,6 +4902,8 @@ BOOL WINAPI SetMenuItemInfoA(HMENU hmenu
                                  const MENUITEMINFOA *lpmii)
 {
     MENUITEMINFOA mii;
+    POPUPMENU menu;
+    UINT pos;
     if( lpmii->cbSize != sizeof( mii) &&
             lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
         SetLastError( ERROR_INVALID_PARAMETER);
@@ -4855,8 +4914,11 @@ BOOL WINAPI SetMenuItemInfoA(HMENU hmenu
         mii.cbSize = sizeof( mii);
         mii.hbmpItem = NULL;
     }
-    return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
-				    (const MENUITEMINFOW *)&mii, FALSE);
+    pos = MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0);
+    MENU_GetMenu(hmenu, &menu);
+    return SetMenuItemInfo_common(pos == ITEM_NOT_FOUND ? NULL
+                                  : &menu.items[pos],
+                                  (const MENUITEMINFOW *)&mii, FALSE);
 }
 
 /**********************************************************************
@@ -4866,6 +4928,8 @@ BOOL WINAPI SetMenuItemInfoW(HMENU hmenu
                                  const MENUITEMINFOW *lpmii)
 {
     MENUITEMINFOW mii;
+    POPUPMENU menu;
+    UINT pos;
     if( lpmii->cbSize != sizeof( mii) &&
             lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
         SetLastError( ERROR_INVALID_PARAMETER);
@@ -4876,8 +4940,10 @@ BOOL WINAPI SetMenuItemInfoW(HMENU hmenu
         mii.cbSize = sizeof( mii);
         mii.hbmpItem = NULL;
     }
-    return SetMenuItemInfo_common(MENU_FindItem(&hmenu,
-                &item, bypos? MF_BYPOSITION : 0), &mii, TRUE);
+    pos = MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0);
+    MENU_GetMenu(hmenu, &menu);
+    return SetMenuItemInfo_common(pos == ITEM_NOT_FOUND ? NULL
+                                  : &menu.items[pos], &mii, TRUE);
 }
 
 /**********************************************************************
@@ -5024,31 +5090,34 @@ BOOL WINAPI CheckMenuRadioItem(HMENU hMe
 				   UINT first, UINT last, UINT check,
 				   UINT bypos)
 {
-     MENUITEM *mifirst, *milast, *micheck;
+     UINT posfirst, poslast, poscheck;
      HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
+     POPUPMENU menu;
 
      TRACE("%p: %d-%d, check %d, bypos=%d\n", hMenu, first, last, check, bypos);
 
-     mifirst = MENU_FindItem (&mfirst, &first, bypos);
-     milast = MENU_FindItem (&mlast, &last, bypos);
-     micheck = MENU_FindItem (&mcheck, &check, bypos);
+     posfirst = MENU_FindItem (&mfirst, &first, bypos);
+     poslast = MENU_FindItem (&mlast, &last, bypos);
+     poscheck = MENU_FindItem (&mcheck, &check, bypos);
 
-     if (mifirst == NULL || milast == NULL || micheck == NULL ||
-	 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
-	 micheck > milast || micheck < mifirst)
+     if (posfirst == ITEM_NOT_FOUND || poslast == ITEM_NOT_FOUND
+         || poscheck == ITEM_NOT_FOUND || posfirst > poslast
+         || mfirst != mlast || mfirst != mcheck
+         || poscheck > poslast || poscheck < posfirst)
 	  return FALSE;
 
-     while (mifirst <= milast)
+     MENU_GetMenu(mfirst, &menu);
+     while (posfirst <= poslast)
      {
-	  if (mifirst == micheck)
+	  if (posfirst == poscheck)
 	  {
-	       mifirst->fType |= MFT_RADIOCHECK;
-	       mifirst->fState |= MFS_CHECKED;
+               menu.items[posfirst].fType |= MFT_RADIOCHECK;
+               menu.items[posfirst].fState |= MFS_CHECKED;
 	  } else {
-	       mifirst->fType &= ~MFT_RADIOCHECK;
-	       mifirst->fState &= ~MFS_CHECKED;
+               menu.items[posfirst].fType &= ~MFT_RADIOCHECK;
+               menu.items[posfirst].fState &= ~MFS_CHECKED;
 	  }
-	  mifirst++;
+	  posfirst++;
      }
 
      return TRUE;
@@ -5070,10 +5139,13 @@ BOOL WINAPI GetMenuItemRect (HWND hwnd, 
      POPUPMENU *itemMenu = &tmpitemMenu;
      MENUITEM *item;
      HWND referenceHwnd;
+     UINT pos;
 
      TRACE("(%p,%p,%d,%p)\n", hwnd, hMenu, uItem, rect);
 
-     item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
+     pos = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
+     MENU_GetMenu(hMenu, itemMenu);
+     item = (pos == ITEM_NOT_FOUND) ? NULL : &itemMenu->items[pos];
      referenceHwnd = hwnd;
 
      if(!hwnd)

