Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to avoid GtkImageMenuItem and keep icons on menu items
/*
* Build:
* gcc $(pkg-config --cflags gtk+-3.0) sample.c -o sample $(pkg-config --libs gtk+-3.0)
*/
#include <gtk/gtk.h>
static GtkWidget*
create_menu_item (const gchar *str, const char *icon_name)
{
GtkWidget *mi, *label, *box, *image;
mi = gtk_menu_item_new ();
label = gtk_label_new (str);
image = gtk_image_new_from_icon_name (icon_name ? icon_name : "", GTK_ICON_SIZE_BUTTON);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
gtk_widget_set_halign (label, GTK_ALIGN_START);
gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 6);
gtk_container_add (GTK_CONTAINER (mi), box);
return mi;
}
gboolean
on_button_pressed (GtkWidget *treeview, GdkEventButton *event, gpointer data)
{
if (event->type == GDK_BUTTON_PRESS && event->button == 3)
{
GtkWidget *menu = gtk_menu_new ();
gtk_menu_shell_append (GTK_MENU_SHELL (menu),
create_menu_item ("Copy", "edit-copy"));
gtk_menu_shell_append (GTK_MENU_SHELL (menu),
create_menu_item ("Paste", "edit-paste"));
gtk_menu_shell_append (GTK_MENU_SHELL (menu),
create_menu_item ("Another option", NULL));
gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
gtk_widget_show_all (menu);
gtk_menu_popup_at_pointer(GTK_MENU (menu), NULL);
return TRUE;
}
return FALSE;
}
int
main (int argc, char *argv[])
{
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK (on_button_pressed), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.