output: Make GUI output window scrollable.
[pspp] / src / ui / gui / psppire-output-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009  Free Software Foundation
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 <gtk/gtksignal.h>
20 #include <gtk/gtkbox.h>
21 #include "helper.h"
22
23 #include <libpspp/message.h>
24 #include <output/cairo.h>
25 #include <output/manager.h>
26 #include <output/output.h>
27 #include <output/table.h>
28 #include <stdlib.h>
29
30 #include "about.h"
31
32 #include "psppire-output-window.h"
33
34
35 #include "xalloc.h"
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40
41 #include <gettext.h>
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
44
45
46
47 static void psppire_output_window_base_finalize (PsppireOutputWindowClass *, gpointer);
48 static void psppire_output_window_base_init     (PsppireOutputWindowClass *class);
49 static void psppire_output_window_class_init    (PsppireOutputWindowClass *class);
50 static void psppire_output_window_init          (PsppireOutputWindow      *window);
51
52
53 GType
54 psppire_output_window_get_type (void)
55 {
56   static GType psppire_output_window_type = 0;
57
58   if (!psppire_output_window_type)
59     {
60       static const GTypeInfo psppire_output_window_info =
61       {
62         sizeof (PsppireOutputWindowClass),
63         (GBaseInitFunc) psppire_output_window_base_init,
64         (GBaseFinalizeFunc) psppire_output_window_base_finalize,
65         (GClassInitFunc)psppire_output_window_class_init,
66         (GClassFinalizeFunc) NULL,
67         NULL,
68         sizeof (PsppireOutputWindow),
69         0,
70         (GInstanceInitFunc) psppire_output_window_init,
71       };
72
73       psppire_output_window_type =
74         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireOutputWindow",
75                                 &psppire_output_window_info, 0);
76     }
77
78   return psppire_output_window_type;
79 }
80
81 static GObjectClass *parent_class;
82
83 static void
84 psppire_output_window_finalize (GObject *object)
85 {
86   if (G_OBJECT_CLASS (parent_class)->finalize)
87     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
88 }
89
90
91 static void
92 psppire_output_window_class_init (PsppireOutputWindowClass *class)
93 {
94   parent_class = g_type_class_peek_parent (class);
95 }
96
97
98 static void
99 psppire_output_window_base_init (PsppireOutputWindowClass *class)
100 {
101   GObjectClass *object_class = G_OBJECT_CLASS (class);
102
103   object_class->finalize = psppire_output_window_finalize;
104 }
105
106
107
108 static void
109 psppire_output_window_base_finalize (PsppireOutputWindowClass *class,
110                                      gpointer class_data)
111 {
112 }
113 \f
114 /* Output driver class. */
115
116 static PsppireOutputWindow *the_output_viewer = NULL;
117
118 static gboolean
119 expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
120 {
121   struct som_entity *entity = g_object_get_data (G_OBJECT (widget), "entity");
122   GdkWindow *window = widget->window;
123   cairo_t *cairo = gdk_cairo_create (GDK_DRAWABLE (window));
124   struct outp_driver *driver = xr_create_driver (cairo); /* XXX can fail */
125   struct tab_table *t = entity->ext;
126   void *rendering;
127
128   rendering = entity->class->render_init (entity, driver, tab_l (t),
129                                           tab_r (t), tab_t (t), tab_b (t));
130
131   entity->class->title (rendering, 0, 0,
132                         entity->table_num, entity->subtable_num);
133   entity->class->render (rendering, tab_l (t), tab_t (t),
134                          tab_nc (t) - tab_r (t),
135                          tab_nr (t) - tab_b (t));
136
137   entity->class->render_free (rendering);
138   driver->class->close_driver (driver);
139   outp_free_driver (driver);
140   return TRUE;
141 }
142
143 static void
144 psppire_output_submit (struct outp_driver *this, struct som_entity *entity)
145 {
146   if (the_output_viewer == NULL)
147     {
148       the_output_viewer = PSPPIRE_OUTPUT_WINDOW (psppire_output_window_new ());
149       gtk_widget_show_now (GTK_WIDGET (the_output_viewer));
150     }
151
152   if (entity->type == SOM_TABLE)
153     {
154       GdkWindow *window = GTK_WIDGET (the_output_viewer)->window;
155       cairo_t *cairo = gdk_cairo_create (GDK_DRAWABLE (window));
156       struct outp_driver *driver = xr_create_driver (cairo); /* XXX can fail */
157       struct tab_table *t = entity->ext;
158       GtkWidget *drawing_area;
159       void *rendering;
160       int tw, th;
161
162       tab_ref (t);
163       rendering = entity->class->render_init (entity, driver, tab_l (t),
164                                               tab_r (t), tab_t (t), tab_b (t));
165       entity->class->area (rendering, &tw, &th);
166
167       drawing_area = gtk_drawing_area_new ();
168       g_object_set_data (G_OBJECT (drawing_area),
169                          "entity", xmemdup (entity, sizeof *entity));
170       gtk_widget_set_size_request (drawing_area, tw / 1024, th / 1024);
171       gtk_layout_put (the_output_viewer->output, drawing_area,
172                       0, the_output_viewer->y);
173       gtk_widget_show (drawing_area);
174       g_signal_connect (G_OBJECT (drawing_area), "expose_event",
175                         G_CALLBACK (expose_event_callback), NULL);
176
177       entity->class->render_free (rendering);
178       driver->class->close_driver (driver);
179       outp_free_driver (driver);
180
181       if (tw / 1024 > the_output_viewer->max_width)
182         the_output_viewer->max_width = tw / 1024;
183       the_output_viewer->y += th / 1024;
184
185       gtk_layout_set_size (the_output_viewer->output,
186                            the_output_viewer->max_width, the_output_viewer->y);
187     }
188
189   gtk_window_set_urgency_hint (GTK_WINDOW (the_output_viewer), TRUE);
190 }
191
192 static struct outp_class psppire_output_class =
193   {
194     "PSPPIRE",                  /* name */
195     true,                       /* special */
196     NULL,                       /* open_driver */
197     NULL,                       /* close_driver */
198     NULL,                       /* open_page */
199     NULL,                       /* close_page */
200     NULL,                       /* flush */
201     psppire_output_submit,      /* submit */
202     NULL,                       /* line */
203     NULL,                       /* text_metrics */
204     NULL,                       /* text_draw */
205     NULL,                       /* initialise_chart */
206     NULL,                       /* finalise_chart */
207   };
208
209 void
210 psppire_output_window_setup (void)
211 {
212   outp_register_driver (outp_allocate_driver (&psppire_output_class,
213                                               "PSPPIRE", 0));
214 }
215 \f
216 int viewer_length = 16;
217 int viewer_width = 59;
218
219 /* Callback for the "delete" action (clicking the x on the top right
220    hand corner of the window) */
221 static gboolean
222 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
223 {
224   PsppireOutputWindow *ow = PSPPIRE_OUTPUT_WINDOW (user_data);
225
226   gtk_widget_destroy (GTK_WIDGET (ow));
227
228   the_output_viewer = NULL;
229
230   return FALSE;
231 }
232
233
234
235 static void
236 cancel_urgency (GtkWindow *window,  gpointer data)
237 {
238   gtk_window_set_urgency_hint (window, FALSE);
239 }
240
241
242 static void
243 psppire_output_window_init (PsppireOutputWindow *window)
244 {
245   GtkBuilder *xml = builder_new ("output-viewer.ui");
246
247   gtk_widget_reparent (get_widget_assert (xml, "vbox1"), GTK_WIDGET (window));
248
249   window->output = GTK_LAYOUT (get_widget_assert (xml, "output"));
250   window->y = 0;
251
252   connect_help (xml);
253
254   g_signal_connect (window,
255                     "focus-in-event",
256                     G_CALLBACK (cancel_urgency),
257                     NULL);
258
259   g_signal_connect (get_action_assert (xml,"help_about"),
260                     "activate",
261                     G_CALLBACK (about_new),
262                     window);
263
264   g_signal_connect (get_action_assert (xml,"help_reference"),
265                     "activate",
266                     G_CALLBACK (reference_manual),
267                     NULL);
268
269   g_signal_connect (get_action_assert (xml,"windows_minimise-all"),
270                     "activate",
271                     G_CALLBACK (psppire_window_minimise_all),
272                     NULL);
273
274   {
275     GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
276
277     PSPPIRE_WINDOW (window)->menu =
278       GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar1/windows_menuitem/windows_minimise-all")->parent);
279   }
280
281   g_object_unref (xml);
282
283   g_signal_connect (window, "delete-event",
284                     G_CALLBACK (on_delete), window);
285 }
286
287
288 GtkWidget*
289 psppire_output_window_new (void)
290 {
291   return GTK_WIDGET (g_object_new (psppire_output_window_get_type (),
292                                    "filename", "Output",
293                                    "description", _("Output Viewer"),
294                                    NULL));
295 }