output: Make groups contain their subitems, and get rid of spv_item.
[pspp] / src / output / output-item.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 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 #ifndef OUTPUT_ITEM_H
18 #define OUTPUT_ITEM_H 1
19
20 /* Output items.
21
22    An output item is a self-contained chunk of output.
23 */
24
25 #include <cairo.h>
26 #include <stdbool.h>
27 #include "libpspp/cast.h"
28 #include "libpspp/string-array.h"
29
30 enum output_item_type
31   {
32     OUTPUT_ITEM_CHART,
33     OUTPUT_ITEM_GROUP,
34     OUTPUT_ITEM_IMAGE,
35     OUTPUT_ITEM_MESSAGE,
36     OUTPUT_ITEM_PAGE_BREAK,
37     OUTPUT_ITEM_PAGE_SETUP,
38     OUTPUT_ITEM_TABLE,
39     OUTPUT_ITEM_TEXT,
40   };
41
42 const char *output_item_type_to_string (enum output_item_type);
43
44 /* A single output item. */
45 struct output_item
46   {
47     /* Reference count.  An output item may be shared between multiple owners,
48        indicated by a reference count greater than 1.  When this is the case,
49        the output item must not be modified. */
50     int ref_cnt;
51
52     /* The localized label for the item that appears in the outline pane in the
53        PSPPIRE output viewer and in PDF outlines.  This is NULL if no label has
54        been explicitly set.
55
56        Use output_item_get_label() to read an item's label. */
57     char *label;
58
59     /* A locale-invariant identifier for the command that produced the output,
60        which may be NULL if unknown or if a command did not produce this
61        output. */
62     char *command_name;
63
64     /* For OUTPUT_ITEM_GROUP, this is true if the group's subtree should
65        be expanded in an outline view, false otherwise.
66
67        For other kinds of output items, this is true to show the item's
68        content, false to hide it.  The item's label is always shown in an
69        outline view. */
70     bool show;
71
72     /* Information about the SPV file this output_item was read from.
73        May be NULL. */
74     struct spv_info *spv_info;
75
76     enum output_item_type type;
77     union
78       {
79         struct chart *chart;
80
81         cairo_surface_t *image;
82
83         struct
84           {
85             struct output_item **children;
86             size_t n_children;
87             size_t allocated_children;
88           }
89         group;
90
91         struct msg *message;
92
93         struct page_setup *page_setup;
94
95         struct pivot_table *table;
96
97         struct
98           {
99             enum text_item_subtype
100               {
101                 TEXT_ITEM_PAGE_TITLE,       /* TITLE and SUBTITLE commands. */
102                 TEXT_ITEM_TITLE,            /* Title. */
103                 TEXT_ITEM_SYNTAX,           /* Syntax printback logging. */
104                 TEXT_ITEM_LOG,              /* Other logging. */
105               }
106             subtype;
107             struct pivot_value *content;
108           }
109         text;
110       };
111
112     char *cached_label;
113   };
114
115 struct output_item *output_item_ref (const struct output_item *);
116 void output_item_unref (struct output_item *);
117 bool output_item_is_shared (const struct output_item *);
118 struct output_item *output_item_unshare (struct output_item *);
119
120 void output_item_submit (struct output_item *);
121 void output_item_submit_children (struct output_item *);
122
123 const char *output_item_get_label (const struct output_item *);
124 void output_item_set_label (struct output_item *, const char *);
125 void output_item_set_label_nocopy (struct output_item *, char *);
126
127 void output_item_set_command_name (struct output_item *, const char *);
128 void output_item_set_command_name_nocopy (struct output_item *, char *);
129
130 char *output_item_get_subtype (const struct output_item *);
131
132 void output_item_add_spv_info (struct output_item *);
133
134 void output_item_dump (const struct output_item *, int indentation);
135 \f
136 /* In-order traversal of a tree of output items. */
137
138 struct output_iterator_node
139   {
140     const struct output_item *group;
141     size_t idx;
142   };
143
144 struct output_iterator
145   {
146     const struct output_item *cur;
147     struct output_iterator_node *nodes;
148     size_t n, allocated;
149   };
150 #define OUTPUT_ITERATOR_INIT(ITEM) { .cur = ITEM }
151
152 /* Iteration functions. */
153 void output_iterator_init (struct output_iterator *,
154                            const struct output_item *);
155 void output_iterator_destroy (struct output_iterator *);
156 void output_iterator_next (struct output_iterator *);
157
158 /* Iteration helper macros. */
159 #define OUTPUT_ITEM_FOR_EACH(ITER, ROOT) \
160   for (output_iterator_init (ITER, ROOT); (ITER)->cur; \
161        output_iterator_next (ITER))
162 #define OUTPUT_ITEM_FOR_EACH_SKIP_ROOT(ITER, ROOT) \
163   for (output_iterator_init (ITER, ROOT), output_iterator_next (ITER); \
164        (ITER)->cur; output_iterator_next (ITER))
165 \f
166 /* OUTPUT_ITEM_CHART. */
167 struct output_item *chart_item_create (struct chart *);
168 \f
169 /* OUTPUT_ITEM_GROUP. */
170 struct output_item *group_item_create (const char *command_name,
171                                        const char *label);
172 struct output_item *group_item_create_nocopy (char *command_name, char *label);
173
174 void group_item_add_child (struct output_item *parent,
175                            struct output_item *child);
176
177 struct output_item *root_item_create (void);
178
179 struct output_item *group_item_clone_empty (const struct output_item *);
180 \f
181 /* OUTPUT_ITEM_IMAGE. */
182
183 struct output_item *image_item_create (cairo_surface_t *);
184 \f
185 /* OUTPUT_ITEM_MESSAGE. */
186
187 struct output_item *message_item_create (const struct msg *);
188
189 const struct msg *message_item_get_msg (const struct output_item *);
190
191 struct output_item *message_item_to_text_item (struct output_item *);
192 \f
193 /* OUTPUT_ITEM_PAGE_BREAK. */
194
195 struct output_item *page_break_item_create (void);
196 \f
197 /* OUTPUT_ITEM_PAGE_SETUP. */
198
199 struct output_item *page_setup_item_create (const struct page_setup *);
200 \f
201 /* OUTPUT_ITEM_TABLE. */
202
203 struct output_item *table_item_create (struct pivot_table *);
204 \f
205 /* OUTPUT_ITEM_TEXT. */
206
207 struct output_item *text_item_create (enum text_item_subtype,
208                                       const char *text, const char *label);
209 struct output_item *text_item_create_nocopy (enum text_item_subtype,
210                                              char *text, char *label);
211 struct output_item *text_item_create_value (enum text_item_subtype,
212                                             struct pivot_value *value,
213                                             char *label);
214
215 enum text_item_subtype text_item_get_subtype (const struct output_item *);
216 char *text_item_get_plain_text (const struct output_item *);
217
218 bool text_item_append (struct output_item *dst, const struct output_item *src);
219
220 struct output_item *text_item_to_table_item (struct output_item *);
221
222 const char *text_item_subtype_to_string (enum text_item_subtype);
223 \f
224 /* An informational node for output items that were read from an .spv file.
225    This is mostly for debugging and troubleshooting purposes with the
226    pspp-output program. */
227 struct spv_info
228   {
229     /* The .spv file. */
230     struct zip_reader *zip_reader;
231
232     /* True if there was an error reading the output item (e.g. because of
233        corruption or because PSPP doesn't understand the format.) */
234     bool error;
235
236     /* Zip member names.  All may be NULL. */
237     char *structure_member;
238     char *xml_member;
239     char *bin_member;
240     char *png_member;
241   };
242
243 void spv_info_destroy (struct spv_info *);
244 struct spv_info *spv_info_clone (const struct spv_info *);
245 size_t spv_info_get_members (const struct spv_info *, const char **members,
246                              size_t allocated_members);
247
248 #endif /* output/output-item.h */