Expanded the hittest example by handling keyboard input. I should probably rename it to The Caret and Graphemes.

This commit is contained in:
Pietro Gagliardi 2017-02-10 19:22:25 -05:00
parent 5354aa5262
commit 5f05ebbffe
4 changed files with 41 additions and 2 deletions

View File

@ -254,6 +254,7 @@ struct example *mkBasicExample(void)
basicExample.panel = uiControl(panel);
basicExample.draw = draw;
basicExample.mouse = NULL;
basicExample.key = NULL;
attrstr = uiNewAttributedString(text);

View File

@ -8,6 +8,7 @@ struct example {
uiControl *panel;
void (*draw)(uiAreaDrawParams *p);
void (*mouse)(uiAreaMouseEvent *e);
int (*key)(uiAreaKeyEvent *e);
// TODO key?
};

View File

@ -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);

View File

@ -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;
}