35216d4e518502d809fd8e02965d82945fd53f86
[pspp-builds.git] / 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 enum
46   {
47     COL_TITLE,                  /* Table title. */
48     COL_Y,                      /* Y position of top of title. */
49     N_COLS
50   };
51
52 static void psppire_output_window_base_finalize (PsppireOutputWindowClass *, gpointer);
53 static void psppire_output_window_base_init     (PsppireOutputWindowClass *class);
54 static void psppire_output_window_class_init    (PsppireOutputWindowClass *class);
55 static void psppire_output_window_init          (PsppireOutputWindow      *window);
56
57
58 GType
59 psppire_output_window_get_type (void)
60 {
61   static GType psppire_output_window_type = 0;
62
63   if (!psppire_output_window_type)
64     {
65       static const GTypeInfo psppire_output_window_info =
66       {
67         sizeof (PsppireOutputWindowClass),
68         (GBaseInitFunc) psppire_output_window_base_init,
69         (GBaseFinalizeFunc) psppire_output_window_base_finalize,
70         (GClassInitFunc)psppire_output_window_class_init,
71         (GClassFinalizeFunc) NULL,
72         NULL,
73         sizeof (PsppireOutputWindow),
74         0,
75         (GInstanceInitFunc) psppire_output_window_init,
76       };
77
78       psppire_output_window_type =
79         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireOutputWindow",
80                                 &psppire_output_window_info, 0);
81     }
82
83   return psppire_output_window_type;
84 }
85
86 static GObjectClass *parent_class;
87
88 static void
89 psppire_output_window_finalize (GObject *object)
90 {
91   if (G_OBJECT_CLASS (parent_class)->finalize)
92     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
93 }
94
95
96 static void
97 psppire_output_window_class_init (PsppireOutputWindowClass *class)
98 {
99   parent_class = g_type_class_peek_parent (class);
100 }
101
102
103 static void
104 psppire_output_window_base_init (PsppireOutputWindowClass *class)
105 {
106   GObjectClass *object_class = G_OBJECT_CLASS (class);
107
108   object_class->finalize = psppire_output_window_finalize;
109 }
110
111
112
113 static void
114 psppire_output_window_base_finalize (PsppireOutputWindowClass *class,
115                                      gpointer class_data)
116 {
117 }
118 \f
119 /* Output driver class. */
120
121 static PsppireOutputWindow *the_output_viewer = NULL;
122
123 static gboolean
124 expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
125 {
126   struct som_entity *entity = g_object_get_data (G_OBJECT (widget), "entity");
127   GdkWindow *window = widget->window;
128   cairo_t *cairo = gdk_cairo_create (GDK_DRAWABLE (window));
129   struct outp_driver *driver = xr_create_driver (cairo); /* XXX can fail */
130   struct tab_table *t = entity->ext;
131   void *rendering;
132
133   rendering = entity->class->render_init (entity, driver, tab_l (t),
134                                           tab_r (t), tab_t (t), tab_b (t));
135
136   entity->class->title (rendering, 0, 0,
137                         entity->table_num, entity->subtable_num,
138                         entity->command_name);
139   entity->class->render (rendering, tab_l (t), tab_t (t),
140                          tab_nc (t) - tab_r (t),
141                          tab_nr (t) - tab_b (t));
142
143   entity->class->render_free (rendering);
144   driver->class->close_driver (driver);
145   outp_free_driver (driver);
146   return TRUE;
147 }
148
149 static void
150 psppire_output_submit (struct outp_driver *this, struct som_entity *entity)
151 {
152   if (the_output_viewer == NULL)
153     {
154       the_output_viewer = PSPPIRE_OUTPUT_WINDOW (psppire_output_window_new ());
155       gtk_widget_show_all (GTK_WIDGET (the_output_viewer));
156     }
157
158   if (entity->type == SOM_TABLE)
159     {
160       GdkWindow *window = GTK_WIDGET (the_output_viewer)->window;
161       cairo_t *cairo = gdk_cairo_create (GDK_DRAWABLE (window));
162       struct outp_driver *driver = xr_create_driver (cairo); /* XXX can fail */
163       struct tab_table *t = entity->ext;
164       GtkTreeStore *store;
165       GtkTreeIter item;
166       GtkTreePath *path;
167       GtkWidget *drawing_area;
168       void *rendering;
169       struct string title;
170       int tw, th;
171
172       tab_ref (t);
173       rendering = entity->class->render_init (entity, driver, tab_l (t),
174                                               tab_r (t), tab_t (t), tab_b (t));
175       entity->class->area (rendering, &tw, &th);
176
177       drawing_area = gtk_drawing_area_new ();
178       gtk_widget_modify_bg (GTK_WIDGET (drawing_area), GTK_STATE_NORMAL,
179                             &gtk_widget_get_style (drawing_area)->base[GTK_STATE_NORMAL]);
180       g_object_set_data (G_OBJECT (drawing_area),
181                          "entity", som_entity_clone (entity));
182       gtk_widget_set_size_request (drawing_area, tw / 1024, th / 1024);
183       gtk_layout_put (the_output_viewer->output, drawing_area,
184                       0, the_output_viewer->y);
185       gtk_widget_show (drawing_area);
186       g_signal_connect (G_OBJECT (drawing_area), "expose_event",
187                         G_CALLBACK (expose_event_callback), NULL);
188
189       entity->class->render_free (rendering);
190       driver->class->close_driver (driver);
191       outp_free_driver (driver);
192
193       store = GTK_TREE_STORE (gtk_tree_view_get_model (
194                                 the_output_viewer->overview));
195
196       ds_init_empty (&title);
197       if (entity->table_num != the_output_viewer->last_table_num)
198         {
199           gtk_tree_store_append (store, &item, NULL);
200
201           ds_put_format (&title, "%d %s",
202                          entity->table_num, entity->command_name);
203           gtk_tree_store_set (store, &item,
204                               COL_TITLE, ds_cstr (&title),
205                               COL_Y, the_output_viewer->y,
206                               -1);
207
208           /* XXX shouldn't save a GtkTreeIter */
209           the_output_viewer->last_table_num = entity->table_num;
210           the_output_viewer->last_top_level = item;
211         }
212
213       gtk_tree_store_append (store, &item,
214                              &the_output_viewer->last_top_level);
215       ds_clear (&title);
216       ds_put_format (&title, "%d.%d %s",
217                      entity->table_num, entity->subtable_num,
218                      t->title ? t->title : entity->command_name);
219       gtk_tree_store_set (store, &item,
220                           COL_TITLE, ds_cstr (&title),
221                           COL_Y, the_output_viewer->y,
222                           -1);
223       ds_destroy (&title);
224
225       path = gtk_tree_model_get_path (GTK_TREE_MODEL (store),
226                                       &the_output_viewer->last_top_level);
227       gtk_tree_view_expand_row (the_output_viewer->overview, path, TRUE);
228       gtk_tree_path_free (path);
229
230       if (tw / 1024 > the_output_viewer->max_width)
231         the_output_viewer->max_width = tw / 1024;
232       the_output_viewer->y += th / 1024;
233
234       gtk_layout_set_size (the_output_viewer->output,
235                            the_output_viewer->max_width, the_output_viewer->y);
236     }
237
238   gtk_window_set_urgency_hint (GTK_WINDOW (the_output_viewer), TRUE);
239 }
240
241 static struct outp_class psppire_output_class =
242   {
243     "PSPPIRE",                  /* name */
244     true,                       /* special */
245     NULL,                       /* open_driver */
246     NULL,                       /* close_driver */
247     NULL,                       /* open_page */
248     NULL,                       /* close_page */
249     NULL,                       /* flush */
250     NULL,                       /* output_chart */
251     psppire_output_submit,      /* submit */
252     NULL,                       /* line */
253     NULL,                       /* text_metrics */
254     NULL,                       /* text_draw */
255   };
256
257 void
258 psppire_output_window_setup (void)
259 {
260   outp_register_driver (outp_allocate_driver (&psppire_output_class,
261                                               "PSPPIRE", 0));
262 }
263 \f
264 int viewer_length = 16;
265 int viewer_width = 59;
266
267 /* Callback for the "delete" action (clicking the x on the top right
268    hand corner of the window) */
269 static gboolean
270 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
271 {
272   PsppireOutputWindow *ow = PSPPIRE_OUTPUT_WINDOW (user_data);
273
274   gtk_widget_destroy (GTK_WIDGET (ow));
275
276   the_output_viewer = NULL;
277
278   return FALSE;
279 }
280
281
282
283 static void
284 cancel_urgency (GtkWindow *window,  gpointer data)
285 {
286   gtk_window_set_urgency_hint (window, FALSE);
287 }
288
289 static void
290 on_row_activate (GtkTreeView *overview,
291                  GtkTreePath *path,
292                  GtkTreeViewColumn *column,
293                  PsppireOutputWindow *window)
294 {
295   GtkTreeModel *model;
296   GtkTreeIter iter;
297   GtkAdjustment *vadj;
298   GValue value = {0};
299   double y, min, max;
300
301   model = gtk_tree_view_get_model (overview);
302   if (!gtk_tree_model_get_iter (model, &iter, path))
303     return;
304
305   gtk_tree_model_get_value (model, &iter, COL_Y, &value);
306   y = g_value_get_long (&value);
307   g_value_unset (&value);
308
309   vadj = gtk_layout_get_vadjustment (window->output);
310   min = vadj->lower;
311   max = vadj->upper - vadj->page_size;
312   if (y < min)
313     y = min;
314   else if (y > max)
315     y = max;
316   gtk_adjustment_set_value (vadj, y);
317 }
318
319 static void
320 psppire_output_window_init (PsppireOutputWindow *window)
321 {
322   GtkTreeViewColumn *column;
323   GtkCellRenderer *renderer;
324   GtkBuilder *xml;
325
326   xml = builder_new ("output-viewer.ui");
327
328   gtk_widget_reparent (get_widget_assert (xml, "vbox1"), GTK_WIDGET (window));
329
330   window->output = GTK_LAYOUT (get_widget_assert (xml, "output"));
331   window->y = 0;
332
333   window->overview = GTK_TREE_VIEW (get_widget_assert (xml, "overview"));
334   gtk_tree_view_set_model (window->overview,
335                            GTK_TREE_MODEL (gtk_tree_store_new (
336                                              N_COLS,
337                                              G_TYPE_STRING, /* COL_TITLE */
338                                              G_TYPE_LONG))); /* COL_Y */
339   window->last_table_num = -1;
340
341   column = gtk_tree_view_column_new ();
342   gtk_tree_view_append_column (GTK_TREE_VIEW (window->overview), column);
343   renderer = gtk_cell_renderer_text_new ();
344   gtk_tree_view_column_pack_start (column, renderer, TRUE);
345   gtk_tree_view_column_add_attribute (column, renderer, "text", COL_TITLE);
346
347   g_signal_connect (GTK_TREE_VIEW (window->overview),
348                     "row-activated", G_CALLBACK (on_row_activate), window);
349
350   gtk_widget_modify_bg (GTK_WIDGET (window->output), GTK_STATE_NORMAL,
351                         &gtk_widget_get_style (GTK_WIDGET (window->output))->base[GTK_STATE_NORMAL]);
352
353   connect_help (xml);
354
355   g_signal_connect (window,
356                     "focus-in-event",
357                     G_CALLBACK (cancel_urgency),
358                     NULL);
359
360   g_signal_connect (get_action_assert (xml,"help_about"),
361                     "activate",
362                     G_CALLBACK (about_new),
363                     window);
364
365   g_signal_connect (get_action_assert (xml,"help_reference"),
366                     "activate",
367                     G_CALLBACK (reference_manual),
368                     NULL);
369
370   g_signal_connect (get_action_assert (xml,"windows_minimise-all"),
371                     "activate",
372                     G_CALLBACK (psppire_window_minimise_all),
373                     NULL);
374
375   {
376     GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
377
378     PSPPIRE_WINDOW (window)->menu =
379       GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar1/windows_menuitem/windows_minimise-all")->parent);
380   }
381
382   g_object_unref (xml);
383
384   g_signal_connect (window, "delete-event",
385                     G_CALLBACK (on_delete), window);
386 }
387
388
389 GtkWidget*
390 psppire_output_window_new (void)
391 {
392   return GTK_WIDGET (g_object_new (psppire_output_window_get_type (),
393                                    "filename", "Output",
394                                    "description", _("Output Viewer"),
395                                    NULL));
396 }