output-item: Make command name part of every output_item.
[pspp] / src / output / chart-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009, 2011 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 "libpspp/str.h"
28 #include "output/driver.h"
29 #include "output/output-item-provider.h"
30
31 #include "gl/xalloc.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 /* Initializes ITEM as a chart item of the specified CLASS.  The new chart item
37    initially has the specified TITLE, which may be NULL if no title is yet
38    available.  The caller retains ownership of TITLE.
39
40    A chart item is an abstract class, that is, a plain chart_item is not useful
41    on its own.  Thus, this function is normally called from the initialization
42    function of some subclass of chart_item. */
43 void
44 chart_item_init (struct chart_item *item, const struct chart_item_class *class,
45                  const char *title)
46 {
47   *item = (struct chart_item) {
48     .output_item = OUTPUT_ITEM_INITIALIZER (&chart_item_class),
49     .class = class,
50     .title = xstrdup_if_nonnull (title)
51   };
52 }
53
54 /* Returns ITEM's title, which is a null pointer if no title has been set. */
55 const char *
56 chart_item_get_title (const struct chart_item *item)
57 {
58   return item->title;
59 }
60
61 /* Sets ITEM's title to TITLE, replacing any previous title.  Specify NULL for
62    TITLE to clear any title from ITEM.  The caller retains ownership of
63    TITLE.
64
65    This function may only be used on a chart_item that is unshared. */
66 void
67 chart_item_set_title (struct chart_item *item, const char *title)
68 {
69   assert (!chart_item_is_shared (item));
70   free (item->title);
71   item->title = xstrdup_if_nonnull (title);
72 }
73
74 /* Submits ITEM to the configured output drivers, and transfers ownership to
75    the output subsystem. */
76 void
77 chart_item_submit (struct chart_item *item)
78 {
79   output_submit (&item->output_item);
80 }
81 \f
82 static const char *
83 chart_item_get_label (const struct output_item *output_item)
84 {
85   const struct chart_item *item = to_chart_item (output_item);
86   return item->title ? item->title : _("Chart");
87 }
88
89 static void
90 chart_item_destroy (struct output_item *output_item)
91 {
92   struct chart_item *item = to_chart_item (output_item);
93   char *title = item->title;
94   item->class->destroy (item);
95   free (title);
96 }
97
98 const struct output_item_class chart_item_class =
99   {
100     chart_item_get_label,
101     chart_item_destroy,
102   };