e5b2756290ab0cc66533eb4d30d02ec535d006da
[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 <libpspp/message.h>
28 #include "message-dialog.h"
29 #include "progname.h"
30
31
32 #include <gtk/gtk.h>
33 #include <glade/glade.h>
34 #include <glib.h>
35
36 #include "helper.h"
37
38 extern GladeXML *xml;
39
40 #define _(A) A
41
42 static void handle_msg(const struct msg *);
43 static void enqueue_msg(const struct msg *m);
44
45
46 static GQueue *message_queue;
47
48 void
49 message_dialog_init (void) 
50 {
51   message_queue = g_queue_new();
52   msg_init(enqueue_msg);
53 }
54
55
56 void
57 message_dialog_done (void)
58 {
59   msg_done();
60   g_queue_free(message_queue);
61 }
62
63 static gboolean 
64 dequeue_message(gpointer data)
65 {
66   struct msg * m ;
67
68   /* If a pointer grab is in effect, then the combination of that, and
69      a modal dialog box, will cause an impossible situation. 
70      So don't pop it up just yet.
71   */ 
72   if ( gdk_pointer_is_grabbed())
73     return TRUE;
74
75   m = g_queue_pop_tail(message_queue);
76
77   if ( m ) 
78     {
79       handle_msg(m);
80       msg_destroy(m);
81       return TRUE;
82     }
83   
84   return FALSE;
85 }
86
87 static void
88 enqueue_msg(const struct msg *msg)
89 {
90   struct msg *m = msg_dup(msg);
91
92   g_queue_push_head(message_queue, m);
93
94   g_idle_add(dequeue_message, 0);
95 }
96
97 static void
98 handle_msg(const struct msg *m)
99 {
100   GtkWindow *parent;
101   GtkWidget *dialog;
102
103   gint message_type;
104   const char *msg;
105
106   switch (m->severity)
107     {
108     case MSG_ERROR:
109       message_type = GTK_MESSAGE_ERROR;
110       break;
111     case MSG_WARNING:
112       message_type = GTK_MESSAGE_WARNING;
113       break;
114     case MSG_NOTE:
115     default:
116       message_type = GTK_MESSAGE_INFO;
117       break;
118     };
119   
120   switch (m->category) 
121     {
122     case MSG_SYNTAX:
123       msg = _("Script Error");
124       break;
125
126     case MSG_DATA:
127       msg = _("Data File Error");
128       break;
129
130     case MSG_GENERAL:
131     default:
132       msg = _("PSPP Error");
133       break;
134     };
135   
136   parent = GTK_WINDOW(get_widget_assert(xml, "data_editor"));
137
138   dialog = gtk_message_dialog_new(parent,
139                                   GTK_DIALOG_MODAL,
140                                   message_type,
141                                   GTK_BUTTONS_CLOSE,
142                                   msg);
143   
144   gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
145                                            "%s", m->text);
146     
147   gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
148
149   gtk_dialog_run(GTK_DIALOG(dialog));
150
151   gtk_widget_destroy (dialog);
152 }
153
154 /* FIXME: This is a stub .
155  * A temporary workaround until getl.c is rearranged
156  */
157 void
158 msg_location (struct msg_locator *loc)
159 {
160         loc->file_name = 0;
161         loc->line_number = -1;
162 }
163