1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-06-26 20:57:48 +02:00

redesign text input field to scroll horizontally, increase max string size

This commit is contained in:
Jfreegman 2014-06-20 23:08:13 -04:00
parent 3e797db16e
commit 9f4248b1e1
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
7 changed files with 94 additions and 56 deletions

View File

@ -547,17 +547,19 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
int x, y, y2, x2; int x, y, y2, x2;
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
int cur_len = 0; int cur_len = 0; /* widechar len of current char */
int len = MAX(0, wcslen(ctx->line)); /* widechar len of line */
if (ltr) { if (ltr) { /* char is printable */
/* prevents buffer overflows and strange behaviour when cursor goes past the window */ if (ctx->len < MAX_STR_SIZE - 1) {
if ( (ctx->len < MAX_STR_SIZE - 1) && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1) - 1)) ) {
add_char_to_buf(ctx, key); add_char_to_buf(ctx, key);
if (x == x2 - 1) if (x >= x2 - 1) {
wmove(self->window, y + 1, 0); wmove(self->window, y, x2 / 2);
else ctx->start += x2 / 2;
} else {
wmove(self->window, y, x + MAX(1, wcwidth(key))); wmove(self->window, y, x + MAX(1, wcwidth(key)));
}
} }
if (!ctx->self_is_typing && ctx->line[0] != '/') if (!ctx->self_is_typing && ctx->line[0] != '/')
@ -569,13 +571,16 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key */ if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key */
if (ctx->pos > 0) { if (ctx->pos > 0) {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1])); cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
del_char_buf_bck(ctx); del_char_buf_bck(ctx);
if (x == 0) if (x == 0) {
wmove(self->window, y - 1, x2 - cur_len); wmove(self->window, y, x2 - cur_len);
else ctx->start = ctx->start >= x2 ? ctx->start - x2 : 0;
ctx->pos = ctx->start + x2 - 1;
} else {
wmove(self->window, y, x - cur_len); wmove(self->window, y, x - cur_len);
}
} else { } else {
beep(); beep();
} }
@ -607,6 +612,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */ else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */
if (ctx->pos > 0) { if (ctx->pos > 0) {
ctx->pos = 0; ctx->pos = 0;
ctx->start = 0;
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} }
} }
@ -614,7 +620,8 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */ else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */
if (ctx->pos != ctx->len) { if (ctx->pos != ctx->len) {
ctx->pos = ctx->len; ctx->pos = ctx->len;
mv_curs_end(self->window, MAX(0, wcswidth(ctx->line, (CHATBOX_HEIGHT - 1)*x2)), y2, x2); ctx->start = x2 * (len / x2);
mv_curs_end(self->window, len, y2, x2);
} }
} }
@ -623,10 +630,13 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
--ctx->pos; --ctx->pos;
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos])); cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
if (x == 0) if (x == 0) {
wmove(self->window, y - 1, x2 - cur_len); wmove(self->window, y, x2 - cur_len);
else ctx->start = ctx->start >= x2 ? ctx->start - x2 : 0;
ctx->pos = ctx->start + x2 - 1;
} else {
wmove(self->window, y, x - cur_len); wmove(self->window, y, x - cur_len);
}
} else { } else {
beep(); beep();
} }
@ -634,13 +644,16 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_RIGHT) { else if (key == KEY_RIGHT) {
if (ctx->pos < ctx->len) { if (ctx->pos < ctx->len) {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
++ctx->pos; ++ctx->pos;
if (x == x2 - 1) if (x == x2 - 1) {
wmove(self->window, y + 1, 0); wmove(self->window, y, 0);
else ctx->start += x2;
ctx->pos = ctx->start;
} else {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
wmove(self->window, y, x + cur_len); wmove(self->window, y, x + cur_len);
}
} else { } else {
beep(); beep();
} }
@ -648,12 +661,16 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_UP) { /* fetches previous item in history */ else if (key == KEY_UP) { /* fetches previous item in history */
fetch_hist_item(ctx, MOVE_UP); fetch_hist_item(ctx, MOVE_UP);
mv_curs_end(self->window, ctx->len, y2, x2); len = wcslen(ctx->line);
ctx->start = x2 * (len / x2);
mv_curs_end(self->window, len, y2, x2);
} }
else if (key == KEY_DOWN) { /* fetches next item in history */ else if (key == KEY_DOWN) { /* fetches next item in history */
fetch_hist_item(ctx, MOVE_DOWN); fetch_hist_item(ctx, MOVE_DOWN);
mv_curs_end(self->window, ctx->len, y2, x2); len = wcslen(ctx->line);
ctx->start = x2 * (len / x2);
mv_curs_end(self->window, len, y2, x2);
} }
else if (key == '\t') { /* TAB key: completes command */ else if (key == '\t') { /* TAB key: completes command */
@ -662,8 +679,9 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (diff != -1) { if (diff != -1) {
if (x + diff > x2 - 1) { if (x + diff > x2 - 1) {
int ofst = (x + diff - 1) - (x2 - 1); //int ofst = x + diff - x2;
wmove(self->window, y + 1, ofst); wmove(self->window, y, x + diff);
ctx->start += x2 / 2;
} else { } else {
wmove(self->window, y, x + diff); wmove(self->window, y, x + diff);
} }
@ -739,16 +757,15 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
wclear(ctx->linewin); wclear(ctx->linewin);
curs_set(1); curs_set(1);
scrollok(ctx->history, 0);
if (ctx->len> 0) { if (ctx->len > 0) {
uint8_t line[MAX_STR_SIZE]; uint8_t line[MAX_STR_SIZE];
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) { if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) {
reset_buf(ctx); reset_buf(ctx);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else { } else {
mvwprintw(ctx->linewin, 1, 0, "%s", line); mvwprintw(ctx->linewin, 1, 0, "%s", &line[ctx->start]);
} }
} }
@ -888,6 +905,7 @@ static void chat_onInit(ToxWindow *self, Tox *m)
execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE);
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
scrollok(ctx->history, 0);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} }

View File

@ -389,16 +389,19 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
int x, y, y2, x2; int x, y, y2, x2;
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
int cur_len = 0; int cur_len = 0; /* widechar len of current char */
int len = MAX(0, wcslen(ctx->line)); /* widechar len of line */
if (ltr) { if (ltr) { /* char is printable */
if ( (ctx->len < MAX_STR_SIZE - 1) && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1) - 1)) ) { if (ctx->len < MAX_STR_SIZE - 1) {
add_char_to_buf(ctx, key); add_char_to_buf(ctx, key);
if (x == x2 - 1) if (x >= x2 - 1) {
wmove(self->window, y + 1, 0); wmove(self->window, y, x2 / 2);
else ctx->start += x2 / 2;
} else {
wmove(self->window, y, x + MAX(1, wcwidth(key))); wmove(self->window, y, x + MAX(1, wcwidth(key)));
}
} }
} else { /* if (!ltr) */ } else { /* if (!ltr) */
@ -406,15 +409,18 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (line_info_onKey(self, key)) if (line_info_onKey(self, key))
return; return;
if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key */
if (ctx->pos > 0) { if (ctx->pos > 0) {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1])); cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
del_char_buf_bck(ctx); del_char_buf_bck(ctx);
if (x == 0) if (x == 0) {
wmove(self->window, y - 1, x2 - cur_len); wmove(self->window, y, x2 - cur_len);
else ctx->start = ctx->start >= x2 ? ctx->start - x2 : 0;
ctx->pos = ctx->start + x2 - 1;
} else {
wmove(self->window, y, x - cur_len); wmove(self->window, y, x - cur_len);
}
} else { } else {
beep(); beep();
} }
@ -446,6 +452,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */ else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */
if (ctx->pos > 0) { if (ctx->pos > 0) {
ctx->pos = 0; ctx->pos = 0;
ctx->start = 0;
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} }
} }
@ -453,7 +460,8 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */ else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */
if (ctx->pos != ctx->len) { if (ctx->pos != ctx->len) {
ctx->pos = ctx->len; ctx->pos = ctx->len;
mv_curs_end(self->window, MAX(0, wcswidth(ctx->line, (CHATBOX_HEIGHT - 1)*x2)), y2, x2); ctx->start = x2 * (len / x2);
mv_curs_end(self->window, len, y2, x2);
} }
} }
@ -462,10 +470,13 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
--ctx->pos; --ctx->pos;
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos])); cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
if (x == 0) if (x == 0) {
wmove(self->window, y - 1, x2 - cur_len); wmove(self->window, y, x2 - cur_len);
else ctx->start = ctx->start >= x2 ? ctx->start - x2 : 0;
ctx->pos = ctx->start + x2 - 1;
} else {
wmove(self->window, y, x - cur_len); wmove(self->window, y, x - cur_len);
}
} else { } else {
beep(); beep();
} }
@ -473,13 +484,16 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_RIGHT) { else if (key == KEY_RIGHT) {
if (ctx->pos < ctx->len) { if (ctx->pos < ctx->len) {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
++ctx->pos; ++ctx->pos;
if (x == x2 - 1) if (x == x2 - 1) {
wmove(self->window, y + 1, 0); wmove(self->window, y, 0);
else ctx->start += x2;
ctx->pos = ctx->start;
} else {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
wmove(self->window, y, x + cur_len); wmove(self->window, y, x + cur_len);
}
} else { } else {
beep(); beep();
} }
@ -487,12 +501,16 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_UP) { /* fetches previous item in history */ else if (key == KEY_UP) { /* fetches previous item in history */
fetch_hist_item(ctx, MOVE_UP); fetch_hist_item(ctx, MOVE_UP);
mv_curs_end(self->window, ctx->len, y2, x2); len = wcslen(ctx->line);
ctx->start = x2 * (len / x2);
mv_curs_end(self->window, len, y2, x2);
} }
else if (key == KEY_DOWN) { /* fetches next item in history */ else if (key == KEY_DOWN) { /* fetches next item in history */
fetch_hist_item(ctx, MOVE_DOWN); fetch_hist_item(ctx, MOVE_DOWN);
mv_curs_end(self->window, ctx->len, y2, x2); len = wcslen(ctx->line);
ctx->start = x2 * (len / x2);
mv_curs_end(self->window, len, y2, x2);
} }
else if (key == '\t') { /* TAB key: completes peer name */ else if (key == '\t') { /* TAB key: completes peer name */
@ -597,7 +615,7 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m)
reset_buf(ctx); reset_buf(ctx);
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else { } else {
mvwprintw(ctx->linewin, 1, 0, "%s", line); mvwprintw(ctx->linewin, 1, 0, "%s", &line[ctx->start]);
} }
} }

View File

@ -226,9 +226,8 @@ int valid_nick(uint8_t *nick)
/* Moves cursor to the end of the line in given window */ /* Moves cursor to the end of the line in given window */
void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x) void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x)
{ {
int end_y = (len / max_x) + (max_y - CURS_Y_OFFSET); int new_x = len < max_x ? len : len % max_x;
int end_x = len % max_x; wmove(w, max_y - CURS_Y_OFFSET, new_x);
wmove(w, end_y, end_x);
} }
/* gets base file name from path or original file name if no path is supplied */ /* gets base file name from path or original file name if no path is supplied */

View File

@ -35,7 +35,7 @@
#define UNKNOWN_NAME "Anonymous" #define UNKNOWN_NAME "Anonymous"
#define MAX_FRIENDS_NUM 500 #define MAX_FRIENDS_NUM 500
#define MAX_STR_SIZE 256 #define MAX_STR_SIZE 1024
#define MAX_CMDNAME_SIZE 64 #define MAX_CMDNAME_SIZE 64
#define TOXIC_MAX_NAME_LENGTH 32 /* Must be <= TOX_MAX_NAME_LENGTH */ #define TOXIC_MAX_NAME_LENGTH 32 /* Must be <= TOX_MAX_NAME_LENGTH */
#define KEY_IDENT_DIGITS 2 /* number of hex digits to display for the pub-key based identifier */ #define KEY_IDENT_DIGITS 2 /* number of hex digits to display for the pub-key based identifier */

View File

@ -91,6 +91,7 @@ void discard_buf(ChatContext *ctx)
ctx->line[c++] = ctx->line[i]; ctx->line[c++] = ctx->line[i];
ctx->pos = 0; ctx->pos = 0;
ctx->start = 0;
ctx->len = c - 1; ctx->len = c - 1;
} }
@ -104,12 +105,13 @@ void kill_buf(ChatContext *ctx)
ctx->len = ctx->pos; ctx->len = ctx->pos;
} }
/* nulls line and sets pos and len to 0 */ /* nulls line and sets pos, len and start to 0 */
void reset_buf(ChatContext *ctx) void reset_buf(ChatContext *ctx)
{ {
ctx->line[0] = L'\0'; ctx->line[0] = L'\0';
ctx->pos = 0; ctx->pos = 0;
ctx->len = 0; ctx->len = 0;
ctx->start = 0;
} }
/* Removes trailing spaces from line. */ /* Removes trailing spaces from line. */

View File

@ -40,7 +40,7 @@ void discard_buf(ChatContext *ctx);
/* Deletes the line from pos to len */ /* Deletes the line from pos to len */
void kill_buf(ChatContext *ctx); void kill_buf(ChatContext *ctx);
/* nulls line and sets pos and len to 0 */ /* nulls line and sets pos, len and start to 0 */
void reset_buf(ChatContext *ctx); void reset_buf(ChatContext *ctx);
/* Removes trailing spaces from line. */ /* Removes trailing spaces from line. */

View File

@ -36,8 +36,8 @@
#include "toxic.h" #include "toxic.h"
#define MAX_WINDOWS_NUM 32 #define MAX_WINDOWS_NUM 32
#define CURS_Y_OFFSET 3 /* y-axis cursor offset for chat contexts */ #define CURS_Y_OFFSET 1 /* y-axis cursor offset for chat contexts */
#define CHATBOX_HEIGHT 4 #define CHATBOX_HEIGHT 2
/* Curses foreground colours (background is black) */ /* Curses foreground colours (background is black) */
enum { enum {
@ -150,6 +150,7 @@ struct ChatContext {
wchar_t line[MAX_STR_SIZE]; wchar_t line[MAX_STR_SIZE];
size_t pos; size_t pos;
size_t len; size_t len;
size_t start; /* the position to start printing line at */
wchar_t ln_history[MAX_LINE_HIST][MAX_STR_SIZE]; /* history for input lines/commands */ wchar_t ln_history[MAX_LINE_HIST][MAX_STR_SIZE]; /* history for input lines/commands */
int hst_pos; int hst_pos;