FrameView.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }
  57. public override void Redraw (Rect bounds)
  58. {
  59. if (!NeedDisplay.IsEmpty) {
  60. Driver.SetAttribute (ColorScheme.Normal);
  61. DrawFrame ();
  62. if (HasFocus)
  63. Driver.SetAttribute (ColorScheme.Normal);
  64. var width = Frame.Width;
  65. if (Title != null && width > 4) {
  66. Move (1, 0);
  67. Driver.AddRune (' ');
  68. var str = Title.Length > width ? Title [0, width - 4] : Title;
  69. Driver.AddStr (str);
  70. Driver.AddRune (' ');
  71. }
  72. Driver.SetAttribute (ColorScheme.Normal);
  73. }
  74. contentView.Redraw (contentView.Bounds);
  75. ClearNeedsDisplay ();
  76. }
  77. }
  78. }