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