Start reforming error message support. In this phase, we get rid of
[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
30
31 #include <gtk/gtk.h>
32 #include <glade/glade.h>
33
34 #include "helper.h"
35
36 extern GladeXML *xml;
37
38 #define _(A) A
39
40
41 void 
42 vmsg(int klass, const char *fmt, va_list args)
43 {
44   gchar *msg = 0;
45   gchar *text = g_strdup_vprintf (fmt, args);
46
47   GtkWindow *parent ;
48   GtkWidget *dialog ;
49                     
50   gint message_type;
51
52   switch (klass)
53     {
54     case SE:
55     case DE:
56     case ME:
57       message_type = GTK_MESSAGE_ERROR;
58       break;
59     case SW:
60     case DW:
61     case MW:
62       message_type = GTK_MESSAGE_WARNING;
63       break;
64     case SM:
65     case MM:
66     default:
67       message_type = GTK_MESSAGE_INFO;
68       break;
69     };
70   
71   switch (klass) 
72     {
73     case SE:
74     case SW:
75     case SM:
76       msg = g_strdup(_("Script Error"));
77       break;
78
79     case DE:
80     case DW:
81       msg = g_strdup(_("Data File Error"));
82       break;
83
84     case ME:
85     case MW:
86     case MM:
87     default:
88       msg = g_strdup(_("PSPP Error"));
89       break;
90     };
91   
92   parent = GTK_WINDOW(get_widget_assert(xml, "data_editor"));
93
94   dialog = gtk_message_dialog_new(parent,
95                                   GTK_DIALOG_MODAL,
96                                   message_type,
97                                   GTK_BUTTONS_CLOSE,
98                                   msg);
99   
100   gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), text);
101
102   g_free(text);
103   g_free(msg);
104     
105   gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
106
107   gtk_dialog_run(GTK_DIALOG(dialog));
108
109   gtk_widget_destroy (dialog);
110
111 }
112
113
114 void 
115 msg(int klass, const char *fmt, ...)
116 {
117   va_list ap;
118   va_start(ap, fmt);
119   vmsg(klass, fmt, ap);
120   va_end(ap);
121 }
122
123
124 void
125 err_vmsg (const struct error *e, const char *format, va_list args)
126 {
127   vmsg(e->class, format, args);
128 }
129
130
131 void 
132 err_assert_fail(const char *expr, const char *file, int line)
133 {
134   msg(ME, "Assertion failed: %s:%d; (%s)\n",file,line,expr);
135 }
136
137 /* The GUI is always interactive.
138    So this function does nothing */
139 void 
140 err_cond_fail(void)
141 {
142 }
143
144
145 void
146 err_failure(void)
147 {
148   msg(ME, _("Terminating NOW due to fatal error"));
149   gtk_main_quit();
150 }
151
152
153 /* FIXME: This is a stub .
154  * A temporary workaround until getl.c is rearranged
155  */
156 void
157 err_location (struct file_locator *f)
158 {
159         f->filename = 0;
160         f->line_number = -1;
161 }
162