FrameView.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Gui.FrameView"/> class with
  34. /// a title.
  35. /// </summary>
  36. /// <param name="frame">Frame.</param>
  37. /// <param name="title">Title.</param>
  38. public FrameView (Rect frame, ustring title) : base (frame)
  39. {
  40. var cFrame = new Rect (1, 1 , frame.Width - 2, frame.Height - 2);
  41. contentView = new ContentView (cFrame);
  42. base.Add (contentView);
  43. Title = title;
  44. }
  45. void DrawFrame ()
  46. {
  47. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  48. }
  49. /// <summary>
  50. /// Add the specified view to the ContentView.
  51. /// </summary>
  52. /// <param name="view">View to add to the window.</param>
  53. public override void Add (View view)
  54. {
  55. contentView.Add (view);
  56. if (view.CanFocus)
  57. CanFocus = true;
  58. }
  59. /// <summary>
  60. /// Removes a widget from this container.
  61. /// </summary>
  62. /// <remarks>
  63. /// </remarks>
  64. public virtual void Remove (View view)
  65. {
  66. if (view == null)
  67. return;
  68. SetNeedsDisplay ();
  69. var touched = view.Frame;
  70. contentView.Remove (view);
  71. if (contentView.Subviews.Count < 1)
  72. this.CanFocus = false;
  73. }
  74. public override void Redraw (Rect bounds)
  75. {
  76. if (!NeedDisplay.IsEmpty) {
  77. Driver.SetAttribute (ColorScheme.Normal);
  78. DrawFrame ();
  79. if (HasFocus)
  80. Driver.SetAttribute (ColorScheme.Normal);
  81. var width = Frame.Width;
  82. if (Title != null && width > 4) {
  83. Move (1, 0);
  84. Driver.AddRune (' ');
  85. var str = Title.Length > width ? Title [0, width - 4] : Title;
  86. Driver.AddStr (str);
  87. Driver.AddRune (' ');
  88. }
  89. Driver.SetAttribute (ColorScheme.Normal);
  90. }
  91. contentView.Redraw (contentView.Bounds);
  92. ClearNeedsDisplay ();
  93. }
  94. }
  95. }