/* Build with
   gcc -o select-item `pkg-config --cflags --libs gtk+-3.0` -Wall select-item.c
 */

#include <gtk/gtk.h>

static void
on_clicked (GtkWidget *widget,
	    gpointer   user_data)
{
	gtk_menu_shell_select_first (GTK_MENU_SHELL (user_data), FALSE);
}

static void
on_destroy (GtkWidget *widget,
	    gpointer   user_data)
{
	gtk_main_quit ();
}

int
main (int argc, char **argv)
{
	GtkWidget *window;
	GtkWidget *box;
	GtkWidget *button;
	GtkWidget *menubar;
	GtkWidget *item;
	GtkWidget *menu;

	gtk_init (&argc, &argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	g_signal_connect (window, "destroy",
			  G_CALLBACK (on_destroy), NULL);

	box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
	gtk_container_add (GTK_CONTAINER (window), box);

	menubar = gtk_menu_bar_new ();
	item = gtk_menu_item_new_with_label ("Menu");
	gtk_menu_shell_append (GTK_MENU_SHELL (menubar), item);

	menu = gtk_menu_new ();
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);

	item = gtk_menu_item_new_with_label ("A");
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
	item = gtk_menu_item_new_with_label ("B");
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
	item = gtk_menu_item_new_with_label ("C");
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);

	gtk_container_add (GTK_CONTAINER (box), menubar);

	button = gtk_button_new_with_label ("Call gtk_menu_shell_select_item()");

	g_signal_connect (button, "clicked",
			  G_CALLBACK (on_clicked), menubar);

	gtk_container_add (GTK_CONTAINER (box), button);

	gtk_widget_show_all (GTK_WIDGET (window));

	gtk_main ();

	return 0;
}
