TreeNode.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. #if NET_2_0
  32. [DefaultProperty ("Text")]
  33. #endif
  34. [TypeConverter(typeof(TreeNodeConverter))]
  35. [Serializable]
  36. public class TreeNode : MarshalByRefObject, ICloneable, ISerializable {
  37. #region Fields
  38. private TreeView tree_view;
  39. internal TreeNode parent;
  40. private string text;
  41. private int image_index = -1;
  42. private int selected_image_index = -1;
  43. #if NET_2_0
  44. private string image_key = String.Empty;
  45. private string selected_image_key = String.Empty;
  46. #endif
  47. internal TreeNodeCollection nodes;
  48. internal TreeViewAction check_reason = TreeViewAction.Unknown;
  49. internal int visible_order = 0;
  50. internal int width = -1;
  51. internal bool is_expanded = false;
  52. private bool check;
  53. internal OwnerDrawPropertyBag prop_bag;
  54. private object tag;
  55. internal IntPtr handle;
  56. #if NET_2_0
  57. private string name = string.Empty;
  58. #endif
  59. #endregion // Fields
  60. #region Internal Constructors
  61. internal TreeNode (TreeView tree_view) : this ()
  62. {
  63. this.tree_view = tree_view;
  64. is_expanded = true;
  65. }
  66. private TreeNode (SerializationInfo info, StreamingContext context) : this ()
  67. {
  68. SerializationInfoEnumerator en;
  69. SerializationEntry e;
  70. int children;
  71. en = info.GetEnumerator();
  72. children = 0;
  73. while (en.MoveNext()) {
  74. e = en.Current;
  75. switch(e.Name) {
  76. case "Text": Text = (string)e.Value; break;
  77. case "PropBag": prop_bag = (OwnerDrawPropertyBag)e.Value; break;
  78. case "ImageIndex": image_index = (int)e.Value; break;
  79. case "SelectedImageIndex": selected_image_index = (int)e.Value; break;
  80. case "Tag": tag = e.Value; break;
  81. case "IsChecked": check = (bool)e.Value; break;
  82. case "ChildCount": children = (int)e.Value; break;
  83. }
  84. }
  85. if (children > 0) {
  86. for (int i = 0; i < children; i++) {
  87. TreeNode node = (TreeNode) info.GetValue ("children" + i, typeof (TreeNode));
  88. Nodes.Add (node);
  89. }
  90. }
  91. }
  92. #endregion // Internal Constructors
  93. #region Public Constructors
  94. public TreeNode ()
  95. {
  96. nodes = new TreeNodeCollection (this);
  97. }
  98. public TreeNode (string text) : this ()
  99. {
  100. Text = text;
  101. }
  102. public TreeNode (string text, TreeNode [] children) : this (text)
  103. {
  104. Nodes.AddRange (children);
  105. }
  106. public TreeNode (string text, int image_index, int selected_image_index) : this (text)
  107. {
  108. this.image_index = image_index;
  109. this.selected_image_index = selected_image_index;
  110. }
  111. public TreeNode (string text, int image_index, int selected_image_index,
  112. TreeNode [] children) : this (text, image_index, selected_image_index)
  113. {
  114. Nodes.AddRange (children);
  115. }
  116. #endregion // Public Constructors
  117. #region ICloneable Members
  118. public virtual object Clone()
  119. {
  120. TreeNode tn = new TreeNode (text, image_index, selected_image_index);
  121. if (nodes != null) {
  122. foreach (TreeNode child in nodes)
  123. tn.Nodes.Add ((TreeNode)child.Clone ());
  124. }
  125. tn.Tag = tag;
  126. tn.Checked = Checked;
  127. if (prop_bag != null)
  128. tn.prop_bag = OwnerDrawPropertyBag.Copy (prop_bag);
  129. return tn;
  130. }
  131. #endregion // ICloneable Members
  132. #region ISerializable Members
  133. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  134. {
  135. info.AddValue ("Text", Text);
  136. info.AddValue ("prop_bag", prop_bag, typeof (OwnerDrawPropertyBag));
  137. info.AddValue ("ImageIndex", ImageIndex);
  138. info.AddValue ("SelectedImageIndex", SelectedImageIndex);
  139. info.AddValue ("Tag", Tag);
  140. info.AddValue ("Checked", Checked);
  141. info.AddValue ("NumberOfChildren", Nodes.Count);
  142. for (int i = 0; i < Nodes.Count; i++)
  143. info.AddValue ("Child-" + i, Nodes [i], typeof (TreeNode));
  144. }
  145. #endregion // ISerializable Members
  146. #region Public Instance Properties
  147. public Color BackColor {
  148. get {
  149. if (prop_bag != null)
  150. return prop_bag.BackColor;
  151. if (TreeView != null)
  152. return TreeView.BackColor;
  153. return Color.Empty;
  154. }
  155. set {
  156. if (prop_bag == null)
  157. prop_bag = new OwnerDrawPropertyBag ();
  158. prop_bag.BackColor = value;
  159. TreeView tree_view = TreeView;
  160. if (tree_view != null)
  161. tree_view.UpdateNode (this);
  162. }
  163. }
  164. #if NET_2_0
  165. [Browsable (false)]
  166. #endif
  167. public Rectangle Bounds {
  168. get {
  169. if (TreeView == null)
  170. return Rectangle.Empty;
  171. int x = GetX ();
  172. int y = GetY ();
  173. if (width == -1)
  174. width = TreeView.GetNodeWidth (this);
  175. Rectangle res = new Rectangle (x, y, width, TreeView.ActualItemHeight);
  176. return res;
  177. }
  178. }
  179. internal int GetY ()
  180. {
  181. if (TreeView == null)
  182. return 0;
  183. return (visible_order - 1) * TreeView.ActualItemHeight - (TreeView.skipped_nodes * TreeView.ActualItemHeight);
  184. }
  185. internal int GetX ()
  186. {
  187. if (TreeView == null)
  188. return 0;
  189. int indent_level = IndentLevel;
  190. int roots = (TreeView.ShowRootLines ? 1 : 0);
  191. int cb = (TreeView.CheckBoxes ? 19 : 0);
  192. int imgs = (TreeView.ImageList != null ? TreeView.ImageList.ImageSize.Width + 3 : 0);
  193. return ((indent_level + roots) * TreeView.Indent) + cb + imgs - TreeView.hbar_offset;
  194. }
  195. internal int GetLinesX ()
  196. {
  197. int roots = (TreeView.ShowRootLines ? 1 : 0);
  198. return (IndentLevel + roots) * TreeView.Indent - TreeView.hbar_offset;
  199. }
  200. internal int GetImageX ()
  201. {
  202. return GetLinesX () + (TreeView.CheckBoxes ? 19 : 0);
  203. }
  204. // In theory we should be able to track this instead of computing
  205. // every single time we need it, however for now I am going to
  206. // do it this way to reduce bugs in my new bounds computing code
  207. internal int IndentLevel {
  208. get {
  209. TreeNode walk = this;
  210. int res = 0;
  211. while (walk.Parent != null) {
  212. walk = walk.Parent;
  213. res++;
  214. }
  215. return res;
  216. }
  217. }
  218. #if NET_2_0
  219. [DefaultValue (false)]
  220. #endif
  221. public bool Checked {
  222. get { return check; }
  223. set {
  224. if (check == value)
  225. return;
  226. TreeViewCancelEventArgs args = new TreeViewCancelEventArgs (this, false, check_reason);
  227. if (TreeView != null)
  228. TreeView.OnBeforeCheck (args);
  229. if (!args.Cancel) {
  230. check = value;
  231. if (TreeView != null) {
  232. TreeView.OnAfterCheck (new TreeViewEventArgs (this, check_reason));
  233. TreeView.UpdateNode (this);
  234. }
  235. }
  236. check_reason = TreeViewAction.Unknown;
  237. }
  238. }
  239. #if NET_2_0
  240. [Browsable (false)]
  241. #endif
  242. public TreeNode FirstNode {
  243. get {
  244. if (nodes.Count > 0)
  245. return nodes [0];
  246. return null;
  247. }
  248. }
  249. public Color ForeColor {
  250. get {
  251. if (prop_bag != null)
  252. return prop_bag.ForeColor;
  253. if (TreeView != null)
  254. return TreeView.ForeColor;
  255. return Color.Empty;
  256. }
  257. set {
  258. if (prop_bag == null)
  259. prop_bag = new OwnerDrawPropertyBag ();
  260. prop_bag.ForeColor = value;
  261. TreeView tree_view = TreeView;
  262. if (tree_view != null)
  263. tree_view.UpdateNode (this);
  264. }
  265. }
  266. #if NET_2_0
  267. [Browsable (false)]
  268. #endif
  269. public string FullPath {
  270. get {
  271. if (TreeView == null)
  272. #if NET_2_0
  273. throw new InvalidOperationException ("No TreeView associated");
  274. #else
  275. throw new Exception ("No TreeView associated");
  276. #endif
  277. StringBuilder builder = new StringBuilder ();
  278. BuildFullPath (builder);
  279. return builder.ToString ();
  280. }
  281. }
  282. #if NET_2_0
  283. [DefaultValue (-1)]
  284. [RelatedImageList ("TreeView.ImageList")]
  285. [TypeConverter (typeof (TreeViewImageIndexConverter))]
  286. [RefreshProperties (RefreshProperties.Repaint)]
  287. [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  288. #endif
  289. [Localizable(true)]
  290. public int ImageIndex {
  291. get { return image_index; }
  292. set {
  293. if (image_index == value)
  294. return;
  295. image_index = value;
  296. TreeView tree = TreeView;
  297. if (tree != null)
  298. tree.UpdateNode (this);
  299. }
  300. }
  301. #if NET_2_0
  302. [Localizable(true)]
  303. [DefaultValue ("")]
  304. [RelatedImageList ("TreeView.ImageList")]
  305. [RefreshProperties (RefreshProperties.Repaint)]
  306. [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  307. public string ImageKey {
  308. get { return image_key; }
  309. set {
  310. if (image_key == value)
  311. return;
  312. image_key = value;
  313. TreeView tree = TreeView;
  314. if (tree != null)
  315. tree.UpdateNode(this);
  316. }
  317. }
  318. #endif
  319. #if NET_2_0
  320. [Browsable (false)]
  321. #endif
  322. public bool IsEditing {
  323. get {
  324. TreeView tv = TreeView;
  325. if (tv == null)
  326. return false;
  327. return tv.edit_node == this;
  328. }
  329. }
  330. #if NET_2_0
  331. [Browsable (false)]
  332. #endif
  333. public bool IsExpanded {
  334. get {
  335. TreeView tv = TreeView;
  336. if (tv != null && tv.IsHandleCreated) {
  337. // This is ridiculous
  338. bool found = false;
  339. foreach (TreeNode walk in TreeView.Nodes) {
  340. if (walk.Nodes.Count > 0)
  341. found = true;
  342. }
  343. if (!found)
  344. return false;
  345. }
  346. return is_expanded;
  347. }
  348. }
  349. #if NET_2_0
  350. [Browsable (false)]
  351. #endif
  352. public bool IsSelected {
  353. get {
  354. if (TreeView == null || !TreeView.IsHandleCreated)
  355. return false;
  356. return TreeView.SelectedNode == this;
  357. }
  358. }
  359. #if NET_2_0
  360. [Browsable (false)]
  361. #endif
  362. public bool IsVisible {
  363. get {
  364. if (TreeView == null || !TreeView.IsHandleCreated || !TreeView.Visible)
  365. return false;
  366. if (visible_order <= TreeView.skipped_nodes || visible_order - TreeView.skipped_nodes > TreeView.VisibleCount)
  367. return false;
  368. TreeNode parent = Parent;
  369. while (parent != null) {
  370. if (!parent.is_expanded)
  371. return false;
  372. parent = parent.Parent;
  373. }
  374. return true;
  375. }
  376. }
  377. #if NET_2_0
  378. [Browsable (false)]
  379. #endif
  380. public TreeNode LastNode {
  381. get {
  382. return (nodes == null || nodes.Count == 0) ? null : nodes [nodes.Count - 1];
  383. }
  384. }
  385. #if NET_2_0
  386. public string Name
  387. {
  388. get { return this.name; }
  389. set {
  390. // Value should never be null as per spec
  391. this.name = (value == null) ? string.Empty : value;
  392. }
  393. }
  394. #endif
  395. #if NET_2_0
  396. [Browsable (false)]
  397. #endif
  398. public TreeNode NextNode {
  399. get {
  400. if (parent == null)
  401. return null;
  402. int index = Index;
  403. if (parent.Nodes.Count > index + 1)
  404. return parent.Nodes [index + 1];
  405. return null;
  406. }
  407. }
  408. #if NET_2_0
  409. [Browsable (false)]
  410. #endif
  411. public TreeNode NextVisibleNode {
  412. get {
  413. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
  414. if (!o.MoveNext ())
  415. return null;
  416. TreeNode c = o.CurrentNode;
  417. if (!c.IsInClippingRect)
  418. return null;
  419. return c;
  420. }
  421. }
  422. #if NET_2_0
  423. [DefaultValue (null)]
  424. #endif
  425. [Localizable (true)]
  426. public Font NodeFont {
  427. get {
  428. if (prop_bag != null)
  429. return prop_bag.Font;
  430. if (TreeView != null)
  431. return TreeView.Font;
  432. return null;
  433. }
  434. set {
  435. if (prop_bag == null)
  436. prop_bag = new OwnerDrawPropertyBag ();
  437. prop_bag.Font = value;
  438. InvalidateWidth ();
  439. }
  440. }
  441. #if NET_2_0
  442. [Browsable (false)]
  443. #endif
  444. [ListBindable (false)]
  445. public TreeNodeCollection Nodes {
  446. get {
  447. if (nodes == null)
  448. nodes = new TreeNodeCollection (this);
  449. return nodes;
  450. }
  451. }
  452. #if NET_2_0
  453. [Browsable (false)]
  454. #endif
  455. public TreeNode Parent {
  456. get {
  457. TreeView tree_view = TreeView;
  458. if (tree_view != null && tree_view.root_node == parent)
  459. return null;
  460. return parent;
  461. }
  462. }
  463. #if NET_2_0
  464. [Browsable (false)]
  465. #endif
  466. public TreeNode PrevNode {
  467. get {
  468. if (parent == null)
  469. return null;
  470. int index = Index;
  471. if (index <= 0 || index > parent.Nodes.Count)
  472. return null;
  473. return parent.Nodes [index - 1];
  474. }
  475. }
  476. #if NET_2_0
  477. [Browsable (false)]
  478. #endif
  479. public TreeNode PrevVisibleNode {
  480. get {
  481. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
  482. if (!o.MovePrevious ())
  483. return null;
  484. TreeNode c = o.CurrentNode;
  485. if (!c.IsInClippingRect)
  486. return null;
  487. return c;
  488. }
  489. }
  490. #if NET_2_0
  491. [DefaultValue (-1)]
  492. [RelatedImageList ("TreeView.ImageList")]
  493. [TypeConverter (typeof (TreeViewImageIndexConverter))]
  494. [RefreshProperties (RefreshProperties.Repaint)]
  495. [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  496. #endif
  497. [Localizable (true)]
  498. public int SelectedImageIndex {
  499. get { return selected_image_index; }
  500. set { selected_image_index = value; }
  501. }
  502. #if NET_2_0
  503. [Localizable (true)]
  504. [DefaultValue ("")]
  505. [RelatedImageList ("TreeView.ImageList")]
  506. [RefreshProperties (RefreshProperties.Repaint)]
  507. [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  508. public string SelectedImageKey {
  509. get { return selected_image_key; }
  510. set { selected_image_key = value; }
  511. }
  512. #endif
  513. [Bindable(true)]
  514. [Localizable(false)]
  515. [TypeConverter(typeof(System.ComponentModel.StringConverter))]
  516. [DefaultValue(null)]
  517. public object Tag {
  518. get { return tag; }
  519. set { tag = value; }
  520. }
  521. [Localizable(true)]
  522. public string Text {
  523. get {
  524. if (text == null)
  525. return String.Empty;
  526. return text;
  527. }
  528. set {
  529. if (text == value)
  530. return;
  531. text = value;
  532. InvalidateWidth ();
  533. }
  534. }
  535. #if NET_2_0
  536. [Browsable (false)]
  537. #endif
  538. public TreeView TreeView {
  539. get {
  540. if (tree_view != null)
  541. return tree_view;
  542. TreeNode walk = parent;
  543. while (walk != null) {
  544. if (walk.TreeView != null)
  545. break;
  546. walk = walk.parent;
  547. }
  548. if (walk == null)
  549. return null;
  550. return walk.TreeView;
  551. }
  552. }
  553. #if NET_2_0
  554. [Browsable (false)]
  555. #endif
  556. public IntPtr Handle {
  557. get {
  558. // MS throws a NullReferenceException if the TreeView isn't set...
  559. if (handle == IntPtr.Zero && TreeView != null)
  560. handle = TreeView.CreateNodeHandle ();
  561. return handle;
  562. }
  563. }
  564. #endregion // Public Instance Properties
  565. public static TreeNode FromHandle (TreeView tree, IntPtr handle)
  566. {
  567. if (handle == IntPtr.Zero)
  568. return null;
  569. // No arg checking on MS it just throws a NullRef if treeview is null
  570. return tree.NodeFromHandle (handle);
  571. }
  572. #region Public Instance Methods
  573. public void BeginEdit () {
  574. TreeView tv = TreeView;
  575. if (tv != null)
  576. tv.BeginEdit (this);
  577. }
  578. public void Collapse () {
  579. CollapseInternal (false);
  580. }
  581. #if NET_2_0
  582. public void Collapse (bool ignore_children)
  583. {
  584. if (ignore_children)
  585. Collapse ();
  586. else
  587. CollapseRecursive (this);
  588. }
  589. #endif
  590. public void EndEdit (bool cancel) {
  591. TreeView tv = TreeView;
  592. if (!cancel && tv != null)
  593. tv.EndEdit (this);
  594. }
  595. public void Expand () {
  596. Expand(false);
  597. }
  598. public void ExpandAll () {
  599. ExpandRecursive (this);
  600. if(TreeView != null)
  601. TreeView.UpdateNode (TreeView.root_node);
  602. }
  603. public void EnsureVisible ()
  604. {
  605. if (TreeView == null)
  606. return;
  607. if (this.Parent != null)
  608. ExpandParentRecursive (this.Parent);
  609. Rectangle bounds = Bounds;
  610. if (bounds.Y < 0) {
  611. TreeView.SetTop (this);
  612. } else if (bounds.Bottom > TreeView.ViewportRectangle.Bottom) {
  613. TreeView.SetBottom (this);
  614. }
  615. }
  616. public int GetNodeCount (bool include_subtrees) {
  617. if (!include_subtrees)
  618. return Nodes.Count;
  619. int count = 0;
  620. GetNodeCountRecursive (this, ref count);
  621. return count;
  622. }
  623. public void Remove () {
  624. if (parent == null)
  625. return;
  626. int index = Index;
  627. parent.Nodes.RemoveAt (index);
  628. }
  629. public void Toggle () {
  630. if (is_expanded)
  631. Collapse ();
  632. else
  633. Expand ();
  634. }
  635. public override String ToString () {
  636. return String.Concat ("TreeNode: ", Text);
  637. }
  638. #endregion // Public Instance Methods
  639. #region Internal & Private Methods and Properties
  640. internal bool IsRoot {
  641. get {
  642. TreeView tree_view = TreeView;
  643. if (tree_view == null)
  644. return false;
  645. if (tree_view.root_node == this)
  646. return true;
  647. return false;
  648. }
  649. }
  650. bool BuildFullPath (StringBuilder path)
  651. {
  652. if (parent == null)
  653. return false;
  654. if (parent.BuildFullPath (path))
  655. path.Append (TreeView.PathSeparator);
  656. path.Append (text);
  657. return true;
  658. }
  659. public int Index {
  660. get {
  661. if (parent == null)
  662. return 0;
  663. return parent.Nodes.IndexOf (this);
  664. }
  665. }
  666. private void Expand (bool byInternal)
  667. {
  668. if (is_expanded || nodes.Count < 1) {
  669. is_expanded = true;
  670. return;
  671. }
  672. bool cancel = false;
  673. TreeView tree_view = TreeView;
  674. if (tree_view != null) {
  675. TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Expand);
  676. tree_view.OnBeforeExpand (e);
  677. cancel = e.Cancel;
  678. }
  679. if (!cancel) {
  680. is_expanded = true;
  681. int count_to_next = CountToNext ();
  682. if (tree_view != null) {
  683. tree_view.OnAfterExpand (new TreeViewEventArgs (this));
  684. tree_view.RecalculateVisibleOrder (this);
  685. tree_view.UpdateScrollBars (false);
  686. if (IsVisible)
  687. tree_view.ExpandBelow (this, count_to_next);
  688. }
  689. }
  690. }
  691. private void CollapseInternal (bool byInternal)
  692. {
  693. if (!is_expanded || nodes.Count < 1)
  694. return;
  695. if (IsRoot)
  696. return;
  697. bool cancel = false;
  698. TreeView tree_view = TreeView;
  699. if (tree_view != null) {
  700. TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Collapse);
  701. tree_view.OnBeforeCollapse (e);
  702. cancel = e.Cancel;
  703. }
  704. if (!cancel) {
  705. int count_to_next = CountToNext ();
  706. is_expanded = false;
  707. if (tree_view != null) {
  708. tree_view.OnAfterCollapse (new TreeViewEventArgs (this));
  709. tree_view.RecalculateVisibleOrder (this);
  710. tree_view.UpdateScrollBars (false);
  711. if (IsVisible)
  712. tree_view.CollapseBelow (this, count_to_next);
  713. if(!byInternal && HasFocusInChildren ())
  714. tree_view.SelectedNode = this;
  715. }
  716. }
  717. }
  718. private int CountToNext ()
  719. {
  720. bool expanded = is_expanded;
  721. is_expanded = false;
  722. OpenTreeNodeEnumerator walk = new OpenTreeNodeEnumerator (this);
  723. TreeNode next= null;
  724. if (walk.MoveNext () && walk.MoveNext ())
  725. next = walk.CurrentNode;
  726. is_expanded = expanded;
  727. walk.Reset ();
  728. walk.MoveNext ();
  729. int count = 0;
  730. while (walk.MoveNext () && walk.CurrentNode != next)
  731. count++;
  732. return count;
  733. }
  734. private bool HasFocusInChildren()
  735. {
  736. if(TreeView == null) return false;
  737. foreach (TreeNode node in nodes) {
  738. if(node == TreeView.SelectedNode)
  739. return true;
  740. if(node.HasFocusInChildren ())
  741. return true;
  742. }
  743. return false;
  744. }
  745. private void ExpandRecursive (TreeNode node)
  746. {
  747. node.Expand (true);
  748. foreach (TreeNode child in node.Nodes) {
  749. ExpandRecursive (child);
  750. }
  751. }
  752. private void ExpandParentRecursive (TreeNode node)
  753. {
  754. node.Expand (true);
  755. if (node.Parent != null)
  756. ExpandParentRecursive (node.Parent);
  757. }
  758. internal void CollapseAll ()
  759. {
  760. CollapseRecursive (this);
  761. }
  762. internal void CollapseAllUncheck ()
  763. {
  764. CollapseUncheckRecursive (this);
  765. }
  766. private void CollapseRecursive (TreeNode node)
  767. {
  768. node.Collapse ();
  769. foreach (TreeNode child in node.Nodes) {
  770. CollapseRecursive (child);
  771. }
  772. }
  773. private void CollapseUncheckRecursive (TreeNode node)
  774. {
  775. node.Collapse ();
  776. node.Checked = false;
  777. foreach (TreeNode child in node.Nodes) {
  778. CollapseUncheckRecursive (child);
  779. }
  780. }
  781. internal void SetNodes (TreeNodeCollection nodes)
  782. {
  783. this.nodes = nodes;
  784. }
  785. private void GetNodeCountRecursive (TreeNode node, ref int count)
  786. {
  787. count += node.Nodes.Count;
  788. foreach (TreeNode child in node.Nodes) {
  789. GetNodeCountRecursive (child, ref count);
  790. }
  791. }
  792. internal bool NeedsWidth {
  793. get { return width == -1; }
  794. }
  795. internal void InvalidateWidth ()
  796. {
  797. // bounds.Width = 0;
  798. width = -1;
  799. }
  800. internal void SetWidth (int width)
  801. {
  802. this.width = width;
  803. }
  804. internal void SetParent (TreeNode parent)
  805. {
  806. this.parent = parent;
  807. }
  808. private bool IsInClippingRect
  809. {
  810. get {
  811. if (TreeView == null)
  812. return false;
  813. Rectangle bounds = Bounds;
  814. if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
  815. return false;
  816. return true;
  817. }
  818. }
  819. #endregion // Internal & Private Methods and Properties
  820. }
  821. }