aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJoel Bodenmann <joel@seriouslyembedded.com>2015-12-13 18:46:53 +0100
committerJoel Bodenmann <joel@seriouslyembedded.com>2015-12-13 18:46:53 +0100
commit371e68beeeb0204a8efc963f9d0f68124a551395 (patch)
tree1bff15826e49b43690c3be17972239a21c9660be /src
parent682a5efc42f4117b8e8add981f54392f6798ca40 (diff)
downloaduGFX-371e68beeeb0204a8efc963f9d0f68124a551395.tar.gz
uGFX-371e68beeeb0204a8efc963f9d0f68124a551395.tar.bz2
uGFX-371e68beeeb0204a8efc963f9d0f68124a551395.zip
Allowing the cursor to be set anywhere in the string of a TextEdit widget
Diffstat (limited to 'src')
-rw-r--r--src/gwin/gwin_textedit.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/gwin/gwin_textedit.c b/src/gwin/gwin_textedit.c
index 7de57336..8c60ddd2 100644
--- a/src/gwin/gwin_textedit.c
+++ b/src/gwin/gwin_textedit.c
@@ -46,6 +46,31 @@ static bool_t resizeText(GWidgetObject* gw, size_t pos, int32_t diff) {
return TRUE;
}
+// Function that allows to set the cursor to any position in the string
+// This should be optimized. Currently it is an O(n^2) problem and therefore very
+// slow. An optimized version would copy the behavior of mf_get_string_width()
+// and do the comparation directly inside of that loop so we only iterate
+// the string once.
+static void TextEditMouseDown(GWidgetObject* gw, coord_t x, coord_t y) {
+ uint16_t i = 0;
+
+ // Directly jump to the end of the string
+ if (x > gdispGetStringWidth(gw->text, gw->g.font)) {
+ gw2obj->cursorPos = strlen(gw->text);
+
+ // Otherwise iterate through each character and get the size in pixels to compare
+ } else {
+ i = 1;
+ while (gdispGetStringWidthCount(gw->text, gw->g.font, i) < x) {
+ i++;
+ }
+
+ gw2obj->cursorPos = i-1;
+ }
+
+ _gwinUpdate((GHandle)gw);
+}
+
#if (GFX_USE_GINPUT && GINPUT_NEED_KEYBOARD) || GWIN_NEED_KEYBOARD
static void TextEditKeyboard(GWidgetObject* gw, GEventKeyboard* pke) {
// Only react on KEYDOWN events. Ignore KEYUP events.
@@ -138,7 +163,7 @@ static const gwidgetVMT texteditVMT = {
gwinTexteditDefaultDraw, // default drawing routine
#if GINPUT_NEED_MOUSE
{
- 0, // Process mouse down events (NOT USED)
+ TextEditMouseDown, // Process mouse down events (NOT USED)
0, // Process mouse up events (NOT USED)
0, // Process mouse move events (NOT USED)
},