View.cs 59 KB

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