John's original code for patch #6210.
[pspp-builds.git] / src / ui / gui / output-viewer.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007 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 #include <gtk/gtk.h>
19 #include "window-manager.h"
20 #include "output-viewer.h"
21 #include "helper.h"
22 #include "about.h"
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include <glade/glade.h>
29 #include <ctype.h>
30
31 struct output_viewer
32 {
33   struct editor_window parent;
34   GtkTextBuffer *buffer;  /* The buffer which contains the text */
35   GtkWidget *textview ;
36   FILE *fp;               /* The file it's viewing */
37 };
38
39
40 static void
41 cancel_urgency (GtkWindow *window,  gpointer data)
42 {
43   gtk_window_set_urgency_hint (window, FALSE);
44 }
45
46
47 static struct output_viewer *the_output_viewer = NULL;
48
49 int viewer_length = -1;
50 int viewer_width = -1;
51
52 /* Callback for the "delete" action (clicking the x on the top right
53    hand corner of the window) */
54 static gboolean
55 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
56 {
57   struct output_viewer *ov = user_data;
58
59   g_free (ov);
60
61   the_output_viewer = NULL;
62
63   unlink (OUTPUT_FILE_NAME);
64
65   return FALSE;
66 }
67
68
69 /* Sets width and length according to the new size
70    of the output window */
71 static void
72 on_textview_resize (GtkWidget     *widget,
73                     GtkAllocation *allocation,
74                     gpointer       user_data)
75 {
76   PangoContext * context ;
77   PangoLayout *layout ;
78   PangoRectangle logical;
79   GtkStyle *style;
80   gint right_margin, left_margin;
81   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
82
83   context = gtk_widget_create_pango_context (widget);
84   layout = pango_layout_new (context);
85
86   style = gtk_widget_get_style (widget);
87
88   pango_layout_set_font_description (layout, style->font_desc);
89
90   /* Find the width of one character.  We can use any character, because
91      the textview has a monospaced font */
92   pango_layout_set_text (layout, "M", 1);
93
94   pango_layout_get_extents (layout,  NULL, &logical);
95
96   left_margin = gtk_text_view_get_left_margin (text_view);
97   right_margin = gtk_text_view_get_right_margin (text_view);
98
99   viewer_length = allocation->height / PANGO_PIXELS (logical.height);
100   viewer_width = (allocation->width - right_margin - left_margin)
101     / PANGO_PIXELS (logical.width);
102
103   g_object_unref (G_OBJECT (layout));
104   g_object_unref (G_OBJECT (context));
105 }
106
107
108
109 /*
110   Create a new output viewer
111 */
112 struct output_viewer *
113 new_output_viewer (void)
114 {
115   GladeXML *xml = XML_NEW ("output-viewer.glade");
116
117   struct output_viewer *ov ;
118   struct editor_window *e;
119
120   connect_help (xml);
121
122   ov = g_malloc (sizeof (*ov));
123
124   e = (struct editor_window *)ov;
125
126
127   e->window = GTK_WINDOW (get_widget_assert (xml, "output-viewer-window"));
128   ov->textview = get_widget_assert (xml, "output-viewer-textview");
129   ov->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (ov->textview));
130
131   g_signal_connect (e->window,
132                     "focus-in-event",
133                     G_CALLBACK (cancel_urgency),
134                     NULL);
135
136   {
137     /* Output uses ascii characters for tabular material.
138        So we need a monospaced font otherwise it'll look silly */
139     PangoFontDescription *font_desc =
140       pango_font_description_from_string ("monospace");
141
142     gtk_widget_modify_font (ov->textview, font_desc);
143     pango_font_description_free (font_desc);
144   }
145
146   g_signal_connect (ov->textview, "size-allocate",
147                     G_CALLBACK (on_textview_resize), NULL);
148
149   ov->fp = NULL;
150
151   g_signal_connect (get_widget_assert (xml,"help_about"),
152                     "activate",
153                     G_CALLBACK (about_new),
154                     e->window);
155
156   g_signal_connect (get_widget_assert (xml,"help_reference"),
157                     "activate",
158                     G_CALLBACK (reference_manual),
159                     NULL);
160
161   g_signal_connect (get_widget_assert (xml,"windows_minimise-all"),
162                     "activate",
163                     G_CALLBACK (minimise_all_windows),
164                     NULL);
165
166   g_object_unref (xml);
167
168
169   g_signal_connect (e->window, "delete-event",
170                     G_CALLBACK (on_delete), ov);
171
172   return ov;
173 }
174
175
176 void
177 reload_the_viewer (void)
178 {
179   struct stat buf;
180
181   /* If there is no output, then don't do anything */
182   if (0 != stat (OUTPUT_FILE_NAME, &buf))
183     return ;
184
185   if ( NULL == the_output_viewer )
186     {
187       the_output_viewer =
188         (struct output_viewer *) window_create (WINDOW_OUTPUT, NULL);
189     }
190
191   reload_viewer (the_output_viewer);
192 }
193
194
195 void
196 reload_viewer (struct output_viewer *ov)
197 {
198   GtkTextIter end_iter;
199   char line[OUTPUT_LINE_WIDTH];
200   GtkTextMark *mark ;
201   gboolean chars_inserted = FALSE;
202
203
204   if ( ov->fp == NULL)
205     {
206       ov->fp = fopen (OUTPUT_FILE_NAME, "r");
207       if ( ov->fp == NULL)
208         {
209           g_print ("Cannot open %s\n", OUTPUT_FILE_NAME);
210           return;
211         }
212     }
213
214   gtk_text_buffer_get_end_iter (ov->buffer, &end_iter);
215
216   mark = gtk_text_buffer_create_mark (ov->buffer, NULL, &end_iter, TRUE);
217
218   /* Read in the next lot of text */
219   while (fgets (line, OUTPUT_LINE_WIDTH, ov->fp) != NULL)
220     {
221       chars_inserted = TRUE;
222       gtk_text_buffer_insert (ov->buffer, &end_iter, line, -1);
223     }
224
225   /* Scroll to where the start of this lot of text begins */
226   gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (ov->textview),
227                                 mark,
228                                 0.1, TRUE, 0.0, 0.0);
229
230
231   if ( chars_inserted )
232     gtk_window_set_urgency_hint ( ((struct editor_window *)ov)->window, TRUE);
233 }
234
235
236