+ if (bl->pixmap)
+ gdk_pixmap_unref(bl->pixmap);
+}
+
+static void do_blitter_save(frontend *fe, blitter *bl, int x, int y)
+{
+ if (!bl->pixmap)
+ bl->pixmap = gdk_pixmap_new(fe->area->window, bl->w, bl->h, -1);
+ gdk_draw_pixmap(bl->pixmap,
+ fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
+ fe->pixmap,
+ x, y, 0, 0, bl->w, bl->h);
+}
+
+static void do_blitter_load(frontend *fe, blitter *bl, int x, int y)
+{
+ assert(bl->pixmap);
+ gdk_draw_pixmap(fe->pixmap,
+ fe->area->style->fg_gc[GTK_WIDGET_STATE(fe->area)],
+ bl->pixmap,
+ 0, 0, x, y, bl->w, bl->h);
+}
+
+static void clear_backing_store(frontend *fe)
+{
+ fe->pixmap = NULL;
+}
+
+static void setup_backing_store(frontend *fe)
+{
+ GdkGC *gc;
+
+ fe->pixmap = gdk_pixmap_new(fe->area->window, fe->pw, fe->ph, -1);
+
+ gc = gdk_gc_new(fe->area->window);
+ gdk_gc_set_foreground(gc, &fe->colours[0]);
+ gdk_draw_rectangle(fe->pixmap, gc, 1, 0, 0, fe->pw, fe->ph);
+ gdk_draw_rectangle(fe->area->window, gc, 1, 0, 0, fe->w, fe->h);
+ gdk_gc_unref(gc);
+}
+
+static int backing_store_ok(frontend *fe)
+{
+ return (!!fe->pixmap);
+}
+
+static void teardown_backing_store(frontend *fe)
+{
+ gdk_pixmap_unref(fe->pixmap);
+ fe->pixmap = NULL;
+}
+
+#endif
+
+static void repaint_rectangle(frontend *fe, GtkWidget *widget,
+ int x, int y, int w, int h)
+{
+ GdkGC *gc = gdk_gc_new(widget->window);
+#ifdef USE_CAIRO
+ gdk_gc_set_foreground(gc, &fe->background);
+#else
+ gdk_gc_set_foreground(gc, &fe->colours[fe->backgroundindex]);
+#endif
+ if (x < fe->ox) {
+ gdk_draw_rectangle(widget->window, gc,
+ TRUE, x, y, fe->ox - x, h);
+ w -= (fe->ox - x);
+ x = fe->ox;
+ }
+ if (y < fe->oy) {
+ gdk_draw_rectangle(widget->window, gc,
+ TRUE, x, y, w, fe->oy - y);
+ h -= (fe->oy - y);
+ y = fe->oy;
+ }
+ if (w > fe->pw) {
+ gdk_draw_rectangle(widget->window, gc,
+ TRUE, x + fe->pw, y, w - fe->pw, h);
+ w = fe->pw;
+ }
+ if (h > fe->ph) {
+ gdk_draw_rectangle(widget->window, gc,
+ TRUE, x, y + fe->ph, w, h - fe->ph);
+ h = fe->ph;
+ }
+ gdk_draw_pixmap(widget->window, gc, fe->pixmap,
+ x - fe->ox, y - fe->oy, x, y, w, h);
+ gdk_gc_unref(gc);
+}
+
+/* ----------------------------------------------------------------------
+ * Pango font functions.
+ */
+
+#ifdef USE_PANGO
+
+static void add_font(frontend *fe, int index, int fonttype, int fontsize)
+{
+ /*
+ * Use Pango to find the closest match to the requested
+ * font.
+ */
+ PangoFontDescription *fd;
+
+ fd = pango_font_description_new();
+ /* `Monospace' and `Sans' are meta-families guaranteed to exist */
+ pango_font_description_set_family(fd, fonttype == FONT_FIXED ?
+ "Monospace" : "Sans");
+ pango_font_description_set_weight(fd, PANGO_WEIGHT_BOLD);
+ /*
+ * I found some online Pango documentation which
+ * described a function called
+ * pango_font_description_set_absolute_size(), which is
+ * _exactly_ what I want here. Unfortunately, none of
+ * my local Pango installations have it (presumably
+ * they're too old), so I'm going to have to hack round
+ * it by figuring out the point size myself. This
+ * limits me to X and probably also breaks in later
+ * Pango installations, so ideally I should add another
+ * CHECK_VERSION type ifdef and use set_absolute_size
+ * where available. All very annoying.
+ */
+#ifdef HAVE_SENSIBLE_ABSOLUTE_SIZE_FUNCTION
+ pango_font_description_set_absolute_size(fd, PANGO_SCALE*fontsize);
+#else
+ {
+ Display *d = GDK_DISPLAY();
+ int s = DefaultScreen(d);
+ double resolution =
+ (PANGO_SCALE * 72.27 / 25.4) *
+ ((double) DisplayWidthMM(d, s) / DisplayWidth (d, s));
+ pango_font_description_set_size(fd, resolution * fontsize);
+ }
+#endif
+ fe->fonts[index].desc = fd;
+}
+
+static void align_and_draw_text(frontend *fe,
+ int index, int align, int x, int y,
+ const char *text)
+{
+ PangoLayout *layout;
+ PangoRectangle rect;
+
+ layout = make_pango_layout(fe);
+
+ /*
+ * Create a layout.
+ */
+ pango_layout_set_font_description(layout, fe->fonts[index].desc);
+ pango_layout_set_text(layout, text, strlen(text));
+ pango_layout_get_pixel_extents(layout, NULL, &rect);
+
+ if (align & ALIGN_VCENTRE)
+ rect.y -= rect.height / 2;
+ else
+ rect.y -= rect.height;
+
+ if (align & ALIGN_HCENTRE)
+ rect.x -= rect.width / 2;
+ else if (align & ALIGN_HRIGHT)
+ rect.x -= rect.width;
+
+ draw_pango_layout(fe, layout, rect.x + x, rect.y + y);
+
+ g_object_unref(layout);
+}
+
+#endif
+
+/* ----------------------------------------------------------------------
+ * Old-fashioned font functions.
+ */
+
+#ifndef USE_PANGO
+
+static void add_font(int index, int fonttype, int fontsize)
+{
+ /*
+ * In GTK 1.2, I don't know of any plausible way to
+ * pick a suitable font, so I'm just going to be
+ * tedious.
+ */
+ fe->fonts[i].font = gdk_font_load(fonttype == FONT_FIXED ?
+ "fixed" : "variable");
+}
+
+static void align_and_draw_text(int index, int align, int x, int y,
+ const char *text)
+{
+ int lb, rb, wid, asc, desc;
+
+ /*
+ * Measure vertical string extents with respect to the same
+ * string always...
+ */
+ gdk_string_extents(fe->fonts[i].font,
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
+ &lb, &rb, &wid, &asc, &desc);
+ if (align & ALIGN_VCENTRE)
+ y += asc - (asc+desc)/2;
+ else
+ y += asc;
+
+ /*
+ * ... but horizontal extents with respect to the provided
+ * string. This means that multiple pieces of text centred
+ * on the same y-coordinate don't have different baselines.
+ */
+ gdk_string_extents(fe->fonts[i].font, text,
+ &lb, &rb, &wid, &asc, &desc);
+
+ if (align & ALIGN_HCENTRE)
+ x -= wid / 2;
+ else if (align & ALIGN_HRIGHT)
+ x -= wid;
+
+ /*
+ * Actually draw the text.
+ */
+ gdk_draw_string(fe->pixmap, fe->fonts[i].font, fe->gc, x, y, text);
+}
+
+#endif
+
+/* ----------------------------------------------------------------------
+ * The exported drawing functions.
+ */
+
+void gtk_start_draw(void *handle)
+{
+ frontend *fe = (frontend *)handle;
+ fe->bbox_l = fe->w;
+ fe->bbox_r = 0;
+ fe->bbox_u = fe->h;
+ fe->bbox_d = 0;
+ setup_drawing(fe);
+}
+
+void gtk_clip(void *handle, int x, int y, int w, int h)
+{
+ frontend *fe = (frontend *)handle;
+ do_clip(fe, x, y, w, h);
+}
+
+void gtk_unclip(void *handle)
+{
+ frontend *fe = (frontend *)handle;
+ do_unclip(fe);
+}
+
+void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
+ int align, int colour, char *text)
+{
+ frontend *fe = (frontend *)handle;