2015-01-30 20:37:14 +01:00
|
|
|
/*
|
2010-08-08 10:06:34 +02:00
|
|
|
* +-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
* | | Enno Rehling <enno@eressea.de>
|
|
|
|
* | Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
|
2015-01-30 20:37:14 +01:00
|
|
|
* | (c) 1998 - 2006 |
|
2010-08-08 10:06:34 +02:00
|
|
|
* | | This program may not be used, modified or distributed
|
|
|
|
* +-------------------+ without prior permission by the authors of Eressea.
|
2015-01-30 20:37:14 +01:00
|
|
|
*
|
2010-08-08 10:06:34 +02:00
|
|
|
*/
|
|
|
|
|
2017-12-29 06:13:28 +01:00
|
|
|
#ifdef _MSC_VER
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
2017-12-29 06:13:28 +01:00
|
|
|
#endif
|
|
|
|
|
2013-12-29 14:12:48 +01:00
|
|
|
#include <curses.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#include "listbox.h"
|
2010-09-12 05:11:15 +02:00
|
|
|
#include "gmtool_structs.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2017-12-28 18:29:40 +01:00
|
|
|
#include <util/strings.h>
|
2011-02-15 16:36:26 +01:00
|
|
|
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <stdlib.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
void
|
2011-03-07 08:02:35 +01:00
|
|
|
insert_selection(list_selection ** p_sel, list_selection * prev,
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *str, void *payload)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
list_selection *sel = calloc(sizeof(list_selection), 1);
|
2017-12-28 18:29:40 +01:00
|
|
|
sel->str = str_strdup(str);
|
2015-01-30 20:37:14 +01:00
|
|
|
sel->data = payload;
|
|
|
|
if (*p_sel) {
|
|
|
|
list_selection *s;
|
|
|
|
sel->next = *p_sel;
|
|
|
|
sel->prev = sel->next->prev;
|
|
|
|
sel->next->prev = sel;
|
|
|
|
if (sel->prev) {
|
|
|
|
sel->prev->next = sel;
|
|
|
|
sel->index = sel->prev->index + 1;
|
|
|
|
}
|
|
|
|
for (s = sel->next; s; s = s->next) {
|
|
|
|
s->index = s->prev->index + 1;
|
|
|
|
}
|
|
|
|
*p_sel = sel;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
else {
|
|
|
|
*p_sel = sel;
|
|
|
|
sel->prev = prev;
|
|
|
|
if (prev)
|
|
|
|
sel->index = prev->index + 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
list_selection **push_selection(list_selection ** p_sel, char *str,
|
2015-01-30 20:37:14 +01:00
|
|
|
void *payload)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
list_selection *sel = calloc(sizeof(list_selection), 1);
|
|
|
|
list_selection *prev = NULL;
|
|
|
|
sel->str = str;
|
|
|
|
sel->data = payload;
|
|
|
|
while (*p_sel) {
|
|
|
|
prev = *p_sel;
|
|
|
|
p_sel = &prev->next;
|
|
|
|
}
|
|
|
|
*p_sel = sel;
|
|
|
|
if (prev) {
|
|
|
|
sel->prev = prev;
|
|
|
|
sel->index = prev->index + 1;
|
|
|
|
}
|
|
|
|
return p_sel;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-02-15 16:36:26 +01:00
|
|
|
#define SX (getmaxx(stdscr))
|
|
|
|
#define SY (getmaxy(stdscr))
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
list_selection *do_selection(list_selection * sel, const char *title,
|
2015-01-30 20:37:14 +01:00
|
|
|
void(*perform) (list_selection *, void *), void *data)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
WINDOW *wn;
|
|
|
|
bool update = true;
|
|
|
|
list_selection *s;
|
|
|
|
list_selection *top = sel;
|
|
|
|
list_selection *current = top;
|
|
|
|
int i;
|
|
|
|
int height = 0, width = (int)strlen(title) + 8;
|
|
|
|
for (s = sel; s; s = s->next) {
|
|
|
|
if ((int)strlen(s->str) > width) {
|
|
|
|
width = (int)strlen(s->str);
|
|
|
|
}
|
|
|
|
++height;
|
2012-05-16 07:21:59 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
if (height == 0 || width == 0)
|
|
|
|
return 0;
|
|
|
|
if (width + 3 > SX)
|
|
|
|
width = SX - 4;
|
|
|
|
if (height + 2 > SY)
|
|
|
|
height = SY - 2;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
wn =
|
|
|
|
newwin(height + 2, width + 4, (SY - height - 2) / 2, (SX - width - 4) / 2);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
for (;;) {
|
|
|
|
int input;
|
|
|
|
if (update) {
|
|
|
|
for (s = top; s != NULL && top->index + height != s->index; s = s->next) {
|
|
|
|
i = s->index - top->index;
|
|
|
|
wmove(wn, i + 1, 4);
|
|
|
|
waddnstr(wn, s->str, -1);
|
|
|
|
wclrtoeol(wn);
|
|
|
|
}
|
|
|
|
wclrtobot(wn);
|
|
|
|
wxborder(wn);
|
|
|
|
mvwprintw(wn, 0, 2, "[ %s ]", title);
|
|
|
|
update = false;
|
|
|
|
}
|
|
|
|
i = current->index - top->index;
|
|
|
|
wattron(wn, A_BOLD | COLOR_PAIR(COLOR_YELLOW));
|
|
|
|
wmove(wn, i + 1, 2);
|
|
|
|
waddstr(wn, "->");
|
2010-08-08 10:06:34 +02:00
|
|
|
wmove(wn, i + 1, 4);
|
2015-01-30 20:37:14 +01:00
|
|
|
waddnstr(wn, current->str, width - 2);
|
|
|
|
wattroff(wn, A_BOLD | COLOR_PAIR(COLOR_YELLOW));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
wrefresh(wn);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
input = getch();
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
wmove(wn, i + 1, 2);
|
|
|
|
waddstr(wn, " ");
|
|
|
|
wmove(wn, i + 1, 4);
|
|
|
|
waddnstr(wn, current->str, width);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
switch (input) {
|
|
|
|
case KEY_NPAGE:
|
|
|
|
for (i = 0; i != height / 2; ++i) {
|
|
|
|
if (current->next) {
|
|
|
|
current = current->next;
|
|
|
|
if (current->index - height >= top->index) {
|
|
|
|
top = current;
|
|
|
|
update = true;
|
|
|
|
}
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
break;
|
|
|
|
case KEY_PPAGE:
|
|
|
|
for (i = 0; i != height / 2; ++i) {
|
|
|
|
if (current->prev) {
|
|
|
|
if (current == top) {
|
|
|
|
top = sel;
|
|
|
|
while (top->index + height < current->index)
|
|
|
|
top = top->next;
|
|
|
|
update = true;
|
|
|
|
}
|
|
|
|
current = current->prev;
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
break;
|
|
|
|
case KEY_DOWN:
|
|
|
|
if (current->next) {
|
|
|
|
current = current->next;
|
|
|
|
if (current->index - height >= top->index) {
|
|
|
|
top = current;
|
|
|
|
update = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_UP:
|
|
|
|
if (current->prev) {
|
|
|
|
if (current == top) {
|
|
|
|
top = sel;
|
|
|
|
while (top->index + height < current->index)
|
|
|
|
top = top->next;
|
|
|
|
update = true;
|
|
|
|
}
|
|
|
|
current = current->prev;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 27:
|
|
|
|
case 'q':
|
|
|
|
delwin(wn);
|
|
|
|
return NULL;
|
|
|
|
case 10:
|
|
|
|
case 13:
|
|
|
|
if (perform)
|
|
|
|
perform(current, data);
|
|
|
|
else {
|
|
|
|
delwin(wn);
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s = current->next;
|
2011-03-07 08:02:35 +01:00
|
|
|
if (s == NULL)
|
2015-01-30 20:37:14 +01:00
|
|
|
s = top;
|
|
|
|
while (s != current) {
|
|
|
|
if (tolower(s->str[0]) == tolower(input)) {
|
|
|
|
current = s;
|
|
|
|
update = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s = s->next;
|
|
|
|
if (s == NULL)
|
|
|
|
s = top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (current->index - height >= top->index) {
|
|
|
|
top = current;
|
|
|
|
update = true;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|