2011-10-26 10:00:55 +00:00
|
|
|
#include "../util.h"
|
2010-08-10 18:49:07 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
2010-03-12 13:48:12 +00:00
|
|
|
#include <sys/ttydefaults.h>
|
2010-03-11 23:12:44 +00:00
|
|
|
|
2010-08-10 18:58:50 +00:00
|
|
|
#include "../cache.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "browser.h"
|
2011-10-26 09:11:03 +00:00
|
|
|
#include "keysyms.h"
|
2010-08-10 18:58:50 +00:00
|
|
|
#include "helpline.h"
|
2011-03-01 13:24:43 +00:00
|
|
|
#include "ui.h"
|
2010-08-10 18:58:50 +00:00
|
|
|
#include "util.h"
|
2011-10-26 10:00:55 +00:00
|
|
|
#include "libslang.h"
|
2010-03-11 23:12:44 +00:00
|
|
|
|
2011-10-26 09:11:03 +00:00
|
|
|
static void ui_browser__argv_write(struct ui_browser *browser,
|
|
|
|
void *entry, int row)
|
|
|
|
{
|
|
|
|
char **arg = entry;
|
|
|
|
bool current_entry = ui_browser__is_current_entry(browser, row);
|
|
|
|
|
|
|
|
ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
|
|
|
|
HE_COLORSET_NORMAL);
|
|
|
|
slsmg_write_nstring(*arg, browser->width);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int popup_menu__run(struct ui_browser *menu)
|
|
|
|
{
|
|
|
|
int key;
|
|
|
|
|
|
|
|
if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
key = ui_browser__run(menu, 0);
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case K_RIGHT:
|
|
|
|
case K_ENTER:
|
|
|
|
key = menu->index;
|
|
|
|
break;
|
|
|
|
case K_LEFT:
|
|
|
|
case K_ESC:
|
|
|
|
case 'q':
|
|
|
|
case CTRL('c'):
|
|
|
|
key = -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_browser__hide(menu);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2010-08-10 18:58:50 +00:00
|
|
|
int ui__popup_menu(int argc, char * const argv[])
|
2010-03-24 19:40:14 +00:00
|
|
|
{
|
2011-10-26 09:11:03 +00:00
|
|
|
struct ui_browser menu = {
|
|
|
|
.entries = (void *)argv,
|
|
|
|
.refresh = ui_browser__argv_refresh,
|
|
|
|
.seek = ui_browser__argv_seek,
|
|
|
|
.write = ui_browser__argv_write,
|
|
|
|
.nr_entries = argc,
|
|
|
|
};
|
2010-03-24 19:40:14 +00:00
|
|
|
|
2011-10-26 09:11:03 +00:00
|
|
|
return popup_menu__run(&menu);
|
2010-03-24 19:40:14 +00:00
|
|
|
}
|
|
|
|
|
2011-10-26 10:00:55 +00:00
|
|
|
int ui__question_window(const char *title, const char *text,
|
|
|
|
const char *exit_msg, int delay_secs)
|
2010-05-17 00:04:27 +00:00
|
|
|
{
|
2011-10-26 10:00:55 +00:00
|
|
|
int x, y;
|
2010-05-17 00:04:27 +00:00
|
|
|
int max_len = 0, nr_lines = 0;
|
|
|
|
const char *t;
|
|
|
|
|
|
|
|
t = text;
|
|
|
|
while (1) {
|
|
|
|
const char *sep = strchr(t, '\n');
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (sep == NULL)
|
|
|
|
sep = strchr(t, '\0');
|
|
|
|
len = sep - t;
|
|
|
|
if (max_len < len)
|
|
|
|
max_len = len;
|
|
|
|
++nr_lines;
|
|
|
|
if (*sep == '\0')
|
|
|
|
break;
|
|
|
|
t = sep + 1;
|
|
|
|
}
|
|
|
|
|
2011-10-26 10:00:55 +00:00
|
|
|
max_len += 2;
|
|
|
|
nr_lines += 4;
|
|
|
|
y = SLtt_Screen_Rows / 2 - nr_lines / 2,
|
|
|
|
x = SLtt_Screen_Cols / 2 - max_len / 2;
|
|
|
|
|
|
|
|
SLsmg_set_color(0);
|
|
|
|
SLsmg_draw_box(y, x++, nr_lines, max_len);
|
|
|
|
if (title) {
|
|
|
|
SLsmg_gotorc(y, x + 1);
|
|
|
|
SLsmg_write_string((char *)title);
|
|
|
|
}
|
|
|
|
SLsmg_gotorc(++y, x);
|
|
|
|
nr_lines -= 2;
|
|
|
|
max_len -= 2;
|
|
|
|
SLsmg_write_wrapped_string((unsigned char *)text, y, x,
|
|
|
|
nr_lines, max_len, 1);
|
|
|
|
SLsmg_gotorc(y + nr_lines - 2, x);
|
|
|
|
SLsmg_write_nstring((char *)" ", max_len);
|
|
|
|
SLsmg_gotorc(y + nr_lines - 1, x);
|
|
|
|
SLsmg_write_nstring((char *)exit_msg, max_len);
|
|
|
|
SLsmg_refresh();
|
|
|
|
return ui__getch(delay_secs);
|
2010-05-17 00:04:27 +00:00
|
|
|
}
|
|
|
|
|
2011-10-26 10:00:55 +00:00
|
|
|
int ui__help_window(const char *text)
|
|
|
|
{
|
|
|
|
return ui__question_window("Help", text, "Press any key...", 0);
|
|
|
|
}
|
2010-11-06 08:47:24 +00:00
|
|
|
|
2011-10-26 14:04:37 +00:00
|
|
|
int ui__dialog_yesno(const char *msg)
|
2010-03-24 19:40:14 +00:00
|
|
|
{
|
2011-10-26 14:04:37 +00:00
|
|
|
return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0);
|
2011-10-26 10:00:55 +00:00
|
|
|
}
|
|
|
|
|
2011-10-26 14:04:37 +00:00
|
|
|
int __ui__warning(const char *title, const char *format, va_list args)
|
2011-10-26 10:00:55 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (use_browser > 0 && vasprintf(&s, format, args) > 0) {
|
2011-10-26 14:04:37 +00:00
|
|
|
int key;
|
|
|
|
|
2011-10-26 10:00:55 +00:00
|
|
|
pthread_mutex_lock(&ui__lock);
|
2011-10-26 14:04:37 +00:00
|
|
|
key = ui__question_window(title, s, "Press any key...", 0);
|
2011-10-26 10:00:55 +00:00
|
|
|
pthread_mutex_unlock(&ui__lock);
|
|
|
|
free(s);
|
2011-10-26 14:04:37 +00:00
|
|
|
return key;
|
2011-10-26 10:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s:\n", title);
|
|
|
|
vfprintf(stderr, format, args);
|
2011-10-26 14:04:37 +00:00
|
|
|
return K_ESC;
|
2010-03-24 19:40:14 +00:00
|
|
|
}
|
2010-11-27 04:41:01 +00:00
|
|
|
|
2011-10-26 14:04:37 +00:00
|
|
|
int ui__warning(const char *format, ...)
|
2010-11-27 04:41:01 +00:00
|
|
|
{
|
2011-10-26 14:04:37 +00:00
|
|
|
int key;
|
2010-11-27 04:41:01 +00:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
2011-10-26 14:04:37 +00:00
|
|
|
key = __ui__warning("Warning", format, args);
|
2011-10-26 10:00:55 +00:00
|
|
|
va_end(args);
|
2011-10-26 14:04:37 +00:00
|
|
|
return key;
|
2011-10-26 10:00:55 +00:00
|
|
|
}
|
|
|
|
|
2011-10-26 14:04:37 +00:00
|
|
|
int ui__error(const char *format, ...)
|
2011-10-26 10:00:55 +00:00
|
|
|
{
|
2011-10-26 14:04:37 +00:00
|
|
|
int key;
|
2011-10-26 10:00:55 +00:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
2011-10-26 14:04:37 +00:00
|
|
|
key = __ui__warning("Error", format, args);
|
2010-11-27 04:41:01 +00:00
|
|
|
va_end(args);
|
2011-10-26 14:04:37 +00:00
|
|
|
return key;
|
2010-11-27 04:41:01 +00:00
|
|
|
}
|