[Change] Limit the input widgets in a more sane compatible manner.

This commit is contained in:
Allanis 2014-05-24 14:35:32 +01:00
parent 21cfcbfb05
commit e761bb52e1

View File

@ -1940,8 +1940,12 @@ static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) {
int n; int n;
SDLMod mods; SDLMod mods;
/* Must be input widget. */
if(inp->type != WIDGET_INPUT) return 0; if(inp->type != WIDGET_INPUT) return 0;
/* We only actually handle keydowns. */
if(type != SDL_KEYDOWN) return 0;
/* /*
* Handle arrow keys. * Handle arrow keys.
* @todo Finish implementin, no cursor makes it complicated * @todo Finish implementin, no cursor makes it complicated
@ -1965,10 +1969,15 @@ static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) {
} }
#endif #endif
/* Only catch some keys. */
if(!toolkit_isalnum(key) && (key != SDLK_BACKSPACE) &&
(key != SDLK_SPACE) && (key != SDLK_RETURN))
return 0;
mods = SDL_GetModState(); mods = SDL_GetModState();
if(inp->dat.inp.oneline && isascii(key)) { if(inp->dat.inp.oneline && isascii(key)) {
/* Backspace -> delete text. */ /* Backspace -> delete text. */
if((type == SDL_KEYDOWN) && (key == '\b') && (inp->dat.inp.pos > 0)) { if((key == SDLK_BACKSPACE) && (inp->dat.inp.pos > 0)) {
inp->dat.inp.input[--inp->dat.inp.pos] = '\0'; inp->dat.inp.input[--inp->dat.inp.pos] = '\0';
if(inp->dat.inp.view > 0) { if(inp->dat.inp.view > 0) {
@ -1978,7 +1987,8 @@ static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) {
inp->dat.inp.view--; inp->dat.inp.view--;
} }
} }
else if((type == SDL_KEYDOWN) && (inp->dat.inp.pos < inp->dat.inp.max-1)) { /* In limits. */
else if((inp->dat.inp.pos < inp->dat.inp.max-1)) {
if((key == SDLK_RETURN) && !inp->dat.inp.oneline) if((key == SDLK_RETURN) && !inp->dat.inp.oneline)
inp->dat.inp.input[inp->dat.inp.pos++] = '\n'; inp->dat.inp.input[inp->dat.inp.pos++] = '\n';
@ -2312,8 +2322,7 @@ void toolkit_update(void) {
if(nwindows > 0) { if(nwindows > 0) {
wdw = &windows[nwindows-1]; wdw = &windows[nwindows-1];
wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL; wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL;
if(wgt && (wgt->type == WIDGET_INPUT) && if(wgt && (wgt->type == WIDGET_INPUT))
(input_key == SDLK_BACKSPACE || toolkit_isalnum(input_key)))
toolkit_inputInput(SDL_KEYDOWN, wgt, input_key); toolkit_inputInput(SDL_KEYDOWN, wgt, input_key);
} }