| Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 1 | #include <newt.h> | 
| Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 2 | #include <signal.h> | 
 | 3 | #include <stdio.h> | 
 | 4 | #include <stdbool.h> | 
 | 5 | #include <string.h> | 
| Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 6 | #include <sys/ttydefaults.h> | 
| Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 7 |  | 
| Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 8 | #include "../cache.h" | 
 | 9 | #include "../debug.h" | 
 | 10 | #include "browser.h" | 
 | 11 | #include "helpline.h" | 
 | 12 | #include "util.h" | 
| Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 13 |  | 
| Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 14 | static void newt_form__set_exit_keys(newtComponent self) | 
 | 15 | { | 
| Arnaldo Carvalho de Melo | a308f3a | 2010-05-16 20:29:38 -0300 | [diff] [blame] | 16 | 	newtFormAddHotKey(self, NEWT_KEY_LEFT); | 
| Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 17 | 	newtFormAddHotKey(self, NEWT_KEY_ESCAPE); | 
 | 18 | 	newtFormAddHotKey(self, 'Q'); | 
 | 19 | 	newtFormAddHotKey(self, 'q'); | 
 | 20 | 	newtFormAddHotKey(self, CTRL('c')); | 
 | 21 | } | 
 | 22 |  | 
| Arnaldo Carvalho de Melo | 4c1c952 | 2010-08-12 12:37:51 -0300 | [diff] [blame] | 23 | static newtComponent newt_form__new(void) | 
| Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 24 | { | 
 | 25 | 	newtComponent self = newtForm(NULL, NULL, 0); | 
 | 26 | 	if (self) | 
 | 27 | 		newt_form__set_exit_keys(self); | 
 | 28 | 	return self; | 
 | 29 | } | 
 | 30 |  | 
| Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 31 | int ui__popup_menu(int argc, char * const argv[]) | 
| Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 32 | { | 
 | 33 | 	struct newtExitStruct es; | 
 | 34 | 	int i, rc = -1, max_len = 5; | 
 | 35 | 	newtComponent listbox, form = newt_form__new(); | 
 | 36 |  | 
 | 37 | 	if (form == NULL) | 
 | 38 | 		return -1; | 
 | 39 |  | 
 | 40 | 	listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT); | 
 | 41 | 	if (listbox == NULL) | 
 | 42 | 		goto out_destroy_form; | 
 | 43 |  | 
| Arnaldo Carvalho de Melo | 7f82645 | 2010-05-10 10:51:25 -0300 | [diff] [blame] | 44 | 	newtFormAddComponent(form, listbox); | 
| Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 45 |  | 
 | 46 | 	for (i = 0; i < argc; ++i) { | 
 | 47 | 		int len = strlen(argv[i]); | 
 | 48 | 		if (len > max_len) | 
 | 49 | 			max_len = len; | 
 | 50 | 		if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i)) | 
 | 51 | 			goto out_destroy_form; | 
 | 52 | 	} | 
 | 53 |  | 
 | 54 | 	newtCenteredWindow(max_len, argc, NULL); | 
 | 55 | 	newtFormRun(form, &es); | 
 | 56 | 	rc = newtListboxGetCurrent(listbox) - NULL; | 
 | 57 | 	if (es.reason == NEWT_EXIT_HOTKEY) | 
 | 58 | 		rc = -1; | 
 | 59 | 	newtPopWindow(); | 
 | 60 | out_destroy_form: | 
 | 61 | 	newtFormDestroy(form); | 
 | 62 | 	return rc; | 
 | 63 | } | 
 | 64 |  | 
| Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 65 | int ui__help_window(const char *text) | 
| Arnaldo Carvalho de Melo | a9a4ab7 | 2010-05-16 21:04:27 -0300 | [diff] [blame] | 66 | { | 
 | 67 | 	struct newtExitStruct es; | 
 | 68 | 	newtComponent tb, form = newt_form__new(); | 
 | 69 | 	int rc = -1; | 
 | 70 | 	int max_len = 0, nr_lines = 0; | 
 | 71 | 	const char *t; | 
 | 72 |  | 
 | 73 | 	if (form == NULL) | 
 | 74 | 		return -1; | 
 | 75 |  | 
 | 76 | 	t = text; | 
 | 77 | 	while (1) { | 
 | 78 | 		const char *sep = strchr(t, '\n'); | 
 | 79 | 		int len; | 
 | 80 |  | 
 | 81 | 		if (sep == NULL) | 
 | 82 | 			sep = strchr(t, '\0'); | 
 | 83 | 		len = sep - t; | 
 | 84 | 		if (max_len < len) | 
 | 85 | 			max_len = len; | 
 | 86 | 		++nr_lines; | 
 | 87 | 		if (*sep == '\0') | 
 | 88 | 			break; | 
 | 89 | 		t = sep + 1; | 
 | 90 | 	} | 
 | 91 |  | 
 | 92 | 	tb = newtTextbox(0, 0, max_len, nr_lines, 0); | 
 | 93 | 	if (tb == NULL) | 
 | 94 | 		goto out_destroy_form; | 
 | 95 |  | 
 | 96 | 	newtTextboxSetText(tb, text); | 
 | 97 | 	newtFormAddComponent(form, tb); | 
 | 98 | 	newtCenteredWindow(max_len, nr_lines, NULL); | 
 | 99 | 	newtFormRun(form, &es); | 
 | 100 | 	newtPopWindow(); | 
 | 101 | 	rc = 0; | 
 | 102 | out_destroy_form: | 
 | 103 | 	newtFormDestroy(form); | 
 | 104 | 	return rc; | 
 | 105 | } | 
 | 106 |  | 
| Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 107 | static const char yes[] = "Yes", no[] = "No", | 
 | 108 | 		  warning_str[] = "Warning!", ok[] = "Ok"; | 
| Cyrill Gorcunov | a3da8e4 | 2010-11-06 11:47:24 +0300 | [diff] [blame] | 109 |  | 
| Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 110 | bool ui__dialog_yesno(const char *msg) | 
| Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 111 | { | 
 | 112 | 	/* newtWinChoice should really be accepting const char pointers... */ | 
| Cyrill Gorcunov | a3da8e4 | 2010-11-06 11:47:24 +0300 | [diff] [blame] | 113 | 	return newtWinChoice(NULL, (char *)yes, (char *)no, (char *)msg) == 1; | 
| Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 114 | } | 
| Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 115 |  | 
 | 116 | void ui__warning(const char *format, ...) | 
 | 117 | { | 
 | 118 | 	va_list args; | 
 | 119 |  | 
 | 120 | 	va_start(args, format); | 
 | 121 | 	if (use_browser > 0) | 
 | 122 | 		newtWinMessagev((char *)warning_str, (char *)ok, | 
 | 123 | 				(char *)format, args); | 
 | 124 | 	else | 
 | 125 | 		vfprintf(stderr, format, args); | 
 | 126 | 	va_end(args); | 
 | 127 | } |