DVX_GUI/dvx/widgets/widgetInternal.h

507 lines
29 KiB
C

// widgetInternal.h — Shared internal header for widget implementation files
//
// This header is included only by widget implementation .c files (in the
// widgets/ directory), never by application code. It exposes the vtable
// system, shared module-level state, and internal helper functions that
// widget implementations need but the public API should not expose.
//
// The widget system is split across multiple .c files by concern:
// widgetCore.c — allocation, tree ops, focus management, shared helpers
// widgetLayout.c — measure + layout algorithms for box containers
// widgetEvent.c — mouse/keyboard dispatch, scrollbar management
// widgetOps.c — paint dispatch, overlay rendering
// widget*.c — per-type paint, event, and layout implementations
#ifndef WIDGET_INTERNAL_H
#define WIDGET_INTERNAL_H
#include "../dvxWidget.h"
#include "../dvxApp.h"
#include "../dvxDraw.h"
#include "../dvxWm.h"
#include "../dvxVideo.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// ============================================================
// Widget class vtable
// ============================================================
//
// Each widget type has a WidgetClassT entry in widgetClassTable[], indexed
// by WidgetTypeE. This is a C implementation of virtual dispatch: instead
// of a switch(type) in every operation, the code calls w->wclass->paint()
// etc. The vtable pointer is set once at widget creation and never changes.
//
// Flags encode static properties that the framework needs without calling
// into the vtable:
// FOCUSABLE — can receive keyboard focus (Tab navigation)
// BOX_CONTAINER — uses the generic VBox/HBox layout algorithm
// HORIZ_CONTAINER — lays out children horizontally (vs. default vertical)
// PAINTS_CHILDREN — the widget's paint function handles child rendering
// (e.g. TabControl only paints the active tab page)
// NO_HIT_RECURSE — hit testing stops at this widget and doesn't recurse
// into children (e.g. ListBox handles its own items)
//
// NULL function pointers are valid and mean "no-op" for that operation.
// paintOverlay is used by widgets that need to draw outside their bounds
// (dropdown popup lists), rendered in a separate pass after all widgets.
#define WCLASS_FOCUSABLE 0x0001
#define WCLASS_BOX_CONTAINER 0x0002
#define WCLASS_HORIZ_CONTAINER 0x0004
#define WCLASS_PAINTS_CHILDREN 0x0008
#define WCLASS_NO_HIT_RECURSE 0x0010
typedef struct WidgetClassT {
uint32_t flags;
void (*paint)(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void (*paintOverlay)(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void (*calcMinSize)(WidgetT *w, const BitmapFontT *font);
void (*layout)(WidgetT *w, const BitmapFontT *font);
void (*onMouse)(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void (*onKey)(WidgetT *w, int32_t key, int32_t mod);
void (*destroy)(WidgetT *w);
const char *(*getText)(const WidgetT *w);
void (*setText)(WidgetT *w, const char *text);
} WidgetClassT;
// Global vtable array — one entry per WidgetTypeE value. Defined in
// widgetCore.c. Must stay in sync with the WidgetTypeE enum order.
extern const WidgetClassT *widgetClassTable[];
// ============================================================
// Validation macros
// ============================================================
//
// Defensive type-checking at the top of public widget API functions.
// Since WidgetT is a tagged union, calling a ListBox function with a
// Button widget would access the wrong union member and corrupt state.
// These macros provide a consistent early-return guard pattern.
#define VALIDATE_WIDGET(w, wtype, retval) \
do { if (!(w) || (w)->type != (wtype)) { return (retval); } } while (0)
#define VALIDATE_WIDGET_VOID(w, wtype) \
do { if (!(w) || (w)->type != (wtype)) { return; } } while (0)
// ============================================================
// Constants
// ============================================================
// Modifier flags (BIOS INT 16h shift state bits). Duplicated from
// dvxTypes.h ACCEL_xxx constants because widget code references them
// frequently and the KEY_MOD_ prefix reads better in event handlers.
#define KEY_MOD_SHIFT 0x03
#define KEY_MOD_CTRL 0x04
#define KEY_MOD_ALT 0x08
// Layout and geometry constants. These define the visual metrics of
// each widget type and are tuned for readability at 640x480 with 8px
// wide fonts. Changing these affects every instance of the widget type.
#define DEFAULT_SPACING 4
#define DEFAULT_PADDING 4
#define SEPARATOR_THICKNESS 2
#define BUTTON_PAD_H 8
#define BUTTON_PAD_V 4
#define BUTTON_FOCUS_INSET 3 // focus rect inset from button edge
#define BUTTON_PRESS_OFFSET 1 // text/focus shift when button is pressed
#define CHECKBOX_BOX_SIZE 12
#define CHECKBOX_GAP 4
#define FRAME_BEVEL_BORDER 2
#define FRAME_FLAT_BORDER 1
#define TEXT_INPUT_PAD 3
#define DROPDOWN_BTN_WIDTH 16
#define DROPDOWN_MAX_VISIBLE 8
#define SLIDER_TRACK_H 4
#define SLIDER_THUMB_W 11
#define TAB_PAD_H 8
#define TAB_PAD_V 4
#define TAB_BORDER 2
#define LISTBOX_BORDER 2
#define LISTVIEW_BORDER 2
#define LISTVIEW_MIN_COL_W 20 // minimum column width during resize
#define TREE_INDENT 16
#define TREE_EXPAND_SIZE 9
#define TREE_ICON_GAP 4
#define TREE_BORDER 2
// WGT_SB_W is the widget-internal scrollbar width (slightly narrower than
// the window-level SCROLLBAR_WIDTH) to fit within widget content areas.
#define WGT_SB_W 14
#define TREE_MIN_ROWS 4
#define SB_MIN_THUMB 14
#define SPLITTER_BAR_W 5
#define SPLITTER_MIN_PANE 20
#define TOOLBAR_PAD 2 // padding inside toolbar/statusbar
#define TOOLBAR_GAP 2 // gap between toolbar/statusbar children
// ============================================================
// Inline helpers
// ============================================================
static inline int32_t clampInt(int32_t val, int32_t lo, int32_t hi) {
if (val < lo) { return lo; }
if (val > hi) { return hi; }
return val;
}
// Classic Windows 3.1 embossed (etched) text for disabled widgets.
// The illusion works by drawing the text twice: first offset +1,+1 in
// the highlight color (creating a light "shadow" behind the text), then
// at the original position in the shadow color. The result is text that
// appears chiseled into the surface — the universal visual indicator for
// "greyed out" in Motif and Windows 3.x era GUIs.
static inline void drawTextEmbossed(DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, int32_t x, int32_t y, const char *text, const ColorSchemeT *colors) {
drawText(d, ops, font, x + 1, y + 1, text, colors->windowHighlight, 0, false);
drawText(d, ops, font, x, y, text, colors->windowShadow, 0, false);
}
static inline void drawTextAccelEmbossed(DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, int32_t x, int32_t y, const char *text, const ColorSchemeT *colors) {
drawTextAccel(d, ops, font, x + 1, y + 1, text, colors->windowHighlight, 0, false);
drawTextAccel(d, ops, font, x, y, text, colors->windowShadow, 0, false);
}
// ============================================================
// Shared state (defined in widgetCore.c)
// ============================================================
//
// Module-level globals for tracking active interactive operations.
// Only one of these can be active at a time (e.g. you can't drag a
// slider and resize a listview column simultaneously). Using globals
// rather than per-widget state avoids bloating every widget with fields
// that are only relevant during a single drag operation. The tradeoff
// is that these are truly global — but since the GUI is single-threaded
// and only one mouse can exist, this is safe.
extern bool sCursorBlinkOn; // text cursor blink phase (toggled by wgtUpdateCursorBlink)
extern bool sDebugLayout;
extern WidgetT *sClosedPopup; // popup that was just closed (prevents immediate reopen)
extern WidgetT *sFocusedWidget; // currently focused widget across all windows
extern WidgetT *sKeyPressedBtn; // button being held via keyboard (Space/Enter)
extern WidgetT *sOpenPopup; // dropdown/combobox with open popup list
extern WidgetT *sPressedButton; // button/imagebutton being held via mouse
extern WidgetT *sDragSlider; // slider being dragged
extern WidgetT *sDrawingCanvas; // canvas receiving drag events
extern WidgetT *sDragTextSelect; // text widget with active mouse selection drag
extern int32_t sDragOffset; // mouse offset within thumb/handle at drag start
extern WidgetT *sResizeListView; // listview whose column is being resized
extern int32_t sResizeCol; // which column is being resized
extern int32_t sResizeStartX; // mouse X at start of column resize
extern int32_t sResizeOrigW; // original column width at start of resize
extern bool sResizeDragging; // true once mouse has moved from click point
extern WidgetT *sDragSplitter; // splitter being dragged
extern int32_t sDragSplitStart; // mouse position at start of splitter drag
extern WidgetT *sDragReorder; // listbox/treeview item being drag-reordered
extern WidgetT *sDragScrollbar; // widget whose scrollbar thumb is being dragged
extern int32_t sDragScrollbarOff; // mouse offset within thumb at drag start
extern int32_t sDragScrollbarOrient; // 0=vertical, 1=horizontal
// ============================================================
// Core functions (widgetCore.c)
// ============================================================
// Tree manipulation
void widgetAddChild(WidgetT *parent, WidgetT *child);
void widgetRemoveChild(WidgetT *parent, WidgetT *child);
void widgetDestroyChildren(WidgetT *w);
// Allocate a new widget, set its type and vtable, and add it as a child.
WidgetT *widgetAlloc(WidgetT *parent, WidgetTypeE type);
// Focus management — Tab/Shift-Tab navigation walks the tree in creation
// order, skipping non-focusable and hidden widgets.
void widgetClearFocus(WidgetT *root);
WidgetT *widgetFindNextFocusable(WidgetT *root, WidgetT *after);
WidgetT *widgetFindPrevFocusable(WidgetT *root, WidgetT *before);
// Alt+key accelerator lookup — searches the tree for a widget whose
// accelKey matches, used for keyboard navigation of labeled controls.
WidgetT *widgetFindByAccel(WidgetT *root, char key);
// Utility queries
int32_t widgetCountVisibleChildren(const WidgetT *w);
int32_t widgetFrameBorderWidth(const WidgetT *w);
bool widgetIsFocusable(WidgetTypeE type);
bool widgetIsBoxContainer(WidgetTypeE type);
bool widgetIsHorizContainer(WidgetTypeE type);
// Hit testing — find the deepest widget containing the point (x,y).
// Respects WCLASS_NO_HIT_RECURSE to stop at list/tree widgets.
WidgetT *widgetHitTest(WidgetT *w, int32_t x, int32_t y);
// Translate arrow/PgUp/PgDn/Home/End keys into list index changes.
// Shared by ListBox, ListView, Dropdown, and TreeView.
int32_t widgetNavigateIndex(int32_t key, int32_t current, int32_t count, int32_t pageSize);
// Compute popup position for a dropdown/combobox, clamped to screen bounds.
void widgetDropdownPopupRect(WidgetT *w, const BitmapFontT *font, int32_t contentH, int32_t *popX, int32_t *popY, int32_t *popW, int32_t *popH);
// Paint a generic popup item list (used by dropdown, combobox, and popup menus).
void widgetPaintPopupList(DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors, int32_t popX, int32_t popY, int32_t popW, int32_t popH, const char **items, int32_t itemCount, int32_t hoverIdx, int32_t scrollPos);
// Compute scrollbar thumb position and size for a given track length,
// content size, visible size, and scroll position. Used by both widget-
// internal scrollbars and the window-level scrollbar drawing code.
void widgetScrollbarThumb(int32_t trackLen, int32_t totalSize, int32_t visibleSize, int32_t scrollPos, int32_t *thumbPos, int32_t *thumbSize);
// ============================================================
// Shared scrollbar functions (widgetScrollbar.c)
// ============================================================
//
// Widget-internal scrollbar rendering and hit testing, shared across
// ListBox, ListView, TreeView, TextArea, ScrollPane, and AnsiTerm.
// These operate on abstract track coordinates (position + length) so
// the same code handles both horizontal and vertical scrollbars.
typedef enum {
ScrollHitNoneE,
ScrollHitArrowDecE,
ScrollHitArrowIncE,
ScrollHitPageDecE,
ScrollHitPageIncE,
ScrollHitThumbE
} ScrollHitE;
void widgetDrawScrollbarH(DisplayT *d, const BlitOpsT *ops, const ColorSchemeT *colors, int32_t sbX, int32_t sbY, int32_t sbW, int32_t totalSize, int32_t visibleSize, int32_t scrollPos);
void widgetDrawScrollbarV(DisplayT *d, const BlitOpsT *ops, const ColorSchemeT *colors, int32_t sbX, int32_t sbY, int32_t sbH, int32_t totalSize, int32_t visibleSize, int32_t scrollPos);
void widgetScrollbarDragUpdate(WidgetT *w, int32_t orient, int32_t dragOff, int32_t mouseX, int32_t mouseY);
ScrollHitE widgetScrollbarHitTest(int32_t sbLen, int32_t relPos, int32_t totalSize, int32_t visibleSize, int32_t scrollPos);
// ============================================================
// Layout functions (widgetLayout.c)
// ============================================================
//
// The layout algorithm is a two-pass flexbox-like system:
//
// calcMinSize (bottom-up): each leaf widget reports its minimum size
// (e.g. button = text width + padding). Containers sum their children's
// minimums plus spacing along the main axis, and take the maximum of
// children's minimums on the cross axis.
//
// layout (top-down): starting from the available space (window content
// area), each container distributes space to children. Children that fit
// at their minimum get their minimum. Remaining space is distributed
// proportionally by weight to flexible children. This happens recursively
// down the tree.
void widgetCalcMinSizeBox(WidgetT *w, const BitmapFontT *font);
void widgetCalcMinSizeTree(WidgetT *w, const BitmapFontT *font);
void widgetLayoutBox(WidgetT *w, const BitmapFontT *font);
void widgetLayoutChildren(WidgetT *w, const BitmapFontT *font);
// ============================================================
// Event functions (widgetEvent.c)
// ============================================================
//
// These are the WindowT callback implementations installed by wgtInitWindow().
// They bridge the window manager's callback interface to the widget tree's
// event dispatch. Each one locates the appropriate widget (via hit-test for
// mouse events, or the focused widget for keyboard events) and calls the
// widget's vtable handler.
// Sync window-level scrollbar ranges to match the widget tree's overflow.
void widgetManageScrollbars(WindowT *win, AppContextT *ctx);
void widgetOnKey(WindowT *win, int32_t key, int32_t mod);
void widgetOnMouse(WindowT *win, int32_t x, int32_t y, int32_t buttons);
void widgetOnPaint(WindowT *win, RectT *dirtyArea);
void widgetOnResize(WindowT *win, int32_t newW, int32_t newH);
void widgetOnScroll(WindowT *win, ScrollbarOrientE orient, int32_t value);
// ============================================================
// Paint/ops functions (widgetOps.c)
// ============================================================
void widgetPaintOne(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetPaintOverlays(WidgetT *root, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
// ============================================================
// Per-widget paint functions
// ============================================================
void widgetAnsiTermPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetButtonPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetImageButtonPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetCanvasPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetCheckboxPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetComboBoxPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetComboBoxPaintPopup(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetDropdownPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetDropdownPaintPopup(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetFramePaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetImagePaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetLabelPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetListBoxPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetListViewPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
bool widgetListViewColBorderHit(const WidgetT *w, int32_t vx, int32_t vy);
void widgetProgressBarPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetRadioPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetSeparatorPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetScrollPanePaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetSplitterPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetSliderPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetSpinnerPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetStatusBarPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetTabControlPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetTextAreaPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetTextInputPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetToolbarPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
void widgetTreeViewPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
// ============================================================
// Per-widget calcMinSize functions
// ============================================================
void widgetAnsiTermCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetButtonCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetImageButtonCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetCanvasCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetCheckboxCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetComboBoxCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetDropdownCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetImageCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetLabelCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetListBoxCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetListViewCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetProgressBarCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetRadioCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetSeparatorCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetScrollPaneCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetSplitterCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetSliderCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetSpinnerCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetSpacerCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetTabControlCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetTextAreaCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetTextInputCalcMinSize(WidgetT *w, const BitmapFontT *font);
void widgetTreeViewCalcMinSize(WidgetT *w, const BitmapFontT *font);
// ============================================================
// Per-widget layout functions (for special containers)
// ============================================================
void widgetScrollPaneLayout(WidgetT *w, const BitmapFontT *font);
void widgetSplitterLayout(WidgetT *w, const BitmapFontT *font);
void widgetTabControlLayout(WidgetT *w, const BitmapFontT *font);
void widgetTreeViewLayout(WidgetT *w, const BitmapFontT *font);
// ============================================================
// Per-widget getText/setText functions
// ============================================================
const char *widgetButtonGetText(const WidgetT *w);
void widgetButtonSetText(WidgetT *w, const char *text);
const char *widgetCheckboxGetText(const WidgetT *w);
void widgetCheckboxSetText(WidgetT *w, const char *text);
const char *widgetComboBoxGetText(const WidgetT *w);
void widgetComboBoxSetText(WidgetT *w, const char *text);
const char *widgetDropdownGetText(const WidgetT *w);
const char *widgetLabelGetText(const WidgetT *w);
void widgetLabelSetText(WidgetT *w, const char *text);
const char *widgetRadioGetText(const WidgetT *w);
void widgetRadioSetText(WidgetT *w, const char *text);
const char *widgetTextAreaGetText(const WidgetT *w);
void widgetTextAreaSetText(WidgetT *w, const char *text);
const char *widgetTextInputGetText(const WidgetT *w);
void widgetTextInputSetText(WidgetT *w, const char *text);
const char *widgetTreeItemGetText(const WidgetT *w);
void widgetTreeItemSetText(WidgetT *w, const char *text);
// ============================================================
// Per-widget destroy functions
// ============================================================
void widgetAnsiTermDestroy(WidgetT *w);
void widgetCanvasDestroy(WidgetT *w);
void widgetComboBoxDestroy(WidgetT *w);
void widgetImageButtonDestroy(WidgetT *w);
void widgetImageDestroy(WidgetT *w);
void widgetListBoxDestroy(WidgetT *w);
void widgetListViewDestroy(WidgetT *w);
void widgetTextAreaDestroy(WidgetT *w);
void widgetTextInputDestroy(WidgetT *w);
// ============================================================
// Per-widget mouse/key functions
// ============================================================
//
// Shared text editing helpers used by TextInput, TextArea, ComboBox, and
// Spinner. The widgetTextEditOnKey function implements the common subset
// of single-line text editing behavior (cursor movement, selection,
// copy/paste, undo) shared across all text-editing widgets, so changes
// to editing behavior only need to happen in one place.
// Clear text selections in all widgets except the given one (ensures only
// one text selection is active at a time across the entire GUI).
void clearOtherSelections(WidgetT *except);
void clipboardCopy(const char *text, int32_t len);
const char *clipboardGet(int32_t *outLen);
bool isWordChar(char c);
// Detect double/triple clicks based on timing and position proximity.
// Returns 1 for single, 2 for double, 3 for triple click.
int32_t multiClickDetect(int32_t vx, int32_t vy);
void widgetAnsiTermOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetAnsiTermOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetButtonOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetButtonOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetCanvasOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetCheckboxOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetCheckboxOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetComboBoxOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetComboBoxOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetDropdownOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetDropdownOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetImageButtonOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetImageButtonOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetImageOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetListBoxOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetListBoxOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetListViewOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetListViewOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetRadioOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetRadioOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetScrollPaneOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetScrollPaneOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetSplitterClampPos(WidgetT *w, int32_t *pos);
void widgetSplitterOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetSliderOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetSliderOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetSpinnerOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetSpinnerOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
const char *widgetSpinnerGetText(const WidgetT *w);
void widgetSpinnerSetText(WidgetT *w, const char *text);
void widgetTabControlOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetTabControlOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetTextAreaOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetTextAreaOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
void widgetTextDragUpdate(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
// Shared single-line text editing logic. Takes pointers to all the edit
// state fields so the same code works for TextInput, ComboBox, and Spinner
// (which store these fields in different union members). This avoids
// duplicating the full editing logic (cursor movement, word selection,
// clipboard, undo) three times.
void widgetTextEditDragUpdateLine(int32_t vx, int32_t leftEdge, int32_t maxChars, const BitmapFontT *font, int32_t len, int32_t *pCursorPos, int32_t *pScrollOff, int32_t *pSelEnd);
void widgetTextEditMouseClick(WidgetT *w, int32_t vx, int32_t vy, int32_t textLeftX, const BitmapFontT *font, const char *buf, int32_t len, int32_t scrollOff, int32_t *pCursorPos, int32_t *pSelStart, int32_t *pSelEnd, bool wordSelect, bool dragSelect);
void widgetTextEditOnKey(WidgetT *w, int32_t key, int32_t mod, char *buf, int32_t bufSize, int32_t *pLen, int32_t *pCursor, int32_t *pScrollOff, int32_t *pSelStart, int32_t *pSelEnd, char *undoBuf, int32_t *pUndoLen, int32_t *pUndoCursor);
void widgetTextEditPaintLine(DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors, int32_t textX, int32_t textY, const char *buf, int32_t visLen, int32_t scrollOff, int32_t cursorPos, int32_t selStart, int32_t selEnd, uint32_t fg, uint32_t bg, bool showCursor, int32_t cursorMinX, int32_t cursorMaxX);
void widgetTextInputOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetTextInputOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
int32_t wordEnd(const char *buf, int32_t len, int32_t pos);
int32_t wordStart(const char *buf, int32_t pos);
void widgetTreeViewOnKey(WidgetT *w, int32_t key, int32_t mod);
void widgetTreeViewOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy);
// Drag-reorder helpers — generic drag-and-drop reordering shared by
// ListBox, ListView, and TreeView. The update function tracks the mouse
// position and computes the drop target, the drop function commits the
// reorder by relinking the item in the data structure.
void widgetReorderUpdate(WidgetT *w, WidgetT *root, int32_t x, int32_t y);
void widgetReorderDrop(WidgetT *w);
// Iterate to the next visible item in a tree view (skipping collapsed
// subtrees). Used for keyboard navigation and rendering.
WidgetT *widgetTreeViewNextVisible(WidgetT *item, WidgetT *treeView);
#endif // WIDGET_INTERNAL_H