measure line properly

This commit is contained in:
Nishi 2026-04-27 03:14:12 +09:00
commit ca703f24d5
Signed by: nishi
GPG key ID: 27EF69B208EB9343
3 changed files with 53 additions and 17 deletions

View file

@ -211,7 +211,7 @@ static MwWidget MwCreateWidget_Internal(MwClass widget_class, const char* name,
h->internal = NULL;
h->opaque = NULL;
h->user = NULL;
h->user = NULL;
h->top_step = 0;
h->draw_queue = NULL;
@ -1068,10 +1068,10 @@ void MwGetClipboard(MwWidget handle, int clipboard_type) {
MwLLGetClipboard(handle->lowlevel, clipboard_type);
}
void MwSetUser(MwWidget handle, void* user){
void MwSetUser(MwWidget handle, void* user) {
handle->user = user;
}
void* MwGetUser(MwWidget handle){
void* MwGetUser(MwWidget handle) {
return handle->user;
}

View file

@ -71,19 +71,53 @@ void MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text,
}
int MwTextWidth(MwWidget handle, MwFLFont ttf, const char* text) {
/* TODO: check newline */
#ifdef TTF
int st;
if(ttf <= MwFLBuildFont(0xff)) ttf = assume_ttf(handle, ttf);
if(MwFLTextWidth)
if(!MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextWidth(ttf, text)) != -1) return st;
#else
int len = 0;
int st;
int cp;
char* input = (char*)text;
char* last = input;
#ifndef TTF
(void)handle;
(void)ttf;
#endif
return MwUTF8Length(text) * FontWidth;
#ifdef TTF
if(ttf <= MwFLBuildFont(0xff)) ttf = assume_ttf(handle, ttf);
#endif
while(*input != 0) {
input += MwUTF8ToUTF32(input, &cp);
if(*input == 0 || cp == '\n') {
char* line = malloc(input - last + 1);
int i;
memcpy(line, last, input - last);
line[input - last] = 0;
for(i = 0; line[i] != 0; i++) {
if(line[i] == '\r' || line[i] == '\n') {
line[i] = 0;
break;
}
}
#ifdef TTF
if(MwFLTextWidth && !MwGetInteger(handle, MwNbitmapFont) && ttf != NULL && (st = MwFLTextWidth(ttf, line)) != -1) {
} else
#endif
st = MwUTF8Length(line) * FontWidth;
if(st > len) len = st;
free(line);
last = input + 1;
}
}
return len;
}
int MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) {

View file

@ -193,14 +193,13 @@ static void frame_draw(MwWidget handle) {
char* str;
int l = (j == 0 ? MwGetInteger(handle->parent, MwNleftPadding) : 0);
MwPoint p2 = p;
p2.y += (MwTextHeight(handle, Font, "M") + Padding) / 2;
if(t == NULL) t = "";
str = MwStringDuplicate(t);
if(MwUTF8Length(str) > (get_col_width(lb, j) - l) / MwTextWidth(handle, Font, "M")) {
int ind = (get_col_width(lb, j) - l) / MwTextWidth(handle, Font, "M");
int ind = (get_col_width(lb, j) - l) / MwTextWidth(handle, Font, "M") - 3;
int seek = 0;
int l = 0;
@ -219,18 +218,21 @@ static void frame_draw(MwWidget handle) {
if(j == (arrlen(lb->list[i].name) - 1)) p.x -= MwDefaultBorderWidth(handle);
if(arrlen(lb->alignment) <= j || lb->alignment[j] == MwALIGNMENT_BEGINNING) {
p.x += 4;
p2.x = p.x;
MwDrawText(handle, Font, &p2, str, MwALIGNMENT_BEGINNING, selected ? base2 : text2);
p.x -= 4;
p.x += get_col_width(lb, j);
} else if(lb->alignment[j] == MwALIGNMENT_CENTER) {
p.x += (get_col_width(lb, j) - l) / 2;
p2.x = p.x;
MwDrawText(handle, Font, &p2, str, MwALIGNMENT_CENTER, selected ? base2 : text2);
p.x += (get_col_width(lb, j) - l) / 2;
p.x += l;
} else if(lb->alignment[j] == MwALIGNMENT_END) {
p.x += get_col_width(lb, j);
p.x -= 4;
p2.x = p.x;
MwDrawText(handle, Font, &p2, str, MwALIGNMENT_END, selected ? base2 : text2);
p.x += 4;
}
@ -410,8 +412,8 @@ static void redrawIfNeeded(MwWidget handle, int row) {
static int mwListBoxSetImpl(MwWidget handle, int row, int col, const char* text) {
MwListBox lb = handle->internal;
MwListBoxEntry entry;
char* t = text == NULL ? NULL : MwStringDuplicate(text);
int new = 0;
char* t = text == NULL ? NULL : MwStringDuplicate(text);
int new = 0;
if(row == -1) row = arrlen(lb->list);
entry.name = NULL;
@ -439,7 +441,7 @@ static int mwListBoxSetImpl(MwWidget handle, int row, int col, const char* text)
static void mwListBoxSetIconImpl(MwWidget handle, int index, MwLLPixmap icon) {
MwListBox lb = handle->internal;
MwListBoxEntry entry;
int new = 0;
int new = 0;
if(index == -1) index = arrlen(lb->list);
entry.name = NULL;