From dd62a2c3756d4335479a561a52468a548f2f1d65 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 31 Dec 2012 16:24:49 +0000 Subject: text autocomplete - make the popup box line up the X axis with the current word. - add poll function for the operator --- source/blender/blenkernel/BKE_text.h | 1 + source/blender/blenkernel/intern/text.c | 11 ++++++++++ .../blender/editors/space_text/text_autocomplete.c | 10 ++------- source/blender/editors/space_text/text_draw.c | 24 ++++++++++++++-------- source/blender/editors/space_text/text_intern.h | 3 +++ source/blender/editors/space_text/text_ops.c | 2 +- 6 files changed, 34 insertions(+), 17 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index accac8694a9..1e3dd426efa 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -106,6 +106,7 @@ int text_check_delim(const char ch); int text_check_digit(const char ch); int text_check_identifier(const char ch); int text_check_whitespace(const char ch); +int text_find_identifier_start(const char *str, int i); enum { TXT_MOVE_LINE_UP = -1, diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index 322b77e0462..a9a9f6538d3 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -2936,3 +2936,14 @@ int text_check_whitespace(const char ch) return 1; return 0; } + +int text_find_identifier_start(const char *str, int i) +{ + while (i--) { + if (!text_check_identifier(str[i])) { + break; + } + } + i++; + return i; +} diff --git a/source/blender/editors/space_text/text_autocomplete.c b/source/blender/editors/space_text/text_autocomplete.c index 097174f2645..46e2f99d3fa 100644 --- a/source/blender/editors/space_text/text_autocomplete.c +++ b/source/blender/editors/space_text/text_autocomplete.c @@ -147,13 +147,7 @@ static GHash *text_autocomplete_build(Text *text) /* first get the word we're at */ { - int i = text->curc; - while (i--) { - if (!text_check_identifier(text->curl->line[i])) { - break; - } - } - i++; + const int i = text_find_identifier_start(text->curl->line, text->curc); seek_len = text->curc - i; seek = text->curl->line + i; @@ -550,7 +544,7 @@ void TEXT_OT_autocomplete(wmOperatorType *ot) ot->invoke = text_autocomplete_invoke; ot->cancel = text_autocomplete_cancel; ot->modal = text_autocomplete_modal; - //ot->poll = ED_operator_view3d_active; + ot->poll = text_space_edit_poll; /* flags */ ot->flag = OPTYPE_BLOCKING; diff --git a/source/blender/editors/space_text/text_draw.c b/source/blender/editors/space_text/text_draw.c index 6e33fc122b5..a26b4f56e02 100644 --- a/source/blender/editors/space_text/text_draw.c +++ b/source/blender/editors/space_text/text_draw.c @@ -958,8 +958,10 @@ static void draw_suggestion_list(SpaceText *st, ARegion *ar) TextLine *tmp; char str[SUGG_LIST_WIDTH + 1]; int w, boxw = 0, boxh, i, l, x, y, b, *top; + const int lheight = st->lheight_dpi + TXT_LINE_SPACING; + const int margin_x = 2; - if (!st || !st->text) return; + if (!st->text) return; if (!texttool_text_is_active(st->text)) return; first = texttool_suggest_first(); @@ -981,14 +983,20 @@ static void draw_suggestion_list(SpaceText *st, ARegion *ar) else { x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET - 4; } - y = ar->winy - (st->lheight_dpi + TXT_LINE_SPACING) * l - 2; + /* offset back so the start of the text lines up with the suggestions, + * not essential but makes suggestions easier to follow */ + x -= st->cwidth * (st->text->curc - text_find_identifier_start(st->text->curl->line, st->text->curc)); + y = ar->winy - lheight * l - 2; boxw = SUGG_LIST_WIDTH * st->cwidth + 20; - boxh = SUGG_LIST_SIZE * st->lheight_dpi + 8; + boxh = SUGG_LIST_SIZE * lheight + 8; + /* not needed but stands out nicer */ + uiDrawBoxShadow(220, x, y - boxh, x + boxw, y); + UI_ThemeColor(TH_SHADE1); glRecti(x - 1, y + 1, x + boxw + 1, y - boxh - 1); - UI_ThemeColor(TH_BACK); + UI_ThemeColorShade(TH_BACK, 16); glRecti(x, y, x + boxw, y - boxh); /* Set the top 'item' of the visible list */ @@ -996,7 +1004,7 @@ static void draw_suggestion_list(SpaceText *st, ARegion *ar) for (i = 0; i < SUGG_LIST_SIZE && item; i++, item = item->next) { - y -= st->lheight_dpi; + y -= lheight; BLI_strncpy(str, item->name, SUGG_LIST_WIDTH); @@ -1004,7 +1012,7 @@ static void draw_suggestion_list(SpaceText *st, ARegion *ar) if (item == sel) { UI_ThemeColor(TH_SHADE2); - glRecti(x + 16, y - 3, x + 16 + w, y + st->lheight_dpi - 3); + glRecti(x + margin_x, y - 3, x + margin_x + w, y + lheight - 3); } b = 1; /* b=1 color block, text is default. b=0 no block, color text */ switch (item->type) { @@ -1018,7 +1026,7 @@ static void draw_suggestion_list(SpaceText *st, ARegion *ar) glRecti(x + 8, y + 2, x + 11, y + 5); UI_ThemeColor(TH_TEXT); } - text_draw(st, str, 0, 0, 1, x + 16, y - 1, NULL); + text_draw(st, str, 0, 0, 1, x + margin_x, y - 1, NULL); if (item == last) break; } @@ -1031,7 +1039,7 @@ static void draw_cursor(SpaceText *st, ARegion *ar) Text *text = st->text; int vcurl, vcurc, vsell, vselc, hidden = 0; int x, y, w, i; - int lheight = st->lheight_dpi + TXT_LINE_SPACING; + const int lheight = st->lheight_dpi + TXT_LINE_SPACING; /* Draw the selection */ if (text->curl != text->sell || text->curc != text->selc) { diff --git a/source/blender/editors/space_text/text_intern.h b/source/blender/editors/space_text/text_intern.h index 8fa74a5c866..799bc49b624 100644 --- a/source/blender/editors/space_text/text_intern.h +++ b/source/blender/editors/space_text/text_intern.h @@ -143,6 +143,9 @@ void TEXT_OT_to_3d_object(struct wmOperatorType *ot); void TEXT_OT_resolve_conflict(struct wmOperatorType *ot); +int text_space_edit_poll(struct bContext *C); + +/* text_autocomplete.c */ void TEXT_OT_autocomplete(struct wmOperatorType *ot); /* space_text.c */ diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index 01041c0e385..89cc396efbb 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -98,7 +98,7 @@ static int text_edit_poll(bContext *C) return 1; } -static int text_space_edit_poll(bContext *C) +int text_space_edit_poll(bContext *C) { SpaceText *st = CTX_wm_space_text(C); Text *text = CTX_data_edit_text(C); -- cgit v1.2.3