From 5b0964ae32dbed82950efaa3b24ad2af2e4ac86b Mon Sep 17 00:00:00 2001 From: Quentin Glidic Date: Thu, 7 Apr 2016 15:01:14 +0200 Subject: [PATCH] textbox: Split keybinding and text handling Signed-off-by: Quentin Glidic --- include/textbox.h | 3 ++- source/textbox.c | 17 +++++++++++--- source/view.c | 59 +++++++++++++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/include/textbox.h b/include/textbox.h index 3d3b8d09..e61a44fa 100644 --- a/include/textbox.h +++ b/include/textbox.h @@ -109,7 +109,8 @@ void textbox_draw ( textbox *tb, cairo_t *draw ); * * @returns if the key was handled (1), unhandled(0) or handled and return was pressed (-1) */ -int textbox_keypress ( textbox *tb, char *pad, int pad_len, unsigned int modstate, xkb_keysym_t key ); +int textbox_keybinding ( textbox *tb, unsigned int modstate, xkb_keysym_t key ); +int textbox_keypress ( textbox *tb, char *pad, int pad_len ); /** * @param tb Handle to the textbox diff --git a/source/textbox.c b/source/textbox.c index b443c2ee..c9b63842 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -525,14 +525,14 @@ static void textbox_cursor_del_word ( textbox *tb ) // 0 = unhandled // 1 = handled // -1 = handled and return pressed (finished) -int textbox_keypress ( textbox *tb, char *pad, int pad_len, unsigned int modstate, xkb_keysym_t key ) +int textbox_keybinding ( textbox *tb, unsigned int modstate, xkb_keysym_t key ) { if ( !( tb->flags & TB_EDITABLE ) ) { - return 0; + g_return_val_if_reached(0); } int old_blink = tb->blink; tb->blink = 2; - if ( key != XKB_KEY_NoSymbol ) { + // Left or Ctrl-b if ( abe_test_action ( MOVE_CHAR_BACK, modstate, key ) ) { textbox_cursor_dec ( tb ); @@ -594,7 +594,18 @@ int textbox_keypress ( textbox *tb, char *pad, int pad_len, unsigned int modstat else if ( abe_test_action ( ACCEPT_ENTRY, modstate, key ) ) { return -1; } + + tb->blink = old_blink; + return 0; +} + +int textbox_keypress ( textbox *tb, char *pad, int pad_len ) +{ + if ( !( tb->flags & TB_EDITABLE ) ) { + return 0; } + int old_blink = tb->blink; + tb->blink = 2; if ( pad_len > 0 ) { // Filter When alt/ctrl is pressed do not accept the character. if ( !g_ascii_iscntrl ( *pad ) ) { diff --git a/source/view.c b/source/view.c index 17a4dbbc..b59c560e 100644 --- a/source/view.c +++ b/source/view.c @@ -1425,35 +1425,44 @@ static void rofi_view_handle_keypress ( RofiViewState *state, xkb_stuff *xkb, xc if ( rofi_view_keyboard_navigation ( state, key, modstate ) ) { return; } + + int rc = textbox_keybinding ( state->text, modstate, key ); + // Row is accepted. + if ( rc < 0 ) { + // If a valid item is selected, return that.. + state->selected_line = UINT32_MAX; + if ( state->selected < state->filtered_lines ) { + ( state->selected_line ) = state->line_map[state->selected]; + state->retv = MENU_OK; + } + else { + // Nothing entered and nothing selected. + state->retv = MENU_CUSTOM_INPUT; + } + if ( rc == -2 ) { + state->retv |= MENU_CUSTOM_ACTION; + } + + state->quit = TRUE; + return; + } + // Key press is handled by entry box. + else if ( rc == 1 ) { + state->refilter = TRUE; + state->update = TRUE; + return; + } + else if ( rc == 2 ) { + // redraw. + state->update = TRUE; + return; + } } - int rc = textbox_keypress ( state->text, pad, len, modstate, key ); - // Row is accepted. - if ( rc < 0 ) { - // If a valid item is selected, return that.. - state->selected_line = UINT32_MAX; - if ( state->selected < state->filtered_lines ) { - ( state->selected_line ) = state->line_map[state->selected]; - state->retv = MENU_OK; - } - else{ - // Nothing entered and nothing selected. - state->retv = MENU_CUSTOM_INPUT; - } - if ( rc == -2 ) { - state->retv |= MENU_CUSTOM_ACTION; - } - - state->quit = TRUE; - } - // Key press is handled by entry box. - else if ( rc == 1 ) { + if ( ( len > 0 ) && ( textbox_keypress ( state->text, pad, len ) ) ) { state->refilter = TRUE; state->update = TRUE; - } - else if ( rc == 2 ) { - // redraw. - state->update = TRUE; + return; } }