Move definition of OUTPUT_FILE_NAME to before first use.
[pspp-builds.git] / src / ui / gui / output-viewer.c
index d2f46bab59173d6c21459c288c52237703e5c3b0..2b0c4af8094dbdd1668d7c6254d3f2e7f018ef18 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <config.h>
 #include <gtk/gtk.h>
+#include <data/file-name.h>
 #include "window-manager.h"
 #include "output-viewer.h"
 #include "helper.h"
@@ -28,6 +29,8 @@
 #include <glade/glade.h>
 #include <ctype.h>
 
+#include "xalloc.h"
+
 struct output_viewer
 {
   struct editor_window parent;
@@ -46,6 +49,8 @@ cancel_urgency (GtkWindow *window,  gpointer data)
 
 static struct output_viewer *the_output_viewer = NULL;
 
+int viewer_length = 16;
+int viewer_width = 59;
 
 /* Callback for the "delete" action (clicking the x on the top right
    hand corner of the window) */
@@ -58,12 +63,52 @@ on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
 
   the_output_viewer = NULL;
 
-  unlink (OUTPUT_FILE_NAME);
+  unlink (output_file_name ());
 
   return FALSE;
 }
 
 
+/* Sets width and length according to the new size
+   of the output window */
+static void
+on_textview_resize (GtkWidget     *widget,
+                   GtkAllocation *allocation,
+                   gpointer       user_data)
+{
+  PangoContext * context ;
+  PangoLayout *layout ;
+  PangoRectangle logical;
+  GtkStyle *style;
+  gint right_margin, left_margin;
+  GtkTextView *text_view = GTK_TEXT_VIEW (widget);
+
+  context = gtk_widget_create_pango_context (widget);
+  layout = pango_layout_new (context);
+
+  style = gtk_widget_get_style (widget);
+
+  pango_layout_set_font_description (layout, style->font_desc);
+
+  /* Find the width of one character.  We can use any character, because
+     the textview has a monospaced font */
+  pango_layout_set_text (layout, "M", 1);
+
+  pango_layout_get_extents (layout,  NULL, &logical);
+
+  left_margin = gtk_text_view_get_left_margin (text_view);
+  right_margin = gtk_text_view_get_right_margin (text_view);
+
+  viewer_length = allocation->height / PANGO_PIXELS (logical.height);
+  viewer_width = (allocation->width - right_margin - left_margin)
+    / PANGO_PIXELS (logical.width);
+
+  g_object_unref (G_OBJECT (layout));
+  g_object_unref (G_OBJECT (context));
+}
+
+
+
 /*
   Create a new output viewer
 */
@@ -101,6 +146,9 @@ new_output_viewer (void)
     pango_font_description_free (font_desc);
   }
 
+  g_signal_connect (ov->textview, "size-allocate",
+                   G_CALLBACK (on_textview_resize), NULL);
+
   ov->fp = NULL;
 
   g_signal_connect (get_widget_assert (xml,"help_about"),
@@ -134,7 +182,7 @@ reload_the_viewer (void)
   struct stat buf;
 
   /* If there is no output, then don't do anything */
-  if (0 != stat (OUTPUT_FILE_NAME, &buf))
+  if (0 != stat (output_file_name (), &buf))
     return ;
 
   if ( NULL == the_output_viewer )
@@ -146,36 +194,78 @@ reload_the_viewer (void)
   reload_viewer (the_output_viewer);
 }
 
+#define OUTPUT_FILE_NAME "psppire.txt"
 
 void
 reload_viewer (struct output_viewer *ov)
 {
   GtkTextIter end_iter;
-  char line[OUTPUT_LINE_WIDTH];
   GtkTextMark *mark ;
-  gboolean chars_inserted = FALSE;
 
+  static char *line = NULL;
 
-  if ( ov->fp == NULL)
-    {
-      ov->fp = fopen (OUTPUT_FILE_NAME, "r");
-      if ( ov->fp == NULL)
-       {
-         g_print ("Cannot open %s\n", OUTPUT_FILE_NAME);
-         return;
-       }
-    }
+  gboolean chars_inserted = FALSE;
 
   gtk_text_buffer_get_end_iter (ov->buffer, &end_iter);
 
+  line = xrealloc (line, sizeof (char) * (viewer_width + 1));
+
+
   mark = gtk_text_buffer_create_mark (ov->buffer, NULL, &end_iter, TRUE);
 
-  /* Read in the next lot of text */
-  while (fgets (line, OUTPUT_LINE_WIDTH, ov->fp) != NULL)
-    {
-      chars_inserted = TRUE;
-      gtk_text_buffer_insert (ov->buffer, &end_iter, line, -1);
-    }
+#ifdef __CYGWIN__
+  /*
+    Apparently Windoze is not capabale of writing to a file whilst
+    another (or the same) process is reading from it.   Therefore, we
+    must close the file after reading it, and clear the entire buffer
+    before writing to it.
+    This will be slower for large buffers, but should work
+    (in so far as anything ever works on windows).
+  */
+  {
+    GtkTextIter start_iter;
+    FILE *fp = fopen (OUTPUT_FILE_NAME, "r");
+    if ( !fp)
+      {
+       g_print ("Cannot open %s\n", OUTPUT_FILE_NAME);
+       return;
+      }
+
+    /* Delete all the entire buffer */
+    gtk_text_buffer_get_start_iter (ov->buffer, &start_iter);
+    gtk_text_buffer_delete (ov->buffer, &start_iter, &end_iter);
+
+
+    gtk_text_buffer_get_start_iter (ov->buffer, &start_iter);
+    /* Read in the next lot of text */
+    while (fgets (line, viewer_width + 1, fp) != NULL)
+      {
+       chars_inserted = TRUE;
+       gtk_text_buffer_insert (ov->buffer, &start_iter, line, -1);
+      }
+
+    fclose (fp);
+  }
+#else
+  {
+    if ( ov->fp == NULL)
+      {
+       ov->fp = fopen (output_file_name (), "r");
+       if ( ov->fp == NULL)
+         {
+           g_print ("Cannot open %s\n", output_file_name ());
+           return;
+         }
+      }
+
+    /* Read in the next lot of text */
+    while (fgets (line, viewer_width + 1, ov->fp) != NULL)
+      {
+       chars_inserted = TRUE;
+       gtk_text_buffer_insert (ov->buffer, &end_iter, line, -1);
+      }
+  }
+#endif
 
   /* Scroll to where the start of this lot of text begins */
   gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (ov->textview),
@@ -189,3 +279,16 @@ reload_viewer (struct output_viewer *ov)
 
 
 
+
+const char *
+output_file_name (void)
+{
+  const char *dir = default_output_path ();
+  static char *filename = NULL;
+
+  if ( NULL == filename )
+    filename = xasprintf ("%s%s", dir, OUTPUT_FILE_NAME);
+
+
+  return filename;
+}