Merge commit 'origin/stable'
[pspp-builds.git] / src / ui / gui / message-dialog.c
index 708d397e066cd8332dd72cb00ef86c489fd1d5fb..7513295323988677b38c26807cff73b96d10b04a 100644 (file)
 #define N_(msgid) msgid
 
 #include <libpspp/message.h>
+#include <libpspp/str.h>
 #include <libpspp/msg-locator.h>
 #include "message-dialog.h"
 #include "progname.h"
 
 
 #include <gtk/gtk.h>
-#include <glade/glade.h>
 #include <glib.h>
 
 #include "helper.h"
 
 static void enqueue_msg (const struct msg *m);
+static gboolean popup_messages (gpointer);
 
+#define MAX_EARLY_MESSAGES 100
+static GQueue *early_queue;
 
-static GQueue *message_queue;
+static unsigned long dropped_messages;
 
+#define MAX_LATE_MESSAGES 10
+static GQueue *late_queue;
+
+static int error_cnt, warning_cnt, note_cnt;
+
+static GtkBuilder *message_xml;
+static GtkWidget *message_dialog;
 
 void
 message_dialog_init (struct source_stream *ss)
 {
-  message_queue = g_queue_new ();
+  early_queue = g_queue_new ();
+  dropped_messages = 0;
+  late_queue = g_queue_new ();
+  error_cnt = warning_cnt = note_cnt = 0;
   msg_init (ss, enqueue_msg);
+  message_xml = builder_new ("message-dialog.ui");
+  message_dialog = get_widget_assert (message_xml, "message-dialog");
+
+  GTK_WIDGET_SET_FLAGS (get_widget_assert (message_xml, "close-button"),
+                       GTK_CAN_DEFAULT);
+
 }
 
 void
 message_dialog_done (void)
 {
   msg_done ();
-  g_queue_free (message_queue);
-}
-
-static gboolean
-dequeue_message (gpointer data)
-{
-  struct msg * m ;
-
-  /* If a pointer grab is in effect, then the combination of that, and
-     a modal dialog box, will cause an impossible situation.
-     So don't pop it up just yet.
-  */
-  if ( gdk_pointer_is_grabbed ())
-    return TRUE;
-
-  m = g_queue_pop_tail (message_queue);
-
-  if ( m )
-    {
-      popup_message (m);
-      msg_destroy (m);
-      return TRUE;
-    }
-
-  return FALSE;
+  g_queue_free (early_queue);
+  dropped_messages = 0;
+  g_queue_free (late_queue);
+  gtk_widget_destroy (message_dialog);
+  g_object_unref (message_xml);
 }
 
 static void
-enqueue_msg (const struct msg *msg)
+format_message (struct msg *m, struct string *msg)
 {
-  struct msg *m = msg_dup (msg);
-
-  g_queue_push_head (message_queue, m);
-
-  g_idle_add (dequeue_message, 0);
-}
-
+  const char *label;
 
-void
-popup_message (const struct msg *m)
-{
-  GtkWidget *dialog;
-  gchar *location = NULL;
-
-  gint message_type;
-  const char *msg;
+  if (m->where.file_name)
+    ds_put_format (msg, "%s:", m->where.file_name);
+  if (m->where.line_number != -1)
+    ds_put_format (msg, "%d:", m->where.line_number);
+  if (m->where.file_name || m->where.line_number != -1)
+    ds_put_char (msg, ' ');
 
   switch (m->severity)
     {
     case MSG_ERROR:
-      message_type = GTK_MESSAGE_ERROR;
       switch (m->category)
        {
        case MSG_SYNTAX:
-         msg = _("Syntax Error");
+         label = _("syntax error");
          break;
 
        case MSG_DATA:
-         msg = _("Data File Error");
+         label = _("data file error");
          break;
 
        case MSG_GENERAL:
        default:
-         msg = _("PSPP Error");
+         label = _("PSPP error");
          break;
-       };
+       }
       break;
     case MSG_WARNING:
-      message_type = GTK_MESSAGE_WARNING;
       switch (m->category)
        {
        case MSG_SYNTAX:
-         msg = _("Syntax Warning");
-      break;
+         label = _("syntax warning");
+          break;
 
        case MSG_DATA:
-         msg = _("Data File Warning");
+         label = _("data file warning");
          break;
 
        case MSG_GENERAL:
-    default:
-         msg = _("PSPP Warning");
-      break;
-    };
+        default:
+         label = _("PSPP warning");
+          break;
+        }
       break;
     case MSG_NOTE:
     default:
-      message_type = GTK_MESSAGE_INFO;
-  switch (m->category)
-    {
-    case MSG_SYNTAX:
-         msg = _("Syntax Information");
+      switch (m->category)
+        {
+        case MSG_SYNTAX:
+         label = _("syntax information");
+          break;
+
+        case MSG_DATA:
+         label = _("data file information");
+          break;
+
+        case MSG_GENERAL:
+        default:
+         label = _("PSPP information");
+         break;
+       }
       break;
+    }
+  ds_put_format (msg, "%s: %s\n", label, m->text);
+  msg_destroy (m);
+}
 
-    case MSG_DATA:
-         msg = _("Data File Information");
-      break;
+static void
+enqueue_msg (const struct msg *msg)
+{
+  struct msg *m = msg_dup (msg);
 
-    case MSG_GENERAL:
-    default:
-         msg = _("PSPP Information");
-         break;
-       };
+  switch (m->severity)
+    {
+    case MSG_ERROR:
+      error_cnt++;
+      break;
+    case MSG_WARNING:
+      warning_cnt++;
+      break;
+    case MSG_NOTE:
+      note_cnt++;
       break;
-    };
-
-  dialog = gtk_message_dialog_new ( NULL,
-                                 GTK_DIALOG_MODAL,
-                                 message_type,
-                                 GTK_BUTTONS_CLOSE,
-                                 msg);
-  if ( m->where.line_number != -1)
+    }
+
+  if (g_queue_get_length (early_queue) < MAX_EARLY_MESSAGES)
+    {
+      if (g_queue_is_empty (early_queue))
+        g_idle_add (popup_messages, NULL);
+      g_queue_push_tail (early_queue, m);
+    }
+  else
+    {
+      if (g_queue_get_length (late_queue) >= MAX_LATE_MESSAGES)
+        {
+          struct msg *m = g_queue_pop_head (late_queue);
+          msg_destroy (m);
+          dropped_messages++;
+        }
+      g_queue_push_tail (late_queue, m);
+    }
+}
+
+static gboolean
+popup_messages (gpointer unused UNUSED)
+{
+  GtkTextBuffer *text_buffer;
+  GtkTextIter end;
+  GtkTextView *text_view;
+  GtkLabel *label;
+  struct string lead = DS_EMPTY_INITIALIZER;
+  struct string msg = DS_EMPTY_INITIALIZER;
+  int message_cnt;
+
+  gdk_threads_enter ();
+
+  /* Set up the dialog. */
+  if (message_xml == NULL || message_dialog == NULL)
+    goto use_fallback;
+
+  /* If a pointer grab is in effect, then the combination of that, and
+     a modal dialog box, will cause an impossible situation.
+     So don't pop it up just yet.
+  */
+  if ( gdk_display_pointer_is_grabbed (gtk_widget_get_display (message_dialog)))
     {
-      location = g_strdup_printf (_("%s (line %d)"),
-                                 m->where.file_name ? m->where.file_name : "",
-                                 m->where.line_number);
+      ds_destroy (&lead);
+      ds_destroy (&msg);
+      gdk_threads_leave ();
+      return TRUE;
     }
+
+  /* Compose the lead-in. */
+  message_cnt = error_cnt + warning_cnt + note_cnt;
+  if (dropped_messages == 0)
+    ds_put_format (
+      &lead,
+      ngettext ("The PSPP processing engine reported the following message:",
+                "The PSPP processing engine reported the following messages:",
+                message_cnt));
   else
     {
-      location = g_strdup_printf (_("%s"),
-                                 m->where.file_name ? m->where.file_name : "");    }
+      ds_put_format (
+        &lead,
+        ngettext ("The PSPP processing engine reported %d message.",
+                  "The PSPP processing engine reported %d messages.",
+                  message_cnt),
+        message_cnt);
+      ds_put_cstr (&lead, "  ");
+      ds_put_format (
+        &lead,
+        ngettext ("%d of these messages are displayed below.",
+                  "%d of these messages are displayed below.",
+                  MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES),
+        MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES);
+    }
+
+
+  /* Compose the messages. */
+  while (!g_queue_is_empty (early_queue))
+    format_message (g_queue_pop_head (early_queue), &msg);
+  if (dropped_messages)
+    {
+      ds_put_format (&msg, "...\nOmitting %lu messages\n...\n",
+                     dropped_messages);
+      dropped_messages = 0;
+    }
+  while (!g_queue_is_empty (late_queue))
+    format_message (g_queue_pop_head (late_queue), &msg);
 
-  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
-                                           _("%s %s"),
-                                           location,
-                                           m->text);
-  g_free (location);
+  text_buffer = gtk_text_buffer_new (NULL);
+  gtk_text_buffer_get_end_iter (text_buffer, &end);
+  gtk_text_buffer_insert (text_buffer, &end, ds_data (&msg), ds_length (&msg));
 
-  gtk_window_set_keep_above (GTK_WINDOW (dialog), TRUE);
+  label = GTK_LABEL (get_widget_assert (message_xml, "lead-in"));
+  if (label == NULL)
+    goto use_fallback;
+  gtk_label_set_text (label, ds_cstr (&lead));
 
-  gtk_dialog_run (GTK_DIALOG (dialog));
+  text_view = GTK_TEXT_VIEW (get_widget_assert (message_xml, "message"));
+  if (text_view == NULL)
+    goto use_fallback;
+  gtk_text_view_set_buffer (text_view, text_buffer);
 
-  gtk_widget_destroy (dialog);
+  gtk_widget_grab_default (get_widget_assert (message_xml, "close-button"));
+  gtk_widget_grab_focus (get_widget_assert (message_xml, "close-button"));
+  gtk_dialog_run ( GTK_DIALOG (message_dialog));
+  gtk_widget_hide (message_dialog);
+
+  ds_destroy (&lead);
+  ds_destroy (&msg);
+
+  gdk_threads_leave ();
+  return FALSE;
+
+use_fallback:
+  g_warning ("Could not create message dialog.  "
+             "Is PSPPIRE properly installed?");
+  fputs (ds_cstr (&msg), stderr);
+  ds_destroy (&lead);
+  ds_destroy (&msg);
+  gdk_threads_leave ();
+  return FALSE;
 }