FrameView.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Authors:
  3. // Miguel de Icaza ([email protected])
  4. //
  5. // NOTE: FrameView is functionally identical to Window with the following exceptions.
  6. // - Is not a Toplevel
  7. // - Does not support mouse dragging
  8. // - Does not support padding (but should)
  9. // - Does not support IEnumerable
  10. // Any udpates done here should probably be done in Window as well; TODO: Merge these classes
  11. using NStack;
  12. namespace Terminal.Gui {
  13. /// <summary>
  14. /// The FrameView is a container frame that draws a frame around the contents. It is similar to
  15. /// a GroupBox in Windows.
  16. /// </summary>
  17. public class FrameView : View {
  18. View contentView;
  19. ustring title;
  20. /// <summary>
  21. /// The title to be displayed for this <see cref="FrameView"/>.
  22. /// </summary>
  23. /// <value>The title.</value>
  24. public ustring Title {
  25. get => title;
  26. set {
  27. title = value;
  28. SetNeedsDisplay ();
  29. }
  30. }
  31. /// <summary>
  32. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  33. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  34. /// are actually deflated due to the border.
  35. /// </summary>
  36. class ContentView : View {
  37. public ContentView (Rect frame) : base (frame) { }
  38. public ContentView () : base () { }
  39. }
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  42. /// </summary>
  43. /// <param name="frame">Frame.</param>
  44. /// <param name="title">Title.</param>
  45. public FrameView (Rect frame, ustring title = null) : base (frame)
  46. {
  47. var cFrame = new Rect (1, 1, frame.Width - 2, frame.Height - 2);
  48. this.title = title;
  49. contentView = new ContentView (cFrame);
  50. Initialize ();
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  54. /// </summary>
  55. /// <param name="frame">Frame.</param>
  56. /// <param name="title">Title.</param>
  57. /// /// <param name="views">Views.</param>
  58. public FrameView (Rect frame, ustring title, View [] views) : this (frame, title)
  59. {
  60. foreach (var view in views) {
  61. contentView.Add (view);
  62. }
  63. Initialize ();
  64. }
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  67. /// </summary>
  68. /// <param name="title">Title.</param>
  69. public FrameView (ustring title)
  70. {
  71. this.title = title;
  72. contentView = new ContentView () {
  73. X = 1,
  74. Y = 1,
  75. Width = Dim.Fill (1),
  76. Height = Dim.Fill (1)
  77. };
  78. Initialize ();
  79. }
  80. /// <summary>
  81. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  82. /// </summary>
  83. public FrameView () : this (title: string.Empty) { }
  84. void Initialize ()
  85. {
  86. if (Subviews?.Count == 0) {
  87. base.Add (contentView);
  88. contentView.Text = base.Text;
  89. }
  90. }
  91. void DrawFrame ()
  92. {
  93. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  94. }
  95. /// <summary>
  96. /// Add the specified <see cref="View"/> to this container.
  97. /// </summary>
  98. /// <param name="view"><see cref="View"/> to add to this container</param>
  99. public override void Add (View view)
  100. {
  101. contentView.Add (view);
  102. if (view.CanFocus)
  103. CanFocus = true;
  104. }
  105. /// <summary>
  106. /// Removes a <see cref="View"/> from this container.
  107. /// </summary>
  108. /// <remarks>
  109. /// </remarks>
  110. public override void Remove (View view)
  111. {
  112. if (view == null)
  113. return;
  114. SetNeedsDisplay ();
  115. var touched = view.Frame;
  116. contentView.Remove (view);
  117. if (contentView.InternalSubviews.Count < 1)
  118. this.CanFocus = false;
  119. }
  120. /// <summary>
  121. /// Removes all <see cref="View"/>s from this container.
  122. /// </summary>
  123. /// <remarks>
  124. /// </remarks>
  125. public override void RemoveAll ()
  126. {
  127. contentView.RemoveAll ();
  128. }
  129. ///<inheritdoc/>
  130. public override void Redraw (Rect bounds)
  131. {
  132. var padding = 0;
  133. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  134. if (!NeedDisplay.IsEmpty) {
  135. Driver.SetAttribute (ColorScheme.Normal);
  136. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  137. }
  138. var savedClip = ClipToBounds ();
  139. contentView.Redraw (contentView.Bounds);
  140. Driver.Clip = savedClip;
  141. ClearNeedsDisplay ();
  142. Driver.SetAttribute (ColorScheme.Normal);
  143. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  144. if (HasFocus)
  145. Driver.SetAttribute (ColorScheme.HotNormal);
  146. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  147. Driver.SetAttribute (ColorScheme.Normal);
  148. }
  149. /// <summary>
  150. /// The text displayed by the <see cref="Label"/>.
  151. /// </summary>
  152. public override ustring Text {
  153. get => contentView.Text;
  154. set {
  155. base.Text = value;
  156. if (contentView != null) {
  157. contentView.Text = value;
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  163. /// </summary>
  164. /// <value>The text alignment.</value>
  165. public override TextAlignment TextAlignment {
  166. get => contentView.TextAlignment;
  167. set {
  168. base.TextAlignment = contentView.TextAlignment = value;
  169. }
  170. }
  171. }
  172. }