View.cs 59 KB

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