From 31f638f04fa897ec79a1b5ed91af80a74ca9fe2a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 17 Sep 2012 00:57:33 +0000 Subject: code cleanup: text clipping function ui_text_leftclip() was also used for cursor clipping which made it a bit confusing, split into 2 functions. ui_text_clip_left(), ui_text_clip_cursor(). --- .../blender/editors/interface/interface_widgets.c | 104 ++++++++++++--------- 1 file changed, 61 insertions(+), 43 deletions(-) (limited to 'source/blender') diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c index 34c4a7dd3f7..f3539af821e 100644 --- a/source/blender/editors/interface/interface_widgets.c +++ b/source/blender/editors/interface/interface_widgets.c @@ -971,11 +971,10 @@ static void ui_text_clip_give_next_off(uiBut *but) * \note Sets but->ofs to make sure text is correctly visible. * \note Clips right in some cases, this function could be cleaned up. */ -static void ui_text_leftclip(uiFontStyle *fstyle, uiBut *but, rcti *rect) +static void ui_text_clip_left(uiFontStyle *fstyle, uiBut *but, rcti *rect) { int border = (but->flag & UI_BUT_ALIGN_RIGHT) ? 8 : 10; int okwidth = BLI_rcti_size_x(rect) - border; - if (but->flag & UI_HAS_ICON) okwidth -= UI_DPI_ICON_SIZE; /* need to set this first */ @@ -984,34 +983,55 @@ static void ui_text_leftclip(uiFontStyle *fstyle, uiBut *but, rcti *rect) if (fstyle->kerning == 1) /* for BLF_width */ BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT); - /* if text editing we define ofs dynamically */ - if (but->editstr && but->pos >= 0) { - if (but->ofs > but->pos) - but->ofs = but->pos; + but->ofs = 0; + but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr); - if (BLF_width(fstyle->uifont_id, but->drawstr) <= okwidth) { - but->ofs = 0; - } + while (but->strwidth > okwidth) { + ui_text_clip_give_next_off(but); + but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr + but->ofs); + if (but->strwidth < 10) break; } - else { + + if (fstyle->kerning == 1) { + BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT); + } +} + +/** + * Cut off the text, taking into account the cursor location (text display while editing). + */ +static void ui_text_clip_cursor(uiFontStyle *fstyle, uiBut *but, rcti *rect) +{ + int border = (but->flag & UI_BUT_ALIGN_RIGHT) ? 8 : 10; + int okwidth = BLI_rcti_size_x(rect) - border; + if (but->flag & UI_HAS_ICON) okwidth -= UI_DPI_ICON_SIZE; + + BLI_assert(but->editstr && but->pos >= 0); + + /* need to set this first */ + uiStyleFontSet(fstyle); + + if (fstyle->kerning == 1) /* for BLF_width */ + BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT); + + if ((but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr)) <= okwidth) { but->ofs = 0; } - - but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr + but->ofs); - - while (but->strwidth > okwidth) { - - /* textbut exception, clip right when... */ - if (but->editstr && but->pos >= 0) { + else { + /* define ofs dynamically */ + if (but->ofs > but->pos) + but->ofs = but->pos; + + while (but->strwidth > okwidth) { float width; char buf[UI_MAX_DRAW_STR]; - + /* copy draw string */ BLI_strncpy_utf8(buf, but->drawstr, sizeof(buf)); /* string position of cursor */ buf[but->pos] = 0; width = BLF_width(fstyle->uifont_id, buf + but->ofs); - + /* if cursor is at 20 pixels of right side button we clip left */ if (width > okwidth - 20) { ui_text_clip_give_next_off(but); @@ -1025,14 +1045,11 @@ static void ui_text_leftclip(uiFontStyle *fstyle, uiBut *but, rcti *rect) bytes = BLI_str_utf8_size(BLI_str_find_prev_char_utf8(but->drawstr, but->drawstr + len)); but->drawstr[len - bytes] = 0; } - } - else { - ui_text_clip_give_next_off(but); - } - but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr + but->ofs); - - if (but->strwidth < 10) break; + but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr + but->ofs); + + if (but->strwidth < 10) break; + } } if (fstyle->kerning == 1) { @@ -1045,7 +1062,7 @@ static void ui_text_leftclip(uiFontStyle *fstyle, uiBut *but, rcti *rect) * * \note deals with ': ' especially for number buttons */ -static void ui_text_label_rightclip(uiFontStyle *fstyle, uiBut *but, rcti *rect) +static void ui_text_clip_right_label(uiFontStyle *fstyle, uiBut *but, rcti *rect) { int border = (but->flag & UI_BUT_ALIGN_RIGHT) ? 8 : 10; int okwidth = BLI_rcti_size_x(rect) - border; @@ -1259,30 +1276,31 @@ static void widget_draw_text(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *b /* draws text and icons for buttons */ static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *but, rcti *rect) { - - if (but == NULL) return; + if (but == NULL) { + return; + } /* clip but->drawstr to fit in available space */ if (but->editstr && but->pos >= 0) { - ui_text_leftclip(fstyle, but, rect); + ui_text_clip_cursor(fstyle, but, rect); } else if (ELEM4(but->type, NUM, NUMABS, NUMSLI, SLI)) { - ui_text_label_rightclip(fstyle, but, rect); + ui_text_clip_right_label(fstyle, but, rect); } else if (ELEM(but->type, TEX, SEARCH_MENU)) { - ui_text_leftclip(fstyle, but, rect); + ui_text_clip_left(fstyle, but, rect); } else if ((but->block->flag & UI_BLOCK_LOOP) && (but->type == BUT)) { - ui_text_leftclip(fstyle, but, rect); + ui_text_clip_left(fstyle, but, rect); } else but->ofs = 0; - + /* check for button text label */ if (but->type == ICONTEXTROW) { widget_draw_icon(but, (BIFIconID) (but->icon + but->iconadd), 1.0f, rect); } else { - + if (but->type == BUT_TOGDUAL) { int dualset = 0; if (but->pointype == UI_BUT_POIN_SHORT) { @@ -1291,7 +1309,7 @@ static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiB else if (but->pointype == UI_BUT_POIN_INT) { dualset = UI_BITBUT_TEST(*(((int *)but->poin) + 1), but->bitnr); } - + widget_draw_icon(but, ICON_DOT, dualset ? 1.0f : 0.25f, rect); } else if (but->type == MENU && (but->flag & UI_BUT_NODE_LINK)) { @@ -1300,21 +1318,21 @@ static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiB widget_draw_icon(but, ICON_LAYER_USED, 1.0f, rect); rect->xmin = tmp; } - + /* If there's an icon too (made with uiDefIconTextBut) then draw the icon * and offset the text label to accommodate it */ - + if (but->flag & UI_HAS_ICON) { widget_draw_icon(but, but->icon + but->iconadd, 1.0f, rect); - + rect->xmin += (int)((float)UI_icon_get_width(but->icon + but->iconadd) * UI_DPI_ICON_FAC); - - if (but->editstr || (but->flag & UI_TEXT_LEFT)) + + if (but->editstr || (but->flag & UI_TEXT_LEFT)) rect->xmin += 5; } - else if ((but->flag & UI_TEXT_LEFT)) + else if ((but->flag & UI_TEXT_LEFT)) rect->xmin += 5; - + /* always draw text for textbutton cursor */ widget_draw_text(fstyle, wcol, but, rect); -- cgit v1.2.3