#include <gtk/gtk.h>

#include <string.h>
#include <stdio.h>

static void
on_entry_changed(GtkEntry* entry, void* data)
{
  const gchar* text = gtk_entry_get_text(entry);
  printf("Entry changed: %s\n", text);
}

static void
on_entry_editing_done(GtkEntry* entry, void* data)
{
  const gchar* text = gtk_entry_get_text(entry);
  printf("Entry editing-done: %s\n", text);
}

static void
on_entry_activate(GtkEntry* entry, void* data)
{
  const gchar* text = gtk_entry_get_text(entry);
  printf("Entry activate: %s\n", text);
}

static gboolean
on_entry_key_press_event(GtkEntry* entry, GdkEventKey* event, void* data)
{
  if (event->keyval == GDK_KEY_Return ||
      event->keyval == GDK_KEY_ISO_Enter ||
      event->keyval == GDK_KEY_KP_Enter)
  {
    const gchar* text = gtk_entry_get_text(entry);
    printf("Entry key-press-event(Return): %s\n", text);
  }
  return FALSE;
}

static gboolean
on_entry_focus_out_event(GtkEntry* entry, GdkEventFocus* event, void* data)
{
  const gchar* text = gtk_entry_get_text(entry);
  printf("Entry focus-out-event: %s\n", text);
  return FALSE;
}

int
main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *entry;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 5);
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);

  /* GtkEntry */
  entry = gtk_entry_new ();
  gtk_container_add (GTK_CONTAINER (window), entry);

  gtk_widget_add_events(GTK_WIDGET(entry), GDK_KEY_PRESS_MASK | GDK_FOCUS_CHANGE_MASK);
  g_signal_connect (entry, "changed", G_CALLBACK (on_entry_changed), NULL);
  g_signal_connect (entry, "editing-done", G_CALLBACK (on_entry_editing_done), NULL);
  g_signal_connect (entry, "activate", G_CALLBACK (on_entry_activate), NULL);
  g_signal_connect (entry, "key-press-event", G_CALLBACK (on_entry_key_press_event), NULL);
  g_signal_connect (entry, "focus-out-event", G_CALLBACK (on_entry_focus_out_event), NULL);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}

