View.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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. view.Redraw (view.Bounds);
  808. }
  809. view.NeedDisplay = Rect.Empty;
  810. view.childNeedsDisplay = false;
  811. }
  812. }
  813. }
  814. ClearNeedsDisplay ();
  815. }
  816. /// <summary>
  817. /// Focuses the specified sub-view.
  818. /// </summary>
  819. /// <param name="view">View.</param>
  820. public void SetFocus (View view)
  821. {
  822. if (view == null)
  823. return;
  824. //Console.WriteLine ($"Request to focus {view}");
  825. if (!view.CanFocus)
  826. return;
  827. if (focused == view)
  828. return;
  829. // Make sure that this view is a subview
  830. View c;
  831. for (c = view.container; c != null; c = c.container)
  832. if (c == this)
  833. break;
  834. if (c == null)
  835. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  836. if (focused != null)
  837. focused.HasFocus = false;
  838. focused = view;
  839. focused.HasFocus = true;
  840. focused.EnsureFocus ();
  841. // Send focus upwards
  842. SuperView?.SetFocus (this);
  843. }
  844. /// <summary>
  845. /// Specifies the event arguments for <see cref="KeyEvent"/>
  846. /// </summary>
  847. public class KeyEventEventArgs : EventArgs {
  848. /// <summary>
  849. /// Constructs.
  850. /// </summary>
  851. /// <param name="ke"></param>
  852. public KeyEventEventArgs (KeyEvent ke) => KeyEvent = ke;
  853. /// <summary>
  854. /// The <see cref="KeyEvent"/> for the event.
  855. /// </summary>
  856. public KeyEvent KeyEvent { get; set; }
  857. /// <summary>
  858. /// Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber.
  859. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  860. /// </summary>
  861. public bool Handled { get; set; } = false;
  862. }
  863. /// <summary>
  864. /// Invoked when a character key is pressed and occurs after the key up event.
  865. /// </summary>
  866. public event EventHandler<KeyEventEventArgs> KeyPress;
  867. /// <inheritdoc cref="ProcessKey"/>
  868. public override bool ProcessKey (KeyEvent keyEvent)
  869. {
  870. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  871. KeyPress?.Invoke (this, args);
  872. if (args.Handled)
  873. return true;
  874. if (Focused?.ProcessKey (keyEvent) == true)
  875. return true;
  876. return false;
  877. }
  878. /// <inheritdoc cref="ProcessHotKey"/>
  879. public override bool ProcessHotKey (KeyEvent keyEvent)
  880. {
  881. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  882. KeyPress?.Invoke (this, args);
  883. if (args.Handled)
  884. return true;
  885. if (subviews == null || subviews.Count == 0)
  886. return false;
  887. foreach (var view in subviews)
  888. if (view.ProcessHotKey (keyEvent))
  889. return true;
  890. return false;
  891. }
  892. /// <inheritdoc cref="ProcessColdKey"/>
  893. public override bool ProcessColdKey (KeyEvent keyEvent)
  894. {
  895. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  896. KeyPress?.Invoke (this, args);
  897. if (args.Handled)
  898. return true;
  899. if (subviews == null || subviews.Count == 0)
  900. return false;
  901. foreach (var view in subviews)
  902. if (view.ProcessColdKey (keyEvent))
  903. return true;
  904. return false;
  905. }
  906. /// <summary>
  907. /// Invoked when a key is pressed
  908. /// </summary>
  909. public event EventHandler<KeyEventEventArgs> KeyDown;
  910. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  911. public override bool OnKeyDown (KeyEvent keyEvent)
  912. {
  913. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  914. KeyDown?.Invoke (this, args);
  915. if (args.Handled)
  916. return true;
  917. if (subviews == null || subviews.Count == 0)
  918. return false;
  919. foreach (var view in subviews)
  920. if (view.OnKeyDown (keyEvent))
  921. return true;
  922. return false;
  923. }
  924. /// <summary>
  925. /// Invoked when a key is released
  926. /// </summary>
  927. public event EventHandler<KeyEventEventArgs> KeyUp;
  928. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  929. public override bool OnKeyUp (KeyEvent keyEvent)
  930. {
  931. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  932. KeyUp?.Invoke (this, args);
  933. if (args.Handled)
  934. return true;
  935. if (subviews == null || subviews.Count == 0)
  936. return false;
  937. foreach (var view in subviews)
  938. if (view.OnKeyUp (keyEvent))
  939. return true;
  940. return false;
  941. }
  942. /// <summary>
  943. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
  944. /// </summary>
  945. public void EnsureFocus ()
  946. {
  947. if (focused == null)
  948. if (FocusDirection == Direction.Forward)
  949. FocusFirst ();
  950. else
  951. FocusLast ();
  952. }
  953. /// <summary>
  954. /// Focuses the first focusable subview if one exists.
  955. /// </summary>
  956. public void FocusFirst ()
  957. {
  958. if (subviews == null) {
  959. SuperView?.SetFocus (this);
  960. return;
  961. }
  962. foreach (var view in subviews) {
  963. if (view.CanFocus) {
  964. SetFocus (view);
  965. return;
  966. }
  967. }
  968. }
  969. /// <summary>
  970. /// Focuses the last focusable subview if one exists.
  971. /// </summary>
  972. public void FocusLast ()
  973. {
  974. if (subviews == null) {
  975. SuperView?.SetFocus (this);
  976. return;
  977. }
  978. for (int i = subviews.Count; i > 0;) {
  979. i--;
  980. View v = subviews [i];
  981. if (v.CanFocus) {
  982. SetFocus (v);
  983. return;
  984. }
  985. }
  986. }
  987. /// <summary>
  988. /// Focuses the previous view.
  989. /// </summary>
  990. /// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
  991. public bool FocusPrev ()
  992. {
  993. FocusDirection = Direction.Backward;
  994. if (subviews == null || subviews.Count == 0)
  995. return false;
  996. if (focused == null) {
  997. FocusLast ();
  998. return focused != null;
  999. }
  1000. int focused_idx = -1;
  1001. for (int i = subviews.Count; i > 0;) {
  1002. i--;
  1003. View w = subviews [i];
  1004. if (w.HasFocus) {
  1005. if (w.FocusPrev ())
  1006. return true;
  1007. focused_idx = i;
  1008. continue;
  1009. }
  1010. if (w.CanFocus && focused_idx != -1) {
  1011. focused.HasFocus = false;
  1012. if (w != null && w.CanFocus)
  1013. w.FocusLast ();
  1014. SetFocus (w);
  1015. return true;
  1016. }
  1017. }
  1018. if (focused != null) {
  1019. focused.HasFocus = false;
  1020. focused = null;
  1021. }
  1022. return false;
  1023. }
  1024. /// <summary>
  1025. /// Focuses the next view.
  1026. /// </summary>
  1027. /// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
  1028. public bool FocusNext ()
  1029. {
  1030. FocusDirection = Direction.Forward;
  1031. if (subviews == null || subviews.Count == 0)
  1032. return false;
  1033. if (focused == null) {
  1034. FocusFirst ();
  1035. return focused != null;
  1036. }
  1037. int n = subviews.Count;
  1038. int focused_idx = -1;
  1039. for (int i = 0; i < n; i++) {
  1040. View w = subviews [i];
  1041. if (w.HasFocus) {
  1042. if (w.FocusNext ())
  1043. return true;
  1044. focused_idx = i;
  1045. continue;
  1046. }
  1047. if (w.CanFocus && focused_idx != -1) {
  1048. focused.HasFocus = false;
  1049. if (w != null && w.CanFocus)
  1050. w.FocusFirst ();
  1051. SetFocus (w);
  1052. return true;
  1053. }
  1054. }
  1055. if (focused != null) {
  1056. focused.HasFocus = false;
  1057. focused = null;
  1058. }
  1059. return false;
  1060. }
  1061. /// <summary>
  1062. /// Computes the RelativeLayout for the view, given the frame for its container.
  1063. /// </summary>
  1064. /// <param name="hostFrame">The Frame for the host.</param>
  1065. internal void RelativeLayout (Rect hostFrame)
  1066. {
  1067. int w, h, _x, _y;
  1068. if (x is Pos.PosCenter) {
  1069. if (width == null)
  1070. w = hostFrame.Width;
  1071. else
  1072. w = width.Anchor (hostFrame.Width);
  1073. _x = x.Anchor (hostFrame.Width - w);
  1074. } else {
  1075. if (x == null)
  1076. _x = 0;
  1077. else
  1078. _x = x.Anchor (hostFrame.Width);
  1079. if (width == null)
  1080. w = hostFrame.Width;
  1081. else
  1082. w = width.Anchor (hostFrame.Width - _x);
  1083. }
  1084. if (y is Pos.PosCenter) {
  1085. if (height == null)
  1086. h = hostFrame.Height;
  1087. else
  1088. h = height.Anchor (hostFrame.Height);
  1089. _y = y.Anchor (hostFrame.Height - h);
  1090. } else {
  1091. if (y == null)
  1092. _y = 0;
  1093. else
  1094. _y = y.Anchor (hostFrame.Height);
  1095. if (height == null)
  1096. h = hostFrame.Height;
  1097. else
  1098. h = height.Anchor (hostFrame.Height - _y);
  1099. }
  1100. Frame = new Rect (_x, _y, w, h);
  1101. // layoutNeeded = false;
  1102. }
  1103. // https://en.wikipedia.org/wiki/Topological_sorting
  1104. List<View> TopologicalSort (HashSet<View> nodes, HashSet<(View From, View To)> edges)
  1105. {
  1106. var result = new List<View> ();
  1107. // Set of all nodes with no incoming edges
  1108. var S = new HashSet<View> (nodes.Where (n => edges.All (e => e.To.Equals (n) == false)));
  1109. while (S.Any ()) {
  1110. // remove a node n from S
  1111. var n = S.First ();
  1112. S.Remove (n);
  1113. // add n to tail of L
  1114. if (n != this?.SuperView)
  1115. result.Add (n);
  1116. // for each node m with an edge e from n to m do
  1117. foreach (var e in edges.Where (e => e.From.Equals (n)).ToArray ()) {
  1118. var m = e.To;
  1119. // remove edge e from the graph
  1120. edges.Remove (e);
  1121. // if m has no other incoming edges then
  1122. if (edges.All (me => me.To.Equals (m) == false) && m != this?.SuperView) {
  1123. // insert m into S
  1124. S.Add (m);
  1125. }
  1126. }
  1127. }
  1128. // if graph has edges then
  1129. if (edges.Any ()) {
  1130. // return error (graph has at least one cycle)
  1131. return null;
  1132. } else {
  1133. // return L (a topologically sorted order)
  1134. return result;
  1135. }
  1136. }
  1137. /// <summary>
  1138. /// This virtual method is invoked when a view starts executing or
  1139. /// when the dimensions of the view have changed, for example in
  1140. /// response to the container view or terminal resizing.
  1141. /// </summary>
  1142. public virtual void LayoutSubviews ()
  1143. {
  1144. if (!layoutNeeded)
  1145. return;
  1146. // Sort out the dependencies of the X, Y, Width, Height properties
  1147. var nodes = new HashSet<View> ();
  1148. var edges = new HashSet<(View, View)> ();
  1149. foreach (var v in InternalSubviews) {
  1150. nodes.Add (v);
  1151. if (v.LayoutStyle == LayoutStyle.Computed) {
  1152. if (v.X is Pos.PosView vX)
  1153. edges.Add ((vX.Target, v));
  1154. if (v.Y is Pos.PosView vY)
  1155. edges.Add ((vY.Target, v));
  1156. if (v.Width is Dim.DimView vWidth)
  1157. edges.Add ((vWidth.Target, v));
  1158. if (v.Height is Dim.DimView vHeight)
  1159. edges.Add ((vHeight.Target, v));
  1160. }
  1161. }
  1162. var ordered = TopologicalSort (nodes, edges);
  1163. if (ordered == null)
  1164. throw new Exception ("There is a recursive cycle in the relative Pos/Dim in the views of " + this);
  1165. foreach (var v in ordered) {
  1166. if (v.LayoutStyle == LayoutStyle.Computed)
  1167. v.RelativeLayout (Frame);
  1168. v.LayoutSubviews ();
  1169. v.layoutNeeded = false;
  1170. }
  1171. if (SuperView == Application.Top && layoutNeeded && ordered.Count == 0 && LayoutStyle == LayoutStyle.Computed) {
  1172. RelativeLayout (Frame);
  1173. }
  1174. layoutNeeded = false;
  1175. }
  1176. /// <inheritdoc cref="ToString"/>
  1177. public override string ToString ()
  1178. {
  1179. return $"{GetType ().Name}({Id})({Frame})";
  1180. }
  1181. /// <summary>
  1182. /// Specifies the event arguments for <see cref="MouseEvent"/>
  1183. /// </summary>
  1184. public class MouseEventEventArgs : EventArgs {
  1185. /// <summary>
  1186. /// Constructs.
  1187. /// </summary>
  1188. /// <param name="me"></param>
  1189. public MouseEventEventArgs (MouseEvent me) => MouseEvent = me;
  1190. /// <summary>
  1191. /// The <see cref="MouseEvent"/> for the event.
  1192. /// </summary>
  1193. public MouseEvent MouseEvent { get; set; }
  1194. /// <summary>
  1195. /// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
  1196. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1197. /// </summary>
  1198. public bool Handled { get; set; }
  1199. }
  1200. /// <inheritdoc cref="OnMouseEnter(Gui.MouseEvent)"/>
  1201. public override bool OnMouseEnter (MouseEvent mouseEvent)
  1202. {
  1203. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  1204. MouseEnter?.Invoke (this, args);
  1205. if (args.Handled)
  1206. return true;
  1207. if (base.OnMouseEnter (mouseEvent))
  1208. return true;
  1209. return false;
  1210. }
  1211. /// <inheritdoc cref="OnMouseLeave(Gui.MouseEvent)"/>
  1212. public override bool OnMouseLeave (MouseEvent mouseEvent)
  1213. {
  1214. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  1215. MouseLeave?.Invoke (this, args);
  1216. if (args.Handled)
  1217. return true;
  1218. if (base.OnMouseLeave (mouseEvent))
  1219. return true;
  1220. return false;
  1221. }
  1222. /// <summary>
  1223. /// Method invoked when a mouse event is generated
  1224. /// </summary>
  1225. /// <param name="mouseEvent"></param>
  1226. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  1227. public virtual bool OnMouseEvent (MouseEvent mouseEvent)
  1228. {
  1229. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  1230. MouseClick?.Invoke (this, args);
  1231. if (args.Handled)
  1232. return true;
  1233. if (MouseEvent (mouseEvent))
  1234. return true;
  1235. return false;
  1236. }
  1237. }
  1238. }