View.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. //
  2. // Authors:
  3. // Miguel de Icaza ([email protected])
  4. //
  5. // Pending:
  6. // - Check for NeedDisplay on the hierarchy and repaint
  7. // - Layout support
  8. // - "Colors" type or "Attributes" type?
  9. // - What to surface as "BackgroundCOlor" when clearing a window, an attribute or colors?
  10. //
  11. // Optimziations
  12. // - Add rendering limitation to the exposed area
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Linq;
  18. using NStack;
  19. namespace Terminal.Gui {
  20. /// <summary>
  21. /// Determines the LayoutStyle for a view, if Absolute, during LayoutSubviews, the
  22. /// value from the Frame will be used, if the value is Computed, then the Frame
  23. /// will be updated from the X, Y Pos objects and the Width and Height Dim objects.
  24. /// </summary>
  25. public enum LayoutStyle {
  26. /// <summary>
  27. /// The position and size of the view are based on the Frame value.
  28. /// </summary>
  29. Absolute,
  30. /// <summary>
  31. /// The position and size of the view will be computed based on the
  32. /// X, Y, Width and Height properties and set on the Frame.
  33. /// </summary>
  34. Computed
  35. }
  36. /// <summary>
  37. /// View is the base class for all views on the screen and represents a visible element that can render itself and contains zero or more nested views.
  38. /// </summary>
  39. /// <remarks>
  40. /// <para>
  41. /// The View defines the base functionality for user interface elements in Terminal.Gui. Views
  42. /// can contain one or more subviews, can respond to user input and render themselves on the screen.
  43. /// </para>
  44. /// <para>
  45. /// Views supports two layout styles: Absolute or Computed. The choice as to which layout style is used by the View
  46. /// is determined when the View is initizlied. To create a View using Absolute layout, call a constructor that takes a
  47. /// Rect parameter to specify the absolute position and size (the <c>View.<see cref="Frame "/></c>)/. To create a View
  48. /// using Computed layout use a constructor that does not take a Rect parametr and set the X, Y, Width and Height
  49. /// properties on the view. Both approaches use coordinates that are relative to the container they are being added to.
  50. /// </para>
  51. /// <para>
  52. /// To switch between Absolute and Computed layout, use the <see cref="LayoutStyle"/> property.
  53. /// </para>
  54. /// <para>
  55. /// Computed layout is more flexible and supports dynamic console apps where controls adjust layout
  56. /// as the terminal resizes or other Views change size or position. The X, Y, Width and Height
  57. /// properties are Dim and Pos objects that dynamically update the position of a view.
  58. /// The X and Y properties are of type <see cref="Pos"/>
  59. /// and you can use either absolute positions, percentages or anchor
  60. /// points. The Width and Height properties are of type
  61. /// <see cref="Dim"/> and can use absolute position,
  62. /// percentages and anchors. These are useful as they will take
  63. /// care of repositioning views when view's frames are resized or
  64. /// if the terminal size changes.
  65. /// </para>
  66. /// <para>
  67. /// Absolute layout requires specifying coordiantes and sizes of Views explicitly, and the
  68. /// View will typcialy stay in a fixed position and size. To change the position and size use the
  69. /// <see cref="Frame"/> property.
  70. /// </para>
  71. /// <para>
  72. /// Subviews (child views) can be added to a View by calling the <see cref="Add(View)"/> method.
  73. /// The container of a View can be accessed with the <see cref="SuperView"/> property.
  74. /// </para>
  75. /// <para>
  76. /// To flag a region of the View's <see cref="Bounds"/> to be redrawn call <see cref="SetNeedsDisplay(Rect)"/>. To flag the entire view
  77. /// for redraw call <see cref="SetNeedsDisplay()"/>.
  78. /// </para>
  79. /// <para>
  80. /// Views have a <see cref="ColorScheme"/> property that defines the default colors that subviews
  81. /// should use for rendering. This ensures that the views fit in the context where
  82. /// they are being used, and allows for themes to be plugged in. For example, the
  83. /// default colors for windows and toplevels uses a blue background, while it uses
  84. /// a white background for dialog boxes and a red background for errors.
  85. /// </para>
  86. /// <para>
  87. /// Subclasses should not rely on <see cref="ColorScheme"/> being
  88. /// set at construction time. If a <see cref="ColorScheme"/> is not set on a view, the view will inherit the
  89. /// value from its <see cref="SuperView"/> and the value might only be valid once a view has been
  90. /// added to a SuperView.
  91. /// </para>
  92. /// <para>
  93. /// By using <see cref="ColorScheme"/> applications will work both
  94. /// in color as well as black and white displays.
  95. /// </para>
  96. /// <para>
  97. /// Views that are focusable should implement the <see cref="PositionCursor"/> to make sure that
  98. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  99. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  100. /// the last focused view. So views should make sure that they place the cursor
  101. /// in a visually sensible place.
  102. /// </para>
  103. /// <para>
  104. /// The <see cref="LayoutSubviews"/> method is invoked when the size or layout of a view has
  105. /// changed. The default processing system will keep the size and dimensions
  106. /// for views that use the <see cref="LayoutStyle.Absolute"/>, and will recompute the
  107. /// frames for the vies that use <see cref="LayoutStyle.Computed"/>.
  108. /// </para>
  109. /// </remarks>
  110. public partial class View : Responder, IEnumerable {
  111. internal enum Direction {
  112. Forward,
  113. Backward
  114. }
  115. // container == SuperView
  116. View container = null;
  117. View focused = null;
  118. Direction focusDirection;
  119. TextFormatter textFormatter;
  120. /// <summary>
  121. /// Event fired when a subview is being added to this view.
  122. /// </summary>
  123. public Action<View> Added;
  124. /// <summary>
  125. /// Event fired when a subview is being removed from this view.
  126. /// </summary>
  127. public Action<View> Removed;
  128. /// <summary>
  129. /// Event fired when the view gets focus.
  130. /// </summary>
  131. public Action<FocusEventArgs> Enter;
  132. /// <summary>
  133. /// Event fired when the view looses focus.
  134. /// </summary>
  135. public Action<FocusEventArgs> Leave;
  136. /// <summary>
  137. /// Event fired when the view receives the mouse event for the first time.
  138. /// </summary>
  139. public Action<MouseEventArgs> MouseEnter;
  140. /// <summary>
  141. /// Event fired when the view receives a mouse event for the last time.
  142. /// </summary>
  143. public Action<MouseEventArgs> MouseLeave;
  144. /// <summary>
  145. /// Event fired when a mouse event is generated.
  146. /// </summary>
  147. public Action<MouseEventArgs> MouseClick;
  148. /// <summary>
  149. /// Gets or sets the HotKey defined for this view. A user pressing HotKey on the keyboard while this view has focus will cause the Clicked event to fire.
  150. /// </summary>
  151. public Key HotKey { get => textFormatter.HotKey; set => textFormatter.HotKey = value; }
  152. /// <summary>
  153. /// Gets or sets the specifier character for the hotkey (e.g. '_'). Set to '\xffff' to disable hotkey support for this View instance. The default is '\xffff'.
  154. /// </summary>
  155. public Rune HotKeySpecifier { get => textFormatter.HotKeySpecifier; set => textFormatter.HotKeySpecifier = value; }
  156. internal Direction FocusDirection {
  157. get => SuperView?.FocusDirection ?? focusDirection;
  158. set {
  159. if (SuperView != null)
  160. SuperView.FocusDirection = value;
  161. else
  162. focusDirection = value;
  163. }
  164. }
  165. /// <summary>
  166. /// Points to the current driver in use by the view, it is a convenience property
  167. /// for simplifying the development of new views.
  168. /// </summary>
  169. public static ConsoleDriver Driver { get { return Application.Driver; } }
  170. static IList<View> empty = new List<View> (0).AsReadOnly ();
  171. // This is null, and allocated on demand.
  172. List<View> subviews;
  173. /// <summary>
  174. /// This returns a list of the subviews contained by this view.
  175. /// </summary>
  176. /// <value>The subviews.</value>
  177. public IList<View> Subviews => subviews == null ? empty : subviews.AsReadOnly ();
  178. // Internally, we use InternalSubviews rather than subviews, as we do not expect us
  179. // to make the same mistakes our users make when they poke at the Subviews.
  180. internal IList<View> InternalSubviews => subviews ?? empty;
  181. // This is null, and allocated on demand.
  182. List<View> tabIndexes;
  183. /// <summary>
  184. /// This returns a tab index list of the subviews contained by this view.
  185. /// </summary>
  186. /// <value>The tabIndexes.</value>
  187. public IList<View> TabIndexes => tabIndexes == null ? empty : tabIndexes.AsReadOnly ();
  188. int tabIndex = -1;
  189. /// <summary>
  190. /// Indicates the index of the current <see cref="View"/> from the <see cref="TabIndexes"/> list.
  191. /// </summary>
  192. public int TabIndex {
  193. get { return tabIndex; }
  194. set {
  195. if (!CanFocus) {
  196. tabIndex = -1;
  197. return;
  198. } else if (SuperView?.tabIndexes == null || SuperView?.tabIndexes.Count == 1) {
  199. tabIndex = 0;
  200. return;
  201. } else if (tabIndex == value) {
  202. return;
  203. }
  204. tabIndex = value > SuperView.tabIndexes.Count - 1 ? SuperView.tabIndexes.Count - 1 : value < 0 ? 0 : value;
  205. tabIndex = GetTabIndex (tabIndex);
  206. if (SuperView.tabIndexes.IndexOf (this) != tabIndex) {
  207. SuperView.tabIndexes.Remove (this);
  208. SuperView.tabIndexes.Insert (tabIndex, this);
  209. SetTabIndex ();
  210. }
  211. }
  212. }
  213. private int GetTabIndex (int idx)
  214. {
  215. int i = 0;
  216. foreach (var v in SuperView.tabIndexes) {
  217. if (v.tabIndex == -1 || v == this) {
  218. continue;
  219. }
  220. i++;
  221. }
  222. return Math.Min (i, idx);
  223. }
  224. private void SetTabIndex ()
  225. {
  226. int i = 0;
  227. foreach (var v in SuperView.tabIndexes) {
  228. if (v.tabIndex == -1) {
  229. continue;
  230. }
  231. v.tabIndex = i;
  232. i++;
  233. }
  234. }
  235. bool tabStop = true;
  236. /// <summary>
  237. /// This only be <c>true</c> if the <see cref="CanFocus"/> is also <c>true</c> and the focus can be avoided by setting this to <c>false</c>
  238. /// </summary>
  239. public bool TabStop {
  240. get { return tabStop; }
  241. set {
  242. if (tabStop == value) {
  243. return;
  244. }
  245. tabStop = CanFocus && value;
  246. }
  247. }
  248. /// <inheritdoc/>
  249. public override bool CanFocus {
  250. get => base.CanFocus;
  251. set {
  252. if (base.CanFocus != value) {
  253. base.CanFocus = value;
  254. if (!value && tabIndex > -1) {
  255. TabIndex = -1;
  256. } else if (value && tabIndex == -1) {
  257. TabIndex = SuperView != null ? SuperView.tabIndexes.IndexOf (this) : -1;
  258. }
  259. TabStop = value;
  260. }
  261. }
  262. }
  263. internal Rect NeedDisplay { get; private set; } = Rect.Empty;
  264. // The frame for the object. Superview relative.
  265. Rect frame;
  266. /// <summary>
  267. /// Gets or sets an identifier for the view;
  268. /// </summary>
  269. /// <value>The identifier.</value>
  270. /// <remarks>The id should be unique across all Views that share a SuperView.</remarks>
  271. public ustring Id { get; set; } = "";
  272. /// <summary>
  273. /// Returns a value indicating if this View is currently on Top (Active)
  274. /// </summary>
  275. public bool IsCurrentTop {
  276. get {
  277. return Application.Current == this;
  278. }
  279. }
  280. /// <summary>
  281. /// Gets or sets a value indicating whether this <see cref="View"/> wants mouse position reports.
  282. /// </summary>
  283. /// <value><c>true</c> if want mouse position reports; otherwise, <c>false</c>.</value>
  284. public virtual bool WantMousePositionReports { get; set; } = false;
  285. /// <summary>
  286. /// Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.
  287. /// </summary>
  288. public virtual bool WantContinuousButtonPressed { get; set; } = false;
  289. /// <summary>
  290. /// Gets or sets the frame for the view. The frame is relative to the view's container (<see cref="SuperView"/>).
  291. /// </summary>
  292. /// <value>The frame.</value>
  293. /// <remarks>
  294. /// <para>
  295. /// Change the Frame when using the <see cref="LayoutStyle.Absolute"/> layout style to move or resize views.
  296. /// </para>
  297. /// <para>
  298. /// Altering the Frame of a view will trigger the redrawing of the
  299. /// view as well as the redrawing of the affected regions of the <see cref="SuperView"/>.
  300. /// </para>
  301. /// </remarks>
  302. public virtual Rect Frame {
  303. get => frame;
  304. set {
  305. if (SuperView != null) {
  306. SuperView.SetNeedsDisplay (frame);
  307. SuperView.SetNeedsDisplay (value);
  308. }
  309. frame = value;
  310. SetNeedsLayout ();
  311. SetNeedsDisplay (frame);
  312. }
  313. }
  314. /// <summary>
  315. /// Gets an enumerator that enumerates the subviews in this view.
  316. /// </summary>
  317. /// <returns>The enumerator.</returns>
  318. public IEnumerator GetEnumerator ()
  319. {
  320. foreach (var v in InternalSubviews)
  321. yield return v;
  322. }
  323. LayoutStyle layoutStyle;
  324. /// <summary>
  325. /// Controls how the View's <see cref="Frame"/> is computed during the LayoutSubviews method, if the style is set to <see cref="LayoutStyle.Absolute"/>,
  326. /// LayoutSubviews does not change the <see cref="Frame"/>. If the style is <see cref="LayoutStyle.Computed"/> the <see cref="Frame"/> is updated using
  327. /// the <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties.
  328. /// </summary>
  329. /// <value>The layout style.</value>
  330. public LayoutStyle LayoutStyle {
  331. get => layoutStyle;
  332. set {
  333. layoutStyle = value;
  334. SetNeedsLayout ();
  335. }
  336. }
  337. /// <summary>
  338. /// The bounds represent the View-relative rectangle used for this view; the area inside of the view.
  339. /// </summary>
  340. /// <value>The bounds.</value>
  341. /// <remarks>
  342. /// <para>
  343. /// Updates to the Bounds update the <see cref="Frame"/>,
  344. /// and has the same side effects as updating the <see cref="Frame"/>.
  345. /// </para>
  346. /// <para>
  347. /// Because <see cref="Bounds"/> coordinates are relative to the upper-left corner of the <see cref="View"/>,
  348. /// the coordinates of the upper-left corner of the rectangle returned by this property are (0,0).
  349. /// Use this property to obtain the size and coordinates of the client area of the
  350. /// control for tasks such as drawing on the surface of the control.
  351. /// </para>
  352. /// </remarks>
  353. public Rect Bounds {
  354. get => new Rect (Point.Empty, Frame.Size);
  355. set {
  356. Frame = new Rect (frame.Location, value.Size);
  357. }
  358. }
  359. Pos x, y;
  360. /// <summary>
  361. /// Gets or sets the X position for the view (the column). Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
  362. /// </summary>
  363. /// <value>The X Position.</value>
  364. /// <remarks>
  365. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
  366. /// </remarks>
  367. public Pos X {
  368. get => x;
  369. set {
  370. x = value;
  371. SetNeedsLayout ();
  372. SetNeedsDisplay (frame);
  373. }
  374. }
  375. /// <summary>
  376. /// Gets or sets the Y position for the view (the row). Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
  377. /// </summary>
  378. /// <value>The y position (line).</value>
  379. /// <remarks>
  380. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
  381. /// </remarks>
  382. public Pos Y {
  383. get => y;
  384. set {
  385. y = value;
  386. SetNeedsLayout ();
  387. SetNeedsDisplay (frame);
  388. }
  389. }
  390. Dim width, height;
  391. /// <summary>
  392. /// Gets or sets the width of the view. Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
  393. /// </summary>
  394. /// <value>The width.</value>
  395. /// <remarks>
  396. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
  397. /// </remarks>
  398. public Dim Width {
  399. get => width;
  400. set {
  401. width = value;
  402. SetNeedsLayout ();
  403. SetNeedsDisplay (frame);
  404. }
  405. }
  406. /// <summary>
  407. /// Gets or sets the height of the view. Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
  408. /// </summary>
  409. /// <value>The height.</value>
  410. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
  411. public Dim Height {
  412. get => height;
  413. set {
  414. height = value;
  415. SetNeedsLayout ();
  416. SetNeedsDisplay (frame);
  417. }
  418. }
  419. /// <summary>
  420. /// Returns the container for this view, or null if this view has not been added to a container.
  421. /// </summary>
  422. /// <value>The super view.</value>
  423. public View SuperView => container;
  424. /// <summary>
  425. /// Initializes a new instance of a <see cref="LayoutStyle.Absolute"/> <see cref="View"/> class with the absolute
  426. /// dimensions specified in the <c>frame</c> parameter.
  427. /// </summary>
  428. /// <param name="frame">The region covered by this view.</param>
  429. /// <remarks>
  430. /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use <see cref="View()"/> to
  431. /// initialize a View with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  432. /// </remarks>
  433. public View (Rect frame)
  434. {
  435. textFormatter = new TextFormatter ();
  436. this.Text = ustring.Empty;
  437. this.Frame = frame;
  438. LayoutStyle = LayoutStyle.Absolute;
  439. }
  440. /// <summary>
  441. /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Computed"/> layout.
  442. /// </summary>
  443. /// <remarks>
  444. /// <para>
  445. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically control the size and location of the view.
  446. /// The <see cref="Label"/> will be created using <see cref="LayoutStyle.Computed"/>
  447. /// coordinates. The initial size (<see cref="View.Frame"/> will be
  448. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  449. /// </para>
  450. /// <para>
  451. /// If <c>Height</c> is greater than one, word wrapping is provided.
  452. /// </para>
  453. /// <para>
  454. /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  455. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically control the size and location of the view.
  456. /// </para>
  457. /// </remarks>
  458. public View () : this (text: string.Empty) { }
  459. /// <summary>
  460. /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Absolute"/> layout.
  461. /// </summary>
  462. /// <remarks>
  463. /// <para>
  464. /// The <see cref="View"/> will be created at the given
  465. /// coordinates with the given string. The size (<see cref="View.Frame"/> will be
  466. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  467. /// </para>
  468. /// <para>
  469. /// No line wrapping is provided.
  470. /// </para>
  471. /// </remarks>
  472. /// <param name="x">column to locate the Label.</param>
  473. /// <param name="y">row to locate the Label.</param>
  474. /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
  475. public View (int x, int y, ustring text) : this (TextFormatter.CalcRect (x, y, text), text) { }
  476. /// <summary>
  477. /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Absolute"/> layout.
  478. /// </summary>
  479. /// <remarks>
  480. /// <para>
  481. /// The <see cref="View"/> will be created at the given
  482. /// coordinates with the given string. The initial size (<see cref="View.Frame"/> will be
  483. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  484. /// </para>
  485. /// <para>
  486. /// If <c>rect.Height</c> is greater than one, word wrapping is provided.
  487. /// </para>
  488. /// </remarks>
  489. /// <param name="rect">Location.</param>
  490. /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
  491. public View (Rect rect, ustring text) : this (rect)
  492. {
  493. textFormatter = new TextFormatter ();
  494. this.Text = text;
  495. }
  496. /// <summary>
  497. /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Computed"/> layout.
  498. /// </summary>
  499. /// <remarks>
  500. /// <para>
  501. /// The <see cref="View"/> will be created using <see cref="LayoutStyle.Computed"/>
  502. /// coordinates with the given string. The initial size (<see cref="View.Frame"/> will be
  503. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  504. /// </para>
  505. /// <para>
  506. /// If <c>Height</c> is greater than one, word wrapping is provided.
  507. /// </para>
  508. /// </remarks>
  509. /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
  510. public View (ustring text) : base ()
  511. {
  512. textFormatter = new TextFormatter ();
  513. this.Text = text;
  514. CanFocus = false;
  515. TabIndex = -1;
  516. TabStop = false;
  517. LayoutStyle = LayoutStyle.Computed;
  518. // BUGBUG: CalcRect doesn't account for line wrapping
  519. var r = TextFormatter.CalcRect (0, 0, text);
  520. x = Pos.At (0);
  521. y = Pos.At (0);
  522. Width = r.Width;
  523. Height = r.Height;
  524. }
  525. /// <summary>
  526. /// Sets a flag indicating this view needs to be redisplayed because its state has changed.
  527. /// </summary>
  528. public void SetNeedsDisplay ()
  529. {
  530. SetNeedsDisplay (Bounds);
  531. }
  532. internal bool layoutNeeded = true;
  533. internal void SetNeedsLayout ()
  534. {
  535. if (layoutNeeded)
  536. return;
  537. layoutNeeded = true;
  538. if (SuperView == null)
  539. return;
  540. SuperView.SetNeedsLayout ();
  541. textFormatter.NeedsFormat = true;
  542. }
  543. /// <summary>
  544. /// Flags the view-relative region on this View as needing to be repainted.
  545. /// </summary>
  546. /// <param name="region">The view-relative region that must be flagged for repaint.</param>
  547. public void SetNeedsDisplay (Rect region)
  548. {
  549. if (NeedDisplay == null || NeedDisplay.IsEmpty)
  550. NeedDisplay = region;
  551. else {
  552. var x = Math.Min (NeedDisplay.X, region.X);
  553. var y = Math.Min (NeedDisplay.Y, region.Y);
  554. var w = Math.Max (NeedDisplay.Width, region.Width);
  555. var h = Math.Max (NeedDisplay.Height, region.Height);
  556. NeedDisplay = new Rect (x, y, w, h);
  557. }
  558. if (container != null)
  559. container.ChildNeedsDisplay ();
  560. if (subviews == null)
  561. return;
  562. foreach (var view in subviews)
  563. if (view.Frame.IntersectsWith (region)) {
  564. var childRegion = Rect.Intersect (view.Frame, region);
  565. childRegion.X -= view.Frame.X;
  566. childRegion.Y -= view.Frame.Y;
  567. view.SetNeedsDisplay (childRegion);
  568. }
  569. }
  570. internal bool childNeedsDisplay;
  571. /// <summary>
  572. /// Indicates that any child views (in the <see cref="Subviews"/> list) need to be repainted.
  573. /// </summary>
  574. public void ChildNeedsDisplay ()
  575. {
  576. childNeedsDisplay = true;
  577. if (container != null)
  578. container.ChildNeedsDisplay ();
  579. }
  580. /// <summary>
  581. /// Adds a subview (child) to this view.
  582. /// </summary>
  583. /// <remarks>
  584. /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
  585. /// </remarks>
  586. public virtual void Add (View view)
  587. {
  588. if (view == null)
  589. return;
  590. if (subviews == null) {
  591. subviews = new List<View> ();
  592. }
  593. if (tabIndexes == null) {
  594. tabIndexes = new List<View> ();
  595. }
  596. subviews.Add (view);
  597. tabIndexes.Add (view);
  598. view.container = this;
  599. OnAdded (view);
  600. if (view.CanFocus) {
  601. CanFocus = true;
  602. view.tabIndex = tabIndexes.IndexOf (view);
  603. }
  604. SetNeedsLayout ();
  605. SetNeedsDisplay ();
  606. }
  607. /// <summary>
  608. /// Adds the specified views (children) to the view.
  609. /// </summary>
  610. /// <param name="views">Array of one or more views (can be optional parameter).</param>
  611. /// <remarks>
  612. /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
  613. /// </remarks>
  614. public void Add (params View [] views)
  615. {
  616. if (views == null)
  617. return;
  618. foreach (var view in views)
  619. Add (view);
  620. }
  621. /// <summary>
  622. /// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  623. /// </summary>
  624. public virtual void RemoveAll ()
  625. {
  626. if (subviews == null)
  627. return;
  628. while (subviews.Count > 0) {
  629. Remove (subviews [0]);
  630. if (tabIndexes.Count > 0) {
  631. Remove (tabIndexes [0]);
  632. }
  633. }
  634. }
  635. /// <summary>
  636. /// Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  637. /// </summary>
  638. /// <remarks>
  639. /// </remarks>
  640. public virtual void Remove (View view)
  641. {
  642. if (view == null || subviews == null)
  643. return;
  644. SetNeedsLayout ();
  645. SetNeedsDisplay ();
  646. var touched = view.Frame;
  647. subviews.Remove (view);
  648. tabIndexes.Remove (view);
  649. view.container = null;
  650. OnRemoved (view);
  651. view.tabIndex = -1;
  652. if (subviews.Count < 1)
  653. this.CanFocus = false;
  654. foreach (var v in subviews) {
  655. if (v.Frame.IntersectsWith (touched))
  656. view.SetNeedsDisplay ();
  657. }
  658. }
  659. void PerformActionForSubview (View subview, Action<View> action)
  660. {
  661. if (subviews.Contains (subview)) {
  662. action (subview);
  663. }
  664. SetNeedsDisplay ();
  665. subview.SetNeedsDisplay ();
  666. }
  667. /// <summary>
  668. /// Brings the specified subview to the front so it is drawn on top of any other views.
  669. /// </summary>
  670. /// <param name="subview">The subview to send to the front</param>
  671. /// <remarks>
  672. /// <seealso cref="SendSubviewToBack"/>.
  673. /// </remarks>
  674. public void BringSubviewToFront (View subview)
  675. {
  676. PerformActionForSubview (subview, x => {
  677. subviews.Remove (x);
  678. subviews.Add (x);
  679. });
  680. }
  681. /// <summary>
  682. /// Sends the specified subview to the front so it is the first view drawn
  683. /// </summary>
  684. /// <param name="subview">The subview to send to the front</param>
  685. /// <remarks>
  686. /// <seealso cref="BringSubviewToFront(View)"/>.
  687. /// </remarks>
  688. public void SendSubviewToBack (View subview)
  689. {
  690. PerformActionForSubview (subview, x => {
  691. subviews.Remove (x);
  692. subviews.Insert (0, subview);
  693. });
  694. }
  695. /// <summary>
  696. /// Moves the subview backwards in the hierarchy, only one step
  697. /// </summary>
  698. /// <param name="subview">The subview to send backwards</param>
  699. /// <remarks>
  700. /// If you want to send the view all the way to the back use SendSubviewToBack.
  701. /// </remarks>
  702. public void SendSubviewBackwards (View subview)
  703. {
  704. PerformActionForSubview (subview, x => {
  705. var idx = subviews.IndexOf (x);
  706. if (idx > 0) {
  707. subviews.Remove (x);
  708. subviews.Insert (idx - 1, x);
  709. }
  710. });
  711. }
  712. /// <summary>
  713. /// Moves the subview backwards in the hierarchy, only one step
  714. /// </summary>
  715. /// <param name="subview">The subview to send backwards</param>
  716. /// <remarks>
  717. /// If you want to send the view all the way to the back use SendSubviewToBack.
  718. /// </remarks>
  719. public void BringSubviewForward (View subview)
  720. {
  721. PerformActionForSubview (subview, x => {
  722. var idx = subviews.IndexOf (x);
  723. if (idx + 1 < subviews.Count) {
  724. subviews.Remove (x);
  725. subviews.Insert (idx + 1, x);
  726. }
  727. });
  728. }
  729. /// <summary>
  730. /// Clears the view region with the current color.
  731. /// </summary>
  732. /// <remarks>
  733. /// <para>
  734. /// This clears the entire region used by this view.
  735. /// </para>
  736. /// </remarks>
  737. public void Clear ()
  738. {
  739. var h = Frame.Height;
  740. var w = Frame.Width;
  741. for (int line = 0; line < h; line++) {
  742. Move (0, line);
  743. for (int col = 0; col < w; col++)
  744. Driver.AddRune (' ');
  745. }
  746. }
  747. /// <summary>
  748. /// Clears the specified region with the current color.
  749. /// </summary>
  750. /// <remarks>
  751. /// </remarks>
  752. /// <param name="regionScreen">The screen-relative region to clear.</param>
  753. public void Clear (Rect regionScreen)
  754. {
  755. var h = regionScreen.Height;
  756. var w = regionScreen.Width;
  757. for (int line = regionScreen.Y; line < regionScreen.Y + h; line++) {
  758. Driver.Move (regionScreen.X, line);
  759. for (int col = 0; col < w; col++)
  760. Driver.AddRune (' ');
  761. }
  762. }
  763. /// <summary>
  764. /// Converts a view-relative (col,row) position to a screen-relative position (col,row). The values are optionally clamped to the screen dimensions.
  765. /// </summary>
  766. /// <param name="col">View-relative column.</param>
  767. /// <param name="row">View-relative row.</param>
  768. /// <param name="rcol">Absolute column; screen-relative.</param>
  769. /// <param name="rrow">Absolute row; screen-relative.</param>
  770. /// <param name="clipped">Whether to clip the result of the ViewToScreen method, if set to <c>true</c>, the rcol, rrow values are clamped to the screen (terminal) dimensions (0..TerminalDim-1).</param>
  771. internal void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  772. {
  773. // Computes the real row, col relative to the screen.
  774. rrow = row + frame.Y;
  775. rcol = col + frame.X;
  776. var ccontainer = container;
  777. while (ccontainer != null) {
  778. rrow += ccontainer.frame.Y;
  779. rcol += ccontainer.frame.X;
  780. ccontainer = ccontainer.container;
  781. }
  782. // The following ensures that the cursor is always in the screen boundaries.
  783. if (clipped) {
  784. rrow = Math.Min (rrow, Driver.Rows - 1);
  785. rcol = Math.Min (rcol, Driver.Cols - 1);
  786. }
  787. }
  788. /// <summary>
  789. /// Converts a point from screen-relative coordinates to view-relative coordinates.
  790. /// </summary>
  791. /// <returns>The mapped point.</returns>
  792. /// <param name="x">X screen-coordinate point.</param>
  793. /// <param name="y">Y screen-coordinate point.</param>
  794. public Point ScreenToView (int x, int y)
  795. {
  796. if (SuperView == null) {
  797. return new Point (x - Frame.X, y - frame.Y);
  798. } else {
  799. var parent = SuperView.ScreenToView (x, y);
  800. return new Point (parent.X - frame.X, parent.Y - frame.Y);
  801. }
  802. }
  803. /// <summary>
  804. /// Converts a region in view-relative coordinates to screen-relative coordinates.
  805. /// </summary>
  806. internal Rect ViewToScreen (Rect region)
  807. {
  808. ViewToScreen (region.X, region.Y, out var x, out var y, clipped: false);
  809. return new Rect (x, y, region.Width, region.Height);
  810. }
  811. // Clips a rectangle in screen coordinates to the dimensions currently available on the screen
  812. internal Rect ScreenClip (Rect regionScreen)
  813. {
  814. var x = regionScreen.X < 0 ? 0 : regionScreen.X;
  815. var y = regionScreen.Y < 0 ? 0 : regionScreen.Y;
  816. var w = regionScreen.X + regionScreen.Width >= Driver.Cols ? Driver.Cols - regionScreen.X : regionScreen.Width;
  817. var h = regionScreen.Y + regionScreen.Height >= Driver.Rows ? Driver.Rows - regionScreen.Y : regionScreen.Height;
  818. return new Rect (x, y, w, h);
  819. }
  820. /// <summary>
  821. /// Sets the <see cref="ConsoleDriver"/>'s clip region to the current View's <see cref="Bounds"/>.
  822. /// </summary>
  823. /// <returns>The existing driver's clip region, which can be then re-eapplied by setting <c><see cref="Driver"/>.Clip</c> (<see cref="ConsoleDriver.Clip"/>).</returns>
  824. /// <remarks>
  825. /// <see cref="Bounds"/> is View-relative.
  826. /// </remarks>
  827. public Rect ClipToBounds ()
  828. {
  829. return SetClip (Bounds);
  830. }
  831. /// <summary>
  832. /// Sets the clip region to the specified view-relative region.
  833. /// </summary>
  834. /// <returns>The previous screen-relative clip region.</returns>
  835. /// <param name="region">View-relative clip region.</param>
  836. public Rect SetClip (Rect region)
  837. {
  838. var previous = Driver.Clip;
  839. Driver.Clip = Rect.Intersect (previous, ViewToScreen (region));
  840. return previous;
  841. }
  842. /// <summary>
  843. /// Draws a frame in the current view, clipped by the boundary of this view
  844. /// </summary>
  845. /// <param name="region">View-relative region for the frame to be drawn.</param>
  846. /// <param name="padding">The padding to add around the outside of the drawn frame.</param>
  847. /// <param name="fill">If set to <c>true</c> it fill will the contents.</param>
  848. public void DrawFrame (Rect region, int padding = 0, bool fill = false)
  849. {
  850. var scrRect = ViewToScreen (region);
  851. var savedClip = ClipToBounds ();
  852. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: fill);
  853. Driver.Clip = savedClip;
  854. }
  855. /// <summary>
  856. /// Utility function to draw strings that contain a hotkey.
  857. /// </summary>
  858. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  859. /// <param name="hotColor">Hot color.</param>
  860. /// <param name="normalColor">Normal color.</param>
  861. /// <remarks>
  862. /// <para>The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by default.</para>
  863. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  864. /// </remarks>
  865. public void DrawHotString (ustring text, Attribute hotColor, Attribute normalColor)
  866. {
  867. var hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  868. Application.Driver.SetAttribute (normalColor);
  869. foreach (var rune in text) {
  870. if (rune == hotkeySpec) {
  871. Application.Driver.SetAttribute (hotColor);
  872. continue;
  873. }
  874. Application.Driver.AddRune (rune);
  875. Application.Driver.SetAttribute (normalColor);
  876. }
  877. }
  878. /// <summary>
  879. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused" state.
  880. /// </summary>
  881. /// <param name="text">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
  882. /// <param name="focused">If set to <c>true</c> this uses the focused colors from the color scheme, otherwise the regular ones.</param>
  883. /// <param name="scheme">The color scheme to use.</param>
  884. public void DrawHotString (ustring text, bool focused, ColorScheme scheme)
  885. {
  886. if (focused)
  887. DrawHotString (text, scheme.HotFocus, scheme.Focus);
  888. else
  889. DrawHotString (text, scheme.HotNormal, scheme.Normal);
  890. }
  891. /// <summary>
  892. /// This moves the cursor to the specified column and row in the view.
  893. /// </summary>
  894. /// <returns>The move.</returns>
  895. /// <param name="col">Col.</param>
  896. /// <param name="row">Row.</param>
  897. public void Move (int col, int row)
  898. {
  899. ViewToScreen (col, row, out var rcol, out var rrow);
  900. Driver.Move (rcol, rrow);
  901. }
  902. /// <summary>
  903. /// Positions the cursor in the right position based on the currently focused view in the chain.
  904. /// </summary>
  905. /// Views that are focusable should override <see cref="PositionCursor"/> to ensure
  906. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  907. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  908. /// the last focused view. Views should make sure that they place the cursor
  909. /// in a visually sensible place.
  910. public virtual void PositionCursor ()
  911. {
  912. if (focused != null)
  913. focused.PositionCursor ();
  914. else {
  915. if (CanFocus && HasFocus) {
  916. Move (textFormatter.HotKeyPos == -1 ? 1 : textFormatter.HotKeyPos, 0);
  917. } else {
  918. Move (frame.X, frame.Y);
  919. }
  920. }
  921. }
  922. bool hasFocus;
  923. /// <inheritdoc/>
  924. public override bool HasFocus {
  925. get {
  926. return hasFocus;
  927. }
  928. }
  929. void SetHasFocus (bool value, View view)
  930. {
  931. if (hasFocus != value) {
  932. hasFocus = value;
  933. }
  934. if (value) {
  935. OnEnter (view);
  936. } else {
  937. OnLeave (view);
  938. }
  939. SetNeedsDisplay ();
  940. // Remove focus down the chain of subviews if focus is removed
  941. if (!value && focused != null) {
  942. focused.OnLeave (view);
  943. focused.SetHasFocus (false, view);
  944. focused = null;
  945. }
  946. }
  947. /// <summary>
  948. /// Defines the event arguments for <see cref="SetFocus(View)"/>
  949. /// </summary>
  950. public class FocusEventArgs : EventArgs {
  951. /// <summary>
  952. /// Constructs.
  953. /// </summary>
  954. /// <param name="view">The view that gets or loses focus.</param>
  955. public FocusEventArgs (View view) { View = view; }
  956. /// <summary>
  957. /// Indicates if the current focus event has already been processed and the driver should stop notifying any other event subscriber.
  958. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  959. /// </summary>
  960. public bool Handled { get; set; }
  961. /// <summary>
  962. /// Indicates the current view that gets or loses focus.
  963. /// </summary>
  964. public View View { get; set; }
  965. }
  966. /// <summary>
  967. /// Method invoked when a subview is being added to this view.
  968. /// </summary>
  969. /// <param name="view">The subview being added.</param>
  970. public virtual void OnAdded (View view)
  971. {
  972. view.Added?.Invoke (this);
  973. }
  974. /// <summary>
  975. /// Method invoked when a subview is being removed from this view.
  976. /// </summary>
  977. /// <param name="view">The subview being removed.</param>
  978. public virtual void OnRemoved (View view)
  979. {
  980. view.Removed?.Invoke (this);
  981. }
  982. /// <inheritdoc/>
  983. public override bool OnEnter (View view)
  984. {
  985. FocusEventArgs args = new FocusEventArgs (view);
  986. Enter?.Invoke (args);
  987. if (args.Handled)
  988. return true;
  989. if (base.OnEnter (view))
  990. return true;
  991. return false;
  992. }
  993. /// <inheritdoc/>
  994. public override bool OnLeave (View view)
  995. {
  996. FocusEventArgs args = new FocusEventArgs (view);
  997. Leave?.Invoke (args);
  998. if (args.Handled)
  999. return true;
  1000. if (base.OnLeave (view))
  1001. return true;
  1002. return false;
  1003. }
  1004. /// <summary>
  1005. /// Returns the currently focused view inside this view, or null if nothing is focused.
  1006. /// </summary>
  1007. /// <value>The focused.</value>
  1008. public View Focused => focused;
  1009. /// <summary>
  1010. /// Returns the most focused view in the chain of subviews (the leaf view that has the focus).
  1011. /// </summary>
  1012. /// <value>The most focused.</value>
  1013. public View MostFocused {
  1014. get {
  1015. if (Focused == null)
  1016. return null;
  1017. var most = Focused.MostFocused;
  1018. if (most != null)
  1019. return most;
  1020. return Focused;
  1021. }
  1022. }
  1023. /// <summary>
  1024. /// The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s
  1025. /// color scheme.
  1026. /// </summary>
  1027. public ColorScheme ColorScheme {
  1028. get {
  1029. if (colorScheme == null)
  1030. return SuperView?.ColorScheme;
  1031. return colorScheme;
  1032. }
  1033. set {
  1034. colorScheme = value;
  1035. }
  1036. }
  1037. ColorScheme colorScheme;
  1038. /// <summary>
  1039. /// Displays the specified character in the specified column and row of the View.
  1040. /// </summary>
  1041. /// <param name="col">Column (view-relative).</param>
  1042. /// <param name="row">Row (view-relative).</param>
  1043. /// <param name="ch">Ch.</param>
  1044. public void AddRune (int col, int row, Rune ch)
  1045. {
  1046. if (row < 0 || col < 0)
  1047. return;
  1048. if (row > frame.Height - 1 || col > frame.Width - 1)
  1049. return;
  1050. Move (col, row);
  1051. Driver.AddRune (ch);
  1052. }
  1053. /// <summary>
  1054. /// Removes the <see cref="SetNeedsDisplay()"/> and the <see cref="ChildNeedsDisplay"/> setting on this view.
  1055. /// </summary>
  1056. protected void ClearNeedsDisplay ()
  1057. {
  1058. NeedDisplay = Rect.Empty;
  1059. childNeedsDisplay = false;
  1060. }
  1061. /// <summary>
  1062. /// Redraws this view and its subviews; only redraws the views that have been flagged for a re-display.
  1063. /// </summary>
  1064. /// <param name="bounds">The bounds (view-relative region) to redraw.</param>
  1065. /// <remarks>
  1066. /// <para>
  1067. /// Always use <see cref="Bounds"/> (view-relative) when calling <see cref="Redraw(Rect)"/>, NOT <see cref="Frame"/> (superview-relative).
  1068. /// </para>
  1069. /// <para>
  1070. /// Views should set the color that they want to use on entry, as otherwise this will inherit
  1071. /// the last color that was set globaly on the driver.
  1072. /// </para>
  1073. /// <para>
  1074. /// Overrides of <see cref="Redraw"/> must ensure they do not set <c>Driver.Clip</c> to a clip region
  1075. /// larger than the <c>region</c> parameter.
  1076. /// </para>
  1077. /// </remarks>
  1078. public virtual void Redraw (Rect bounds)
  1079. {
  1080. var clipRect = new Rect (Point.Empty, frame.Size);
  1081. if (ColorScheme != null)
  1082. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  1083. if (!ustring.IsNullOrEmpty (Text)) {
  1084. Clear ();
  1085. // Draw any Text
  1086. if (textFormatter != null) {
  1087. textFormatter.NeedsFormat = true;
  1088. }
  1089. textFormatter?.Draw (ViewToScreen (Bounds), HasFocus ? ColorScheme.Focus : ColorScheme.Normal, HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  1090. }
  1091. // Invoke DrawContentEvent
  1092. OnDrawContent (bounds);
  1093. if (subviews != null) {
  1094. foreach (var view in subviews) {
  1095. if (view.NeedDisplay != null && (!view.NeedDisplay.IsEmpty || view.childNeedsDisplay)) {
  1096. if (view.Frame.IntersectsWith (clipRect) && (view.Frame.IntersectsWith (bounds) || bounds.X < 0 || bounds.Y < 0)) {
  1097. if (view.layoutNeeded)
  1098. view.LayoutSubviews ();
  1099. Application.CurrentView = view;
  1100. // Draw the subview
  1101. // Use the view's bounds (view-relative; Location will always be (0,0) because
  1102. view.Redraw (view.Bounds);
  1103. }
  1104. view.NeedDisplay = Rect.Empty;
  1105. view.childNeedsDisplay = false;
  1106. }
  1107. }
  1108. }
  1109. ClearNeedsDisplay ();
  1110. }
  1111. /// <summary>
  1112. /// Event invoked when the content area of the View is to be drawn.
  1113. /// </summary>
  1114. /// <remarks>
  1115. /// <para>
  1116. /// Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.
  1117. /// </para>
  1118. /// <para>
  1119. /// Rect provides the view-relative rectangle describing the currently visible viewport into the <see cref="View"/>.
  1120. /// </para>
  1121. /// </remarks>
  1122. public Action<Rect> DrawContent;
  1123. /// <summary>
  1124. /// Enables overrides to draw infinitely scrolled content and/or a background behind added controls.
  1125. /// </summary>
  1126. /// <param name="viewport">The view-relative rectangle describing the currently visible viewport into the <see cref="View"/></param>
  1127. /// <remarks>
  1128. /// This method will be called before any subviews added with <see cref="Add(View)"/> have been drawn.
  1129. /// </remarks>
  1130. public virtual void OnDrawContent (Rect viewport)
  1131. {
  1132. DrawContent?.Invoke (viewport);
  1133. }
  1134. /// <summary>
  1135. /// Causes the specified subview to have focus.
  1136. /// </summary>
  1137. /// <param name="view">View.</param>
  1138. public void SetFocus (View view)
  1139. {
  1140. if (view == null)
  1141. return;
  1142. //Console.WriteLine ($"Request to focus {view}");
  1143. if (!view.CanFocus)
  1144. return;
  1145. if (focused == view)
  1146. return;
  1147. // Make sure that this view is a subview
  1148. View c;
  1149. for (c = view.container; c != null; c = c.container)
  1150. if (c == this)
  1151. break;
  1152. if (c == null)
  1153. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  1154. if (focused != null)
  1155. focused.SetHasFocus (false, view);
  1156. var f = focused;
  1157. focused = view;
  1158. focused.SetHasFocus (true, f);
  1159. focused.EnsureFocus ();
  1160. // Send focus upwards
  1161. SuperView?.SetFocus (this);
  1162. }
  1163. /// <summary>
  1164. /// Defines the event arguments for <see cref="KeyEvent"/>
  1165. /// </summary>
  1166. public class KeyEventEventArgs : EventArgs {
  1167. /// <summary>
  1168. /// Constructs.
  1169. /// </summary>
  1170. /// <param name="ke"></param>
  1171. public KeyEventEventArgs (KeyEvent ke) => KeyEvent = ke;
  1172. /// <summary>
  1173. /// The <see cref="KeyEvent"/> for the event.
  1174. /// </summary>
  1175. public KeyEvent KeyEvent { get; set; }
  1176. /// <summary>
  1177. /// Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber.
  1178. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1179. /// </summary>
  1180. public bool Handled { get; set; } = false;
  1181. }
  1182. /// <summary>
  1183. /// Invoked when a character key is pressed and occurs after the key up event.
  1184. /// </summary>
  1185. public Action<KeyEventEventArgs> KeyPress;
  1186. /// <inheritdoc/>
  1187. public override bool ProcessKey (KeyEvent keyEvent)
  1188. {
  1189. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1190. KeyPress?.Invoke (args);
  1191. if (args.Handled)
  1192. return true;
  1193. if (Focused?.ProcessKey (keyEvent) == true)
  1194. return true;
  1195. return false;
  1196. }
  1197. /// <inheritdoc/>
  1198. public override bool ProcessHotKey (KeyEvent keyEvent)
  1199. {
  1200. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1201. KeyPress?.Invoke (args);
  1202. if (args.Handled)
  1203. return true;
  1204. if (subviews == null || subviews.Count == 0)
  1205. return false;
  1206. foreach (var view in subviews)
  1207. if (view.ProcessHotKey (keyEvent))
  1208. return true;
  1209. return false;
  1210. }
  1211. /// <inheritdoc/>
  1212. public override bool ProcessColdKey (KeyEvent keyEvent)
  1213. {
  1214. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1215. KeyPress?.Invoke (args);
  1216. if (args.Handled)
  1217. return true;
  1218. if (subviews == null || subviews.Count == 0)
  1219. return false;
  1220. foreach (var view in subviews)
  1221. if (view.ProcessColdKey (keyEvent))
  1222. return true;
  1223. return false;
  1224. }
  1225. /// <summary>
  1226. /// Invoked when a key is pressed
  1227. /// </summary>
  1228. public Action<KeyEventEventArgs> KeyDown;
  1229. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  1230. public override bool OnKeyDown (KeyEvent keyEvent)
  1231. {
  1232. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1233. KeyDown?.Invoke (args);
  1234. if (args.Handled)
  1235. return true;
  1236. if (subviews == null || subviews.Count == 0)
  1237. return false;
  1238. foreach (var view in subviews)
  1239. if (view.HasFocus && view.OnKeyDown (keyEvent))
  1240. return true;
  1241. return false;
  1242. }
  1243. /// <summary>
  1244. /// Invoked when a key is released
  1245. /// </summary>
  1246. public Action<KeyEventEventArgs> KeyUp;
  1247. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  1248. public override bool OnKeyUp (KeyEvent keyEvent)
  1249. {
  1250. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1251. KeyUp?.Invoke (args);
  1252. if (args.Handled)
  1253. return true;
  1254. if (subviews == null || subviews.Count == 0)
  1255. return false;
  1256. foreach (var view in subviews)
  1257. if (view.HasFocus && view.OnKeyUp (keyEvent))
  1258. return true;
  1259. return false;
  1260. }
  1261. /// <summary>
  1262. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
  1263. /// </summary>
  1264. public void EnsureFocus ()
  1265. {
  1266. if (focused == null)
  1267. if (FocusDirection == Direction.Forward)
  1268. FocusFirst ();
  1269. else
  1270. FocusLast ();
  1271. }
  1272. /// <summary>
  1273. /// Focuses the first focusable subview if one exists.
  1274. /// </summary>
  1275. public void FocusFirst ()
  1276. {
  1277. if (tabIndexes == null) {
  1278. SuperView?.SetFocus (this);
  1279. return;
  1280. }
  1281. foreach (var view in tabIndexes) {
  1282. if (view.CanFocus && view.tabStop) {
  1283. SetFocus (view);
  1284. return;
  1285. }
  1286. }
  1287. }
  1288. /// <summary>
  1289. /// Focuses the last focusable subview if one exists.
  1290. /// </summary>
  1291. public void FocusLast ()
  1292. {
  1293. if (tabIndexes == null) {
  1294. SuperView?.SetFocus (this);
  1295. return;
  1296. }
  1297. for (int i = tabIndexes.Count; i > 0;) {
  1298. i--;
  1299. View v = tabIndexes [i];
  1300. if (v.CanFocus && v.tabStop) {
  1301. SetFocus (v);
  1302. return;
  1303. }
  1304. }
  1305. }
  1306. /// <summary>
  1307. /// Focuses the previous view.
  1308. /// </summary>
  1309. /// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
  1310. public bool FocusPrev ()
  1311. {
  1312. FocusDirection = Direction.Backward;
  1313. if (tabIndexes == null || tabIndexes.Count == 0)
  1314. return false;
  1315. if (focused == null) {
  1316. FocusLast ();
  1317. return focused != null;
  1318. }
  1319. int focused_idx = -1;
  1320. for (int i = tabIndexes.Count; i > 0;) {
  1321. i--;
  1322. View w = tabIndexes [i];
  1323. if (w.HasFocus) {
  1324. if (w.FocusPrev ())
  1325. return true;
  1326. focused_idx = i;
  1327. continue;
  1328. }
  1329. if (w.CanFocus && focused_idx != -1 && w.tabStop) {
  1330. focused.SetHasFocus (false, w);
  1331. if (w != null && w.CanFocus && w.tabStop)
  1332. w.FocusLast ();
  1333. SetFocus (w);
  1334. return true;
  1335. }
  1336. }
  1337. if (focused != null) {
  1338. focused.SetHasFocus (false, this);
  1339. focused = null;
  1340. }
  1341. return false;
  1342. }
  1343. /// <summary>
  1344. /// Focuses the next view.
  1345. /// </summary>
  1346. /// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
  1347. public bool FocusNext ()
  1348. {
  1349. FocusDirection = Direction.Forward;
  1350. if (tabIndexes == null || tabIndexes.Count == 0)
  1351. return false;
  1352. if (focused == null) {
  1353. FocusFirst ();
  1354. return focused != null;
  1355. }
  1356. int n = tabIndexes.Count;
  1357. int focused_idx = -1;
  1358. for (int i = 0; i < n; i++) {
  1359. View w = tabIndexes [i];
  1360. if (w.HasFocus) {
  1361. if (w.FocusNext ())
  1362. return true;
  1363. focused_idx = i;
  1364. continue;
  1365. }
  1366. if (w.CanFocus && focused_idx != -1 && w.tabStop) {
  1367. focused.SetHasFocus (false, w);
  1368. if (w != null && w.CanFocus && w.tabStop)
  1369. w.FocusFirst ();
  1370. SetFocus (w);
  1371. return true;
  1372. }
  1373. }
  1374. if (focused != null) {
  1375. focused.SetHasFocus (false, this);
  1376. focused = null;
  1377. }
  1378. return false;
  1379. }
  1380. /// <summary>
  1381. /// Sets the View's <see cref="Frame"/> to the relative coordinates if its container, given the <see cref="Frame"/> for its container.
  1382. /// </summary>
  1383. /// <param name="hostFrame">The screen-relative frame for the host.</param>
  1384. /// <remarks>
  1385. /// Reminder: <see cref="Frame"/> is superview-relative; <see cref="Bounds"/> is view-relative.
  1386. /// </remarks>
  1387. internal void SetRelativeLayout (Rect hostFrame)
  1388. {
  1389. int w, h, _x, _y;
  1390. if (x is Pos.PosCenter) {
  1391. if (width == null)
  1392. w = hostFrame.Width;
  1393. else
  1394. w = width.Anchor (hostFrame.Width);
  1395. _x = x.Anchor (hostFrame.Width - w);
  1396. } else {
  1397. if (x == null)
  1398. _x = 0;
  1399. else
  1400. _x = x.Anchor (hostFrame.Width);
  1401. if (width == null)
  1402. w = hostFrame.Width;
  1403. else if (width is Dim.DimFactor && !((Dim.DimFactor)width).IsFromRemaining ())
  1404. w = width.Anchor (hostFrame.Width);
  1405. else
  1406. w = Math.Max (width.Anchor (hostFrame.Width - _x), 0);
  1407. }
  1408. if (y is Pos.PosCenter) {
  1409. if (height == null)
  1410. h = hostFrame.Height;
  1411. else
  1412. h = height.Anchor (hostFrame.Height);
  1413. _y = y.Anchor (hostFrame.Height - h);
  1414. } else {
  1415. if (y == null)
  1416. _y = 0;
  1417. else
  1418. _y = y.Anchor (hostFrame.Height);
  1419. if (height == null)
  1420. h = hostFrame.Height;
  1421. else if (height is Dim.DimFactor && !((Dim.DimFactor)height).IsFromRemaining ())
  1422. h = height.Anchor (hostFrame.Height);
  1423. else
  1424. h = Math.Max (height.Anchor (hostFrame.Height - _y), 0);
  1425. }
  1426. Frame = new Rect (_x, _y, w, h);
  1427. }
  1428. // https://en.wikipedia.org/wiki/Topological_sorting
  1429. List<View> TopologicalSort (HashSet<View> nodes, HashSet<(View From, View To)> edges)
  1430. {
  1431. var result = new List<View> ();
  1432. // Set of all nodes with no incoming edges
  1433. var S = new HashSet<View> (nodes.Where (n => edges.All (e => e.To.Equals (n) == false)));
  1434. while (S.Any ()) {
  1435. // remove a node n from S
  1436. var n = S.First ();
  1437. S.Remove (n);
  1438. // add n to tail of L
  1439. if (n != this?.SuperView)
  1440. result.Add (n);
  1441. // for each node m with an edge e from n to m do
  1442. foreach (var e in edges.Where (e => e.From.Equals (n)).ToArray ()) {
  1443. var m = e.To;
  1444. // remove edge e from the graph
  1445. edges.Remove (e);
  1446. // if m has no other incoming edges then
  1447. if (edges.All (me => me.To.Equals (m) == false) && m != this?.SuperView) {
  1448. // insert m into S
  1449. S.Add (m);
  1450. }
  1451. }
  1452. }
  1453. if (edges.Any ()) {
  1454. if (!object.ReferenceEquals (edges.First ().From, edges.First ().To)) {
  1455. throw new InvalidOperationException ($"TopologicalSort (for Pos/Dim) cannot find {edges.First ().From}. Did you forget to add it to {this}?");
  1456. } else {
  1457. throw new InvalidOperationException ("TopologicalSort encountered a recursive cycle in the relative Pos/Dim in the views of " + this);
  1458. }
  1459. } else {
  1460. // return L (a topologically sorted order)
  1461. return result;
  1462. }
  1463. }
  1464. /// <summary>
  1465. /// Event arguments for the <see cref="LayoutComplete"/> event.
  1466. /// </summary>
  1467. public class LayoutEventArgs : EventArgs {
  1468. /// <summary>
  1469. /// The view-relative bounds of the <see cref="View"/> before it was laid out.
  1470. /// </summary>
  1471. public Rect OldBounds { get; set; }
  1472. }
  1473. /// <summary>
  1474. /// Fired after the Views's <see cref="LayoutSubviews"/> method has completed.
  1475. /// </summary>
  1476. /// <remarks>
  1477. /// Subscribe to this event to perform tasks when the <see cref="View"/> has been resized or the layout has otherwise changed.
  1478. /// </remarks>
  1479. public Action<LayoutEventArgs> LayoutStarted;
  1480. /// <summary>
  1481. /// Raises the <see cref="LayoutStarted"/> event. Called from <see cref="LayoutSubviews"/> before any subviews have been laid out.
  1482. /// </summary>
  1483. internal virtual void OnLayoutStarted (LayoutEventArgs args)
  1484. {
  1485. LayoutStarted?.Invoke (args);
  1486. }
  1487. /// <summary>
  1488. /// Fired after the Views's <see cref="LayoutSubviews"/> method has completed.
  1489. /// </summary>
  1490. /// <remarks>
  1491. /// Subscribe to this event to perform tasks when the <see cref="View"/> has been resized or the layout has otherwise changed.
  1492. /// </remarks>
  1493. public Action<LayoutEventArgs> LayoutComplete;
  1494. /// <summary>
  1495. /// Raises the <see cref="LayoutComplete"/> event. Called from <see cref="LayoutSubviews"/> before all sub-views have been laid out.
  1496. /// </summary>
  1497. internal virtual void OnLayoutComplete (LayoutEventArgs args)
  1498. {
  1499. LayoutComplete?.Invoke (args);
  1500. }
  1501. /// <summary>
  1502. /// Invoked when a view starts executing or when the dimensions of the view have changed, for example in
  1503. /// response to the container view or terminal resizing.
  1504. /// </summary>
  1505. /// <remarks>
  1506. /// Calls <see cref="OnLayoutComplete"/> (which raises the <see cref="LayoutComplete"/> event) before it returns.
  1507. /// </remarks>
  1508. public virtual void LayoutSubviews ()
  1509. {
  1510. if (!layoutNeeded) {
  1511. return;
  1512. }
  1513. Rect oldBounds = Bounds;
  1514. OnLayoutStarted (new LayoutEventArgs () { OldBounds = oldBounds });
  1515. textFormatter.Size = Bounds.Size;
  1516. // Sort out the dependencies of the X, Y, Width, Height properties
  1517. var nodes = new HashSet<View> ();
  1518. var edges = new HashSet<(View, View)> ();
  1519. foreach (var v in InternalSubviews) {
  1520. nodes.Add (v);
  1521. if (v.LayoutStyle == LayoutStyle.Computed) {
  1522. if (v.X is Pos.PosView vX)
  1523. edges.Add ((vX.Target, v));
  1524. if (v.Y is Pos.PosView vY)
  1525. edges.Add ((vY.Target, v));
  1526. if (v.Width is Dim.DimView vWidth)
  1527. edges.Add ((vWidth.Target, v));
  1528. if (v.Height is Dim.DimView vHeight)
  1529. edges.Add ((vHeight.Target, v));
  1530. }
  1531. }
  1532. var ordered = TopologicalSort (nodes, edges);
  1533. foreach (var v in ordered) {
  1534. if (v.LayoutStyle == LayoutStyle.Computed) {
  1535. v.SetRelativeLayout (Frame);
  1536. }
  1537. v.LayoutSubviews ();
  1538. v.layoutNeeded = false;
  1539. }
  1540. if (SuperView == Application.Top && layoutNeeded && ordered.Count == 0 && LayoutStyle == LayoutStyle.Computed) {
  1541. SetRelativeLayout (Frame);
  1542. }
  1543. layoutNeeded = false;
  1544. OnLayoutComplete (new LayoutEventArgs () { OldBounds = oldBounds });
  1545. }
  1546. /// <summary>
  1547. /// The text displayed by the <see cref="View"/>.
  1548. /// </summary>
  1549. /// <remarks>
  1550. /// <para>
  1551. /// If provided, the text will be drawn before any subviews are drawn.
  1552. /// </para>
  1553. /// <para>
  1554. /// The text will be drawn starting at the view origin (0, 0) and will be formatted according
  1555. /// to the <see cref="TextAlignment"/> property. If the view's height is greater than 1, the
  1556. /// text will word-wrap to additional lines if it does not fit horizontally. If the view's height
  1557. /// is 1, the text will be clipped.
  1558. /// </para>
  1559. /// <para>
  1560. /// Set the <see cref="HotKeySpecifier"/> to enable hotkey support. To disable hotkey support set <see cref="HotKeySpecifier"/> to
  1561. /// <c>(Rune)0xffff</c>.
  1562. /// </para>
  1563. /// </remarks>
  1564. public virtual ustring Text {
  1565. get => textFormatter.Text;
  1566. set {
  1567. textFormatter.Text = value;
  1568. SetNeedsDisplay ();
  1569. }
  1570. }
  1571. /// <summary>
  1572. /// Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will redisplay the <see cref="View"/>.
  1573. /// </summary>
  1574. /// <value>The text alignment.</value>
  1575. public virtual TextAlignment TextAlignment {
  1576. get => textFormatter.Alignment;
  1577. set {
  1578. textFormatter.Alignment = value;
  1579. SetNeedsDisplay ();
  1580. }
  1581. }
  1582. /// <summary>
  1583. /// Pretty prints the View
  1584. /// </summary>
  1585. /// <returns></returns>
  1586. public override string ToString ()
  1587. {
  1588. return $"{GetType ().Name}({Id})({Frame})";
  1589. }
  1590. /// <summary>
  1591. /// Specifies the event arguments for <see cref="MouseEvent"/>
  1592. /// </summary>
  1593. public class MouseEventArgs : EventArgs {
  1594. /// <summary>
  1595. /// Constructs.
  1596. /// </summary>
  1597. /// <param name="me"></param>
  1598. public MouseEventArgs (MouseEvent me) => MouseEvent = me;
  1599. /// <summary>
  1600. /// The <see cref="MouseEvent"/> for the event.
  1601. /// </summary>
  1602. public MouseEvent MouseEvent { get; set; }
  1603. /// <summary>
  1604. /// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
  1605. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1606. /// </summary>
  1607. public bool Handled { get; set; }
  1608. }
  1609. /// <inheritdoc/>
  1610. public override bool OnMouseEnter (MouseEvent mouseEvent)
  1611. {
  1612. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  1613. MouseEnter?.Invoke (args);
  1614. if (args.Handled)
  1615. return true;
  1616. if (base.OnMouseEnter (mouseEvent))
  1617. return true;
  1618. return false;
  1619. }
  1620. /// <inheritdoc/>
  1621. public override bool OnMouseLeave (MouseEvent mouseEvent)
  1622. {
  1623. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  1624. MouseLeave?.Invoke (args);
  1625. if (args.Handled)
  1626. return true;
  1627. if (base.OnMouseLeave (mouseEvent))
  1628. return true;
  1629. return false;
  1630. }
  1631. /// <summary>
  1632. /// Method invoked when a mouse event is generated
  1633. /// </summary>
  1634. /// <param name="mouseEvent"></param>
  1635. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  1636. public virtual bool OnMouseEvent (MouseEvent mouseEvent)
  1637. {
  1638. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  1639. MouseClick?.Invoke (args);
  1640. if (args.Handled)
  1641. return true;
  1642. if (MouseEvent (mouseEvent))
  1643. return true;
  1644. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  1645. if (!HasFocus && SuperView != null) {
  1646. SuperView.SetFocus (this);
  1647. SetNeedsDisplay ();
  1648. }
  1649. return true;
  1650. }
  1651. return false;
  1652. }
  1653. /// <inheritdoc/>
  1654. protected override void Dispose (bool disposing)
  1655. {
  1656. for (int i = InternalSubviews.Count - 1; i >= 0; i--) {
  1657. View subview = InternalSubviews [i];
  1658. Remove (subview);
  1659. subview.Dispose ();
  1660. }
  1661. base.Dispose (disposing);
  1662. }
  1663. }
  1664. }