View.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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 = ClipToBounds ();
  616. Driver.DrawFrame (scrRect, padding, fill);
  617. Driver.Clip = savedClip;
  618. }
  619. /// <summary>
  620. /// Utility function to draw strings that contain a hotkey
  621. /// </summary>
  622. /// <param name="text">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
  623. /// <param name="hotColor">Hot color.</param>
  624. /// <param name="normalColor">Normal color.</param>
  625. public void DrawHotString (ustring text, Attribute hotColor, Attribute normalColor)
  626. {
  627. Driver.SetAttribute (normalColor);
  628. foreach (var rune in text) {
  629. if (rune == '_') {
  630. Driver.SetAttribute (hotColor);
  631. continue;
  632. }
  633. Driver.AddRune (rune);
  634. Driver.SetAttribute (normalColor);
  635. }
  636. }
  637. /// <summary>
  638. /// Utility function to draw strings that contains a hotkey using a colorscheme and the "focused" state.
  639. /// </summary>
  640. /// <param name="text">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
  641. /// <param name="focused">If set to <c>true</c> this uses the focused colors from the color scheme, otherwise the regular ones.</param>
  642. /// <param name="scheme">The color scheme to use.</param>
  643. public void DrawHotString (ustring text, bool focused, ColorScheme scheme)
  644. {
  645. if (focused)
  646. DrawHotString (text, scheme.HotFocus, scheme.Focus);
  647. else
  648. DrawHotString (text, scheme.HotNormal, scheme.Normal);
  649. }
  650. /// <summary>
  651. /// This moves the cursor to the specified column and row in the view.
  652. /// </summary>
  653. /// <returns>The move.</returns>
  654. /// <param name="col">Col.</param>
  655. /// <param name="row">Row.</param>
  656. public void Move (int col, int row)
  657. {
  658. ViewToScreen (col, row, out var rcol, out var rrow);
  659. Driver.Move (rcol, rrow);
  660. }
  661. /// <summary>
  662. /// Positions the cursor in the right position based on the currently focused view in the chain.
  663. /// </summary>
  664. public virtual void PositionCursor ()
  665. {
  666. if (focused != null)
  667. focused.PositionCursor ();
  668. else
  669. Move (frame.X, frame.Y);
  670. }
  671. /// <inheritdoc cref="HasFocus"/>
  672. public override bool HasFocus {
  673. get {
  674. return base.HasFocus;
  675. }
  676. internal set {
  677. if (base.HasFocus != value)
  678. if (value)
  679. OnEnter ();
  680. else
  681. OnLeave ();
  682. SetNeedsDisplay ();
  683. base.HasFocus = value;
  684. // Remove focus down the chain of subviews if focus is removed
  685. if (!value && focused != null) {
  686. focused.OnLeave ();
  687. focused.HasFocus = false;
  688. focused = null;
  689. }
  690. }
  691. }
  692. /// <summary>
  693. /// Specifies the event arguments for <see cref="SetFocus(View)"/>
  694. /// </summary>
  695. public class FocusEventArgs : EventArgs {
  696. /// <summary>
  697. /// Constructs.
  698. /// </summary>
  699. public FocusEventArgs () { }
  700. /// <summary>
  701. /// Indicates if the current focus event has already been processed and the driver should stop notifying any other event subscriber.
  702. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  703. /// </summary>
  704. public bool Handled { get; set; }
  705. }
  706. /// <inheritdoc cref="OnEnter"/>
  707. public override bool OnEnter ()
  708. {
  709. FocusEventArgs args = new FocusEventArgs ();
  710. Enter?.Invoke (this, args);
  711. if (args.Handled)
  712. return true;
  713. if (base.OnEnter ())
  714. return true;
  715. return false;
  716. }
  717. /// <inheritdoc cref="OnLeave"/>
  718. public override bool OnLeave ()
  719. {
  720. FocusEventArgs args = new FocusEventArgs ();
  721. Leave?.Invoke (this, args);
  722. if (args.Handled)
  723. return true;
  724. if (base.OnLeave ())
  725. return true;
  726. return false;
  727. }
  728. /// <summary>
  729. /// Returns the currently focused view inside this view, or null if nothing is focused.
  730. /// </summary>
  731. /// <value>The focused.</value>
  732. public View Focused => focused;
  733. /// <summary>
  734. /// Returns the most focused view in the chain of subviews (the leaf view that has the focus).
  735. /// </summary>
  736. /// <value>The most focused.</value>
  737. public View MostFocused {
  738. get {
  739. if (Focused == null)
  740. return null;
  741. var most = Focused.MostFocused;
  742. if (most != null)
  743. return most;
  744. return Focused;
  745. }
  746. }
  747. /// <summary>
  748. /// The color scheme for this view, if it is not defined, it returns the parent's
  749. /// color scheme.
  750. /// </summary>
  751. public ColorScheme ColorScheme {
  752. get {
  753. if (colorScheme == null)
  754. return SuperView?.ColorScheme;
  755. return colorScheme;
  756. }
  757. set {
  758. colorScheme = value;
  759. }
  760. }
  761. ColorScheme colorScheme;
  762. /// <summary>
  763. /// Displays the specified character in the specified column and row.
  764. /// </summary>
  765. /// <param name="col">Col.</param>
  766. /// <param name="row">Row.</param>
  767. /// <param name="ch">Ch.</param>
  768. public void AddRune (int col, int row, Rune ch)
  769. {
  770. if (row < 0 || col < 0)
  771. return;
  772. if (row > frame.Height - 1 || col > frame.Width - 1)
  773. return;
  774. Move (col, row);
  775. Driver.AddRune (ch);
  776. }
  777. /// <summary>
  778. /// Removes the SetNeedsDisplay and the ChildNeedsDisplay setting on this view.
  779. /// </summary>
  780. protected void ClearNeedsDisplay ()
  781. {
  782. NeedDisplay = Rect.Empty;
  783. childNeedsDisplay = false;
  784. }
  785. /// <summary>
  786. /// Performs a redraw of this view and its subviews, only redraws the views that have been flagged for a re-display.
  787. /// </summary>
  788. /// <param name="region">The region to redraw, this is relative to the view itself.</param>
  789. /// <remarks>
  790. /// <para>
  791. /// Views should set the color that they want to use on entry, as otherwise this will inherit
  792. /// the last color that was set globaly on the driver.
  793. /// </para>
  794. /// </remarks>
  795. public virtual void Redraw (Rect region)
  796. {
  797. var clipRect = new Rect (Point.Empty, frame.Size);
  798. if (subviews != null) {
  799. foreach (var view in subviews) {
  800. if (view.NeedDisplay != null && (!view.NeedDisplay.IsEmpty || view.childNeedsDisplay)) {
  801. if (view.Frame.IntersectsWith (clipRect) && view.Frame.IntersectsWith (region)) {
  802. // FIXED: optimize this by computing the intersection of region and view.Bounds
  803. if (view.layoutNeeded)
  804. view.LayoutSubviews ();
  805. Application.CurrentView = view;
  806. // Ensure we don't make the Driver's clip rect any bigger
  807. if (Driver.Clip.IsEmpty || Driver.Clip.Contains(RectToScreen (view.Frame))) {
  808. var savedClip = view.ClipToBounds ();
  809. view.Redraw (view.Bounds);
  810. Driver.Clip = savedClip;
  811. } else {
  812. view.Redraw (view.Bounds);
  813. }
  814. }
  815. view.NeedDisplay = Rect.Empty;
  816. view.childNeedsDisplay = false;
  817. }
  818. }
  819. }
  820. ClearNeedsDisplay ();
  821. }
  822. /// <summary>
  823. /// Focuses the specified sub-view.
  824. /// </summary>
  825. /// <param name="view">View.</param>
  826. public void SetFocus (View view)
  827. {
  828. if (view == null)
  829. return;
  830. //Console.WriteLine ($"Request to focus {view}");
  831. if (!view.CanFocus)
  832. return;
  833. if (focused == view)
  834. return;
  835. // Make sure that this view is a subview
  836. View c;
  837. for (c = view.container; c != null; c = c.container)
  838. if (c == this)
  839. break;
  840. if (c == null)
  841. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  842. if (focused != null)
  843. focused.HasFocus = false;
  844. focused = view;
  845. focused.HasFocus = true;
  846. focused.EnsureFocus ();
  847. // Send focus upwards
  848. SuperView?.SetFocus (this);
  849. }
  850. /// <summary>
  851. /// Specifies the event arguments for <see cref="KeyEvent"/>
  852. /// </summary>
  853. public class KeyEventEventArgs : EventArgs {
  854. /// <summary>
  855. /// Constructs.
  856. /// </summary>
  857. /// <param name="ke"></param>
  858. public KeyEventEventArgs (KeyEvent ke) => KeyEvent = ke;
  859. /// <summary>
  860. /// The <see cref="KeyEvent"/> for the event.
  861. /// </summary>
  862. public KeyEvent KeyEvent { get; set; }
  863. /// <summary>
  864. /// Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber.
  865. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  866. /// </summary>
  867. public bool Handled { get; set; } = false;
  868. }
  869. /// <summary>
  870. /// Invoked when a character key is pressed and occurs after the key up event.
  871. /// </summary>
  872. public event EventHandler<KeyEventEventArgs> KeyPress;
  873. /// <inheritdoc cref="ProcessKey"/>
  874. public override bool ProcessKey (KeyEvent keyEvent)
  875. {
  876. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  877. KeyPress?.Invoke (this, args);
  878. if (args.Handled)
  879. return true;
  880. if (Focused?.ProcessKey (keyEvent) == true)
  881. return true;
  882. return false;
  883. }
  884. /// <inheritdoc cref="ProcessHotKey"/>
  885. public override bool ProcessHotKey (KeyEvent keyEvent)
  886. {
  887. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  888. KeyPress?.Invoke (this, args);
  889. if (args.Handled)
  890. return true;
  891. if (subviews == null || subviews.Count == 0)
  892. return false;
  893. foreach (var view in subviews)
  894. if (view.ProcessHotKey (keyEvent))
  895. return true;
  896. return false;
  897. }
  898. /// <inheritdoc cref="ProcessColdKey"/>
  899. public override bool ProcessColdKey (KeyEvent keyEvent)
  900. {
  901. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  902. KeyPress?.Invoke (this, args);
  903. if (args.Handled)
  904. return true;
  905. if (subviews == null || subviews.Count == 0)
  906. return false;
  907. foreach (var view in subviews)
  908. if (view.ProcessColdKey (keyEvent))
  909. return true;
  910. return false;
  911. }
  912. /// <summary>
  913. /// Invoked when a key is pressed
  914. /// </summary>
  915. public event EventHandler<KeyEventEventArgs> KeyDown;
  916. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  917. public override bool OnKeyDown (KeyEvent keyEvent)
  918. {
  919. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  920. KeyDown?.Invoke (this, args);
  921. if (args.Handled)
  922. return true;
  923. if (subviews == null || subviews.Count == 0)
  924. return false;
  925. foreach (var view in subviews)
  926. if (view.OnKeyDown (keyEvent))
  927. return true;
  928. return false;
  929. }
  930. /// <summary>
  931. /// Invoked when a key is released
  932. /// </summary>
  933. public event EventHandler<KeyEventEventArgs> KeyUp;
  934. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  935. public override bool OnKeyUp (KeyEvent keyEvent)
  936. {
  937. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  938. KeyUp?.Invoke (this, args);
  939. if (args.Handled)
  940. return true;
  941. if (subviews == null || subviews.Count == 0)
  942. return false;
  943. foreach (var view in subviews)
  944. if (view.OnKeyUp (keyEvent))
  945. return true;
  946. return false;
  947. }
  948. /// <summary>
  949. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
  950. /// </summary>
  951. public void EnsureFocus ()
  952. {
  953. if (focused == null)
  954. if (FocusDirection == Direction.Forward)
  955. FocusFirst ();
  956. else
  957. FocusLast ();
  958. }
  959. /// <summary>
  960. /// Focuses the first focusable subview if one exists.
  961. /// </summary>
  962. public void FocusFirst ()
  963. {
  964. if (subviews == null) {
  965. SuperView?.SetFocus (this);
  966. return;
  967. }
  968. foreach (var view in subviews) {
  969. if (view.CanFocus) {
  970. SetFocus (view);
  971. return;
  972. }
  973. }
  974. }
  975. /// <summary>
  976. /// Focuses the last focusable subview if one exists.
  977. /// </summary>
  978. public void FocusLast ()
  979. {
  980. if (subviews == null) {
  981. SuperView?.SetFocus (this);
  982. return;
  983. }
  984. for (int i = subviews.Count; i > 0;) {
  985. i--;
  986. View v = subviews [i];
  987. if (v.CanFocus) {
  988. SetFocus (v);
  989. return;
  990. }
  991. }
  992. }
  993. /// <summary>
  994. /// Focuses the previous view.
  995. /// </summary>
  996. /// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
  997. public bool FocusPrev ()
  998. {
  999. FocusDirection = Direction.Backward;
  1000. if (subviews == null || subviews.Count == 0)
  1001. return false;
  1002. if (focused == null) {
  1003. FocusLast ();
  1004. return focused != null;
  1005. }
  1006. int focused_idx = -1;
  1007. for (int i = subviews.Count; i > 0;) {
  1008. i--;
  1009. View w = subviews [i];
  1010. if (w.HasFocus) {
  1011. if (w.FocusPrev ())
  1012. return true;
  1013. focused_idx = i;
  1014. continue;
  1015. }
  1016. if (w.CanFocus && focused_idx != -1) {
  1017. focused.HasFocus = false;
  1018. if (w != null && w.CanFocus)
  1019. w.FocusLast ();
  1020. SetFocus (w);
  1021. return true;
  1022. }
  1023. }
  1024. if (focused != null) {
  1025. focused.HasFocus = false;
  1026. focused = null;
  1027. }
  1028. return false;
  1029. }
  1030. /// <summary>
  1031. /// Focuses the next view.
  1032. /// </summary>
  1033. /// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
  1034. public bool FocusNext ()
  1035. {
  1036. FocusDirection = Direction.Forward;
  1037. if (subviews == null || subviews.Count == 0)
  1038. return false;
  1039. if (focused == null) {
  1040. FocusFirst ();
  1041. return focused != null;
  1042. }
  1043. int n = subviews.Count;
  1044. int focused_idx = -1;
  1045. for (int i = 0; i < n; i++) {
  1046. View w = subviews [i];
  1047. if (w.HasFocus) {
  1048. if (w.FocusNext ())
  1049. return true;
  1050. focused_idx = i;
  1051. continue;
  1052. }
  1053. if (w.CanFocus && focused_idx != -1) {
  1054. focused.HasFocus = false;
  1055. if (w != null && w.CanFocus)
  1056. w.FocusFirst ();
  1057. SetFocus (w);
  1058. return true;
  1059. }
  1060. }
  1061. if (focused != null) {
  1062. focused.HasFocus = false;
  1063. focused = null;
  1064. }
  1065. return false;
  1066. }
  1067. /// <summary>
  1068. /// Computes the RelativeLayout for the view, given the frame for its container.
  1069. /// </summary>
  1070. /// <param name="hostFrame">The Frame for the host.</param>
  1071. internal void RelativeLayout (Rect hostFrame)
  1072. {
  1073. int w, h, _x, _y;
  1074. if (x is Pos.PosCenter) {
  1075. if (width == null)
  1076. w = hostFrame.Width;
  1077. else
  1078. w = width.Anchor (hostFrame.Width);
  1079. _x = x.Anchor (hostFrame.Width - w);
  1080. } else {
  1081. if (x == null)
  1082. _x = 0;
  1083. else
  1084. _x = x.Anchor (hostFrame.Width);
  1085. if (width == null)
  1086. w = hostFrame.Width;
  1087. else
  1088. w = width.Anchor (hostFrame.Width - _x);
  1089. }
  1090. if (y is Pos.PosCenter) {
  1091. if (height == null)
  1092. h = hostFrame.Height;
  1093. else
  1094. h = height.Anchor (hostFrame.Height);
  1095. _y = y.Anchor (hostFrame.Height - h);
  1096. } else {
  1097. if (y == null)
  1098. _y = 0;
  1099. else
  1100. _y = y.Anchor (hostFrame.Height);
  1101. if (height == null)
  1102. h = hostFrame.Height;
  1103. else
  1104. h = height.Anchor (hostFrame.Height - _y);
  1105. }
  1106. Frame = new Rect (_x, _y, w, h);
  1107. // layoutNeeded = false;
  1108. }
  1109. // https://en.wikipedia.org/wiki/Topological_sorting
  1110. List<View> TopologicalSort (HashSet<View> nodes, HashSet<(View From, View To)> edges)
  1111. {
  1112. var result = new List<View> ();
  1113. // Set of all nodes with no incoming edges
  1114. var S = new HashSet<View> (nodes.Where (n => edges.All (e => e.To.Equals (n) == false)));
  1115. while (S.Any ()) {
  1116. // remove a node n from S
  1117. var n = S.First ();
  1118. S.Remove (n);
  1119. // add n to tail of L
  1120. if (n != this?.SuperView)
  1121. result.Add (n);
  1122. // for each node m with an edge e from n to m do
  1123. foreach (var e in edges.Where (e => e.From.Equals (n)).ToArray ()) {
  1124. var m = e.To;
  1125. // remove edge e from the graph
  1126. edges.Remove (e);
  1127. // if m has no other incoming edges then
  1128. if (edges.All (me => me.To.Equals (m) == false) && m != this?.SuperView) {
  1129. // insert m into S
  1130. S.Add (m);
  1131. }
  1132. }
  1133. }
  1134. // if graph has edges then
  1135. if (edges.Any ()) {
  1136. // return error (graph has at least one cycle)
  1137. return null;
  1138. } else {
  1139. // return L (a topologically sorted order)
  1140. return result;
  1141. }
  1142. }
  1143. /// <summary>
  1144. /// This virtual method is invoked when a view starts executing or
  1145. /// when the dimensions of the view have changed, for example in
  1146. /// response to the container view or terminal resizing.
  1147. /// </summary>
  1148. public virtual void LayoutSubviews ()
  1149. {
  1150. if (!layoutNeeded)
  1151. return;
  1152. // Sort out the dependencies of the X, Y, Width, Height properties
  1153. var nodes = new HashSet<View> ();
  1154. var edges = new HashSet<(View, View)> ();
  1155. foreach (var v in InternalSubviews) {
  1156. nodes.Add (v);
  1157. if (v.LayoutStyle == LayoutStyle.Computed) {
  1158. if (v.X is Pos.PosView vX)
  1159. edges.Add ((vX.Target, v));
  1160. if (v.Y is Pos.PosView vY)
  1161. edges.Add ((vY.Target, v));
  1162. if (v.Width is Dim.DimView vWidth)
  1163. edges.Add ((vWidth.Target, v));
  1164. if (v.Height is Dim.DimView vHeight)
  1165. edges.Add ((vHeight.Target, v));
  1166. }
  1167. }
  1168. var ordered = TopologicalSort (nodes, edges);
  1169. if (ordered == null)
  1170. throw new Exception ("There is a recursive cycle in the relative Pos/Dim in the views of " + this);
  1171. foreach (var v in ordered) {
  1172. if (v.LayoutStyle == LayoutStyle.Computed)
  1173. v.RelativeLayout (Frame);
  1174. v.LayoutSubviews ();
  1175. v.layoutNeeded = false;
  1176. }
  1177. if (SuperView == Application.Top && layoutNeeded && ordered.Count == 0 && LayoutStyle == LayoutStyle.Computed) {
  1178. RelativeLayout (Frame);
  1179. }
  1180. layoutNeeded = false;
  1181. }
  1182. /// <inheritdoc cref="ToString"/>
  1183. public override string ToString ()
  1184. {
  1185. return $"{GetType ().Name}({Id})({Frame})";
  1186. }
  1187. /// <summary>
  1188. /// Specifies the event arguments for <see cref="MouseEvent"/>
  1189. /// </summary>
  1190. public class MouseEventEventArgs : EventArgs {
  1191. /// <summary>
  1192. /// Constructs.
  1193. /// </summary>
  1194. /// <param name="me"></param>
  1195. public MouseEventEventArgs (MouseEvent me) => MouseEvent = me;
  1196. /// <summary>
  1197. /// The <see cref="MouseEvent"/> for the event.
  1198. /// </summary>
  1199. public MouseEvent MouseEvent { get; set; }
  1200. /// <summary>
  1201. /// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
  1202. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1203. /// </summary>
  1204. public bool Handled { get; set; }
  1205. }
  1206. /// <inheritdoc cref="OnMouseEnter(Gui.MouseEvent)"/>
  1207. public override bool OnMouseEnter (MouseEvent mouseEvent)
  1208. {
  1209. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  1210. MouseEnter?.Invoke (this, args);
  1211. if (args.Handled)
  1212. return true;
  1213. if (base.OnMouseEnter (mouseEvent))
  1214. return true;
  1215. return false;
  1216. }
  1217. /// <inheritdoc cref="OnMouseLeave(Gui.MouseEvent)"/>
  1218. public override bool OnMouseLeave (MouseEvent mouseEvent)
  1219. {
  1220. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  1221. MouseLeave?.Invoke (this, args);
  1222. if (args.Handled)
  1223. return true;
  1224. if (base.OnMouseLeave (mouseEvent))
  1225. return true;
  1226. return false;
  1227. }
  1228. /// <summary>
  1229. /// Method invoked when a mouse event is generated
  1230. /// </summary>
  1231. /// <param name="mouseEvent"></param>
  1232. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  1233. public virtual bool OnMouseEvent (MouseEvent mouseEvent)
  1234. {
  1235. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  1236. MouseClick?.Invoke (this, args);
  1237. if (args.Handled)
  1238. return true;
  1239. if (MouseEvent (mouseEvent))
  1240. return true;
  1241. return false;
  1242. }
  1243. }
  1244. }