View.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  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 (IsInitialized && !view.IsInitialized) {
  565. view.BeginInit ();
  566. view.EndInit ();
  567. }
  568. if (view.Frame.IntersectsWith (region)) {
  569. var childRegion = Rect.Intersect (view.Frame, region);
  570. childRegion.X -= view.Frame.X;
  571. childRegion.Y -= view.Frame.Y;
  572. view.SetNeedsDisplay (childRegion);
  573. }
  574. }
  575. }
  576. internal bool childNeedsDisplay;
  577. /// <summary>
  578. /// Indicates that any child views (in the <see cref="Subviews"/> list) need to be repainted.
  579. /// </summary>
  580. public void ChildNeedsDisplay ()
  581. {
  582. childNeedsDisplay = true;
  583. if (container != null)
  584. container.ChildNeedsDisplay ();
  585. }
  586. /// <summary>
  587. /// Adds a subview (child) to this view.
  588. /// </summary>
  589. /// <remarks>
  590. /// 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"/>
  591. /// </remarks>
  592. public virtual void Add (View view)
  593. {
  594. if (view == null)
  595. return;
  596. if (subviews == null) {
  597. subviews = new List<View> ();
  598. }
  599. if (tabIndexes == null) {
  600. tabIndexes = new List<View> ();
  601. }
  602. subviews.Add (view);
  603. tabIndexes.Add (view);
  604. view.container = this;
  605. OnAdded (view);
  606. if (view.CanFocus) {
  607. CanFocus = true;
  608. view.tabIndex = tabIndexes.IndexOf (view);
  609. }
  610. SetNeedsLayout ();
  611. SetNeedsDisplay ();
  612. }
  613. /// <summary>
  614. /// Adds the specified views (children) to the view.
  615. /// </summary>
  616. /// <param name="views">Array of one or more views (can be optional parameter).</param>
  617. /// <remarks>
  618. /// 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"/>
  619. /// </remarks>
  620. public void Add (params View [] views)
  621. {
  622. if (views == null)
  623. return;
  624. foreach (var view in views)
  625. Add (view);
  626. }
  627. /// <summary>
  628. /// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  629. /// </summary>
  630. public virtual void RemoveAll ()
  631. {
  632. if (subviews == null)
  633. return;
  634. while (subviews.Count > 0) {
  635. Remove (subviews [0]);
  636. }
  637. }
  638. /// <summary>
  639. /// Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  640. /// </summary>
  641. /// <remarks>
  642. /// </remarks>
  643. public virtual void Remove (View view)
  644. {
  645. if (view == null || subviews == null)
  646. return;
  647. SetNeedsLayout ();
  648. SetNeedsDisplay ();
  649. var touched = view.Frame;
  650. subviews.Remove (view);
  651. tabIndexes.Remove (view);
  652. view.container = null;
  653. OnRemoved (view);
  654. view.tabIndex = -1;
  655. if (subviews.Count < 1)
  656. this.CanFocus = false;
  657. foreach (var v in subviews) {
  658. if (v.Frame.IntersectsWith (touched))
  659. view.SetNeedsDisplay ();
  660. }
  661. }
  662. void PerformActionForSubview (View subview, Action<View> action)
  663. {
  664. if (subviews.Contains (subview)) {
  665. action (subview);
  666. }
  667. SetNeedsDisplay ();
  668. subview.SetNeedsDisplay ();
  669. }
  670. /// <summary>
  671. /// Brings the specified subview to the front so it is drawn on top of any other views.
  672. /// </summary>
  673. /// <param name="subview">The subview to send to the front</param>
  674. /// <remarks>
  675. /// <seealso cref="SendSubviewToBack"/>.
  676. /// </remarks>
  677. public void BringSubviewToFront (View subview)
  678. {
  679. PerformActionForSubview (subview, x => {
  680. subviews.Remove (x);
  681. subviews.Add (x);
  682. });
  683. }
  684. /// <summary>
  685. /// Sends the specified subview to the front so it is the first view drawn
  686. /// </summary>
  687. /// <param name="subview">The subview to send to the front</param>
  688. /// <remarks>
  689. /// <seealso cref="BringSubviewToFront(View)"/>.
  690. /// </remarks>
  691. public void SendSubviewToBack (View subview)
  692. {
  693. PerformActionForSubview (subview, x => {
  694. subviews.Remove (x);
  695. subviews.Insert (0, subview);
  696. });
  697. }
  698. /// <summary>
  699. /// Moves the subview backwards in the hierarchy, only one step
  700. /// </summary>
  701. /// <param name="subview">The subview to send backwards</param>
  702. /// <remarks>
  703. /// If you want to send the view all the way to the back use SendSubviewToBack.
  704. /// </remarks>
  705. public void SendSubviewBackwards (View subview)
  706. {
  707. PerformActionForSubview (subview, x => {
  708. var idx = subviews.IndexOf (x);
  709. if (idx > 0) {
  710. subviews.Remove (x);
  711. subviews.Insert (idx - 1, x);
  712. }
  713. });
  714. }
  715. /// <summary>
  716. /// Moves the subview backwards in the hierarchy, only one step
  717. /// </summary>
  718. /// <param name="subview">The subview to send backwards</param>
  719. /// <remarks>
  720. /// If you want to send the view all the way to the back use SendSubviewToBack.
  721. /// </remarks>
  722. public void BringSubviewForward (View subview)
  723. {
  724. PerformActionForSubview (subview, x => {
  725. var idx = subviews.IndexOf (x);
  726. if (idx + 1 < subviews.Count) {
  727. subviews.Remove (x);
  728. subviews.Insert (idx + 1, x);
  729. }
  730. });
  731. }
  732. /// <summary>
  733. /// Clears the view region with the current color.
  734. /// </summary>
  735. /// <remarks>
  736. /// <para>
  737. /// This clears the entire region used by this view.
  738. /// </para>
  739. /// </remarks>
  740. public void Clear ()
  741. {
  742. var h = Frame.Height;
  743. var w = Frame.Width;
  744. for (int line = 0; line < h; line++) {
  745. Move (0, line);
  746. for (int col = 0; col < w; col++)
  747. Driver.AddRune (' ');
  748. }
  749. }
  750. /// <summary>
  751. /// Clears the specified region with the current color.
  752. /// </summary>
  753. /// <remarks>
  754. /// </remarks>
  755. /// <param name="regionScreen">The screen-relative region to clear.</param>
  756. public void Clear (Rect regionScreen)
  757. {
  758. var h = regionScreen.Height;
  759. var w = regionScreen.Width;
  760. for (int line = regionScreen.Y; line < regionScreen.Y + h; line++) {
  761. Driver.Move (regionScreen.X, line);
  762. for (int col = 0; col < w; col++)
  763. Driver.AddRune (' ');
  764. }
  765. }
  766. /// <summary>
  767. /// Converts a view-relative (col,row) position to a screen-relative position (col,row). The values are optionally clamped to the screen dimensions.
  768. /// </summary>
  769. /// <param name="col">View-relative column.</param>
  770. /// <param name="row">View-relative row.</param>
  771. /// <param name="rcol">Absolute column; screen-relative.</param>
  772. /// <param name="rrow">Absolute row; screen-relative.</param>
  773. /// <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>
  774. internal void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  775. {
  776. // Computes the real row, col relative to the screen.
  777. rrow = row + frame.Y;
  778. rcol = col + frame.X;
  779. var ccontainer = container;
  780. while (ccontainer != null) {
  781. rrow += ccontainer.frame.Y;
  782. rcol += ccontainer.frame.X;
  783. ccontainer = ccontainer.container;
  784. }
  785. // The following ensures that the cursor is always in the screen boundaries.
  786. if (clipped) {
  787. rrow = Math.Min (rrow, Driver.Rows - 1);
  788. rcol = Math.Min (rcol, Driver.Cols - 1);
  789. }
  790. }
  791. /// <summary>
  792. /// Converts a point from screen-relative coordinates to view-relative coordinates.
  793. /// </summary>
  794. /// <returns>The mapped point.</returns>
  795. /// <param name="x">X screen-coordinate point.</param>
  796. /// <param name="y">Y screen-coordinate point.</param>
  797. public Point ScreenToView (int x, int y)
  798. {
  799. if (SuperView == null) {
  800. return new Point (x - Frame.X, y - frame.Y);
  801. } else {
  802. var parent = SuperView.ScreenToView (x, y);
  803. return new Point (parent.X - frame.X, parent.Y - frame.Y);
  804. }
  805. }
  806. /// <summary>
  807. /// Converts a region in view-relative coordinates to screen-relative coordinates.
  808. /// </summary>
  809. internal Rect ViewToScreen (Rect region)
  810. {
  811. ViewToScreen (region.X, region.Y, out var x, out var y, clipped: false);
  812. return new Rect (x, y, region.Width, region.Height);
  813. }
  814. // Clips a rectangle in screen coordinates to the dimensions currently available on the screen
  815. internal Rect ScreenClip (Rect regionScreen)
  816. {
  817. var x = regionScreen.X < 0 ? 0 : regionScreen.X;
  818. var y = regionScreen.Y < 0 ? 0 : regionScreen.Y;
  819. var w = regionScreen.X + regionScreen.Width >= Driver.Cols ? Driver.Cols - regionScreen.X : regionScreen.Width;
  820. var h = regionScreen.Y + regionScreen.Height >= Driver.Rows ? Driver.Rows - regionScreen.Y : regionScreen.Height;
  821. return new Rect (x, y, w, h);
  822. }
  823. /// <summary>
  824. /// Sets the <see cref="ConsoleDriver"/>'s clip region to the current View's <see cref="Bounds"/>.
  825. /// </summary>
  826. /// <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>
  827. /// <remarks>
  828. /// <see cref="Bounds"/> is View-relative.
  829. /// </remarks>
  830. public Rect ClipToBounds ()
  831. {
  832. return SetClip (Bounds);
  833. }
  834. /// <summary>
  835. /// Sets the clip region to the specified view-relative region.
  836. /// </summary>
  837. /// <returns>The previous screen-relative clip region.</returns>
  838. /// <param name="region">View-relative clip region.</param>
  839. public Rect SetClip (Rect region)
  840. {
  841. var previous = Driver.Clip;
  842. Driver.Clip = Rect.Intersect (previous, ViewToScreen (region));
  843. return previous;
  844. }
  845. /// <summary>
  846. /// Draws a frame in the current view, clipped by the boundary of this view
  847. /// </summary>
  848. /// <param name="region">View-relative region for the frame to be drawn.</param>
  849. /// <param name="padding">The padding to add around the outside of the drawn frame.</param>
  850. /// <param name="fill">If set to <c>true</c> it fill will the contents.</param>
  851. public void DrawFrame (Rect region, int padding = 0, bool fill = false)
  852. {
  853. var scrRect = ViewToScreen (region);
  854. var savedClip = ClipToBounds ();
  855. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: fill);
  856. Driver.Clip = savedClip;
  857. }
  858. /// <summary>
  859. /// Utility function to draw strings that contain a hotkey.
  860. /// </summary>
  861. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  862. /// <param name="hotColor">Hot color.</param>
  863. /// <param name="normalColor">Normal color.</param>
  864. /// <remarks>
  865. /// <para>The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by default.</para>
  866. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  867. /// </remarks>
  868. public void DrawHotString (ustring text, Attribute hotColor, Attribute normalColor)
  869. {
  870. var hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  871. Application.Driver.SetAttribute (normalColor);
  872. foreach (var rune in text) {
  873. if (rune == hotkeySpec) {
  874. Application.Driver.SetAttribute (hotColor);
  875. continue;
  876. }
  877. Application.Driver.AddRune (rune);
  878. Application.Driver.SetAttribute (normalColor);
  879. }
  880. }
  881. /// <summary>
  882. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused" state.
  883. /// </summary>
  884. /// <param name="text">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
  885. /// <param name="focused">If set to <c>true</c> this uses the focused colors from the color scheme, otherwise the regular ones.</param>
  886. /// <param name="scheme">The color scheme to use.</param>
  887. public void DrawHotString (ustring text, bool focused, ColorScheme scheme)
  888. {
  889. if (focused)
  890. DrawHotString (text, scheme.HotFocus, scheme.Focus);
  891. else
  892. DrawHotString (text, scheme.HotNormal, scheme.Normal);
  893. }
  894. /// <summary>
  895. /// This moves the cursor to the specified column and row in the view.
  896. /// </summary>
  897. /// <returns>The move.</returns>
  898. /// <param name="col">Col.</param>
  899. /// <param name="row">Row.</param>
  900. public void Move (int col, int row)
  901. {
  902. ViewToScreen (col, row, out var rcol, out var rrow);
  903. Driver.Move (rcol, rrow);
  904. }
  905. /// <summary>
  906. /// Positions the cursor in the right position based on the currently focused view in the chain.
  907. /// </summary>
  908. /// Views that are focusable should override <see cref="PositionCursor"/> to ensure
  909. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  910. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  911. /// the last focused view. Views should make sure that they place the cursor
  912. /// in a visually sensible place.
  913. public virtual void PositionCursor ()
  914. {
  915. if (focused != null)
  916. focused.PositionCursor ();
  917. else {
  918. if (CanFocus && HasFocus) {
  919. Move (textFormatter.HotKeyPos == -1 ? 0 : textFormatter.HotKeyPos, 0);
  920. } else {
  921. Move (frame.X, frame.Y);
  922. }
  923. }
  924. }
  925. bool hasFocus;
  926. /// <inheritdoc/>
  927. public override bool HasFocus {
  928. get {
  929. return hasFocus;
  930. }
  931. }
  932. void SetHasFocus (bool value, View view)
  933. {
  934. if (hasFocus != value) {
  935. hasFocus = value;
  936. }
  937. if (value) {
  938. OnEnter (view);
  939. } else {
  940. OnLeave (view);
  941. }
  942. SetNeedsDisplay ();
  943. // Remove focus down the chain of subviews if focus is removed
  944. if (!value && focused != null) {
  945. focused.OnLeave (view);
  946. focused.SetHasFocus (false, view);
  947. focused = null;
  948. }
  949. }
  950. /// <summary>
  951. /// Defines the event arguments for <see cref="SetFocus(View)"/>
  952. /// </summary>
  953. public class FocusEventArgs : EventArgs {
  954. /// <summary>
  955. /// Constructs.
  956. /// </summary>
  957. /// <param name="view">The view that gets or loses focus.</param>
  958. public FocusEventArgs (View view) { View = view; }
  959. /// <summary>
  960. /// Indicates if the current focus event has already been processed and the driver should stop notifying any other event subscriber.
  961. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  962. /// </summary>
  963. public bool Handled { get; set; }
  964. /// <summary>
  965. /// Indicates the current view that gets or loses focus.
  966. /// </summary>
  967. public View View { get; set; }
  968. }
  969. /// <summary>
  970. /// Method invoked when a subview is being added to this view.
  971. /// </summary>
  972. /// <param name="view">The subview being added.</param>
  973. public virtual void OnAdded (View view)
  974. {
  975. view.Added?.Invoke (this);
  976. }
  977. /// <summary>
  978. /// Method invoked when a subview is being removed from this view.
  979. /// </summary>
  980. /// <param name="view">The subview being removed.</param>
  981. public virtual void OnRemoved (View view)
  982. {
  983. view.Removed?.Invoke (this);
  984. }
  985. /// <inheritdoc/>
  986. public override bool OnEnter (View view)
  987. {
  988. FocusEventArgs args = new FocusEventArgs (view);
  989. Enter?.Invoke (args);
  990. if (args.Handled)
  991. return true;
  992. if (base.OnEnter (view))
  993. return true;
  994. return false;
  995. }
  996. /// <inheritdoc/>
  997. public override bool OnLeave (View view)
  998. {
  999. FocusEventArgs args = new FocusEventArgs (view);
  1000. Leave?.Invoke (args);
  1001. if (args.Handled)
  1002. return true;
  1003. if (base.OnLeave (view))
  1004. return true;
  1005. return false;
  1006. }
  1007. /// <summary>
  1008. /// Returns the currently focused view inside this view, or null if nothing is focused.
  1009. /// </summary>
  1010. /// <value>The focused.</value>
  1011. public View Focused => focused;
  1012. /// <summary>
  1013. /// Returns the most focused view in the chain of subviews (the leaf view that has the focus).
  1014. /// </summary>
  1015. /// <value>The most focused.</value>
  1016. public View MostFocused {
  1017. get {
  1018. if (Focused == null)
  1019. return null;
  1020. var most = Focused.MostFocused;
  1021. if (most != null)
  1022. return most;
  1023. return Focused;
  1024. }
  1025. }
  1026. /// <summary>
  1027. /// The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s
  1028. /// color scheme.
  1029. /// </summary>
  1030. public ColorScheme ColorScheme {
  1031. get {
  1032. if (colorScheme == null)
  1033. return SuperView?.ColorScheme;
  1034. return colorScheme;
  1035. }
  1036. set {
  1037. colorScheme = value;
  1038. }
  1039. }
  1040. ColorScheme colorScheme;
  1041. /// <summary>
  1042. /// Displays the specified character in the specified column and row of the View.
  1043. /// </summary>
  1044. /// <param name="col">Column (view-relative).</param>
  1045. /// <param name="row">Row (view-relative).</param>
  1046. /// <param name="ch">Ch.</param>
  1047. public void AddRune (int col, int row, Rune ch)
  1048. {
  1049. if (row < 0 || col < 0)
  1050. return;
  1051. if (row > frame.Height - 1 || col > frame.Width - 1)
  1052. return;
  1053. Move (col, row);
  1054. Driver.AddRune (ch);
  1055. }
  1056. /// <summary>
  1057. /// Removes the <see cref="SetNeedsDisplay()"/> and the <see cref="ChildNeedsDisplay"/> setting on this view.
  1058. /// </summary>
  1059. protected void ClearNeedsDisplay ()
  1060. {
  1061. NeedDisplay = Rect.Empty;
  1062. childNeedsDisplay = false;
  1063. }
  1064. /// <summary>
  1065. /// Redraws this view and its subviews; only redraws the views that have been flagged for a re-display.
  1066. /// </summary>
  1067. /// <param name="bounds">The bounds (view-relative region) to redraw.</param>
  1068. /// <remarks>
  1069. /// <para>
  1070. /// Always use <see cref="Bounds"/> (view-relative) when calling <see cref="Redraw(Rect)"/>, NOT <see cref="Frame"/> (superview-relative).
  1071. /// </para>
  1072. /// <para>
  1073. /// Views should set the color that they want to use on entry, as otherwise this will inherit
  1074. /// the last color that was set globaly on the driver.
  1075. /// </para>
  1076. /// <para>
  1077. /// Overrides of <see cref="Redraw"/> must ensure they do not set <c>Driver.Clip</c> to a clip region
  1078. /// larger than the <c>region</c> parameter.
  1079. /// </para>
  1080. /// </remarks>
  1081. public virtual void Redraw (Rect bounds)
  1082. {
  1083. var clipRect = new Rect (Point.Empty, frame.Size);
  1084. if (ColorScheme != null)
  1085. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  1086. if (!ustring.IsNullOrEmpty (Text)) {
  1087. Clear ();
  1088. // Draw any Text
  1089. if (textFormatter != null) {
  1090. textFormatter.NeedsFormat = true;
  1091. }
  1092. textFormatter?.Draw (ViewToScreen (Bounds), HasFocus ? ColorScheme.Focus : ColorScheme.Normal, HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  1093. }
  1094. // Invoke DrawContentEvent
  1095. OnDrawContent (bounds);
  1096. if (subviews != null) {
  1097. foreach (var view in subviews) {
  1098. if (view.NeedDisplay != null && (!view.NeedDisplay.IsEmpty || view.childNeedsDisplay)) {
  1099. if (view.Frame.IntersectsWith (clipRect) && (view.Frame.IntersectsWith (bounds) || bounds.X < 0 || bounds.Y < 0)) {
  1100. if (view.layoutNeeded)
  1101. view.LayoutSubviews ();
  1102. Application.CurrentView = view;
  1103. // Draw the subview
  1104. // Use the view's bounds (view-relative; Location will always be (0,0) because
  1105. view.Redraw (view.Bounds);
  1106. }
  1107. view.NeedDisplay = Rect.Empty;
  1108. view.childNeedsDisplay = false;
  1109. }
  1110. }
  1111. }
  1112. ClearNeedsDisplay ();
  1113. }
  1114. /// <summary>
  1115. /// Event invoked when the content area of the View is to be drawn.
  1116. /// </summary>
  1117. /// <remarks>
  1118. /// <para>
  1119. /// Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.
  1120. /// </para>
  1121. /// <para>
  1122. /// Rect provides the view-relative rectangle describing the currently visible viewport into the <see cref="View"/>.
  1123. /// </para>
  1124. /// </remarks>
  1125. public Action<Rect> DrawContent;
  1126. /// <summary>
  1127. /// Enables overrides to draw infinitely scrolled content and/or a background behind added controls.
  1128. /// </summary>
  1129. /// <param name="viewport">The view-relative rectangle describing the currently visible viewport into the <see cref="View"/></param>
  1130. /// <remarks>
  1131. /// This method will be called before any subviews added with <see cref="Add(View)"/> have been drawn.
  1132. /// </remarks>
  1133. public virtual void OnDrawContent (Rect viewport)
  1134. {
  1135. DrawContent?.Invoke (viewport);
  1136. }
  1137. /// <summary>
  1138. /// Causes the specified subview to have focus.
  1139. /// </summary>
  1140. /// <param name="view">View.</param>
  1141. public void SetFocus (View view)
  1142. {
  1143. if (view == null)
  1144. return;
  1145. //Console.WriteLine ($"Request to focus {view}");
  1146. if (!view.CanFocus)
  1147. return;
  1148. if (focused == view)
  1149. return;
  1150. // Make sure that this view is a subview
  1151. View c;
  1152. for (c = view.container; c != null; c = c.container)
  1153. if (c == this)
  1154. break;
  1155. if (c == null)
  1156. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  1157. if (focused != null)
  1158. focused.SetHasFocus (false, view);
  1159. var f = focused;
  1160. focused = view;
  1161. focused.SetHasFocus (true, f);
  1162. focused.EnsureFocus ();
  1163. // Send focus upwards
  1164. SuperView?.SetFocus (this);
  1165. }
  1166. /// <summary>
  1167. /// Defines the event arguments for <see cref="KeyEvent"/>
  1168. /// </summary>
  1169. public class KeyEventEventArgs : EventArgs {
  1170. /// <summary>
  1171. /// Constructs.
  1172. /// </summary>
  1173. /// <param name="ke"></param>
  1174. public KeyEventEventArgs (KeyEvent ke) => KeyEvent = ke;
  1175. /// <summary>
  1176. /// The <see cref="KeyEvent"/> for the event.
  1177. /// </summary>
  1178. public KeyEvent KeyEvent { get; set; }
  1179. /// <summary>
  1180. /// Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber.
  1181. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1182. /// </summary>
  1183. public bool Handled { get; set; } = false;
  1184. }
  1185. /// <summary>
  1186. /// Invoked when a character key is pressed and occurs after the key up event.
  1187. /// </summary>
  1188. public Action<KeyEventEventArgs> KeyPress;
  1189. /// <inheritdoc/>
  1190. public override bool ProcessKey (KeyEvent keyEvent)
  1191. {
  1192. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1193. KeyPress?.Invoke (args);
  1194. if (args.Handled)
  1195. return true;
  1196. if (Focused?.ProcessKey (keyEvent) == true)
  1197. return true;
  1198. return false;
  1199. }
  1200. /// <inheritdoc/>
  1201. public override bool ProcessHotKey (KeyEvent keyEvent)
  1202. {
  1203. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1204. KeyPress?.Invoke (args);
  1205. if (args.Handled)
  1206. return true;
  1207. if (subviews == null || subviews.Count == 0)
  1208. return false;
  1209. foreach (var view in subviews)
  1210. if (view.ProcessHotKey (keyEvent))
  1211. return true;
  1212. return false;
  1213. }
  1214. /// <inheritdoc/>
  1215. public override bool ProcessColdKey (KeyEvent keyEvent)
  1216. {
  1217. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1218. KeyPress?.Invoke (args);
  1219. if (args.Handled)
  1220. return true;
  1221. if (subviews == null || subviews.Count == 0)
  1222. return false;
  1223. foreach (var view in subviews)
  1224. if (view.ProcessColdKey (keyEvent))
  1225. return true;
  1226. return false;
  1227. }
  1228. /// <summary>
  1229. /// Invoked when a key is pressed
  1230. /// </summary>
  1231. public Action<KeyEventEventArgs> KeyDown;
  1232. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  1233. public override bool OnKeyDown (KeyEvent keyEvent)
  1234. {
  1235. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1236. KeyDown?.Invoke (args);
  1237. if (args.Handled)
  1238. return true;
  1239. if (subviews == null || subviews.Count == 0)
  1240. return false;
  1241. foreach (var view in subviews)
  1242. if (view.HasFocus && view.OnKeyDown (keyEvent))
  1243. return true;
  1244. return false;
  1245. }
  1246. /// <summary>
  1247. /// Invoked when a key is released
  1248. /// </summary>
  1249. public Action<KeyEventEventArgs> KeyUp;
  1250. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  1251. public override bool OnKeyUp (KeyEvent keyEvent)
  1252. {
  1253. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1254. KeyUp?.Invoke (args);
  1255. if (args.Handled)
  1256. return true;
  1257. if (subviews == null || subviews.Count == 0)
  1258. return false;
  1259. foreach (var view in subviews)
  1260. if (view.HasFocus && view.OnKeyUp (keyEvent))
  1261. return true;
  1262. return false;
  1263. }
  1264. /// <summary>
  1265. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
  1266. /// </summary>
  1267. public void EnsureFocus ()
  1268. {
  1269. if (focused == null)
  1270. if (FocusDirection == Direction.Forward)
  1271. FocusFirst ();
  1272. else
  1273. FocusLast ();
  1274. }
  1275. /// <summary>
  1276. /// Focuses the first focusable subview if one exists.
  1277. /// </summary>
  1278. public void FocusFirst ()
  1279. {
  1280. if (tabIndexes == null) {
  1281. SuperView?.SetFocus (this);
  1282. return;
  1283. }
  1284. foreach (var view in tabIndexes) {
  1285. if (view.CanFocus && view.tabStop) {
  1286. SetFocus (view);
  1287. return;
  1288. }
  1289. }
  1290. }
  1291. /// <summary>
  1292. /// Focuses the last focusable subview if one exists.
  1293. /// </summary>
  1294. public void FocusLast ()
  1295. {
  1296. if (tabIndexes == null) {
  1297. SuperView?.SetFocus (this);
  1298. return;
  1299. }
  1300. for (int i = tabIndexes.Count; i > 0;) {
  1301. i--;
  1302. View v = tabIndexes [i];
  1303. if (v.CanFocus && v.tabStop) {
  1304. SetFocus (v);
  1305. return;
  1306. }
  1307. }
  1308. }
  1309. /// <summary>
  1310. /// Focuses the previous view.
  1311. /// </summary>
  1312. /// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
  1313. public bool FocusPrev ()
  1314. {
  1315. FocusDirection = Direction.Backward;
  1316. if (tabIndexes == null || tabIndexes.Count == 0)
  1317. return false;
  1318. if (focused == null) {
  1319. FocusLast ();
  1320. return focused != null;
  1321. }
  1322. int focused_idx = -1;
  1323. for (int i = tabIndexes.Count; i > 0;) {
  1324. i--;
  1325. View w = tabIndexes [i];
  1326. if (w.HasFocus) {
  1327. if (w.FocusPrev ())
  1328. return true;
  1329. focused_idx = i;
  1330. continue;
  1331. }
  1332. if (w.CanFocus && focused_idx != -1 && w.tabStop) {
  1333. focused.SetHasFocus (false, w);
  1334. if (w != null && w.CanFocus && w.tabStop)
  1335. w.FocusLast ();
  1336. SetFocus (w);
  1337. return true;
  1338. }
  1339. }
  1340. if (focused != null) {
  1341. focused.SetHasFocus (false, this);
  1342. focused = null;
  1343. }
  1344. return false;
  1345. }
  1346. /// <summary>
  1347. /// Focuses the next view.
  1348. /// </summary>
  1349. /// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
  1350. public bool FocusNext ()
  1351. {
  1352. FocusDirection = Direction.Forward;
  1353. if (tabIndexes == null || tabIndexes.Count == 0)
  1354. return false;
  1355. if (focused == null) {
  1356. FocusFirst ();
  1357. return focused != null;
  1358. }
  1359. int n = tabIndexes.Count;
  1360. int focused_idx = -1;
  1361. for (int i = 0; i < n; i++) {
  1362. View w = tabIndexes [i];
  1363. if (w.HasFocus) {
  1364. if (w.FocusNext ())
  1365. return true;
  1366. focused_idx = i;
  1367. continue;
  1368. }
  1369. if (w.CanFocus && focused_idx != -1 && w.tabStop) {
  1370. focused.SetHasFocus (false, w);
  1371. if (w != null && w.CanFocus && w.tabStop)
  1372. w.FocusFirst ();
  1373. SetFocus (w);
  1374. return true;
  1375. }
  1376. }
  1377. if (focused != null) {
  1378. focused.SetHasFocus (false, this);
  1379. focused = null;
  1380. }
  1381. return false;
  1382. }
  1383. /// <summary>
  1384. /// Sets the View's <see cref="Frame"/> to the relative coordinates if its container, given the <see cref="Frame"/> for its container.
  1385. /// </summary>
  1386. /// <param name="hostFrame">The screen-relative frame for the host.</param>
  1387. /// <remarks>
  1388. /// Reminder: <see cref="Frame"/> is superview-relative; <see cref="Bounds"/> is view-relative.
  1389. /// </remarks>
  1390. internal void SetRelativeLayout (Rect hostFrame)
  1391. {
  1392. int w, h, _x, _y;
  1393. if (x is Pos.PosCenter) {
  1394. if (width == null)
  1395. w = hostFrame.Width;
  1396. else
  1397. w = width.Anchor (hostFrame.Width);
  1398. _x = x.Anchor (hostFrame.Width - w);
  1399. } else {
  1400. if (x == null)
  1401. _x = 0;
  1402. else
  1403. _x = x.Anchor (hostFrame.Width);
  1404. if (width == null)
  1405. w = hostFrame.Width;
  1406. else if (width is Dim.DimFactor && !((Dim.DimFactor)width).IsFromRemaining ())
  1407. w = width.Anchor (hostFrame.Width);
  1408. else
  1409. w = Math.Max (width.Anchor (hostFrame.Width - _x), 0);
  1410. }
  1411. if (y is Pos.PosCenter) {
  1412. if (height == null)
  1413. h = hostFrame.Height;
  1414. else
  1415. h = height.Anchor (hostFrame.Height);
  1416. _y = y.Anchor (hostFrame.Height - h);
  1417. } else {
  1418. if (y == null)
  1419. _y = 0;
  1420. else
  1421. _y = y.Anchor (hostFrame.Height);
  1422. if (height == null)
  1423. h = hostFrame.Height;
  1424. else if (height is Dim.DimFactor && !((Dim.DimFactor)height).IsFromRemaining ())
  1425. h = height.Anchor (hostFrame.Height);
  1426. else
  1427. h = Math.Max (height.Anchor (hostFrame.Height - _y), 0);
  1428. }
  1429. Frame = new Rect (_x, _y, w, h);
  1430. }
  1431. // https://en.wikipedia.org/wiki/Topological_sorting
  1432. List<View> TopologicalSort (HashSet<View> nodes, HashSet<(View From, View To)> edges)
  1433. {
  1434. var result = new List<View> ();
  1435. // Set of all nodes with no incoming edges
  1436. var S = new HashSet<View> (nodes.Where (n => edges.All (e => e.To.Equals (n) == false)));
  1437. while (S.Any ()) {
  1438. // remove a node n from S
  1439. var n = S.First ();
  1440. S.Remove (n);
  1441. // add n to tail of L
  1442. if (n != this?.SuperView)
  1443. result.Add (n);
  1444. // for each node m with an edge e from n to m do
  1445. foreach (var e in edges.Where (e => e.From.Equals (n)).ToArray ()) {
  1446. var m = e.To;
  1447. // remove edge e from the graph
  1448. edges.Remove (e);
  1449. // if m has no other incoming edges then
  1450. if (edges.All (me => me.To.Equals (m) == false) && m != this?.SuperView) {
  1451. // insert m into S
  1452. S.Add (m);
  1453. }
  1454. }
  1455. }
  1456. if (edges.Any ()) {
  1457. if (!object.ReferenceEquals (edges.First ().From, edges.First ().To)) {
  1458. throw new InvalidOperationException ($"TopologicalSort (for Pos/Dim) cannot find {edges.First ().From}. Did you forget to add it to {this}?");
  1459. } else {
  1460. throw new InvalidOperationException ("TopologicalSort encountered a recursive cycle in the relative Pos/Dim in the views of " + this);
  1461. }
  1462. } else {
  1463. // return L (a topologically sorted order)
  1464. return result;
  1465. }
  1466. }
  1467. /// <summary>
  1468. /// Event arguments for the <see cref="LayoutComplete"/> event.
  1469. /// </summary>
  1470. public class LayoutEventArgs : EventArgs {
  1471. /// <summary>
  1472. /// The view-relative bounds of the <see cref="View"/> before it was laid out.
  1473. /// </summary>
  1474. public Rect OldBounds { get; set; }
  1475. }
  1476. /// <summary>
  1477. /// Fired after the Views's <see cref="LayoutSubviews"/> method has completed.
  1478. /// </summary>
  1479. /// <remarks>
  1480. /// Subscribe to this event to perform tasks when the <see cref="View"/> has been resized or the layout has otherwise changed.
  1481. /// </remarks>
  1482. public Action<LayoutEventArgs> LayoutStarted;
  1483. /// <summary>
  1484. /// Raises the <see cref="LayoutStarted"/> event. Called from <see cref="LayoutSubviews"/> before any subviews have been laid out.
  1485. /// </summary>
  1486. internal virtual void OnLayoutStarted (LayoutEventArgs args)
  1487. {
  1488. LayoutStarted?.Invoke (args);
  1489. }
  1490. /// <summary>
  1491. /// Fired after the Views's <see cref="LayoutSubviews"/> method has completed.
  1492. /// </summary>
  1493. /// <remarks>
  1494. /// Subscribe to this event to perform tasks when the <see cref="View"/> has been resized or the layout has otherwise changed.
  1495. /// </remarks>
  1496. public Action<LayoutEventArgs> LayoutComplete;
  1497. /// <summary>
  1498. /// Event called only once when the <see cref="View"/> is being initialized for the first time.
  1499. /// Allows configurations and assignments to be performed before the <see cref="View"/> being shown.
  1500. /// This derived from <see cref="ISupportInitializeNotification"/> to allow notify all the views that are being initialized.
  1501. /// </summary>
  1502. public event EventHandler Initialized;
  1503. /// <summary>
  1504. /// Raises the <see cref="LayoutComplete"/> event. Called from <see cref="LayoutSubviews"/> before all sub-views have been laid out.
  1505. /// </summary>
  1506. internal virtual void OnLayoutComplete (LayoutEventArgs args)
  1507. {
  1508. LayoutComplete?.Invoke (args);
  1509. }
  1510. /// <summary>
  1511. /// Invoked when a view starts executing or when the dimensions of the view have changed, for example in
  1512. /// response to the container view or terminal resizing.
  1513. /// </summary>
  1514. /// <remarks>
  1515. /// Calls <see cref="OnLayoutComplete"/> (which raises the <see cref="LayoutComplete"/> event) before it returns.
  1516. /// </remarks>
  1517. public virtual void LayoutSubviews ()
  1518. {
  1519. if (!layoutNeeded) {
  1520. return;
  1521. }
  1522. Rect oldBounds = Bounds;
  1523. OnLayoutStarted (new LayoutEventArgs () { OldBounds = oldBounds });
  1524. textFormatter.Size = Bounds.Size;
  1525. // Sort out the dependencies of the X, Y, Width, Height properties
  1526. var nodes = new HashSet<View> ();
  1527. var edges = new HashSet<(View, View)> ();
  1528. foreach (var v in InternalSubviews) {
  1529. nodes.Add (v);
  1530. if (v.LayoutStyle == LayoutStyle.Computed) {
  1531. if (v.X is Pos.PosView vX)
  1532. edges.Add ((vX.Target, v));
  1533. if (v.Y is Pos.PosView vY)
  1534. edges.Add ((vY.Target, v));
  1535. if (v.Width is Dim.DimView vWidth)
  1536. edges.Add ((vWidth.Target, v));
  1537. if (v.Height is Dim.DimView vHeight)
  1538. edges.Add ((vHeight.Target, v));
  1539. }
  1540. }
  1541. var ordered = TopologicalSort (nodes, edges);
  1542. foreach (var v in ordered) {
  1543. if (v.LayoutStyle == LayoutStyle.Computed) {
  1544. v.SetRelativeLayout (Frame);
  1545. }
  1546. v.LayoutSubviews ();
  1547. v.layoutNeeded = false;
  1548. }
  1549. if (SuperView == Application.Top && layoutNeeded && ordered.Count == 0 && LayoutStyle == LayoutStyle.Computed) {
  1550. SetRelativeLayout (Frame);
  1551. }
  1552. layoutNeeded = false;
  1553. OnLayoutComplete (new LayoutEventArgs () { OldBounds = oldBounds });
  1554. }
  1555. /// <summary>
  1556. /// The text displayed by the <see cref="View"/>.
  1557. /// </summary>
  1558. /// <remarks>
  1559. /// <para>
  1560. /// If provided, the text will be drawn before any subviews are drawn.
  1561. /// </para>
  1562. /// <para>
  1563. /// The text will be drawn starting at the view origin (0, 0) and will be formatted according
  1564. /// to the <see cref="TextAlignment"/> property. If the view's height is greater than 1, the
  1565. /// text will word-wrap to additional lines if it does not fit horizontally. If the view's height
  1566. /// is 1, the text will be clipped.
  1567. /// </para>
  1568. /// <para>
  1569. /// Set the <see cref="HotKeySpecifier"/> to enable hotkey support. To disable hotkey support set <see cref="HotKeySpecifier"/> to
  1570. /// <c>(Rune)0xffff</c>.
  1571. /// </para>
  1572. /// </remarks>
  1573. public virtual ustring Text {
  1574. get => textFormatter.Text;
  1575. set {
  1576. textFormatter.Text = value;
  1577. SetNeedsDisplay ();
  1578. }
  1579. }
  1580. /// <summary>
  1581. /// Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will redisplay the <see cref="View"/>.
  1582. /// </summary>
  1583. /// <value>The text alignment.</value>
  1584. public virtual TextAlignment TextAlignment {
  1585. get => textFormatter.Alignment;
  1586. set {
  1587. textFormatter.Alignment = value;
  1588. SetNeedsDisplay ();
  1589. }
  1590. }
  1591. /// <summary>
  1592. /// Get or sets if the <see cref="View"/> was already initialized.
  1593. /// This derived from <see cref="ISupportInitializeNotification"/> to allow notify all the views that are being initialized.
  1594. /// </summary>
  1595. public bool IsInitialized { get; set; }
  1596. /// <summary>
  1597. /// Pretty prints the View
  1598. /// </summary>
  1599. /// <returns></returns>
  1600. public override string ToString ()
  1601. {
  1602. return $"{GetType ().Name}({Id})({Frame})";
  1603. }
  1604. /// <summary>
  1605. /// Specifies the event arguments for <see cref="MouseEvent"/>
  1606. /// </summary>
  1607. public class MouseEventArgs : EventArgs {
  1608. /// <summary>
  1609. /// Constructs.
  1610. /// </summary>
  1611. /// <param name="me"></param>
  1612. public MouseEventArgs (MouseEvent me) => MouseEvent = me;
  1613. /// <summary>
  1614. /// The <see cref="MouseEvent"/> for the event.
  1615. /// </summary>
  1616. public MouseEvent MouseEvent { get; set; }
  1617. /// <summary>
  1618. /// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
  1619. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1620. /// </summary>
  1621. public bool Handled { get; set; }
  1622. }
  1623. /// <inheritdoc/>
  1624. public override bool OnMouseEnter (MouseEvent mouseEvent)
  1625. {
  1626. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  1627. MouseEnter?.Invoke (args);
  1628. if (args.Handled)
  1629. return true;
  1630. if (base.OnMouseEnter (mouseEvent))
  1631. return true;
  1632. return false;
  1633. }
  1634. /// <inheritdoc/>
  1635. public override bool OnMouseLeave (MouseEvent mouseEvent)
  1636. {
  1637. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  1638. MouseLeave?.Invoke (args);
  1639. if (args.Handled)
  1640. return true;
  1641. if (base.OnMouseLeave (mouseEvent))
  1642. return true;
  1643. return false;
  1644. }
  1645. /// <summary>
  1646. /// Method invoked when a mouse event is generated
  1647. /// </summary>
  1648. /// <param name="mouseEvent"></param>
  1649. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  1650. public virtual bool OnMouseEvent (MouseEvent mouseEvent)
  1651. {
  1652. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  1653. MouseClick?.Invoke (args);
  1654. if (args.Handled)
  1655. return true;
  1656. if (MouseEvent (mouseEvent))
  1657. return true;
  1658. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  1659. if (!HasFocus && SuperView != null) {
  1660. SuperView.SetFocus (this);
  1661. SetNeedsDisplay ();
  1662. }
  1663. return true;
  1664. }
  1665. return false;
  1666. }
  1667. /// <inheritdoc/>
  1668. protected override void Dispose (bool disposing)
  1669. {
  1670. for (int i = InternalSubviews.Count - 1; i >= 0; i--) {
  1671. View subview = InternalSubviews [i];
  1672. Remove (subview);
  1673. subview.Dispose ();
  1674. }
  1675. base.Dispose (disposing);
  1676. }
  1677. /// <summary>
  1678. /// This derived from <see cref="ISupportInitializeNotification"/> to allow notify all the views that are beginning initialized.
  1679. /// </summary>
  1680. public void BeginInit ()
  1681. {
  1682. if (!IsInitialized) {
  1683. Initialized?.Invoke (this, new EventArgs ());
  1684. }
  1685. if (subviews?.Count > 0) {
  1686. foreach (var view in subviews) {
  1687. if (!view.IsInitialized) {
  1688. view.BeginInit ();
  1689. }
  1690. }
  1691. }
  1692. }
  1693. /// <summary>
  1694. /// This derived from <see cref="ISupportInitializeNotification"/> to allow notify all the views that are ending initialized.
  1695. /// </summary>
  1696. public void EndInit ()
  1697. {
  1698. IsInitialized = true;
  1699. if (subviews?.Count > 0) {
  1700. foreach (var view in subviews) {
  1701. if (!view.IsInitialized) {
  1702. view.EndInit ();
  1703. }
  1704. }
  1705. }
  1706. }
  1707. }
  1708. }