From a1bebc82d858f73ad08d9ac06b2f5eba82ee8c08 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 6 Feb 2017 00:26:22 -0500 Subject: [PATCH] And implemented caret motions in the hit test examples. --- darwin/drawtext.m | 3 +-- examples/drawtext/basic.c | 1 + examples/drawtext/drawtext.h | 3 ++- examples/drawtext/hittest.c | 23 +++++++++++++++++++++++ examples/drawtext/main.c | 3 ++- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/darwin/drawtext.m b/darwin/drawtext.m index 218c06d4..452619f5 100644 --- a/darwin/drawtext.m +++ b/darwin/drawtext.m @@ -349,8 +349,7 @@ void uiDrawTextLayoutByteRangeToRectangle(uiDrawTextLayout *tl, size_t start, si for (i = 0; i < tl->nLines; i++) { line = (CTLineRef) CFArrayGetValueAtIndex(tl->lines, i); range = CTLineGetStringRange(line); - // TODO explain this check - if (range.location >= start) + if (start >= range.location && start < (range.location + range.length)) break; } if (i == tl->nLines) diff --git a/examples/drawtext/basic.c b/examples/drawtext/basic.c index 04ea42e1..c40cf36c 100644 --- a/examples/drawtext/basic.c +++ b/examples/drawtext/basic.c @@ -253,6 +253,7 @@ struct example *mkBasicExample(void) basicExample.name = "Basic Paragraph of Text"; basicExample.panel = uiControl(panel); basicExample.draw = draw; + basicExample.mouse = NULL; attrstr = uiNewAttributedString(text); diff --git a/examples/drawtext/drawtext.h b/examples/drawtext/drawtext.h index ec821eb8..c82383eb 100644 --- a/examples/drawtext/drawtext.h +++ b/examples/drawtext/drawtext.h @@ -7,7 +7,8 @@ struct example { const char *name; uiControl *panel; void (*draw)(uiAreaDrawParams *p); - // TODO mouse and key? + void (*mouse)(uiAreaMouseEvent *e); + // TODO key? }; // main.c diff --git a/examples/drawtext/hittest.c b/examples/drawtext/hittest.c index 554e4a8e..1896fc2a 100644 --- a/examples/drawtext/hittest.c +++ b/examples/drawtext/hittest.c @@ -133,6 +133,28 @@ static void draw(uiAreaDrawParams *p) 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; // TODO share? @@ -159,6 +181,7 @@ struct example *mkHitTestExample(void) hitTestExample.name = "Hit-Testing and Grapheme Boundaries"; hitTestExample.panel = uiControl(panel); hitTestExample.draw = draw; + hitTestExample.mouse = mouse; attrstr = uiNewAttributedString(text); cursorPos = uiAttributedStringLen(attrstr); diff --git a/examples/drawtext/main.c b/examples/drawtext/main.c index 8e5a93ec..552b2f6b 100644 --- a/examples/drawtext/main.c +++ b/examples/drawtext/main.c @@ -31,7 +31,8 @@ static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p) 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)