# 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.listview
.title ListView
.toc 0 ListView
.index ListView
.index wgtListView
.index wgtListViewSetColumns
.index wgtListViewSetData
.index ListViewColT
.index ListViewSortE

.h2 ListView

A multi-column list with sortable headers. Supports single and multi-selection, column alignment, header click sorting, and drag-to-reorder. Data is provided as a flat array of strings (row-major order, one string per cell).

Header: widgets/listView.h

.h3 Creation

.code
WidgetT *lv = wgtListView(parent);
ListViewColT cols[] = {
    { "Name", wgtChars(20), ListViewAlignLeftE },
    { "Size", wgtChars(10), ListViewAlignRightE }
};
wgtListViewSetColumns(lv, cols, 2);
const char *cells[] = { "file.txt", "1234", "readme.md", "5678" };
wgtListViewSetData(lv, cells, 2);
.endcode

.h3 Column Definition

.code
typedef struct {
    const char      *title;
    int32_t          width;   // tagged size (wgtPixels/wgtChars/wgtPercent, 0 = auto)
    ListViewAlignE   align;   // ListViewAlignLeftE, ListViewAlignCenterE, ListViewAlignRightE
} ListViewColT;
.endcode

.h3 Sort Direction

.code
typedef enum {
    ListViewSortNoneE,
    ListViewSortAscE,
    ListViewSortDescE
} ListViewSortE;
.endcode

.h3 Macros

.index wgtListViewGetSelected
.index wgtListViewSetSelected
.index wgtListViewSetSort
.index wgtListViewSetHeaderClickCallback
.index wgtListViewSetMultiSelect
.index wgtListViewSelectAll
.index wgtListViewClearSelection
.index wgtListViewSetReorderable

.table
  Macro                                              Description
  -----                                              -----------
  wgtListView(parent)                                Create a list view.
  wgtListViewSetColumns(w, cols, count)              Define columns from an array of ListViewColT.
  wgtListViewSetData(w, cellData, rowCount)          Set row data. cellData is a flat const char ** array of size rowCount * colCount.
  wgtListViewGetSelected(w)                          Get the index of the selected row (-1 if none).
  wgtListViewSetSelected(w, idx)                     Set the selected row by index.
  wgtListViewSetSort(w, col, dir)                    Set the sort column and direction.
  wgtListViewSetHeaderClickCallback(w, cb)           Set a callback for header clicks. Signature: void (*cb)(WidgetT *w, int32_t col, ListViewSortE dir).
  wgtListViewSetMultiSelect(w, multi)                Enable or disable multi-selection.
  wgtListViewIsItemSelected(w, idx)                  Check if a specific row is selected.
  wgtListViewSetItemSelected(w, idx, selected)       Select or deselect a specific row.
  wgtListViewSelectAll(w)                            Select all rows.
  wgtListViewClearSelection(w)                       Deselect all rows.
  wgtListViewSetReorderable(w, reorderable)          Enable drag-to-reorder of rows.
.endtable

.h3 Events

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

.h3 Properties (BASIC Interface)

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

.h3 Methods (BASIC Interface)

.table
  Method                          Description
  ------                          -----------
  AddItem text$                   Add a row (sets first column text).
  Clear                           Remove all rows.
  ClearSelection                  Deselect all rows.
  GetCell(row%, col%)             Returns the text of a cell.
  IsItemSelected(index%)          Check if a specific row is selected.
  RemoveItem index%               Remove a row by index.
  RowCount()                      Returns the number of rows.
  SelectAll                       Select all rows.
  SetCell row%, col%, text$       Set the text of a cell.
  SetColumns spec$                Define columns from a pipe-delimited spec string ("Name,Width|Name,Width").
  SetItemSelected idx%, sel       Select or deselect a specific row by index.
  SetMultiSelect multi            Enable or disable multi-selection.
  SetReorderable reorderable      Enable or disable drag-to-reorder.
  SetSort col%, dir%              Set the sort column and direction (0=none, 1=ascending, 2=descending).
.endtable

.h3 Default Event

"Click" (VB basName: ListView).
