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