TreeNode.cs 15 KB

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