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().

This commit is contained in:
2012-09-17 00:57:33 +00:00
parent b8a67bccc0
commit 31f638f04f

View File

@@ -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 Sets but->ofs to make sure text is correctly visible.
* \note Clips right in some cases, this function could be cleaned up. * \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 border = (but->flag & UI_BUT_ALIGN_RIGHT) ? 8 : 10;
int okwidth = BLI_rcti_size_x(rect) - border; int okwidth = BLI_rcti_size_x(rect) - border;
if (but->flag & UI_HAS_ICON) okwidth -= UI_DPI_ICON_SIZE; if (but->flag & UI_HAS_ICON) okwidth -= UI_DPI_ICON_SIZE;
/* need to set this first */ /* need to set this first */
@@ -984,25 +983,46 @@ static void ui_text_leftclip(uiFontStyle *fstyle, uiBut *but, rcti *rect)
if (fstyle->kerning == 1) /* for BLF_width */ if (fstyle->kerning == 1) /* for BLF_width */
BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT); BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
/* if text editing we define ofs dynamically */ but->ofs = 0;
if (but->editstr && but->pos >= 0) { but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr);
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;
}
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;
}
else {
/* define ofs dynamically */
if (but->ofs > but->pos) if (but->ofs > but->pos)
but->ofs = but->pos; but->ofs = but->pos;
if (BLF_width(fstyle->uifont_id, but->drawstr) <= okwidth) {
but->ofs = 0;
}
}
else {
but->ofs = 0;
}
but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr + but->ofs);
while (but->strwidth > okwidth) { while (but->strwidth > okwidth) {
/* textbut exception, clip right when... */
if (but->editstr && but->pos >= 0) {
float width; float width;
char buf[UI_MAX_DRAW_STR]; char buf[UI_MAX_DRAW_STR];
@@ -1025,15 +1045,12 @@ 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)); bytes = BLI_str_utf8_size(BLI_str_find_prev_char_utf8(but->drawstr, but->drawstr + len));
but->drawstr[len - bytes] = 0; but->drawstr[len - bytes] = 0;
} }
}
else {
ui_text_clip_give_next_off(but);
}
but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr + but->ofs); but->strwidth = BLF_width(fstyle->uifont_id, but->drawstr + but->ofs);
if (but->strwidth < 10) break; if (but->strwidth < 10) break;
} }
}
if (fstyle->kerning == 1) { if (fstyle->kerning == 1) {
BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT); BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
@@ -1045,7 +1062,7 @@ static void ui_text_leftclip(uiFontStyle *fstyle, uiBut *but, rcti *rect)
* *
* \note deals with ': ' especially for number buttons * \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 border = (but->flag & UI_BUT_ALIGN_RIGHT) ? 8 : 10;
int okwidth = BLI_rcti_size_x(rect) - border; int okwidth = BLI_rcti_size_x(rect) - border;
@@ -1259,21 +1276,22 @@ static void widget_draw_text(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *b
/* draws text and icons for buttons */ /* draws text and icons for buttons */
static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *but, rcti *rect) static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *but, rcti *rect)
{ {
if (but == NULL) {
if (but == NULL) return; return;
}
/* clip but->drawstr to fit in available space */ /* clip but->drawstr to fit in available space */
if (but->editstr && but->pos >= 0) { 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)) { 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)) { 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)) { 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; else but->ofs = 0;