View.cs 58 KB

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