public RowColInfo[] Split(double offset) { RowColInfo[] info = new RowColInfo[2]; int newPercent = (int)(Percent * offset / Extent); info[0] = new RowColInfo(newPercent); info[1] = new RowColInfo(Percent - newPercent); return(info); }
private void GridEditor_Loaded(object sender, RoutedEventArgs e) { GridLayoutModel model = (GridLayoutModel)DataContext; if (model != null) { int rows = model.Rows; int cols = model.Columns; _rowInfo = new RowColInfo[rows]; for (int row = 0; row < rows; row++) { _rowInfo[row] = new RowColInfo(model.RowPercents[row]); } _colInfo = new RowColInfo[cols]; for (int col = 0; col < cols; col++) { _colInfo[col] = new RowColInfo(model.ColumnPercents[col]); } int maxIndex = 0; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { maxIndex = Math.Max(maxIndex, model.CellChildMap[row, col]); } } for (int i = 0; i <= maxIndex; i++) { AddZone(); } } Model = model; if (Model == null) { Model = new GridLayoutModel(); DataContext = Model; } Model.PropertyChanged += OnGridDimensionsChanged; AddDragHandles(); }
private void OnSplit(object o, SplitEventArgs e) { UIElementCollection previewChildren = Preview.Children; GridZone splitee = (GridZone)o; int spliteeIndex = previewChildren.IndexOf(splitee); GridLayoutModel model = Model; int rows = model.Rows; int cols = model.Columns; int foundRow = -1; int foundCol = -1; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (model.CellChildMap[row, col] == spliteeIndex) { foundRow = row; foundCol = col; break; } } if (foundRow != -1) { break; } } int newChildIndex = AddZone(); double offset = e.Offset; if (e.Orientation == Orientation.Vertical) { if (splitee.VerticalSnapPoints != null) { offset += Canvas.GetLeft(splitee); int count = splitee.VerticalSnapPoints.Length; bool foundExistingSplit = false; for (int i = 0; i <= count; i++) { if (foundExistingSplit) { int walkRow = foundRow; while ((walkRow < rows) && (model.CellChildMap[walkRow, foundCol + i] == spliteeIndex)) { model.CellChildMap[walkRow++, foundCol + i] = newChildIndex; } } if (_colInfo[foundCol + i].End == offset) { foundExistingSplit = true; // use existing division } } if (foundExistingSplit) { OnGridDimensionsChanged(); return; } while (_colInfo[foundCol].End < offset) { foundCol++; } offset -= _colInfo[foundCol].Start; } AddDragHandle(Orientation.Vertical, cols - 1); cols++; int[,] newCellChildMap = new int[rows, cols]; int[] newColPercents = new int[cols]; RowColInfo[] newColInfo = new RowColInfo[cols]; int sourceCol = 0; for (int col = 0; col < cols; col++) { for (int row = 0; row < rows; row++) { if ((col > foundCol) && (model.CellChildMap[row, sourceCol] == spliteeIndex)) { newCellChildMap[row, col] = newChildIndex; } else { newCellChildMap[row, col] = model.CellChildMap[row, sourceCol]; } } if (col != foundCol) { sourceCol++; } } model.CellChildMap = newCellChildMap; sourceCol = 0; for (int col = 0; col < cols; col++) { if (col == foundCol) { RowColInfo[] split = _colInfo[col].Split(offset); newColPercents[col] = split[0].Percent; newColInfo[col++] = split[0]; newColPercents[col] = split[1].Percent; newColInfo[col] = split[1]; sourceCol++; } else { newColPercents[col] = model.ColumnPercents[sourceCol]; newColInfo[col] = _colInfo[sourceCol++]; } } _colInfo = newColInfo; model.ColumnPercents = newColPercents; model.Columns++; } else { // Horizontal if (splitee.HorizontalSnapPoints != null) { offset += Canvas.GetTop(splitee); int count = splitee.HorizontalSnapPoints.Length; bool foundExistingSplit = false; for (int i = 0; i <= count; i++) { if (foundExistingSplit) { int walkCol = foundCol; while ((walkCol < cols) && (model.CellChildMap[foundRow + i, walkCol] == spliteeIndex)) { model.CellChildMap[foundRow + i, walkCol] = newChildIndex; } } if (_rowInfo[foundRow + i].End == offset) { foundExistingSplit = true; // use existing division } } if (foundExistingSplit) { OnGridDimensionsChanged(); return; } while (_rowInfo[foundRow].End < offset) { foundRow++; } offset -= _rowInfo[foundRow].Start; } AddDragHandle(Orientation.Horizontal, rows - 1); rows++; int[,] newCellChildMap = new int[rows, cols]; int[] newRowPercents = new int[rows]; RowColInfo[] newRowInfo = new RowColInfo[rows]; int sourceRow = 0; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if ((row > foundRow) && (model.CellChildMap[sourceRow, col] == spliteeIndex)) { newCellChildMap[row, col] = newChildIndex; } else { newCellChildMap[row, col] = model.CellChildMap[sourceRow, col]; } } if (row != foundRow) { sourceRow++; } } model.CellChildMap = newCellChildMap; sourceRow = 0; for (int row = 0; row < rows; row++) { if (row == foundRow) { RowColInfo[] split = _rowInfo[row].Split(offset); newRowPercents[row] = split[0].Percent; newRowInfo[row++] = split[0]; newRowPercents[row] = split[1].Percent; newRowInfo[row] = split[1]; sourceRow++; } else { newRowPercents[row] = model.RowPercents[sourceRow]; newRowInfo[row] = _rowInfo[sourceRow++]; } } _rowInfo = newRowInfo; model.RowPercents = newRowPercents; model.Rows++; } }
private void RenderActualScalePriview(GridLayoutModel grid) { int rows = grid.Rows; int cols = grid.Columns; RowColInfo[] rowInfo = new RowColInfo[rows]; for (int row = 0; row < rows; row++) { rowInfo[row] = new RowColInfo(grid.RowPercents[row]); } RowColInfo[] colInfo = new RowColInfo[cols]; for (int col = 0; col < cols; col++) { colInfo[col] = new RowColInfo(grid.ColumnPercents[col]); } Settings settings = ((App)Application.Current).ZoneSettings; int spacing = settings.ShowSpacing ? settings.Spacing : 0; int width = (int)SystemParameters.WorkArea.Width; int height = (int)SystemParameters.WorkArea.Height; double totalWidth = width - (spacing * (cols + 1)); double totalHeight = height - (spacing * (rows + 1)); double top = spacing; for (int row = 0; row < rows; row++) { double cellHeight = rowInfo[row].Recalculate(top, totalHeight); top += cellHeight + spacing; } double left = spacing; for (int col = 0; col < cols; col++) { double cellWidth = colInfo[col].Recalculate(left, totalWidth); left += cellWidth + spacing; } Viewbox viewbox = new Viewbox { Stretch = Stretch.Uniform, }; Body.Children.Add(viewbox); Canvas frame = new Canvas { Width = width, Height = height, }; viewbox.Child = frame; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { int i = grid.CellChildMap[row, col]; if (((row == 0) || (grid.CellChildMap[row - 1, col] != i)) && ((col == 0) || (grid.CellChildMap[row, col - 1] != i))) { Rectangle rect = new Rectangle(); left = colInfo[col].Start; top = rowInfo[row].Start; Canvas.SetTop(rect, top); Canvas.SetLeft(rect, left); int maxRow = row; while (((maxRow + 1) < rows) && (grid.CellChildMap[maxRow + 1, col] == i)) { maxRow++; } int maxCol = col; while (((maxCol + 1) < cols) && (grid.CellChildMap[row, maxCol + 1] == i)) { maxCol++; } rect.Width = colInfo[maxCol].End - left; rect.Height = rowInfo[maxRow].End - top; rect.StrokeThickness = 1; rect.Stroke = Brushes.DarkGray; rect.Fill = Brushes.LightGray; frame.Children.Add(rect); } } } }