diff options
Diffstat (limited to 'ui_input.c')
-rw-r--r-- | ui_input.c | 74 |
1 files changed, 57 insertions, 17 deletions
@@ -6,19 +6,20 @@ struct input * -init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len) +init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs) { struct input *a = calloc(1, sizeof(struct input)); a->x = x; a->y = y; a->width = width; + a->cur_pos = 0; a->input = calloc(input_len+1, sizeof(char)); a->input_max = input_len; a->input_len = 0; a->input_pos = 0; a->prompt = strdup(prompt); - a->active = false; + a->attrs = attrs; return (a); } @@ -33,12 +34,32 @@ free_input(struct input *a) } static void +print_input_text(WINDOW *win, struct input *a) +{ + size_t start = 0; + size_t p_len = strlen(a->prompt); + + char tmp[a->width + 1]; + memset(tmp, '\0', a->width + 1); + if (a->input_pos >= a->width) { + start = a->input_pos - a->width; + } + strncpy(tmp, (char *)(a->input + start), a->width); + if (win == NULL) { + mvprintw(a->y, a->x + p_len, "%s", tmp); + } else { + mvwprintw(win, a->y, a->x + p_len, "%s", tmp); + } +} + +static void print_input(WINDOW *win, struct input *a) { char *tmp; int i; size_t p_len = strlen(a->prompt); + attron(a->attrs); if (win == NULL) { mvprintw(a->y, a->x, a->prompt); tmp = calloc(a->width+1, sizeof(char)); @@ -47,40 +68,59 @@ print_input(WINDOW *win, struct input *a) } mvprintw(a->y, a->x + p_len, tmp); free(tmp); - mvprintw(a->y, a->x + p_len, a->input); + } else { } + print_input_text(win, a); + attroff(a->attrs); } int -post_input_cb(WINDOW *win, void *data, bool needs_update) +activate_input(WINDOW *win, struct input *a) { - struct input *a = (struct input *) data; - if (a == NULL) return (UICB_ERR_UNDEF); - if (a->active == true) { - if (win == NULL) { - move(a->y, a->x + a->input_pos); - } else { - wmove(win, a->y, a->x + a->input_pos); - } - return (UICB_CURSOR); + size_t p_len = strlen(a->prompt); + if (win == NULL) { + move(a->y, a->x + p_len + a->cur_pos); + } else { + wmove(win, a->y, a->x + p_len + a->cur_pos); } return (UICB_OK); } +int +add_input(WINDOW *win, struct input *a, int 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_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); + return (UICB_OK); +} int -input_cb(WINDOW *win, void *data, bool needs_update) +del_input(WINDOW *win, struct input *a) +{ + return (UICB_OK); +} + +int +input_cb(WINDOW *win, void *data, bool needs_update, bool timed_out) { struct input *a = (struct input *) data; if (a == NULL) return (UICB_ERR_UNDEF); - print_input(win, a); + if (needs_update || timed_out) { + print_input(win, a); + } return (UICB_OK); } void -register_input(WINDOW *win, struct input *a, chtype attr) +register_input(WINDOW *win, struct input *a) { - register_ui_elt(input_cb, post_input_cb, (void *) a, win, attr); + register_ui_elt(input_cb, (void *) a, win); } |