From e40344186e14bb7baacf9e9ad99c5a76f88f479e Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 9 Aug 2013 00:25:45 -0400 Subject: [PATCH] fixed chat window text wrapping --- chat.c | 51 +++++++++++++++++++++++++++++++++------------------ main.c | 4 ++-- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/chat.c b/chat.c index 73cb145..33f51f2 100644 --- a/chat.c +++ b/chat.c @@ -14,6 +14,8 @@ #include "windows.h" +#define CURS_Y_OFFSET 3 + typedef struct { int friendnum; char line[MAX_STR_SIZE]; @@ -123,16 +125,35 @@ static void chat_onKey(ToxWindow *self, int key) struct tm * timeinfo; timeinfo = localtime(&now); - /* Add printable characters to line */ + int x, y, y2, x2; + getyx(self->window, y, x); + getmaxyx(self->window, y2, x2); + + /* Add printable chars to buffer and print on input space */ if (isprint(key)) { if (ctx->pos != sizeof(ctx->line)-1) { + mvwaddch(self->window, y, x, key); ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = '\0'; } } + /* BACKSPACE key: Remove one character from line */ + else if (key == 0x107 || key == 0x8 || key == 0x7f) { + if (ctx->pos > 0) { + ctx->line[--ctx->pos] = '\0'; + if (x == 0) + mvwdelch(self->window, y-1, x2-1); + else + mvwdelch(self->window, y, x-1); + } + } + /* RETURN key: Execute command or print line */ - else if (key == '\n') { + if (key == '\n') { + wclear(ctx->linewin); + wmove(self->window, y2-CURS_Y_OFFSET, 0); + wclrtobot(self->window); if (ctx->line[0] == '/') execute(self, ctx, ctx->line, timeinfo); else { @@ -155,13 +176,6 @@ static void chat_onKey(ToxWindow *self, int key) ctx->line[0] = '\0'; ctx->pos = 0; } - - /* BACKSPACE key: Remove one character from line */ - else if (key == 0x107 || key == 0x8 || key == 0x7f) { - if (ctx->pos != 0) { - ctx->line[--ctx->pos] = '\0'; - } - } } void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo) @@ -169,6 +183,10 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo) if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { wclear(self->window); wclear(ctx->history); + int x, y; + getmaxyx(self->window, y, x); + (void) x; + wmove(self->window, y-CURS_Y_OFFSET, 0); } else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) @@ -287,14 +305,10 @@ static void chat_onDraw(ToxWindow *self) { curs_set(1); int x, y; - ChatContext *ctx = (ChatContext*) self->x; getmaxyx(self->window, y, x); - (void) x; - if (y < 3) return; - - wclear(ctx->linewin); - mvwhline(ctx->linewin, 0, 0, '_', COLS); - mvwprintw(self->window, y-1, 0, "%s\n", ctx->line); + (void) y; + ChatContext *ctx = (ChatContext*) self->x; + mvwhline(ctx->linewin, 0, 0, '_', x); wrefresh(self->window); } @@ -303,10 +317,11 @@ static void chat_onInit(ToxWindow *self) int x, y; ChatContext *ctx = (ChatContext*) self->x; getmaxyx(self->window, y, x); - ctx->history = subwin(self->window, y - 4, x, 0, 0); + ctx->history = subwin(self->window, y-4, x, 0, 0); scrollok(ctx->history, 1); - ctx->linewin = subwin(self->window, 2, x, y - 3, 0); + ctx->linewin = subwin(self->window, 2, x, y-4, 0); print_help(ctx); + wmove(self->window, y-CURS_Y_OFFSET, 0); } void print_help(ChatContext *self) diff --git a/main.c b/main.c index 2922456..ce2e246 100644 --- a/main.c +++ b/main.c @@ -82,7 +82,7 @@ void on_action(int friendnumber, uint8_t *string, uint16_t length) void on_nickchange(int friendnumber, uint8_t *string, uint16_t length) { - wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); + wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); int i; for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { if (windows[i].onNickChange != NULL) @@ -281,7 +281,7 @@ static void draw_bar() move(LINES - 1, 0); attron(COLOR_PAIR(4) | A_BOLD); - printw(" TOXIC " TOXICVER " |"); + printw(" TOXIC " TOXICVER "|"); attroff(COLOR_PAIR(4) | A_BOLD); int i;