diff options
-rw-r--r-- | ui.c | 16 | ||||
-rw-r--r-- | ui_input.c | 56 | ||||
-rw-r--r-- | ui_input.h | 11 |
3 files changed, 62 insertions, 21 deletions
@@ -131,7 +131,7 @@ init_ui(void) wnd_main = initscr(); start_color(); init_pair(1, COLOR_RED, COLOR_BLACK); - init_pair(2, COLOR_WHITE, COLOR_WHITE); + init_pair(2, COLOR_WHITE, COLOR_BLACK); init_pair(3, COLOR_BLACK, COLOR_WHITE); raw(); keypad(stdscr, TRUE); @@ -165,7 +165,7 @@ stop_ui_thrd(void) { } static bool -process_key(int key, struct input *a, WINDOW *win) +process_key(wchar_t key, struct input *a, WINDOW *win) { bool retval = true; @@ -196,12 +196,8 @@ process_key(int key, struct input *a, WINDOW *win) int main(int argc, char **argv) { - struct input *pw_input = init_input(1,7,20,"PASSWORD",128,COLOR_PAIR(3)); - struct anic *heartbeat = init_anic(2,2,A_BOLD | COLOR_PAIR(3)); - struct anic *a = init_anic(4,4,0); - struct anic *b = init_anic(6,6,COLOR_PAIR(1)); - a->state = '-'; - b->state = '\\'; + struct input *pw_input = init_input(10,10,20,"PASSWORD",128,COLOR_PAIR(3), COLOR_PAIR(2)); + struct anic *heartbeat = init_anic(2,2,A_BOLD | COLOR_PAIR(1)); char key = '\0'; if (sem_init(&sem_rdy, 0, 0) == -1) { @@ -210,7 +206,6 @@ main(int argc, char **argv) } init_ui(); register_anic(heartbeat); - register_anic(a); register_anic(b); register_input(NULL, pw_input); activate_input(wnd_main, pw_input); if (run_ui_thrd() != 0) { @@ -224,13 +219,10 @@ main(int argc, char **argv) pthread_mutex_unlock(&mtx_busy); } stop_ui_thrd(); - unregister_ui_elt(a); unregister_ui_elt(heartbeat); - unregister_ui_elt(b); unregister_ui_elt(pw_input); free_input(pw_input); free_anic(heartbeat); - free_anic(a); free_anic(b); free_ui(); return (0); } @@ -1,12 +1,13 @@ #include <stdlib.h> #include <string.h> +#include <wchar.h> #include "ui.h" #include "ui_input.h" struct input * -init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs) +init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs, chtype shadow) { struct input *a = calloc(1, sizeof(struct input)); @@ -18,8 +19,13 @@ init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, siz a->input_max = input_len; a->input_len = 0; a->input_pos = 0; +#ifdef NCURSES_WIDECHAR + a->prompt = wcsdup(prompt); +#else a->prompt = strdup(prompt); +#endif a->attrs = attrs; + a->shadow = shadow; return (a); } @@ -34,6 +40,37 @@ free_input(struct input *a) } static void +print_wnd(size_t addwidth, struct input *a) +{ + int i, x = a->x, y = a->y; + size_t relwidth = addwidth*2, len = strlen(a->prompt) + a->width; + char tmp[len+relwidth+1]; + + memset(tmp, ' ', len+relwidth); + tmp[len+relwidth] = '\0'; + for (i = -1; i <= 1; i++) + mvprintw(y+i, x-addwidth, tmp); + + mvhline(y-2, x-addwidth, 0, len+relwidth); + mvhline(y+2, x-addwidth, 0, len+relwidth); + mvvline(y-1, x-addwidth-1, 0, 3); + mvvline(y-1, x+len+addwidth, 0, 3); + mvaddch(y-2, x-addwidth-1, ACS_ULCORNER); + mvaddch(y+2, x-addwidth-1, ACS_LLCORNER); + mvaddch(y-2, x+len+addwidth, ACS_URCORNER); + mvaddch(y+2, x+len+addwidth, ACS_LRCORNER); + + attron(a->shadow); + for (i = x-addwidth+1; i < x+len+relwidth; i++) + mvaddch(y+3, i, ACS_CKBOARD); + for (i = -1; i < 3; i++) { + mvaddch(y+i, x+len+relwidth-2, ACS_CKBOARD); + mvaddch(y+i, x+len+relwidth-1, ACS_CKBOARD); + } + attroff(a->shadow); +} + +static void print_input_text(WINDOW *win, struct input *a) { size_t start = 0; @@ -60,6 +97,8 @@ print_input(WINDOW *win, struct input *a) size_t p_len = strlen(a->prompt); attron(a->attrs); + print_wnd(3, a); + attron(a->attrs); if (win == NULL) { mvprintw(a->y, a->x, a->prompt); tmp = calloc(a->width+1, sizeof(char)); @@ -88,17 +127,16 @@ activate_input(WINDOW *win, struct input *a) } int -add_input(WINDOW *win, struct input *a, int key) +add_input(WINDOW *win, struct input *a, wchar_t key) { if (a == NULL) return (UICB_ERR_UNDEF); if (a->input_len >= a->input_max) return (UICB_ERR_BUF); - *(a->input + a->input_pos) = (char) key; + *(a->input + a->input_pos) = (wchar_t) key; ++a->input_pos; ++a->input_len; a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos); print_input(win, a); - - mvwprintw(win, 10, 1, "w:%d,cp:%d,im:%lu,il:%lu,ip:%lu,s:%s", a->width, a->cur_pos, a->input_max, a->input_len, a->input_pos, a->input); + //mvwprintw(win, 10, 1, "w:%d,cp:%d,im:%lu,il:%lu,ip:%lu,s:%s", a->width, a->cur_pos, a->input_max, a->input_len, a->input_pos, a->input); return (UICB_OK); } @@ -112,11 +150,13 @@ del_input(WINDOW *win, struct input *a) if (a->input_pos-1 == a->input_len) { --a->input_pos; } - a->cur_pos = (a->cur_pos+1 < a->width && a->cur_pos > 0 ? a->cur_pos-1 : a->cur_pos); + if (a->cur_pos+1 < a->width && a->cur_pos > 0) { + --a->cur_pos; + } else if (a->cur_pos-1 == a->input_pos) { + --a->cur_pos; + } mvwprintw(win, a->y, a->x + a->cur_pos + strlen(a->prompt), "_"); print_input(win, a); - - mvwprintw(win, 10, 1, "w:%d,cp:%d,im:%lu,il:%lu,ip:%lu,s:%s", a->width, a->cur_pos, a->input_max, a->input_len, a->input_pos, a->input); return (UICB_OK); } @@ -9,16 +9,25 @@ struct input { unsigned int y; unsigned int width; unsigned int cur_pos; +#ifdef NCURSES_WIDECHAR + wchar_t *input; +#else char *input; +#endif size_t input_max; size_t input_len; size_t input_pos; +#ifdef NCURSES_WIDECHAR + wchar_t *prompt; +#else char *prompt; +#endif chtype attrs; + chtype shadow; }; struct input * -init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs); +init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs, chtype shadow); void free_input(struct input *a); |