227994dd219060676f218ae0ee57e565adc379d1
[pspp-builds.git] / src / output / chart-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <output/chart-item.h>
20 #include <output/chart-item-provider.h>
21
22 #include <assert.h>
23 #include <stdlib.h>
24
25 #include <libpspp/cast.h>
26 #include <libpspp/compiler.h>
27 #include <output/driver.h>
28 #include <output/output-item-provider.h>
29
30 #include "gl/xalloc.h"
31
32 /* Initializes ITEM as a chart item of the specified CLASS.  The new chart item
33    initially has the specified TITLE, which may be NULL if no title is yet
34    available.  The caller retains ownership of TITLE.
35
36    A chart item is an abstract class, that is, a plain chart_item is not useful
37    on its own.  Thus, this function is normally called from the initialization
38    function of some subclass of chart_item. */
39 void
40 chart_item_init (struct chart_item *item, const struct chart_item_class *class,
41                  const char *title)
42 {
43   output_item_init (&item->output_item, &chart_item_class);
44   item->class = class;
45   item->title = title != NULL ? xstrdup (title) : NULL;
46 }
47
48 /* Returns ITEM's title, which is a null pointer if no title has been set. */
49 const char *
50 chart_item_get_title (const struct chart_item *item)
51 {
52   return item->title;
53 }
54
55 /* Sets ITEM's title to TITLE, replacing any previous title.  Specify NULL for
56    TITLE to clear any title from ITEM.  The caller retains ownership of
57    TITLE.
58
59    This function may only be used on a chart_item that is unshared. */
60 void
61 chart_item_set_title (struct chart_item *item, const char *title)
62 {
63   assert (!chart_item_is_shared (item));
64   free (item->title);
65   item->title = title != NULL ? xstrdup (title) : NULL;
66 }
67
68 /* Submits ITEM to the configured output drivers, and transfers ownership to
69    the output subsystem. */
70 void
71 chart_item_submit (struct chart_item *item)
72 {
73   output_submit (&item->output_item);
74 }
75 \f
76 static void
77 chart_item_destroy (struct output_item *output_item)
78 {
79   struct chart_item *item = to_chart_item (output_item);
80   char *title = item->title;
81   item->class->destroy (item);
82   free (title);
83 }
84
85 const struct output_item_class chart_item_class =
86   {
87     chart_item_destroy,
88   };