And implemented uiDrawCaret() on GTK+.

This commit is contained in:
Pietro Gagliardi 2017-02-10 16:53:08 -05:00
parent 642363ccae
commit b5b0fae052
5 changed files with 47 additions and 5 deletions

View File

@ -10,8 +10,6 @@ void uiDrawCaret(uiDrawContext *c, double x, double y, uiDrawTextLayout *layout,
uiDrawPath *path;
uiDrawBrush brush;
caretDrawParams(c, &cdp);
xoff = uiDrawTextLayoutByteLocationInLine(layout, pos, *line);
if (xoff < 0) {
size_t start, end;
@ -33,12 +31,14 @@ void uiDrawCaret(uiDrawContext *c, double x, double y, uiDrawTextLayout *layout,
}
uiDrawTextLayoutLineGetMetrics(layout, *line, &m);
caretDrawParams(c, m.Height, &cdp);
uiDrawSave(c);
path = uiDrawNewPath(uiDrawFillModeWinding);
uiDrawPathAddRectangle(path,
// TODO add m.X?
x + xoff, y + m.Y,
x + xoff - cdp.xoff, y + m.Y,
cdp.width, m.Height);
uiDrawPathEnd(path);
brush.Type = uiDrawBrushTypeSolid;

View File

@ -95,9 +95,10 @@ struct caretDrawParams {
double g;
double b;
double a;
double xoff;
double width;
};
extern void caretDrawParams(uiDrawContext *c, struct caretDrawParams *p);
extern void caretDrawParams(uiDrawContext *c, double height, struct caretDrawParams *p);
#ifdef __cplusplus
}

View File

@ -111,9 +111,9 @@ static void draw(uiAreaDrawParams *p)
caretLine = uiDrawTextLayoutNumLines(layout) - 1;
caretPos = uiAttributedStringLen(attrstr);
}
#if 0
caretX = uiDrawTextLayoutByteLocationInLine(layout,
caretPos, caretLine);
printf("%g\n", caretX);
uiDrawTextLayoutLineGetMetrics(layout, caretLine, &m);
path = uiDrawNewPath(uiDrawFillModeWinding);
uiDrawPathNewFigure(path, margins + caretX, margins + m.Y);
@ -126,6 +126,10 @@ printf("%g\n", caretX);
brush.A = 1.0;
uiDrawStroke(p->Context, path, &brush, &strokeParams);
uiDrawFreePath(path);
#else
uiDrawCaret(p->Context, margins, margins,
layout, caretPos, &caretLine);
#endif
if (uiCheckboxChecked(showLineBounds)) {
int i, n;

View File

@ -163,3 +163,4 @@ _UI_EXTERN void uiDrawTextLayoutHitTest(uiDrawTextLayout *tl, double x, double y
_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);
// TODO allow blinking

View File

@ -244,3 +244,39 @@ double uiDrawTextLayoutByteLocationInLine(uiDrawTextLayout *tl, size_t pos, int
// TODO unref pll?
return pangoToCairo(pangox);
}
// note: we can't use gtk_render_insertion_cursor() because that doesn't take information about what line to render on
// we'll just recreate what it does
void caretDrawParams(uiDrawContext *c, double height, struct caretDrawParams *p)
{
GdkColor *color;
GdkRGBA rgba;
gfloat aspectRatio;
gint width, xoff;
gtk_style_context_get_style(c->style,
"cursor-color", &color,
"cursor-aspect-ratio", &aspectRatio,
NULL);
if (color != NULL) {
p->r = ((double) (color->red)) / 65535.0;
p->g = ((double) (color->green)) / 65535.0;
p->b = ((double) (color->blue)) / 65535.0;
p->a = 1.0;
gdk_color_free(color);
} else {
gtk_style_context_get_color(c->style, GTK_STATE_FLAG_NORMAL, &rgba);
p->r = rgba.red;
p->g = rgba.green;
p->b = rgba.blue;
p->a = rgba.alpha;
}
// GTK+ itself uses integer arithmetic here; let's do the same
width = height * aspectRatio + 1;
// TODO this is for LTR
xoff = width / 2;
p->xoff = xoff;
p->width = width;
}