3 * $Id: xmsg.c,v 1.1 2002/01/13 14:42:18 mdw Exp $
5 * Display a message to the user
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Edgeware X tools collection.
14 * X tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * X tools 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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with X tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 2002/01/13 14:42:18 mdw
33 * New program to display messages and get answers.
37 /*----- Header files ------------------------------------------------------*/
45 #include <mLib/darray.h>
46 #include <mLib/dstr.h>
47 #include <mLib/mdwopt.h>
48 #include <mLib/quis.h>
49 #include <mLib/report.h>
51 #include <mgLib/msg.h>
53 /*----- Data structures ---------------------------------------------------*/
55 typedef struct button
{
63 DA_DECL(button_v
, button
);
65 /*----- Main code ---------------------------------------------------------*/
67 /* --- @version@ --- */
69 static void version(FILE *fp
)
71 fprintf(fp
, "%s (xtoys version " VERSION
")\n", QUIS
);
76 static void usage(FILE *fp
)
78 fprintf(fp
, "Usage: %s [-f] [-t TITLE] [-c|d BUTTON] MSG [BUTTON...]\n",
82 /* --- @findbutton@ --- */
84 static button
*findbutton(button_v
*bv
, const char *tag
)
86 size_t i
, n
= DA_LEN(bv
);
92 for (i
= 0; i
< n
; i
++) {
93 if (strcmp(b
[i
].text
, tag
) == 0)
96 while (*tag
&& isspace((unsigned char)*tag
))
98 i
= strtoul(tag
, &q
, 0);
101 die(EXIT_FAILURE
, "unknown button `%s'", tag
);
111 int main(int argc
, char *argv
[])
115 const char *b_cancel
= 0, *b_default
= 0;
116 button_v bv
= DA_INIT
;
125 gtk_init(&argc
, &argv
);
127 /* --- Parse options --- */
131 static struct option opt
[] = {
132 { "help", 0, 0, 'h' },
133 { "usage", 0, 0, 'u' },
134 { "version", 0, 0, 'v' },
135 { "focus", 0, 0, 'f' },
136 { "title", OPTF_ARGREQ
, 0, 't' },
137 { "cancel", OPTF_ARGREQ
, 0, 'c' },
138 { "default", OPTF_ARGREQ
, 0, 'd' },
143 i
= getopt_long(argc
, argv
, "huv t:c:d:q", opt
, 0);
155 "Pops up a message box containing a message and some buttons, reporting\n"
156 "which button was selected.\n"
158 "Options available are:\n"
160 "-h, --help Display this help text\n"
161 "-u, --usage Display a short usage summary\n"
162 "-v, --version Display the program's version number\n"
164 "-f, --focus Give the window the focus (obsolete mdw thing)\n"
165 "-t, --title=TITLE Select the title string in the message box\n"
166 "-c, --cancel=BUTTON Select which button is to have the Cancel action\n"
167 "-d, --default=BUTTON Select which button is the default\n",
199 if (optind
>= argc
) {
203 message
= argv
[optind
++];
205 if (optind
>= argc
) {
210 DA_UNSAFE_EXTEND(&bv
, 1);
211 } else for (; optind
< argc
; optind
++) {
213 b
= &DA(&bv
)[DA_LEN(&bv
)];
215 b
->text
= argv
[optind
];
216 DA_UNSAFE_EXTEND(&bv
, 1);
219 if ((b
= findbutton(&bv
, b_cancel
)) != 0)
222 DA(&bv
)[DA_LEN(&bv
) - 1].f
|= f_cancel
;
224 if ((b
= findbutton(&bv
, b_default
)) != 0)
227 DA(&bv
)[0].f
|= f_default
;
233 for (i
= 0; i
< n
; i
++) {
234 if (b
[i
].f
& f_default
)
236 if (b
[i
].f
& f_cancel
)
238 DPUTS(&d
, b
[i
].text
);
243 i
= msg(title
, d
.buf
, "%s", message
);
249 /*----- That's all, folks -------------------------------------------------*/