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