mirror of
https://github.com/netsurf-browser/netsurf
synced 2024-11-26 08:19:55 +03:00
Use enum instead of magic numbers for tracking key modifier states. Pass special keys to core (e.g. WORD_LEFT, SELECT_ALL, etc).
This commit is contained in:
parent
46527795db
commit
b238791002
@ -74,6 +74,15 @@ struct fbtk_bitmap {
|
||||
int hot_y;
|
||||
};
|
||||
|
||||
/* Key modifier status */
|
||||
typedef enum fbtk_modifier_type {
|
||||
FBTK_MOD_CLEAR = 0,
|
||||
FBTK_MOD_LSHIFT = (1 << 0),
|
||||
FBTK_MOD_RSHIFT = (1 << 1),
|
||||
FBTK_MOD_LCTRL = (1 << 2),
|
||||
FBTK_MOD_RCTRL = (1 << 3)
|
||||
} fbtk_modifier_type;
|
||||
|
||||
typedef int (*fbtk_callback)(fbtk_widget_t *widget, fbtk_callback_info *cbi);
|
||||
|
||||
/* enter pressed on writable icon */
|
||||
@ -198,7 +207,7 @@ bool fbtk_tgrab_pointer(fbtk_widget_t *widget);
|
||||
*
|
||||
* Character mapping between keycode with modifier state and ucs-4.
|
||||
*/
|
||||
int fbtk_keycode_to_ucs4(int code, uint8_t mods);
|
||||
int fbtk_keycode_to_ucs4(int code, fbtk_modifier_type mods);
|
||||
|
||||
|
||||
/******************* Widget Information **********************/
|
||||
|
@ -34,6 +34,7 @@
|
||||
#include "css/css.h"
|
||||
#include "desktop/browser.h"
|
||||
#include "desktop/plotters.h"
|
||||
#include "desktop/textinput.h"
|
||||
|
||||
#include "framebuffer/gui.h"
|
||||
#include "framebuffer/fbtk.h"
|
||||
@ -292,16 +293,46 @@ static int sh_keymap[] = {
|
||||
|
||||
/* exported function documented in fbtk.h */
|
||||
int
|
||||
fbtk_keycode_to_ucs4(int code, uint8_t mods)
|
||||
fbtk_keycode_to_ucs4(int code, fbtk_modifier_type mods)
|
||||
{
|
||||
int ucs4 = -1;
|
||||
|
||||
if (mods) {
|
||||
if (mods & FBTK_MOD_LSHIFT || mods & FBTK_MOD_RSHIFT) {
|
||||
if ((code >= 0) && (code < (int) NOF_ELEMENTS(sh_keymap)))
|
||||
ucs4 = sh_keymap[code];
|
||||
} else {
|
||||
|
||||
} else if (mods == FBTK_MOD_CLEAR) {
|
||||
if ((code >= 0) && (code < (int) NOF_ELEMENTS(keymap)))
|
||||
ucs4 = keymap[code];
|
||||
|
||||
} else if (mods & FBTK_MOD_LCTRL || mods & FBTK_MOD_RCTRL) {
|
||||
switch (code) {
|
||||
case NSFB_KEY_a:
|
||||
ucs4 = KEY_SELECT_ALL;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_c:
|
||||
ucs4 = KEY_COPY_SELECTION;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_u:
|
||||
ucs4 = KEY_CUT_LINE;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_v:
|
||||
ucs4 = KEY_PASTE;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_x:
|
||||
ucs4 = KEY_CUT_SELECTION;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_z:
|
||||
ucs4 = KEY_CLEAR_SELECTION;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ucs4;
|
||||
}
|
||||
|
@ -262,7 +262,7 @@ static int
|
||||
text_input(fbtk_widget_t *widget, fbtk_callback_info *cbi)
|
||||
{
|
||||
int value;
|
||||
static uint8_t modifier = 0;
|
||||
static fbtk_modifier_type modifier = FBTK_MOD_CLEAR;
|
||||
char *temp;
|
||||
plot_font_style_t font_style;
|
||||
int fh;
|
||||
@ -284,19 +284,19 @@ text_input(fbtk_widget_t *widget, fbtk_callback_info *cbi)
|
||||
if (cbi->event->type != NSFB_EVENT_KEY_DOWN) {
|
||||
switch (value) {
|
||||
case NSFB_KEY_RSHIFT:
|
||||
modifier &= ~1;
|
||||
modifier &= ~FBTK_MOD_RSHIFT;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_LSHIFT:
|
||||
modifier &= ~(1<<1);
|
||||
modifier &= ~FBTK_MOD_LSHIFT;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_RCTRL:
|
||||
modifier &= ~(1<<2);
|
||||
modifier &= ~FBTK_MOD_RCTRL;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_LCTRL:
|
||||
modifier &= ~(1<<3);
|
||||
modifier &= ~FBTK_MOD_LCTRL;
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -328,7 +328,7 @@ text_input(fbtk_widget_t *widget, fbtk_callback_info *cbi)
|
||||
|
||||
case NSFB_KEY_RIGHT:
|
||||
if (widget->u.text.idx < widget->u.text.len) {
|
||||
if (modifier == 0)
|
||||
if (modifier == FBTK_MOD_CLEAR)
|
||||
widget->u.text.idx++;
|
||||
else
|
||||
widget->u.text.idx = widget->u.text.len;
|
||||
@ -339,7 +339,7 @@ text_input(fbtk_widget_t *widget, fbtk_callback_info *cbi)
|
||||
|
||||
case NSFB_KEY_LEFT:
|
||||
if (widget->u.text.idx > 0) {
|
||||
if (modifier == 0)
|
||||
if (modifier == FBTK_MOD_CLEAR)
|
||||
widget->u.text.idx--;
|
||||
else
|
||||
widget->u.text.idx = 0;
|
||||
@ -357,23 +357,23 @@ text_input(fbtk_widget_t *widget, fbtk_callback_info *cbi)
|
||||
break;
|
||||
|
||||
case NSFB_KEY_RSHIFT:
|
||||
modifier |= 1;
|
||||
modifier |= FBTK_MOD_RSHIFT;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_LSHIFT:
|
||||
modifier |= 1<<1;
|
||||
modifier |= FBTK_MOD_LSHIFT;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_RCTRL:
|
||||
modifier |= 1<<2;
|
||||
modifier |= FBTK_MOD_RCTRL;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_LCTRL:
|
||||
modifier |= 1<<3;
|
||||
modifier |= FBTK_MOD_LCTRL;
|
||||
break;
|
||||
|
||||
default:
|
||||
if (modifier & 1<<2 || modifier & 1<<3) {
|
||||
if (modifier & FBTK_MOD_LCTRL || modifier & FBTK_MOD_RCTRL) {
|
||||
/* CTRL pressed, don't enter any text */
|
||||
if (value == NSFB_KEY_u) {
|
||||
/* CTRL+U: clear writable */
|
||||
|
@ -688,7 +688,7 @@ static int
|
||||
fb_browser_window_input(fbtk_widget_t *widget, fbtk_callback_info *cbi)
|
||||
{
|
||||
struct gui_window *gw = cbi->context;
|
||||
static uint8_t modifier = 0;
|
||||
static fbtk_modifier_type modifier = FBTK_MOD_CLEAR;
|
||||
int ucs4 = -1;
|
||||
|
||||
LOG(("got value %d", cbi->event->value.keycode));
|
||||
@ -698,41 +698,93 @@ fb_browser_window_input(fbtk_widget_t *widget, fbtk_callback_info *cbi)
|
||||
switch (cbi->event->value.keycode) {
|
||||
|
||||
case NSFB_KEY_PAGEUP:
|
||||
if (browser_window_key_press(gw->bw, KEY_PAGE_UP) == false)
|
||||
widget_scroll_y(gw, -fbtk_get_height(gw->browser), false);
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_PAGE_UP) == false)
|
||||
widget_scroll_y(gw, -fbtk_get_height(
|
||||
gw->browser), false);
|
||||
break;
|
||||
|
||||
case NSFB_KEY_PAGEDOWN:
|
||||
if (browser_window_key_press(gw->bw, KEY_PAGE_DOWN) == false)
|
||||
widget_scroll_y(gw, fbtk_get_height(gw->browser), false);
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_PAGE_DOWN) == false)
|
||||
widget_scroll_y(gw, fbtk_get_height(
|
||||
gw->browser), false);
|
||||
break;
|
||||
|
||||
case NSFB_KEY_RIGHT:
|
||||
if (browser_window_key_press(gw->bw, KEY_RIGHT) == false)
|
||||
widget_scroll_x(gw, 100, false);
|
||||
if (modifier & FBTK_MOD_RCTRL ||
|
||||
modifier & FBTK_MOD_LCTRL) {
|
||||
/* CTRL held */
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_LINE_END) == false)
|
||||
widget_scroll_x(gw, INT_MAX, true);
|
||||
|
||||
} else if (modifier & FBTK_MOD_RSHIFT ||
|
||||
modifier & FBTK_MOD_LSHIFT) {
|
||||
/* SHIFT held */
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_WORD_RIGHT) == false)
|
||||
widget_scroll_x(gw, fbtk_get_width(
|
||||
gw->browser), false);
|
||||
|
||||
} else {
|
||||
/* no modifier */
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_RIGHT) == false)
|
||||
widget_scroll_x(gw, 100, false);
|
||||
}
|
||||
break;
|
||||
|
||||
case NSFB_KEY_LEFT:
|
||||
if (browser_window_key_press(gw->bw, KEY_LEFT) == false)
|
||||
widget_scroll_x(gw, -100, false);
|
||||
if (modifier & FBTK_MOD_RCTRL ||
|
||||
modifier & FBTK_MOD_LCTRL) {
|
||||
/* CTRL held */
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_LINE_START) == false)
|
||||
widget_scroll_x(gw, 0, true);
|
||||
|
||||
} else if (modifier & FBTK_MOD_RSHIFT ||
|
||||
modifier & FBTK_MOD_LSHIFT) {
|
||||
/* SHIFT held */
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_WORD_LEFT) == false)
|
||||
widget_scroll_x(gw, -fbtk_get_width(
|
||||
gw->browser), false);
|
||||
|
||||
} else {
|
||||
/* no modifier */
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_LEFT) == false)
|
||||
widget_scroll_x(gw, -100, false);
|
||||
}
|
||||
break;
|
||||
|
||||
case NSFB_KEY_UP:
|
||||
if (browser_window_key_press(gw->bw, KEY_UP) == false)
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_UP) == false)
|
||||
widget_scroll_y(gw, -100, false);
|
||||
break;
|
||||
|
||||
case NSFB_KEY_DOWN:
|
||||
if (browser_window_key_press(gw->bw, KEY_DOWN) == false)
|
||||
if (browser_window_key_press(gw->bw,
|
||||
KEY_DOWN) == false)
|
||||
widget_scroll_y(gw, 100, false);
|
||||
break;
|
||||
|
||||
case NSFB_KEY_RSHIFT:
|
||||
modifier |= 1;
|
||||
modifier |= FBTK_MOD_RSHIFT;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_LSHIFT:
|
||||
modifier |= 1<<1;
|
||||
modifier |= FBTK_MOD_LSHIFT;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_RCTRL:
|
||||
modifier |= FBTK_MOD_RCTRL;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_LCTRL:
|
||||
modifier |= FBTK_MOD_LCTRL;
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -747,11 +799,19 @@ fb_browser_window_input(fbtk_widget_t *widget, fbtk_callback_info *cbi)
|
||||
case NSFB_EVENT_KEY_UP:
|
||||
switch (cbi->event->value.keycode) {
|
||||
case NSFB_KEY_RSHIFT:
|
||||
modifier &= ~1;
|
||||
modifier &= ~FBTK_MOD_RSHIFT;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_LSHIFT:
|
||||
modifier &= ~(1<<1);
|
||||
modifier &= ~FBTK_MOD_LSHIFT;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_RCTRL:
|
||||
modifier &= ~FBTK_MOD_RCTRL;
|
||||
break;
|
||||
|
||||
case NSFB_KEY_LCTRL:
|
||||
modifier &= ~FBTK_MOD_LCTRL;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
Loading…
Reference in New Issue
Block a user