From f1ede211fb341ebd9bf0d31ff3ca4cf7aba43b19 Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Wed, 12 Aug 2015 20:22:34 +0200 Subject: [PATCH] First (simple) implementation of textbox behavior --- src/gwin/gwin_textedit.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/gwin/gwin_textedit.c b/src/gwin/gwin_textedit.c index 4aa41d61..fe15f441 100644 --- a/src/gwin/gwin_textedit.c +++ b/src/gwin/gwin_textedit.c @@ -26,9 +26,25 @@ const int TEXT_PADDING = 3; #if GINPUT_NEED_KEYBOARD static void _keyboardEvent(GWidgetObject* gw, GEventKeyboard* pke) { - if (pke->bytecount = 1) { - //gw->text = pke->c[0]; - gwinSetText((GHandle)gw, &(pke->c[0]), TRUE); + // Create a temporary buffer containing the current size + unsigned bufSize = strlen(gwinGetText((GHandle)gw))+1; + char buf[bufSize]; + strncpy(buf, gwinGetText((GHandle)gw), bufSize); + + // Parse the key press + if (pke->bytecount == 1) { + // Check backspace + if (pke->c[0] == GKEY_BACKSPACE) { + buf[strlen(buf)-1] = '\0'; + } + + // Append new character + else { + strncat(buf, &(pke->c[0]), 1); + } + + // Set the new text + gwinSetText((GHandle)gw, buf, TRUE); } _gwinUpdate(&gw); @@ -103,7 +119,6 @@ static void gwinTexteditDefaultDraw(GWidgetObject* gw, void* param) textColor = (gw->g.flags & GWIN_FLG_SYSENABLED) ? gw->pstyle->enabled.text : gw->pstyle->disabled.text; - gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, gw->text, gw->g.font, textColor, gw->pstyle->background, justifyLeft); }