FrameView.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 using <see cref="LayoutStyle.Absolute"/> layout.
  36. /// </summary>
  37. /// <param name="frame">Frame.</param>
  38. /// <param name="title">Title.</param>
  39. public FrameView (Rect frame, ustring title) : base (frame)
  40. {
  41. var cFrame = new Rect (1, 1, frame.Width - 2, frame.Height - 2);
  42. this.title = title;
  43. contentView = new ContentView (cFrame);
  44. Initialize ();
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  48. /// </summary>
  49. /// <param name="frame">Frame.</param>
  50. /// <param name="title">Title.</param>
  51. /// /// <param name="views">Views.</param>
  52. public FrameView (Rect frame, ustring title, View [] views) : this (frame, title)
  53. {
  54. foreach (var view in views) {
  55. contentView.Add (view);
  56. }
  57. Initialize ();
  58. }
  59. /// <summary>
  60. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  61. /// </summary>
  62. /// <param name="title">Title.</param>
  63. public FrameView (ustring title)
  64. {
  65. this.title = title;
  66. contentView = new ContentView () {
  67. X = 1,
  68. Y = 1,
  69. Width = Dim.Fill (1),
  70. Height = Dim.Fill (1)
  71. };
  72. Initialize ();
  73. }
  74. /// <summary>
  75. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  76. /// </summary>
  77. public FrameView () : this (title: string.Empty) { }
  78. void Initialize ()
  79. {
  80. base.Add (contentView);
  81. }
  82. void DrawFrame ()
  83. {
  84. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  85. }
  86. /// <summary>
  87. /// Add the specified <see cref="View"/> to this container.
  88. /// </summary>
  89. /// <param name="view"><see cref="View"/> to add to this container</param>
  90. public override void Add (View view)
  91. {
  92. contentView.Add (view);
  93. if (view.CanFocus)
  94. CanFocus = true;
  95. }
  96. /// <summary>
  97. /// Removes a <see cref="View"/> from this container.
  98. /// </summary>
  99. /// <remarks>
  100. /// </remarks>
  101. public override void Remove (View view)
  102. {
  103. if (view == null)
  104. return;
  105. SetNeedsDisplay ();
  106. var touched = view.Frame;
  107. contentView.Remove (view);
  108. if (contentView.InternalSubviews.Count < 1)
  109. this.CanFocus = false;
  110. }
  111. /// <summary>
  112. /// Removes all <see cref="View"/>s from this container.
  113. /// </summary>
  114. /// <remarks>
  115. /// </remarks>
  116. public override void RemoveAll ()
  117. {
  118. contentView.RemoveAll ();
  119. }
  120. ///<inheritdoc/>
  121. public override void Redraw (Rect bounds)
  122. {
  123. var padding = 0;
  124. Application.CurrentView = this;
  125. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  126. if (NeedDisplay != null && !NeedDisplay.IsEmpty) {
  127. Driver.SetAttribute (ColorScheme.Normal);
  128. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  129. }
  130. var savedClip = ClipToBounds ();
  131. contentView.Redraw (contentView.Bounds);
  132. Driver.Clip = savedClip;
  133. ClearNeedsDisplay ();
  134. Driver.SetAttribute (ColorScheme.Normal);
  135. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  136. if (HasFocus)
  137. Driver.SetAttribute (ColorScheme.HotNormal);
  138. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  139. Driver.SetAttribute (ColorScheme.Normal);
  140. }
  141. }
  142. }