Expanded the hittest example by handling keyboard input. I should probably rename it to The Caret and Graphemes.
This commit is contained in:
parent
5354aa5262
commit
5f05ebbffe
|
@ -254,6 +254,7 @@ struct example *mkBasicExample(void)
|
||||||
basicExample.panel = uiControl(panel);
|
basicExample.panel = uiControl(panel);
|
||||||
basicExample.draw = draw;
|
basicExample.draw = draw;
|
||||||
basicExample.mouse = NULL;
|
basicExample.mouse = NULL;
|
||||||
|
basicExample.key = NULL;
|
||||||
|
|
||||||
attrstr = uiNewAttributedString(text);
|
attrstr = uiNewAttributedString(text);
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ struct example {
|
||||||
uiControl *panel;
|
uiControl *panel;
|
||||||
void (*draw)(uiAreaDrawParams *p);
|
void (*draw)(uiAreaDrawParams *p);
|
||||||
void (*mouse)(uiAreaMouseEvent *e);
|
void (*mouse)(uiAreaMouseEvent *e);
|
||||||
|
int (*key)(uiAreaKeyEvent *e);
|
||||||
// TODO key?
|
// TODO key?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,6 @@ static uiCheckbox *showLineBounds;
|
||||||
|
|
||||||
static int caretLine = -1;
|
static int caretLine = -1;
|
||||||
static size_t caretPos;
|
static size_t caretPos;
|
||||||
//static double caretX;
|
|
||||||
|
|
||||||
// TODO should be const?
|
// TODO should be const?
|
||||||
static uiDrawBrush fillBrushes[4] = {
|
static uiDrawBrush fillBrushes[4] = {
|
||||||
|
@ -136,6 +135,7 @@ static void mouse(uiAreaMouseEvent *e)
|
||||||
&caretPos, &caretLine);
|
&caretPos, &caretLine);
|
||||||
uiDrawFreeTextLayout(layout);
|
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()
|
// urgh %zd is not supported by MSVC with sprintf()
|
||||||
// TODO get that part in test/ about having no other option
|
// TODO get that part in test/ about having no other option
|
||||||
sprintf(labelText, "pos %d line %d",
|
sprintf(labelText, "pos %d line %d",
|
||||||
|
@ -145,6 +145,41 @@ static void mouse(uiAreaMouseEvent *e)
|
||||||
redraw();
|
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;
|
static struct example hitTestExample;
|
||||||
|
|
||||||
// TODO share?
|
// TODO share?
|
||||||
|
@ -174,6 +209,7 @@ struct example *mkHitTestExample(void)
|
||||||
hitTestExample.panel = uiControl(panel);
|
hitTestExample.panel = uiControl(panel);
|
||||||
hitTestExample.draw = draw;
|
hitTestExample.draw = draw;
|
||||||
hitTestExample.mouse = mouse;
|
hitTestExample.mouse = mouse;
|
||||||
|
hitTestExample.key = key;
|
||||||
|
|
||||||
attrstr = uiNewAttributedString(text);
|
attrstr = uiNewAttributedString(text);
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,8 @@ static void handlerDragBroken(uiAreaHandler *ah, uiArea *a)
|
||||||
|
|
||||||
static int handlerKeyEvent(uiAreaHandler *ah, uiArea *a, uiAreaKeyEvent *e)
|
static int handlerKeyEvent(uiAreaHandler *ah, uiArea *a, uiAreaKeyEvent *e)
|
||||||
{
|
{
|
||||||
// reject all keys
|
if (examples[curExample]->key != NULL)
|
||||||
|
return examples[curExample]->key(e);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue