Menu.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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. if (!reopen) {
  914. selected = -1;
  915. }
  916. LastFocused.SuperView?.SetFocus (LastFocused);
  917. } else {
  918. SuperView.SetFocus (this);
  919. PositionCursor ();
  920. }
  921. IsMenuOpen = false;
  922. break;
  923. case true:
  924. selectedSub = -1;
  925. SetNeedsDisplay ();
  926. RemoveAllOpensSubMenus ();
  927. openCurrentMenu.previousSubFocused?.SuperView?.SetFocus (openCurrentMenu.previousSubFocused);
  928. openSubMenu = null;
  929. IsMenuOpen = true;
  930. break;
  931. }
  932. this.reopen = false;
  933. isMenuClosing = false;
  934. }
  935. void RemoveSubMenu (int index)
  936. {
  937. if (openSubMenu == null)
  938. return;
  939. for (int i = openSubMenu.Count - 1; i > index; i--) {
  940. isMenuClosing = true;
  941. if (openSubMenu.Count - 1 > 0)
  942. SuperView.SetFocus (openSubMenu [i - 1]);
  943. else
  944. SuperView.SetFocus (openMenu);
  945. if (openSubMenu != null) {
  946. var menu = openSubMenu [i];
  947. SuperView.Remove (menu);
  948. openSubMenu.Remove (menu);
  949. menu.Dispose ();
  950. }
  951. RemoveSubMenu (i);
  952. }
  953. if (openSubMenu.Count > 0)
  954. openCurrentMenu = openSubMenu.Last ();
  955. //if (openMenu.Subviews.Count == 0)
  956. // return;
  957. //if (index == 0) {
  958. // //SuperView.SetFocus (previousSubFocused);
  959. // FocusPrev ();
  960. // return;
  961. //}
  962. //for (int i = openMenu.Subviews.Count - 1; i > index; i--) {
  963. // isMenuClosing = true;
  964. // if (openMenu.Subviews.Count - 1 > 0)
  965. // SuperView.SetFocus (openMenu.Subviews [i - 1]);
  966. // else
  967. // SuperView.SetFocus (openMenu);
  968. // if (openMenu != null) {
  969. // Remove (openMenu.Subviews [i]);
  970. // openMenu.Remove (openMenu.Subviews [i]);
  971. // }
  972. // RemoveSubMenu (i);
  973. //}
  974. isMenuClosing = false;
  975. }
  976. internal void RemoveAllOpensSubMenus ()
  977. {
  978. if (openSubMenu != null) {
  979. foreach (var item in openSubMenu) {
  980. SuperView.Remove (item);
  981. item.Dispose ();
  982. }
  983. }
  984. }
  985. internal void CloseAllMenus ()
  986. {
  987. if (!isMenuOpening && !isMenuClosing) {
  988. if (openSubMenu != null)
  989. CloseMenu (false, true);
  990. CloseMenu ();
  991. if (LastFocused != null && LastFocused != this)
  992. selected = -1;
  993. }
  994. IsMenuOpen = false;
  995. openedByHotKey = false;
  996. openedByAltKey = false;
  997. }
  998. View FindDeepestMenu (View view, ref int count)
  999. {
  1000. count = count > 0 ? count : 0;
  1001. foreach (var menu in view.Subviews) {
  1002. if (menu is Menu) {
  1003. count++;
  1004. return FindDeepestMenu ((Menu)menu, ref count);
  1005. }
  1006. }
  1007. return view;
  1008. }
  1009. internal void PreviousMenu (bool isSubMenu = false)
  1010. {
  1011. switch (isSubMenu) {
  1012. case false:
  1013. if (selected <= 0)
  1014. selected = Menus.Length - 1;
  1015. else
  1016. selected--;
  1017. if (selected > -1)
  1018. CloseMenu (true, false);
  1019. OpenMenu (selected);
  1020. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current, false)) {
  1021. openCurrentMenu.current = 0;
  1022. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1023. CloseMenu ();
  1024. }
  1025. break;
  1026. }
  1027. break;
  1028. case true:
  1029. if (selectedSub > -1) {
  1030. selectedSub--;
  1031. RemoveSubMenu (selectedSub);
  1032. SetNeedsDisplay ();
  1033. } else
  1034. PreviousMenu ();
  1035. break;
  1036. }
  1037. }
  1038. internal void NextMenu (bool isSubMenu = false)
  1039. {
  1040. switch (isSubMenu) {
  1041. case false:
  1042. if (selected == -1)
  1043. selected = 0;
  1044. else if (selected + 1 == Menus.Length)
  1045. selected = 0;
  1046. else
  1047. selected++;
  1048. if (selected > -1)
  1049. CloseMenu (true);
  1050. OpenMenu (selected);
  1051. SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current);
  1052. break;
  1053. case true:
  1054. if (UseKeysUpDownAsKeysLeftRight) {
  1055. CloseMenu (false, true);
  1056. NextMenu ();
  1057. } else {
  1058. if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && openCurrentMenu.barItems.Children [openCurrentMenu.current].SubMenu == null) {
  1059. if (openSubMenu != null)
  1060. CloseMenu (false, true);
  1061. NextMenu ();
  1062. } else if (openCurrentMenu.barItems.Children [openCurrentMenu.current].SubMenu != null ||
  1063. !openCurrentMenu.barItems.Children [openCurrentMenu.current].IsFromSubMenu)
  1064. selectedSub++;
  1065. else
  1066. return;
  1067. SetNeedsDisplay ();
  1068. openCurrentMenu.CheckSubMenu ();
  1069. }
  1070. break;
  1071. }
  1072. }
  1073. bool openedByHotKey;
  1074. internal bool FindAndOpenMenuByHotkey (KeyEvent kb)
  1075. {
  1076. //int pos = 0;
  1077. var c = ((uint)kb.Key & (uint)Key.CharMask);
  1078. for (int i = 0; i < Menus.Length; i++) {
  1079. // TODO: this code is duplicated, hotkey should be part of the MenuBarItem
  1080. var mi = Menus [i];
  1081. int p = mi.Title.IndexOf ('_');
  1082. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1083. if (Char.ToUpperInvariant ((char)mi.Title [p + 1]) == c) {
  1084. ProcessMenu (i, mi);
  1085. return true;
  1086. }
  1087. }
  1088. }
  1089. return false;
  1090. }
  1091. private void ProcessMenu (int i, MenuBarItem mi)
  1092. {
  1093. if (mi.IsTopLevel) {
  1094. var menu = new Menu (this, i, 0, mi);
  1095. menu.Run (mi.Action);
  1096. menu.Dispose ();
  1097. } else {
  1098. openedByHotKey = true;
  1099. Application.GrabMouse (this);
  1100. selected = i;
  1101. OpenMenu (i);
  1102. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1103. CloseMenu ();
  1104. }
  1105. openCurrentMenu.CheckSubMenu ();
  1106. }
  1107. }
  1108. ///<inheritdoc/>
  1109. public override bool ProcessHotKey (KeyEvent kb)
  1110. {
  1111. if (kb.Key == Key.F9) {
  1112. if (!IsMenuOpen)
  1113. OpenMenu ();
  1114. else
  1115. CloseAllMenus ();
  1116. return true;
  1117. }
  1118. // To ncurses simulate a AltMask key pressing Alt+Space because
  1119. // it can�t detect an alone special key down was pressed.
  1120. if (kb.IsAlt && kb.Key == Key.AltMask && openMenu == null) {
  1121. OnKeyDown (kb);
  1122. OnKeyUp (kb);
  1123. return true;
  1124. } else if (kb.IsAlt) {
  1125. if (FindAndOpenMenuByHotkey (kb)) return true;
  1126. }
  1127. //var kc = kb.KeyValue;
  1128. return base.ProcessHotKey (kb);
  1129. }
  1130. ///<inheritdoc/>
  1131. public override bool ProcessKey (KeyEvent kb)
  1132. {
  1133. switch (kb.Key) {
  1134. case Key.CursorLeft:
  1135. selected--;
  1136. if (selected < 0)
  1137. selected = Menus.Length - 1;
  1138. break;
  1139. case Key.CursorRight:
  1140. selected = (selected + 1) % Menus.Length;
  1141. break;
  1142. case Key.Esc:
  1143. case Key.ControlC:
  1144. //TODO: Running = false;
  1145. CloseMenu ();
  1146. if (openedByAltKey) {
  1147. openedByAltKey = false;
  1148. LastFocused.SuperView?.SetFocus (LastFocused);
  1149. }
  1150. break;
  1151. case Key.CursorDown:
  1152. case Key.Enter:
  1153. if (selected > -1) {
  1154. ProcessMenu (selected, Menus [selected]);
  1155. }
  1156. break;
  1157. default:
  1158. var key = kb.KeyValue;
  1159. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  1160. char c = Char.ToUpper ((char)key);
  1161. if (selected == -1 || Menus [selected].IsTopLevel)
  1162. return false;
  1163. foreach (var mi in Menus [selected].Children) {
  1164. if (mi == null)
  1165. continue;
  1166. int p = mi.Title.IndexOf ('_');
  1167. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1168. if (mi.Title [p + 1] == c) {
  1169. Selected (mi);
  1170. return true;
  1171. }
  1172. }
  1173. }
  1174. }
  1175. return false;
  1176. }
  1177. SetNeedsDisplay ();
  1178. return true;
  1179. }
  1180. ///<inheritdoc/>
  1181. public override bool MouseEvent (MouseEvent me)
  1182. {
  1183. if (!handled && !HandleGrabView (me, this)) {
  1184. return false;
  1185. }
  1186. handled = false;
  1187. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.Button1Clicked ||
  1188. (me.Flags == MouseFlags.ReportMousePosition && selected > -1) ||
  1189. (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && selected > -1)) {
  1190. int pos = 1;
  1191. int cx = me.X;
  1192. for (int i = 0; i < Menus.Length; i++) {
  1193. if (cx >= pos && cx < pos + 1 + Menus [i].TitleLength + 2) {
  1194. if (me.Flags == MouseFlags.Button1Clicked) {
  1195. if (Menus [i].IsTopLevel) {
  1196. var menu = new Menu (this, i, 0, Menus [i]);
  1197. menu.Run (Menus [i].Action);
  1198. menu.Dispose ();
  1199. }
  1200. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked) {
  1201. if (IsMenuOpen && !Menus [i].IsTopLevel) {
  1202. CloseAllMenus ();
  1203. } else if (!Menus [i].IsTopLevel) {
  1204. Activate (i);
  1205. }
  1206. } else if (selected != i && selected > -1 && (me.Flags == MouseFlags.ReportMousePosition ||
  1207. me.Flags == MouseFlags.Button1Pressed && me.Flags == MouseFlags.ReportMousePosition)) {
  1208. if (IsMenuOpen) {
  1209. CloseMenu (true, false);
  1210. Activate (i);
  1211. }
  1212. } else {
  1213. if (IsMenuOpen)
  1214. Activate (i);
  1215. }
  1216. return true;
  1217. }
  1218. pos += 1 + Menus [i].TitleLength + 2;
  1219. }
  1220. }
  1221. return false;
  1222. }
  1223. internal bool handled;
  1224. internal bool HandleGrabView (MouseEvent me, View current)
  1225. {
  1226. if (Application.mouseGrabView != null) {
  1227. if (me.View is MenuBar || me.View is Menu) {
  1228. if (me.View != current) {
  1229. Application.UngrabMouse ();
  1230. var v = me.View;
  1231. Application.GrabMouse (v);
  1232. var newxy = v.ScreenToView (me.X, me.Y);
  1233. var nme = new MouseEvent () {
  1234. X = newxy.X,
  1235. Y = newxy.Y,
  1236. Flags = me.Flags,
  1237. OfX = me.X - newxy.X,
  1238. OfY = me.Y - newxy.Y,
  1239. View = v
  1240. };
  1241. v.MouseEvent (nme);
  1242. }
  1243. } else if (!(me.View is MenuBar || me.View is Menu) && (me.Flags.HasFlag (MouseFlags.Button1Clicked) ||
  1244. me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked)) {
  1245. Application.UngrabMouse ();
  1246. CloseAllMenus ();
  1247. handled = false;
  1248. return false;
  1249. } else {
  1250. handled = false;
  1251. return false;
  1252. }
  1253. } else if (!IsMenuOpen && (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  1254. Application.GrabMouse (current);
  1255. } else if (IsMenuOpen && (me.View is MenuBar || me.View is Menu)) {
  1256. Application.GrabMouse (me.View);
  1257. } else {
  1258. handled = false;
  1259. return false;
  1260. }
  1261. //if (me.View != this && me.Flags != MouseFlags.Button1Pressed)
  1262. // return true;
  1263. //else if (me.View != this && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1264. // Application.UngrabMouse ();
  1265. // host.CloseAllMenus ();
  1266. // return true;
  1267. //}
  1268. //if (!(me.View is MenuBar) && !(me.View is Menu) && me.Flags != MouseFlags.Button1Pressed))
  1269. // return false;
  1270. //if (Application.mouseGrabView != null) {
  1271. // if (me.View is MenuBar || me.View is Menu) {
  1272. // me.X -= me.OfX;
  1273. // me.Y -= me.OfY;
  1274. // me.View.MouseEvent (me);
  1275. // return true;
  1276. // } else if (!(me.View is MenuBar || me.View is Menu) && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1277. // Application.UngrabMouse ();
  1278. // CloseAllMenus ();
  1279. // }
  1280. //} else if (!isMenuClosed && selected == -1 && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1281. // Application.GrabMouse (this);
  1282. // return true;
  1283. //}
  1284. //if (Application.mouseGrabView != null) {
  1285. // if (Application.mouseGrabView == me.View && me.View == current) {
  1286. // me.X -= me.OfX;
  1287. // me.Y -= me.OfY;
  1288. // } else if (me.View != current && me.View is MenuBar && me.View is Menu) {
  1289. // Application.UngrabMouse ();
  1290. // Application.GrabMouse (me.View);
  1291. // } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1292. // Application.UngrabMouse ();
  1293. // CloseMenu ();
  1294. // }
  1295. //} else if ((!isMenuClosed && selected > -1)) {
  1296. // Application.GrabMouse (current);
  1297. //}
  1298. handled = true;
  1299. return true;
  1300. }
  1301. }
  1302. }