DVX_GUI/dvx/widgets/widgetButton.c
2026-03-14 20:27:59 -05:00

114 lines
3.3 KiB
C

// widgetButton.c — Button widget
#include "widgetInternal.h"
// ============================================================
// wgtButton
// ============================================================
WidgetT *wgtButton(WidgetT *parent, const char *text) {
WidgetT *w = widgetAlloc(parent, WidgetButtonE);
if (w) {
w->as.button.text = text;
w->as.button.pressed = false;
w->accelKey = accelParse(text);
}
return w;
}
// ============================================================
// widgetButtonGetText
// ============================================================
const char *widgetButtonGetText(const WidgetT *w) {
return w->as.button.text;
}
// ============================================================
// widgetButtonSetText
// ============================================================
void widgetButtonSetText(WidgetT *w, const char *text) {
w->as.button.text = text;
w->accelKey = accelParse(text);
}
// ============================================================
// widgetButtonCalcMinSize
// ============================================================
void widgetButtonCalcMinSize(WidgetT *w, const BitmapFontT *font) {
w->calcMinW = textWidthAccel(font, w->as.button.text) + BUTTON_PAD_H * 2;
w->calcMinH = font->charHeight + BUTTON_PAD_V * 2;
}
// ============================================================
// widgetButtonOnKey
// ============================================================
void widgetButtonOnKey(WidgetT *w, int32_t key, int32_t mod) {
(void)mod;
if (key == ' ' || key == 0x0D) {
w->as.button.pressed = true;
sKeyPressedBtn = w;
wgtInvalidatePaint(w);
}
}
// ============================================================
// widgetButtonOnMouse
// ============================================================
void widgetButtonOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy) {
(void)root;
(void)vx;
(void)vy;
w->focused = true;
w->as.button.pressed = true;
sPressedButton = w;
}
// ============================================================
// widgetButtonPaint
// ============================================================
void widgetButtonPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) {
uint32_t fg = w->fgColor ? w->fgColor : colors->contentFg;
uint32_t bgFace = w->bgColor ? w->bgColor : colors->buttonFace;
BevelStyleT bevel;
bevel.highlight = w->as.button.pressed ? colors->windowShadow : colors->windowHighlight;
bevel.shadow = w->as.button.pressed ? colors->windowHighlight : colors->windowShadow;
bevel.face = bgFace;
bevel.width = 2;
drawBevel(d, ops, w->x, w->y, w->w, w->h, &bevel);
int32_t textW = textWidthAccel(font, w->as.button.text);
int32_t textX = w->x + (w->w - textW) / 2;
int32_t textY = w->y + (w->h - font->charHeight) / 2;
if (w->as.button.pressed) {
textX++;
textY++;
}
drawTextAccel(d, ops, font, textX, textY,
w->as.button.text,
w->enabled ? fg : colors->windowShadow,
bgFace, true);
if (w->focused) {
int32_t off = w->as.button.pressed ? 1 : 0;
drawFocusRect(d, ops, w->x + 3 + off, w->y + 3 + off, w->w - 6, w->h - 6, fg);
}
}