Java Reference
In-Depth Information
Next, we add some functions. First, we need a function to walk through all the
nodes and calculate the maximum width for each column and the maximum
height for each row. We also use this function to calculate the overall dimensions
for the GridLayout object. This function is shown in Listing 5.20.
Listing 5.20
Grid Layout Functions - calcDimensions()
function calcDimensions (): Void {
// calculate either rows or cols
if(cols <= 0) {
cols = (sizeof content + rows -1)/rows;
}else {
rows = (sizeof content + cols - 1)/cols;
}
// calculate the colWidths and rowHeights
var index = 0;
colWidths = for(i in [0..<cols]) 0.0;
rowHeights = for(i in [0..<rows]) 0.0;
for(node in content) {
if(node.visible) {
var w = getNodePreferredWidth(node);
var h = getNodePreferredHeight(node);
var row = index / cols;
var col = index mod cols;
if(colWidths[col] < w) colWidths[col] = w;
if(rowHeights[row] < h) rowHeights[row] = h;
index++;
}
}
// merge the provided alignments with the default
delete _hAlignments;
insert horizontalAlignments into _hAlignments;
while(sizeof _hAlignments < cols) {
insert defaultAlignment into _hAlignments;
}
delete _vAlignments;
insert verticalAlignments into _vAlignments;
while(sizeof _vAlignments < rows) {
insert defaultAlignment into _vAlignments;
}
//calculate total width for container
preferredWidth = horizontalMargin;
for(i in [0..<cols]) {
if(i > 0) preferredWidth += horizontalSpacing;
insert preferredWidth into colOffsets;
preferredWidth += colWidths[i];
}
continues
Search WWH ::




Custom Search