summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2016-06-17 21:15:20 +0100
committerCaolán McNamara <caolanm@redhat.com>2016-06-19 17:19:50 +0100
commit1409b974003ca69e4682507f5d34c55f346b864a (patch)
treee17754232d4cd1c457253bd2cfbe8a2656f6673e
parent0e2a6561043c23b712b534fc3d33336d6bfcc50d (diff)
Resolve: tdf#100429 gtk3: set custom icons in native gtk menus
Change-Id: I4798274f38c34c99d6f22f3c7959ebd9673a8966
-rw-r--r--vcl/inc/unx/gtk/glomenu.h10
-rw-r--r--vcl/inc/unx/gtk/gtksalmenu.hxx1
-rw-r--r--vcl/unx/gtk/glomenu.cxx37
-rw-r--r--vcl/unx/gtk/gtksalmenu.cxx32
4 files changed, 80 insertions, 0 deletions
diff --git a/vcl/inc/unx/gtk/glomenu.h b/vcl/inc/unx/gtk/glomenu.h
index b2ed606da19f..5586aa4edce1 100644
--- a/vcl/inc/unx/gtk/glomenu.h
+++ b/vcl/inc/unx/gtk/glomenu.h
@@ -69,11 +69,21 @@ void g_lo_menu_set_label (GLOMenu
gint position,
const gchar *label);
+void g_lo_menu_set_icon (GLOMenu *menu,
+ gint position,
+ const GIcon *icon);
+
+
void g_lo_menu_set_label_to_item_in_section (GLOMenu *menu,
gint section,
gint position,
const gchar *label);
+void g_lo_menu_set_icon_to_item_in_section (GLOMenu *menu,
+ gint section,
+ gint position,
+ const GIcon *icon);
+
gchar * g_lo_menu_get_label_from_item_in_section (GLOMenu *menu,
gint section,
gint position);
diff --git a/vcl/inc/unx/gtk/gtksalmenu.hxx b/vcl/inc/unx/gtk/gtksalmenu.hxx
index ba2c858d29fd..74f03dd729d3 100644
--- a/vcl/inc/unx/gtk/gtksalmenu.hxx
+++ b/vcl/inc/unx/gtk/gtksalmenu.hxx
@@ -93,6 +93,7 @@ public:
bool IsItemVisible( unsigned nPos );
void NativeSetItemText( unsigned nSection, unsigned nItemPos, const OUString& rText );
+ void NativeSetItemIcon( unsigned nSection, unsigned nItemPos, const Image& rImage );
bool NativeSetItemCommand( unsigned nSection,
unsigned nItemPos,
sal_uInt16 nId,
diff --git a/vcl/unx/gtk/glomenu.cxx b/vcl/unx/gtk/glomenu.cxx
index bf2b701b2b6b..ffa43e52c125 100644
--- a/vcl/unx/gtk/glomenu.cxx
+++ b/vcl/unx/gtk/glomenu.cxx
@@ -229,6 +229,23 @@ g_lo_menu_set_label (GLOMenu *menu,
}
void
+g_lo_menu_set_icon (GLOMenu *menu,
+ gint position,
+ const GIcon *icon)
+{
+ g_return_if_fail (G_IS_LO_MENU (menu));
+
+ GVariant *value;
+
+ if (icon != nullptr)
+ value = g_icon_serialize (const_cast<GIcon*>(icon));
+ else
+ value = nullptr;
+
+ g_lo_menu_set_attribute_value (menu, position, G_MENU_ATTRIBUTE_ICON, value);
+}
+
+void
g_lo_menu_set_label_to_item_in_section (GLOMenu *menu,
gint section,
gint position,
@@ -248,6 +265,26 @@ g_lo_menu_set_label_to_item_in_section (GLOMenu *menu,
g_object_unref (model);
}
+void
+g_lo_menu_set_icon_to_item_in_section (GLOMenu *menu,
+ gint section,
+ gint position,
+ const GIcon *icon)
+{
+ g_return_if_fail (G_IS_LO_MENU (menu));
+
+ GLOMenu *model = g_lo_menu_get_section (menu, section);
+
+ g_return_if_fail (model != nullptr);
+
+ g_lo_menu_set_icon (model, position, icon);
+
+ // Notify the update.
+ g_menu_model_items_changed (G_MENU_MODEL (model), position, 1, 1);
+
+ g_object_unref (model);
+}
+
gchar *
g_lo_menu_get_label_from_item_in_section (GLOMenu *menu,
gint section,
diff --git a/vcl/unx/gtk/gtksalmenu.cxx b/vcl/unx/gtk/gtksalmenu.cxx
index 19fd6a085246..af48f1b9ced1 100644
--- a/vcl/unx/gtk/gtksalmenu.cxx
+++ b/vcl/unx/gtk/gtksalmenu.cxx
@@ -18,6 +18,7 @@
#include <unx/gtk/gloactiongroup.h>
#include <vcl/floatwin.hxx>
#include <vcl/menu.hxx>
+#include <vcl/pngwrite.hxx>
#include <unx/gtk/gtkinst.hxx>
#if GTK_CHECK_VERSION(3,0,0)
@@ -236,6 +237,7 @@ void GtkSalMenu::ImplUpdate(bool bRecurse, bool bRemoveDisabledEntries)
// Get internal menu item values.
OUString aText = pVCLMenu->GetItemText( nId );
+ Image aImage = pVCLMenu->GetItemImage( nId );
bool bEnabled = pVCLMenu->IsItemEnabled( nId );
vcl::KeyCode nAccelKey = pVCLMenu->GetAccelKey( nId );
bool bChecked = pVCLMenu->IsItemChecked( nId );
@@ -252,6 +254,8 @@ void GtkSalMenu::ImplUpdate(bool bRecurse, bool bRemoveDisabledEntries)
// Force updating of native menu labels.
NativeSetItemText( nSection, nItemPos, aText );
+ if (!!aImage)
+ NativeSetItemIcon( nSection, nItemPos, aImage );
NativeSetAccelerator( nSection, nItemPos, nAccelKey, nAccelKey.GetName( GetFrame()->GetWindow() ) );
if ( g_strcmp0( aNativeCommand, "" ) != 0 && pSalMenuItem->mpSubMenu == nullptr )
@@ -844,6 +848,34 @@ void GtkSalMenu::NativeSetItemText( unsigned nSection, unsigned nItemPos, const
g_free( aLabel );
}
+namespace
+{
+ void DestroyMemoryStream(gpointer data)
+ {
+ SvMemoryStream* pMemStm = reinterpret_cast<SvMemoryStream*>(data);
+ delete pMemStm;
+ }
+}
+
+void GtkSalMenu::NativeSetItemIcon( unsigned nSection, unsigned nItemPos, const Image& rImage )
+{
+ SolarMutexGuard aGuard;
+
+ SvMemoryStream* pMemStm = new SvMemoryStream;
+ vcl::PNGWriter aWriter(rImage.GetBitmapEx());
+ aWriter.Write(*pMemStm);
+
+ GBytes *pBytes = g_bytes_new_with_free_func(pMemStm->GetData(),
+ pMemStm->Seek(STREAM_SEEK_TO_END),
+ DestroyMemoryStream,
+ pMemStm);
+
+ GIcon *pIcon = g_bytes_icon_new(pBytes);
+ g_lo_menu_set_icon_to_item_in_section( G_LO_MENU( mpMenuModel ), nSection, nItemPos, pIcon );
+ g_object_unref(pIcon);
+ g_bytes_unref(pBytes);
+}
+
void GtkSalMenu::NativeSetAccelerator( unsigned nSection, unsigned nItemPos, const vcl::KeyCode& rKeyCode, const OUString& rKeyName )
{
SolarMutexGuard aGuard;