Menu.cs 57 KB

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