From 4597519598b424e0e6e7dbb83be9f8ebd203bc89 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sun, 28 Sep 2025 22:10:42 +0000 Subject: [PATCH] draw button git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@32 b9cfdab3-6d41-4d17-bbe4-086880011989 --- include/Milsko/LowLevel.h | 2 +- include/Milsko/TypeDefs.h | 6 ++++-- src/button.c | 2 +- src/core.c | 19 +++++++++++++++++-- src/x11.c | 10 ++++++++++ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/include/Milsko/LowLevel.h b/include/Milsko/LowLevel.h index 9905fe7..ea5064c 100644 --- a/include/Milsko/LowLevel.h +++ b/include/Milsko/LowLevel.h @@ -28,8 +28,8 @@ typedef void* MilskoLLColor; struct _MilskoLLCallback { void (*draw)(MilskoLL handle); - void (*down)(MilskoLL handle); void (*up)(MilskoLL handle); + void (*down)(MilskoLL handle); }; /* lowlevel.c, common part */ diff --git a/include/Milsko/TypeDefs.h b/include/Milsko/TypeDefs.h index a958323..c5d57db 100644 --- a/include/Milsko/TypeDefs.h +++ b/include/Milsko/TypeDefs.h @@ -10,7 +10,7 @@ typedef struct _MilskoRect MilskoRect; typedef struct _MilskoTextKeyValue MilskoTextKeyValue; typedef struct _MilskoIntegerKeyValue MilskoIntegerKeyValue; #ifdef _MILSKO -typedef struct _Milsko *MilskoWidget, MilskoWidgetRec; +typedef struct _MilskoWidget *MilskoWidget, MilskoWidgetRec; #else typedef void* MilskoWidget; #endif @@ -46,7 +46,7 @@ struct _MilskoIntegerKeyValue { }; #ifdef _MILSKO -struct _Milsko { +struct _MilskoWidget { char* name; MilskoLL lowlevel; @@ -54,6 +54,8 @@ struct _Milsko { MilskoWidget* children; MilskoClass class; + int pressed; + MilskoTextKeyValue* text; MilskoIntegerKeyValue* integer; }; diff --git a/src/button.c b/src/button.c index 77faa54..cd3fa74 100644 --- a/src/button.c +++ b/src/button.c @@ -13,7 +13,7 @@ static void draw(MilskoWidget handle) { r.width = MilskoGetInteger(handle, MilskoNwidth); r.height = MilskoGetInteger(handle, MilskoNheight); - MilskoDrawFrame(handle, &r, MilskoParseColor(handle, MilskoGetText(handle, MilskoNbackground)), 0); + MilskoDrawFrame(handle, &r, MilskoParseColor(handle, MilskoGetText(handle, MilskoNbackground)), handle->pressed); MilskoDrawRect(handle, &r, MilskoParseColor(handle, MilskoGetText(handle, MilskoNbackground))); } diff --git a/src/core.c b/src/core.c index 669b4dd..f2215b2 100644 --- a/src/core.c +++ b/src/core.c @@ -3,11 +3,23 @@ #include "stb_ds.h" -static void llhandler(MilskoLL handle) { +static void lldrawhandler(MilskoLL handle) { MilskoWidget h = (MilskoWidget)handle->user; MilskoDispatch(h, draw); } +static void lluphandler(MilskoLL handle) { + MilskoWidget h = (MilskoWidget)handle->user; + h->pressed = 0; + + MilskoDispatch(h, click); +} + +static void lldownhandler(MilskoLL handle) { + MilskoWidget h = (MilskoWidget)handle->user; + h->pressed = 1; +} + MilskoWidget MilskoCreateWidget(MilskoClass class, const char* name, MilskoWidget parent, int x, int y, unsigned int width, unsigned int height) { MilskoWidget h = malloc(sizeof(*h)); @@ -18,9 +30,12 @@ MilskoWidget MilskoCreateWidget(MilskoClass class, const char* name, MilskoWidge h->children = NULL; h->lowlevel = MilskoLLCreate(parent == NULL ? NULL : parent->lowlevel, x, y, width, height); h->class = class; + h->pressed = 0; h->lowlevel->user = h; - h->lowlevel->callback->draw = llhandler; + h->lowlevel->callback->draw = lldrawhandler; + h->lowlevel->callback->up = lluphandler; + h->lowlevel->callback->down = lldownhandler; if(parent != NULL) arrput(parent->children, h); diff --git a/src/x11.c b/src/x11.c index 7ea17a8..4431e6e 100644 --- a/src/x11.c +++ b/src/x11.c @@ -110,6 +110,16 @@ void MilskoLLNextEvent(MilskoLL handle) { if(XCheckWindowEvent(handle->display, handle->window, mask, &ev)) { if(ev.type == Expose) { MilskoLLDispatch(handle, draw); + } else if(ev.type == ButtonPress) { + if(ev.xbutton.button == Button1) { + MilskoLLDispatch(handle, down); + MilskoLLDispatch(handle, draw); + } + } else if(ev.type == ButtonRelease) { + if(ev.xbutton.button == Button1) { + MilskoLLDispatch(handle, up); + MilskoLLDispatch(handle, draw); + } } } }