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
;
111 dstr msgbuf
= DSTR_INIT
;
118 gtk_init(&argc
, &argv
);
120 /* --- Parse options --- */
124 static struct option opt
[] = {
125 { "help", 0, 0, 'h' },
126 { "usage", 0, 0, 'u' },
127 { "version", 0, 0, 'v' },
128 { "focus", 0, 0, 'f' },
129 { "title", OPTF_ARGREQ
, 0, 't' },
130 { "cancel", OPTF_ARGREQ
, 0, 'c' },
131 { "default", OPTF_ARGREQ
, 0, 'd' },
136 i
= getopt_long(argc
, argv
, "huv t:c:d:q", opt
, 0);
148 "Pops up a message box containing a message and some buttons, reporting\n"
149 "which button was selected.\n"
151 "Options available are:\n"
153 "-h, --help Display this help text\n"
154 "-u, --usage Display a short usage summary\n"
155 "-v, --version Display the program's version number\n"
157 "-f, --focus Give the window the focus (obsolete mdw thing)\n"
158 "-t, --title=TITLE Select the title string in the message box\n"
159 "-c, --cancel=BUTTON Select which button is to have the Cancel action\n"
160 "-d, --default=BUTTON Select which button is the default\n",
192 if (optind
>= argc
) {
196 message
= argv
[optind
++];
199 else if (strcmp(message
, "-") == 0) {
203 dstr_ensure(&msgbuf
, 4096);
204 n
= fread(msgbuf
.buf
+ msgbuf
.len
, 1,
205 msgbuf
.sz
- msgbuf
.len
, stdin
);
210 if (msgbuf
.len
&& msgbuf
.buf
[msgbuf
.len
- 1])
213 message
= msgbuf
.buf
;
216 if (optind
>= argc
) {
221 DA_UNSAFE_EXTEND(&bv
, 1);
222 } else for (; optind
< argc
; optind
++) {
224 b
= &DA(&bv
)[DA_LEN(&bv
)];
226 b
->text
= argv
[optind
];
227 DA_UNSAFE_EXTEND(&bv
, 1);
230 if ((b
= findbutton(&bv
, b_cancel
)) != 0)
233 DA(&bv
)[DA_LEN(&bv
) - 1].f
|= f_cancel
;
235 if ((b
= findbutton(&bv
, b_default
)) != 0)
238 DA(&bv
)[0].f
|= f_default
;
244 for (i
= 0; i
< n
; i
++) {
245 if (b
[i
].f
& f_default
)
247 if (b
[i
].f
& f_cancel
)
249 DPUTS(&d
, b
[i
].text
);
254 i
= msg(title
, d
.buf
, "%s", message
);
260 /*----- That's all, folks -------------------------------------------------*/