1 /*
2 * $Id: tuidemo.c,v 1.22 2008/07/14 12:35:23 wmcbrine Exp $
3 *
4 * Author : P.J. Kunst <[email protected]>
5 * Date : 25-02-93
6 *
7 * Purpose: This program demonstrates the use of the 'curses' library
8 * for the creation of (simple) menu-operated programs.
9 * In the PDCurses version, use is made of colors for the
10 * highlighting of subwindows (title bar, status bar etc).
11 *
12 * Acknowledgement: some ideas were borrowed from Mark Hessling's
13 * version of the 'testcurs' program.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <locale.h>
20 #include "tui.h"
21
22 /* change this if source at other location */
23
24 #ifdef XCURSES
25 # define FNAME "../demos/tui.c"
26 #else
27 # define FNAME "..\\demos\\tui.c"
28 #endif
29
30 /**************************** strings entry box ***************************/
31
address(void)32 void address(void)
33 {
34 char *fieldname[6] =
35 {
36 "Name", "Street", "City", "State", "Country", (char *)0
37 };
38
39 char *fieldbuf[5];
40 WINDOW *wbody = bodywin();
41 int i, field = 50;
42
43 for (i = 0; i < 5; i++)
44 fieldbuf[i] = calloc(1, field + 1);
45
46 if (getstrings(fieldname, fieldbuf, field) != KEY_ESC)
47 {
48 for (i = 0; fieldname[i]; i++)
49 wprintw(wbody, "%10s : %s\n",
50 fieldname[i], fieldbuf[i]);
51
52 wrefresh(wbody);
53 }
54
55 for (i = 0; i < 5; i++)
56 free(fieldbuf[i]);
57 }
58
59 /**************************** string entry box ****************************/
60
getfname(char * desc,char * fname,int field)61 char *getfname(char *desc, char *fname, int field)
62 {
63 char *fieldname[2];
64 char *fieldbuf[1];
65
66 fieldname[0] = desc;
67 fieldname[1] = 0;
68 fieldbuf[0] = fname;
69
70 return (getstrings(fieldname, fieldbuf, field) == KEY_ESC) ? NULL : fname;
71 }
72
73 /**************************** a very simple file browser ******************/
74
showfile(char * fname)75 void showfile(char *fname)
76 {
77 int i, bh = bodylen();
78 FILE *fp;
79 char buf[MAXSTRLEN];
80 bool ateof = FALSE;
81
82 statusmsg("FileBrowser: Hit key to continue, Q to quit");
83
84 if ((fp = fopen(fname, "r")) != NULL) /* file available? */
85 {
86 while (!ateof)
87 {
88 clsbody();
89
90 for (i = 0; i < bh - 1 && !ateof; i++)
91 {
92 buf[0] = '\0';
93 fgets(buf, MAXSTRLEN, fp);
94
95 if (strlen(buf))
96 bodymsg(buf);
97 else
98 ateof = TRUE;
99 }
100
101 switch (waitforkey())
102 {
103 case 'Q':
104 case 'q':
105 case 0x1b:
106 ateof = TRUE;
107 }
108 }
109
110 fclose(fp);
111 }
112 else
113 {
114 sprintf(buf, "ERROR: file '%s' not found", fname);
115 errormsg(buf);
116 }
117 }
118
119 /***************************** forward declarations ***********************/
120
121 void sub0(void), sub1(void), sub2(void), sub3(void);
122 void func1(void), func2(void);
123 void subfunc1(void), subfunc2(void);
124 void subsub(void);
125
126 /***************************** menus initialization ***********************/
127
128 menu MainMenu[] =
129 {
130 { "Asub", sub0, "Go inside first submenu" },
131 { "Bsub", sub1, "Go inside second submenu" },
132 { "Csub", sub2, "Go inside third submenu" },
133 { "Dsub", sub3, "Go inside fourth submenu" },
134 { "", (FUNC)0, "" } /* always add this as the last item! */
135 };
136
137 menu SubMenu0[] =
138 {
139 { "Exit", DoExit, "Terminate program" },
140 { "", (FUNC)0, "" }
141 };
142
143 menu SubMenu1[] =
144 {
145 { "OneBeep", func1, "Sound one beep" },
146 { "TwoBeeps", func2, "Sound two beeps" },
147 { "", (FUNC)0, "" }
148 };
149
150 menu SubMenu2[] =
151 {
152 { "Browse", subfunc1, "Source file lister" },
153 { "Input", subfunc2, "Interactive file lister" },
154 { "Address", address, "Get address data" },
155 { "", (FUNC)0, "" }
156 };
157
158 menu SubMenu3[] =
159 {
160 { "SubSub", subsub, "Go inside sub-submenu" },
161 { "", (FUNC)0, "" }
162 };
163
164 /***************************** main menu functions ************************/
165
sub0(void)166 void sub0(void)
167 {
168 domenu(SubMenu0);
169 }
170
sub1(void)171 void sub1(void)
172 {
173 domenu(SubMenu1);
174 }
175
sub2(void)176 void sub2(void)
177 {
178 domenu(SubMenu2);
179 }
180
sub3(void)181 void sub3(void)
182 {
183 domenu(SubMenu3);
184 }
185
186 /***************************** submenu1 functions *************************/
187
func1(void)188 void func1(void)
189 {
190 beep();
191 bodymsg("One beep! ");
192 }
193
func2(void)194 void func2(void)
195 {
196 beep();
197 bodymsg("Two beeps! ");
198 beep();
199 }
200
201 /***************************** submenu2 functions *************************/
202
subfunc1(void)203 void subfunc1(void)
204 {
205 showfile(FNAME);
206 }
207
subfunc2(void)208 void subfunc2(void)
209 {
210 char fname[MAXSTRLEN];
211
212 strcpy(fname, FNAME);
213 if (getfname ("File to browse:", fname, 50))
214 showfile(fname);
215 }
216
217 /***************************** submenu3 functions *************************/
218
subsub(void)219 void subsub(void)
220 {
221 domenu(SubMenu2);
222 }
223
224 /***************************** start main menu ***************************/
225
main(int argc,char ** argv)226 int main(int argc, char **argv)
227 {
228 setlocale(LC_ALL, "");
229
230 startmenu(MainMenu, "TUI - 'textual user interface' demonstration program");
231
232 return 0;
233 }
234