chiark / gitweb /
Expunge revision histories in files.
[mgLib] / msg.c
1 /* -*-c-*-
2  *
3  * $Id: msg.c,v 1.11 2004/04/08 01:36:14 mdw Exp $
4  *
5  * Display a message and get an answer
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mgLib GTK utilities library.
13  *
14  * mgLib is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (at your option) any later version.
18  * 
19  * mgLib is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Library General Public License for more details.
23  * 
24  * You should have received a copy of the GNU Library General Public
25  * License along with mgLib; if not, write to the Free
26  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <gtk/gtk.h>
38
39 #include <mLib/alloc.h>
40 #include <mLib/dstr.h>
41
42 #include "cancel.h"
43 #include "mdwfocus.h"
44 #include "msg.h"
45
46 /*----- Static variables --------------------------------------------------*/
47
48 static int reply;
49 static int creply;
50
51 /*----- Main code ---------------------------------------------------------*/
52
53 /* --- @msg@ --- *
54  *
55  * Arguments:   @const char *title@ = the title for the message box
56  *              @const char *buttons@ = the button strings to display
57  *              @const char *msg@ = the message skeleton string
58  *
59  * Returns:     Index of the button selected.
60  *
61  * Use:         Displays a message to the user in a nice dialogue box and
62  *              returns the index of the button selected.
63  *
64  *              The @msg@ argument is a @printf@-style format string, which
65  *              contains the message to actually be shown.  The @buttons@
66  *              argument is a comma-separated list of buttons to be drawn,
67  *              from right to left.  A button name can be preceded with `:'
68  *              to indicate that it's the default, or `~' if it's the
69  *              `cancel' button.  The return value is the zero-based index
70  *              of the button selected.
71  */
72
73 static int close(GtkWidget *w, gpointer p)
74 {
75   reply = creply;
76   gtk_main_quit();
77   return (1);
78 }
79
80 static void click(GtkWidget *w, gpointer p)
81 {
82   reply = (int)p;
83   gtk_main_quit();
84 }
85
86 int msg(const char *title, const char *buttons, const char *msg, ...)
87 {
88   GtkWidget *dbox, *w;
89
90   /* --- Make most of the dialogue box --- */
91
92   dbox = gtk_dialog_new();
93   gtk_window_set_title(GTK_WINDOW(dbox), title);
94   gtk_signal_connect(GTK_OBJECT(dbox), "delete_event",
95                      GTK_SIGNAL_FUNC(close), 0);
96   gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(dbox)->action_area), 0);  
97
98   /* --- Set up the message string --- */
99
100   {
101     dstr d = DSTR_INIT;
102     va_list ap;
103
104     va_start(ap, msg);
105     dstr_vputf(&d, msg, &ap);
106     va_end(ap);
107     w = gtk_label_new(d.buf);
108     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dbox)->vbox), w, 1, 1, 0);
109     gtk_window_position(GTK_WINDOW(dbox), GTK_WIN_POS_MOUSE);
110     gtk_widget_show(w);
111     gtk_misc_set_padding(GTK_MISC(w), 16, 16);
112     dstr_destroy(&d);
113   }
114
115   /* --- Set up the buttons --- */
116
117   {
118     char *p = xstrdup(buttons);
119     unsigned f = 0;
120     int i = 0;
121
122 #define f_ok 1u
123 #define f_cancel 2u
124 #define f_mdwfocus 4u
125
126     if (*p == '!') {
127       f |= f_mdwfocus;
128       p++;
129     }
130
131     creply = -1;
132
133     for (p = strtok(p, ","); p; p = strtok(0, ","), i++) {
134       unsigned ff = 0;
135       if (*p == ':') {
136         ff |= f_ok;
137         p++;
138       }
139       if (*p == '~') {
140         ff |= f_cancel;
141         creply = i;
142         p++;
143       }
144       w = gtk_button_new_with_label(p);
145       GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
146       gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dbox)->action_area), w, 0, 0, 0);
147       gtk_signal_connect(GTK_OBJECT(w), "clicked",
148                          GTK_SIGNAL_FUNC(click), (gpointer)i);
149       if (ff & f_ok)
150         gtk_widget_grab_default(w);
151       if (ff & f_cancel)
152         cancel(GTK_WINDOW(dbox), w);
153       gtk_widget_show(w);
154     }
155     free(p);
156
157     /* --- Preflight checklist --- */
158
159     gtk_widget_realize(dbox);
160     if (f & f_mdwfocus)
161       mdwfocus(dbox);
162
163 #undef f_ok
164 #undef f_cancel
165 #undef f_mdwfocus
166   }
167
168   /* --- Ready --- */
169
170   gtk_grab_add(GTK_WIDGET(dbox));
171   gtk_widget_show(dbox);
172   gtk_main();
173   gtk_grab_remove(GTK_WIDGET(dbox));
174   gtk_widget_destroy(dbox);
175   return (reply);
176 }
177
178 /*----- That's all, folks -------------------------------------------------*/