+ VALID_WINDOW(win, return);
+}
+
+/* Called when the graphics window is resized, restacked, or moved. Resize the
+backing store if necessary. */
+gboolean
+on_graphics_configure(GtkWidget *widget, GdkEventConfigure *event, winid_t win)
+{
+ int oldwidth = 0, oldheight = 0;
+
+ /* Determine whether the backing store can stay the same size */
+ gboolean needs_resize = FALSE;
+ if(win->backing_store == NULL)
+ needs_resize = TRUE;
+ else {
+ oldwidth = cairo_image_surface_get_width(win->backing_store);
+ oldheight = cairo_image_surface_get_height(win->backing_store);
+ if(oldwidth != event->width || oldheight != event->height)
+ needs_resize = TRUE;
+ }
+
+ if(needs_resize) {
+ /* Create a new backing store */
+ cairo_surface_t *new_backing_store = gdk_window_create_similar_surface( gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, gtk_widget_get_allocated_width(widget), gtk_widget_get_allocated_height(widget) );
+ cairo_t *cr = cairo_create(new_backing_store);
+
+ /* Clear to background color */
+ glkcairo_set_source_glkcolor(cr, win->background_color);
+ cairo_paint(cr);
+
+ if(win->backing_store != NULL) {
+ /* Copy the contents of the old backing store */
+ cairo_set_source_surface(cr, win->backing_store, 0, 0);
+ cairo_paint(cr);
+ cairo_surface_destroy(win->backing_store);
+ }
+
+ cairo_destroy(cr);
+ /* Use the new backing store */
+ win->backing_store = new_backing_store;
+ }
+
+ return TRUE; /* Event handled, stop processing */
+}
+
+/* Draw the backing store to the screen. Called whenever the drawing area is
+exposed. */
+gboolean
+on_graphics_draw(GtkWidget *widget, cairo_t *cr, winid_t win)
+{
+ cairo_set_source_surface(cr, win->backing_store, 0, 0);
+ cairo_paint(cr);
+ return FALSE;