Fix string truncation in fluid (STR #2722 continued).

This commit fixes the truncated string drawing that was partly fixed
in svn r 9112 (FLTK 1.3.1, 2011-10-01).


git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@12057 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Albrecht Schlosser 2016-10-28 17:26:10 +00:00
parent 8b93da8f20
commit da17124908

View File

@ -166,6 +166,39 @@ extern int show_comments;
////////////////////////////////////////////////////////////////
// Copy the given string str to buffer p with no more than maxl characters.
// Add "..." if string was truncated.
// If parameter quote is true (1) the string is quoted with "".
// Quote characters are NOT counted.
// The returned buffer (string) is terminated with a null byte.
// Returns pointer to end of string (before terminating null byte).
// Note: the buffer p must be large enough to hold (3 * (maxl+1) + 1) bytes
// or (3 * (maxl+1) + 3) bytes if quoted, e.g. "369..." because each UTF-8
// character can be 3 bytes, "..." adds 3 bytes, quotes '""' add 2 bytes,
// and the terminating null byte adds another byte.
static char *copy_trunc(char *p, const char *str, int maxl, int quote) {
int size = 0; // truncated string size in characters
int bs; // size of UTF-8 character in bytes
if (quote) *p++ = '"'; // opening quote
while (size < maxl) { // maximum <maxl> characters
if (!(*str&(-32))) break; // end of string (0 or control char.)
bs = fl_utf8len(*str); // size of next character
if (bs <= 0) break; // some error - leave
while (bs--) *p++ = *str++; // copy that character into the buffer
size++; // count copied characters
}
if (*str) { // string was truncated
strcpy(p,"..."); p += 3;
}
if (quote) *p++ = '"'; // closing quote
*p = 0; // terminating null byte
return p;
}
////////////////////////////////////////////////////////////////
class Widget_Browser : public Fl_Browser_ {
friend class Fl_Type;
@ -287,6 +320,8 @@ void Widget_Browser::item_draw(void *v, int X, int Y, int, int) const {
// cast to a more general type
Fl_Type *l = (Fl_Type *)v;
char buf[256]; // edit buffer: large enough to hold 80 UTF-8 chars + nul
// calculate the horizontal start position of this item
// 3 is the edge of the browser
// 13 is the width of the arrow that indicates children for the item
@ -302,14 +337,7 @@ void Widget_Browser::item_draw(void *v, int X, int Y, int, int) const {
// line inside this browser line
int comment_incr = 0;
if (show_comments && l->comment()) {
char buf[82], *d = buf;
const char *s = l->comment();
for (int i=0; i<80; i++) {
char c = *s++;
if (c==0 || c=='\n') break;
*d++ = c;
}
*d = 0;
copy_trunc(buf, l->comment(), 80, 0);
comment_incr = textsize()-1;
if (l->new_selected) fl_color(fl_contrast(FL_DARK_GREEN,FL_SELECTION_COLOR));
else fl_color(fl_contrast(FL_DARK_GREEN,color()));
@ -367,31 +395,12 @@ void Widget_Browser::item_draw(void *v, int X, int Y, int, int) const {
if (c) {
fl_font(textfont()|FL_BOLD, textsize());
fl_draw(c, X, Y+13);
} else if ((c=l->label())) {
char buf[100]; char* p = buf;
*p++ = '"';
int b,l=strlen(c); // size in bytes
for (int i = 20; i>0;i--) { // maximum 20 characters
if (*c==0) break; // end of string
fl_utf8decode(c, c+l, &b); // b=size of char in bytes
if (b==-1) break; // some error - leave
l-=b; // l = bytes left in string
while (b--)*p++ = *c++; // copy that character into the buffer
}
if (*c) {strcpy(p,"..."); p+=3;} // there would be more to this string
*p++ = '"';
*p = 0;
} else if ((c = l->label())) {
copy_trunc(buf, c, 20, 1); // quoted string
fl_draw(buf, X, Y+13);
}
} else {
const char* c = l->title();
char buf[60]; char* p = buf;
for (int i = 55; i--;) {
if (! (*c & -32)) break;
*p++ = *c++;
}
if (*c) {strcpy(p,"..."); p+=3;}
*p = 0;
copy_trunc(buf, l->title(), 55, 0);
fl_font(textfont() | (l->is_code_block() && (l->level==0 || l->parent->is_class())?0:FL_BOLD), textsize());
fl_draw(buf, X, Y+13);
}
@ -405,6 +414,9 @@ void Widget_Browser::item_draw(void *v, int X, int Y, int, int) const {
}
int Widget_Browser::item_width(void *v) const {
char buf[256]; // edit buffer: large enough to hold 80 UTF-8 chars + nul
Fl_Type *l = (Fl_Type *)v;
if (!l->visible) return 0;
@ -420,27 +432,12 @@ int Widget_Browser::item_width(void *v) const {
if (c) {
fl_font(textfont()|FL_BOLD, textsize());
W += int(fl_width(c));
} else if ((c=l->label())) {
char buf[50]; char* p = buf;
*p++ = '"';
for (int i = 20; i--;) {
if (! (*c & -32)) break;
*p++ = *c++;
}
if (*c) {strcpy(p,"..."); p+=3;}
*p++ = '"';
*p = 0;
} else if (l->label()) {
copy_trunc(buf, l->label(), 20, 1); // quoted string
W += int(fl_width(buf));
}
} else {
const char* c = l->title();
char buf[60]; char* p = buf;
for (int i = 55; i--;) {
if (! (*c & -32)) break;
*p++ = *c++;
}
if (*c) {strcpy(p,"..."); p+=3;}
*p = 0;
copy_trunc(buf, l->title(), 55, 0);
fl_font(textfont() | (l->is_code_block() && (l->level==0 || l->parent->is_class())?0:FL_BOLD), textsize());
W += int(fl_width(buf));
}