#include <gtk/gtk.h>

static void on_activate_print_string	(GtkMenuItem	*menuitem,
					 gpointer	userdata)
{
	const gchar* text = (const gchar*)userdata;

	g_message(text);
}

static gboolean on_button_release_event	(GtkWidget	*widget,
					 GdkEventButton	*event,
					 gpointer	userdata)
{
	GtkWidget	*w		= NULL;
	GtkMenu		*popup		= GTK_MENU( gtk_menu_new() );
	GtkMenuShell	*popup_ms	= GTK_MENU_SHELL(popup);
	GtkWidget	*mitem		= NULL;
	GtkMenu		*child_menu	= GTK_MENU( gtk_menu_new() );
	GtkWidget	*child_mitem	= NULL;

	mitem = gtk_menu_item_new_with_mnemonic("_Normal");
	g_signal_connect_after(
		mitem,
		"activate",
		G_CALLBACK(on_activate_print_string),
		"Normal");
	gtk_menu_shell_append(popup_ms, mitem);

	mitem = gtk_menu_item_new_with_mnemonic("_Parent");
	gtk_menu_shell_append(popup_ms, mitem);

	child_mitem = gtk_menu_item_new_with_mnemonic("_Child");
	g_signal_connect_after(
		child_mitem,
		"activate",
		G_CALLBACK(on_activate_print_string),
		"Child");
	gtk_menu_shell_append(
		GTK_MENU_SHELL(child_menu),
		child_mitem);
	
	gtk_menu_item_set_submenu(
		GTK_MENU_ITEM(mitem),
		GTK_WIDGET(child_menu) );

	gtk_widget_show_all( GTK_WIDGET(popup) );

	gtk_menu_popup(
		popup,
		NULL,
		NULL,
		NULL,
		NULL,
		event->button,
		event->time);


	return FALSE;
}


int main(int argc, const char* argv[])
{
	GtkWidget	*w		= NULL;
	GtkWindow	*window		= NULL;
	GtkEventBox	*eventbox	= NULL;
	GtkLabel	*label		= NULL;

	gtk_init(&argc, (char***)&argv);

	w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	window = GTK_WINDOW(w);
	g_signal_connect_after(
		window,
		"delete-event",
		gtk_main_quit,
		NULL);

	w = gtk_label_new(
		"Clicking here will display a popup menu.\n"
		"\n"
		"- Clicking on 'Normal' should print 'Normal' to "
		"the console.\n"
		"- Clicking on 'Child' *should* print 'Child' to "
		"the console.\n"
		"\n"
		"1.)  Click on 'Normal' to print that string "
		"to the console.  This is the normal case.\n"
		"2.)  Mouse over 'Parent' and click on 'Child'.  "
		"This should print to the console, but doesn't.\n"
		"3.)  Mouse over 'Parent' and mouse over 'Child', "
		"then press ENTER while 'Child' is still "
		"highlighted.  This *will* print 'Child'.\n"
		"4.)\n"
		"   a.)  Click on 'Parent', this will hide the "
		"child menu.\n"
		"   b.)  Click on 'Parent' again, this will "
		"redisplay the child menu.\n"
		"   c.)  Click on 'Child'.  'Child' **will** be "
		"printed to the console!");
	label = GTK_LABEL(w);
	gtk_label_set_width_chars(label, 40);
	gtk_label_set_line_wrap(label, TRUE);

	w = gtk_event_box_new();
	eventbox = GTK_EVENT_BOX(w);
	gtk_widget_add_events(w, GDK_BUTTON_RELEASE_MASK);
	g_signal_connect_after(
		eventbox,
		"button-release-event",
		G_CALLBACK(on_button_release_event),
		NULL);
	gtk_container_add(
		GTK_CONTAINER(eventbox),
		GTK_WIDGET(label));


	gtk_container_add(
		GTK_CONTAINER(window),
		GTK_WIDGET(eventbox));

	gtk_widget_show_all( GTK_WIDGET(window) );

	gtk_main();


	return 0;
}
