Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / lib / libdialog / TESTS / check2.c
1 /*
2  * small test-driver for new dialog functionality
3  *
4  * Copyright (c) 1995, Jordan Hubbard
5  *
6  * All rights reserved.
7  *
8  * This source code may be used, modified, copied, distributed, and
9  * sold, in both source and binary form provided that the above
10  * copyright and these terms are retained, verbatim, as the first
11  * lines of this file.  Under no circumstances is the author
12  * responsible for the proper functioning of the software nor does
13  * the author assume any responsibility for damages incurred with
14  * its use.
15  *
16  * $FreeBSD: src/gnu/lib/libdialog/TESTS/check2.c,v 1.6 2000/01/10 11:52:02 phantom Exp $
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/wait.h>
24 #include <dialog.h>
25
26 /* Hook functions */
27
28 static int
29 getBool(dialogMenuItem *self)
30 {
31     if (self->data && *((int *)self->data))
32         return TRUE;
33     return FALSE;
34 }
35
36 static int
37 setBool(dialogMenuItem *self)
38 {
39     if (self->data) {
40         *((int *)self->data) = !*((int *)self->data);
41         return DITEM_SUCCESS;
42     }
43     return DITEM_FAILURE;
44 }
45
46 static int german_book, italian_book, slang_book;
47
48 static int
49 clearBooks(dialogMenuItem *self)
50 {
51     german_book = italian_book = slang_book = FALSE;
52     return DITEM_SUCCESS | DITEM_REDRAW;
53 }
54
55 static int
56 buyBooks(dialogMenuItem *self)
57 {
58     char foo[256];
59     
60     if (german_book || italian_book || slang_book) {
61         strcpy(foo, "Ok, you're buying books on");
62         if (german_book)
63             strcat(foo, " german");
64         if (italian_book)
65             strcat(foo, " italian");
66         if (slang_book)
67             strcat(foo, " slang");
68     }
69     else
70         strcpy(foo, "You're not buying any books?");
71     dialog_mesgbox("This is a direct callback for the `Buy' button", foo, -1, -1);
72     return DITEM_SUCCESS;
73 }
74
75 /* menu3 - Look mom!  We can finally use our own OK and Cancel buttons! */
76 /* prompt       title                                   checked         fire            sel   data */
77 static dialogMenuItem menu3[] = {
78     { "Buy!",   NULL,                                   NULL,           buyBooks        }, /* New "OK" button */
79     { "No Way!",        NULL,                                   NULL,           NULL            }, /* New "Cancel" button */
80     { "German", "Buy books on learning German",         getBool,        setBool,        NULL, &german_book },
81     { "Italian",        "Buy books on learning Italian",        getBool,        setBool,        NULL, &italian_book },
82     { "Slang",  "Buy books on commonly used insults",   getBool,        setBool,        NULL, &slang_book },
83     { "Clear",  "Clear book list",                      NULL,           clearBooks,     NULL, NULL, ' ', ' ', ' ' },
84 };
85
86 /* End of hook functions */
87
88 /* Kick it off, James! */
89 int
90 main(int argc, char **argv)
91 {
92     int retval;
93     
94     init_dialog();
95     
96     retval = dialog_checklist("this is dialog_checklist() in action, test #2",
97                               "Same as before, but now we relabel the buttons and override the OK action.",
98                               -1, -1, 4, -4, menu3 + 2, (char *)TRUE);
99     dialog_clear();
100     fprintf(stderr, "returned value for dialog_checklist was %d\n", retval);
101     
102     end_dialog();
103     return 0;
104 }