Menu.cs 56 KB

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