/*
 *  main.c
 *  RectangleTest
 */


#undef GTK_DISABLE_DEPRECATED
#undef GDK_DISABLE_DEPRECATED


#include <gtk/gtk.h>


typedef struct _FooPrivate FooPrivate;

typedef struct {
  GtkDrawingArea parent;

  GdkPixmap *p;
} Foo;

typedef struct {
  GtkDrawingAreaClass parent;
} FooClass;

#define FOO_TYPE (foo_get_type ())
#define FOO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FOO_TYPE, Foo))
#define FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), FOO_TYPE, FooClass))
#define IS_FOO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FOO_TYPE))
#define IS_FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), FOO_TYPE))
#define FOO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), FOO_TYPE, FooClass))

G_DEFINE_TYPE (Foo, foo, GTK_TYPE_DRAWING_AREA);


static void
foo_init (Foo *self)
{
}

static void
foo_map (GtkWidget *w)
{
  Foo *self = FOO(w);
  gtk_widget_add_events (w, GDK_EXPOSURE_MASK);
  self->p = gdk_pixmap_new (w->window, w->allocation.width,
                            w->allocation.height, -1);

  GTK_WIDGET_CLASS (foo_parent_class)->map (w);
}

static gboolean
foo_expose (GtkWidget *w, GdkEventExpose *event)
{
  int i, k;
  Foo *self = FOO(w);
  GdkPixmap *tmp;

  for (k = 0; k < 100; k++)
    {
      tmp = gdk_pixmap_new (self->p, 200, 10, -1);
      gdk_draw_rectangle (self->p, w->style->white_gc, TRUE, 0, 0, 200, 200);

      for (i = 10; i >= 0; i--)
        {
          int j;

          gdk_draw_rectangle (tmp, w->style->white_gc, TRUE, 0, 0, 200, 10);

          for (j = 0; j < i; j++)
            gdk_draw_rectangle (tmp, w->style->black_gc, TRUE,
                                j * 15, 0, 10, 10);

          gdk_draw_drawable (self->p, w->style->white_gc,
                             tmp, 0, 0, 0, (10 - i) * 15, 200, 10);
        }

      gdk_draw_drawable (w->window, w->style->white_gc, self->p, 0, 0, 0, 0,
                         w->allocation.width, w->allocation.height);
    }

  return TRUE;
}

static GtkWidget *
foo_new (void) 
{
  return (GtkWidget *) gtk_object_new (FOO_TYPE, NULL);
}

static void
foo_class_init (FooClass *self_class)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (self_class);  
  widget_class->expose_event = foo_expose;
  widget_class->map = foo_map;
}


int
main (int argc, char **argv)
{
  GtkWidget *window;
  
  gtk_init (&argc, &argv);
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  
  /* Add your widgets to the window here. */
  gtk_container_add((GtkContainer*)window, foo_new());
  gtk_widget_show_all (window);
  
  /* Run the main loop. */
  gtk_main ();
  
  return 0;
}
