Menu.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. using System.Reflection;
  15. namespace Terminal.Gui {
  16. /// <summary>
  17. /// A menu item has a title, an associated help text, and an action to execute on activation.
  18. /// </summary>
  19. public class MenuItem {
  20. public MenuItem ()
  21. {
  22. Title = "";
  23. Help = "";
  24. }
  25. /// <summary>
  26. /// Initializes a new <see cref="T:Terminal.Gui.MenuItem"/>.
  27. /// </summary>
  28. /// <param name="title">Title for the menu item.</param>
  29. /// <param name="help">Help text to display.</param>
  30. /// <param name="action">Action to invoke when the menu item is activated.</param>
  31. /// <param name="canExecute">Function to determine if the action can currently be executred.</param>
  32. public MenuItem (ustring title, string help, Action action, Func<bool> canExecute = null)
  33. {
  34. Title = title ?? "";
  35. Help = help ?? "";
  36. Action = action;
  37. CanExecute = canExecute;
  38. bool nextIsHot = false;
  39. foreach (var x in Title) {
  40. if (x == '_')
  41. nextIsHot = true;
  42. else {
  43. if (nextIsHot) {
  44. HotKey = Char.ToUpper ((char)x);
  45. break;
  46. }
  47. nextIsHot = false;
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// Initializes a new <see cref="T:Terminal.Gui.MenuItem"/>.
  53. /// </summary>
  54. /// <param name="title">Title for the menu item.</param>
  55. /// <param name="subMenu">The menu sub-menu.</param>
  56. public MenuItem (ustring title, MenuBarItem subMenu) : this (title, "", null)
  57. {
  58. SubMenu = subMenu;
  59. IsFromSubMenu = true;
  60. }
  61. //
  62. //
  63. /// <summary>
  64. /// The hotkey is used when the menu is active, the shortcut can be triggered when the menu is not active.
  65. /// For example HotKey would be "N" when the File Menu is open (assuming there is a "_New" entry
  66. /// if the ShortCut is set to "Control-N", this would be a global hotkey that would trigger as well
  67. /// </summary>
  68. public Rune HotKey;
  69. /// <summary>
  70. /// This is the global setting that can be used as a global shortcut to invoke the action on the menu.
  71. /// </summary>
  72. public Key ShortCut;
  73. /// <summary>
  74. /// Gets or sets the title.
  75. /// </summary>
  76. /// <value>The title.</value>
  77. public ustring Title { get; set; }
  78. /// <summary>
  79. /// Gets or sets the help text for the menu item.
  80. /// </summary>
  81. /// <value>The help text.</value>
  82. public ustring Help { get; set; }
  83. /// <summary>
  84. /// Gets or sets the action to be invoked when the menu is triggered
  85. /// </summary>
  86. /// <value>Method to invoke.</value>
  87. public Action Action { get; set; }
  88. /// <summary>
  89. /// Gets or sets the action to be invoked if the menu can be triggered
  90. /// </summary>
  91. /// <value>Function to determine if action is ready to be executed.</value>
  92. public Func<bool> CanExecute { get; set; }
  93. /// <summary>
  94. /// Shortcut to check if the menu item is enabled
  95. /// </summary>
  96. public bool IsEnabled ()
  97. {
  98. return CanExecute == null ? true : CanExecute ();
  99. }
  100. internal int Width => Title.Length + Help.Length + 1 + 2;
  101. /// <summary>
  102. /// Gets or sets the parent for this MenuBarItem
  103. /// </summary>
  104. /// <value>The parent.</value>
  105. internal MenuBarItem SubMenu { get; set; }
  106. internal bool IsFromSubMenu { get; set; }
  107. /// <summary>
  108. /// Merely a debugging aid to see the interaction with main
  109. /// </summary>
  110. public MenuItem GetMenuItem ()
  111. {
  112. return this;
  113. }
  114. /// <summary>
  115. /// Merely a debugging aid to see the interaction with main
  116. /// </summary>
  117. public bool GetMenuBarItem ()
  118. {
  119. return IsFromSubMenu;
  120. }
  121. }
  122. /// <summary>
  123. /// A menu bar item contains other menu items.
  124. /// </summary>
  125. public class MenuBarItem : MenuItem {
  126. /// <summary>
  127. /// Initializes a new <see cref="T:Terminal.Gui.MenuBarItem"/> as a <see cref="T:Terminal.Gui.MenuItem"/>.
  128. /// </summary>
  129. /// <param name="title">Title for the menu item.</param>
  130. /// <param name="help">Help text to display.</param>
  131. /// <param name="action">Action to invoke when the menu item is activated.</param>
  132. /// <param name="canExecute">Function to determine if the action can currently be executred.</param>
  133. public MenuBarItem (ustring title, string help, Action action, Func<bool> canExecute = null) : base (title, help, action, canExecute)
  134. {
  135. SetTitle (title ?? "");
  136. Children = null;
  137. }
  138. /// <summary>
  139. /// Initializes a new <see cref="T:Terminal.Gui.MenuBarItem"/>.
  140. /// </summary>
  141. /// <param name="title">Title for the menu item.</param>
  142. /// <param name="children">The items in the current menu.</param>
  143. public MenuBarItem (ustring title, MenuItem [] children)
  144. {
  145. SetTitle (title ?? "");
  146. Children = children;
  147. }
  148. /// <summary>
  149. /// Initializes a new <see cref="T:Terminal.Gui.MenuBarItem"/>.
  150. /// </summary>
  151. /// <param name="children">The items in the current menu.</param>
  152. public MenuBarItem (MenuItem [] children) : this (new string (' ', GetMaxTitleLength (children)), children)
  153. {
  154. }
  155. static int GetMaxTitleLength (MenuItem [] children)
  156. {
  157. int maxLength = 0;
  158. foreach (var item in children) {
  159. int len = GetMenuBarItemLength (item.Title);
  160. if (len > maxLength)
  161. maxLength = len;
  162. item.IsFromSubMenu = true;
  163. }
  164. return maxLength;
  165. }
  166. void SetTitle (ustring title)
  167. {
  168. if (title == null)
  169. title = "";
  170. Title = title;
  171. TitleLength = GetMenuBarItemLength (Title);
  172. }
  173. static int GetMenuBarItemLength (ustring title)
  174. {
  175. int len = 0;
  176. foreach (var ch in title) {
  177. if (ch == '_')
  178. continue;
  179. len++;
  180. }
  181. return len;
  182. }
  183. /// <summary>
  184. /// Gets or sets the title to display.
  185. /// </summary>
  186. /// <value>The title.</value>
  187. //public ustring Title { get; set; }
  188. /// <summary>
  189. /// Gets or sets the children for this MenuBarItem
  190. /// </summary>
  191. /// <value>The children.</value>
  192. public MenuItem [] Children { get; set; }
  193. internal int TitleLength { get; private set; }
  194. internal bool IsTopLevel { get => (Children == null || Children.Length == 0); }
  195. }
  196. class Menu : View {
  197. internal MenuBarItem barItems;
  198. MenuBar host;
  199. internal int current;
  200. internal View previousSubFocused;
  201. static Rect MakeFrame (int x, int y, MenuItem [] items)
  202. {
  203. if (items == null || items.Length == 0) {
  204. return new Rect ();
  205. }
  206. int maxW = items.Max (z => z?.Width) ?? 0;
  207. return new Rect (x, y, maxW + 2, items.Length + 2);
  208. }
  209. public Menu (MenuBar host, int x, int y, MenuBarItem barItems) : base (MakeFrame (x, y, barItems.Children))
  210. {
  211. this.barItems = barItems;
  212. this.host = host;
  213. if (barItems.IsTopLevel) {
  214. // This is a standalone MenuItem on a MenuBar
  215. ColorScheme = Colors.Menu;
  216. CanFocus = true;
  217. } else {
  218. current = -1;
  219. for (int i = 0; i < barItems.Children.Length; i++) {
  220. if (barItems.Children [i] != null) {
  221. current = i;
  222. break;
  223. }
  224. }
  225. ColorScheme = Colors.Menu;
  226. CanFocus = true;
  227. WantMousePositionReports = host.WantMousePositionReports;
  228. }
  229. }
  230. internal Attribute DetermineColorSchemeFor (MenuItem item, int index)
  231. {
  232. if (item != null) {
  233. if (index == current) return ColorScheme.Focus;
  234. if (!item.IsEnabled ()) return ColorScheme.Disabled;
  235. }
  236. return ColorScheme.Normal;
  237. }
  238. public override void Redraw (Rect region)
  239. {
  240. Driver.SetAttribute (ColorScheme.Normal);
  241. DrawFrame (region, padding: 0, fill: true);
  242. for (int i = 0; i < barItems.Children.Length; i++) {
  243. var item = barItems.Children [i];
  244. Driver.SetAttribute (item == null ? ColorScheme.Normal : i == current ? ColorScheme.Focus : ColorScheme.Normal);
  245. if (item == null) {
  246. Move (0, i + 1);
  247. Driver.AddRune (Driver.LeftTee);
  248. } else
  249. Move (1, i + 1);
  250. Driver.SetAttribute (DetermineColorSchemeFor (item, i));
  251. for (int p = 0; p < Frame.Width - 2; p++)
  252. if (item == null)
  253. Driver.AddRune (Driver.HLine);
  254. else if (p == Frame.Width - 3 && barItems.Children [i].SubMenu != null)
  255. Driver.AddRune ('>');
  256. else
  257. Driver.AddRune (' ');
  258. if (item == null) {
  259. Move (Frame.Right - 1, i + 1);
  260. Driver.AddRune (Driver.RightTee);
  261. continue;
  262. }
  263. Move (2, i + 1);
  264. if (!item.IsEnabled ())
  265. DrawHotString (item.Title, ColorScheme.Disabled, ColorScheme.Disabled);
  266. else
  267. DrawHotString (item.Title,
  268. i == current ? ColorScheme.HotFocus : ColorScheme.HotNormal,
  269. i == current ? ColorScheme.Focus : ColorScheme.Normal);
  270. // The help string
  271. var l = item.Help.Length;
  272. Move (Frame.Width - l - 2, 1 + i);
  273. Driver.AddStr (item.Help);
  274. }
  275. PositionCursor ();
  276. }
  277. public override void PositionCursor ()
  278. {
  279. if (host == null || !host.isMenuClosed)
  280. if (barItems.IsTopLevel) {
  281. host.PositionCursor ();
  282. } else
  283. Move (2, 1 + current);
  284. else
  285. host.PositionCursor ();
  286. }
  287. public void Run (Action action)
  288. {
  289. if (action == null)
  290. return;
  291. Application.UngrabMouse ();
  292. host.CloseAllMenus ();
  293. Application.Refresh ();
  294. Application.MainLoop.AddIdle (() => {
  295. action ();
  296. return false;
  297. });
  298. }
  299. public override bool ProcessKey (KeyEvent kb)
  300. {
  301. bool disabled;
  302. switch (kb.Key) {
  303. case Key.CursorUp:
  304. if (barItems.IsTopLevel || current == -1)
  305. break;
  306. do {
  307. disabled = false;
  308. current--;
  309. if (host.UseKeysUpDownAsKeysLeftRight) {
  310. if (current == -1 && barItems.Children [current + 1].IsFromSubMenu && host.selectedSub > -1) {
  311. current++;
  312. host.PreviousMenu (true);
  313. break;
  314. }
  315. }
  316. if (current < 0)
  317. current = barItems.Children.Length - 1;
  318. var item = barItems.Children [current];
  319. if (item == null || !item.IsEnabled ()) disabled = true;
  320. } while (barItems.Children [current] == null || disabled);
  321. SetNeedsDisplay ();
  322. break;
  323. case Key.CursorDown:
  324. if (barItems.IsTopLevel) {
  325. break;
  326. }
  327. do {
  328. current++;
  329. disabled = false;
  330. if (current == barItems.Children.Length)
  331. current = 0;
  332. var item = barItems.Children [current];
  333. if (item == null || !item.IsEnabled ()) disabled = true;
  334. if (host.UseKeysUpDownAsKeysLeftRight && barItems.Children [current]?.SubMenu != null &&
  335. !disabled && !host.isMenuClosed) {
  336. CheckSubMenu ();
  337. break;
  338. }
  339. if (host.isMenuClosed)
  340. host.OpenMenu (host.selected);
  341. } while (barItems.Children [current] == null || disabled);
  342. SetNeedsDisplay ();
  343. break;
  344. case Key.CursorLeft:
  345. host.PreviousMenu (true);
  346. break;
  347. case Key.CursorRight:
  348. host.NextMenu (barItems.IsTopLevel || barItems.Children [current].IsFromSubMenu ? true : false);
  349. break;
  350. case Key.Esc:
  351. Application.UngrabMouse ();
  352. host.CloseAllMenus ();
  353. break;
  354. case Key.Enter:
  355. if (barItems.IsTopLevel) {
  356. Run (barItems.Action);
  357. } else {
  358. CheckSubMenu ();
  359. Run (barItems.Children [current].Action);
  360. }
  361. break;
  362. default:
  363. // TODO: rune-ify
  364. if (barItems.Children != null && Char.IsLetterOrDigit ((char)kb.KeyValue)) {
  365. var x = Char.ToUpper ((char)kb.KeyValue);
  366. foreach (var item in barItems.Children) {
  367. if (item == null) continue;
  368. if (item.IsEnabled () && item.HotKey == x) {
  369. host.CloseMenu ();
  370. Run (item.Action);
  371. return true;
  372. }
  373. }
  374. }
  375. break;
  376. }
  377. return true;
  378. }
  379. public override bool MouseEvent (MouseEvent me)
  380. {
  381. if (!host.handled && !host.HandleGrabView (me, this)) {
  382. return false;
  383. }
  384. host.handled = false;
  385. bool disabled;
  386. if (me.Flags == MouseFlags.Button1Clicked || me.Flags == MouseFlags.Button1Released) {
  387. disabled = false;
  388. if (me.Y < 1)
  389. return true;
  390. var meY = me.Y - 1;
  391. if (meY >= barItems.Children.Length)
  392. return true;
  393. var item = barItems.Children [meY];
  394. if (item == null || !item.IsEnabled ()) disabled = true;
  395. if (item != null && !disabled)
  396. Run (barItems.Children [meY].Action);
  397. return true;
  398. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.ReportMousePosition) {
  399. disabled = false;
  400. if (me.Y < 1)
  401. return true;
  402. if (me.Y - 1 >= barItems.Children.Length)
  403. return true;
  404. var item = barItems.Children [me.Y - 1];
  405. if (item == null || !item.IsEnabled ()) disabled = true;
  406. if (item != null && !disabled)
  407. current = me.Y - 1;
  408. HasFocus = true;
  409. SetNeedsDisplay ();
  410. CheckSubMenu ();
  411. return true;
  412. }
  413. return false;
  414. }
  415. internal void CheckSubMenu ()
  416. {
  417. if (barItems.Children [current] == null)
  418. return;
  419. var subMenu = barItems.Children [current].SubMenu;
  420. if (subMenu != null) {
  421. int pos = -1;
  422. if (host.openSubMenu != null)
  423. pos = host.openSubMenu.FindIndex (o => o?.barItems == subMenu);
  424. host.Activate (host.selected, pos, subMenu);
  425. } else if (host.openSubMenu != null && !barItems.Children [current].IsFromSubMenu)
  426. host.CloseMenu (false, true);
  427. }
  428. int GetSubMenuIndex (MenuBarItem subMenu)
  429. {
  430. int pos = -1;
  431. if (this != null && Subviews.Count > 0) {
  432. Menu v = null;
  433. foreach (var menu in Subviews) {
  434. if (((Menu)menu).barItems == subMenu)
  435. v = (Menu)menu;
  436. }
  437. if (v != null)
  438. pos = Subviews.IndexOf (v);
  439. }
  440. return pos;
  441. }
  442. }
  443. /// <summary>
  444. /// A menu bar for your application.
  445. /// </summary>
  446. public class MenuBar : View {
  447. /// <summary>
  448. /// The menus that were defined when the menubar was created. This can be updated if the menu is not currently visible.
  449. /// </summary>
  450. /// <value>The menu array.</value>
  451. public MenuBarItem [] Menus { get; set; }
  452. internal int selected;
  453. internal int selectedSub;
  454. Action action;
  455. /// <summary>
  456. /// Used for change the navigation key style.
  457. /// </summary>
  458. public bool UseKeysUpDownAsKeysLeftRight { get; set; } = true;
  459. /// <summary>
  460. /// Initializes a new instance of the <see cref="T:Terminal.Gui.MenuBar"/> class with the specified set of toplevel menu items.
  461. /// </summary>
  462. /// <param name="menus">Individual menu items, if one of those contains a null, then a separator is drawn.</param>
  463. public MenuBar (MenuBarItem [] menus) : base ()
  464. {
  465. X = 0;
  466. Y = 0;
  467. Width = Dim.Fill ();
  468. Height = 1;
  469. Menus = menus;
  470. CanFocus = true;
  471. selected = -1;
  472. selectedSub = -1;
  473. ColorScheme = Colors.Menu;
  474. WantMousePositionReports = true;
  475. isMenuClosed = true;
  476. }
  477. public override bool KeyDown (KeyEvent keyEvent)
  478. {
  479. if (keyEvent.IsAlt) {
  480. openedByHotKey = false;
  481. }
  482. return false;
  483. }
  484. /// <summary>
  485. /// Track Alt key-up events. On Windows, when a user releases Alt (without another key), the menu gets focus but doesn't open.
  486. /// We mimic that behavior here.
  487. /// </summary>
  488. /// <param name="keyEvent"></param>
  489. /// <returns></returns>
  490. public override bool KeyUp (KeyEvent keyEvent)
  491. {
  492. if (keyEvent.IsAlt) {
  493. // User pressed Alt - this may be a precursor to a menu accellerator (e.g. Alt-F)
  494. if (openMenu == null) {
  495. // There's no open menu, the first menu item should be highlight.
  496. // The right way to do this is to SetFocus(MenuBar), but for some reason
  497. // that faults.
  498. Activate (0);
  499. SetNeedsDisplay ();
  500. } else {
  501. // There's an open menu. If this Alt key-up is a pre-cursor to an acellerator
  502. // we don't want to close the menu because it'll flash.
  503. // How to deal with that?
  504. if (!openedByHotKey) {
  505. CloseAllMenus ();
  506. }
  507. }
  508. return true;
  509. }
  510. return false;
  511. }
  512. public override void Redraw (Rect region)
  513. {
  514. Move (0, 0);
  515. Driver.SetAttribute (Colors.Menu.Normal);
  516. for (int i = 0; i < Frame.Width; i++)
  517. Driver.AddRune (' ');
  518. Move (1, 0);
  519. int pos = 1;
  520. for (int i = 0; i < Menus.Length; i++) {
  521. var menu = Menus [i];
  522. Move (pos, 0);
  523. Attribute hotColor, normalColor;
  524. if (i == selected) {
  525. hotColor = i == selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  526. normalColor = i == selected ? ColorScheme.Focus : ColorScheme.Normal;
  527. } else {
  528. hotColor = ColorScheme.HotNormal;
  529. normalColor = ColorScheme.Normal;
  530. }
  531. DrawHotString ($" {menu.Title} ", hotColor, normalColor);
  532. pos += 1 + menu.TitleLength + 2;
  533. }
  534. PositionCursor ();
  535. }
  536. public override void PositionCursor ()
  537. {
  538. int pos = 0;
  539. for (int i = 0; i < Menus.Length; i++) {
  540. if (i == selected) {
  541. pos++;
  542. if (!isMenuClosed)
  543. Move (pos + 1, 0);
  544. else
  545. Move (pos + 1, 0);
  546. return;
  547. } else {
  548. if (!isMenuClosed)
  549. pos += 1 + Menus [i].TitleLength + 2;
  550. else
  551. pos += 2 + Menus [i].TitleLength + 1;
  552. }
  553. }
  554. //Move (0, 0);
  555. }
  556. void Selected (MenuItem item)
  557. {
  558. // TODO: Running = false;
  559. action = item.Action;
  560. }
  561. public event EventHandler OnOpenMenu;
  562. public event EventHandler OnCloseMenu;
  563. internal Menu openMenu;
  564. Menu openCurrentMenu;
  565. internal List<Menu> openSubMenu;
  566. View previousFocused;
  567. internal bool isMenuOpening;
  568. internal bool isMenuClosing;
  569. internal bool isMenuClosed;
  570. public bool MenuOpen;
  571. View lastFocused;
  572. /// <summary>
  573. /// Get the lasted focused view before open the menu.
  574. /// </summary>
  575. public View LastFocused { get; private set; }
  576. internal void OpenMenu (int index, int sIndex = -1, MenuBarItem subMenu = null)
  577. {
  578. isMenuOpening = true;
  579. OnOpenMenu?.Invoke (this, null);
  580. int pos = 0;
  581. switch (subMenu) {
  582. case null:
  583. lastFocused = lastFocused ?? SuperView.MostFocused;
  584. if (openSubMenu != null)
  585. CloseMenu (false, true);
  586. if (openMenu != null)
  587. SuperView.Remove (openMenu);
  588. for (int i = 0; i < index; i++)
  589. pos += Menus [i].Title.Length + 2;
  590. openMenu = new Menu (this, pos, 1, Menus [index]);
  591. openCurrentMenu = openMenu;
  592. openCurrentMenu.previousSubFocused = openMenu;
  593. SuperView.Add (openMenu);
  594. SuperView.SetFocus (openMenu);
  595. break;
  596. default:
  597. if (openSubMenu == null)
  598. openSubMenu = new List<Menu> ();
  599. if (sIndex > -1) {
  600. RemoveSubMenu (sIndex);
  601. } else {
  602. var last = openSubMenu.Count > 0 ? openSubMenu.Last () : openMenu;
  603. openCurrentMenu = new Menu (this, last.Frame.Left + last.Frame.Width, last.Frame.Top + 1 + last.current, subMenu);
  604. openCurrentMenu.previousSubFocused = last.previousSubFocused;
  605. openSubMenu.Add (openCurrentMenu);
  606. SuperView.Add (openCurrentMenu);
  607. }
  608. selectedSub = openSubMenu.Count - 1;
  609. SuperView?.SetFocus (openCurrentMenu);
  610. break;
  611. }
  612. isMenuOpening = false;
  613. isMenuClosed = false;
  614. MenuOpen = true;
  615. }
  616. // Starts the menu from a hotkey
  617. void StartMenu ()
  618. {
  619. if (openMenu != null)
  620. return;
  621. selected = 0;
  622. SetNeedsDisplay ();
  623. previousFocused = SuperView.Focused;
  624. OpenMenu (selected);
  625. Application.GrabMouse (this);
  626. }
  627. // Activates the menu, handles either first focus, or activating an entry when it was already active
  628. // For mouse events.
  629. internal void Activate (int idx, int sIdx = -1, MenuBarItem subMenu = null)
  630. {
  631. selected = idx;
  632. selectedSub = sIdx;
  633. if (openMenu == null)
  634. previousFocused = SuperView.Focused;
  635. OpenMenu (idx, sIdx, subMenu);
  636. SetNeedsDisplay ();
  637. }
  638. internal void CloseMenu (bool reopen = false, bool isSubMenu = false)
  639. {
  640. isMenuClosing = true;
  641. OnCloseMenu?.Invoke (this, null);
  642. switch (isSubMenu) {
  643. case false:
  644. if (openMenu != null)
  645. SuperView.Remove (openMenu);
  646. SetNeedsDisplay ();
  647. if (previousFocused != null && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
  648. previousFocused?.SuperView?.SetFocus (previousFocused);
  649. openMenu = null;
  650. if (lastFocused is Menu) {
  651. lastFocused = null;
  652. }
  653. LastFocused = lastFocused;
  654. lastFocused = null;
  655. if (LastFocused != null) {
  656. if (!reopen)
  657. selected = -1;
  658. LastFocused.SuperView?.SetFocus (LastFocused);
  659. } else {
  660. SuperView.SetFocus (this);
  661. isMenuClosed = true;
  662. PositionCursor ();
  663. }
  664. isMenuClosed = true;
  665. break;
  666. case true:
  667. selectedSub = -1;
  668. SetNeedsDisplay ();
  669. RemoveAllOpensSubMenus ();
  670. openCurrentMenu.previousSubFocused?.SuperView?.SetFocus (openCurrentMenu.previousSubFocused);
  671. openSubMenu = null;
  672. break;
  673. }
  674. isMenuClosing = false;
  675. MenuOpen = false;
  676. }
  677. void RemoveSubMenu (int index)
  678. {
  679. if (openSubMenu == null)
  680. return;
  681. for (int i = openSubMenu.Count - 1; i > index; i--) {
  682. isMenuClosing = true;
  683. if (openSubMenu.Count - 1 > 0)
  684. SuperView.SetFocus (openSubMenu [i - 1]);
  685. else
  686. SuperView.SetFocus (openMenu);
  687. if (openSubMenu != null) {
  688. SuperView.Remove (openSubMenu [i]);
  689. openSubMenu.Remove (openSubMenu [i]);
  690. }
  691. RemoveSubMenu (i);
  692. }
  693. if (openSubMenu.Count > 0)
  694. openCurrentMenu = openSubMenu.Last ();
  695. //if (openMenu.Subviews.Count == 0)
  696. // return;
  697. //if (index == 0) {
  698. // //SuperView.SetFocus (previousSubFocused);
  699. // FocusPrev ();
  700. // return;
  701. //}
  702. //for (int i = openMenu.Subviews.Count - 1; i > index; i--) {
  703. // isMenuClosing = true;
  704. // if (openMenu.Subviews.Count - 1 > 0)
  705. // SuperView.SetFocus (openMenu.Subviews [i - 1]);
  706. // else
  707. // SuperView.SetFocus (openMenu);
  708. // if (openMenu != null) {
  709. // Remove (openMenu.Subviews [i]);
  710. // openMenu.Remove (openMenu.Subviews [i]);
  711. // }
  712. // RemoveSubMenu (i);
  713. //}
  714. isMenuClosing = false;
  715. }
  716. internal void RemoveAllOpensSubMenus ()
  717. {
  718. if (openSubMenu != null) {
  719. foreach (var item in openSubMenu) {
  720. SuperView.Remove (item);
  721. }
  722. }
  723. }
  724. internal void CloseAllMenus ()
  725. {
  726. if (!isMenuOpening && !isMenuClosing) {
  727. if (openSubMenu != null)
  728. CloseMenu (false, true);
  729. CloseMenu ();
  730. if (LastFocused != null && LastFocused != this)
  731. selected = -1;
  732. }
  733. isMenuClosed = true;
  734. openedByHotKey = false;
  735. }
  736. View FindDeepestMenu (View view, ref int count)
  737. {
  738. count = count > 0 ? count : 0;
  739. foreach (var menu in view.Subviews) {
  740. if (menu is Menu) {
  741. count++;
  742. return FindDeepestMenu ((Menu)menu, ref count);
  743. }
  744. }
  745. return view;
  746. }
  747. internal void PreviousMenu (bool isSubMenu = false)
  748. {
  749. switch (isSubMenu) {
  750. case false:
  751. if (selected <= 0)
  752. selected = Menus.Length - 1;
  753. else
  754. selected--;
  755. if (selected > -1)
  756. CloseMenu (true, false);
  757. OpenMenu (selected);
  758. break;
  759. case true:
  760. if (selectedSub > -1) {
  761. selectedSub--;
  762. RemoveSubMenu (selectedSub);
  763. SetNeedsDisplay ();
  764. } else
  765. PreviousMenu ();
  766. break;
  767. }
  768. }
  769. internal void NextMenu (bool isSubMenu = false)
  770. {
  771. switch (isSubMenu) {
  772. case false:
  773. if (selected == -1)
  774. selected = 0;
  775. else if (selected + 1 == Menus.Length)
  776. selected = 0;
  777. else
  778. selected++;
  779. if (selected > -1)
  780. CloseMenu (true);
  781. OpenMenu (selected);
  782. break;
  783. case true:
  784. if (UseKeysUpDownAsKeysLeftRight) {
  785. CloseMenu (false, true);
  786. NextMenu ();
  787. } else {
  788. if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && openCurrentMenu.barItems.Children [openCurrentMenu.current].SubMenu == null) {
  789. if (openSubMenu != null)
  790. CloseMenu (false, true);
  791. NextMenu ();
  792. } else if (openCurrentMenu.barItems.Children [openCurrentMenu.current].SubMenu != null ||
  793. !openCurrentMenu.barItems.Children [openCurrentMenu.current].IsFromSubMenu)
  794. selectedSub++;
  795. else
  796. return;
  797. SetNeedsDisplay ();
  798. openCurrentMenu.CheckSubMenu ();
  799. }
  800. break;
  801. }
  802. }
  803. bool openedByHotKey = false;
  804. internal bool FindAndOpenMenuByHotkey (KeyEvent kb)
  805. {
  806. int pos = 0;
  807. var c = ((uint)kb.Key & (uint)Key.CharMask);
  808. for (int i = 0; i < Menus.Length; i++) {
  809. // TODO: this code is duplicated, hotkey should be part of the MenuBarItem
  810. var mi = Menus [i];
  811. int p = mi.Title.IndexOf ('_');
  812. if (p != -1 && p + 1 < mi.Title.Length) {
  813. if (Char.ToUpperInvariant ((char)mi.Title [p + 1]) == c) {
  814. if (mi.IsTopLevel) {
  815. var menu = new Menu (this, i, 0, mi);
  816. menu.Run (mi.Action);
  817. } else {
  818. openedByHotKey = true;
  819. Application.GrabMouse (this);
  820. selected = i;
  821. OpenMenu (i);
  822. }
  823. return true;
  824. }
  825. }
  826. }
  827. return false;
  828. }
  829. public override bool ProcessHotKey (KeyEvent kb)
  830. {
  831. if (kb.Key == Key.F9) {
  832. if (isMenuClosed)
  833. StartMenu ();
  834. else
  835. CloseAllMenus ();
  836. return true;
  837. }
  838. if (kb.IsAlt) {
  839. if (FindAndOpenMenuByHotkey (kb)) return true;
  840. }
  841. var kc = kb.KeyValue;
  842. return base.ProcessHotKey (kb);
  843. }
  844. public override bool ProcessKey (KeyEvent kb)
  845. {
  846. switch (kb.Key) {
  847. case Key.CursorLeft:
  848. selected--;
  849. if (selected < 0)
  850. selected = Menus.Length - 1;
  851. break;
  852. case Key.CursorRight:
  853. selected = (selected + 1) % Menus.Length;
  854. break;
  855. case Key.Esc:
  856. case Key.ControlC:
  857. //TODO: Running = false;
  858. CloseMenu ();
  859. break;
  860. default:
  861. var key = kb.KeyValue;
  862. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  863. char c = Char.ToUpper ((char)key);
  864. if (Menus [selected].IsTopLevel)
  865. return false;
  866. foreach (var mi in Menus [selected].Children) {
  867. int p = mi.Title.IndexOf ('_');
  868. if (p != -1 && p + 1 < mi.Title.Length) {
  869. if (mi.Title [p + 1] == c) {
  870. Selected (mi);
  871. return true;
  872. }
  873. }
  874. }
  875. }
  876. return false;
  877. }
  878. SetNeedsDisplay ();
  879. return true;
  880. }
  881. public override bool MouseEvent (MouseEvent me)
  882. {
  883. if (!handled && !HandleGrabView (me, this)) {
  884. return false;
  885. }
  886. handled = false;
  887. if (me.Flags == MouseFlags.Button1Clicked ||
  888. (me.Flags == MouseFlags.ReportMousePosition && selected > -1)) {
  889. int pos = 1;
  890. int cx = me.X;
  891. for (int i = 0; i < Menus.Length; i++) {
  892. if (cx > pos && me.X < pos + 1 + Menus [i].TitleLength) {
  893. if (selected == i && me.Flags == MouseFlags.Button1Clicked && !isMenuClosed) {
  894. Application.UngrabMouse ();
  895. if (Menus [i].IsTopLevel) {
  896. var menu = new Menu (this, i, 0, Menus [i]);
  897. menu.Run (Menus [i].Action);
  898. } else {
  899. CloseMenu ();
  900. }
  901. } else if (me.Flags == MouseFlags.Button1Clicked && isMenuClosed) {
  902. if (Menus [i].IsTopLevel) {
  903. var menu = new Menu (this, i, 0, Menus [i]);
  904. menu.Run (Menus [i].Action);
  905. } else {
  906. Activate (i);
  907. }
  908. } else if (selected != i && selected > -1 && me.Flags == MouseFlags.ReportMousePosition) {
  909. if (!isMenuClosed) {
  910. CloseMenu ();
  911. Activate (i);
  912. }
  913. } else {
  914. if (!isMenuClosed)
  915. Activate (i);
  916. }
  917. return true;
  918. }
  919. pos += 2 + Menus [i].TitleLength + 1;
  920. }
  921. }
  922. return false;
  923. }
  924. internal bool handled;
  925. internal bool HandleGrabView (MouseEvent me, View current)
  926. {
  927. if (Application.mouseGrabView != null) {
  928. if (me.View is MenuBar || me.View is Menu) {
  929. if (me.View != current) {
  930. Application.UngrabMouse ();
  931. Application.GrabMouse (me.View);
  932. me.View.MouseEvent (me);
  933. }
  934. } else if (!(me.View is MenuBar || me.View is Menu) && me.Flags.HasFlag (MouseFlags.Button1Clicked)) {
  935. Application.UngrabMouse ();
  936. CloseAllMenus ();
  937. handled = false;
  938. return false;
  939. } else {
  940. handled = false;
  941. return false;
  942. }
  943. } else if (isMenuClosed && me.Flags.HasFlag (MouseFlags.Button1Clicked)) {
  944. Application.GrabMouse (current);
  945. } else {
  946. handled = false;
  947. return false;
  948. }
  949. //if (me.View != this && me.Flags != MouseFlags.Button1Clicked)
  950. // return true;
  951. //else if (me.View != this && me.Flags == MouseFlags.Button1Clicked) {
  952. // Application.UngrabMouse ();
  953. // host.CloseAllMenus ();
  954. // return true;
  955. //}
  956. //if (!(me.View is MenuBar) && !(me.View is Menu) && me.Flags != MouseFlags.Button1Clicked)
  957. // return false;
  958. //if (Application.mouseGrabView != null) {
  959. // if (me.View is MenuBar || me.View is Menu) {
  960. // me.X -= me.OfX;
  961. // me.Y -= me.OfY;
  962. // me.View.MouseEvent (me);
  963. // return true;
  964. // } else if (!(me.View is MenuBar || me.View is Menu) && me.Flags == MouseFlags.Button1Clicked) {
  965. // Application.UngrabMouse ();
  966. // CloseAllMenus ();
  967. // }
  968. //} else if (!isMenuClosed && selected == -1 && me.Flags == MouseFlags.Button1Clicked) {
  969. // Application.GrabMouse (this);
  970. // return true;
  971. //}
  972. //if (Application.mouseGrabView != null) {
  973. // if (Application.mouseGrabView == me.View && me.View == current) {
  974. // me.X -= me.OfX;
  975. // me.Y -= me.OfY;
  976. // } else if (me.View != current && me.View is MenuBar && me.View is Menu) {
  977. // Application.UngrabMouse ();
  978. // Application.GrabMouse (me.View);
  979. // } else if (me.Flags == MouseFlags.Button1Clicked) {
  980. // Application.UngrabMouse ();
  981. // CloseMenu ();
  982. // }
  983. //} else if ((!isMenuClosed && selected > -1)) {
  984. // Application.GrabMouse (current);
  985. //}
  986. handled = true;
  987. return true;
  988. }
  989. }
  990. }