From e0e9cd0163a62e74d9837becad4e9097228ce60e Mon Sep 17 00:00:00 2001 From: Martin Felke Date: Thu, 8 Jan 2015 01:14:07 +1100 Subject: PyAPI: Call to get the pixel x,y in a text block This allows scripts to request the screen location of any (line, column) pair. --- source/blender/editors/include/ED_text.h | 3 +++ source/blender/editors/space_text/text_draw.c | 36 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'source/blender/editors') diff --git a/source/blender/editors/include/ED_text.h b/source/blender/editors/include/ED_text.h index 9a36cb3d6ab..5df7d9cfaef 100644 --- a/source/blender/editors/include/ED_text.h +++ b/source/blender/editors/include/ED_text.h @@ -31,8 +31,11 @@ #define __ED_TEXT_H__ struct bContext; +struct SpaceText; +struct ARegion; void ED_text_undo_step(struct bContext *C, int step); +bool ED_text_region_location_from_cursor(struct SpaceText *st, struct ARegion *ar, const int cursor_co[2], int r_pixel_co[2]); #endif /* __ED_TEXT_H__ */ diff --git a/source/blender/editors/space_text/text_draw.c b/source/blender/editors/space_text/text_draw.c index a43d430045e..ed1cbec8fbf 100644 --- a/source/blender/editors/space_text/text_draw.c +++ b/source/blender/editors/space_text/text_draw.c @@ -44,6 +44,8 @@ #include "BKE_text.h" #include "BKE_screen.h" +#include "ED_text.h" + #include "BIF_gl.h" #include "UI_interface.h" @@ -1547,3 +1549,37 @@ void text_update_cursor_moved(bContext *C) text_scroll_to_cursor__area(st, sa, true); } + +/** + * Takes a cursor (row, character) and returns x,y pixel coords. + */ +bool ED_text_region_location_from_cursor(SpaceText *st, ARegion* ar, const int cursor_co[2], int r_pixel_co[2]) +{ + TextLine *line = NULL; + + if (!st->text) { + goto error; + } + + line = BLI_findlink(&st->text->lines, cursor_co[0]); + if (!line || (cursor_co[1] < 0) || (cursor_co[1] > line->len)) { + goto error; + } + else { + int offl, offc; + int linenr_offset = st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; + /* handle tabs as well! */ + int char_pos = text_get_char_pos(st, line->line, cursor_co[1]); + + wrap_offset(st, ar, line, cursor_co[1], &offl, &offc); + r_pixel_co[0] = (char_pos + offc - st->left) * st->cwidth + linenr_offset; + r_pixel_co[1] = (cursor_co[0] + offl - st->top) * (st->lheight_dpi + TXT_LINE_SPACING); + r_pixel_co[1] = (ar->winy - (r_pixel_co[1] + TXT_OFFSET)) - st->lheight_dpi; + } + return true; + + +error: + r_pixel_co[0] = r_pixel_co[1] = -1; + return false; +} -- cgit v1.2.3