basics work now
[pspp] / src / output / chart.c
index 2a2bc12451d8c5fc5187c4d13415c09b47f88933..6f1ce034a3a47875ccb51228513ce22de72e2199 100644 (file)
@@ -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
 
 #include <config.h>
 
-#include <output/chart.h>
-#include <output/chart-provider.h>
-
 #include <assert.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)
-{
-  chart->class = class;
-  chart->ref_cnt = 1;
-}
-
-void
-chart_geometry_init (plPlotter *lp, struct chart_geometry *geom)
-{
-  /* Start output page. */
-  pl_openpl_r (lp);
-
-  /* Set coordinate system. */
-  pl_fspace_r (lp, 0.0, 0.0, 1000.0, 1000.0);
-
-  /* Set line thickness. */
-  pl_flinewidth_r (lp, 0.25);
-  pl_pencolorname_r (lp, "black");
-
-  /* Erase graphics display. */
-  pl_erase_r (lp);
-
-  pl_filltype_r (lp, 0);
-  pl_savestate_r(lp);
-
-  /* Set default chartetry. */
-  geom->data_top = 900;
-  geom->data_right = 800;
-  geom->data_bottom = 120;
-  geom->data_left = 150;
-  geom->abscissa_top = 70;
-  geom->ordinate_right = 120;
-  geom->title_bottom = 920;
-  geom->legend_left = 810;
-  geom->legend_right = 1000;
-  geom->font_size = 0;
-  strcpy (geom->fill_colour, "red");
-
-  /* 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)
-{
-  chart->class->draw (chart, lp);
-}
-
 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;
 }
@@ -113,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));
 }