Menu.cs 56 KB

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