6e5664f5cfda8474918f010f9170543a192200b6
[pspp-builds.git] / src / ui / gui / message-dialog.c
1 /* 
2    PSPPIRE --- A Graphical User Interface for PSPP
3    Copyright (C) 2004,2005  Free Software Foundation
4    Written by John Darrington
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA. 
20 */
21
22
23 #include <stdio.h>
24 #include <stdarg.h>
25
26 #include <config.h>
27 #include <gettext.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
30
31 #include <libpspp/message.h>
32 #include <libpspp/msg-locator.h>
33 #include "message-dialog.h"
34 #include "progname.h"
35
36
37 #include <gtk/gtk.h>
38 #include <glade/glade.h>
39 #include <glib.h>
40
41 #include "helper.h"
42
43 extern GladeXML *xml;
44
45
46
47 static void enqueue_msg (const struct msg *m);
48
49
50 static GQueue *message_queue;
51
52
53 void
54 message_dialog_init (struct source_stream *ss) 
55 {
56   message_queue = g_queue_new();
57   msg_init (ss, enqueue_msg);
58 }
59
60 void
61 message_dialog_done (void)
62 {
63   msg_done();
64   g_queue_free(message_queue);
65 }
66
67 static gboolean 
68 dequeue_message(gpointer data)
69 {
70   struct msg * m ;
71
72   /* If a pointer grab is in effect, then the combination of that, and
73      a modal dialog box, will cause an impossible situation. 
74      So don't pop it up just yet.
75   */ 
76   if ( gdk_pointer_is_grabbed())
77     return TRUE;
78
79   m = g_queue_pop_tail(message_queue);
80
81   if ( m ) 
82     {
83       popup_message(m);
84       msg_destroy(m);
85       return TRUE;
86     }
87   
88   return FALSE;
89 }
90
91 static void
92 enqueue_msg(const struct msg *msg)
93 {
94   struct msg *m = msg_dup(msg);
95
96   g_queue_push_head(message_queue, m);
97
98   g_idle_add(dequeue_message, 0);
99 }
100
101
102 void 
103 popup_message(const struct msg *m)
104 {
105   GtkWindow *parent;
106   GtkWidget *dialog;
107
108   gint message_type;
109   const char *msg;
110
111   switch (m->severity)
112     {
113     case MSG_ERROR:
114       message_type = GTK_MESSAGE_ERROR;
115       break;
116     case MSG_WARNING:
117       message_type = GTK_MESSAGE_WARNING;
118       break;
119     case MSG_NOTE:
120     default:
121       message_type = GTK_MESSAGE_INFO;
122       break;
123     };
124   
125   switch (m->category) 
126     {
127     case MSG_SYNTAX:
128       msg = _("Script Error");
129       break;
130
131     case MSG_DATA:
132       msg = _("Data File Error");
133       break;
134
135     case MSG_GENERAL:
136     default:
137       msg = _("PSPP Error");
138       break;
139     };
140   
141   parent = GTK_WINDOW(get_widget_assert(xml, "data_editor"));
142
143   dialog = gtk_message_dialog_new(parent,
144                                   GTK_DIALOG_MODAL,
145                                   message_type,
146                                   GTK_BUTTONS_CLOSE,
147                                   msg);
148   
149   gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
150                                            "%s", m->text);
151     
152   gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
153
154   gtk_dialog_run(GTK_DIALOG(dialog));
155
156   gtk_widget_destroy (dialog);
157 }
158