From 62ebb863edd1efc9f6f616a82d4cfe6fb2ea473f Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Thu, 27 Oct 2022 22:22:11 +0200 Subject: [PATCH] [Textbox] Add a 'get_cursor_x_pos' function. --- include/widgets/textbox.h | 9 +++++++++ source/widgets/textbox.c | 19 +++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/include/widgets/textbox.h b/include/widgets/textbox.h index 7ec36ce1..a3383208 100644 --- a/include/widgets/textbox.h +++ b/include/widgets/textbox.h @@ -76,6 +76,8 @@ typedef struct { double yalign; double xalign; + int cursor_x_pos; + TBFontConfig *tbfc; PangoEllipsizeMode emode; @@ -335,5 +337,12 @@ void textbox_cursor_end(textbox *tb); * Set the ellipsizing mode used on the string. */ void textbox_set_ellipsize(textbox *tb, PangoEllipsizeMode mode); + +/** + * @param tb Handle to the textbox + * + * @returns the position of the cursor (0 if no cursor). + */ +int textbox_get_cursor_x_pos(const textbox *tb); /**@}*/ #endif // ROFI_TEXTBOX_H diff --git a/source/widgets/textbox.c b/source/widgets/textbox.c index 80e4be09..758e32d5 100644 --- a/source/widgets/textbox.c +++ b/source/widgets/textbox.c @@ -525,7 +525,7 @@ static void textbox_draw(widget *wid, cairo_t *draw) { // draw the cursor rofi_theme_get_color(WIDGET(tb), "text-color", draw); - if (tb->flags & TB_EDITABLE && tb->blink) { + if (tb->flags & TB_EDITABLE) { // We want to place the cursor based on the text shown. const char *text = pango_layout_get_text(tb->layout); // Clamp the position, should not be needed, but we are paranoid. @@ -538,9 +538,14 @@ static void textbox_draw(widget *wid, cairo_t *draw) { int cursor_y = pos.y / PANGO_SCALE; int cursor_height = pos.height / PANGO_SCALE; int cursor_width = 2; - cairo_rectangle(draw, x + cursor_x, y + cursor_y, cursor_width, - cursor_height); - cairo_fill(draw); + if ((x + cursor_x) != tb->cursor_x_pos) { + tb->cursor_x_pos = x + cursor_x; + } + if (tb->blink) { + cairo_rectangle(draw, x + cursor_x, y + cursor_y, cursor_width, + cursor_height); + cairo_fill(draw); + } } } @@ -993,3 +998,9 @@ void textbox_set_ellipsize(textbox *tb, PangoEllipsizeMode mode) { } } } +int textbox_get_cursor_x_pos(const textbox *tb) { + if (tb == NULL) { + return 0; + } + return tb->cursor_x_pos; +}