#define DVX_WIDGET_IMPL // widgetTextInput.c -- TextInput and TextArea widgets // // This file implements two text editing widgets: // // 1. TextInput -- single-line text field with scroll, selection, // undo, password masking, and masked input (e.g., phone/SSN). // 2. TextArea -- multi-line text editor with row/col cursor, // dual-axis scrolling, and full selection/clipboard support. // // Shared infrastructure (clipboard, multi-click detection, word // boundary logic, cross-widget selection clearing, and the // single-line editing engine widgetTextEditOnKey) lives in // widgetCore.c so it can be linked from any widget DXE. // // All text editing is done in-place in fixed-size char buffers // allocated at widget creation. No dynamic resizing -- this keeps // memory management simple and predictable on DOS where heap // fragmentation is a real concern. // // Undo is single-level swap: before each mutation, the current // buffer is copied to undoBuf. Ctrl+Z swaps current<->undo, so // a second Ctrl+Z is "redo". This is simpler than a multi-level // undo stack and sufficient for typical DOS text entry. // // Selection model: selStart/selEnd (single-line) or selAnchor/ // selCursor (multi-line) form a directed range. selStart is where // the user began selecting, selEnd is where they stopped. The // "low" end for deletion/copy is always min(start, end). The // -1 sentinel means "no selection". // // Clipboard is a simple static buffer (4KB). This is a process-wide // clipboard, not per-widget and not OS-integrated (DOS has no // clipboard API). Text cut/copied from any widget is available to // paste in any other widget. // // Multi-click detection uses clock() timestamps with a 500ms // threshold and 4px spatial tolerance. Double-click selects word, // triple-click selects line (TextArea) or all (TextInput). // // Cross-widget selection clearing: when a widget gains selection, // clearOtherSelections() deselects any other widget that had an // active selection. This prevents the confusing visual state of // multiple selected ranges across different widgets. The tracking // uses sLastSelectedWidget to achieve O(1) clearing rather than // walking the entire widget tree. // // Masked input: a special TextInput mode where the buffer is // pre-filled from a mask pattern (e.g., "###-##-####" for SSN). // '#' accepts digits, 'A' accepts letters, '*' accepts any // printable char. Literal characters in the mask are fixed and the // cursor skips over them. This provides constrained input without // needing a separate widget type. // // Password mode: renders bullets instead of characters (CP437 0xF9) // and blocks copy/cut operations for security. #include "dvxWidgetPlugin.h" #include "../texthelp/textHelp.h" static int32_t sTextInputTypeId = -1; static int32_t sTextAreaTypeId = -1; typedef enum { InputNormalE, InputPasswordE, InputMaskedE } InputModeE; typedef struct { char *buf; int32_t bufSize; int32_t len; int32_t cursorPos; int32_t scrollOff; int32_t selStart; int32_t selEnd; char *undoBuf; int32_t undoLen; int32_t undoCursor; InputModeE inputMode; const char *mask; } TextInputDataT; typedef struct { char *buf; int32_t bufSize; int32_t len; int32_t cursorRow; int32_t cursorCol; int32_t scrollRow; int32_t scrollCol; int32_t desiredCol; int32_t selAnchor; int32_t selCursor; char *undoBuf; int32_t undoLen; int32_t undoCursor; int32_t cachedLines; int32_t cachedMaxLL; int32_t sbDragOrient; int32_t sbDragOff; bool sbDragging; } TextAreaDataT; #include #include #define TEXTAREA_BORDER 2 #define TEXTAREA_PAD 2 #define TEXTAREA_SB_W 14 #define TEXTAREA_MIN_ROWS 4 #define TEXTAREA_MIN_COLS 20 #define CLIPBOARD_MAX 4096 // Match the ANSI terminal cursor blink rate (CURSOR_MS in widgetAnsiTerm.c) #define CURSOR_BLINK_MS 250 // ============================================================ // Prototypes // ============================================================ static bool maskCharValid(char slot, char ch); static int32_t maskFirstSlot(const char *mask); static bool maskIsSlot(char ch); static int32_t maskNextSlot(const char *mask, int32_t pos); static int32_t maskPrevSlot(const char *mask, int32_t pos); static void maskedInputOnKey(WidgetT *w, int32_t key, int32_t mod); static int32_t textAreaCountLines(const char *buf, int32_t len); static int32_t textAreaCursorToOff(const char *buf, int32_t len, int32_t row, int32_t col); static inline void textAreaDirtyCache(WidgetT *w); static void textAreaEnsureVisible(WidgetT *w, int32_t visRows, int32_t visCols); static int32_t textAreaGetLineCount(WidgetT *w); static int32_t textAreaGetMaxLineLen(WidgetT *w); static int32_t textAreaLineLen(const char *buf, int32_t len, int32_t row); static int32_t textAreaLineStart(const char *buf, int32_t len, int32_t row); static int32_t textAreaMaxLineLen(const char *buf, int32_t len); static void textAreaOffToRowCol(const char *buf, int32_t off, int32_t *row, int32_t *col); static void textEditSaveUndo(char *buf, int32_t len, int32_t cursor, char *undoBuf, int32_t *pUndoLen, int32_t *pUndoCursor, int32_t bufSize); static void widgetTextAreaDragSelect(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy); static void widgetTextAreaOnDragUpdate(WidgetT *w, WidgetT *root, int32_t x, int32_t y); static int32_t wordBoundaryLeft(const char *buf, int32_t pos); static int32_t wordBoundaryRight(const char *buf, int32_t len, int32_t pos); WidgetT *wgtTextInput(WidgetT *parent, int32_t maxLen); // sCursorBlinkOn is defined in widgetCore.c (shared state) // sCursorBlinkTime is local to this module static clock_t sCursorBlinkTime = 0; // ============================================================ // Shared undo helpers // ============================================================ static void textEditSaveUndo(char *buf, int32_t len, int32_t cursor, char *undoBuf, int32_t *pUndoLen, int32_t *pUndoCursor, int32_t bufSize) { if (!undoBuf) { return; } int32_t copyLen = len < bufSize ? len : bufSize - 1; memcpy(undoBuf, buf, copyLen); undoBuf[copyLen] = '\0'; *pUndoLen = copyLen; *pUndoCursor = cursor; } // ============================================================ // wordBoundaryLeft // ============================================================ // // From position pos, skip non-word chars left, then skip word chars left. // Returns the position at the start of the word (or 0). static int32_t wordBoundaryLeft(const char *buf, int32_t pos) { if (pos <= 0) { return 0; } // Skip non-word characters while (pos > 0 && !isalnum((unsigned char)buf[pos - 1]) && buf[pos - 1] != '_') { pos--; } // Skip word characters while (pos > 0 && (isalnum((unsigned char)buf[pos - 1]) || buf[pos - 1] == '_')) { pos--; } return pos; } // ============================================================ // wordBoundaryRight // ============================================================ // // From position pos, skip word chars right, then skip non-word chars right. // Returns the position at the end of the word (or len). static int32_t wordBoundaryRight(const char *buf, int32_t len, int32_t pos) { if (pos >= len) { return len; } // Skip word characters while (pos < len && (isalnum((unsigned char)buf[pos]) || buf[pos] == '_')) { pos++; } // Skip non-word characters while (pos < len && !isalnum((unsigned char)buf[pos]) && buf[pos] != '_') { pos++; } return pos; } // ============================================================ // Mask helpers // ============================================================ static bool maskIsSlot(char ch) { return ch == '#' || ch == 'A' || ch == '*'; } static bool maskCharValid(char slot, char ch) { switch (slot) { case '#': return ch >= '0' && ch <= '9'; case 'A': return isalpha((unsigned char)ch); case '*': return ch >= 32 && ch < 127; default: return false; } } static int32_t maskFirstSlot(const char *mask) { for (int32_t i = 0; mask[i]; i++) { if (maskIsSlot(mask[i])) { return i; } } return 0; } static int32_t maskNextSlot(const char *mask, int32_t pos) { for (int32_t i = pos + 1; mask[i]; i++) { if (maskIsSlot(mask[i])) { return i; } } return (int32_t)strlen(mask); } static int32_t maskPrevSlot(const char *mask, int32_t pos) { for (int32_t i = pos - 1; i >= 0; i--) { if (maskIsSlot(mask[i])) { return i; } } return pos; } // Masked input key handling is entirely separate from the shared // text editor because the editing semantics are fundamentally different: // the buffer length is fixed (it's always maskLen), characters can // only be placed in slot positions, and backspace clears a slot rather // than removing a character. The cursor skips over literal characters // when moving left/right. Cut clears slots to '_' rather than removing // text. Paste fills consecutive slots, skipping non-matching clipboard // characters. static void maskedInputOnKey(WidgetT *w, int32_t key, int32_t mod) { TextInputDataT *ti = (TextInputDataT *)w->data; char *buf = ti->buf; const char *mask = ti->mask; int32_t *pCur = &ti->cursorPos; int32_t maskLen = ti->len; bool shift = (mod & KEY_MOD_SHIFT) != 0; (void)shift; // Ctrl+A -- select all if (key == 1) { ti->selStart = 0; ti->selEnd = maskLen; *pCur = maskLen; goto done; } // Ctrl+C -- copy formatted text if (key == 3) { if (ti->selStart >= 0 && ti->selEnd >= 0 && ti->selStart != ti->selEnd) { int32_t selLo = ti->selStart < ti->selEnd ? ti->selStart : ti->selEnd; int32_t selHi = ti->selStart < ti->selEnd ? ti->selEnd : ti->selStart; if (selLo < 0) { selLo = 0; } if (selHi > maskLen) { selHi = maskLen; } if (selHi > selLo) { clipboardCopy(buf + selLo, selHi - selLo); } } return; } // Ctrl+V -- paste valid chars into slots if (key == 22) { int32_t clipLen; const char *clip = clipboardGet(&clipLen); if (clipLen > 0) { if (ti->undoBuf) { textEditSaveUndo(buf, maskLen, *pCur, ti->undoBuf, &ti->undoLen, &ti->undoCursor, ti->bufSize); } int32_t slotPos = *pCur; bool changed = false; for (int32_t i = 0; i < clipLen && slotPos < maskLen; i++) { // Skip to next slot if not on one while (slotPos < maskLen && !maskIsSlot(mask[slotPos])) { slotPos++; } if (slotPos >= maskLen) { break; } // Skip non-matching clipboard chars if (maskCharValid(mask[slotPos], clip[i])) { buf[slotPos] = clip[i]; slotPos = maskNextSlot(mask, slotPos); changed = true; } } if (changed) { *pCur = slotPos <= maskLen ? slotPos : maskLen; ti->selStart = -1; ti->selEnd = -1; if (w->onChange) { w->onChange(w); } } } goto done; } // Ctrl+X -- copy and clear selected slots if (key == 24) { if (ti->selStart >= 0 && ti->selEnd >= 0 && ti->selStart != ti->selEnd) { int32_t selLo = ti->selStart < ti->selEnd ? ti->selStart : ti->selEnd; int32_t selHi = ti->selStart < ti->selEnd ? ti->selEnd : ti->selStart; if (selLo < 0) { selLo = 0; } if (selHi > maskLen) { selHi = maskLen; } clipboardCopy(buf + selLo, selHi - selLo); if (ti->undoBuf) { textEditSaveUndo(buf, maskLen, *pCur, ti->undoBuf, &ti->undoLen, &ti->undoCursor, ti->bufSize); } for (int32_t i = selLo; i < selHi; i++) { if (maskIsSlot(mask[i])) { buf[i] = '_'; } } *pCur = selLo; ti->selStart = -1; ti->selEnd = -1; if (w->onChange) { w->onChange(w); } } goto done; } // Ctrl+Z -- undo if (key == 26 && ti->undoBuf) { char tmpBuf[256]; int32_t tmpLen = maskLen + 1 < (int32_t)sizeof(tmpBuf) ? maskLen + 1 : (int32_t)sizeof(tmpBuf); int32_t tmpCursor = *pCur; memcpy(tmpBuf, buf, tmpLen); memcpy(buf, ti->undoBuf, maskLen + 1); *pCur = ti->undoCursor < maskLen ? ti->undoCursor : maskLen; memcpy(ti->undoBuf, tmpBuf, tmpLen); ti->undoCursor = tmpCursor; ti->selStart = -1; ti->selEnd = -1; if (w->onChange) { w->onChange(w); } goto done; } if (key >= 32 && key < 127) { // Printable character -- place at current slot if valid if (*pCur < maskLen && maskIsSlot(mask[*pCur]) && maskCharValid(mask[*pCur], (char)key)) { if (ti->undoBuf) { textEditSaveUndo(buf, maskLen, *pCur, ti->undoBuf, &ti->undoLen, &ti->undoCursor, ti->bufSize); } buf[*pCur] = (char)key; *pCur = maskNextSlot(mask, *pCur); ti->selStart = -1; ti->selEnd = -1; if (w->onChange) { w->onChange(w); } } } else if (key == 8) { // Backspace -- clear previous slot int32_t prev = maskPrevSlot(mask, *pCur); if (prev != *pCur) { if (ti->undoBuf) { textEditSaveUndo(buf, maskLen, *pCur, ti->undoBuf, &ti->undoLen, &ti->undoCursor, ti->bufSize); } buf[prev] = '_'; *pCur = prev; ti->selStart = -1; ti->selEnd = -1; if (w->onChange) { w->onChange(w); } } } else if (key == (0x53 | 0x100)) { // Delete -- clear current slot if (*pCur < maskLen && maskIsSlot(mask[*pCur])) { if (ti->undoBuf) { textEditSaveUndo(buf, maskLen, *pCur, ti->undoBuf, &ti->undoLen, &ti->undoCursor, ti->bufSize); } buf[*pCur] = '_'; ti->selStart = -1; ti->selEnd = -1; if (w->onChange) { w->onChange(w); } } } else if (key == (0x4B | 0x100)) { // Left arrow -- move to previous slot int32_t prev = maskPrevSlot(mask, *pCur); if (shift) { if (ti->selStart < 0) { ti->selStart = *pCur; ti->selEnd = *pCur; } *pCur = prev; ti->selEnd = *pCur; } else { ti->selStart = -1; ti->selEnd = -1; *pCur = prev; } } else if (key == (0x4D | 0x100)) { // Right arrow -- move to next slot int32_t next = maskNextSlot(mask, *pCur); if (shift) { if (ti->selStart < 0) { ti->selStart = *pCur; ti->selEnd = *pCur; } *pCur = next; ti->selEnd = *pCur; } else { ti->selStart = -1; ti->selEnd = -1; *pCur = next; } } else if (key == (0x47 | 0x100)) { // Home -- first slot if (shift) { if (ti->selStart < 0) { ti->selStart = *pCur; ti->selEnd = *pCur; } *pCur = maskFirstSlot(mask); ti->selEnd = *pCur; } else { ti->selStart = -1; ti->selEnd = -1; *pCur = maskFirstSlot(mask); } } else if (key == (0x4F | 0x100)) { // End -- past last slot int32_t last = maskLen; if (shift) { if (ti->selStart < 0) { ti->selStart = *pCur; ti->selEnd = *pCur; } *pCur = last; ti->selEnd = *pCur; } else { ti->selStart = -1; ti->selEnd = -1; *pCur = last; } } else { return; } done: // Adjust scroll { AppContextT *ctx = wgtGetContext(w); const BitmapFontT *font = &ctx->font; int32_t visibleChars = (w->w - TEXT_INPUT_PAD * 2) / font->charWidth; if (*pCur < ti->scrollOff) { ti->scrollOff = *pCur; } if (*pCur >= ti->scrollOff + visibleChars) { ti->scrollOff = *pCur - visibleChars + 1; } } wgtInvalidatePaint(w); } // ============================================================ // TextArea line helpers // ============================================================ static int32_t textAreaCountLines(const char *buf, int32_t len) { int32_t lines = 1; for (int32_t i = 0; i < len; i++) { if (buf[i] == '\n') { lines++; } } return lines; } // Cached line count -- sentinel value -1 means "dirty, recompute". // This avoids O(N) buffer scans on every paint frame. The cache is // invalidated (set to -1) by textAreaDirtyCache() after any buffer // mutation. The same pattern is used for max line length. static int32_t textAreaGetLineCount(WidgetT *w) { TextAreaDataT *ta = (TextAreaDataT *)w->data; if (ta->cachedLines < 0) { ta->cachedLines = textAreaCountLines(ta->buf, ta->len); } return ta->cachedLines; } static int32_t textAreaLineStart(const char *buf, int32_t len, int32_t row) { (void)len; int32_t off = 0; for (int32_t r = 0; r < row; r++) { while (off < len && buf[off] != '\n') { off++; } if (off < len) { off++; } } return off; } static int32_t textAreaLineLen(const char *buf, int32_t len, int32_t row) { int32_t start = textAreaLineStart(buf, len, row); int32_t end = start; while (end < len && buf[end] != '\n') { end++; } return end - start; } static int32_t textAreaMaxLineLen(const char *buf, int32_t len) { int32_t maxLen = 0; int32_t curLen = 0; for (int32_t i = 0; i < len; i++) { if (buf[i] == '\n') { if (curLen > maxLen) { maxLen = curLen; } curLen = 0; } else { curLen++; } } if (curLen > maxLen) { maxLen = curLen; } return maxLen; } static int32_t textAreaGetMaxLineLen(WidgetT *w) { TextAreaDataT *ta = (TextAreaDataT *)w->data; if (ta->cachedMaxLL < 0) { ta->cachedMaxLL = textAreaMaxLineLen(ta->buf, ta->len); } return ta->cachedMaxLL; } static inline void textAreaDirtyCache(WidgetT *w) { TextAreaDataT *ta = (TextAreaDataT *)w->data; ta->cachedLines = -1; ta->cachedMaxLL = -1; } static int32_t textAreaCursorToOff(const char *buf, int32_t len, int32_t row, int32_t col) { int32_t start = textAreaLineStart(buf, len, row); int32_t lineL = textAreaLineLen(buf, len, row); int32_t clampC = col < lineL ? col : lineL; return start + clampC; } static void textAreaOffToRowCol(const char *buf, int32_t off, int32_t *row, int32_t *col) { int32_t r = 0; int32_t c = 0; for (int32_t i = 0; i < off; i++) { if (buf[i] == '\n') { r++; c = 0; } else { c++; } } *row = r; *col = c; } static void textAreaEnsureVisible(WidgetT *w, int32_t visRows, int32_t visCols) { TextAreaDataT *ta = (TextAreaDataT *)w->data; int32_t row = ta->cursorRow; int32_t col = ta->cursorCol; if (row < ta->scrollRow) { ta->scrollRow = row; } if (row >= ta->scrollRow + visRows) { ta->scrollRow = row - visRows + 1; } if (col < ta->scrollCol) { ta->scrollCol = col; } if (col >= ta->scrollCol + visCols) { ta->scrollCol = col - visCols + 1; } } // ============================================================ // widgetTextAreaCalcMinSize // ============================================================ void widgetTextAreaCalcMinSize(WidgetT *w, const BitmapFontT *font) { w->calcMinW = font->charWidth * TEXTAREA_MIN_COLS + TEXTAREA_PAD * 2 + TEXTAREA_BORDER * 2 + TEXTAREA_SB_W; w->calcMinH = font->charHeight * TEXTAREA_MIN_ROWS + TEXTAREA_BORDER * 2; } // ============================================================ // widgetTextAreaDestroy // ============================================================ void widgetTextAreaDestroy(WidgetT *w) { TextAreaDataT *ta = (TextAreaDataT *)w->data; if (ta) { free(ta->buf); free(ta->undoBuf); free(ta); w->data = NULL; } } // ============================================================ // widgetTextAreaGetText // ============================================================ const char *widgetTextAreaGetText(const WidgetT *w) { const TextAreaDataT *ta = (const TextAreaDataT *)w->data; return ta->buf ? ta->buf : ""; } // ============================================================ // widgetTextAreaOnKey // ============================================================ // TextArea key handling is inline (not using widgetTextEditOnKey) // because multi-line editing has fundamentally different cursor // semantics: row/col instead of linear offset, desiredCol for // vertical movement (so moving down from a long line to a short // line remembers the original column), and Enter inserts newlines. // // The SEL_BEGIN/SEL_END/HAS_SEL macros factor out the repetitive // selection-start/selection-extend pattern: SEL_BEGIN initializes // the anchor at the current offset if Shift is held and no selection // exists yet. SEL_END updates the selection cursor to the new // position (or clears selection if Shift isn't held). This keeps // the per-key handler code manageable despite the large number of // key combinations. // // textAreaEnsureVisible() is called after every cursor movement to // auto-scroll the viewport. It adjusts scrollRow/scrollCol so the // cursor is within the visible range. void widgetTextAreaOnKey(WidgetT *w, int32_t key, int32_t mod) { TextAreaDataT *ta = (TextAreaDataT *)w->data; if (!ta->buf) { return; } clearOtherSelections(w); char *buf = ta->buf; int32_t bufSize = ta->bufSize; int32_t *pLen = &ta->len; int32_t *pRow = &ta->cursorRow; int32_t *pCol = &ta->cursorCol; int32_t *pSA = &ta->selAnchor; int32_t *pSC = &ta->selCursor; bool shift = (mod & KEY_MOD_SHIFT) != 0; AppContextT *ctx = wgtGetContext(w); const BitmapFontT *font = &ctx->font; int32_t innerW = w->w - TEXTAREA_BORDER * 2 - TEXTAREA_PAD * 2 - TEXTAREA_SB_W; int32_t visCols = innerW / font->charWidth; int32_t maxLL = textAreaGetMaxLineLen(w); bool needHSb = (maxLL > visCols); int32_t innerH = w->h - TEXTAREA_BORDER * 2 - (needHSb ? TEXTAREA_SB_W : 0); int32_t visRows = innerH / font->charHeight; if (visRows < 1) { visRows = 1; } if (visCols < 1) { visCols = 1; } int32_t totalLines = textAreaGetLineCount(w); // Helper macros for cursor offset #define CUR_OFF() textAreaCursorToOff(buf, *pLen, *pRow, *pCol) // Start/extend selection #define SEL_BEGIN() do { \ if (shift && *pSA < 0) { *pSA = CUR_OFF(); *pSC = *pSA; } \ } while (0) #define SEL_END() do { \ if (shift) { *pSC = CUR_OFF(); } \ else { *pSA = -1; *pSC = -1; } \ } while (0) #define HAS_SEL() (*pSA >= 0 && *pSC >= 0 && *pSA != *pSC) #define SEL_LO() (*pSA < *pSC ? *pSA : *pSC) #define SEL_HI() (*pSA < *pSC ? *pSC : *pSA) // Clamp selection to buffer bounds if (HAS_SEL()) { if (*pSA > *pLen) { *pSA = *pLen; } if (*pSC > *pLen) { *pSC = *pLen; } } // Ctrl+A -- select all if (key == 1) { *pSA = 0; *pSC = *pLen; textAreaOffToRowCol(buf, *pLen, pRow, pCol); ta->desiredCol = *pCol; textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Ctrl+C -- copy if (key == 3) { if (HAS_SEL()) { clipboardCopy(buf + SEL_LO(), SEL_HI() - SEL_LO()); } return; } // Read-only: allow select-all, copy, and navigation but block editing if (w->readOnly) { goto navigation; } // Ctrl+V -- paste if (key == 22) { int32_t clipLen = 0; const char *clip = clipboardGet(&clipLen); if (clipLen > 0) { textEditSaveUndo(buf, *pLen, CUR_OFF(), ta->undoBuf, &ta->undoLen, &ta->undoCursor, bufSize); if (HAS_SEL()) { int32_t lo = SEL_LO(); int32_t hi = SEL_HI(); memmove(buf + lo, buf + hi, *pLen - hi + 1); *pLen -= (hi - lo); textAreaOffToRowCol(buf, lo, pRow, pCol); *pSA = -1; *pSC = -1; } int32_t off = CUR_OFF(); int32_t canFit = bufSize - 1 - *pLen; int32_t paste = clipLen < canFit ? clipLen : canFit; if (paste > 0) { memmove(buf + off + paste, buf + off, *pLen - off + 1); memcpy(buf + off, clip, paste); *pLen += paste; textAreaOffToRowCol(buf, off + paste, pRow, pCol); ta->desiredCol = *pCol; } if (w->onChange) { w->onChange(w); } textAreaDirtyCache(w); } textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Ctrl+X -- cut if (key == 24) { if (HAS_SEL()) { clipboardCopy(buf + SEL_LO(), SEL_HI() - SEL_LO()); textEditSaveUndo(buf, *pLen, CUR_OFF(), ta->undoBuf, &ta->undoLen, &ta->undoCursor, bufSize); int32_t lo = SEL_LO(); int32_t hi = SEL_HI(); memmove(buf + lo, buf + hi, *pLen - hi + 1); *pLen -= (hi - lo); textAreaOffToRowCol(buf, lo, pRow, pCol); *pSA = -1; *pSC = -1; ta->desiredCol = *pCol; if (w->onChange) { w->onChange(w); } textAreaDirtyCache(w); } textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Ctrl+Z -- undo if (key == 26) { if (ta->undoBuf && ta->undoLen >= 0) { // Swap current and undo char tmpBuf[CLIPBOARD_MAX]; int32_t tmpLen = *pLen; int32_t tmpCursor = CUR_OFF(); int32_t copyLen = tmpLen < (int32_t)sizeof(tmpBuf) - 1 ? tmpLen : (int32_t)sizeof(tmpBuf) - 1; memcpy(tmpBuf, buf, copyLen); tmpBuf[copyLen] = '\0'; int32_t restLen = ta->undoLen < bufSize - 1 ? ta->undoLen : bufSize - 1; memcpy(buf, ta->undoBuf, restLen); buf[restLen] = '\0'; *pLen = restLen; // Save current as new undo int32_t saveLen = copyLen < bufSize - 1 ? copyLen : bufSize - 1; memcpy(ta->undoBuf, tmpBuf, saveLen); ta->undoBuf[saveLen] = '\0'; ta->undoLen = saveLen; ta->undoCursor = tmpCursor; // Restore cursor int32_t restoreOff = ta->undoCursor < *pLen ? ta->undoCursor : *pLen; ta->undoCursor = tmpCursor; textAreaOffToRowCol(buf, restoreOff, pRow, pCol); ta->desiredCol = *pCol; *pSA = -1; *pSC = -1; if (w->onChange) { w->onChange(w); } textAreaDirtyCache(w); } textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Enter -- insert newline if (key == 0x0D) { if (*pLen < bufSize - 1) { textEditSaveUndo(buf, *pLen, CUR_OFF(), ta->undoBuf, &ta->undoLen, &ta->undoCursor, bufSize); if (HAS_SEL()) { int32_t lo = SEL_LO(); int32_t hi = SEL_HI(); memmove(buf + lo, buf + hi, *pLen - hi + 1); *pLen -= (hi - lo); textAreaOffToRowCol(buf, lo, pRow, pCol); *pSA = -1; *pSC = -1; } int32_t off = CUR_OFF(); if (*pLen < bufSize - 1) { memmove(buf + off + 1, buf + off, *pLen - off + 1); buf[off] = '\n'; (*pLen)++; (*pRow)++; *pCol = 0; ta->desiredCol = 0; } if (w->onChange) { w->onChange(w); } textAreaDirtyCache(w); } textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Backspace if (key == 8) { if (HAS_SEL()) { textEditSaveUndo(buf, *pLen, CUR_OFF(), ta->undoBuf, &ta->undoLen, &ta->undoCursor, bufSize); int32_t lo = SEL_LO(); int32_t hi = SEL_HI(); memmove(buf + lo, buf + hi, *pLen - hi + 1); *pLen -= (hi - lo); textAreaOffToRowCol(buf, lo, pRow, pCol); *pSA = -1; *pSC = -1; ta->desiredCol = *pCol; textAreaDirtyCache(w); if (w->onChange) { w->onChange(w); } } else { int32_t off = CUR_OFF(); if (off > 0) { textEditSaveUndo(buf, *pLen, off, ta->undoBuf, &ta->undoLen, &ta->undoCursor, bufSize); memmove(buf + off - 1, buf + off, *pLen - off + 1); (*pLen)--; textAreaOffToRowCol(buf, off - 1, pRow, pCol); ta->desiredCol = *pCol; textAreaDirtyCache(w); if (w->onChange) { w->onChange(w); } } } textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Delete if (key == (0x53 | 0x100)) { if (HAS_SEL()) { textEditSaveUndo(buf, *pLen, CUR_OFF(), ta->undoBuf, &ta->undoLen, &ta->undoCursor, bufSize); int32_t lo = SEL_LO(); int32_t hi = SEL_HI(); memmove(buf + lo, buf + hi, *pLen - hi + 1); *pLen -= (hi - lo); textAreaOffToRowCol(buf, lo, pRow, pCol); *pSA = -1; *pSC = -1; ta->desiredCol = *pCol; textAreaDirtyCache(w); if (w->onChange) { w->onChange(w); } } else { int32_t off = CUR_OFF(); if (off < *pLen) { textEditSaveUndo(buf, *pLen, off, ta->undoBuf, &ta->undoLen, &ta->undoCursor, bufSize); memmove(buf + off, buf + off + 1, *pLen - off); (*pLen)--; textAreaDirtyCache(w); if (w->onChange) { w->onChange(w); } } } textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } navigation: // Left arrow if (key == (0x4B | 0x100)) { SEL_BEGIN(); int32_t off = CUR_OFF(); if (off > 0) { textAreaOffToRowCol(buf, off - 1, pRow, pCol); } ta->desiredCol = *pCol; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Right arrow if (key == (0x4D | 0x100)) { SEL_BEGIN(); int32_t off = CUR_OFF(); if (off < *pLen) { textAreaOffToRowCol(buf, off + 1, pRow, pCol); } ta->desiredCol = *pCol; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Ctrl+Left -- word left if (key == (0x73 | 0x100)) { SEL_BEGIN(); int32_t off = CUR_OFF(); int32_t newOff = wordBoundaryLeft(buf, off); textAreaOffToRowCol(buf, newOff, pRow, pCol); ta->desiredCol = *pCol; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Ctrl+Right -- word right if (key == (0x74 | 0x100)) { SEL_BEGIN(); int32_t off = CUR_OFF(); int32_t newOff = wordBoundaryRight(buf, *pLen, off); textAreaOffToRowCol(buf, newOff, pRow, pCol); ta->desiredCol = *pCol; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Up arrow if (key == (0x48 | 0x100)) { SEL_BEGIN(); if (*pRow > 0) { (*pRow)--; int32_t lineL = textAreaLineLen(buf, *pLen, *pRow); *pCol = ta->desiredCol < lineL ? ta->desiredCol : lineL; } SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Down arrow if (key == (0x50 | 0x100)) { SEL_BEGIN(); if (*pRow < totalLines - 1) { (*pRow)++; int32_t lineL = textAreaLineLen(buf, *pLen, *pRow); *pCol = ta->desiredCol < lineL ? ta->desiredCol : lineL; } SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Home if (key == (0x47 | 0x100)) { SEL_BEGIN(); *pCol = 0; ta->desiredCol = 0; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // End if (key == (0x4F | 0x100)) { SEL_BEGIN(); *pCol = textAreaLineLen(buf, *pLen, *pRow); ta->desiredCol = *pCol; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Page Up if (key == (0x49 | 0x100)) { SEL_BEGIN(); *pRow -= visRows; if (*pRow < 0) { *pRow = 0; } int32_t lineL = textAreaLineLen(buf, *pLen, *pRow); *pCol = ta->desiredCol < lineL ? ta->desiredCol : lineL; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Page Down if (key == (0x51 | 0x100)) { SEL_BEGIN(); *pRow += visRows; if (*pRow >= totalLines) { *pRow = totalLines - 1; } int32_t lineL = textAreaLineLen(buf, *pLen, *pRow); *pCol = ta->desiredCol < lineL ? ta->desiredCol : lineL; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Ctrl+Home (scancode 0x77) if (key == (0x77 | 0x100)) { SEL_BEGIN(); *pRow = 0; *pCol = 0; ta->desiredCol = 0; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Ctrl+End (scancode 0x75) if (key == (0x75 | 0x100)) { SEL_BEGIN(); textAreaOffToRowCol(buf, *pLen, pRow, pCol); ta->desiredCol = *pCol; SEL_END(); textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } // Printable character (blocked in read-only mode) if (key >= 32 && key < 127 && !w->readOnly) { if (*pLen < bufSize - 1) { textEditSaveUndo(buf, *pLen, CUR_OFF(), ta->undoBuf, &ta->undoLen, &ta->undoCursor, bufSize); if (HAS_SEL()) { int32_t lo = SEL_LO(); int32_t hi = SEL_HI(); memmove(buf + lo, buf + hi, *pLen - hi + 1); *pLen -= (hi - lo); textAreaOffToRowCol(buf, lo, pRow, pCol); *pSA = -1; *pSC = -1; } int32_t off = CUR_OFF(); if (*pLen < bufSize - 1) { memmove(buf + off + 1, buf + off, *pLen - off + 1); buf[off] = (char)key; (*pLen)++; (*pCol)++; ta->desiredCol = *pCol; } textAreaDirtyCache(w); if (w->onChange) { w->onChange(w); } } textAreaEnsureVisible(w, visRows, visCols); wgtInvalidatePaint(w); return; } #undef CUR_OFF #undef SEL_BEGIN #undef SEL_END #undef HAS_SEL #undef SEL_LO #undef SEL_HI } // ============================================================ // widgetTextAreaOnMouse // ============================================================ // Mouse handling: scrollbar clicks (both V and H), then content area // clicks. Content clicks convert pixel coordinates to row/col using // font metrics and scroll offset. Multi-click: double-click selects // word, triple-click selects entire line. Single click starts a // drag-select (sets sDragWidget which the event loop monitors // on mouse-move to extend the selection). The drag-select global // is cleared on double/triple click since the selection is already // complete. void widgetTextAreaOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy) { TextAreaDataT *ta = (TextAreaDataT *)w->data; w->focused = true; clearOtherSelections(w); AppContextT *ctx = (AppContextT *)root->userData; const BitmapFontT *font = &ctx->font; int32_t innerX = w->x + TEXTAREA_BORDER + TEXTAREA_PAD; int32_t innerY = w->y + TEXTAREA_BORDER; int32_t innerW = w->w - TEXTAREA_BORDER * 2 - TEXTAREA_PAD * 2 - TEXTAREA_SB_W; int32_t visCols = innerW / font->charWidth; int32_t maxLL = textAreaGetMaxLineLen(w); bool needHSb = (maxLL > visCols); int32_t innerH = w->h - TEXTAREA_BORDER * 2 - (needHSb ? TEXTAREA_SB_W : 0); int32_t visRows = innerH / font->charHeight; if (visRows < 1) { visRows = 1; } if (visCols < 1) { visCols = 1; } int32_t totalLines = textAreaCountLines(ta->buf, ta->len); int32_t maxScroll = totalLines - visRows; if (maxScroll < 0) { maxScroll = 0; } ta->scrollRow = clampInt(ta->scrollRow, 0, maxScroll); int32_t maxHScroll = maxLL - visCols; if (maxHScroll < 0) { maxHScroll = 0; } ta->scrollCol = clampInt(ta->scrollCol, 0, maxHScroll); // Check horizontal scrollbar click if (needHSb) { int32_t hsbY = w->y + w->h - TEXTAREA_BORDER - TEXTAREA_SB_W; if (vy >= hsbY && vx < w->x + w->w - TEXTAREA_BORDER - TEXTAREA_SB_W) { int32_t hsbX = w->x + TEXTAREA_BORDER; int32_t hsbW = w->w - TEXTAREA_BORDER * 2 - TEXTAREA_SB_W; int32_t relX = vx - hsbX; int32_t trackLen = hsbW - TEXTAREA_SB_W * 2; if (relX < TEXTAREA_SB_W) { // Left arrow if (ta->scrollCol > 0) { ta->scrollCol--; } } else if (relX >= hsbW - TEXTAREA_SB_W) { // Right arrow if (ta->scrollCol < maxHScroll) { ta->scrollCol++; } } else if (trackLen > 0) { int32_t thumbPos; int32_t thumbSize; widgetScrollbarThumb(trackLen, maxLL, visCols, ta->scrollCol, &thumbPos, &thumbSize); int32_t trackRelX = relX - TEXTAREA_SB_W; if (trackRelX < thumbPos) { ta->scrollCol -= visCols; ta->scrollCol = clampInt(ta->scrollCol, 0, maxHScroll); } else if (trackRelX >= thumbPos + thumbSize) { ta->scrollCol += visCols; ta->scrollCol = clampInt(ta->scrollCol, 0, maxHScroll); } else { sDragWidget = w; ta->sbDragOrient = 1; ta->sbDragging = true; ta->sbDragOff = trackRelX - thumbPos; return; } } return; } } // Check vertical scrollbar click int32_t sbX = w->x + w->w - TEXTAREA_BORDER - TEXTAREA_SB_W; if (vx >= sbX) { int32_t sbY = w->y + TEXTAREA_BORDER; int32_t sbH = innerH; int32_t relY = vy - sbY; int32_t trackLen = sbH - TEXTAREA_SB_W * 2; if (relY < TEXTAREA_SB_W) { // Up arrow if (ta->scrollRow > 0) { ta->scrollRow--; } } else if (relY >= sbH - TEXTAREA_SB_W) { // Down arrow if (ta->scrollRow < maxScroll) { ta->scrollRow++; } } else if (trackLen > 0) { int32_t thumbPos; int32_t thumbSize; widgetScrollbarThumb(trackLen, totalLines, visRows, ta->scrollRow, &thumbPos, &thumbSize); int32_t trackRelY = relY - TEXTAREA_SB_W; if (trackRelY < thumbPos) { ta->scrollRow -= visRows; ta->scrollRow = clampInt(ta->scrollRow, 0, maxScroll); } else if (trackRelY >= thumbPos + thumbSize) { ta->scrollRow += visRows; ta->scrollRow = clampInt(ta->scrollRow, 0, maxScroll); } else { sDragWidget = w; ta->sbDragOrient = 0; ta->sbDragging = true; ta->sbDragOff = trackRelY - thumbPos; return; } } return; } // Click on text area -- place cursor int32_t relX = vx - innerX; int32_t relY = vy - innerY; int32_t clickRow = ta->scrollRow + relY / font->charHeight; int32_t clickCol = ta->scrollCol + relX / font->charWidth; if (clickRow < 0) { clickRow = 0; } if (clickRow >= totalLines) { clickRow = totalLines - 1; } if (clickCol < 0) { clickCol = 0; } int32_t lineL = textAreaLineLen(ta->buf, ta->len, clickRow); if (clickCol > lineL) { clickCol = lineL; } int32_t clicks = multiClickDetect(vx, vy); if (clicks >= 3) { // Triple-click: select entire line int32_t lineStart = textAreaCursorToOff(ta->buf, ta->len, clickRow, 0); int32_t lineEnd = textAreaCursorToOff(ta->buf, ta->len, clickRow, lineL); ta->cursorRow = clickRow; ta->cursorCol = lineL; ta->desiredCol = lineL; ta->selAnchor = lineStart; ta->selCursor = lineEnd; sDragWidget = NULL; return; } if (clicks == 2 && ta->buf) { // Double-click: select word int32_t off = textAreaCursorToOff(ta->buf, ta->len, clickRow, clickCol); int32_t ws = wordStart(ta->buf, off); int32_t we = wordEnd(ta->buf, ta->len, off); int32_t weRow; int32_t weCol; textAreaOffToRowCol(ta->buf, we, &weRow, &weCol); ta->cursorRow = weRow; ta->cursorCol = weCol; ta->desiredCol = weCol; ta->selAnchor = ws; ta->selCursor = we; sDragWidget = NULL; return; } // Single click: place cursor + start drag-select ta->cursorRow = clickRow; ta->cursorCol = clickCol; ta->desiredCol = clickCol; int32_t anchorOff = textAreaCursorToOff(ta->buf, ta->len, clickRow, clickCol); ta->selAnchor = anchorOff; ta->selCursor = anchorOff; sDragWidget = w; ta->sbDragging = false; } // ============================================================ // widgetTextAreaPaint // ============================================================ // TextArea paint uses an optimized incremental line-offset approach: // instead of calling textAreaLineStart() for each visible row (which // would re-scan from the buffer start each time), we compute the // starting offset of the first visible line once, then advance it // line by line. This makes the paint cost O(visRows * maxLineLen) // rather than O(visRows * totalLines). // // Each line is drawn in up to 3 runs (before-selection, selection, // after-selection) to avoid overdraw. Selection highlighting that // extends past the end of a line (the newline itself is "selected") // is rendered as a highlight-colored rectFill past the text. // // Scrollbars are drawn inline (not using the shared scrollbar // functions) because TextArea scrolling is in rows/cols (logical // units) rather than pixels, so the thumb calculation parameters // differ. The dead corner between scrollbars is filled with // windowFace color. void widgetTextAreaPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) { TextAreaDataT *ta = (TextAreaDataT *)w->data; uint32_t fg = w->fgColor ? w->fgColor : colors->contentFg; uint32_t bg = w->bgColor ? w->bgColor : colors->contentBg; char *buf = ta->buf; int32_t len = ta->len; int32_t innerW = w->w - TEXTAREA_BORDER * 2 - TEXTAREA_PAD * 2 - TEXTAREA_SB_W; int32_t visCols = innerW / font->charWidth; int32_t maxLL = textAreaGetMaxLineLen(w); bool needHSb = (maxLL > visCols); int32_t innerH = w->h - TEXTAREA_BORDER * 2 - (needHSb ? TEXTAREA_SB_W : 0); int32_t visRows = innerH / font->charHeight; int32_t totalLines = textAreaGetLineCount(w); bool needVSb = (totalLines > visRows); // Sunken border BevelStyleT bevel = BEVEL_SUNKEN(colors, bg, 2); drawBevel(d, ops, w->x, w->y, w->w, w->h, &bevel); // Clamp vertical scroll int32_t maxScroll = totalLines - visRows; if (maxScroll < 0) { maxScroll = 0; } ta->scrollRow = clampInt(ta->scrollRow, 0, maxScroll); // Clamp horizontal scroll int32_t maxHScroll = maxLL - visCols; if (maxHScroll < 0) { maxHScroll = 0; } ta->scrollCol = clampInt(ta->scrollCol, 0, maxHScroll); // Selection range int32_t selLo = -1; int32_t selHi = -1; if (ta->selAnchor >= 0 && ta->selCursor >= 0 && ta->selAnchor != ta->selCursor) { selLo = ta->selAnchor < ta->selCursor ? ta->selAnchor : ta->selCursor; selHi = ta->selAnchor < ta->selCursor ? ta->selCursor : ta->selAnchor; } // Draw lines -- compute first visible line offset once, then advance incrementally int32_t textX = w->x + TEXTAREA_BORDER + TEXTAREA_PAD; int32_t textY = w->y + TEXTAREA_BORDER; int32_t lineOff = textAreaLineStart(buf, len, ta->scrollRow); for (int32_t i = 0; i < visRows; i++) { int32_t row = ta->scrollRow + i; if (row >= totalLines) { break; } // Compute line length by scanning from lineOff (not from buffer start) int32_t lineL = 0; while (lineOff + lineL < len && buf[lineOff + lineL] != '\n') { lineL++; } int32_t drawY = textY + i * font->charHeight; // Visible range within line int32_t scrollCol = ta->scrollCol; int32_t visStart = scrollCol; int32_t visEnd = scrollCol + visCols; int32_t textEnd = lineL; // chars in this line // Clamp visible range to actual line content for text drawing int32_t drawStart = visStart < textEnd ? visStart : textEnd; int32_t drawEnd = visEnd < textEnd ? visEnd : textEnd; // Determine selection intersection with this line int32_t lineSelLo = -1; int32_t lineSelHi = -1; if (selLo >= 0) { // Selection range in column-space for this line if (selLo < lineOff + lineL + 1 && selHi > lineOff) { lineSelLo = selLo - lineOff; lineSelHi = selHi - lineOff; if (lineSelLo < 0) { lineSelLo = 0; } // selHi can extend past line (newline selected) } } if (lineSelLo >= 0 && lineSelLo < lineSelHi) { // Clamp selection to visible columns for text runs int32_t vSelLo = lineSelLo < drawStart ? drawStart : lineSelLo; int32_t vSelHi = lineSelHi < drawEnd ? lineSelHi : drawEnd; if (vSelLo > vSelHi) { vSelLo = vSelHi; } // Before selection if (drawStart < vSelLo) { drawTextN(d, ops, font, textX + (drawStart - scrollCol) * font->charWidth, drawY, buf + lineOff + drawStart, vSelLo - drawStart, fg, bg, true); } // Selection (text portion) if (vSelLo < vSelHi) { drawTextN(d, ops, font, textX + (vSelLo - scrollCol) * font->charWidth, drawY, buf + lineOff + vSelLo, vSelHi - vSelLo, colors->menuHighlightFg, colors->menuHighlightBg, true); } // After selection if (vSelHi < drawEnd) { drawTextN(d, ops, font, textX + (vSelHi - scrollCol) * font->charWidth, drawY, buf + lineOff + vSelHi, drawEnd - vSelHi, fg, bg, true); } // Past end of text: fill selected area with highlight bg int32_t nlOff = lineOff + lineL; bool pastEolSelected = (nlOff >= selLo && nlOff < selHi); if (pastEolSelected && drawEnd < visEnd) { int32_t selPastStart = drawEnd < lineSelLo ? lineSelLo : drawEnd; int32_t selPastEnd = visEnd; if (selPastStart < visStart) { selPastStart = visStart; } if (selPastStart < selPastEnd) { rectFill(d, ops, textX + (selPastStart - scrollCol) * font->charWidth, drawY, (selPastEnd - selPastStart) * font->charWidth, font->charHeight, colors->menuHighlightBg); } } } else { // No selection on this line -- single run if (drawStart < drawEnd) { drawTextN(d, ops, font, textX + (drawStart - scrollCol) * font->charWidth, drawY, buf + lineOff + drawStart, drawEnd - drawStart, fg, bg, true); } } // Advance lineOff to the next line lineOff += lineL; if (lineOff < len && buf[lineOff] == '\n') { lineOff++; } } // Draw cursor (blinks at same rate as terminal cursor) if (w->focused && sCursorBlinkOn) { int32_t curDrawCol = ta->cursorCol - ta->scrollCol; int32_t curDrawRow = ta->cursorRow - ta->scrollRow; if (curDrawCol >= 0 && curDrawCol <= visCols && curDrawRow >= 0 && curDrawRow < visRows) { int32_t cursorX = textX + curDrawCol * font->charWidth; int32_t cursorY = textY + curDrawRow * font->charHeight; drawVLine(d, ops, cursorX, cursorY, font->charHeight, fg); } } BevelStyleT btnBevel = BEVEL_RAISED(colors, 1); // Draw vertical scrollbar if (needVSb) { int32_t sbX = w->x + w->w - TEXTAREA_BORDER - TEXTAREA_SB_W; int32_t sbY = w->y + TEXTAREA_BORDER; int32_t sbH = innerH; // Trough BevelStyleT troughBevel = BEVEL_TROUGH(colors); drawBevel(d, ops, sbX, sbY, TEXTAREA_SB_W, sbH, &troughBevel); // Up arrow button drawBevel(d, ops, sbX, sbY, TEXTAREA_SB_W, TEXTAREA_SB_W, &btnBevel); // Up arrow triangle { int32_t cx = sbX + TEXTAREA_SB_W / 2; int32_t cy = sbY + TEXTAREA_SB_W / 2; for (int32_t i = 0; i < 4; i++) { drawHLine(d, ops, cx - i, cy - 2 + i, 1 + i * 2, colors->contentFg); } } // Down arrow button int32_t downY = sbY + sbH - TEXTAREA_SB_W; drawBevel(d, ops, sbX, downY, TEXTAREA_SB_W, TEXTAREA_SB_W, &btnBevel); // Down arrow triangle { int32_t cx = sbX + TEXTAREA_SB_W / 2; int32_t cy = downY + TEXTAREA_SB_W / 2; for (int32_t i = 0; i < 4; i++) { drawHLine(d, ops, cx - i, cy + 2 - i, 1 + i * 2, colors->contentFg); } } // Thumb int32_t trackLen = sbH - TEXTAREA_SB_W * 2; if (trackLen > 0) { int32_t thumbPos; int32_t thumbSize; widgetScrollbarThumb(trackLen, totalLines, visRows, ta->scrollRow, &thumbPos, &thumbSize); drawBevel(d, ops, sbX, sbY + TEXTAREA_SB_W + thumbPos, TEXTAREA_SB_W, thumbSize, &btnBevel); } } // Draw horizontal scrollbar if (needHSb) { int32_t hsbX = w->x + TEXTAREA_BORDER; int32_t hsbY = w->y + w->h - TEXTAREA_BORDER - TEXTAREA_SB_W; int32_t hsbW = w->w - TEXTAREA_BORDER * 2 - TEXTAREA_SB_W; // Trough BevelStyleT troughBevel = BEVEL_TROUGH(colors); drawBevel(d, ops, hsbX, hsbY, hsbW, TEXTAREA_SB_W, &troughBevel); // Left arrow button drawBevel(d, ops, hsbX, hsbY, TEXTAREA_SB_W, TEXTAREA_SB_W, &btnBevel); // Left arrow triangle { int32_t cx = hsbX + TEXTAREA_SB_W / 2; int32_t cy = hsbY + TEXTAREA_SB_W / 2; for (int32_t i = 0; i < 4; i++) { drawVLine(d, ops, cx - 2 + i, cy - i, 1 + i * 2, colors->contentFg); } } // Right arrow button int32_t rightX = hsbX + hsbW - TEXTAREA_SB_W; drawBevel(d, ops, rightX, hsbY, TEXTAREA_SB_W, TEXTAREA_SB_W, &btnBevel); // Right arrow triangle { int32_t cx = rightX + TEXTAREA_SB_W / 2; int32_t cy = hsbY + TEXTAREA_SB_W / 2; for (int32_t i = 0; i < 4; i++) { drawVLine(d, ops, cx + 2 - i, cy - i, 1 + i * 2, colors->contentFg); } } // Thumb int32_t trackLen = hsbW - TEXTAREA_SB_W * 2; if (trackLen > 0) { int32_t thumbPos; int32_t thumbSize; widgetScrollbarThumb(trackLen, maxLL, visCols, ta->scrollCol, &thumbPos, &thumbSize); drawBevel(d, ops, hsbX + TEXTAREA_SB_W + thumbPos, hsbY, thumbSize, TEXTAREA_SB_W, &btnBevel); } // Dead corner between scrollbars if (needVSb) { int32_t cornerX = w->x + w->w - TEXTAREA_BORDER - TEXTAREA_SB_W; rectFill(d, ops, cornerX, hsbY, TEXTAREA_SB_W, TEXTAREA_SB_W, colors->windowFace); } } // Focus rect if (w->focused) { drawFocusRect(d, ops, w->x + 1, w->y + 1, w->w - 2, w->h - 2, fg); } } // ============================================================ // widgetTextAreaSetText // ============================================================ void widgetTextAreaSetText(WidgetT *w, const char *text) { TextAreaDataT *ta = (TextAreaDataT *)w->data; if (ta->buf) { strncpy(ta->buf, text, ta->bufSize - 1); ta->buf[ta->bufSize - 1] = '\0'; ta->len = (int32_t)strlen(ta->buf); ta->cursorRow = 0; ta->cursorCol = 0; ta->scrollRow = 0; ta->scrollCol = 0; ta->desiredCol = 0; ta->selAnchor = -1; ta->selCursor = -1; ta->cachedLines = -1; ta->cachedMaxLL = -1; } } // ============================================================ // widgetTextInputCalcMinSize // ============================================================ void widgetTextInputCalcMinSize(WidgetT *w, const BitmapFontT *font) { w->calcMinW = font->charWidth * 8 + TEXT_INPUT_PAD * 2; w->calcMinH = font->charHeight + TEXT_INPUT_PAD * 2; } // ============================================================ // widgetTextInputDestroy // ============================================================ void widgetTextInputDestroy(WidgetT *w) { TextInputDataT *ti = (TextInputDataT *)w->data; if (ti) { free(ti->buf); free(ti->undoBuf); free(ti); w->data = NULL; } } // ============================================================ // widgetTextInputGetText // ============================================================ const char *widgetTextInputGetText(const WidgetT *w) { const TextInputDataT *ti = (const TextInputDataT *)w->data; return ti->buf ? ti->buf : ""; } // ============================================================ // widgetTextInputOnKey // ============================================================ // TextInput key handling delegates to the shared widgetTextEditOnKey // engine, passing pointers to its state fields. Masked input mode // gets its own handler since the editing semantics are completely // different. Password mode blocks copy/cut at this level before // reaching the shared engine. void widgetTextInputOnKey(WidgetT *w, int32_t key, int32_t mod) { TextInputDataT *ti = (TextInputDataT *)w->data; if (!ti->buf) { return; } clearOtherSelections(w); if (ti->inputMode == InputMaskedE) { maskedInputOnKey(w, key, mod); return; } // Password mode: block copy (Ctrl+C) and cut (Ctrl+X) if (ti->inputMode == InputPasswordE) { if (key == 3 || key == 24) { return; } } widgetTextEditOnKey(w, key, mod, ti->buf, ti->bufSize, &ti->len, &ti->cursorPos, &ti->scrollOff, &ti->selStart, &ti->selEnd, ti->undoBuf, &ti->undoLen, &ti->undoCursor, w->w - TEXT_INPUT_PAD * 2); } // ============================================================ // widgetTextInputOnMouse // ============================================================ // Mouse handling for single-line input. Cursor position is computed // from pixel offset using the fixed-width font (relX / charWidth + // scrollOff). Multi-click: double-click selects word (using // wordStart/wordEnd), triple-click selects all. Single click starts // drag-select by setting both selStart and selEnd to the click // position and registering sDragWidget. void widgetTextInputOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy) { TextInputDataT *ti = (TextInputDataT *)w->data; w->focused = true; clearOtherSelections(w); AppContextT *ctx = (AppContextT *)root->userData; widgetTextEditMouseClick(w, vx, vy, w->x + TEXT_INPUT_PAD, &ctx->font, ti->buf, ti->len, ti->scrollOff, &ti->cursorPos, &ti->selStart, &ti->selEnd, true, true); } // ============================================================ // widgetTextInputPaint // ============================================================ // TextInput paint: sunken 2px bevel, then text with optional selection // highlighting, then cursor line. Text is drawn from a display buffer // that may be either the actual text or bullets (password mode, using // CP437 character 0xF9 which renders as a small centered dot). // // The 3-run approach (before/during/after selection) draws text in a // single pass without overdraw. The scroll offset ensures only the // visible portion of the text is drawn. The cursor is a 1px-wide // vertical line drawn at the character boundary. void widgetTextInputPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) { TextInputDataT *ti = (TextInputDataT *)w->data; uint32_t fg = w->enabled ? (w->fgColor ? w->fgColor : colors->contentFg) : colors->windowShadow; uint32_t bg = w->bgColor ? w->bgColor : colors->contentBg; // Sunken border BevelStyleT bevel; bevel.highlight = colors->windowShadow; bevel.shadow = colors->windowHighlight; bevel.face = bg; bevel.width = 2; drawBevel(d, ops, w->x, w->y, w->w, w->h, &bevel); // Draw text if (ti->buf) { int32_t textX = w->x + TEXT_INPUT_PAD; int32_t textY = w->y + (w->h - font->charHeight) / 2; int32_t maxChars = (w->w - TEXT_INPUT_PAD * 2) / font->charWidth; int32_t off = ti->scrollOff; int32_t len = ti->len - off; if (len > maxChars) { len = maxChars; } bool isPassword = (ti->inputMode == InputPasswordE); // Build display buffer (password masking) char dispBuf[256]; int32_t dispLen = len > 255 ? 255 : len; if (isPassword) { memset(dispBuf, '\xF9', dispLen); // CP437 bullet } else { memcpy(dispBuf, ti->buf + off, dispLen); } widgetTextEditPaintLine(d, ops, font, colors, textX, textY, dispBuf, dispLen, off, ti->cursorPos, ti->selStart, ti->selEnd, fg, bg, w->focused && w->enabled, w->x + TEXT_INPUT_PAD, w->x + w->w - TEXT_INPUT_PAD); } } // ============================================================ // widgetTextInputSetText // ============================================================ void widgetTextInputSetText(WidgetT *w, const char *text) { TextInputDataT *ti = (TextInputDataT *)w->data; if (ti->buf) { strncpy(ti->buf, text, ti->bufSize - 1); ti->buf[ti->bufSize - 1] = '\0'; ti->len = (int32_t)strlen(ti->buf); ti->cursorPos = ti->len; ti->scrollOff = 0; ti->selStart = -1; ti->selEnd = -1; } } // ============================================================ // DXE registration // ============================================================ static bool widgetTextInputClearSelection(WidgetT *w) { TextInputDataT *ti = (TextInputDataT *)w->data; if (ti->selStart >= 0 && ti->selEnd >= 0 && ti->selStart != ti->selEnd) { ti->selStart = -1; ti->selEnd = -1; return true; } return false; } static void widgetTextInputOnDragUpdate(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy) { (void)root; (void)vy; TextInputDataT *ti = (TextInputDataT *)w->data; AppContextT *ctx = wgtGetContext(w); int32_t maxChars = (w->w - TEXT_INPUT_PAD * 2) / ctx->font.charWidth; widgetTextEditDragUpdateLine(vx, w->x + TEXT_INPUT_PAD, maxChars, &ctx->font, ti->len, &ti->cursorPos, &ti->scrollOff, &ti->selEnd); } static const WidgetClassT sClassTextInput = { .flags = WCLASS_FOCUSABLE, .paint = widgetTextInputPaint, .paintOverlay = NULL, .calcMinSize = widgetTextInputCalcMinSize, .layout = NULL, .onMouse = widgetTextInputOnMouse, .onKey = widgetTextInputOnKey, .destroy = widgetTextInputDestroy, .getText = widgetTextInputGetText, .setText = widgetTextInputSetText, .clearSelection = widgetTextInputClearSelection, .onDragUpdate = widgetTextInputOnDragUpdate }; // ============================================================ // widgetTextAreaScrollDragUpdate // ============================================================ // Handle scrollbar thumb drag for TextArea vertical and horizontal scrollbars. // The TextArea always reserves space for the V scrollbar on the right. // The H scrollbar appears at the bottom only when the longest line exceeds // visible columns. static void widgetTextAreaScrollDragUpdate(WidgetT *w, int32_t orient, int32_t dragOff, int32_t mouseX, int32_t mouseY) { TextAreaDataT *ta = (TextAreaDataT *)w->data; AppContextT *ctx = wgtGetContext(w); const BitmapFontT *font = &ctx->font; int32_t innerW = w->w - TEXTAREA_BORDER * 2 - TEXTAREA_PAD * 2 - TEXTAREA_SB_W; int32_t visCols = innerW / font->charWidth; int32_t maxLL = textAreaGetMaxLineLen(w); bool needHSb = (maxLL > visCols); int32_t innerH = w->h - TEXTAREA_BORDER * 2 - (needHSb ? TEXTAREA_SB_W : 0); int32_t visRows = innerH / font->charHeight; if (visRows < 1) { visRows = 1; } if (visCols < 1) { visCols = 1; } if (orient == 0) { // Vertical scrollbar drag int32_t totalLines = textAreaGetLineCount(w); int32_t maxScroll = totalLines - visRows; if (maxScroll <= 0) { return; } int32_t trackLen = innerH - TEXTAREA_SB_W * 2; int32_t thumbPos; int32_t thumbSize; widgetScrollbarThumb(trackLen, totalLines, visRows, ta->scrollRow, &thumbPos, &thumbSize); int32_t sbY = w->y + TEXTAREA_BORDER; int32_t relMouse = mouseY - sbY - TEXTAREA_SB_W - dragOff; int32_t newScroll = (trackLen > thumbSize) ? (maxScroll * relMouse) / (trackLen - thumbSize) : 0; ta->scrollRow = clampInt(newScroll, 0, maxScroll); } else if (orient == 1) { // Horizontal scrollbar drag int32_t maxHScroll = maxLL - visCols; if (maxHScroll <= 0) { return; } int32_t hsbW = w->w - TEXTAREA_BORDER * 2 - TEXTAREA_SB_W; int32_t trackLen = hsbW - TEXTAREA_SB_W * 2; int32_t thumbPos; int32_t thumbSize; widgetScrollbarThumb(trackLen, maxLL, visCols, ta->scrollCol, &thumbPos, &thumbSize); int32_t sbX = w->x + TEXTAREA_BORDER; int32_t relMouse = mouseX - sbX - TEXTAREA_SB_W - dragOff; int32_t newScroll = (trackLen > thumbSize) ? (maxHScroll * relMouse) / (trackLen - thumbSize) : 0; ta->scrollCol = clampInt(newScroll, 0, maxHScroll); } } static void widgetTextAreaOnDragUpdate(WidgetT *w, WidgetT *root, int32_t x, int32_t y) { TextAreaDataT *ta = (TextAreaDataT *)w->data; if (ta->sbDragging) { widgetTextAreaScrollDragUpdate(w, ta->sbDragOrient, ta->sbDragOff, x, y); } else { widgetTextAreaDragSelect(w, root, x, y); } } static bool widgetTextAreaClearSelection(WidgetT *w) { TextAreaDataT *ta = (TextAreaDataT *)w->data; if (ta->selAnchor >= 0 && ta->selCursor >= 0 && ta->selAnchor != ta->selCursor) { ta->selAnchor = -1; ta->selCursor = -1; return true; } return false; } static void widgetTextAreaDragSelect(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy) { (void)root; TextAreaDataT *ta = (TextAreaDataT *)w->data; AppContextT *ctx = wgtGetContext(w); const BitmapFontT *font = &ctx->font; int32_t innerX = w->x + TEXTAREA_BORDER + TEXTAREA_PAD; int32_t innerY = w->y + TEXTAREA_BORDER; int32_t relX = vx - innerX; int32_t relY = vy - innerY; int32_t totalLines = textAreaGetLineCount(w); int32_t visRows = (w->h - TEXTAREA_BORDER * 2) / font->charHeight; // Auto-scroll when dragging past edges if (relY < 0 && ta->scrollRow > 0) { ta->scrollRow--; } else if (relY >= visRows * font->charHeight && ta->scrollRow < totalLines - visRows) { ta->scrollRow++; } int32_t clickRow = ta->scrollRow + relY / font->charHeight; int32_t clickCol = ta->scrollCol + relX / font->charWidth; if (clickRow < 0) { clickRow = 0; } if (clickRow >= totalLines) { clickRow = totalLines - 1; } if (clickCol < 0) { clickCol = 0; } int32_t lineL = textAreaLineLen(ta->buf, ta->len, clickRow); if (clickCol > lineL) { clickCol = lineL; } ta->cursorRow = clickRow; ta->cursorCol = clickCol; ta->desiredCol = clickCol; ta->selCursor = textAreaCursorToOff(ta->buf, ta->len, clickRow, clickCol); } static const WidgetClassT sClassTextArea = { .flags = WCLASS_FOCUSABLE | WCLASS_SCROLLABLE, .paint = widgetTextAreaPaint, .paintOverlay = NULL, .calcMinSize = widgetTextAreaCalcMinSize, .layout = NULL, .onMouse = widgetTextAreaOnMouse, .onKey = widgetTextAreaOnKey, .destroy = widgetTextAreaDestroy, .getText = widgetTextAreaGetText, .setText = widgetTextAreaSetText, .clearSelection = widgetTextAreaClearSelection, .onDragUpdate = widgetTextAreaOnDragUpdate }; // ============================================================ // Widget creation functions // ============================================================ WidgetT *wgtMaskedInput(WidgetT *parent, const char *mask) { if (!mask) { return NULL; } int32_t maskLen = (int32_t)strlen(mask); WidgetT *w = wgtTextInput(parent, maskLen); if (w) { TextInputDataT *ti = (TextInputDataT *)w->data; ti->inputMode = InputMaskedE; ti->mask = mask; // Pre-fill buffer: literals copied as-is, slots filled with '_' for (int32_t i = 0; i < maskLen; i++) { char ch = mask[i]; if (ch == '#' || ch == 'A' || ch == '*') { ti->buf[i] = '_'; } else { ti->buf[i] = ch; } } ti->buf[maskLen] = '\0'; ti->len = maskLen; // Position cursor at first editable slot ti->cursorPos = 0; for (int32_t i = 0; mask[i]; i++) { char ch = mask[i]; if (ch == '#' || ch == 'A' || ch == '*') { ti->cursorPos = i; break; } } } return w; } WidgetT *wgtPasswordInput(WidgetT *parent, int32_t maxLen) { WidgetT *w = wgtTextInput(parent, maxLen); if (w) { TextInputDataT *ti = (TextInputDataT *)w->data; ti->inputMode = InputPasswordE; } return w; } WidgetT *wgtTextArea(WidgetT *parent, int32_t maxLen) { WidgetT *w = widgetAlloc(parent, sTextAreaTypeId); if (w) { TextAreaDataT *ta = (TextAreaDataT *)calloc(1, sizeof(TextAreaDataT)); if (!ta) { return w; } w->data = ta; int32_t bufSize = maxLen > 0 ? maxLen + 1 : 256; ta->buf = (char *)malloc(bufSize); ta->undoBuf = (char *)malloc(bufSize); ta->bufSize = bufSize; if (!ta->buf || !ta->undoBuf) { free(ta->buf); free(ta->undoBuf); ta->buf = NULL; ta->undoBuf = NULL; } else { ta->buf[0] = '\0'; } ta->selAnchor = -1; ta->selCursor = -1; ta->desiredCol = 0; ta->cachedLines = -1; ta->cachedMaxLL = -1; w->weight = 100; } return w; } WidgetT *wgtTextInput(WidgetT *parent, int32_t maxLen) { WidgetT *w = widgetAlloc(parent, sTextInputTypeId); if (w) { TextInputDataT *ti = (TextInputDataT *)calloc(1, sizeof(TextInputDataT)); if (!ti) { return w; } w->data = ti; int32_t bufSize = maxLen > 0 ? maxLen + 1 : 256; ti->buf = (char *)malloc(bufSize); ti->bufSize = bufSize; if (ti->buf) { ti->buf[0] = '\0'; } ti->undoBuf = (char *)malloc(bufSize); ti->selStart = -1; ti->selEnd = -1; w->weight = 100; } return w; } // ============================================================ // DXE registration // ============================================================ static const struct { WidgetT *(*create)(WidgetT *parent, int32_t maxLen); WidgetT *(*password)(WidgetT *parent, int32_t maxLen); WidgetT *(*masked)(WidgetT *parent, const char *mask); WidgetT *(*textArea)(WidgetT *parent, int32_t maxLen); } sApi = { .create = wgtTextInput, .password = wgtPasswordInput, .masked = wgtMaskedInput, .textArea = wgtTextArea }; void wgtRegister(void) { sTextInputTypeId = wgtRegisterClass(&sClassTextInput); sTextAreaTypeId = wgtRegisterClass(&sClassTextArea); wgtRegisterApi("textinput", &sApi); }