/* PSPP - a program for statistical analysis.
- Copyright (C) 2004, 2009 Free Software Foundation, Inc.
+ Copyright (C) 2004, 2009, 2011 Free Software Foundation, Inc.
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
#include <config.h>
-#include <output/chart.h>
-#include <output/chart-provider.h>
-
#include <assert.h>
-#include <cairo/cairo.h>
-#include <errno.h>
-#include <float.h>
-#include <math.h>
-#include <stdarg.h>
-#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include <libpspp/str.h>
-#include <output/manager.h>
-#include <output/output.h>
+#include "libpspp/cast.h"
+#include "libpspp/compiler.h"
+#include "libpspp/str.h"
+#include "output/chart-provider.h"
+#include "output/output-item.h"
-#include "error.h"
-#include "xalloc.h"
+#include "gl/xalloc.h"
#include "gettext.h"
#define _(msgid) gettext (msgid)
-extern struct som_table_class tab_table_class;
-
-void
-chart_init (struct chart *chart, const struct chart_class *class)
+struct chart *
+chart_ref (const struct chart *chart_)
{
- chart->class = class;
- chart->ref_cnt = 1;
+ struct chart *chart = CONST_CAST (struct chart *, chart_);
+ assert (chart->ref_cnt > 0);
+ chart->ref_cnt++;
+ return chart;
}
void
-chart_geometry_init (cairo_t *cr, struct chart_geometry *geom,
- double width, double length)
+chart_unref (struct chart *chart)
{
- /* Set default chartetry. */
- geom->data_top = 0.900 * length;
- geom->data_right = 0.800 * width;
- geom->data_bottom = 0.120 * length;
- geom->data_left = 0.150 * width;
- geom->abscissa_top = 0.070 * length;
- geom->ordinate_right = 0.120 * width;
- geom->title_bottom = 0.920 * length;
- geom->legend_left = 0.810 * width;
- geom->legend_right = width;
- geom->font_size = 15.0;
-
- geom->fill_colour.red = 255;
- geom->fill_colour.green = 0;
- geom->fill_colour.blue = 0;
-
- cairo_set_line_width (cr, 1.0);
-
- cairo_rectangle (cr, geom->data_left, geom->data_bottom,
- geom->data_right - geom->data_left,
- geom->data_top - geom->data_bottom);
- cairo_stroke (cr);
+ if (chart)
+ {
+ assert (chart->ref_cnt > 0);
+ if (!--chart->ref_cnt)
+ {
+ char *title = chart->title;
+ chart->class->destroy (chart);
+ free (title);
+ }
+ }
}
-void
-chart_geometry_free (cairo_t *cr UNUSED)
+bool
+chart_is_shared (const struct chart *chart)
{
+ assert (chart->ref_cnt > 0);
+ return chart->ref_cnt > 1;
}
+/* Initializes CHART as a chart of the specified CLASS. The new chart
+ initially has the specified TITLE, which may be NULL if no title is yet
+ available. The caller retains ownership of TITLE.
+
+ A chart is abstract, that is, a plain chart is not useful on its own. Thus,
+ this function is normally called from the initialization function of some
+ subclass of chart. */
void
-chart_draw (const struct chart *chart, cairo_t *cr,
- struct chart_geometry *geom)
+chart_init (struct chart *chart, const struct chart_class *class,
+ const char *title)
{
- chart->class->draw (chart, cr, geom);
+ *chart = (struct chart) {
+ .ref_cnt = 1,
+ .class = class,
+ .title = xstrdup_if_nonnull (title),
+ };
}
-char *
-chart_draw_png (const struct chart *chart, const char *file_name_template,
- int number)
+/* Returns CHART's title, which is a null pointer if no title has been set. */
+const char *
+chart_get_title (const struct chart *chart)
{
- const int width = 640;
- const int length = 480;
-
- struct chart_geometry geom;
- cairo_surface_t *surface;
- cairo_status_t status;
- const char *number_pos;
- char *file_name;
- cairo_t *cr;
-
- number_pos = strchr (file_name_template, '#');
- if (number_pos != NULL)
- file_name = xasprintf ("%.*s%d%s", (int) (number_pos - file_name_template),
- file_name_template, number, number_pos + 1);
- else
- file_name = xstrdup (file_name_template);
-
- surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, length);
- cr = cairo_create (surface);
-
- cairo_translate (cr, 0.0, length);
- cairo_scale (cr, 1.0, -1.0);
-
- cairo_save (cr);
- cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
- cairo_rectangle (cr, 0, 0, width, length);
- cairo_fill (cr);
- cairo_restore (cr);
-
- cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
-
- chart_geometry_init (cr, &geom, width, length);
- chart_draw (chart, cr, &geom);
- chart_geometry_free (cr);
-
- status = cairo_surface_write_to_png (surface, file_name);
- if (status != CAIRO_STATUS_SUCCESS)
- error (0, 0, _("writing output file \"%s\": %s"),
- file_name, cairo_status_to_string (status));
-
- cairo_destroy (cr);
- cairo_surface_destroy (surface);
-
- return file_name;
+ return chart->title;
}
+/* Sets CHART's title to TITLE, replacing any previous title. Specify NULL for
+ TITLE to clear any title from CHART. The caller retains ownership of
+ TITLE.
-struct chart *
-chart_ref (const struct chart *chart_)
-{
- struct chart *chart = (struct chart *) chart_;
- chart->ref_cnt++;
- return chart;
-}
-
+ This function may only be used on a chart that is unshared. */
void
-chart_unref (struct chart *chart)
+chart_set_title (struct chart *chart, const char *title)
{
- if (chart != NULL)
- {
- assert (chart->ref_cnt > 0);
- if (--chart->ref_cnt == 0)
- chart->class->destroy (chart);
- }
+ assert (!chart_is_shared (chart));
+ free (chart->title);
+ chart->title = xstrdup_if_nonnull (title);
}
+/* Submits CHART to the configured output drivers, and transfers ownership to
+ the output subsystem. */
void
chart_submit (struct chart *chart)
{
-#ifdef HAVE_CAIRO
- struct outp_driver *d;
-
- for (d = outp_drivers (NULL); d; d = outp_drivers (d))
- if (d->class->output_chart != NULL)
- d->class->output_chart (d, chart);
-#endif
-
- chart_unref (chart);
+ if (chart)
+ output_item_submit (chart_item_create (chart));
}