7b8a23c8840420eb5a2fa42d767c11825e0c353f
[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/pivot-table.h"
28 #include "output/table-item.h"
29
30 #include "gl/xalloc.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 /* Initializes ITEM as a table item for rendering PT.  Takes ownership of
36    PT. */
37 struct table_item *
38 table_item_create (struct pivot_table *pt)
39 {
40   pivot_table_assign_label_depth (pt);
41
42   struct table_item *item = xmalloc (sizeof *item);
43   *item = (struct table_item) {
44     .output_item = OUTPUT_ITEM_INITIALIZER (&table_item_class),
45     .pt = pt,
46   };
47   return item;
48 }
49
50 /* Submits TABLE_ITEM to the configured output drivers, and transfers ownership
51    to the output subsystem. */
52 void
53 table_item_submit (struct table_item *table_item)
54 {
55   output_submit (&table_item->output_item);
56 }
57 \f
58 static const char *
59 table_item_get_label (const struct output_item *output_item)
60 {
61   struct table_item *item = to_table_item (output_item);
62
63   if (!item->cached_label)
64     {
65       if (!item->pt->title)
66         return _("Table");
67
68       item->cached_label = pivot_value_to_string (item->pt->title,
69                                                   SETTINGS_VALUE_SHOW_DEFAULT,
70                                                   SETTINGS_VALUE_SHOW_DEFAULT);
71     }
72   return item->cached_label;
73 }
74
75 static void
76 table_item_destroy (struct output_item *output_item)
77 {
78   struct table_item *item = to_table_item (output_item);
79   pivot_table_unref (item->pt);
80   free (item->cached_label);
81   free (item);
82 }
83
84 const struct output_item_class table_item_class =
85   {
86     table_item_get_label,
87     table_item_destroy,
88   };