vcl/osx/salmenu.cxx         |   10 ++++++++--
 vcl/unx/gtk3/gtksalmenu.cxx |   21 ++++++++++++++-------
 2 files changed, 22 insertions(+), 9 deletions(-)

New commits:
commit 0084db9109705594c997d7b93ad8c5d58f5abab1
Author:     Neil Roberts <[email protected]>
AuthorDate: Sun Sep 14 11:16:18 2025 +0200
Commit:     Noel Grandin <[email protected]>
CommitDate: Sun Sep 14 22:12:36 2025 +0200

    Handle removing shortcuts in gtksalmenu
    
    When a menu item has no shortcut its accelerator key code is set to
    zero. When the GTK backend encounters this it was just ignoring the key
    code and doing nothing to update the menu item. This meant that if an
    accelerator is changed from one menu item to another then removing the
    shortcut from the old menu item wouldn’t work. This patch fixes it to
    handle that case.
    
    The line calling g_strcmp0 looked suspicious. It was only called if
    aCurrentAccel was nullptr, in which case it’s not worth calling because
    a check whether aAccelerator is not nullptr would have the same effect.
    I wonder if it was meant to be || instead of &&. In either case
    g_strcmp0 is meant to handle null pointers so it seems like it should do
    the right thing without having to check if aCurrentAccel is null.
    
    This fixes the issues with the GTK backend mentioned in comment #8 of
    tdf#144560.
    
    Change-Id: Ica63a8666d2a4ca79a0c566dd43e7cb53ec9df67
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190930
    Reviewed-by: Noel Grandin <[email protected]>
    Tested-by: Jenkins

diff --git a/vcl/unx/gtk3/gtksalmenu.cxx b/vcl/unx/gtk3/gtksalmenu.cxx
index 4ec62d42550d..0e7795fc9e15 100644
--- a/vcl/unx/gtk3/gtksalmenu.cxx
+++ b/vcl/unx/gtk3/gtksalmenu.cxx
@@ -1231,18 +1231,25 @@ void GtkSalMenu::NativeSetAccelerator( unsigned 
nSection, unsigned nItemPos, con
 {
     SolarMutexGuard aGuard;
 
-    if ( rKeyName.empty() )
-        return;
+    gchar* aAccelerator;
 
-    guint nKeyCode;
-    GdkModifierType nModifiers;
-    GtkSalFrame::KeyCodeToGdkKey(rKeyCode, &nKeyCode, &nModifiers);
+    if ( rKeyName.empty() )
+    {
+        // An empty key name means we should remove the accelerator
+        aAccelerator = nullptr;
+    }
+    else
+    {
+        guint nKeyCode;
+        GdkModifierType nModifiers;
+        GtkSalFrame::KeyCodeToGdkKey(rKeyCode, &nKeyCode, &nModifiers);
 
-    gchar* aAccelerator = gtk_accelerator_name( nKeyCode, nModifiers );
+        aAccelerator = gtk_accelerator_name( nKeyCode, nModifiers );
+    }
 
     gchar* aCurrentAccel = g_lo_menu_get_accelerator_from_item_in_section( 
G_LO_MENU( mpMenuModel ), nSection, nItemPos );
 
-    if ( aCurrentAccel == nullptr && g_strcmp0( aCurrentAccel, aAccelerator ) 
!= 0 )
+    if ( g_strcmp0( aCurrentAccel, aAccelerator ) != 0 )
         g_lo_menu_set_accelerator_to_item_in_section ( G_LO_MENU( mpMenuModel 
), nSection, nItemPos, aAccelerator );
 
     g_free( aAccelerator );
commit da5b9ee231c1cce5d5c4d8ac3b39d3a047d74afe
Author:     Neil Roberts <[email protected]>
AuthorDate: Sun Sep 14 11:41:59 2025 +0200
Commit:     Noel Grandin <[email protected]>
CommitDate: Sun Sep 14 22:12:24 2025 +0200

    tdf#144560 Handle removing shortcuts in osx/salmenu
    
    When a menu item has no shortcut its accelerator key code is set to
    zero. When the MacOS backend encounters this it was just ignoring the
    key code and doing nothing to update the menu item. This meant that if
    an accelerator is changed from one menu item to another then removing
    the shortcut from the old menu item wouldn’t work. This patch fixes it
    to handle that case.
    
    Change-Id: I38fa09958b78b9cbc4ea8fb9b222d4671c91fb92
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190929
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <[email protected]>

diff --git a/vcl/osx/salmenu.cxx b/vcl/osx/salmenu.cxx
index 0c625dba27f3..d30771495aee 100644
--- a/vcl/osx/salmenu.cxx
+++ b/vcl/osx/salmenu.cxx
@@ -698,6 +698,8 @@ void AquaSalMenu::SetAccelerator( unsigned /*nPos*/, 
SalMenuItem* pSalMenuItem,
     sal_uInt16 nModifier;
     sal_Unicode nCommandKey = 0;
 
+    AquaSalMenuItem *pAquaSalMenuItem = static_cast<AquaSalMenuItem 
*>(pSalMenuItem);
+
     sal_uInt16 nKeyCode=rKeyCode.GetCode();
     if( nKeyCode )
     {
@@ -775,8 +777,13 @@ void AquaSalMenu::SetAccelerator( unsigned /*nPos*/, 
SalMenuItem* pSalMenuItem,
             }
         }
     }
-    else // not even a code ? nonsense -> ignore
+    else
+    {
+        // Zero key-code, used to remove the accelerator
+        [pAquaSalMenuItem->mpMenuItem setKeyEquivalent: @""];
+        [pAquaSalMenuItem->mpMenuItem setKeyEquivalentModifierMask: 0];
         return;
+    }
 
     SAL_WARN_IF( !nCommandKey, "vcl", "unmapped accelerator key" );
 
@@ -801,7 +808,6 @@ void AquaSalMenu::SetAccelerator( unsigned /*nPos*/, 
SalMenuItem* pSalMenuItem,
     if(nModifier & KEY_MOD3)
         nItemModifier |= NSEventModifierFlagControl;
 
-    AquaSalMenuItem *pAquaSalMenuItem = static_cast<AquaSalMenuItem 
*>(pSalMenuItem);
     NSString* pString = CreateNSString( OUString( &nCommandKey, 1 ) );
     [pAquaSalMenuItem->mpMenuItem setKeyEquivalent: pString];
     [pAquaSalMenuItem->mpMenuItem setKeyEquivalentModifierMask: nItemModifier];

Reply via email to