TreeGridCell.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //---------------------------------------------------------------------
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
  6. //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  8. //PARTICULAR PURPOSE.
  9. //---------------------------------------------------------------------
  10. using System.Drawing;
  11. using System.Windows.Forms;
  12. using System.Threading;
  13. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  14. {
  15. /// <summary>
  16. /// Summary description for TreeGridCell.
  17. /// </summary>
  18. public class TreeGridCell : DataGridViewTextBoxCell
  19. {
  20. private const int INDENT_WIDTH = 20;
  21. private const int INDENT_MARGIN = 5;
  22. private int glyphWidth;
  23. private int calculatedLeftPadding;
  24. internal bool IsSited;
  25. private Padding _previousPadding;
  26. private int _imageWidth = 0;
  27. private int _imageHeight = 0;
  28. private Image _image1;
  29. private Image _image2;
  30. public TreeGridCell()
  31. {
  32. glyphWidth = 15;
  33. calculatedLeftPadding = 0;
  34. this.IsSited = false;
  35. //_image1 = (Image)System.Resources.UpgradeReport_Minus;
  36. //_image2 = (Image)Resources.UpgradeReport_Plus;
  37. }
  38. public override object Clone()
  39. {
  40. TreeGridCell c = (TreeGridCell)base.Clone();
  41. c.glyphWidth = this.glyphWidth;
  42. c.calculatedLeftPadding = this.calculatedLeftPadding;
  43. return c;
  44. }
  45. internal protected virtual void UnSited()
  46. {
  47. // The row this cell is in is being removed from the grid.
  48. this.IsSited = false;
  49. this.Style.Padding = this._previousPadding;
  50. }
  51. internal protected virtual void Sited()
  52. {
  53. // when we are added to the DGV we can realize our style
  54. this.IsSited = true;
  55. // remember what the previous padding size is so it can be restored when unsiting
  56. this._previousPadding = this.Style.Padding;
  57. this.UpdateStyle();
  58. }
  59. internal protected virtual void UpdateStyle()
  60. {
  61. // styles shouldn't be modified when we are not sited.
  62. if (this.IsSited == false) return;
  63. int level = this.Level;
  64. Padding p = this._previousPadding;
  65. Size preferredSize;
  66. using (Graphics g = this.OwningNode._grid.CreateGraphics())
  67. {
  68. preferredSize = this.GetPreferredSize(g, this.InheritedStyle, this.RowIndex, new Size(0, 0));
  69. }
  70. //Image image = this.OwningNode.Image;
  71. //if (image != null)
  72. //{
  73. // // calculate image size
  74. // _imageWidth = image.Width + 2;
  75. // _imageHeight = image.Height + 2;
  76. //}
  77. //else
  78. //{
  79. // _imageWidth = glyphWidth;
  80. // _imageHeight = 0;
  81. //}
  82. // TODO: Make this cleaner
  83. if (preferredSize.Height < _imageHeight)
  84. {
  85. this.Style.Padding = new Padding(p.Left + (level * INDENT_WIDTH) + _imageWidth + INDENT_MARGIN,
  86. p.Top + (_imageHeight / 2), p.Right, p.Bottom + (_imageHeight / 2));
  87. }
  88. else
  89. {
  90. this.Style.Padding = new Padding(p.Left + (level * INDENT_WIDTH) + _imageWidth + INDENT_MARGIN,
  91. p.Top, p.Right, p.Bottom);
  92. }
  93. calculatedLeftPadding = ((level - 1) * glyphWidth) + _imageWidth + INDENT_MARGIN;
  94. }
  95. public int Level
  96. {
  97. get
  98. {
  99. TreeGridNode row = this.OwningNode;
  100. if (row != null)
  101. {
  102. return row.Level;
  103. }
  104. else
  105. return -1;
  106. }
  107. }
  108. protected virtual int GlyphMargin
  109. {
  110. get
  111. {
  112. return ((this.Level - 1) * INDENT_WIDTH) + INDENT_MARGIN;
  113. }
  114. }
  115. protected virtual int GlyphOffset
  116. {
  117. get
  118. {
  119. return ((this.Level - 1) * INDENT_WIDTH);
  120. }
  121. }
  122. //protected override void Paint(Graphics graphics, Rectangle clipBounds,
  123. // Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
  124. // object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
  125. // DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
  126. //{
  127. // TreeGridNode node = this.OwningNode;
  128. // if (node == null) return;
  129. // //Image image = node.Image;
  130. // if (this._imageHeight == 0 && image != null) this.UpdateStyle();
  131. // // paint the cell normally
  132. // base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
  133. // formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
  134. // // TODO: Indent width needs to take image size into account
  135. // Rectangle glyphRect = new Rectangle(cellBounds.X + this.GlyphMargin,
  136. // cellBounds.Y, INDENT_WIDTH, cellBounds.Height - 1);
  137. // int glyphHalf = glyphRect.Width / 2;
  138. // //TODO: This painting code needs to be rehashed to be cleaner
  139. // int level = this.Level;
  140. // // Paint tree lines
  141. // if (node._grid != null && node._grid.ShowLines)
  142. // {
  143. // using (Pen linePen = new Pen(SystemBrushes.ControlDark, 1.0f))
  144. // {
  145. // linePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
  146. // bool isLastSibling = node.IsLastSibling;
  147. // bool isFirstSibling = node.IsFirstSibling;
  148. // if (node.Level == 1)
  149. // {
  150. // // the Root nodes display their lines differently
  151. // if (isFirstSibling && isLastSibling)
  152. // {
  153. // // only node, both first and last. Just draw horizontal line
  154. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top
  155. // + cellBounds.Height / 2, glyphRect.Right, cellBounds.Top + cellBounds.Height / 2);
  156. // }
  157. // else if (isLastSibling)
  158. // {
  159. // // last sibling doesn't draw the line extended below. Paint horizontal then vertical
  160. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top
  161. // + cellBounds.Height / 2, glyphRect.Right, cellBounds.Top + cellBounds.Height / 2);
  162. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top, glyphRect.X + 4,
  163. // cellBounds.Top + cellBounds.Height / 2);
  164. // }
  165. // else if (isFirstSibling)
  166. // {
  167. // // first sibling doesn't draw the line extended above. Paint horizontal then vertical
  168. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top
  169. // + cellBounds.Height / 2, glyphRect.Right, cellBounds.Top + cellBounds.Height / 2);
  170. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top
  171. // + cellBounds.Height / 2, glyphRect.X + 4, cellBounds.Bottom);
  172. // }
  173. // else
  174. // {
  175. // // normal drawing draws extended from top to bottom. Paint horizontal then vertical
  176. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top
  177. // + cellBounds.Height / 2, glyphRect.Right, cellBounds.Top + cellBounds.Height / 2);
  178. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top, glyphRect.X + 4, cellBounds.Bottom);
  179. // }
  180. // }
  181. // else
  182. // {
  183. // if (isLastSibling)
  184. // {
  185. // // last sibling doesn't draw the line extended below. Paint horizontal then vertical
  186. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top
  187. // + cellBounds.Height / 2, glyphRect.Right, cellBounds.Top + cellBounds.Height / 2);
  188. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top,
  189. // glyphRect.X + 4, cellBounds.Top + cellBounds.Height / 2);
  190. // }
  191. // else
  192. // {
  193. // // normal drawing draws extended from top to bottom. Paint horizontal then vertical
  194. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top
  195. // + cellBounds.Height / 2, glyphRect.Right, cellBounds.Top + cellBounds.Height / 2);
  196. // graphics.DrawLine(linePen, glyphRect.X + 4, cellBounds.Top, glyphRect.X + 4, cellBounds.Bottom);
  197. // }
  198. // // paint lines of previous levels to the root
  199. // TreeGridNode previousNode = node.Parent;
  200. // int horizontalStop = (glyphRect.X + 4) - INDENT_WIDTH;
  201. // while (!previousNode.IsRoot)
  202. // {
  203. // if (previousNode.HasChildren && !previousNode.IsLastSibling)
  204. // {
  205. // // paint vertical line
  206. // graphics.DrawLine(linePen, horizontalStop, cellBounds.Top, horizontalStop, cellBounds.Bottom);
  207. // }
  208. // previousNode = previousNode.Parent;
  209. // horizontalStop = horizontalStop - INDENT_WIDTH;
  210. // }
  211. // }
  212. // }
  213. // }
  214. // if (node.HasChildren || (node._grid != null && node._grid.VirtualNodes))
  215. // {
  216. // Point pp = new Point();
  217. // //Paint node glyphs
  218. // if (node.IsExpanded)
  219. // {
  220. // pp = new Point(glyphRect.X + this.glyphWidth, cellBounds.Height / 4 + cellBounds.Y);
  221. // graphics.DrawImage(_image1, pp);
  222. // }
  223. // else
  224. // {
  225. // pp = new Point(glyphRect.X + this.glyphWidth, cellBounds.Height / 4 + cellBounds.Y);
  226. // graphics.DrawImage(_image2, pp);
  227. // }
  228. // }
  229. //}
  230. protected override void OnMouseUp(DataGridViewCellMouseEventArgs e)
  231. {
  232. base.OnMouseUp(e);
  233. TreeGridNode node = this.OwningNode;
  234. if (node != null)
  235. node._grid._inExpandCollapseMouseCapture = false;
  236. }
  237. //protected override void OnMouseDown(DataGridViewCellMouseEventArgs e)
  238. //{
  239. // if (e.Location.X > this.InheritedStyle.Padding.Left)
  240. // {
  241. // base.OnMouseDown(e);
  242. // }
  243. // else
  244. // {
  245. // // Expand the node
  246. // //TODO: Calculate more precise location
  247. // TreeGridNode node = this.OwningNode;
  248. // if (node != null)
  249. // {
  250. // node._grid._inExpandCollapseMouseCapture = true;
  251. // if (node.IsExpanded)
  252. // node.Collapse();
  253. // else
  254. // node.Expand();
  255. // }
  256. // }
  257. //}
  258. public TreeGridNode OwningNode
  259. {
  260. get { return base.OwningRow as TreeGridNode; }
  261. }
  262. }
  263. public class TreeGridColumn : DataGridViewTextBoxColumn
  264. {
  265. internal Image _defaultNodeImage;
  266. public TreeGridColumn()
  267. {
  268. this.CellTemplate = new TreeGridCell();
  269. }
  270. // Need to override Clone for design-time support.
  271. public override object Clone()
  272. {
  273. TreeGridColumn c = (TreeGridColumn)base.Clone();
  274. c._defaultNodeImage = this._defaultNodeImage;
  275. return c;
  276. }
  277. public Image DefaultNodeImage
  278. {
  279. get { return _defaultNodeImage; }
  280. set { _defaultNodeImage = value; }
  281. }
  282. }
  283. }