private void SaveRowHtml(StringBuilder sb, GridPanel p, int row, SaveOptions options, string tdth) { if ((options & SaveOptions.VisibleRows) != SaveOptions.None && p.Rows[row].ActualHeight <= 0) { return; } bool formatted = (options & SaveOptions.Formatted) != SaveOptions.None; bool visCols = (options & SaveOptions.VisibleColumns) != SaveOptions.None; for (int i = 0; i < p.Columns.Count; i++) { if (!visCols || p.Columns[i].ActualWidth > 0) { string cellText = GetCellText(p, row, i, formatted, visCols); cellText = HtmlEncode(cellText); string format; if (p.CellType == CellType.ColumnHeader || p.CellType == CellType.Cell) { format = string.Format("c{0}", i); } else { format = string.Format("h{0}", i); } sb.AppendFormat("<{0} class={1}>{2}</{0}>\r\n", tdth, format, cellText); } } }
private static CellRange GetMergedRange(C1FlexGrid grid, GridPanel panel, CellRange rng) { int row = rng.Row; int column = rng.Column; RowCollection rows = panel.Rows; ColumnCollection columns = panel.Columns; CellRange viewRange = panel.ViewRange; Row item = panel.Rows[row]; if (item is NewRowTemplate) { return rng; } object addItem = grid.EditableCollectionView != null ? grid.EditableCollectionView.CurrentAddItem : null; if (addItem != null && Equals(addItem, item.DataItem)) { return rng; } bool isColumnFrozen = columns.IsFrozen(rng.Column); if (isColumnFrozen) { viewRange.Column = 0; viewRange.Column2 = Math.Min(columns.Frozen - 1, columns.Count - 1); } bool isRowFrozen = rows.IsFrozen(rng.Row); if (isRowFrozen) { viewRange.Row = 0; viewRange.Row2 = Math.Min(rows.Frozen - 1, rows.Count - 1); } if (item is GroupRow) { while (rng.Column > 0 && columns[rng.Column - 1].GroupAggregate == Aggregate.None && columns.IsFrozen(rng.Column - 1) == isColumnFrozen) { if (panel[row, rng.Column] == null) { rng.Column = rng.Column - 1; } else { break; } } while (rng.Column2 < panel.Columns.Count - 1 && columns[rng.Column2].GroupAggregate == Aggregate.None && columns[rng.Column2 + 1].GroupAggregate == Aggregate.None && columns.IsFrozen(rng.Column2 + 1) == isColumnFrozen && panel[row, rng.Column2 + 1] == null) { rng.Column2 = rng.Column2 + 1; } return rng; } if (panel.Columns[column].AllowMerging) { bool nonNullableType = panel.Columns[column].DataType.GetNonNullableType() == typeof (bool); object data = panel.GetDataFormatted(row, column); if (nonNullableType || data != null && !Equals(data, string.Empty)) { for (int i = row - 1; i >= viewRange.TopRow && rows.IsFrozen(i) == isRowFrozen && !(rows[i] is GroupRow) && (addItem == null || !Equals(rows[i].DataItem, addItem)) && Equals(data, panel.GetDataFormatted(i, column)); i--) { rng.Row = i; } for (int j = row + 1; j <= viewRange.BottomRow && rows.IsFrozen(j) == isRowFrozen && !(rows[j] is GroupRow) && (addItem == null || !Equals(rows[j].DataItem, addItem)) && Equals(data, panel.GetDataFormatted(j, column)); j++) { rng.Row2 = j; } } } if (rng.IsSingleCell && item.AllowMerging) { object data = panel.GetDataFormatted(rng.Row, rng.Column); if (data != null && !Equals(data, string.Empty)) { for (int k = column - 1; k >= viewRange.LeftColumn && columns.IsFrozen(k) == isColumnFrozen && Equals(data, panel.GetDataFormatted(row, k)); k--) { rng.Column = k; } for (int l = column + 1; l <= viewRange.RightColumn && columns.IsFrozen(l) == isColumnFrozen && Equals(data, panel.GetDataFormatted(row, l)); l++) { rng.Column2 = l; } } } return rng; }
private string GetCellText(GridPanel p, int row, int col, bool formatted, bool visCols) { object o = p[row, col]; if (o != null || !(p.Rows[row] is GroupRow) || p.CellType != CellType.Cell || (visCols || col != 0) && (!visCols || col != p.Columns.FirstVisibleIndex)) { if (o == null && p.CellType == CellType.ColumnHeader && row == 0) { o = GetColumnHeader(col); } if (formatted && (p.CellType == CellType.Cell || p.CellType == CellType.ColumnFooter)) { o = p.Rows[row].GetDataFormatted(p.Columns[col]); } return o == null ? string.Empty : o.ToString(); } GroupRow groupRow = (GroupRow) p.Rows[row]; o = (string) GetGroupHeaderConverter().Convert(null, typeof (string), groupRow, p.Grid.GetCultureInfo()); return o == null ? string.Empty : o.ToString(); }
private void SaveRow(StringBuilder sb, GridPanel p, int row, SaveOptions options, string[] parms) { if ((options & SaveOptions.VisibleRows) != SaveOptions.None && p.Rows[row].ActualHeight <= 0) { return; } int j = 0; bool formatted = (options & SaveOptions.Formatted) != SaveOptions.None; bool visCols = (options & SaveOptions.VisibleColumns) != SaveOptions.None; for (int i = 0; i < p.Columns.Count; i++) { if (!visCols || p.Columns[i].ActualWidth > 0) { if (sb.Length > 0) { sb.Append(parms[0]); } string cellText = GetCellText(p, row, i, formatted, visCols); cellText = cellText.Replace(parms[1], parms[2]); sb.AppendFormat(parms[3], cellText); j++; } } if (j > 0 && sb.Length == 0) { sb.Append(' '); } }
internal HitTestInfo(GridPanel panel, RoutedEventArgs e) { MouseEventArgs = e; Point position = C1InputEventArgs.GetPosition(e, panel); Initialize(panel, position); }
/// <summary> /// Initializes a new instance of a <see cref="T:C1.WPF.FlexGrid.CellEditEventArgs" />. /// </summary> /// <param name="panel"> /// <see cref="T:C1.WPF.FlexGrid.GridPanel" /> that contains the range. /// </param> /// <param name="rng"> /// <see cref="T:C1.WPF.FlexGrid.CellRange" /> represented by this object. /// </param> /// <param name="editor"> /// <see cref="T:System.Windows.FrameworkElement" /> used to edit the cell value. /// </param> /// <param name="cancelEdits">Value that indicates whether the edits are being committed or canceled.</param> public CellEditEventArgs(GridPanel panel, CellRange rng, FrameworkElement editor, bool cancelEdits) : base(panel, rng) { Editor = editor; CancelEdits = cancelEdits; }
private static RowCollection CloneRows(GridPanel panel) { RowCollection rows = new RowCollection(panel, (int) panel.Rows.DefaultSize); using (rows.DeferNotifications()) { foreach (Row row in panel.Rows) { rows.Add(row); row.List = panel.Rows; } } rows.MinSize = panel.Rows.MinSize; return rows; }
/// <summary> /// Gets a <see cref="T:System.Windows.Thickness" /> value that specifies the padding applied between /// the edge of the cell and its content area. /// </summary> /// <param name="grid"> /// <see cref="T:C1.WPF.FlexGrid.C1FlexGrid" /> that owns the cell. /// </param> /// <param name="panel"> /// <see cref="T:C1.WPF.FlexGrid.GridPanel" /> that specifies the part of the grid that contains the cell. /// </param> /// <param name="rng"> /// <see cref="T:C1.WPF.FlexGrid.CellRange" /> that specifies the row and column represented by the cell. /// </param> /// <returns> /// A <see cref="T:System.Windows.Thickness" /> value that specifies the padding applied between /// the edge of the cell and its content area. /// </returns> public virtual Thickness GetCellPadding(C1FlexGrid grid, GridPanel panel, CellRange rng) { Thickness left = _padding; if (grid.Rows.MaxGroupLevel > -1) { GroupRow groupRow = panel.Rows[rng.Row] as GroupRow; if (rng.Column <= panel.Columns.FirstVisibleIndex) { int level = (groupRow != null ? Math.Max(0, groupRow.Level) : grid.Rows.MaxGroupLevel + 1); left.Left = left.Left + grid.TreeIndent*level; } } return left; }
internal void UpdateAggregates(GridPanel p, Column c) { CellRange cellRange = (c == null ? new CellRange(0, 0, 0, Columns.Count - 1) : new CellRange(0, c.Index, 0, c.Index)); if (cellRange.IsValid) { for (int i = 0; i < p.Rows.Count; i++) { GroupRow groupRow = p.Rows[i] as GroupRow; if (groupRow != null) { for (int j = cellRange.LeftColumn; j <= cellRange.RightColumn; j++) { c = Columns[j]; if (c.GroupAggregate != Aggregate.None) { groupRow[c] = null; } } } } } }
private static ColumnCollection CloneColumns(GridPanel panel) { ColumnCollection columns = new ColumnCollection(panel, (int) panel.Columns.DefaultSize); columns.Indent = panel.Columns.Indent; using (columns.DeferNotifications()) { foreach (Column column in panel.Columns) { columns.Add(column); column.List = panel.Columns; } } return columns; }
internal CellRange GetMergedRange(GridPanel panel, CellRange rng) { if (_mergeManager != null) { bool flag = (ReferenceEquals(panel, Cells) && Rows[rng.Row] is GroupRow); AllowMerging allowMerging = AllowMerging; if (allowMerging != AllowMerging.None || flag) { switch (panel.CellType) { case CellType.Cell: { if ((allowMerging & AllowMerging.Cells) == AllowMerging.None && !flag) { return rng; } return _mergeManager.GetMergedRange(this, panel.CellType, rng); } case CellType.ColumnHeader: { if ((allowMerging & AllowMerging.ColumnHeaders) == AllowMerging.None) { return rng; } return _mergeManager.GetMergedRange(this, panel.CellType, rng); } case CellType.RowHeader: { if ((allowMerging & AllowMerging.RowHeaders) == AllowMerging.None) { return rng; } return _mergeManager.GetMergedRange(this, panel.CellType, rng); } case CellType.TopLeft: { if ((allowMerging & AllowMerging.AllHeaders) == AllowMerging.None) { return rng; } return _mergeManager.GetMergedRange(this, panel.CellType, rng); } } } } return rng; }
internal void DisposeCell(GridPanel panel, FrameworkElement cell) { GetCellFactory().DisposeCell(this, panel.CellType, cell); }
internal FrameworkElement CreateCell(GridPanel panel, CellRange rng) { ICellFactory cellFactory = GetCellFactory(); return cellFactory.CreateCell(this, panel.CellType, rng); }
private void ApplyCellStyles(C1FlexGrid grid, GridPanel panel, CellRange rng, Border bdr, bool editing) { Row row = panel.Rows[rng.Row]; Column column = panel.Columns[rng.Column]; CellStyle rowCellStyle = row.CellStyle; CellStyle colCellStyle = column.CellStyle; switch (panel.CellType) { case CellType.Cell: if (row is GroupRow && rng.ColumnSpan > 1) { colCellStyle = null; } break; case CellType.ColumnHeader: colCellStyle = column.HeaderCellStyle; break; case CellType.RowHeader: case CellType.BottomLeft: rowCellStyle = row.HeaderCellStyle; break; case CellType.TopLeft: rowCellStyle = row.HeaderCellStyle; colCellStyle = column.HeaderCellStyle; break; case CellType.ColumnFooter: colCellStyle = column.FooterCellStyle; break; } if (rowCellStyle != null) { rowCellStyle.Apply(bdr); } if (colCellStyle != null) { colCellStyle.Apply(bdr); } if (panel.CellType == CellType.Cell && !editing && grid.ShowErrors && grid.ErrorStyle != null) { string errors = row.GetErrors(column); if (!string.IsNullOrEmpty(errors)) { grid.ErrorStyle.Apply(bdr); SetErrorTip(grid, bdr, errors); } } }
/// <summary> /// Initializes a new instance of a <see cref="T:C1.WPF.FlexGrid.C1FlexGrid" />. /// </summary> public C1FlexGrid() { DefaultStyleKey = typeof (C1FlexGrid); _mouseHandler = new MouseHandler(this); SelectionHandler = new SelectionHandler(this); _editHandler = new EditHandler(this); _mergeManager = new MergeManager(); _addNewHandler = new AddNewHandler(this); _kbdHandler = new KeyboardHandler(this); Cells = new GridPanel(this, CellType.Cell, ROWHEIGHT, COLWIDTH); ColumnHeaders = new GridPanel(this, CellType.ColumnHeader, ROWHEIGHT, COLWIDTH); ColumnHeaders.Columns = Cells.Columns; RowHeaders = new GridPanel(this, CellType.RowHeader, ROWHEIGHT, COLWIDTH); RowHeaders.Rows = Cells.Rows; TopLeftCells = new GridPanel(this, CellType.TopLeft, ROWHEIGHT, COLWIDTH); TopLeftCells.Rows = ColumnHeaders.Rows; TopLeftCells.Columns = RowHeaders.Columns; ColumnFooters = new GridPanel(this, CellType.ColumnFooter, ROWHEIGHT, COLWIDTH); ColumnFooters.Columns = Cells.Columns; BottomLeftCells = new GridPanel(this, CellType.BottomLeft, ROWHEIGHT, COLWIDTH); BottomLeftCells.Rows = ColumnFooters.Rows; BottomLeftCells.Columns = RowHeaders.Columns; _gridPanelArray = new[] {ColumnHeaders, RowHeaders, Cells, TopLeftCells, ColumnFooters, BottomLeftCells}; ICellFactory cellFactory = GetCellFactory(); _botRight = cellFactory.CreateCell(this, CellType.BottomRight, CellRange.Empty); ColumnHeaders.Rows.Add(new Row()); RowHeaders.Columns.Add(new Column()); _canvas = new Canvas(); _canvas.Clip = new RectangleGeometry(); Marquee = new Rectangle(); Marquee.IsHitTestVisible = false; Marquee.StrokeThickness = 2; Marquee.Stroke = Brushes.Black; Marquee.Visibility = Visibility.Collapsed; _canvas.Children.Add(Marquee); _lnFX = new Line(); _lnFX.Visibility = Visibility.Collapsed; _lnFX.StrokeThickness = FROZEN_LINE_WIDTH; _canvas.Children.Add(_lnFX); _lnFY = new Line(); _lnFY.Visibility = Visibility.Collapsed; _lnFY.StrokeThickness = FROZEN_LINE_WIDTH; _canvas.Children.Add(_lnFY); ErrorStyle = new CellStyle(); ErrorStyle.BorderBrush = _errorBorderBrush; ErrorStyle.BorderThickness = new Thickness(1); ErrorStyle.CornerRadius = new CornerRadius(2); Rows.CollectionChanged += (s, e) => { if (ItemsSource == null) { UpdateAggregates(null, false); } }; LayoutUpdated += (s, e) => { if (!ReferenceEquals(Foreground, _lastForeground)) { _lastForeground = Foreground; Invalidate(); return; } if (ViewRange.IsValid && Rows.Count > 0 && Columns.Count > 0 && Cells.IsEmpty() && Rows.GetTotalSize() > 0 && Columns.GetTotalSize() > 0) { InvalidateMeasure(); } }; FocusVisualStyle = null; // bool checkLicense = true; // try // { // string fullName = Assembly.GetCallingAssembly().FullName; // if (fullName.ToLower().StartsWith("c1.wpf.olap") && fullName.EndsWith("PublicKeyToken=2aa4ec5576d6c3ce")) // { // checkLicense = false; // _skipLicenseCheck = true; // } // else if (_skipLicenseCheck) // { // checkLicense = false; // } // } // catch // { // } // if (checkLicense) // { // lock (typeof (C1FlexGrid)) // { // ProviderInfoWPF.Validate(typeof (C1FlexGrid), this, true); // } // } }
private void CreateCellContent(C1FlexGrid grid, GridPanel panel, Border bdr, CellRange rng) { Row row = panel.Rows[rng.Row]; Column col = panel.Columns[rng.Column]; GroupRow groupRow = row as GroupRow; if (row.DataItem != null && col.CellTemplate != null && panel.CellType == CellType.Cell) { bdr.Padding = GetTemplatePadding(bdr.Padding); bdr.Child = GetTemplatedCell(grid, col.CellTemplate); ApplyCellStyles(grid, panel, rng, bdr, false); AddTreeIcon(grid, rng, bdr); return; } Binding binding = row.DataItem != null ? col.Binding : null; object obj = (row.DataItem != null ? null : panel[rng.Row, rng.Column]); bool gridHasChildren = grid.GetChildItemsPropertyInfo() != null; if (groupRow != null && groupRow.Group != null && obj == null && !gridHasChildren && rng.Column <= grid.Columns.FirstVisibleIndex) { binding = new Binding(); binding.Source = groupRow.Group; binding.Converter = grid.GetGroupHeaderConverter(); binding.ConverterParameter = row; } Brush foreground = GetForegroundBrush(grid, row, grid.Foreground); Type dataType = col.DataType; TextBlock tb = null; if ((groupRow == null || gridHasChildren) && panel.CellType == CellType.Cell && (dataType == typeof (bool) || dataType == typeof (bool?))) { CheckBox cb = new CheckBox(); cb.IsThreeState = (dataType == typeof (bool?)); cb.HorizontalAlignment = HorizontalAlignment.Center; cb.VerticalAlignment = VerticalAlignment.Center; cb.IsHitTestVisible = false; cb.IsTabStop = false; if (foreground != null) { cb.Foreground = foreground; } bdr.Child = cb; if (binding == null) { cb.IsChecked = obj as bool?; } else { cb.SetBinding(ToggleButton.IsCheckedProperty, binding); } bdr.MouseLeftButtonDown += bdr_MouseLeftButtonDown; } else { tb = new TextBlock(); tb.VerticalAlignment = VerticalAlignment.Center; if (foreground != null) { tb.Foreground = foreground; } bdr.Child = tb; if (binding != null) { tb.SetBinding(TextBlock.TextProperty, binding); if (AlignRight(row, col, col.DataType)) { tb.HorizontalAlignment = HorizontalAlignment.Right; tb.TextAlignment = TextAlignment.Right; } } else if (obj != null) { tb.Text = row.GetDataFormatted(col); if (AlignRight(row, col, obj.GetType())) { tb.HorizontalAlignment = HorizontalAlignment.Right; tb.TextAlignment = TextAlignment.Right; } } } ApplyCellStyles(grid, panel, rng, bdr, false); if (panel.CellType == CellType.Cell) { AddTreeIcon(grid, rng, bdr); if (tb != null && grid.AllowEditing(rng.Row, rng.Column)) { ColumnValueConverter converter = col.ValueConverter as ColumnValueConverter; if (converter != null && converter.Values != null && converter.Values.Count > 0) { ShowDropDownButton(grid, bdr); } } } }
private void AutoSizeColumns(GridPanel p1, GridPanel p2, int startCol, int endCol, double extra, bool allCells, bool skipStars) { UpdateLayout(); using (p1.Columns.DeferNotifications()) { for (int j = startCol; j <= endCol; j++) { if (!skipStars || !p2.Columns[j].Width.IsStar) { double colSize1 = p1.AutoSizeColumn(j, extra, allCells); double colSize2 = p2.AutoSizeColumn(j, extra, allCells); double colSize = Math.Max(colSize1, colSize2); if (colSize > -1) { p1.Columns[j].Width = new GridLength(colSize); } } } } }
private void Initialize(GridPanel panel, Point pt) { _grid = panel.Grid; Panel = panel; Point = pt; Row = Column = -1; double colFrozenSize = Panel.Columns.GetFrozenSize(); double rowFrozenSize = Panel.Rows.GetFrozenSize(); Point scrollPosition = Panel.ScrollPosition; if (pt.X < 0.0 || pt.X > colFrozenSize) { pt.X = pt.X - scrollPosition.X; } if (pt.Y < 0.0 || pt.Y > rowFrozenSize) { pt.Y = pt.Y - scrollPosition.Y; } Row = Panel.Rows.GetItemAt(pt.Y); Column = Panel.Columns.GetItemAt(pt.X); }
private void AutoSizeRows(GridPanel p1, GridPanel p2, int startRow, int endRow, double extra, bool allCells) { try { UpdateLayout(); } catch { } using (p1.Rows.DeferNotifications()) { for (int i = startRow; i <= endRow; i++) { double rowSize1 = p1.AutoSizeRow(i, extra, allCells); double rowSize2 = p2.AutoSizeRow(i, extra, allCells); double rowSize = Math.Max(rowSize1, rowSize2); if (rowSize > -1) { p1.Rows[i].Height = rowSize; } } } }
private static int GetResizableColumn(GridPanel p, int c) { int column = c; for (int i = c + 1; i < p.Columns.Count; i++) { Column item = p.Columns[i]; if (item.ActualWidth > MOUSE_THRESHOLD) { return column; } if (item.Visible && item.AllowResizing) { column = i; } } return column; }
/// <summary> /// Initializes a new instance of a <see cref="T:C1.WPF.FlexGrid.CellRangeEventArgs" />. /// </summary> /// <param name="panel"> /// <see cref="T:C1.WPF.FlexGrid.GridPanel" /> that contains the range. /// </param> /// <param name="rng"> /// <see cref="P:C1.WPF.FlexGrid.CellRangeEventArgs.CellRange" /> represented by this object. /// </param> public CellRangeEventArgs(GridPanel panel, CellRange rng) { Panel = panel; CellRange = rng; }