1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-07-01 03:36:45 +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
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;
getyx(self->window, y, x);
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) {
/* prevents buffer overflows and strange behaviour when cursor goes past the window */
if ( (ctx->len < MAX_STR_SIZE - 1) && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1) - 1)) ) {
if (ltr) { /* char is printable */
if (ctx->len < MAX_STR_SIZE - 1) {
add_char_to_buf(ctx, key);
if (x == x2 - 1)
wmove(self->window, y + 1, 0);
else
if (x >= x2 - 1) {
wmove(self->window, y, x2 / 2);
ctx->start += x2 / 2;
} else {
wmove(self->window, y, x + MAX(1, wcwidth(key)));
}
}
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 (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);
if (x == 0)
wmove(self->window, y - 1, x2 - cur_len);
else
if (x == 0) {
wmove(self->window, y, x2 - cur_len);
ctx->start = ctx->start >= x2 ? ctx->start - x2 : 0;
ctx->pos = ctx->start + x2 - 1;
} else {
wmove(self->window, y, x - cur_len);
}
} else {
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 */
if (ctx->pos > 0) {
ctx->pos = 0;
ctx->start = 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 */
if (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;
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
if (x == 0)
wmove(self->window, y - 1, x2 - cur_len);
else
if (x == 0) {
wmove(self->window, y, x2 - cur_len);
ctx->start = ctx->start >= x2 ? ctx->start - x2 : 0;
ctx->pos = ctx->start + x2 - 1;
} else {
wmove(self->window, y, x - cur_len);
}
} else {
beep();
}
@ -634,13 +644,16 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
else if (key == KEY_RIGHT) {
if (ctx->pos < ctx->len) {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
++ctx->pos;
if (x == x2 - 1)
wmove(self->window, y + 1, 0);
else
if (x == x2 - 1) {
wmove(self->window, y, 0);
ctx->start += x2;
ctx->pos = ctx->start;
} else {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
wmove(self->window, y, x + cur_len);
}
} else {
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 */
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 */
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 */
@ -662,8 +679,9 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (diff != -1) {
if (x + diff > x2 - 1) {
int ofst = (x + diff - 1) - (x2 - 1);
wmove(self->window, y + 1, ofst);
//int ofst = x + diff - x2;
wmove(self->window, y, x + diff);
ctx->start += x2 / 2;
} else {
wmove(self->window, y, x + diff);
}
@ -739,16 +757,15 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
wclear(ctx->linewin);
curs_set(1);
scrollok(ctx->history, 0);
if (ctx->len> 0) {
if (ctx->len > 0) {
uint8_t line[MAX_STR_SIZE];
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) {
reset_buf(ctx);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} 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, "/log", GLOBAL_COMMAND_MODE);
scrollok(ctx->history, 0);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
}