Files
milsko/src/widget/entry.c
NishiOwO cf8870ece7 calculate utf8 length
git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@241 b9cfdab3-6d41-4d17-bbe4-086880011989
2025-10-09 10:57:21 +00:00

148 lines
3.1 KiB
C

/* $Id$ */
#include <Mw/Milsko.h>
typedef struct text {
int cursor;
} text_t;
static int create(MwWidget handle) {
text_t* t = malloc(sizeof(*t));
t->cursor = 0;
handle->internal = t;
MwSetDefault(handle);
MwSetText(handle, MwNtext, "こんにちは、世界");
MwLLSetCursor(handle->lowlevel, &MwCursorText, &MwCursorTextMask);
return 0;
}
static void destroy(MwWidget handle) {
free(handle->internal);
}
static void draw(MwWidget handle) {
MwRect r;
text_t* t = handle->internal;
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
MwLLColor text = MwParseColor(handle, MwGetText(handle, MwNforeground));
const char* str = MwGetText(handle, MwNtext);
r.x = 0;
r.y = 0;
r.width = MwGetInteger(handle, MwNwidth);
r.height = MwGetInteger(handle, MwNheight);
MwDrawFrame(handle, &r, base, (handle->pressed || MwGetInteger(handle, MwNchecked)) ? 1 : 0);
MwDrawRect(handle, &r, base);
if(str != NULL) {
int w = MwTextWidth(handle, "M");
int h = MwTextHeight(handle, "M");
MwPoint p;
char* show;
int len;
int i;
int start;
int textlen;
MwRect currc;
p.x = (r.width - (r.width / w * w)) / 2;
p.y = r.height / 2;
len = (r.width - p.x * 2) / w;
show = malloc(len + 1);
memset(show, 0, len + 1);
start = (t->cursor - 1) / len;
start *= len;
for(i = start; i < (int)strlen(str) && i < start + len; i++) {
show[i - start] = str[i];
}
MwDrawText(handle, &p, show, 0, MwALIGNMENT_BEGINNING, text);
textlen = (t->cursor - 1) % len + 1;
for(i = 0; i < textlen; i++) show[i] = 'M';
show[i] = 0;
currc.x = p.x + MwTextWidth(handle, show);
currc.y = (r.height - h) / 2;
currc.width = 1;
currc.height = h;
MwDrawRect(handle, &currc, text);
free(show);
}
MwLLFreeColor(text);
MwLLFreeColor(base);
}
static void key(MwWidget handle, int code) {
text_t* t = handle->internal;
const char* str = MwGetText(handle, MwNtext);
char* out;
if(str == NULL) str = "";
if(code == MwLLKeyBackSpace) {
int i, incr = 0;
if(t->cursor == 0) return;
out = malloc(strlen(str) + 1);
t->cursor--;
for(i = 0; str[i] != 0; i++) {
if(i != t->cursor) {
out[incr++] = str[i];
}
}
out[incr++] = 0;
MwSetText(handle, MwNtext, out);
free(out);
} else if(code == MwLLKeyLeft) {
if(t->cursor == 0) return;
t->cursor--;
} else if(code == MwLLKeyRight) {
if(t->cursor == (int)strlen(str)) return;
t->cursor++;
} else {
int i, incr = 0;
out = malloc(strlen(str) + 1 + 1);
for(i = 0; i < t->cursor; i++) out[incr++] = str[i];
out[incr++] = code;
for(i = t->cursor; i < (int)strlen(str); i++) out[incr++] = str[i];
out[incr++] = 0;
t->cursor++;
MwSetText(handle, MwNtext, out);
free(out);
}
MwForceRender(handle);
}
MwClassRec MwEntryClassRec = {
create, /* create */
destroy, /* destroy */
draw, /* draw */
NULL, /* click */
NULL, /* parent_resize */
NULL, /* prop_change */
NULL, /* mouse_move */
MwForceRender, /* mouse_up */
MwForceRender, /* mouse_down */
key, /* key */
NULL,
NULL,
NULL,
NULL,
NULL};
MwClass MwEntryClass = &MwEntryClassRec;