[WIN32K] -MENU_TrackMouseMenuBar: Use IntGetSystemMenu instead of get_win_sys_menu...
[reactos.git] / reactos / win32ss / user / ntuser / menu.c
index d672ef3..586e64b 100644 (file)
@@ -2,7 +2,7 @@
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
  * PURPOSE:          Menus
- * FILE:             subsys/win32k/ntuser/menu.c
+ * FILE:             win32ss/user/ntuser/menu.c
  * PROGRAMER:        Thomas Weidenmueller (w3seek@users.sourceforge.net)
  */
 
@@ -242,6 +242,15 @@ PMENU FASTCALL VerifyMenu(PMENU pMenu)
    return pMenu;
 }
 
+BOOL
+FASTCALL
+IntIsMenu(HMENU Menu)
+{
+  if (UserGetMenuObject(Menu)) return TRUE;
+  return FALSE;
+}
+
+
 PMENU WINAPI
 IntGetMenu(HWND hWnd)
 {
@@ -313,13 +322,9 @@ UserDestroyMenuObject(PVOID Object)
 BOOL FASTCALL
 IntDestroyMenuObject(PMENU Menu, BOOL bRecurse)
 {
-   if(Menu)
+   if (Menu)
    {
       PWND Window;
-      ULONG Error;
-
-      /* Remove all menu items */
-      IntDestroyMenu( Menu, bRecurse);
 
       if (PsGetCurrentProcessSessionId() == Menu->head.rpdesk->rpwinstaParent->dwSessionId)
       {
@@ -341,22 +346,14 @@ IntDestroyMenuObject(PMENU Menu, BOOL bRecurse)
                }
             }
          }
-         if (UserObjectInDestroy(Menu->head.h))
-         {
-            WARN("Menu already dead!\n");
-            return FALSE;
-         }
+
+         if (!UserMarkObjectDestroy(Menu)) return TRUE;
+
+         /* Remove all menu items */
+         IntDestroyMenu( Menu, bRecurse);
+
          ret = UserDeleteObject(Menu->head.h, TYPE_MENU);
-         if (!ret)
-         {  // Make sure it is really dead or just marked for deletion.
-            Error = EngGetLastError();
-            ret = UserObjectInDestroy(Menu->head.h);
-            if (ret && EngGetLastError() == ERROR_INVALID_HANDLE)
-            {
-               EngSetLastError(Error);
-               ret = FALSE;
-            }
-         }  // See test_subpopup_locked_by_menu tests....
+         TRACE("IntDestroyMenuObject %d\n",ret);
          return ret;
       }
    }
@@ -1327,7 +1324,7 @@ IntGetMenuDefaultItem(PMENU MenuObject, UINT fByPos, UINT gmdiFlags, DWORD *gism
    if ( (!(GMDI_USEDISABLED & gmdiFlags)) && (MenuItem->fState & MFS_DISABLED )) return -1;
 
    /* search rekursiv when needed */
-   if ( (MenuItem->fType & MF_POPUP) && (gmdiFlags & GMDI_GOINTOPOPUPS) && MenuItem->spSubMenu)
+   if ( (gmdiFlags & GMDI_GOINTOPOPUPS) && MenuItem->spSubMenu )
    {
       UINT ret;
       (*gismc)++;
@@ -2210,9 +2207,9 @@ static void FASTCALL MENU_DrawMenuItem(PWND Wnd, PMENU Menu, PWND WndOwner, HDC
 
     if (lpitem->fType & MF_SYSMENU)
     {
-        if ( (Wnd->style & WS_MINIMIZE))
+        if (!(Wnd->style & WS_MINIMIZE))
         {
-          UserGetInsideRectNC(Wnd, &rect);
+          NC_GetInsideRect(Wnd, &rect);
           UserDrawSysMenuButton(Wnd, hdc, &rect, lpitem->fState & (MF_HILITE | MF_MOUSESELECT));
        }
         return;
@@ -2266,6 +2263,7 @@ static void FASTCALL MENU_DrawMenuItem(PWND Wnd, PMENU Menu, PWND WndOwner, HDC
         ** the menu owner has finished drawing.
         */
         DRAWITEMSTRUCT dis;
+        COLORREF old_bk, old_text;
 
         dis.CtlType   = ODT_MENU;
         dis.CtlID     = 0;
@@ -2289,9 +2287,13 @@ static void FASTCALL MENU_DrawMenuItem(PWND Wnd, PMENU Menu, PWND WndOwner, HDC
                dis.hDC, dis.rcItem.left, dis.rcItem.top, dis.rcItem.right,
                dis.rcItem.bottom);
         TRACE("Ownerdraw: Width %d Height %d\n", dis.rcItem.right-dis.rcItem.left, dis.rcItem.bottom-dis.rcItem.top);
+        old_bk = GreGetBkColor(hdc);
+        old_text = GreGetTextColor(hdc);
         co_IntSendMessage(UserHMGetHandle(WndOwner), WM_DRAWITEM, 0, (LPARAM) &dis);
+        IntGdiSetBkColor(hdc, old_bk);
+        IntGdiSetTextColor(hdc, old_text);
         /* Draw the popup-menu arrow */
-        if (lpitem->spSubMenu)
+        if (!menuBar && lpitem->spSubMenu)
         {
             RECT rectTemp;
             RtlCopyMemory(&rectTemp, &rect, sizeof(RECT));
@@ -2582,7 +2584,7 @@ static void FASTCALL MENU_DrawPopupMenu(PWND wnd, HDC hdc, PMENU menu )
                 UINT u;
 
                 item = menu->rgItems;
-                for (u = 0; u < menu->cItems; u++, item++)
+                for( u = menu->cItems; u > 0; u--, item++)
                 {
                     MENU_DrawMenuItem(wnd, menu, menu->spwndNotify, hdc, item,
                                          menu->cyMenu, FALSE, ODA_DRAWENTIRE);
@@ -2701,7 +2703,13 @@ UINT MENU_DrawMenuBar( HDC hDC, LPRECT lprect, PWND pWnd, BOOL suppress_draw )
     HFONT hfontOld = 0;
     PMENU lppop = UserGetMenuObject(UlongToHandle(pWnd->IDMenu));
 
-    if (lppop == NULL || lprect == NULL)
+    if (lppop == NULL)
+    {
+        // No menu. Do not reserve any space
+        return 0;
+    }
+
+    if (lprect == NULL)
     {
         return UserGetSystemMetrics(SM_CYMENU);
     }
@@ -2932,11 +2940,15 @@ static void FASTCALL MENU_SelectItem(PWND pwndOwner, PMENU menu, UINT wIndex,
                                     BOOL sendMenuSelect, PMENU topmenu)
 {
     HDC hdc;
-    PWND pWnd = ValidateHwndNoErr(menu->hWnd);
+    PWND pWnd;
 
     TRACE("M_SI: owner=%p menu=%p index=0x%04x select=0x%04x\n", pwndOwner, menu, wIndex, sendMenuSelect);
 
-    if (!menu || !menu->cItems || !pWnd) return;
+    if (!menu || !menu->cItems) return;
+
+    pWnd = ValidateHwndNoErr(menu->hWnd);
+
+    if (!pWnd) return;
 
     if (menu->iItem == wIndex) return;
 
@@ -3107,6 +3119,8 @@ static PMENU FASTCALL MENU_ShowSubPopup(PWND WndOwner, PMENU Menu, BOOL SelectFi
 
   TRACE("owner=%x menu=%p 0x%04x\n", WndOwner, Menu, SelectFirst);
 
+  if (!Menu) return Menu;
+
   if (Menu->iItem == NO_SELECTED_ITEM) return Menu;
   
   Item = &Menu->rgItems[Menu->iItem];
@@ -3445,7 +3459,15 @@ static BOOL FASTCALL MENU_MouseMove(MTRACKER *pmt, PMENU PtMenu, UINT Flags)
   if ( PtMenu )
   {
       if (IS_SYSTEM_MENU(PtMenu))
+      {
           Index = 0;
+          //// ReactOS only HACK: CORE-2338
+          // Windows tracks mouse moves to the system menu but does not open it.
+          // Only keyboard tracking can do that.
+          //
+          TRACE("SystemMenu\n");
+          return TRUE; // Stay inside the Loop!
+      }
       else
           MENU_FindItemByCoords( PtMenu, pmt->Pt, &Index );
   }
@@ -3564,7 +3586,7 @@ static LRESULT FASTCALL MENU_DoNextMenu(MTRACKER* pmt, UINT Vk, UINT wFlags)
           {
               /* switch to the system menu */
               MenuTmp = get_win_sys_menu(hNewWnd);
-              hNewMenu = UserHMGetHandle(MenuTmp);
+              if (MenuTmp) hNewMenu = UserHMGetHandle(MenuTmp);
           }
           else
               return FALSE;
@@ -3906,7 +3928,7 @@ static INT FASTCALL MENU_TrackMenu(PMENU pmenu, UINT wFlags, INT x, INT y,
         IntTranslateKbdMessage(&msg, 0);
         mt.Pt = msg.pt;
 
-        if ( (msg.hwnd == mt.CurrentMenu->hWnd) || ((msg.message!=WM_TIMER) || (msg.message!=WM_SYSTIMER)) )
+        if ( (msg.hwnd == mt.CurrentMenu->hWnd) || ((msg.message!=WM_TIMER) && (msg.message!=WM_SYSTIMER)) )
             enterIdleSent=FALSE;
 
         fRemove = FALSE;
@@ -3929,7 +3951,11 @@ static INT FASTCALL MENU_TrackMenu(PMENU pmenu, UINT wFlags, INT x, INT y,
 
                 case WM_RBUTTONDBLCLK:
                 case WM_RBUTTONDOWN:
-                     if (!(wFlags & TPM_RIGHTBUTTON)) break;
+                     if (!(wFlags & TPM_RIGHTBUTTON))
+                     {
+                        if ( msg.message == WM_RBUTTONDBLCLK ) fInsideMenuLoop = FALSE; // Must exit or loop forever!
+                        break;
+                     }
                     /* fall through */
                 case WM_LBUTTONDBLCLK:
                 case WM_LBUTTONDOWN:
@@ -3937,7 +3963,8 @@ static INT FASTCALL MENU_TrackMenu(PMENU pmenu, UINT wFlags, INT x, INT y,
                     /* Else, end menu tracking */
                     fRemove = MENU_ButtonDown(&mt, pmMouse, wFlags);
                     fInsideMenuLoop = fRemove;
-                    if (msg.message == WM_LBUTTONDBLCLK) fInsideMenuLoop = FALSE; // Must exit or loop forever!
+                    if ( msg.message == WM_LBUTTONDBLCLK ||
+                         msg.message == WM_RBUTTONDBLCLK ) fInsideMenuLoop = FALSE; // Must exit or loop forever!
                     break;
 
                 case WM_RBUTTONUP:
@@ -4098,9 +4125,12 @@ static INT FASTCALL MENU_TrackMenu(PMENU pmenu, UINT wFlags, INT x, INT y,
            if (mt.TopMenu->fFlags & MNF_POPUP)
            {
               PWND pwndTM = ValidateHwndNoErr(mt.TopMenu->hWnd);
-              IntNotifyWinEvent(EVENT_SYSTEM_MENUPOPUPEND, pwndTM, OBJID_CLIENT, CHILDID_SELF, 0);
+              if (pwndTM)
+              {
+                 IntNotifyWinEvent(EVENT_SYSTEM_MENUPOPUPEND, pwndTM, OBJID_CLIENT, CHILDID_SELF, 0);
 
-              co_UserDestroyWindow(pwndTM);
+                 co_UserDestroyWindow(pwndTM);
+              }
               mt.TopMenu->hWnd = NULL;
 
               if (!(wFlags & TPM_NONOTIFY))
@@ -4210,7 +4240,7 @@ static BOOL FASTCALL MENU_ExitTracking(PWND pWnd, BOOL bPopup, UINT wFlags)
  */
 VOID MENU_TrackMouseMenuBar( PWND pWnd, ULONG ht, POINT pt)
 {
-    PMENU pMenu = (ht == HTSYSMENU) ? get_win_sys_menu( UserHMGetHandle(pWnd) ) : IntGetMenu( UserHMGetHandle(pWnd) );
+    PMENU pMenu = (ht == HTSYSMENU) ? IntGetSystemMenu(pWnd, FALSE) : IntGetMenu( UserHMGetHandle(pWnd) );
     UINT wFlags = TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
 
     TRACE("wnd=%p ht=0x%04x (%ld,%ld)\n", pWnd, ht, pt.x, pt.y);
@@ -4331,6 +4361,9 @@ BOOL WINAPI IntTrackPopupMenuEx( PMENU menu, UINT wFlags, int x, int y,
           co_IntSendMessage( UserHMGetHandle(pWnd), WM_INITMENUPOPUP, (WPARAM) UserHMGetHandle(menu), 0);
        }
 
+       if (menu->fFlags & MNF_SYSMENU)
+          MENU_InitSysMenuPopup( menu, pWnd->style, pWnd->pcls->style, HTSYSMENU);
+
        if (MENU_ShowPopup(pWnd, menu, 0, wFlags, x, y, 0, 0 ))
           ret = MENU_TrackMenu( menu, wFlags | TPM_POPUPMENU, 0, 0, pWnd,
                                 lpTpm ? &lpTpm->rcExclude : NULL);
@@ -4713,7 +4746,7 @@ UINT FASTCALL IntGetMenuState( HMENU hMenu, UINT uId, UINT uFlags)
 
    if (pItem->spSubMenu)
    {
-      return (pItem->spSubMenu->cItems << 8) | ((pItem->fState|pItem->fType) & 0xff);
+      return (pItem->spSubMenu->cItems << 8) | ((pItem->fState|pItem->fType|MF_POPUP) & 0xff);
    }
    else
       return (pItem->fType | pItem->fState);
@@ -4771,7 +4804,7 @@ HMENU FASTCALL UserCreateMenu(PDESKTOP Desktop, BOOL PopupMenu)
        */
 
       Status = IntValidateWindowStationHandle(CurrentProcess->Win32WindowStation,
-                     KernelMode,
+                     UserMode,
                      0,
                      &WinStaObject,
                      0);
@@ -4872,6 +4905,10 @@ UserMenuItemInfo(
 
    if (!(MenuItem = MENU_FindItem( &Menu, &Item, (ByPosition ? MF_BYPOSITION : MF_BYCOMMAND) )))
    {
+      /* workaround for Word 95: pretend that SC_TASKLIST item exists. */
+      if ( SetOrGet && Item == SC_TASKLIST && !ByPosition )
+         return TRUE;
+
       EngSetLastError(ERROR_MENU_ITEM_NOT_FOUND);
       return( FALSE);
    }
@@ -5085,7 +5122,7 @@ PMENU FASTCALL MENU_GetSystemMenu(PWND Window, PMENU Popup)
 
       ItemInfo.cbSize = sizeof(MENUITEMINFOW);
       ItemInfo.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_SUBMENU;
-      ItemInfo.fType = 0;
+      ItemInfo.fType = MF_POPUP;
       ItemInfo.fState = MFS_ENABLED;
       ItemInfo.dwTypeData = NULL;
       ItemInfo.cch = 0;
@@ -5154,7 +5191,7 @@ IntSetSystemMenu(PWND Window, PMENU Menu)
       if (OldMenu)
       {
           OldMenu->fFlags &= ~MNF_SYSMENU;
-         IntDestroyMenuObject(OldMenu, TRUE);
+          IntDestroyMenuObject(OldMenu, TRUE);
       }
    }
 
@@ -6008,6 +6045,41 @@ CLEANUP:
 }
 
 
+DWORD
+APIENTRY
+NtUserPaintMenuBar(
+    HWND hWnd,
+    HDC hDC,
+    ULONG leftBorder,
+    ULONG rightBorder,
+    ULONG top,
+    BOOL bActive)
+{
+   PWND Window;
+   RECT Rect;
+   DWORD ret;
+
+   UserEnterExclusive();
+
+   if(!(Window = UserGetWindowObject(hWnd)))
+   {
+      EngSetLastError(ERROR_INVALID_WINDOW_HANDLE);
+      UserLeave();
+      return 0;
+   }
+
+   Rect.left = leftBorder;
+   Rect.right = Window->rcWindow.right - Window->rcWindow.left - rightBorder;
+   Rect.top = top;
+   Rect.bottom = 0;
+
+   ret = MENU_DrawMenuBar(hDC, &Rect, Window, FALSE);
+
+   UserLeave();
+
+   return ret;
+}
+
 /*
  * @implemented
  */
@@ -6211,13 +6283,13 @@ NtUserThunkedMenuItemInfo(
    /* lpszCaption may be NULL, check for it and call RtlInitUnicodeString()
       if bInsert == TRUE call UserInsertMenuItem() else UserSetMenuItemInfo()   */
 
+   RtlInitEmptyUnicodeString(&lstrCaption, NULL, 0);
+
    if (!(Menu = UserGetMenuObject(hMenu)))
    {
       RETURN(FALSE);
    }
 
-   RtlInitUnicodeString(&lstrCaption, 0);
-
    /* Check if we got a Caption */
    if (lpszCaption && lpszCaption->Buffer)
    {
@@ -6238,6 +6310,11 @@ NtUserThunkedMenuItemInfo(
    RETURN( UserMenuItemInfo(Menu, uItem, fByPosition, (PROSMENUITEMINFO)lpmii, TRUE, &lstrCaption));
 
 CLEANUP:
+   if (lstrCaption.Buffer)
+   {
+      ReleaseCapturedUnicodeString(&lstrCaption, UserMode);
+   }
+
    TRACE("Leave NtUserThunkedMenuItemInfo, ret=%i\n",_ret_);
    UserLeave();
    END_CLEANUP;