ViewDrawing.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. namespace Terminal.Gui {
  5. public partial class View {
  6. ColorScheme _colorScheme;
  7. /// <summary>
  8. /// The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s
  9. /// color scheme.
  10. /// </summary>
  11. public virtual ColorScheme ColorScheme {
  12. get {
  13. if (_colorScheme == null) {
  14. return SuperView?.ColorScheme;
  15. }
  16. return _colorScheme;
  17. }
  18. set {
  19. if (_colorScheme != value) {
  20. _colorScheme = value;
  21. SetNeedsDisplay ();
  22. }
  23. }
  24. }
  25. /// <summary>
  26. /// Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.
  27. /// </summary>
  28. /// <returns><see cref="Terminal.Gui.ColorScheme.Normal"/> if <see cref="Enabled"/> is <see langword="true"/>
  29. /// or <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>.
  30. /// If it's overridden can return other values.</returns>
  31. public virtual Attribute GetNormalColor ()
  32. {
  33. return Enabled ? ColorScheme.Normal : ColorScheme.Disabled;
  34. }
  35. /// <summary>
  36. /// Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.
  37. /// </summary>
  38. /// <returns><see cref="Terminal.Gui.ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/>
  39. /// or <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>.
  40. /// If it's overridden can return other values.</returns>
  41. public virtual Attribute GetFocusColor ()
  42. {
  43. return Enabled ? ColorScheme.Focus : ColorScheme.Disabled;
  44. }
  45. /// <summary>
  46. /// Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.
  47. /// </summary>
  48. /// <returns><see cref="Terminal.Gui.ColorScheme.HotNormal"/> if <see cref="Enabled"/> is <see langword="true"/>
  49. /// or <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>.
  50. /// If it's overridden can return other values.</returns>
  51. public virtual Attribute GetHotNormalColor ()
  52. {
  53. return Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled;
  54. }
  55. /// <summary>
  56. /// Displays the specified character in the specified column and row of the View.
  57. /// </summary>
  58. /// <param name="col">Column (view-relative).</param>
  59. /// <param name="row">Row (view-relative).</param>
  60. /// <param name="ch">Ch.</param>
  61. public void AddRune (int col, int row, Rune ch)
  62. {
  63. if (row < 0 || col < 0)
  64. return;
  65. if (row > _frame.Height - 1 || col > _frame.Width - 1)
  66. return;
  67. Move (col, row);
  68. Driver.AddRune (ch);
  69. }
  70. /// <summary>
  71. /// Removes the <see cref="_needsDisplay"/> and the <see cref="_subViewNeedsDisplay"/> setting on this view.
  72. /// </summary>
  73. protected void ClearNeedsDisplay ()
  74. {
  75. _needsDisplay = Rect.Empty;
  76. _subViewNeedsDisplay = false;
  77. }
  78. // The view-relative region that needs to be redrawn
  79. internal Rect _needsDisplay { get; private set; } = Rect.Empty;
  80. /// <summary>
  81. /// Sets a flag indicating this view needs to be redisplayed because its state has changed.
  82. /// </summary>
  83. public void SetNeedsDisplay ()
  84. {
  85. if (!IsInitialized) return;
  86. SetNeedsDisplay (Bounds);
  87. }
  88. /// <summary>
  89. /// Flags the view-relative region on this View as needing to be redrawn.
  90. /// </summary>
  91. /// <param name="region">The view-relative region that needs to be redrawn.</param>
  92. public void SetNeedsDisplay (Rect region)
  93. {
  94. if (_needsDisplay.IsEmpty) {
  95. _needsDisplay = region;
  96. } else {
  97. var x = Math.Min (_needsDisplay.X, region.X);
  98. var y = Math.Min (_needsDisplay.Y, region.Y);
  99. var w = Math.Max (_needsDisplay.Width, region.Width);
  100. var h = Math.Max (_needsDisplay.Height, region.Height);
  101. _needsDisplay = new Rect (x, y, w, h);
  102. }
  103. _superView?.SetSubViewNeedsDisplay ();
  104. if (_subviews == null)
  105. return;
  106. foreach (var view in _subviews)
  107. if (view.Frame.IntersectsWith (region)) {
  108. var childRegion = Rect.Intersect (view.Frame, region);
  109. childRegion.X -= view.Frame.X;
  110. childRegion.Y -= view.Frame.Y;
  111. view.SetNeedsDisplay (childRegion);
  112. }
  113. }
  114. internal bool _subViewNeedsDisplay { get; private set; }
  115. /// <summary>
  116. /// Indicates that any Subviews (in the <see cref="Subviews"/> list) need to be repainted.
  117. /// </summary>
  118. public void SetSubViewNeedsDisplay ()
  119. {
  120. if (_subViewNeedsDisplay) {
  121. return;
  122. }
  123. _subViewNeedsDisplay = true;
  124. if (_superView != null && !_superView._subViewNeedsDisplay)
  125. _superView.SetSubViewNeedsDisplay ();
  126. }
  127. /// <summary>
  128. /// Clears the view region with the current color.
  129. /// </summary>
  130. /// <remarks>
  131. /// <para>
  132. /// This clears the entire region used by this view.
  133. /// </para>
  134. /// </remarks>
  135. public void Clear ()
  136. {
  137. var h = Frame.Height;
  138. var w = Frame.Width;
  139. for (var line = 0; line < h; line++) {
  140. Move (0, line);
  141. for (var col = 0; col < w; col++)
  142. Driver.AddRune ((Rune)' ');
  143. }
  144. }
  145. // BUGBUG: Stupid that this takes screen-relative. We should have a tenet that says
  146. // "View APIs only deal with View-relative coords".
  147. /// <summary>
  148. /// Clears the specified region with the current color.
  149. /// </summary>
  150. /// <remarks>
  151. /// </remarks>
  152. /// <param name="regionScreen">The screen-relative region to clear.</param>
  153. public void Clear (Rect regionScreen)
  154. {
  155. var h = regionScreen.Height;
  156. var w = regionScreen.Width;
  157. for (var line = regionScreen.Y; line < regionScreen.Y + h; line++) {
  158. Driver.Move (regionScreen.X, line);
  159. for (var col = 0; col < w; col++)
  160. Driver.AddRune ((Rune)' ');
  161. }
  162. }
  163. // Clips a rectangle in screen coordinates to the dimensions currently available on the screen
  164. internal Rect ScreenClip (Rect regionScreen)
  165. {
  166. var x = regionScreen.X < 0 ? 0 : regionScreen.X;
  167. var y = regionScreen.Y < 0 ? 0 : regionScreen.Y;
  168. var w = regionScreen.X + regionScreen.Width >= Driver.Cols ? Driver.Cols - regionScreen.X : regionScreen.Width;
  169. var h = regionScreen.Y + regionScreen.Height >= Driver.Rows ? Driver.Rows - regionScreen.Y : regionScreen.Height;
  170. return new Rect (x, y, w, h);
  171. }
  172. /// <summary>
  173. /// Sets the <see cref="ConsoleDriver"/>'s clip region to <see cref="Bounds"/>.
  174. /// </summary>
  175. /// <returns>The current screen-relative clip region, which can be then re-applied by setting <see cref="ConsoleDriver.Clip"/>.</returns>
  176. /// <remarks>
  177. /// <para>
  178. /// <see cref="Bounds"/> is View-relative.
  179. /// </para>
  180. /// <para>
  181. /// If <see cref="ConsoleDriver.Clip"/> and <see cref="Bounds"/> do not intersect, the clip region will be set to <see cref="Rect.Empty"/>.
  182. /// </para>
  183. /// </remarks>
  184. public Rect ClipToBounds ()
  185. {
  186. return SetClip (Bounds);
  187. }
  188. // BUGBUG: v2 - SetClip should return VIEW-relative so that it can be used to reset it; using Driver.Clip directly should not be necessary.
  189. /// <summary>
  190. /// Sets the clip region to the specified view-relative region.
  191. /// </summary>
  192. /// <returns>The current screen-relative clip region, which can be then re-applied by setting <see cref="ConsoleDriver.Clip"/>.</returns>
  193. /// <param name="region">View-relative clip region.</param>
  194. /// <remarks>
  195. /// If <see cref="ConsoleDriver.Clip"/> and <paramref name="region"/> do not intersect, the clip region will be set to <see cref="Rect.Empty"/>.
  196. /// </remarks>
  197. public Rect SetClip (Rect region)
  198. {
  199. var previous = Driver.Clip;
  200. Driver.Clip = Rect.Intersect (previous, ViewToScreen (region));
  201. return previous;
  202. }
  203. /// <summary>
  204. /// Utility function to draw strings that contain a hotkey.
  205. /// </summary>
  206. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  207. /// <param name="hotColor">Hot color.</param>
  208. /// <param name="normalColor">Normal color.</param>
  209. /// <remarks>
  210. /// <para>The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by default.</para>
  211. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  212. /// </remarks>
  213. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  214. {
  215. var hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  216. Application.Driver.SetAttribute (normalColor);
  217. foreach (var rune in text) {
  218. if (rune == hotkeySpec.Value) {
  219. Application.Driver.SetAttribute (hotColor);
  220. continue;
  221. }
  222. Application.Driver.AddRune ((Rune)rune);
  223. Application.Driver.SetAttribute (normalColor);
  224. }
  225. }
  226. /// <summary>
  227. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused" state.
  228. /// </summary>
  229. /// <param name="text">String to display, the underscore before a letter flags the next letter as the hotkey.</param>
  230. /// <param name="focused">If set to <see langword="true"/> this uses the focused colors from the color scheme, otherwise the regular ones.</param>
  231. /// <param name="scheme">The color scheme to use.</param>
  232. public void DrawHotString (string text, bool focused, ColorScheme scheme)
  233. {
  234. if (focused)
  235. DrawHotString (text, scheme.HotFocus, scheme.Focus);
  236. else
  237. DrawHotString (text, Enabled ? scheme.HotNormal : scheme.Disabled, Enabled ? scheme.Normal : scheme.Disabled);
  238. }
  239. /// <summary>
  240. /// This moves the cursor to the specified column and row in the view.
  241. /// </summary>
  242. /// <returns>The move.</returns>
  243. /// <param name="col">The column to move to, in view-relative coordinates.</param>
  244. /// <param name="row">the row to move to, in view-relative coordinates.</param>
  245. /// <param name="clipped">Whether to clip the result of the ViewToScreen method,
  246. /// If <see langword="true"/>, the <paramref name="col"/> and <paramref name="row"/> values are clamped to the screen (terminal) dimensions (0..TerminalDim-1).</param>
  247. public void Move (int col, int row, bool clipped = true)
  248. {
  249. if (Driver.Rows == 0) {
  250. return;
  251. }
  252. ViewToScreen (col, row, out var rCol, out var rRow, clipped);
  253. Driver.Move (rCol, rRow);
  254. }
  255. /// <summary>
  256. /// The canvas that any line drawing that is to be shared by subviews of this view should add lines to.
  257. /// </summary>
  258. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  259. public virtual LineCanvas LineCanvas { get; set; } = new LineCanvas ();
  260. /// <summary>
  261. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for
  262. /// rendering any border lines. If <see langword="true"/> the rendering of any borders drawn
  263. /// by this Frame will be done by it's parent's SuperView. If <see langword="false"/> (the default)
  264. /// this View's <see cref="OnDrawFrames()"/> method will be called to render the borders.
  265. /// </summary>
  266. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  267. // TODO: Make this cancelable
  268. /// <summary>
  269. /// Prepares <see cref="View.LineCanvas"/>. If <see cref="SuperViewRendersLineCanvas"/> is true, only the <see cref="LineCanvas"/> of
  270. /// this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is false (the default), this
  271. /// method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  272. /// </summary>
  273. /// <returns></returns>
  274. public virtual bool OnDrawFrames ()
  275. {
  276. if (!IsInitialized) {
  277. return false;
  278. }
  279. var prevClip = Driver.Clip;
  280. Driver.Clip = ViewToScreen (Frame);
  281. // TODO: Figure out what we should do if we have no superview
  282. //if (SuperView != null) {
  283. // TODO: Clipping is disabled for now to ensure we see errors
  284. Driver.Clip = new Rect (0, 0, Driver.Cols, Driver.Rows);// screenBounds;// SuperView.ClipToBounds ();
  285. //}
  286. // Each of these renders lines to either this View's LineCanvas
  287. // Those lines will be finally rendered in OnRenderLineCanvas
  288. Margin?.OnDrawContent (Bounds);
  289. Border?.OnDrawContent (Bounds);
  290. Padding?.OnDrawContent (Bounds);
  291. Driver.Clip = prevClip;
  292. return true;
  293. }
  294. /// <summary>
  295. /// Draws the view. Causes the following virtual methods to be called (along with their related events):
  296. /// <see cref="OnDrawContent"/>, <see cref="OnDrawContentComplete"/>.
  297. /// </summary>
  298. /// <remarks>
  299. /// <para>
  300. /// Always use <see cref="Bounds"/> (view-relative) when calling <see cref="OnDrawContent(Rect)"/>, NOT <see cref="Frame"/> (superview-relative).
  301. /// </para>
  302. /// <para>
  303. /// Views should set the color that they want to use on entry, as otherwise this will inherit
  304. /// the last color that was set globally on the driver.
  305. /// </para>
  306. /// <para>
  307. /// Overrides of <see cref="OnDrawContent(Rect)"/> must ensure they do not set <c>Driver.Clip</c> to a clip region
  308. /// larger than the <ref name="Bounds"/> property, as this will cause the driver to clip the entire region.
  309. /// </para>
  310. /// </remarks>
  311. public void Draw ()
  312. {
  313. if (!CanBeVisible (this)) {
  314. return;
  315. }
  316. OnDrawFrames ();
  317. var prevClip = ClipToBounds ();
  318. if (ColorScheme != null) {
  319. //Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
  320. Driver.SetAttribute (GetNormalColor ());
  321. }
  322. // Invoke DrawContentEvent
  323. var dev = new DrawEventArgs (Bounds);
  324. DrawContent?.Invoke (this, dev);
  325. if (!dev.Cancel) {
  326. OnDrawContent (Bounds);
  327. }
  328. Driver.Clip = prevClip;
  329. OnRenderLineCanvas ();
  330. // Invoke DrawContentCompleteEvent
  331. OnDrawContentComplete (Bounds);
  332. // BUGBUG: v2 - We should be able to use View.SetClip here and not have to resort to knowing Driver details.
  333. ClearLayoutNeeded ();
  334. ClearNeedsDisplay ();
  335. }
  336. // TODO: Make this cancelable
  337. /// <summary>
  338. /// Renders <see cref="View.LineCanvas"/>. If <see cref="SuperViewRendersLineCanvas"/> is true, only the <see cref="LineCanvas"/> of
  339. /// this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is false (the default), this
  340. /// method will cause the <see cref="LineCanvas"/> to be rendered.
  341. /// </summary>
  342. /// <returns></returns>
  343. public virtual bool OnRenderLineCanvas ()
  344. {
  345. if (!IsInitialized) {
  346. return false;
  347. }
  348. //Driver.SetAttribute (new Attribute(Color.White, Color.Black));
  349. // If we have a SuperView, it'll render our frames.
  350. if (!SuperViewRendersLineCanvas && LineCanvas.Bounds != Rect.Empty) {
  351. foreach (var p in LineCanvas.GetCellMap ()) { // Get the entire map
  352. Driver.SetAttribute (p.Value.Attribute?.Value ?? ColorScheme.Normal);
  353. Driver.Move (p.Key.X, p.Key.Y);
  354. Driver.AddRune (p.Value.Rune.Value);
  355. }
  356. LineCanvas.Clear ();
  357. }
  358. if (Subviews.Where (s => s.SuperViewRendersLineCanvas).Count () > 0) {
  359. foreach (var subview in Subviews.Where (s => s.SuperViewRendersLineCanvas == true)) {
  360. // Combine the LineCavas'
  361. LineCanvas.Merge (subview.LineCanvas);
  362. subview.LineCanvas.Clear ();
  363. }
  364. foreach (var p in LineCanvas.GetCellMap ()) { // Get the entire map
  365. Driver.SetAttribute (p.Value.Attribute?.Value ?? ColorScheme.Normal);
  366. Driver.Move (p.Key.X, p.Key.Y);
  367. Driver.AddRune (p.Value.Rune.Value);
  368. }
  369. LineCanvas.Clear ();
  370. }
  371. return true;
  372. }
  373. /// <summary>
  374. /// Event invoked when the content area of the View is to be drawn.
  375. /// </summary>
  376. /// <remarks>
  377. /// <para>
  378. /// Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.
  379. /// </para>
  380. /// <para>
  381. /// Rect provides the view-relative rectangle describing the currently visible viewport into the <see cref="View"/>.
  382. /// </para>
  383. /// </remarks>
  384. public event EventHandler<DrawEventArgs> DrawContent;
  385. /// <summary>
  386. /// Enables overrides to draw infinitely scrolled content and/or a background behind added controls.
  387. /// </summary>
  388. /// <param name="contentArea">The view-relative rectangle describing the currently visible viewport into the <see cref="View"/></param>
  389. /// <remarks>
  390. /// This method will be called before any subviews added with <see cref="Add(View)"/> have been drawn.
  391. /// </remarks>
  392. public virtual void OnDrawContent (Rect contentArea)
  393. {
  394. if (SuperView != null) {
  395. Clear (ViewToScreen (Bounds));
  396. }
  397. if (!string.IsNullOrEmpty (TextFormatter.Text)) {
  398. if (TextFormatter != null) {
  399. TextFormatter.NeedsFormat = true;
  400. }
  401. // This should NOT clear
  402. TextFormatter?.Draw (ViewToScreen (Bounds), HasFocus ? GetFocusColor () : GetNormalColor (),
  403. HasFocus ? ColorScheme.HotFocus : GetHotNormalColor (),
  404. Rect.Empty, false);
  405. SetSubViewNeedsDisplay ();
  406. }
  407. // Draw subviews
  408. // TODO: Implement OnDrawSubviews (cancelable);
  409. if (_subviews != null) {
  410. foreach (var view in _subviews) {
  411. if (view.Visible) { //!view._needsDisplay.IsEmpty || view._childNeedsDisplay || view.LayoutNeeded) {
  412. if (true) { //view.Frame.IntersectsWith (bounds)) { // && (view.Frame.IntersectsWith (bounds) || bounds.X < 0 || bounds.Y < 0)) {
  413. if (view.LayoutNeeded) {
  414. view.LayoutSubviews ();
  415. }
  416. // Draw the subview
  417. // Use the view's bounds (view-relative; Location will always be (0,0)
  418. //if (view.Visible && view.Frame.Width > 0 && view.Frame.Height > 0) {
  419. view.Draw ();
  420. //}
  421. }
  422. }
  423. }
  424. }
  425. }
  426. /// <summary>
  427. /// Event invoked when the content area of the View is completed drawing.
  428. /// </summary>
  429. /// <remarks>
  430. /// <para>
  431. /// Will be invoked after any subviews removed with <see cref="Remove(View)"/> have been completed drawing.
  432. /// </para>
  433. /// <para>
  434. /// Rect provides the view-relative rectangle describing the currently visible viewport into the <see cref="View"/>.
  435. /// </para>
  436. /// </remarks>
  437. public event EventHandler<DrawEventArgs> DrawContentComplete;
  438. /// <summary>
  439. /// Enables overrides after completed drawing infinitely scrolled content and/or a background behind removed controls.
  440. /// </summary>
  441. /// <param name="contentArea">The view-relative rectangle describing the currently visible viewport into the <see cref="View"/></param>
  442. /// <remarks>
  443. /// This method will be called after any subviews removed with <see cref="Remove(View)"/> have been completed drawing.
  444. /// </remarks>
  445. public virtual void OnDrawContentComplete (Rect contentArea)
  446. {
  447. DrawContentComplete?.Invoke (this, new DrawEventArgs (contentArea));
  448. }
  449. }
  450. }