e9b854040a823982c36ac96690f7594c9a3a90b0
[pspp] / src / output / table-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011, 2014 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/table-provider.h"
20
21 #include <assert.h>
22
23 #include "libpspp/assertion.h"
24 #include "libpspp/cast.h"
25 #include "output/driver.h"
26 #include "output/output-item-provider.h"
27 #include "output/table-item.h"
28
29 #include "gl/xalloc.h"
30
31 struct table_item_text *
32 table_item_text_create (const char *content)
33 {
34   if (!content)
35     return NULL;
36
37   struct table_item_text *text = xmalloc (sizeof *text);
38   *text = (struct table_item_text) { .content = xstrdup (content) };
39   return text;
40 }
41
42 struct table_item_text *
43 table_item_text_clone (const struct table_item_text *old)
44 {
45   if (!old)
46     return NULL;
47
48   struct table_item_text *new = xmalloc (sizeof *new);
49   *new = (struct table_item_text) {
50     .content = xstrdup (old->content),
51     .footnotes = xmemdup (old->footnotes,
52                           old->n_footnotes * sizeof *old->footnotes),
53     .n_footnotes = old->n_footnotes,
54     .style = area_style_clone (NULL, old->style),
55   };
56   return new;
57 }
58
59 void
60 table_item_text_destroy (struct table_item_text *text)
61 {
62   if (text)
63     {
64       free (text->content);
65       free (text->footnotes);
66       area_style_free (text->style);
67       free (text);
68     }
69 }
70
71 /* Initializes ITEM as a table item for rendering TABLE.  The new table item
72    initially has the specified TITLE and CAPTION, which may each be NULL.  The
73    caller retains ownership of TITLE and CAPTION. */
74 struct table_item *
75 table_item_create (struct table *table, const char *title, const char *caption)
76 {
77   struct table_item *item = xmalloc (sizeof *item);
78   output_item_init (&item->output_item, &table_item_class);
79   item->table = table;
80   item->title = table_item_text_create (title);
81   item->layers = NULL;
82   item->caption = table_item_text_create (caption);
83   return item;
84 }
85
86 /* Returns the table contained by TABLE_ITEM.  The caller must not modify or
87    unref the returned table. */
88 const struct table *
89 table_item_get_table (const struct table_item *table_item)
90 {
91   return table_item->table;
92 }
93
94 /* Returns ITEM's title, which is a null pointer if no title has been
95    set. */
96 const struct table_item_text *
97 table_item_get_title (const struct table_item *item)
98 {
99   return item->title;
100 }
101
102 /* Sets ITEM's title to TITLE, replacing any previous title.  Specify NULL for
103    TITLE to clear any title from ITEM.  The caller retains ownership of TITLE.
104
105    This function may only be used on a table_item that is unshared. */
106 void
107 table_item_set_title (struct table_item *item,
108                       const struct table_item_text *title)
109 {
110   assert (!table_item_is_shared (item));
111   table_item_text_destroy (item->title);
112   item->title = table_item_text_clone (title);
113 }
114
115 /* Returns ITEM's layers, which will be a null pointer if no layers have been
116    set. */
117 const struct table_item_text *
118 table_item_get_layers (const struct table_item *item)
119 {
120   return item->layers;
121 }
122
123 /* Sets ITEM's layers to LAYERS, replacing any previous layers.  Specify NULL
124    for LAYERS to clear any layers from ITEM.  The caller retains ownership of
125    LAYERS.
126
127    This function may only be used on a table_item that is unshared. */
128 void
129 table_item_set_layers (struct table_item *item,
130                       const struct table_item_text *layers)
131 {
132   assert (!table_item_is_shared (item));
133   table_item_text_destroy (item->layers);
134   item->layers = table_item_text_clone (layers);
135 }
136
137 /* Returns ITEM's caption, which is a null pointer if no caption has been
138    set. */
139 const struct table_item_text *
140 table_item_get_caption (const struct table_item *item)
141 {
142   return item->caption;
143 }
144
145 /* Sets ITEM's caption to CAPTION, replacing any previous caption.  Specify
146    NULL for CAPTION to clear any caption from ITEM.  The caller retains
147    ownership of CAPTION.
148
149    This function may only be used on a table_item that is unshared. */
150 void
151 table_item_set_caption (struct table_item *item,
152                         const struct table_item_text *caption)
153 {
154   assert (!table_item_is_shared (item));
155   table_item_text_destroy (item->caption);
156   item->caption = table_item_text_clone (caption);
157 }
158
159 /* Submits TABLE_ITEM to the configured output drivers, and transfers ownership
160    to the output subsystem. */
161 void
162 table_item_submit (struct table_item *table_item)
163 {
164   output_submit (&table_item->output_item);
165 }
166 \f
167 static void
168 table_item_destroy (struct output_item *output_item)
169 {
170   struct table_item *item = to_table_item (output_item);
171   table_item_text_destroy (item->title);
172   table_item_text_destroy (item->layers);
173   table_item_text_destroy (item->caption);
174   table_unref (item->table);
175   free (item);
176 }
177
178 const struct output_item_class table_item_class =
179   {
180     "table",
181     table_item_destroy,
182   };