File: table.cpp

package info (click to toggle)
warzone2100 4.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,348 kB
  • sloc: cpp: 675,711; ansic: 387,204; javascript: 75,107; python: 16,628; php: 4,294; sh: 3,941; makefile: 2,330; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (857 lines) | stat: -rw-r--r-- 27,179 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
	This file is part of Warzone 2100.
	Copyright (C) 2020-2021  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
 * @file
 * Functions for scrollable table.
 */

#include "table.h"
#include "form.h"
#include "widgbase.h"
#include "lib/framework/input.h"
#include "lib/ivis_opengl/pieblitfunc.h"

#include <algorithm>
#include <numeric>

#define TABLE_ROW_PADDING 5
#define TABLE_COL_PADDING 10
#define TABLE_COL_HEADER_HEIGHT	(20 + (TABLE_ROW_PADDING * 2))

// MARK: - TableRow

TableRow::TableRow()
: W_BUTTON()
, highlightColor(WZCOL_TRANSPARENT_BOX)
, disabledColor(WZCOL_FORM_DISABLE)
{
	AudioCallback = nullptr;
}

std::shared_ptr<TableRow> TableRow::make(const std::vector<std::shared_ptr<WIDGET>>& _columnWidgets, int rowHeight /*= 0*/)
{
	class make_shared_enabler: public TableRow {};
	auto result = std::make_shared<make_shared_enabler>();

	result->columnWidgets = _columnWidgets;

	if (rowHeight <= 0)
	{
		// expand row height to match maximum height of any contained widget
		rowHeight = 0;
		for (auto& widget : result->columnWidgets)
		{
			rowHeight = std::max(rowHeight, widget->height());
		}
		// vertically center any column widgets that are less than the rowHeight
		for (auto& widget : result->columnWidgets)
		{
			if (widget->height() < rowHeight)
			{
				widget->setGeometry(widget->x(), (rowHeight - widget->height()) / 2, widget->width(), widget->height());
			}
		}
	}
	else
	{
		// use fixed row height
		for (auto& widget : result->columnWidgets)
		{
			widget->setGeometry(widget->x(), 0, widget->width(), rowHeight);
		}
	}
	result->setGeometry(result->x(), result->y(), result->width(), rowHeight);

	for (auto& widget : result->columnWidgets)
	{
		result->attach(widget);
	}

	return result;
}

void TableRow::setHighlightsOnMouseOver(bool value)
{
	highlightsOnMouseOver = value;
}

void TableRow::setHighlightColor(PIELIGHT newHighlightColor)
{
	highlightColor = newHighlightColor;
}

void TableRow::setDrawBorder(optional<PIELIGHT> newBorderColor)
{
	borderColor = newBorderColor;
}

void TableRow::setBackgroundColor(optional<PIELIGHT> newBackgroundColor)
{
	backgroundColor = newBackgroundColor;
}

// Set whether row is "disabled"
void TableRow::setDisabled(bool disabled)
{
	disabledRow = disabled;
	W_BUTTON::setState((disabled) ? WBUT_DISABLE : 0);
}

// Set row disable overlay color
void TableRow::setDisabledColor(PIELIGHT newDisabledColor)
{
	disabledColor = newDisabledColor;
}

int32_t TableRow::getColumnTotalContentIdealWidth()
{
	int32_t totalContentIdealWidth = 0;
	for (auto& column : columnWidgets)
	{
		totalContentIdealWidth += column->idealWidth();
	}
	return totalContentIdealWidth;
}

void TableRow::display(int xOffset, int yOffset)
{
	int x0 = x() + xOffset;
	int y0 = y() + yOffset;
	int x1 = x0 + width();
	int y1 = y0 + height();

	if (highlightsOnMouseOver && isMouseOverRowOrChildren() && !disabledRow)
	{
		pie_UniTransBoxFill(x0, y0, x1, y1, highlightColor);
	}
	else if (backgroundColor.has_value())
	{
		pie_UniTransBoxFill(x0, y0, x1, y1, backgroundColor.value());
	}

	if (borderColor.has_value())
	{
		iV_Box(x0, y0, x1, y1, borderColor.value());
	}
}

void TableRow::displayRecursive(WidgetGraphicsContext const& context)
{
	WIDGET::displayRecursive(context);

	// *after* displaying all children, draw the disabled overlay (if needed)
	if (!disabledRow)
	{
		return;
	}

	if (!context.clipContains(geometry())) {
		return;
	}

	int xOffset = context.getXOffset();
	int yOffset = context.getYOffset();

	int x0 = x() + xOffset;
	int y0 = y() + yOffset;

	pie_UniTransBoxFill(x0, y0, x0 + width(), y0 + height(), disabledColor);
}

bool TableRow::hitTest(int x, int y) const
{
	if (x >= maxDisplayedColumnX1) { return false; }
	return W_BUTTON::hitTest(x, y);
}

std::shared_ptr<WIDGET> TableRow::findMouseTargetRecursive(W_CONTEXT *psContext, WIDGET_KEY key, bool wasPressed)
{
	// if findMouseTargetRecursive was called for this row, it means the mouse is over it
	// (see WIDGET::findMouseTargetRecursive)
	lastFrameMouseIsOverRowOrChildren = frameGetFrameNumber();


	if (disabledRow)
	{
		return shared_from_this();
	}
	return WIDGET::findMouseTargetRecursive(psContext, key, wasPressed);
}

bool TableRow::isMouseOverRowOrChildren() const
{
	if (!lastFrameMouseIsOverRowOrChildren.has_value())
	{
		return false;
	}
	return lastFrameMouseIsOverRowOrChildren.value() == frameGetFrameNumber();
}

void TableRow::resizeColumns(const std::vector<size_t>& newColumnWidths, int columnPadding)
{
	columnPadding = std::max<int>(columnPadding, 0);
	int lastColumnEndX = -columnPadding;
	size_t colIdx = 0;
	for (; colIdx < std::min(newColumnWidths.size(), columnWidgets.size()); colIdx++)
	{
		columnWidgets[colIdx]->setGeometry(lastColumnEndX + columnPadding + columnPadding, columnWidgets[colIdx]->y(), static_cast<int>(newColumnWidths[colIdx]), columnWidgets[colIdx]->height());
		columnWidgets[colIdx]->show();
		lastColumnEndX = (columnWidgets[colIdx]->x() + columnWidgets[colIdx]->width());
	}
	maxDisplayedColumnX1 = lastColumnEndX;
	// hide any additional column widgets
	for (; colIdx < columnWidgets.size(); colIdx++)
	{
		columnWidgets[colIdx]->hide();
	}
}

std::shared_ptr<WIDGET> TableRow::getWidgetAtColumn(size_t col) const
{
	ASSERT_OR_RETURN(nullptr, col < columnWidgets.size(), "Invalid column index: %zu", col);
	return columnWidgets[col];
}

// MARK: - TableHeader

class TableHeader : public W_FORM
{
public:
	TableHeader(const std::shared_ptr<ScrollableTableWidget>& parentTable);
	virtual ~TableHeader() {}

protected:
	virtual void display(int xOffset, int yOffset) override;
	virtual void clicked(W_CONTEXT *, WIDGET_KEY) override;
	virtual void released(W_CONTEXT *, WIDGET_KEY) override;
	virtual void run(W_CONTEXT *psContext) override;
	virtual void geometryChanged() override;
	virtual bool capturesMouseDrag(WIDGET_KEY) override;
	virtual void mouseDragged(WIDGET_KEY, W_CONTEXT *start, W_CONTEXT *current) override;

public:
	void addColumn(const TableColumn& column);
	void changeColumnWidths(const std::vector<size_t>& newColumnWidths);
	bool isUserDraggingColumnHeader() const;
	void setColumnPadding(Vector2i padding);

private:
	int columnWidgetHeight() const;

private:
	bool userResizableHeaders = true;
	Vector2i columnPadding;
	std::vector<TableColumn> columns;
	optional<size_t> colBeingResized = nullopt;
	optional<Vector2i> dragStart = nullopt;
	std::weak_ptr<ScrollableTableWidget> parentTable;
};

TableHeader::TableHeader(const std::shared_ptr<ScrollableTableWidget>& _parentTable)
	: W_FORM()
{
	ASSERT(_parentTable != nullptr, "Null parent table");
	parentTable = _parentTable;
	columnPadding = _parentTable->getColumnPadding();
}

void TableHeader::addColumn(const TableColumn& column)
{
	attach(column.columnWidget);
	// Position it to the right of the prior column (child)
	int columnX = columnPadding.x;
	int colHeaderY = 0;
	if (columns.size() > 0)
	{
		columnX += columns.back().columnWidget->x() + columns.back().columnWidget->width() + columnPadding.x;
	}
	column.columnWidget->setGeometry(columnX, colHeaderY, column.columnWidget->width(), columnWidgetHeight());

	columns.push_back(column);
}

void TableHeader::display(int xOffset, int yOffset)
{
	int x0 = x() + xOffset;
	int y0 = y() + yOffset;
	int x1 = x0 + width();
	int y1 = y0 + height() - 1;
	iV_TransBoxFill(x0, y0, x1, y1);
	iV_Line(x0, y1, x1, y1, WZCOL_MENU_SEPARATOR);

	// draw a line between every column widget
	for (auto& child : columns)
	{
		// column lines
		int xPos = x0 + child.columnWidget->x() + child.columnWidget->width() + columnPadding.x;
		iV_Line(xPos, y0 + 5, xPos, y1 - 5, WZCOL_MENU_SEPARATOR);
	}
}

bool TableHeader::capturesMouseDrag(WIDGET_KEY)
{
	return userResizableHeaders;
}

void TableHeader::mouseDragged(WIDGET_KEY key, W_CONTEXT *start, W_CONTEXT *current)
{
	if (!userResizableHeaders || key != WKEY_PRIMARY || columns.empty())
	{
		return;
	}

	if (!dragStart.has_value())
	{
		dragStart = Vector2i(start->mx, start->my);

		// determine the column that is being resized
		colBeingResized = nullopt;
		for (size_t colIdx = columns.size() - 1; /* termination handled in loop body */; colIdx--)
		{
			auto& columnWidget = columns[colIdx].columnWidget;
			int col_x1 = columnWidget->x() + columnWidget->width();
			if (start->mx > col_x1)
			{
				if (columns[colIdx].resizeBehavior != TableColumn::ResizeBehavior::FIXED_WIDTH)
				{
					colBeingResized = colIdx;
				}
				break;
			}
			if (colIdx == 0)
			{
				break;
			}
		}
		if (!colBeingResized.has_value()) { return; }
	}

	if (dragStart.has_value() && colBeingResized.has_value())
	{
		// handle column resize while dragging
		Vector2i currentMousePos(current->mx, current->my);
		Vector2i dragDelta(currentMousePos.x - dragStart.value().x, currentMousePos.y - dragStart.value().y);

		int priorColumnWidth = columns[colBeingResized.value()].columnWidget->width();
		size_t newProposedColumnWidth = static_cast<size_t>(std::max<int>(priorColumnWidth + dragDelta.x, 0));
		if (auto table = parentTable.lock())
		{
			auto result = table->header_changeColumnWidth(colBeingResized.value(), static_cast<size_t>(newProposedColumnWidth));
			if (result.has_value())
			{
				Vector2i currentDragLogicalPos = dragStart.value();
				currentDragLogicalPos.x += (static_cast<int>(result.value()) - priorColumnWidth);
				dragStart = currentDragLogicalPos;
			}
		}
	}
}

void TableHeader::clicked(W_CONTEXT *psContext, WIDGET_KEY key)
{
	// currently, no-op
}

void TableHeader::released(W_CONTEXT *, WIDGET_KEY)
{
	colBeingResized = nullopt;
	dragStart = nullopt;
}

bool TableHeader::isUserDraggingColumnHeader() const
{
	return userResizableHeaders && dragStart.has_value();
}

void TableHeader::setColumnPadding(Vector2i padding)
{
	columnPadding = padding;

	// Reposition all column widgets
	int lastColumnX = columnPadding.x;
	for (size_t columnIdx = 0; columnIdx < columns.size(); ++columnIdx)
	{
		// Position it to the right of the prior column (child)
		int columnX = lastColumnX;
		int colHeaderY = 0;
		if (columnIdx > 0)
		{
			columnX += columns[columnIdx-1].columnWidget->x() + columns[columnIdx-1].columnWidget->width() + columnPadding.x;
		}
		columns[columnIdx].columnWidget->setGeometry(columnX, colHeaderY, columns[columnIdx].columnWidget->width(), columnWidgetHeight());
	}
}

void TableHeader::run(W_CONTEXT *psContext)
{
	// currently, no-op
}

void TableHeader::geometryChanged()
{
	// resize column header widgets height based on parent TableHeader height
	for(auto& column : columns)
	{
		auto& columnWidget = column.columnWidget;
		columnWidget->setGeometry(columnWidget->x(), columnWidget->y(), columnWidget->width(), columnWidgetHeight());
	}
}

int TableHeader::columnWidgetHeight() const
{
	return height() - 2;
}

void TableHeader::changeColumnWidths(const std::vector<size_t>& newColumnWidths)
{
	int columnX = columnPadding.x;
	size_t i = 0;
	for(; i < std::min(newColumnWidths.size(), columns.size()); i++)
	{
		auto& columnWidget = columns[i].columnWidget;
		columnWidget->setGeometry(columnX, columnWidget->y(), static_cast<int>(newColumnWidths[i]), columnWidget->height());
		columnX += columnPadding.x + columnWidget->width() + columnPadding.x;
	}
	for(; i < columns.size(); i++)
	{
		auto& columnWidget = columns[i].columnWidget;
		columnWidget->setGeometry(columnX, columnWidget->y(), columnWidget->width(), columnWidget->height());
		columnX += columnPadding.x + columnWidget->width() + columnPadding.x;
	}
}

// MARK: - ScrollableTableWidget

ScrollableTableWidget::ScrollableTableWidget()
: WIDGET()
, columnPadding(TABLE_COL_PADDING, 0)
{ }

std::shared_ptr<ScrollableTableWidget> ScrollableTableWidget::make(const std::vector<TableColumn>& columns, int headerHeight /*= -1*/)
{
	class make_shared_enabler: public ScrollableTableWidget {};
	auto result = std::make_shared<make_shared_enabler>();

	// Add table header row
	if (headerHeight <= 0)
	{
		headerHeight = TABLE_COL_HEADER_HEIGHT;
	}
	result->header = std::make_shared<TableHeader>(result);
	result->attach(result->header);
	result->header->setGeometry(0, 0, result->width(), headerHeight); // initialize height
	result->header->setCalcLayout(LAMBDA_CALCLAYOUT_SIMPLE({
		if (auto psParent = psWidget->parent())
		{
			// set width to parent width
			psWidget->setGeometry(0, 0, psParent->width(), psWidget->height());
		}
	}));

	// Add columns
	result->tableColumns = columns;
	for (auto& column : result->tableColumns)
	{
		result->header->addColumn(column);
		result->columnWidths.push_back(column.columnWidget->width());
	}
	result->minColumnWidths.resize(result->tableColumns.size(), 0);

	// Create and add scrollableList
	result->scrollableList = ScrollableListWidget::make();
	result->attach(result->scrollableList);
	result->scrollableList->setBackgroundColor(WZCOL_TRANSPARENT_BOX);
	// Position scrollableList below column headers
	result->scrollableList->setCalcLayout(LAMBDA_CALCLAYOUT_SIMPLE({
		if (auto psParent = std::dynamic_pointer_cast<ScrollableTableWidget>(psWidget->parent()))
		{
			int y0 = 0;
			if (psParent->header->visible())
			{
				y0 = psParent->header->y() + psParent->header->height() + psParent->scrollableList->getItemSpacing();
			}
			psWidget->setGeometry(0, y0, psParent->width(), psParent->height() - y0);
		}
	}));

	return result;
}

void ScrollableTableWidget::geometryChanged()
{
	// Trigger calcLayout on children
	header->callCalcLayout();
	scrollableList->callCalcLayout();
}

int32_t ScrollableTableWidget::idealHeight()
{
	ASSERT_OR_RETURN(0, scrollableList != nullptr, "scrollableList not yet initialized?");
	return scrollableList->y() + scrollableList->idealHeight();
}

void ScrollableTableWidget::setBackgroundColor(PIELIGHT const &color)
{
	scrollableList->setBackgroundColor(color);
}

uint16_t ScrollableTableWidget::getScrollPosition() const
{
	return scrollableList->getScrollPosition();
}

void ScrollableTableWidget::setScrollPosition(uint16_t newPosition)
{
	scrollableList->setScrollPosition(newPosition);
}

void ScrollableTableWidget::addRow(const std::shared_ptr<TableRow> &row)
{
	ASSERT_OR_RETURN(, row != nullptr, "row is null");
	ASSERT_OR_RETURN(, row->numColumns() == columnWidths.size(), "Unexpected number of columns in row: %zu", row->numColumns());
	row->resizeColumns(columnWidths, columnPadding.x);
	// Add row to internal ScrollableListWidget, and rows list
	scrollableList->addItem(row);
	rows.push_back(row);
}

void ScrollableTableWidget::clearRows()
{
	scrollableList->clear();
	rows.clear();
}

void ScrollableTableWidget::setHeaderVisible(bool visible)
{
	if (visible == header->visible())
	{
		return;
	}
	header->show(visible);
	geometryChanged();
}

void ScrollableTableWidget::setRowDisabled(size_t row, bool disabled)
{
	ASSERT_OR_RETURN(, row < rows.size(), "Invalid row idx: %zu", row);
	rows[row]->setDisabled(disabled);
}

bool ScrollableTableWidget::changeColumnWidths(const std::vector<size_t>& newColumnWidths, bool overrideUserColumnResizing /*= false*/)
{
	ASSERT_OR_RETURN(false, newColumnWidths.size() == columnWidths.size(), "newColumnWidths.size (%zu) does not match existing number of columns (%zu)", newColumnWidths.size(), columnWidths.size());
	if (userDidResizeColumnWidths && !overrideUserColumnResizing)
	{
		return false;
	}
	if (!relayoutColumns(newColumnWidths))
	{
		debug(LOG_WZ, "The proposed column widths are not possible to achieve given the table width and layout constraints.");
		return false;
	}
	return true;
}

optional<size_t> ScrollableTableWidget::changeColumnWidth(size_t col, size_t newColumnWidth, bool overrideUserColumnResizing /*= false*/)
{
	ASSERT_OR_RETURN(nullopt, col < columnWidths.size(), "Invalid column index: %zu", col);
	if (userDidResizeColumnWidths && !overrideUserColumnResizing)
	{
		return nullopt;
	}
	auto newProposedColumnWidths = columnWidths;
	newProposedColumnWidths[col] = newColumnWidth;
	if (!relayoutColumns(newProposedColumnWidths, {col}))
	{
		return nullopt;
	}
	return columnWidths[col];
}

// Called specifically from the header to inform the Table that the user has resized the columns
optional<size_t> ScrollableTableWidget::header_changeColumnWidth(size_t col, size_t newColumnWidth)
{
	auto result = changeColumnWidth(col, newColumnWidth, true);
	userDidResizeColumnWidths = true;
	return result;
}

void ScrollableTableWidget::setMinimumColumnWidths(const std::vector<size_t>& newMinColumnWidths)
{
	ASSERT_OR_RETURN(, newMinColumnWidths.size() == columnWidths.size(), "newMinColumnWidths.size (%zu) does not match existing number of columns (%zu)", newMinColumnWidths.size(), columnWidths.size());
	minColumnWidths = newMinColumnWidths;
	relayoutColumns(columnWidths);
}

void ScrollableTableWidget::setMinimumColumnWidth(size_t col, size_t newMinColumnWidth)
{
	ASSERT_OR_RETURN(, col < minColumnWidths.size(), "Invalid column index: %zu", col);
	minColumnWidths[col] = newMinColumnWidth;
	relayoutColumns(columnWidths);
}

// Get the maximum idealWidth() returned by any of the row widgets in the specified column
int32_t ScrollableTableWidget::getColumnMaxContentIdealWidth(size_t col)
{
	int32_t maxIdealWidth = 0;
	for (auto& row : rows)
	{
		auto pColWidget = row->getWidgetAtColumn(col);
		if (!pColWidget) { continue; }
		maxIdealWidth = std::max(maxIdealWidth, pColWidget->idealWidth());
	}
	return maxIdealWidth;
}

std::vector<size_t> ScrollableTableWidget::getShrinkableColumnIndexes(const std::vector<size_t>& currentColumnWidths)
{
	return getColumnIndexes([this, currentColumnWidths](size_t i, const TableColumn& col) {
		if (col.resizeBehavior == TableColumn::ResizeBehavior::FIXED_WIDTH)
		{
			return false;
		}
		if ((std::min(currentColumnWidths.size(), minColumnWidths.size()) > i) && (currentColumnWidths[i] <= minColumnWidths[i]))
		{
			return false;
		}
		return true;
	});
}

std::vector<size_t> ScrollableTableWidget::getExpandToFillColumnIndexes()
{
	return getColumnIndexes([](size_t, const TableColumn& col) {
		return (col.resizeBehavior == TableColumn::ResizeBehavior::RESIZABLE_AUTOEXPAND);
	});
}

void ScrollableTableWidget::updateColumnWidths()
{
	// actually resize header and row columns
	header->changeColumnWidths(columnWidths);
	for (auto& row : rows)
	{
		row->resizeColumns(columnWidths, columnPadding.x);
	}
}

bool ScrollableTableWidget::relayoutColumns(std::vector<size_t> proposedColumnWidths, std::unordered_set<size_t> priorityIndexes)
{
	// respect any minimum column widths (first pass)
	std::vector<size_t> colIndexesIncreasedToMinimumSize;
	for (size_t i = 0; i < std::min(proposedColumnWidths.size(), minColumnWidths.size()); i++)
	{
		if (proposedColumnWidths[i] < minColumnWidths[i])
		{
			proposedColumnWidths[i] = minColumnWidths[i];
			colIndexesIncreasedToMinimumSize.push_back(i);
		}
	}

	auto maxColumnWidthAvailable = getMaxColumnTotalWidth(proposedColumnWidths.size());
	auto totalColWidth = std::accumulate(proposedColumnWidths.begin(), proposedColumnWidths.end(), decltype(proposedColumnWidths)::value_type(0));
	if (totalColWidth > maxColumnWidthAvailable)
	{
		// need to shrink all shrinkable columns to fit
		size_t extraWidth = totalColWidth - maxColumnWidthAvailable;
		std::vector<size_t> allShrinkableColumnIndexes = getShrinkableColumnIndexes(proposedColumnWidths);
		std::vector<size_t> shrinkableColumnIndexes = allShrinkableColumnIndexes;
		shrinkableColumnIndexes.erase(
			std::remove_if(shrinkableColumnIndexes.begin(), shrinkableColumnIndexes.end(),
							[priorityIndexes](const size_t &colIdx) { return priorityIndexes.count(colIdx) > 0; }),
			shrinkableColumnIndexes.end());
		bool ignoringPriorityIndexes = false;
		if (shrinkableColumnIndexes.empty())
		{
			// ignore "priorityIndexes"
			shrinkableColumnIndexes = allShrinkableColumnIndexes;
			ignoringPriorityIndexes = true;
		}
		if (shrinkableColumnIndexes.empty())
		{
			ASSERT(!shrinkableColumnIndexes.empty(), "All columns are fixed width (or non-shrinkable) but desired sizes exceed maxColumnWidthAvailable: %zu", maxColumnWidthAvailable);
			return false;
		}
		auto shrinkColumnWidths = [this, &proposedColumnWidths](std::vector<size_t> shrinkableColumnIndexes, size_t extraWidth) -> size_t {
			size_t currentTotalColumnWidthReduction = 0;
			std::vector<size_t> stillShrinkableColumnIndexes;
			// order shrinkableColumnIndexes by maxColumnWidthReduction for associated column
			std::sort(shrinkableColumnIndexes.begin(), shrinkableColumnIndexes.end(), [this, &proposedColumnWidths](size_t colIndexA, size_t coldIndexB) -> bool {
				size_t maxColumnWidthReductionA = proposedColumnWidths[colIndexA];
				if (minColumnWidths.size() > colIndexA)
				{
					maxColumnWidthReductionA = (proposedColumnWidths[colIndexA] > minColumnWidths[colIndexA]) ? (proposedColumnWidths[colIndexA] - minColumnWidths[colIndexA]) : 0;
				}
				size_t maxColumnWidthReductionB = proposedColumnWidths[coldIndexB];
				if (minColumnWidths.size() > coldIndexB)
				{
					maxColumnWidthReductionB = (proposedColumnWidths[coldIndexB] > minColumnWidths[coldIndexB]) ? (proposedColumnWidths[coldIndexB] - minColumnWidths[coldIndexB]) : 0;
				}
				return maxColumnWidthReductionA < maxColumnWidthReductionB;
			});
			// try to shrink all shrinkable columns proportionally
			for (size_t i = 0; i < shrinkableColumnIndexes.size(); i++)
			{
				size_t colIdx = shrinkableColumnIndexes[i];
				size_t remainingExtraWidth = extraWidth - currentTotalColumnWidthReduction;
				size_t widthReductionAmount = (i < shrinkableColumnIndexes.size()-1) ? (remainingExtraWidth / (shrinkableColumnIndexes.size() - i)) : (remainingExtraWidth);
				size_t maxColumnWidthReduction = proposedColumnWidths[colIdx];
				if (minColumnWidths.size() > colIdx)
				{
					maxColumnWidthReduction = (proposedColumnWidths[colIdx] > minColumnWidths[colIdx]) ? (proposedColumnWidths[colIdx] - minColumnWidths[colIdx]) : 0;
				}
				widthReductionAmount = std::min(widthReductionAmount, maxColumnWidthReduction);
				proposedColumnWidths[colIdx] -= widthReductionAmount;
				currentTotalColumnWidthReduction += widthReductionAmount;
			}
			return currentTotalColumnWidthReduction;
		};
		size_t currentTotalColumnWidthReduction = shrinkColumnWidths(shrinkableColumnIndexes, extraWidth);
		if (currentTotalColumnWidthReduction < extraWidth)
		{
			if (!ignoringPriorityIndexes && !priorityIndexes.empty())
			{
				// One more thing we can do - try to shrink the priority column indexes
				shrinkableColumnIndexes.clear();
				shrinkableColumnIndexes.insert(shrinkableColumnIndexes.end(), priorityIndexes.begin(), priorityIndexes.end());
				currentTotalColumnWidthReduction += shrinkColumnWidths(shrinkableColumnIndexes, (extraWidth - currentTotalColumnWidthReduction));
			}
			if (currentTotalColumnWidthReduction < extraWidth)
			{
				// Unable to shrink other columns - reject this proposed set of column widths
				return false;
			}
		}
		ASSERT(currentTotalColumnWidthReduction == extraWidth, "Logic error");
		totalColWidth = std::accumulate(proposedColumnWidths.begin(), proposedColumnWidths.end(), decltype(proposedColumnWidths)::value_type(0));
	}
	if (totalColWidth < maxColumnWidthAvailable)
	{
		// expand any "expand to fill" columns to proportionally take up the extra space
		auto extraWidth = maxColumnWidthAvailable - totalColWidth;
		std::vector<size_t> expandToFillColumnIndexes = getExpandToFillColumnIndexes();
		size_t currentTotalColumnWidthIncrease = 0;
		for (size_t i = 0; i < expandToFillColumnIndexes.size(); i++)
		{
			size_t colIdx = expandToFillColumnIndexes[i];
			size_t widthIncreaseAmount = (i < expandToFillColumnIndexes.size()-1) ? (extraWidth / expandToFillColumnIndexes.size()) : (extraWidth - currentTotalColumnWidthIncrease);
			proposedColumnWidths[colIdx] += widthIncreaseAmount;
			currentTotalColumnWidthIncrease += widthIncreaseAmount;
		}
	}
	totalColWidth = std::accumulate(proposedColumnWidths.begin(), proposedColumnWidths.end(), decltype(proposedColumnWidths)::value_type(0));

	// store proposed column widths
	columnWidths = proposedColumnWidths;

	// actually resize header and row columns
	updateColumnWidths();

	return true;
}

size_t ScrollableTableWidget::totalPaddingWidthFor(size_t numColumns) const
{
	// TABLE_COL_PADDING is on all sides of all columns
	// also factor in scrollbar width
	size_t totalPaddingWidth = columnPadding.x + ((numColumns - 1) * 2 * columnPadding.x) + columnPadding.x + scrollableList->getScrollbarWidth();
	return totalPaddingWidth;
}

size_t ScrollableTableWidget::getMaxColumnTotalWidth(size_t numColumns) const
{
	return static_cast<size_t>(width()) - totalPaddingWidthFor(numColumns);
}

size_t ScrollableTableWidget::getTableWidthNeededForTotalColumnWidth(size_t numColumns, size_t totalMinimumColumnWidth) const
{
	return totalPaddingWidthFor(numColumns) + totalMinimumColumnWidth;
}

bool ScrollableTableWidget::isUserDraggingColumnHeader() const
{
	return header->isUserDraggingColumnHeader();
}

void ScrollableTableWidget::setColumnPadding(Vector2i newPadding)
{
	if (newPadding == columnPadding)
	{
		return;
	}
	columnPadding = newPadding;

	// Need to inform header + resize columns and also resize columns in list
	header->setColumnPadding(newPadding);
	updateColumnWidths();
}

const Vector2i& ScrollableTableWidget::getColumnPadding()
{
	return columnPadding;
}

void ScrollableTableWidget::setDrawColumnLines(bool bEnabled)
{
	drawColumnLines = bEnabled;
}

void ScrollableTableWidget::setItemSpacing(uint32_t value)
{
	scrollableList->setItemSpacing(value);
}

void ScrollableTableWidget::displayRecursive(WidgetGraphicsContext const& context)
{
	WIDGET::displayRecursive(context);

	// *after* displaying all children, draw the column lines (if desired)
	if (!drawColumnLines)
	{
		return;
	}

	if (!context.clipContains(geometry())) {
		return;
	}

	int xOffset = context.getXOffset();
	int yOffset = context.getYOffset();

	int x0 = x() + xOffset;
	int y0 = y() + yOffset + scrollableList->y();
	int y1 = y0 + scrollableList->height();

	lines.clear();
	lines.reserve(columnWidths.size());

	// draw a line between every column widget
	int lastColumnEndX = -columnPadding.x;
	for (size_t colIdx = 0; colIdx < columnWidths.size(); ++colIdx)
	{
		// column lines
		int columnXPos = lastColumnEndX + columnPadding.x + columnPadding.x;
		int columRightLineXPos = columnXPos + static_cast<int>(columnWidths[colIdx]) + columnPadding.x;

		lines.emplace_back(x0 + columRightLineXPos, y0, x0 + columRightLineXPos, y1);
		lastColumnEndX = columnXPos + static_cast<int>(columnWidths[colIdx]);
	}

	iV_Lines(lines, WZCOL_MENU_SEPARATOR);
}