DVX_GUI/dvx/widgets/widgetRadio.c

101 lines
3 KiB
C

// widgetRadio.c — RadioGroup and Radio button widgets
#include "widgetInternal.h"
// ============================================================
// wgtRadio
// ============================================================
WidgetT *wgtRadio(WidgetT *parent, const char *text) {
WidgetT *w = widgetAlloc(parent, WidgetRadioE);
if (w) {
w->as.radio.text = text;
// Auto-assign index based on position in parent
int32_t idx = 0;
for (WidgetT *c = parent->firstChild; c != w; c = c->nextSibling) {
if (c->type == WidgetRadioE) {
idx++;
}
}
w->as.radio.index = idx;
}
return w;
}
// ============================================================
// wgtRadioGroup
// ============================================================
WidgetT *wgtRadioGroup(WidgetT *parent) {
WidgetT *w = widgetAlloc(parent, WidgetRadioGroupE);
if (w) {
w->as.radioGroup.selectedIdx = 0;
}
return w;
}
// ============================================================
// widgetRadioCalcMinSize
// ============================================================
void widgetRadioCalcMinSize(WidgetT *w, const BitmapFontT *font) {
w->calcMinW = CHECKBOX_BOX_SIZE + CHECKBOX_GAP +
(int32_t)strlen(w->as.radio.text) * font->charWidth;
w->calcMinH = DVX_MAX(CHECKBOX_BOX_SIZE, font->charHeight);
}
// ============================================================
// widgetRadioOnMouse
// ============================================================
void widgetRadioOnMouse(WidgetT *hit) {
if (hit->parent && hit->parent->type == WidgetRadioGroupE) {
hit->parent->as.radioGroup.selectedIdx = hit->as.radio.index;
if (hit->parent->onChange) {
hit->parent->onChange(hit->parent);
}
}
}
// ============================================================
// widgetRadioPaint
// ============================================================
void widgetRadioPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) {
uint32_t fg = w->fgColor ? w->fgColor : colors->contentFg;
uint32_t bg = w->bgColor ? w->bgColor : colors->contentBg;
int32_t boxY = w->y + (w->h - CHECKBOX_BOX_SIZE) / 2;
// Draw radio box
BevelStyleT bevel;
bevel.highlight = colors->windowShadow;
bevel.shadow = colors->windowHighlight;
bevel.face = bg;
bevel.width = 1;
drawBevel(d, ops, w->x, boxY, CHECKBOX_BOX_SIZE, CHECKBOX_BOX_SIZE, &bevel);
// Draw filled dot if selected
if (w->parent && w->parent->type == WidgetRadioGroupE &&
w->parent->as.radioGroup.selectedIdx == w->as.radio.index) {
rectFill(d, ops, w->x + 3, boxY + 3,
CHECKBOX_BOX_SIZE - 6, CHECKBOX_BOX_SIZE - 6, fg);
}
drawText(d, ops, font,
w->x + CHECKBOX_BOX_SIZE + CHECKBOX_GAP,
w->y + (w->h - font->charHeight) / 2,
w->as.radio.text, fg, bg, false);
}