1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 15:13:27 +01:00

line edit support for home, del and end keys

This commit is contained in:
Jfreegman 2013-12-01 02:58:21 -05:00
parent 37dd2bee2d
commit 5850e1c333
4 changed files with 50 additions and 16 deletions

View File

@ -280,17 +280,26 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
/* BACKSPACE key: Remove one character from line */ if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */
if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) { if (ctx->pos > 0) {
del_char_from_buf(key, ctx->line, &ctx->pos, &ctx->len); del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len);
if (x == 0) if (x == 0)
wmove(self->window, y-1, x2-1); wmove(self->window, y-1, x2-1);
else else
wmove(self->window, y, x-1); wmove(self->window, y, x-1);
} }
} else if (key == KEY_LEFT && ctx->pos > 0) { } else if (key == KEY_DC) { /* DEL key: Remove character at pos */
del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len);
} else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */
ctx->pos = 0;
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else if (key == KEY_END) { /* END key: move cursor to end of line */
ctx->pos = ctx->len;
int end_y = (ctx->pos / x2) + y;
int end_x = ctx->len % x2;
wmove(self->window, end_y, end_x);
} else if (key == KEY_LEFT && ctx->pos > 0) {
--ctx->pos; --ctx->pos;
if (x == 0) if (x == 0)
@ -305,7 +314,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
else else
wmove(self->window, y, x+1); wmove(self->window, y, x+1);
} else } else
/* Add printable chars to buffer and print on input space */
#if HAVE_WIDECHAR #if HAVE_WIDECHAR
if (iswprint(key)) if (iswprint(key))
#else #else

View File

@ -220,17 +220,26 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
/* BACKSPACE key: Remove one character from line */ if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */
if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) { if (ctx->pos > 0) {
del_char_from_buf(key, ctx->line, &ctx->pos, &ctx->len); del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len);
if (x == 0) if (x == 0)
wmove(self->window, y-1, x2-1); wmove(self->window, y-1, x2-1);
else else
wmove(self->window, y, x-1); wmove(self->window, y, x-1);
} }
} else if (key == KEY_LEFT && ctx->pos > 0) { } else if (key == KEY_DC) { /* DEL key: Remove character at pos */
del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len);
} else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */
ctx->pos = 0;
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else if (key == KEY_END) { /* END key: move cursor to end of line */
ctx->pos = ctx->len;
int end_y = (ctx->pos / x2) + y;
int end_x = ctx->len % x2;
wmove(self->window, end_y, end_x);
} else if (key == KEY_LEFT && ctx->pos > 0) {
--ctx->pos; --ctx->pos;
if (x == 0) if (x == 0)

View File

@ -188,8 +188,8 @@ void add_char_to_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len)
++(*len); ++(*len);
} }
/* Deletes the character before pos via the backspace key */ /* Deletes the character before pos */
void del_char_from_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len) void del_char_buf_bck(wchar_t *buf, size_t *pos, size_t *len)
{ {
if (*pos <= 0) if (*pos <= 0)
return; return;
@ -211,7 +211,21 @@ void del_char_from_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len)
--(*len); --(*len);
} }
/* sets pos and len to 0 */ /* Deletes the character at pos */
void del_char_buf_frnt(wchar_t *buf, size_t *pos, size_t *len)
{
if (*pos < 0 || *pos >= *len)
return;
int i;
for (i = *pos; i < *len; ++i)
buf[i] = buf[i+1];
--(*len);
}
/* nulls buf and sets pos and len to 0 */
void reset_buf(wchar_t *buf, size_t *pos, size_t *len) void reset_buf(wchar_t *buf, size_t *pos, size_t *len)
{ {
buf[0] = L'\0'; buf[0] = L'\0';

View File

@ -44,8 +44,11 @@ bool valid_nick(uint8_t *nick);
/* Adds char to buffer at pos */ /* Adds char to buffer at pos */
void add_char_to_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len); void add_char_to_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len);
/* Deletes the character before pos via the backspace key */ /* Deletes the character before pos */
void del_char_from_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len); void del_char_buf_bck(wchar_t *buf, size_t *pos, size_t *len);
/* sets pos and len to 0 */ /* Deletes the character at pos */
void reset_buf(wchar_t *buf, size_t *pos, size_t *len); void del_char_buf_frnt(wchar_t *buf, size_t *pos, size_t *len);
/* nulls buf and sets pos and len to 0 */
void reset_buf(wchar_t *buf, size_t *pos, size_t *len);