Implemented the system on the hit-test example and on OS X.
This commit is contained in:
parent
a5dac81855
commit
bbc03a489e
|
@ -328,6 +328,14 @@ void uiDrawTextLayoutHitTest(uiDrawTextLayout *tl, double x, double y, uiDrawTex
|
|||
// TODO
|
||||
}
|
||||
result->Pos = tl->u16tou8[pos];
|
||||
|
||||
// desired caret behavior: clicking on the right trailing space places inserted text at the start of the next line but the caret is on the clicked line at the trailing edge of the last grapheme
|
||||
// hitting Right moves to the second point of the next line, then Left to go to the first, then Left to go to the start of the last character of the original line (so the keyboard can't move the caret back to that clicked space)
|
||||
// this happens regardless of word-wrapping on whitespace, hyphens, or very long words
|
||||
// use CTLineGetOffsetForStringIndex() here instead of x directly as that isn't always adjusted properly
|
||||
result->CaretX = CTLineGetOffsetForStringIndex(line, pos, NULL);
|
||||
result->CaretY = tl->lineMetrics[i].Y;
|
||||
result->CaretHeight = tl->lineMetrics[i].Height;
|
||||
}
|
||||
|
||||
// TODO document this is appropriate for a caret
|
||||
|
|
|
@ -33,7 +33,9 @@ static uiBox *panel;
|
|||
static uiLabel *caretLabel;
|
||||
static uiCheckbox *showLineBounds;
|
||||
|
||||
static size_t cursorPos;
|
||||
static int caretInit = 0;
|
||||
// TODO rename all this to caret, as well as in the text above
|
||||
static double cursorX, cursorY, cursorHeight;
|
||||
|
||||
// TODO should be const?
|
||||
static uiDrawBrush fillBrushes[4] = {
|
||||
|
@ -82,7 +84,6 @@ static void draw(uiAreaDrawParams *p)
|
|||
{
|
||||
uiDrawPath *path;
|
||||
uiDrawTextLayout *layout;
|
||||
uiDrawTextLayoutByteRangeRectangle r;
|
||||
uiDrawBrush brush;
|
||||
|
||||
// only clip the text, not the guides
|
||||
|
@ -103,10 +104,21 @@ static void draw(uiAreaDrawParams *p)
|
|||
|
||||
uiDrawRestore(p->Context);
|
||||
|
||||
uiDrawTextLayoutByteRangeToRectangle(layout, cursorPos, cursorPos, &r);
|
||||
if (!caretInit) {
|
||||
uiDrawTextLayoutByteRangeRectangle r;
|
||||
|
||||
uiDrawTextLayoutByteRangeToRectangle(layout,
|
||||
uiAttributedStringLen(attrstr),
|
||||
uiAttributedStringLen(attrstr),
|
||||
&r);
|
||||
cursorX = r.X;
|
||||
cursorY = r.Y;
|
||||
cursorHeight = r.Height;
|
||||
caretInit = 1;
|
||||
}
|
||||
path = uiDrawNewPath(uiDrawFillModeWinding);
|
||||
uiDrawPathNewFigure(path, margins + r.X, margins + r.Y);
|
||||
uiDrawPathLineTo(path, margins + r.X, margins + r.Y + r.Height);
|
||||
uiDrawPathNewFigure(path, margins + cursorX, margins + cursorY);
|
||||
uiDrawPathLineTo(path, margins + cursorX, margins + cursorY + cursorHeight);
|
||||
uiDrawPathEnd(path);
|
||||
brush.Type = uiDrawBrushTypeSolid;
|
||||
brush.R = 0.0;
|
||||
|
@ -162,13 +174,16 @@ static void mouse(uiAreaMouseEvent *e)
|
|||
|
||||
// urgh %zd is not supported by MSVC with sprintf()
|
||||
// TODO get that part in test/ about having no other option
|
||||
// TODO byte 1 is actually byte 684?!
|
||||
sprintf(labelText, "pos %d line %d x position %s y position %s",
|
||||
(int) (res.Pos), res.Line,
|
||||
positions[res.XPosition],
|
||||
positions[res.YPosition]);
|
||||
uiLabelSetText(caretLabel, labelText);
|
||||
|
||||
cursorPos = res.Pos;
|
||||
cursorX = res.CaretX;
|
||||
cursorY = res.CaretY;
|
||||
cursorHeight = res.CaretHeight;
|
||||
redraw();
|
||||
}
|
||||
|
||||
|
@ -203,7 +218,6 @@ struct example *mkHitTestExample(void)
|
|||
hitTestExample.mouse = mouse;
|
||||
|
||||
attrstr = uiNewAttributedString(text);
|
||||
cursorPos = uiAttributedStringLen(attrstr);
|
||||
|
||||
return &hitTestExample;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue