TreeView.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jackson Harper ([email protected])
  24. using System;
  25. using System.Drawing;
  26. using System.Drawing.Drawing2D;
  27. using System.Collections;
  28. namespace System.Windows.Forms {
  29. public class TreeView : Control {
  30. private string path_separator = "\\";
  31. private int item_height = -1;
  32. private bool sorted;
  33. private TreeNode top_node;
  34. internal TreeNode root_node;
  35. private TreeNodeCollection nodes;
  36. private int total_node_count;
  37. private ImageList image_list;
  38. private int image_index = -1;
  39. private int selected_image_index = -1;
  40. private bool full_row_select;
  41. private bool hot_tracking;
  42. private int indent = 19;
  43. private bool checkboxes;
  44. private bool label_edit;
  45. private bool scrollable;
  46. private bool show_lines = true;
  47. private bool show_root_lines = true;
  48. private bool show_plus_minus = true;
  49. private int update_stack;
  50. private TreeViewEventHandler on_after_check;
  51. private TreeViewEventHandler on_after_collapse;
  52. private TreeViewEventHandler on_after_expand;
  53. private NodeLabelEditEventHandler on_after_label_edit;
  54. private TreeViewEventHandler on_after_select;
  55. private TreeViewCancelEventHandler on_before_check;
  56. private TreeViewCancelEventHandler on_before_collapse;
  57. private TreeViewCancelEventHandler on_before_expand;
  58. private NodeLabelEditEventHandler on_before_label_edit;
  59. private TreeViewCancelEventHandler on_before_select;
  60. private Pen dash;
  61. public TreeView ()
  62. {
  63. root_node = new TreeNode (this);
  64. root_node.Text = "ROOT NODE";
  65. nodes = new TreeNodeCollection (root_node);
  66. root_node.SetNodes (nodes);
  67. MouseDown += new MouseEventHandler (MouseDownHandler);
  68. SizeChanged += new EventHandler (SizeChangedHandler);
  69. SetStyle (ControlStyles.AllPaintingInWmPaint, true);
  70. SetStyle (ControlStyles.UserPaint, true);
  71. dash = new Pen (SystemColors.ControlLight, 1);
  72. }
  73. public string PathSeparator {
  74. get { return path_separator; }
  75. set { path_separator = value; }
  76. }
  77. public bool Sorted {
  78. get { return sorted; }
  79. set {
  80. if (sorted != value)
  81. sorted = value;
  82. if (sorted)
  83. Nodes.Sort ();
  84. }
  85. }
  86. public TreeNode TopNode {
  87. get { return top_node; }
  88. }
  89. public TreeNodeCollection Nodes {
  90. get { return nodes; }
  91. }
  92. public int ItemHeight {
  93. get {
  94. if (item_height == -1)
  95. return FontHeight + 3;
  96. return item_height;
  97. }
  98. set {
  99. if (value == item_height)
  100. return;
  101. item_height = value;
  102. Refresh ();
  103. }
  104. }
  105. public int VisibleCount {
  106. get {
  107. return ClientRectangle.Height / ItemHeight;
  108. }
  109. }
  110. [MonoTODO ("Anything special need to be done here?")]
  111. public ImageList ImageList {
  112. get { return image_list; }
  113. set { image_list = value; }
  114. }
  115. public int ImageIndex {
  116. get { return image_index; }
  117. set {
  118. if (value < 0) {
  119. throw new ArgumentException ("'" + value + "' is not a valid value for 'value'. " +
  120. "'value' must be greater than or equal to 0.");
  121. }
  122. image_index = value;
  123. }
  124. }
  125. public int SelectedImageIndex {
  126. get { return selected_image_index; }
  127. set {
  128. if (value < 0) {
  129. throw new ArgumentException ("'" + value + "' is not a valid value for 'value'. " +
  130. "'value' must be greater than or equal to 0.");
  131. }
  132. }
  133. }
  134. public override Color BackColor {
  135. get { return base.BackColor;}
  136. set { base.BackColor = value; }
  137. }
  138. public override Image BackgroundImage {
  139. get { return base.BackgroundImage; }
  140. set { base.BackgroundImage = value; }
  141. }
  142. /*
  143. Commented out until this is implemented in Control
  144. public override BorderStyle BorderStyle {
  145. get { return base.BorderStyle; }
  146. set { base.BorderStyle = value; }
  147. }
  148. */
  149. public override Color ForeColor {
  150. get { return base.ForeColor; }
  151. set { base.ForeColor = value; }
  152. }
  153. public override string Text {
  154. get { return base.Text; }
  155. set { base.Text = value; }
  156. }
  157. public bool FullRowSelect {
  158. get { return full_row_select; }
  159. set {
  160. if (value == full_row_select)
  161. return;
  162. full_row_select = value;
  163. Refresh ();
  164. }
  165. }
  166. public bool HotTracking {
  167. get { return hot_tracking; }
  168. set { hot_tracking = value; }
  169. }
  170. public int Indent {
  171. get { return indent; }
  172. set {
  173. if (indent == value)
  174. return;
  175. if (value > 32000) {
  176. throw new ArgumentException ("'" + value + "' is not a valid value for 'Indent'. " +
  177. "'Indent' must be less than or equal to 32000");
  178. }
  179. if (value < 0) {
  180. throw new ArgumentException ("'" + value + "' is not a valid value for 'Indent'. " +
  181. "'Indent' must be greater than or equal to 0.");
  182. }
  183. indent = value;
  184. Refresh ();
  185. }
  186. }
  187. public bool LabelEdit {
  188. get { return label_edit; }
  189. set { label_edit = value; }
  190. }
  191. public bool Scrollable {
  192. get { return scrollable; }
  193. set {
  194. if (scrollable == value)
  195. return;
  196. scrollable = value;
  197. }
  198. }
  199. public bool ShowLines {
  200. get { return show_lines; }
  201. set {
  202. if (show_lines == value)
  203. return;
  204. show_lines = value;
  205. Refresh ();
  206. }
  207. }
  208. public bool ShowRootLines {
  209. get { return show_root_lines; }
  210. set {
  211. if (show_root_lines == value)
  212. return;
  213. show_root_lines = value;
  214. Refresh ();
  215. }
  216. }
  217. public bool CheckBoxes {
  218. get { return checkboxes; }
  219. set {
  220. if (value == checkboxes)
  221. return;
  222. checkboxes = value;
  223. Refresh ();
  224. }
  225. }
  226. public bool ShowPlusMinus {
  227. get { return show_plus_minus; }
  228. set {
  229. if (show_plus_minus == value)
  230. return;
  231. show_plus_minus = value;
  232. Refresh ();
  233. }
  234. }
  235. [MonoTODO ("Anything extra needed here")]
  236. protected override CreateParams CreateParams {
  237. get {
  238. CreateParams cp = base.CreateParams;
  239. return cp;
  240. }
  241. }
  242. protected override Size DefaultSize {
  243. get { return new Size (121, 97); }
  244. }
  245. internal int TotalNodeCount {
  246. get { return total_node_count; }
  247. set { total_node_count = value; }
  248. }
  249. protected override void CreateHandle ()
  250. {
  251. base.CreateHandle ();
  252. }
  253. protected override void Dispose (bool disposing)
  254. {
  255. if (disposing) {
  256. if (image_list != null)
  257. image_list.Dispose ();
  258. }
  259. base.Dispose (disposing);
  260. }
  261. [MonoTODO ("What does the state effect?")]
  262. protected OwnerDrawPropertyBag GetItemRenderStyles (TreeNode node, int state)
  263. {
  264. return node.prop_bag;
  265. }
  266. [MonoTODO ("Need to know if we are editing, not if editing is enabled")]
  267. protected override bool IsInputKey (Keys key_data)
  268. {
  269. if (label_edit && (key_data & Keys.Alt) == 0)
  270. {
  271. switch (key_data & Keys.KeyCode) {
  272. case Keys.Enter:
  273. case Keys.Escape:
  274. case Keys.Prior:
  275. case Keys.Next:
  276. case Keys.End:
  277. case Keys.Home:
  278. case Keys.Left:
  279. case Keys.Up:
  280. case Keys.Right:
  281. case Keys.Down:
  282. return true;
  283. }
  284. }
  285. return base.IsInputKey (key_data);
  286. }
  287. protected virtual void OnAfterCheck (TreeViewEventArgs e)
  288. {
  289. if (on_after_check != null)
  290. on_after_check (this, e);
  291. }
  292. protected internal virtual void OnAfterCollapse (TreeViewEventArgs e)
  293. {
  294. if (on_after_collapse != null)
  295. on_after_collapse (this, e);
  296. }
  297. protected internal virtual void OnAfterExpand (TreeViewEventArgs e)
  298. {
  299. if (on_after_expand != null)
  300. on_after_expand (this, e);
  301. }
  302. protected virtual void OnAfterLabelEdit (NodeLabelEditEventArgs e)
  303. {
  304. if (on_after_label_edit != null)
  305. on_after_label_edit (this, e);
  306. }
  307. protected virtual void OnAfterSelect (TreeViewEventArgs e)
  308. {
  309. if (on_after_select != null)
  310. on_after_select (this, e);
  311. }
  312. protected virtual void OnBeforeCheck (TreeViewCancelEventArgs e)
  313. {
  314. if (on_before_check != null)
  315. on_before_check (this, e);
  316. }
  317. protected internal virtual void OnBeforeCollapse (TreeViewCancelEventArgs e)
  318. {
  319. if (on_before_collapse != null)
  320. on_before_collapse (this, e);
  321. }
  322. protected internal virtual void OnBeforeExpand (TreeViewCancelEventArgs e)
  323. {
  324. if (on_before_expand != null)
  325. on_before_expand (this, e);
  326. }
  327. protected virtual void OnBeforeLabelEdit (NodeLabelEditEventArgs e)
  328. {
  329. if (on_before_label_edit != null)
  330. on_before_label_edit (this, e);
  331. }
  332. protected virtual void OnBeforeSelect (TreeViewCancelEventArgs e)
  333. {
  334. if (on_before_select != null)
  335. on_before_select (this, e);
  336. }
  337. protected override void OnHandleCreated (EventArgs e)
  338. {
  339. base.OnHandleCreated (e);
  340. }
  341. protected override void OnHandleDestroyed (EventArgs e)
  342. {
  343. base.OnHandleDestroyed (e);
  344. }
  345. public void BeginUpdate ()
  346. {
  347. if (!IsHandleCreated)
  348. return;
  349. update_stack++;
  350. }
  351. public void EndUpdate ()
  352. {
  353. if (!IsHandleCreated)
  354. return;
  355. if (update_stack > 1) {
  356. update_stack--;
  357. } else {
  358. update_stack = 0;
  359. Refresh ();
  360. }
  361. }
  362. public void ExpandAll ()
  363. {
  364. root_node.ExpandAll ();
  365. }
  366. public void CollapseAll ()
  367. {
  368. root_node.CollapseAll ();
  369. }
  370. public TreeNode GetNodeAt (Point pt)
  371. {
  372. return GetNodeAt (pt.X, pt.Y);
  373. }
  374. public TreeNode GetNodeAt (int x, int y)
  375. {
  376. if (top_node == null)
  377. top_node = nodes [0];
  378. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (TopNode);
  379. int move = y / ItemHeight;
  380. for (int i = 0; i < move; i++) {
  381. if (!o.MoveNext ())
  382. return null;
  383. }
  384. // Make sure it is in the horizontal bounding box
  385. if (o.CurrentNode.Bounds.Left > x && o.CurrentNode.Bounds.Right < x)
  386. return o.CurrentNode;
  387. return null;
  388. }
  389. public int GetNodeCount (bool include_subtrees)
  390. {
  391. return root_node.GetNodeCount (include_subtrees);
  392. }
  393. public override string ToString ()
  394. {
  395. int count = Nodes.Count;
  396. if (count < 0)
  397. return String.Concat (base.ToString (), "Node Count: 0");
  398. return String.Concat (base.ToString (), "Node Count: ", count, " Nodes[0]: ", Nodes [0]);
  399. }
  400. protected override void WndProc(ref Message m)
  401. {
  402. switch ((Msg) m.Msg) {
  403. case Msg.WM_PAINT: {
  404. PaintEventArgs paint_event;
  405. paint_event = XplatUI.PaintEventStart (Handle);
  406. DoPaint (paint_event);
  407. XplatUI.PaintEventEnd (Handle);
  408. return;
  409. }
  410. case Msg.WM_LBUTTONDBLCLK:
  411. Console.WriteLine ("double click");
  412. break;
  413. }
  414. base.WndProc (ref m);
  415. }
  416. internal void UpdateBelow (TreeNode node)
  417. {
  418. // Invalidate all these nodes and the nodes below it
  419. }
  420. private void DoPaint (PaintEventArgs pe)
  421. {
  422. if (Width <= 0 || Height <= 0 || Visible == false)
  423. return;
  424. Draw();
  425. pe.Graphics.DrawImage (ImageBuffer, 0, 0);
  426. }
  427. private bool add_hscroll;
  428. private bool add_vscroll;
  429. private int max_node_width;
  430. private void Draw ()
  431. {
  432. DateTime start = DateTime.Now;
  433. if (top_node == null && Nodes.Count > 0)
  434. top_node = nodes [0];
  435. // Decide if we need a scrollbar
  436. int visible_node_count = GetVisibleNodeCount ();
  437. Console.WriteLine ("time to get visible node count: " + (DateTime.Now - start));
  438. int node_count = 0;
  439. Rectangle fill = ClientRectangle;
  440. Rectangle vclip = Rectangle.Empty;
  441. add_vscroll = false;
  442. add_hscroll = false;
  443. if ((visible_node_count * ItemHeight) > ClientRectangle.Height) {
  444. add_vscroll = true;
  445. if (vbar == null)
  446. vbar = new VScrollBar ();
  447. vclip = new Rectangle (ClientRectangle.Width - vbar.Width, 0, vbar.Width, Height);
  448. fill.Width -= vbar.Width;
  449. DeviceContext.ExcludeClip (vclip);
  450. }
  451. DeviceContext.FillRectangle (new SolidBrush (Color.White), fill);
  452. int depth = 0;
  453. int item_height = ItemHeight;
  454. Font font = Font;
  455. int height = ClientRectangle.Height;
  456. foreach (TreeNode node in nodes) {
  457. DrawNode (node, ref depth, ref node_count, item_height,
  458. font, ref visible_node_count, height);
  459. depth = 0;
  460. }
  461. if (max_node_width > ClientRectangle.Width) {
  462. add_hscroll = true;
  463. AddHorizontalScrollBar ();
  464. }
  465. if (add_vscroll)
  466. AddVerticalScrollBar (node_count, vclip);
  467. if (add_hscroll && add_vscroll) {
  468. Rectangle grip = new Rectangle (hbar.Right, vbar.Bottom, vbar.Width, hbar.Height);
  469. DeviceContext.FillRectangle (new SolidBrush (BackColor), grip);
  470. ControlPaint.DrawSizeGrip (DeviceContext, BackColor, grip);
  471. }
  472. /*
  473. ControlPaint.DrawBorder3D (DeviceContext, ClientRectangle, Border3DStyle.Sunken,
  474. Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom);
  475. */
  476. /*
  477. int depth = 0;
  478. foreach (TreeNode node in nodes) {
  479. DumpNode (node, ref depth);
  480. depth = 0;
  481. }
  482. */
  483. Console.WriteLine ("treeview drawing time: " + (DateTime.Now - start));
  484. Console.WriteLine ("node count: " + node_count);
  485. Console.WriteLine ("total node count: " + total_node_count);
  486. }
  487. private void DumpNode (TreeNode node, ref int depth)
  488. {
  489. for (int i = 0; i < depth; i++)
  490. Console.Write ("****");
  491. Console.WriteLine (node.Text);
  492. if (node.PrevNode != null)
  493. Console.WriteLine (" -- " + node.PrevNode.Text);
  494. depth++;
  495. foreach (TreeNode child in node.Nodes) {
  496. DumpNode (child, ref depth);
  497. }
  498. depth--;
  499. }
  500. private void DrawNodePlusMinus (TreeNode node, int x, int y, int middle)
  501. {
  502. node.UpdatePlusMinusBounds (x, middle - 4, 8, 8);
  503. DeviceContext.DrawRectangle (SystemPens.ControlDark, node.plus_minus_bounds);
  504. if (node.IsExpanded) {
  505. DeviceContext.DrawLine (SystemPens.ControlDarkDark, x + 2, middle, x + 6, middle);
  506. } else {
  507. DeviceContext.DrawLine (SystemPens.ControlDarkDark, x + 2, middle, x + 6, middle);
  508. DeviceContext.DrawLine (SystemPens.ControlDarkDark, x + 4, y + 6, x + 4, y + 10);
  509. }
  510. }
  511. private void DrawNodeLines (TreeNode node, Pen dash, int x, int y, int middle, int item_height, int node_count)
  512. {
  513. int xadjust = 9;
  514. if (node_count > 0 && show_plus_minus)
  515. xadjust = 13;
  516. DeviceContext.DrawLine (dash, x - indent + xadjust, middle, x, middle);
  517. int ly = 0;
  518. if (node.PrevNode != null) {
  519. int prevadjust = (node.Nodes.Count > 0 && show_plus_minus ? 4 : 0);
  520. int myadjust = (node.Nodes.Count > 0 && show_plus_minus ? 4 : 0);
  521. ly = node.PrevNode.Bounds.Bottom - (item_height / 2) + prevadjust;
  522. DeviceContext.DrawLine (dash, x - indent + 9, middle - myadjust, x - indent + 9, ly);
  523. } else if (node.Parent != null) {
  524. int myadjust = (node.Nodes.Count > 0 && show_plus_minus ? 4 : 0);
  525. ly = node.Parent.Bounds.Bottom;
  526. DeviceContext.DrawLine (dash, x - indent + 9, middle - myadjust, x - indent + 9, ly);
  527. }
  528. }
  529. private void DrawNodeImage (TreeNode node, int x, int y)
  530. {
  531. if (node.ImageIndex > -1 && ImageList != null && node.ImageIndex < ImageList.Images.Count) {
  532. ImageList.Draw (DeviceContext, x, y + 2, ImageList.ImageSize.Width,
  533. ImageList.ImageSize.Height, node.ImageIndex);
  534. } else if (ImageIndex > -1 && ImageList != null && ImageIndex < ImageList.Images.Count) {
  535. ImageList.Draw (DeviceContext, x, y + 2, ImageList.ImageSize.Width,
  536. ImageList.ImageSize.Height, ImageIndex);
  537. }
  538. }
  539. private void UpdateNodeBounds (TreeNode node, int x, int y, int item_height)
  540. {
  541. int width = (int) (node.Text.Length * Font.Size);
  542. int xoff = indent;
  543. if (!show_root_lines && node.Parent == null)
  544. xoff = 0;
  545. if (image_list != null)
  546. xoff += image_list.ImageSize.Width;
  547. node.UpdateBounds (x + xoff, y, width, item_height);
  548. }
  549. private void DrawNode (TreeNode node, ref int depth, ref int node_count, int item_height,
  550. Font font, ref int visible_node_count, int max_height)
  551. {
  552. node_count++;
  553. int x = (!show_root_lines && node.Parent != null ? depth - 1 : depth) * indent;
  554. int y = (item_height + 1) * (node_count - skipped_nodes - 1);
  555. bool visible = (y >= 0 && y < max_height);
  556. if (visible)
  557. visible_node_count++;
  558. int _n_count = node.nodes.Count;
  559. int middle = y + (item_height / 2);
  560. UpdateNodeBounds (node, x, y, item_height);
  561. if (show_root_lines || node.Parent != null) {
  562. x += 5;
  563. if (_n_count > 0) {
  564. if (show_plus_minus && visible) {
  565. DrawNodePlusMinus (node, x, y, middle);
  566. }
  567. }
  568. x += indent - 5;
  569. }
  570. if (show_lines)
  571. DrawNodeLines (node, dash, x, y, middle, item_height, _n_count);
  572. int ox = x;
  573. if (ImageList != null) {
  574. if (visible)
  575. DrawNodeImage (node, x, y);
  576. // MS leaves the space for the image if the ImageList is
  577. // non null regardless of whether or not an image is drawn
  578. ox += ImageList.ImageSize.Width + 3; // leave a little space so the text isn't against the image
  579. }
  580. if (visible) {
  581. DeviceContext.DrawString (node.Text, font, new SolidBrush (Color.Black), ox, y + 2);
  582. y += item_height + 1;
  583. }
  584. if (node.Bounds.Right > max_node_width)
  585. max_node_width = node.Bounds.Right;
  586. depth++;
  587. if (node.IsExpanded) {
  588. for (int i = 0; i < _n_count; i++) {
  589. int tdepth = depth;
  590. DrawNode (node.nodes [i], ref tdepth, ref node_count, item_height,
  591. font, ref visible_node_count, max_height);
  592. }
  593. }
  594. }
  595. VScrollBar vbar;
  596. bool vbar_added;
  597. int skipped_nodes;
  598. private void AddVerticalScrollBar (int total_nodes, Rectangle bounds)
  599. {
  600. vbar.Maximum = total_nodes;
  601. int height = ClientRectangle.Height;
  602. vbar.LargeChange = height / ItemHeight;
  603. if (add_hscroll)
  604. bounds.Height -= hbar.Height;
  605. vbar.Bounds = bounds;
  606. if (!vbar_added) {
  607. Controls.Add (vbar);
  608. vbar.ValueChanged += new EventHandler (VScrollBarValueChanged);
  609. vbar_added = true;
  610. }
  611. }
  612. HScrollBar hbar;
  613. bool hbar_added;
  614. int hbar_offset;
  615. private void AddHorizontalScrollBar ()
  616. {
  617. if (hbar == null) {
  618. hbar = new HScrollBar ();
  619. }
  620. hbar.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Bottom - hbar.Height,
  621. (add_vscroll ? Width - vbar.Width : Width), hbar.Height);
  622. if (!hbar_added) {
  623. Controls.Add (hbar);
  624. hbar_added = true;
  625. }
  626. }
  627. private void SizeChangedHandler (object sender, EventArgs e)
  628. {
  629. if (max_node_width > ClientRectangle.Width) {
  630. add_hscroll = true;
  631. AddHorizontalScrollBar ();
  632. }
  633. if (vbar != null) {
  634. vbar.Left = Right - vbar.Width;
  635. vbar.Height = Height;
  636. }
  637. if (hbar != null) {
  638. hbar.Top = Bottom - hbar.Height;
  639. hbar.Width = Width;
  640. }
  641. }
  642. private void VScrollBarValueChanged (object sender, EventArgs e)
  643. {
  644. skipped_nodes = vbar.Value;
  645. Refresh ();
  646. }
  647. private int GetVisibleNodeCount ()
  648. {
  649. if (Nodes.Count < 1)
  650. return 0;
  651. OpenTreeNodeEnumerator e = new OpenTreeNodeEnumerator (root_node.Nodes [0]);
  652. int count = 0;
  653. while (e.MoveNext ()) {
  654. count++;
  655. }
  656. return count;
  657. }
  658. // TODO: Handle all sorts o stuff here
  659. private void MouseDownHandler (object sender, MouseEventArgs e)
  660. {
  661. if (!show_plus_minus)
  662. return;
  663. OpenTreeNodeEnumerator walk = new OpenTreeNodeEnumerator (root_node);
  664. // TODO: So much optimization potential here
  665. while (walk.MoveNext ()) {
  666. TreeNode node = (TreeNode) walk.Current;
  667. if (node.PlusMinusBounds.Contains (e.X, e.Y)) {
  668. node.Toggle ();
  669. break;
  670. }
  671. }
  672. }
  673. public event TreeViewEventHandler AfterCheck {
  674. add { on_after_check += value; }
  675. remove { on_after_check -= value; }
  676. }
  677. public event TreeViewEventHandler AfterCollapse {
  678. add { on_after_collapse += value; }
  679. remove { on_after_collapse -= value; }
  680. }
  681. public event TreeViewEventHandler AfterExpand {
  682. add { on_after_expand += value; }
  683. remove { on_after_expand -= value; }
  684. }
  685. public event NodeLabelEditEventHandler AfterLabelEdit {
  686. add { on_after_label_edit += value; }
  687. remove { on_after_label_edit -= value; }
  688. }
  689. public event TreeViewEventHandler AfterSelect {
  690. add { on_after_select += value; }
  691. remove { on_after_select -= value; }
  692. }
  693. public event TreeViewCancelEventHandler BeforeCheck {
  694. add { on_before_check += value; }
  695. remove { on_before_check -= value; }
  696. }
  697. public event TreeViewCancelEventHandler BeforeCollapse {
  698. add { on_before_collapse += value; }
  699. remove { on_before_collapse -= value; }
  700. }
  701. public event TreeViewCancelEventHandler BeforeExpand {
  702. add { on_before_expand += value; }
  703. remove { on_before_expand -= value; }
  704. }
  705. public event NodeLabelEditEventHandler BeforeLabelEdit {
  706. add { on_before_label_edit += value; }
  707. remove { on_before_label_edit -= value; }
  708. }
  709. public event TreeViewCancelEventHandler BeforeSelect {
  710. add { on_before_select += value; }
  711. remove { on_before_select -= value; }
  712. }
  713. public new event PaintEventHandler Paint {
  714. add { base.Paint += value; }
  715. remove { base.Paint -= value; }
  716. }
  717. public new event EventHandler TextChanged {
  718. add { base.TextChanged += value; }
  719. remove { base.TextChanged -= value; }
  720. }
  721. }
  722. }