TableClipboard.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. namespace FastReport.Table
  6. {
  7. internal class TableClipboard
  8. {
  9. #region Fields
  10. private TableBase table;
  11. private List<TableColumn> columns;
  12. private List<TableRow> rows;
  13. private TableCell[,] cells;
  14. private Size cellsSize;
  15. #endregion
  16. #region Properties
  17. public bool CanPasteColumns
  18. {
  19. get { return columns.Count > 0; }
  20. }
  21. public bool CanPasteRows
  22. {
  23. get { return rows.Count > 0; }
  24. }
  25. public bool CanPasteCells
  26. {
  27. get { return !CanPasteColumns && cells != null; }
  28. }
  29. #endregion
  30. #region Private Methods
  31. private void ClearColumns()
  32. {
  33. columns.Clear();
  34. cells = null;
  35. }
  36. private void ClearRows()
  37. {
  38. rows.Clear();
  39. }
  40. private void CutCells(Rectangle selection, bool remove)
  41. {
  42. cells = new TableCell[selection.Width, selection.Height];
  43. cellsSize = selection.Size;
  44. for (int x = 0; x < selection.Width; x++)
  45. {
  46. for (int y = 0; y < selection.Height; y++)
  47. {
  48. cells[x, y] = table[x + selection.X, y + selection.Y];
  49. if (remove)
  50. table[x + selection.X, y + selection.Y] = new TableCell();
  51. }
  52. }
  53. }
  54. #endregion
  55. #region Public Methods
  56. public void CutColumns(TableColumn[] columns)
  57. {
  58. ClearColumns();
  59. cells = new TableCell[columns.Length, table.RowCount];
  60. for (int x = 0; x < columns.Length; x++)
  61. {
  62. this.columns.Add(columns[x]);
  63. for (int y = 0; y < table.RowCount; y++)
  64. {
  65. cells[x, y] = table[columns[x].Index, y];
  66. }
  67. }
  68. foreach (TableColumn c in columns)
  69. {
  70. table.Columns.Remove(c);
  71. }
  72. }
  73. public void PasteColumns(int index)
  74. {
  75. for (int x = 0; x < columns.Count; x++)
  76. {
  77. table.Columns.Insert(index + x, columns[x]);
  78. for (int y = 0; y < table.RowCount; y++)
  79. {
  80. table[index + x, y] = cells[x, y];
  81. }
  82. }
  83. ClearColumns();
  84. }
  85. public void CutRows(TableRow[] rows)
  86. {
  87. ClearRows();
  88. foreach (TableRow r in rows)
  89. {
  90. table.Rows.Remove(r);
  91. this.rows.Add(r);
  92. }
  93. }
  94. public void PasteRows(int index)
  95. {
  96. for (int i = 0; i < rows.Count; i++)
  97. {
  98. table.Rows.Insert(index + i, rows[i]);
  99. }
  100. ClearRows();
  101. }
  102. public void CutCells(Rectangle selection)
  103. {
  104. ClearColumns();
  105. CutCells(selection, true);
  106. }
  107. public void CopyCells(Rectangle selection)
  108. {
  109. CutCells(selection, false);
  110. }
  111. public void PasteCells(Point newLocation)
  112. {
  113. for (int x = 0; x < cellsSize.Width; x++)
  114. {
  115. for (int y = 0; y < cellsSize.Height; y++)
  116. {
  117. table[x + newLocation.X, y + newLocation.Y] = cells[x, y].Clone();
  118. }
  119. }
  120. }
  121. #endregion
  122. public TableClipboard(TableBase table)
  123. {
  124. this.table = table;
  125. columns = new List<TableColumn>();
  126. rows = new List<TableRow>();
  127. }
  128. }
  129. }