SplitContainerExample.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Terminal.Gui;
  2. using System;
  3. using Terminal.Gui.Graphs;
  4. using NStack;
  5. using System.Linq;
  6. namespace UICatalog.Scenarios {
  7. [ScenarioMetadata (Name: "Split Container", Description: "Demonstrates the SplitContainer functionality")]
  8. [ScenarioCategory ("Controls")]
  9. [ScenarioCategory ("LineView")]
  10. public class SplitContainerExample : Scenario {
  11. private SplitContainer splitContainer;
  12. private SplitContainer splitContainer2;
  13. private MenuItem miVertical;
  14. private MenuItem miShowBoth;
  15. private MenuItem miShowPanel1;
  16. private MenuItem miShowPanel2;
  17. private MenuItem miShowNeither;
  18. /// <summary>
  19. /// Setup the scenario.
  20. /// </summary>
  21. public override void Setup ()
  22. {
  23. // Scenario Windows.
  24. Win.Title = this.GetName ();
  25. Win.Y = 1;
  26. Win.Add (new Label ("This is a SplitContainer with a minimum panel size of 4. Drag the splitter to resize:"));
  27. splitContainer = new SplitContainer {
  28. Y = 2,
  29. X = 2,
  30. Width = Dim.Fill () - 2,
  31. Height = Dim.Fill () - 1,
  32. SplitterDistance = Pos.Percent (50), // TODO: get this to work with drag resizing and percents
  33. };
  34. splitContainer2 = new SplitContainer(){
  35. Width = Dim.Fill(),
  36. Height = Dim.Fill(),
  37. Orientation = Orientation.Horizontal
  38. };
  39. splitContainer.Panels [0].MinSize = 4;
  40. splitContainer.Panels [1].MinSize = 4;
  41. Label lbl1;
  42. splitContainer.Panels [0].Title = "Hello";
  43. splitContainer.Panels [0].Add (lbl1 = new Label ("Type Something:") { Y = 1 });
  44. splitContainer.Panels [0].Add (new TextField () { Width = Dim.Fill (), Y = 1, X = Pos.Right (lbl1) + 1 });
  45. Label lbl2;
  46. splitContainer.Panels [1].Title = "World";
  47. splitContainer.Panels[1].Add(splitContainer2);
  48. splitContainer2.Panels [0].Add (new TextView ()
  49. {
  50. Width = Dim.Fill(),
  51. Height = Dim.Fill(),
  52. Text = GenerateLotsOfText(),
  53. AllowsTab = false,
  54. WordWrap = true,
  55. });
  56. splitContainer2.Border.BorderStyle = BorderStyle.None;
  57. splitContainer2.Panels [1].Add (lbl2 = new Label ("Type Here Too:") { Y = 1 });
  58. splitContainer2.Panels [1].Add (new TextField () { Width = Dim.Fill (), Y = 1, X = Pos.Right (lbl2) + 1 });
  59. splitContainer2.Panels [1].Add (new Label ("Here is a Text box:") { Y = 3 });
  60. splitContainer2.Panels [1].Add (new TextView () { Y = 4, Width = Dim.Fill (), Height = Dim.Fill (), AllowsTab = false });
  61. Win.Add (splitContainer);
  62. var menu = new MenuBar (new MenuBarItem [] {
  63. new MenuBarItem ("_File", new MenuItem [] {
  64. new MenuItem ("_Quit", "", () => Quit()),
  65. }),
  66. new MenuBarItem ("_Options", new MenuItem [] {
  67. miVertical = new MenuItem ("_Vertical", "", () => ToggleOrientation())
  68. {
  69. Checked = splitContainer.Orientation == Orientation.Vertical,
  70. CheckType = MenuItemCheckStyle.Checked
  71. },
  72. new MenuBarItem ("_Show", new MenuItem [] {
  73. miShowBoth = new MenuItem ("Both", "",()=>{
  74. splitContainer.Panels [0].Visible = true;
  75. splitContainer.Panels [1].Visible = true;
  76. UpdateShowMenuCheckedStates();
  77. }),
  78. miShowPanel1 = new MenuItem ("Panel 1", "", () => {
  79. splitContainer.Panels [0].Visible = true;
  80. splitContainer.Panels [1].Visible = false;
  81. UpdateShowMenuCheckedStates();
  82. }),
  83. miShowPanel2 = new MenuItem ("Panel 2", "", () => {
  84. splitContainer.Panels [0].Visible = false;
  85. splitContainer.Panels [1].Visible = true;
  86. UpdateShowMenuCheckedStates();
  87. }),
  88. miShowNeither = new MenuItem ("Neither", "",()=>{
  89. splitContainer.Panels [0].Visible = false;
  90. splitContainer.Panels [1].Visible = false;
  91. UpdateShowMenuCheckedStates();
  92. }),
  93. })
  94. }),
  95. });
  96. UpdateShowMenuCheckedStates ();
  97. Application.Top.Add (menu);
  98. }
  99. private ustring GenerateLotsOfText ()
  100. {
  101. return "Hello There ".Repeat(100);
  102. }
  103. private void UpdateShowMenuCheckedStates ()
  104. {
  105. miShowBoth.Checked = (splitContainer.Panels [0].Visible) && (splitContainer.Panels [1].Visible);
  106. miShowBoth.CheckType = MenuItemCheckStyle.Checked;
  107. miShowPanel1.Checked = splitContainer.Panels [0].Visible && !splitContainer.Panels [1].Visible;
  108. miShowPanel1.CheckType = MenuItemCheckStyle.Checked;
  109. miShowPanel2.Checked = !splitContainer.Panels [0].Visible && splitContainer.Panels [1].Visible;
  110. miShowPanel2.CheckType = MenuItemCheckStyle.Checked;
  111. miShowNeither.Checked = (!splitContainer.Panels [0].Visible) && (!splitContainer.Panels [1].Visible);
  112. miShowNeither.CheckType = MenuItemCheckStyle.Checked;
  113. }
  114. public void ToggleOrientation ()
  115. {
  116. miVertical.Checked = !miVertical.Checked;
  117. splitContainer.Orientation = miVertical.Checked ? Orientation.Vertical : Orientation.Horizontal;
  118. splitContainer2.Orientation = miVertical.Checked ? Orientation.Horizontal : Orientation.Vertical;
  119. }
  120. private void Quit ()
  121. {
  122. Application.RequestStop ();
  123. }
  124. }
  125. }