Menu.cs 40 KB

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