debian: Fix package sections.
[mgLib] / msg.c
CommitLineData
1c48db0c 1/* -*-c-*-
2 *
eae4bac6 3 * $Id$
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
1c48db0c 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>
f2189185 40#include <mLib/dstr.h>
1c48db0c 41
42#include "cancel.h"
43#include "mdwfocus.h"
44#include "msg.h"
45
46/*----- Static variables --------------------------------------------------*/
47
48static int reply;
49static int creply;
50
51/*----- Main code ---------------------------------------------------------*/
52
53/* --- @msg@ --- *
54 *
d1495df3 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
1c48db0c 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.
ed2737aa 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.
1c48db0c 71 */
72
73static int close(GtkWidget *w, gpointer p)
74{
75 reply = creply;
76 gtk_main_quit();
77 return (1);
78}
79
80static void click(GtkWidget *w, gpointer p)
81{
82 reply = (int)p;
83 gtk_main_quit();
84}
85
d1495df3 86int msg(const char *title, const char *buttons, const char *msg, ...)
1c48db0c 87{
88 GtkWidget *dbox, *w;
89
90 /* --- Make most of the dialogue box --- */
91
92 dbox = gtk_dialog_new();
d1495df3 93 gtk_window_set_title(GTK_WINDOW(dbox), title);
1c48db0c 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 {
084b2d62 101 dstr d = DSTR_INIT;
1c48db0c 102 va_list ap;
103
104 va_start(ap, msg);
ca8a1a25 105 dstr_vputf(&d, msg, &ap);
1c48db0c 106 va_end(ap);
f2189185 107 w = gtk_label_new(d.buf);
eae4bac6 108 gtk_label_set_justify(GTK_LABEL(w), GTK_JUSTIFY_LEFT);
1c48db0c 109 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dbox)->vbox), w, 1, 1, 0);
110 gtk_window_position(GTK_WINDOW(dbox), GTK_WIN_POS_MOUSE);
111 gtk_widget_show(w);
112 gtk_misc_set_padding(GTK_MISC(w), 16, 16);
f2189185 113 dstr_destroy(&d);
1c48db0c 114 }
115
116 /* --- Set up the buttons --- */
117
118 {
119 char *p = xstrdup(buttons);
120 unsigned f = 0;
121 int i = 0;
122
d1495df3 123#define f_ok 1u
124#define f_cancel 2u
125#define f_mdwfocus 4u
1c48db0c 126
127 if (*p == '!') {
128 f |= f_mdwfocus;
129 p++;
130 }
131
132 creply = -1;
133
134 for (p = strtok(p, ","); p; p = strtok(0, ","), i++) {
135 unsigned ff = 0;
136 if (*p == ':') {
137 ff |= f_ok;
138 p++;
139 }
140 if (*p == '~') {
141 ff |= f_cancel;
142 creply = i;
143 p++;
144 }
145 w = gtk_button_new_with_label(p);
146 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
147 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dbox)->action_area), w, 0, 0, 0);
148 gtk_signal_connect(GTK_OBJECT(w), "clicked",
149 GTK_SIGNAL_FUNC(click), (gpointer)i);
150 if (ff & f_ok)
151 gtk_widget_grab_default(w);
152 if (ff & f_cancel)
153 cancel(GTK_WINDOW(dbox), w);
154 gtk_widget_show(w);
155 }
dcfeaab3 156 xfree(p);
f2189185 157
158 /* --- Preflight checklist --- */
1c48db0c 159
160 gtk_widget_realize(dbox);
161 if (f & f_mdwfocus)
162 mdwfocus(dbox);
f2189185 163
d1495df3 164#undef f_ok
165#undef f_cancel
166#undef f_mdwfocus
1c48db0c 167 }
168
169 /* --- Ready --- */
170
171 gtk_grab_add(GTK_WIDGET(dbox));
172 gtk_widget_show(dbox);
173 gtk_main();
174 gtk_grab_remove(GTK_WIDGET(dbox));
175 gtk_widget_destroy(dbox);
176 return (reply);
177}
178
179/*----- That's all, folks -------------------------------------------------*/