#include <gtk/gtk.h>

#define EXAMPLE_APP_TYPE (example_app_get_type ())
#define EXAMPLE_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EXAMPLE_APP_TYPE, ExampleApp))

typedef struct _ExampleApp
{
  GtkApplication parent;
} ExampleApp;

typedef struct _ExampleAppClass
{
  GtkApplicationClass parent_class;
} ExampleAppClass;

GType           example_app_get_type    (void);
ExampleApp     *example_app_new         (void);

G_DEFINE_TYPE(ExampleApp, example_app, GTK_TYPE_APPLICATION);

static void
example_app_init (ExampleApp *app)
{
}

static void
on_toggle_toggled (GSimpleAction *action,
                   GVariant      *parameter,
                   gpointer       app)
{
  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 GActionEntry app_entries[] =
{
  { "toggle", on_toggle_toggled, NULL, "false", NULL }
};

static void
example_app_startup (GApplication *app)
{
  GMenu *app_menu;

  G_APPLICATION_CLASS (example_app_parent_class)->startup (app);

  g_action_map_add_action_entries (G_ACTION_MAP (app),
                                   app_entries, G_N_ELEMENTS (app_entries),
                                   app);

  app_menu = g_menu_new ();
  g_menu_append (app_menu, "I, Toggle", "app.toggle");
  gtk_application_set_app_menu (GTK_APPLICATION (app), G_MENU_MODEL (app_menu));
}

static void
example_app_activate (GApplication *app)
{
  GtkWidget *win;

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


static void
example_app_class_init (ExampleAppClass *class)
{
  G_APPLICATION_CLASS (class)->startup = example_app_startup;
  G_APPLICATION_CLASS (class)->activate = example_app_activate;
}

ExampleApp *
example_app_new (void)
{
  return g_object_new (EXAMPLE_APP_TYPE,
                       "application-id", "org.gtk.exampleapp",
                       "flags", NULL);
}

int
main (int argc, char **argv)
{
  ExampleApp *app = example_app_new ();
  return g_application_run (G_APPLICATION (app), argc, argv);
}
