diff options
author | Martin Felke <martin.felke@googlemail.com> | 2015-01-07 17:14:07 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-01-07 17:22:11 +0300 |
commit | e0e9cd0163a62e74d9837becad4e9097228ce60e (patch) | |
tree | 9113d0789883de7673d81ee63cdf20b212b13bf3 /source/blender/editors/space_text/text_draw.c | |
parent | 14f2597d7e518be552020875e52c5d6804b48a1e (diff) |
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.
Diffstat (limited to 'source/blender/editors/space_text/text_draw.c')
-rw-r--r-- | source/blender/editors/space_text/text_draw.c | 36 |
1 files changed, 36 insertions, 0 deletions
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; +} |