#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <gdk/gdkkeysyms.h>

gboolean fs;

static gboolean
key_press_cb (GtkWidget   *widget,
              GdkEventKey *event,
              gpointer     user_data)
{
  if (event->keyval == GDK_F11)
    {
      if (fs)
        {
          printf ("key_press_cb: restoring window\n");
          gtk_window_unfullscreen (GTK_WINDOW (widget));
        }
      else
        {
          printf ("key_press_cb: going to fullscreen\n");
          gtk_window_fullscreen (GTK_WINDOW (widget));
        }
    }

  return FALSE;
}

static gboolean
window_state_cb (GtkWidget           *widget,
                 GdkEventWindowState *event,
                 GtkWidget           *scroll)
{
  if ((event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) == 0)
    return FALSE;

  if (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN)
    {
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
                                      GTK_POLICY_NEVER,
                                      GTK_POLICY_NEVER);
      printf ("window_state_cb: policy never\n");
    }
  else
    {
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
                                      GTK_POLICY_AUTOMATIC,
                                      GTK_POLICY_AUTOMATIC);
      printf ("window_state_cb: policy automatic\n");
    }

  if (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN)
    fs = TRUE;
  else
    fs = FALSE;

  return FALSE;
}

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

  fs = FALSE;

  gtk_init (&argc, &argv);

  text   = gtk_text_view_new ();
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  scroll = gtk_scrolled_window_new (NULL, NULL);

  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);

  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scroll),
					 text);  
  gtk_container_add (GTK_CONTAINER (window), scroll);

  g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect (window, "key-press-event", G_CALLBACK (key_press_cb), NULL);
  g_signal_connect (window, "window-state-event", G_CALLBACK (window_state_cb), scroll);

  gtk_widget_show_all (window);

  gtk_main ();
  return 0;  
}
