fix issue #113 - TableLayout FILL_PARENT support

This commit is contained in:
Vadim Lopatin 2016-04-18 10:49:03 +03:00
parent 234693e408
commit 8b28219174
2 changed files with 70 additions and 10 deletions

View File

@ -20,19 +20,22 @@ extern (C) int UIAppMain(string[] args) {
VerticalLayout { VerticalLayout {
margins: 10 margins: 10
padding: 10 padding: 10
layoutWidth: fill
backgroundColor: "#C0E0E070" // semitransparent yellow background backgroundColor: "#C0E0E070" // semitransparent yellow background
// red bold text with size = 150% of base style size and font face Arial // red bold text with size = 150% of base style size and font face Arial
TextWidget { text: "Hello World example for DlangUI"; textColor: "red"; fontSize: 150%; fontWeight: 800; fontFace: "Arial" } TextWidget { text: "Hello World example for DlangUI"; textColor: "red"; fontSize: 150%; fontWeight: 800; fontFace: "Arial" }
// arrange controls as form - table with two columns // arrange controls as form - table with two columns
TableLayout { TableLayout {
colCount: 2 colCount: 2
layoutWidth: fill
TextWidget { text: "param 1" } TextWidget { text: "param 1" }
EditLine { id: edit1; text: "some text" } EditLine { id: edit1; text: "some text"; layoutWidth: fill }
TextWidget { text: "param 2" } TextWidget { text: "param 2" }
EditLine { id: edit2; text: "some text for param2" } EditLine { id: edit2; text: "some text for param2"; layoutWidth: fill }
TextWidget { text: "some radio buttons" } TextWidget { text: "some radio buttons" }
// arrange some radio buttons vertically // arrange some radio buttons vertically
VerticalLayout { VerticalLayout {
layoutWidth: fill
RadioButton { id: rb1; text: "Item 1" } RadioButton { id: rb1; text: "Item 1" }
RadioButton { id: rb2; text: "Item 2" } RadioButton { id: rb2; text: "Item 2" }
RadioButton { id: rb3; text: "Item 3" } RadioButton { id: rb3; text: "Item 3" }
@ -40,6 +43,7 @@ extern (C) int UIAppMain(string[] args) {
TextWidget { text: "and checkboxes" } TextWidget { text: "and checkboxes" }
// arrange some checkboxes horizontally // arrange some checkboxes horizontally
HorizontalLayout { HorizontalLayout {
layoutWidth: fill
CheckBox { id: cb1; text: "checkbox 1" } CheckBox { id: cb1; text: "checkbox 1" }
CheckBox { id: cb2; text: "checkbox 2" } CheckBox { id: cb2; text: "checkbox 2" }
} }

View File

@ -663,6 +663,8 @@ class TableLayout : WidgetGroupDefaultDrawing {
int col; int col;
int row; int row;
Widget widget; Widget widget;
@property bool layoutWidthFill() { return widget ? widget.layoutWidth == FILL_PARENT : false; }
@property bool layoutHeightFill() { return widget ? widget.layoutHeight == FILL_PARENT : false; }
@property int measuredWidth() { return widget ? widget.measuredWidth : 0; } @property int measuredWidth() { return widget ? widget.measuredWidth : 0; }
@property int measuredHeight() { return widget ? widget.measuredHeight : 0; } @property int measuredHeight() { return widget ? widget.measuredHeight : 0; }
@property int layoutWidth() { return widget ? widget.layoutWidth : 0; } @property int layoutWidth() { return widget ? widget.layoutWidth : 0; }
@ -690,11 +692,15 @@ class TableLayout : WidgetGroupDefaultDrawing {
int minSize; int minSize;
int maxSize; int maxSize;
int size; int size;
bool fill;
void initialize(int index) { void initialize(int index) {
measuredSize = minSize = maxSize = layoutSize = size = 0; measuredSize = minSize = maxSize = layoutSize = size = 0;
fill = false;
this.index = index; this.index = index;
} }
void rowCellMeasured(ref TableLayoutCell cell) { void rowCellMeasured(ref TableLayoutCell cell) {
if (cell.layoutHeightFill)
fill = true;
if (measuredSize < cell.measuredHeight) if (measuredSize < cell.measuredHeight)
measuredSize = cell.measuredHeight; measuredSize = cell.measuredHeight;
if (minSize < cell.minHeight) if (minSize < cell.minHeight)
@ -704,6 +710,8 @@ class TableLayout : WidgetGroupDefaultDrawing {
size = measuredSize; size = measuredSize;
} }
void colCellMeasured(ref TableLayoutCell cell) { void colCellMeasured(ref TableLayoutCell cell) {
if (cell.layoutWidthFill)
fill = true;
if (measuredSize < cell.measuredWidth) if (measuredSize < cell.measuredWidth)
measuredSize = cell.measuredWidth; measuredSize = cell.measuredWidth;
if (minSize < cell.minWidth) if (minSize < cell.minWidth)
@ -720,10 +728,14 @@ class TableLayout : WidgetGroupDefaultDrawing {
protected TableLayoutCell[] _cells; protected TableLayoutCell[] _cells;
protected int colCount; protected int colCount;
protected int rowCount; protected int rowCount;
protected bool layoutWidthFill;
protected bool layoutHeightFill;
void initialize(int cols, int rows) { void initialize(int cols, int rows, bool layoutWidthFill, bool layoutHeightFill) {
colCount = cols; colCount = cols;
rowCount = rows; rowCount = rows;
this.layoutWidthFill = layoutWidthFill;
this.layoutHeightFill = layoutHeightFill;
_cells.length = cols * rows; _cells.length = cols * rows;
_rows.length = rows; _rows.length = rows;
_cols.length = cols; _cols.length = cols;
@ -750,8 +762,8 @@ class TableLayout : WidgetGroupDefaultDrawing {
return _rows[r]; return _rows[r];
} }
Point measure(Widget parent, int cc, int rc, int pwidth, int pheight) { Point measure(Widget parent, int cc, int rc, int pwidth, int pheight, bool layoutWidthFill, bool layoutHeightFill) {
initialize(cc, rc); initialize(cc, rc, layoutWidthFill, layoutHeightFill);
for (int y = 0; y < rc; y++) { for (int y = 0; y < rc; y++) {
for (int x = 0; x < cc; x++) { for (int x = 0; x < cc; x++) {
int index = y * cc + x; int index = y * cc + x;
@ -778,14 +790,58 @@ class TableLayout : WidgetGroupDefaultDrawing {
return Point(totalWidth, totalHeight); return Point(totalWidth, totalHeight);
} }
void layoutRows() { void layoutRows(int parentSize) {
if (layoutHeightFill && rowCount) {
int totalSize = 0;
int fillCount = 0;
for (int y = 0; y < rowCount; y++) {
totalSize += row(y).size;
if (row(y).fill)
fillCount++;
}
int extraSize = parentSize - totalSize;
int resizeCount = fillCount > 0 ? fillCount : rowCount;
int delta = extraSize / resizeCount;
int delta0 = extraSize % resizeCount;
if (extraSize > 0) {
for (int y = 0; y < rowCount; y++) {
if (fillCount == 0 || row(y).fill) {
row(y).size += delta + delta0;
delta0 = 0;
}
}
}
}
} }
void layoutCols() { void layoutCols(int parentSize) {
if (layoutWidthFill) {
int totalSize = 0;
int fillCount = 0;
for (int x = 0; x < colCount; x++) {
totalSize += col(x).size;
if (col(x).fill)
fillCount++;
}
int extraSize = parentSize - totalSize;
int resizeCount = fillCount > 0 ? fillCount : colCount;
int delta = extraSize / resizeCount;
int delta0 = extraSize % resizeCount;
if (extraSize > 0) {
for (int x = 0; x < colCount; x++) {
if (fillCount == 0 || col(x).fill) {
col(x).size += delta + delta0;
delta0 = 0;
}
}
}
}
} }
void layout(Rect rc) { void layout(Rect rc) {
layoutRows(); layoutRows(rc.height);
layoutCols(); layoutCols(rc.width);
int y0 = 0; int y0 = 0;
for (int y = 0; y < rowCount; y++) { for (int y = 0; y < rowCount; y++) {
int x0 = 0; int x0 = 0;
@ -831,7 +887,7 @@ class TableLayout : WidgetGroupDefaultDrawing {
pheight -= m.top + m.bottom + p.top + p.bottom; pheight -= m.top + m.bottom + p.top + p.bottom;
int rc = rowCount; int rc = rowCount;
Point sz = _cells.measure(this, colCount, rc, pwidth, pheight); Point sz = _cells.measure(this, colCount, rc, pwidth, pheight, layoutWidth == FILL_PARENT, layoutHeight == FILL_PARENT);
measuredContent(parentWidth, parentHeight, sz.x, sz.y); measuredContent(parentWidth, parentHeight, sz.x, sz.y);
} }