chiark / gitweb /
More Debian fixes.
[mgLib] / msg.c
CommitLineData
1c48db0c 1/* -*-c-*-
2 *
ca8a1a25 3 * $Id: msg.c,v 1.10 2002/01/13 14:33:43 mdw Exp $
1c48db0c 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
19c0192c 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.
1c48db0c 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
19c0192c 22 * GNU Library General Public License for more details.
1c48db0c 23 *
19c0192c 24 * You should have received a copy of the GNU Library General Public
e962a3e2 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.
1c48db0c 28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: msg.c,v $
ca8a1a25 33 * Revision 1.10 2002/01/13 14:33:43 mdw
34 * Track @dstr_vputf@ change.
35 *
d1495df3 36 * Revision 1.9 2002/01/13 14:32:22 mdw
37 * Allow control over the message box title.
38 *
ff3a8b71 39 * Revision 1.8 1999/11/11 19:54:37 mdw
40 * Prep for standalone release.
41 *
084b2d62 42 * Revision 1.7 1999/05/21 22:08:20 mdw
43 * Take advantage of new dynamic string macros.
44 *
e962a3e2 45 * Revision 1.6 1999/05/06 19:51:48 mdw
46 * Reformatted the LGPL notice a little bit.
47 *
19c0192c 48 * Revision 1.5 1999/05/05 18:52:45 mdw
49 * Change licensing conditions to LGPL.
50 *
ed2737aa 51 * Revision 1.4 1999/04/29 20:48:13 mdw
52 * Add documentation for `msg'.
53 *
8e774312 54 * Revision 1.3 1999/03/25 23:36:10 mdw
55 * Compile to nothing in absence of GTK, for the benefit of parent packages
56 * which contain non-GTK-dependent parts.
57 *
f2189185 58 * Revision 1.2 1998/12/15 23:48:06 mdw
59 * Use `dstr_putf' for formatting, rather than `sprintf'.
60 *
1c48db0c 61 * Revision 1.1 1998/12/11 09:44:21 mdw
62 * Initial version.
63 *
64 */
65
66/*----- Header files ------------------------------------------------------*/
67
68#include <stdarg.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72
73#include <gtk/gtk.h>
74
75#include <mLib/alloc.h>
f2189185 76#include <mLib/dstr.h>
1c48db0c 77
78#include "cancel.h"
79#include "mdwfocus.h"
80#include "msg.h"
81
82/*----- Static variables --------------------------------------------------*/
83
84static int reply;
85static int creply;
86
87/*----- Main code ---------------------------------------------------------*/
88
89/* --- @msg@ --- *
90 *
d1495df3 91 * Arguments: @const char *title@ = the title for the message box
92 * @const char *buttons@ = the button strings to display
93 * @const char *msg@ = the message skeleton string
1c48db0c 94 *
95 * Returns: Index of the button selected.
96 *
97 * Use: Displays a message to the user in a nice dialogue box and
98 * returns the index of the button selected.
ed2737aa 99 *
100 * The @msg@ argument is a @printf@-style format string, which
101 * contains the message to actually be shown. The @buttons@
102 * argument is a comma-separated list of buttons to be drawn,
103 * from right to left. A button name can be preceded with `:'
104 * to indicate that it's the default, or `~' if it's the
105 * `cancel' button. The return value is the zero-based index
106 * of the button selected.
1c48db0c 107 */
108
109static int close(GtkWidget *w, gpointer p)
110{
111 reply = creply;
112 gtk_main_quit();
113 return (1);
114}
115
116static void click(GtkWidget *w, gpointer p)
117{
118 reply = (int)p;
119 gtk_main_quit();
120}
121
d1495df3 122int msg(const char *title, const char *buttons, const char *msg, ...)
1c48db0c 123{
124 GtkWidget *dbox, *w;
125
126 /* --- Make most of the dialogue box --- */
127
128 dbox = gtk_dialog_new();
d1495df3 129 gtk_window_set_title(GTK_WINDOW(dbox), title);
1c48db0c 130 gtk_signal_connect(GTK_OBJECT(dbox), "delete_event",
131 GTK_SIGNAL_FUNC(close), 0);
132 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(dbox)->action_area), 0);
133
134 /* --- Set up the message string --- */
135
136 {
084b2d62 137 dstr d = DSTR_INIT;
1c48db0c 138 va_list ap;
139
140 va_start(ap, msg);
ca8a1a25 141 dstr_vputf(&d, msg, &ap);
1c48db0c 142 va_end(ap);
f2189185 143 w = gtk_label_new(d.buf);
1c48db0c 144 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dbox)->vbox), w, 1, 1, 0);
145 gtk_window_position(GTK_WINDOW(dbox), GTK_WIN_POS_MOUSE);
146 gtk_widget_show(w);
147 gtk_misc_set_padding(GTK_MISC(w), 16, 16);
f2189185 148 dstr_destroy(&d);
1c48db0c 149 }
150
151 /* --- Set up the buttons --- */
152
153 {
154 char *p = xstrdup(buttons);
155 unsigned f = 0;
156 int i = 0;
157
d1495df3 158#define f_ok 1u
159#define f_cancel 2u
160#define f_mdwfocus 4u
1c48db0c 161
162 if (*p == '!') {
163 f |= f_mdwfocus;
164 p++;
165 }
166
167 creply = -1;
168
169 for (p = strtok(p, ","); p; p = strtok(0, ","), i++) {
170 unsigned ff = 0;
171 if (*p == ':') {
172 ff |= f_ok;
173 p++;
174 }
175 if (*p == '~') {
176 ff |= f_cancel;
177 creply = i;
178 p++;
179 }
180 w = gtk_button_new_with_label(p);
181 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
182 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dbox)->action_area), w, 0, 0, 0);
183 gtk_signal_connect(GTK_OBJECT(w), "clicked",
184 GTK_SIGNAL_FUNC(click), (gpointer)i);
185 if (ff & f_ok)
186 gtk_widget_grab_default(w);
187 if (ff & f_cancel)
188 cancel(GTK_WINDOW(dbox), w);
189 gtk_widget_show(w);
190 }
f2189185 191 free(p);
192
193 /* --- Preflight checklist --- */
1c48db0c 194
195 gtk_widget_realize(dbox);
196 if (f & f_mdwfocus)
197 mdwfocus(dbox);
f2189185 198
d1495df3 199#undef f_ok
200#undef f_cancel
201#undef f_mdwfocus
1c48db0c 202 }
203
204 /* --- Ready --- */
205
206 gtk_grab_add(GTK_WIDGET(dbox));
207 gtk_widget_show(dbox);
208 gtk_main();
209 gtk_grab_remove(GTK_WIDGET(dbox));
210 gtk_widget_destroy(dbox);
211 return (reply);
212}
213
214/*----- That's all, folks -------------------------------------------------*/