// widgetComboBox.c -- ComboBox widget (editable text + dropdown list) // // Combines a single-line text input with a dropdown list. The text area // supports full editing (cursor movement, selection, undo, clipboard) via // the shared widgetTextEditOnKey helper, while the dropdown button opens // a popup list overlay. // // This is a "combo" box in the Windows sense: the user can either type a // value or select from the list. When an item is selected from the list, // its text is copied into the edit buffer. The edit buffer is independently // allocated (malloc'd) so the user can modify the text after selecting. // // The popup list is painted as an overlay (widgetComboBoxPaintPopup) that // renders on top of all other widgets. Popup visibility is coordinated // through the sOpenPopup global -- only one popup can be open at a time. // The sClosedPopup mechanism prevents click-to-close from immediately // reopening the popup when the close click lands on the dropdown button. // // Text selection supports single-click (cursor placement + drag start), // double-click (word select), and triple-click (select all). Drag-select // is tracked via the sDragTextSelect global. #include "widgetInternal.h" // ============================================================ // wgtComboBox // ============================================================ // Create a combo box. maxLen controls the edit buffer size (0 = default 256). // Two buffers are allocated: the edit buffer and an undo buffer (for Ctrl+Z // single-level undo). Selection indices start at -1 (nothing selected). // Default weight=100 makes combo boxes expand to fill available space in // a layout container, which is the typical desired behavior. 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.undoBuf = (char *)malloc(bufSize); w->as.comboBox.bufSize = bufSize; if (!w->as.comboBox.buf || !w->as.comboBox.undoBuf) { free(w->as.comboBox.buf); free(w->as.comboBox.undoBuf); w->as.comboBox.buf = NULL; w->as.comboBox.undoBuf = NULL; } else { w->as.comboBox.buf[0] = '\0'; } w->as.comboBox.selStart = -1; w->as.comboBox.selEnd = -1; w->as.comboBox.selectedIdx = -1; w->weight = 100; } return w; } // ============================================================ // wgtComboBoxGetSelected // ============================================================ int32_t wgtComboBoxGetSelected(const WidgetT *w) { VALIDATE_WIDGET(w, WidgetComboBoxE, -1); return w->as.comboBox.selectedIdx; } // ============================================================ // wgtComboBoxSetItems // ============================================================ void wgtComboBoxSetItems(WidgetT *w, const char **items, int32_t count) { VALIDATE_WIDGET_VOID(w, WidgetComboBoxE); w->as.comboBox.items = items; w->as.comboBox.itemCount = count; // Cache max item string length so calcMinSize doesn't need to re-scan // the entire item array on every layout pass. Items are stored as // external pointers (not copied) -- the caller owns the string data. int32_t maxLen = 0; for (int32_t i = 0; i < count; i++) { int32_t slen = (int32_t)strlen(items[i]); if (slen > maxLen) { maxLen = slen; } } w->as.comboBox.maxItemLen = maxLen; if (w->as.comboBox.selectedIdx >= count) { w->as.comboBox.selectedIdx = -1; } // wgtInvalidate (not wgtInvalidatePaint) triggers a full relayout because // changing items may change the widget's minimum width wgtInvalidate(w); } // ============================================================ // wgtComboBoxSetSelected // ============================================================ void wgtComboBoxSetSelected(WidgetT *w, int32_t idx) { VALIDATE_WIDGET_VOID(w, WidgetComboBoxE); 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; w->as.comboBox.selStart = -1; w->as.comboBox.selEnd = -1; } wgtInvalidatePaint(w); } // ============================================================ // widgetComboBoxCalcMinSize // ============================================================ void widgetComboBoxCalcMinSize(WidgetT *w, const BitmapFontT *font) { int32_t maxItemW = w->as.comboBox.maxItemLen * font->charWidth; int32_t minW = font->charWidth * 8; if (maxItemW < minW) { maxItemW = minW; } w->calcMinW = maxItemW + DROPDOWN_BTN_WIDTH + TEXT_INPUT_PAD * 2 + 4; w->calcMinH = font->charHeight + TEXT_INPUT_PAD * 2; } // ============================================================ // widgetComboBoxDestroy // ============================================================ void widgetComboBoxDestroy(WidgetT *w) { free(w->as.comboBox.buf); free(w->as.comboBox.undoBuf); } // ============================================================ // widgetComboBoxGetText // ============================================================ const char *widgetComboBoxGetText(const WidgetT *w) { return w->as.comboBox.buf ? w->as.comboBox.buf : ""; } // ============================================================ // widgetComboBoxOnKey // ============================================================ // Key handling has two modes: when the popup is open, Up/Down navigate the list // and Enter confirms the selection. When closed, keys go to the text editor // (via widgetTextEditOnKey) except Down-arrow which opens the popup. This split // behavior is necessary because the same widget must serve as both a text input // and a list selector depending on popup state. // Key codes: 0x48|0x100 = Up, 0x50|0x100 = Down (BIOS scan codes with extended bit). void widgetComboBoxOnKey(WidgetT *w, int32_t key, int32_t mod) { 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; } } wgtInvalidatePaint(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; } } wgtInvalidatePaint(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.selStart = -1; w->as.comboBox.selEnd = -1; } w->as.comboBox.open = false; sOpenPopup = NULL; if (w->onChange) { w->onChange(w); } wgtInvalidatePaint(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; } wgtInvalidatePaint(w); return; } // Text editing (when popup is closed, or non-navigation keys with popup open) if (!w->as.comboBox.buf) { return; } clearOtherSelections(w); widgetTextEditOnKey(w, key, mod, w->as.comboBox.buf, w->as.comboBox.bufSize, &w->as.comboBox.len, &w->as.comboBox.cursorPos, &w->as.comboBox.scrollOff, &w->as.comboBox.selStart, &w->as.comboBox.selEnd, w->as.comboBox.undoBuf, &w->as.comboBox.undoLen, &w->as.comboBox.undoCursor); } // ============================================================ // widgetComboBoxOnMouse // ============================================================ void widgetComboBoxOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t 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 clearOtherSelections(w); AppContextT *ctx = (AppContextT *)root->userData; widgetTextEditMouseClick(w, vx, vy, w->x + TEXT_INPUT_PAD, &ctx->font, w->as.comboBox.buf, w->as.comboBox.len, w->as.comboBox.scrollOff, &w->as.comboBox.cursorPos, &w->as.comboBox.selStart, &w->as.comboBox.selEnd, true, true); } } // ============================================================ // widgetComboBoxPaint // ============================================================ // Paint: two regions side-by-side -- a sunken text area (left) and a raised // dropdown button (right). The text area renders the edit buffer with optional // selection highlighting (up to 3 text runs: pre-selection, selection, // post-selection). The dropdown button has a small triangular arrow glyph // drawn as horizontal lines of decreasing width. When the popup is open, // the button bevel is inverted (sunken) to show it's active. void widgetComboBoxPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) { uint32_t fg = w->enabled ? (w->fgColor ? w->fgColor : colors->contentFg) : colors->windowShadow; 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; } widgetTextEditPaintLine(d, ops, font, colors, textX, textY, w->as.comboBox.buf + off, len, off, w->as.comboBox.cursorPos, w->as.comboBox.selStart, w->as.comboBox.selEnd, fg, bg, w->focused && w->enabled && !w->as.comboBox.open, w->x + TEXT_INPUT_PAD, w->x + textAreaW - TEXT_INPUT_PAD); } // 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 uint32_t arrowFg = w->enabled ? colors->contentFg : colors->windowShadow; 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, arrowFg); } } // ============================================================ // 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); widgetPaintPopupList(d, ops, font, colors, popX, popY, popW, popH, w->as.comboBox.items, w->as.comboBox.itemCount, w->as.comboBox.hoverIdx, w->as.comboBox.listScrollPos); } // ============================================================ // 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; w->as.comboBox.selStart = -1; w->as.comboBox.selEnd = -1; } }