3 * $Id: xmsg.c,v 1.2 2004/04/08 01:36:29 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 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/darray.h>
38 #include <mLib/dstr.h>
39 #include <mLib/mdwopt.h>
40 #include <mLib/quis.h>
41 #include <mLib/report.h>
43 #include <mgLib/msg.h>
45 /*----- Data structures ---------------------------------------------------*/
47 typedef struct button {
55 DA_DECL(button_v, button);
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @version@ --- */
61 static void version(FILE *fp)
63 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
68 static void usage(FILE *fp)
70 fprintf(fp, "Usage: %s [-f] [-t TITLE] [-c|d BUTTON] MSG [BUTTON...]\n",
74 /* --- @findbutton@ --- */
76 static button *findbutton(button_v *bv, const char *tag)
78 size_t i, n = DA_LEN(bv);
84 for (i = 0; i < n; i++) {
85 if (strcmp(b[i].text, tag) == 0)
88 while (*tag && isspace((unsigned char)*tag))
90 i = strtoul(tag, &q, 0);
93 die(EXIT_FAILURE, "unknown button `%s'", tag);
103 int main(int argc, char *argv[])
107 const char *b_cancel = 0, *b_default = 0;
108 button_v bv = DA_INIT;
117 gtk_init(&argc, &argv);
119 /* --- Parse options --- */
123 static struct option opt[] = {
124 { "help", 0, 0, 'h' },
125 { "usage", 0, 0, 'u' },
126 { "version", 0, 0, 'v' },
127 { "focus", 0, 0, 'f' },
128 { "title", OPTF_ARGREQ, 0, 't' },
129 { "cancel", OPTF_ARGREQ, 0, 'c' },
130 { "default", OPTF_ARGREQ, 0, 'd' },
135 i = getopt_long(argc, argv, "huv t:c:d:q", opt, 0);
147 "Pops up a message box containing a message and some buttons, reporting\n"
148 "which button was selected.\n"
150 "Options available are:\n"
152 "-h, --help Display this help text\n"
153 "-u, --usage Display a short usage summary\n"
154 "-v, --version Display the program's version number\n"
156 "-f, --focus Give the window the focus (obsolete mdw thing)\n"
157 "-t, --title=TITLE Select the title string in the message box\n"
158 "-c, --cancel=BUTTON Select which button is to have the Cancel action\n"
159 "-d, --default=BUTTON Select which button is the default\n",
191 if (optind >= argc) {
195 message = argv[optind++];
197 if (optind >= argc) {
202 DA_UNSAFE_EXTEND(&bv, 1);
203 } else for (; optind < argc; optind++) {
205 b = &DA(&bv)[DA_LEN(&bv)];
207 b->text = argv[optind];
208 DA_UNSAFE_EXTEND(&bv, 1);
211 if ((b = findbutton(&bv, b_cancel)) != 0)
214 DA(&bv)[DA_LEN(&bv) - 1].f |= f_cancel;
216 if ((b = findbutton(&bv, b_default)) != 0)
219 DA(&bv)[0].f |= f_default;
225 for (i = 0; i < n; i++) {
226 if (b[i].f & f_default)
228 if (b[i].f & f_cancel)
230 DPUTS(&d, b[i].text);
235 i = msg(title, d.buf, "%s", message);
241 /*----- That's all, folks -------------------------------------------------*/