--- /dev/null
+/*
+ * Copyright (C) 2008 Carl Worth
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/ .
+ *
+ * Author: Carl Worth <cworth@cworth.org>
+ */
+
+#include <gtk/gtk.h>
+#include <math.h>
+
+#define BOARD_SIZE 8
+
+typedef struct {
+ int x_offset;
+ int y_offset;
+ int width;
+ int height;
+ int cell_size;
+} layout_t;
+
+static gboolean
+on_delete_event_quit (GtkWidget *widget,
+ GdkEvent *event,
+ gpointer user_data)
+{
+ gtk_main_quit ();
+
+ /* Returning FALSE allows the default handler for delete-event
+ * to proceed to cleanup the widget. */
+ return FALSE;
+}
+
+static gboolean
+on_expose_event_draw (GtkWidget *widget,
+ GdkEventExpose *event,
+ gpointer user_data)
+{
+ cairo_t *cr;
+ layout_t *layout = user_data;
+ int x, y;
+
+ if (layout->width != widget->allocation.width ||
+ layout->height != widget->allocation.height)
+ {
+ int size;
+
+ layout->width = widget->allocation.width;
+ layout->height = widget->allocation.height;
+
+ size = MIN (layout->width, layout->height);
+ /* Size must be a multiple of the integer cell_size */
+ layout->cell_size = size / BOARD_SIZE;
+ size = layout->cell_size * BOARD_SIZE;
+
+ layout->x_offset = (layout->width - size) / 2;
+ layout->y_offset = (layout->height - size) / 2;
+ }
+
+ cr = gdk_cairo_create (widget->window);
+
+ cairo_translate (cr, layout->x_offset, layout->y_offset);
+
+ for (y = 0; y < BOARD_SIZE; y++) {
+ for (x = 0; x < BOARD_SIZE; x++) {
+ if ((x + y) % 2 == 0)
+ cairo_set_source_rgb (cr, 0.89, 0.70, 0.40);
+ else
+ cairo_set_source_rgb (cr, 0.26, 0.02, 0.01);
+ cairo_rectangle (cr,
+ x * layout->cell_size, y * layout->cell_size,
+ layout->cell_size, layout->cell_size);
+ cairo_fill (cr);
+ }
+ }
+
+ cairo_destroy (cr);
+
+ return TRUE;
+}
+
+int
+main (int argc, char *argv[])
+{
+ GtkWidget *window;
+ GtkWidget *drawing_area;
+ layout_t layout;
+
+ layout.width = 0;
+ layout.height = 0;
+
+ gtk_init (&argc, &argv);
+
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+
+ gtk_window_set_default_size (GTK_WINDOW (window), 100, 100);
+
+ g_signal_connect (window, "delete-event",
+ G_CALLBACK (on_delete_event_quit), NULL);
+
+ drawing_area = gtk_drawing_area_new ();
+
+ gtk_container_add (GTK_CONTAINER (window), drawing_area);
+
+ g_signal_connect (drawing_area, "expose-event",
+ G_CALLBACK (on_expose_event_draw), &layout);
+
+ gtk_widget_show_all (window);
+
+ gtk_main ();
+
+ return 0;
+}