aboutsummaryrefslogtreecommitdiffstats
path: root/menuselect/menuselect_newt.c
blob: 3aad4655067c741c2f77f7d7e2f89f42560f70f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2008 Sean Bright
 *
 * Sean Bright <sean.bright@gmail.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*
 * \file
 *
 * \author Sean Bright <sean.bright@gmail.com>
 * 
 * \brief newt frontend for selection maintenance
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <newt.h>

#include "menuselect.h"

#define MIN_X 80
#define MIN_Y 21

extern int changes_made;

static newtComponent rootOptions;
static newtComponent subOptions;

static newtComponent memberNameTextbox;
static newtComponent dependsLabel;
static newtComponent usesLabel;
static newtComponent conflictsLabel;
static newtComponent dependsDataTextbox;
static newtComponent usesDataTextbox;
static newtComponent conflictsDataTextbox;

static newtComponent exitButton;
static newtComponent saveAndExitButton;

static void build_members_menu(int overlay);
static void root_menu_callback(newtComponent component, void *data);

static void toggle_all_options(int select)
{
	struct category *cat = newtListboxGetCurrent(rootOptions);

	set_all(cat, select);

	/* Redraw */
	build_members_menu(1);

	return;
}

static void toggle_selected_option()
{
	struct member *mem = newtListboxGetCurrent(subOptions);

	toggle_enabled(mem);

	/* Redraw */
	build_members_menu(1);

	return;
}

static void reset_display()
{
	newtTextboxSetText(memberNameTextbox, "");
	newtTextboxSetText(dependsDataTextbox, "");
	newtTextboxSetText(usesDataTextbox, "");
	newtTextboxSetText(conflictsDataTextbox, "");
	newtRefresh();
}

static void display_member_info(struct member *mem)
{
	char buffer[128] = { 0 };

	struct depend *dep;
	struct conflict *con;
	struct use *uses;

	reset_display();

	if (mem->displayname) {
		newtTextboxSetText(memberNameTextbox, mem->displayname);
	}

	if (AST_LIST_EMPTY(&mem->deps)) {
		newtTextboxSetText(dependsDataTextbox, "N/A");
	} else {
		strcpy(buffer, "");
		AST_LIST_TRAVERSE(&mem->deps, dep, list) {
			strncat(buffer, dep->displayname, sizeof(buffer) - strlen(buffer) - 1);
			strncat(buffer, dep->member ? "(M)" : "(E)", sizeof(buffer) - strlen(buffer) - 1);
			if (AST_LIST_NEXT(dep, list))
				strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1);
		}
		newtTextboxSetText(dependsDataTextbox, buffer);
	}

	if (AST_LIST_EMPTY(&mem->uses)) {
		newtTextboxSetText(usesDataTextbox, "N/A");
	} else {
		strcpy(buffer, "");
		AST_LIST_TRAVERSE(&mem->uses, uses, list) {
			strncat(buffer, uses->displayname, sizeof(buffer) - strlen(buffer) - 1);
			if (AST_LIST_NEXT(uses, list))
				strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1);
		}
		newtTextboxSetText(usesDataTextbox, buffer);
	}

	if (AST_LIST_EMPTY(&mem->conflicts)) {
		newtTextboxSetText(conflictsDataTextbox, "N/A");
	} else {
		strcpy(buffer, "");
		AST_LIST_TRAVERSE(&mem->conflicts, con, list) {
			strncat(buffer, con->displayname, sizeof(buffer) - strlen(buffer) - 1);
			strncat(buffer, con->member ? "(M)" : "(E)", sizeof(buffer) - strlen(buffer) - 1);
			if (AST_LIST_NEXT(con, list))
				strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1);
		}
		newtTextboxSetText(conflictsDataTextbox, buffer);
	}

	return;
}

static void build_members_menu(int overlay)
{
	struct category *cat;
	struct member *mem;
	char buf[64];
	int i = 0;

	if (!overlay) {
		reset_display();
		newtListboxClear(subOptions);
	}

	cat = newtListboxGetCurrent(rootOptions);

	AST_LIST_TRAVERSE(&cat->members, mem, list) {

		if ((mem->depsfailed == HARD_FAILURE) || (mem->conflictsfailed == HARD_FAILURE)) {
			snprintf(buf, sizeof(buf), "XXX %s", mem->name);
		} else if (mem->depsfailed == SOFT_FAILURE) {
			snprintf(buf, sizeof(buf), "<%s> %s", mem->enabled ? "*" : " ", mem->name);
		} else if (mem->conflictsfailed == SOFT_FAILURE) {
			snprintf(buf, sizeof(buf), "(%s) %s", mem->enabled ? "*" : " ", mem->name);
		} else {
			snprintf(buf, sizeof(buf), "[%s] %s", mem->enabled ? "*" : " ", mem->name);
		}

		if (overlay) {
			newtListboxSetEntry(subOptions, i, buf);
		} else {
			newtListboxAppendEntry(subOptions, buf, mem);
		}

		i++;
	}

	if (!overlay) {
		display_member_info(AST_LIST_FIRST(&cat->members));
	}

	return;
}

static void build_main_menu()
{
	struct category *cat;
	char buf[64];
	int i = 1;

	newtListboxClear(rootOptions);

	AST_LIST_TRAVERSE(&categories, cat, list) {
		if (!strlen_zero(cat->displayname))
			snprintf(buf, sizeof(buf), " %s ", cat->displayname);
		else
			snprintf(buf, sizeof(buf), " %s ", cat->name);

		newtListboxAppendEntry(rootOptions, buf, cat);

		i++;
	}
}

static void category_menu_callback(newtComponent component, void *data)
{
	display_member_info(newtListboxGetCurrent(subOptions));
}

static void root_menu_callback(newtComponent component, void *data)
{
	build_members_menu(0);
}

int run_confirmation_dialog(int *result)
{
	int res = newtWinTernary("Are You Sure?", "Discard changes & Exit", "Save & Exit", "Cancel",
				   "It appears you have made some changes, and you have opted to Quit "
				   "without saving these changes.  Please choose \"Discard changes & Exit\" to exit "
				   "without saving; Choose \"Cancel\" to cancel your decision to quit, and keep "
				   "working in menuselect, or choose \"Save & Exit\" to save your changes, and exit.");

	switch (res) {
	case 1:
		/* Discard and exit */
		*result = -1;
		return 1;
	case 2:
		/* Save and exit */
		*result = 0;
		return 1;
	case 3:
		/* They either chose "No" or they hit F12 */
	default:
		*result = -1;
		return 0;
	}
}

int run_menu(void)
{
	struct newtExitStruct es;
	newtComponent form;
	int x = 0, y = 0, res = 0;

	newtInit();
	newtCls();
	newtGetScreenSize(&x, &y);

	if (x < MIN_X || y < MIN_Y) {
		newtFinished();
		fprintf(stderr, "Terminal must be at least %d x %d.\n", MIN_X, MIN_Y);
		return -1;
	}

	newtPushHelpLine("  <ENTER> toggles selection | <F12> saves & exits | <ESC> exits without save");
	newtRefresh();

	newtCenteredWindow(x - 8, y - 7, menu_name);
	form = newtForm(NULL, NULL, 0);

	/* F8 for select all */
	newtFormAddHotKey(form, NEWT_KEY_F8);

	/* F7 for deselect all */
	newtFormAddHotKey(form, NEWT_KEY_F7);

	newtFormSetTimer(form, 200);

	rootOptions = newtListbox(2, 1, y - 15, 0);
	newtListboxSetWidth(rootOptions, 34);
	newtFormAddComponent(form, rootOptions);
	newtComponentAddCallback(rootOptions, root_menu_callback, NULL);

	subOptions = newtListbox(38, 1, y - 15, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
	newtListboxSetWidth(subOptions, x - 47);
	newtFormAddComponent(form, subOptions);
	newtComponentAddCallback(subOptions, category_menu_callback, NULL);

	memberNameTextbox    = newtTextbox(2, y - 13, x - 10, 1, 0);
	dependsLabel         = newtLabel(2, y - 11, "    Depends on:");
	usesLabel            = newtLabel(2, y - 10, "       Can use:");
	conflictsLabel       = newtLabel(2, y - 9, "Conflicts with:");
	dependsDataTextbox   = newtTextbox(18, y - 11, x - 27, 1, 0);
	usesDataTextbox      = newtTextbox(18, y - 10, x - 27, 1, 0);
	conflictsDataTextbox = newtTextbox(18, y - 9, x - 27, 1, 0);

	exitButton = newtButton(x - 23, y - 11, "  Exit  ");
	saveAndExitButton = newtButton(x - 43, y - 11, " Save & Exit ");

	newtFormAddComponents(
		form,
		memberNameTextbox,
		dependsLabel,
		dependsDataTextbox,
		usesLabel,
		usesDataTextbox,
		conflictsLabel,
		conflictsDataTextbox,
		saveAndExitButton,
		exitButton,
		NULL);

	build_main_menu();

	root_menu_callback(rootOptions, AST_LIST_FIRST(&categories));

	for (;;) {
		do {
			newtFormRun(form, &es);
		} while (es.reason == NEWT_EXIT_TIMER);

		if (es.reason == NEWT_EXIT_HOTKEY) {
			int done = 1;

			switch (es.u.key) {
			case NEWT_KEY_F12:
				res = 0;
				break;
			case NEWT_KEY_F7:
				toggle_all_options(0);
				done = 0;
				break;
			case NEWT_KEY_F8:
				toggle_all_options(1);
				done = 0;
				break;
			case NEWT_KEY_ESCAPE:
				if (changes_made) {
					done = run_confirmation_dialog(&res);
				} else {
					res = -1;
				}
				break;
			default:
				done = 0;
				break;
			}

			if (done) {
				break;
			}
		} else if (es.reason == NEWT_EXIT_COMPONENT) {
			if (es.u.co == saveAndExitButton) {
				res = 0;
				break;
			} else if (es.u.co == exitButton) {
				int done = 1;

				if (changes_made) {
					done = run_confirmation_dialog(&res);
				} else {
					res = -1;
				}

				if (done) {
					break;
				}
			} else if (es.u.co == subOptions) {
				toggle_selected_option();
			}
		}
	}

	/* Cleanup */
	reset_display();
	newtFormDestroy(form);
	newtPopWindow();
	newtPopHelpLine();
	newtCls();
	newtFinished();

	return res;
}