From 5f05ebbffe7cc5bdff988f4be8e6277b1eedf8dd Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 10 Feb 2017 19:22:25 -0500 Subject: [PATCH] Expanded the hittest example by handling keyboard input. I should probably rename it to The Caret and Graphemes. --- examples/drawtext/basic.c | 1 + examples/drawtext/drawtext.h | 1 + examples/drawtext/hittest.c | 38 +++++++++++++++++++++++++++++++++++- examples/drawtext/main.c | 3 ++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/examples/drawtext/basic.c b/examples/drawtext/basic.c index c40cf36c..e1aaca02 100644 --- a/examples/drawtext/basic.c +++ b/examples/drawtext/basic.c @@ -254,6 +254,7 @@ struct example *mkBasicExample(void) basicExample.panel = uiControl(panel); basicExample.draw = draw; basicExample.mouse = NULL; + basicExample.key = NULL; attrstr = uiNewAttributedString(text); diff --git a/examples/drawtext/drawtext.h b/examples/drawtext/drawtext.h index c82383eb..f14ee485 100644 --- a/examples/drawtext/drawtext.h +++ b/examples/drawtext/drawtext.h @@ -8,6 +8,7 @@ struct example { uiControl *panel; void (*draw)(uiAreaDrawParams *p); void (*mouse)(uiAreaMouseEvent *e); + int (*key)(uiAreaKeyEvent *e); // TODO key? }; diff --git a/examples/drawtext/hittest.c b/examples/drawtext/hittest.c index 8fe78ab7..cbd0bd90 100644 --- a/examples/drawtext/hittest.c +++ b/examples/drawtext/hittest.c @@ -35,7 +35,6 @@ static uiCheckbox *showLineBounds; static int caretLine = -1; static size_t caretPos; -//static double caretX; // TODO should be const? static uiDrawBrush fillBrushes[4] = { @@ -136,6 +135,7 @@ static void mouse(uiAreaMouseEvent *e) &caretPos, &caretLine); uiDrawFreeTextLayout(layout); + // TODO move this into the draw handler so it is reflected by keyboard-based position changes // urgh %zd is not supported by MSVC with sprintf() // TODO get that part in test/ about having no other option sprintf(labelText, "pos %d line %d", @@ -145,6 +145,41 @@ static void mouse(uiAreaMouseEvent *e) redraw(); } +static int key(uiAreaKeyEvent *e) +{ + size_t grapheme; + + if (e->Up) + return 0; + if (e->Key != 0) + return 0; + switch (e->ExtKey) { + case uiExtKeyUp: + // TODO + return 1; + case uiExtKeyDown: + // TODO + return 1; + case uiExtKeyLeft: + grapheme = uiAttributedStringByteIndexToGrapheme(attrstr, caretPos); + if (grapheme == 0) + return 0; + grapheme--; + caretPos = uiAttributedStringGraphemeToByteIndex(attrstr, grapheme); + redraw(); + return 1; + case uiExtKeyRight: + grapheme = uiAttributedStringByteIndexToGrapheme(attrstr, caretPos); + if (grapheme == uiAttributedStringNumGraphemes(attrstr)) + return 0; + grapheme++; + caretPos = uiAttributedStringGraphemeToByteIndex(attrstr, grapheme); + redraw(); + return 1; + } + return 0; +} + static struct example hitTestExample; // TODO share? @@ -174,6 +209,7 @@ struct example *mkHitTestExample(void) hitTestExample.panel = uiControl(panel); hitTestExample.draw = draw; hitTestExample.mouse = mouse; + hitTestExample.key = key; attrstr = uiNewAttributedString(text); diff --git a/examples/drawtext/main.c b/examples/drawtext/main.c index 552b2f6b..cc28acd3 100644 --- a/examples/drawtext/main.c +++ b/examples/drawtext/main.c @@ -47,7 +47,8 @@ static void handlerDragBroken(uiAreaHandler *ah, uiArea *a) static int handlerKeyEvent(uiAreaHandler *ah, uiArea *a, uiAreaKeyEvent *e) { - // reject all keys + if (examples[curExample]->key != NULL) + return examples[curExample]->key(e); return 0; }