2
0

FrameView.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. base.Add (contentView);
  87. contentView.Text = base.Text;
  88. }
  89. void DrawFrame ()
  90. {
  91. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  92. }
  93. /// <summary>
  94. /// Add the specified <see cref="View"/> to this container.
  95. /// </summary>
  96. /// <param name="view"><see cref="View"/> to add to this container</param>
  97. public override void Add (View view)
  98. {
  99. contentView.Add (view);
  100. if (view.CanFocus)
  101. CanFocus = true;
  102. }
  103. /// <summary>
  104. /// Removes a <see cref="View"/> from this container.
  105. /// </summary>
  106. /// <remarks>
  107. /// </remarks>
  108. public override void Remove (View view)
  109. {
  110. if (view == null)
  111. return;
  112. SetNeedsDisplay ();
  113. var touched = view.Frame;
  114. contentView.Remove (view);
  115. if (contentView.InternalSubviews.Count < 1)
  116. this.CanFocus = false;
  117. }
  118. /// <summary>
  119. /// Removes all <see cref="View"/>s from this container.
  120. /// </summary>
  121. /// <remarks>
  122. /// </remarks>
  123. public override void RemoveAll ()
  124. {
  125. contentView.RemoveAll ();
  126. }
  127. ///<inheritdoc/>
  128. public override void Redraw (Rect bounds)
  129. {
  130. var padding = 0;
  131. Application.CurrentView = this;
  132. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  133. if (NeedDisplay != null && !NeedDisplay.IsEmpty) {
  134. Driver.SetAttribute (ColorScheme.Normal);
  135. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  136. }
  137. var savedClip = ClipToBounds ();
  138. contentView.Redraw (contentView.Bounds);
  139. Driver.Clip = savedClip;
  140. ClearNeedsDisplay ();
  141. Driver.SetAttribute (ColorScheme.Normal);
  142. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  143. if (HasFocus)
  144. Driver.SetAttribute (ColorScheme.HotNormal);
  145. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  146. Driver.SetAttribute (ColorScheme.Normal);
  147. }
  148. /// <summary>
  149. /// The text displayed by the <see cref="Label"/>.
  150. /// </summary>
  151. public override ustring Text {
  152. get => contentView.Text;
  153. set {
  154. base.Text = value;
  155. if (contentView != null) {
  156. contentView.Text = value;
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  162. /// </summary>
  163. /// <value>The text alignment.</value>
  164. public override TextAlignment TextAlignment {
  165. get => contentView.TextAlignment;
  166. set {
  167. base.TextAlignment = contentView.TextAlignment = value;
  168. }
  169. }
  170. }
  171. }