X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fchart.c;h=6f1ce034a3a47875ccb51228513ce22de72e2199;hb=8a0397328b6230fd49724e1c6d91a5a545d2fb4b;hp=fb561a26b86d23a5455cec2a47c8e6c0b738f942;hpb=e0c37920bb2cc46ee559e3992470572d4b4d27e6;p=pspp diff --git a/src/output/chart.c b/src/output/chart.c index fb561a26b8..6f1ce034a3 100644 --- a/src/output/chart.c +++ b/src/output/chart.c @@ -1,5 +1,5 @@ /* 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 @@ -16,101 +16,25 @@ #include -#include -#include - #include -#include -#include -#include -#include -#include #include -#include -#include -#include -#include +#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) -{ - chart->class = class; - chart->ref_cnt = 1; -} - -void -chart_geometry_init (plPlotter *lp, struct chart_geometry *geom, - double width, double length) -{ - /* Start output page. */ - pl_openpl_r (lp); - - /* Set coordinate system. */ - pl_fspace_r (lp, 0.0, 0.0, width, length); - - /* Set line thickness. */ - pl_flinewidth_r (lp, 0.25); - pl_pencolor_r (lp, 0, 0, 0); - - /* Erase graphics display. */ - pl_erase_r (lp); - - pl_filltype_r (lp, 0); - pl_savestate_r(lp); - - /* 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 = 0; - - geom->fill_colour.red = 255; - geom->fill_colour.green = 0; - geom->fill_colour.blue = 0; - - /* Get default font size */ - if (!geom->font_size) - geom->font_size = pl_fontsize_r (lp, -1); - - /* Draw the data area */ - pl_box_r (lp, - geom->data_left, geom->data_bottom, - geom->data_right, geom->data_top); -} - -void -chart_geometry_free (plPlotter *lp) -{ - if (pl_closepl_r (lp) < 0) - fprintf (stderr, "Couldn't close Plotter\n"); -} - -void -chart_draw (const struct chart *chart, plPlotter *lp, - struct chart_geometry *geom) -{ - chart->class->draw (chart, lp, geom); -} - struct chart * chart_ref (const struct chart *chart_) { - struct chart *chart = (struct chart *) chart_; + struct chart *chart = CONST_CAST (struct chart *, chart_); + assert (chart->ref_cnt > 0); chart->ref_cnt++; return chart; } @@ -118,72 +42,68 @@ chart_ref (const struct chart *chart_) void chart_unref (struct chart *chart) { - if (chart != NULL) + if (chart) { assert (chart->ref_cnt > 0); - if (--chart->ref_cnt == 0) - chart->class->destroy (chart); + if (!--chart->ref_cnt) + { + char *title = chart->title; + chart->class->destroy (chart); + free (title); + } } } -void -chart_submit (struct chart *chart) +bool +chart_is_shared (const struct chart *chart) { -#ifdef HAVE_LIBPLOT - struct outp_driver *d; + assert (chart->ref_cnt > 0); + return chart->ref_cnt > 1; +} - for (d = outp_drivers (NULL); d; d = outp_drivers (d)) - if (d->class->output_chart != NULL) - d->class->output_chart (d, chart); -#endif +/* 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. - chart_unref (chart); + 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_init (struct chart *chart, const struct chart_class *class, + const char *title) +{ + *chart = (struct chart) { + .ref_cnt = 1, + .class = class, + .title = xstrdup_if_nonnull (title), + }; } -bool -chart_create_file (const char *type, const char *file_name_tmpl, int number, - plPlotterParams *params, char **file_namep, plPlotter **lpp) +/* Returns CHART's title, which is a null pointer if no title has been set. */ +const char * +chart_get_title (const struct chart *chart) { - char *file_name = NULL; - FILE *fp = NULL; - int number_pos; - plPlotter *lp; - - number_pos = strchr (file_name_tmpl, '#') - file_name_tmpl; - file_name = xasprintf ("%.*s%d%s", number_pos, file_name_tmpl, - number, file_name_tmpl + number_pos + 1); - - fp = fopen (file_name, "wb"); - if (fp == NULL) - { - error (0, errno, _("creating \"%s\""), file_name); - goto error; - } + return chart->title; +} - if (params != NULL) - lp = pl_newpl_r (type, 0, fp, stderr, params); - else - { - params = pl_newplparams (); - lp = pl_newpl_r (type, 0, fp, stderr, params); - pl_deleteplparams (params); - } - if (lp == NULL) - goto error; +/* 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. - *file_namep = file_name; - *lpp = lp; - return true; + This function may only be used on a chart that is unshared. */ +void +chart_set_title (struct chart *chart, const char *title) +{ + assert (!chart_is_shared (chart)); + free (chart->title); + chart->title = xstrdup_if_nonnull (title); +} -error: - if (fp != NULL) - { - fclose (fp); - if (file_name != NULL) - unlink (file_name); - } - free (file_name); - *file_namep = NULL; - *lpp = NULL; - return false; +/* Submits CHART to the configured output drivers, and transfers ownership to + the output subsystem. */ +void +chart_submit (struct chart *chart) +{ + if (chart) + output_item_submit (chart_item_create (chart)); }