milsko/src/widget/subwindow.c

140 lines
3 KiB
C
Raw Normal View History

2026-04-30 08:37:50 +09:00
#include <Mw/Milsko.h>
#define TitleHeight 20
2026-04-30 14:28:56 +09:00
#define ButtonSize 18
static void resize(MwWidget handle){
MwSubWindow sw = handle->internal;
int x, y, w, h;
int ww = MwGetInteger(handle, MwNwidth);
int wh = MwGetInteger(handle, MwNheight);
int bw = MwDefaultBorderWidth(handle);
int p;
x = bw * 2;
y = bw * 2 + TitleHeight;
w = ww - bw * 4;
h = wh - bw * 4 - TitleHeight;
if(sw->frame == NULL){
sw->frame = MwCreateWidget(MwFrameClass, "frame", handle, x, y, w, h);
}else{
MwVaApply(sw->frame,
MwNx, x,
MwNy, y,
MwNwidth, w,
MwNheight, h,
NULL);
}
p = (TitleHeight - ButtonSize) / 2;
x = ww - bw - p;
y = bw + p;
w = ButtonSize;
h = ButtonSize;
#define BUTTON(name) \
x -= ButtonSize; \
if(sw->name == NULL){ \
sw->name = MwCreateWidget(MwButtonClass, #name, handle, x, y, w, h); \
}else{ \
MwVaApply(sw->name, \
MwNx, x, \
MwNy, y, \
MwNwidth, w, \
MwNheight, h, \
NULL); \
} \
x -= p;
BUTTON(close);
BUTTON(maximize);
BUTTON(minimize);
MwSetText(sw->frame, MwNbackground, "#f00");
}
2026-04-30 08:37:50 +09:00
static int wcreate(MwWidget handle) {
2026-04-30 14:28:56 +09:00
MwSubWindow sw = malloc(sizeof(*sw));
memset(sw, 0, sizeof(*sw));
handle->internal = sw;
2026-04-30 08:37:50 +09:00
2026-04-30 14:28:56 +09:00
resize(handle);
MwSetDefault(handle);
2026-04-30 12:35:42 +09:00
2026-04-30 08:37:50 +09:00
return 0;
}
2026-04-30 14:28:56 +09:00
static void destroy(MwWidget handle) {
MwSubWindow sw = handle->internal;
free(handle->internal);
}
2026-04-30 08:37:50 +09:00
static void draw(MwWidget handle) {
2026-04-30 12:35:42 +09:00
MwLLColor c = MwParseColor(handle, MwGetText(handle, MwNbackground));
2026-04-30 14:28:56 +09:00
MwLLColor tb = MwParseColor(handle, MwGetText(handle, MwNtitleBackground));
MwLLColor tf = MwParseColor(handle, MwGetText(handle, MwNtitleForeground));
2026-04-30 08:37:50 +09:00
MwRect r, r2;
2026-04-30 14:28:56 +09:00
const char* title = MwGetText(handle, MwNtitle);
2026-04-30 08:37:50 +09:00
r.x = 0;
r.y = 0;
r.width = MwGetInteger(handle, MwNwidth);
r.height = MwGetInteger(handle, MwNheight);
MwDrawFrame(handle, &r, c, 0);
MwDrawRect(handle, &r, c);
2026-04-30 12:35:42 +09:00
r2 = r;
2026-04-30 08:37:50 +09:00
r2.height = TitleHeight;
2026-04-30 14:28:56 +09:00
MwDrawRect(handle, &r2, tb);
if(title != NULL){
MwPoint p;
p.x = r2.x + (TitleHeight - ButtonSize);
p.y = r2.y + TitleHeight / 2;
handle->bgcolor = tb;
MwDrawText(handle, NULL, &p, title, MwALIGNMENT_BEGINNING, tf);
handle->bgcolor = NULL;
}
2026-04-30 08:37:50 +09:00
r.y += TitleHeight;
r.height -= TitleHeight;
MwDrawFrame(handle, &r, c, 1);
2026-04-30 14:28:56 +09:00
MwLLFreeColor(tf);
MwLLFreeColor(tb);
2026-04-30 08:37:50 +09:00
MwLLFreeColor(c);
}
static void prop_change(MwWidget handle, const char* key) {
if(strcmp(key, MwNtitle) == 0) MwForceRender(handle);
}
static void func_handler(MwWidget handle, const char* name, void* out, va_list va) {
(void)out;
}
MwClassRec MwSubWindowClassRec = {
wcreate, /* create */
2026-04-30 14:28:56 +09:00
destroy, /* destroy */
2026-04-30 08:37:50 +09:00
draw, /* draw */
NULL, /* click */
NULL, /* parent_resize */
prop_change, /* prop_change */
NULL, /* mouse_move */
NULL, /* mouse_up */
NULL, /* mouse_down */
NULL, /* key */
func_handler, /* execute */
NULL, /* tick */
NULL, /* resize */
NULL, /* children_update */
NULL, /* children_prop_change */
NULL, /* clipboard */
NULL, /* props_change */
NULL,
NULL,
NULL};
MwClass MwSubWindowClass = &MwSubWindowClassRec;