Menu.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  1. //
  2. // Menu.cs: application menus and submenus
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // TODO:
  8. // Add accelerator support, but should also support chords (Shortcut in MenuItem)
  9. // Allow menus inside menus
  10. using System;
  11. using NStack;
  12. using System.Linq;
  13. using System.Collections.Generic;
  14. namespace Terminal.Gui {
  15. /// <summary>
  16. /// Specifies how a <see cref="MenuItem"/> shows selection state.
  17. /// </summary>
  18. [Flags]
  19. public enum MenuItemCheckStyle {
  20. /// <summary>
  21. /// The menu item will be shown normally, with no check indicator.
  22. /// </summary>
  23. NoCheck = 0b_0000_0000,
  24. /// <summary>
  25. /// The menu item will indicate checked/un-checked state (see <see cref="Checked"/>.
  26. /// </summary>
  27. Checked = 0b_0000_0001,
  28. /// <summary>
  29. /// The menu item is part of a menu radio group (see <see cref="Checked"/> and will indicate selected state.
  30. /// </summary>
  31. Radio = 0b_0000_0010,
  32. };
  33. /// <summary>
  34. /// A <see cref="MenuItem"/> has a title, an associated help text, and an action to execute on activation.
  35. /// </summary>
  36. public class MenuItem {
  37. ustring title;
  38. ShortcutHelper shortcutHelper;
  39. internal int TitleLength => GetMenuBarItemLength (Title);
  40. /// <summary>
  41. /// Gets or sets arbitrary data for the menu item.
  42. /// </summary>
  43. /// <remarks>This property is not used internally.</remarks>
  44. public object Data { get; set; }
  45. /// <summary>
  46. /// Initializes a new instance of <see cref="MenuItem"/>
  47. /// </summary>
  48. public MenuItem (Key shortcut = Key.Null) : this ("", "", null, null, null, shortcut) { }
  49. /// <summary>
  50. /// Initializes a new instance of <see cref="MenuItem"/>.
  51. /// </summary>
  52. /// <param name="title">Title for the menu item.</param>
  53. /// <param name="help">Help text to display.</param>
  54. /// <param name="action">Action to invoke when the menu item is activated.</param>
  55. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  56. /// <param name="parent">The <see cref="Parent"/> of this menu item.</param>
  57. /// <param name="shortcut">The <see cref="Shortcut"/> keystroke combination.</param>
  58. public MenuItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null, Key shortcut = Key.Null)
  59. {
  60. Title = title ?? "";
  61. Help = help ?? "";
  62. Action = action;
  63. CanExecute = canExecute;
  64. Parent = parent;
  65. shortcutHelper = new ShortcutHelper ();
  66. if (shortcut != Key.Null) {
  67. shortcutHelper.Shortcut = shortcut;
  68. }
  69. }
  70. /// <summary>
  71. /// The HotKey is used when the menu is active, the shortcut can be triggered when the menu is not active.
  72. /// For example HotKey would be "N" when the File Menu is open (assuming there is a "_New" entry
  73. /// if the Shortcut is set to "Control-N", this would be a global hotkey that would trigger as well
  74. /// </summary>
  75. public Rune HotKey;
  76. /// <summary>
  77. /// This is the global setting that can be used as a global <see cref="ShortcutHelper.Shortcut"/> to invoke the action on the menu.
  78. /// </summary>
  79. public Key Shortcut {
  80. get => shortcutHelper.Shortcut;
  81. set {
  82. if (shortcutHelper.Shortcut != value && (ShortcutHelper.PostShortcutValidation (value) || value == Key.Null)) {
  83. shortcutHelper.Shortcut = value;
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// The keystroke combination used in the <see cref="ShortcutHelper.ShortcutTag"/> as string.
  89. /// </summary>
  90. public ustring ShortcutTag => ShortcutHelper.GetShortcutTag (shortcutHelper.Shortcut);
  91. /// <summary>
  92. /// Gets or sets the title.
  93. /// </summary>
  94. /// <value>The title.</value>
  95. public ustring Title {
  96. get { return title; }
  97. set {
  98. if (title != value) {
  99. title = value;
  100. GetHotKey ();
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// Gets or sets the help text for the menu item.
  106. /// </summary>
  107. /// <value>The help text.</value>
  108. public ustring Help { get; set; }
  109. /// <summary>
  110. /// Gets or sets the action to be invoked when the menu is triggered
  111. /// </summary>
  112. /// <value>Method to invoke.</value>
  113. public Action Action { get; set; }
  114. /// <summary>
  115. /// Gets or sets the action to be invoked if the menu can be triggered
  116. /// </summary>
  117. /// <value>Function to determine if action is ready to be executed.</value>
  118. public Func<bool> CanExecute { get; set; }
  119. /// <summary>
  120. /// Shortcut to check if the menu item is enabled
  121. /// </summary>
  122. public bool IsEnabled ()
  123. {
  124. return CanExecute == null ? true : CanExecute ();
  125. }
  126. internal int Width => 1 + TitleLength + (Help.ConsoleWidth > 0 ? Help.ConsoleWidth + 2 : 0) +
  127. (Checked || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio) ? 2 : 0) +
  128. (ShortcutTag.ConsoleWidth > 0 ? ShortcutTag.ConsoleWidth + 2 : 0) + 2;
  129. /// <summary>
  130. /// Sets or gets whether the <see cref="MenuItem"/> shows a check indicator or not. See <see cref="MenuItemCheckStyle"/>.
  131. /// </summary>
  132. public bool Checked { set; get; }
  133. /// <summary>
  134. /// Sets or gets the type selection indicator the menu item will be displayed with.
  135. /// </summary>
  136. public MenuItemCheckStyle CheckType { get; set; }
  137. /// <summary>
  138. /// Gets or sets the parent for this <see cref="MenuItem"/>.
  139. /// </summary>
  140. /// <value>The parent.</value>
  141. public MenuItem Parent { get; internal set; }
  142. /// <summary>
  143. /// Gets if this <see cref="MenuItem"/> is from a sub-menu.
  144. /// </summary>
  145. internal bool IsFromSubMenu { get { return Parent != null; } }
  146. /// <summary>
  147. /// Merely a debugging aid to see the interaction with main
  148. /// </summary>
  149. public MenuItem GetMenuItem ()
  150. {
  151. return this;
  152. }
  153. /// <summary>
  154. /// Merely a debugging aid to see the interaction with main
  155. /// </summary>
  156. public bool GetMenuBarItem ()
  157. {
  158. return IsFromSubMenu;
  159. }
  160. void GetHotKey ()
  161. {
  162. bool nextIsHot = false;
  163. foreach (var x in title) {
  164. if (x == MenuBar.HotKeySpecifier) {
  165. nextIsHot = true;
  166. } else {
  167. if (nextIsHot) {
  168. HotKey = Char.ToUpper ((char)x);
  169. break;
  170. }
  171. nextIsHot = false;
  172. HotKey = default;
  173. }
  174. }
  175. }
  176. int GetMenuBarItemLength (ustring title)
  177. {
  178. int len = 0;
  179. foreach (var ch in title) {
  180. if (ch == MenuBar.HotKeySpecifier)
  181. continue;
  182. len += Math.Max (Rune.ColumnWidth (ch), 1);
  183. }
  184. return len;
  185. }
  186. }
  187. /// <summary>
  188. /// A <see cref="MenuBarItem"/> contains <see cref="MenuBarItem"/>s or <see cref="MenuItem"/>s.
  189. /// </summary>
  190. public class MenuBarItem : MenuItem {
  191. /// <summary>
  192. /// Initializes a new <see cref="MenuBarItem"/> as a <see cref="MenuItem"/>.
  193. /// </summary>
  194. /// <param name="title">Title for the menu item.</param>
  195. /// <param name="help">Help text to display.</param>
  196. /// <param name="action">Action to invoke when the menu item is activated.</param>
  197. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  198. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  199. public MenuBarItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null) : base (title, help, action, canExecute, parent)
  200. {
  201. Initialize (title, null, null, true);
  202. }
  203. /// <summary>
  204. /// Initializes a new <see cref="MenuBarItem"/>.
  205. /// </summary>
  206. /// <param name="title">Title for the menu item.</param>
  207. /// <param name="children">The items in the current menu.</param>
  208. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  209. public MenuBarItem (ustring title, MenuItem [] children, MenuItem parent = null)
  210. {
  211. Initialize (title, children, parent);
  212. }
  213. /// <summary>
  214. /// Initializes a new <see cref="MenuBarItem"/> with separate list of items.
  215. /// </summary>
  216. /// <param name="title">Title for the menu item.</param>
  217. /// <param name="children">The list of items in the current menu.</param>
  218. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  219. public MenuBarItem (ustring title, List<MenuItem []> children, MenuItem parent = null)
  220. {
  221. Initialize (title, children, parent);
  222. }
  223. /// <summary>
  224. /// Initializes a new <see cref="MenuBarItem"/>.
  225. /// </summary>
  226. /// <param name="children">The items in the current menu.</param>
  227. public MenuBarItem (MenuItem [] children) : this ("", children) { }
  228. /// <summary>
  229. /// Initializes a new <see cref="MenuBarItem"/>.
  230. /// </summary>
  231. public MenuBarItem () : this (children: new MenuItem [] { }) { }
  232. void Initialize (ustring title, object children, MenuItem parent = null, bool isTopLevel = false)
  233. {
  234. if (!isTopLevel && children == null) {
  235. throw new ArgumentNullException (nameof (children), "The parameter cannot be null. Use an empty array instead.");
  236. }
  237. SetTitle (title ?? "");
  238. if (parent != null) {
  239. Parent = parent;
  240. }
  241. if (children is List<MenuItem []>) {
  242. MenuItem [] childrens = new MenuItem [] { };
  243. foreach (var item in (List<MenuItem []>)children) {
  244. for (int i = 0; i < item.Length; i++) {
  245. SetChildrensParent (item);
  246. Array.Resize (ref childrens, childrens.Length + 1);
  247. childrens [childrens.Length - 1] = item [i];
  248. }
  249. }
  250. Children = childrens;
  251. } else if (children is MenuItem []) {
  252. SetChildrensParent ((MenuItem [])children);
  253. Children = (MenuItem [])children;
  254. } else {
  255. Children = null;
  256. }
  257. }
  258. //static int GetMaxTitleLength (MenuItem [] children)
  259. //{
  260. // int maxLength = 0;
  261. // foreach (var item in children) {
  262. // int len = GetMenuBarItemLength (item.Title);
  263. // if (len > maxLength)
  264. // maxLength = len;
  265. // item.IsFromSubMenu = true;
  266. // }
  267. // return maxLength;
  268. //}
  269. void SetChildrensParent (MenuItem [] childrens)
  270. {
  271. foreach (var child in childrens) {
  272. if (child != null && child.Parent == null) {
  273. child.Parent = this;
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// Check if the children parameter is a <see cref="MenuBarItem"/>.
  279. /// </summary>
  280. /// <param name="children"></param>
  281. /// <returns>Returns a <see cref="MenuBarItem"/> or null otherwise.</returns>
  282. public MenuBarItem SubMenu (MenuItem children)
  283. {
  284. return children as MenuBarItem;
  285. }
  286. /// <summary>
  287. /// Check if the <see cref="MenuItem"/> parameter is a child of this.
  288. /// </summary>
  289. /// <param name="menuItem"></param>
  290. /// <returns>Returns <c>true</c> if it is a child of this. <c>false</c> otherwise.</returns>
  291. public bool IsSubMenuOf (MenuItem menuItem)
  292. {
  293. foreach (var child in Children) {
  294. if (child == menuItem && child.Parent == menuItem.Parent) {
  295. return true;
  296. }
  297. }
  298. return false;
  299. }
  300. /// <summary>
  301. /// Get the index of the <see cref="MenuItem"/> parameter.
  302. /// </summary>
  303. /// <param name="children"></param>
  304. /// <returns>Returns a value bigger than -1 if the <see cref="MenuItem"/> is a child of this.</returns>
  305. public int GetChildrenIndex (MenuItem children)
  306. {
  307. if (Children?.Length == 0) {
  308. return -1;
  309. }
  310. int i = 0;
  311. foreach (var child in Children) {
  312. if (child == children) {
  313. return i;
  314. }
  315. i++;
  316. }
  317. return -1;
  318. }
  319. void SetTitle (ustring title)
  320. {
  321. if (title == null)
  322. title = ustring.Empty;
  323. Title = title;
  324. }
  325. ///// <summary>
  326. ///// Gets or sets the title to display.
  327. ///// </summary>
  328. ///// <value>The title.</value>
  329. //public ustring Title { get; set; }
  330. /// <summary>
  331. /// Gets or sets an array of <see cref="MenuItem"/> objects that are the children of this <see cref="MenuBarItem"/>
  332. /// </summary>
  333. /// <value>The children.</value>
  334. public MenuItem [] Children { get; set; }
  335. internal bool IsTopLevel { get => Parent == null && (Children == null || Children.Length == 0) && Action != null; }
  336. }
  337. class Menu : View {
  338. internal MenuBarItem barItems;
  339. internal MenuBar host;
  340. internal int current;
  341. internal View previousSubFocused;
  342. internal static Rect MakeFrame (int x, int y, MenuItem [] items)
  343. {
  344. if (items == null || items.Length == 0) {
  345. return new Rect ();
  346. }
  347. int maxW = items.Max (z => z?.Width) ?? 0;
  348. return new Rect (x, y, maxW + 2, items.Length + 2);
  349. }
  350. public Menu (MenuBar host, int x, int y, MenuBarItem barItems) : base (MakeFrame (x, y, barItems.Children))
  351. {
  352. this.barItems = barItems;
  353. this.host = host;
  354. if (barItems.IsTopLevel) {
  355. // This is a standalone MenuItem on a MenuBar
  356. ColorScheme = host.ColorScheme;
  357. CanFocus = true;
  358. } else {
  359. current = -1;
  360. for (int i = 0; i < barItems.Children?.Length; i++) {
  361. if (barItems.Children [i] != null) {
  362. current = i;
  363. break;
  364. }
  365. }
  366. ColorScheme = host.ColorScheme;
  367. CanFocus = true;
  368. WantMousePositionReports = host.WantMousePositionReports;
  369. }
  370. // Things this view knows how to do
  371. AddCommand (Command.LineUp, () => MoveUp ());
  372. AddCommand (Command.LineDown, () => MoveDown ());
  373. AddCommand (Command.Left, () => { this.host.PreviousMenu (true); return true; });
  374. AddCommand (Command.Right, () => {
  375. this.host.NextMenu (!this.barItems.IsTopLevel || (this.barItems.Children != null
  376. && this.barItems.Children.Length > 0 && current > -1
  377. && current < this.barItems.Children.Length && this.barItems.Children [current].IsFromSubMenu),
  378. this.barItems.Children != null && this.barItems.Children.Length > 0 && current > -1
  379. && host.UseSubMenusSingleFrame && this.barItems.SubMenu (this.barItems.Children [current]) != null);
  380. return true;
  381. });
  382. AddCommand (Command.Cancel, () => { CloseAllMenus (); return true; });
  383. AddCommand (Command.Accept, () => { RunSelected (); return true; });
  384. // Default keybindings for this view
  385. AddKeyBinding (Key.CursorUp, Command.LineUp);
  386. AddKeyBinding (Key.CursorDown, Command.LineDown);
  387. AddKeyBinding (Key.CursorLeft, Command.Left);
  388. AddKeyBinding (Key.CursorRight, Command.Right);
  389. AddKeyBinding (Key.Esc, Command.Cancel);
  390. AddKeyBinding (Key.Enter, Command.Accept);
  391. }
  392. internal Attribute DetermineColorSchemeFor (MenuItem item, int index)
  393. {
  394. if (item != null) {
  395. if (index == current) return ColorScheme.Focus;
  396. if (!item.IsEnabled ()) return ColorScheme.Disabled;
  397. }
  398. return GetNormalColor ();
  399. }
  400. public override void Redraw (Rect bounds)
  401. {
  402. Driver.SetAttribute (GetNormalColor ());
  403. DrawFrame (Bounds, padding: 0, fill: true);
  404. for (int i = Bounds.Y; i < barItems.Children.Length; i++) {
  405. if (i < 0)
  406. continue;
  407. var item = barItems.Children [i];
  408. Driver.SetAttribute (item == null ? GetNormalColor ()
  409. : i == current ? ColorScheme.Focus : GetNormalColor ());
  410. if (item == null) {
  411. Move (0, i + 1);
  412. Driver.AddRune (Driver.LeftTee);
  413. } else if (Frame.X + 1 < Driver.Cols)
  414. Move (1, i + 1);
  415. Driver.SetAttribute (DetermineColorSchemeFor (item, i));
  416. for (int p = Bounds.X; p < Frame.Width - 2; p++) {
  417. if (p < 0)
  418. continue;
  419. if (item == null)
  420. Driver.AddRune (Driver.HLine);
  421. else if (i == 0 && p == 0 && host.UseSubMenusSingleFrame && item.Parent.Parent != null)
  422. Driver.AddRune (Driver.LeftArrow);
  423. else if (p == Frame.Width - 3 && barItems.SubMenu (barItems.Children [i]) != null)
  424. Driver.AddRune (Driver.RightArrow);
  425. else
  426. Driver.AddRune (' ');
  427. }
  428. if (item == null) {
  429. if (SuperView?.Frame.Right - Frame.X > Frame.Width - 1) {
  430. Move (Frame.Width - 1, i + 1);
  431. Driver.AddRune (Driver.RightTee);
  432. }
  433. continue;
  434. }
  435. ustring textToDraw;
  436. var checkChar = Driver.Selected;
  437. var uncheckedChar = Driver.UnSelected;
  438. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked)) {
  439. checkChar = Driver.Checked;
  440. uncheckedChar = Driver.UnChecked;
  441. }
  442. // Support Checked even though CheckType wasn't set
  443. if (item.Checked) {
  444. textToDraw = ustring.Make (new Rune [] { checkChar, ' ' }) + item.Title;
  445. } else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio)) {
  446. textToDraw = ustring.Make (new Rune [] { uncheckedChar, ' ' }) + item.Title;
  447. } else {
  448. textToDraw = item.Title;
  449. }
  450. ViewToScreen (2, i + 1, out int vtsCol, out _, false);
  451. if (vtsCol < Driver.Cols) {
  452. Move (2, i + 1);
  453. if (!item.IsEnabled ()) {
  454. DrawHotString (textToDraw, ColorScheme.Disabled, ColorScheme.Disabled);
  455. } else if (i == 0 && host.UseSubMenusSingleFrame && item.Parent.Parent != null) {
  456. var tf = new TextFormatter () {
  457. Alignment = TextAlignment.Centered,
  458. HotKeySpecifier = MenuBar.HotKeySpecifier,
  459. Text = textToDraw
  460. };
  461. tf.Draw (ViewToScreen (new Rect (2, i + 1, Frame.Width - 3, 1)),
  462. i == current ? ColorScheme.Focus : GetNormalColor (),
  463. i == current ? ColorScheme.HotFocus : ColorScheme.HotNormal,
  464. SuperView == null ? default : SuperView.ViewToScreen (SuperView.Bounds));
  465. } else {
  466. DrawHotString (textToDraw,
  467. i == current ? ColorScheme.HotFocus : ColorScheme.HotNormal,
  468. i == current ? ColorScheme.Focus : GetNormalColor ());
  469. }
  470. // The help string
  471. var l = item.ShortcutTag.ConsoleWidth == 0 ? item.Help.ConsoleWidth : item.Help.ConsoleWidth + item.ShortcutTag.ConsoleWidth + 2;
  472. var col = Frame.Width - l - 2;
  473. ViewToScreen (col, i + 1, out vtsCol, out _, false);
  474. if (vtsCol < Driver.Cols) {
  475. Move (col, 1 + i);
  476. Driver.AddStr (item.Help);
  477. // The shortcut tag string
  478. if (!item.ShortcutTag.IsEmpty) {
  479. l = item.ShortcutTag.ConsoleWidth;
  480. Move (Frame.Width - l - 2, 1 + i);
  481. Driver.AddStr (item.ShortcutTag);
  482. }
  483. }
  484. }
  485. }
  486. PositionCursor ();
  487. }
  488. public override void PositionCursor ()
  489. {
  490. if (host == null || host.IsMenuOpen)
  491. if (barItems.IsTopLevel) {
  492. host.PositionCursor ();
  493. } else
  494. Move (2, 1 + current);
  495. else
  496. host.PositionCursor ();
  497. }
  498. public void Run (Action action)
  499. {
  500. if (action == null)
  501. return;
  502. Application.UngrabMouse ();
  503. host.CloseAllMenus ();
  504. Application.Refresh ();
  505. Application.MainLoop.AddIdle (() => {
  506. action ();
  507. return false;
  508. });
  509. }
  510. public override bool OnLeave (View view)
  511. {
  512. return host.OnLeave (view);
  513. }
  514. public override bool OnKeyDown (KeyEvent keyEvent)
  515. {
  516. if (keyEvent.IsAlt) {
  517. host.CloseAllMenus ();
  518. return true;
  519. }
  520. return false;
  521. }
  522. public override bool ProcessHotKey (KeyEvent keyEvent)
  523. {
  524. // To ncurses simulate a AltMask key pressing Alt+Space because
  525. // it can�t detect an alone special key down was pressed.
  526. if (keyEvent.IsAlt && keyEvent.Key == Key.AltMask) {
  527. OnKeyDown (keyEvent);
  528. return true;
  529. }
  530. return false;
  531. }
  532. public override bool ProcessKey (KeyEvent kb)
  533. {
  534. var result = InvokeKeybindings (kb);
  535. if (result != null)
  536. return (bool)result;
  537. // TODO: rune-ify
  538. if (barItems.Children != null && Char.IsLetterOrDigit ((char)kb.KeyValue)) {
  539. var x = Char.ToUpper ((char)kb.KeyValue);
  540. var idx = -1;
  541. foreach (var item in barItems.Children) {
  542. idx++;
  543. if (item == null) continue;
  544. if (item.IsEnabled () && item.HotKey == x) {
  545. current = idx;
  546. RunSelected ();
  547. return true;
  548. }
  549. }
  550. }
  551. return false;
  552. }
  553. void RunSelected ()
  554. {
  555. if (barItems.IsTopLevel) {
  556. Run (barItems.Action);
  557. } else if (current > -1 && barItems.Children [current].Action != null) {
  558. Run (barItems.Children [current].Action);
  559. } else if (current == 0 && host.UseSubMenusSingleFrame
  560. && barItems.Children [current].Parent.Parent != null) {
  561. host.PreviousMenu (barItems.Children [current].Parent.IsFromSubMenu, true);
  562. } else if (current > -1 && barItems.SubMenu (barItems.Children [current]) != null) {
  563. CheckSubMenu ();
  564. }
  565. }
  566. void CloseAllMenus ()
  567. {
  568. Application.UngrabMouse ();
  569. host.CloseAllMenus ();
  570. }
  571. bool MoveDown ()
  572. {
  573. if (barItems.IsTopLevel) {
  574. return true;
  575. }
  576. bool disabled;
  577. do {
  578. current++;
  579. if (current >= barItems.Children.Length) {
  580. current = 0;
  581. }
  582. if (this != host.openCurrentMenu && barItems.Children [current]?.IsFromSubMenu == true && host.selectedSub > -1) {
  583. host.PreviousMenu (true);
  584. host.SelectEnabledItem (barItems.Children, current, out current);
  585. host.openCurrentMenu = this;
  586. }
  587. var item = barItems.Children [current];
  588. if (item?.IsEnabled () != true) {
  589. disabled = true;
  590. } else {
  591. disabled = false;
  592. }
  593. if (!host.UseSubMenusSingleFrame && host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
  594. !disabled && host.IsMenuOpen) {
  595. if (!CheckSubMenu ())
  596. return false;
  597. break;
  598. }
  599. if (!host.IsMenuOpen) {
  600. host.OpenMenu (host.selected);
  601. }
  602. } while (barItems.Children [current] == null || disabled);
  603. SetNeedsDisplay ();
  604. if (!host.UseSubMenusSingleFrame)
  605. host.OnMenuOpened ();
  606. return true;
  607. }
  608. bool MoveUp ()
  609. {
  610. if (barItems.IsTopLevel || current == -1) {
  611. return true;
  612. }
  613. bool disabled;
  614. do {
  615. current--;
  616. if (host.UseKeysUpDownAsKeysLeftRight && !host.UseSubMenusSingleFrame) {
  617. if ((current == -1 || this != host.openCurrentMenu) && barItems.Children [current + 1].IsFromSubMenu && host.selectedSub > -1) {
  618. current++;
  619. host.PreviousMenu (true);
  620. if (current > 0) {
  621. current--;
  622. host.openCurrentMenu = this;
  623. }
  624. break;
  625. }
  626. }
  627. if (current < 0)
  628. current = barItems.Children.Length - 1;
  629. if (!host.SelectEnabledItem (barItems.Children, current, out current, false)) {
  630. current = 0;
  631. if (!host.SelectEnabledItem (barItems.Children, current, out current) && !host.CloseMenu (false)) {
  632. return false;
  633. }
  634. break;
  635. }
  636. var item = barItems.Children [current];
  637. if (item?.IsEnabled () != true) {
  638. disabled = true;
  639. } else {
  640. disabled = false;
  641. }
  642. if (!host.UseSubMenusSingleFrame && host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
  643. !disabled && host.IsMenuOpen) {
  644. if (!CheckSubMenu ())
  645. return false;
  646. break;
  647. }
  648. } while (barItems.Children [current] == null || disabled);
  649. SetNeedsDisplay ();
  650. if (!host.UseSubMenusSingleFrame)
  651. host.OnMenuOpened ();
  652. return true;
  653. }
  654. public override bool MouseEvent (MouseEvent me)
  655. {
  656. if (!host.handled && !host.HandleGrabView (me, this)) {
  657. return false;
  658. }
  659. host.handled = false;
  660. bool disabled;
  661. if (me.Flags == MouseFlags.Button1Clicked) {
  662. disabled = false;
  663. if (me.Y < 1)
  664. return true;
  665. var meY = me.Y - 1;
  666. if (meY >= barItems.Children.Length)
  667. return true;
  668. var item = barItems.Children [meY];
  669. if (item == null || !item.IsEnabled ()) disabled = true;
  670. current = meY;
  671. if (item != null && !disabled)
  672. RunSelected ();
  673. return true;
  674. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked ||
  675. me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.ReportMousePosition ||
  676. me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  677. disabled = false;
  678. if (me.Y < 1 || me.Y - 1 >= barItems.Children.Length) {
  679. return true;
  680. }
  681. var item = barItems.Children [me.Y - 1];
  682. if (item == null) return true;
  683. if (item == null || !item.IsEnabled ()) disabled = true;
  684. if (item != null && !disabled)
  685. current = me.Y - 1;
  686. if (host.UseSubMenusSingleFrame || !CheckSubMenu ()) {
  687. SetNeedsDisplay ();
  688. return true;
  689. }
  690. host.OnMenuOpened ();
  691. return true;
  692. }
  693. return false;
  694. }
  695. internal bool CheckSubMenu ()
  696. {
  697. if (current == -1 || barItems.Children [current] == null) {
  698. return true;
  699. }
  700. var subMenu = barItems.SubMenu (barItems.Children [current]);
  701. if (subMenu != null) {
  702. int pos = -1;
  703. if (host.openSubMenu != null) {
  704. pos = host.openSubMenu.FindIndex (o => o?.barItems == subMenu);
  705. }
  706. if (pos == -1 && this != host.openCurrentMenu && subMenu.Children != host.openCurrentMenu.barItems.Children
  707. && !host.CloseMenu (false, true)) {
  708. return false;
  709. }
  710. host.Activate (host.selected, pos, subMenu);
  711. } else if (host.openSubMenu?.Count == 0 || host.openSubMenu?.Last ().barItems.IsSubMenuOf (barItems.Children [current]) == false) {
  712. return host.CloseMenu (false, true);
  713. } else {
  714. SetNeedsDisplay ();
  715. }
  716. return true;
  717. }
  718. int GetSubMenuIndex (MenuBarItem subMenu)
  719. {
  720. int pos = -1;
  721. if (this != null && Subviews.Count > 0) {
  722. Menu v = null;
  723. foreach (var menu in Subviews) {
  724. if (((Menu)menu).barItems == subMenu)
  725. v = (Menu)menu;
  726. }
  727. if (v != null)
  728. pos = Subviews.IndexOf (v);
  729. }
  730. return pos;
  731. }
  732. ///<inheritdoc/>
  733. public override bool OnEnter (View view)
  734. {
  735. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  736. return base.OnEnter (view);
  737. }
  738. }
  739. /// <summary>
  740. /// Provides a menu bar with drop-down and cascading menus.
  741. /// </summary>
  742. /// <remarks>
  743. /// <para>
  744. /// The <see cref="MenuBar"/> appears on the first row of the terminal.
  745. /// </para>
  746. /// <para>
  747. /// The <see cref="MenuBar"/> provides global hotkeys for the application.
  748. /// </para>
  749. /// </remarks>
  750. public class MenuBar : View {
  751. internal int selected;
  752. internal int selectedSub;
  753. /// <summary>
  754. /// Gets or sets the array of <see cref="MenuBarItem"/>s for the menu. Only set this when the <see cref="MenuBar"/> is visible.
  755. /// </summary>
  756. /// <value>The menu array.</value>
  757. public MenuBarItem [] Menus { get; set; }
  758. private bool useKeysUpDownAsKeysLeftRight = false;
  759. /// <summary>
  760. /// Used for change the navigation key style.
  761. /// </summary>
  762. public bool UseKeysUpDownAsKeysLeftRight {
  763. get => useKeysUpDownAsKeysLeftRight;
  764. set {
  765. useKeysUpDownAsKeysLeftRight = value;
  766. if (value && UseSubMenusSingleFrame) {
  767. UseSubMenusSingleFrame = false;
  768. SetNeedsDisplay ();
  769. }
  770. }
  771. }
  772. static ustring shortcutDelimiter = "+";
  773. /// <summary>
  774. /// Used for change the shortcut delimiter separator.
  775. /// </summary>
  776. public static ustring ShortcutDelimiter {
  777. get => shortcutDelimiter;
  778. set {
  779. if (shortcutDelimiter != value) {
  780. shortcutDelimiter = value == ustring.Empty ? " " : value;
  781. }
  782. }
  783. }
  784. /// <summary>
  785. /// The specifier character for the hotkey to all menus.
  786. /// </summary>
  787. new public static Rune HotKeySpecifier => '_';
  788. private bool useSubMenusSingleFrame;
  789. /// <summary>
  790. /// Gets or sets if the sub-menus must be displayed in a single or multiple frames.
  791. /// </summary>
  792. public bool UseSubMenusSingleFrame {
  793. get => useSubMenusSingleFrame;
  794. set {
  795. useSubMenusSingleFrame = value;
  796. if (value && UseKeysUpDownAsKeysLeftRight) {
  797. useKeysUpDownAsKeysLeftRight = false;
  798. SetNeedsDisplay ();
  799. }
  800. }
  801. }
  802. /// <summary>
  803. /// Initializes a new instance of the <see cref="MenuBar"/>.
  804. /// </summary>
  805. public MenuBar () : this (new MenuBarItem [] { }) { }
  806. /// <summary>
  807. /// Initializes a new instance of the <see cref="MenuBar"/> class with the specified set of toplevel menu items.
  808. /// </summary>
  809. /// <param name="menus">Individual menu items; a null item will result in a separator being drawn.</param>
  810. public MenuBar (MenuBarItem [] menus) : base ()
  811. {
  812. X = 0;
  813. Y = 0;
  814. Width = Dim.Fill ();
  815. Height = 1;
  816. Menus = menus;
  817. //CanFocus = true;
  818. selected = -1;
  819. selectedSub = -1;
  820. ColorScheme = Colors.Menu;
  821. WantMousePositionReports = true;
  822. IsMenuOpen = false;
  823. // Things this view knows how to do
  824. AddCommand (Command.Left, () => { MoveLeft (); return true; });
  825. AddCommand (Command.Right, () => { MoveRight (); return true; });
  826. AddCommand (Command.Cancel, () => { CloseMenuBar (); return true; });
  827. AddCommand (Command.Accept, () => { ProcessMenu (selected, Menus [selected]); return true; });
  828. // Default keybindings for this view
  829. AddKeyBinding (Key.CursorLeft, Command.Left);
  830. AddKeyBinding (Key.CursorRight, Command.Right);
  831. AddKeyBinding (Key.Esc, Command.Cancel);
  832. AddKeyBinding (Key.C | Key.CtrlMask, Command.Cancel);
  833. AddKeyBinding (Key.CursorDown, Command.Accept);
  834. AddKeyBinding (Key.Enter, Command.Accept);
  835. }
  836. bool openedByAltKey;
  837. bool isCleaning;
  838. ///<inheritdoc/>
  839. public override bool OnLeave (View view)
  840. {
  841. if ((!(view is MenuBar) && !(view is Menu) || !(view is MenuBar) && !(view is Menu) && openMenu != null) && !isCleaning && !reopen) {
  842. CleanUp ();
  843. return true;
  844. }
  845. return false;
  846. }
  847. ///<inheritdoc/>
  848. public override bool OnKeyDown (KeyEvent keyEvent)
  849. {
  850. if (keyEvent.IsAlt || (keyEvent.IsCtrl && keyEvent.Key == (Key.CtrlMask | Key.Space))) {
  851. openedByAltKey = true;
  852. SetNeedsDisplay ();
  853. openedByHotKey = false;
  854. }
  855. return false;
  856. }
  857. ///<inheritdoc/>
  858. public override bool OnKeyUp (KeyEvent keyEvent)
  859. {
  860. if (keyEvent.IsAlt || keyEvent.Key == Key.AltMask || (keyEvent.IsCtrl && keyEvent.Key == (Key.CtrlMask | Key.Space))) {
  861. // User pressed Alt - this may be a precursor to a menu accelerator (e.g. Alt-F)
  862. if (openedByAltKey && !IsMenuOpen && openMenu == null && (((uint)keyEvent.Key & (uint)Key.CharMask) == 0
  863. || ((uint)keyEvent.Key & (uint)Key.CharMask) == (uint)Key.Space)) {
  864. // There's no open menu, the first menu item should be highlight.
  865. // The right way to do this is to SetFocus(MenuBar), but for some reason
  866. // that faults.
  867. var mbar = GetMouseGrabViewInstance (this);
  868. if (mbar != null) {
  869. mbar.CleanUp ();
  870. }
  871. //Activate (0);
  872. //StartMenu ();
  873. IsMenuOpen = true;
  874. selected = 0;
  875. CanFocus = true;
  876. lastFocused = SuperView == null ? Application.Current.MostFocused : SuperView.MostFocused;
  877. SetFocus ();
  878. SetNeedsDisplay ();
  879. Application.GrabMouse (this);
  880. } else if (!openedByHotKey) {
  881. // There's an open menu. If this Alt key-up is a pre-cursor to an accelerator
  882. // we don't want to close the menu because it'll flash.
  883. // How to deal with that?
  884. CleanUp ();
  885. }
  886. return true;
  887. }
  888. return false;
  889. }
  890. internal void CleanUp ()
  891. {
  892. isCleaning = true;
  893. if (openMenu != null) {
  894. CloseAllMenus ();
  895. }
  896. openedByAltKey = false;
  897. IsMenuOpen = false;
  898. selected = -1;
  899. CanFocus = false;
  900. if (lastFocused != null) {
  901. lastFocused.SetFocus ();
  902. }
  903. SetNeedsDisplay ();
  904. Application.UngrabMouse ();
  905. isCleaning = false;
  906. }
  907. ///<inheritdoc/>
  908. public override void Redraw (Rect bounds)
  909. {
  910. Move (0, 0);
  911. Driver.SetAttribute (GetNormalColor ());
  912. for (int i = 0; i < Frame.Width; i++)
  913. Driver.AddRune (' ');
  914. Move (1, 0);
  915. int pos = 1;
  916. for (int i = 0; i < Menus.Length; i++) {
  917. var menu = Menus [i];
  918. Move (pos, 0);
  919. Attribute hotColor, normalColor;
  920. if (i == selected && IsMenuOpen) {
  921. hotColor = i == selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  922. normalColor = i == selected ? ColorScheme.Focus :
  923. GetNormalColor ();
  924. } else if (openedByAltKey) {
  925. hotColor = ColorScheme.HotNormal;
  926. normalColor = GetNormalColor ();
  927. } else {
  928. hotColor = GetNormalColor ();
  929. normalColor = GetNormalColor ();
  930. }
  931. DrawHotString (menu.Help.IsEmpty ? $" {menu.Title} " : $" {menu.Title} {menu.Help} ", hotColor, normalColor);
  932. pos += 1 + menu.TitleLength + (menu.Help.ConsoleWidth > 0 ? menu.Help.ConsoleWidth + 2 : 0) + 2;
  933. }
  934. PositionCursor ();
  935. }
  936. ///<inheritdoc/>
  937. public override void PositionCursor ()
  938. {
  939. if (selected == -1 && HasFocus && Menus.Length > 0) {
  940. selected = 0;
  941. }
  942. int pos = 0;
  943. for (int i = 0; i < Menus.Length; i++) {
  944. if (i == selected) {
  945. pos++;
  946. if (IsMenuOpen)
  947. Move (pos + 1, 0);
  948. else {
  949. Move (pos + 1, 0);
  950. }
  951. return;
  952. } else {
  953. pos += 1 + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + 2 : 0) + 2;
  954. }
  955. }
  956. }
  957. void Selected (MenuItem item)
  958. {
  959. var action = item.Action;
  960. if (action == null)
  961. return;
  962. Application.UngrabMouse ();
  963. CloseAllMenus ();
  964. Application.Refresh ();
  965. Application.MainLoop.AddIdle (() => {
  966. action ();
  967. return false;
  968. });
  969. }
  970. /// <summary>
  971. /// Raised as a menu is opening.
  972. /// </summary>
  973. public event Action<MenuOpeningEventArgs> MenuOpening;
  974. /// <summary>
  975. /// Raised when a menu is opened.
  976. /// </summary>
  977. public event Action<MenuItem> MenuOpened;
  978. /// <summary>
  979. /// Raised when a menu is closing passing <see cref="MenuClosingEventArgs"/>.
  980. /// </summary>
  981. public event Action<MenuClosingEventArgs> MenuClosing;
  982. /// <summary>
  983. /// Raised when all the menu are closed.
  984. /// </summary>
  985. public event Action MenuAllClosed;
  986. internal Menu openMenu;
  987. Menu ocm;
  988. internal Menu openCurrentMenu {
  989. get => ocm;
  990. set {
  991. if (ocm != value) {
  992. ocm = value;
  993. if (ocm.current > -1) {
  994. OnMenuOpened ();
  995. }
  996. }
  997. }
  998. }
  999. internal List<Menu> openSubMenu;
  1000. View previousFocused;
  1001. internal bool isMenuOpening;
  1002. internal bool isMenuClosing;
  1003. /// <summary>
  1004. /// True if the menu is open; otherwise false.
  1005. /// </summary>
  1006. public bool IsMenuOpen { get; protected set; }
  1007. /// <summary>
  1008. /// Virtual method that will invoke the <see cref="MenuOpening"/> event if it's defined.
  1009. /// </summary>
  1010. /// <param name="currentMenu">The current menu to be replaced.</param>
  1011. /// <returns>Returns the <see cref="MenuOpeningEventArgs"/></returns>
  1012. public virtual MenuOpeningEventArgs OnMenuOpening (MenuBarItem currentMenu)
  1013. {
  1014. var ev = new MenuOpeningEventArgs (currentMenu);
  1015. MenuOpening?.Invoke (ev);
  1016. return ev;
  1017. }
  1018. /// <summary>
  1019. /// Virtual method that will invoke the <see cref="MenuOpened"/> event if it's defined.
  1020. /// </summary>
  1021. public virtual void OnMenuOpened ()
  1022. {
  1023. MenuItem mi = null;
  1024. if (openCurrentMenu.barItems.Children != null && openCurrentMenu?.current > -1) {
  1025. mi = openCurrentMenu.barItems.Children [openCurrentMenu.current];
  1026. } else if (openCurrentMenu.barItems.IsTopLevel) {
  1027. mi = openCurrentMenu.barItems;
  1028. } else {
  1029. mi = openMenu.barItems.Children [openMenu.current];
  1030. }
  1031. MenuOpened?.Invoke (mi);
  1032. }
  1033. /// <summary>
  1034. /// Virtual method that will invoke the <see cref="MenuClosing"/>
  1035. /// </summary>
  1036. /// <param name="currentMenu">The current menu to be closed.</param>
  1037. /// <param name="reopen">Whether the current menu will be reopen.</param>
  1038. /// <param name="isSubMenu">Whether is a sub-menu or not.</param>
  1039. public virtual MenuClosingEventArgs OnMenuClosing (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  1040. {
  1041. var ev = new MenuClosingEventArgs (currentMenu, reopen, isSubMenu);
  1042. MenuClosing?.Invoke (ev);
  1043. return ev;
  1044. }
  1045. /// <summary>
  1046. /// Virtual method that will invoke the <see cref="MenuAllClosed"/>
  1047. /// </summary>
  1048. public virtual void OnMenuAllClosed ()
  1049. {
  1050. MenuAllClosed?.Invoke ();
  1051. }
  1052. View lastFocused;
  1053. /// <summary>
  1054. /// Get the lasted focused view before open the menu.
  1055. /// </summary>
  1056. public View LastFocused { get; private set; }
  1057. internal void OpenMenu (int index, int sIndex = -1, MenuBarItem subMenu = null)
  1058. {
  1059. isMenuOpening = true;
  1060. var newMenu = OnMenuOpening (Menus [index]);
  1061. if (newMenu.Cancel) {
  1062. isMenuOpening = false;
  1063. return;
  1064. }
  1065. if (newMenu.NewMenuBarItem != null) {
  1066. Menus [index] = newMenu.NewMenuBarItem;
  1067. }
  1068. int pos = 0;
  1069. switch (subMenu) {
  1070. case null:
  1071. lastFocused = lastFocused ?? (SuperView == null ? Application.Current.MostFocused : SuperView.MostFocused);
  1072. if (openSubMenu != null && !CloseMenu (false, true))
  1073. return;
  1074. if (openMenu != null) {
  1075. if (SuperView == null) {
  1076. Application.Current.Remove (openMenu);
  1077. } else {
  1078. SuperView.Remove (openMenu);
  1079. }
  1080. openMenu.Dispose ();
  1081. }
  1082. for (int i = 0; i < index; i++)
  1083. pos += 1 + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + 2 : 0) + 2;
  1084. openMenu = new Menu (this, Frame.X + pos, Frame.Y + 1, Menus [index]);
  1085. openCurrentMenu = openMenu;
  1086. openCurrentMenu.previousSubFocused = openMenu;
  1087. if (SuperView == null) {
  1088. Application.Current.Add (openMenu);
  1089. } else {
  1090. SuperView.Add (openMenu);
  1091. }
  1092. openMenu.SetFocus ();
  1093. break;
  1094. default:
  1095. if (openSubMenu == null)
  1096. openSubMenu = new List<Menu> ();
  1097. if (sIndex > -1) {
  1098. RemoveSubMenu (sIndex);
  1099. } else {
  1100. var last = openSubMenu.Count > 0 ? openSubMenu.Last () : openMenu;
  1101. if (!UseSubMenusSingleFrame) {
  1102. openCurrentMenu = new Menu (this, last.Frame.Left + last.Frame.Width, last.Frame.Top + 1 + last.current, subMenu);
  1103. } else {
  1104. var first = openSubMenu.Count > 0 ? openSubMenu.First () : openMenu;
  1105. var mbi = new MenuItem [2 + subMenu.Children.Length];
  1106. mbi [0] = new MenuItem () { Title = subMenu.Title, Parent = subMenu };
  1107. mbi [1] = null;
  1108. for (int j = 0; j < subMenu.Children.Length; j++) {
  1109. mbi [j + 2] = subMenu.Children [j];
  1110. }
  1111. var newSubMenu = new MenuBarItem (mbi);
  1112. openCurrentMenu = new Menu (this, first.Frame.Left, first.Frame.Top, newSubMenu);
  1113. last.Visible = false;
  1114. Application.GrabMouse (openCurrentMenu);
  1115. }
  1116. openCurrentMenu.previousSubFocused = last.previousSubFocused;
  1117. openSubMenu.Add (openCurrentMenu);
  1118. if (SuperView == null) {
  1119. Application.Current.Add (openCurrentMenu);
  1120. } else {
  1121. SuperView.Add (openCurrentMenu);
  1122. }
  1123. }
  1124. selectedSub = openSubMenu.Count - 1;
  1125. if (selectedSub > -1 && SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1126. openCurrentMenu.SetFocus ();
  1127. }
  1128. break;
  1129. }
  1130. isMenuOpening = false;
  1131. IsMenuOpen = true;
  1132. }
  1133. /// <summary>
  1134. /// Opens the current Menu programatically.
  1135. /// </summary>
  1136. public void OpenMenu ()
  1137. {
  1138. var mbar = GetMouseGrabViewInstance (this);
  1139. if (mbar != null) {
  1140. mbar.CleanUp ();
  1141. }
  1142. if (openMenu != null)
  1143. return;
  1144. selected = 0;
  1145. SetNeedsDisplay ();
  1146. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1147. OpenMenu (selected);
  1148. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current) && !CloseMenu (false)) {
  1149. return;
  1150. }
  1151. if (!openCurrentMenu.CheckSubMenu ())
  1152. return;
  1153. Application.GrabMouse (this);
  1154. }
  1155. // Activates the menu, handles either first focus, or activating an entry when it was already active
  1156. // For mouse events.
  1157. internal void Activate (int idx, int sIdx = -1, MenuBarItem subMenu = null)
  1158. {
  1159. selected = idx;
  1160. selectedSub = sIdx;
  1161. if (openMenu == null)
  1162. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1163. OpenMenu (idx, sIdx, subMenu);
  1164. SetNeedsDisplay ();
  1165. }
  1166. internal bool SelectEnabledItem (IEnumerable<MenuItem> chldren, int current, out int newCurrent, bool forward = true)
  1167. {
  1168. if (chldren == null) {
  1169. newCurrent = -1;
  1170. return true;
  1171. }
  1172. IEnumerable<MenuItem> childrens;
  1173. if (forward) {
  1174. childrens = chldren;
  1175. } else {
  1176. childrens = chldren.Reverse ();
  1177. }
  1178. int count;
  1179. if (forward) {
  1180. count = -1;
  1181. } else {
  1182. count = childrens.Count ();
  1183. }
  1184. foreach (var child in childrens) {
  1185. if (forward) {
  1186. if (++count < current) {
  1187. continue;
  1188. }
  1189. } else {
  1190. if (--count > current) {
  1191. continue;
  1192. }
  1193. }
  1194. if (child == null || !child.IsEnabled ()) {
  1195. if (forward) {
  1196. current++;
  1197. } else {
  1198. current--;
  1199. }
  1200. } else {
  1201. newCurrent = current;
  1202. return true;
  1203. }
  1204. }
  1205. newCurrent = -1;
  1206. return false;
  1207. }
  1208. /// <summary>
  1209. /// Closes the current Menu programatically, if open and not canceled.
  1210. /// </summary>
  1211. public bool CloseMenu (bool ignoreUseSubMenusSingleFrame = false)
  1212. {
  1213. return CloseMenu (false, false, ignoreUseSubMenusSingleFrame);
  1214. }
  1215. bool reopen;
  1216. internal bool CloseMenu (bool reopen = false, bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1217. {
  1218. var mbi = isSubMenu ? openCurrentMenu.barItems : openMenu?.barItems;
  1219. if (UseSubMenusSingleFrame && mbi != null &&
  1220. !ignoreUseSubMenusSingleFrame && mbi.Parent != null) {
  1221. return false;
  1222. }
  1223. isMenuClosing = true;
  1224. this.reopen = reopen;
  1225. var args = OnMenuClosing (mbi, reopen, isSubMenu);
  1226. if (args.Cancel) {
  1227. isMenuClosing = false;
  1228. if (args.CurrentMenu.Parent != null)
  1229. openMenu.current = ((MenuBarItem)args.CurrentMenu.Parent).Children.IndexOf (args.CurrentMenu);
  1230. return false;
  1231. }
  1232. switch (isSubMenu) {
  1233. case false:
  1234. if (openMenu != null) {
  1235. if (SuperView == null) {
  1236. Application.Current.Remove (openMenu);
  1237. } else {
  1238. SuperView?.Remove (openMenu);
  1239. }
  1240. }
  1241. SetNeedsDisplay ();
  1242. if (previousFocused != null && previousFocused is Menu && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
  1243. previousFocused.SetFocus ();
  1244. openMenu?.Dispose ();
  1245. openMenu = null;
  1246. if (lastFocused is Menu || lastFocused is MenuBar) {
  1247. lastFocused = null;
  1248. }
  1249. LastFocused = lastFocused;
  1250. lastFocused = null;
  1251. if (LastFocused != null && LastFocused.CanFocus) {
  1252. if (!reopen) {
  1253. selected = -1;
  1254. }
  1255. LastFocused.SetFocus ();
  1256. } else {
  1257. SetFocus ();
  1258. PositionCursor ();
  1259. }
  1260. IsMenuOpen = false;
  1261. break;
  1262. case true:
  1263. selectedSub = -1;
  1264. SetNeedsDisplay ();
  1265. RemoveAllOpensSubMenus ();
  1266. openCurrentMenu.previousSubFocused.SetFocus ();
  1267. openSubMenu = null;
  1268. IsMenuOpen = true;
  1269. break;
  1270. }
  1271. this.reopen = false;
  1272. isMenuClosing = false;
  1273. return true;
  1274. }
  1275. void RemoveSubMenu (int index, bool ignoreUseSubMenusSingleFrame = false)
  1276. {
  1277. if (openSubMenu == null || (UseSubMenusSingleFrame
  1278. && !ignoreUseSubMenusSingleFrame && openSubMenu.Count == 0))
  1279. return;
  1280. for (int i = openSubMenu.Count - 1; i > index; i--) {
  1281. isMenuClosing = true;
  1282. Menu menu;
  1283. if (openSubMenu.Count - 1 > 0)
  1284. menu = openSubMenu [i - 1];
  1285. else
  1286. menu = openMenu;
  1287. if (!menu.Visible)
  1288. menu.Visible = true;
  1289. openCurrentMenu = menu;
  1290. openCurrentMenu.SetFocus ();
  1291. if (openSubMenu != null) {
  1292. menu = openSubMenu [i];
  1293. if (SuperView == null) {
  1294. Application.Current.Remove (menu);
  1295. } else {
  1296. SuperView.Remove (menu);
  1297. }
  1298. openSubMenu.Remove (menu);
  1299. menu.Dispose ();
  1300. }
  1301. RemoveSubMenu (i, ignoreUseSubMenusSingleFrame);
  1302. }
  1303. if (openSubMenu.Count > 0)
  1304. openCurrentMenu = openSubMenu.Last ();
  1305. //if (openMenu.Subviews.Count == 0)
  1306. // return;
  1307. //if (index == 0) {
  1308. // //SuperView.SetFocus (previousSubFocused);
  1309. // FocusPrev ();
  1310. // return;
  1311. //}
  1312. //for (int i = openMenu.Subviews.Count - 1; i > index; i--) {
  1313. // isMenuClosing = true;
  1314. // if (openMenu.Subviews.Count - 1 > 0)
  1315. // SuperView.SetFocus (openMenu.Subviews [i - 1]);
  1316. // else
  1317. // SuperView.SetFocus (openMenu);
  1318. // if (openMenu != null) {
  1319. // Remove (openMenu.Subviews [i]);
  1320. // openMenu.Remove (openMenu.Subviews [i]);
  1321. // }
  1322. // RemoveSubMenu (i);
  1323. //}
  1324. isMenuClosing = false;
  1325. }
  1326. internal void RemoveAllOpensSubMenus ()
  1327. {
  1328. if (openSubMenu != null) {
  1329. foreach (var item in openSubMenu) {
  1330. if (SuperView == null) {
  1331. Application.Current.Remove (item);
  1332. } else {
  1333. SuperView.Remove (item);
  1334. }
  1335. item.Dispose ();
  1336. }
  1337. }
  1338. }
  1339. internal void CloseAllMenus ()
  1340. {
  1341. if (!isMenuOpening && !isMenuClosing) {
  1342. if (openSubMenu != null && !CloseMenu (false, true))
  1343. return;
  1344. if (!CloseMenu (false))
  1345. return;
  1346. if (LastFocused != null && LastFocused != this)
  1347. selected = -1;
  1348. Application.UngrabMouse ();
  1349. }
  1350. IsMenuOpen = false;
  1351. openedByHotKey = false;
  1352. openedByAltKey = false;
  1353. OnMenuAllClosed ();
  1354. }
  1355. View FindDeepestMenu (View view, ref int count)
  1356. {
  1357. count = count > 0 ? count : 0;
  1358. foreach (var menu in view.Subviews) {
  1359. if (menu is Menu) {
  1360. count++;
  1361. return FindDeepestMenu ((Menu)menu, ref count);
  1362. }
  1363. }
  1364. return view;
  1365. }
  1366. internal void PreviousMenu (bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1367. {
  1368. switch (isSubMenu) {
  1369. case false:
  1370. if (selected <= 0)
  1371. selected = Menus.Length - 1;
  1372. else
  1373. selected--;
  1374. if (selected > -1 && !CloseMenu (true, false, ignoreUseSubMenusSingleFrame))
  1375. return;
  1376. OpenMenu (selected);
  1377. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current, false)) {
  1378. openCurrentMenu.current = 0;
  1379. }
  1380. break;
  1381. case true:
  1382. if (selectedSub > -1) {
  1383. selectedSub--;
  1384. RemoveSubMenu (selectedSub, ignoreUseSubMenusSingleFrame);
  1385. SetNeedsDisplay ();
  1386. } else
  1387. PreviousMenu ();
  1388. break;
  1389. }
  1390. }
  1391. internal void NextMenu (bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1392. {
  1393. switch (isSubMenu) {
  1394. case false:
  1395. if (selected == -1)
  1396. selected = 0;
  1397. else if (selected + 1 == Menus.Length)
  1398. selected = 0;
  1399. else
  1400. selected++;
  1401. if (selected > -1 && !CloseMenu (true, ignoreUseSubMenusSingleFrame))
  1402. return;
  1403. OpenMenu (selected);
  1404. SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current);
  1405. break;
  1406. case true:
  1407. if (UseKeysUpDownAsKeysLeftRight) {
  1408. if (CloseMenu (false, true, ignoreUseSubMenusSingleFrame)) {
  1409. NextMenu (false, ignoreUseSubMenusSingleFrame);
  1410. }
  1411. } else {
  1412. var subMenu = openCurrentMenu.current > -1 && openCurrentMenu.barItems.Children.Length > 0
  1413. ? openCurrentMenu.barItems.SubMenu (openCurrentMenu.barItems.Children [openCurrentMenu.current])
  1414. : null;
  1415. if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && subMenu == null) {
  1416. if (openSubMenu != null && !CloseMenu (false, true))
  1417. return;
  1418. NextMenu (false, ignoreUseSubMenusSingleFrame);
  1419. } else if (subMenu != null || (openCurrentMenu.current > -1
  1420. && !openCurrentMenu.barItems.Children [openCurrentMenu.current].IsFromSubMenu)) {
  1421. selectedSub++;
  1422. openCurrentMenu.CheckSubMenu ();
  1423. } else {
  1424. if (CloseMenu (false, true, ignoreUseSubMenusSingleFrame)) {
  1425. NextMenu (false, ignoreUseSubMenusSingleFrame);
  1426. }
  1427. return;
  1428. }
  1429. SetNeedsDisplay ();
  1430. if (UseKeysUpDownAsKeysLeftRight)
  1431. openCurrentMenu.CheckSubMenu ();
  1432. }
  1433. break;
  1434. }
  1435. }
  1436. bool openedByHotKey;
  1437. internal bool FindAndOpenMenuByHotkey (KeyEvent kb)
  1438. {
  1439. //int pos = 0;
  1440. var c = ((uint)kb.Key & (uint)Key.CharMask);
  1441. for (int i = 0; i < Menus.Length; i++) {
  1442. // TODO: this code is duplicated, hotkey should be part of the MenuBarItem
  1443. var mi = Menus [i];
  1444. int p = mi.Title.IndexOf (MenuBar.HotKeySpecifier);
  1445. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1446. if (Char.ToUpperInvariant ((char)mi.Title [p + 1]) == c) {
  1447. ProcessMenu (i, mi);
  1448. return true;
  1449. }
  1450. }
  1451. }
  1452. return false;
  1453. }
  1454. internal bool FindAndOpenMenuByShortcut (KeyEvent kb, MenuItem [] children = null)
  1455. {
  1456. if (children == null) {
  1457. children = Menus;
  1458. }
  1459. var key = kb.KeyValue;
  1460. var keys = ShortcutHelper.GetModifiersKey (kb);
  1461. key |= (int)keys;
  1462. for (int i = 0; i < children.Length; i++) {
  1463. var mi = children [i];
  1464. if (mi == null) {
  1465. continue;
  1466. }
  1467. if ((!(mi is MenuBarItem mbiTopLevel) || mbiTopLevel.IsTopLevel) && mi.Shortcut != Key.Null && mi.Shortcut == (Key)key) {
  1468. var action = mi.Action;
  1469. if (action != null) {
  1470. Application.MainLoop.AddIdle (() => {
  1471. action ();
  1472. return false;
  1473. });
  1474. }
  1475. return true;
  1476. }
  1477. if (mi is MenuBarItem menuBarItem && !menuBarItem.IsTopLevel && FindAndOpenMenuByShortcut (kb, menuBarItem.Children)) {
  1478. return true;
  1479. }
  1480. }
  1481. return false;
  1482. }
  1483. private void ProcessMenu (int i, MenuBarItem mi)
  1484. {
  1485. if (selected < 0 && IsMenuOpen) {
  1486. return;
  1487. }
  1488. if (mi.IsTopLevel) {
  1489. var menu = new Menu (this, i, 0, mi);
  1490. menu.Run (mi.Action);
  1491. menu.Dispose ();
  1492. } else {
  1493. openedByHotKey = true;
  1494. Application.GrabMouse (this);
  1495. selected = i;
  1496. OpenMenu (i);
  1497. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current) && !CloseMenu (false)) {
  1498. return;
  1499. }
  1500. if (!openCurrentMenu.CheckSubMenu ())
  1501. return;
  1502. }
  1503. SetNeedsDisplay ();
  1504. }
  1505. ///<inheritdoc/>
  1506. public override bool ProcessHotKey (KeyEvent kb)
  1507. {
  1508. if (kb.Key == Key.F9) {
  1509. if (!IsMenuOpen)
  1510. OpenMenu ();
  1511. else
  1512. CloseAllMenus ();
  1513. return true;
  1514. }
  1515. // To ncurses simulate a AltMask key pressing Alt+Space because
  1516. // it can't detect an alone special key down was pressed.
  1517. if (kb.IsAlt && kb.Key == Key.AltMask && openMenu == null) {
  1518. OnKeyDown (kb);
  1519. OnKeyUp (kb);
  1520. return true;
  1521. } else if (kb.IsAlt && !kb.IsCtrl && !kb.IsShift) {
  1522. if (FindAndOpenMenuByHotkey (kb)) return true;
  1523. }
  1524. //var kc = kb.KeyValue;
  1525. return base.ProcessHotKey (kb);
  1526. }
  1527. ///<inheritdoc/>
  1528. public override bool ProcessKey (KeyEvent kb)
  1529. {
  1530. if (InvokeKeybindings (kb) == true)
  1531. return true;
  1532. var key = kb.KeyValue;
  1533. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  1534. char c = Char.ToUpper ((char)key);
  1535. if (selected == -1 || Menus [selected].IsTopLevel)
  1536. return false;
  1537. foreach (var mi in Menus [selected].Children) {
  1538. if (mi == null)
  1539. continue;
  1540. int p = mi.Title.IndexOf (MenuBar.HotKeySpecifier);
  1541. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1542. if (mi.Title [p + 1] == c) {
  1543. Selected (mi);
  1544. return true;
  1545. }
  1546. }
  1547. }
  1548. }
  1549. return false;
  1550. }
  1551. void CloseMenuBar ()
  1552. {
  1553. if (!CloseMenu (false))
  1554. return;
  1555. if (openedByAltKey) {
  1556. openedByAltKey = false;
  1557. LastFocused?.SetFocus ();
  1558. }
  1559. SetNeedsDisplay ();
  1560. }
  1561. void MoveRight ()
  1562. {
  1563. selected = (selected + 1) % Menus.Length;
  1564. OpenMenu (selected);
  1565. SetNeedsDisplay ();
  1566. }
  1567. void MoveLeft ()
  1568. {
  1569. selected--;
  1570. if (selected < 0)
  1571. selected = Menus.Length - 1;
  1572. OpenMenu (selected);
  1573. SetNeedsDisplay ();
  1574. }
  1575. ///<inheritdoc/>
  1576. public override bool ProcessColdKey (KeyEvent kb)
  1577. {
  1578. return FindAndOpenMenuByShortcut (kb);
  1579. }
  1580. ///<inheritdoc/>
  1581. public override bool MouseEvent (MouseEvent me)
  1582. {
  1583. if (!handled && !HandleGrabView (me, this)) {
  1584. return false;
  1585. }
  1586. handled = false;
  1587. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.Button1Clicked ||
  1588. (me.Flags == MouseFlags.ReportMousePosition && selected > -1) ||
  1589. (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && selected > -1)) {
  1590. int pos = 1;
  1591. int cx = me.X;
  1592. for (int i = 0; i < Menus.Length; i++) {
  1593. if (cx >= pos && cx < pos + 1 + Menus [i].TitleLength + Menus [i].Help.ConsoleWidth + 2) {
  1594. if (me.Flags == MouseFlags.Button1Clicked) {
  1595. if (Menus [i].IsTopLevel) {
  1596. var menu = new Menu (this, i, 0, Menus [i]);
  1597. menu.Run (Menus [i].Action);
  1598. menu.Dispose ();
  1599. } else if (!IsMenuOpen) {
  1600. Activate (i);
  1601. }
  1602. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked) {
  1603. if (IsMenuOpen && !Menus [i].IsTopLevel) {
  1604. CloseAllMenus ();
  1605. } else if (!Menus [i].IsTopLevel) {
  1606. Activate (i);
  1607. }
  1608. } else if (selected != i && selected > -1 && (me.Flags == MouseFlags.ReportMousePosition ||
  1609. me.Flags == MouseFlags.Button1Pressed && me.Flags == MouseFlags.ReportMousePosition)) {
  1610. if (IsMenuOpen) {
  1611. if (!CloseMenu (true, false)) {
  1612. return true;
  1613. }
  1614. Activate (i);
  1615. }
  1616. } else {
  1617. if (IsMenuOpen)
  1618. Activate (i);
  1619. }
  1620. return true;
  1621. }
  1622. pos += 1 + Menus [i].TitleLength + 2;
  1623. }
  1624. }
  1625. return false;
  1626. }
  1627. internal bool handled;
  1628. internal bool isContextMenuLoading;
  1629. internal bool HandleGrabView (MouseEvent me, View current)
  1630. {
  1631. if (Application.mouseGrabView != null) {
  1632. if (me.View is MenuBar || me.View is Menu) {
  1633. var mbar = GetMouseGrabViewInstance (me.View);
  1634. if (mbar != null) {
  1635. if (me.Flags == MouseFlags.Button1Clicked) {
  1636. mbar.CleanUp ();
  1637. Application.GrabMouse (me.View);
  1638. } else {
  1639. handled = false;
  1640. return false;
  1641. }
  1642. }
  1643. if (me.View != current) {
  1644. Application.UngrabMouse ();
  1645. var v = me.View;
  1646. Application.GrabMouse (v);
  1647. MouseEvent nme;
  1648. if (me.Y > -1) {
  1649. var newxy = v.ScreenToView (me.X, me.Y);
  1650. nme = new MouseEvent () {
  1651. X = newxy.X,
  1652. Y = newxy.Y,
  1653. Flags = me.Flags,
  1654. OfX = me.X - newxy.X,
  1655. OfY = me.Y - newxy.Y,
  1656. View = v
  1657. };
  1658. } else {
  1659. nme = new MouseEvent () {
  1660. X = me.X + current.Frame.X,
  1661. Y = 0,
  1662. Flags = me.Flags,
  1663. View = v
  1664. };
  1665. }
  1666. v.MouseEvent (nme);
  1667. return false;
  1668. }
  1669. } else if (!isContextMenuLoading && !(me.View is MenuBar || me.View is Menu)
  1670. && me.Flags != MouseFlags.ReportMousePosition && me.Flags != 0) {
  1671. Application.UngrabMouse ();
  1672. if (IsMenuOpen)
  1673. CloseAllMenus ();
  1674. handled = false;
  1675. return false;
  1676. } else {
  1677. handled = false;
  1678. isContextMenuLoading = false;
  1679. return false;
  1680. }
  1681. } else if (!IsMenuOpen && (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked
  1682. || me.Flags == MouseFlags.Button1TripleClicked || me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  1683. Application.GrabMouse (current);
  1684. } else if (IsMenuOpen && (me.View is MenuBar || me.View is Menu)) {
  1685. Application.GrabMouse (me.View);
  1686. } else {
  1687. handled = false;
  1688. return false;
  1689. }
  1690. //if (me.View != this && me.Flags != MouseFlags.Button1Pressed)
  1691. // return true;
  1692. //else if (me.View != this && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1693. // Application.UngrabMouse ();
  1694. // host.CloseAllMenus ();
  1695. // return true;
  1696. //}
  1697. //if (!(me.View is MenuBar) && !(me.View is Menu) && me.Flags != MouseFlags.Button1Pressed))
  1698. // return false;
  1699. //if (Application.mouseGrabView != null) {
  1700. // if (me.View is MenuBar || me.View is Menu) {
  1701. // me.X -= me.OfX;
  1702. // me.Y -= me.OfY;
  1703. // me.View.MouseEvent (me);
  1704. // return true;
  1705. // } else if (!(me.View is MenuBar || me.View is Menu) && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1706. // Application.UngrabMouse ();
  1707. // CloseAllMenus ();
  1708. // }
  1709. //} else if (!isMenuClosed && selected == -1 && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1710. // Application.GrabMouse (this);
  1711. // return true;
  1712. //}
  1713. //if (Application.mouseGrabView != null) {
  1714. // if (Application.mouseGrabView == me.View && me.View == current) {
  1715. // me.X -= me.OfX;
  1716. // me.Y -= me.OfY;
  1717. // } else if (me.View != current && me.View is MenuBar && me.View is Menu) {
  1718. // Application.UngrabMouse ();
  1719. // Application.GrabMouse (me.View);
  1720. // } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1721. // Application.UngrabMouse ();
  1722. // CloseMenu ();
  1723. // }
  1724. //} else if ((!isMenuClosed && selected > -1)) {
  1725. // Application.GrabMouse (current);
  1726. //}
  1727. handled = true;
  1728. return true;
  1729. }
  1730. MenuBar GetMouseGrabViewInstance (View view)
  1731. {
  1732. if (view == null || Application.mouseGrabView == null) {
  1733. return null;
  1734. }
  1735. MenuBar hostView = null;
  1736. if (view is MenuBar) {
  1737. hostView = (MenuBar)view;
  1738. } else if (view is Menu) {
  1739. hostView = ((Menu)view).host;
  1740. }
  1741. var grabView = Application.mouseGrabView;
  1742. MenuBar hostGrabView = null;
  1743. if (grabView is MenuBar) {
  1744. hostGrabView = (MenuBar)grabView;
  1745. } else if (grabView is Menu) {
  1746. hostGrabView = ((Menu)grabView).host;
  1747. }
  1748. return hostView != hostGrabView ? hostGrabView : null;
  1749. }
  1750. ///<inheritdoc/>
  1751. public override bool OnEnter (View view)
  1752. {
  1753. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  1754. return base.OnEnter (view);
  1755. }
  1756. }
  1757. /// <summary>
  1758. /// An <see cref="EventArgs"/> which allows passing a cancelable menu opening event or replacing with a new <see cref="MenuBarItem"/>.
  1759. /// </summary>
  1760. public class MenuOpeningEventArgs : EventArgs {
  1761. /// <summary>
  1762. /// The current <see cref="MenuBarItem"/> parent.
  1763. /// </summary>
  1764. public MenuBarItem CurrentMenu { get; }
  1765. /// <summary>
  1766. /// The new <see cref="MenuBarItem"/> to be replaced.
  1767. /// </summary>
  1768. public MenuBarItem NewMenuBarItem { get; set; }
  1769. /// <summary>
  1770. /// Flag that allows you to cancel the opening of the menu.
  1771. /// </summary>
  1772. public bool Cancel { get; set; }
  1773. /// <summary>
  1774. /// Initializes a new instance of <see cref="MenuOpeningEventArgs"/>
  1775. /// </summary>
  1776. /// <param name="currentMenu">The current <see cref="MenuBarItem"/> parent.</param>
  1777. public MenuOpeningEventArgs (MenuBarItem currentMenu)
  1778. {
  1779. CurrentMenu = currentMenu;
  1780. }
  1781. }
  1782. /// <summary>
  1783. /// An <see cref="EventArgs"/> which allows passing a cancelable menu closing event.
  1784. /// </summary>
  1785. public class MenuClosingEventArgs : EventArgs {
  1786. /// <summary>
  1787. /// The current <see cref="MenuBarItem"/> parent.
  1788. /// </summary>
  1789. public MenuBarItem CurrentMenu { get; }
  1790. /// <summary>
  1791. /// Indicates whether the current menu will be reopen.
  1792. /// </summary>
  1793. public bool Reopen { get; }
  1794. /// <summary>
  1795. /// Indicates whether the current menu is a sub-menu.
  1796. /// </summary>
  1797. public bool IsSubMenu { get; }
  1798. /// <summary>
  1799. /// Flag that allows you to cancel the opening of the menu.
  1800. /// </summary>
  1801. public bool Cancel { get; set; }
  1802. /// <summary>
  1803. /// Initializes a new instance of <see cref="MenuClosingEventArgs"/>
  1804. /// </summary>
  1805. /// <param name="currentMenu">The current <see cref="MenuBarItem"/> parent.</param>
  1806. /// <param name="reopen">Whether the current menu will be reopen.</param>
  1807. /// <param name="isSubMenu">Indicates whether it is a sub-menu.</param>
  1808. public MenuClosingEventArgs (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  1809. {
  1810. CurrentMenu = currentMenu;
  1811. Reopen = reopen;
  1812. IsSubMenu = isSubMenu;
  1813. }
  1814. }
  1815. }