teststockbuttonmenu.c 4.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include <glib.h>
#define GDK_VERSION_MIN_REQUIRED G_ENCODE_VERSION (3, 8)
#include <gtk/gtk.h>

int main (int argc, char **argv)
{
	GtkWidget *window;
	GtkWidget *grid;
	GtkWidget *button;
	GtkWidget *menu;
	GtkWidget *item;
	GtkWidget *box;
13
	GtkWidget *label;
14 15
	GtkAction *action1;
	GtkAction *action2;
16
        GtkAccelGroup *accel_group;
17 18 19 20 21 22 23 24 25

	gtk_init (&argc, &argv);

	action1 = gtk_action_new ("bold", NULL, NULL, GTK_STOCK_BOLD);
	action2 = gtk_action_new ("new", NULL, NULL, GTK_STOCK_NEW);
	gtk_action_set_always_show_image (action2, TRUE);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

26 27 28
        accel_group = gtk_accel_group_new ();
        gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	grid = gtk_grid_new ();
	gtk_container_add (GTK_CONTAINER (window), grid);

        /* plain old stock button */
        button = gtk_button_new_from_stock (GTK_STOCK_DELETE);
        gtk_container_add (GTK_CONTAINER (grid), button);

	/* gtk_button_set_always_show_image still works */
        button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
        gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
        gtk_container_add (GTK_CONTAINER (grid), button);

	/* old-style image-only button */
	button = gtk_button_new ();
	gtk_button_set_image (GTK_BUTTON (button), gtk_image_new_from_icon_name ("edit-find", GTK_ICON_SIZE_BUTTON));
        gtk_container_add (GTK_CONTAINER (grid), button);

	/* new-style image-only button */
        button = gtk_button_new_from_icon_name ("edit-clear", GTK_ICON_SIZE_BUTTON);
        gtk_container_add (GTK_CONTAINER (grid), button);

	/* GtkAction-backed button */
	button = gtk_button_new ();
	gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
        gtk_activatable_set_related_action (GTK_ACTIVATABLE (button), action1);
        gtk_container_add (GTK_CONTAINER (grid), button);

	/* gtk_action_set_always_show_image still works for buttons */
	button = gtk_button_new ();
	gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
        gtk_activatable_set_related_action (GTK_ACTIVATABLE (button), action2);
        gtk_container_add (GTK_CONTAINER (grid), button);

	button = gtk_menu_button_new ();
        gtk_container_add (GTK_CONTAINER (grid), button);

	menu = gtk_menu_new ();
66 67 68
        gtk_menu_set_accel_group (GTK_MENU (menu), accel_group);
        gtk_menu_set_accel_path (GTK_MENU (menu), "<menu>/TEST");

69 70 71 72 73 74 75
	gtk_menu_button_set_popup (GTK_MENU_BUTTON (button), menu);

	/* plain old stock menuitem */
	item = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);

	/* gtk_image_menu_item_set_always_show_image still works */
76
	item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLOSE, accel_group);
77 78 79 80 81 82 83 84
	gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);

	/* new-style menuitem with image */
	item = gtk_menu_item_new ();
	box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
	gtk_container_add (GTK_CONTAINER (item), box);
	gtk_container_add (GTK_CONTAINER (box), gtk_image_new_from_icon_name ("edit-clear", GTK_ICON_SIZE_MENU));
85 86 87 88 89 90 91 92 93
        label = gtk_accel_label_new ("C_lear");
        gtk_label_set_use_underline (GTK_LABEL (label), TRUE);
        gtk_label_set_xalign (GTK_LABEL (label), 0.0);
        gtk_widget_set_halign (label, GTK_ALIGN_FILL);

        gtk_widget_add_accelerator (item, "activate", accel_group,
                                    GDK_KEY_x, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
        gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), item);
	gtk_box_pack_end (GTK_BOX (box), label, TRUE, TRUE, 0);
94
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	/* GtkAction-backed menuitem */
	item = gtk_image_menu_item_new ();
	gtk_activatable_set_related_action (GTK_ACTIVATABLE (item), action1);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);

	/* gtk_action_set_always_show_image still works for menuitems */
	item = gtk_image_menu_item_new ();
	gtk_activatable_set_related_action (GTK_ACTIVATABLE (item), action2);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);

	gtk_widget_show_all (menu);

	gtk_widget_show_all (window);

	gtk_main ();

	return 0;
}