Skip to content

Commit

Permalink
Fixes #662. Change grid widget to use UIString
Browse files Browse the repository at this point in the history
  • Loading branch information
GrimMaple committed May 18, 2023
1 parent f339555 commit 475e0bb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 38 deletions.
8 changes: 4 additions & 4 deletions examples/example1/src/widgets/controls.d
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ class BasicControls : LinearLayout
gbgrid.layoutHeight(FILL_PARENT);
grid.layoutWidth(FILL_PARENT);
grid.layoutHeight(FILL_PARENT);
foreach (index, month; ["January"d, "February"d, "March"d, "April"d, "May"d, "June"d, "July"d, "August"d, "September"d, "October"d, "November"d, "December"d])
grid.setColTitle(cast(int)index, month);
foreach (index, month; ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"])
grid.setColTitle(cast(int)index, UIString.fromRaw(month));
for (int y = 0; y < 10; y++)
grid.setRowTitle(y, to!dstring(y+1));
grid.setRowTitle(y, UIString.fromRaw(to!string(y+1)));

grid.setColWidth(0, 30.pointsToPixels);
grid.autoFit();
Expand All @@ -118,7 +118,7 @@ class BasicControls : LinearLayout
for (int x = 0; x < 12; x++) {
for (int y = 0; y < 10; y++) {
int n = uniform(0, 10000);
grid.setCellText(x, y, to!dstring("%.2f".format(n / 100.0)));
grid.setCellText(x, y, UIString.fromRaw(to!string("%.2f".format(n / 100.0))));
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/example1/src/widgets/grid.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class GridExample : VerticalLayout
// create sample grid content
for (int y = 0; y < grid.rows; y++) {
for (int x = 0; x < grid.cols; x++) {
grid.setCellText(x, y, "cell("d ~ to!dstring(x + 1) ~ ","d ~ to!dstring(y + 1) ~ ")"d);
grid.setCellText(x, y, UIString.fromRaw("cell(" ~ to!string(x + 1) ~ "," ~ to!string(y + 1) ~ ")"));
}
grid.setRowTitle(y, to!dstring(y + 1));
grid.setRowTitle(y, UIString.fromRaw(to!string(y + 1)));
}
for (int x = 0; x < grid.cols; x++) {
int col = x + 1;
Expand All @@ -39,7 +39,7 @@ class GridExample : VerticalLayout
if (n1)
res ~= n1 + 'A';
res ~= n2 + 'A';
grid.setColTitle(x, res);
grid.setColTitle(x, UIString.fromRaw(res));
}
grid.autoFit();
addChild(grid);
Expand Down
31 changes: 16 additions & 15 deletions src/dlangui/dialogs/filedlg.d
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,14 @@ class FileDialog : Dialog, CustomGridCellAdapter {
}
/// predicate for sorting items - SIZE
static bool compareItemsBySize(ref DirEntry item1, ref DirEntry item2) {
return ((item1.isDir && !item2.isDir)
return ((item1.isDir && !item2.isDir)
|| ((item1.isDir && item2.isDir) && (item1.name < item2.name))
|| ((!item1.isDir && !item2.isDir) && (item1.size < item2.size))
);
}
/// predicate for sorting items - SIZE DESC
static bool compareItemsBySizeDesc(ref DirEntry item1, ref DirEntry item2) {
return ((item1.isDir && !item2.isDir)
return ((item1.isDir && !item2.isDir)
|| ((item1.isDir && item2.isDir) && (item1.name < item2.name))
|| ((!item1.isDir && !item2.isDir) && (item1.size > item2.size))
);
Expand Down Expand Up @@ -387,9 +387,9 @@ class FileDialog : Dialog, CustomGridCellAdapter {
string sz;
string date;
bool d = _entries[i].isDir;
_fileList.setCellText(1, i, toUTF32(fname));
_fileList.setCellText(1, i, UIString.fromRaw(toUTF32(fname)));
if (d) {
_fileList.setCellText(0, i, "folder");
_fileList.setCellText(0, i, UIString.fromRaw("folder"));
if (fname != "..")
date = formatTimestamp(_entries[i]);
} else {
Expand All @@ -401,7 +401,7 @@ class FileDialog : Dialog, CustomGridCellAdapter {
resname = _filetypeIcons[baseName(fname)];
else
resname = "text-plain";
_fileList.setCellText(0, i, toUTF32(resname));
_fileList.setCellText(0, i, UIString.fromRaw(toUTF32(resname)));
double size = double.nan;
try {
size = _entries[i].size;
Expand All @@ -420,8 +420,8 @@ class FileDialog : Dialog, CustomGridCellAdapter {
}
date = formatTimestamp(_entries[i]);
}
_fileList.setCellText(2, i, toUTF32(sz));
_fileList.setCellText(3, i, toUTF32(date));
_fileList.setCellText(2, i, UIString.fromRaw(toUTF32(sz)));
_fileList.setCellText(3, i, UIString.fromRaw(toUTF32(date)));
}
if(_fileList.height > 0)
_fileList.scrollTo(0, 0);
Expand Down Expand Up @@ -719,7 +719,7 @@ class FileDialog : Dialog, CustomGridCellAdapter {
override void initialize() {
// remember filename specified by user, file grid initialization can change it
string defaultFilename = _filename;

_roots = getRootPaths() ~ getBookmarkPaths();

layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT).minWidth(WIDGET_STYLE_CONSOLE ? 50 : 600);
Expand Down Expand Up @@ -783,7 +783,7 @@ class FileDialog : Dialog, CustomGridCellAdapter {
_fileList.fullColumnOnLeft(false);
_fileList.fullRowOnTop(false);
_fileList.resize(4, 3);
_fileList.setColTitle(0, " "d);
_fileList.setColTitle(0, UIString.fromRaw(" "));
updateColumnHeaders();
_fileList.showRowHeaders = false;
_fileList.rowSelect = true;
Expand Down Expand Up @@ -834,18 +834,19 @@ class FileDialog : Dialog, CustomGridCellAdapter {
}

/// get sort order suffix for column title
protected dstring appendSortOrderSuffix(dstring columnName, FileListSortOrder arrowUp, FileListSortOrder arrowDown) {
protected UIString appendSortOrderSuffix(UIString columnName, FileListSortOrder arrowUp, FileListSortOrder arrowDown)
{
if (_sortOrder == arrowUp)
return columnName ~ "";
return UIString.fromRaw(columnName ~ UIString.fromRaw(""d));
if (_sortOrder == arrowDown)
return columnName ~ "";
return UIString.fromRaw(columnName ~ UIString.fromRaw(""d));
return columnName;
}

protected void updateColumnHeaders() {
_fileList.setColTitle(1, appendSortOrderSuffix(UIString.fromId("COL_NAME"c).value, FileListSortOrder.NAME_DESC, FileListSortOrder.NAME));
_fileList.setColTitle(2, appendSortOrderSuffix(UIString.fromId("COL_SIZE"c).value, FileListSortOrder.SIZE_DESC, FileListSortOrder.SIZE));
_fileList.setColTitle(3, appendSortOrderSuffix(UIString.fromId("COL_MODIFIED"c).value, FileListSortOrder.TIMESTAMP_DESC, FileListSortOrder.TIMESTAMP));
_fileList.setColTitle(1, appendSortOrderSuffix(UIString.fromId("COL_NAME"c), FileListSortOrder.NAME_DESC, FileListSortOrder.NAME));
_fileList.setColTitle(2, appendSortOrderSuffix(UIString.fromId("COL_SIZE"c), FileListSortOrder.SIZE_DESC, FileListSortOrder.SIZE));
_fileList.setColTitle(3, appendSortOrderSuffix(UIString.fromId("COL_MODIFIED"c), FileListSortOrder.TIMESTAMP_DESC, FileListSortOrder.TIMESTAMP));
}

protected void onHeaderCellClicked(GridWidgetBase source, int col, int row) {
Expand Down
51 changes: 35 additions & 16 deletions src/dlangui/widgets/grid.d
Original file line number Diff line number Diff line change
Expand Up @@ -1828,17 +1828,17 @@ class StringGridWidgetBase : GridWidgetBase {
super(ID);
}
/// get cell text
abstract dstring cellText(int col, int row);
abstract UIString cellText(int col, int row);
/// set cell text
abstract StringGridWidgetBase setCellText(int col, int row, dstring text);
abstract StringGridWidgetBase setCellText(int col, int row, UIString text);
/// returns row header title
abstract dstring rowTitle(int row);
abstract UIString rowTitle(int row);
/// set row header title
abstract StringGridWidgetBase setRowTitle(int row, dstring title);
abstract StringGridWidgetBase setRowTitle(int row, UIString title);
/// returns row header title
abstract dstring colTitle(int col);
abstract UIString colTitle(int col);
/// set col header title
abstract StringGridWidgetBase setColTitle(int col, dstring title);
abstract StringGridWidgetBase setColTitle(int col, UIString title);

///// selected column
//@property override int col() { return _col - _headerCols; }
Expand All @@ -1865,9 +1865,9 @@ class StringGridWidgetBase : GridWidgetBase {
*/
class StringGridWidget : StringGridWidgetBase {

protected dstring[][] _data;
protected dstring[] _rowTitles;
protected dstring[] _colTitles;
protected UIString[][] _data;
protected UIString[] _rowTitles;
protected UIString[] _colTitles;

/// empty parameter list constructor - for usage by factory
this() {
Expand All @@ -1881,14 +1881,21 @@ class StringGridWidget : StringGridWidgetBase {
}

/// get cell text
override dstring cellText(int col, int row) {
override UIString cellText(int col, int row) {
if (col >= 0 && col < cols && row >= 0 && row < rows)
return _data[row][col];
return ""d;
return UIString.fromRaw("");
}

/// set cell text
override StringGridWidgetBase setCellText(int col, int row, dstring text) {
override StringGridWidgetBase setCellText(int col, int row, UIString text) {
if (col >= 0 && col < cols && row >= 0 && row < rows)
_data[row][col] = text;
return this;
}

deprecated("This overload is deprecated, use the `UIString` overload instead")
StringGridWidgetBase setCellText(int col, int row, dstring text) {
if (col >= 0 && col < cols && row >= 0 && row < rows)
_data[row][col] = text;
return this;
Expand All @@ -1909,22 +1916,34 @@ class StringGridWidget : StringGridWidgetBase {
}

/// returns row header title
override dstring rowTitle(int row) {
override UIString rowTitle(int row) {
return _rowTitles[row];
}
/// set row header title
override StringGridWidgetBase setRowTitle(int row, dstring title) {
override StringGridWidgetBase setRowTitle(int row, UIString title) {
_rowTitles[row] = title;
return this;
}

deprecated("This overload is deprecated, use the `UIString` overload instead")
StringGridWidgetBase setRowTitle(int row, dstring title) {
_rowTitles[row] = title;
return this;
}

/// returns row header title
override dstring colTitle(int col) {
override UIString colTitle(int col) {
return _colTitles[col];
}

/// set col header title
override StringGridWidgetBase setColTitle(int col, dstring title) {
override StringGridWidgetBase setColTitle(int col, UIString title) {
_colTitles[col] = title;
return this;
}

deprecated("This overload is deprecated, use the `UIString` overload instead")
StringGridWidgetBase setColTitle(int col, dstring title) {
_colTitles[col] = title;
return this;
}
Expand Down

0 comments on commit 475e0bb

Please sign in to comment.