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