TreeNode.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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.ComponentModel;
  27. using System.Drawing;
  28. using System.Runtime.Serialization;
  29. using System.Text;
  30. namespace System.Windows.Forms {
  31. [TypeConverter(typeof(TreeNodeConverter))]
  32. [Serializable]
  33. public class TreeNode : MarshalByRefObject, ICloneable, ISerializable {
  34. #region Fields
  35. private TreeView tree_view;
  36. internal TreeNode parent;
  37. private string text;
  38. private int image_index = -1;
  39. private int selected_image_index = -1;
  40. internal TreeNodeCollection nodes;
  41. private bool is_expanded = false;
  42. private Rectangle bounds = Rectangle.Empty;
  43. private bool check;
  44. private bool is_editing;
  45. internal OwnerDrawPropertyBag prop_bag;
  46. private object tag;
  47. internal IntPtr handle;
  48. #endregion // Fields
  49. #region Internal Constructors
  50. internal TreeNode (TreeView tree_view) : this ()
  51. {
  52. this.tree_view = tree_view;
  53. is_expanded = true;
  54. }
  55. private TreeNode (SerializationInfo info, StreamingContext context) : this ()
  56. {
  57. Text = (string) info.GetValue ("Text", typeof (string));
  58. prop_bag = (OwnerDrawPropertyBag) info.GetValue ("prop_bag", typeof (OwnerDrawPropertyBag));
  59. image_index = (int) info.GetValue ("ImageIndex", typeof (int));
  60. selected_image_index = (int) info.GetValue ("SelectedImageIndex", typeof (int));
  61. tag = info.GetValue ("Tag", typeof (object));
  62. check = (bool) info.GetValue ("Checked", typeof (bool));
  63. int count = (int) info.GetValue ("NumberOfChildren", typeof (int));
  64. for (int i = 0; i < count; i++) {
  65. TreeNode node = (TreeNode) info.GetValue ("Child-" + i, typeof (TreeNode));
  66. Nodes.Add (node);
  67. }
  68. }
  69. #endregion // Internal Constructors
  70. #region Public Constructors
  71. public TreeNode ()
  72. {
  73. nodes = new TreeNodeCollection (this);
  74. }
  75. public TreeNode (string text) : this ()
  76. {
  77. Text = text;
  78. }
  79. public TreeNode (string text, TreeNode [] children) : this (text)
  80. {
  81. Nodes.AddRange (children);
  82. }
  83. public TreeNode (string text, int image_index, int selected_image_index) : this (text)
  84. {
  85. this.image_index = image_index;
  86. this.selected_image_index = selected_image_index;
  87. }
  88. public TreeNode (string text, int image_index, int selected_image_index,
  89. TreeNode [] children) : this (text, image_index, selected_image_index)
  90. {
  91. Nodes.AddRange (children);
  92. }
  93. #endregion // Public Constructors
  94. #region ICloneable Members
  95. public object Clone()
  96. {
  97. TreeNode tn = new TreeNode (text, image_index, selected_image_index);
  98. if (nodes != null) {
  99. foreach (TreeNode child in nodes)
  100. tn.Nodes.Add ((TreeNode)child.Clone ());
  101. }
  102. tn.Tag = tag;
  103. tn.Checked = Checked;
  104. if (prop_bag != null)
  105. tn.prop_bag = OwnerDrawPropertyBag.Copy (prop_bag);
  106. return tn;
  107. }
  108. #endregion // ICloneable Members
  109. #region ISerializable Members
  110. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  111. {
  112. info.AddValue ("Text", Text);
  113. info.AddValue ("prop_bag", prop_bag, typeof (OwnerDrawPropertyBag));
  114. info.AddValue ("ImageIndex", ImageIndex);
  115. info.AddValue ("SelectedImageIndex", SelectedImageIndex);
  116. info.AddValue ("Tag", Tag);
  117. info.AddValue ("Checked", Checked);
  118. info.AddValue ("NumberOfChildren", Nodes.Count);
  119. for (int i = 0; i < Nodes.Count; i++)
  120. info.AddValue ("Child-" + i, Nodes [i], typeof (TreeNode));
  121. }
  122. #endregion // ISerializable Members
  123. #region Public Instance Properties
  124. public Color BackColor {
  125. get {
  126. if (prop_bag != null)
  127. return prop_bag.BackColor;
  128. if (TreeView != null)
  129. return TreeView.BackColor;
  130. return Color.Empty;
  131. }
  132. set {
  133. if (prop_bag == null)
  134. prop_bag = new OwnerDrawPropertyBag ();
  135. prop_bag.BackColor = value;
  136. }
  137. }
  138. public Rectangle Bounds {
  139. get { return bounds; }
  140. }
  141. public bool Checked {
  142. get { return check; }
  143. set {
  144. if (check == value)
  145. return;
  146. check = value;
  147. if (TreeView != null)
  148. TreeView.UpdateNode (this);
  149. }
  150. }
  151. public TreeNode FirstNode {
  152. get {
  153. if (nodes.Count > 0)
  154. return nodes [0];
  155. return null;
  156. }
  157. }
  158. public Color ForeColor {
  159. get {
  160. if (prop_bag != null)
  161. return prop_bag.ForeColor;
  162. if (TreeView != null)
  163. return TreeView.ForeColor;
  164. return Color.Empty;
  165. }
  166. set {
  167. if (prop_bag == null)
  168. prop_bag = new OwnerDrawPropertyBag ();
  169. prop_bag.ForeColor = value;
  170. }
  171. }
  172. public string FullPath {
  173. get {
  174. if (TreeView == null)
  175. throw new Exception ("No TreeView associated");
  176. StringBuilder builder = new StringBuilder ();
  177. BuildFullPath (builder);
  178. return builder.ToString ();
  179. }
  180. }
  181. [Localizable(true)]
  182. public int ImageIndex {
  183. get { return image_index; }
  184. set { image_index = value; }
  185. }
  186. public bool IsEditing {
  187. get { return is_editing; }
  188. }
  189. public bool IsExpanded {
  190. get { return is_expanded; }
  191. }
  192. public bool IsSelected {
  193. get {
  194. if (TreeView == null)
  195. return false;
  196. return TreeView.SelectedNode == this;
  197. }
  198. }
  199. public bool IsVisible {
  200. get {
  201. if (TreeView == null)
  202. return false;
  203. if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
  204. return false;
  205. TreeNode parent = Parent;
  206. while (parent != null) {
  207. if (!parent.IsExpanded)
  208. return false;
  209. parent = parent.Parent;
  210. }
  211. return true;
  212. }
  213. }
  214. public TreeNode LastNode {
  215. get {
  216. return (nodes == null || nodes.Count == 0) ? null : nodes [nodes.Count - 1];
  217. }
  218. }
  219. public TreeNode NextNode {
  220. get {
  221. if (parent == null)
  222. return null;
  223. int index = Index;
  224. if (parent.Nodes.Count > index + 1)
  225. return parent.Nodes [index + 1];
  226. return null;
  227. }
  228. }
  229. public TreeNode NextVisibleNode {
  230. get {
  231. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
  232. if (!o.MoveNext ())
  233. return null;
  234. TreeNode c = (TreeNode) o.Current;
  235. if (!c.IsInClippingRect)
  236. return null;
  237. return c;
  238. }
  239. }
  240. [Localizable(true)]
  241. public Font NodeFont {
  242. get {
  243. if (prop_bag != null)
  244. return prop_bag.Font;
  245. if (TreeView != null)
  246. return TreeView.Font;
  247. return null;
  248. }
  249. set {
  250. if (prop_bag == null)
  251. prop_bag = new OwnerDrawPropertyBag ();
  252. prop_bag.Font = value;
  253. InvalidateWidth ();
  254. }
  255. }
  256. [ListBindable(false)]
  257. public TreeNodeCollection Nodes {
  258. get {
  259. if (nodes == null)
  260. nodes = new TreeNodeCollection (this);
  261. return nodes;
  262. }
  263. }
  264. public TreeNode Parent {
  265. get {
  266. TreeView tree_view = TreeView;
  267. if (tree_view != null && tree_view.root_node == parent)
  268. return null;
  269. return parent;
  270. }
  271. }
  272. public TreeNode PrevNode {
  273. get {
  274. if (parent == null)
  275. return null;
  276. int index = Index;
  277. if (index <= 0 || index > parent.Nodes.Count)
  278. return null;
  279. return parent.Nodes [index - 1];
  280. }
  281. }
  282. public TreeNode PrevVisibleNode {
  283. get {
  284. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
  285. if (!o.MovePrevious ())
  286. return null;
  287. TreeNode c = (TreeNode) o.Current;
  288. if (!c.IsInClippingRect)
  289. return null;
  290. return c;
  291. }
  292. }
  293. [Localizable(true)]
  294. public int SelectedImageIndex {
  295. get { return selected_image_index; }
  296. set { selected_image_index = value; }
  297. }
  298. [Bindable(true)]
  299. [Localizable(false)]
  300. [TypeConverter(typeof(System.ComponentModel.StringConverter))]
  301. [DefaultValue(null)]
  302. public object Tag {
  303. get { return tag; }
  304. set { tag = value; }
  305. }
  306. [Localizable(true)]
  307. public string Text {
  308. get {
  309. if (text == null)
  310. return String.Empty;
  311. return text;
  312. }
  313. set {
  314. if (text == value)
  315. return;
  316. text = value;
  317. bounds.Width = 0;
  318. }
  319. }
  320. public TreeView TreeView {
  321. get {
  322. if (tree_view != null)
  323. return tree_view;
  324. TreeNode walk = parent;
  325. while (walk != null) {
  326. if (walk.TreeView != null)
  327. break;
  328. walk = walk.parent;
  329. }
  330. if (walk == null)
  331. return null;
  332. return walk.TreeView;
  333. }
  334. }
  335. public IntPtr Handle {
  336. get {
  337. // MS throws a NullReferenceException if the TreeView isn't set...
  338. if (handle == IntPtr.Zero && TreeView != null)
  339. handle = TreeView.CreateNodeHandle ();
  340. return handle;
  341. }
  342. }
  343. #endregion // Public Instance Properties
  344. public static TreeNode FromHandle (TreeView tree, IntPtr handle)
  345. {
  346. if (handle == IntPtr.Zero)
  347. return null;
  348. // No arg checking on MS it just throws a NullRef if treeview is null
  349. return tree.NodeFromHandle (handle);
  350. }
  351. #region Public Instance Methods
  352. public void BeginEdit () {
  353. is_editing = true;
  354. }
  355. public void Collapse () {
  356. Collapse(false);
  357. }
  358. public void EndEdit (bool cancel) {
  359. is_editing = false;
  360. if (!cancel && TreeView != null)
  361. Text = TreeView.LabelEditText;
  362. }
  363. public void Expand () {
  364. Expand(false);
  365. }
  366. public void ExpandAll () {
  367. ExpandRecursive (this);
  368. if(TreeView != null)
  369. TreeView.UpdateNode (TreeView.root_node);
  370. }
  371. public void EnsureVisible ()
  372. {
  373. if (TreeView == null)
  374. return;
  375. if (this.Parent != null)
  376. ExpandParentRecursive (this.Parent);
  377. if (bounds.Y < 0) {
  378. TreeView.SetTop (this);
  379. } else if (bounds.Bottom > TreeView.ViewportRectangle.Bottom) {
  380. TreeView.SetBottom (this);
  381. }
  382. }
  383. public int GetNodeCount (bool include_subtrees) {
  384. if (!include_subtrees)
  385. return Nodes.Count;
  386. int count = 0;
  387. GetNodeCountRecursive (this, ref count);
  388. return count;
  389. }
  390. public void Remove () {
  391. if (parent == null)
  392. return;
  393. int index = Index;
  394. parent.Nodes.RemoveAt (index);
  395. }
  396. public void Toggle () {
  397. if (is_expanded)
  398. Collapse ();
  399. else
  400. Expand ();
  401. }
  402. public override String ToString () {
  403. return String.Concat ("TreeNode: ", Text);
  404. }
  405. #endregion // Public Instance Methods
  406. #region Internal & Private Methods and Properties
  407. internal bool IsRoot {
  408. get {
  409. TreeView tree_view = TreeView;
  410. if (tree_view == null)
  411. return false;
  412. if (tree_view.root_node == this)
  413. return true;
  414. return false;
  415. }
  416. }
  417. bool BuildFullPath (StringBuilder path)
  418. {
  419. if (parent == null)
  420. return false;
  421. if (parent.BuildFullPath (path))
  422. path.Append (TreeView.PathSeparator);
  423. path.Append (text);
  424. return true;
  425. }
  426. public int Index {
  427. get {
  428. if (parent == null)
  429. return 0;
  430. return parent.Nodes.IndexOf (this);
  431. }
  432. }
  433. private void Expand (bool byInternal)
  434. {
  435. if (is_expanded)
  436. return;
  437. bool cancel = false;
  438. if (TreeView != null) {
  439. TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Expand);
  440. TreeView.OnBeforeExpand (e);
  441. cancel = e.Cancel;
  442. }
  443. if (!cancel) {
  444. is_expanded = true;
  445. if (TreeView != null)
  446. TreeView.OnAfterExpand (new TreeViewEventArgs (this));
  447. if (IsVisible && TreeView != null)
  448. TreeView.UpdateBelow (this);
  449. }
  450. }
  451. private void Collapse (bool byInternal)
  452. {
  453. if (!is_expanded)
  454. return;
  455. if (IsRoot)
  456. return;
  457. bool cancel = false;
  458. if (TreeView != null) {
  459. TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Collapse);
  460. TreeView.OnBeforeCollapse (e);
  461. cancel = e.Cancel;
  462. }
  463. if (!cancel) {
  464. is_expanded = false;
  465. if (TreeView != null)
  466. TreeView.OnAfterCollapse (new TreeViewEventArgs (this));
  467. if (IsVisible && TreeView != null)
  468. TreeView.UpdateBelow (this);
  469. if(!byInternal && TreeView != null && HasFocusInChildren ())
  470. TreeView.SelectedNode = this;
  471. }
  472. }
  473. private bool HasFocusInChildren()
  474. {
  475. if(TreeView == null) return false;
  476. foreach(TreeNode node in nodes) {
  477. if(node == TreeView.SelectedNode) return true;
  478. if(node.HasFocusInChildren())
  479. return true;
  480. }
  481. return false;
  482. }
  483. private void ExpandRecursive (TreeNode node)
  484. {
  485. node.Expand (true);
  486. foreach (TreeNode child in node.Nodes) {
  487. ExpandRecursive (child);
  488. }
  489. }
  490. private void ExpandParentRecursive (TreeNode node)
  491. {
  492. node.Expand (true);
  493. if (node.Parent != null)
  494. ExpandParentRecursive (node.Parent);
  495. }
  496. internal void CollapseAll ()
  497. {
  498. CollapseRecursive (this);
  499. }
  500. internal void CollapseAllUncheck ()
  501. {
  502. CollapseUncheckRecursive (this);
  503. }
  504. private void CollapseRecursive (TreeNode node)
  505. {
  506. node.Collapse ();
  507. foreach (TreeNode child in node.Nodes) {
  508. CollapseRecursive (child);
  509. }
  510. }
  511. private void CollapseUncheckRecursive (TreeNode node)
  512. {
  513. node.Collapse ();
  514. node.Checked = false;
  515. foreach (TreeNode child in node.Nodes) {
  516. CollapseUncheckRecursive (child);
  517. }
  518. }
  519. internal void SetNodes (TreeNodeCollection nodes)
  520. {
  521. this.nodes = nodes;
  522. }
  523. private void GetNodeCountRecursive (TreeNode node, ref int count)
  524. {
  525. count += node.Nodes.Count;
  526. foreach (TreeNode child in node.Nodes) {
  527. GetNodeCountRecursive (child, ref count);
  528. }
  529. }
  530. internal bool NeedsWidth {
  531. get { return bounds.Width == 0; }
  532. }
  533. internal void InvalidateWidth ()
  534. {
  535. bounds.Width = 0;
  536. }
  537. internal void SetWidth (int width)
  538. {
  539. bounds.Width = width;
  540. }
  541. internal void SetHeight (int height)
  542. {
  543. bounds.Height = height;
  544. }
  545. internal void SetPosition (int x, int y)
  546. {
  547. bounds.X = x;
  548. bounds.Y = y;
  549. }
  550. internal void SetParent (TreeNode parent)
  551. {
  552. this.parent = parent;
  553. }
  554. private bool IsInClippingRect
  555. {
  556. get {
  557. if (TreeView == null)
  558. return false;
  559. if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
  560. return false;
  561. return true;
  562. }
  563. }
  564. #endregion // Internal & Private Methods and Properties
  565. }
  566. }