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