#include <gtk/gtk.h>

static void window_active_state_change_cb (GtkWidget *widget,
                                           GParamSpec *pspec,
                                           gchar *title);
static void window_focus_state_change_cb (GtkWidget *widget,
                                          GParamSpec *pspec,
                                          gchar *title);
static gboolean window_focus_in_cb (GtkWidget * widget,
                                    GdkEventFocus *event,
                                    gchar *title);
static gboolean window_focus_out_cb (GtkWidget * widget,
                                     GdkEventFocus *event,
                                     gchar *title);

int
main(int argc, char **argv)
{
  GtkWidget *window1;
  GtkWidget *window2;
  GtkWidget *window3;

  gchar *window1_title = "Window 1";
  gchar *window2_title = "Window 2";
  gchar *window3_title = "Window 3";

  gtk_init (&argc, &argv);

  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  window3 = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_window_set_title (GTK_WINDOW (window1), window1_title);
  gtk_window_set_title (GTK_WINDOW (window2), window2_title);
  gtk_window_set_title (GTK_WINDOW (window3), window3_title);

  g_signal_connect (G_OBJECT (window1), "notify::is-active",
  		    G_CALLBACK (window_active_state_change_cb),
                    window1_title);

  g_signal_connect (G_OBJECT (window2), "notify::is-active",
  		    G_CALLBACK (window_active_state_change_cb),
                    window2_title);

  g_signal_connect (G_OBJECT (window3), "notify::is-active",
  		    G_CALLBACK (window_active_state_change_cb),
                    window3_title);

  g_signal_connect (G_OBJECT (window1), "notify::has-toplevel-focus",
  		    G_CALLBACK (window_focus_state_change_cb),
                    window1_title);

  g_signal_connect (G_OBJECT (window2), "notify::has-toplevel-focus",
  		    G_CALLBACK (window_focus_state_change_cb),
                    window2_title);

  g_signal_connect (G_OBJECT (window3), "notify::has-toplevel-focus",
  		    G_CALLBACK (window_focus_state_change_cb),
                    window3_title);

  g_signal_connect (G_OBJECT (window1), "focus-in-event",
                    G_CALLBACK (window_focus_in_cb),
                    window1_title);

  g_signal_connect (G_OBJECT (window2), "focus-in-event",
                    G_CALLBACK (window_focus_in_cb),
                    window2_title);

  g_signal_connect (G_OBJECT (window3), "focus-in-event",
                    G_CALLBACK (window_focus_in_cb),
                    window3_title);

  g_signal_connect (G_OBJECT (window1), "focus-out-event",
                    G_CALLBACK (window_focus_out_cb),
                    window1_title);

  g_signal_connect (G_OBJECT (window2), "focus-out-event",
                    G_CALLBACK (window_focus_out_cb),
                    window2_title);

  g_signal_connect (G_OBJECT (window3), "focus-out-event",
                    G_CALLBACK (window_focus_out_cb),
                    window3_title);

  gtk_window_set_default_size (GTK_WINDOW(window1), 300, 200);
  gtk_window_set_default_size (GTK_WINDOW(window2), 300, 200);
  gtk_window_set_default_size (GTK_WINDOW(window3), 300, 200);
  gtk_window_move (GTK_WINDOW(window2), 350, 0);
  gtk_window_move (GTK_WINDOW(window3), 700, 0);

  gtk_widget_show (window1);

  gtk_widget_show (window2);

  gtk_widget_show (window3);

  gtk_main ();

  return 0;
}

static void
window_active_state_change_cb (GtkWidget *widget,
                               GParamSpec *pspec,
                               gchar *title)
{
  if (GTK_WINDOW (widget)->is_active)
    g_print ("%s activated\n", title);
  else
    g_print ("%s deactivated\n", title);
}

static void
window_focus_state_change_cb (GtkWidget *widget,
                              GParamSpec *pspec,
                              gchar *title)
{
  if (GTK_WINDOW (widget)->has_toplevel_focus)
    g_print ("%s got toplevel focus\n", title);
  else
    g_print ("%s lost toplevel focus\n", title);
}

static gboolean
window_focus_in_cb (GtkWidget * widget,
                   GdkEventFocus *event,
                   gchar *title)
{
  g_print ("%s focus in\n", title);
  return FALSE;
}

static gboolean
window_focus_out_cb (GtkWidget * widget,
                    GdkEventFocus *event,
                    gchar *title)
{
  g_print ("%s focus out\n", title);
  return FALSE;
}
