And implemented caret motions in the hit test examples.

This commit is contained in:
Pietro Gagliardi 2017-02-06 00:26:22 -05:00
parent 64a1167e5f
commit a1bebc82d8
5 changed files with 29 additions and 4 deletions

View File

@ -349,8 +349,7 @@ void uiDrawTextLayoutByteRangeToRectangle(uiDrawTextLayout *tl, size_t start, si
for (i = 0; i < tl->nLines; i++) { for (i = 0; i < tl->nLines; i++) {
line = (CTLineRef) CFArrayGetValueAtIndex(tl->lines, i); line = (CTLineRef) CFArrayGetValueAtIndex(tl->lines, i);
range = CTLineGetStringRange(line); range = CTLineGetStringRange(line);
// TODO explain this check if (start >= range.location && start < (range.location + range.length))
if (range.location >= start)
break; break;
} }
if (i == tl->nLines) if (i == tl->nLines)

View File

@ -253,6 +253,7 @@ struct example *mkBasicExample(void)
basicExample.name = "Basic Paragraph of Text"; basicExample.name = "Basic Paragraph of Text";
basicExample.panel = uiControl(panel); basicExample.panel = uiControl(panel);
basicExample.draw = draw; basicExample.draw = draw;
basicExample.mouse = NULL;
attrstr = uiNewAttributedString(text); attrstr = uiNewAttributedString(text);

View File

@ -7,7 +7,8 @@ struct example {
const char *name; const char *name;
uiControl *panel; uiControl *panel;
void (*draw)(uiAreaDrawParams *p); void (*draw)(uiAreaDrawParams *p);
// TODO mouse and key? void (*mouse)(uiAreaMouseEvent *e);
// TODO key?
}; };
// main.c // main.c

View File

@ -133,6 +133,28 @@ static void draw(uiAreaDrawParams *p)
uiDrawFreeTextLayout(layout); uiDrawFreeTextLayout(layout);
} }
static void mouse(uiAreaMouseEvent *e)
{
uiDrawTextLayout *layout;
uiDrawTextLayoutHitTestResult res;
if (e->Down != 1)
return;
layout = uiDrawNewTextLayout(attrstr,
&defaultFont,
e->AreaWidth - 2 * margins);
uiDrawTextLayoutHitTest(layout,
e->X - margins, e->Y - margins,
&res);
uiDrawFreeTextLayout(layout);
// TODO make a label and set it
cursorPos = res.Pos;
redraw();
}
static struct example hitTestExample; static struct example hitTestExample;
// TODO share? // TODO share?
@ -159,6 +181,7 @@ struct example *mkHitTestExample(void)
hitTestExample.name = "Hit-Testing and Grapheme Boundaries"; hitTestExample.name = "Hit-Testing and Grapheme Boundaries";
hitTestExample.panel = uiControl(panel); hitTestExample.panel = uiControl(panel);
hitTestExample.draw = draw; hitTestExample.draw = draw;
hitTestExample.mouse = mouse;
attrstr = uiNewAttributedString(text); attrstr = uiNewAttributedString(text);
cursorPos = uiAttributedStringLen(attrstr); cursorPos = uiAttributedStringLen(attrstr);

View File

@ -31,7 +31,8 @@ static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p)
static void handlerMouseEvent(uiAreaHandler *a, uiArea *area, uiAreaMouseEvent *e) static void handlerMouseEvent(uiAreaHandler *a, uiArea *area, uiAreaMouseEvent *e)
{ {
// do nothing if (examples[curExample]->mouse != NULL)
examples[curExample]->mouse(e);
} }
static void handlerMouseCrossed(uiAreaHandler *ah, uiArea *a, int left) static void handlerMouseCrossed(uiAreaHandler *ah, uiArea *a, int left)