Menu.cs 38 KB

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