📄 gridlayout.java
字号:
} // The total height is the margin plus border height, plus space between each row, // plus the height of the tallest child in each row. totalHeight = (totalMarginHeight * 2) + ((rows - 1) * verticalSpacing); //Add up the height of each row. for (int i = 0; i < pixelRowHeights.length; i++) { totalHeight = totalHeight + pixelRowHeights[i]; } if (wHint != SWT.DEFAULT) totalWidth = wHint; if (hHint != SWT.DEFAULT) totalHeight = hHint; // The preferred extent is the width and height that will accomodate the grid's controls. return new Point(totalWidth, totalHeight);}protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { Control[] children = composite.getChildren(); int numChildren = children.length; if (numChildren == 0) return new Point(0,0); if (flushCache) { // Cause the grid and its related information to be calculated // again. grid.removeAllElements(); } return computeLayoutSize(composite, wHint, hHint, flushCache);}Point getFirstEmptyCell(int row, int column) { GridData[] rowData = (GridData[]) grid.elementAt(row); while (column < numColumns && rowData[column] != null) { column++; } if (column == numColumns) { row++; column = 0; if (row == grid.size()) { grid.addElement(emptyRow()); } return getFirstEmptyCell(row, column); } return new Point(row, column);}Point getLastEmptyCell(int row, int column) { GridData[] rowData = (GridData[])grid.elementAt(row); while (column < numColumns && rowData[column] == null ) { column++; } return new Point(row, column - 1);} Point getCell(int row, int column, int width, int height) { Point start = getFirstEmptyCell(row, column); Point end = getLastEmptyCell(start.x, start.y); if (end.y + 1 - start.y >= width) return start; GridData[] rowData = (GridData[]) grid.elementAt(start.x); for (int j = start.y; j < end.y + 1; j++) { GridData spacerSpec = new GridData(); spacerSpec.isItemData = false; rowData[j] = spacerSpec; } return getCell(end.x, end.y, width, height);}void createGrid(Composite composite) { int row, column, rowFill, columnFill; Control[] children; GridData spacerSpec; // children = composite.getChildren(); // grid.addElement(emptyRow()); row = 0; column = 0; // Loop through the children and place their associated layout specs in the // grid. Placement occurs left to right, top to bottom (i.e., by row). for (int i = 0; i < children.length; i++) { // Find the first available spot in the grid. Control child = children[i]; GridData spec = (GridData) child.getLayoutData(); if (spec == null) { spec = new GridData(); child.setLayoutData(spec); } spec.hSpan = Math.min(spec.horizontalSpan, numColumns); Point p = getCell(row, column, spec.hSpan, spec.verticalSpan); row = p.x; column = p.y; // The vertical span for the item will be at least 1. If it is > 1, // add other rows to the grid. for (int j = 2; j <= spec.verticalSpan; j++) { if (row + j > grid.size()) { grid.addElement(emptyRow()); } } // Store the layout spec. Also cache the childIndex. NOTE: That we assume the children of a // composite are maintained in the order in which they are created and added to the composite. ((GridData[]) grid.elementAt(row))[column] = spec; spec.childIndex = i; // Put spacers in the grid to account for the item's vertical and horizontal // span. rowFill = spec.verticalSpan - 1; columnFill = spec.hSpan - 1; for (int r = 1; r <= rowFill; r++) { for (int c = 0; c < spec.hSpan; c++) { spacerSpec = new GridData(); spacerSpec.isItemData = false; ((GridData[]) grid.elementAt(row + r))[column + c] = spacerSpec; } } for (int c = 1; c <= columnFill; c++) { for (int r = 0; r < spec.verticalSpan; r++) { spacerSpec = new GridData(); spacerSpec.isItemData = false; ((GridData[]) grid.elementAt(row + r))[column + c] = spacerSpec; } } column = column + spec.hSpan - 1; } // Fill out empty grid cells with spacers. for (int r = row; r < grid.size(); r++) { GridData[] rowData = (GridData[]) grid.elementAt(r); for (int c = 0; c < numColumns; c++) { if (rowData[c] == null) { spacerSpec = new GridData(); spacerSpec.isItemData = false; rowData[c] = spacerSpec; } } }}GridData[] emptyRow() { GridData[] row = new GridData[numColumns]; for (int i = 0; i < numColumns; i++) { row[i] = null;} return row;}protected void layout(Composite composite, boolean flushCache) { int[] columnWidths; int[] rowHeights; int rowSize, rowY, columnX; int compositeWidth, compositeHeight; int excessHorizontal, excessVertical; Control[] children; if (flushCache) { // Cause the grid and its related information to be calculated // again. grid.removeAllElements(); } children = composite.getChildren(); if (children.length == 0) return; // Point extent = computeSize(composite, SWT.DEFAULT, SWT.DEFAULT, flushCache); columnWidths = new int[numColumns]; for (int i = 0; i < pixelColumnWidths.length; i++) { columnWidths[i] = pixelColumnWidths[i]; } rowHeights = new int[grid.size()]; for (int i = 0; i < pixelRowHeights.length; i++) { rowHeights[i] = pixelRowHeights[i]; } int columnWidth = 0; rowSize = Math.max(1, grid.size()); // compositeWidth = extent.x; compositeHeight = extent.y; // Calculate whether or not there is any extra space or not enough space due to a resize // operation. Then allocate/deallocate the space to columns and rows that are expandable. // If a control grabs excess space, its last column or row will be expandable. excessHorizontal = composite.getClientArea().width - compositeWidth; excessVertical = composite.getClientArea().height - compositeHeight; // Allocate/deallocate horizontal space. if (expandableColumns.length != 0) { int excess, remainder, last; int colWidth; excess = excessHorizontal / expandableColumns.length; remainder = excessHorizontal % expandableColumns.length; last = 0; for (int i = 0; i < expandableColumns.length; i++) { int expandableCol = expandableColumns[i]; colWidth = columnWidths[expandableCol]; colWidth = colWidth + excess; columnWidths[expandableCol] = colWidth; last = Math.max(last, expandableCol); } colWidth = columnWidths[last]; colWidth = colWidth + remainder; columnWidths[last] = colWidth; } // Go through all specs in each expandable column and get the maximum specified // widthHint. Use this as the minimumWidth for the column. for (int i = 0; i < expandableColumns.length; i++) { int expandableCol = expandableColumns[i]; int colWidth = columnWidths[expandableCol]; int minWidth = 0; for (int j = 0; j < grid.size(); j++) { GridData[] row = (GridData[]) grid.elementAt(j); GridData spec = row[expandableCol]; if (spec.hSpan == 1) { minWidth = Math.max(minWidth, spec.widthHint); } } columnWidths[expandableCol] = Math.max(colWidth, minWidth); } // Allocate/deallocate vertical space. if (expandableRows.length != 0) { int excess, remainder, last; int rowHeight; excess = excessVertical / expandableRows.length; remainder = excessVertical % expandableRows.length; last = 0; for (int i = 0; i < expandableRows.length; i++) { int expandableRow = expandableRows[i]; rowHeight = rowHeights[expandableRow]; rowHeight = rowHeight + excess; rowHeights[expandableRow] = rowHeight; last = Math.max(last, expandableRow); } rowHeight = rowHeights[last]; rowHeight = rowHeight + remainder; rowHeights[last] = rowHeight; } // Go through all specs in each expandable row and get the maximum specified // heightHint. Use this as the minimumHeight for the row. for (int i = 0; i < expandableRows.length; i++) { int expandableRow = expandableRows[i]; int rowHeight = rowHeights[expandableRow]; int minHeight = 0; GridData[] row = (GridData[]) grid.elementAt(expandableRow); for (int j = 0; j < numColumns; j++) { GridData spec = row[j]; if (spec.verticalSpan == 1) { minHeight = Math.max(minHeight, spec.heightHint); } } rowHeights[expandableRow] = Math.max(rowHeight, minHeight); } // Get the starting x and y. columnX = marginWidth + composite.getClientArea().x; rowY = marginHeight + composite.getClientArea().y; // Layout the control left to right, top to bottom. for (int r = 0; r < rowSize; r++) { int rowHeight = rowHeights[r]; GridData[] row = (GridData[]) grid.elementAt(r); // for (int c = 0; c < row.length; c++) { int spannedWidth = 0, spannedHeight = 0; int hAlign = 0, vAlign = 0; int widgetX = 0, widgetY = 0; int widgetW = 0, widgetH = 0; // GridData spec = (GridData) row[c]; if (makeColumnsEqualWidth) { columnWidth = composite.getClientArea().width - 2 * (marginWidth) - ((numColumns - 1) * horizontalSpacing); columnWidth = columnWidth / numColumns; for (int i = 0; i < columnWidths.length; i++) { columnWidths[i] = columnWidth; } } else { columnWidth = columnWidths[c]; } // spannedWidth = columnWidth; for (int k = 1; k < spec.hSpan; k++) { if ((c + k) <= numColumns) { if (!makeColumnsEqualWidth) { columnWidth = columnWidths[c + k]; } spannedWidth = spannedWidth + columnWidth + horizontalSpacing; } } // spannedHeight = rowHeight; for (int k = 1; k < spec.verticalSpan; k++) { if ((r + k) <= grid.size()) { spannedHeight = spannedHeight + rowHeights[r + k] + verticalSpacing; } } // if (spec.isItemData()) { Control child = children[spec.childIndex]; Point childExtent = child.computeSize(spec.widthHint, spec.heightHint, flushCache); hAlign = spec.horizontalAlignment; widgetX = columnX; // Calculate the x and width values for the control. if (hAlign == GridData.CENTER || hAlign == SWT.CENTER) { widgetX = widgetX + (spannedWidth / 2) - (childExtent.x / 2); } else if (hAlign == GridData.END || hAlign == SWT.END || hAlign == SWT.RIGHT) { widgetX = widgetX + spannedWidth - childExtent.x - spec.horizontalIndent; } else { widgetX = widgetX + spec.horizontalIndent; } if (hAlign == GridData.FILL) { widgetW = spannedWidth - spec.horizontalIndent; widgetX = columnX + spec.horizontalIndent; } else { widgetW = childExtent.x; } // Calculate the y and height values for the control. vAlign = spec.verticalAlignment; widgetY = rowY; if (vAlign == GridData.CENTER || vAlign == SWT.CENTER) { widgetY = widgetY + (spannedHeight / 2) - (childExtent.y / 2); } else if (vAlign == GridData.END || vAlign == SWT.END || vAlign == SWT.BOTTOM) { widgetY = widgetY + spannedHeight - childExtent.y; } if (vAlign == GridData.FILL) { widgetH = spannedHeight; widgetY = rowY; } else { widgetH = childExtent.y; } // Place the control. child.setBounds(widgetX, widgetY, widgetW, widgetH); } // Update the starting x value. columnX = columnX + columnWidths[c] + horizontalSpacing; } // Update the starting y value and since we're starting a new row, reset the starting x value. rowY = rowY + rowHeights[r] + verticalSpacing; columnX = marginWidth + composite.getClientArea().x; }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -