25#ifndef TGUI_LIST_VIEW_HPP
26#define TGUI_LIST_VIEW_HPP
28#include <TGUI/CopiedSharedPtr.hpp>
29#include <TGUI/Widgets/Scrollbar.hpp>
30#include <TGUI/Renderers/ListViewRenderer.hpp>
31#include <TGUI/Text.hpp>
33#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
40TGUI_MODULE_EXPORT
namespace tgui
51 using Ptr = std::shared_ptr<ListView>;
52 using ConstPtr = std::shared_ptr<const ListView>;
54 static constexpr const char StaticWidgetType[] =
"ListView";
68 std::vector<Text> texts;
76 float designWidth = 0;
77 float widestItemWidth = 0;
78 std::size_t widestItemIndex = std::numeric_limits<std::size_t>::max();
81 bool autoResize =
false;
82 bool expanded =
false;
92 ListView(
const char* typeName = StaticWidgetType,
bool initRenderer =
true);
129 using Widget::setSize;
336 std::size_t
addItem(
const std::vector<String>& item);
359 void insertItem(std::size_t index,
const std::vector<String>& item);
377 bool changeItem(std::size_t index,
const std::vector<String>& item);
471 template <
typename DataType>
474 if (index < m_items.size())
475 return AnyCast<DataType>(m_items[index].data);
477 throw std::bad_cast();
522 TGUI_NODISCARD std::vector<String>
getItemRow(std::size_t index)
const;
539 TGUI_NODISCARD std::vector<String>
getItems()
const;
546 TGUI_NODISCARD std::vector<std::vector<String>>
getItemRows()
const;
559 void sort(std::size_t index,
const std::function<
bool(
const String&,
const String&)>& cmp);
686 TGUI_DEPRECATED(
"Use setColumnAutoExpanded and setColumnAutoResize instead") void setExpandLastColumn(
bool expand);
693 TGUI_DEPRECATED("Use ColumnAutoExpanded and ColumnAutoResize setters and getters instead") TGUI_NODISCARD
bool getExpandLastColumn() const;
699 void setVerticalScrollbarPolicy(
Scrollbar::Policy policy);
705 TGUI_NODISCARD
Scrollbar::Policy getVerticalScrollbarPolicy() const;
711 void setHorizontalScrollbarPolicy(
Scrollbar::Policy policy);
717 TGUI_NODISCARD
Scrollbar::Policy getHorizontalScrollbarPolicy() const;
724 void setVerticalScrollbarValue(
unsigned int value);
731 TGUI_NODISCARD
unsigned int getVerticalScrollbarValue() const;
738 void setHorizontalScrollbarValue(
unsigned int value);
745 TGUI_NODISCARD
unsigned int getHorizontalScrollbarValue() const;
773 void setResizableColumns(
bool resizable);
782 TGUI_NODISCARD
bool getResizableColumns() const;
788 TGUI_NODISCARD
bool isMouseOnWidget(
Vector2f pos) const override;
798 void leftMouseReleased(
Vector2f pos) override;
803 void rightMousePressed(
Vector2f pos) override;
808 void mouseMoved(
Vector2f pos) override;
813 bool scrolled(
float delta,
Vector2f pos,
bool touch) override;
818 void mouseNoLongerOnWidget() override;
823 void leftMouseButtonNoLongerDown() override;
828 void keyPressed(const
Event::KeyEvent& event) override;
839 bool canHandleKeyPress(const
Event::KeyEvent& event) override;
868 void rendererChanged(const
String& property) override;
873 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
878 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
883 void mouseEnteredWidget() override;
888 void mouseLeftWidget() override;
893 void updateTextSize() override;
898 TGUI_NODISCARD
Vector2f getInnerSize() const;
903 TGUI_NODISCARD
Text createText(const
String& caption);
908 TGUI_NODISCARD
Text createHeaderText(const
String& caption);
913 virtual
void setItemColor(std::
size_t index, const
Color& color);
918 TGUI_NODISCARD
float calculateAutoColumnWidth(const
Text& text);
923 void updateSelectedAndhoveredItemColors();
928 void updateItemColors();
933 void updateHeaderTextsColor();
938 void updateHoveredItem(
int item);
943 void updateSelectedItem(
int item);
948 void selectRangeFromEvent(std::
size_t item);
953 bool updateWidestItemInColumn(std::
size_t columnIndex);
958 bool updateWidestItemInColumn(std::
size_t columnIndex, std::
size_t itemIndex);
963 bool updateWidestItem();
968 bool updateWidestItem(std::
size_t itemIndex);
974 void incrementWidestItemIndices(std::
size_t itemIndex);
980 void updateColumnWidths();
985 bool hasExpandedColumn() const;
990 void addSelectedItem(
int item);
995 void removeSelectedItem(std::
size_t item);
1000 void updateHoveredItemByMousePos(
Vector2f mousePos);
1005 TGUI_NODISCARD
unsigned int getTotalSeparatorWidth() const;
1010 TGUI_NODISCARD
float getItemTotalWidth(const
Item& item, std::
size_t columnIndex) const;
1016 TGUI_NODISCARD
int getColumnIndexBelowMouse(
float mouseLeft);
1023 TGUI_NODISCARD
bool findBorderBelowMouse(
Vector2f pos, std::
size_t& columnIndex) const;
1028 void updateScrollbars();
1033 void updateVerticalScrollbarMaximum();
1043 void drawColumn(
BackendRenderTarget& target,
RenderStates states, std::
size_t firstItem, std::
size_t lastItem, std::
size_t column,
float columnWidth) const;
1048 bool updateTime(
Duration elapsedTime) override;
1072 std::vector<Column> m_columns;
1073 std::vector<Item> m_items;
1074 std::set<std::size_t> m_selectedItems;
1076 int m_hoveredItem = -1;
1077 int m_firstSelectedItemIndex = -1;
1078 int m_focusedItemIndex = -1;
1080 float m_requestedHeaderHeight = 0;
1081 unsigned int m_itemHeight = 0;
1082 unsigned int m_headerTextSize = 0;
1083 unsigned int m_headerSeparatorHeight = 0;
1084 unsigned int m_separatorWidth = 1;
1085 unsigned int m_gridLinesWidth = 1;
1086 unsigned int m_iconCount = 0;
1087 float m_maxIconWidth = 0;
1088 float m_widestItemWidth = 0;
1089 std::size_t m_widestItemIndex = std::numeric_limits<std::size_t>::max();
1090 bool m_headerVisible =
true;
1091 bool m_showHorizontalGridLines =
false;
1092 bool m_showVerticalGridLines =
true;
1093 bool m_expandLastColumn =
false;
1094 bool m_multiSelect =
false;
1095 bool m_resizableColumns =
false;
1097 Cursor::Type m_currentListViewMouseCursor = Cursor::Type::Arrow;
1098 std::size_t m_resizingColumn = 0;
1099 float m_resizingColumnLastMouseX = 0;
1106 int m_mouseOnHeaderIndex = -1;
1107 int m_possibleDoubleClick =
false;
1108 bool m_autoScroll =
true;
1110 Sprite m_spriteHeaderBackground;
1111 Sprite m_spriteBackground;
1116 Color m_borderColorCached;
1117 Color m_separatorColorCached;
1118 Color m_gridLinesColorCached;
1119 Color m_headerTextColorCached;
1120 Color m_headerBackgroundColorCached;
1121 Color m_backgroundColorCached;
1122 Color m_backgroundColorHoverCached;
1123 Color m_selectedBackgroundColorCached;
1124 Color m_selectedBackgroundColorHoverCached;
1125 Color m_textColorCached;
1126 Color m_textColorHoverCached;
1127 Color m_selectedTextColorCached;
1128 Color m_selectedTextColorHoverCached;
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Wrapper for colors.
Definition Color.hpp:72
Definition CopiedSharedPtr.hpp:45
Type
List of available cursors.
Definition Cursor.hpp:51
Wrapper for durations.
Definition Duration.hpp:56
Class to store the position or size of a widget.
Definition Layout.hpp:305
Definition ListViewRenderer.hpp:37
List view widget.
Definition ListView.hpp:48
TGUI_NODISCARD float getColumnWidth(std::size_t index) const
Returns the width of a column.
void setShowVerticalGridLines(bool showGridLines)
Changes whether lines are drawn between columns.
TGUI_NODISCARD unsigned int getGridLinesWidth() const
Returns the width of the grid lines.
TGUI_NODISCARD Texture getItemIcon(std::size_t index) const
Gets the icon displayed in front of the item.
void setSelectedItems(const std::set< std::size_t > &indices)
Selects items in the list view.
void setHeaderTextSize(unsigned int textSize)
Changes the text size of the header caption.
bool changeItem(std::size_t index, const std::vector< String > &item)
Changes an item with values for multiple columns to the list.
std::size_t addColumn(const String &text, float width=0, ColumnAlignment alignment=ColumnAlignment::Left)
Adds a column.
TGUI_NODISCARD String getItem(std::size_t index) const
Retrieves an item in the list.
TGUI_NODISCARD DataType getItemData(std::size_t index) const
Returns user data stored in the item.
Definition ListView.hpp:472
void removeAllItems()
Removes all items from the list.
void setColumnAlignment(std::size_t columnIndex, ColumnAlignment alignment)
Changes the text alignment within a column.
TGUI_NODISCARD bool getShowVerticalGridLines() const
Returns whether lines are drawn between items.
void setColumnWidth(std::size_t index, float width)
Changes the width of a column.
TGUI_NODISCARD float getHeaderHeight() const
Returns the height of the header row.
void setSize(const Layout2d &size) override
Changes the size of the list view.
void setHeaderVisible(bool showHeader)
Changes whether the header is shown.
TGUI_NODISCARD unsigned int getHeaderTextSize() const
Returns the text size of the header caption.
void insertItem(std::size_t index, const String &text)
Inserts an item into the list.
TGUI_NODISCARD unsigned int getItemHeight() const
Returns the height of the items in the list view.
static TGUI_NODISCARD ListView::Ptr create()
Creates a new list view widget.
void setColumnText(std::size_t index, const String &text)
Changes the text of a column.
std::shared_ptr< ListView > Ptr
Shared widget pointer.
Definition ListView.hpp:51
TGUI_NODISCARD unsigned int getHeaderSeparatorHeight() const
Returns the height of the separator between the header and the items.
TGUI_NODISCARD std::size_t getColumnCount() const
Returns the amount of columns in the list view.
static TGUI_NODISCARD ListView::Ptr copy(const ListView::ConstPtr &listView)
Makes a copy of another list view.
void sort(std::size_t index, const std::function< bool(const String &, const String &)> &cmp)
Sort items.
TGUI_NODISCARD String getColumnText(std::size_t index) const
Returns the text of a column.
void insertMultipleItems(std::size_t index, const std::vector< std::vector< String > > &items)
Inserts multiple items into the list.
bool changeSubItem(std::size_t index, std::size_t column, const String &item)
Changes the caption of a single value in the item.
int getSelectedItemIndex() const
Gets the index of the selected item.
std::size_t addItem(const String &text)
Adds an item to the list.
void insertItem(std::size_t index, const std::vector< String > &item)
Inserts an item into the list.
void setItemIcon(std::size_t index, const Texture &texture)
Sets a small icon in front of the item.
TGUI_NODISCARD bool getAutoScroll() const
Returns whether the list view scrolls to the bottom when a new item is added.
TGUI_NODISCARD std::vector< std::vector< String > > getItemRows() const
Returns a list of all column values for all items in the list view.
TGUI_NODISCARD std::set< std::size_t > getSelectedItemIndices() const
Gets the indices of the selected items.
ColumnAlignment
The text alignment for all texts within a column.
Definition ListView.hpp:60
TGUI_NODISCARD bool getMultiSelect() const
Returns multi selection of the items is allowed.
TGUI_NODISCARD bool getShowHorizontalGridLines() const
Returns whether lines are drawn between items.
void setHeaderHeight(float height)
Changes the height of the header row.
void setShowHorizontalGridLines(bool showGridLines)
Changes whether lines are drawn between items.
std::shared_ptr< const ListView > ConstPtr
Shared constant widget pointer.
Definition ListView.hpp:52
TGUI_NODISCARD bool getColumnAutoResize(std::size_t index) const
Returns whether the column is expanded to fill the list view (if all columns fit inside the list view...
void deselectItems()
Deselects the selected items.
void addMultipleItems(const std::vector< std::vector< String > > &items)
Adds multiple items to the list.
TGUI_NODISCARD bool getColumnExpanded(std::size_t index) const
Returns whether the column is expanded to fill the list view (if all columns fit inside the list view...
TGUI_NODISCARD float getCurrentHeaderHeight() const
Returns the height of the header or 0 if no header row is shown.
void setAutoScroll(bool autoScroll)
Changes whether the list view scrolls to the bottom when a new item is added.
TGUI_NODISCARD float getColumnDesignWidth(std::size_t index) const
Returns the original width of a column (the value passed to addColumn or the last setColumnWidth call...
std::size_t addItem(const std::vector< String > &item)
Adds an item with values for multiple columns to the list.
TGUI_NODISCARD bool getHeaderVisible() const
Returns whether the header is shown.
void setItemHeight(unsigned int itemHeight)
Changes the height of the items in the list view.
void setSelectedItem(std::size_t index)
Selects an item in the list view.
void setSeparatorWidth(unsigned int width)
Changes the width of the column separator.
void setHeaderSeparatorHeight(unsigned int height)
Changes the height of the separator between the header and the items.
void setItemData(std::size_t index, Any data)
Store some user data with the item.
TGUI_NODISCARD unsigned int getSeparatorWidth() const
Returns the width of the column separator.
TGUI_NODISCARD ListViewRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD std::vector< String > getItems() const
Returns a list of the texts in the first column for all items in the list view.
void setMultiSelect(bool multiSelect)
Allow multi selection of the items.
void removeAllColumns()
Removes all columns.
TGUI_NODISCARD ListViewRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD ColumnAlignment getColumnAlignment(std::size_t columnIndex) const
Returns the current text alignment within a column.
TGUI_NODISCARD std::vector< String > getItemRow(std::size_t index) const
Retrieves the values of all columns for an item in the list.
bool removeItem(std::size_t index)
Removes the item from the list view.
TGUI_NODISCARD std::size_t getItemCount() const
Returns the amount of items in the list view.
void setColumnAutoResize(std::size_t index, bool autoResize)
Changes whether the column width depends on the widest item added to that column.
TGUI_NODISCARD String getItemCell(std::size_t rowIndex, std::size_t columnIndex) const
Retrieves the value for a cell in the list.
void setColumnExpanded(std::size_t index, bool expand)
Changes whether a column is expanded to fill the list view (if all columns fit inside the list view)
void setGridLinesWidth(unsigned int width)
Changes the width of the grid lines.
Definition Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
Wrapper class to store strings.
Definition String.hpp:101
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:50
Texture wrapper that internally reuses resources when multiple Texture objects are loaded from the sa...
Definition Texture.hpp:57
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Definition ListView.hpp:74
Definition ListView.hpp:67
States used for drawing.
Definition RenderStates.hpp:39