#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <string.h>

static GtkWidget* s_pMenu;


static void _close (GtkWidget *pWidget, gpointer data)
{
	gtk_main_quit ();
}

static void _popmenu_position_func (GtkMenu *pMenu, gint *x, gint *y,
                                   gboolean *push_in, gpointer data)
{
	*x = 50;
	*y = gdk_screen_get_height (gdk_screen_get_default ()) - 50;
	*push_in = FALSE;
	g_print ("Position: %dx%d\n", *x, *y);
}

static void _clicked (GtkWidget *pWidget, gpointer data)
{
	gtk_menu_popup (GTK_MENU (s_pMenu), NULL, NULL, _popmenu_position_func,
	                NULL, 0, gtk_get_current_event_time());
}


int main (int argc, char** argv)
{
	gtk_init (&argc, &argv);

	GtkWidget* pWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	g_signal_connect (G_OBJECT (pWindow), "delete_event", G_CALLBACK (_close), NULL);
	g_signal_connect (G_OBJECT (pWindow), "destroy", G_CALLBACK (_close), NULL);
	gtk_container_set_border_width (GTK_CONTAINER (pWindow), 10);

	GtkWidget* pButton = gtk_button_new_with_label ("Popup the Menu at the bottom");
	gtk_widget_show (pButton);
	g_signal_connect (G_OBJECT (pButton), "clicked",
	                  G_CALLBACK (_clicked), NULL);
	gtk_container_add (GTK_CONTAINER (pWindow), pButton);
	gtk_widget_show (pWindow);

	s_pMenu = gtk_menu_new ();
	GtkWidget* pMenuitem1 = gtk_menu_item_new_with_label ("Menu item 1");
	gtk_menu_shell_append (GTK_MENU_SHELL (s_pMenu), pMenuitem1);
	GtkWidget* pMenuitem2 = gtk_menu_item_new_with_label ("Menu item 2");
	gtk_menu_shell_append (GTK_MENU_SHELL (s_pMenu), pMenuitem2);
	GtkWidget* pMenuitem3 = gtk_menu_item_new_with_label ("Menu item 3");
	gtk_menu_shell_append (GTK_MENU_SHELL (s_pMenu), pMenuitem3);
	GtkWidget* pMenuitem4 = gtk_menu_item_new_with_label ("Menu item 4");
	gtk_menu_shell_append (GTK_MENU_SHELL (s_pMenu), pMenuitem4);
	gtk_widget_show_all (GTK_WIDGET (s_pMenu));

	gtk_main ();

	return 0;
}
