#include <gtk/gtk.h>

static void
action_toggle_state (GSimpleAction *action,
                     GVariant *param,
                     gpointer user_data)
{
  g_debug ("toggle");

  GVariant *state;

  state = g_action_get_state (G_ACTION (action));
  g_action_change_state (G_ACTION (action),
                         g_variant_new_boolean (!g_variant_get_boolean (state)));
  g_variant_unref (state);
}

static void
activate_cb (GApplication *application, gpointer user_data)
{
    gtk_window_present (GTK_WINDOW (user_data));
}


const GActionEntry win_entries[] =  {
      { "gear-menu", action_toggle_state, NULL, "false", NULL },
};

static void
startup_cb (GApplication *application, gpointer user_data)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *grid;
  GtkWidget *entry;
  GtkWidget *menu_widget;
  GtkAccelGroup *accel_group;
  guint i;
  GMenu *menu;

  window = gtk_application_window_new (GTK_APPLICATION (application));
  gtk_window_resize (GTK_WINDOW (window), 400, 300);
  grid = gtk_grid_new ();
  gtk_container_add (GTK_CONTAINER (window), grid);

  accel_group = gtk_accel_group_new ();
  gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);

  /* Button next to entry */
  entry = gtk_entry_new ();
  gtk_grid_attach (GTK_GRID (grid),
                   entry,
                   0, 0,
                   1, 1);
  button = gtk_menu_button_new ();
  gtk_grid_attach (GTK_GRID (grid),
                   button,
                   1, 0,
                   1, 1);

  /* Button with GtkMenu */
  menu_widget = gtk_menu_new ();
  for (i = 5; i > 0; i--) {
      GtkWidget *item;

      if (i == 3) {
          item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, accel_group);
      } else {
          char *label;

          label = g_strdup_printf ("Item _%d", i);
          item = gtk_menu_item_new_with_mnemonic (label);
          g_free (label);
      }
      gtk_menu_item_set_use_underline (GTK_MENU_ITEM (item), TRUE);
      gtk_menu_attach (GTK_MENU (menu_widget),
                       item,
                       0, 1,
                       i - 1, i);
  }
  gtk_widget_show_all (menu_widget);

  button = gtk_menu_button_new ();
  gtk_menu_button_set_popup (GTK_MENU_BUTTON (button), menu_widget);
  gtk_grid_attach (GTK_GRID (grid),
                   button,
                   1, 1,
                   1, 1);

  gtk_actionable_set_action_name (GTK_ACTIONABLE (button), "win.gear-menu");
  /* Button with GMenuModel */
  menu = g_menu_new ();
  for (i = 5; i > 0; i--) {
      char *label;
      label = g_strdup_printf ("Item _%d", i);
      g_menu_insert (menu, i - 1, label, NULL);
      g_free (label);
  }
  button = gtk_menu_button_new ();
  gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), G_MENU_MODEL (menu));
  gtk_grid_attach (GTK_GRID (grid),
                   button,
                   1, 2,
                   1, 1);

  g_action_map_add_action_entries (G_ACTION_MAP (window),
                                   win_entries, G_N_ELEMENTS (win_entries),
                                   application);

  gtk_application_add_accelerator (GTK_APPLICATION (application),
                                   "F10", "win.gear-menu", NULL);

  gtk_application_add_window (GTK_APPLICATION (application), GTK_WINDOW (window));
  gtk_widget_show_all (GTK_WIDGET (window));
}

int main (int argc, char **argv)
{
  GtkApplication *application;

  application =
    gtk_application_new ("org.gnome.test-menu-button", G_APPLICATION_FLAGS_NONE);


  g_signal_connect (application, "startup", G_CALLBACK (startup_cb), NULL);
  g_signal_connect (application, "activate", G_CALLBACK (activate_cb), NULL);


  g_application_run (G_APPLICATION (application), argc, argv);

  g_object_unref (application);

  return 0;
}
