work
[pspp] / src / output / table-provider.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997, 1998, 1999, 2000, 2009, 2011, 2013, 2014, 2018 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_TABLE_PROVIDER
18 #define OUTPUT_TABLE_PROVIDER 1
19
20 #include <stdint.h>
21 #include "output/table.h"
22
23 struct pool;
24 struct string;
25
26 enum table_halign table_halign_interpret (enum table_halign, bool numeric);
27
28 struct footnote
29   {
30     size_t idx;
31     char *content;
32     char *marker;
33     struct area_style *style;
34   };
35
36 /* A cell in a table. */
37 struct table_cell
38   {
39     /* Occupied table region.
40
41        d[TABLE_HORZ][0] is the leftmost column.
42        d[TABLE_HORZ][1] is the rightmost column, plus 1.
43        d[TABLE_VERT][0] is the top row.
44        d[TABLE_VERT][1] is the bottom row, plus 1.
45
46        For an ordinary cell:
47            d[TABLE_HORZ][1] == d[TABLE_HORZ][0] + 1
48        and d[TABLE_VERT][1] == d[TABLE_VERT][0] + 1
49
50        For a joined cell:
51           d[TABLE_HORZ][1] > d[TABLE_HORZ][0] + 1
52        or d[TABLE_VERT][1] > d[TABLE_VERT][0] + 1
53        or both. */
54     int d[TABLE_N_AXES][2];
55
56     unsigned int options;       /* TAB_*. */
57     char *text;                 /* A paragraph of text. */
58
59     /* Optional footnote(s). */
60     const struct footnote **footnotes;
61     size_t n_footnotes;
62
63     const struct area_style *style;
64   };
65
66 void table_cell_free (struct table_cell *);
67
68 void table_cell_format_footnote_markers (const struct table_cell *,
69                                          struct string *);
70
71 /* Returns the number of columns that CELL spans.  This is 1 for an ordinary
72    cell and greater than one for a cell that joins multiple columns. */
73 static inline int
74 table_cell_colspan (const struct table_cell *cell)
75 {
76   return cell->d[TABLE_HORZ][1] - cell->d[TABLE_HORZ][0];
77 }
78
79 /* Returns the number of rows that CELL spans.  This is 1 for an ordinary cell
80    and greater than one for a cell that joins multiple rows. */
81 static inline int
82 table_cell_rowspan (const struct table_cell *cell)
83 {
84   return cell->d[TABLE_VERT][1] - cell->d[TABLE_VERT][0];
85 }
86
87 /* Returns true if CELL is a joined cell, that is, if it spans multiple rows
88    or columns.  Otherwise, returns false. */
89 static inline bool
90 table_cell_is_joined (const struct table_cell *cell)
91 {
92   return table_cell_colspan (cell) > 1 || table_cell_rowspan (cell) > 1;
93 }
94 \f
95 /* Declarations to allow defining table classes. */
96
97 struct table_class
98   {
99     /* Frees TABLE. */
100     void (*destroy) (struct table *table);
101
102     /* Initializes CELL with the contents of the table cell at column X and row
103        Y within TABLE.  All members of CELL must be initialized.  The
104        'contents' member of CELL must be set to a nonnull value.
105
106        The table class must allow any number of cells in the table to be
107        retrieved simultaneously; that is, TABLE must not assume that a given
108        cell will be freed before another one is retrieved using 'get_cell'.
109
110        The table class must allow joined cells to be retrieved, with identical
111        contents, using any (X,Y) location inside the cell.
112
113        The table class must not allow cells to overlap.
114
115        The table class should not allow a joined cell to cross the border
116        between header rows/columns and the interior of the table.  That is, a
117        joined cell should be entirely within headers rows and columns or
118        entirely outside them.
119
120        The table class may assume that CELL will be freed before TABLE is
121        destroyed. */
122     void (*get_cell) (const struct table *table, int x, int y,
123                       struct table_cell *cell);
124
125     /* Returns one of the TAL_* enumeration constants (declared in
126        output/table.h) representing a rule running alongside one of the cells
127        in TABLE.
128
129        See table_get_rule() in table.c for a detailed explanation of the
130        meaning of AXIS and X and Y, including a diagram. */
131     int (*get_rule) (const struct table *table,
132                      enum table_axis axis, int x, int y,
133                      struct cell_color *color);
134   };
135
136 void table_init (struct table *, const struct table_class *);
137
138 /* Table class implementations can call these functions or just set the
139    table's n[] and h[][] members directly. */
140 void table_set_nc (struct table *, int nc);
141 void table_set_nr (struct table *, int nr);
142 \f
143 /* For use primarily by output drivers. */
144
145 void table_get_cell (const struct table *, int x, int y, struct table_cell *);
146 int table_get_rule (const struct table *, enum table_axis, int x, int y,
147                     struct cell_color *);
148 size_t table_collect_footnotes (const struct table_item *,
149                                 const struct footnote ***);
150
151 #endif /* output/table-provider.h */