#include <gtk/gtk.h>

static char *menu_string = 
"<?xml version=\"1.0\"?>"
"<interface>"
"  <menu id=\"menubar\">"
"    <submenu>"
"      <attribute name=\"label\">Help</attribute>"
"      <item>"
"        <attribute name=\"label\">Contents</attribute>"
"        <attribute name=\"action\">win.help</attribute>"
"      </item>"
"    </submenu>"
"  </menu>"
"  <menu id=\"frob-menu\">"
"    <section>"
"      <item>"
"        <attribute name=\"label\">_Frobnicate</attribute>"
"        <attribute name=\"action\">win.frobnicate</attribute>"
"      </item>"
"    </section>"
"  </menu>"
"</interface>";


static void
on_startup (GApplication *app,
            gpointer      user_data)
{
  GtkWidget *win, *vbox, *hbox, *w;
  GtkBuilder *builder;
  GMenuModel *menu;

  builder = gtk_builder_new ();
  gtk_builder_add_from_string (builder, menu_string, -1, NULL);

  win = gtk_application_window_new (GTK_APPLICATION (app));
  gtk_window_set_default_size (GTK_WINDOW (win), 400, 200);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_container_add (GTK_CONTAINER (win), vbox);

  menu = G_MENU_MODEL (gtk_builder_get_object (builder, "menubar"));

  gtk_application_set_menubar (GTK_APPLICATION (app), menu);

  w = gtk_menu_bar_new_from_model (menu);
  gtk_container_add (GTK_CONTAINER (vbox), w);

  g_object_bind_property (G_OBJECT (win), "show-menubar", w, "visible",
                          G_BINDING_INVERT_BOOLEAN | G_BINDING_SYNC_CREATE);

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  g_object_set (hbox, "margin", 6, NULL);
  gtk_container_add (GTK_CONTAINER (vbox), hbox);

  menu = G_MENU_MODEL (gtk_builder_get_object (builder, "frob-menu"));

  w = gtk_label_new ("Menu:");
  gtk_container_add (GTK_CONTAINER (hbox), w);

  w = gtk_menu_button_new ();
  gtk_menu_button_set_use_popover (GTK_MENU_BUTTON (w), FALSE);
  gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (w), menu);
  gtk_container_add (GTK_CONTAINER (hbox), w);

  w = gtk_label_new ("Popover:");
  gtk_container_add (GTK_CONTAINER (hbox), w);

  w = gtk_menu_button_new ();
  gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (w), menu);
  gtk_container_add (GTK_CONTAINER (hbox), w);

  w = gtk_check_button_new_with_label ("Use GtkApplication Menubar");
  gtk_container_add (GTK_CONTAINER (hbox), w);

  g_object_bind_property (G_OBJECT (w), "active",
                          win, "show-menubar",
                          G_BINDING_SYNC_CREATE);

  gtk_widget_show_all (win);
}

static void
on_activate (GApplication *app,
             gpointer      user_data)
{
  gtk_window_present (gtk_application_get_active_window (GTK_APPLICATION (app)));
}

int
main (int argc, char *argv[])
{
  GtkApplication *app = gtk_application_new ("org.gnome.fmuellner.Example", 0);
  g_set_application_name ("Popover MenuButton Test Case");

  g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
  g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);

  return g_application_run (G_APPLICATION (app), argc, argv);
}
