1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 06:13:03 +01:00

Merge pull request #113 from graboy/master

Fixed support for wide characters
This commit is contained in:
JFreegman 2014-03-30 17:22:41 -04:00
commit bd817e77f0
6 changed files with 485 additions and 476 deletions

View File

@ -500,7 +500,7 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *acti
}
}
static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
{
ChatContext *ctx = self->chatwin;
StatusBar *statusbar = self->stb;
@ -510,7 +510,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
getmaxyx(self->window, y2, x2);
int cur_len = 0;
if (key == T_KEY_ESC) { /* ESC key: Toggle history scroll mode */
if (ltr && (key == T_KEY_ESC)) { /* ESC key: Toggle history scroll mode */
bool scroll = ctx->hst->scroll_mode ? false : true;
line_info_toggle_scroll(self, scroll);
}
@ -521,6 +521,22 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
return;
}
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)) ) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
if (x == x2-1)
wmove(self->window, y+1, 0);
else
wmove(self->window, y, x + MAX(1, wcwidth(key)));
}
if (!ctx->self_is_typing && ctx->line[0] != '/')
set_typingstatus(self, m, 1);
} else { /* if (!ltr) */
if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key */
if (ctx->pos > 0) {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1]));
@ -632,25 +648,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
}
}
else
#if HAVE_WIDECHAR
if (iswprint(key))
#else
if (isprint(key))
#endif
{ /* 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)) ) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
if (x == x2-1)
wmove(self->window, y+1, 0);
else
wmove(self->window, y, x + MAX(1, wcwidth(key)));
}
if (!ctx->self_is_typing && ctx->line[0] != '/')
set_typingstatus(self, m, 1);
}
/* RETURN key: Execute command or print line */
else if (key == '\n') {
uint8_t line[MAX_STR_SIZE];
@ -695,6 +692,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
reset_buf(ctx->line, &ctx->pos, &ctx->len);
}
}
if (ctx->len <= 0 && ctx->self_is_typing)
set_typingstatus(self, m, 0);

View File

@ -322,7 +322,7 @@ static void draw_popup(ToxWindow *self, Tox *m)
wrefresh(self->popup);
}
static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
{
if (num_friends == 0)
return;
@ -337,6 +337,7 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
return;
}
if (key != ltr) {
if (key == '\n') {
/* Jump to chat window if already open */
if (friends[f].chatwin != -1) {
@ -356,6 +357,7 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
select_friend(self, m, key);
}
}
}
#define FLIST_OFST 4 /* Accounts for the lines at top */

View File

@ -325,7 +325,7 @@ static void send_group_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t
}
}
static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
{
ChatContext *ctx = self->chatwin;
@ -334,7 +334,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
getmaxyx(self->window, y2, x2);
int cur_len = 0;
if (key == T_KEY_ESC) { /* ESC key: Toggle history scroll mode */
if (ltr && (key == T_KEY_ESC) ) { /* ESC key: Toggle history scroll mode */
bool scroll = ctx->hst->scroll_mode ? false : true;
line_info_toggle_scroll(self, scroll);
}
@ -345,6 +345,18 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
return;
}
if (ltr) {
if ( (ctx->len < MAX_STR_SIZE-1) && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1)-1)) ) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
if (x == x2-1)
wmove(self->window, y+1, 0);
else
wmove(self->window, y, x + MAX(1, wcwidth(key)));
}
} else { /* if (!ltr) */
if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */
if (ctx->pos > 0) {
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1]));
@ -475,23 +487,6 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
--groupchats[self->num].side_pos;
}
else
#if HAVE_WIDECHAR
if (iswprint(key))
#else
if (isprint(key))
#endif
{ /* 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)) ) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
if (x == x2-1)
wmove(self->window, y+1, 0);
else
wmove(self->window, y, x + MAX(1, wcwidth(key)));
}
}
/* RETURN key: Execute command or print line */
else if (key == '\n') {
uint8_t line[MAX_STR_SIZE];
@ -531,6 +526,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
reset_buf(ctx->line, &ctx->pos, &ctx->len);
}
}
}
static void groupchat_onDraw(ToxWindow *self, Tox *m)
{

View File

@ -119,7 +119,7 @@ static int add_friend_request(uint8_t *public_key)
return -1;
}
static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
{
ChatContext *ctx = self->chatwin;
@ -127,11 +127,14 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
getyx(ctx->history, y, x);
getmaxyx(ctx->history, y2, x2);
/* this is buggy */
//if (key == T_KEY_ESC) { /* ESC key: Toggle history scroll mode */
// bool scroll = ctx->hst->scroll_mode ? false : true;
// line_info_toggle_scroll(self, scroll);
//}
/* TODO this is buggy */
/* ESC key: Toggle history scroll mode */
/*
if (key == T_KEY_ESC) {
bool scroll = ctx->hst->scroll_mode ? false : true;
line_info_toggle_scroll(self, scroll);
}
*/
/* If we're in scroll mode ignore rest of function */
if (ctx->hst->scroll_mode) {
@ -139,6 +142,12 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
return;
}
if (ltr) {
if (ctx->len < (MAX_STR_SIZE-1)) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
}
} else { /* if (!ltr) */
/* BACKSPACE key: Remove one character from line */
if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) {
@ -235,17 +244,6 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
}
}
else
#if HAVE_WIDECHAR
if (iswprint(key))
#else
if (isprint(key))
#endif
{
if (ctx->len < (MAX_STR_SIZE-1)) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
}
}
/* RETURN key: execute command */
else if (key == '\n') {
wprintw(ctx->history, "\n");
@ -262,6 +260,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
reset_buf(ctx->line, &ctx->pos, &ctx->len);
}
}
}
static void prompt_onDraw(ToxWindow *self, Tox *m)
{

View File

@ -97,7 +97,7 @@ typedef struct PromptBuf PromptBuf;
typedef struct ChatContext ChatContext;
struct ToxWindow {
void(*onKey)(ToxWindow *, Tox *, wint_t);
void(*onKey)(ToxWindow *, Tox *, wint_t, bool);
void(*onDraw)(ToxWindow *, Tox *);
void(*onInit)(ToxWindow *, Tox *);
void(*onFriendRequest)(ToxWindow *, Tox *, uint8_t *, uint8_t *, uint16_t);

View File

@ -394,18 +394,32 @@ void draw_active_window(Tox *m)
wrefresh(a->window);
/* Handle input */
bool ltr;
#ifdef HAVE_WIDECHAR
if (wget_wch(stdscr, &ch) == ERR)
#else
if ((ch = getch()) == ERR)
#endif
int status = wget_wch(stdscr, &ch);
if (status == ERR)
return;
if (ch == T_KEY_NEXT || ch == T_KEY_PREV) {
if (status == OK)
ltr = iswprint(ch);
else /* if (status == KEY_CODE_YES) */
ltr = false;
#else
ch = getch();
if (ch == ERR)
return;
/* TODO verify if this works */
ltr = isprint(ch);
#endif
if (!ltr && (ch == T_KEY_NEXT || ch == T_KEY_PREV) ) {
set_next_window((int) ch);
} else {
pthread_mutex_lock(&Winthread.lock);
a->onKey(a, m, ch);
a->onKey(a, m, ch, ltr);
pthread_mutex_unlock(&Winthread.lock);
}
}