View.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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.Linq;
  17. using NStack;
  18. namespace Terminal.Gui {
  19. /// <summary>
  20. /// Determines the LayoutStyle for a view, if Absolute, during LayoutSubviews, the
  21. /// value from the Frame will be used, if the value is Computer, then the Frame
  22. /// will be updated from the X, Y Pos objects and the Width and Height Dim objects.
  23. /// </summary>
  24. public enum LayoutStyle {
  25. /// <summary>
  26. /// The position and size of the view are based on the Frame value.
  27. /// </summary>
  28. Absolute,
  29. /// <summary>
  30. /// The position and size of the view will be computed based on the
  31. /// X, Y, Width and Height properties and set on the Frame.
  32. /// </summary>
  33. Computed
  34. }
  35. /// <summary>
  36. /// 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.
  37. /// </summary>
  38. /// <remarks>
  39. /// <para>
  40. /// The View defines the base functionality for user interface elements in Terminal/gui.cs. Views
  41. /// can contain one or more subviews, can respond to user input and render themselves on the screen.
  42. /// </para>
  43. /// <para>
  44. /// Views can either be created with an absolute position, by calling the constructor that takes a
  45. /// Rect parameter to specify the absolute position and size (the Frame of the View) or by setting the
  46. /// X, Y, Width and Height properties on the view. Both approaches use coordinates that are relative
  47. /// to the container they are being added to.
  48. /// </para>
  49. /// <para>
  50. /// When you do not specify a Rect frame you can use the more flexible
  51. /// Dim and Pos objects that can dynamically update the position of a view.
  52. /// The X and Y properties are of type <see cref="Pos"/>
  53. /// and you can use either absolute positions, percentages or anchor
  54. /// points. The Width and Height properties are of type
  55. /// <see cref="Dim"/> and can use absolute position,
  56. /// percentages and anchors. These are useful as they will take
  57. /// care of repositioning your views if your view's frames are resized
  58. /// or if the terminal size changes.
  59. /// </para>
  60. /// <para>
  61. /// When you specify the Rect parameter to a view, you are setting the LayoutStyle to Absolute, and the
  62. /// view will always stay in the position that you placed it. To change the position change the
  63. /// Frame property to the new position.
  64. /// </para>
  65. /// <para>
  66. /// Subviews can be added to a View by calling the Add method. The container of a view is the
  67. /// Superview.
  68. /// </para>
  69. /// <para>
  70. /// Developers can call the SetNeedsDisplay method on the view to flag a region or the entire view
  71. /// as requiring to be redrawn.
  72. /// </para>
  73. /// <para>
  74. /// Views have a ColorScheme property that defines the default colors that subviews
  75. /// should use for rendering. This ensures that the views fit in the context where
  76. /// they are being used, and allows for themes to be plugged in. For example, the
  77. /// default colors for windows and toplevels uses a blue background, while it uses
  78. /// a white background for dialog boxes and a red background for errors.
  79. /// </para>
  80. /// <para>
  81. /// If a ColorScheme is not set on a view, the result of the ColorScheme is the
  82. /// value of the SuperView and the value might only be valid once a view has been
  83. /// added to a SuperView, so your subclasses should not rely on ColorScheme being
  84. /// set at construction time.
  85. /// </para>
  86. /// <para>
  87. /// Using ColorSchemes has the advantage that your application will work both
  88. /// in color as well as black and white displays.
  89. /// </para>
  90. /// <para>
  91. /// Views that are focusable should implement the PositionCursor to make sure that
  92. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  93. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  94. /// the last focused view. So views should make sure that they place the cursor
  95. /// in a visually sensible place.
  96. /// </para>
  97. /// <para>
  98. /// The metnod LayoutSubviews is invoked when the size or layout of a view has
  99. /// changed. The default processing system will keep the size and dimensions
  100. /// for views that use the LayoutKind.Absolute, and will recompute the
  101. /// frames for the vies that use LayoutKind.Computed.
  102. /// </para>
  103. /// </remarks>
  104. public class View : Responder, IEnumerable {
  105. internal enum Direction {
  106. Forward,
  107. Backward
  108. }
  109. View container = null;
  110. View focused = null;
  111. Direction focusDirection;
  112. /// <summary>
  113. /// Event fired when the view get focus.
  114. /// </summary>
  115. public event EventHandler<FocusEventArgs> Enter;
  116. /// <summary>
  117. /// Event fired when the view lost focus.
  118. /// </summary>
  119. public event EventHandler<FocusEventArgs> Leave;
  120. /// <summary>
  121. /// Event fired when the view receives the mouse event for the first time.
  122. /// </summary>
  123. public event EventHandler<MouseEventEventArgs> MouseEnter;
  124. /// <summary>
  125. /// Event fired when the view loses mouse event for the last time.
  126. /// </summary>
  127. public event EventHandler<MouseEventEventArgs> MouseLeave;
  128. /// <summary>
  129. /// Event fired when a mouse event is generated.
  130. /// </summary>
  131. public event EventHandler<MouseEventEventArgs> MouseClick;
  132. internal Direction FocusDirection {
  133. get => SuperView?.FocusDirection ?? focusDirection;
  134. set {
  135. if (SuperView != null)
  136. SuperView.FocusDirection = value;
  137. else
  138. focusDirection = value;
  139. }
  140. }
  141. /// <summary>
  142. /// Points to the current driver in use by the view, it is a convenience property
  143. /// for simplifying the development of new views.
  144. /// </summary>
  145. public static ConsoleDriver Driver { get { return Application.Driver; } }
  146. static IList<View> empty = new List<View> (0).AsReadOnly ();
  147. // This is null, and allocated on demand.
  148. List<View> subviews;
  149. /// <summary>
  150. /// This returns a list of the subviews contained by this view.
  151. /// </summary>
  152. /// <value>The subviews.</value>
  153. public IList<View> Subviews => subviews == null ? empty : subviews.AsReadOnly ();
  154. // Internally, we use InternalSubviews rather than subviews, as we do not expect us
  155. // to make the same mistakes our users make when they poke at the Subviews.
  156. internal IList<View> InternalSubviews => subviews ?? empty;
  157. internal Rect NeedDisplay { get; private set; } = Rect.Empty;
  158. // The frame for the object
  159. Rect frame;
  160. /// <summary>
  161. /// Gets or sets an identifier for the view;
  162. /// </summary>
  163. /// <value>The identifier.</value>
  164. public ustring Id { get; set; } = "";
  165. /// <summary>
  166. /// Returns a value indicating if this View is currently on Top (Active)
  167. /// </summary>
  168. public bool IsCurrentTop {
  169. get {
  170. return Application.Current == this;
  171. }
  172. }
  173. /// <summary>
  174. /// Gets or sets a value indicating whether this <see cref="View"/> want mouse position reports.
  175. /// </summary>
  176. /// <value><c>true</c> if want mouse position reports; otherwise, <c>false</c>.</value>
  177. public virtual bool WantMousePositionReports { get; set; } = false;
  178. /// <summary>
  179. /// Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.
  180. /// </summary>
  181. public virtual bool WantContinuousButtonPressed { get; set; } = false;
  182. /// <summary>
  183. /// Gets or sets the frame for the view.
  184. /// </summary>
  185. /// <value>The frame.</value>
  186. /// <remarks>
  187. /// Altering the Frame of a view will trigger the redrawing of the
  188. /// view as well as the redrawing of the affected regions in the superview.
  189. /// </remarks>
  190. public virtual Rect Frame {
  191. get => frame;
  192. set {
  193. if (SuperView != null) {
  194. SuperView.SetNeedsDisplay (frame);
  195. SuperView.SetNeedsDisplay (value);
  196. }
  197. frame = value;
  198. SetNeedsLayout ();
  199. SetNeedsDisplay (frame);
  200. }
  201. }
  202. /// <summary>
  203. /// Gets an enumerator that enumerates the subviews in this view.
  204. /// </summary>
  205. /// <returns>The enumerator.</returns>
  206. public IEnumerator GetEnumerator ()
  207. {
  208. foreach (var v in InternalSubviews)
  209. yield return v;
  210. }
  211. LayoutStyle layoutStyle;
  212. /// <summary>
  213. /// Controls how the view's Frame is computed during the LayoutSubviews method, if Absolute, then
  214. /// LayoutSubviews does not change the Frame properties, otherwise the Frame is updated from the
  215. /// values in X, Y, Width and Height properties.
  216. /// </summary>
  217. /// <value>The layout style.</value>
  218. public LayoutStyle LayoutStyle {
  219. get => layoutStyle;
  220. set {
  221. layoutStyle = value;
  222. SetNeedsLayout ();
  223. }
  224. }
  225. /// <summary>
  226. /// The bounds represent the View-relative rectangle used for this view. Updates to the Bounds update the Frame, and has the same side effects as updating the frame.
  227. /// </summary>
  228. /// <value>The bounds.</value>
  229. public Rect Bounds {
  230. get => new Rect (Point.Empty, Frame.Size);
  231. set {
  232. Frame = new Rect (frame.Location, value.Size);
  233. }
  234. }
  235. Pos x, y;
  236. /// <summary>
  237. /// Gets or sets the X position for the view (the column). This is only used when the LayoutStyle is Computed, if the
  238. /// LayoutStyle is set to Absolute, this value is ignored.
  239. /// </summary>
  240. /// <value>The X Position.</value>
  241. public Pos X {
  242. get => x;
  243. set {
  244. x = value;
  245. SetNeedsLayout ();
  246. }
  247. }
  248. /// <summary>
  249. /// Gets or sets the Y position for the view (line). This is only used when the LayoutStyle is Computed, if the
  250. /// LayoutStyle is set to Absolute, this value is ignored.
  251. /// </summary>
  252. /// <value>The y position (line).</value>
  253. public Pos Y {
  254. get => y;
  255. set {
  256. y = value;
  257. SetNeedsLayout ();
  258. }
  259. }
  260. Dim width, height;
  261. /// <summary>
  262. /// Gets or sets the width for the view. This is only used when the LayoutStyle is Computed, if the
  263. /// LayoutStyle is set to Absolute, this value is ignored.
  264. /// </summary>
  265. /// <value>The width.</value>
  266. public Dim Width {
  267. get => width;
  268. set {
  269. width = value;
  270. SetNeedsLayout ();
  271. }
  272. }
  273. /// <summary>
  274. /// Gets or sets the height for the view. This is only used when the LayoutStyle is Computed, if the
  275. /// LayoutStyle is set to Absolute, this value is ignored.
  276. /// </summary>
  277. /// <value>The height.</value>
  278. public Dim Height {
  279. get => height;
  280. set {
  281. height = value;
  282. SetNeedsLayout ();
  283. }
  284. }
  285. /// <summary>
  286. /// Returns the container for this view, or null if this view has not been added to a container.
  287. /// </summary>
  288. /// <value>The super view.</value>
  289. public View SuperView => container;
  290. /// <summary>
  291. /// Initializes a new instance of the <see cref="View"/> class with the absolute
  292. /// dimensions specified in the frame. If you want to have Views that can be positioned with
  293. /// Pos and Dim properties on X, Y, Width and Height, use the empty constructor.
  294. /// </summary>
  295. /// <param name="frame">The region covered by this view.</param>
  296. public View (Rect frame)
  297. {
  298. this.Frame = frame;
  299. CanFocus = false;
  300. LayoutStyle = LayoutStyle.Absolute;
  301. }
  302. /// <summary>
  303. /// Initializes a new instance of the <see cref="View"/> class and sets the
  304. /// view up for Computed layout, which will use the values in X, Y, Width and Height to
  305. /// compute the View's Frame.
  306. /// </summary>
  307. public View ()
  308. {
  309. CanFocus = false;
  310. LayoutStyle = LayoutStyle.Computed;
  311. }
  312. /// <summary>
  313. /// Invoke to flag that this view needs to be redisplayed, by any code
  314. /// that alters the state of the view.
  315. /// </summary>
  316. public void SetNeedsDisplay ()
  317. {
  318. SetNeedsDisplay (Bounds);
  319. }
  320. internal bool layoutNeeded = true;
  321. internal void SetNeedsLayout ()
  322. {
  323. if (layoutNeeded)
  324. return;
  325. layoutNeeded = true;
  326. if (SuperView == null)
  327. return;
  328. SuperView.SetNeedsLayout ();
  329. }
  330. /// <summary>
  331. /// Flags the specified rectangle region on this view as needing to be repainted.
  332. /// </summary>
  333. /// <param name="region">The region that must be flagged for repaint.</param>
  334. public void SetNeedsDisplay (Rect region)
  335. {
  336. if (NeedDisplay == null || NeedDisplay.IsEmpty)
  337. NeedDisplay = region;
  338. else {
  339. var x = Math.Min (NeedDisplay.X, region.X);
  340. var y = Math.Min (NeedDisplay.Y, region.Y);
  341. var w = Math.Max (NeedDisplay.Width, region.Width);
  342. var h = Math.Max (NeedDisplay.Height, region.Height);
  343. NeedDisplay = new Rect (x, y, w, h);
  344. }
  345. if (container != null)
  346. container.ChildNeedsDisplay ();
  347. if (subviews == null)
  348. return;
  349. foreach (var view in subviews)
  350. if (view.Frame.IntersectsWith (region)) {
  351. var childRegion = Rect.Intersect (view.Frame, region);
  352. childRegion.X -= view.Frame.X;
  353. childRegion.Y -= view.Frame.Y;
  354. view.SetNeedsDisplay (childRegion);
  355. }
  356. }
  357. internal bool childNeedsDisplay;
  358. /// <summary>
  359. /// Flags this view for requiring the children views to be repainted.
  360. /// </summary>
  361. public void ChildNeedsDisplay ()
  362. {
  363. childNeedsDisplay = true;
  364. if (container != null)
  365. container.ChildNeedsDisplay ();
  366. }
  367. /// <summary>
  368. /// Adds a subview to this view.
  369. /// </summary>
  370. /// <remarks>
  371. /// </remarks>
  372. public virtual void Add (View view)
  373. {
  374. if (view == null)
  375. return;
  376. if (subviews == null)
  377. subviews = new List<View> ();
  378. subviews.Add (view);
  379. view.container = this;
  380. if (view.CanFocus)
  381. CanFocus = true;
  382. SetNeedsLayout ();
  383. SetNeedsDisplay ();
  384. }
  385. /// <summary>
  386. /// Adds the specified views to the view.
  387. /// </summary>
  388. /// <param name="views">Array of one or more views (can be optional parameter).</param>
  389. public void Add (params View [] views)
  390. {
  391. if (views == null)
  392. return;
  393. foreach (var view in views)
  394. Add (view);
  395. }
  396. /// <summary>
  397. /// Removes all the widgets from this container.
  398. /// </summary>
  399. /// <remarks>
  400. /// </remarks>
  401. public virtual void RemoveAll ()
  402. {
  403. if (subviews == null)
  404. return;
  405. while (subviews.Count > 0) {
  406. Remove (subviews [0]);
  407. }
  408. }
  409. /// <summary>
  410. /// Removes a widget from this container.
  411. /// </summary>
  412. /// <remarks>
  413. /// </remarks>
  414. public virtual void Remove (View view)
  415. {
  416. if (view == null || subviews == null)
  417. return;
  418. SetNeedsLayout ();
  419. SetNeedsDisplay ();
  420. var touched = view.Frame;
  421. subviews.Remove (view);
  422. view.container = null;
  423. if (subviews.Count < 1)
  424. this.CanFocus = false;
  425. foreach (var v in subviews) {
  426. if (v.Frame.IntersectsWith (touched))
  427. view.SetNeedsDisplay ();
  428. }
  429. }
  430. void PerformActionForSubview (View subview, Action<View> action)
  431. {
  432. if (subviews.Contains (subview)) {
  433. action (subview);
  434. }
  435. SetNeedsDisplay ();
  436. subview.SetNeedsDisplay ();
  437. }
  438. /// <summary>
  439. /// Brings the specified subview to the front so it is drawn on top of any other views.
  440. /// </summary>
  441. /// <param name="subview">The subview to send to the front</param>
  442. /// <remarks>
  443. /// <seealso cref="SendSubviewToBack"/>.
  444. /// </remarks>
  445. public void BringSubviewToFront (View subview)
  446. {
  447. PerformActionForSubview (subview, x => {
  448. subviews.Remove (x);
  449. subviews.Add (x);
  450. });
  451. }
  452. /// <summary>
  453. /// Sends the specified subview to the front so it is the first view drawn
  454. /// </summary>
  455. /// <param name="subview">The subview to send to the front</param>
  456. /// <remarks>
  457. /// <seealso cref="BringSubviewToFront(View)"/>.
  458. /// </remarks>
  459. public void SendSubviewToBack (View subview)
  460. {
  461. PerformActionForSubview (subview, x => {
  462. subviews.Remove (x);
  463. subviews.Insert (0, subview);
  464. });
  465. }
  466. /// <summary>
  467. /// Moves the subview backwards in the hierarchy, only one step
  468. /// </summary>
  469. /// <param name="subview">The subview to send backwards</param>
  470. /// <remarks>
  471. /// If you want to send the view all the way to the back use SendSubviewToBack.
  472. /// </remarks>
  473. public void SendSubviewBackwards (View subview)
  474. {
  475. PerformActionForSubview (subview, x => {
  476. var idx = subviews.IndexOf (x);
  477. if (idx > 0) {
  478. subviews.Remove (x);
  479. subviews.Insert (idx - 1, x);
  480. }
  481. });
  482. }
  483. /// <summary>
  484. /// Moves the subview backwards in the hierarchy, only one step
  485. /// </summary>
  486. /// <param name="subview">The subview to send backwards</param>
  487. /// <remarks>
  488. /// If you want to send the view all the way to the back use SendSubviewToBack.
  489. /// </remarks>
  490. public void BringSubviewForward (View subview)
  491. {
  492. PerformActionForSubview (subview, x => {
  493. var idx = subviews.IndexOf (x);
  494. if (idx + 1 < subviews.Count) {
  495. subviews.Remove (x);
  496. subviews.Insert (idx + 1, x);
  497. }
  498. });
  499. }
  500. /// <summary>
  501. /// Clears the view region with the current color.
  502. /// </summary>
  503. /// <remarks>
  504. /// <para>
  505. /// This clears the entire region used by this view.
  506. /// </para>
  507. /// </remarks>
  508. public void Clear ()
  509. {
  510. var h = Frame.Height;
  511. var w = Frame.Width;
  512. for (int line = 0; line < h; line++) {
  513. Move (0, line);
  514. for (int col = 0; col < w; col++)
  515. Driver.AddRune (' ');
  516. }
  517. }
  518. /// <summary>
  519. /// Clears the specified rectangular region with the current color
  520. /// </summary>
  521. public void Clear (Rect r)
  522. {
  523. var h = r.Height;
  524. var w = r.Width;
  525. for (int line = r.Y; line < r.Y + h; line++) {
  526. Driver.Move (r.X, line);
  527. for (int col = 0; col < w; col++)
  528. Driver.AddRune (' ');
  529. }
  530. }
  531. /// <summary>
  532. /// Converts the (col,row) position from the view into a screen (col,row). The values are clamped to (0..ScreenDim-1)
  533. /// </summary>
  534. /// <param name="col">View-based column.</param>
  535. /// <param name="row">View-based row.</param>
  536. /// <param name="rcol">Absolute column, display relative.</param>
  537. /// <param name="rrow">Absolute row, display relative.</param>
  538. /// <param name="clipped">Whether to clip the result of the ViewToScreen method, if set to true, the rcol, rrow values are clamped to the screen dimensions.</param>
  539. internal void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  540. {
  541. // Computes the real row, col relative to the screen.
  542. rrow = row + frame.Y;
  543. rcol = col + frame.X;
  544. var ccontainer = container;
  545. while (ccontainer != null) {
  546. rrow += ccontainer.frame.Y;
  547. rcol += ccontainer.frame.X;
  548. ccontainer = ccontainer.container;
  549. }
  550. // The following ensures that the cursor is always in the screen boundaries.
  551. if (clipped) {
  552. rrow = Math.Max (0, Math.Min (rrow, Driver.Rows - 1));
  553. rcol = Math.Max (0, Math.Min (rcol, Driver.Cols - 1));
  554. }
  555. }
  556. /// <summary>
  557. /// Converts a point from screen coordinates into the view coordinate space.
  558. /// </summary>
  559. /// <returns>The mapped point.</returns>
  560. /// <param name="x">X screen-coordinate point.</param>
  561. /// <param name="y">Y screen-coordinate point.</param>
  562. public Point ScreenToView (int x, int y)
  563. {
  564. if (SuperView == null) {
  565. return new Point (x - Frame.X, y - frame.Y);
  566. } else {
  567. var parent = SuperView.ScreenToView (x, y);
  568. return new Point (parent.X - frame.X, parent.Y - frame.Y);
  569. }
  570. }
  571. // Converts a rectangle in view coordinates to screen coordinates.
  572. internal Rect RectToScreen (Rect rect)
  573. {
  574. ViewToScreen (rect.X, rect.Y, out var x, out var y, clipped: false);
  575. return new Rect (x, y, rect.Width, rect.Height);
  576. }
  577. // Clips a rectangle in screen coordinates to the dimensions currently available on the screen
  578. internal Rect ScreenClip (Rect rect)
  579. {
  580. var x = rect.X < 0 ? 0 : rect.X;
  581. var y = rect.Y < 0 ? 0 : rect.Y;
  582. var w = rect.X + rect.Width >= Driver.Cols ? Driver.Cols - rect.X : rect.Width;
  583. var h = rect.Y + rect.Height >= Driver.Rows ? Driver.Rows - rect.Y : rect.Height;
  584. return new Rect (x, y, w, h);
  585. }
  586. /// <summary>
  587. /// Sets the Console driver's clip region to the current View's Bounds.
  588. /// </summary>
  589. /// <returns>The existing driver's Clip region, which can be then set by setting the Driver.Clip property.</returns>
  590. public Rect ClipToBounds ()
  591. {
  592. return SetClip (Bounds);
  593. }
  594. /// <summary>
  595. /// Sets the clipping region to the specified region, the region is view-relative
  596. /// </summary>
  597. /// <returns>The previous clip region.</returns>
  598. /// <param name="rect">Rectangle region to clip into, the region is view-relative.</param>
  599. public Rect SetClip (Rect rect)
  600. {
  601. var bscreen = RectToScreen (rect);
  602. var previous = Driver.Clip;
  603. Driver.Clip = ScreenClip (RectToScreen (Bounds));
  604. return previous;
  605. }
  606. /// <summary>
  607. /// Draws a frame in the current view, clipped by the boundary of this view
  608. /// </summary>
  609. /// <param name="rect">Rectangular region for the frame to be drawn.</param>
  610. /// <param name="padding">The padding to add to the drawn frame.</param>
  611. /// <param name="fill">If set to <c>true</c> it fill will the contents.</param>
  612. public void DrawFrame (Rect rect, int padding = 0, bool fill = false)
  613. {
  614. var scrRect = RectToScreen (rect);
  615. var savedClip = Driver.Clip;
  616. Driver.Clip = ScreenClip (RectToScreen (Bounds));
  617. Driver.DrawFrame (scrRect, padding, fill);
  618. Driver.Clip = savedClip;
  619. }
  620. /// <summary>
  621. /// Utility function to draw strings that contain a hotkey
  622. /// </summary>
  623. /// <param name="text">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
  624. /// <param name="hotColor">Hot color.</param>
  625. /// <param name="normalColor">Normal color.</param>
  626. public void DrawHotString (ustring text, Attribute hotColor, Attribute normalColor)
  627. {
  628. Driver.SetAttribute (normalColor);
  629. foreach (var rune in text) {
  630. if (rune == '_') {
  631. Driver.SetAttribute (hotColor);
  632. continue;
  633. }
  634. Driver.AddRune (rune);
  635. Driver.SetAttribute (normalColor);
  636. }
  637. }
  638. /// <summary>
  639. /// Utility function to draw strings that contains a hotkey using a colorscheme and the "focused" state.
  640. /// </summary>
  641. /// <param name="text">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
  642. /// <param name="focused">If set to <c>true</c> this uses the focused colors from the color scheme, otherwise the regular ones.</param>
  643. /// <param name="scheme">The color scheme to use.</param>
  644. public void DrawHotString (ustring text, bool focused, ColorScheme scheme)
  645. {
  646. if (focused)
  647. DrawHotString (text, scheme.HotFocus, scheme.Focus);
  648. else
  649. DrawHotString (text, scheme.HotNormal, scheme.Normal);
  650. }
  651. /// <summary>
  652. /// This moves the cursor to the specified column and row in the view.
  653. /// </summary>
  654. /// <returns>The move.</returns>
  655. /// <param name="col">Col.</param>
  656. /// <param name="row">Row.</param>
  657. public void Move (int col, int row)
  658. {
  659. ViewToScreen (col, row, out var rcol, out var rrow);
  660. Driver.Move (rcol, rrow);
  661. }
  662. /// <summary>
  663. /// Positions the cursor in the right position based on the currently focused view in the chain.
  664. /// </summary>
  665. public virtual void PositionCursor ()
  666. {
  667. if (focused != null)
  668. focused.PositionCursor ();
  669. else
  670. Move (frame.X, frame.Y);
  671. }
  672. /// <inheritdoc cref="HasFocus"/>
  673. public override bool HasFocus {
  674. get {
  675. return base.HasFocus;
  676. }
  677. internal set {
  678. if (base.HasFocus != value)
  679. if (value)
  680. OnEnter ();
  681. else
  682. OnLeave ();
  683. SetNeedsDisplay ();
  684. base.HasFocus = value;
  685. // Remove focus down the chain of subviews if focus is removed
  686. if (!value && focused != null) {
  687. focused.OnLeave ();
  688. focused.HasFocus = false;
  689. focused = null;
  690. }
  691. }
  692. }
  693. /// <summary>
  694. /// Specifies the event arguments for <see cref="SetFocus(View)"/>
  695. /// </summary>
  696. public class FocusEventArgs : EventArgs {
  697. /// <summary>
  698. /// Constructs.
  699. /// </summary>
  700. public FocusEventArgs () { }
  701. /// <summary>
  702. /// Indicates if the current focus event has already been processed and the driver should stop notifying any other event subscriber.
  703. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  704. /// </summary>
  705. public bool Handled { get; set; }
  706. }
  707. /// <inheritdoc cref="OnEnter"/>
  708. public override bool OnEnter ()
  709. {
  710. FocusEventArgs args = new FocusEventArgs ();
  711. Enter?.Invoke (this, args);
  712. if (args.Handled)
  713. return true;
  714. if (base.OnEnter ())
  715. return true;
  716. return false;
  717. }
  718. /// <inheritdoc cref="OnLeave"/>
  719. public override bool OnLeave ()
  720. {
  721. FocusEventArgs args = new FocusEventArgs ();
  722. Leave?.Invoke (this, args);
  723. if (args.Handled)
  724. return true;
  725. if (base.OnLeave ())
  726. return true;
  727. return false;
  728. }
  729. /// <summary>
  730. /// Returns the currently focused view inside this view, or null if nothing is focused.
  731. /// </summary>
  732. /// <value>The focused.</value>
  733. public View Focused => focused;
  734. /// <summary>
  735. /// Returns the most focused view in the chain of subviews (the leaf view that has the focus).
  736. /// </summary>
  737. /// <value>The most focused.</value>
  738. public View MostFocused {
  739. get {
  740. if (Focused == null)
  741. return null;
  742. var most = Focused.MostFocused;
  743. if (most != null)
  744. return most;
  745. return Focused;
  746. }
  747. }
  748. /// <summary>
  749. /// The color scheme for this view, if it is not defined, it returns the parent's
  750. /// color scheme.
  751. /// </summary>
  752. public ColorScheme ColorScheme {
  753. get {
  754. if (colorScheme == null)
  755. return SuperView?.ColorScheme;
  756. return colorScheme;
  757. }
  758. set {
  759. colorScheme = value;
  760. }
  761. }
  762. ColorScheme colorScheme;
  763. /// <summary>
  764. /// Displays the specified character in the specified column and row.
  765. /// </summary>
  766. /// <param name="col">Col.</param>
  767. /// <param name="row">Row.</param>
  768. /// <param name="ch">Ch.</param>
  769. public void AddRune (int col, int row, Rune ch)
  770. {
  771. if (row < 0 || col < 0)
  772. return;
  773. if (row > frame.Height - 1 || col > frame.Width - 1)
  774. return;
  775. Move (col, row);
  776. Driver.AddRune (ch);
  777. }
  778. /// <summary>
  779. /// Removes the SetNeedsDisplay and the ChildNeedsDisplay setting on this view.
  780. /// </summary>
  781. protected void ClearNeedsDisplay ()
  782. {
  783. NeedDisplay = Rect.Empty;
  784. childNeedsDisplay = false;
  785. }
  786. /// <summary>
  787. /// Performs a redraw of this view and its subviews, only redraws the views that have been flagged for a re-display.
  788. /// </summary>
  789. /// <param name="region">The region to redraw, this is relative to the view itself.</param>
  790. /// <remarks>
  791. /// <para>
  792. /// Views should set the color that they want to use on entry, as otherwise this will inherit
  793. /// the last color that was set globaly on the driver.
  794. /// </para>
  795. /// </remarks>
  796. public virtual void Redraw (Rect region)
  797. {
  798. var clipRect = new Rect (Point.Empty, frame.Size);
  799. if (subviews != null) {
  800. foreach (var view in subviews) {
  801. if (view.NeedDisplay != null && (!view.NeedDisplay.IsEmpty || view.childNeedsDisplay)) {
  802. if (view.Frame.IntersectsWith (clipRect) && view.Frame.IntersectsWith (region)) {
  803. // FIXED: optimize this by computing the intersection of region and view.Bounds
  804. if (view.layoutNeeded)
  805. view.LayoutSubviews ();
  806. Application.CurrentView = view;
  807. // Ensure we don't make the Driver's clip rect any bigger
  808. if (Driver.Clip.IsEmpty || Driver.Clip.Contains(RectToScreen (view.Bounds))) {
  809. var savedClip = ClipToBounds ();
  810. view.Redraw (view.Bounds);
  811. Driver.Clip = savedClip;
  812. } else {
  813. view.Redraw (view.Bounds);
  814. }
  815. }
  816. view.NeedDisplay = Rect.Empty;
  817. view.childNeedsDisplay = false;
  818. }
  819. }
  820. }
  821. ClearNeedsDisplay ();
  822. }
  823. /// <summary>
  824. /// Focuses the specified sub-view.
  825. /// </summary>
  826. /// <param name="view">View.</param>
  827. public void SetFocus (View view)
  828. {
  829. if (view == null)
  830. return;
  831. //Console.WriteLine ($"Request to focus {view}");
  832. if (!view.CanFocus)
  833. return;
  834. if (focused == view)
  835. return;
  836. // Make sure that this view is a subview
  837. View c;
  838. for (c = view.container; c != null; c = c.container)
  839. if (c == this)
  840. break;
  841. if (c == null)
  842. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  843. if (focused != null)
  844. focused.HasFocus = false;
  845. focused = view;
  846. focused.HasFocus = true;
  847. focused.EnsureFocus ();
  848. // Send focus upwards
  849. SuperView?.SetFocus (this);
  850. }
  851. /// <summary>
  852. /// Specifies the event arguments for <see cref="KeyEvent"/>
  853. /// </summary>
  854. public class KeyEventEventArgs : EventArgs {
  855. /// <summary>
  856. /// Constructs.
  857. /// </summary>
  858. /// <param name="ke"></param>
  859. public KeyEventEventArgs (KeyEvent ke) => KeyEvent = ke;
  860. /// <summary>
  861. /// The <see cref="KeyEvent"/> for the event.
  862. /// </summary>
  863. public KeyEvent KeyEvent { get; set; }
  864. /// <summary>
  865. /// Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber.
  866. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  867. /// </summary>
  868. public bool Handled { get; set; } = false;
  869. }
  870. /// <summary>
  871. /// Invoked when a character key is pressed and occurs after the key up event.
  872. /// </summary>
  873. public event EventHandler<KeyEventEventArgs> KeyPress;
  874. /// <inheritdoc cref="ProcessKey"/>
  875. public override bool ProcessKey (KeyEvent keyEvent)
  876. {
  877. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  878. KeyPress?.Invoke (this, args);
  879. if (args.Handled)
  880. return true;
  881. if (Focused?.ProcessKey (keyEvent) == true)
  882. return true;
  883. return false;
  884. }
  885. /// <inheritdoc cref="ProcessHotKey"/>
  886. public override bool ProcessHotKey (KeyEvent keyEvent)
  887. {
  888. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  889. KeyPress?.Invoke (this, args);
  890. if (args.Handled)
  891. return true;
  892. if (subviews == null || subviews.Count == 0)
  893. return false;
  894. foreach (var view in subviews)
  895. if (view.ProcessHotKey (keyEvent))
  896. return true;
  897. return false;
  898. }
  899. /// <inheritdoc cref="ProcessColdKey"/>
  900. public override bool ProcessColdKey (KeyEvent keyEvent)
  901. {
  902. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  903. KeyPress?.Invoke (this, args);
  904. if (args.Handled)
  905. return true;
  906. if (subviews == null || subviews.Count == 0)
  907. return false;
  908. foreach (var view in subviews)
  909. if (view.ProcessColdKey (keyEvent))
  910. return true;
  911. return false;
  912. }
  913. /// <summary>
  914. /// Invoked when a key is pressed
  915. /// </summary>
  916. public event EventHandler<KeyEventEventArgs> KeyDown;
  917. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  918. public override bool OnKeyDown (KeyEvent keyEvent)
  919. {
  920. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  921. KeyDown?.Invoke (this, args);
  922. if (args.Handled)
  923. return true;
  924. if (subviews == null || subviews.Count == 0)
  925. return false;
  926. foreach (var view in subviews)
  927. if (view.OnKeyDown (keyEvent))
  928. return true;
  929. return false;
  930. }
  931. /// <summary>
  932. /// Invoked when a key is released
  933. /// </summary>
  934. public event EventHandler<KeyEventEventArgs> KeyUp;
  935. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  936. public override bool OnKeyUp (KeyEvent keyEvent)
  937. {
  938. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  939. KeyUp?.Invoke (this, args);
  940. if (args.Handled)
  941. return true;
  942. if (subviews == null || subviews.Count == 0)
  943. return false;
  944. foreach (var view in subviews)
  945. if (view.OnKeyUp (keyEvent))
  946. return true;
  947. return false;
  948. }
  949. /// <summary>
  950. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
  951. /// </summary>
  952. public void EnsureFocus ()
  953. {
  954. if (focused == null)
  955. if (FocusDirection == Direction.Forward)
  956. FocusFirst ();
  957. else
  958. FocusLast ();
  959. }
  960. /// <summary>
  961. /// Focuses the first focusable subview if one exists.
  962. /// </summary>
  963. public void FocusFirst ()
  964. {
  965. if (subviews == null) {
  966. SuperView?.SetFocus (this);
  967. return;
  968. }
  969. foreach (var view in subviews) {
  970. if (view.CanFocus) {
  971. SetFocus (view);
  972. return;
  973. }
  974. }
  975. }
  976. /// <summary>
  977. /// Focuses the last focusable subview if one exists.
  978. /// </summary>
  979. public void FocusLast ()
  980. {
  981. if (subviews == null) {
  982. SuperView?.SetFocus (this);
  983. return;
  984. }
  985. for (int i = subviews.Count; i > 0;) {
  986. i--;
  987. View v = subviews [i];
  988. if (v.CanFocus) {
  989. SetFocus (v);
  990. return;
  991. }
  992. }
  993. }
  994. /// <summary>
  995. /// Focuses the previous view.
  996. /// </summary>
  997. /// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
  998. public bool FocusPrev ()
  999. {
  1000. FocusDirection = Direction.Backward;
  1001. if (subviews == null || subviews.Count == 0)
  1002. return false;
  1003. if (focused == null) {
  1004. FocusLast ();
  1005. return focused != null;
  1006. }
  1007. int focused_idx = -1;
  1008. for (int i = subviews.Count; i > 0;) {
  1009. i--;
  1010. View w = subviews [i];
  1011. if (w.HasFocus) {
  1012. if (w.FocusPrev ())
  1013. return true;
  1014. focused_idx = i;
  1015. continue;
  1016. }
  1017. if (w.CanFocus && focused_idx != -1) {
  1018. focused.HasFocus = false;
  1019. if (w != null && w.CanFocus)
  1020. w.FocusLast ();
  1021. SetFocus (w);
  1022. return true;
  1023. }
  1024. }
  1025. if (focused != null) {
  1026. focused.HasFocus = false;
  1027. focused = null;
  1028. }
  1029. return false;
  1030. }
  1031. /// <summary>
  1032. /// Focuses the next view.
  1033. /// </summary>
  1034. /// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
  1035. public bool FocusNext ()
  1036. {
  1037. FocusDirection = Direction.Forward;
  1038. if (subviews == null || subviews.Count == 0)
  1039. return false;
  1040. if (focused == null) {
  1041. FocusFirst ();
  1042. return focused != null;
  1043. }
  1044. int n = subviews.Count;
  1045. int focused_idx = -1;
  1046. for (int i = 0; i < n; i++) {
  1047. View w = subviews [i];
  1048. if (w.HasFocus) {
  1049. if (w.FocusNext ())
  1050. return true;
  1051. focused_idx = i;
  1052. continue;
  1053. }
  1054. if (w.CanFocus && focused_idx != -1) {
  1055. focused.HasFocus = false;
  1056. if (w != null && w.CanFocus)
  1057. w.FocusFirst ();
  1058. SetFocus (w);
  1059. return true;
  1060. }
  1061. }
  1062. if (focused != null) {
  1063. focused.HasFocus = false;
  1064. focused = null;
  1065. }
  1066. return false;
  1067. }
  1068. /// <summary>
  1069. /// Computes the RelativeLayout for the view, given the frame for its container.
  1070. /// </summary>
  1071. /// <param name="hostFrame">The Frame for the host.</param>
  1072. internal void RelativeLayout (Rect hostFrame)
  1073. {
  1074. int w, h, _x, _y;
  1075. if (x is Pos.PosCenter) {
  1076. if (width == null)
  1077. w = hostFrame.Width;
  1078. else
  1079. w = width.Anchor (hostFrame.Width);
  1080. _x = x.Anchor (hostFrame.Width - w);
  1081. } else {
  1082. if (x == null)
  1083. _x = 0;
  1084. else
  1085. _x = x.Anchor (hostFrame.Width);
  1086. if (width == null)
  1087. w = hostFrame.Width;
  1088. else
  1089. w = width.Anchor (hostFrame.Width - _x);
  1090. }
  1091. if (y is Pos.PosCenter) {
  1092. if (height == null)
  1093. h = hostFrame.Height;
  1094. else
  1095. h = height.Anchor (hostFrame.Height);
  1096. _y = y.Anchor (hostFrame.Height - h);
  1097. } else {
  1098. if (y == null)
  1099. _y = 0;
  1100. else
  1101. _y = y.Anchor (hostFrame.Height);
  1102. if (height == null)
  1103. h = hostFrame.Height;
  1104. else
  1105. h = height.Anchor (hostFrame.Height - _y);
  1106. }
  1107. Frame = new Rect (_x, _y, w, h);
  1108. // layoutNeeded = false;
  1109. }
  1110. // https://en.wikipedia.org/wiki/Topological_sorting
  1111. List<View> TopologicalSort (HashSet<View> nodes, HashSet<(View From, View To)> edges)
  1112. {
  1113. var result = new List<View> ();
  1114. // Set of all nodes with no incoming edges
  1115. var S = new HashSet<View> (nodes.Where (n => edges.All (e => e.To.Equals (n) == false)));
  1116. while (S.Any ()) {
  1117. // remove a node n from S
  1118. var n = S.First ();
  1119. S.Remove (n);
  1120. // add n to tail of L
  1121. if (n != this?.SuperView)
  1122. result.Add (n);
  1123. // for each node m with an edge e from n to m do
  1124. foreach (var e in edges.Where (e => e.From.Equals (n)).ToArray ()) {
  1125. var m = e.To;
  1126. // remove edge e from the graph
  1127. edges.Remove (e);
  1128. // if m has no other incoming edges then
  1129. if (edges.All (me => me.To.Equals (m) == false) && m != this?.SuperView) {
  1130. // insert m into S
  1131. S.Add (m);
  1132. }
  1133. }
  1134. }
  1135. // if graph has edges then
  1136. if (edges.Any ()) {
  1137. // return error (graph has at least one cycle)
  1138. return null;
  1139. } else {
  1140. // return L (a topologically sorted order)
  1141. return result;
  1142. }
  1143. }
  1144. /// <summary>
  1145. /// This virtual method is invoked when a view starts executing or
  1146. /// when the dimensions of the view have changed, for example in
  1147. /// response to the container view or terminal resizing.
  1148. /// </summary>
  1149. public virtual void LayoutSubviews ()
  1150. {
  1151. if (!layoutNeeded)
  1152. return;
  1153. // Sort out the dependencies of the X, Y, Width, Height properties
  1154. var nodes = new HashSet<View> ();
  1155. var edges = new HashSet<(View, View)> ();
  1156. foreach (var v in InternalSubviews) {
  1157. nodes.Add (v);
  1158. if (v.LayoutStyle == LayoutStyle.Computed) {
  1159. if (v.X is Pos.PosView vX)
  1160. edges.Add ((vX.Target, v));
  1161. if (v.Y is Pos.PosView vY)
  1162. edges.Add ((vY.Target, v));
  1163. if (v.Width is Dim.DimView vWidth)
  1164. edges.Add ((vWidth.Target, v));
  1165. if (v.Height is Dim.DimView vHeight)
  1166. edges.Add ((vHeight.Target, v));
  1167. }
  1168. }
  1169. var ordered = TopologicalSort (nodes, edges);
  1170. if (ordered == null)
  1171. throw new Exception ("There is a recursive cycle in the relative Pos/Dim in the views of " + this);
  1172. foreach (var v in ordered) {
  1173. if (v.LayoutStyle == LayoutStyle.Computed)
  1174. v.RelativeLayout (Frame);
  1175. v.LayoutSubviews ();
  1176. v.layoutNeeded = false;
  1177. }
  1178. if (SuperView == Application.Top && layoutNeeded && ordered.Count == 0 && LayoutStyle == LayoutStyle.Computed) {
  1179. RelativeLayout (Frame);
  1180. }
  1181. layoutNeeded = false;
  1182. }
  1183. /// <inheritdoc cref="ToString"/>
  1184. public override string ToString ()
  1185. {
  1186. return $"{GetType ().Name}({Id})({Frame})";
  1187. }
  1188. /// <summary>
  1189. /// Specifies the event arguments for <see cref="MouseEvent"/>
  1190. /// </summary>
  1191. public class MouseEventEventArgs : EventArgs {
  1192. /// <summary>
  1193. /// Constructs.
  1194. /// </summary>
  1195. /// <param name="me"></param>
  1196. public MouseEventEventArgs (MouseEvent me) => MouseEvent = me;
  1197. /// <summary>
  1198. /// The <see cref="MouseEvent"/> for the event.
  1199. /// </summary>
  1200. public MouseEvent MouseEvent { get; set; }
  1201. /// <summary>
  1202. /// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
  1203. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1204. /// </summary>
  1205. public bool Handled { get; set; }
  1206. }
  1207. /// <inheritdoc cref="OnMouseEnter(Gui.MouseEvent)"/>
  1208. public override bool OnMouseEnter (MouseEvent mouseEvent)
  1209. {
  1210. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  1211. MouseEnter?.Invoke (this, args);
  1212. if (args.Handled)
  1213. return true;
  1214. if (base.OnMouseEnter (mouseEvent))
  1215. return true;
  1216. return false;
  1217. }
  1218. /// <inheritdoc cref="OnMouseLeave(Gui.MouseEvent)"/>
  1219. public override bool OnMouseLeave (MouseEvent mouseEvent)
  1220. {
  1221. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  1222. MouseLeave?.Invoke (this, args);
  1223. if (args.Handled)
  1224. return true;
  1225. if (base.OnMouseLeave (mouseEvent))
  1226. return true;
  1227. return false;
  1228. }
  1229. /// <summary>
  1230. /// Method invoked when a mouse event is generated
  1231. /// </summary>
  1232. /// <param name="mouseEvent"></param>
  1233. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  1234. public virtual bool OnMouseEvent (MouseEvent mouseEvent)
  1235. {
  1236. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  1237. MouseClick?.Invoke (this, args);
  1238. if (args.Handled)
  1239. return true;
  1240. if (MouseEvent (mouseEvent))
  1241. return true;
  1242. return false;
  1243. }
  1244. }
  1245. }