vcl/inc/unx/gtk/gtksalmenu.hxx |    2 -
 vcl/unx/gtk3/gtksalmenu.cxx    |   52 +++++++++++++++++------------------------
 2 files changed, 23 insertions(+), 31 deletions(-)

New commits:
commit bf0c4166478c6e124bb4969e16444c8f478b8989
Author:     Michael Weghorn <[email protected]>
AuthorDate: Wed Nov 20 08:44:32 2024 +0100
Commit:     Michael Weghorn <[email protected]>
CommitDate: Wed Nov 20 13:19:09 2024 +0100

    gtk: Switch param from const char* to const OString&
    
    Change-Id: Ie92591770a2828a5f828d76d8c1bf7e5a253c3de
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176814
    Tested-by: Jenkins
    Reviewed-by: Michael Weghorn <[email protected]>

diff --git a/vcl/inc/unx/gtk/gtksalmenu.hxx b/vcl/inc/unx/gtk/gtksalmenu.hxx
index 1a152a7142e1..eadb096e0c1e 100644
--- a/vcl/inc/unx/gtk/gtksalmenu.hxx
+++ b/vcl/inc/unx/gtk/gtksalmenu.hxx
@@ -103,7 +103,7 @@ public:
                                                       MenuItemBits nBits,
                                                       bool bChecked,
                                                       bool bIsSubmenu );
-    void                        NativeSetEnableItem( gchar const * aCommand, 
gboolean bEnable );
+    void                        NativeSetEnableItem(const OString& sCommand, 
gboolean bEnable);
     void                        NativeCheckItem( unsigned nSection, unsigned 
nItemPos, MenuItemBits bits, gboolean bCheck );
     void                        NativeSetAccelerator( unsigned nSection, 
unsigned nItemPos, const vcl::KeyCode& rKeyCode, std::u16string_view rKeyName );
 
diff --git a/vcl/unx/gtk3/gtksalmenu.cxx b/vcl/unx/gtk3/gtksalmenu.cxx
index e187cbe40212..efe4da3dde44 100644
--- a/vcl/unx/gtk3/gtksalmenu.cxx
+++ b/vcl/unx/gtk3/gtksalmenu.cxx
@@ -308,7 +308,7 @@ void GtkSalMenu::ImplUpdate(bool bRecurse, bool 
bRemoveDisabledEntries)
             NativeSetAccelerator(nSection, nItemPos, nAccelKey, 
nAccelKey.GetName());
             NativeSetItemCommand(nSection, nItemPos, nId, 
sNativeCommand.getStr(), itemBits, bChecked, false);
             NativeCheckItem( nSection, nItemPos, itemBits, bChecked );
-            NativeSetEnableItem(sNativeCommand.getStr(), bEnabled);
+            NativeSetEnableItem(sNativeCommand, bEnabled);
 
             pNewCommandList = g_list_append(pNewCommandList, 
g_strdup(sNativeCommand.getStr()));
         }
@@ -383,7 +383,7 @@ void GtkSalMenu::ImplUpdate(bool bRecurse, bool 
bRemoveDisabledEntries)
         g_lo_menu_insert_in_section(pLOMenu, nSection-1, 0,
                                     OUStringToOString(aPlaceholderText, 
RTL_TEXTENCODING_UTF8).getStr());
         NativeSetItemCommand(nSection - 1, 0, 0xFFFF, sNativeCommand.getStr(), 
MenuItemBits::NONE, false, false);
-        NativeSetEnableItem(sNativeCommand.getStr(), false);
+        NativeSetEnableItem(sNativeCommand, false);
     }
 }
 
@@ -1227,13 +1227,13 @@ void GtkSalMenu::NativeCheckItem( unsigned nSection, 
unsigned nItemPos, MenuItem
         g_free( aCommand );
 }
 
-void GtkSalMenu::NativeSetEnableItem( gchar const * aCommand, gboolean bEnable 
)
+void GtkSalMenu::NativeSetEnableItem(const OString& sCommand, gboolean bEnable)
 {
     SolarMutexGuard aGuard;
     GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP( mpActionGroup );
 
-    if ( g_action_group_get_action_enabled( G_ACTION_GROUP( pActionGroup ), 
aCommand ) != bEnable )
-        g_lo_action_group_set_action_enabled( pActionGroup, aCommand, bEnable 
);
+    if (g_action_group_get_action_enabled(G_ACTION_GROUP(pActionGroup), 
sCommand.getStr()) != bEnable)
+        g_lo_action_group_set_action_enabled(pActionGroup, sCommand.getStr(), 
bEnable);
 }
 
 void GtkSalMenu::NativeSetItemText( unsigned nSection, unsigned nItemPos, 
const OUString& rText, bool bFireEvent )
@@ -1571,7 +1571,7 @@ void GtkSalMenu::EnableItem( unsigned nPos, bool bEnable )
     if ( bUnityMode && !mbInActivateCallback && !mbNeedsUpdate && 
GetTopLevel()->mbMenuBar && ( nPos < maItems.size() ) )
     {
         OString sCommand = GetCommandForItem(GetItemAtPos(nPos));
-        NativeSetEnableItem(sCommand.getStr(), bEnable);
+        NativeSetEnableItem(sCommand, bEnable);
     }
 }
 
commit a560f22c79c18df57b2e905fb7ff01e6ee2b5593
Author:     Michael Weghorn <[email protected]>
AuthorDate: Wed Nov 20 08:38:28 2024 +0100
Commit:     Michael Weghorn <[email protected]>
CommitDate: Wed Nov 20 13:19:01 2024 +0100

    gtk: Let GetCommandForItem return OString instead of char*
    
    This simplifies memory management, as it removes the
    need to manually free the memory using `g_free` again.
    
    Change-Id: I71f8e6a8df46c0090f2f003d3f443500028b1f83
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176813
    Tested-by: Jenkins
    Reviewed-by: Michael Weghorn <[email protected]>

diff --git a/vcl/unx/gtk3/gtksalmenu.cxx b/vcl/unx/gtk3/gtksalmenu.cxx
index a95d80a8c0ed..e187cbe40212 100644
--- a/vcl/unx/gtk3/gtksalmenu.cxx
+++ b/vcl/unx/gtk3/gtksalmenu.cxx
@@ -28,15 +28,15 @@ static bool bUnityMode = false;
 /*
  * This function generates a unique command name for each menu item
  */
-static gchar* GetCommandForItem(GtkSalMenu* pParentMenu, sal_uInt16 nItemId)
+static OString GetCommandForItem(GtkSalMenu* pParentMenu, sal_uInt16 nItemId)
 {
     OString aCommand = "window-" +
         OString::number(reinterpret_cast<sal_uIntPtr>(pParentMenu)) +
         "-" + OString::number(nItemId);
-    return g_strdup(aCommand.getStr());
+    return aCommand;
 }
 
-static gchar* GetCommandForItem(GtkSalMenuItem* pSalMenuItem)
+static OString GetCommandForItem(GtkSalMenuItem* pSalMenuItem)
 {
     return GetCommandForItem(pSalMenuItem->mpParentMenu,
                              pSalMenuItem->mnId);
@@ -297,20 +297,20 @@ void GtkSalMenu::ImplUpdate(bool bRecurse, bool 
bRemoveDisabledEntries)
             pOldCommandList = g_list_append( pOldCommandList, aCurrentCommand 
);
 
         // Get the new command for the item.
-        gchar* aNativeCommand = GetCommandForItem(pSalMenuItem);
+        OString sNativeCommand = GetCommandForItem(pSalMenuItem);
 
         // Force updating of native menu labels.
 
-        if ( g_strcmp0( aNativeCommand, "" ) != 0 && pSalMenuItem->mpSubMenu 
== nullptr )
+        if (!sNativeCommand.isEmpty() && pSalMenuItem->mpSubMenu == nullptr)
         {
             NativeSetItemText( nSection, nItemPos, aText, false );
             NativeSetItemIcon( nSection, nItemPos, aImage );
             NativeSetAccelerator(nSection, nItemPos, nAccelKey, 
nAccelKey.GetName());
-            NativeSetItemCommand( nSection, nItemPos, nId, aNativeCommand, 
itemBits, bChecked, false );
+            NativeSetItemCommand(nSection, nItemPos, nId, 
sNativeCommand.getStr(), itemBits, bChecked, false);
             NativeCheckItem( nSection, nItemPos, itemBits, bChecked );
-            NativeSetEnableItem( aNativeCommand, bEnabled );
+            NativeSetEnableItem(sNativeCommand.getStr(), bEnabled);
 
-            pNewCommandList = g_list_append( pNewCommandList, g_strdup( 
aNativeCommand ) );
+            pNewCommandList = g_list_append(pNewCommandList, 
g_strdup(sNativeCommand.getStr()));
         }
         else
         {
@@ -323,8 +323,8 @@ void GtkSalMenu::ImplUpdate(bool bRecurse, bool 
bRemoveDisabledEntries)
 
         if ( pSubmenu && pSubmenu->GetMenu() )
         {
-            bool bNonMenuChangedToMenu = NativeSetItemCommand( nSection, 
nItemPos, nId, aNativeCommand, itemBits, false, true );
-            pNewCommandList = g_list_append( pNewCommandList, g_strdup( 
aNativeCommand ) );
+            bool bNonMenuChangedToMenu = NativeSetItemCommand(nSection, 
nItemPos, nId, sNativeCommand.getStr(), itemBits, false, true);
+            pNewCommandList = g_list_append(pNewCommandList, 
g_strdup(sNativeCommand.getStr()));
 
             GLOMenu* pSubMenuModel = 
g_lo_menu_get_submenu_from_item_in_section( pLOMenu, nSection, nItemPos );
 
@@ -347,8 +347,6 @@ void GtkSalMenu::ImplUpdate(bool bRecurse, bool 
bRemoveDisabledEntries)
             g_object_unref( pSubMenuModel );
         }
 
-        g_free( aNativeCommand );
-
         ++nItemPos;
         ++validItems;
     }
@@ -380,13 +378,12 @@ void GtkSalMenu::ImplUpdate(bool bRecurse, bool 
bRemoveDisabledEntries)
     }
     if (!nItemsCount)
     {
-        gchar* aNativeCommand = GetCommandForItem(this, 0xFFFF);
+        OString sNativeCommand = GetCommandForItem(this, 0xFFFF);
         OUString 
aPlaceholderText(VclResId(SV_RESID_STRING_NOSELECTIONPOSSIBLE));
         g_lo_menu_insert_in_section(pLOMenu, nSection-1, 0,
                                     OUStringToOString(aPlaceholderText, 
RTL_TEXTENCODING_UTF8).getStr());
-        NativeSetItemCommand(nSection-1, 0, 0xFFFF, aNativeCommand, 
MenuItemBits::NONE, false, false);
-        NativeSetEnableItem(aNativeCommand, false);
-        g_free(aNativeCommand);
+        NativeSetItemCommand(nSection - 1, 0, 0xFFFF, sNativeCommand.getStr(), 
MenuItemBits::NONE, false, false);
+        NativeSetEnableItem(sNativeCommand.getStr(), false);
     }
 }
 
@@ -689,9 +686,8 @@ void GtkSalMenu::RemoveItem( unsigned nPos )
     if (mpActionGroup)
     {
         GLOActionGroup* pActionGroup = G_LO_ACTION_GROUP(mpActionGroup);
-        gchar* pCommand = GetCommandForItem(maItems[nPos]);
-        g_lo_action_group_remove(pActionGroup, pCommand);
-        g_free(pCommand);
+        OString sCommand = GetCommandForItem(maItems[nPos]);
+        g_lo_action_group_remove(pActionGroup, sCommand.getStr());
     }
 
     maItems.erase( maItems.begin() + nPos );
@@ -1574,9 +1570,8 @@ void GtkSalMenu::EnableItem( unsigned nPos, bool bEnable )
     SolarMutexGuard aGuard;
     if ( bUnityMode && !mbInActivateCallback && !mbNeedsUpdate && 
GetTopLevel()->mbMenuBar && ( nPos < maItems.size() ) )
     {
-        gchar* pCommand = GetCommandForItem( GetItemAtPos( nPos ) );
-        NativeSetEnableItem( pCommand, bEnable );
-        g_free( pCommand );
+        OString sCommand = GetCommandForItem(GetItemAtPos(nPos));
+        NativeSetEnableItem(sCommand.getStr(), bEnable);
     }
 }
 
@@ -1597,7 +1592,7 @@ void GtkSalMenu::SetItemText( unsigned nPos, SalMenuItem* 
pSalMenuItem, const OU
     if ( !bUnityMode || mbInActivateCallback || mbNeedsUpdate || 
!GetTopLevel()->mbMenuBar || ( nPos >= maItems.size() ) )
         return;
 
-    gchar* pCommand = GetCommandForItem( static_cast< GtkSalMenuItem* >( 
pSalMenuItem ) );
+    OString sCommand = 
GetCommandForItem(static_cast<GtkSalMenuItem*>(pSalMenuItem));
 
     gint nSectionsCount = g_menu_model_get_n_items( mpMenuModel );
     for ( gint nSection = 0; nSection < nSectionsCount; ++nSection )
@@ -1607,19 +1602,16 @@ void GtkSalMenu::SetItemText( unsigned nPos, 
SalMenuItem* pSalMenuItem, const OU
         {
             gchar* pCommandFromModel = 
g_lo_menu_get_command_from_item_in_section( G_LO_MENU( mpMenuModel ), nSection, 
nItem );
 
-            if ( !g_strcmp0( pCommandFromModel, pCommand ) )
+            if (pCommandFromModel == sCommand)
             {
                 NativeSetItemText( nSection, nItem, rText );
                 g_free( pCommandFromModel );
-                g_free( pCommand );
                 return;
             }
 
             g_free( pCommandFromModel );
         }
     }
-
-    g_free( pCommand );
 }
 
 void GtkSalMenu::SetItemImage( unsigned, SalMenuItem*, const Image& )

Reply via email to