DVX_GUI/dvx/widgets/widgetOps.c

399 lines
9.9 KiB
C

// widgetOps.c — Paint dispatcher and public widget operations
#include "widgetInternal.h"
// ============================================================
// debugContainerBorder
// ============================================================
//
// Draw a 1px border in a garish neon color derived from the widget
// pointer so every container gets a distinct, ugly color.
static void debugContainerBorder(WidgetT *w, DisplayT *d, const BlitOpsT *ops) {
static const uint8_t palette[][3] = {
{255, 0, 255}, // magenta
{ 0, 255, 0}, // lime
{255, 255, 0}, // yellow
{ 0, 255, 255}, // cyan
{255, 128, 0}, // orange
{128, 0, 255}, // purple
{255, 0, 128}, // hot pink
{ 0, 128, 255}, // sky blue
{128, 255, 0}, // chartreuse
{255, 64, 64}, // salmon
{ 64, 255, 128}, // mint
{255, 128, 255}, // orchid
};
uint32_t h = (uint32_t)(uintptr_t)w * 2654435761u;
int32_t idx = (int32_t)((h >> 16) % 12);
uint32_t color = packColor(d, palette[idx][0], palette[idx][1], palette[idx][2]);
drawHLine(d, ops, w->x, w->y, w->w, color);
drawHLine(d, ops, w->x, w->y + w->h - 1, w->w, color);
drawVLine(d, ops, w->x, w->y, w->h, color);
drawVLine(d, ops, w->x + w->w - 1, w->y, w->h, color);
}
// ============================================================
// widgetPaintOne
// ============================================================
//
// Paint a single widget and its children. Dispatches to per-widget
// paint functions defined in their respective files.
void widgetPaintOne(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) {
if (!w->visible) {
return;
}
// Paint this widget via vtable
if (w->wclass && w->wclass->paint) {
w->wclass->paint(w, d, ops, font, colors);
}
// Widgets that paint their own children return early
if (w->wclass && (w->wclass->flags & WCLASS_PAINTS_CHILDREN)) {
if (sDebugLayout) {
debugContainerBorder(w, d, ops);
}
return;
}
// Paint children
for (WidgetT *c = w->firstChild; c; c = c->nextSibling) {
widgetPaintOne(c, d, ops, font, colors);
}
// Debug: draw container borders on top of children
if (sDebugLayout && w->firstChild) {
debugContainerBorder(w, d, ops);
}
}
// ============================================================
// widgetPaintOverlays
// ============================================================
//
// Paints popup overlays (open dropdowns/comboboxes) on top of
// the widget tree. Called after the main paint pass.
void widgetPaintOverlays(WidgetT *root, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) {
if (!sOpenPopup) {
return;
}
// Verify the popup belongs to this widget tree
WidgetT *check = sOpenPopup;
while (check->parent) {
check = check->parent;
}
if (check != root) {
return;
}
if (sOpenPopup->wclass && sOpenPopup->wclass->paintOverlay) {
sOpenPopup->wclass->paintOverlay(sOpenPopup, d, ops, font, colors);
}
}
// ============================================================
// wgtDestroy
// ============================================================
void wgtDestroy(WidgetT *w) {
if (!w) {
return;
}
if (w->parent) {
widgetRemoveChild(w->parent, w);
}
widgetDestroyChildren(w);
if (w->wclass && w->wclass->destroy) {
w->wclass->destroy(w);
}
// Clear static references
if (sFocusedWidget == w) {
sFocusedWidget = NULL;
}
if (sOpenPopup == w) {
sOpenPopup = NULL;
}
if (sDragSlider == w) {
sDragSlider = NULL;
}
if (sDrawingCanvas == w) {
sDrawingCanvas = NULL;
}
// If this is the root, clear the window's reference
if (w->window && w->window->widgetRoot == w) {
w->window->widgetRoot = NULL;
}
free(w);
}
// ============================================================
// widgetHashName — djb2 hash for widget name lookup
// ============================================================
static uint32_t widgetHashName(const char *s) {
uint32_t h = 5381;
while (*s) {
h = ((h << 5) + h) + (uint8_t)*s;
s++;
}
return h;
}
// ============================================================
// wgtFindByHash — recursive search with hash fast-reject
// ============================================================
static WidgetT *wgtFindByHash(WidgetT *root, const char *name, uint32_t hash) {
if (root->nameHash == hash && root->name[0] && strcmp(root->name, name) == 0) {
return root;
}
for (WidgetT *c = root->firstChild; c; c = c->nextSibling) {
WidgetT *found = wgtFindByHash(c, name, hash);
if (found) {
return found;
}
}
return NULL;
}
// ============================================================
// wgtFind
// ============================================================
WidgetT *wgtFind(WidgetT *root, const char *name) {
if (!root || !name) {
return NULL;
}
uint32_t hash = widgetHashName(name);
return wgtFindByHash(root, name, hash);
}
// ============================================================
// wgtSetName
// ============================================================
void wgtSetName(WidgetT *w, const char *name) {
if (!w || !name) {
return;
}
strncpy(w->name, name, MAX_WIDGET_NAME - 1);
w->name[MAX_WIDGET_NAME - 1] = '\0';
w->nameHash = widgetHashName(w->name);
}
// ============================================================
// wgtGetText
// ============================================================
const char *wgtGetText(const WidgetT *w) {
if (!w) {
return "";
}
if (w->wclass && w->wclass->getText) {
return w->wclass->getText(w);
}
return "";
}
// ============================================================
// wgtInitWindow
// ============================================================
WidgetT *wgtInitWindow(AppContextT *ctx, WindowT *win) {
WidgetT *root = widgetAlloc(NULL, WidgetVBoxE);
if (!root) {
return NULL;
}
root->window = win;
root->userData = ctx;
win->widgetRoot = root;
win->onPaint = widgetOnPaint;
win->onMouse = widgetOnMouse;
win->onKey = widgetOnKey;
win->onResize = widgetOnResize;
return root;
}
// ============================================================
// wgtInvalidate
// ============================================================
void wgtInvalidate(WidgetT *w) {
if (!w || !w->window) {
return;
}
// Find the root
WidgetT *root = w;
while (root->parent) {
root = root->parent;
}
AppContextT *ctx = (AppContextT *)root->userData;
if (!ctx) {
return;
}
// Manage scrollbars (measures, adds/removes scrollbars, relayouts)
// Skip if window has a custom paint handler (e.g. dialog) that manages its own layout
if (w->window->onPaint == widgetOnPaint) {
widgetManageScrollbars(w->window, ctx);
}
// Repaint (use win->onPaint so custom paint handlers like dialogs work)
WindowT *win = w->window;
RectT fullRect = {0, 0, win->contentW, win->contentH};
win->onPaint(win, &fullRect);
win->contentDirty = true;
// Dirty the window on screen
dvxInvalidateWindow(ctx, win);
}
// ============================================================
// wgtInvalidatePaint
// ============================================================
//
// Lightweight repaint — skips measure/layout/scrollbar management.
// Use when only visual state changed (slider value, cursor blink,
// selection highlight, checkbox toggle) but widget sizes are stable.
void wgtInvalidatePaint(WidgetT *w) {
if (!w || !w->window) {
return;
}
WidgetT *root = w;
while (root->parent) {
root = root->parent;
}
AppContextT *ctx = (AppContextT *)root->userData;
if (!ctx) {
return;
}
// Repaint without measure/layout (use win->onPaint so custom handlers work)
WindowT *win = w->window;
RectT fullRect = {0, 0, win->contentW, win->contentH};
win->onPaint(win, &fullRect);
win->contentDirty = true;
dvxInvalidateWindow(ctx, win);
}
// ============================================================
// wgtPaint
// ============================================================
void wgtPaint(WidgetT *root, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) {
if (!root) {
return;
}
widgetPaintOne(root, d, ops, font, colors);
}
// ============================================================
// wgtSetDebugLayout
// ============================================================
void wgtSetDebugLayout(AppContextT *ctx, bool enabled) {
sDebugLayout = enabled;
for (int32_t i = 0; i < ctx->stack.count; i++) {
WindowT *win = ctx->stack.windows[i];
if (win->widgetRoot) {
wgtInvalidate(win->widgetRoot);
}
}
}
// ============================================================
// wgtSetEnabled
// ============================================================
void wgtSetEnabled(WidgetT *w, bool enabled) {
if (w) {
w->enabled = enabled;
wgtInvalidatePaint(w);
}
}
// ============================================================
// wgtSetText
// ============================================================
void wgtSetText(WidgetT *w, const char *text) {
if (!w) {
return;
}
if (w->wclass && w->wclass->setText) {
w->wclass->setText(w, text);
}
}
// ============================================================
// wgtSetVisible
// ============================================================
void wgtSetVisible(WidgetT *w, bool visible) {
if (w) {
w->visible = visible;
}
}