DVX_GUI/dvx/widgets/widgetButton.c

76 lines
2.2 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;
}
return w;
}
// ============================================================
// widgetButtonCalcMinSize
// ============================================================
void widgetButtonCalcMinSize(WidgetT *w, const BitmapFontT *font) {
w->calcMinW = (int32_t)strlen(w->as.button.text) * font->charWidth + BUTTON_PAD_H * 2;
w->calcMinH = font->charHeight + BUTTON_PAD_V * 2;
}
// ============================================================
// widgetButtonOnMouse
// ============================================================
void widgetButtonOnMouse(WidgetT *hit) {
hit->as.button.pressed = true;
wgtInvalidate(hit);
if (hit->onClick) {
hit->onClick(hit);
}
hit->as.button.pressed = false;
}
// ============================================================
// 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 = (int32_t)strlen(w->as.button.text) * font->charWidth;
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++;
}
drawText(d, ops, font, textX, textY,
w->as.button.text,
w->enabled ? fg : colors->windowShadow,
bgFace, true);
}