#include <unistd.h>
#include <gtk/gtk.h>

static int obscured = 0;

/**
 * Resize this window larger and then rapidly smaller.  You will often
 * see event sequences where the window has received visibility events
 * indicating that it is fully obscured, yet it gets an expose event.
 */

gboolean expose_event (GtkWidget *widget, GdkEventExpose *event,
	gpointer user_data)
{
	GdkGC *gc = gdk_gc_new (event->window);

	gdk_draw_rectangle (event->window, gc, TRUE,
		event->area.x, event->area.y,
		event->area.width, event->area.height);
	gdk_gc_unref (gc);

	printf ("expose (%s)\n", obscured ? "obscured" : "unobscured");

	/* Fake some intensive drawing work. */
	usleep (500);

	return 0;
}

gboolean visibility_notify (GtkWidget *widget, GdkEventVisibility *event,
	gpointer user_data)
{
	if (event->state == GDK_VISIBILITY_FULLY_OBSCURED) {
		printf ("fully obscured\n");
		obscured = 1;
	} else if (event->state == GDK_VISIBILITY_UNOBSCURED) {
		printf ("unobscured\n");
		obscured = 0;
	} else {
		printf ("partially obscured\n");
		obscured = 0;
	}

	return 0;
}

int main (int argc, char **argv)
{
	GtkWidget *shellHandle, *da, *vbox, *label, *scrolled, *viewport;

	gtk_init_check (&argc, &argv);

	shellHandle = gtk_window_new (GTK_WINDOW_TOPLEVEL);	
	g_signal_connect (G_OBJECT (shellHandle), "destroy",
		G_CALLBACK (gtk_main_quit), 0);
	g_signal_connect (G_OBJECT (shellHandle), "delete_event",
		G_CALLBACK (gtk_main_quit), 0);

	gtk_window_set_default_size (GTK_WINDOW (shellHandle), 280, 250);

	scrolled = gtk_scrolled_window_new( 0, 0 );
	viewport = gtk_viewport_new( 0, 0 );

	vbox = gtk_vbox_new (FALSE, 0);

	label = gtk_label_new ("resize me big and small\nla la la\nla la la\nla la la");
	gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);

	da = gtk_drawing_area_new ();
	gtk_widget_add_events (GTK_WIDGET (da),
		GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK);
	g_signal_connect (G_OBJECT (da), "visibility-notify-event",
		G_CALLBACK (visibility_notify), 0);	
	g_signal_connect (G_OBJECT (da), "expose-event",
		G_CALLBACK (expose_event), 0);	
	gtk_widget_set_size_request (GTK_WIDGET (da), 280, 50);
	gtk_box_pack_start (GTK_BOX (vbox), da, FALSE, FALSE, 0);

	gtk_container_add (GTK_CONTAINER (viewport), vbox);
	gtk_container_add (GTK_CONTAINER (scrolled), viewport );
	gtk_container_add (GTK_CONTAINER (shellHandle), scrolled );

	gtk_widget_show_all (shellHandle);
	gtk_main();
}

