/*
 *  main.c
 *  RectangleTest
 */

#include <gtk/gtk.h>


G_BEGIN_DECLS

typedef struct _FooPrivate FooPrivate;

typedef struct {
  GtkDrawingArea parent;
} 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_END_DECLS

G_DEFINE_TYPE (Foo, foo, GTK_TYPE_DRAWING_AREA);


static void
foo_init (Foo *self)
{
}

static gboolean
foo_expose (GtkWidget *self, GdkEventExpose *event)
{
  GdkRectangle r;
  gtk_widget_get_allocation(self, &r);
  GdkGC *gc = gdk_gc_new(gtk_widget_get_window(self));  
  
  int x, y;
  for (x = 0; x < r.width; x += 10) {
    for (y = 0; y < r.height; y += 10) {
      GdkColor c1 = { 
        0, 
        (int)(x * 65535 / r.width), 
        (int)(y * 65535 / r.height), 
        (int)((x + y) * 65535 / (r.width + r.height)) 
      };
      gdk_gc_set_rgb_fg_color(gc, &c1);
      gdk_draw_rectangle(gtk_widget_get_window(self), gc, TRUE, x, y, 9, 9);      
    }
  }
  
  gdk_gc_unref(gc);
  
  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;
}


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;
}
