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");
65 GTK_WIDGET_SET_FLAGS (get_widget_assert (message_xml, "close-button"),
71 message_dialog_done (void)
74 g_queue_free (early_queue);
76 g_queue_free (late_queue);
77 gtk_widget_destroy (message_dialog);
78 g_object_unref (message_xml);
82 format_message (struct msg *m, struct string *msg)
86 if (m->where.file_name)
87 ds_put_format (msg, "%s:", m->where.file_name);
88 if (m->where.line_number != -1)
89 ds_put_format (msg, "%d:", m->where.line_number);
90 if (m->where.file_name || m->where.line_number != -1)
91 ds_put_char (msg, ' ');
99 label = _("syntax error");
103 label = _("data file error");
108 label = _("PSPP error");
116 label = _("syntax warning");
120 label = _("data file warning");
125 label = _("PSPP warning");
134 label = _("syntax information");
138 label = _("data file information");
143 label = _("PSPP information");
148 ds_put_format (msg, "%s: %s\n", label, m->text);
153 enqueue_msg (const struct msg *msg)
155 struct msg *m = msg_dup (msg);
170 if (g_queue_get_length (early_queue) < MAX_EARLY_MESSAGES)
172 if (g_queue_is_empty (early_queue))
173 g_idle_add (popup_messages, NULL);
174 g_queue_push_tail (early_queue, m);
178 if (g_queue_get_length (late_queue) >= MAX_LATE_MESSAGES)
180 struct msg *m = g_queue_pop_head (late_queue);
184 g_queue_push_tail (late_queue, m);
189 popup_messages (gpointer unused UNUSED)
191 GtkTextBuffer *text_buffer;
193 GtkTextView *text_view;
195 struct string lead = DS_EMPTY_INITIALIZER;
196 struct string msg = DS_EMPTY_INITIALIZER;
199 gdk_threads_enter ();
201 /* Set up the dialog. */
202 if (message_xml == NULL || message_dialog == NULL)
205 /* If a pointer grab is in effect, then the combination of that, and
206 a modal dialog box, will cause an impossible situation.
207 So don't pop it up just yet.
209 if ( gdk_display_pointer_is_grabbed (gtk_widget_get_display (message_dialog)))
213 gdk_threads_leave ();
217 /* Compose the lead-in. */
218 message_cnt = error_cnt + warning_cnt + note_cnt;
219 if (dropped_messages == 0)
222 ngettext ("The PSPP processing engine reported the following message:",
223 "The PSPP processing engine reported the following messages:",
229 ngettext ("The PSPP processing engine reported %d message.",
230 "The PSPP processing engine reported %d messages.",
233 ds_put_cstr (&lead, " ");
236 ngettext ("%d of these messages are displayed below.",
237 "%d of these messages are displayed below.",
238 MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES),
239 MAX_EARLY_MESSAGES + MAX_LATE_MESSAGES);
243 /* Compose the messages. */
244 while (!g_queue_is_empty (early_queue))
245 format_message (g_queue_pop_head (early_queue), &msg);
246 if (dropped_messages)
248 ds_put_format (&msg, "...\nOmitting %lu messages\n...\n",
250 dropped_messages = 0;
252 while (!g_queue_is_empty (late_queue))
253 format_message (g_queue_pop_head (late_queue), &msg);
255 text_buffer = gtk_text_buffer_new (NULL);
256 gtk_text_buffer_get_end_iter (text_buffer, &end);
257 gtk_text_buffer_insert (text_buffer, &end, ds_data (&msg), ds_length (&msg));
259 label = GTK_LABEL (get_widget_assert (message_xml, "lead-in"));
262 gtk_label_set_text (label, ds_cstr (&lead));
264 text_view = GTK_TEXT_VIEW (get_widget_assert (message_xml, "message"));
265 if (text_view == NULL)
267 gtk_text_view_set_buffer (text_view, text_buffer);
269 gtk_widget_grab_default (get_widget_assert (message_xml, "close-button"));
270 gtk_widget_grab_focus (get_widget_assert (message_xml, "close-button"));
271 gtk_dialog_run ( GTK_DIALOG (message_dialog));
272 gtk_widget_hide (message_dialog);
277 gdk_threads_leave ();
281 g_warning ("Could not create message dialog. "
282 "Is PSPPIRE properly installed?");
283 fputs (ds_cstr (&msg), stderr);
286 gdk_threads_leave ();