output: Move text_item and group_item usage closer to the SPV model.
[pspp] / src / output / text-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 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 #include <config.h>
18
19 #include "output/text-item.h"
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24
25 #include "libpspp/cast.h"
26 #include "libpspp/pool.h"
27 #include "output/driver.h"
28 #include "output/output-item-provider.h"
29 #include "output/tab.h"
30 #include "output/table-item.h"
31 #include "output/table-provider.h"
32
33 #include "gl/xalloc.h"
34 #include "gl/xvasprintf.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 const char *
40 text_item_type_to_string (enum text_item_type type)
41 {
42   switch (type)
43     {
44     case TEXT_ITEM_PAGE_TITLE:
45       return _("Page Title");
46
47     case TEXT_ITEM_TITLE:
48       return _("Title");
49
50     case TEXT_ITEM_SYNTAX:
51     case TEXT_ITEM_LOG:
52       return _("Log");
53
54     case TEXT_ITEM_EJECT_PAGE:
55       return _("Page Break");
56
57     default:
58       return _("Text");
59     }
60 }
61
62 /* Creates and returns a new text item containing TEXT and the specified TYPE.
63    The new text item takes ownership of TEXT. */
64 struct text_item *
65 text_item_create_nocopy (enum text_item_type type, char *text)
66 {
67   struct text_item *item = xzalloc (sizeof *item);
68   output_item_init (&item->output_item, &text_item_class);
69   item->text = text;
70   item->type = type;
71   return item;
72 }
73
74 /* Creates and returns a new text item containing a copy of TEXT and the
75    specified TYPE.  The caller retains ownership of TEXT. */
76 struct text_item *
77 text_item_create (enum text_item_type type, const char *text)
78 {
79   return text_item_create_nocopy (type, xstrdup (text));
80 }
81
82 /* Creates and returns a new text item containing a copy of FORMAT, which is
83    formatted as if by printf(), and the specified TYPE.  The caller retains
84    ownership of FORMAT. */
85 struct text_item *
86 text_item_create_format (enum text_item_type type, const char *format, ...)
87 {
88   struct text_item *item;
89   va_list args;
90
91   va_start (args, format);
92   item = text_item_create_nocopy (type, xvasprintf (format, args));
93   va_end (args);
94
95   return item;
96 }
97
98 /* Returns ITEM's type. */
99 enum text_item_type
100 text_item_get_type (const struct text_item *item)
101 {
102   return item->type;
103 }
104
105 /* Returns ITEM's text, which the caller may not modify or free. */
106 const char *
107 text_item_get_text (const struct text_item *item)
108 {
109   return item->text;
110 }
111
112 /* Submits ITEM to the configured output drivers, and transfers ownership to
113    the output subsystem. */
114 void
115 text_item_submit (struct text_item *item)
116 {
117   output_submit (&item->output_item);
118 }
119
120 struct table_item *
121 text_item_to_table_item (struct text_item *text_item)
122 {
123   struct pivot_value *text = pivot_value_new_user_text (
124     text_item_get_text (text_item), -1);
125
126   struct font_style *font_style = xmalloc (sizeof *font_style);
127   if (text_item->typeface)
128     font_style->typeface = xstrdup (tab->container, text_item->typeface);
129   else if (text_item->type == TEXT_ITEM_SYNTAX
130            || text_item->type == TEXT_ITEM_LOG)
131     font_style->typeface = xstrdup ("Monospace");
132   font_style->size = text_item->size;
133   font_style->bold = text_item->bold;
134   font_style->italic = text_item->italic;
135   font_style->underline = text_item->underline;
136   font_style->markup = text_item->markup;
137   text->font_style = font_style;
138
139   struct table_item *table_item = table_item_create (
140     pivot_table_create_for_text (NULL, text));
141   text_item_unref (text_item);
142   return table_item;
143 }
144 \f
145 static void
146 text_item_destroy (struct output_item *output_item)
147 {
148   struct text_item *item = to_text_item (output_item);
149   free (item->text);
150   free (item->typeface);
151   free (item);
152 }
153
154 const struct output_item_class text_item_class =
155   {
156     "text",
157     text_item_destroy,
158   };