#define DVX_WIDGET_IMPL // widgetTabControl.c -- TabControl and TabPage widgets // // Two-level architecture: TabControlE is the container holding // selection state and rendering the tab header strip. TabPageE // children act as invisible sub-containers, each holding the content // widgets for that page. Only the active page's children are visible // and receive layout. // // Tab header rendering: each tab is a manually-drawn chrome piece // (not a button widget) with top/left/right edges in highlight/shadow. // The active tab is 2px taller than inactive tabs, extending down to // overlap the content panel's top border -- this creates the classic // "folder tab" illusion where the active tab appears connected to the // panel below it. The panel's top border is erased under the active // tab to complete the effect. // // Scrolling tab headers: when the total tab header width exceeds the // available space, left/right arrow buttons appear and the header area // becomes a clipped scrolling region. The scrollOffset tracks how many // pixels the tab strip has scrolled. tabEnsureVisible() auto-scrolls // to keep the active tab visible after keyboard navigation. // // Tab switching closes any open dropdown/combobox popup before // switching, because the popup's owning widget may be on the // now-hidden page and would become orphaned visually. // // Layout: all tab pages are positioned at the same content area // coordinates, but only the active page has visible=true. This means // widgetLayoutChildren is only called for the active page, saving // layout computation for hidden pages. When switching tabs, the old // page becomes invisible and the new page becomes visible + relaid out. #include "dvxWidgetPlugin.h" #define TAB_PAD_H 8 #define TAB_PAD_V 4 #define TAB_BORDER 2 static int32_t sTabControlTypeId = -1; static int32_t sTabPageTypeId = -1; typedef struct { int32_t activeTab; int32_t scrollOffset; } TabControlDataT; typedef struct { const char *title; } TabPageDataT; #define TAB_ARROW_W 16 // ============================================================ // Prototypes // ============================================================ static void tabClosePopup(void); static void tabEnsureVisible(WidgetT *w, const BitmapFontT *font); static int32_t tabHeaderTotalW(const WidgetT *w, const BitmapFontT *font); static bool tabNeedScroll(const WidgetT *w, const BitmapFontT *font); // ============================================================ // tabClosePopup -- close any open dropdown/combobox popup // ============================================================ static void tabClosePopup(void) { if (sOpenPopup) { wclsClosePopup(sOpenPopup); sOpenPopup = NULL; } } // ============================================================ // tabEnsureVisible -- scroll so active tab is visible // ============================================================ static void tabEnsureVisible(WidgetT *w, const BitmapFontT *font) { TabControlDataT *d = (TabControlDataT *)w->data; if (!tabNeedScroll(w, font)) { d->scrollOffset = 0; return; } int32_t headerW = w->w - TAB_ARROW_W * 2 - 4; if (headerW < 1) { return; } // Find start and end X of the active tab int32_t tabX = 0; int32_t tabIdx = 0; for (WidgetT *c = w->firstChild; c; c = c->nextSibling) { if (c->type != sTabPageTypeId) { continue; } TabPageDataT *pd = (TabPageDataT *)c->data; int32_t tw = textWidthAccel(font, pd->title) + TAB_PAD_H * 2; if (tabIdx == d->activeTab) { int32_t tabLeft = tabX - d->scrollOffset; int32_t tabRight = tabLeft + tw; if (tabLeft < 0) { d->scrollOffset += tabLeft; } else if (tabRight > headerW) { d->scrollOffset += tabRight - headerW; } break; } tabX += tw; tabIdx++; } // Clamp int32_t totalW = tabHeaderTotalW(w, font); int32_t maxOff = totalW - headerW; if (maxOff < 0) { maxOff = 0; } d->scrollOffset = clampInt(d->scrollOffset, 0, maxOff); } // ============================================================ // tabHeaderTotalW -- total width of all tab headers // ============================================================ static int32_t tabHeaderTotalW(const WidgetT *w, const BitmapFontT *font) { int32_t total = 0; for (WidgetT *c = w->firstChild; c; c = c->nextSibling) { if (c->type == sTabPageTypeId) { TabPageDataT *pd = (TabPageDataT *)c->data; total += textWidthAccel(font, pd->title) + TAB_PAD_H * 2; } } return total; } // ============================================================ // tabNeedScroll -- do tab headers overflow? // ============================================================ static bool tabNeedScroll(const WidgetT *w, const BitmapFontT *font) { int32_t totalW = tabHeaderTotalW(w, font); return totalW > (w->w - 4); } // ============================================================ // widgetTabControlCalcMinSize // ============================================================ // Min size: tab header height + the maximum min size across ALL pages // (not just the active one). This ensures the tab control reserves // enough space for the largest page, preventing resize flicker when // switching tabs. Children are recursively measured. void widgetTabControlCalcMinSize(WidgetT *w, const BitmapFontT *font) { int32_t tabH = font->charHeight + TAB_PAD_V * 2; int32_t maxPageW = 0; int32_t maxPageH = 0; for (WidgetT *c = w->firstChild; c; c = c->nextSibling) { if (c->type != sTabPageTypeId) { continue; } widgetCalcMinSizeTree(c, font); maxPageW = DVX_MAX(maxPageW, c->calcMinW); maxPageH = DVX_MAX(maxPageH, c->calcMinH); } w->calcMinW = maxPageW + TAB_BORDER * 2; w->calcMinH = tabH + maxPageH + TAB_BORDER * 2; } // ============================================================ // widgetTabControlLayout // ============================================================ void widgetTabControlLayout(WidgetT *w, const BitmapFontT *font) { TabControlDataT *d = (TabControlDataT *)w->data; int32_t tabH = font->charHeight + TAB_PAD_V * 2; int32_t contentX = w->x + TAB_BORDER; int32_t contentY = w->y + tabH + TAB_BORDER; int32_t contentW = w->w - TAB_BORDER * 2; int32_t contentH = w->h - tabH - TAB_BORDER * 2; if (contentW < 0) { contentW = 0; } if (contentH < 0) { contentH = 0; } tabEnsureVisible(w, font); int32_t idx = 0; for (WidgetT *c = w->firstChild; c; c = c->nextSibling) { if (c->type != sTabPageTypeId) { continue; } c->x = contentX; c->y = contentY; c->w = contentW; c->h = contentH; if (idx == d->activeTab) { c->visible = true; widgetLayoutChildren(c, font); } else { c->visible = false; } idx++; } } // ============================================================ // widgetTabControlOnKey // ============================================================ // Keyboard navigation: Left/Right cycle through tabs with wrapping // (modular arithmetic). Home/End jump to first/last tab. The tab // control only handles these keys when it has focus -- if a child // widget inside the active page has focus, keys go there instead. void widgetTabControlOnKey(WidgetT *w, int32_t key, int32_t mod) { (void)mod; TabControlDataT *d = (TabControlDataT *)w->data; int32_t tabCount = 0; for (WidgetT *c = w->firstChild; c; c = c->nextSibling) { if (c->type == sTabPageTypeId) { tabCount++; } } if (tabCount <= 1) { return; } int32_t active = d->activeTab; if (key == (0x4D | 0x100)) { active = (active + 1) % tabCount; } else if (key == (0x4B | 0x100)) { active = (active - 1 + tabCount) % tabCount; } else if (key == (0x47 | 0x100)) { active = 0; } else if (key == (0x4F | 0x100)) { active = tabCount - 1; } else { return; } if (active != d->activeTab) { tabClosePopup(); d->activeTab = active; if (w->onChange) { w->onChange(w); } wgtInvalidate(w); } } // ============================================================ // widgetTabControlOnMouse // ============================================================ // Mouse clicks in the tab header area walk the tab list computing // accumulated X positions to find which tab was clicked. Only clicks // in the header strip (top tabH pixels) are handled here -- clicks // on the content area go through normal child hit-testing. Scroll // arrow clicks adjust scrollOffset by 4 character widths at a time. void widgetTabControlOnMouse(WidgetT *hit, WidgetT *root, int32_t vx, int32_t vy) { hit->focused = true; TabControlDataT *d = (TabControlDataT *)hit->data; AppContextT *ctx = (AppContextT *)root->userData; const BitmapFontT *font = &ctx->font; int32_t tabH = font->charHeight + TAB_PAD_V * 2; // Only handle clicks in the tab header area if (vy < hit->y || vy >= hit->y + tabH) { return; } bool scroll = tabNeedScroll(hit, font); // Check scroll arrow clicks if (scroll) { int32_t totalW = tabHeaderTotalW(hit, font); int32_t headerW = hit->w - TAB_ARROW_W * 2 - 4; int32_t maxOff = totalW - headerW; if (maxOff < 0) { maxOff = 0; } // Left arrow if (vx >= hit->x && vx < hit->x + TAB_ARROW_W) { d->scrollOffset -= font->charWidth * 4; d->scrollOffset = clampInt(d->scrollOffset, 0, maxOff); wgtInvalidatePaint(hit); return; } // Right arrow if (vx >= hit->x + hit->w - TAB_ARROW_W && vx < hit->x + hit->w) { d->scrollOffset += font->charWidth * 4; d->scrollOffset = clampInt(d->scrollOffset, 0, maxOff); wgtInvalidatePaint(hit); return; } } // Click on tab header int32_t headerLeft = hit->x + 2 + (scroll ? TAB_ARROW_W : 0); int32_t tabX = headerLeft - d->scrollOffset; int32_t tabIdx = 0; for (WidgetT *c = hit->firstChild; c; c = c->nextSibling) { if (c->type != sTabPageTypeId) { continue; } TabPageDataT *pd = (TabPageDataT *)c->data; int32_t tw = textWidthAccel(font, pd->title) + TAB_PAD_H * 2; if (vx >= tabX && vx < tabX + tw && vx >= headerLeft) { if (tabIdx != d->activeTab) { tabClosePopup(); d->activeTab = tabIdx; if (hit->onChange) { hit->onChange(hit); } } break; } tabX += tw; tabIdx++; } } // ============================================================ // widgetTabControlPaint // ============================================================ // Paint order: content panel first (raised bevel below the tab strip), // then scroll arrows if needed, then tab headers in a clipped region, // then the active page's children. Tab headers are painted with a clip // rect so partially-scrolled tabs at the edges are cleanly truncated. // // The active tab is drawn 2px taller (extending from w->y instead of // w->y+2) and erases the panel's top border beneath it (2px of // contentBg), creating the visual connection between tab and panel. // Inactive tabs sit 2px lower and draw a bottom border to separate // them from the panel. // // Only the active page's children are painted (WCLASS_PAINTS_CHILDREN // flag means the generic paint won't descend into tab control children). // This is critical for performance on 486 -- we skip painting all // hidden pages entirely. void widgetTabControlPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) { TabControlDataT *td = (TabControlDataT *)w->data; int32_t tabH = font->charHeight + TAB_PAD_V * 2; bool scroll = tabNeedScroll(w, font); // Content panel BevelStyleT panelBevel; panelBevel.highlight = colors->windowHighlight; panelBevel.shadow = colors->windowShadow; panelBevel.face = colors->contentBg; panelBevel.width = 2; drawBevel(d, ops, w->x, w->y + tabH, w->w, w->h - tabH, &panelBevel); // Scroll arrows if (scroll) { int32_t totalW = tabHeaderTotalW(w, font); int32_t headerW = w->w - TAB_ARROW_W * 2 - 4; int32_t maxOff = totalW - headerW; if (maxOff < 0) { maxOff = 0; } td->scrollOffset = clampInt(td->scrollOffset, 0, maxOff); uint32_t fg = colors->contentFg; BevelStyleT btnBevel = BEVEL_RAISED(colors, 1); // Left arrow button drawBevel(d, ops, w->x, w->y, TAB_ARROW_W, tabH, &btnBevel); { int32_t cx = w->x + TAB_ARROW_W / 2; int32_t cy = w->y + tabH / 2; for (int32_t i = 0; i < 4; i++) { drawVLine(d, ops, cx - 2 + i, cy - i, 1 + i * 2, fg); } } // Right arrow button int32_t rx = w->x + w->w - TAB_ARROW_W; drawBevel(d, ops, rx, w->y, TAB_ARROW_W, tabH, &btnBevel); { int32_t cx = rx + TAB_ARROW_W / 2; int32_t cy = w->y + tabH / 2; for (int32_t i = 0; i < 4; i++) { drawVLine(d, ops, cx + 2 - i, cy - i, 1 + i * 2, fg); } } } // Tab headers -- clip to header area int32_t headerLeft = w->x + 2 + (scroll ? TAB_ARROW_W : 0); int32_t headerRight = scroll ? (w->x + w->w - TAB_ARROW_W) : (w->x + w->w); int32_t oldClipX = d->clipX; int32_t oldClipY = d->clipY; int32_t oldClipW = d->clipW; int32_t oldClipH = d->clipH; setClipRect(d, headerLeft, w->y, headerRight - headerLeft, tabH + 2); int32_t tabX = headerLeft - td->scrollOffset; int32_t tabIdx = 0; for (WidgetT *c = w->firstChild; c; c = c->nextSibling) { if (c->type != sTabPageTypeId) { continue; } TabPageDataT *pd = (TabPageDataT *)c->data; int32_t tw = textWidthAccel(font, pd->title) + TAB_PAD_H * 2; bool isActive = (tabIdx == td->activeTab); int32_t ty = isActive ? w->y : w->y + 2; int32_t th = isActive ? tabH + 2 : tabH; uint32_t tabFace = isActive ? colors->contentBg : colors->windowFace; // Only draw tabs that are at least partially visible if (tabX + tw > headerLeft && tabX < headerRight) { // Fill tab background rectFill(d, ops, tabX + 2, ty + 2, tw - 4, th - 2, tabFace); // Top edge drawHLine(d, ops, tabX + 2, ty, tw - 4, colors->windowHighlight); drawHLine(d, ops, tabX + 2, ty + 1, tw - 4, colors->windowHighlight); // Left edge drawVLine(d, ops, tabX, ty + 2, th - 2, colors->windowHighlight); drawVLine(d, ops, tabX + 1, ty + 2, th - 2, colors->windowHighlight); // Right edge drawVLine(d, ops, tabX + tw - 1, ty + 2, th - 2, colors->windowShadow); drawVLine(d, ops, tabX + tw - 2, ty + 2, th - 2, colors->windowShadow); if (isActive) { // Erase panel top border under active tab rectFill(d, ops, tabX + 2, w->y + tabH, tw - 4, 2, colors->contentBg); } else { // Bottom edge for inactive tab drawHLine(d, ops, tabX, ty + th - 1, tw, colors->windowShadow); drawHLine(d, ops, tabX + 1, ty + th - 2, tw - 2, colors->windowShadow); } // Tab label int32_t labelY = ty + TAB_PAD_V; if (!isActive) { labelY++; } drawTextAccel(d, ops, font, tabX + TAB_PAD_H, labelY, pd->title, colors->contentFg, tabFace, true); if (isActive && w->focused) { drawFocusRect(d, ops, tabX + 3, ty + 3, tw - 6, th - 4, colors->contentFg); } } tabX += tw; tabIdx++; } setClipRect(d, oldClipX, oldClipY, oldClipW, oldClipH); // Paint only active tab page's children tabIdx = 0; for (WidgetT *c = w->firstChild; c; c = c->nextSibling) { if (c->type != sTabPageTypeId) { continue; } if (tabIdx == td->activeTab) { for (WidgetT *gc = c->firstChild; gc; gc = gc->nextSibling) { widgetPaintOne(gc, d, ops, font, colors); } break; } tabIdx++; } } // ============================================================ // DXE registration // ============================================================ // ============================================================ // widgetTabPageAccelActivate // ============================================================ void widgetTabPageAccelActivate(WidgetT *w, WidgetT *root) { (void)root; if (!w->parent || w->parent->type != sTabControlTypeId) { return; } TabControlDataT *d = (TabControlDataT *)w->parent->data; // Find our index among sibling tab pages int32_t idx = 0; for (WidgetT *c = w->parent->firstChild; c; c = c->nextSibling) { if (c->type != sTabPageTypeId) { continue; } if (c == w) { tabClosePopup(); d->activeTab = idx; wgtInvalidate(w->parent); return; } idx++; } } // ============================================================ // widgetTabControlDestroy // ============================================================ void widgetTabControlDestroy(WidgetT *w) { free(w->data); } // ============================================================ // widgetTabPageDestroy // ============================================================ void widgetTabPageDestroy(WidgetT *w) { free(w->data); } static const WidgetClassT sClassTabControl = { .version = WGT_CLASS_VERSION, .flags = WCLASS_FOCUSABLE | WCLASS_PAINTS_CHILDREN, .handlers = { [WGT_METHOD_PAINT] = (void *)widgetTabControlPaint, [WGT_METHOD_CALC_MIN_SIZE] = (void *)widgetTabControlCalcMinSize, [WGT_METHOD_LAYOUT] = (void *)widgetTabControlLayout, [WGT_METHOD_ON_MOUSE] = (void *)widgetTabControlOnMouse, [WGT_METHOD_ON_KEY] = (void *)widgetTabControlOnKey, [WGT_METHOD_DESTROY] = (void *)widgetTabControlDestroy, } }; static const WidgetClassT sClassTabPage = { .version = WGT_CLASS_VERSION, .flags = WCLASS_BOX_CONTAINER | WCLASS_ACCEL_WHEN_HIDDEN, .handlers = { [WGT_METHOD_ON_ACCEL_ACTIVATE] = (void *)widgetTabPageAccelActivate, [WGT_METHOD_DESTROY] = (void *)widgetTabPageDestroy, } }; // ============================================================ // Widget creation functions // ============================================================ WidgetT *wgtTabControl(WidgetT *parent) { WidgetT *w = widgetAlloc(parent, sTabControlTypeId); if (w) { TabControlDataT *d = calloc(1, sizeof(TabControlDataT)); d->activeTab = 0; d->scrollOffset = 0; w->data = d; w->weight = 100; } return w; } int32_t wgtTabControlGetActive(const WidgetT *w) { VALIDATE_WIDGET(w, sTabControlTypeId, 0); TabControlDataT *d = (TabControlDataT *)w->data; return d->activeTab; } void wgtTabControlSetActive(WidgetT *w, int32_t idx) { VALIDATE_WIDGET_VOID(w, sTabControlTypeId); TabControlDataT *d = (TabControlDataT *)w->data; d->activeTab = idx; wgtInvalidate(w); } WidgetT *wgtTabPage(WidgetT *parent, const char *title) { WidgetT *w = widgetAlloc(parent, sTabPageTypeId); if (w) { TabPageDataT *d = calloc(1, sizeof(TabPageDataT)); d->title = title; w->data = d; w->accelKey = accelParse(title); } return w; } // ============================================================ // DXE registration // ============================================================ static const struct { WidgetT *(*create)(WidgetT *parent); WidgetT *(*page)(WidgetT *parent, const char *title); void (*setActive)(WidgetT *w, int32_t idx); int32_t (*getActive)(const WidgetT *w); } sApi = { .create = wgtTabControl, .page = wgtTabPage, .setActive = wgtTabControlSetActive, .getActive = wgtTabControlGetActive }; static const WgtPropDescT sProps[] = { { "TabIndex", WGT_IFACE_INT, (void *)wgtTabControlGetActive, (void *)wgtTabControlSetActive } }; static const WgtMethodDescT sMethods[] = { { "SetActive", WGT_SIG_INT, (void *)wgtTabControlSetActive } }; static const WgtIfaceT sIface = { .basName = "TabStrip", .props = sProps, .propCount = 1, .methods = sMethods, .methodCount = 1, .events = NULL, .eventCount = 0 }; void wgtRegister(void) { sTabControlTypeId = wgtRegisterClass(&sClassTabControl); sTabPageTypeId = wgtRegisterClass(&sClassTabPage); wgtRegisterApi("tabcontrol", &sApi); wgtRegisterIface("tabcontrol", &sIface); }