2025-09-28 06:52:49 +00:00
|
|
|
/* $Id$ */
|
2025-09-29 00:10:20 +00:00
|
|
|
#include <Mw/Milsko.h>
|
2025-09-28 06:52:49 +00:00
|
|
|
|
2025-09-28 07:03:05 +00:00
|
|
|
#include "stb_ds.h"
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
static void lldrawhandler(MwLL handle) {
|
|
|
|
|
MwWidget h = (MwWidget)handle->user;
|
|
|
|
|
MwDispatch(h, draw);
|
2025-09-28 09:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
static void lluphandler(MwLL handle) {
|
|
|
|
|
MwWidget h = (MwWidget)handle->user;
|
2025-09-29 00:10:20 +00:00
|
|
|
h->pressed = 0;
|
2025-09-28 22:10:42 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwDispatch(h, click);
|
2025-09-28 22:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
static void lldownhandler(MwLL handle) {
|
|
|
|
|
MwWidget h = (MwWidget)handle->user;
|
2025-09-29 00:10:20 +00:00
|
|
|
h->pressed = 1;
|
2025-09-28 22:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 05:58:28 +00:00
|
|
|
static void llresizehandler(MwLL handle) {
|
|
|
|
|
MwWidget h = (MwWidget)handle->user;
|
|
|
|
|
|
|
|
|
|
MwDispatchUserHandler(h, MwNresizeHandler, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 07:05:09 +00:00
|
|
|
static void llclosehandler(MwLL handle) {
|
|
|
|
|
MwWidget h = (MwWidget)handle->user;
|
|
|
|
|
|
|
|
|
|
h->close = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:19:49 +00:00
|
|
|
MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent, int x, int y, unsigned int width, unsigned int height) {
|
2025-09-29 00:04:04 +00:00
|
|
|
MwWidget h = malloc(sizeof(*h));
|
2025-09-28 07:03:05 +00:00
|
|
|
|
2025-09-28 10:06:00 +00:00
|
|
|
h->name = malloc(strlen(name) + 1);
|
|
|
|
|
strcpy(h->name, name);
|
|
|
|
|
|
2025-09-29 00:19:49 +00:00
|
|
|
h->parent = parent;
|
|
|
|
|
h->children = NULL;
|
|
|
|
|
h->lowlevel = MwLLCreate(parent == NULL ? NULL : parent->lowlevel, x, y, width, height);
|
|
|
|
|
h->widget_class = widget_class;
|
|
|
|
|
h->pressed = 0;
|
2025-09-29 07:05:09 +00:00
|
|
|
h->close = 0;
|
2025-09-28 07:03:05 +00:00
|
|
|
|
2025-09-29 05:58:28 +00:00
|
|
|
h->lowlevel->user = h;
|
|
|
|
|
h->lowlevel->handler->draw = lldrawhandler;
|
|
|
|
|
h->lowlevel->handler->up = lluphandler;
|
|
|
|
|
h->lowlevel->handler->down = lldownhandler;
|
|
|
|
|
h->lowlevel->handler->resize = llresizehandler;
|
2025-09-29 07:05:09 +00:00
|
|
|
h->lowlevel->handler->close = llclosehandler;
|
2025-09-28 09:50:28 +00:00
|
|
|
|
2025-09-28 08:37:12 +00:00
|
|
|
if(parent != NULL) arrput(parent->children, h);
|
2025-09-28 07:03:05 +00:00
|
|
|
|
2025-09-28 09:14:57 +00:00
|
|
|
sh_new_strdup(h->text);
|
|
|
|
|
sh_new_strdup(h->integer);
|
2025-09-29 05:02:24 +00:00
|
|
|
sh_new_strdup(h->handler);
|
2025-09-28 09:14:57 +00:00
|
|
|
|
|
|
|
|
shdefault(h->integer, -1);
|
2025-09-28 22:21:51 +00:00
|
|
|
shdefault(h->text, NULL);
|
|
|
|
|
shdefault(h->handler, NULL);
|
2025-09-28 09:14:57 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwDispatch(h, create);
|
2025-09-28 09:27:28 +00:00
|
|
|
|
2025-09-28 07:03:05 +00:00
|
|
|
return h;
|
2025-09-28 06:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:19:49 +00:00
|
|
|
MwWidget MwVaCreateWidget(MwClass widget_class, const char* name, MwWidget parent, int x, int y, unsigned int width, unsigned int height, ...) {
|
2025-09-29 00:04:04 +00:00
|
|
|
MwWidget h;
|
2025-09-29 00:10:20 +00:00
|
|
|
va_list va;
|
2025-09-28 22:29:05 +00:00
|
|
|
|
|
|
|
|
va_start(va, height);
|
2025-09-29 00:19:49 +00:00
|
|
|
h = MwVaListCreateWidget(widget_class, name, parent, x, y, width, height, va);
|
2025-09-28 22:29:05 +00:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:19:49 +00:00
|
|
|
MwWidget MwVaListCreateWidget(MwClass widget_class, const char* name, MwWidget parent, int x, int y, unsigned int width, unsigned int height, va_list va) {
|
2025-09-29 00:04:04 +00:00
|
|
|
MwWidget h;
|
2025-09-28 22:29:05 +00:00
|
|
|
|
2025-09-29 00:19:49 +00:00
|
|
|
h = MwCreateWidget(widget_class, name, parent, x, y, width, height);
|
2025-09-29 00:04:04 +00:00
|
|
|
MwVaListApply(h, va);
|
2025-09-28 22:29:05 +00:00
|
|
|
|
|
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwDestroyWidget(MwWidget handle) {
|
2025-09-28 07:03:05 +00:00
|
|
|
int i;
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwDispatch(handle, destroy);
|
2025-09-28 09:27:28 +00:00
|
|
|
|
2025-09-28 10:06:00 +00:00
|
|
|
free(handle->name);
|
|
|
|
|
|
2025-09-28 07:03:05 +00:00
|
|
|
if(handle->children != NULL) {
|
|
|
|
|
for(i = 0; i < arrlen(handle->children); i++) {
|
|
|
|
|
if(handle->children[i] == handle) {
|
2025-09-29 00:04:04 +00:00
|
|
|
MwDestroyWidget(handle->children[i]);
|
2025-09-28 07:03:05 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
arrfree(handle->children);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(handle->parent != NULL) {
|
|
|
|
|
for(i = 0; i < arrlen(handle->parent->children); i++) {
|
|
|
|
|
if(handle->parent->children[i] == handle) {
|
|
|
|
|
arrdel(handle->parent->children, i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLDestroy(handle->lowlevel);
|
2025-09-28 09:14:57 +00:00
|
|
|
|
|
|
|
|
shfree(handle->integer);
|
|
|
|
|
|
|
|
|
|
for(i = 0; i < shlen(handle->text); i++) {
|
|
|
|
|
free(handle->text[i].value);
|
|
|
|
|
handle->text[i].value = NULL;
|
|
|
|
|
}
|
|
|
|
|
shfree(handle->text);
|
2025-09-29 00:01:58 +00:00
|
|
|
shfree(handle->handler);
|
2025-09-28 09:14:57 +00:00
|
|
|
|
2025-09-28 06:52:49 +00:00
|
|
|
free(handle);
|
|
|
|
|
}
|
2025-09-28 08:37:12 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwStep(MwWidget handle) {
|
2025-09-28 14:36:24 +00:00
|
|
|
int i;
|
2025-09-29 00:04:04 +00:00
|
|
|
for(i = 0; i < arrlen(handle->children); i++) MwStep(handle->children[i]);
|
|
|
|
|
MwLLNextEvent(handle->lowlevel);
|
2025-09-28 08:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
int MwPending(MwWidget handle) {
|
2025-09-28 08:37:12 +00:00
|
|
|
int i;
|
2025-09-28 08:37:21 +00:00
|
|
|
for(i = 0; i < arrlen(handle->children); i++) {
|
2025-09-29 00:04:04 +00:00
|
|
|
if(MwPending(handle->children[i])) return 1;
|
2025-09-28 08:37:12 +00:00
|
|
|
}
|
2025-09-29 00:04:04 +00:00
|
|
|
return MwLLPending(handle->lowlevel);
|
2025-09-28 08:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLoop(MwWidget handle) {
|
2025-09-29 07:05:09 +00:00
|
|
|
while(!handle->close) {
|
2025-09-29 00:04:04 +00:00
|
|
|
MwStep(handle);
|
2025-09-29 06:40:08 +00:00
|
|
|
|
|
|
|
|
MwDispatchUserHandler(handle, MwNtickHandler, NULL);
|
|
|
|
|
MwLLSleep(MwWaitMS);
|
2025-09-28 08:37:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-28 08:49:49 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwSetInteger(MwWidget handle, const char* key, int n) {
|
2025-09-28 09:04:35 +00:00
|
|
|
int xy = 0;
|
|
|
|
|
int wh = 0;
|
2025-09-29 00:04:04 +00:00
|
|
|
if((xy = (strcmp(key, MwNx) == 0 || strcmp(key, MwNy) == 0)) || (wh = (strcmp(key, MwNwidth) == 0 || strcmp(key, MwNheight) == 0))) {
|
2025-09-28 09:04:35 +00:00
|
|
|
int x, y;
|
|
|
|
|
unsigned int w, h;
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLGetXYWH(handle->lowlevel, &x, &y, &w, &h);
|
|
|
|
|
if(strcmp(key, MwNx) == 0) x = n;
|
|
|
|
|
if(strcmp(key, MwNy) == 0) y = n;
|
|
|
|
|
if(strcmp(key, MwNwidth) == 0) w = n;
|
|
|
|
|
if(strcmp(key, MwNheight) == 0) h = n;
|
|
|
|
|
if(xy) MwLLSetXY(handle->lowlevel, x, y);
|
|
|
|
|
if(wh) MwLLSetWH(handle->lowlevel, w, h);
|
2025-09-28 09:14:57 +00:00
|
|
|
} else {
|
|
|
|
|
shput(handle->integer, key, n);
|
2025-09-28 09:04:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwSetText(MwWidget handle, const char* key, const char* value) {
|
|
|
|
|
if(strcmp(key, MwNtitle) == 0) {
|
|
|
|
|
MwLLSetTitle(handle->lowlevel, value);
|
2025-09-28 09:14:57 +00:00
|
|
|
} else {
|
|
|
|
|
char* v = malloc(strlen(value) + 1);
|
|
|
|
|
strcpy(v, value);
|
|
|
|
|
|
2025-09-28 14:36:24 +00:00
|
|
|
if(shgeti(handle->text, key) != -1) free(shget(handle->text, key));
|
|
|
|
|
|
2025-09-28 09:14:57 +00:00
|
|
|
shput(handle->text, key, v);
|
2025-09-28 09:08:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
int MwGetInteger(MwWidget handle, const char* key) {
|
|
|
|
|
if(strcmp(key, MwNx) == 0 || strcmp(key, MwNy) == 0 || strcmp(key, MwNwidth) == 0 || strcmp(key, MwNheight) == 0) {
|
2025-09-28 09:27:28 +00:00
|
|
|
int x, y;
|
|
|
|
|
unsigned int w, h;
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLGetXYWH(handle->lowlevel, &x, &y, &w, &h);
|
2025-09-28 09:27:28 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
if(strcmp(key, MwNx) == 0) return x;
|
|
|
|
|
if(strcmp(key, MwNy) == 0) return y;
|
|
|
|
|
if(strcmp(key, MwNwidth) == 0) return w;
|
|
|
|
|
if(strcmp(key, MwNheight) == 0) return h;
|
2025-09-28 09:27:28 +00:00
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
return shget(handle->integer, key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
const char* MwGetText(MwWidget handle, const char* key) {
|
2025-09-28 09:27:28 +00:00
|
|
|
return shget(handle->text, key);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwVaApply(MwWidget handle, ...) {
|
2025-09-28 08:49:49 +00:00
|
|
|
va_list va;
|
|
|
|
|
|
|
|
|
|
va_start(va, handle);
|
2025-09-29 00:04:04 +00:00
|
|
|
MwVaListApply(handle, va);
|
2025-09-28 22:29:05 +00:00
|
|
|
va_end(va);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwVaListApply(MwWidget handle, va_list va) {
|
2025-09-28 22:29:05 +00:00
|
|
|
char* key;
|
|
|
|
|
|
2025-09-28 09:04:35 +00:00
|
|
|
while((key = va_arg(va, char*)) != NULL) {
|
|
|
|
|
if(key[0] == 'I') {
|
|
|
|
|
int n = va_arg(va, int);
|
2025-09-29 00:04:04 +00:00
|
|
|
MwSetInteger(handle, key, n);
|
2025-09-28 09:08:53 +00:00
|
|
|
} else if(key[0] == 'S') {
|
|
|
|
|
char* t = va_arg(va, char*);
|
2025-09-29 00:04:04 +00:00
|
|
|
MwSetText(handle, key, t);
|
2025-09-28 22:21:51 +00:00
|
|
|
} else if(key[0] == 'C') {
|
2025-09-29 00:04:04 +00:00
|
|
|
MwUserHandler h = va_arg(va, MwUserHandler);
|
2025-09-29 00:10:20 +00:00
|
|
|
int ind;
|
2025-09-29 00:01:58 +00:00
|
|
|
|
|
|
|
|
shput(handle->handler, key, h);
|
|
|
|
|
ind = shgeti(handle->handler, key);
|
|
|
|
|
handle->handler[ind].user_data = NULL;
|
2025-09-28 09:04:35 +00:00
|
|
|
}
|
2025-09-28 08:49:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-28 13:53:35 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwSetDefault(MwWidget handle) {
|
|
|
|
|
MwSetText(handle, MwNbackground, MwDefaultBackground);
|
2025-09-29 02:54:08 +00:00
|
|
|
MwSetText(handle, MwNforeground, MwDefaultForeground);
|
2025-09-28 13:53:35 +00:00
|
|
|
}
|
2025-09-28 22:21:51 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwDispatchUserHandler(MwWidget handle, const char* key, void* handler_data) {
|
2025-09-29 00:01:58 +00:00
|
|
|
int ind = shgeti(handle->handler, key);
|
|
|
|
|
if(ind == -1) return;
|
|
|
|
|
|
|
|
|
|
handle->handler[ind].value(handle, handle->handler[ind].user_data, handler_data);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwAddUserHandler(MwWidget handle, const char* key, MwUserHandler handler, void* user_data) {
|
2025-09-29 00:01:58 +00:00
|
|
|
int ind;
|
|
|
|
|
|
|
|
|
|
shput(handle->handler, key, handler);
|
|
|
|
|
ind = shgeti(handle->handler, key);
|
|
|
|
|
handle->handler[ind].user_data = user_data;
|
2025-09-28 22:21:51 +00:00
|
|
|
}
|