DVX_GUI/dvx/widgets/widgetCheckbox.c

125 lines
3.7 KiB
C

// widgetCheckbox.c — Checkbox widget
#include "widgetInternal.h"
// ============================================================
// wgtCheckbox
// ============================================================
WidgetT *wgtCheckbox(WidgetT *parent, const char *text) {
WidgetT *w = widgetAlloc(parent, WidgetCheckboxE);
if (w) {
w->as.checkbox.text = text;
w->as.checkbox.checked = false;
w->accelKey = accelParse(text);
}
return w;
}
// ============================================================
// widgetCheckboxGetText
// ============================================================
const char *widgetCheckboxGetText(const WidgetT *w) {
return w->as.checkbox.text;
}
// ============================================================
// widgetCheckboxSetText
// ============================================================
void widgetCheckboxSetText(WidgetT *w, const char *text) {
w->as.checkbox.text = text;
w->accelKey = accelParse(text);
}
// ============================================================
// widgetCheckboxCalcMinSize
// ============================================================
void widgetCheckboxCalcMinSize(WidgetT *w, const BitmapFontT *font) {
w->calcMinW = CHECKBOX_BOX_SIZE + CHECKBOX_GAP +
textWidthAccel(font, w->as.checkbox.text);
w->calcMinH = DVX_MAX(CHECKBOX_BOX_SIZE, font->charHeight);
}
// ============================================================
// widgetCheckboxOnKey
// ============================================================
void widgetCheckboxOnKey(WidgetT *w, int32_t key) {
if (key == ' ' || key == 0x0D) {
w->as.checkbox.checked = !w->as.checkbox.checked;
if (w->onChange) {
w->onChange(w);
}
wgtInvalidate(w);
}
}
// ============================================================
// widgetCheckboxOnMouse
// ============================================================
void widgetCheckboxOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy) {
(void)root;
(void)vx;
(void)vy;
w->focused = true;
w->as.checkbox.checked = !w->as.checkbox.checked;
if (w->onChange) {
w->onChange(w);
}
}
// ============================================================
// widgetCheckboxPaint
// ============================================================
void widgetCheckboxPaint(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 checkbox 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 check mark if checked
if (w->as.checkbox.checked) {
int32_t cx = w->x + 3;
int32_t cy = boxY + 3;
int32_t cs = CHECKBOX_BOX_SIZE - 6;
for (int32_t i = 0; i < cs; i++) {
drawHLine(d, ops, cx + i, cy + i, 1, fg);
drawHLine(d, ops, cx + cs - 1 - i, cy + i, 1, fg);
}
}
// Draw label
int32_t labelX = w->x + CHECKBOX_BOX_SIZE + CHECKBOX_GAP;
int32_t labelY = w->y + (w->h - font->charHeight) / 2;
drawTextAccel(d, ops, font, labelX, labelY, w->as.checkbox.text, fg, bg, false);
if (w->focused) {
int32_t labelW = textWidthAccel(font, w->as.checkbox.text);
drawFocusRect(d, ops, labelX - 1, labelY - 1, labelW + 2, font->charHeight + 2, fg);
}
}