# 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.dropdown
.title Dropdown
.toc 0 Dropdown
.index Dropdown
.index wgtDropdown
.index wgtDropdownSetItems
.index wgtDropdownAddItem
.index wgtDropdownRemoveItem
.index wgtDropdownClear
.index wgtDropdownGetSelected
.index wgtDropdownSetSelected

.h2 Dropdown

A drop-down list that displays a single selected item and expands to a popup list when clicked. Unlike ComboBox, the user cannot type free text -- only list items can be selected. Keyboard navigation: Down/Up to move, Enter to select, type-ahead search on printable keys. The popup overlay is rendered above all other widgets via the widget paint overlay mechanism.

Depends on the "listhelp" helper library (declared in dropdown.dep).

Header: widgets/dropdown.h

.h3 Creation

.code
WidgetT *dd = wgtDropdown(parent);
wgtDropdownAddItem(dd, "Red");
wgtDropdownAddItem(dd, "Green");
wgtDropdownAddItem(dd, "Blue");
.endcode

.h3 API Functions

.table
  Function                                            Description
  --------                                            -----------
  WidgetT *wgtDropdown(parent)                        Create a dropdown list.
  void wgtDropdownSetItems(w, items, count)           Set the list from a caller-owned const char ** array.
  int32_t wgtDropdownGetSelected(w)                   Get the index of the selected item (-1 if none).
  void wgtDropdownSetSelected(w, idx)                 Set the selected item by index.
  void wgtDropdownAddItem(w, text)                    Append an owned item (strdup'd).
  void wgtDropdownRemoveItem(w, idx)                  Remove an owned item by index.
  void wgtDropdownClear(w)                            Remove all owned items.
  const char *wgtDropdownGetItem(w, idx)              Get the text of an item by index.
  int32_t wgtDropdownGetItemCount(w)                  Get the total number of items.
.endtable

.h3 API Struct (wgtRegisterApi "dropdown")

.table
  Slot           Function
  ----           --------
  create         wgtDropdown
  setItems       wgtDropdownSetItems
  getSelected    wgtDropdownGetSelected
  setSelected    wgtDropdownSetSelected
  addItem        wgtDropdownAddItem
  removeItem     wgtDropdownRemoveItem
  clear          wgtDropdownClear
  getItem        wgtDropdownGetItem
  getItemCount   wgtDropdownGetItemCount
.endtable

.h3 Properties (BASIC Interface)

.table
  Property     Type       Access       Description
  --------     ----       ------       -----------
  ListIndex    Integer    Read/Write   Index of the currently selected item (-1 if none).
.endtable

.h3 Methods (BASIC Interface)

.table
  Method              Description
  ------              -----------
  AddItem text$       Append an item to the drop-down list.
  Clear               Remove all items.
  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.
.endtable

.h3 Events

.table
  Callback     Description
  --------     -----------
  onChange     Fires when the selected item changes.
.endtable

.h3 Default Event

"Click" (VB basName: DropDown).
