SplitContainerExample.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Terminal.Gui;
  2. using Terminal.Gui.Graphs;
  3. using NStack;
  4. namespace UICatalog.Scenarios {
  5. [ScenarioMetadata (Name: "Split Container", Description: "Demonstrates the SplitContainer functionality")]
  6. [ScenarioCategory ("Controls")]
  7. [ScenarioCategory ("LineView")]
  8. public class SplitContainerExample : Scenario {
  9. private SplitContainer splitContainer;
  10. private SplitContainer nestedSplitContainer;
  11. private MenuItem miVertical;
  12. private MenuItem miShowBoth;
  13. private MenuItem miShowPanel1;
  14. private MenuItem miShowPanel2;
  15. private MenuItem miShowNeither;
  16. private MenuItem miSplitContainer1Border;
  17. private MenuItem minestedSplitContainerBorder;
  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),
  33. };
  34. nestedSplitContainer = new SplitContainer(){
  35. Width = Dim.Fill(),
  36. Height = Dim.Fill(),
  37. Orientation = Orientation.Horizontal
  38. };
  39. splitContainer.Panel1MinSize = 4;
  40. splitContainer.Panel2MinSize = 4;
  41. Label lbl1;
  42. splitContainer.Panel1Title = "Hello";
  43. splitContainer.Panel1.Add (lbl1 = new Label ("Type Something:") { Y = 0 });
  44. splitContainer.Panel1.Add (new TextField () { Width = Dim.Fill (), Y = 0, X = Pos.Right (lbl1) + 1 });
  45. Label lbl2;
  46. splitContainer.Panel2Title = "World";
  47. splitContainer.Panel2.Add(nestedSplitContainer);
  48. nestedSplitContainer.Panel1.Add (new TextView ()
  49. {
  50. Width = Dim.Fill(),
  51. Height = Dim.Fill(),
  52. Text = GenerateLotsOfText(),
  53. AllowsTab = false,
  54. WordWrap = true,
  55. });
  56. nestedSplitContainer.IntegratedBorder = BorderStyle.None;
  57. nestedSplitContainer.Panel2.Add (lbl2 = new Label ("Type Here Too:") { Y = 0 });
  58. nestedSplitContainer.Panel2.Add (new TextField () { Width = Dim.Fill (), Y = 0, X = Pos.Right (lbl2) + 1 });
  59. nestedSplitContainer.Panel2.Add (new Label ("Here is a Text box:") { Y = 1 });
  60. nestedSplitContainer.Panel2.Add (new TextView () { Y = 2, 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. miSplitContainer1Border = new MenuItem ("_Outer Panel Border", "", () => ToggleBorder(miSplitContainer1Border, splitContainer))
  73. {
  74. Checked = splitContainer.IntegratedBorder == BorderStyle.Single,
  75. CheckType = MenuItemCheckStyle.Checked
  76. },
  77. minestedSplitContainerBorder = new MenuItem ("_Inner Panel Border", "", () => ToggleBorder(minestedSplitContainerBorder,nestedSplitContainer))
  78. {
  79. Checked = nestedSplitContainer.IntegratedBorder == BorderStyle.Single,
  80. CheckType = MenuItemCheckStyle.Checked
  81. },
  82. new MenuBarItem ("_Show", new MenuItem [] {
  83. miShowBoth = new MenuItem ("Both", "",()=>{
  84. splitContainer.Panel1.Visible = true;
  85. splitContainer.Panel2.Visible = true;
  86. UpdateShowMenuCheckedStates();
  87. }),
  88. miShowPanel1 = new MenuItem ("Panel 1", "", () => {
  89. splitContainer.Panel1.Visible = true;
  90. splitContainer.Panel2.Visible = false;
  91. UpdateShowMenuCheckedStates();
  92. }),
  93. miShowPanel2 = new MenuItem ("Panel 2", "", () => {
  94. splitContainer.Panel1.Visible = false;
  95. splitContainer.Panel2.Visible = true;
  96. UpdateShowMenuCheckedStates();
  97. }),
  98. miShowNeither = new MenuItem ("Neither", "",()=>{
  99. splitContainer.Panel1.Visible = false;
  100. splitContainer.Panel2.Visible = false;
  101. UpdateShowMenuCheckedStates();
  102. }),
  103. })
  104. }),
  105. });
  106. UpdateShowMenuCheckedStates ();
  107. Application.Top.Add (menu);
  108. }
  109. private ustring GenerateLotsOfText ()
  110. {
  111. return "Hello There ".Repeat(100);
  112. }
  113. private void UpdateShowMenuCheckedStates ()
  114. {
  115. miShowBoth.Checked = (splitContainer.Panel1.Visible) && (splitContainer.Panel2.Visible);
  116. miShowBoth.CheckType = MenuItemCheckStyle.Checked;
  117. miShowPanel1.Checked = splitContainer.Panel1.Visible && !splitContainer.Panel2.Visible;
  118. miShowPanel1.CheckType = MenuItemCheckStyle.Checked;
  119. miShowPanel2.Checked = !splitContainer.Panel1.Visible && splitContainer.Panel2.Visible;
  120. miShowPanel2.CheckType = MenuItemCheckStyle.Checked;
  121. miShowNeither.Checked = (!splitContainer.Panel1.Visible) && (!splitContainer.Panel2.Visible);
  122. miShowNeither.CheckType = MenuItemCheckStyle.Checked;
  123. }
  124. public void ToggleOrientation ()
  125. {
  126. miVertical.Checked = !miVertical.Checked;
  127. splitContainer.Orientation = miVertical.Checked ? Orientation.Vertical : Orientation.Horizontal;
  128. nestedSplitContainer.Orientation = miVertical.Checked ? Orientation.Horizontal : Orientation.Vertical;
  129. }
  130. private void ToggleBorder (MenuItem menuItem, SplitContainer splitContainer)
  131. {
  132. menuItem.Checked = !menuItem.Checked;
  133. if(menuItem.Checked) {
  134. splitContainer.IntegratedBorder = BorderStyle.Single;
  135. } else {
  136. splitContainer.IntegratedBorder = BorderStyle.None;
  137. }
  138. }
  139. private void Quit ()
  140. {
  141. Application.RequestStop ();
  142. }
  143. }
  144. }