Menu.cs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. //
  2. // Menu.cs: application menus and submenus
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // TODO:
  8. // Add accelerator support, but should also support chords (Shortcut in MenuItem)
  9. // Allow menus inside menus
  10. using System;
  11. using NStack;
  12. using System.Linq;
  13. using System.Collections.Generic;
  14. namespace Terminal.Gui {
  15. /// <summary>
  16. /// Specifies how a <see cref="MenuItem"/> shows selection state.
  17. /// </summary>
  18. [Flags]
  19. public enum MenuItemCheckStyle {
  20. /// <summary>
  21. /// The menu item will be shown normally, with no check indicator.
  22. /// </summary>
  23. NoCheck = 0b_0000_0000,
  24. /// <summary>
  25. /// The menu item will indicate checked/un-checked state (see <see cref="Checked"/>.
  26. /// </summary>
  27. Checked = 0b_0000_0001,
  28. /// <summary>
  29. /// The menu item is part of a menu radio group (see <see cref="Checked"/> and will indicate selected state.
  30. /// </summary>
  31. Radio = 0b_0000_0010,
  32. };
  33. /// <summary>
  34. /// A <see cref="MenuItem"/> has a title, an associated help text, and an action to execute on activation.
  35. /// </summary>
  36. public class MenuItem {
  37. ustring title;
  38. ShortcutHelper shortcutHelper;
  39. internal int TitleLength => GetMenuBarItemLength (Title);
  40. /// <summary>
  41. /// Gets or sets arbitrary data for the menu item.
  42. /// </summary>
  43. /// <remarks>This property is not used internally.</remarks>
  44. public object Data { get; set; }
  45. /// <summary>
  46. /// Initializes a new instance of <see cref="MenuItem"/>
  47. /// </summary>
  48. public MenuItem (Key shortcut = Key.Null) : this ("", "", null, null, null, shortcut) { }
  49. /// <summary>
  50. /// Initializes a new instance of <see cref="MenuItem"/>.
  51. /// </summary>
  52. /// <param name="title">Title for the menu item.</param>
  53. /// <param name="help">Help text to display.</param>
  54. /// <param name="action">Action to invoke when the menu item is activated.</param>
  55. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  56. /// <param name="parent">The <see cref="Parent"/> of this menu item.</param>
  57. /// <param name="shortcut">The <see cref="Shortcut"/> keystroke combination.</param>
  58. public MenuItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null, Key shortcut = Key.Null)
  59. {
  60. Title = title ?? "";
  61. Help = help ?? "";
  62. Action = action;
  63. CanExecute = canExecute;
  64. Parent = parent;
  65. shortcutHelper = new ShortcutHelper ();
  66. if (shortcut != Key.Null) {
  67. shortcutHelper.Shortcut = shortcut;
  68. }
  69. }
  70. /// <summary>
  71. /// The HotKey is used when the menu is active, the shortcut can be triggered when the menu is not active.
  72. /// For example HotKey would be "N" when the File Menu is open (assuming there is a "_New" entry
  73. /// if the Shortcut is set to "Control-N", this would be a global hotkey that would trigger as well
  74. /// </summary>
  75. public Rune HotKey;
  76. /// <summary>
  77. /// This is the global setting that can be used as a global <see cref="ShortcutHelper.Shortcut"/> to invoke the action on the menu.
  78. /// </summary>
  79. public Key Shortcut {
  80. get => shortcutHelper.Shortcut;
  81. set {
  82. if (shortcutHelper.Shortcut != value && (ShortcutHelper.PostShortcutValidation (value) || value == Key.Null)) {
  83. shortcutHelper.Shortcut = value;
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// The keystroke combination used in the <see cref="ShortcutHelper.ShortcutTag"/> as string.
  89. /// </summary>
  90. public ustring ShortcutTag => ShortcutHelper.GetShortcutTag (shortcutHelper.Shortcut);
  91. /// <summary>
  92. /// Gets or sets the title.
  93. /// </summary>
  94. /// <value>The title.</value>
  95. public ustring Title {
  96. get { return title; }
  97. set {
  98. if (title != value) {
  99. title = value;
  100. GetHotKey ();
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// Gets or sets the help text for the menu item.
  106. /// </summary>
  107. /// <value>The help text.</value>
  108. public ustring Help { get; set; }
  109. /// <summary>
  110. /// Gets or sets the action to be invoked when the menu is triggered
  111. /// </summary>
  112. /// <value>Method to invoke.</value>
  113. public Action Action { get; set; }
  114. /// <summary>
  115. /// Gets or sets the action to be invoked if the menu can be triggered
  116. /// </summary>
  117. /// <value>Function to determine if action is ready to be executed.</value>
  118. public Func<bool> CanExecute { get; set; }
  119. /// <summary>
  120. /// Shortcut to check if the menu item is enabled
  121. /// </summary>
  122. public bool IsEnabled ()
  123. {
  124. return CanExecute == null ? true : CanExecute ();
  125. }
  126. internal int Width => 1 + TitleLength + (Help.ConsoleWidth > 0 ? Help.ConsoleWidth + 2 : 0) +
  127. (Checked || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio) ? 2 : 0) +
  128. (ShortcutTag.ConsoleWidth > 0 ? ShortcutTag.ConsoleWidth + 2 : 0) + 2;
  129. /// <summary>
  130. /// Sets or gets whether the <see cref="MenuItem"/> shows a check indicator or not. See <see cref="MenuItemCheckStyle"/>.
  131. /// </summary>
  132. public bool Checked { set; get; }
  133. /// <summary>
  134. /// Sets or gets the type selection indicator the menu item will be displayed with.
  135. /// </summary>
  136. public MenuItemCheckStyle CheckType { get; set; }
  137. /// <summary>
  138. /// Gets or sets the parent for this <see cref="MenuItem"/>.
  139. /// </summary>
  140. /// <value>The parent.</value>
  141. public MenuItem Parent { get; internal set; }
  142. /// <summary>
  143. /// Gets if this <see cref="MenuItem"/> is from a sub-menu.
  144. /// </summary>
  145. internal bool IsFromSubMenu { get { return Parent != null; } }
  146. /// <summary>
  147. /// Merely a debugging aid to see the interaction with main
  148. /// </summary>
  149. public MenuItem GetMenuItem ()
  150. {
  151. return this;
  152. }
  153. /// <summary>
  154. /// Merely a debugging aid to see the interaction with main
  155. /// </summary>
  156. public bool GetMenuBarItem ()
  157. {
  158. return IsFromSubMenu;
  159. }
  160. void GetHotKey ()
  161. {
  162. bool nextIsHot = false;
  163. foreach (var x in title) {
  164. if (x == MenuBar.HotKeySpecifier) {
  165. nextIsHot = true;
  166. } else {
  167. if (nextIsHot) {
  168. HotKey = Char.ToUpper ((char)x);
  169. break;
  170. }
  171. nextIsHot = false;
  172. HotKey = default;
  173. }
  174. }
  175. }
  176. int GetMenuBarItemLength (ustring title)
  177. {
  178. int len = 0;
  179. foreach (var ch in title) {
  180. if (ch == MenuBar.HotKeySpecifier)
  181. continue;
  182. len += Math.Max (Rune.ColumnWidth (ch), 1);
  183. }
  184. return len;
  185. }
  186. }
  187. /// <summary>
  188. /// A <see cref="MenuBarItem"/> contains <see cref="MenuBarItem"/>s or <see cref="MenuItem"/>s.
  189. /// </summary>
  190. public class MenuBarItem : MenuItem {
  191. /// <summary>
  192. /// Initializes a new <see cref="MenuBarItem"/> as a <see cref="MenuItem"/>.
  193. /// </summary>
  194. /// <param name="title">Title for the menu item.</param>
  195. /// <param name="help">Help text to display.</param>
  196. /// <param name="action">Action to invoke when the menu item is activated.</param>
  197. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  198. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  199. public MenuBarItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null) : base (title, help, action, canExecute, parent)
  200. {
  201. Initialize (title, null, null, true);
  202. }
  203. /// <summary>
  204. /// Initializes a new <see cref="MenuBarItem"/>.
  205. /// </summary>
  206. /// <param name="title">Title for the menu item.</param>
  207. /// <param name="children">The items in the current menu.</param>
  208. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  209. public MenuBarItem (ustring title, MenuItem [] children, MenuItem parent = null)
  210. {
  211. Initialize (title, children, parent);
  212. }
  213. /// <summary>
  214. /// Initializes a new <see cref="MenuBarItem"/> with separate list of items.
  215. /// </summary>
  216. /// <param name="title">Title for the menu item.</param>
  217. /// <param name="children">The list of items in the current menu.</param>
  218. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  219. public MenuBarItem (ustring title, List<MenuItem []> children, MenuItem parent = null)
  220. {
  221. Initialize (title, children, parent);
  222. }
  223. /// <summary>
  224. /// Initializes a new <see cref="MenuBarItem"/>.
  225. /// </summary>
  226. /// <param name="children">The items in the current menu.</param>
  227. public MenuBarItem (MenuItem [] children) : this ("", children) { }
  228. /// <summary>
  229. /// Initializes a new <see cref="MenuBarItem"/>.
  230. /// </summary>
  231. public MenuBarItem () : this (children: new MenuItem [] { }) { }
  232. void Initialize (ustring title, object children, MenuItem parent = null, bool isTopLevel = false)
  233. {
  234. if (!isTopLevel && children == null) {
  235. throw new ArgumentNullException (nameof (children), "The parameter cannot be null. Use an empty array instead.");
  236. }
  237. SetTitle (title ?? "");
  238. if (parent != null) {
  239. Parent = parent;
  240. }
  241. if (children is List<MenuItem []>) {
  242. MenuItem [] childrens = new MenuItem [] { };
  243. foreach (var item in (List<MenuItem []>)children) {
  244. for (int i = 0; i < item.Length; i++) {
  245. SetChildrensParent (item);
  246. Array.Resize (ref childrens, childrens.Length + 1);
  247. childrens [childrens.Length - 1] = item [i];
  248. }
  249. }
  250. Children = childrens;
  251. } else if (children is MenuItem []) {
  252. SetChildrensParent ((MenuItem [])children);
  253. Children = (MenuItem [])children;
  254. } else {
  255. Children = null;
  256. }
  257. }
  258. //static int GetMaxTitleLength (MenuItem [] children)
  259. //{
  260. // int maxLength = 0;
  261. // foreach (var item in children) {
  262. // int len = GetMenuBarItemLength (item.Title);
  263. // if (len > maxLength)
  264. // maxLength = len;
  265. // item.IsFromSubMenu = true;
  266. // }
  267. // return maxLength;
  268. //}
  269. void SetChildrensParent (MenuItem [] childrens)
  270. {
  271. foreach (var child in childrens) {
  272. if (child != null && child.Parent == null) {
  273. child.Parent = this;
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// Check if the children parameter is a <see cref="MenuBarItem"/>.
  279. /// </summary>
  280. /// <param name="children"></param>
  281. /// <returns>Returns a <see cref="MenuBarItem"/> or null otherwise.</returns>
  282. public MenuBarItem SubMenu (MenuItem children)
  283. {
  284. return children as MenuBarItem;
  285. }
  286. /// <summary>
  287. /// Check if the <see cref="MenuItem"/> parameter is a child of this.
  288. /// </summary>
  289. /// <param name="menuItem"></param>
  290. /// <returns>Returns <c>true</c> if it is a child of this. <c>false</c> otherwise.</returns>
  291. public bool IsSubMenuOf (MenuItem menuItem)
  292. {
  293. foreach (var child in Children) {
  294. if (child == menuItem && child.Parent == menuItem.Parent) {
  295. return true;
  296. }
  297. }
  298. return false;
  299. }
  300. /// <summary>
  301. /// Get the index of the <see cref="MenuItem"/> parameter.
  302. /// </summary>
  303. /// <param name="children"></param>
  304. /// <returns>Returns a value bigger than -1 if the <see cref="MenuItem"/> is a child of this.</returns>
  305. public int GetChildrenIndex (MenuItem children)
  306. {
  307. if (Children?.Length == 0) {
  308. return -1;
  309. }
  310. int i = 0;
  311. foreach (var child in Children) {
  312. if (child == children) {
  313. return i;
  314. }
  315. i++;
  316. }
  317. return -1;
  318. }
  319. void SetTitle (ustring title)
  320. {
  321. if (title == null)
  322. title = "";
  323. Title = title;
  324. }
  325. ///// <summary>
  326. ///// Gets or sets the title to display.
  327. ///// </summary>
  328. ///// <value>The title.</value>
  329. //public ustring Title { get; set; }
  330. /// <summary>
  331. /// Gets or sets an array of <see cref="MenuItem"/> objects that are the children of this <see cref="MenuBarItem"/>
  332. /// </summary>
  333. /// <value>The children.</value>
  334. public MenuItem [] Children { get; set; }
  335. internal bool IsTopLevel { get => Parent == null && (Children == null || Children.Length == 0) && Action != null; }
  336. }
  337. class Menu : View {
  338. internal MenuBarItem barItems;
  339. internal MenuBar host;
  340. internal int current;
  341. internal View previousSubFocused;
  342. internal static Rect MakeFrame (int x, int y, MenuItem [] items)
  343. {
  344. if (items == null || items.Length == 0) {
  345. return new Rect ();
  346. }
  347. int maxW = items.Max (z => z?.Width) ?? 0;
  348. return new Rect (x, y, maxW + 2, items.Length + 2);
  349. }
  350. public Menu (MenuBar host, int x, int y, MenuBarItem barItems) : base (MakeFrame (x, y, barItems.Children))
  351. {
  352. this.barItems = barItems;
  353. this.host = host;
  354. if (barItems.IsTopLevel) {
  355. // This is a standalone MenuItem on a MenuBar
  356. ColorScheme = host.ColorScheme;
  357. CanFocus = true;
  358. } else {
  359. current = -1;
  360. for (int i = 0; i < barItems.Children?.Length; i++) {
  361. if (barItems.Children [i] != null) {
  362. current = i;
  363. break;
  364. }
  365. }
  366. ColorScheme = host.ColorScheme;
  367. CanFocus = true;
  368. WantMousePositionReports = host.WantMousePositionReports;
  369. }
  370. // Things this view knows how to do
  371. AddCommand (Command.LineUp, () => MoveUp ());
  372. AddCommand (Command.LineDown, () => MoveDown ());
  373. AddCommand (Command.Left, () => { this.host.PreviousMenu (true); return true; });
  374. AddCommand (Command.Right, () => {
  375. this.host.NextMenu (this.barItems.IsTopLevel || (this.barItems.Children != null
  376. && 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. SuperView == null ? default : SuperView.ViewToScreen (SuperView.Bounds));
  463. } else {
  464. DrawHotString (textToDraw,
  465. i == current ? ColorScheme.HotFocus : ColorScheme.HotNormal,
  466. i == current ? ColorScheme.Focus : GetNormalColor ());
  467. }
  468. // The help string
  469. var l = item.ShortcutTag.ConsoleWidth == 0 ? item.Help.ConsoleWidth : item.Help.ConsoleWidth + item.ShortcutTag.ConsoleWidth + 2;
  470. var col = Frame.Width - l - 2;
  471. ViewToScreen (col, i + 1, out vtsCol, out _, false);
  472. if (vtsCol < Driver.Cols) {
  473. Move (col, 1 + i);
  474. Driver.AddStr (item.Help);
  475. // The shortcut tag string
  476. if (!item.ShortcutTag.IsEmpty) {
  477. l = item.ShortcutTag.ConsoleWidth;
  478. Move (Frame.Width - l - 2, 1 + i);
  479. Driver.AddStr (item.ShortcutTag);
  480. }
  481. }
  482. }
  483. }
  484. PositionCursor ();
  485. }
  486. public override void PositionCursor ()
  487. {
  488. if (host == null || host.IsMenuOpen)
  489. if (barItems.IsTopLevel) {
  490. host.PositionCursor ();
  491. } else
  492. Move (2, 1 + current);
  493. else
  494. host.PositionCursor ();
  495. }
  496. public void Run (Action action)
  497. {
  498. if (action == null)
  499. return;
  500. Application.UngrabMouse ();
  501. host.CloseAllMenus ();
  502. Application.Refresh ();
  503. Application.MainLoop.AddIdle (() => {
  504. action ();
  505. return false;
  506. });
  507. }
  508. public override bool OnLeave (View view)
  509. {
  510. return host.OnLeave (view);
  511. }
  512. public override bool OnKeyDown (KeyEvent keyEvent)
  513. {
  514. if (keyEvent.IsAlt) {
  515. host.CloseAllMenus ();
  516. return true;
  517. }
  518. return false;
  519. }
  520. public override bool ProcessHotKey (KeyEvent keyEvent)
  521. {
  522. // To ncurses simulate a AltMask key pressing Alt+Space because
  523. // it can�t detect an alone special key down was pressed.
  524. if (keyEvent.IsAlt && keyEvent.Key == Key.AltMask) {
  525. OnKeyDown (keyEvent);
  526. return true;
  527. }
  528. return false;
  529. }
  530. public override bool ProcessKey (KeyEvent kb)
  531. {
  532. var result = InvokeKeybindings (kb);
  533. if (result != null)
  534. return (bool)result;
  535. // TODO: rune-ify
  536. if (barItems.Children != null && Char.IsLetterOrDigit ((char)kb.KeyValue)) {
  537. var x = Char.ToUpper ((char)kb.KeyValue);
  538. var idx = -1;
  539. foreach (var item in barItems.Children) {
  540. idx++;
  541. if (item == null) continue;
  542. if (item.IsEnabled () && item.HotKey == x) {
  543. current = idx;
  544. RunSelected ();
  545. return true;
  546. }
  547. }
  548. }
  549. return false;
  550. }
  551. void RunSelected ()
  552. {
  553. if (barItems.IsTopLevel) {
  554. Run (barItems.Action);
  555. } else if (current > -1 && barItems.Children [current].Action != null) {
  556. Run (barItems.Children [current].Action);
  557. } else if (current == 0 && host.UseSubMenusSingleFrame
  558. && barItems.Children [current].Parent.Parent != null) {
  559. host.PreviousMenu (barItems.Children [current].Parent.IsFromSubMenu, true);
  560. } else if (current > -1 && barItems.SubMenu (barItems.Children [current]) != null) {
  561. CheckSubMenu ();
  562. }
  563. }
  564. void CloseAllMenus ()
  565. {
  566. Application.UngrabMouse ();
  567. host.CloseAllMenus ();
  568. }
  569. bool MoveDown ()
  570. {
  571. if (barItems.IsTopLevel) {
  572. return true;
  573. }
  574. bool disabled;
  575. do {
  576. current++;
  577. if (current >= barItems.Children.Length) {
  578. current = 0;
  579. }
  580. if (this != host.openCurrentMenu && barItems.Children [current].IsFromSubMenu && host.selectedSub > -1) {
  581. host.PreviousMenu (true);
  582. host.SelectEnabledItem (barItems.Children, current, out current);
  583. host.openCurrentMenu = this;
  584. }
  585. var item = barItems.Children [current];
  586. if (item?.IsEnabled () != true) {
  587. disabled = true;
  588. } else {
  589. disabled = false;
  590. }
  591. if (!host.UseSubMenusSingleFrame && host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
  592. !disabled && host.IsMenuOpen) {
  593. if (!CheckSubMenu ())
  594. return false;
  595. break;
  596. }
  597. if (!host.IsMenuOpen) {
  598. host.OpenMenu (host.selected);
  599. }
  600. } while (barItems.Children [current] == null || disabled);
  601. SetNeedsDisplay ();
  602. if (!host.UseSubMenusSingleFrame)
  603. host.OnMenuOpened ();
  604. return true;
  605. }
  606. bool MoveUp ()
  607. {
  608. if (barItems.IsTopLevel || current == -1) {
  609. return true;
  610. }
  611. bool disabled;
  612. do {
  613. current--;
  614. if (host.UseKeysUpDownAsKeysLeftRight && !host.UseSubMenusSingleFrame) {
  615. if ((current == -1 || this != host.openCurrentMenu) && barItems.Children [current + 1].IsFromSubMenu && host.selectedSub > -1) {
  616. current++;
  617. host.PreviousMenu (true);
  618. if (current > 0) {
  619. current--;
  620. host.openCurrentMenu = this;
  621. }
  622. break;
  623. }
  624. }
  625. if (current < 0)
  626. current = barItems.Children.Length - 1;
  627. if (!host.SelectEnabledItem (barItems.Children, current, out current, false)) {
  628. current = 0;
  629. if (!host.SelectEnabledItem (barItems.Children, current, out current) && !host.CloseMenu (false)) {
  630. return false;
  631. }
  632. break;
  633. }
  634. var item = barItems.Children [current];
  635. if (item?.IsEnabled () != true) {
  636. disabled = true;
  637. } else {
  638. disabled = false;
  639. }
  640. if (!host.UseSubMenusSingleFrame && host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
  641. !disabled && host.IsMenuOpen) {
  642. if (!CheckSubMenu ())
  643. return false;
  644. break;
  645. }
  646. } while (barItems.Children [current] == null || disabled);
  647. SetNeedsDisplay ();
  648. if (!host.UseSubMenusSingleFrame)
  649. host.OnMenuOpened ();
  650. return true;
  651. }
  652. public override bool MouseEvent (MouseEvent me)
  653. {
  654. if (!host.handled && !host.HandleGrabView (me, this)) {
  655. return false;
  656. }
  657. host.handled = false;
  658. bool disabled;
  659. if (me.Flags == MouseFlags.Button1Clicked) {
  660. disabled = false;
  661. if (me.Y < 1)
  662. return true;
  663. var meY = me.Y - 1;
  664. if (meY >= barItems.Children.Length)
  665. return true;
  666. var item = barItems.Children [meY];
  667. if (item == null || !item.IsEnabled ()) disabled = true;
  668. current = meY;
  669. if (item != null && !disabled)
  670. RunSelected ();
  671. return true;
  672. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked ||
  673. me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.ReportMousePosition ||
  674. me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  675. disabled = false;
  676. if (me.Y < 1 || me.Y - 1 >= barItems.Children.Length) {
  677. return true;
  678. }
  679. var item = barItems.Children [me.Y - 1];
  680. if (item == null) return true;
  681. if (item == null || !item.IsEnabled ()) disabled = true;
  682. if (item != null && !disabled)
  683. current = me.Y - 1;
  684. if (host.UseSubMenusSingleFrame || !CheckSubMenu ()) {
  685. SetNeedsDisplay ();
  686. return true;
  687. }
  688. host.OnMenuOpened ();
  689. return true;
  690. }
  691. return false;
  692. }
  693. internal bool CheckSubMenu ()
  694. {
  695. if (current == -1 || barItems.Children [current] == null) {
  696. return true;
  697. }
  698. var subMenu = barItems.SubMenu (barItems.Children [current]);
  699. if (subMenu != null) {
  700. int pos = -1;
  701. if (host.openSubMenu != null) {
  702. pos = host.openSubMenu.FindIndex (o => o?.barItems == subMenu);
  703. }
  704. if (pos == -1 && this != host.openCurrentMenu && subMenu.Children != host.openCurrentMenu.barItems.Children
  705. && !host.CloseMenu (false, true)) {
  706. return false;
  707. }
  708. host.Activate (host.selected, pos, subMenu);
  709. } else if (host.openSubMenu?.Count == 0 || host.openSubMenu?.Last ().barItems.IsSubMenuOf (barItems.Children [current]) == false) {
  710. return host.CloseMenu (false, true);
  711. } else {
  712. SetNeedsDisplay ();
  713. }
  714. return true;
  715. }
  716. int GetSubMenuIndex (MenuBarItem subMenu)
  717. {
  718. int pos = -1;
  719. if (this != null && Subviews.Count > 0) {
  720. Menu v = null;
  721. foreach (var menu in Subviews) {
  722. if (((Menu)menu).barItems == subMenu)
  723. v = (Menu)menu;
  724. }
  725. if (v != null)
  726. pos = Subviews.IndexOf (v);
  727. }
  728. return pos;
  729. }
  730. ///<inheritdoc/>
  731. public override bool OnEnter (View view)
  732. {
  733. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  734. return base.OnEnter (view);
  735. }
  736. }
  737. /// <summary>
  738. /// The MenuBar provides a menu for Terminal.Gui applications.
  739. /// </summary>
  740. /// <remarks>
  741. /// <para>
  742. /// The <see cref="MenuBar"/> appears on the first row of the terminal.
  743. /// </para>
  744. /// <para>
  745. /// The <see cref="MenuBar"/> provides global hotkeys for the application.
  746. /// </para>
  747. /// </remarks>
  748. public class MenuBar : View {
  749. internal int selected;
  750. internal int selectedSub;
  751. /// <summary>
  752. /// Gets or sets the array of <see cref="MenuBarItem"/>s for the menu. Only set this when the <see cref="MenuBar"/> is visible.
  753. /// </summary>
  754. /// <value>The menu array.</value>
  755. public MenuBarItem [] Menus { get; set; }
  756. /// <summary>
  757. /// Used for change the navigation key style.
  758. /// </summary>
  759. public bool UseKeysUpDownAsKeysLeftRight { get; set; } = true;
  760. static ustring shortcutDelimiter = "+";
  761. /// <summary>
  762. /// Used for change the shortcut delimiter separator.
  763. /// </summary>
  764. public static ustring ShortcutDelimiter {
  765. get => shortcutDelimiter;
  766. set {
  767. if (shortcutDelimiter != value) {
  768. shortcutDelimiter = value == ustring.Empty ? " " : value;
  769. }
  770. }
  771. }
  772. /// <summary>
  773. /// The specifier character for the hotkey to all menus.
  774. /// </summary>
  775. new public static Rune HotKeySpecifier => '_';
  776. /// <summary>
  777. /// Gets or sets if the sub-menus must be displayed in a single or multiple frames.
  778. /// </summary>
  779. public bool UseSubMenusSingleFrame { get; set; }
  780. /// <summary>
  781. /// Initializes a new instance of the <see cref="MenuBar"/>.
  782. /// </summary>
  783. public MenuBar () : this (new MenuBarItem [] { }) { }
  784. /// <summary>
  785. /// Initializes a new instance of the <see cref="MenuBar"/> class with the specified set of toplevel menu items.
  786. /// </summary>
  787. /// <param name="menus">Individual menu items; a null item will result in a separator being drawn.</param>
  788. public MenuBar (MenuBarItem [] menus) : base ()
  789. {
  790. X = 0;
  791. Y = 0;
  792. Width = Dim.Fill ();
  793. Height = 1;
  794. Menus = menus;
  795. //CanFocus = true;
  796. selected = -1;
  797. selectedSub = -1;
  798. ColorScheme = Colors.Menu;
  799. WantMousePositionReports = true;
  800. IsMenuOpen = false;
  801. // Things this view knows how to do
  802. AddCommand (Command.Left, () => { MoveLeft (); return true; });
  803. AddCommand (Command.Right, () => { MoveRight (); return true; });
  804. AddCommand (Command.Cancel, () => { CloseMenuBar (); return true; });
  805. AddCommand (Command.Accept, () => { ProcessMenu (selected, Menus [selected]); return true; });
  806. // Default keybindings for this view
  807. AddKeyBinding (Key.CursorLeft, Command.Left);
  808. AddKeyBinding (Key.CursorRight, Command.Right);
  809. AddKeyBinding (Key.Esc, Command.Cancel);
  810. AddKeyBinding (Key.C | Key.CtrlMask, Command.Cancel);
  811. AddKeyBinding (Key.CursorDown, Command.Accept);
  812. AddKeyBinding (Key.Enter, Command.Accept);
  813. }
  814. bool openedByAltKey;
  815. bool isCleaning;
  816. ///<inheritdoc/>
  817. public override bool OnLeave (View view)
  818. {
  819. if ((!(view is MenuBar) && !(view is Menu) || !(view is MenuBar) && !(view is Menu) && openMenu != null) && !isCleaning && !reopen) {
  820. CleanUp ();
  821. return true;
  822. }
  823. return false;
  824. }
  825. ///<inheritdoc/>
  826. public override bool OnKeyDown (KeyEvent keyEvent)
  827. {
  828. if (keyEvent.IsAlt || (keyEvent.IsCtrl && keyEvent.Key == (Key.CtrlMask | Key.Space))) {
  829. openedByAltKey = true;
  830. SetNeedsDisplay ();
  831. openedByHotKey = false;
  832. }
  833. return false;
  834. }
  835. ///<inheritdoc/>
  836. public override bool OnKeyUp (KeyEvent keyEvent)
  837. {
  838. if (keyEvent.IsAlt || keyEvent.Key == Key.AltMask || (keyEvent.IsCtrl && keyEvent.Key == (Key.CtrlMask | Key.Space))) {
  839. // User pressed Alt - this may be a precursor to a menu accelerator (e.g. Alt-F)
  840. if (openedByAltKey && !IsMenuOpen && openMenu == null && (((uint)keyEvent.Key & (uint)Key.CharMask) == 0
  841. || ((uint)keyEvent.Key & (uint)Key.CharMask) == (uint)Key.Space)) {
  842. // There's no open menu, the first menu item should be highlight.
  843. // The right way to do this is to SetFocus(MenuBar), but for some reason
  844. // that faults.
  845. var mbar = GetMouseGrabViewInstance (this);
  846. if (mbar != null) {
  847. mbar.CleanUp ();
  848. }
  849. //Activate (0);
  850. //StartMenu ();
  851. IsMenuOpen = true;
  852. selected = 0;
  853. CanFocus = true;
  854. lastFocused = SuperView == null ? Application.Current.MostFocused : SuperView.MostFocused;
  855. SetFocus ();
  856. SetNeedsDisplay ();
  857. Application.GrabMouse (this);
  858. } else if (!openedByHotKey) {
  859. // There's an open menu. If this Alt key-up is a pre-cursor to an accelerator
  860. // we don't want to close the menu because it'll flash.
  861. // How to deal with that?
  862. CleanUp ();
  863. }
  864. return true;
  865. }
  866. return false;
  867. }
  868. internal void CleanUp ()
  869. {
  870. isCleaning = true;
  871. if (openMenu != null) {
  872. CloseAllMenus ();
  873. }
  874. openedByAltKey = false;
  875. IsMenuOpen = false;
  876. selected = -1;
  877. CanFocus = false;
  878. if (lastFocused != null) {
  879. lastFocused.SetFocus ();
  880. }
  881. SetNeedsDisplay ();
  882. Application.UngrabMouse ();
  883. isCleaning = false;
  884. }
  885. ///<inheritdoc/>
  886. public override void Redraw (Rect bounds)
  887. {
  888. Move (0, 0);
  889. Driver.SetAttribute (GetNormalColor ());
  890. for (int i = 0; i < Frame.Width; i++)
  891. Driver.AddRune (' ');
  892. Move (1, 0);
  893. int pos = 1;
  894. for (int i = 0; i < Menus.Length; i++) {
  895. var menu = Menus [i];
  896. Move (pos, 0);
  897. Attribute hotColor, normalColor;
  898. if (i == selected && IsMenuOpen) {
  899. hotColor = i == selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  900. normalColor = i == selected ? ColorScheme.Focus :
  901. GetNormalColor ();
  902. } else if (openedByAltKey) {
  903. hotColor = ColorScheme.HotNormal;
  904. normalColor = GetNormalColor ();
  905. } else {
  906. hotColor = GetNormalColor ();
  907. normalColor = GetNormalColor ();
  908. }
  909. DrawHotString (menu.Help.IsEmpty ? $" {menu.Title} " : $" {menu.Title} {menu.Help} ", hotColor, normalColor);
  910. pos += 1 + menu.TitleLength + (menu.Help.ConsoleWidth > 0 ? menu.Help.ConsoleWidth + 2 : 0) + 2;
  911. }
  912. PositionCursor ();
  913. }
  914. ///<inheritdoc/>
  915. public override void PositionCursor ()
  916. {
  917. if (selected == -1 && HasFocus && Menus.Length > 0) {
  918. selected = 0;
  919. }
  920. int pos = 0;
  921. for (int i = 0; i < Menus.Length; i++) {
  922. if (i == selected) {
  923. pos++;
  924. if (IsMenuOpen)
  925. Move (pos + 1, 0);
  926. else {
  927. Move (pos + 1, 0);
  928. }
  929. return;
  930. } else {
  931. pos += 1 + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + 2 : 0) + 2;
  932. }
  933. }
  934. }
  935. void Selected (MenuItem item)
  936. {
  937. var action = item.Action;
  938. if (action == null)
  939. return;
  940. Application.UngrabMouse ();
  941. CloseAllMenus ();
  942. Application.Refresh ();
  943. Application.MainLoop.AddIdle (() => {
  944. action ();
  945. return false;
  946. });
  947. }
  948. /// <summary>
  949. /// Raised as a menu is opening.
  950. /// </summary>
  951. public event Action<MenuOpeningEventArgs> MenuOpening;
  952. /// <summary>
  953. /// Raised when a menu is opened.
  954. /// </summary>
  955. public event Action<MenuItem> MenuOpened;
  956. /// <summary>
  957. /// Raised when a menu is closing passing <see cref="MenuClosingEventArgs"/>.
  958. /// </summary>
  959. public event Action<MenuClosingEventArgs> MenuClosing;
  960. /// <summary>
  961. /// Raised when all the menu are closed.
  962. /// </summary>
  963. public event Action MenuAllClosed;
  964. internal Menu openMenu;
  965. Menu ocm;
  966. internal Menu openCurrentMenu {
  967. get => ocm;
  968. set {
  969. if (ocm != value) {
  970. ocm = value;
  971. if (ocm.current > -1) {
  972. OnMenuOpened ();
  973. }
  974. }
  975. }
  976. }
  977. internal List<Menu> openSubMenu;
  978. View previousFocused;
  979. internal bool isMenuOpening;
  980. internal bool isMenuClosing;
  981. /// <summary>
  982. /// True if the menu is open; otherwise false.
  983. /// </summary>
  984. public bool IsMenuOpen { get; protected set; }
  985. /// <summary>
  986. /// Virtual method that will invoke the <see cref="MenuOpening"/> event if it's defined.
  987. /// </summary>
  988. /// <param name="currentMenu">The current menu to be replaced.</param>
  989. /// <returns>Returns the <see cref="MenuOpeningEventArgs"/></returns>
  990. public virtual MenuOpeningEventArgs OnMenuOpening (MenuBarItem currentMenu)
  991. {
  992. var ev = new MenuOpeningEventArgs (currentMenu);
  993. MenuOpening?.Invoke (ev);
  994. return ev;
  995. }
  996. /// <summary>
  997. /// Virtual method that will invoke the <see cref="MenuOpened"/> event if it's defined.
  998. /// </summary>
  999. public virtual void OnMenuOpened ()
  1000. {
  1001. MenuItem mi = null;
  1002. if (openCurrentMenu.barItems.Children != null) {
  1003. mi = openCurrentMenu.barItems.Children [openCurrentMenu.current];
  1004. }
  1005. MenuOpened?.Invoke (mi);
  1006. }
  1007. /// <summary>
  1008. /// Virtual method that will invoke the <see cref="MenuClosing"/>
  1009. /// </summary>
  1010. /// <param name="currentMenu">The current menu to be closed.</param>
  1011. /// <param name="reopen">Whether the current menu will be reopen.</param>
  1012. /// <param name="isSubMenu">Whether is a sub-menu or not.</param>
  1013. public virtual MenuClosingEventArgs OnMenuClosing (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  1014. {
  1015. var ev = new MenuClosingEventArgs (currentMenu, reopen, isSubMenu);
  1016. MenuClosing?.Invoke (ev);
  1017. return ev;
  1018. }
  1019. /// <summary>
  1020. /// Virtual method that will invoke the <see cref="MenuAllClosed"/>
  1021. /// </summary>
  1022. public virtual void OnMenuAllClosed ()
  1023. {
  1024. MenuAllClosed?.Invoke ();
  1025. }
  1026. View lastFocused;
  1027. /// <summary>
  1028. /// Get the lasted focused view before open the menu.
  1029. /// </summary>
  1030. public View LastFocused { get; private set; }
  1031. internal void OpenMenu (int index, int sIndex = -1, MenuBarItem subMenu = null)
  1032. {
  1033. isMenuOpening = true;
  1034. var newMenu = OnMenuOpening (Menus [index]);
  1035. if (newMenu.Cancel) {
  1036. isMenuOpening = false;
  1037. return;
  1038. }
  1039. if (newMenu.NewMenuBarItem != null) {
  1040. Menus [index] = newMenu.NewMenuBarItem;
  1041. }
  1042. int pos = 0;
  1043. switch (subMenu) {
  1044. case null:
  1045. lastFocused = lastFocused ?? (SuperView == null ? Application.Current.MostFocused : SuperView.MostFocused);
  1046. if (openSubMenu != null && !CloseMenu (false, true))
  1047. return;
  1048. if (openMenu != null) {
  1049. if (SuperView == null) {
  1050. Application.Current.Remove (openMenu);
  1051. } else {
  1052. SuperView.Remove (openMenu);
  1053. }
  1054. openMenu.Dispose ();
  1055. }
  1056. for (int i = 0; i < index; i++)
  1057. pos += 1 + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + 2 : 0) + 2;
  1058. openMenu = new Menu (this, Frame.X + pos, Frame.Y + 1, Menus [index]);
  1059. openCurrentMenu = openMenu;
  1060. openCurrentMenu.previousSubFocused = openMenu;
  1061. if (SuperView == null) {
  1062. Application.Current.Add (openMenu);
  1063. } else {
  1064. SuperView.Add (openMenu);
  1065. }
  1066. openMenu.SetFocus ();
  1067. break;
  1068. default:
  1069. if (openSubMenu == null)
  1070. openSubMenu = new List<Menu> ();
  1071. if (sIndex > -1) {
  1072. RemoveSubMenu (sIndex);
  1073. } else {
  1074. var last = openSubMenu.Count > 0 ? openSubMenu.Last () : openMenu;
  1075. if (!UseSubMenusSingleFrame) {
  1076. openCurrentMenu = new Menu (this, last.Frame.Left + last.Frame.Width, last.Frame.Top + 1 + last.current, subMenu);
  1077. } else {
  1078. var first = openSubMenu.Count > 0 ? openSubMenu.First () : openMenu;
  1079. var mbi = new MenuItem [2 + subMenu.Children.Length];
  1080. mbi [0] = new MenuItem () { Title = subMenu.Title, Parent = subMenu };
  1081. mbi [1] = null;
  1082. for (int j = 0; j < subMenu.Children.Length; j++) {
  1083. mbi [j + 2] = subMenu.Children [j];
  1084. }
  1085. var newSubMenu = new MenuBarItem (mbi);
  1086. openCurrentMenu = new Menu (this, first.Frame.Left, first.Frame.Top, newSubMenu);
  1087. last.Visible = false;
  1088. Application.GrabMouse (openCurrentMenu);
  1089. }
  1090. openCurrentMenu.previousSubFocused = last.previousSubFocused;
  1091. openSubMenu.Add (openCurrentMenu);
  1092. if (SuperView == null) {
  1093. Application.Current.Add (openCurrentMenu);
  1094. } else {
  1095. SuperView.Add (openCurrentMenu);
  1096. }
  1097. }
  1098. selectedSub = openSubMenu.Count - 1;
  1099. if (selectedSub > -1 && SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1100. openCurrentMenu.SetFocus ();
  1101. }
  1102. break;
  1103. }
  1104. isMenuOpening = false;
  1105. IsMenuOpen = true;
  1106. }
  1107. /// <summary>
  1108. /// Opens the current Menu programatically.
  1109. /// </summary>
  1110. public void OpenMenu ()
  1111. {
  1112. var mbar = GetMouseGrabViewInstance (this);
  1113. if (mbar != null) {
  1114. mbar.CleanUp ();
  1115. }
  1116. if (openMenu != null)
  1117. return;
  1118. selected = 0;
  1119. SetNeedsDisplay ();
  1120. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1121. OpenMenu (selected);
  1122. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current) && !CloseMenu (false)) {
  1123. return;
  1124. }
  1125. if (!openCurrentMenu.CheckSubMenu ())
  1126. return;
  1127. Application.GrabMouse (this);
  1128. }
  1129. // Activates the menu, handles either first focus, or activating an entry when it was already active
  1130. // For mouse events.
  1131. internal void Activate (int idx, int sIdx = -1, MenuBarItem subMenu = null)
  1132. {
  1133. selected = idx;
  1134. selectedSub = sIdx;
  1135. if (openMenu == null)
  1136. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1137. OpenMenu (idx, sIdx, subMenu);
  1138. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)
  1139. && subMenu == null && !CloseMenu (false)) {
  1140. return;
  1141. }
  1142. SetNeedsDisplay ();
  1143. }
  1144. internal bool SelectEnabledItem (IEnumerable<MenuItem> chldren, int current, out int newCurrent, bool forward = true)
  1145. {
  1146. if (chldren == null) {
  1147. newCurrent = -1;
  1148. return true;
  1149. }
  1150. IEnumerable<MenuItem> childrens;
  1151. if (forward) {
  1152. childrens = chldren;
  1153. } else {
  1154. childrens = chldren.Reverse ();
  1155. }
  1156. int count;
  1157. if (forward) {
  1158. count = -1;
  1159. } else {
  1160. count = childrens.Count ();
  1161. }
  1162. foreach (var child in childrens) {
  1163. if (forward) {
  1164. if (++count < current) {
  1165. continue;
  1166. }
  1167. } else {
  1168. if (--count > current) {
  1169. continue;
  1170. }
  1171. }
  1172. if (child == null || !child.IsEnabled ()) {
  1173. if (forward) {
  1174. current++;
  1175. } else {
  1176. current--;
  1177. }
  1178. } else {
  1179. newCurrent = current;
  1180. return true;
  1181. }
  1182. }
  1183. newCurrent = -1;
  1184. return false;
  1185. }
  1186. /// <summary>
  1187. /// Closes the current Menu programatically, if open and not canceled.
  1188. /// </summary>
  1189. public bool CloseMenu (bool ignoreUseSubMenusSingleFrame = false)
  1190. {
  1191. return CloseMenu (false, false, ignoreUseSubMenusSingleFrame);
  1192. }
  1193. bool reopen;
  1194. internal bool CloseMenu (bool reopen = false, bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1195. {
  1196. var mbi = isSubMenu ? openCurrentMenu.barItems : openMenu?.barItems;
  1197. if (UseSubMenusSingleFrame && mbi != null &&
  1198. !ignoreUseSubMenusSingleFrame && mbi.Parent != null) {
  1199. return false;
  1200. }
  1201. isMenuClosing = true;
  1202. this.reopen = reopen;
  1203. var args = OnMenuClosing (mbi, reopen, isSubMenu);
  1204. if (args.Cancel) {
  1205. isMenuClosing = false;
  1206. if (args.CurrentMenu.Parent != null)
  1207. openMenu.current = ((MenuBarItem)args.CurrentMenu.Parent).Children.IndexOf (args.CurrentMenu);
  1208. return false;
  1209. }
  1210. switch (isSubMenu) {
  1211. case false:
  1212. if (openMenu != null) {
  1213. if (SuperView == null) {
  1214. Application.Current.Remove (openMenu);
  1215. } else {
  1216. SuperView?.Remove (openMenu);
  1217. }
  1218. }
  1219. SetNeedsDisplay ();
  1220. if (previousFocused != null && previousFocused is Menu && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
  1221. previousFocused.SetFocus ();
  1222. openMenu?.Dispose ();
  1223. openMenu = null;
  1224. if (lastFocused is Menu || lastFocused is MenuBar) {
  1225. lastFocused = null;
  1226. }
  1227. LastFocused = lastFocused;
  1228. lastFocused = null;
  1229. if (LastFocused != null && LastFocused.CanFocus) {
  1230. if (!reopen) {
  1231. selected = -1;
  1232. }
  1233. LastFocused.SetFocus ();
  1234. } else {
  1235. SetFocus ();
  1236. PositionCursor ();
  1237. }
  1238. IsMenuOpen = false;
  1239. break;
  1240. case true:
  1241. selectedSub = -1;
  1242. SetNeedsDisplay ();
  1243. RemoveAllOpensSubMenus ();
  1244. openCurrentMenu.previousSubFocused.SetFocus ();
  1245. openSubMenu = null;
  1246. IsMenuOpen = true;
  1247. break;
  1248. }
  1249. this.reopen = false;
  1250. isMenuClosing = false;
  1251. return true;
  1252. }
  1253. void RemoveSubMenu (int index, bool ignoreUseSubMenusSingleFrame = false)
  1254. {
  1255. if (openSubMenu == null || (UseSubMenusSingleFrame && !ignoreUseSubMenusSingleFrame && openSubMenu.Count > 0))
  1256. return;
  1257. for (int i = openSubMenu.Count - 1; i > index; i--) {
  1258. isMenuClosing = true;
  1259. Menu menu;
  1260. if (openSubMenu.Count - 1 > 0)
  1261. menu = openSubMenu [i - 1];
  1262. else
  1263. menu = openMenu;
  1264. if (!menu.Visible)
  1265. menu.Visible = true;
  1266. openCurrentMenu = menu;
  1267. openCurrentMenu.SetFocus ();
  1268. if (openSubMenu != null) {
  1269. menu = openSubMenu [i];
  1270. if (SuperView == null) {
  1271. Application.Current.Remove (menu);
  1272. } else {
  1273. SuperView.Remove (menu);
  1274. }
  1275. openSubMenu.Remove (menu);
  1276. menu.Dispose ();
  1277. }
  1278. RemoveSubMenu (i, ignoreUseSubMenusSingleFrame);
  1279. }
  1280. if (openSubMenu.Count > 0)
  1281. openCurrentMenu = openSubMenu.Last ();
  1282. //if (openMenu.Subviews.Count == 0)
  1283. // return;
  1284. //if (index == 0) {
  1285. // //SuperView.SetFocus (previousSubFocused);
  1286. // FocusPrev ();
  1287. // return;
  1288. //}
  1289. //for (int i = openMenu.Subviews.Count - 1; i > index; i--) {
  1290. // isMenuClosing = true;
  1291. // if (openMenu.Subviews.Count - 1 > 0)
  1292. // SuperView.SetFocus (openMenu.Subviews [i - 1]);
  1293. // else
  1294. // SuperView.SetFocus (openMenu);
  1295. // if (openMenu != null) {
  1296. // Remove (openMenu.Subviews [i]);
  1297. // openMenu.Remove (openMenu.Subviews [i]);
  1298. // }
  1299. // RemoveSubMenu (i);
  1300. //}
  1301. isMenuClosing = false;
  1302. }
  1303. internal void RemoveAllOpensSubMenus ()
  1304. {
  1305. if (openSubMenu != null) {
  1306. foreach (var item in openSubMenu) {
  1307. if (SuperView == null) {
  1308. Application.Current.Remove (item);
  1309. } else {
  1310. SuperView.Remove (item);
  1311. }
  1312. item.Dispose ();
  1313. }
  1314. }
  1315. }
  1316. internal void CloseAllMenus ()
  1317. {
  1318. if (!isMenuOpening && !isMenuClosing) {
  1319. if (openSubMenu != null && !CloseMenu (false, true))
  1320. return;
  1321. if (!CloseMenu (false))
  1322. return;
  1323. if (LastFocused != null && LastFocused != this)
  1324. selected = -1;
  1325. Application.UngrabMouse ();
  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.ConsoleWidth + 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. }