Menu.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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 = host.ColorScheme;
  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 = host.ColorScheme;
  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. ///<inheritdoc/>
  676. public override bool OnEnter (View view)
  677. {
  678. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  679. return base.OnEnter (view);
  680. }
  681. }
  682. /// <summary>
  683. /// The MenuBar provides a menu for Terminal.Gui applications.
  684. /// </summary>
  685. /// <remarks>
  686. /// <para>
  687. /// The <see cref="MenuBar"/> appears on the first row of the terminal.
  688. /// </para>
  689. /// <para>
  690. /// The <see cref="MenuBar"/> provides global hotkeys for the application.
  691. /// </para>
  692. /// </remarks>
  693. public class MenuBar : View {
  694. /// <summary>
  695. /// Gets or sets the array of <see cref="MenuBarItem"/>s for the menu. Only set this when the <see cref="MenuBar"/> is vislble.
  696. /// </summary>
  697. /// <value>The menu array.</value>
  698. public MenuBarItem [] Menus { get; set; }
  699. internal int selected;
  700. internal int selectedSub;
  701. Action action;
  702. /// <summary>
  703. /// Used for change the navigation key style.
  704. /// </summary>
  705. public bool UseKeysUpDownAsKeysLeftRight { get; set; } = true;
  706. static ustring shortcutDelimiter = "+";
  707. /// <summary>
  708. /// Used for change the shortcut delimiter separator.
  709. /// </summary>
  710. public static ustring ShortcutDelimiter {
  711. get => shortcutDelimiter;
  712. set {
  713. if (shortcutDelimiter != value) {
  714. shortcutDelimiter = value == ustring.Empty ? " " : value;
  715. }
  716. }
  717. }
  718. /// <summary>
  719. /// Initializes a new instance of the <see cref="MenuBar"/>.
  720. /// </summary>
  721. public MenuBar () : this (new MenuBarItem [] { }) { }
  722. /// <summary>
  723. /// Initializes a new instance of the <see cref="MenuBar"/> class with the specified set of toplevel menu items.
  724. /// </summary>
  725. /// <param name="menus">Individual menu items; a null item will result in a separator being drawn.</param>
  726. public MenuBar (MenuBarItem [] menus) : base ()
  727. {
  728. X = 0;
  729. Y = 0;
  730. Width = Dim.Fill ();
  731. Height = 1;
  732. Menus = menus;
  733. //CanFocus = true;
  734. selected = -1;
  735. selectedSub = -1;
  736. ColorScheme = Colors.Menu;
  737. WantMousePositionReports = true;
  738. IsMenuOpen = false;
  739. }
  740. bool openedByAltKey;
  741. bool isCleaning;
  742. ///<inheritdoc/>
  743. public override bool OnLeave (View view)
  744. {
  745. if ((!(view is MenuBar) && !(view is Menu) || !(view is MenuBar) && !(view is Menu) && openMenu != null) && !isCleaning && !reopen) {
  746. CleanUp ();
  747. return true;
  748. }
  749. return false;
  750. }
  751. ///<inheritdoc/>
  752. public override bool OnKeyDown (KeyEvent keyEvent)
  753. {
  754. if (keyEvent.IsAlt || (keyEvent.IsCtrl && keyEvent.Key == (Key.CtrlMask | Key.Space))) {
  755. openedByAltKey = true;
  756. SetNeedsDisplay ();
  757. openedByHotKey = false;
  758. }
  759. return false;
  760. }
  761. ///<inheritdoc/>
  762. public override bool OnKeyUp (KeyEvent keyEvent)
  763. {
  764. if (keyEvent.IsAlt || (keyEvent.IsCtrl && keyEvent.Key == (Key.CtrlMask | Key.Space))) {
  765. // User pressed Alt - this may be a precursor to a menu accelerator (e.g. Alt-F)
  766. if (openedByAltKey && !IsMenuOpen && openMenu == null && (((uint)keyEvent.Key & (uint)Key.CharMask) == 0
  767. || ((uint)keyEvent.Key & (uint)Key.CharMask) == (uint)Key.Space)) {
  768. // There's no open menu, the first menu item should be highlight.
  769. // The right way to do this is to SetFocus(MenuBar), but for some reason
  770. // that faults.
  771. //Activate (0);
  772. //StartMenu ();
  773. IsMenuOpen = true;
  774. selected = 0;
  775. CanFocus = true;
  776. lastFocused = SuperView.MostFocused;
  777. SetFocus ();
  778. SetNeedsDisplay ();
  779. Application.GrabMouse (this);
  780. } else if (!openedByHotKey) {
  781. // There's an open menu. If this Alt key-up is a pre-cursor to an accelerator
  782. // we don't want to close the menu because it'll flash.
  783. // How to deal with that?
  784. CleanUp ();
  785. }
  786. return true;
  787. }
  788. return false;
  789. }
  790. internal void CleanUp ()
  791. {
  792. isCleaning = true;
  793. if (openMenu != null) {
  794. CloseAllMenus ();
  795. }
  796. openedByAltKey = false;
  797. IsMenuOpen = false;
  798. selected = -1;
  799. CanFocus = false;
  800. if (lastFocused != null) {
  801. lastFocused.SetFocus ();
  802. }
  803. SetNeedsDisplay ();
  804. Application.UngrabMouse ();
  805. isCleaning = false;
  806. }
  807. ///<inheritdoc/>
  808. public override void Redraw (Rect bounds)
  809. {
  810. Move (0, 0);
  811. Driver.SetAttribute (ColorScheme.Normal);
  812. for (int i = 0; i < Frame.Width; i++)
  813. Driver.AddRune (' ');
  814. Move (1, 0);
  815. int pos = 1;
  816. for (int i = 0; i < Menus.Length; i++) {
  817. var menu = Menus [i];
  818. Move (pos, 0);
  819. Attribute hotColor, normalColor;
  820. if (i == selected) {
  821. hotColor = i == selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  822. normalColor = i == selected ? ColorScheme.Focus : ColorScheme.Normal;
  823. } else if (openedByAltKey) {
  824. hotColor = ColorScheme.HotNormal;
  825. normalColor = ColorScheme.Normal;
  826. } else {
  827. hotColor = ColorScheme.Normal;
  828. normalColor = ColorScheme.Normal;
  829. }
  830. DrawHotString (menu.Help.IsEmpty ? $" {menu.Title} " : $" {menu.Title} {menu.Help} ", hotColor, normalColor);
  831. pos += 1 + menu.TitleLength + (menu.Help.Length > 0 ? menu.Help.Length + 2 : 0) + 2;
  832. }
  833. PositionCursor ();
  834. }
  835. ///<inheritdoc/>
  836. public override void PositionCursor ()
  837. {
  838. if (selected == -1 && HasFocus && Menus.Length > 0) {
  839. selected = 0;
  840. }
  841. int pos = 0;
  842. for (int i = 0; i < Menus.Length; i++) {
  843. if (i == selected) {
  844. pos++;
  845. if (IsMenuOpen)
  846. Move (pos + 1, 0);
  847. else {
  848. Move (pos + 1, 0);
  849. }
  850. return;
  851. } else if (IsMenuOpen) {
  852. pos += 1 + Menus [i].TitleLength + (Menus [i].Help.Length > 0 ? Menus [i].Help.Length + 2 : 0) + 2;
  853. } else {
  854. pos += 2 + Menus [i].TitleLength + (Menus [i].Help.Length > 0 ? Menus [i].Help.Length + 2 : 0) + 1;
  855. }
  856. }
  857. //Move (0, 0);
  858. }
  859. void Selected (MenuItem item)
  860. {
  861. // TODO: Running = false;
  862. action = item.Action;
  863. }
  864. /// <summary>
  865. /// Raised as a menu is opening.
  866. /// </summary>
  867. public event Action MenuOpening;
  868. /// <summary>
  869. /// Raised when a menu is closing.
  870. /// </summary>
  871. public event Action MenuClosing;
  872. internal Menu openMenu;
  873. internal Menu openCurrentMenu;
  874. internal List<Menu> openSubMenu;
  875. View previousFocused;
  876. internal bool isMenuOpening;
  877. internal bool isMenuClosing;
  878. /// <summary>
  879. /// True if the menu is open; otherwise false.
  880. /// </summary>
  881. public bool IsMenuOpen { get; protected set; }
  882. /// <summary>
  883. /// Virtual method that will invoke the <see cref="MenuOpening"/>
  884. /// </summary>
  885. public virtual void OnMenuOpening ()
  886. {
  887. MenuOpening?.Invoke ();
  888. }
  889. /// <summary>
  890. /// Virtual method that will invoke the <see cref="MenuClosing"/>
  891. /// </summary>
  892. public virtual void OnMenuClosing ()
  893. {
  894. MenuClosing?.Invoke ();
  895. }
  896. View lastFocused;
  897. /// <summary>
  898. /// Get the lasted focused view before open the menu.
  899. /// </summary>
  900. public View LastFocused { get; private set; }
  901. internal void OpenMenu (int index, int sIndex = -1, MenuBarItem subMenu = null)
  902. {
  903. isMenuOpening = true;
  904. OnMenuOpening ();
  905. int pos = 0;
  906. switch (subMenu) {
  907. case null:
  908. lastFocused = lastFocused ?? SuperView.MostFocused;
  909. if (openSubMenu != null)
  910. CloseMenu (false, true);
  911. if (openMenu != null) {
  912. SuperView.Remove (openMenu);
  913. openMenu.Dispose ();
  914. }
  915. for (int i = 0; i < index; i++)
  916. pos += Menus [i].Title.RuneCount + (Menus [i].Help.RuneCount > 0 ? Menus [i].Help.RuneCount + 2 : 0) + 2;
  917. openMenu = new Menu (this, pos, 1, Menus [index]);
  918. openCurrentMenu = openMenu;
  919. openCurrentMenu.previousSubFocused = openMenu;
  920. SuperView.Add (openMenu);
  921. openMenu.SetFocus ();
  922. break;
  923. default:
  924. if (openSubMenu == null)
  925. openSubMenu = new List<Menu> ();
  926. if (sIndex > -1) {
  927. RemoveSubMenu (sIndex);
  928. } else {
  929. var last = openSubMenu.Count > 0 ? openSubMenu.Last () : openMenu;
  930. openCurrentMenu = new Menu (this, last.Frame.Left + last.Frame.Width, last.Frame.Top + 1 + last.current, subMenu);
  931. openCurrentMenu.previousSubFocused = last.previousSubFocused;
  932. openSubMenu.Add (openCurrentMenu);
  933. SuperView.Add (openCurrentMenu);
  934. }
  935. selectedSub = openSubMenu.Count - 1;
  936. if (selectedSub > -1 && SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  937. openCurrentMenu.SetFocus ();
  938. }
  939. break;
  940. }
  941. isMenuOpening = false;
  942. IsMenuOpen = true;
  943. }
  944. /// <summary>
  945. /// Opens the current Menu programatically.
  946. /// </summary>
  947. public void OpenMenu ()
  948. {
  949. if (openMenu != null)
  950. return;
  951. selected = 0;
  952. SetNeedsDisplay ();
  953. previousFocused = SuperView.Focused;
  954. OpenMenu (selected);
  955. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  956. CloseMenu ();
  957. }
  958. openCurrentMenu.CheckSubMenu ();
  959. Application.GrabMouse (this);
  960. }
  961. // Activates the menu, handles either first focus, or activating an entry when it was already active
  962. // For mouse events.
  963. internal void Activate (int idx, int sIdx = -1, MenuBarItem subMenu = null)
  964. {
  965. selected = idx;
  966. selectedSub = sIdx;
  967. if (openMenu == null)
  968. previousFocused = SuperView.Focused;
  969. OpenMenu (idx, sIdx, subMenu);
  970. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  971. if (subMenu == null) {
  972. CloseMenu ();
  973. }
  974. }
  975. SetNeedsDisplay ();
  976. }
  977. internal bool SelectEnabledItem (IEnumerable<MenuItem> chldren, int current, out int newCurrent, bool forward = true)
  978. {
  979. if (chldren == null) {
  980. newCurrent = -1;
  981. return true;
  982. }
  983. IEnumerable<MenuItem> childrens;
  984. if (forward) {
  985. childrens = chldren;
  986. } else {
  987. childrens = chldren.Reverse ();
  988. }
  989. int count;
  990. if (forward) {
  991. count = -1;
  992. } else {
  993. count = childrens.Count ();
  994. }
  995. foreach (var child in childrens) {
  996. if (forward) {
  997. if (++count < current) {
  998. continue;
  999. }
  1000. } else {
  1001. if (--count > current) {
  1002. continue;
  1003. }
  1004. }
  1005. if (child == null || !child.IsEnabled ()) {
  1006. if (forward) {
  1007. current++;
  1008. } else {
  1009. current--;
  1010. }
  1011. } else {
  1012. newCurrent = current;
  1013. return true;
  1014. }
  1015. }
  1016. newCurrent = -1;
  1017. return false;
  1018. }
  1019. /// <summary>
  1020. /// Closes the current Menu programatically, if open.
  1021. /// </summary>
  1022. public void CloseMenu ()
  1023. {
  1024. CloseMenu (false, false);
  1025. }
  1026. bool reopen;
  1027. internal void CloseMenu (bool reopen = false, bool isSubMenu = false)
  1028. {
  1029. isMenuClosing = true;
  1030. this.reopen = reopen;
  1031. OnMenuClosing ();
  1032. switch (isSubMenu) {
  1033. case false:
  1034. if (openMenu != null) {
  1035. SuperView?.Remove (openMenu);
  1036. }
  1037. SetNeedsDisplay ();
  1038. if (previousFocused != null && previousFocused is Menu && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
  1039. previousFocused.SetFocus ();
  1040. openMenu?.Dispose ();
  1041. openMenu = null;
  1042. if (lastFocused is Menu || lastFocused is MenuBar) {
  1043. lastFocused = null;
  1044. }
  1045. LastFocused = lastFocused;
  1046. lastFocused = null;
  1047. if (LastFocused != null) {
  1048. if (!reopen) {
  1049. selected = -1;
  1050. }
  1051. LastFocused.SetFocus ();
  1052. } else {
  1053. SetFocus ();
  1054. PositionCursor ();
  1055. }
  1056. IsMenuOpen = false;
  1057. break;
  1058. case true:
  1059. selectedSub = -1;
  1060. SetNeedsDisplay ();
  1061. RemoveAllOpensSubMenus ();
  1062. openCurrentMenu.previousSubFocused.SetFocus ();
  1063. openSubMenu = null;
  1064. IsMenuOpen = true;
  1065. break;
  1066. }
  1067. this.reopen = false;
  1068. isMenuClosing = false;
  1069. }
  1070. void RemoveSubMenu (int index)
  1071. {
  1072. if (openSubMenu == null)
  1073. return;
  1074. for (int i = openSubMenu.Count - 1; i > index; i--) {
  1075. isMenuClosing = true;
  1076. if (openSubMenu.Count - 1 > 0)
  1077. openSubMenu [i - 1].SetFocus ();
  1078. else
  1079. openMenu.SetFocus ();
  1080. if (openSubMenu != null) {
  1081. var menu = openSubMenu [i];
  1082. SuperView.Remove (menu);
  1083. openSubMenu.Remove (menu);
  1084. menu.Dispose ();
  1085. }
  1086. RemoveSubMenu (i);
  1087. }
  1088. if (openSubMenu.Count > 0)
  1089. openCurrentMenu = openSubMenu.Last ();
  1090. //if (openMenu.Subviews.Count == 0)
  1091. // return;
  1092. //if (index == 0) {
  1093. // //SuperView.SetFocus (previousSubFocused);
  1094. // FocusPrev ();
  1095. // return;
  1096. //}
  1097. //for (int i = openMenu.Subviews.Count - 1; i > index; i--) {
  1098. // isMenuClosing = true;
  1099. // if (openMenu.Subviews.Count - 1 > 0)
  1100. // SuperView.SetFocus (openMenu.Subviews [i - 1]);
  1101. // else
  1102. // SuperView.SetFocus (openMenu);
  1103. // if (openMenu != null) {
  1104. // Remove (openMenu.Subviews [i]);
  1105. // openMenu.Remove (openMenu.Subviews [i]);
  1106. // }
  1107. // RemoveSubMenu (i);
  1108. //}
  1109. isMenuClosing = false;
  1110. }
  1111. internal void RemoveAllOpensSubMenus ()
  1112. {
  1113. if (openSubMenu != null) {
  1114. foreach (var item in openSubMenu) {
  1115. SuperView.Remove (item);
  1116. item.Dispose ();
  1117. }
  1118. }
  1119. }
  1120. internal void CloseAllMenus ()
  1121. {
  1122. if (!isMenuOpening && !isMenuClosing) {
  1123. if (openSubMenu != null)
  1124. CloseMenu (false, true);
  1125. CloseMenu ();
  1126. if (LastFocused != null && LastFocused != this)
  1127. selected = -1;
  1128. }
  1129. IsMenuOpen = false;
  1130. openedByHotKey = false;
  1131. openedByAltKey = false;
  1132. }
  1133. View FindDeepestMenu (View view, ref int count)
  1134. {
  1135. count = count > 0 ? count : 0;
  1136. foreach (var menu in view.Subviews) {
  1137. if (menu is Menu) {
  1138. count++;
  1139. return FindDeepestMenu ((Menu)menu, ref count);
  1140. }
  1141. }
  1142. return view;
  1143. }
  1144. internal void PreviousMenu (bool isSubMenu = false)
  1145. {
  1146. switch (isSubMenu) {
  1147. case false:
  1148. if (selected <= 0)
  1149. selected = Menus.Length - 1;
  1150. else
  1151. selected--;
  1152. if (selected > -1)
  1153. CloseMenu (true, false);
  1154. OpenMenu (selected);
  1155. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current, false)) {
  1156. openCurrentMenu.current = 0;
  1157. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1158. CloseMenu ();
  1159. }
  1160. break;
  1161. }
  1162. break;
  1163. case true:
  1164. if (selectedSub > -1) {
  1165. selectedSub--;
  1166. RemoveSubMenu (selectedSub);
  1167. SetNeedsDisplay ();
  1168. } else
  1169. PreviousMenu ();
  1170. break;
  1171. }
  1172. }
  1173. internal void NextMenu (bool isSubMenu = false)
  1174. {
  1175. switch (isSubMenu) {
  1176. case false:
  1177. if (selected == -1)
  1178. selected = 0;
  1179. else if (selected + 1 == Menus.Length)
  1180. selected = 0;
  1181. else
  1182. selected++;
  1183. if (selected > -1)
  1184. CloseMenu (true);
  1185. OpenMenu (selected);
  1186. SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current);
  1187. break;
  1188. case true:
  1189. if (UseKeysUpDownAsKeysLeftRight) {
  1190. CloseMenu (false, true);
  1191. NextMenu ();
  1192. } else {
  1193. var subMenu = openCurrentMenu.barItems.SubMenu (openCurrentMenu.barItems.Children [openCurrentMenu.current]);
  1194. if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && subMenu == null) {
  1195. if (openSubMenu != null)
  1196. CloseMenu (false, true);
  1197. NextMenu ();
  1198. } else if (subMenu != null ||
  1199. !openCurrentMenu.barItems.Children [openCurrentMenu.current].IsFromSubMenu)
  1200. selectedSub++;
  1201. else
  1202. return;
  1203. SetNeedsDisplay ();
  1204. openCurrentMenu.CheckSubMenu ();
  1205. }
  1206. break;
  1207. }
  1208. }
  1209. bool openedByHotKey;
  1210. internal bool FindAndOpenMenuByHotkey (KeyEvent kb)
  1211. {
  1212. //int pos = 0;
  1213. var c = ((uint)kb.Key & (uint)Key.CharMask);
  1214. for (int i = 0; i < Menus.Length; i++) {
  1215. // TODO: this code is duplicated, hotkey should be part of the MenuBarItem
  1216. var mi = Menus [i];
  1217. int p = mi.Title.IndexOf ('_');
  1218. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1219. if (Char.ToUpperInvariant ((char)mi.Title [p + 1]) == c) {
  1220. ProcessMenu (i, mi);
  1221. return true;
  1222. }
  1223. }
  1224. }
  1225. return false;
  1226. }
  1227. internal bool FindAndOpenMenuByShortcut (KeyEvent kb, MenuItem [] children = null)
  1228. {
  1229. if (children == null) {
  1230. children = Menus;
  1231. }
  1232. var key = kb.KeyValue;
  1233. var keys = ShortcutHelper.GetModifiersKey (kb);
  1234. key |= (int)keys;
  1235. for (int i = 0; i < children.Length; i++) {
  1236. var mi = children [i];
  1237. if (mi == null) {
  1238. continue;
  1239. }
  1240. if ((!(mi is MenuBarItem mbiTopLevel) || mbiTopLevel.IsTopLevel) && mi.Shortcut != Key.Null && mi.Shortcut == (Key)key) {
  1241. var action = mi.Action;
  1242. if (action != null) {
  1243. Application.MainLoop.AddIdle (() => {
  1244. action ();
  1245. return false;
  1246. });
  1247. }
  1248. return true;
  1249. }
  1250. if (mi is MenuBarItem menuBarItem && !menuBarItem.IsTopLevel && FindAndOpenMenuByShortcut (kb, menuBarItem.Children)) {
  1251. return true;
  1252. }
  1253. }
  1254. return false;
  1255. }
  1256. private void ProcessMenu (int i, MenuBarItem mi)
  1257. {
  1258. if (mi.IsTopLevel) {
  1259. var menu = new Menu (this, i, 0, mi);
  1260. menu.Run (mi.Action);
  1261. menu.Dispose ();
  1262. } else {
  1263. openedByHotKey = true;
  1264. Application.GrabMouse (this);
  1265. selected = i;
  1266. OpenMenu (i);
  1267. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1268. CloseMenu ();
  1269. }
  1270. openCurrentMenu.CheckSubMenu ();
  1271. }
  1272. }
  1273. ///<inheritdoc/>
  1274. public override bool ProcessHotKey (KeyEvent kb)
  1275. {
  1276. if (kb.Key == Key.F9) {
  1277. if (!IsMenuOpen)
  1278. OpenMenu ();
  1279. else
  1280. CloseAllMenus ();
  1281. return true;
  1282. }
  1283. // To ncurses simulate a AltMask key pressing Alt+Space because
  1284. // it can�t detect an alone special key down was pressed.
  1285. if (kb.IsAlt && kb.Key == Key.AltMask && openMenu == null) {
  1286. OnKeyDown (kb);
  1287. OnKeyUp (kb);
  1288. return true;
  1289. } else if (kb.IsAlt && !kb.IsCtrl && !kb.IsShift) {
  1290. if (FindAndOpenMenuByHotkey (kb)) return true;
  1291. }
  1292. //var kc = kb.KeyValue;
  1293. return base.ProcessHotKey (kb);
  1294. }
  1295. ///<inheritdoc/>
  1296. public override bool ProcessKey (KeyEvent kb)
  1297. {
  1298. switch (kb.Key) {
  1299. case Key.CursorLeft:
  1300. selected--;
  1301. if (selected < 0)
  1302. selected = Menus.Length - 1;
  1303. break;
  1304. case Key.CursorRight:
  1305. selected = (selected + 1) % Menus.Length;
  1306. break;
  1307. case Key.Esc:
  1308. case Key.C | Key.CtrlMask:
  1309. //TODO: Running = false;
  1310. CloseMenu ();
  1311. if (openedByAltKey) {
  1312. openedByAltKey = false;
  1313. LastFocused?.SetFocus ();
  1314. }
  1315. break;
  1316. case Key.CursorDown:
  1317. case Key.Enter:
  1318. if (selected > -1) {
  1319. ProcessMenu (selected, Menus [selected]);
  1320. }
  1321. break;
  1322. default:
  1323. var key = kb.KeyValue;
  1324. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  1325. char c = Char.ToUpper ((char)key);
  1326. if (selected == -1 || Menus [selected].IsTopLevel)
  1327. return false;
  1328. foreach (var mi in Menus [selected].Children) {
  1329. if (mi == null)
  1330. continue;
  1331. int p = mi.Title.IndexOf ('_');
  1332. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1333. if (mi.Title [p + 1] == c) {
  1334. Selected (mi);
  1335. return true;
  1336. }
  1337. }
  1338. }
  1339. }
  1340. return false;
  1341. }
  1342. SetNeedsDisplay ();
  1343. return true;
  1344. }
  1345. ///<inheritdoc/>
  1346. public override bool ProcessColdKey (KeyEvent kb)
  1347. {
  1348. return FindAndOpenMenuByShortcut (kb);
  1349. }
  1350. ///<inheritdoc/>
  1351. public override bool MouseEvent (MouseEvent me)
  1352. {
  1353. if (!handled && !HandleGrabView (me, this)) {
  1354. return false;
  1355. }
  1356. handled = false;
  1357. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.Button1Clicked ||
  1358. (me.Flags == MouseFlags.ReportMousePosition && selected > -1) ||
  1359. (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && selected > -1)) {
  1360. int pos = 1;
  1361. int cx = me.X;
  1362. for (int i = 0; i < Menus.Length; i++) {
  1363. if (cx >= pos && cx < pos + 1 + Menus [i].TitleLength + Menus [i].Help.RuneCount + 2) {
  1364. if (me.Flags == MouseFlags.Button1Clicked) {
  1365. if (Menus [i].IsTopLevel) {
  1366. var menu = new Menu (this, i, 0, Menus [i]);
  1367. menu.Run (Menus [i].Action);
  1368. menu.Dispose ();
  1369. }
  1370. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked) {
  1371. if (IsMenuOpen && !Menus [i].IsTopLevel) {
  1372. CloseAllMenus ();
  1373. } else if (!Menus [i].IsTopLevel) {
  1374. Activate (i);
  1375. }
  1376. } else if (selected != i && selected > -1 && (me.Flags == MouseFlags.ReportMousePosition ||
  1377. me.Flags == MouseFlags.Button1Pressed && me.Flags == MouseFlags.ReportMousePosition)) {
  1378. if (IsMenuOpen) {
  1379. CloseMenu (true, false);
  1380. Activate (i);
  1381. }
  1382. } else {
  1383. if (IsMenuOpen)
  1384. Activate (i);
  1385. }
  1386. return true;
  1387. }
  1388. pos += 1 + Menus [i].TitleLength + 2;
  1389. }
  1390. }
  1391. return false;
  1392. }
  1393. internal bool handled;
  1394. internal bool HandleGrabView (MouseEvent me, View current)
  1395. {
  1396. if (Application.mouseGrabView != null) {
  1397. if (me.View is MenuBar || me.View is Menu) {
  1398. if (me.View != current) {
  1399. Application.UngrabMouse ();
  1400. var v = me.View;
  1401. Application.GrabMouse (v);
  1402. var newxy = v.ScreenToView (me.X, me.Y);
  1403. var nme = new MouseEvent () {
  1404. X = newxy.X,
  1405. Y = newxy.Y,
  1406. Flags = me.Flags,
  1407. OfX = me.X - newxy.X,
  1408. OfY = me.Y - newxy.Y,
  1409. View = v
  1410. };
  1411. v.MouseEvent (nme);
  1412. return false;
  1413. }
  1414. } else if (!(me.View is MenuBar || me.View is Menu) && (me.Flags.HasFlag (MouseFlags.Button1Clicked) ||
  1415. me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked)) {
  1416. Application.UngrabMouse ();
  1417. CloseAllMenus ();
  1418. handled = false;
  1419. return false;
  1420. } else {
  1421. handled = false;
  1422. return false;
  1423. }
  1424. } else if (!IsMenuOpen && (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  1425. Application.GrabMouse (current);
  1426. } else if (IsMenuOpen && (me.View is MenuBar || me.View is Menu)) {
  1427. Application.GrabMouse (me.View);
  1428. } else {
  1429. handled = false;
  1430. return false;
  1431. }
  1432. //if (me.View != this && me.Flags != MouseFlags.Button1Pressed)
  1433. // return true;
  1434. //else if (me.View != this && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1435. // Application.UngrabMouse ();
  1436. // host.CloseAllMenus ();
  1437. // return true;
  1438. //}
  1439. //if (!(me.View is MenuBar) && !(me.View is Menu) && me.Flags != MouseFlags.Button1Pressed))
  1440. // return false;
  1441. //if (Application.mouseGrabView != null) {
  1442. // if (me.View is MenuBar || me.View is Menu) {
  1443. // me.X -= me.OfX;
  1444. // me.Y -= me.OfY;
  1445. // me.View.MouseEvent (me);
  1446. // return true;
  1447. // } else if (!(me.View is MenuBar || me.View is Menu) && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1448. // Application.UngrabMouse ();
  1449. // CloseAllMenus ();
  1450. // }
  1451. //} else if (!isMenuClosed && selected == -1 && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1452. // Application.GrabMouse (this);
  1453. // return true;
  1454. //}
  1455. //if (Application.mouseGrabView != null) {
  1456. // if (Application.mouseGrabView == me.View && me.View == current) {
  1457. // me.X -= me.OfX;
  1458. // me.Y -= me.OfY;
  1459. // } else if (me.View != current && me.View is MenuBar && me.View is Menu) {
  1460. // Application.UngrabMouse ();
  1461. // Application.GrabMouse (me.View);
  1462. // } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1463. // Application.UngrabMouse ();
  1464. // CloseMenu ();
  1465. // }
  1466. //} else if ((!isMenuClosed && selected > -1)) {
  1467. // Application.GrabMouse (current);
  1468. //}
  1469. handled = true;
  1470. return true;
  1471. }
  1472. ///<inheritdoc/>
  1473. public override bool OnEnter (View view)
  1474. {
  1475. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  1476. return base.OnEnter (view);
  1477. }
  1478. }
  1479. }