Menu.cs 43 KB

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