TreeView.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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.Drawing;
  27. using System.Drawing.Drawing2D;
  28. using System.Collections;
  29. namespace System.Windows.Forms {
  30. public class TreeView : Control {
  31. private string path_separator = "\\";
  32. private int item_height = -1;
  33. private bool sorted;
  34. private TreeNode top_node;
  35. internal TreeNode root_node;
  36. private TreeNodeCollection nodes;
  37. private int total_node_count;
  38. private TreeNode selected_node = null;
  39. private TreeNode focused_node = null;
  40. private bool select_mmove = false;
  41. private ImageList image_list;
  42. private int image_index = -1;
  43. private int selected_image_index = -1;
  44. private bool full_row_select;
  45. private bool hot_tracking;
  46. private int indent = 19;
  47. private bool checkboxes;
  48. private bool label_edit;
  49. private bool scrollable;
  50. private bool show_lines = true;
  51. private bool show_root_lines = true;
  52. private bool show_plus_minus = true;
  53. private bool hide_selection = true;
  54. private bool add_hscroll;
  55. private bool add_vscroll;
  56. private int max_node_width;
  57. private VScrollBar vbar;
  58. private bool vbar_added;
  59. private int skipped_nodes;
  60. private HScrollBar hbar;
  61. private bool hbar_added;
  62. private int hbar_offset;
  63. private int update_stack;
  64. private TreeViewEventHandler on_after_check;
  65. private TreeViewEventHandler on_after_collapse;
  66. private TreeViewEventHandler on_after_expand;
  67. private NodeLabelEditEventHandler on_after_label_edit;
  68. private TreeViewEventHandler on_after_select;
  69. private TreeViewCancelEventHandler on_before_check;
  70. private TreeViewCancelEventHandler on_before_collapse;
  71. private TreeViewCancelEventHandler on_before_expand;
  72. private NodeLabelEditEventHandler on_before_label_edit;
  73. private TreeViewCancelEventHandler on_before_select;
  74. private Pen dash;
  75. private int open_node_count = -1;
  76. public TreeView ()
  77. {
  78. base.background_color = ThemeEngine.Current.ColorWindow;
  79. base.foreground_color = ThemeEngine.Current.ColorWindowText;
  80. root_node = new TreeNode (this);
  81. root_node.Text = "ROOT NODE";
  82. nodes = new TreeNodeCollection (root_node);
  83. root_node.SetNodes (nodes);
  84. MouseDown += new MouseEventHandler (MouseDownHandler);
  85. MouseUp += new MouseEventHandler(MouseUpHandler);
  86. MouseMove += new MouseEventHandler(MouseMoveHandler);
  87. SizeChanged += new EventHandler (SizeChangedHandler);
  88. SetStyle (ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
  89. SetStyle (ControlStyles.UserPaint | ControlStyles.Selectable, true);
  90. dash = new Pen (SystemColors.ControlLight, 1);
  91. }
  92. public string PathSeparator {
  93. get { return path_separator; }
  94. set { path_separator = value; }
  95. }
  96. public bool HideSelection {
  97. get { return hide_selection; }
  98. set {
  99. if (hide_selection == value)
  100. return;
  101. hide_selection = value;
  102. this.Refresh ();
  103. }
  104. }
  105. public bool Sorted {
  106. get { return sorted; }
  107. set {
  108. if (sorted != value)
  109. sorted = value;
  110. if (sorted)
  111. Nodes.Sort ();
  112. }
  113. }
  114. public TreeNode TopNode {
  115. get { return top_node; }
  116. }
  117. public TreeNodeCollection Nodes {
  118. get { return nodes; }
  119. }
  120. public int ItemHeight {
  121. get {
  122. if (item_height == -1)
  123. return FontHeight + 3;
  124. return item_height;
  125. }
  126. set {
  127. if (value == item_height)
  128. return;
  129. item_height = value;
  130. Refresh ();
  131. }
  132. }
  133. public int VisibleCount {
  134. get {
  135. return ClientRectangle.Height / ItemHeight;
  136. }
  137. }
  138. [MonoTODO ("Anything special need to be done here?")]
  139. public ImageList ImageList {
  140. get { return image_list; }
  141. set { image_list = value; }
  142. }
  143. public int ImageIndex {
  144. get { return image_index; }
  145. set {
  146. if (value < -1) {
  147. throw new ArgumentException ("'" + value + "' is not a valid value for 'value'. " +
  148. "'value' must be greater than or equal to 0.");
  149. }
  150. image_index = value;
  151. }
  152. }
  153. public int SelectedImageIndex {
  154. get { return selected_image_index; }
  155. set {
  156. if (value < -1) {
  157. throw new ArgumentException ("'" + value + "' is not a valid value for 'value'. " +
  158. "'value' must be greater than or equal to 0.");
  159. }
  160. }
  161. }
  162. public TreeNode SelectedNode {
  163. get { return selected_node; }
  164. set {
  165. if (selected_node == value)
  166. return;
  167. TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (value, false, TreeViewAction.Unknown);
  168. OnBeforeSelect (e);
  169. if (e.Cancel)
  170. return;
  171. selected_node = value;
  172. focused_node = value;
  173. Refresh ();
  174. OnAfterSelect (new TreeViewEventArgs (value, TreeViewAction.Unknown));
  175. }
  176. }
  177. public override Color BackColor {
  178. get { return base.BackColor;}
  179. set { base.BackColor = value; }
  180. }
  181. public override Image BackgroundImage {
  182. get { return base.BackgroundImage; }
  183. set { base.BackgroundImage = value; }
  184. }
  185. /*
  186. Commented out until this is implemented in Control
  187. public override BorderStyle BorderStyle {
  188. get { return base.BorderStyle; }
  189. set { base.BorderStyle = value; }
  190. }
  191. */
  192. public override Color ForeColor {
  193. get { return base.ForeColor; }
  194. set { base.ForeColor = value; }
  195. }
  196. public override string Text {
  197. get { return base.Text; }
  198. set { base.Text = value; }
  199. }
  200. public bool FullRowSelect {
  201. get { return full_row_select; }
  202. set {
  203. if (value == full_row_select)
  204. return;
  205. full_row_select = value;
  206. Refresh ();
  207. }
  208. }
  209. public bool HotTracking {
  210. get { return hot_tracking; }
  211. set { hot_tracking = value; }
  212. }
  213. public int Indent {
  214. get { return indent; }
  215. set {
  216. if (indent == value)
  217. return;
  218. if (value > 32000) {
  219. throw new ArgumentException ("'" + value + "' is not a valid value for 'Indent'. " +
  220. "'Indent' must be less than or equal to 32000");
  221. }
  222. if (value < 0) {
  223. throw new ArgumentException ("'" + value + "' is not a valid value for 'Indent'. " +
  224. "'Indent' must be greater than or equal to 0.");
  225. }
  226. indent = value;
  227. Refresh ();
  228. }
  229. }
  230. public bool LabelEdit {
  231. get { return label_edit; }
  232. set { label_edit = value; }
  233. }
  234. public bool Scrollable {
  235. get { return scrollable; }
  236. set {
  237. if (scrollable == value)
  238. return;
  239. scrollable = value;
  240. }
  241. }
  242. public bool ShowLines {
  243. get { return show_lines; }
  244. set {
  245. if (show_lines == value)
  246. return;
  247. show_lines = value;
  248. Refresh ();
  249. }
  250. }
  251. public bool ShowRootLines {
  252. get { return show_root_lines; }
  253. set {
  254. if (show_root_lines == value)
  255. return;
  256. show_root_lines = value;
  257. Refresh ();
  258. }
  259. }
  260. public bool CheckBoxes {
  261. get { return checkboxes; }
  262. set {
  263. if (value == checkboxes)
  264. return;
  265. checkboxes = value;
  266. // Match a "bug" in the MS implementation where disabling checkboxes
  267. // collapses the entire tree, but enabling them does not affect the
  268. // state of the tree.
  269. if (!checkboxes)
  270. root_node.CollapseAllUncheck ();
  271. Refresh ();
  272. }
  273. }
  274. public bool ShowPlusMinus {
  275. get { return show_plus_minus; }
  276. set {
  277. if (show_plus_minus == value)
  278. return;
  279. show_plus_minus = value;
  280. Refresh ();
  281. }
  282. }
  283. [MonoTODO ("Anything extra needed here")]
  284. protected override CreateParams CreateParams {
  285. get {
  286. CreateParams cp = base.CreateParams;
  287. return cp;
  288. }
  289. }
  290. protected override Size DefaultSize {
  291. get { return new Size (121, 97); }
  292. }
  293. internal int TotalNodeCount {
  294. get { return total_node_count; }
  295. set { total_node_count = value; }
  296. }
  297. protected override void CreateHandle ()
  298. {
  299. base.CreateHandle ();
  300. }
  301. protected override void Dispose (bool disposing)
  302. {
  303. if (disposing) {
  304. if (image_list != null)
  305. image_list.Dispose ();
  306. }
  307. base.Dispose (disposing);
  308. }
  309. [MonoTODO ("What does the state effect?")]
  310. protected OwnerDrawPropertyBag GetItemRenderStyles (TreeNode node, int state)
  311. {
  312. return node.prop_bag;
  313. }
  314. [MonoTODO ("Need to know if we are editing, not if editing is enabled")]
  315. protected override bool IsInputKey (Keys key_data)
  316. {
  317. if (label_edit && (key_data & Keys.Alt) == 0)
  318. {
  319. switch (key_data & Keys.KeyCode) {
  320. case Keys.Enter:
  321. case Keys.Escape:
  322. case Keys.Prior:
  323. case Keys.Next:
  324. case Keys.End:
  325. case Keys.Home:
  326. case Keys.Left:
  327. case Keys.Up:
  328. case Keys.Right:
  329. case Keys.Down:
  330. return true;
  331. }
  332. }
  333. return base.IsInputKey (key_data);
  334. }
  335. protected virtual void OnAfterCheck (TreeViewEventArgs e)
  336. {
  337. if (on_after_check != null)
  338. on_after_check (this, e);
  339. }
  340. protected internal virtual void OnAfterCollapse (TreeViewEventArgs e)
  341. {
  342. if (on_after_collapse != null)
  343. on_after_collapse (this, e);
  344. }
  345. protected internal virtual void OnAfterExpand (TreeViewEventArgs e)
  346. {
  347. if (on_after_expand != null)
  348. on_after_expand (this, e);
  349. }
  350. protected virtual void OnAfterLabelEdit (NodeLabelEditEventArgs e)
  351. {
  352. if (on_after_label_edit != null)
  353. on_after_label_edit (this, e);
  354. }
  355. protected virtual void OnAfterSelect (TreeViewEventArgs e)
  356. {
  357. if (on_after_select != null)
  358. on_after_select (this, e);
  359. }
  360. protected virtual void OnBeforeCheck (TreeViewCancelEventArgs e)
  361. {
  362. if (on_before_check != null)
  363. on_before_check (this, e);
  364. }
  365. protected internal virtual void OnBeforeCollapse (TreeViewCancelEventArgs e)
  366. {
  367. if (on_before_collapse != null)
  368. on_before_collapse (this, e);
  369. }
  370. protected internal virtual void OnBeforeExpand (TreeViewCancelEventArgs e)
  371. {
  372. if (on_before_expand != null)
  373. on_before_expand (this, e);
  374. }
  375. protected virtual void OnBeforeLabelEdit (NodeLabelEditEventArgs e)
  376. {
  377. if (on_before_label_edit != null)
  378. on_before_label_edit (this, e);
  379. }
  380. protected virtual void OnBeforeSelect (TreeViewCancelEventArgs e)
  381. {
  382. if (on_before_select != null)
  383. on_before_select (this, e);
  384. }
  385. protected override void OnHandleCreated (EventArgs e)
  386. {
  387. base.OnHandleCreated (e);
  388. }
  389. protected override void OnHandleDestroyed (EventArgs e)
  390. {
  391. base.OnHandleDestroyed (e);
  392. }
  393. public void BeginUpdate ()
  394. {
  395. if (!IsHandleCreated)
  396. return;
  397. update_stack++;
  398. }
  399. public void EndUpdate ()
  400. {
  401. if (!IsHandleCreated)
  402. return;
  403. if (update_stack > 1) {
  404. update_stack--;
  405. } else {
  406. update_stack = 0;
  407. Refresh ();
  408. }
  409. }
  410. public void ExpandAll ()
  411. {
  412. root_node.ExpandAll ();
  413. }
  414. public void CollapseAll ()
  415. {
  416. root_node.CollapseAll ();
  417. }
  418. public TreeNode GetNodeAt (Point pt)
  419. {
  420. return GetNodeAt (pt.X, pt.Y);
  421. }
  422. public TreeNode GetNodeAt (int x, int y)
  423. {
  424. TreeNode node = GetNodeAt (y);
  425. if (node == null || !IsTextArea (node, x))
  426. return null;
  427. return node;
  428. }
  429. private TreeNode GetNodeAt (int y)
  430. {
  431. if (top_node == null)
  432. top_node = nodes [0];
  433. OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (TopNode);
  434. int move = y / ItemHeight + skipped_nodes;
  435. for (int i = -1; i < move; i++) {
  436. if (!o.MoveNext ())
  437. return null;
  438. }
  439. return o.CurrentNode;
  440. }
  441. private bool IsTextArea (TreeNode node, int x)
  442. {
  443. return node != null && node.Bounds.Left <= x && node.Bounds.Right >= x;
  444. }
  445. public int GetNodeCount (bool include_subtrees)
  446. {
  447. return root_node.GetNodeCount (include_subtrees);
  448. }
  449. public override string ToString ()
  450. {
  451. int count = Nodes.Count;
  452. if (count < 0)
  453. return String.Concat (base.ToString (), "Node Count: 0");
  454. return String.Concat (base.ToString (), "Node Count: ", count, " Nodes[0]: ", Nodes [0]);
  455. }
  456. protected override void WndProc(ref Message m)
  457. {
  458. switch ((Msg) m.Msg) {
  459. case Msg.WM_PAINT: {
  460. PaintEventArgs paint_event;
  461. paint_event = XplatUI.PaintEventStart (Handle);
  462. DoPaint (paint_event);
  463. XplatUI.PaintEventEnd (Handle);
  464. return;
  465. }
  466. case Msg.WM_LBUTTONDBLCLK:
  467. int val = m.LParam.ToInt32();
  468. DoubleClickHandler (null, new MouseEventArgs (MouseButtons.Left, 2, val & 0xffff, (val>>16) & 0xffff, 0));
  469. break;
  470. }
  471. base.WndProc (ref m);
  472. }
  473. // TODO: Update from supplied node down
  474. internal void UpdateBelow (TreeNode node)
  475. {
  476. // We need to update the current node so the plus/minus block gets update too
  477. Rectangle invalid = new Rectangle (0, node.Bounds.Top, Width, Height - node.Bounds.Top);
  478. Invalidate (invalid);
  479. }
  480. internal void UpdateNode (TreeNode node)
  481. {
  482. Rectangle invalid = new Rectangle (0, node.Bounds.Top, Width, node.Bounds.Height);
  483. Invalidate (invalid);
  484. }
  485. int draw_count = 0;
  486. private void DoPaint (PaintEventArgs pe)
  487. {
  488. if (Width <= 0 || Height <= 0 || Visible == false)
  489. return;
  490. Draw (pe.ClipRectangle);
  491. pe.Graphics.DrawImage (ImageBuffer, pe.ClipRectangle, pe.ClipRectangle, GraphicsUnit.Pixel);
  492. }
  493. private void Draw (Rectangle clip)
  494. {
  495. DateTime start = DateTime.Now;
  496. if (top_node == null && Nodes.Count > 0)
  497. top_node = nodes [0];
  498. // Decide if we need a scrollbar
  499. int old_open_node_count = open_node_count;
  500. open_node_count = GetOpenNodeCount ();
  501. int node_count = 0;
  502. Rectangle fill = ClientRectangle;
  503. add_vscroll = false;
  504. add_hscroll = false;
  505. add_vscroll = (open_node_count * ItemHeight) > ClientRectangle.Height;
  506. DeviceContext.FillRectangle (new SolidBrush (BackColor), fill);
  507. int depth = 0;
  508. int item_height = ItemHeight;
  509. Font font = Font;
  510. int height = ClientRectangle.Height;
  511. int visible_node_count = 0;
  512. foreach (TreeNode node in nodes) {
  513. DrawNode (node, clip, ref depth, ref node_count, item_height,
  514. font, ref visible_node_count, height);
  515. depth = 0;
  516. }
  517. if (max_node_width > ClientRectangle.Width)
  518. add_hscroll = true;
  519. if (add_vscroll)
  520. add_hscroll = max_node_width > ClientRectangle.Width - ThemeEngine.Current.VScrollBarDefaultSize.Width;
  521. if (add_hscroll)
  522. add_vscroll = (open_node_count * ItemHeight) > ClientRectangle.Height - ThemeEngine.Current.HScrollBarDefaultSize.Width;
  523. if (add_hscroll) {
  524. AddHorizontalScrollBar ();
  525. } else if (hbar != null) {
  526. hbar_offset = 0;
  527. hbar.Visible = false;
  528. }
  529. if (add_vscroll) {
  530. AddVerticalScrollBar (open_node_count, old_open_node_count != open_node_count);
  531. } else if (vbar != null) {
  532. vbar.Visible = false;
  533. skipped_nodes = 0;
  534. }
  535. if (add_hscroll && add_vscroll) {
  536. Rectangle grip = new Rectangle (hbar.Right, vbar.Bottom, vbar.Width, hbar.Height);
  537. DeviceContext.FillRectangle (new SolidBrush (ThemeEngine.Current.ColorButtonFace), grip);
  538. ControlPaint.DrawSizeGrip (DeviceContext, ThemeEngine.Current.ColorButtonFace, grip);
  539. }
  540. Console.WriteLine ("treeview drawing time: " + (DateTime.Now - start));
  541. Console.WriteLine ("node count: " + node_count);
  542. Console.WriteLine ("total node count: " + total_node_count);
  543. }
  544. private void DumpNode (TreeNode node, ref int depth)
  545. {
  546. for (int i = 0; i < depth; i++)
  547. Console.Write ("****");
  548. Console.WriteLine (node.Text);
  549. if (node.PrevNode != null)
  550. Console.WriteLine (" -- " + node.PrevNode.Text);
  551. depth++;
  552. foreach (TreeNode child in node.Nodes) {
  553. DumpNode (child, ref depth);
  554. }
  555. depth--;
  556. }
  557. private void DrawNodePlusMinus (TreeNode node, int x, int y, int middle)
  558. {
  559. node.UpdatePlusMinusBounds (x, middle - 4, 8, 8);
  560. DeviceContext.DrawRectangle (SystemPens.ControlDark, node.PlusMinusBounds);
  561. if (node.IsExpanded) {
  562. DeviceContext.DrawLine (SystemPens.ControlDarkDark, x + 2, middle, x + 6, middle);
  563. } else {
  564. DeviceContext.DrawLine (SystemPens.ControlDarkDark, x + 2, middle, x + 6, middle);
  565. DeviceContext.DrawLine (SystemPens.ControlDarkDark, x + 4, middle - 2, x + 4, middle + 2);
  566. }
  567. }
  568. private void DrawNodeCheckBox (TreeNode node, int x, int y)
  569. {
  570. int offset = (ItemHeight - 13);
  571. node.UpdateCheckBoxBounds (x + 3, y + offset, 10, 10);
  572. DeviceContext.DrawRectangle (new Pen (Color.Black, 2), x + 0.5F + 3, y + 0.5F + offset, 11, 11);
  573. if (node.Checked) {
  574. Pen check_pen = new Pen (Color.Black, 1);
  575. DeviceContext.DrawLine (check_pen, x + 6, y + offset + 5, x + 8, y + offset + 8);
  576. DeviceContext.DrawLine (check_pen, x + 6, y + offset + 6, x + 8, y + offset + 9);
  577. DeviceContext.DrawLine (check_pen, x + 7, y + offset + 8, x + 13, y + offset + 3);
  578. DeviceContext.DrawLine (check_pen, x + 7, y + offset + 9, x + 13, y + offset + 4);
  579. }
  580. }
  581. private void DrawNodeLines (TreeNode node, Pen dash, int x, int y, int middle, int item_height, int node_count)
  582. {
  583. int ladjust = 9; // left adjust
  584. int radjust = 0; // right adjust
  585. if (node_count > 0 && show_plus_minus)
  586. ladjust = 13;
  587. if (checkboxes)
  588. radjust = 3;
  589. DeviceContext.DrawLine (dash, x - indent + ladjust, middle, x + radjust, middle);
  590. int ly = 0;
  591. if (node.PrevNode != null) {
  592. int prevadjust = (node.Nodes.Count > 0 && show_plus_minus ? (node.PrevNode.Nodes.Count == 0 ? 0 : 4) :
  593. (node.PrevNode.Nodes.Count == 0 ? 0 : 4));
  594. int myadjust = (node.Nodes.Count > 0 && show_plus_minus ? 4 : 0);
  595. ly = node.PrevNode.Bounds.Bottom - (item_height / 2) + prevadjust;
  596. DeviceContext.DrawLine (dash, x - indent + 9, middle - myadjust, x - indent + 9, ly);
  597. } else if (node.Parent != null) {
  598. int myadjust = (node.Nodes.Count > 0 && show_plus_minus ? 4 : 0);
  599. ly = node.Parent.Bounds.Bottom - 1;
  600. DeviceContext.DrawLine (dash, x - indent + 9, middle - myadjust, x - indent + 9, ly);
  601. }
  602. }
  603. private void DrawNodeImage (TreeNode node, int x, int y)
  604. {
  605. if (node.ImageIndex > -1 && ImageList != null && node.ImageIndex < ImageList.Images.Count) {
  606. ImageList.Draw (DeviceContext, x, y + 2, ImageList.ImageSize.Width,
  607. ImageList.ImageSize.Height, node.ImageIndex);
  608. } else if (ImageIndex > -1 && ImageList != null && ImageIndex < ImageList.Images.Count) {
  609. ImageList.Draw (DeviceContext, x, y + 2, ImageList.ImageSize.Width,
  610. ImageList.ImageSize.Height, ImageIndex);
  611. }
  612. }
  613. private void UpdateNodeBounds (TreeNode node, int x, int y, int item_height)
  614. {
  615. int width = (int) (node.Text.Length * Font.Size);
  616. int xoff = indent;
  617. if (!show_root_lines && node.Parent == null)
  618. xoff = 0;
  619. if (image_list != null)
  620. xoff += image_list.ImageSize.Width;
  621. node.UpdateBounds (x + xoff, y, width, item_height);
  622. }
  623. private void DrawNode (TreeNode node, Rectangle clip, ref int depth, ref int node_count, int item_height,
  624. Font font, ref int visible_node_count, int max_height)
  625. {
  626. node_count++;
  627. int x = (!show_root_lines && node.Parent != null ? depth - 1 : depth) * indent - hbar_offset;
  628. int y = item_height * (node_count - skipped_nodes - 1);
  629. bool visible = (y >= 0 && y < max_height);
  630. if (visible)
  631. visible_node_count++;
  632. // The thing is totally out of the clipping rectangle
  633. if (clip.Top > y || clip.Bottom < y)
  634. visible = false;
  635. int _n_count = node.nodes.Count;
  636. int middle = y + (item_height / 2);
  637. if (show_root_lines || node.Parent != null) {
  638. x += 5;
  639. if (_n_count > 0) {
  640. if (show_plus_minus && visible) {
  641. DrawNodePlusMinus (node, x, y, middle);
  642. }
  643. }
  644. x += indent - 5;
  645. }
  646. int ox = x;
  647. if (checkboxes) {
  648. DrawNodeCheckBox (node, ox, y);
  649. ox += 19;
  650. }
  651. if (show_lines)
  652. DrawNodeLines (node, dash, x, y, middle, item_height, _n_count);
  653. if (ImageList != null) {
  654. if (visible)
  655. DrawNodeImage (node, ox, y);
  656. // MS leaves the space for the image if the ImageList is
  657. // non null regardless of whether or not an image is drawn
  658. ox += ImageList.ImageSize.Width + 3; // leave a little space so the text isn't against the image
  659. }
  660. UpdateNodeBounds (node, x, y, item_height);
  661. if (visible) {
  662. Rectangle r = node.Bounds;
  663. StringFormat format = new StringFormat ();
  664. format.LineAlignment = StringAlignment.Center;
  665. r.Y += 2; // we have to adjust this to get nice middle alignment
  666. r.X = ox;
  667. Color text_color = (Focused && SelectedNode == node ? ThemeEngine.Current.ColorHilightText : node.ForeColor);
  668. if (Focused) {
  669. if (SelectedNode == node)
  670. DeviceContext.FillRectangle (new SolidBrush (ThemeEngine.Current.ColorHilight), node.Bounds);
  671. if (focused_node == node) {
  672. Pen dot_pen = new Pen (ThemeEngine.Current.ColorButtonHilight, 1);
  673. dot_pen.DashStyle = DashStyle.Dot;
  674. DeviceContext.DrawRectangle (new Pen (ThemeEngine.Current.ColorButtonDkShadow),
  675. node.Bounds.X, node.Bounds.Y, node.Bounds.Width - 1, node.Bounds.Height - 1);
  676. DeviceContext.DrawRectangle (dot_pen, node.Bounds.X, node.Bounds.Y, node.Bounds.Width - 1, node.Bounds.Height - 1);
  677. }
  678. } else {
  679. if (!HideSelection && SelectedNode == node)
  680. DeviceContext.FillRectangle (new SolidBrush (ThemeEngine.Current.ColorButtonFace), node.Bounds);
  681. }
  682. DeviceContext.DrawString (node.Text, font, new SolidBrush (text_color), r, format);
  683. y += item_height + 1;
  684. }
  685. if (node.Bounds.Right > max_node_width)
  686. max_node_width = node.Bounds.Right;
  687. depth++;
  688. if (node.IsExpanded) {
  689. for (int i = 0; i < _n_count; i++) {
  690. int tdepth = depth;
  691. DrawNode (node.nodes [i], clip, ref tdepth, ref node_count, item_height,
  692. font, ref visible_node_count, max_height);
  693. }
  694. }
  695. }
  696. private void AddVerticalScrollBar (int total_nodes, bool count_changed)
  697. {
  698. if (vbar == null) {
  699. vbar = new VScrollBar ();
  700. count_changed = true;
  701. }
  702. vbar.Bounds = new Rectangle (ClientRectangle.Width - vbar.Width,
  703. 0, vbar.Width, (add_hscroll ? Height - ThemeEngine.Current.HScrollBarDefaultSize.Height : Height));
  704. if (count_changed) {
  705. vbar.Maximum = total_nodes;
  706. int height = ClientRectangle.Height;
  707. vbar.LargeChange = height / ItemHeight;
  708. }
  709. if (!vbar_added) {
  710. Controls.Add (vbar);
  711. vbar.ValueChanged += new EventHandler (VScrollBarValueChanged);
  712. vbar_added = true;
  713. }
  714. vbar.Visible = true;
  715. }
  716. private void AddHorizontalScrollBar ()
  717. {
  718. if (hbar == null)
  719. hbar = new HScrollBar ();
  720. hbar.Bounds = new Rectangle (ClientRectangle.Left, ClientRectangle.Bottom - hbar.Height,
  721. (add_vscroll ? Width - ThemeEngine.Current.VScrollBarDefaultSize.Width : Width), hbar.Height);
  722. if (!hbar_added) {
  723. Controls.Add (hbar);
  724. hbar.ValueChanged += new EventHandler (HScrollBarValueChanged);
  725. hbar_added = true;
  726. }
  727. hbar.Visible = true;
  728. }
  729. private void SizeChangedHandler (object sender, EventArgs e)
  730. {
  731. if (max_node_width > ClientRectangle.Width) {
  732. add_hscroll = true;
  733. AddHorizontalScrollBar ();
  734. }
  735. if (vbar != null) {
  736. vbar.Left = Right - vbar.Width;
  737. vbar.Height = Height;
  738. }
  739. if (hbar != null) {
  740. hbar.Top = Bottom - hbar.Height;
  741. hbar.Width = Width;
  742. }
  743. }
  744. private void VScrollBarValueChanged (object sender, EventArgs e)
  745. {
  746. skipped_nodes = vbar.Value;
  747. Refresh ();
  748. }
  749. private void HScrollBarValueChanged(object sender, EventArgs e)
  750. {
  751. hbar_offset = hbar.Value;
  752. Refresh ();
  753. }
  754. private int GetOpenNodeCount ()
  755. {
  756. if (Nodes.Count < 1)
  757. return 0;
  758. OpenTreeNodeEnumerator e = new OpenTreeNodeEnumerator (root_node.Nodes [0]);
  759. int count = 0;
  760. while (e.MoveNext ()) {
  761. count++;
  762. }
  763. return count;
  764. }
  765. // TODO: Handle all sorts o stuff here
  766. private void MouseDownHandler (object sender, MouseEventArgs e)
  767. {
  768. if (!show_plus_minus)
  769. return;
  770. TreeNode node = GetNodeAt (e.Y);
  771. if (node == null)
  772. return;
  773. if (IsTextArea (node, e.X)) {
  774. selected_node = node;
  775. Console.WriteLine ("selected node");
  776. if (selected_node != focused_node) {
  777. select_mmove = true;
  778. Refresh ();
  779. }
  780. } else if (node.PlusMinusBounds.Contains (e.X, e.Y)) {
  781. node.Toggle ();
  782. return;
  783. } else if (node.CheckBoxBounds.Contains (e.X, e.Y)) {
  784. node.Checked = !node.Checked;
  785. return;
  786. }
  787. }
  788. private void MouseUpHandler (object sender, MouseEventArgs e) {
  789. if (!select_mmove)
  790. return;
  791. select_mmove = false;
  792. TreeViewCancelEventArgs ce = new TreeViewCancelEventArgs (selected_node, false, TreeViewAction.ByMouse);
  793. OnBeforeSelect (ce);
  794. if (!ce.Cancel) {
  795. focused_node = selected_node;
  796. OnAfterSelect (new TreeViewEventArgs (selected_node, TreeViewAction.ByMouse));
  797. } else {
  798. selected_node = focused_node;
  799. }
  800. Refresh();
  801. }
  802. private void MouseMoveHandler (object sender, MouseEventArgs e) {
  803. if(!select_mmove)
  804. return;
  805. TreeNode node = GetNodeAt(e.X,e.Y);
  806. if(node == selected_node)
  807. return;
  808. selected_node = focused_node;
  809. select_mmove = false;
  810. Refresh();
  811. }
  812. private void DoubleClickHandler (object sender, MouseEventArgs e) {
  813. TreeNode node = GetNodeAt(e.X,e.Y);
  814. if(node != null) {
  815. node.Toggle();
  816. }
  817. }
  818. public event TreeViewEventHandler AfterCheck {
  819. add { on_after_check += value; }
  820. remove { on_after_check -= value; }
  821. }
  822. public event TreeViewEventHandler AfterCollapse {
  823. add { on_after_collapse += value; }
  824. remove { on_after_collapse -= value; }
  825. }
  826. public event TreeViewEventHandler AfterExpand {
  827. add { on_after_expand += value; }
  828. remove { on_after_expand -= value; }
  829. }
  830. public event NodeLabelEditEventHandler AfterLabelEdit {
  831. add { on_after_label_edit += value; }
  832. remove { on_after_label_edit -= value; }
  833. }
  834. public event TreeViewEventHandler AfterSelect {
  835. add { on_after_select += value; }
  836. remove { on_after_select -= value; }
  837. }
  838. public event TreeViewCancelEventHandler BeforeCheck {
  839. add { on_before_check += value; }
  840. remove { on_before_check -= value; }
  841. }
  842. public event TreeViewCancelEventHandler BeforeCollapse {
  843. add { on_before_collapse += value; }
  844. remove { on_before_collapse -= value; }
  845. }
  846. public event TreeViewCancelEventHandler BeforeExpand {
  847. add { on_before_expand += value; }
  848. remove { on_before_expand -= value; }
  849. }
  850. public event NodeLabelEditEventHandler BeforeLabelEdit {
  851. add { on_before_label_edit += value; }
  852. remove { on_before_label_edit -= value; }
  853. }
  854. public event TreeViewCancelEventHandler BeforeSelect {
  855. add { on_before_select += value; }
  856. remove { on_before_select -= value; }
  857. }
  858. public new event PaintEventHandler Paint {
  859. add { base.Paint += value; }
  860. remove { base.Paint -= value; }
  861. }
  862. public new event EventHandler TextChanged {
  863. add { base.TextChanged += value; }
  864. remove { base.TextChanged -= value; }
  865. }
  866. }
  867. }