Gtk_label_set_extra_menu (GTK_LABEL(label), G_MENU_MODEL(menu)) has no effect

The example shows how a GtkEntry already does that a
PopoverMenu has two other menu entries can be added.
It doesn’t work with GtkLabel.

Why?

 
#pragma
#include"popup-menu.h"

  static void
  item1_activated(GSimpleAction *action, GVariant *parameter, gpointer app) 
	{ g_print("Item1\n");}
  static void
  item2_activated(GSimpleAction *action, GVariant *parameter, gpointer app) 
	{ g_print("Item2\n");}


void activate (GtkApplication *app, gpointer data)
{
  GtkWidget *window;
  
  window =gtk_application_window_new(app);
  gtk_window_set_default_size(GTK_WINDOW(window),500,200); 

  GtkWidget *label = gtk_label_new("    label with additional menu entries ");
  GtkWidget *entry = gtk_entry_new();
  gtk_widget_set_halign (entry, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (entry, GTK_ALIGN_CENTER);

  //G menu model
  GMenu *menu = g_menu_new ();
  GMenuItem *menu_item_item1 = g_menu_item_new ("Item1", "app.item1");
  GMenuItem *menu_item_item2 = g_menu_item_new ("Item2", "app.item2");
  g_menu_append_item (menu, menu_item_item1);
  g_menu_append_item (menu, menu_item_item2);
  g_object_unref (menu_item_item1);
  g_object_unref (menu_item_item2);

  // set additional menu entries
  gtk_entry_set_extra_menu (GTK_ENTRY(entry),G_MENU_MODEL(menu));
  // ????? has no effect ?????
  gtk_label_set_extra_menu (GTK_LABEL(label),G_MENU_MODEL(menu));

  // actions 
  GSimpleAction *act_item1 = g_simple_action_new ("item1", NULL);
  GSimpleAction *act_item2 = g_simple_action_new ("item2", NULL);
  g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_item1));
  g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_item2)); 
  g_signal_connect (act_item1, "activate", G_CALLBACK (item1_activated), app);
  g_signal_connect (act_item2, "activate", G_CALLBACK (item2_activated), app);

  GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,30);
  gtk_box_append(GTK_BOX(box),label);
  gtk_box_append(GTK_BOX(box),entry);

  gtk_window_set_child(GTK_WINDOW(window),box);

 gtk_widget_set_visible(window,TRUE);
}

image

Does anyone have an idea or a suggested solution?

Hi,

I think labels must be selectable to enable context menus.

1 Like