Menu.cs 44 KB

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