From b7375fb2a15af181728f165c80b90be5696adf65 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 13 Sep 2013 02:02:49 -0400 Subject: [PATCH] made statusmessages resize dynamically according to window size --- src/chat.c | 55 +++++++++++++++++++++++++++------------------ src/friendlist.c | 14 ++++++++++-- src/main.c | 2 +- src/prompt.c | 18 +++++++-------- src/toxic_windows.h | 5 +++-- 5 files changed, 58 insertions(+), 36 deletions(-) diff --git a/src/chat.c b/src/chat.c index f1ec13c..18f683b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -44,7 +44,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 if (self->friendnum != num) return; - ChatContext *ctx = (ChatContext *) self->x; + ChatContext *ctx = (ChatContext *) self->chatwin; struct tm *timeinfo = get_time(); @@ -68,7 +68,7 @@ void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) if (self->friendnum != num) return; - StatusBar *statusbar = (StatusBar *) self->s; + StatusBar *statusbar = (StatusBar *) self->stb; statusbar->is_online = status == 1 ? true : false; } @@ -77,7 +77,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin if (self->friendnum != num) return; - ChatContext *ctx = (ChatContext *) self->x; + ChatContext *ctx = (ChatContext *) self->chatwin; struct tm *timeinfo = get_time(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; @@ -108,7 +108,7 @@ static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS if (self->friendnum != num) return; - StatusBar *statusbar = (StatusBar *) self->s; + StatusBar *statusbar = (StatusBar *) self->stb; statusbar->status = status; } @@ -117,7 +117,7 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status if (self->friendnum != num) return; - StatusBar *statusbar = (StatusBar *) self->s; + StatusBar *statusbar = (StatusBar *) self->stb; statusbar->statusmsg_len = len; snprintf(statusbar->statusmsg, len, "%s", status); } @@ -349,8 +349,8 @@ static void execute(ToxWindow *self, ChatContext *ctx, StatusBar *statusbar, Tox static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) { - ChatContext *ctx = (ChatContext *) self->x; - StatusBar *statusbar = (StatusBar *) self->s; + ChatContext *ctx = (ChatContext *) self->chatwin; + StatusBar *statusbar = (StatusBar *) self->stb; struct tm *timeinfo = get_time(); @@ -442,10 +442,10 @@ static void chat_onDraw(ToxWindow *self, Tox *m) int x, y; getmaxyx(self->window, y, x); - ChatContext *ctx = (ChatContext *) self->x; + ChatContext *ctx = (ChatContext *) self->chatwin; /* Draw status bar */ - StatusBar *statusbar = (StatusBar *) self->s; + StatusBar *statusbar = (StatusBar *) self->stb; mvwhline(statusbar->topline, 1, 0, '-', x); wmove(statusbar->topline, 0, 0); @@ -477,7 +477,6 @@ static void chat_onDraw(ToxWindow *self, Tox *m) wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); wprintw(statusbar->topline, "[%s]", status_text); wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); - } else { wattron(statusbar->topline, A_BOLD); wprintw(statusbar->topline, " %s ", self->name); @@ -485,19 +484,30 @@ static void chat_onDraw(ToxWindow *self, Tox *m) wprintw(statusbar->topline, "[Offline]"); } + /* Reset statusbar->statusmsg on window resize */ + if (x != self->x) { + uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; + tox_copy_statusmessage(m, self->friendnum, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); + snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); + statusbar->statusmsg_len = tox_get_statusmessage_size(m, self->friendnum); + } + + self->x = x; + /* Truncate note if it doesn't fit in statusbar */ - uint16_t maxlen = x - getcurx(statusbar->topline) - 5; + uint16_t maxlen = x - getcurx(statusbar->topline) - 6; if (statusbar->statusmsg_len > maxlen) { statusbar->statusmsg[maxlen] = '\0'; statusbar->statusmsg_len = maxlen; } - wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, " | %s |", statusbar->statusmsg); - wattroff(statusbar->topline, A_BOLD); + if (statusbar->statusmsg[0]) { + wattron(statusbar->topline, A_BOLD); + wprintw(statusbar->topline, " | %s | ", statusbar->statusmsg); + wattroff(statusbar->topline, A_BOLD); + } wprintw(statusbar->topline, "\n"); - mvwhline(ctx->linewin, 0, 0, '_', x); wrefresh(self->window); } @@ -506,9 +516,10 @@ static void chat_onInit(ToxWindow *self, Tox *m) { int x, y; getmaxyx(self->window, y, x); + self->x = x; /* Init statusbar info */ - StatusBar *statusbar = (StatusBar *) self->s; + StatusBar *statusbar = (StatusBar *) self->stb; statusbar->status = tox_get_userstatus(m, self->friendnum); statusbar->is_online = tox_get_friend_connectionstatus(m, self->friendnum) == 1; @@ -518,7 +529,7 @@ static void chat_onInit(ToxWindow *self, Tox *m) statusbar->statusmsg_len = tox_get_statusmessage_size(m, self->friendnum); /* Init subwindows */ - ChatContext *ctx = (ChatContext *) self->x; + ChatContext *ctx = (ChatContext *) self->chatwin; statusbar->topline = subwin(self->window, 2, x, 0, 0); ctx->history = subwin(self->window, y-3, x, 0, 0); scrollok(ctx->history, 1); @@ -547,12 +558,12 @@ ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum) tox_getname(m, friendnum, name); snprintf(ret.name, sizeof(ret.name), "%s", name); - ChatContext *x = calloc(1, sizeof(ChatContext)); - StatusBar *s = calloc(1, sizeof(StatusBar)); + ChatContext *chatwin = calloc(1, sizeof(ChatContext)); + StatusBar *stb = calloc(1, sizeof(StatusBar)); - if (s != NULL && x != NULL) { - ret.x = x; - ret.s = s; + if (stb != NULL && chatwin != NULL) { + ret.chatwin = chatwin; + ret.stb = stb; } else { endwin(); fprintf(stderr, "calloc() failed. Aborting...\n"); diff --git a/src/friendlist.c b/src/friendlist.c index 79f91bf..9be6c30 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -219,11 +219,21 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "]%s (", friends[i].name); - /* Truncate note if it doesn't fit on one line */ int x, y; getmaxyx(self->window, y, x); - uint16_t maxlen = x - getcurx(self->window) - 2; + /* Reset friends[i].statusmsg on window resize */ + if (x != self->x) { + uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; + tox_copy_statusmessage(m, friends[i].num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); + snprintf(friends[i].statusmsg, sizeof(friends[i].statusmsg), "%s", statusmsg); + friends[i].statusmsg_len = tox_get_statusmessage_size(m, self->friendnum); + } + + self->x = x; + + /* Truncate note if it doesn't fit on one line */ + uint16_t maxlen = x - getcurx(self->window) - 2; if (friends[i].statusmsg_len > maxlen) { friends[i].statusmsg[maxlen] = '\0'; friends[i].statusmsg_len = maxlen; diff --git a/src/main.c b/src/main.c index c5d9d83..262b154 100644 --- a/src/main.c +++ b/src/main.c @@ -359,7 +359,7 @@ void exit_toxic(Tox *m) store_data(m, DATA_FILE); free(DATA_FILE); free(SRVLIST_FILE); - free(prompt->s); + free(prompt->stb); tox_kill(m); endwin(); exit(EXIT_SUCCESS); diff --git a/src/prompt.c b/src/prompt.c index f65d08a..f9d8248 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -57,28 +57,28 @@ static struct { /* Updates own nick in prompt statusbar */ void prompt_update_nick(ToxWindow *prompt, uint8_t *nick) { - StatusBar *statusbar = (StatusBar *) prompt->s; + StatusBar *statusbar = (StatusBar *) prompt->stb; snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); } /* Updates own statusmessage in prompt statusbar */ void prompt_update_statusmessage(ToxWindow *prompt, uint8_t *statusmsg) { - StatusBar *statusbar = (StatusBar *) prompt->s; + StatusBar *statusbar = (StatusBar *) prompt->stb; snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); } /* Updates own status in prompt statusbar */ void prompt_update_status(ToxWindow *prompt, TOX_USERSTATUS status) { - StatusBar *statusbar = (StatusBar *) prompt->s; + StatusBar *statusbar = (StatusBar *) prompt->stb; statusbar->status = status; } /* Updates own connection status in prompt statusbar */ void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected) { - StatusBar *statusbar = (StatusBar *) prompt->s; + StatusBar *statusbar = (StatusBar *) prompt->stb; statusbar->is_online = is_connected; } @@ -612,7 +612,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) --y; } - StatusBar *statusbar = (StatusBar *) self->s; + StatusBar *statusbar = (StatusBar *) self->stb; werase(statusbar->topline); @@ -676,7 +676,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) getmaxyx(self->window, y, x); /* Init statusbar info */ - StatusBar *statusbar = (StatusBar *) self->s; + StatusBar *statusbar = (StatusBar *) self->stb; statusbar->status = TOX_USERSTATUS_NONE; statusbar->is_online = false; @@ -703,10 +703,10 @@ ToxWindow new_prompt() ret.onFriendRequest = &prompt_onFriendRequest; strcpy(ret.name, "prompt"); - StatusBar *s = calloc(1, sizeof(StatusBar)); + StatusBar *stb = calloc(1, sizeof(StatusBar)); - if (s != NULL) - ret.s = s; + if (stb != NULL) + ret.stb = stb; else { endwin(); fprintf(stderr, "calloc() failed. Aborting...\n"); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 27c786b..11d346e 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -52,9 +52,10 @@ struct ToxWindow_ { char name[TOX_MAX_NAME_LENGTH]; int friendnum; + int x; - void *x; - void *s; + void *chatwin; + void *stb; void *prompt; bool blink;