FrameView.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // FrameView.cs: Frame control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// The FrameView is a container frame that draws a frame around the contents. It is similar to
  14. /// a GroupBox in Windows.
  15. /// </summary>
  16. public class FrameView : View {
  17. View contentView;
  18. ustring title;
  19. /// <summary>
  20. /// The title to be displayed for this <see cref="FrameView"/>.
  21. /// </summary>
  22. /// <value>The title.</value>
  23. public ustring Title {
  24. get => title;
  25. set {
  26. title = value;
  27. SetNeedsDisplay ();
  28. }
  29. }
  30. class ContentView : View {
  31. public ContentView (Rect frame) : base (frame) { }
  32. public ContentView () : base () { }
  33. }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class with
  36. /// an absolute position and a title.
  37. /// </summary>
  38. /// <param name="frame">Frame.</param>
  39. /// <param name="title">Title.</param>
  40. public FrameView (Rect frame, ustring title) : base (frame)
  41. {
  42. var cFrame = new Rect (1, 1 , frame.Width - 2, frame.Height - 2);
  43. this.title = title;
  44. contentView = new ContentView (cFrame);
  45. Initialize ();
  46. }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class with
  49. /// an absolute position, a title and <see cref="View"/>s.
  50. /// </summary>
  51. /// <param name="frame">Frame.</param>
  52. /// <param name="title">Title.</param>
  53. /// /// <param name="views">Views.</param>
  54. public FrameView (Rect frame, ustring title, View[] views) : this (frame, title)
  55. {
  56. foreach (var view in views) {
  57. contentView.Add (view);
  58. }
  59. Initialize ();
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class with
  63. /// a title and the result is suitable to have its X, Y, Width and Height properties computed.
  64. /// </summary>
  65. /// <param name="title">Title.</param>
  66. public FrameView (ustring title)
  67. {
  68. this.title = title;
  69. contentView = new ContentView () {
  70. X = 1,
  71. Y = 1,
  72. Width = Dim.Fill (1),
  73. Height = Dim.Fill (1)
  74. };
  75. Initialize ();
  76. }
  77. void Initialize ()
  78. {
  79. base.Add (contentView);
  80. }
  81. void DrawFrame ()
  82. {
  83. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  84. }
  85. /// <summary>
  86. /// Add the specified <see cref="View"/> to this container.
  87. /// </summary>
  88. /// <param name="view"><see cref="View"/> to add to this container</param>
  89. public override void Add (View view)
  90. {
  91. contentView.Add (view);
  92. if (view.CanFocus)
  93. CanFocus = true;
  94. }
  95. /// <summary>
  96. /// Removes a <see cref="View"/> from this container.
  97. /// </summary>
  98. /// <remarks>
  99. /// </remarks>
  100. public override void Remove (View view)
  101. {
  102. if (view == null)
  103. return;
  104. SetNeedsDisplay ();
  105. var touched = view.Frame;
  106. contentView.Remove (view);
  107. if (contentView.InternalSubviews.Count < 1)
  108. this.CanFocus = false;
  109. }
  110. /// <summary>
  111. /// Removes all <see cref="View"/>s from this container.
  112. /// </summary>
  113. /// <remarks>
  114. /// </remarks>
  115. public override void RemoveAll()
  116. {
  117. contentView.RemoveAll();
  118. }
  119. ///<inheritdoc/>
  120. public override void Redraw (Rect bounds)
  121. {
  122. var padding = 0;
  123. Application.CurrentView = this;
  124. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  125. if (NeedDisplay != null && !NeedDisplay.IsEmpty) {
  126. Driver.SetAttribute (ColorScheme.Normal);
  127. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  128. }
  129. var savedClip = ClipToBounds ();
  130. contentView.Redraw (contentView.Bounds);
  131. Driver.Clip = savedClip;
  132. ClearNeedsDisplay ();
  133. Driver.SetAttribute (ColorScheme.Normal);
  134. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  135. if (HasFocus)
  136. Driver.SetAttribute (ColorScheme.HotNormal);
  137. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  138. Driver.SetAttribute (ColorScheme.Normal);
  139. }
  140. }
  141. }