Clean up output subsystem.
[pspp-builds.git] / src / output / table.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #if !tab_h
21 #define tab_h 1
22
23 #include <limits.h>
24 #include <libpspp/str.h>
25
26 /* Cell options. */
27 enum
28   {
29     TAB_NONE = 0,
30
31     TAB_ALIGN_MASK = 03,        /* Alignment mask. */
32     TAB_RIGHT = 00,             /* Right justify. */
33     TAB_LEFT = 01,              /* Left justify. */
34     TAB_CENTER = 02,            /* Center. */
35
36     /* Cell types. */
37     TAB_JOIN = 004,             /* Joined cell. */
38     TAB_EMPTY = 010,            /* Empty cell. */
39
40     /* Flags. */
41     TAB_EMPH = 020,             /* Emphasize cell contents. */
42     TAB_FIX = 040,              /* Use fixed font. */
43   };
44
45 /* Line styles. */
46 enum
47   {
48     TAL_0 = 0,                  /* No line. */
49     TAL_1 = 1,                  /* Single line. */
50     TAL_2 = 2,                  /* Double line. */
51     TAL_GAP = 3,                /* Spacing but no line. */
52     TAL_COUNT,                  /* Number of line styles. */
53   };
54
55 /* Column styles.  Must correspond to SOM_COL_*. */
56 enum
57   {
58     TAB_COL_NONE,                       /* No columns. */
59     TAB_COL_DOWN                        /* Columns down first. */
60   };
61
62 /* Joined cell. */
63 struct tab_joined_cell
64   {
65     int x1, y1;
66     int x2, y2;
67     int hit;
68     struct fixed_string contents;
69   };
70
71 struct outp_driver;
72 struct tab_table;
73 typedef void tab_dim_func (struct tab_table *, struct outp_driver *);
74
75 /* A table. */
76 struct tab_table
77   {
78     struct pool *container;
79     
80     /* Contents. */
81     int col_style;              /* Columns: One of TAB_COL_*. */
82     int col_group;              /* Number of rows per column group. */
83     struct fixed_string title;  /* Table title. */
84     unsigned flags;             /* SOMF_*. */
85     int nc, nr;                 /* Number of columns, rows. */
86     int cf;                     /* Column factor for indexing purposes. */
87     int l, r, t, b;             /* Number of header rows on each side. */
88     struct fixed_string *cc;    /* Cell contents; fixed_string *[nr][nc]. */
89     unsigned char *ct;          /* Cell types; unsigned char[nr][nc]. */
90     unsigned char *rh;          /* Horiz rules; unsigned char[nr+1][nc]. */
91     unsigned char *rv;          /* Vert rules; unsigned char[nr][nc+1]. */
92     tab_dim_func *dim;          /* Calculates cell widths and heights. */
93
94     /* Calculated during output. */
95     int *w;                     /* Column widths; [nc]. */
96     int *h;                     /* Row heights; [nr]. */
97     int *hrh;                   /* Heights of horizontal rules; [nr+1]. */
98     int *wrv;                   /* Widths of vertical rules; [nc+1]. */
99     int wl, wr, ht, hb;         /* Width/height of header rows/columns. */
100
101     /* Editing info. */
102     int col_ofs, row_ofs;       /* X and Y offsets. */
103   };
104
105 extern int tab_hit;
106
107 /* Number of rows in TABLE. */
108 #define tab_nr(TABLE) ((TABLE)->nr)
109
110 /* Number of columns in TABLE. */
111 #define tab_nc(TABLE) ((TABLE)->nc)
112
113 /* Number of left header columns in TABLE. */
114 #define tab_l(TABLE) ((TABLE)->l)
115
116 /* Number of right header columns in TABLE. */
117 #define tab_r(TABLE) ((TABLE)->r)
118
119 /* Number of top header rows in TABLE. */
120 #define tab_t(TABLE) ((TABLE)->t)
121
122 /* Number of bottom header rows in TABLE. */
123 #define tab_b(TABLE) ((TABLE)->b)
124
125 /* Tables. */
126 struct tab_table *tab_create (int nc, int nr, int reallocable);
127 void tab_destroy (struct tab_table *);
128 void tab_resize (struct tab_table *, int nc, int nr);
129 void tab_realloc (struct tab_table *, int nc, int nr);
130 void tab_headers (struct tab_table *, int l, int r, int t, int b);
131 void tab_columns (struct tab_table *, int style, int group);
132 void tab_title (struct tab_table *, const char *, ...)
133      PRINTF_FORMAT (2, 3);
134 void tab_flags (struct tab_table *, unsigned);
135 void tab_submit (struct tab_table *);
136
137 /* Dimensioning. */
138 tab_dim_func tab_natural_dimensions;
139 int tab_natural_width (struct tab_table *t, struct outp_driver *d, int c);
140 int tab_natural_height (struct tab_table *t, struct outp_driver *d, int r);
141 void tab_dim (struct tab_table *, tab_dim_func *);
142
143 /* Rules. */
144 void tab_hline (struct tab_table *, int style, int x1, int x2, int y);
145 void tab_vline (struct tab_table *, int style, int x, int y1, int y2);
146 void tab_box (struct tab_table *, int f_h, int f_v, int i_h, int i_v,
147               int x1, int y1, int x2, int y2);
148
149 /* Text options, passed in the `opt' argument. */
150 enum
151   {
152     TAT_NONE = 0,               /* No options. */
153     TAT_PRINTF = 0x0100,        /* Format the text string with sprintf. */
154     TAT_TITLE = 0x0200 | TAB_EMPH, /* Title attributes. */
155     TAT_NOWRAP = 0x0800         /* No text wrap (tab_output_text() only). */
156   };
157
158 /* Cells. */
159 struct fmt_spec;
160 union value;
161 void tab_value (struct tab_table *, int c, int r, unsigned char opt,
162                 const union value *, const struct fmt_spec *);
163 void tab_float (struct tab_table *, int c, int r, unsigned char opt,
164                 double v, int w, int d);
165 void tab_text (struct tab_table *, int c, int r, unsigned opt,
166                const char *, ...)
167      PRINTF_FORMAT (5, 6);
168 void tab_joint_text (struct tab_table *, int x1, int y1, int x2, int y2,
169                      unsigned opt, const char *, ...)
170      PRINTF_FORMAT (7, 8);
171
172 /* Cell low-level access. */
173 #define tab_alloc(TABLE, AMT) pool_alloc ((TABLE)->container, (AMT))
174 void tab_raw (struct tab_table *, int c, int r, unsigned opt,
175               struct fixed_string *);
176
177 /* Editing. */
178 void tab_offset (struct tab_table *, int col, int row);
179 void tab_next_row (struct tab_table *);
180
181 /* Current row/column offset. */
182 #define tab_row(TABLE) ((TABLE)->row_ofs)
183 #define tab_col(TABLE) ((TABLE)->col_ofs)
184
185 /* Simple output. */
186 void tab_output_text (int options, const char *string, ...)
187      PRINTF_FORMAT (2, 3);
188
189 /* Embedding the command name in the output. */
190 void tab_set_command_name (const char *);
191
192 #endif /* tab_h */
193