TreeNode.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jackson Harper ([email protected])
  24. // Kazuki Oikawa ([email protected])
  25. using System;
  26. using System.Text;
  27. using System.Drawing;
  28. using System.Runtime.Serialization;
  29. namespace System.Windows.Forms {
  30. [Serializable]
  31. public class TreeNode : MarshalByRefObject, ICloneable /*, ISerializable */ {
  32. private TreeView tree_view;
  33. internal TreeNode parent;
  34. private int index;
  35. private string text;
  36. private int image_index = -1;
  37. private int selected_image_index = -1;
  38. internal TreeNodeCollection nodes;
  39. private bool is_expanded = false;
  40. private Rectangle bounds = Rectangle.Empty;
  41. private Rectangle plus_minus_bounds = Rectangle.Empty;
  42. private Rectangle checkbox_bounds = Rectangle.Empty;
  43. private bool check;
  44. internal OwnerDrawPropertyBag prop_bag;
  45. private object tag;
  46. private IntPtr handle;
  47. internal TreeNode (TreeView tree_view) : this ()
  48. {
  49. this.tree_view = tree_view;
  50. }
  51. public TreeNode ()
  52. {
  53. nodes = new TreeNodeCollection (this);
  54. }
  55. public TreeNode (string text) : this ()
  56. {
  57. Text = text;
  58. }
  59. public TreeNode (string text, TreeNode [] children) : this (text)
  60. {
  61. Nodes.AddRange (children);
  62. }
  63. public TreeNode (string text, int image_index, int selected_image_index) : this (text)
  64. {
  65. this.image_index = image_index;
  66. this.selected_image_index = selected_image_index;
  67. }
  68. public TreeNode (string text, int image_index, int selected_image_index,
  69. TreeNode [] children) : this (text, image_index, selected_image_index)
  70. {
  71. Nodes.AddRange (children);
  72. }
  73. internal TreeView TreeView {
  74. get {
  75. if (tree_view != null)
  76. return tree_view;
  77. TreeNode walk = parent;
  78. while (walk != null) {
  79. if (walk.TreeView != null)
  80. tree_view = walk.TreeView;
  81. walk = walk.parent;
  82. }
  83. return tree_view;
  84. }
  85. }
  86. #region ICloneable Members
  87. public object Clone()
  88. {
  89. TreeNode tn = new TreeNode (text, image_index, selected_image_index);
  90. if (nodes != null) {
  91. foreach (TreeNode child in nodes)
  92. tn.Nodes.Add ((TreeNode)child.Clone ());
  93. }
  94. tn.Tag = tag;
  95. tn.Checked = Checked;
  96. if (prop_bag != null)
  97. tn.prop_bag = OwnerDrawPropertyBag.Copy (prop_bag);
  98. return tn;
  99. }
  100. #endregion
  101. public TreeNode Parent {
  102. get {
  103. if (tree_view != null && tree_view.root_node == parent)
  104. return null;
  105. return parent;
  106. }
  107. }
  108. public string Text {
  109. get {
  110. if (text == null)
  111. return String.Empty;
  112. return text;
  113. }
  114. set {
  115. if (text == value)
  116. return;
  117. text = value;
  118. bounds.Width = 0;
  119. }
  120. }
  121. public Rectangle Bounds {
  122. get { return bounds; }
  123. }
  124. internal Rectangle PlusMinusBounds {
  125. get { return plus_minus_bounds; }
  126. }
  127. internal Rectangle CheckBoxBounds {
  128. get { return checkbox_bounds; }
  129. }
  130. public bool Checked {
  131. get { return check; }
  132. set {
  133. if (check == value)
  134. return;
  135. check = value;
  136. if (TreeView != null)
  137. tree_view.UpdateNode (this);
  138. }
  139. }
  140. public Color BackColor {
  141. get {
  142. if (prop_bag != null)
  143. return prop_bag.BackColor;
  144. if (TreeView != null)
  145. return TreeView.BackColor;
  146. return Color.Empty;
  147. }
  148. set {
  149. if (prop_bag == null)
  150. prop_bag = new OwnerDrawPropertyBag ();
  151. prop_bag.BackColor = value;
  152. }
  153. }
  154. public Color ForeColor {
  155. get {
  156. if (prop_bag != null)
  157. return prop_bag.ForeColor;
  158. if (TreeView != null)
  159. return TreeView.ForeColor;
  160. return Color.Empty;
  161. }
  162. set {
  163. if (prop_bag == null)
  164. prop_bag = new OwnerDrawPropertyBag ();
  165. prop_bag.ForeColor = value;
  166. }
  167. }
  168. public Font NodeFont {
  169. get {
  170. if (prop_bag != null)
  171. return prop_bag.Font;
  172. if (TreeView != null)
  173. return TreeView.Font;
  174. return null;
  175. }
  176. set {
  177. if (prop_bag == null)
  178. prop_bag = new OwnerDrawPropertyBag ();
  179. prop_bag.Font = value;
  180. }
  181. }
  182. public TreeNodeCollection Nodes {
  183. get {
  184. if (nodes == null)
  185. nodes = new TreeNodeCollection (this);
  186. return nodes;
  187. }
  188. }
  189. public TreeNode FirstNode {
  190. get {
  191. if (nodes.Count > 0)
  192. return nodes [0];
  193. return null;
  194. }
  195. }
  196. public string FullPath {
  197. get {
  198. if (tree_view == null)
  199. throw new Exception ("No TreeView associated");
  200. StringBuilder builder = new StringBuilder ();
  201. BuildFullPath (builder);
  202. return builder.ToString ();
  203. }
  204. }
  205. bool BuildFullPath (StringBuilder path)
  206. {
  207. if (parent == null)
  208. return false;
  209. if (parent.BuildFullPath (path))
  210. path.Append (tree_view.PathSeparator);
  211. path.Append (text);
  212. return true;
  213. }
  214. public bool IsExpanded {
  215. get { return is_expanded; }
  216. }
  217. public TreeNode NextNode {
  218. get {
  219. if (parent == null)
  220. return null;
  221. if (parent.Nodes.Count > index + 1)
  222. return parent.Nodes [index + 1];
  223. return null;
  224. }
  225. }
  226. public TreeNode PrevNode {
  227. get {
  228. if (parent == null)
  229. return null;
  230. if (index == 0 || index > parent.Nodes.Count)
  231. return null;
  232. return parent.Nodes [index - 1];
  233. }
  234. }
  235. public TreeNode NextVisibleNode {
  236. get {
  237. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
  238. if (!o.MoveNext ())
  239. return null;
  240. TreeNode c = (TreeNode) o.Current;
  241. if (!c.IsInClippingRect)
  242. return null;
  243. return c;
  244. }
  245. }
  246. public TreeNode PrevVisibleNode {
  247. get {
  248. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
  249. if (!o.MovePrevious ())
  250. return null;
  251. TreeNode c = (TreeNode) o.Current;
  252. if (!c.IsInClippingRect)
  253. return null;
  254. return c;
  255. }
  256. }
  257. public TreeNode LastNode {
  258. get {
  259. return (nodes == null || nodes.Count == 0) ? null : nodes [nodes.Count - 1];
  260. }
  261. }
  262. public int Index {
  263. get { return index; }
  264. }
  265. public int ImageIndex {
  266. get { return image_index; }
  267. set { image_index = value; }
  268. }
  269. public int SelectedImageIndex {
  270. get { return selected_image_index; }
  271. set { selected_image_index = value; }
  272. }
  273. public object Tag {
  274. get { return tag; }
  275. set { tag = value; }
  276. }
  277. public void Expand ()
  278. {
  279. Expand(false);
  280. }
  281. private void Expand (bool byInternal)
  282. {
  283. if (is_expanded)
  284. return;
  285. bool cancel = false;
  286. if (TreeView != null) {
  287. TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Expand);
  288. TreeView.OnBeforeCollapse (e);
  289. cancel = e.Cancel;
  290. }
  291. if (!cancel) {
  292. is_expanded = true;
  293. if (TreeView != null)
  294. TreeView.OnAfterCollapse (new TreeViewEventArgs (this));
  295. if (IsNodeVisible () && TreeView != null)
  296. TreeView.UpdateBelow (this);
  297. }
  298. }
  299. public void Collapse ()
  300. {
  301. Collapse(false);
  302. }
  303. private void Collapse (bool byInternal)
  304. {
  305. if (!is_expanded)
  306. return;
  307. if (tree_view != null && tree_view.root_node == this)
  308. return;
  309. bool cancel = false;
  310. if (TreeView != null) {
  311. TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Collapse);
  312. TreeView.OnBeforeCollapse (e);
  313. cancel = e.Cancel;
  314. }
  315. if (!cancel) {
  316. is_expanded = false;
  317. if (TreeView != null)
  318. TreeView.OnAfterCollapse (new TreeViewEventArgs (this));
  319. if (IsNodeVisible () && TreeView != null)
  320. TreeView.UpdateBelow (this);
  321. if(!byInternal && TreeView != null && HasFocusInChildren ())
  322. TreeView.SelectedNode = this;
  323. }
  324. }
  325. private bool HasFocusInChildren()
  326. {
  327. if(TreeView == null) return false;
  328. foreach(TreeNode node in nodes) {
  329. if(node == TreeView.SelectedNode) return true;
  330. if(node.HasFocusInChildren())
  331. return true;
  332. }
  333. return false;
  334. }
  335. public void Remove ()
  336. {
  337. if (parent == null)
  338. return;
  339. parent.Nodes.RemoveAt (Index);
  340. }
  341. public void ExpandAll ()
  342. {
  343. ExpandRecursive (this);
  344. if(TreeView != null)
  345. TreeView.Refresh();
  346. }
  347. private void ExpandRecursive (TreeNode node)
  348. {
  349. node.Expand (true);
  350. foreach (TreeNode child in node.Nodes) {
  351. ExpandRecursive (child);
  352. }
  353. }
  354. internal void CollapseAll ()
  355. {
  356. CollapseRecursive (this);
  357. }
  358. internal void CollapseAllUncheck ()
  359. {
  360. CollapseUncheckRecursive (this);
  361. }
  362. private void CollapseRecursive (TreeNode node)
  363. {
  364. node.Collapse ();
  365. foreach (TreeNode child in node.Nodes) {
  366. CollapseRecursive (child);
  367. }
  368. }
  369. private void CollapseUncheckRecursive (TreeNode node)
  370. {
  371. node.Collapse ();
  372. node.Checked = false;
  373. foreach (TreeNode child in node.Nodes) {
  374. CollapseUncheckRecursive (child);
  375. }
  376. }
  377. public int GetNodeCount (bool include_subtrees)
  378. {
  379. if (!include_subtrees)
  380. return Nodes.Count;
  381. int count = 0;
  382. GetNodeCountRecursive (this, ref count);
  383. return count;
  384. }
  385. public void Toggle ()
  386. {
  387. if (is_expanded)
  388. Collapse ();
  389. else
  390. Expand ();
  391. }
  392. internal void SetNodes (TreeNodeCollection nodes)
  393. {
  394. this.nodes = nodes;
  395. }
  396. private void GetNodeCountRecursive (TreeNode node, ref int count)
  397. {
  398. count += node.Nodes.Count;
  399. foreach (TreeNode child in node.Nodes) {
  400. GetNodeCountRecursive (child, ref count);
  401. }
  402. }
  403. public override String ToString ()
  404. {
  405. return String.Concat ("TreeNode: ", Text);
  406. }
  407. internal void UpdateBounds (int x, int y, int width, int height)
  408. {
  409. bounds.X = x;
  410. bounds.Y = y;
  411. bounds.Width = width;
  412. bounds.Height = height;
  413. }
  414. internal void UpdatePlusMinusBounds (int x, int y, int width, int height)
  415. {
  416. plus_minus_bounds.X = x;
  417. plus_minus_bounds.Y = y;
  418. plus_minus_bounds.Width = width;
  419. plus_minus_bounds.Height = height;
  420. }
  421. internal void UpdateCheckBoxBounds (int x, int y, int width, int height)
  422. {
  423. checkbox_bounds.X = x;
  424. checkbox_bounds.Y = y;
  425. checkbox_bounds.Width = width;
  426. checkbox_bounds.Height = height;
  427. }
  428. internal void SetAddedData (TreeView tree_view, TreeNode parent, int index)
  429. {
  430. this.tree_view = tree_view;
  431. this.parent = parent;
  432. this.index = index;
  433. }
  434. private bool IsInClippingRect
  435. {
  436. get {
  437. if (TreeView == null)
  438. return false;
  439. if (bounds.Y < 0 && bounds.Y > tree_view.ClientRectangle.Height)
  440. return false;
  441. return true;
  442. }
  443. }
  444. private bool IsNodeVisible ()
  445. {
  446. if (TreeView == null)
  447. return false;
  448. if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
  449. return false;
  450. TreeNode parent = Parent;
  451. while (parent != null) {
  452. if (!parent.IsExpanded)
  453. return false;
  454. parent = parent.Parent;
  455. }
  456. return true;
  457. }
  458. }
  459. }