Continue reforming error message support. In this phase, we divide
[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
35 #include "helper.h"
36
37 extern GladeXML *xml;
38
39 #define _(A) A
40
41
42 void 
43 vmsg(int klass, const char *fmt, va_list args)
44 {
45   gchar *msg = 0;
46   gchar *text = g_strdup_vprintf (fmt, args);
47
48   GtkWindow *parent ;
49   GtkWidget *dialog ;
50                     
51   gint message_type;
52
53   switch (msg_class_to_severity (klass))
54     {
55     case MSG_ERROR:
56       message_type = GTK_MESSAGE_ERROR;
57       break;
58     case MSG_WARNING:
59       message_type = GTK_MESSAGE_WARNING;
60       break;
61     case MSG_NOTE:
62     default:
63       message_type = GTK_MESSAGE_INFO;
64       break;
65     };
66   
67   switch (msg_class_to_category (klass)) 
68     {
69     case MSG_SYNTAX:
70       msg = g_strdup(_("Script Error"));
71       break;
72
73     case MSG_DATA:
74       msg = g_strdup(_("Data File Error"));
75       break;
76
77     case MSG_GENERAL:
78     default:
79       msg = g_strdup(_("PSPP Error"));
80       break;
81     };
82   
83   parent = GTK_WINDOW(get_widget_assert(xml, "data_editor"));
84
85   dialog = gtk_message_dialog_new(parent,
86                                   GTK_DIALOG_MODAL,
87                                   message_type,
88                                   GTK_BUTTONS_CLOSE,
89                                   msg);
90   
91   gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), text);
92
93   g_free(text);
94   g_free(msg);
95     
96   gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
97
98   gtk_dialog_run(GTK_DIALOG(dialog));
99
100   gtk_widget_destroy (dialog);
101
102 }
103
104
105 void 
106 msg(enum msg_class klass, const char *fmt, ...)
107 {
108   va_list ap;
109   va_start(ap, fmt);
110   vmsg(klass, fmt, ap);
111   va_end(ap);
112 }
113
114
115 void
116 err_vmsg (const struct error *e, const char *format, va_list args)
117 {
118   vmsg(msg_class_from_category_and_severity (e->category, e->severity),
119        format, args);
120 }
121
122
123 void 
124 err_assert_fail(const char *expr, const char *file, int line)
125 {
126   msg(ME, "Assertion failed: %s:%d; (%s)\n",file,line,expr);
127 }
128
129 /* Writes MESSAGE formatted with printf, to stderr, if the
130    verbosity level is at least LEVEL. */
131 void
132 verbose_msg (int level, const char *format, ...)
133 {
134   /* Do nothing for now. */
135 }
136
137 /* FIXME: This is a stub .
138  * A temporary workaround until getl.c is rearranged
139  */
140 void
141 err_location (struct file_locator *f)
142 {
143         f->file_name = 0;
144         f->line_number = -1;
145 }
146