#define DVX_WIDGET_IMPL // widgetImageButton.c -- Image button widget (button with image instead of text) // // Combines a Button's beveled border and press behavior with an Image's // bitmap rendering. The image is centered within the button bounds and // shifts by 1px on press, just like text in a regular button. // // Uses the same two-phase press model as Button: mouse press stores in // sPressedButton, keyboard press (Space/Enter) stores in sKeyPressedBtn, // and the event dispatcher handles release/cancel. The onClick callback // fires on release, not press. // // The widget takes ownership of the image data buffer -- if widget creation // fails, the data is freed to prevent leaks. // // The 4px added to min size (widgetImageButtonCalcMinSize) accounts for // the 2px bevel on each side -- no extra padding is added beyond that, // keeping image buttons compact for toolbar use. #include "dvxWidgetPlugin.h" static int32_t sTypeId = -1; typedef struct { uint8_t *pixelData; int32_t imgW; int32_t imgH; int32_t imgPitch; bool pressed; } ImageButtonDataT; // ============================================================ // widgetImageButtonDestroy // ============================================================ void widgetImageButtonDestroy(WidgetT *w) { ImageButtonDataT *d = (ImageButtonDataT *)w->data; if (d) { free(d->pixelData); free(d); } } // ============================================================ // widgetImageButtonCalcMinSize // ============================================================ void widgetImageButtonCalcMinSize(WidgetT *w, const BitmapFontT *font) { (void)font; ImageButtonDataT *d = (ImageButtonDataT *)w->data; // Bevel border only, no extra padding w->calcMinW = d->imgW + 4; w->calcMinH = d->imgH + 4; } // ============================================================ // widgetImageButtonOnKey // ============================================================ void widgetImageButtonOnKey(WidgetT *w, int32_t key, int32_t mod) { (void)mod; ImageButtonDataT *d = (ImageButtonDataT *)w->data; if (key == ' ' || key == 0x0D) { d->pressed = true; sKeyPressedBtn = w; wgtInvalidatePaint(w); } } // ============================================================ // widgetImageButtonOnMouse // ============================================================ void widgetImageButtonOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy) { (void)root; (void)vx; (void)vy; ImageButtonDataT *d = (ImageButtonDataT *)w->data; w->focused = true; d->pressed = true; sPressedButton = w; } // ============================================================ // widgetImageButtonPaint // ============================================================ void widgetImageButtonPaint(WidgetT *w, DisplayT *disp, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) { (void)font; ImageButtonDataT *d = (ImageButtonDataT *)w->data; uint32_t bgFace = w->bgColor ? w->bgColor : colors->buttonFace; bool pressed = d->pressed && w->enabled; BevelStyleT bevel; bevel.highlight = pressed ? colors->windowShadow : colors->windowHighlight; bevel.shadow = pressed ? colors->windowHighlight : colors->windowShadow; bevel.face = bgFace; bevel.width = 2; drawBevel(disp, ops, w->x, w->y, w->w, w->h, &bevel); if (d->pixelData) { int32_t imgX = w->x + (w->w - d->imgW) / 2; int32_t imgY = w->y + (w->h - d->imgH) / 2; if (pressed) { imgX++; imgY++; } rectCopy(disp, ops, imgX, imgY, d->pixelData, d->imgPitch, 0, 0, d->imgW, d->imgH); } if (w->focused) { uint32_t fg = w->fgColor ? w->fgColor : colors->contentFg; int32_t off = pressed ? 1 : 0; drawFocusRect(disp, ops, w->x + 3 + off, w->y + 3 + off, w->w - 6, w->h - 6, fg); } } // ============================================================ // widgetImageButtonAccelActivate // ============================================================ void widgetImageButtonAccelActivate(WidgetT *w, WidgetT *root) { (void)root; ImageButtonDataT *d = (ImageButtonDataT *)w->data; d->pressed = true; sKeyPressedBtn = w; wgtInvalidatePaint(w); } // ============================================================ // widgetImageButtonSetPressed // ============================================================ void widgetImageButtonSetPressed(WidgetT *w, bool pressed) { ImageButtonDataT *d = (ImageButtonDataT *)w->data; d->pressed = pressed; wgtInvalidatePaint(w); } // ============================================================ // DXE registration // ============================================================ static const WidgetClassT sClassImageButton = { .flags = WCLASS_FOCUSABLE | WCLASS_PRESS_RELEASE, .paint = widgetImageButtonPaint, .paintOverlay = NULL, .calcMinSize = widgetImageButtonCalcMinSize, .layout = NULL, .onMouse = widgetImageButtonOnMouse, .onKey = widgetImageButtonOnKey, .onAccelActivate = widgetImageButtonAccelActivate, .destroy = widgetImageButtonDestroy, .getText = NULL, .setText = NULL, .setPressed = widgetImageButtonSetPressed }; // ============================================================ // Widget creation functions // ============================================================ WidgetT *wgtImageButton(WidgetT *parent, uint8_t *pixelData, int32_t w, int32_t h, int32_t pitch) { if (!parent || !pixelData || w <= 0 || h <= 0) { return NULL; } WidgetT *wgt = widgetAlloc(parent, sTypeId); if (wgt) { ImageButtonDataT *d = calloc(1, sizeof(ImageButtonDataT)); d->pixelData = pixelData; d->imgW = w; d->imgH = h; d->imgPitch = pitch; d->pressed = false; wgt->data = d; } else { free(pixelData); } return wgt; } WidgetT *wgtImageButtonFromFile(WidgetT *parent, const char *path) { if (!parent || !path) { return NULL; } AppContextT *ctx = wgtGetContext(parent); if (!ctx) { return NULL; } int32_t imgW; int32_t imgH; int32_t pitch; uint8_t *buf = dvxLoadImage(ctx, path, &imgW, &imgH, &pitch); if (!buf) { return NULL; } return wgtImageButton(parent, buf, imgW, imgH, pitch); } void wgtImageButtonSetData(WidgetT *w, uint8_t *pixelData, int32_t imgW, int32_t imgH, int32_t pitch) { VALIDATE_WIDGET_VOID(w, sTypeId); ImageButtonDataT *d = (ImageButtonDataT *)w->data; free(d->pixelData); d->pixelData = pixelData; d->imgW = imgW; d->imgH = imgH; d->imgPitch = pitch; wgtInvalidate(w); } // ============================================================ // DXE registration // ============================================================ static const struct { WidgetT *(*create)(WidgetT *parent, uint8_t *pixelData, int32_t w, int32_t h, int32_t pitch); WidgetT *(*fromFile)(WidgetT *parent, const char *path); void (*setData)(WidgetT *w, uint8_t *pixelData, int32_t imgW, int32_t imgH, int32_t pitch); } sApi = { .create = wgtImageButton, .fromFile = wgtImageButtonFromFile, .setData = wgtImageButtonSetData }; void wgtRegister(void) { sTypeId = wgtRegisterClass(&sClassImageButton); wgtRegisterApi("imagebutton", &sApi); }