# The MIT License (MIT)
#
# Copyright (C) 2026 Scott Duensing
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

.section Widgets
.topic widget.listbox
.title ListBox
.toc 0 ListBox
.index ListBox
.index wgtListBox
.index wgtListBoxAddItem
.index wgtListBoxRemoveItem
.index wgtListBoxClear
.index wgtListBoxGetSelected
.index wgtListBoxSetSelected

.h2 ListBox

A scrollable list of text items. Supports single and multi-select modes (Shift-click for range, Ctrl-click to toggle), drag-to-reorder, mouse wheel scroll, and keyboard navigation (Up/Down/PgUp/PgDn/Home/End with type-ahead search on printable keys). Depends on the "listhelp" helper library (declared in listbox.dep).

Items can either be set from a caller-owned array (wgtListBoxSetItems) or managed internally via AddItem/RemoveItem/Clear. The two modes are mutually exclusive per widget.

Header: widgets/listBox.h

.h3 Creation

.code
WidgetT *lb = wgtListBox(parent);
wgtListBoxAddItem(lb, "Alpha");
wgtListBoxAddItem(lb, "Beta");
wgtListBoxAddItem(lb, "Gamma");
.endcode

.h3 API Functions

.index wgtListBoxSetMultiSelect
.index wgtListBoxIsItemSelected
.index wgtListBoxSetReorderable
.index wgtListBoxSelectAll
.index wgtListBoxClearSelection

.table
  Function                                                     Description
  --------                                                     -----------
  WidgetT *wgtListBox(parent)                                  Create a list box.
  void wgtListBoxSetItems(w, items, count)                     Set items from a caller-owned const char ** array.
  int32_t wgtListBoxGetSelected(w)                             Get the primary selected index (-1 if none).
  void wgtListBoxSetSelected(w, idx)                           Set the selected index. Scrolls the item into view.
  void wgtListBoxSetMultiSelect(w, multi)                      Enable or disable multi-select mode.
  bool wgtListBoxIsItemSelected(w, idx)                        Returns true if the item is selected (multi-select mode).
  void wgtListBoxSetItemSelected(w, idx, selected)             Select or deselect a specific item (multi-select mode).
  void wgtListBoxSelectAll(w)                                  Select every item (multi-select mode).
  void wgtListBoxClearSelection(w)                             Deselect all items.
  void wgtListBoxSetReorderable(w, r)                          Enable drag-to-reorder of items.
  void wgtListBoxAddItem(w, text)                              Append an owned item (strdup'd).
  void wgtListBoxRemoveItem(w, idx)                            Remove an owned item by index.
  void wgtListBoxClear(w)                                      Remove all owned items.
  const char *wgtListBoxGetItem(w, idx)                        Get the text of an item by index.
  int32_t wgtListBoxGetItemCount(w)                            Get the total number of items.
.endtable

.h3 API Struct (wgtRegisterApi "listbox")

.table
  Slot               Function
  ----               --------
  create             wgtListBox
  setItems           wgtListBoxSetItems
  getSelected        wgtListBoxGetSelected
  setSelected        wgtListBoxSetSelected
  setMultiSelect     wgtListBoxSetMultiSelect
  isItemSelected     wgtListBoxIsItemSelected
  setItemSelected    wgtListBoxSetItemSelected
  selectAll          wgtListBoxSelectAll
  clearSelection     wgtListBoxClearSelection
  setReorderable     wgtListBoxSetReorderable
  addItem            wgtListBoxAddItem
  removeItem         wgtListBoxRemoveItem
  clear              wgtListBoxClear
  getItem            wgtListBoxGetItem
  getItemCount       wgtListBoxGetItemCount
.endtable

.h3 Properties (BASIC Interface)

.table
  Property     Type       Access       Description
  --------     ----       ------       -----------
  ListIndex    Integer    Read/Write   Index of the currently selected item.
.endtable

.h3 Methods (BASIC Interface)

.table
  Method                            Description
  ------                            -----------
  AddItem text$                     Append an item.
  Clear                             Remove all items.
  ClearSelection                    Deselect all items.
  IsItemSelected(index%)            Returns True if the item is selected (multi-select mode).
  List(index%)                      Return the text of the item at the given index.
  ListCount()                       Return the total number of items.
  RemoveItem index%                 Remove the item at the given index.
  SelectAll                         Select all items.
  SetItemSelected index%, sel       Select or deselect a specific item.
  SetItems spec$                    Bulk-load items from a pipe-delimited string (e.g. "Red|Green|Blue"); replaces existing items.
  SetMultiSelect multi              Enable or disable multi-select mode.
  SetReorderable r                  Enable or disable drag-to-reorder.
.endtable

.h3 Events

.table
  Callback     Description
  --------     -----------
  onClick      Fires when an item is clicked.
  onDblClick   Fires when an item is double-clicked.
  onChange     Fires when the selection changes.
.endtable

.h3 Default Event

"Click" (VB basName: ListBox).
