2025-09-28 06:52:49 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
#include <Milsko/Milsko.h>
|
|
|
|
|
|
2025-09-28 07:03:05 +00:00
|
|
|
#include "stb_ds.h"
|
|
|
|
|
|
2025-09-28 09:27:28 +00:00
|
|
|
#define Dispatch(x, y) \
|
|
|
|
|
if(x->class != NULL && x->class->y != NULL) x->class->y(x)
|
|
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
static void llhandler(MilskoLL handle) {
|
2025-09-28 09:55:42 +00:00
|
|
|
MilskoWidget h = (MilskoWidget)handle->user;
|
2025-09-28 09:50:28 +00:00
|
|
|
Dispatch(h, draw);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 10:06:00 +00:00
|
|
|
MilskoWidget MilskoCreateWidget(MilskoClass class, const char* name, MilskoWidget parent, int x, int y, unsigned int width, unsigned int height) {
|
2025-09-28 09:50:28 +00:00
|
|
|
MilskoWidget 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-28 07:03:05 +00:00
|
|
|
h->parent = parent;
|
|
|
|
|
h->children = NULL;
|
2025-09-28 08:37:12 +00:00
|
|
|
h->lowlevel = MilskoLLCreate(parent == NULL ? NULL : parent->lowlevel, x, y, width, height);
|
2025-09-28 07:11:55 +00:00
|
|
|
h->class = class;
|
2025-09-28 07:03:05 +00:00
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
h->lowlevel->user = h;
|
|
|
|
|
h->lowlevel->draw = llhandler;
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
shdefault(h->text, NULL);
|
|
|
|
|
shdefault(h->integer, -1);
|
|
|
|
|
|
2025-09-28 09:27:28 +00:00
|
|
|
Dispatch(h, create);
|
|
|
|
|
|
2025-09-28 07:03:05 +00:00
|
|
|
return h;
|
2025-09-28 06:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
void MilskoDestroyWidget(MilskoWidget handle) {
|
2025-09-28 07:03:05 +00:00
|
|
|
int i;
|
|
|
|
|
|
2025-09-28 09:27:28 +00:00
|
|
|
Dispatch(handle, destroy);
|
|
|
|
|
|
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) {
|
|
|
|
|
MilskoDestroyWidget(handle->children[i]);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MilskoLLDestroy(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-28 06:52:49 +00:00
|
|
|
free(handle);
|
|
|
|
|
}
|
2025-09-28 08:37:12 +00:00
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
void MilskoStep(MilskoWidget handle) {
|
2025-09-28 08:37:12 +00:00
|
|
|
MilskoLLNextEvent(handle->lowlevel);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
int MilskoPending(MilskoWidget 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-28 08:37:12 +00:00
|
|
|
if(MilskoPending(handle->children[i])) return 1;
|
|
|
|
|
}
|
|
|
|
|
return MilskoLLPending(handle->lowlevel);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
void MilskoLoop(MilskoWidget handle) {
|
2025-09-28 08:37:21 +00:00
|
|
|
while(1) {
|
2025-09-28 08:37:12 +00:00
|
|
|
MilskoStep(handle);
|
2025-09-28 09:55:42 +00:00
|
|
|
MilskoLLSleep(5);
|
2025-09-28 08:37:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-28 08:49:49 +00:00
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
void MilskoSetInteger(MilskoWidget handle, const char* key, int n) {
|
2025-09-28 09:04:35 +00:00
|
|
|
int xy = 0;
|
|
|
|
|
int wh = 0;
|
|
|
|
|
if((xy = (strcmp(key, MilskoNx) == 0 || strcmp(key, MilskoNy) == 0)) || (wh = (strcmp(key, MilskoNwidth) == 0 || strcmp(key, MilskoNheight) == 0))) {
|
|
|
|
|
int x, y;
|
|
|
|
|
unsigned int w, h;
|
|
|
|
|
|
|
|
|
|
MilskoLLGetXYWH(handle->lowlevel, &x, &y, &w, &h);
|
|
|
|
|
if(strcmp(key, MilskoNx) == 0) x = n;
|
|
|
|
|
if(strcmp(key, MilskoNy) == 0) y = n;
|
|
|
|
|
if(strcmp(key, MilskoNwidth) == 0) w = n;
|
|
|
|
|
if(strcmp(key, MilskoNheight) == 0) h = n;
|
|
|
|
|
if(xy) MilskoLLSetXY(handle->lowlevel, x, y);
|
|
|
|
|
if(wh) MilskoLLSetWH(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-28 09:50:28 +00:00
|
|
|
void MilskoSetText(MilskoWidget handle, const char* key, const char* value) {
|
2025-09-28 09:08:53 +00:00
|
|
|
if(strcmp(key, MilskoNtitle) == 0) {
|
|
|
|
|
MilskoLLSetTitle(handle->lowlevel, value);
|
2025-09-28 09:14:57 +00:00
|
|
|
} else {
|
|
|
|
|
char* v = malloc(strlen(value) + 1);
|
|
|
|
|
strcpy(v, value);
|
|
|
|
|
|
|
|
|
|
shput(handle->text, key, v);
|
2025-09-28 09:08:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
int MilskoGetInteger(MilskoWidget handle, const char* key) {
|
2025-09-28 09:27:28 +00:00
|
|
|
if(strcmp(key, MilskoNx) == 0 || strcmp(key, MilskoNy) == 0 || strcmp(key, MilskoNwidth) == 0 || strcmp(key, MilskoNheight) == 0) {
|
|
|
|
|
int x, y;
|
|
|
|
|
unsigned int w, h;
|
|
|
|
|
|
|
|
|
|
MilskoLLGetXYWH(handle->lowlevel, &x, &y, &w, &h);
|
|
|
|
|
|
|
|
|
|
if(strcmp(key, MilskoNx) == 0) return x;
|
|
|
|
|
if(strcmp(key, MilskoNy) == 0) return y;
|
|
|
|
|
if(strcmp(key, MilskoNwidth) == 0) return w;
|
|
|
|
|
if(strcmp(key, MilskoNheight) == 0) return h;
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
return shget(handle->integer, key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
const char* MilskoGetText(MilskoWidget handle, const char* key) {
|
2025-09-28 09:27:28 +00:00
|
|
|
return shget(handle->text, key);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 09:50:28 +00:00
|
|
|
void MilskoApply(MilskoWidget handle, ...) {
|
2025-09-28 08:49:49 +00:00
|
|
|
va_list va;
|
2025-09-28 09:04:35 +00:00
|
|
|
char* key;
|
2025-09-28 08:49:49 +00:00
|
|
|
|
|
|
|
|
va_start(va, handle);
|
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);
|
|
|
|
|
MilskoSetInteger(handle, key, n);
|
2025-09-28 09:08:53 +00:00
|
|
|
} else if(key[0] == 'S') {
|
|
|
|
|
char* t = va_arg(va, char*);
|
|
|
|
|
MilskoSetText(handle, key, t);
|
2025-09-28 09:04:35 +00:00
|
|
|
}
|
2025-09-28 08:49:49 +00:00
|
|
|
}
|
|
|
|
|
va_end(va);
|
|
|
|
|
}
|