Files
tim/src/widgets.c
2026-01-12 22:57:31 +05:00

82 lines
2.8 KiB
C
Executable File

#include "tim.h"
void tim_frame(i32 x, i32 y, i32 w, i32 h, TimStyle style) {
if (tim->event.type == TimEvent_Draw) {
TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
tim_draw_box(r.x, r.y, r.w, r.h, style.brd, style.bg);
}
}
void tim_label(cstr s, i32 x, i32 y, i32 w, i32 h, TimStyle style) {
if (tim->event.type == TimEvent_Draw) {
TimText t = tim_scan_str(s);
w = (w == A) ? t.width : w;
h = (h == A) ? t.lines : h;
TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
TimCell c = tim_cell(" ", style.fg, style.bg);
tim_fill(c, r.x, r.y, r.w, r.h);
TimLine l = {.s = s, .line = ""};
for (i32 i = 0; tim_next_line(&l); i++) {
tim_draw_str(l.line, r.x, r.y + i, l.width, c.fg, c.bg);
}
}
}
bool tim_button(cstr txt, i32 x, i32 y, i32 w, i32 h, TimStyle style) {
i32 txt_w = tim_utf8_len(txt);
w = (w == A) ? (txt_w + 4) : w;
h = (h == A) ? 3 : h;
TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
if (tim->event.type == TimEvent_Draw) {
tim_draw_box(r.x, r.y, r.w, r.h, style.brd, style.bg);
tim_draw_str(txt, r.x + (w - txt_w) / 2, r.y + h / 2, w, style.fg, style.bg);
}
return tim_is_mouse_click_over(r);
}
bool tim_button_noborder(cstr txt, i32 x, i32 y, i32 w, i32 h, TimStyle style) {
i32 txt_w = tim_utf8_len(txt);
w = (w == A) ? txt_w : w;
h = (h == A) ? 1 : h;
TimRect r = tim_scope_rect_to_absolute(x, y, w, h);
if (tim->event.type == TimEvent_Draw) {
tim_draw_str(txt, r.x + (w - txt_w) / 2, r.y + h / 2, w, style.fg, style.bg);
}
return tim_is_mouse_click_over(r);
}
bool tim_checkbox(cstr txt, i32* state, i32 x, i32 y, i32 w, TimStyle style) {
w = (w == A) ? tim_utf8_len(txt) + 4 : w;
TimRect r = tim_scope_rect_to_absolute(x, y, w, 1);
if (tim->event.type == TimEvent_Draw) {
cstr st = *state == -1 ? "-" : *state ? "x" : " ";
tim_draw_str("[ ] ", r.x, r.y, 4, style.fg, style.bg);
tim_draw_str(st, r.x + 1, r.y, 1, style.brd, style.bg);
tim_draw_str(txt, r.x + 4, r.y, r.w - 4, style.fg, style.bg);
}
bool click = tim_is_mouse_click_over(r);
*state = click ? !*state : *state;
return click;
}
bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, TimStyle style) {
w = (w == A) ? tim_utf8_len(txt) + 4 : w;
TimRect r = tim_scope_rect_to_absolute(x, y, w, 1);
if (tim->event.type == TimEvent_Draw) {
cstr st = *state == v ? "o" : " ";
tim_draw_str("( ) ", r.x, r.y, 4, style.fg, style.bg);
tim_draw_str(st, r.x + 1, r.y, 1, style.brd, style.bg);
tim_draw_str(txt, r.x + 4, r.y, r.w - 4, style.fg, style.bg);
}
bool click = tim_is_mouse_click_over(r);
*state = click ? v : *state;
return click;
}