From 05c33f9e596c2cdf94479992b6c012cd770d57b5 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Fri, 23 May 2014 10:56:10 +0400 Subject: [PATCH] GridWidget, part 1 --- dlangui-monod-lib.dproj | 1 + src/dlangui/widgets/grid.d | 122 +++++++++++++++++++++++++++++++++++ src/dlangui/widgets/widget.d | 9 ++- 3 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 src/dlangui/widgets/grid.d diff --git a/dlangui-monod-lib.dproj b/dlangui-monod-lib.dproj index 0713c5f1..48d956d8 100644 --- a/dlangui-monod-lib.dproj +++ b/dlangui-monod-lib.dproj @@ -102,5 +102,6 @@ + \ No newline at end of file diff --git a/src/dlangui/widgets/grid.d b/src/dlangui/widgets/grid.d new file mode 100644 index 00000000..d9176508 --- /dev/null +++ b/src/dlangui/widgets/grid.d @@ -0,0 +1,122 @@ +module src.dlangui.widgets.grid; + +import dlangui.widgets.widget; + +/** + * Data provider for GridWidget. + */ +interface GridAdapter { + /// number of columns + @property int cols(); + /// number of rows + @property int rows(); + /// returns widget to draw cell at (col, row) + Widget cellWidget(int col, int row); + /// returns row header widget, null if no header + Widget rowHeader(int row); + /// returns column header widget, null if no header + Widget colHeader(int col); +} + +/** + */ +class StringGridAdapter : GridAdapter { + protected int _cols; + protected int _rows; + protected dstring[][] _data; + protected dstring[] _rowTitles; + protected dstring[] _colTitles; + /// number of columns + @property int cols() { return _cols; } + /// number of columns + @property void cols(int v) { resize(v, _rows); } + /// number of rows + @property int rows() { return _rows; } + /// number of columns + @property void rows(int v) { resize(_cols, v); } + + /// returns row header title + dstring rowTitle(int row) { + return _rowTitles[row]; + } + /// set row header title + StringGridAdapter setRowTitle(int row, dstring title) { + _rowTitles[row] = title; + return this; + } + /// returns row header title + dstring colTitle(int col) { + return _colTitles[col]; + } + /// set col header title + StringGridAdapter setColTitle(int col, dstring title) { + _colTitles[col] = title; + return this; + } + /// get cell text + dstring cellText(int col, int row) { + return _data[row][col]; + } + /// set cell text + StringGridAdapter setCellText(int col, int row, dstring text) { + _data[row][col] = text; + return this; + } + /// set new size + void resize(int cols, int rows) { + if (cols == _cols && rows == _rows) + return; + _cols = cols; + _rows = rows; + _data.length = _rows; + for (int y = 0; y < _rows; y++) + _data[y].length = _cols; + _colTitles.length = _cols; + _rowTitles.length = _rows; + } + /// returns widget to draw cell at (col, row) + Widget cellWidget(int col, int row) { return null; } + /// returns row header widget, null if no header + Widget rowHeader(int row) { return null; } + /// returns column header widget, null if no header + Widget colHeader(int col) { return null; } +} + +/** + * Multicolumn multirow view. + */ +class GridWidget : WidgetGroup { + protected GridAdapter _adapter; + /// when true, widget owns adapter and must destroy it itself. + protected bool _ownAdapter; + protected int _cols; + protected int _rows; + /// returns current grid adapter + @property GridAdapter adapter() { return _adapter; } + /// sets shared adapter (will not be destroyed on widget destroy) + @property GridWidget adapter(GridAdapter a) { + if (_ownAdapter && _adapter) + destroy(_adapter); + _ownAdapter = false; + _adapter = a; + return this; + } + /// sets own adapter (will be destroyed on widget destroy) + @property GridWidget ownAdapter(GridAdapter a) { + if (_ownAdapter && _adapter) + destroy(_adapter); + _adapter = a; + return this; + } + /// destroy own adapter + ~this() { + if (_ownAdapter && _adapter) + destroy(_adapter); + } +} + +/** + * Grid view with string data shown. All rows are of the same height. + */ +class StringGridWidget : GridWidget { +} diff --git a/src/dlangui/widgets/widget.d b/src/dlangui/widgets/widget.d index 60d8847f..18742806 100644 --- a/src/dlangui/widgets/widget.d +++ b/src/dlangui/widgets/widget.d @@ -37,15 +37,16 @@ module dlangui.widgets.widget; public import dlangui.core.types; public import dlangui.core.events; +public import dlangui.core.i18n; public import dlangui.widgets.styles; + public import dlangui.graphics.drawbuf; public import dlangui.graphics.resources; public import dlangui.graphics.fonts; -public import dlangui.core.i18n; public import dlangui.core.signals; -import dlangui.platforms.common.platform; +public import dlangui.platforms.common.platform; import std.algorithm; @@ -117,6 +118,10 @@ enum CursorType { Hand } +/** + * Base class for all widgets. + * + */ class Widget { /// widget id protected string _id;