TreeNode.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 {
  185. if (image_index == value)
  186. return;
  187. image_index = value;
  188. TreeView tree = TreeView;
  189. if (tree != null)
  190. tree.UpdateNode (this);
  191. }
  192. }
  193. public bool IsEditing {
  194. get { return is_editing; }
  195. }
  196. public bool IsExpanded {
  197. get { return is_expanded; }
  198. }
  199. public bool IsSelected {
  200. get {
  201. if (TreeView == null)
  202. return false;
  203. return TreeView.SelectedNode == this;
  204. }
  205. }
  206. public bool IsVisible {
  207. get {
  208. if (TreeView == null)
  209. return false;
  210. if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
  211. return false;
  212. TreeNode parent = Parent;
  213. while (parent != null) {
  214. if (!parent.IsExpanded)
  215. return false;
  216. parent = parent.Parent;
  217. }
  218. return true;
  219. }
  220. }
  221. public TreeNode LastNode {
  222. get {
  223. return (nodes == null || nodes.Count == 0) ? null : nodes [nodes.Count - 1];
  224. }
  225. }
  226. public TreeNode NextNode {
  227. get {
  228. if (parent == null)
  229. return null;
  230. int index = Index;
  231. if (parent.Nodes.Count > index + 1)
  232. return parent.Nodes [index + 1];
  233. return null;
  234. }
  235. }
  236. public TreeNode NextVisibleNode {
  237. get {
  238. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
  239. if (!o.MoveNext ())
  240. return null;
  241. TreeNode c = (TreeNode) o.Current;
  242. if (!c.IsInClippingRect)
  243. return null;
  244. return c;
  245. }
  246. }
  247. [Localizable(true)]
  248. public Font NodeFont {
  249. get {
  250. if (prop_bag != null)
  251. return prop_bag.Font;
  252. if (TreeView != null)
  253. return TreeView.Font;
  254. return null;
  255. }
  256. set {
  257. if (prop_bag == null)
  258. prop_bag = new OwnerDrawPropertyBag ();
  259. prop_bag.Font = value;
  260. InvalidateWidth ();
  261. }
  262. }
  263. [ListBindable(false)]
  264. public TreeNodeCollection Nodes {
  265. get {
  266. if (nodes == null)
  267. nodes = new TreeNodeCollection (this);
  268. return nodes;
  269. }
  270. }
  271. public TreeNode Parent {
  272. get {
  273. TreeView tree_view = TreeView;
  274. if (tree_view != null && tree_view.root_node == parent)
  275. return null;
  276. return parent;
  277. }
  278. }
  279. public TreeNode PrevNode {
  280. get {
  281. if (parent == null)
  282. return null;
  283. int index = Index;
  284. if (index <= 0 || index > parent.Nodes.Count)
  285. return null;
  286. return parent.Nodes [index - 1];
  287. }
  288. }
  289. public TreeNode PrevVisibleNode {
  290. get {
  291. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
  292. if (!o.MovePrevious ())
  293. return null;
  294. TreeNode c = (TreeNode) o.Current;
  295. if (!c.IsInClippingRect)
  296. return null;
  297. return c;
  298. }
  299. }
  300. [Localizable(true)]
  301. public int SelectedImageIndex {
  302. get { return selected_image_index; }
  303. set { selected_image_index = value; }
  304. }
  305. [Bindable(true)]
  306. [Localizable(false)]
  307. [TypeConverter(typeof(System.ComponentModel.StringConverter))]
  308. [DefaultValue(null)]
  309. public object Tag {
  310. get { return tag; }
  311. set { tag = value; }
  312. }
  313. [Localizable(true)]
  314. public string Text {
  315. get {
  316. if (text == null)
  317. return String.Empty;
  318. return text;
  319. }
  320. set {
  321. if (text == value)
  322. return;
  323. text = value;
  324. bounds.Width = 0;
  325. }
  326. }
  327. public TreeView TreeView {
  328. get {
  329. if (tree_view != null)
  330. return tree_view;
  331. TreeNode walk = parent;
  332. while (walk != null) {
  333. if (walk.TreeView != null)
  334. break;
  335. walk = walk.parent;
  336. }
  337. if (walk == null)
  338. return null;
  339. return walk.TreeView;
  340. }
  341. }
  342. public IntPtr Handle {
  343. get {
  344. // MS throws a NullReferenceException if the TreeView isn't set...
  345. if (handle == IntPtr.Zero && TreeView != null)
  346. handle = TreeView.CreateNodeHandle ();
  347. return handle;
  348. }
  349. }
  350. #endregion // Public Instance Properties
  351. public static TreeNode FromHandle (TreeView tree, IntPtr handle)
  352. {
  353. if (handle == IntPtr.Zero)
  354. return null;
  355. // No arg checking on MS it just throws a NullRef if treeview is null
  356. return tree.NodeFromHandle (handle);
  357. }
  358. #region Public Instance Methods
  359. public void BeginEdit () {
  360. is_editing = true;
  361. }
  362. public void Collapse () {
  363. Collapse(false);
  364. }
  365. public void EndEdit (bool cancel) {
  366. is_editing = false;
  367. if (!cancel && TreeView != null)
  368. Text = TreeView.LabelEditText;
  369. }
  370. public void Expand () {
  371. Expand(false);
  372. }
  373. public void ExpandAll () {
  374. ExpandRecursive (this);
  375. if(TreeView != null)
  376. TreeView.UpdateNode (TreeView.root_node);
  377. }
  378. public void EnsureVisible ()
  379. {
  380. if (TreeView == null)
  381. return;
  382. if (this.Parent != null)
  383. ExpandParentRecursive (this.Parent);
  384. if (bounds.Y < 0) {
  385. TreeView.SetTop (this);
  386. } else if (bounds.Bottom > TreeView.ViewportRectangle.Bottom) {
  387. TreeView.SetBottom (this);
  388. }
  389. }
  390. public int GetNodeCount (bool include_subtrees) {
  391. if (!include_subtrees)
  392. return Nodes.Count;
  393. int count = 0;
  394. GetNodeCountRecursive (this, ref count);
  395. return count;
  396. }
  397. public void Remove () {
  398. if (parent == null)
  399. return;
  400. int index = Index;
  401. parent.Nodes.RemoveAt (index);
  402. }
  403. public void Toggle () {
  404. if (is_expanded)
  405. Collapse ();
  406. else
  407. Expand ();
  408. }
  409. public override String ToString () {
  410. return String.Concat ("TreeNode: ", Text);
  411. }
  412. #endregion // Public Instance Methods
  413. #region Internal & Private Methods and Properties
  414. internal bool IsRoot {
  415. get {
  416. TreeView tree_view = TreeView;
  417. if (tree_view == null)
  418. return false;
  419. if (tree_view.root_node == this)
  420. return true;
  421. return false;
  422. }
  423. }
  424. bool BuildFullPath (StringBuilder path)
  425. {
  426. if (parent == null)
  427. return false;
  428. if (parent.BuildFullPath (path))
  429. path.Append (TreeView.PathSeparator);
  430. path.Append (text);
  431. return true;
  432. }
  433. public int Index {
  434. get {
  435. if (parent == null)
  436. return 0;
  437. return parent.Nodes.IndexOf (this);
  438. }
  439. }
  440. private void Expand (bool byInternal)
  441. {
  442. if (is_expanded)
  443. return;
  444. bool cancel = false;
  445. if (TreeView != null) {
  446. TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Expand);
  447. TreeView.OnBeforeExpand (e);
  448. cancel = e.Cancel;
  449. }
  450. if (!cancel) {
  451. is_expanded = true;
  452. if (TreeView != null)
  453. TreeView.OnAfterExpand (new TreeViewEventArgs (this));
  454. if (IsVisible && TreeView != null)
  455. TreeView.UpdateBelow (this);
  456. }
  457. }
  458. private void Collapse (bool byInternal)
  459. {
  460. if (!is_expanded)
  461. return;
  462. if (IsRoot)
  463. return;
  464. bool cancel = false;
  465. if (TreeView != null) {
  466. TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Collapse);
  467. TreeView.OnBeforeCollapse (e);
  468. cancel = e.Cancel;
  469. }
  470. if (!cancel) {
  471. is_expanded = false;
  472. if (TreeView != null)
  473. TreeView.OnAfterCollapse (new TreeViewEventArgs (this));
  474. if (IsVisible && TreeView != null)
  475. TreeView.UpdateBelow (this);
  476. if(!byInternal && TreeView != null && HasFocusInChildren ())
  477. TreeView.SelectedNode = this;
  478. }
  479. }
  480. private bool HasFocusInChildren()
  481. {
  482. if(TreeView == null) return false;
  483. foreach(TreeNode node in nodes) {
  484. if(node == TreeView.SelectedNode) return true;
  485. if(node.HasFocusInChildren())
  486. return true;
  487. }
  488. return false;
  489. }
  490. private void ExpandRecursive (TreeNode node)
  491. {
  492. node.Expand (true);
  493. foreach (TreeNode child in node.Nodes) {
  494. ExpandRecursive (child);
  495. }
  496. }
  497. private void ExpandParentRecursive (TreeNode node)
  498. {
  499. node.Expand (true);
  500. if (node.Parent != null)
  501. ExpandParentRecursive (node.Parent);
  502. }
  503. internal void CollapseAll ()
  504. {
  505. CollapseRecursive (this);
  506. }
  507. internal void CollapseAllUncheck ()
  508. {
  509. CollapseUncheckRecursive (this);
  510. }
  511. private void CollapseRecursive (TreeNode node)
  512. {
  513. node.Collapse ();
  514. foreach (TreeNode child in node.Nodes) {
  515. CollapseRecursive (child);
  516. }
  517. }
  518. private void CollapseUncheckRecursive (TreeNode node)
  519. {
  520. node.Collapse ();
  521. node.Checked = false;
  522. foreach (TreeNode child in node.Nodes) {
  523. CollapseUncheckRecursive (child);
  524. }
  525. }
  526. internal void SetNodes (TreeNodeCollection nodes)
  527. {
  528. this.nodes = nodes;
  529. }
  530. private void GetNodeCountRecursive (TreeNode node, ref int count)
  531. {
  532. count += node.Nodes.Count;
  533. foreach (TreeNode child in node.Nodes) {
  534. GetNodeCountRecursive (child, ref count);
  535. }
  536. }
  537. internal bool NeedsWidth {
  538. get { return bounds.Width == 0; }
  539. }
  540. internal void InvalidateWidth ()
  541. {
  542. bounds.Width = 0;
  543. }
  544. internal void SetWidth (int width)
  545. {
  546. bounds.Width = width;
  547. }
  548. internal void SetHeight (int height)
  549. {
  550. bounds.Height = height;
  551. }
  552. internal void SetPosition (int x, int y)
  553. {
  554. bounds.X = x;
  555. bounds.Y = y;
  556. }
  557. internal void SetParent (TreeNode parent)
  558. {
  559. this.parent = parent;
  560. }
  561. private bool IsInClippingRect
  562. {
  563. get {
  564. if (TreeView == null)
  565. return false;
  566. if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
  567. return false;
  568. return true;
  569. }
  570. }
  571. #endregion // Internal & Private Methods and Properties
  572. }
  573. }