Menu.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. //
  2. // Menu.cs: application menus and submenus
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // TODO:
  8. // Add accelerator support, but should also support chords (ShortCut in MenuItem)
  9. // Allow menus inside menus
  10. using System;
  11. using NStack;
  12. using System.Linq;
  13. using System.Collections.Generic;
  14. namespace Terminal.Gui {
  15. /// <summary>
  16. /// Specifies how a <see cref="MenuItem"/> shows selection state.
  17. /// </summary>
  18. [Flags]
  19. public enum MenuItemCheckStyle {
  20. /// <summary>
  21. /// The menu item will be shown normally, with no check indicator.
  22. /// </summary>
  23. NoCheck = 0b_0000_0000,
  24. /// <summary>
  25. /// The menu item will indicate checked/un-checked state (see <see cref="Checked"/>.
  26. /// </summary>
  27. Checked = 0b_0000_0001,
  28. /// <summary>
  29. /// The menu item is part of a menu radio group (see <see cref="Checked"/> and will indicate selected state.
  30. /// </summary>
  31. Radio = 0b_0000_0010,
  32. };
  33. /// <summary>
  34. /// A <see cref="MenuItem"/> has a title, an associated help text, and an action to execute on activation.
  35. /// </summary>
  36. public class MenuItem {
  37. /// <summary>
  38. /// Initializes a new instance of <see cref="MenuItem"/>
  39. /// </summary>
  40. public MenuItem ()
  41. {
  42. Title = "";
  43. Help = "";
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of <see cref="MenuItem"/>.
  47. /// </summary>
  48. /// <param name="title">Title for the menu item.</param>
  49. /// <param name="help">Help text to display.</param>
  50. /// <param name="action">Action to invoke when the menu item is activated.</param>
  51. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  52. /// <param name="parent">The parent of this menu item.</param>
  53. public MenuItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null)
  54. {
  55. Title = title ?? "";
  56. Help = help ?? "";
  57. Action = action;
  58. CanExecute = canExecute;
  59. bool nextIsHot = false;
  60. foreach (var x in Title) {
  61. if (x == '_')
  62. nextIsHot = true;
  63. else {
  64. if (nextIsHot) {
  65. HotKey = Char.ToUpper ((char)x);
  66. break;
  67. }
  68. nextIsHot = false;
  69. }
  70. }
  71. Parent = parent;
  72. }
  73. /// <summary>
  74. /// The HotKey is used when the menu is active, the shortcut can be triggered when the menu is not active.
  75. /// For example HotKey would be "N" when the File Menu is open (assuming there is a "_New" entry
  76. /// if the ShortCut is set to "Control-N", this would be a global hotkey that would trigger as well
  77. /// </summary>
  78. public Rune HotKey;
  79. /// <summary>
  80. /// Gets or sets the title.
  81. /// </summary>
  82. /// <value>The title.</value>
  83. public ustring Title { get; set; }
  84. /// <summary>
  85. /// Gets or sets the help text for the menu item.
  86. /// </summary>
  87. /// <value>The help text.</value>
  88. public ustring Help { get; set; }
  89. /// <summary>
  90. /// Gets or sets the action to be invoked when the menu is triggered
  91. /// </summary>
  92. /// <value>Method to invoke.</value>
  93. public Action Action { get; set; }
  94. /// <summary>
  95. /// Gets or sets the action to be invoked if the menu can be triggered
  96. /// </summary>
  97. /// <value>Function to determine if action is ready to be executed.</value>
  98. public Func<bool> CanExecute { get; set; }
  99. /// <summary>
  100. /// Shortcut to check if the menu item is enabled
  101. /// </summary>
  102. public bool IsEnabled ()
  103. {
  104. return CanExecute == null ? true : CanExecute ();
  105. }
  106. internal int Width => Title.RuneCount + Help.RuneCount + 1 + 2 +
  107. (Checked || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio) ? 2 : 0);
  108. /// <summary>
  109. /// Sets or gets whether the <see cref="MenuItem"/> shows a check indicator or not. See <see cref="MenuItemCheckStyle"/>.
  110. /// </summary>
  111. public bool Checked { set; get; }
  112. /// <summary>
  113. /// Sets or gets the type selection indicator the menu item will be displayed with.
  114. /// </summary>
  115. public MenuItemCheckStyle CheckType { get; set; }
  116. /// <summary>
  117. /// Gets or sets the parent for this <see cref="MenuItem"/>.
  118. /// </summary>
  119. /// <value>The parent.</value>
  120. public MenuItem Parent { get; internal set; }
  121. /// <summary>
  122. /// Gets if this <see cref="MenuItem"/> is from a sub-menu.
  123. /// </summary>
  124. internal bool IsFromSubMenu { get {return Parent != null; } }
  125. /// <summary>
  126. /// Merely a debugging aid to see the interaction with main
  127. /// </summary>
  128. public MenuItem GetMenuItem ()
  129. {
  130. return this;
  131. }
  132. /// <summary>
  133. /// Merely a debugging aid to see the interaction with main
  134. /// </summary>
  135. public bool GetMenuBarItem ()
  136. {
  137. return IsFromSubMenu;
  138. }
  139. }
  140. /// <summary>
  141. /// A <see cref="MenuBarItem"/> contains <see cref="MenuBarItem"/>s or <see cref="MenuItem"/>s.
  142. /// </summary>
  143. public class MenuBarItem : MenuItem {
  144. /// <summary>
  145. /// Initializes a new <see cref="MenuBarItem"/> as a <see cref="MenuItem"/>.
  146. /// </summary>
  147. /// <param name="title">Title for the menu item.</param>
  148. /// <param name="help">Help text to display.</param>
  149. /// <param name="action">Action to invoke when the menu item is activated.</param>
  150. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  151. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  152. public MenuBarItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null) : base (title, help, action, canExecute, parent)
  153. {
  154. SetTitle (title ?? "");
  155. Children = null;
  156. }
  157. /// <summary>
  158. /// Initializes a new <see cref="MenuBarItem"/>.
  159. /// </summary>
  160. /// <param name="title">Title for the menu item.</param>
  161. /// <param name="children">The items in the current menu.</param>
  162. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  163. public MenuBarItem (ustring title, MenuItem [] children, MenuItem parent = null)
  164. {
  165. if (children == null) {
  166. throw new ArgumentNullException (nameof (children), "The parameter cannot be null. Use an empty array instead.");
  167. }
  168. SetTitle (title ?? "");
  169. if (parent != null) {
  170. Parent = parent;
  171. }
  172. SetChildrensParent (children);
  173. Children = children;
  174. }
  175. /// <summary>
  176. /// Initializes a new <see cref="MenuBarItem"/>.
  177. /// </summary>
  178. /// <param name="children">The items in the current menu.</param>
  179. public MenuBarItem (MenuItem [] children) : this ("", children) { }
  180. /// <summary>
  181. /// Initializes a new <see cref="MenuBarItem"/>.
  182. /// </summary>
  183. public MenuBarItem () : this (children: new MenuItem [] { }) { }
  184. //static int GetMaxTitleLength (MenuItem [] children)
  185. //{
  186. // int maxLength = 0;
  187. // foreach (var item in children) {
  188. // int len = GetMenuBarItemLength (item.Title);
  189. // if (len > maxLength)
  190. // maxLength = len;
  191. // item.IsFromSubMenu = true;
  192. // }
  193. // return maxLength;
  194. //}
  195. void SetChildrensParent (MenuItem [] childrens)
  196. {
  197. foreach (var child in childrens) {
  198. if (child != null && child.Parent == null) {
  199. child.Parent = this;
  200. }
  201. }
  202. }
  203. /// <summary>
  204. /// Check if the children parameter is a <see cref="MenuBarItem"/>.
  205. /// </summary>
  206. /// <param name="children"></param>
  207. /// <returns>Returns a <see cref="MenuBarItem"/> or null otherwise.</returns>
  208. public MenuBarItem SubMenu (MenuItem children)
  209. {
  210. return children as MenuBarItem;
  211. }
  212. /// <summary>
  213. /// Check if the <see cref="MenuItem"/> parameter is a child of this.
  214. /// </summary>
  215. /// <param name="menuItem"></param>
  216. /// <returns>Returns <c>true</c> if it is a child of this. <c>false</c> otherwise.</returns>
  217. public bool IsSubMenuOf (MenuItem menuItem)
  218. {
  219. foreach (var child in Children) {
  220. if (child == menuItem && child.Parent == menuItem.Parent) {
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. /// <summary>
  227. /// Get the index of the <see cref="MenuItem"/> parameter.
  228. /// </summary>
  229. /// <param name="children"></param>
  230. /// <returns>Returns a value bigger than -1 if the <see cref="MenuItem"/> is a child of this.</returns>
  231. public int GetChildrenIndex (MenuItem children)
  232. {
  233. if (Children?.Length == 0) {
  234. return -1;
  235. }
  236. int i = 0;
  237. foreach (var child in Children) {
  238. if (child == children) {
  239. return i;
  240. }
  241. i++;
  242. }
  243. return -1;
  244. }
  245. void SetTitle (ustring title)
  246. {
  247. if (title == null)
  248. title = "";
  249. Title = title;
  250. }
  251. int GetMenuBarItemLength (ustring title)
  252. {
  253. int len = 0;
  254. foreach (var ch in title) {
  255. if (ch == '_')
  256. continue;
  257. len++;
  258. }
  259. return len;
  260. }
  261. ///// <summary>
  262. ///// Gets or sets the title to display.
  263. ///// </summary>
  264. ///// <value>The title.</value>
  265. //public ustring Title { get; set; }
  266. /// <summary>
  267. /// Gets or sets an array of <see cref="MenuItem"/> objects that are the children of this <see cref="MenuBarItem"/>
  268. /// </summary>
  269. /// <value>The children.</value>
  270. public MenuItem [] Children { get; set; }
  271. internal int TitleLength => GetMenuBarItemLength (Title);
  272. internal bool IsTopLevel { get => Parent == null && (Children == null || Children.Length == 0); }
  273. }
  274. class Menu : View {
  275. internal MenuBarItem barItems;
  276. MenuBar host;
  277. internal int current;
  278. internal View previousSubFocused;
  279. static Rect MakeFrame (int x, int y, MenuItem [] items)
  280. {
  281. if (items == null || items.Length == 0) {
  282. return new Rect ();
  283. }
  284. int maxW = items.Max (z => z?.Width) ?? 0;
  285. return new Rect (x, y, maxW + 2, items.Length + 2);
  286. }
  287. public Menu (MenuBar host, int x, int y, MenuBarItem barItems) : base (MakeFrame (x, y, barItems.Children))
  288. {
  289. this.barItems = barItems;
  290. this.host = host;
  291. if (barItems.IsTopLevel) {
  292. // This is a standalone MenuItem on a MenuBar
  293. ColorScheme = Colors.Menu;
  294. CanFocus = true;
  295. } else {
  296. current = -1;
  297. for (int i = 0; i < barItems.Children.Length; i++) {
  298. if (barItems.Children [i] != null) {
  299. current = i;
  300. break;
  301. }
  302. }
  303. ColorScheme = Colors.Menu;
  304. CanFocus = true;
  305. WantMousePositionReports = host.WantMousePositionReports;
  306. }
  307. }
  308. internal Attribute DetermineColorSchemeFor (MenuItem item, int index)
  309. {
  310. if (item != null) {
  311. if (index == current) return ColorScheme.Focus;
  312. if (!item.IsEnabled ()) return ColorScheme.Disabled;
  313. }
  314. return ColorScheme.Normal;
  315. }
  316. public override void Redraw (Rect bounds)
  317. {
  318. Driver.SetAttribute (ColorScheme.Normal);
  319. DrawFrame (bounds, padding: 0, fill: true);
  320. for (int i = 0; i < barItems.Children.Length; i++) {
  321. var item = barItems.Children [i];
  322. Driver.SetAttribute (item == null ? ColorScheme.Normal : i == current ? ColorScheme.Focus : ColorScheme.Normal);
  323. if (item == null) {
  324. Move (0, i + 1);
  325. Driver.AddRune (Driver.LeftTee);
  326. } else
  327. Move (1, i + 1);
  328. Driver.SetAttribute (DetermineColorSchemeFor (item, i));
  329. for (int p = 0; p < Frame.Width - 2; p++)
  330. if (item == null)
  331. Driver.AddRune (Driver.HLine);
  332. else if (p == Frame.Width - 3 && barItems.SubMenu(barItems.Children [i]) != null)
  333. Driver.AddRune (Driver.RightArrow);
  334. else
  335. Driver.AddRune (' ');
  336. if (item == null) {
  337. Move (Frame.Right - 1, i + 1);
  338. Driver.AddRune (Driver.RightTee);
  339. continue;
  340. }
  341. ustring textToDraw;
  342. var checkChar = Driver.Selected;
  343. var uncheckedChar = Driver.UnSelected;
  344. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked)) {
  345. checkChar = Driver.Checked;
  346. uncheckedChar = Driver.UnChecked;
  347. }
  348. // Support Checked even though CHeckType wasn't set
  349. if (item.Checked) {
  350. textToDraw = ustring.Make (new Rune [] { checkChar, ' ' }) + item.Title;
  351. } else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) ||
  352. item.CheckType.HasFlag (MenuItemCheckStyle.Radio)) {
  353. textToDraw = ustring.Make (new Rune [] { uncheckedChar, ' ' }) + item.Title;
  354. } else {
  355. textToDraw = item.Title;
  356. }
  357. Move (2, i + 1);
  358. if (!item.IsEnabled ())
  359. DrawHotString (textToDraw, ColorScheme.Disabled, ColorScheme.Disabled);
  360. else
  361. DrawHotString (textToDraw,
  362. i == current ? ColorScheme.HotFocus : ColorScheme.HotNormal,
  363. i == current ? ColorScheme.Focus : ColorScheme.Normal);
  364. // The help string
  365. var l = item.Help.RuneCount;
  366. Move (Frame.Width - l - 2, 1 + i);
  367. Driver.AddStr (item.Help);
  368. }
  369. PositionCursor ();
  370. }
  371. public override void PositionCursor ()
  372. {
  373. if (host == null || host.IsMenuOpen)
  374. if (barItems.IsTopLevel) {
  375. host.PositionCursor ();
  376. } else
  377. Move (2, 1 + current);
  378. else
  379. host.PositionCursor ();
  380. }
  381. public void Run (Action action)
  382. {
  383. if (action == null)
  384. return;
  385. Application.UngrabMouse ();
  386. host.CloseAllMenus ();
  387. Application.Refresh ();
  388. Application.MainLoop.AddIdle (() => {
  389. action ();
  390. return false;
  391. });
  392. }
  393. public override bool OnLeave (View view)
  394. {
  395. return host.OnLeave (view);
  396. }
  397. public override bool OnKeyDown (KeyEvent keyEvent)
  398. {
  399. if (keyEvent.IsAlt) {
  400. host.CloseAllMenus ();
  401. return true;
  402. }
  403. return false;
  404. }
  405. public override bool ProcessHotKey (KeyEvent keyEvent)
  406. {
  407. // To ncurses simulate a AltMask key pressing Alt+Space because
  408. // it can�t detect an alone special key down was pressed.
  409. if (keyEvent.IsAlt && keyEvent.Key == Key.AltMask) {
  410. OnKeyDown (keyEvent);
  411. return true;
  412. }
  413. return false;
  414. }
  415. public override bool ProcessKey (KeyEvent kb)
  416. {
  417. switch (kb.Key) {
  418. case Key.Tab:
  419. host.CleanUp ();
  420. return true;
  421. case Key.CursorUp:
  422. return MoveUp ();
  423. case Key.CursorDown:
  424. return MoveDown ();
  425. case Key.CursorLeft:
  426. host.PreviousMenu (true);
  427. return true;
  428. case Key.CursorRight:
  429. host.NextMenu (barItems.IsTopLevel || (barItems.Children != null && current > -1 && current < barItems.Children.Length && barItems.Children [current].IsFromSubMenu) ? true : false);
  430. return true;
  431. case Key.Esc:
  432. Application.UngrabMouse ();
  433. host.CloseAllMenus ();
  434. return true;
  435. case Key.Enter:
  436. if (barItems.IsTopLevel) {
  437. Run (barItems.Action);
  438. } else if (current > -1) {
  439. Run (barItems.Children [current].Action);
  440. }
  441. return true;
  442. default:
  443. // TODO: rune-ify
  444. if (barItems.Children != null && Char.IsLetterOrDigit ((char)kb.KeyValue)) {
  445. var x = Char.ToUpper ((char)kb.KeyValue);
  446. foreach (var item in barItems.Children) {
  447. if (item == null) continue;
  448. if (item.IsEnabled () && item.HotKey == x) {
  449. host.CloseMenu ();
  450. Run (item.Action);
  451. return true;
  452. }
  453. }
  454. }
  455. break;
  456. }
  457. return false;
  458. }
  459. bool MoveDown ()
  460. {
  461. if (barItems.IsTopLevel) {
  462. return true;
  463. }
  464. bool disabled;
  465. do {
  466. current++;
  467. if (current >= barItems.Children.Length) {
  468. current = 0;
  469. }
  470. if (this != host.openCurrentMenu && barItems.Children [current].IsFromSubMenu && host.selectedSub > -1) {
  471. host.PreviousMenu (true);
  472. host.SelectEnabledItem (barItems.Children, current, out current);
  473. host.openCurrentMenu = this;
  474. }
  475. var item = barItems.Children [current];
  476. if (item?.IsEnabled () != true) {
  477. disabled = true;
  478. } else {
  479. disabled = false;
  480. }
  481. if (host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
  482. !disabled && host.IsMenuOpen) {
  483. CheckSubMenu ();
  484. break;
  485. }
  486. if (!host.IsMenuOpen) {
  487. host.OpenMenu (host.selected);
  488. }
  489. } while (barItems.Children [current] == null || disabled);
  490. SetNeedsDisplay ();
  491. return true;
  492. }
  493. bool MoveUp ()
  494. {
  495. if (barItems.IsTopLevel || current == -1) {
  496. return true;
  497. }
  498. bool disabled;
  499. do {
  500. current--;
  501. if (host.UseKeysUpDownAsKeysLeftRight) {
  502. if ((current == -1 || this != host.openCurrentMenu) && barItems.Children [current + 1].IsFromSubMenu && host.selectedSub > -1) {
  503. current++;
  504. host.PreviousMenu (true);
  505. if (current > 0) {
  506. current--;
  507. host.openCurrentMenu = this;
  508. }
  509. break;
  510. }
  511. }
  512. if (current < 0)
  513. current = barItems.Children.Length - 1;
  514. if (!host.SelectEnabledItem (barItems.Children, current, out current, false)) {
  515. current = 0;
  516. if (!host.SelectEnabledItem (barItems.Children, current, out current)) {
  517. host.CloseMenu ();
  518. }
  519. break;
  520. }
  521. var item = barItems.Children [current];
  522. if (item?.IsEnabled () != true) {
  523. disabled = true;
  524. } else {
  525. disabled = false;
  526. }
  527. if (host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
  528. !disabled && host.IsMenuOpen) {
  529. CheckSubMenu ();
  530. break;
  531. }
  532. } while (barItems.Children [current] == null || disabled);
  533. SetNeedsDisplay ();
  534. return true;
  535. }
  536. public override bool MouseEvent (MouseEvent me)
  537. {
  538. if (!host.handled && !host.HandleGrabView (me, this)) {
  539. return false;
  540. }
  541. host.handled = false;
  542. bool disabled;
  543. if (me.Flags == MouseFlags.Button1Clicked) {
  544. disabled = false;
  545. if (me.Y < 1)
  546. return true;
  547. var meY = me.Y - 1;
  548. if (meY >= barItems.Children.Length)
  549. return true;
  550. var item = barItems.Children [meY];
  551. if (item == null || !item.IsEnabled ()) disabled = true;
  552. if (item != null && !disabled)
  553. Run (barItems.Children [meY].Action);
  554. return true;
  555. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked ||
  556. me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.ReportMousePosition ||
  557. me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  558. disabled = false;
  559. if (me.Y < 1 || me.Y - 1 >= barItems.Children.Length) {
  560. return true;
  561. }
  562. var item = barItems.Children [me.Y - 1];
  563. if (item == null || !item.IsEnabled ()) disabled = true;
  564. if (item != null && !disabled)
  565. current = me.Y - 1;
  566. CheckSubMenu ();
  567. return true;
  568. }
  569. return false;
  570. }
  571. internal void CheckSubMenu ()
  572. {
  573. if (current == -1 || barItems.Children [current] == null) {
  574. return;
  575. }
  576. var subMenu = barItems.SubMenu (barItems.Children [current]);
  577. if (subMenu != null) {
  578. int pos = -1;
  579. if (host.openSubMenu != null) {
  580. pos = host.openSubMenu.FindIndex (o => o?.barItems == subMenu);
  581. }
  582. if (pos == -1 && this != host.openCurrentMenu && subMenu.Children != host.openCurrentMenu.barItems.Children) {
  583. host.CloseMenu (false, true);
  584. }
  585. host.Activate (host.selected, pos, subMenu);
  586. } else if (host.openSubMenu?.Last ().barItems.IsSubMenuOf (barItems.Children [current]) == false) {
  587. host.CloseMenu (false, true);
  588. } else {
  589. SetNeedsDisplay ();
  590. }
  591. }
  592. int GetSubMenuIndex (MenuBarItem subMenu)
  593. {
  594. int pos = -1;
  595. if (this != null && Subviews.Count > 0) {
  596. Menu v = null;
  597. foreach (var menu in Subviews) {
  598. if (((Menu)menu).barItems == subMenu)
  599. v = (Menu)menu;
  600. }
  601. if (v != null)
  602. pos = Subviews.IndexOf (v);
  603. }
  604. return pos;
  605. }
  606. }
  607. /// <summary>
  608. /// The MenuBar provides a menu for Terminal.Gui applications.
  609. /// </summary>
  610. /// <remarks>
  611. /// <para>
  612. /// The <see cref="MenuBar"/> appears on the first row of the terminal.
  613. /// </para>
  614. /// <para>
  615. /// The <see cref="MenuBar"/> provides global hotkeys for the application.
  616. /// </para>
  617. /// </remarks>
  618. public class MenuBar : View {
  619. /// <summary>
  620. /// Gets or sets the array of <see cref="MenuBarItem"/>s for the menu. Only set this when the <see cref="MenuBar"/> is vislble.
  621. /// </summary>
  622. /// <value>The menu array.</value>
  623. public MenuBarItem [] Menus { get; set; }
  624. internal int selected;
  625. internal int selectedSub;
  626. Action action;
  627. /// <summary>
  628. /// Used for change the navigation key style.
  629. /// </summary>
  630. public bool UseKeysUpDownAsKeysLeftRight { get; set; } = true;
  631. /// <summary>
  632. /// Initializes a new instance of the <see cref="MenuBar"/>.
  633. /// </summary>
  634. public MenuBar () : this (new MenuBarItem [] { }) { }
  635. /// <summary>
  636. /// Initializes a new instance of the <see cref="MenuBar"/> class with the specified set of toplevel menu items.
  637. /// </summary>
  638. /// <param name="menus">Individual menu items; a null item will result in a separator being drawn.</param>
  639. public MenuBar (MenuBarItem [] menus) : base ()
  640. {
  641. X = 0;
  642. Y = 0;
  643. Width = Dim.Fill ();
  644. Height = 1;
  645. Menus = menus;
  646. //CanFocus = true;
  647. selected = -1;
  648. selectedSub = -1;
  649. ColorScheme = Colors.Menu;
  650. WantMousePositionReports = true;
  651. IsMenuOpen = false;
  652. }
  653. bool openedByAltKey;
  654. bool isCleaning;
  655. ///<inheritdoc/>
  656. public override bool OnLeave (View view)
  657. {
  658. if ((!(view is MenuBar) && !(view is Menu) || !(view is MenuBar) && !(view is Menu) && openMenu != null) && !isCleaning && !reopen) {
  659. CleanUp ();
  660. return true;
  661. }
  662. return false;
  663. }
  664. ///<inheritdoc/>
  665. public override bool OnKeyDown (KeyEvent keyEvent)
  666. {
  667. if (keyEvent.IsAlt) {
  668. openedByAltKey = true;
  669. SetNeedsDisplay ();
  670. openedByHotKey = false;
  671. }
  672. return false;
  673. }
  674. ///<inheritdoc/>
  675. public override bool OnKeyUp (KeyEvent keyEvent)
  676. {
  677. if (keyEvent.IsAlt) {
  678. // User pressed Alt - this may be a precursor to a menu accelerator (e.g. Alt-F)
  679. if (!keyEvent.IsCtrl && openedByAltKey && !IsMenuOpen && openMenu == null && ((uint)keyEvent.Key & (uint)Key.CharMask) == 0) {
  680. // There's no open menu, the first menu item should be highlight.
  681. // The right way to do this is to SetFocus(MenuBar), but for some reason
  682. // that faults.
  683. //Activate (0);
  684. //StartMenu ();
  685. IsMenuOpen = true;
  686. selected = 0;
  687. CanFocus = true;
  688. lastFocused = SuperView.MostFocused;
  689. SetFocus ();
  690. SetNeedsDisplay ();
  691. Application.GrabMouse (this);
  692. } else if (!openedByHotKey) {
  693. // There's an open menu. If this Alt key-up is a pre-cursor to an accelerator
  694. // we don't want to close the menu because it'll flash.
  695. // How to deal with that?
  696. CleanUp ();
  697. }
  698. return true;
  699. }
  700. return false;
  701. }
  702. internal void CleanUp ()
  703. {
  704. isCleaning = true;
  705. if (openMenu != null) {
  706. CloseAllMenus ();
  707. }
  708. openedByAltKey = false;
  709. IsMenuOpen = false;
  710. selected = -1;
  711. CanFocus = false;
  712. if (lastFocused != null) {
  713. lastFocused.SetFocus ();
  714. }
  715. SetNeedsDisplay ();
  716. Application.UngrabMouse ();
  717. isCleaning = false;
  718. }
  719. ///<inheritdoc/>
  720. public override void Redraw (Rect bounds)
  721. {
  722. Move (0, 0);
  723. Driver.SetAttribute (Colors.Menu.Normal);
  724. for (int i = 0; i < Frame.Width; i++)
  725. Driver.AddRune (' ');
  726. Move (1, 0);
  727. int pos = 1;
  728. for (int i = 0; i < Menus.Length; i++) {
  729. var menu = Menus [i];
  730. Move (pos, 0);
  731. Attribute hotColor, normalColor;
  732. if (i == selected) {
  733. hotColor = i == selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  734. normalColor = i == selected ? ColorScheme.Focus : ColorScheme.Normal;
  735. } else if (openedByAltKey) {
  736. hotColor = ColorScheme.HotNormal;
  737. normalColor = ColorScheme.Normal;
  738. } else {
  739. hotColor = ColorScheme.Normal;
  740. normalColor = ColorScheme.Normal;
  741. }
  742. DrawHotString ($" {menu.Title} ", hotColor, normalColor);
  743. pos += 1 + menu.TitleLength + 2;
  744. }
  745. PositionCursor ();
  746. }
  747. ///<inheritdoc/>
  748. public override void PositionCursor ()
  749. {
  750. int pos = 0;
  751. for (int i = 0; i < Menus.Length; i++) {
  752. if (i == selected) {
  753. pos++;
  754. if (IsMenuOpen)
  755. Move (pos + 1, 0);
  756. else
  757. Move (pos + 1, 0);
  758. return;
  759. } else {
  760. if (IsMenuOpen)
  761. pos += 1 + Menus [i].TitleLength + 2;
  762. else
  763. pos += 2 + Menus [i].TitleLength + 1;
  764. }
  765. }
  766. //Move (0, 0);
  767. }
  768. void Selected (MenuItem item)
  769. {
  770. // TODO: Running = false;
  771. action = item.Action;
  772. }
  773. /// <summary>
  774. /// Raised as a menu is opening.
  775. /// </summary>
  776. public event Action MenuOpening;
  777. /// <summary>
  778. /// Raised when a menu is closing.
  779. /// </summary>
  780. public event Action MenuClosing;
  781. internal Menu openMenu;
  782. internal Menu openCurrentMenu;
  783. internal List<Menu> openSubMenu;
  784. View previousFocused;
  785. internal bool isMenuOpening;
  786. internal bool isMenuClosing;
  787. /// <summary>
  788. /// True if the menu is open; otherwise false.
  789. /// </summary>
  790. public bool IsMenuOpen { get; protected set; }
  791. /// <summary>
  792. /// Virtual method that will invoke the <see cref="MenuOpening"/>
  793. /// </summary>
  794. public virtual void OnMenuOpening ()
  795. {
  796. MenuOpening?.Invoke ();
  797. }
  798. /// <summary>
  799. /// Virtual method that will invoke the <see cref="MenuClosing"/>
  800. /// </summary>
  801. public virtual void OnMenuClosing ()
  802. {
  803. MenuClosing?.Invoke ();
  804. }
  805. View lastFocused;
  806. /// <summary>
  807. /// Get the lasted focused view before open the menu.
  808. /// </summary>
  809. public View LastFocused { get; private set; }
  810. internal void OpenMenu (int index, int sIndex = -1, MenuBarItem subMenu = null)
  811. {
  812. isMenuOpening = true;
  813. OnMenuOpening ();
  814. int pos = 0;
  815. switch (subMenu) {
  816. case null:
  817. lastFocused = lastFocused ?? SuperView.MostFocused;
  818. if (openSubMenu != null)
  819. CloseMenu (false, true);
  820. if (openMenu != null) {
  821. SuperView.Remove (openMenu);
  822. openMenu.Dispose ();
  823. }
  824. for (int i = 0; i < index; i++)
  825. pos += Menus [i].Title.RuneCount + 2;
  826. openMenu = new Menu (this, pos, 1, Menus [index]);
  827. openCurrentMenu = openMenu;
  828. openCurrentMenu.previousSubFocused = openMenu;
  829. SuperView.Add (openMenu);
  830. openMenu.SetFocus ();
  831. break;
  832. default:
  833. if (openSubMenu == null)
  834. openSubMenu = new List<Menu> ();
  835. if (sIndex > -1) {
  836. RemoveSubMenu (sIndex);
  837. } else {
  838. var last = openSubMenu.Count > 0 ? openSubMenu.Last () : openMenu;
  839. openCurrentMenu = new Menu (this, last.Frame.Left + last.Frame.Width, last.Frame.Top + 1 + last.current, subMenu);
  840. openCurrentMenu.previousSubFocused = last.previousSubFocused;
  841. openSubMenu.Add (openCurrentMenu);
  842. SuperView.Add (openCurrentMenu);
  843. }
  844. selectedSub = openSubMenu.Count - 1;
  845. if (selectedSub > -1 && SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  846. openCurrentMenu.SetFocus ();
  847. }
  848. break;
  849. }
  850. isMenuOpening = false;
  851. IsMenuOpen = true;
  852. }
  853. /// <summary>
  854. /// Opens the current Menu programatically.
  855. /// </summary>
  856. public void OpenMenu ()
  857. {
  858. if (openMenu != null)
  859. return;
  860. selected = 0;
  861. SetNeedsDisplay ();
  862. previousFocused = SuperView.Focused;
  863. OpenMenu (selected);
  864. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  865. CloseMenu ();
  866. }
  867. openCurrentMenu.CheckSubMenu ();
  868. Application.GrabMouse (this);
  869. }
  870. // Activates the menu, handles either first focus, or activating an entry when it was already active
  871. // For mouse events.
  872. internal void Activate (int idx, int sIdx = -1, MenuBarItem subMenu = null)
  873. {
  874. selected = idx;
  875. selectedSub = sIdx;
  876. if (openMenu == null)
  877. previousFocused = SuperView.Focused;
  878. OpenMenu (idx, sIdx, subMenu);
  879. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  880. if (subMenu == null) {
  881. CloseMenu ();
  882. }
  883. }
  884. SetNeedsDisplay ();
  885. }
  886. internal bool SelectEnabledItem (IEnumerable<MenuItem> chldren, int current, out int newCurrent, bool forward = true)
  887. {
  888. if (chldren == null) {
  889. newCurrent = -1;
  890. return true;
  891. }
  892. IEnumerable<MenuItem> childrens;
  893. if (forward) {
  894. childrens = chldren;
  895. } else {
  896. childrens = chldren.Reverse ();
  897. }
  898. int count;
  899. if (forward) {
  900. count = -1;
  901. } else {
  902. count = childrens.Count ();
  903. }
  904. foreach (var child in childrens) {
  905. if (forward) {
  906. if (++count < current) {
  907. continue;
  908. }
  909. } else {
  910. if (--count > current) {
  911. continue;
  912. }
  913. }
  914. if (child == null || !child.IsEnabled ()) {
  915. if (forward) {
  916. current++;
  917. } else {
  918. current--;
  919. }
  920. } else {
  921. newCurrent = current;
  922. return true;
  923. }
  924. }
  925. newCurrent = -1;
  926. return false;
  927. }
  928. /// <summary>
  929. /// Closes the current Menu programatically, if open.
  930. /// </summary>
  931. public void CloseMenu ()
  932. {
  933. CloseMenu (false, false);
  934. }
  935. bool reopen;
  936. internal void CloseMenu (bool reopen = false, bool isSubMenu = false)
  937. {
  938. isMenuClosing = true;
  939. this.reopen = reopen;
  940. OnMenuClosing ();
  941. switch (isSubMenu) {
  942. case false:
  943. if (openMenu != null) {
  944. SuperView?.Remove (openMenu);
  945. }
  946. SetNeedsDisplay ();
  947. if (previousFocused != null && previousFocused is Menu && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
  948. previousFocused.SetFocus ();
  949. openMenu?.Dispose ();
  950. openMenu = null;
  951. if (lastFocused is Menu || lastFocused is MenuBar) {
  952. lastFocused = null;
  953. }
  954. LastFocused = lastFocused;
  955. lastFocused = null;
  956. if (LastFocused != null) {
  957. if (!reopen) {
  958. selected = -1;
  959. }
  960. LastFocused.SetFocus ();
  961. } else {
  962. SetFocus ();
  963. PositionCursor ();
  964. }
  965. IsMenuOpen = false;
  966. break;
  967. case true:
  968. selectedSub = -1;
  969. SetNeedsDisplay ();
  970. RemoveAllOpensSubMenus ();
  971. openCurrentMenu.previousSubFocused.SetFocus ();
  972. openSubMenu = null;
  973. IsMenuOpen = true;
  974. break;
  975. }
  976. this.reopen = false;
  977. isMenuClosing = false;
  978. }
  979. void RemoveSubMenu (int index)
  980. {
  981. if (openSubMenu == null)
  982. return;
  983. for (int i = openSubMenu.Count - 1; i > index; i--) {
  984. isMenuClosing = true;
  985. if (openSubMenu.Count - 1 > 0)
  986. openSubMenu [i - 1].SetFocus ();
  987. else
  988. openMenu.SetFocus ();
  989. if (openSubMenu != null) {
  990. var menu = openSubMenu [i];
  991. SuperView.Remove (menu);
  992. openSubMenu.Remove (menu);
  993. menu.Dispose ();
  994. }
  995. RemoveSubMenu (i);
  996. }
  997. if (openSubMenu.Count > 0)
  998. openCurrentMenu = openSubMenu.Last ();
  999. //if (openMenu.Subviews.Count == 0)
  1000. // return;
  1001. //if (index == 0) {
  1002. // //SuperView.SetFocus (previousSubFocused);
  1003. // FocusPrev ();
  1004. // return;
  1005. //}
  1006. //for (int i = openMenu.Subviews.Count - 1; i > index; i--) {
  1007. // isMenuClosing = true;
  1008. // if (openMenu.Subviews.Count - 1 > 0)
  1009. // SuperView.SetFocus (openMenu.Subviews [i - 1]);
  1010. // else
  1011. // SuperView.SetFocus (openMenu);
  1012. // if (openMenu != null) {
  1013. // Remove (openMenu.Subviews [i]);
  1014. // openMenu.Remove (openMenu.Subviews [i]);
  1015. // }
  1016. // RemoveSubMenu (i);
  1017. //}
  1018. isMenuClosing = false;
  1019. }
  1020. internal void RemoveAllOpensSubMenus ()
  1021. {
  1022. if (openSubMenu != null) {
  1023. foreach (var item in openSubMenu) {
  1024. SuperView.Remove (item);
  1025. item.Dispose ();
  1026. }
  1027. }
  1028. }
  1029. internal void CloseAllMenus ()
  1030. {
  1031. if (!isMenuOpening && !isMenuClosing) {
  1032. if (openSubMenu != null)
  1033. CloseMenu (false, true);
  1034. CloseMenu ();
  1035. if (LastFocused != null && LastFocused != this)
  1036. selected = -1;
  1037. }
  1038. IsMenuOpen = false;
  1039. openedByHotKey = false;
  1040. openedByAltKey = false;
  1041. }
  1042. View FindDeepestMenu (View view, ref int count)
  1043. {
  1044. count = count > 0 ? count : 0;
  1045. foreach (var menu in view.Subviews) {
  1046. if (menu is Menu) {
  1047. count++;
  1048. return FindDeepestMenu ((Menu)menu, ref count);
  1049. }
  1050. }
  1051. return view;
  1052. }
  1053. internal void PreviousMenu (bool isSubMenu = false)
  1054. {
  1055. switch (isSubMenu) {
  1056. case false:
  1057. if (selected <= 0)
  1058. selected = Menus.Length - 1;
  1059. else
  1060. selected--;
  1061. if (selected > -1)
  1062. CloseMenu (true, false);
  1063. OpenMenu (selected);
  1064. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current, false)) {
  1065. openCurrentMenu.current = 0;
  1066. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1067. CloseMenu ();
  1068. }
  1069. break;
  1070. }
  1071. break;
  1072. case true:
  1073. if (selectedSub > -1) {
  1074. selectedSub--;
  1075. RemoveSubMenu (selectedSub);
  1076. SetNeedsDisplay ();
  1077. } else
  1078. PreviousMenu ();
  1079. break;
  1080. }
  1081. }
  1082. internal void NextMenu (bool isSubMenu = false)
  1083. {
  1084. switch (isSubMenu) {
  1085. case false:
  1086. if (selected == -1)
  1087. selected = 0;
  1088. else if (selected + 1 == Menus.Length)
  1089. selected = 0;
  1090. else
  1091. selected++;
  1092. if (selected > -1)
  1093. CloseMenu (true);
  1094. OpenMenu (selected);
  1095. SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current);
  1096. break;
  1097. case true:
  1098. if (UseKeysUpDownAsKeysLeftRight) {
  1099. CloseMenu (false, true);
  1100. NextMenu ();
  1101. } else {
  1102. var subMenu = openCurrentMenu.barItems.SubMenu (openCurrentMenu.barItems.Children [openCurrentMenu.current]);
  1103. if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && subMenu == null) {
  1104. if (openSubMenu != null)
  1105. CloseMenu (false, true);
  1106. NextMenu ();
  1107. } else if (subMenu != null ||
  1108. !openCurrentMenu.barItems.Children [openCurrentMenu.current].IsFromSubMenu)
  1109. selectedSub++;
  1110. else
  1111. return;
  1112. SetNeedsDisplay ();
  1113. openCurrentMenu.CheckSubMenu ();
  1114. }
  1115. break;
  1116. }
  1117. }
  1118. bool openedByHotKey;
  1119. internal bool FindAndOpenMenuByHotkey (KeyEvent kb)
  1120. {
  1121. //int pos = 0;
  1122. var c = ((uint)kb.Key & (uint)Key.CharMask);
  1123. for (int i = 0; i < Menus.Length; i++) {
  1124. // TODO: this code is duplicated, hotkey should be part of the MenuBarItem
  1125. var mi = Menus [i];
  1126. int p = mi.Title.IndexOf ('_');
  1127. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1128. if (Char.ToUpperInvariant ((char)mi.Title [p + 1]) == c) {
  1129. ProcessMenu (i, mi);
  1130. return true;
  1131. }
  1132. }
  1133. }
  1134. return false;
  1135. }
  1136. private void ProcessMenu (int i, MenuBarItem mi)
  1137. {
  1138. if (mi.IsTopLevel) {
  1139. var menu = new Menu (this, i, 0, mi);
  1140. menu.Run (mi.Action);
  1141. menu.Dispose ();
  1142. } else {
  1143. openedByHotKey = true;
  1144. Application.GrabMouse (this);
  1145. selected = i;
  1146. OpenMenu (i);
  1147. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1148. CloseMenu ();
  1149. }
  1150. openCurrentMenu.CheckSubMenu ();
  1151. }
  1152. }
  1153. ///<inheritdoc/>
  1154. public override bool ProcessHotKey (KeyEvent kb)
  1155. {
  1156. if (kb.Key == Key.F9) {
  1157. if (!IsMenuOpen)
  1158. OpenMenu ();
  1159. else
  1160. CloseAllMenus ();
  1161. return true;
  1162. }
  1163. // To ncurses simulate a AltMask key pressing Alt+Space because
  1164. // it can�t detect an alone special key down was pressed.
  1165. if (kb.IsAlt && kb.Key == Key.AltMask && openMenu == null) {
  1166. OnKeyDown (kb);
  1167. OnKeyUp (kb);
  1168. return true;
  1169. } else if (kb.IsAlt) {
  1170. if (FindAndOpenMenuByHotkey (kb)) return true;
  1171. }
  1172. //var kc = kb.KeyValue;
  1173. return base.ProcessHotKey (kb);
  1174. }
  1175. ///<inheritdoc/>
  1176. public override bool ProcessKey (KeyEvent kb)
  1177. {
  1178. switch (kb.Key) {
  1179. case Key.CursorLeft:
  1180. selected--;
  1181. if (selected < 0)
  1182. selected = Menus.Length - 1;
  1183. break;
  1184. case Key.CursorRight:
  1185. selected = (selected + 1) % Menus.Length;
  1186. break;
  1187. case Key.Esc:
  1188. case Key.ControlC:
  1189. //TODO: Running = false;
  1190. CloseMenu ();
  1191. if (openedByAltKey) {
  1192. openedByAltKey = false;
  1193. LastFocused.SetFocus ();
  1194. }
  1195. break;
  1196. case Key.CursorDown:
  1197. case Key.Enter:
  1198. if (selected > -1) {
  1199. ProcessMenu (selected, Menus [selected]);
  1200. }
  1201. break;
  1202. default:
  1203. var key = kb.KeyValue;
  1204. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  1205. char c = Char.ToUpper ((char)key);
  1206. if (selected == -1 || Menus [selected].IsTopLevel)
  1207. return false;
  1208. foreach (var mi in Menus [selected].Children) {
  1209. if (mi == null)
  1210. continue;
  1211. int p = mi.Title.IndexOf ('_');
  1212. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1213. if (mi.Title [p + 1] == c) {
  1214. Selected (mi);
  1215. return true;
  1216. }
  1217. }
  1218. }
  1219. }
  1220. return false;
  1221. }
  1222. SetNeedsDisplay ();
  1223. return true;
  1224. }
  1225. ///<inheritdoc/>
  1226. public override bool MouseEvent (MouseEvent me)
  1227. {
  1228. if (!handled && !HandleGrabView (me, this)) {
  1229. return false;
  1230. }
  1231. handled = false;
  1232. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.Button1Clicked ||
  1233. (me.Flags == MouseFlags.ReportMousePosition && selected > -1) ||
  1234. (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && selected > -1)) {
  1235. int pos = 1;
  1236. int cx = me.X;
  1237. for (int i = 0; i < Menus.Length; i++) {
  1238. if (cx >= pos && cx < pos + 1 + Menus [i].TitleLength + 2) {
  1239. if (me.Flags == MouseFlags.Button1Clicked) {
  1240. if (Menus [i].IsTopLevel) {
  1241. var menu = new Menu (this, i, 0, Menus [i]);
  1242. menu.Run (Menus [i].Action);
  1243. menu.Dispose ();
  1244. }
  1245. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked) {
  1246. if (IsMenuOpen && !Menus [i].IsTopLevel) {
  1247. CloseAllMenus ();
  1248. } else if (!Menus [i].IsTopLevel) {
  1249. Activate (i);
  1250. }
  1251. } else if (selected != i && selected > -1 && (me.Flags == MouseFlags.ReportMousePosition ||
  1252. me.Flags == MouseFlags.Button1Pressed && me.Flags == MouseFlags.ReportMousePosition)) {
  1253. if (IsMenuOpen) {
  1254. CloseMenu (true, false);
  1255. Activate (i);
  1256. }
  1257. } else {
  1258. if (IsMenuOpen)
  1259. Activate (i);
  1260. }
  1261. return true;
  1262. }
  1263. pos += 1 + Menus [i].TitleLength + 2;
  1264. }
  1265. }
  1266. return false;
  1267. }
  1268. internal bool handled;
  1269. internal bool HandleGrabView (MouseEvent me, View current)
  1270. {
  1271. if (Application.mouseGrabView != null) {
  1272. if (me.View is MenuBar || me.View is Menu) {
  1273. if (me.View != current) {
  1274. Application.UngrabMouse ();
  1275. var v = me.View;
  1276. Application.GrabMouse (v);
  1277. var newxy = v.ScreenToView (me.X, me.Y);
  1278. var nme = new MouseEvent () {
  1279. X = newxy.X,
  1280. Y = newxy.Y,
  1281. Flags = me.Flags,
  1282. OfX = me.X - newxy.X,
  1283. OfY = me.Y - newxy.Y,
  1284. View = v
  1285. };
  1286. v.MouseEvent (nme);
  1287. }
  1288. } else if (!(me.View is MenuBar || me.View is Menu) && (me.Flags.HasFlag (MouseFlags.Button1Clicked) ||
  1289. me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked)) {
  1290. Application.UngrabMouse ();
  1291. CloseAllMenus ();
  1292. handled = false;
  1293. return false;
  1294. } else {
  1295. handled = false;
  1296. return false;
  1297. }
  1298. } else if (!IsMenuOpen && (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  1299. Application.GrabMouse (current);
  1300. } else if (IsMenuOpen && (me.View is MenuBar || me.View is Menu)) {
  1301. Application.GrabMouse (me.View);
  1302. } else {
  1303. handled = false;
  1304. return false;
  1305. }
  1306. //if (me.View != this && me.Flags != MouseFlags.Button1Pressed)
  1307. // return true;
  1308. //else if (me.View != this && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1309. // Application.UngrabMouse ();
  1310. // host.CloseAllMenus ();
  1311. // return true;
  1312. //}
  1313. //if (!(me.View is MenuBar) && !(me.View is Menu) && me.Flags != MouseFlags.Button1Pressed))
  1314. // return false;
  1315. //if (Application.mouseGrabView != null) {
  1316. // if (me.View is MenuBar || me.View is Menu) {
  1317. // me.X -= me.OfX;
  1318. // me.Y -= me.OfY;
  1319. // me.View.MouseEvent (me);
  1320. // return true;
  1321. // } else if (!(me.View is MenuBar || me.View is Menu) && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1322. // Application.UngrabMouse ();
  1323. // CloseAllMenus ();
  1324. // }
  1325. //} else if (!isMenuClosed && selected == -1 && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1326. // Application.GrabMouse (this);
  1327. // return true;
  1328. //}
  1329. //if (Application.mouseGrabView != null) {
  1330. // if (Application.mouseGrabView == me.View && me.View == current) {
  1331. // me.X -= me.OfX;
  1332. // me.Y -= me.OfY;
  1333. // } else if (me.View != current && me.View is MenuBar && me.View is Menu) {
  1334. // Application.UngrabMouse ();
  1335. // Application.GrabMouse (me.View);
  1336. // } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1337. // Application.UngrabMouse ();
  1338. // CloseMenu ();
  1339. // }
  1340. //} else if ((!isMenuClosed && selected > -1)) {
  1341. // Application.GrabMouse (current);
  1342. //}
  1343. handled = true;
  1344. return true;
  1345. }
  1346. }
  1347. }