Menu.cs 40 KB

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