// widgetComboBox.c — ComboBox widget (editable text + dropdown list) #include "widgetInternal.h" // ============================================================ // wgtComboBox // ============================================================ WidgetT *wgtComboBox(WidgetT *parent, int32_t maxLen) { WidgetT *w = widgetAlloc(parent, WidgetComboBoxE); if (w) { int32_t bufSize = maxLen > 0 ? maxLen + 1 : 256; w->as.comboBox.buf = (char *)malloc(bufSize); w->as.comboBox.bufSize = bufSize; if (w->as.comboBox.buf) { w->as.comboBox.buf[0] = '\0'; } w->as.comboBox.selectedIdx = -1; w->weight = 100; } return w; } // ============================================================ // wgtComboBoxGetSelected // ============================================================ int32_t wgtComboBoxGetSelected(const WidgetT *w) { if (!w || w->type != WidgetComboBoxE) { return -1; } return w->as.comboBox.selectedIdx; } // ============================================================ // wgtComboBoxSetItems // ============================================================ void wgtComboBoxSetItems(WidgetT *w, const char **items, int32_t count) { if (!w || w->type != WidgetComboBoxE) { return; } w->as.comboBox.items = items; w->as.comboBox.itemCount = count; if (w->as.comboBox.selectedIdx >= count) { w->as.comboBox.selectedIdx = -1; } } // ============================================================ // wgtComboBoxSetSelected // ============================================================ void wgtComboBoxSetSelected(WidgetT *w, int32_t idx) { if (!w || w->type != WidgetComboBoxE) { return; } w->as.comboBox.selectedIdx = idx; // Copy selected item text to buffer if (idx >= 0 && idx < w->as.comboBox.itemCount && w->as.comboBox.buf) { strncpy(w->as.comboBox.buf, w->as.comboBox.items[idx], w->as.comboBox.bufSize - 1); w->as.comboBox.buf[w->as.comboBox.bufSize - 1] = '\0'; w->as.comboBox.len = (int32_t)strlen(w->as.comboBox.buf); w->as.comboBox.cursorPos = w->as.comboBox.len; w->as.comboBox.scrollOff = 0; } } // ============================================================ // widgetComboBoxDestroy // ============================================================ void widgetComboBoxDestroy(WidgetT *w) { free(w->as.comboBox.buf); } // ============================================================ // widgetComboBoxGetText // ============================================================ const char *widgetComboBoxGetText(const WidgetT *w) { return w->as.comboBox.buf ? w->as.comboBox.buf : ""; } // ============================================================ // widgetComboBoxSetText // ============================================================ void widgetComboBoxSetText(WidgetT *w, const char *text) { if (w->as.comboBox.buf) { strncpy(w->as.comboBox.buf, text, w->as.comboBox.bufSize - 1); w->as.comboBox.buf[w->as.comboBox.bufSize - 1] = '\0'; w->as.comboBox.len = (int32_t)strlen(w->as.comboBox.buf); w->as.comboBox.cursorPos = w->as.comboBox.len; w->as.comboBox.scrollOff = 0; } } // ============================================================ // widgetComboBoxCalcMinSize // ============================================================ void widgetComboBoxCalcMinSize(WidgetT *w, const BitmapFontT *font) { int32_t maxItemW = font->charWidth * 8; for (int32_t i = 0; i < w->as.comboBox.itemCount; i++) { int32_t iw = (int32_t)strlen(w->as.comboBox.items[i]) * font->charWidth; if (iw > maxItemW) { maxItemW = iw; } } w->calcMinW = maxItemW + DROPDOWN_BTN_WIDTH + TEXT_INPUT_PAD * 2 + 4; w->calcMinH = font->charHeight + TEXT_INPUT_PAD * 2; } // ============================================================ // widgetComboBoxOnKey // ============================================================ void widgetComboBoxOnKey(WidgetT *w, int32_t key) { if (w->as.comboBox.open) { if (key == (0x48 | 0x100)) { if (w->as.comboBox.hoverIdx > 0) { w->as.comboBox.hoverIdx--; if (w->as.comboBox.hoverIdx < w->as.comboBox.listScrollPos) { w->as.comboBox.listScrollPos = w->as.comboBox.hoverIdx; } } wgtInvalidate(w); return; } if (key == (0x50 | 0x100)) { if (w->as.comboBox.hoverIdx < w->as.comboBox.itemCount - 1) { w->as.comboBox.hoverIdx++; if (w->as.comboBox.hoverIdx >= w->as.comboBox.listScrollPos + DROPDOWN_MAX_VISIBLE) { w->as.comboBox.listScrollPos = w->as.comboBox.hoverIdx - DROPDOWN_MAX_VISIBLE + 1; } } wgtInvalidate(w); return; } if (key == 0x0D) { int32_t idx = w->as.comboBox.hoverIdx; if (idx >= 0 && idx < w->as.comboBox.itemCount) { w->as.comboBox.selectedIdx = idx; const char *itemText = w->as.comboBox.items[idx]; strncpy(w->as.comboBox.buf, itemText, w->as.comboBox.bufSize - 1); w->as.comboBox.buf[w->as.comboBox.bufSize - 1] = '\0'; w->as.comboBox.len = (int32_t)strlen(w->as.comboBox.buf); w->as.comboBox.cursorPos = w->as.comboBox.len; w->as.comboBox.scrollOff = 0; } w->as.comboBox.open = false; sOpenPopup = NULL; if (w->onChange) { w->onChange(w); } wgtInvalidate(w); return; } } // Down arrow on closed combobox opens the popup if (!w->as.comboBox.open && key == (0x50 | 0x100)) { w->as.comboBox.open = true; w->as.comboBox.hoverIdx = w->as.comboBox.selectedIdx; sOpenPopup = w; if (w->as.comboBox.hoverIdx >= w->as.comboBox.listScrollPos + DROPDOWN_MAX_VISIBLE) { w->as.comboBox.listScrollPos = w->as.comboBox.hoverIdx - DROPDOWN_MAX_VISIBLE + 1; } if (w->as.comboBox.hoverIdx < w->as.comboBox.listScrollPos) { w->as.comboBox.listScrollPos = w->as.comboBox.hoverIdx; } wgtInvalidate(w); return; } // Text editing (when popup is closed, or non-navigation keys with popup open) if (!w->as.comboBox.buf) { return; } widgetTextEditOnKey(w, key, w->as.comboBox.buf, w->as.comboBox.bufSize, &w->as.comboBox.len, &w->as.comboBox.cursorPos, &w->as.comboBox.scrollOff); } // ============================================================ // widgetComboBoxOnMouse // ============================================================ void widgetComboBoxOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy) { (void)vy; w->focused = true; // Check if click is on the button area int32_t textAreaW = w->w - DROPDOWN_BTN_WIDTH; if (vx >= w->x + textAreaW) { // If this combobox's popup was just closed by click-outside, don't re-open if (w == sClosedPopup) { return; } // Button click — toggle popup w->as.comboBox.open = !w->as.comboBox.open; w->as.comboBox.hoverIdx = w->as.comboBox.selectedIdx; sOpenPopup = w->as.comboBox.open ? w : NULL; } else { // Text area click — focus for editing AppContextT *ctx = (AppContextT *)root->userData; const BitmapFontT *font = &ctx->font; int32_t relX = vx - w->x - TEXT_INPUT_PAD; int32_t charPos = relX / font->charWidth + w->as.comboBox.scrollOff; if (charPos < 0) { charPos = 0; } if (charPos > w->as.comboBox.len) { charPos = w->as.comboBox.len; } w->as.comboBox.cursorPos = charPos; } } // ============================================================ // widgetComboBoxPaint // ============================================================ void widgetComboBoxPaint(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; // Sunken text area int32_t textAreaW = w->w - DROPDOWN_BTN_WIDTH; BevelStyleT bevel; bevel.highlight = colors->windowShadow; bevel.shadow = colors->windowHighlight; bevel.face = bg; bevel.width = 2; drawBevel(d, ops, w->x, w->y, textAreaW, w->h, &bevel); // Draw text content if (w->as.comboBox.buf) { int32_t textX = w->x + TEXT_INPUT_PAD; int32_t textY = w->y + (w->h - font->charHeight) / 2; int32_t maxChars = (textAreaW - TEXT_INPUT_PAD * 2 - 4) / font->charWidth; int32_t off = w->as.comboBox.scrollOff; int32_t len = w->as.comboBox.len - off; if (len > maxChars) { len = maxChars; } for (int32_t i = 0; i < len; i++) { drawChar(d, ops, font, textX + i * font->charWidth, textY, w->as.comboBox.buf[off + i], fg, bg, true); } // Draw cursor if (w->focused && !w->as.comboBox.open) { int32_t cursorX = textX + (w->as.comboBox.cursorPos - off) * font->charWidth; if (cursorX >= w->x + TEXT_INPUT_PAD && cursorX < w->x + textAreaW - TEXT_INPUT_PAD) { drawVLine(d, ops, cursorX, textY, font->charHeight, fg); } } } // Drop button BevelStyleT btnBevel; btnBevel.highlight = w->as.comboBox.open ? colors->windowShadow : colors->windowHighlight; btnBevel.shadow = w->as.comboBox.open ? colors->windowHighlight : colors->windowShadow; btnBevel.face = colors->buttonFace; btnBevel.width = 2; drawBevel(d, ops, w->x + textAreaW, w->y, DROPDOWN_BTN_WIDTH, w->h, &btnBevel); // Down arrow int32_t arrowX = w->x + textAreaW + DROPDOWN_BTN_WIDTH / 2; int32_t arrowY = w->y + w->h / 2 - 1; for (int32_t i = 0; i < 4; i++) { drawHLine(d, ops, arrowX - 3 + i, arrowY + i, 7 - i * 2, colors->contentFg); } } // ============================================================ // widgetComboBoxPaintPopup // ============================================================ void widgetComboBoxPaintPopup(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) { int32_t popX; int32_t popY; int32_t popW; int32_t popH; widgetDropdownPopupRect(w, font, d->clipH, &popX, &popY, &popW, &popH); // Draw popup border BevelStyleT bevel; bevel.highlight = colors->windowHighlight; bevel.shadow = colors->windowShadow; bevel.face = colors->contentBg; bevel.width = 2; drawBevel(d, ops, popX, popY, popW, popH, &bevel); // Draw items int32_t itemCount = w->as.comboBox.itemCount; const char **items = w->as.comboBox.items; int32_t hoverIdx = w->as.comboBox.hoverIdx; int32_t scrollPos = w->as.comboBox.listScrollPos; int32_t visibleItems = popH / font->charHeight; int32_t textX = popX + TEXT_INPUT_PAD; int32_t textY = popY + 2; int32_t textW = popW - TEXT_INPUT_PAD * 2 - 4; for (int32_t i = 0; i < visibleItems && (scrollPos + i) < itemCount; i++) { int32_t idx = scrollPos + i; int32_t iy = textY + i * font->charHeight; uint32_t ifg = colors->contentFg; uint32_t ibg = colors->contentBg; if (idx == hoverIdx) { ifg = colors->menuHighlightFg; ibg = colors->menuHighlightBg; rectFill(d, ops, popX + 2, iy, textW + TEXT_INPUT_PAD * 2, font->charHeight, ibg); } drawText(d, ops, font, textX, iy, items[idx], ifg, ibg, idx == hoverIdx); } }