+ "cairo",
+ xr_destroy,
+ xr_submit,
+ xr_flush,
+};
+\f
+/* GUI rendering helpers. */
+
+struct xr_rendering
+ {
+ struct output_item *item;
+
+ /* Table items. */
+ struct render_pager *p;
+ struct xr_driver *xr;
+ };
+
+#define CHART_WIDTH 500
+#define CHART_HEIGHT 375
+
+
+
+struct xr_driver *
+xr_driver_create (cairo_t *cairo, struct string_map *options)
+{
+ struct xr_driver *xr = xr_allocate ("cairo", 0, options);
+ if (!xr_set_cairo (xr, cairo))
+ {
+ output_driver_destroy (&xr->driver);
+ return NULL;
+ }
+ return xr;
+}
+
+/* Destroy XR, which should have been created with xr_driver_create(). Any
+ cairo_t added to XR is not destroyed, because it is owned by the client. */
+void
+xr_driver_destroy (struct xr_driver *xr)
+{
+ if (xr != NULL)
+ {
+ xr->cairo = NULL;
+ output_driver_destroy (&xr->driver);
+ }
+}
+
+static struct xr_rendering *
+xr_rendering_create_text (struct xr_driver *xr, const char *text, cairo_t *cr)
+{
+ struct table_item *table_item;
+ struct xr_rendering *r;
+
+ table_item = table_item_create (table_from_string (TAB_LEFT, text),
+ NULL, NULL);
+ r = xr_rendering_create (xr, &table_item->output_item, cr);
+ table_item_unref (table_item);
+
+ return r;
+}
+
+void
+xr_rendering_apply_options (struct xr_rendering *xr, struct string_map *o)
+{
+ if (is_table_item (xr->item))
+ apply_options (xr->xr, o);
+}
+
+struct xr_rendering *
+xr_rendering_create (struct xr_driver *xr, const struct output_item *item,
+ cairo_t *cr)
+{
+ struct xr_rendering *r = NULL;
+
+ if (is_text_item (item))
+ r = xr_rendering_create_text (xr, text_item_get_text (to_text_item (item)),
+ cr);
+ else if (is_message_item (item))
+ {
+ const struct message_item *message_item = to_message_item (item);
+ const struct msg *msg = message_item_get_msg (message_item);
+ char *s = msg_to_string (msg, NULL);
+ r = xr_rendering_create_text (xr, s, cr);
+ free (s);
+ }
+ else if (is_table_item (item))
+ {
+ r = xzalloc (sizeof *r);
+ r->item = output_item_ref (item);
+ r->xr = xr;
+ xr_set_cairo (xr, cr);
+ r->p = render_pager_create (xr->params, to_table_item (item));
+ }
+ else if (is_chart_item (item))
+ {
+ r = xzalloc (sizeof *r);
+ r->item = output_item_ref (item);
+ }
+
+ return r;
+}
+
+void
+xr_rendering_destroy (struct xr_rendering *r)
+{
+ if (r)
+ {
+ output_item_unref (r->item);
+ render_pager_destroy (r->p);
+ free (r);
+ }
+}
+
+void
+xr_rendering_measure (struct xr_rendering *r, int *w, int *h)
+{
+ if (is_table_item (r->item))
+ {
+ *w = render_pager_get_size (r->p, H) / XR_POINT;
+ *h = render_pager_get_size (r->p, V) / XR_POINT;
+ }
+ else
+ {
+ *w = CHART_WIDTH;
+ *h = CHART_HEIGHT;
+ }
+}
+
+static void xr_draw_chart (const struct chart_item *, cairo_t *,
+ double x, double y, double width, double height);
+
+/* Draws onto CR */
+void
+xr_rendering_draw_all (struct xr_rendering *r, cairo_t *cr)
+{
+ if (is_table_item (r->item))
+ {
+ struct xr_driver *xr = r->xr;
+
+ xr_set_cairo (xr, cr);
+
+ render_pager_draw (r->p);
+
+ }
+ else
+ xr_draw_chart (to_chart_item (r->item), cr,
+ 0, 0, CHART_WIDTH, CHART_HEIGHT);