Added the uiDrawCaret() function for drawing text carets. Now to write the backend-specific code and test it out.

This commit is contained in:
Pietro Gagliardi 2017-02-10 16:29:36 -05:00
parent b52600d9cd
commit 642363ccae
4 changed files with 66 additions and 0 deletions

View File

@ -6,6 +6,7 @@ list(APPEND _LIBUI_SOURCES
common/areaevents.c common/areaevents.c
common/control.c common/control.c
common/debug.c common/debug.c
common/drawtext.c
common/matrix.c common/matrix.c
common/shouldquit.c common/shouldquit.c
common/userbugs.c common/userbugs.c

53
common/drawtext.c Normal file
View File

@ -0,0 +1,53 @@
// 10 february 2017
#include "../ui.h"
#include "uipriv.h"
void uiDrawCaret(uiDrawContext *c, double x, double y, uiDrawTextLayout *layout, size_t pos, int *line)
{
double xoff;
uiDrawTextLayoutLineMetrics m;
struct caretDrawParams cdp;
uiDrawPath *path;
uiDrawBrush brush;
caretDrawParams(c, &cdp);
xoff = uiDrawTextLayoutByteLocationInLine(layout, pos, *line);
if (xoff < 0) {
size_t start, end;
int incr;
if (*line > (uiDrawTextLayoutNumLines(layout) - 1)) {
*line = (uiDrawTextLayoutNumLines(layout) - 1);
incr = -1;
} else {
uiDrawTextLayoutLineByteRange(layout, *line, &start, &end);
incr = 1;
if (end < pos)
incr = -1;
}
while (xoff < 0) {
*line += incr;
xoff = uiDrawTextLayoutByteLocationInLine(layout, pos, *line);
}
}
uiDrawTextLayoutLineGetMetrics(layout, *line, &m);
uiDrawSave(c);
path = uiDrawNewPath(uiDrawFillModeWinding);
uiDrawPathAddRectangle(path,
// TODO add m.X?
x + xoff, y + m.Y,
cdp.width, m.Height);
uiDrawPathEnd(path);
brush.Type = uiDrawBrushTypeSolid;
brush.R = cdp.r;
brush.G = cdp.g;
brush.B = cdp.b;
brush.A = cdp.a;
uiDrawFill(c, path, &brush);
uiDrawFreePath(path);
uiDrawRestore(c);
}

View File

@ -89,6 +89,16 @@ extern void attrlistForEach(struct attrlist *alist, uiAttributedString *s, uiAtt
extern struct attrlist *attrlistNew(void); extern struct attrlist *attrlistNew(void);
extern void attrlistFree(struct attrlist *alist); extern void attrlistFree(struct attrlist *alist);
// drawtext.c
struct caretDrawParams {
double r;
double g;
double b;
double a;
double width;
};
extern void caretDrawParams(uiDrawContext *c, struct caretDrawParams *p);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -161,3 +161,5 @@ _UI_EXTERN void uiDrawTextLayoutHitTest(uiDrawTextLayout *tl, double x, double y
// indicating you need to move the cursor to another line. // indicating you need to move the cursor to another line.
// TODO make sure this works right for right-aligned and center-aligned lines and justified lines and RTL text // TODO make sure this works right for right-aligned and center-aligned lines and justified lines and RTL text
_UI_EXTERN double uiDrawTextLayoutByteLocationInLine(uiDrawTextLayout *tl, size_t pos, int line); _UI_EXTERN double uiDrawTextLayoutByteLocationInLine(uiDrawTextLayout *tl, size_t pos, int line);
_UI_EXTERN void uiDrawCaret(uiDrawContext *c, double x, double y, uiDrawTextLayout *layout, size_t pos, int *line);