1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
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.
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.
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/>. */
23 #define _(msgid) gettext (msgid)
24 #define N_(msgid) msgid
26 #include <libpspp/message.h>
27 #include <libpspp/str.h>
28 #include <libpspp/msg-locator.h>
29 #include "message-dialog.h"
38 static void enqueue_msg (const struct msg *m);
39 static gboolean popup_messages (gpointer);
41 #define MAX_EARLY_MESSAGES 100
42 static GQueue *early_queue;
44 static unsigned long dropped_messages;
46 #define MAX_LATE_MESSAGES 10
47 static GQueue *late_queue;
49 static int error_cnt, warning_cnt, note_cnt;
51 static GtkBuilder *message_xml;
52 static GtkWidget *message_dialog;
55 message_dialog_init (struct source_stream *ss)
57 early_queue = g_queue_new ();
59 late_queue = g_queue_new ();
60 error_cnt = warning_cnt = note_cnt = 0;
61 msg_init (ss, enqueue_msg);
62 message_xml = builder_new ("message-dialog.ui");
63 message_dialog = get_widget_assert (message_xml, "message-dialog");
67 message_dialog_done (void)
70 g_queue_free (early_queue);
72 g_queue_free (late_queue);
73 gtk_widget_destroy (message_dialog);
74 g_object_unref (message_xml);
78 format_message (struct msg *m, struct string *msg)
82 if (m->where.file_name)
83 ds_put_format (msg, "%s:", m->where.file_name);
84 if (m->where.line_number != -1)
85 ds_put_format (msg, "%d:", m->where.line_number);
86 if (m->where.file_name || m->where.line_number != -1)
87 ds_put_char (msg, ' ');
95 label = _("syntax error");
99 label = _("data file error");
104 label = _("PSPP error");
112 label = _("syntax warning");
116 label = _("data file warning");
121 label = _("PSPP warning");
130 label = _("syntax information");
134 label = _("data file information");
139 label = _("PSPP information");
144 ds_put_format (msg, "%s: %s\n", label, m->text);
149 enqueue_msg (const struct msg *msg)
151 struct msg *m = msg_dup (msg);
166 if (g_queue_get_length (early_queue) < MAX_EARLY_MESSAGES)
168 if (g_queue_is_empty (early_queue))
169 g_idle_add (popup_messages, NULL);
170 g_queue_push_tail (early_queue, m);
174 if (g_queue_get_length (late_queue) >= MAX_LATE_MESSAGES)
176 struct msg *m = g_queue_pop_head (late_queue);
180 g_queue_push_tail (late_queue, m);
185 popup_messages (gpointer unused UNUSED)
187 GtkTextBuffer *text_buffer;
189 GtkTextView *text_view;
191 struct string lead = DS_EMPTY_INITIALIZER;
192 struct string msg = DS_EMPTY_INITIALIZER;
195 /* Set up the dialog. */
196 if (message_xml == NULL || message_dialog == NULL)
199 /* If a pointer grab is in effect, then the combination of that, and
200 a modal dialog box, will cause an impossible situation.
201 So don't pop it up just yet.
203 if ( gdk_display_pointer_is_grabbed (gtk_widget_get_display (message_dialog)))
210 /* Compose the lead-in. */
211 message_cnt = error_cnt + warning_cnt + note_cnt;
212 if (dropped_messages == 0)
215 ngettext ("The PSPP processing engine reported the following message:",
216 "The PSPP processing engine reported the following messages:",
222 ngettext ("The PSPP processing engine reported %d message.",
223 "The PSPP processing engine reported %d messages.",
226 ds_put_cstr (&lead, " ");
229 ngettext ("%d of these messages are displayed below.",
230 "%d of these messages are displayed below.",
231 MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES),
232 MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES);
236 /* Compose the messages. */
237 while (!g_queue_is_empty (early_queue))
238 format_message (g_queue_pop_head (early_queue), &msg);
239 if (dropped_messages)
241 ds_put_format (&msg, "...\nOmitting %lu messages\n...\n",
243 dropped_messages = 0;
245 while (!g_queue_is_empty (late_queue))
246 format_message (g_queue_pop_head (late_queue), &msg);
248 text_buffer = gtk_text_buffer_new (NULL);
249 gtk_text_buffer_get_end_iter (text_buffer, &end);
250 gtk_text_buffer_insert (text_buffer, &end, ds_data (&msg), ds_length (&msg));
252 label = GTK_LABEL (get_widget_assert (message_xml, "lead-in"));
255 gtk_label_set_text (label, ds_cstr (&lead));
257 text_view = GTK_TEXT_VIEW (get_widget_assert (message_xml, "message"));
258 if (text_view == NULL)
260 gtk_text_view_set_buffer (text_view, text_buffer);
262 gtk_widget_grab_focus (get_widget_assert (message_xml, "close-button"));
263 gtk_dialog_run ( GTK_DIALOG (message_dialog));
264 gtk_widget_hide (message_dialog);
272 g_warning ("Could not create message dialog. "
273 "Is PSPPIRE properly installed?");
274 fputs (ds_cstr (&msg), stderr);