IDynamicGridGridUIComponent.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using sun.tools.tree;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Forms.VisualStyles;
  12. namespace InABox.DynamicGrid;
  13. internal interface IDynamicGridGridUIComponent<T>
  14. {
  15. IList<DynamicColumnBase> ColumnList { get; }
  16. int RowHeight { get; }
  17. }
  18. internal static class DynamicGridGridUIComponentExtension
  19. {
  20. public static Dictionary<int, double> CalculateColumnSizes<T>(this IDynamicGridGridUIComponent<T> component, double width)
  21. {
  22. var fAvailWidth = width - (SystemParameters.VerticalScrollBarWidth);
  23. //if (Data.Rows.Count * (DataGrid.RowHeight + 1) + DataGrid.HeaderRowHeight > height + 0.5F)
  24. //if (height < DataGrid.AutoScroller.VScrollBar.Maximum)
  25. // fAvailWidth -= (SystemParameters.VerticalScrollBarWidth + 0.75);
  26. double fCurWidth = 0.0F;
  27. var NumAutoCols = 0;
  28. var colWidths = new Dictionary<int, double>();
  29. for (var i = 0; i < component.ColumnList.Count; i++)
  30. {
  31. var col = component.ColumnList[i];
  32. if (col is DynamicImageColumn dic)
  33. {
  34. colWidths[i] = component.RowHeight;
  35. fCurWidth += colWidths[i];
  36. }
  37. else if (col is DynamicTextColumn dxc)
  38. {
  39. colWidths[i] = dxc.Width;
  40. if (dxc.Width != 0)
  41. fCurWidth += Math.Max(0.0F, dxc.Width);
  42. else
  43. NumAutoCols++;
  44. }
  45. else if (col is DynamicTemplateColumn dtc)
  46. {
  47. colWidths[i] = dtc.Width;
  48. if (dtc.Width != 0)
  49. fCurWidth += Math.Max(0.0F, dtc.Width);
  50. else
  51. NumAutoCols++;
  52. }
  53. else if (col is DynamicGridColumn dgc)
  54. {
  55. colWidths[i] = dgc.Width;
  56. if (dgc.Width != 0)
  57. fCurWidth += Math.Max(0.0F, dgc.Width);
  58. else
  59. NumAutoCols++;
  60. }
  61. }
  62. if (NumAutoCols > 0)
  63. {
  64. var fAutoWidth = (fAvailWidth - fCurWidth) / NumAutoCols;
  65. if (fAutoWidth < 100)
  66. fAutoWidth = 100;
  67. for (var i = 0; i < component.ColumnList.Count; i++)
  68. if (colWidths[i] == 0)
  69. colWidths[i] = fAutoWidth;
  70. }
  71. return colWidths;
  72. }
  73. public static int DesiredWidth<T>(this IDynamicGridGridUIComponent<T> component)
  74. {
  75. var result = 0;
  76. for (var i = 0; i < component.ColumnList.Count; i++)
  77. {
  78. var col = component.ColumnList[i];
  79. if (col is DynamicActionColumn)
  80. {
  81. result += (int)component.RowHeight;
  82. }
  83. else if (col is DynamicGridColumn)
  84. {
  85. var dgc = (DynamicGridColumn)col;
  86. result += dgc.Width > 0 ? dgc.Width : 300;
  87. }
  88. }
  89. return result;
  90. }
  91. public static bool CreateEditorColumn<T>(this IDynamicGridGridUIComponent<T> component, DynamicGridColumn column,
  92. [NotNullWhen(true)] out IDynamicGridEditorColumn? newcol,
  93. [NotNullWhen(true)] out IProperty? prop)
  94. where T : BaseObject
  95. {
  96. try
  97. {
  98. prop = DatabaseSchema.Property(typeof(T), column.ColumnName);
  99. }
  100. catch (Exception e)
  101. {
  102. Logger.Send(LogType.Error, ClientFactory.UserID,
  103. string.Format("Error constructing Column [{0}] : {1}\n{2}", column.ColumnName, e.Message, e.StackTrace));
  104. prop = null;
  105. }
  106. newcol = null;
  107. if (prop != null)
  108. {
  109. if (prop.Editor is IntegerEditor)
  110. newcol = new DynamicGridIntegerColumn<T>(column);
  111. else if (prop.Editor is CurrencyEditor)
  112. newcol = new DynamicGridCurrencyColumn<T>(column);
  113. else if (prop.Editor is DoubleEditor)
  114. newcol = new DynamicGridDoubleColumn<T>(column);
  115. else if (prop.Editor is DateTimeEditor)
  116. newcol = new DynamicGridDateTimeColumn<T>(column);
  117. else if (prop.Editor is DateEditor)
  118. newcol = new DynamicGridDateColumn<T>(column);
  119. else if (prop.Editor is TimeOfDayEditor)
  120. newcol = new DynamicGridTimeOfDayColumn<T>(column);
  121. else if (prop.Editor is TimestampEditor)
  122. newcol = new DynamicGridTimeStampColumn<T>(column);
  123. else if (prop.Editor is DurationEditor)
  124. newcol = new DynamicGridDurationColumn<T>(column);
  125. else if (prop.Editor is CheckBoxEditor)
  126. newcol = new DynamicGridCheckBoxColumn<T>(column);
  127. else if (prop.Editor is ColorEditor)
  128. newcol = new DynamicGridColorColumn<T>(column, column.Width, component.RowHeight);
  129. else if (prop.Editor is PopupEditor)
  130. newcol = new DynamicGridPopupColumn<T>(column);
  131. else if (prop.Editor is CodePopupEditor)
  132. newcol = new DynamicGridCodePopupColumn<T>(column);
  133. else if (prop.Editor is EnumLookupEditor)
  134. newcol = new DynamicGridEnumLookupColumn<T>(column);
  135. else if (prop.Editor is ComboLookupEditor)
  136. newcol = new DynamicGridComboLookupColumn<T>(column);
  137. else if (prop.Editor is LookupEditor)
  138. newcol = new DynamicGridLookupColumn<T>(column);
  139. else if (prop.Editor is MemoEditor)
  140. newcol = new DynamicGridMemoColumn<T>(column);
  141. else if (prop.Editor is CodeEditor)
  142. newcol = new DynamicGridCodeColumn<T>(column);
  143. else if (prop.Editor is UniqueCodeEditor)
  144. newcol = new DynamicGridUniqueCodeColumn<T>(column);
  145. else if (prop.Editor is TextBoxEditor)
  146. newcol = new DynamicGridTextBoxColumn<T>(column);
  147. return newcol is not null;
  148. }
  149. else
  150. {
  151. return false;
  152. }
  153. }
  154. }