2020-05-06 20:39:57 +01:00
|
|
|
/*****************************************************************************
|
2026-01-05 14:42:59 +01:00
|
|
|
* Copyright (c) 2014-2026 OpenRCT2 developers
|
2020-05-06 20:39:57 +01:00
|
|
|
*
|
|
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
|
*
|
|
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_SCRIPTING
|
|
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "CustomListView.h"
|
2020-05-06 20:39:57 +01:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <numeric>
|
2026-08-11 22:16:10 +01:00
|
|
|
#include <openrct2-ui/interface/Window.h>
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <openrct2/Context.h>
|
2024-12-11 16:58:56 +01:00
|
|
|
#include <openrct2/core/String.hpp>
|
2026-01-13 23:41:59 +01:00
|
|
|
#include <openrct2/drawing/ColourMap.h>
|
2026-03-22 14:14:32 +01:00
|
|
|
#include <openrct2/drawing/Drawing.String.h>
|
2026-01-05 20:09:34 +01:00
|
|
|
#include <openrct2/drawing/Drawing.h>
|
2026-08-11 22:16:10 +01:00
|
|
|
#include <openrct2/drawing/FilterPaletteIds.h>
|
2025-10-23 18:44:45 +02:00
|
|
|
#include <openrct2/drawing/Rectangle.h>
|
2026-07-21 16:00:55 +01:00
|
|
|
#include <openrct2/drawing/RenderTarget.h>
|
2026-03-26 12:55:37 +01:00
|
|
|
#include <openrct2/drawing/Text.h>
|
2026-08-11 22:16:10 +01:00
|
|
|
#include <openrct2/interface/Widget.h>
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <openrct2/localisation/Formatter.h>
|
2026-08-11 22:16:10 +01:00
|
|
|
#include <openrct2/localisation/StringIds.h>
|
|
|
|
|
#include <openrct2/scripting/ScriptEngine.h>
|
2020-05-06 20:39:57 +01:00
|
|
|
|
2025-10-21 12:20:00 +02:00
|
|
|
using namespace OpenRCT2::Drawing;
|
2020-05-06 20:39:57 +01:00
|
|
|
using namespace OpenRCT2::Scripting;
|
|
|
|
|
using namespace OpenRCT2::Ui::Windows;
|
|
|
|
|
|
|
|
|
|
namespace OpenRCT2::Scripting
|
|
|
|
|
{
|
2025-01-19 16:59:45 +01:00
|
|
|
constexpr size_t kColumnHeaderHeight = kListRowHeight + 1;
|
2020-05-16 02:05:00 +01:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
ColumnSortOrder ColumnSortOrderFromJS(JSContext* ctx, JSValue d)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsString(d))
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto s = JSToStdString(ctx, d);
|
2020-05-06 20:39:57 +01:00
|
|
|
if (s == "ascending")
|
2026-08-05 23:05:20 +02:00
|
|
|
return ColumnSortOrder::ascending;
|
2020-05-06 20:39:57 +01:00
|
|
|
if (s == "descending")
|
2026-08-05 23:05:20 +02:00
|
|
|
return ColumnSortOrder::descending;
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
2026-08-05 23:05:20 +02:00
|
|
|
return ColumnSortOrder::none;
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
static JSValue ColumnSortOrderToJS(JSContext* ctx, ColumnSortOrder value)
|
2020-05-07 00:02:28 +01:00
|
|
|
{
|
|
|
|
|
switch (value)
|
|
|
|
|
{
|
2026-08-05 23:05:20 +02:00
|
|
|
case ColumnSortOrder::ascending:
|
2025-09-29 02:05:00 +10:00
|
|
|
return JSFromStdString(ctx, "ascending");
|
2026-08-05 23:05:20 +02:00
|
|
|
case ColumnSortOrder::descending:
|
2025-09-29 02:05:00 +10:00
|
|
|
return JSFromStdString(ctx, "descending");
|
2020-05-07 00:02:28 +01:00
|
|
|
default:
|
2025-09-29 02:05:00 +10:00
|
|
|
return JSFromStdString(ctx, "none");
|
2020-05-07 00:02:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
ListViewColumn ListViewColumnFromJS(JSContext* ctx, JSValue d)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
ListViewColumn result;
|
2025-09-29 02:05:00 +10:00
|
|
|
result.CanSort = AsOrDefault(ctx, d, "canSort", false);
|
|
|
|
|
JSValue sortOrderVal = JS_GetPropertyStr(ctx, d, "sortOrder");
|
|
|
|
|
result.SortOrder = ColumnSortOrderFromJS(ctx, sortOrderVal);
|
|
|
|
|
JS_FreeValue(ctx, sortOrderVal);
|
|
|
|
|
result.Header = AsOrDefault(ctx, d, "header", "");
|
|
|
|
|
result.HeaderTooltip = AsOrDefault(ctx, d, "headerTooltip", "");
|
|
|
|
|
result.MinWidth = JSToOptionalInt(ctx, d, "minWidth");
|
|
|
|
|
result.MaxWidth = JSToOptionalInt(ctx, d, "maxWidth");
|
|
|
|
|
result.RatioWidth = JSToOptionalInt(ctx, d, "ratioWidth");
|
|
|
|
|
|
|
|
|
|
auto width = JSToOptionalInt(ctx, d, "width");
|
|
|
|
|
if (width.has_value())
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
result.MinWidth = width.value();
|
2020-05-06 20:39:57 +01:00
|
|
|
result.MaxWidth = result.MinWidth;
|
|
|
|
|
result.RatioWidth = std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
else if (!result.RatioWidth)
|
|
|
|
|
{
|
|
|
|
|
result.RatioWidth = 1;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue ListViewColumnToJS(JSContext* ctx, const ListViewColumn& value)
|
2020-05-07 00:02:28 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue obj = JS_NewObject(ctx);
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "canSort", JS_NewBool(ctx, value.CanSort));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "sortOrder", ColumnSortOrderToJS(ctx, value.SortOrder));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "header", JSFromStdString(ctx, value.Header));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "headerTooltip", JSFromStdString(ctx, value.HeaderTooltip));
|
|
|
|
|
JS_SetPropertyStr(
|
|
|
|
|
ctx, obj, "minWidth", value.MinWidth.has_value() ? JS_NewInt32(ctx, value.MinWidth.value()) : JS_NULL);
|
|
|
|
|
JS_SetPropertyStr(
|
|
|
|
|
ctx, obj, "maxWidth", value.MaxWidth.has_value() ? JS_NewInt32(ctx, value.MaxWidth.value()) : JS_NULL);
|
|
|
|
|
JS_SetPropertyStr(
|
|
|
|
|
ctx, obj, "ratioWidth", value.RatioWidth.has_value() ? JS_NewInt32(ctx, value.RatioWidth.value()) : JS_NULL);
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "width", JS_NewInt32(ctx, value.Width));
|
|
|
|
|
return obj;
|
2020-05-07 00:02:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
ListViewItem ListViewItemFromJS(JSContext* ctx, JSValue d)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
ListViewItem result;
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsString(d))
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
result = ListViewItem(JSToStdString(ctx, d));
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
else if (JS_IsArray(d))
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
std::vector<std::string> cells;
|
2025-09-29 02:05:00 +10:00
|
|
|
JSIterateArray(ctx, d, [&cells](JSContext* ctx2, JSValue x) { cells.push_back(JSToStdString(ctx2, x)); });
|
2020-05-06 20:39:57 +01:00
|
|
|
result = ListViewItem(std::move(cells));
|
|
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
else if (JS_IsObject(d))
|
2020-12-04 18:46:34 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto type = JSToStdString(ctx, d, "type");
|
2025-08-28 00:33:05 +02:00
|
|
|
// This type was misspelt between 2020 and 2025.
|
2025-09-01 04:43:01 +10:00
|
|
|
if (type == "separator" || type == "seperator")
|
2020-12-04 18:46:34 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto text = JSToStdString(ctx, d, "text");
|
2020-12-04 18:46:34 +00:00
|
|
|
result = ListViewItem(text);
|
|
|
|
|
result.IsSeparator = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-06 20:39:57 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
2020-05-06 22:34:56 +01:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
std::vector<ListViewColumn> ListViewColumnVecFromJS(JSContext* ctx, JSValue d)
|
2020-05-07 00:02:28 +01:00
|
|
|
{
|
|
|
|
|
std::vector<ListViewColumn> result;
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsArray(d))
|
2020-05-07 00:02:28 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSIterateArray(ctx, d, [&result](JSContext* ctx2, JSValue x) { result.push_back(ListViewColumnFromJS(ctx2, x)); });
|
2020-05-07 00:02:28 +01:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
std::vector<ListViewItem> ListViewItemVecFromJS(JSContext* ctx, JSValue d)
|
2020-05-07 00:02:28 +01:00
|
|
|
{
|
|
|
|
|
std::vector<ListViewItem> result;
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsArray(d))
|
2020-05-07 00:02:28 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSIterateArray(ctx, d, [&result](JSContext* ctx2, JSValue x) { result.push_back(ListViewItemFromJS(ctx2, x)); });
|
2020-05-07 00:02:28 +01:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
std::optional<RowColumn> RowColumnFromJS(JSContext* ctx, JSValue d)
|
2020-05-06 22:34:56 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsObject(d))
|
2020-05-06 22:34:56 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto row = JSToOptionalInt(ctx, d, "row");
|
|
|
|
|
auto column = JSToOptionalInt(ctx, d, "column");
|
|
|
|
|
if (row.has_value() && column.has_value())
|
2020-05-06 22:34:56 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
return RowColumn(row.value(), column.value());
|
2020-05-06 22:34:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue RowColumnToJS(JSContext* ctx, const RowColumn value)
|
2020-05-06 22:34:56 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue obj = JS_NewObject(ctx);
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "row", JS_NewInt32(ctx, value.Row));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "column", JS_NewInt32(ctx, value.Column));
|
|
|
|
|
return obj;
|
2020-05-06 22:34:56 +01:00
|
|
|
}
|
2020-05-09 16:06:21 +01:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
ScrollbarType ScrollbarTypeFromJS(JSContext* ctx, JSValue d)
|
2020-05-09 16:06:21 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto value = JSToStdString(ctx, d);
|
2020-05-09 16:06:21 +01:00
|
|
|
if (value == "horizontal")
|
2026-08-05 23:05:01 +02:00
|
|
|
return ScrollbarType::horizontal;
|
2020-05-09 16:06:21 +01:00
|
|
|
if (value == "vertical")
|
2026-08-05 23:05:01 +02:00
|
|
|
return ScrollbarType::vertical;
|
2020-05-09 16:06:21 +01:00
|
|
|
if (value == "both")
|
2026-08-05 23:05:01 +02:00
|
|
|
return ScrollbarType::both;
|
|
|
|
|
return ScrollbarType::none;
|
2020-05-09 16:06:21 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue ScrollbarTypeToJS(JSContext* ctx, const ScrollbarType value)
|
2020-05-09 16:06:21 +01:00
|
|
|
{
|
|
|
|
|
switch (value)
|
|
|
|
|
{
|
|
|
|
|
default:
|
2026-08-05 23:05:01 +02:00
|
|
|
case ScrollbarType::none:
|
2025-09-29 02:05:00 +10:00
|
|
|
return JSFromStdString(ctx, "none");
|
2026-08-05 23:05:01 +02:00
|
|
|
case ScrollbarType::horizontal:
|
2025-09-29 02:05:00 +10:00
|
|
|
return JSFromStdString(ctx, "horizontal");
|
2026-08-05 23:05:01 +02:00
|
|
|
case ScrollbarType::vertical:
|
2025-09-29 02:05:00 +10:00
|
|
|
return JSFromStdString(ctx, "vertical");
|
2026-08-05 23:05:01 +02:00
|
|
|
case ScrollbarType::both:
|
2025-09-29 02:05:00 +10:00
|
|
|
return JSFromStdString(ctx, "both");
|
2020-05-09 16:06:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 20:39:57 +01:00
|
|
|
} // namespace OpenRCT2::Scripting
|
|
|
|
|
|
2023-01-21 19:57:17 +00:00
|
|
|
CustomListView::CustomListView(WindowBase* parent, size_t scrollIndex)
|
2020-05-09 16:06:21 +01:00
|
|
|
: ParentWindow(parent)
|
|
|
|
|
, ScrollIndex(scrollIndex)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScrollbarType CustomListView::GetScrollbars() const
|
|
|
|
|
{
|
|
|
|
|
return Scrollbars;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 17:09:47 +01:00
|
|
|
void CustomListView::SetScrollbars(ScrollbarType value, bool initialising)
|
2020-05-09 16:06:21 +01:00
|
|
|
{
|
|
|
|
|
Scrollbars = value;
|
|
|
|
|
|
2020-05-09 17:09:47 +01:00
|
|
|
if (!initialising)
|
2020-05-09 16:06:21 +01:00
|
|
|
{
|
2020-05-16 02:05:00 +01:00
|
|
|
auto widget = GetWidget();
|
|
|
|
|
if (widget != nullptr)
|
2020-05-09 16:06:21 +01:00
|
|
|
{
|
2026-08-05 23:05:01 +02:00
|
|
|
if (value == ScrollbarType::horizontal)
|
2020-05-16 02:05:00 +01:00
|
|
|
widget->content = SCROLL_HORIZONTAL;
|
2026-08-05 23:05:01 +02:00
|
|
|
else if (value == ScrollbarType::vertical)
|
2020-05-16 02:05:00 +01:00
|
|
|
widget->content = SCROLL_VERTICAL;
|
2026-08-05 23:05:01 +02:00
|
|
|
else if (value == ScrollbarType::both)
|
2020-05-16 02:05:00 +01:00
|
|
|
widget->content = SCROLL_BOTH;
|
|
|
|
|
else
|
|
|
|
|
widget->content = 0;
|
2020-05-09 16:06:21 +01:00
|
|
|
}
|
2022-07-30 21:59:47 +02:00
|
|
|
WindowInitScrollWidgets(*ParentWindow);
|
2020-12-04 17:56:20 +00:00
|
|
|
Invalidate();
|
2020-05-09 16:06:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-07 00:02:28 +01:00
|
|
|
const std::vector<ListViewColumn>& CustomListView::GetColumns() const
|
|
|
|
|
{
|
|
|
|
|
return Columns;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 17:09:47 +01:00
|
|
|
void CustomListView::SetColumns(const std::vector<ListViewColumn>& columns, bool initialising)
|
2020-05-07 00:02:28 +01:00
|
|
|
{
|
|
|
|
|
SelectedCell = std::nullopt;
|
|
|
|
|
Columns = columns;
|
|
|
|
|
LastKnownSize = {};
|
2026-08-05 23:05:20 +02:00
|
|
|
SortItems(0, ColumnSortOrder::none);
|
2020-05-09 17:09:47 +01:00
|
|
|
if (!initialising)
|
2020-05-12 21:03:21 +01:00
|
|
|
{
|
2023-01-16 21:13:42 +01:00
|
|
|
WindowUpdateScrollWidgets(*ParentWindow);
|
2020-12-04 17:56:20 +00:00
|
|
|
Invalidate();
|
2020-05-12 21:03:21 +01:00
|
|
|
}
|
2020-05-07 00:02:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 04:38:20 -03:00
|
|
|
const std::vector<ListViewItem>& CustomListView::GetItems() const
|
2020-05-07 00:02:28 +01:00
|
|
|
{
|
|
|
|
|
return Items;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 17:09:47 +01:00
|
|
|
void CustomListView::SetItems(const std::vector<ListViewItem>& items, bool initialising)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2020-05-07 00:02:28 +01:00
|
|
|
SelectedCell = std::nullopt;
|
2020-05-06 20:39:57 +01:00
|
|
|
Items = items;
|
2026-08-05 23:05:20 +02:00
|
|
|
SortItems(0, ColumnSortOrder::none);
|
2020-05-09 17:09:47 +01:00
|
|
|
if (!initialising)
|
2020-05-12 21:03:21 +01:00
|
|
|
{
|
2023-01-16 21:13:42 +01:00
|
|
|
WindowUpdateScrollWidgets(*ParentWindow);
|
2020-05-12 21:03:21 +01:00
|
|
|
Invalidate();
|
|
|
|
|
}
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-09 17:09:47 +01:00
|
|
|
void CustomListView::SetItems(std::vector<ListViewItem>&& items, bool initialising)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2021-01-12 06:14:15 +09:00
|
|
|
Items = std::move(items);
|
2026-08-05 23:05:20 +02:00
|
|
|
SortItems(0, ColumnSortOrder::none);
|
2020-05-09 17:09:47 +01:00
|
|
|
if (!initialising)
|
2020-05-12 21:03:21 +01:00
|
|
|
{
|
2023-01-16 21:13:42 +01:00
|
|
|
WindowUpdateScrollWidgets(*ParentWindow);
|
2020-12-04 17:56:20 +00:00
|
|
|
Invalidate();
|
2020-05-12 21:03:21 +01:00
|
|
|
}
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-06 23:09:31 +01:00
|
|
|
bool CustomListView::SortItem(size_t indexA, size_t indexB, int32_t column)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
const auto& cellA = Items[indexA].Cells[column];
|
|
|
|
|
const auto& cellB = Items[indexB].Cells[column];
|
2024-12-11 22:28:31 +01:00
|
|
|
return String::logicalCmp(cellA.c_str(), cellB.c_str()) < 0;
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-06 23:09:31 +01:00
|
|
|
void CustomListView::SortItems(int32_t column)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2026-08-05 23:05:20 +02:00
|
|
|
auto sortOrder = ColumnSortOrder::ascending;
|
2020-05-06 20:39:57 +01:00
|
|
|
if (CurrentSortColumn == column)
|
|
|
|
|
{
|
2026-08-05 23:05:20 +02:00
|
|
|
if (CurrentSortOrder == ColumnSortOrder::ascending)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2026-08-05 23:05:20 +02:00
|
|
|
sortOrder = ColumnSortOrder::descending;
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
2026-08-05 23:05:20 +02:00
|
|
|
else if (CurrentSortOrder == ColumnSortOrder::descending)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2026-08-05 23:05:20 +02:00
|
|
|
sortOrder = ColumnSortOrder::none;
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SortItems(column, sortOrder);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 23:09:31 +01:00
|
|
|
void CustomListView::SortItems(int32_t column, ColumnSortOrder order)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
// Reset the sorted index map
|
|
|
|
|
SortedItems.resize(Items.size());
|
|
|
|
|
for (size_t i = 0; i < SortedItems.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
SortedItems[i] = i;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-05 23:05:20 +02:00
|
|
|
if (order != ColumnSortOrder::none)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
std::sort(
|
|
|
|
|
SortedItems.begin(), SortedItems.end(), [this, column](size_t a, size_t b) { return SortItem(a, b, column); });
|
2026-08-05 23:05:20 +02:00
|
|
|
if (order == ColumnSortOrder::descending)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
std::reverse(SortedItems.begin(), SortedItems.end());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurrentSortOrder = order;
|
|
|
|
|
CurrentSortColumn = column;
|
2020-05-12 18:54:00 +01:00
|
|
|
if (column >= 0 && static_cast<size_t>(column) < Columns.size())
|
|
|
|
|
{
|
|
|
|
|
Columns[column].SortOrder = order;
|
|
|
|
|
}
|
2020-05-12 21:03:21 +01:00
|
|
|
Invalidate();
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomListView::Resize(const ScreenSize& size)
|
|
|
|
|
{
|
|
|
|
|
if (size == LastKnownSize)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
LastKnownSize = size;
|
|
|
|
|
|
|
|
|
|
// Calculate the total of all ratios
|
|
|
|
|
int32_t totalRatio = 0;
|
|
|
|
|
for (size_t c = 0; c < Columns.size(); c++)
|
|
|
|
|
{
|
|
|
|
|
auto& column = Columns[c];
|
|
|
|
|
if (column.RatioWidth)
|
|
|
|
|
{
|
|
|
|
|
totalRatio += *column.RatioWidth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate column widths
|
2026-08-05 23:05:01 +02:00
|
|
|
bool hasHorizontalScroll = Scrollbars == ScrollbarType::horizontal || Scrollbars == ScrollbarType::both;
|
2020-05-06 20:39:57 +01:00
|
|
|
int32_t widthRemaining = size.width;
|
|
|
|
|
for (size_t c = 0; c < Columns.size(); c++)
|
|
|
|
|
{
|
|
|
|
|
auto& column = Columns[c];
|
2020-05-09 17:09:47 +01:00
|
|
|
auto isLastColumn = c == Columns.size() - 1;
|
|
|
|
|
if (!hasHorizontalScroll && isLastColumn)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
column.Width = widthRemaining;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
column.Width = 0;
|
|
|
|
|
if (column.RatioWidth && *column.RatioWidth > 0)
|
|
|
|
|
{
|
2020-05-09 17:09:47 +01:00
|
|
|
if (isLastColumn)
|
|
|
|
|
{
|
|
|
|
|
column.Width = widthRemaining;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
column.Width = (size.width * *column.RatioWidth) / totalRatio;
|
|
|
|
|
}
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
if (column.MinWidth)
|
|
|
|
|
{
|
|
|
|
|
column.Width = std::max(column.Width, *column.MinWidth);
|
|
|
|
|
}
|
|
|
|
|
if (column.MaxWidth)
|
|
|
|
|
{
|
|
|
|
|
column.Width = std::min(column.Width, *column.MaxWidth);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-09 17:09:47 +01:00
|
|
|
widthRemaining = std::max(0, widthRemaining - column.Width);
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScreenSize CustomListView::GetSize()
|
|
|
|
|
{
|
|
|
|
|
LastHighlightedCell = HighlightedCell;
|
|
|
|
|
HighlightedCell = std::nullopt;
|
|
|
|
|
ColumnHeaderPressedCurrentState = false;
|
|
|
|
|
LastIsMouseDown = IsMouseDown;
|
|
|
|
|
IsMouseDown = false;
|
|
|
|
|
|
|
|
|
|
ScreenSize result;
|
2026-08-05 23:05:01 +02:00
|
|
|
if (Scrollbars == ScrollbarType::horizontal || Scrollbars == ScrollbarType::both)
|
2020-05-09 17:09:47 +01:00
|
|
|
{
|
|
|
|
|
result.width = std::accumulate(
|
|
|
|
|
Columns.begin(), Columns.end(), 0, [](int32_t acc, const ListViewColumn& column) { return acc + column.Width; });
|
|
|
|
|
|
|
|
|
|
// Fixes an off-by-one error that causes the scrollbar thumb to not fill when the widget is wide enough
|
|
|
|
|
result.width--;
|
|
|
|
|
}
|
2026-08-05 23:05:01 +02:00
|
|
|
if (Scrollbars == ScrollbarType::vertical || Scrollbars == ScrollbarType::both)
|
2020-05-09 17:09:47 +01:00
|
|
|
{
|
2024-04-14 16:37:01 +00:00
|
|
|
result.height = static_cast<int32_t>(Items.size() * kListRowHeight);
|
2020-05-16 02:05:00 +01:00
|
|
|
if (ShowColumnHeaders)
|
|
|
|
|
{
|
2025-01-19 16:59:45 +01:00
|
|
|
result.height += kColumnHeaderHeight;
|
2020-05-16 02:05:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the list is getting bigger than the contents, pan towards top left scroll
|
|
|
|
|
auto widget = GetWidget();
|
|
|
|
|
if (widget != nullptr)
|
|
|
|
|
{
|
|
|
|
|
auto& scroll = ParentWindow->scrolls[ScrollIndex];
|
|
|
|
|
|
|
|
|
|
// Horizontal
|
|
|
|
|
auto left = result.width - widget->right + widget->left + 21;
|
|
|
|
|
if (left < 0)
|
|
|
|
|
left = 0;
|
2024-08-11 16:21:53 +02:00
|
|
|
if (left < scroll.contentOffsetX)
|
2020-05-16 02:05:00 +01:00
|
|
|
{
|
2024-08-11 16:21:53 +02:00
|
|
|
scroll.contentOffsetX = left;
|
2020-05-16 02:05:00 +01:00
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Vertical
|
|
|
|
|
auto top = result.height - widget->bottom + widget->top + 21;
|
|
|
|
|
if (top < 0)
|
|
|
|
|
top = 0;
|
2024-08-11 16:21:53 +02:00
|
|
|
if (top < scroll.contentOffsetY)
|
2020-05-16 02:05:00 +01:00
|
|
|
{
|
2024-08-11 16:21:53 +02:00
|
|
|
scroll.contentOffsetY = top;
|
2020-05-16 02:05:00 +01:00
|
|
|
Invalidate();
|
|
|
|
|
}
|
2020-05-09 17:09:47 +01:00
|
|
|
}
|
2020-05-06 20:39:57 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomListView::MouseOver(const ScreenCoordsXY& pos, bool isMouseDown)
|
|
|
|
|
{
|
|
|
|
|
auto hitResult = GetItemIndexAt(pos);
|
|
|
|
|
if (hitResult)
|
|
|
|
|
{
|
|
|
|
|
HighlightedCell = hitResult;
|
|
|
|
|
if (HighlightedCell != LastHighlightedCell)
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (hitResult->Row != kHeaderRow && OnHighlight.context != nullptr && OnHighlight.IsValid())
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto ctx = OnHighlight.context;
|
|
|
|
|
JSValue jsRow = JS_NewInt32(ctx, HighlightedCell->Row);
|
|
|
|
|
JSValue jsColumn = JS_NewInt32(ctx, HighlightedCell->Column);
|
2020-05-06 20:39:57 +01:00
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
2025-09-29 02:05:00 +10:00
|
|
|
scriptEngine.ExecutePluginCall(Owner, OnHighlight.callback, { jsRow, jsColumn }, false);
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
2020-05-12 21:03:21 +01:00
|
|
|
Invalidate();
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the header currently held down
|
|
|
|
|
if (isMouseDown)
|
|
|
|
|
{
|
2025-01-19 16:59:45 +01:00
|
|
|
if (hitResult && hitResult->Row == kHeaderRow)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
ColumnHeaderPressedCurrentState = (hitResult->Column == ColumnHeaderPressed);
|
2020-05-12 21:03:21 +01:00
|
|
|
Invalidate();
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
IsMouseDown = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (LastIsMouseDown)
|
|
|
|
|
{
|
|
|
|
|
MouseUp(pos);
|
|
|
|
|
}
|
|
|
|
|
IsMouseDown = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomListView::MouseDown(const ScreenCoordsXY& pos)
|
|
|
|
|
{
|
|
|
|
|
auto hitResult = GetItemIndexAt(pos);
|
|
|
|
|
if (hitResult)
|
|
|
|
|
{
|
2025-01-19 16:59:45 +01:00
|
|
|
if (hitResult->Row != kHeaderRow)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
if (CanSelect)
|
|
|
|
|
{
|
|
|
|
|
SelectedCell = hitResult;
|
2020-05-12 21:03:21 +01:00
|
|
|
Invalidate();
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
auto ctx = OnClick.context;
|
|
|
|
|
if (ctx != nullptr && OnClick.IsValid())
|
2021-01-25 02:32:18 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue jsRow = JS_NewInt32(ctx, hitResult->Row);
|
|
|
|
|
JSValue jsColumn = JS_NewInt32(ctx, hitResult->Column);
|
2021-01-25 02:32:18 +00:00
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
2025-09-29 02:05:00 +10:00
|
|
|
scriptEngine.ExecutePluginCall(Owner, OnClick.callback, { jsRow, jsColumn }, false);
|
2021-01-25 02:32:18 +00:00
|
|
|
}
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-19 16:59:45 +01:00
|
|
|
if (hitResult && hitResult->Row == kHeaderRow)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
if (Columns[hitResult->Column].CanSort)
|
|
|
|
|
{
|
|
|
|
|
ColumnHeaderPressed = hitResult->Column;
|
|
|
|
|
ColumnHeaderPressedCurrentState = true;
|
2020-05-12 21:03:21 +01:00
|
|
|
Invalidate();
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
IsMouseDown = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomListView::MouseUp(const ScreenCoordsXY& pos)
|
|
|
|
|
{
|
|
|
|
|
auto hitResult = GetItemIndexAt(pos);
|
2025-01-19 16:59:45 +01:00
|
|
|
if (hitResult && hitResult->Row == kHeaderRow)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
if (hitResult->Column == ColumnHeaderPressed)
|
|
|
|
|
{
|
|
|
|
|
SortItems(hitResult->Column);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 21:03:21 +01:00
|
|
|
if (!ColumnHeaderPressedCurrentState)
|
|
|
|
|
{
|
|
|
|
|
ColumnHeaderPressed = std::nullopt;
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
void CustomListView::Paint(WindowBase* w, RenderTarget& rt, const ScrollArea* scroll) const
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2026-01-13 23:41:59 +01:00
|
|
|
auto paletteIndex = getColourMap(w->colours[1].colour).midLight;
|
2025-10-23 18:44:45 +02:00
|
|
|
Rectangle::fill(rt, { { rt.x, rt.y }, { rt.x + rt.width, rt.y + rt.height } }, paletteIndex);
|
2020-05-06 20:39:57 +01:00
|
|
|
|
2025-01-19 16:59:45 +01:00
|
|
|
int32_t y = ShowColumnHeaders ? kColumnHeaderHeight : 0;
|
2020-05-06 20:39:57 +01:00
|
|
|
for (size_t i = 0; i < Items.size(); i++)
|
|
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
if (y > rt.y + rt.height)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
// Past the scroll view area
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
if (y + kListRowHeight >= rt.y)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
const auto& itemIndex = static_cast<int32_t>(SortedItems[i]);
|
|
|
|
|
const auto& item = Items[itemIndex];
|
|
|
|
|
|
2020-12-04 18:46:34 +00:00
|
|
|
if (item.IsSeparator)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
const auto& text = item.Cells[0];
|
2024-04-14 16:37:01 +00:00
|
|
|
ScreenSize cellSize = { LastKnownSize.width, kListRowHeight };
|
2025-08-28 00:33:05 +02:00
|
|
|
PaintSeparator(rt, { 0, y }, cellSize, text.c_str());
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-12-04 18:46:34 +00:00
|
|
|
// Background colour
|
|
|
|
|
auto isStriped = IsStriped && (i & 1);
|
|
|
|
|
auto isHighlighted = (HighlightedCell && itemIndex == HighlightedCell->Row);
|
|
|
|
|
auto isSelected = (SelectedCell && itemIndex == SelectedCell->Row);
|
|
|
|
|
if (isSelected)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2025-10-23 18:44:45 +02:00
|
|
|
Rectangle::filter(
|
2025-09-23 21:09:54 +02:00
|
|
|
rt, { { rt.x, y }, { rt.x + rt.width, y + (kListRowHeight - 1) } }, FilterPaletteID::paletteDarken2);
|
2020-12-04 18:46:34 +00:00
|
|
|
}
|
|
|
|
|
else if (isHighlighted)
|
|
|
|
|
{
|
2025-10-23 18:44:45 +02:00
|
|
|
Rectangle::filter(
|
2025-09-23 21:09:54 +02:00
|
|
|
rt, { { rt.x, y }, { rt.x + rt.width, y + (kListRowHeight - 1) } }, FilterPaletteID::paletteDarken2);
|
2020-12-04 18:46:34 +00:00
|
|
|
}
|
|
|
|
|
else if (isStriped)
|
|
|
|
|
{
|
2025-10-23 18:44:45 +02:00
|
|
|
Rectangle::fill(
|
2025-05-05 01:06:46 +03:00
|
|
|
rt, { { rt.x, y }, { rt.x + rt.width, y + (kListRowHeight - 1) } },
|
2026-01-13 23:41:59 +01:00
|
|
|
getColourMap(w->colours[1].colour).lighter, true);
|
2020-12-04 18:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Columns
|
2026-02-05 09:14:12 -03:00
|
|
|
if (Columns.empty())
|
2020-12-04 18:46:34 +00:00
|
|
|
{
|
2026-02-05 09:14:12 -03:00
|
|
|
if (!item.Cells.empty())
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2021-01-25 02:32:18 +00:00
|
|
|
const auto& text = item.Cells[0];
|
|
|
|
|
if (!text.empty())
|
|
|
|
|
{
|
2024-04-14 16:37:01 +00:00
|
|
|
ScreenSize cellSize = { std::numeric_limits<int32_t>::max(), kListRowHeight };
|
2025-05-05 01:06:46 +03:00
|
|
|
PaintCell(rt, { 0, y }, cellSize, text.c_str(), isHighlighted);
|
2021-01-25 02:32:18 +00:00
|
|
|
}
|
2020-12-04 18:46:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int32_t x = 0;
|
|
|
|
|
for (size_t j = 0; j < Columns.size(); j++)
|
|
|
|
|
{
|
|
|
|
|
const auto& column = Columns[j];
|
|
|
|
|
if (item.Cells.size() > j)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2020-12-04 18:46:34 +00:00
|
|
|
const auto& text = item.Cells[j];
|
|
|
|
|
if (!text.empty())
|
|
|
|
|
{
|
2024-04-14 16:37:01 +00:00
|
|
|
ScreenSize cellSize = { column.Width, kListRowHeight };
|
2025-05-05 01:06:46 +03:00
|
|
|
PaintCell(rt, { x, y }, cellSize, text.c_str(), isHighlighted);
|
2020-12-04 18:46:34 +00:00
|
|
|
}
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
2020-12-04 18:46:34 +00:00
|
|
|
x += column.Width;
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 16:37:01 +00:00
|
|
|
y += kListRowHeight;
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ShowColumnHeaders)
|
|
|
|
|
{
|
2024-08-11 16:21:53 +02:00
|
|
|
y = scroll->contentOffsetY;
|
2020-05-06 20:39:57 +01:00
|
|
|
|
2026-01-13 23:41:59 +01:00
|
|
|
auto bgColour = getColourMap(w->colours[1].colour).midLight;
|
2025-10-23 18:44:45 +02:00
|
|
|
Rectangle::fill(rt, { { rt.x, y }, { rt.x + rt.width, y + 12 } }, bgColour);
|
2020-05-06 20:39:57 +01:00
|
|
|
|
|
|
|
|
int32_t x = 0;
|
2020-05-06 23:09:31 +01:00
|
|
|
for (int32_t j = 0; j < static_cast<int32_t>(Columns.size()); j++)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
const auto& column = Columns[j];
|
|
|
|
|
auto columnWidth = column.Width;
|
|
|
|
|
if (columnWidth != 0)
|
|
|
|
|
{
|
2026-08-05 23:05:20 +02:00
|
|
|
auto sortOrder = ColumnSortOrder::none;
|
2020-05-06 20:39:57 +01:00
|
|
|
if (CurrentSortColumn == j)
|
|
|
|
|
{
|
|
|
|
|
sortOrder = CurrentSortOrder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isPressed = ColumnHeaderPressed == j && ColumnHeaderPressedCurrentState;
|
2025-05-05 01:06:46 +03:00
|
|
|
PaintHeading(w, rt, { x, y }, { column.Width, kListRowHeight }, column.Header, sortOrder, isPressed);
|
2020-05-06 20:39:57 +01:00
|
|
|
x += columnWidth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CustomListView::PaintHeading(
|
2025-05-05 01:06:46 +03:00
|
|
|
WindowBase* w, RenderTarget& rt, const ScreenCoordsXY& pos, const ScreenSize& size, const std::string& text,
|
2020-05-06 20:39:57 +01:00
|
|
|
ColumnSortOrder sortOrder, bool isPressed) const
|
|
|
|
|
{
|
2025-10-23 18:44:45 +02:00
|
|
|
auto borderStyle = Rectangle::BorderStyle::outset;
|
2020-05-06 20:39:57 +01:00
|
|
|
if (isPressed)
|
|
|
|
|
{
|
2025-10-23 18:44:45 +02:00
|
|
|
borderStyle = Rectangle::BorderStyle::inset;
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
2025-10-23 18:44:45 +02:00
|
|
|
Rectangle::fillInset(rt, { pos, pos + ScreenCoordsXY{ size.width - 1, size.height - 1 } }, w->colours[1], borderStyle);
|
2020-05-06 20:39:57 +01:00
|
|
|
if (!text.empty())
|
|
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
PaintCell(rt, pos, size, text.c_str(), false);
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-08-05 23:05:20 +02:00
|
|
|
if (sortOrder == ColumnSortOrder::ascending)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2020-09-28 01:09:08 +01:00
|
|
|
auto ft = Formatter();
|
2022-07-31 14:22:58 +02:00
|
|
|
ft.Add<StringId>(STR_UP);
|
2026-03-22 18:59:06 +01:00
|
|
|
drawText(rt, pos + ScreenCoordsXY{ size.width - 1, 0 }, STR_BLACK_STRING, ft, { TextAlignment::right });
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
2026-08-05 23:05:20 +02:00
|
|
|
else if (sortOrder == ColumnSortOrder::descending)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2020-09-28 01:09:08 +01:00
|
|
|
auto ft = Formatter();
|
2022-07-31 14:22:58 +02:00
|
|
|
ft.Add<StringId>(STR_DOWN);
|
2026-03-22 18:59:06 +01:00
|
|
|
drawText(rt, pos + ScreenCoordsXY{ size.width - 1, 0 }, STR_BLACK_STRING, ft, { TextAlignment::right });
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-28 00:33:05 +02:00
|
|
|
void CustomListView::PaintSeparator(RenderTarget& rt, const ScreenCoordsXY& pos, const ScreenSize& size, const char* text) const
|
2020-12-04 18:46:34 +00:00
|
|
|
{
|
|
|
|
|
auto hasText = text != nullptr && text[0] != '\0';
|
|
|
|
|
auto left = pos.x + 4;
|
|
|
|
|
auto right = pos.x + size.width - 4;
|
|
|
|
|
auto centreX = size.width / 2;
|
|
|
|
|
auto lineY0 = pos.y + 5;
|
|
|
|
|
auto lineY1 = lineY0 + 1;
|
|
|
|
|
|
|
|
|
|
auto baseColour = ParentWindow->colours[1];
|
2026-01-13 23:41:59 +01:00
|
|
|
auto lightColour = getColourMap(baseColour.colour).lighter;
|
|
|
|
|
auto darkColour = getColourMap(baseColour.colour).midDark;
|
2020-12-04 18:46:34 +00:00
|
|
|
|
|
|
|
|
if (hasText)
|
|
|
|
|
{
|
|
|
|
|
// Draw string
|
2026-03-26 16:23:03 +01:00
|
|
|
drawText(rt, { centreX, pos.y }, text, { baseColour, TextAlignment::centre });
|
2020-12-04 18:46:34 +00:00
|
|
|
|
|
|
|
|
// Get string dimensions
|
2026-03-26 16:23:03 +01:00
|
|
|
int32_t categoryStringHalfWidth = (getStringWidth(text, FontStyle::medium) / 2) + 4;
|
2020-12-04 18:46:34 +00:00
|
|
|
int32_t strLeft = centreX - categoryStringHalfWidth;
|
|
|
|
|
int32_t strRight = centreX + categoryStringHalfWidth;
|
|
|
|
|
|
|
|
|
|
// Draw light horizontal rule
|
|
|
|
|
auto lightLineLeftTop1 = ScreenCoordsXY{ left, lineY0 };
|
|
|
|
|
auto lightLineRightBottom1 = ScreenCoordsXY{ strLeft, lineY0 };
|
2025-05-05 01:06:46 +03:00
|
|
|
GfxDrawLine(rt, { lightLineLeftTop1, lightLineRightBottom1 }, lightColour);
|
2020-12-04 18:46:34 +00:00
|
|
|
|
|
|
|
|
auto lightLineLeftTop2 = ScreenCoordsXY{ strRight, lineY0 };
|
|
|
|
|
auto lightLineRightBottom2 = ScreenCoordsXY{ right, lineY0 };
|
2025-05-05 01:06:46 +03:00
|
|
|
GfxDrawLine(rt, { lightLineLeftTop2, lightLineRightBottom2 }, lightColour);
|
2020-12-04 18:46:34 +00:00
|
|
|
|
|
|
|
|
// Draw dark horizontal rule
|
|
|
|
|
auto darkLineLeftTop1 = ScreenCoordsXY{ left, lineY1 };
|
|
|
|
|
auto darkLineRightBottom1 = ScreenCoordsXY{ strLeft, lineY1 };
|
2025-05-05 01:06:46 +03:00
|
|
|
GfxDrawLine(rt, { darkLineLeftTop1, darkLineRightBottom1 }, darkColour);
|
2020-12-04 18:46:34 +00:00
|
|
|
|
|
|
|
|
auto darkLineLeftTop2 = ScreenCoordsXY{ strRight, lineY1 };
|
|
|
|
|
auto darkLineRightBottom2 = ScreenCoordsXY{ right, lineY1 };
|
2025-05-05 01:06:46 +03:00
|
|
|
GfxDrawLine(rt, { darkLineLeftTop2, darkLineRightBottom2 }, darkColour);
|
2020-12-04 18:46:34 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Draw light horizontal rule
|
|
|
|
|
auto lightLineLeftTop1 = ScreenCoordsXY{ left, lineY0 };
|
|
|
|
|
auto lightLineRightBottom1 = ScreenCoordsXY{ right, lineY0 };
|
2025-05-05 01:06:46 +03:00
|
|
|
GfxDrawLine(rt, { lightLineLeftTop1, lightLineRightBottom1 }, lightColour);
|
2020-12-04 18:46:34 +00:00
|
|
|
|
|
|
|
|
// Draw dark horizontal rule
|
|
|
|
|
auto darkLineLeftTop1 = ScreenCoordsXY{ left, lineY1 };
|
|
|
|
|
auto darkLineRightBottom1 = ScreenCoordsXY{ right, lineY1 };
|
2025-05-05 01:06:46 +03:00
|
|
|
GfxDrawLine(rt, { darkLineLeftTop1, darkLineRightBottom1 }, darkColour);
|
2020-12-04 18:46:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 20:39:57 +01:00
|
|
|
void CustomListView::PaintCell(
|
2025-05-05 01:06:46 +03:00
|
|
|
RenderTarget& rt, const ScreenCoordsXY& pos, const ScreenSize& size, const char* text, bool isHighlighted) const
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
2022-07-31 14:22:58 +02:00
|
|
|
StringId stringId = isHighlighted ? STR_WINDOW_COLOUR_2_STRINGID : STR_BLACK_STRING;
|
2020-05-06 20:39:57 +01:00
|
|
|
|
2020-09-28 01:09:08 +01:00
|
|
|
auto ft = Formatter();
|
2022-07-31 14:22:58 +02:00
|
|
|
ft.Add<StringId>(STR_STRING);
|
2020-05-06 20:39:57 +01:00
|
|
|
ft.Add<const char*>(text);
|
2026-03-26 16:00:06 +01:00
|
|
|
drawTextEllipsised(rt, pos, size.width, stringId, ft);
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<RowColumn> CustomListView::GetItemIndexAt(const ScreenCoordsXY& pos)
|
|
|
|
|
{
|
|
|
|
|
std::optional<RowColumn> result;
|
|
|
|
|
if (pos.x >= 0)
|
|
|
|
|
{
|
|
|
|
|
// Check if we pressed the header
|
2022-07-02 12:34:12 +02:00
|
|
|
auto& scroll = ParentWindow->scrolls[ScrollIndex];
|
2024-08-11 16:21:53 +02:00
|
|
|
int32_t absoluteY = pos.y - scroll.contentOffsetY;
|
2024-04-14 16:37:01 +00:00
|
|
|
if (ShowColumnHeaders && absoluteY >= 0 && absoluteY < kListRowHeight)
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
result = RowColumn();
|
2025-01-19 16:59:45 +01:00
|
|
|
result->Row = kHeaderRow;
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Check what row we pressed
|
2025-01-19 16:59:45 +01:00
|
|
|
int32_t firstY = ShowColumnHeaders ? kColumnHeaderHeight : 0;
|
2024-04-14 16:37:01 +00:00
|
|
|
int32_t row = (pos.y - firstY) / kListRowHeight;
|
2020-05-06 20:39:57 +01:00
|
|
|
if (row >= 0 && row < static_cast<int32_t>(Items.size()))
|
|
|
|
|
{
|
|
|
|
|
result = RowColumn();
|
|
|
|
|
result->Row = static_cast<int32_t>(SortedItems[row]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check what column we pressed if there are any
|
2026-02-05 09:14:12 -03:00
|
|
|
if (result && !Columns.empty())
|
2020-05-06 20:39:57 +01:00
|
|
|
{
|
|
|
|
|
bool found = false;
|
|
|
|
|
int32_t x = 0;
|
|
|
|
|
for (size_t c = 0; c < Columns.size(); c++)
|
|
|
|
|
{
|
|
|
|
|
const auto& column = Columns[c];
|
|
|
|
|
x += column.Width;
|
|
|
|
|
if (column.Width != 0)
|
|
|
|
|
{
|
|
|
|
|
if (pos.x < x)
|
|
|
|
|
{
|
|
|
|
|
result->Column = static_cast<int32_t>(c);
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found)
|
|
|
|
|
{
|
|
|
|
|
// Past all columns
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 14:29:41 +01:00
|
|
|
OpenRCT2::Widget* CustomListView::GetWidget() const
|
2020-05-16 02:05:00 +01:00
|
|
|
{
|
|
|
|
|
size_t scrollIndex = 0;
|
2025-01-18 14:45:17 +02:00
|
|
|
for (WidgetIndex widgetIndex = 0; widgetIndex < ParentWindow->widgets.size(); widgetIndex++)
|
2020-05-16 02:05:00 +01:00
|
|
|
{
|
2025-01-18 14:45:17 +02:00
|
|
|
auto& widget = ParentWindow->widgets[widgetIndex];
|
2025-06-10 19:55:12 +02:00
|
|
|
if (widget.type == WidgetType::scroll)
|
2020-05-16 02:05:00 +01:00
|
|
|
{
|
|
|
|
|
if (scrollIndex == ScrollIndex)
|
|
|
|
|
{
|
2025-01-18 14:45:17 +02:00
|
|
|
return &widget;
|
2020-05-16 02:05:00 +01:00
|
|
|
}
|
|
|
|
|
scrollIndex++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 21:03:21 +01:00
|
|
|
void CustomListView::Invalidate()
|
|
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
ParentWindow->invalidate();
|
2020-05-12 21:03:21 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-06 20:39:57 +01:00
|
|
|
#endif
|