#include <gtk/gtk.h>

static void
on_menu_item_activate (GtkWidget *item G_GNUC_UNUSED,
                       gpointer   data G_GNUC_UNUSED)
{
  g_debug ("menu item activate");
}

static void
populate_menu (GtkMenu *menu)
{
  GtkWidget *item;
  
  item = gtk_menu_item_new_with_label ("Hello");
  g_signal_connect (item, "activate", G_CALLBACK (on_menu_item_activate), NULL);
  gtk_widget_show (item);
  gtk_container_add (GTK_CONTAINER (menu), item);
}

static void
on_show_menu_refill (GtkMenuToolButton *button G_GNUC_UNUSED,
                     GtkWidget         *menu)
{
  g_debug ("show-menu, refill");
  
  gtk_container_foreach (GTK_CONTAINER (menu), (GtkCallback)gtk_widget_destroy, NULL);
  populate_menu (GTK_MENU (menu));
}

static void
on_show_menu_replace (GtkMenuToolButton  *button,
                      GtkWidget          *menu)
{
  g_debug ("show-menu, replace");
  
  menu = gtk_menu_new ();
  populate_menu (GTK_MENU (menu));
  gtk_menu_tool_button_set_menu (button, menu);
}

static void
add_item (GtkToolbar   *toolbar,
          const gchar  *label,
          GCallback     show_menu_callback)
{
  GtkToolItem  *item = gtk_menu_tool_button_new (NULL, label);
  GtkWidget    *menu = gtk_menu_new ();
  
  gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (item), menu);
  g_signal_connect (item, "show-menu", G_CALLBACK (show_menu_callback), menu);
  gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (item));
}

int
main (int     argc,
      char  **argv)
{
  GtkWidget *win, *toolbar;
  
  gtk_init (&argc, &argv);
  
  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (win, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  
  toolbar = gtk_toolbar_new ();
  gtk_container_add (GTK_CONTAINER (win), toolbar);
  
  add_item (GTK_TOOLBAR (toolbar), "Replace menu",
            G_CALLBACK (on_show_menu_replace));
  add_item (GTK_TOOLBAR (toolbar), "Refill menu",
            G_CALLBACK (on_show_menu_refill));
  
  gtk_widget_show_all (win);
  
  gtk_main ();
  
  return 0;
}
