FrameView.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  14. /// </summary>
  15. public class FrameView : View {
  16. View contentView;
  17. ustring title;
  18. /// <summary>
  19. /// The title to be displayed for this window.
  20. /// </summary>
  21. /// <value>The title.</value>
  22. public ustring Title {
  23. get => title;
  24. set {
  25. title = value;
  26. SetNeedsDisplay ();
  27. }
  28. }
  29. class ContentView : View {
  30. public ContentView (Rect frame) : base (frame) { }
  31. public ContentView () : base () { }
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Gui.FrameView"/> class with
  35. /// an absolute position and a title.
  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. contentView = new ContentView (cFrame);
  43. Initialize ();
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Gui.FrameView"/> class with
  47. /// an absolute position, a title and views.
  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="T:Terminal.Gui.Gui.FrameView"/> class with
  61. /// a title and the result is suitable to have its X, Y, Width and Height properties computed.
  62. /// </summary>
  63. /// <param name="title">Title.</param>
  64. public FrameView (ustring title)
  65. {
  66. contentView = new ContentView () {
  67. X = 1,
  68. Y = 1,
  69. Width = Dim.Fill (2),
  70. Height = Dim.Fill (2)
  71. };
  72. Initialize ();
  73. }
  74. void Initialize ()
  75. {
  76. base.Add (contentView);
  77. Title = title;
  78. }
  79. void DrawFrame ()
  80. {
  81. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  82. }
  83. /// <summary>
  84. /// Add the specified view to the ContentView.
  85. /// </summary>
  86. /// <param name="view">View to add to the window.</param>
  87. public override void Add (View view)
  88. {
  89. contentView.Add (view);
  90. if (view.CanFocus)
  91. CanFocus = true;
  92. }
  93. /// <summary>
  94. /// Removes a widget from this container.
  95. /// </summary>
  96. /// <remarks>
  97. /// </remarks>
  98. public override void Remove (View view)
  99. {
  100. if (view == null)
  101. return;
  102. SetNeedsDisplay ();
  103. var touched = view.Frame;
  104. contentView.Remove (view);
  105. if (contentView.InternalSubviews.Count < 1)
  106. this.CanFocus = false;
  107. }
  108. /// <summary>
  109. /// Removes all widgets from this container.
  110. /// </summary>
  111. /// <remarks>
  112. /// </remarks>
  113. public override void RemoveAll()
  114. {
  115. contentView.RemoveAll();
  116. }
  117. public override void Redraw (Rect bounds)
  118. {
  119. if (!NeedDisplay.IsEmpty) {
  120. Driver.SetAttribute (ColorScheme.Normal);
  121. DrawFrame ();
  122. if (HasFocus)
  123. Driver.SetAttribute (ColorScheme.Normal);
  124. var width = Frame.Width;
  125. if (Title != null && width > 4) {
  126. Move (1, 0);
  127. Driver.AddRune (' ');
  128. var str = Title.Length > width ? Title [0, width - 4] : Title;
  129. Driver.AddStr (str);
  130. Driver.AddRune (' ');
  131. }
  132. Driver.SetAttribute (ColorScheme.Normal);
  133. }
  134. contentView.Redraw (contentView.Bounds);
  135. ClearNeedsDisplay ();
  136. }
  137. }
  138. }