1 /* $Id: cdkradio.c,v 1.13 2016/12/04 15:22:16 tom Exp $ */
2
3 #include <cdk_test.h>
4
5 #ifdef XCURSES
6 char *XCursesProgramName = "cdkradio";
7 #endif
8
9 /*
10 * Declare file local prototypes.
11 */
12 static int widgetCB (EObjectType cdktype, void *object, void *clientData, chtype key);
13
14 /*
15 * Define file local variables.
16 */
17 static const char *FPUsage = "-l List | -f filename [-c Choice Character] [-d Default Item] [-s Scroll Bar Position] [-n Numbers] [-i Item Index] [-T Title] [-B Buttons] [-O Output File] [-X X Position] [-Y Y Position] [-H Height] [-W Width] [-N] [-S]";
18
19 /*
20 *
21 */
main(int argc,char ** argv)22 int main (int argc, char **argv)
23 {
24 /* *INDENT-EQLS* */
25 CDKSCREEN *cdkScreen = 0;
26 CDKRADIO *widget = 0;
27 CDKBUTTONBOX *buttonWidget = 0;
28 char *buttons = 0;
29 char *CDK_WIDGET_COLOR = 0;
30 char *temp = 0;
31 chtype *holder = 0;
32 chtype *choiceChar = 0;
33 int answer = 0;
34 int spos = NONE;
35 int scrollLines = -1;
36 int buttonCount = 0;
37 int selection = 0;
38 int shadowHeight = 0;
39 FILE *fp = stderr;
40 char **buttonList = 0;
41 char **scrollList = 0;
42 int j1, j2;
43
44 CDK_PARAMS params;
45 boolean boxWidget;
46 boolean numberOutput;
47 boolean shadowWidget;
48 const char *choice;
49 char *filename;
50 char *list;
51 char *outputFile;
52 char *title;
53 int defaultItem;
54 int height;
55 int width;
56 int xpos;
57 int ypos;
58
59 CDKparseParams (argc, argv, ¶ms, "l:f:s:c:d:iB:O:T:" CDK_CLI_PARAMS);
60
61 /* *INDENT-EQLS* */
62 xpos = CDKparamValue (¶ms, 'X', CENTER);
63 ypos = CDKparamValue (¶ms, 'Y', CENTER);
64 height = CDKparamValue (¶ms, 'H', 10);
65 width = CDKparamValue (¶ms, 'W', 1);
66 boxWidget = CDKparamValue (¶ms, 'N', TRUE);
67 shadowWidget = CDKparamValue (¶ms, 'S', FALSE);
68 defaultItem = CDKparamValue (¶ms, 'd', 0);
69 numberOutput = CDKparamValue (¶ms, 'i', FALSE);
70 filename = CDKparamString (¶ms, 'f');
71 list = CDKparamString (¶ms, 'l');
72 buttons = CDKparamString (¶ms, 'B');
73 outputFile = CDKparamString (¶ms, 'O');
74 title = CDKparamString (¶ms, 'T');
75
76 if ((choice = CDKparamString (¶ms, 'c')) == 0)
77 choice = "</R>X";
78
79 spos = CDKparsePosition (CDKparamString (¶ms, 's'));
80
81 /* If the user asked for an output file, try to open it. */
82 if (outputFile != 0)
83 {
84 if ((fp = fopen (outputFile, "w")) == 0)
85 {
86 fprintf (stderr, "%s: Can not open output file %s\n", argv[0], outputFile);
87 ExitProgram (CLI_ERROR);
88 }
89 }
90
91 /* Did they provide a list of items. */
92 if (list == 0)
93 {
94 /* Maybe they gave a filename to use to read. */
95 if (filename != 0)
96 {
97 /* Read the file in. */
98 scrollLines = CDKreadFile (filename, &scrollList);
99
100 /* Check if there was an error. */
101 if (scrollLines == -1)
102 {
103 fprintf (stderr, "Error: Could not open the file '%s'.\n", filename);
104 ExitProgram (CLI_ERROR);
105 }
106 }
107 else
108 {
109 /* They didn't provide anything. */
110 fprintf (stderr, "Usage: %s %s\n", argv[0], FPUsage);
111 ExitProgram (CLI_ERROR);
112 }
113 }
114 else
115 {
116 /* Split the scroll lines up. */
117 scrollList = CDKsplitString (list, '\n');
118 scrollLines = (int)CDKcountStrings ((CDK_CSTRING2) scrollList);
119 }
120
121 cdkScreen = initCDKScreen (NULL);
122
123 /* Start color. */
124 initCDKColor ();
125
126 /* Check if the user wants to set the background of the main screen. */
127 if ((temp = getenv ("CDK_SCREEN_COLOR")) != 0)
128 {
129 holder = char2Chtype (temp, &j1, &j2);
130 wbkgd (cdkScreen->window, holder[0]);
131 wrefresh (cdkScreen->window);
132 freeChtype (holder);
133 }
134
135 /* Get the widget color background color. */
136 if ((CDK_WIDGET_COLOR = getenv ("CDK_WIDGET_COLOR")) == 0)
137 {
138 CDK_WIDGET_COLOR = 0;
139 }
140
141 /* Convert the char * choiceChar to a chtype * */
142 choiceChar = char2Chtype (choice, &j1, &j2);
143
144 /* Create the scrolling list. */
145 widget = newCDKRadio (cdkScreen, xpos, ypos, spos,
146 height, width, title,
147 (CDK_CSTRING2) scrollList, scrollLines,
148 choiceChar[0], defaultItem,
149 A_REVERSE,
150 boxWidget, shadowWidget);
151 free (choiceChar);
152
153 /* Make sure we could create the widget. */
154 if (widget == 0)
155 {
156 CDKfreeStrings (scrollList);
157
158 destroyCDKScreen (cdkScreen);
159 endCDK ();
160
161 fprintf (stderr,
162 "Error: Could not create the radio list. "
163 "Is the window too small?\n");
164
165 ExitProgram (CLI_ERROR);
166 }
167
168 /* Split the buttons if they supplied some. */
169 if (buttons != 0)
170 {
171 /* Split the button list up. */
172 buttonList = CDKsplitString (buttons, '\n');
173 buttonCount = (int)CDKcountStrings ((CDK_CSTRING2) buttonList);
174
175 /* We need to create a buttonbox widget. */
176 buttonWidget = newCDKButtonbox (cdkScreen,
177 getbegx (widget->win),
178 (getbegy (widget->win)
179 + widget->boxHeight - 1),
180 1, widget->boxWidth - 1,
181 NULL, 1, buttonCount,
182 (CDK_CSTRING2) buttonList, buttonCount,
183 A_REVERSE, boxWidget, FALSE);
184 CDKfreeStrings (buttonList);
185
186 setCDKButtonboxULChar (buttonWidget, ACS_LTEE);
187 setCDKButtonboxURChar (buttonWidget, ACS_RTEE);
188
189 /*
190 * We need to set the lower left and right
191 * characters of the widget.
192 */
193 setCDKRadioLLChar (widget, ACS_LTEE);
194 setCDKRadioLRChar (widget, ACS_RTEE);
195
196 /*
197 * Bind the Tab key in the widget to send a
198 * Tab key to the button box widget.
199 */
200 bindCDKObject (vRADIO, widget, KEY_TAB, widgetCB, buttonWidget);
201 bindCDKObject (vRADIO, widget, CDK_NEXT, widgetCB, buttonWidget);
202 bindCDKObject (vRADIO, widget, CDK_PREV, widgetCB, buttonWidget);
203
204 /* Check if the user wants to set the background of the widget. */
205 setCDKButtonboxBackgroundColor (buttonWidget, CDK_WIDGET_COLOR);
206
207 /* Draw the button widget. */
208 drawCDKButtonbox (buttonWidget, boxWidget);
209 }
210
211 /*
212 * If the user asked for a shadow, we need to create one. Do this instead
213 * of using the shadow parameter because the button widget is not part of
214 * the main widget and if the user asks for both buttons and a shadow, we
215 * need to create a shadow big enough for both widgets. Create the shadow
216 * window using the widgets shadowWin element, so screen refreshes will draw
217 * them as well.
218 */
219 if (shadowWidget == TRUE)
220 {
221 /* Determine the height of the shadow window. */
222 shadowHeight = (buttonWidget == (CDKBUTTONBOX *)NULL ?
223 widget->boxHeight :
224 widget->boxHeight + buttonWidget->boxHeight - 1);
225
226 /* Create the shadow window. */
227 widget->shadowWin = newwin (shadowHeight,
228 widget->boxWidth,
229 getbegy (widget->win) + 1,
230 getbegx (widget->win) + 1);
231
232 if (widget->shadowWin != 0)
233 {
234 widget->shadow = TRUE;
235
236 /*
237 * We force the widget and buttonWidget to be drawn so the
238 * buttonbox widget will be drawn when the widget is activated.
239 * Otherwise the shadow window will draw over the button widget.
240 */
241 drawCDKRadio (widget, ObjOf (widget)->box);
242 eraseCDKButtonbox (buttonWidget);
243 drawCDKButtonbox (buttonWidget, ObjOf (buttonWidget)->box);
244 }
245 }
246
247 /* Check if the user wants to set the background of the widget. */
248 setCDKRadioBackgroundColor (widget, CDK_WIDGET_COLOR);
249
250 /* Activate the scrolling list. */
251 answer = activateCDKRadio (widget, 0);
252
253 /* If there were buttons, get the button selected. */
254 if (buttonWidget != 0)
255 {
256 selection = buttonWidget->currentButton;
257 destroyCDKButtonbox (buttonWidget);
258 }
259
260 /* Shut down curses. */
261 destroyCDKRadio (widget);
262 destroyCDKScreen (cdkScreen);
263 endCDK ();
264
265 /* Print out the answer. */
266 if (answer >= 0)
267 {
268 if (numberOutput == TRUE)
269 {
270 fprintf (fp, "%d\n", answer);
271 }
272 else
273 {
274 fprintf (fp, "%s\n", scrollList[answer]);
275 }
276 }
277 fclose (fp);
278
279 CDKfreeStrings (scrollList);
280
281 /* Exit with the button selected. */
282 ExitProgram (selection);
283 }
284
widgetCB(EObjectType cdktype GCC_UNUSED,void * object GCC_UNUSED,void * clientData,chtype key)285 static int widgetCB (EObjectType cdktype GCC_UNUSED,
286 void *object GCC_UNUSED,
287 void *clientData,
288 chtype key)
289 {
290 CDKBUTTONBOX *buttonbox = (CDKBUTTONBOX *)clientData;
291 (void) injectCDKButtonbox (buttonbox, key);
292 return (TRUE);
293 }
294