Menu.cs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  1. //
  2. // Menu.cs: application menus and submenus
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // TODO:
  8. // Add accelerator support, but should also support chords (Shortcut in MenuItem)
  9. // Allow menus inside menus
  10. using System;
  11. using NStack;
  12. using System.Linq;
  13. using System.Collections.Generic;
  14. namespace Terminal.Gui {
  15. /// <summary>
  16. /// Specifies how a <see cref="MenuItem"/> shows selection state.
  17. /// </summary>
  18. [Flags]
  19. public enum MenuItemCheckStyle {
  20. /// <summary>
  21. /// The menu item will be shown normally, with no check indicator.
  22. /// </summary>
  23. NoCheck = 0b_0000_0000,
  24. /// <summary>
  25. /// The menu item will indicate checked/un-checked state (see <see cref="Checked"/>.
  26. /// </summary>
  27. Checked = 0b_0000_0001,
  28. /// <summary>
  29. /// The menu item is part of a menu radio group (see <see cref="Checked"/> and will indicate selected state.
  30. /// </summary>
  31. Radio = 0b_0000_0010,
  32. };
  33. /// <summary>
  34. /// A <see cref="MenuItem"/> has a title, an associated help text, and an action to execute on activation.
  35. /// </summary>
  36. public class MenuItem {
  37. ustring title;
  38. ShortcutHelper shortcutHelper;
  39. /// <summary>
  40. /// Gets or sets arbitrary data for the menu item.
  41. /// </summary>
  42. /// <remarks>This property is not used internally.</remarks>
  43. public object Data { get; set; }
  44. /// <summary>
  45. /// Initializes a new instance of <see cref="MenuItem"/>
  46. /// </summary>
  47. public MenuItem (Key shortcut = Key.Null) : this ("", "", null, null, null, shortcut) { }
  48. /// <summary>
  49. /// Initializes a new instance of <see cref="MenuItem"/>.
  50. /// </summary>
  51. /// <param name="title">Title for the menu item.</param>
  52. /// <param name="help">Help text to display.</param>
  53. /// <param name="action">Action to invoke when the menu item is activated.</param>
  54. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  55. /// <param name="parent">The <see cref="Parent"/> of this menu item.</param>
  56. /// <param name="shortcut">The <see cref="Shortcut"/> keystroke combination.</param>
  57. public MenuItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null, Key shortcut = Key.Null)
  58. {
  59. Title = title ?? "";
  60. Help = help ?? "";
  61. Action = action;
  62. CanExecute = canExecute;
  63. Parent = parent;
  64. shortcutHelper = new ShortcutHelper ();
  65. if (shortcut != Key.Null) {
  66. shortcutHelper.Shortcut = shortcut;
  67. }
  68. }
  69. /// <summary>
  70. /// The HotKey is used when the menu is active, the shortcut can be triggered when the menu is not active.
  71. /// For example HotKey would be "N" when the File Menu is open (assuming there is a "_New" entry
  72. /// if the Shortcut is set to "Control-N", this would be a global hotkey that would trigger as well
  73. /// </summary>
  74. public Rune HotKey;
  75. /// <summary>
  76. /// This is the global setting that can be used as a global <see cref="ShortcutHelper.Shortcut"/> to invoke the action on the menu.
  77. /// </summary>
  78. public Key Shortcut {
  79. get => shortcutHelper.Shortcut;
  80. set {
  81. if (shortcutHelper.Shortcut != value && (ShortcutHelper.PostShortcutValidation (value) || value == Key.Null)) {
  82. shortcutHelper.Shortcut = value;
  83. }
  84. }
  85. }
  86. /// <summary>
  87. /// The keystroke combination used in the <see cref="ShortcutHelper.ShortcutTag"/> as string.
  88. /// </summary>
  89. public ustring ShortcutTag => ShortcutHelper.GetShortcutTag (shortcutHelper.Shortcut);
  90. /// <summary>
  91. /// Gets or sets the title.
  92. /// </summary>
  93. /// <value>The title.</value>
  94. public ustring Title {
  95. get { return title; }
  96. set {
  97. if (title != value) {
  98. title = value;
  99. GetHotKey ();
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// Gets or sets the help text for the menu item.
  105. /// </summary>
  106. /// <value>The help text.</value>
  107. public ustring Help { get; set; }
  108. /// <summary>
  109. /// Gets or sets the action to be invoked when the menu is triggered
  110. /// </summary>
  111. /// <value>Method to invoke.</value>
  112. public Action Action { get; set; }
  113. /// <summary>
  114. /// Gets or sets the action to be invoked if the menu can be triggered
  115. /// </summary>
  116. /// <value>Function to determine if action is ready to be executed.</value>
  117. public Func<bool> CanExecute { get; set; }
  118. /// <summary>
  119. /// Shortcut to check if the menu item is enabled
  120. /// </summary>
  121. public bool IsEnabled ()
  122. {
  123. return CanExecute == null ? true : CanExecute ();
  124. }
  125. internal int Width => Title.Sum (x => Rune.ColumnWidth (x)) + Help.Sum (x => Rune.ColumnWidth (x)) + 1 + 2 +
  126. (Checked || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio) ? 2 : 0) +
  127. (ShortcutTag.RuneCount > 0 ? ShortcutTag.RuneCount + 2 : 0);
  128. /// <summary>
  129. /// Sets or gets whether the <see cref="MenuItem"/> shows a check indicator or not. See <see cref="MenuItemCheckStyle"/>.
  130. /// </summary>
  131. public bool Checked { set; get; }
  132. /// <summary>
  133. /// Sets or gets the type selection indicator the menu item will be displayed with.
  134. /// </summary>
  135. public MenuItemCheckStyle CheckType { get; set; }
  136. /// <summary>
  137. /// Gets or sets the parent for this <see cref="MenuItem"/>.
  138. /// </summary>
  139. /// <value>The parent.</value>
  140. public MenuItem Parent { get; internal set; }
  141. /// <summary>
  142. /// Gets if this <see cref="MenuItem"/> is from a sub-menu.
  143. /// </summary>
  144. internal bool IsFromSubMenu { get { return Parent != null; } }
  145. /// <summary>
  146. /// Merely a debugging aid to see the interaction with main
  147. /// </summary>
  148. public MenuItem GetMenuItem ()
  149. {
  150. return this;
  151. }
  152. /// <summary>
  153. /// Merely a debugging aid to see the interaction with main
  154. /// </summary>
  155. public bool GetMenuBarItem ()
  156. {
  157. return IsFromSubMenu;
  158. }
  159. void GetHotKey ()
  160. {
  161. bool nextIsHot = false;
  162. foreach (var x in title) {
  163. if (x == MenuBar.HotKeySpecifier) {
  164. nextIsHot = true;
  165. } else {
  166. if (nextIsHot) {
  167. HotKey = Char.ToUpper ((char)x);
  168. break;
  169. }
  170. nextIsHot = false;
  171. HotKey = default;
  172. }
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// A <see cref="MenuBarItem"/> contains <see cref="MenuBarItem"/>s or <see cref="MenuItem"/>s.
  178. /// </summary>
  179. public class MenuBarItem : MenuItem {
  180. /// <summary>
  181. /// Initializes a new <see cref="MenuBarItem"/> as a <see cref="MenuItem"/>.
  182. /// </summary>
  183. /// <param name="title">Title for the menu item.</param>
  184. /// <param name="help">Help text to display.</param>
  185. /// <param name="action">Action to invoke when the menu item is activated.</param>
  186. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  187. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  188. public MenuBarItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null) : base (title, help, action, canExecute, parent)
  189. {
  190. Initialize (title, null, null, true);
  191. }
  192. /// <summary>
  193. /// Initializes a new <see cref="MenuBarItem"/>.
  194. /// </summary>
  195. /// <param name="title">Title for the menu item.</param>
  196. /// <param name="children">The items in the current menu.</param>
  197. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  198. public MenuBarItem (ustring title, MenuItem [] children, MenuItem parent = null)
  199. {
  200. Initialize (title, children, parent);
  201. }
  202. /// <summary>
  203. /// Initializes a new <see cref="MenuBarItem"/> with separate list of items.
  204. /// </summary>
  205. /// <param name="title">Title for the menu item.</param>
  206. /// <param name="children">The list of items in the current menu.</param>
  207. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  208. public MenuBarItem (ustring title, List<MenuItem []> children, MenuItem parent = null)
  209. {
  210. Initialize (title, children, parent);
  211. }
  212. /// <summary>
  213. /// Initializes a new <see cref="MenuBarItem"/>.
  214. /// </summary>
  215. /// <param name="children">The items in the current menu.</param>
  216. public MenuBarItem (MenuItem [] children) : this ("", children) { }
  217. /// <summary>
  218. /// Initializes a new <see cref="MenuBarItem"/>.
  219. /// </summary>
  220. public MenuBarItem () : this (children: new MenuItem [] { }) { }
  221. void Initialize (ustring title, object children, MenuItem parent = null, bool isTopLevel = false)
  222. {
  223. if (!isTopLevel && children == null) {
  224. throw new ArgumentNullException (nameof (children), "The parameter cannot be null. Use an empty array instead.");
  225. }
  226. SetTitle (title ?? "");
  227. if (parent != null) {
  228. Parent = parent;
  229. }
  230. if (children is List<MenuItem []>) {
  231. MenuItem [] childrens = new MenuItem [] { };
  232. foreach (var item in (List<MenuItem []>)children) {
  233. for (int i = 0; i < item.Length; i++) {
  234. SetChildrensParent (item);
  235. Array.Resize (ref childrens, childrens.Length + 1);
  236. childrens [childrens.Length - 1] = item [i];
  237. }
  238. }
  239. Children = childrens;
  240. } else if (children is MenuItem []) {
  241. SetChildrensParent ((MenuItem [])children);
  242. Children = (MenuItem [])children;
  243. } else {
  244. Children = null;
  245. }
  246. }
  247. //static int GetMaxTitleLength (MenuItem [] children)
  248. //{
  249. // int maxLength = 0;
  250. // foreach (var item in children) {
  251. // int len = GetMenuBarItemLength (item.Title);
  252. // if (len > maxLength)
  253. // maxLength = len;
  254. // item.IsFromSubMenu = true;
  255. // }
  256. // return maxLength;
  257. //}
  258. void SetChildrensParent (MenuItem [] childrens)
  259. {
  260. foreach (var child in childrens) {
  261. if (child != null && child.Parent == null) {
  262. child.Parent = this;
  263. }
  264. }
  265. }
  266. /// <summary>
  267. /// Check if the children parameter is a <see cref="MenuBarItem"/>.
  268. /// </summary>
  269. /// <param name="children"></param>
  270. /// <returns>Returns a <see cref="MenuBarItem"/> or null otherwise.</returns>
  271. public MenuBarItem SubMenu (MenuItem children)
  272. {
  273. return children as MenuBarItem;
  274. }
  275. /// <summary>
  276. /// Check if the <see cref="MenuItem"/> parameter is a child of this.
  277. /// </summary>
  278. /// <param name="menuItem"></param>
  279. /// <returns>Returns <c>true</c> if it is a child of this. <c>false</c> otherwise.</returns>
  280. public bool IsSubMenuOf (MenuItem menuItem)
  281. {
  282. foreach (var child in Children) {
  283. if (child == menuItem && child.Parent == menuItem.Parent) {
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. /// <summary>
  290. /// Get the index of the <see cref="MenuItem"/> parameter.
  291. /// </summary>
  292. /// <param name="children"></param>
  293. /// <returns>Returns a value bigger than -1 if the <see cref="MenuItem"/> is a child of this.</returns>
  294. public int GetChildrenIndex (MenuItem children)
  295. {
  296. if (Children?.Length == 0) {
  297. return -1;
  298. }
  299. int i = 0;
  300. foreach (var child in Children) {
  301. if (child == children) {
  302. return i;
  303. }
  304. i++;
  305. }
  306. return -1;
  307. }
  308. void SetTitle (ustring title)
  309. {
  310. if (title == null)
  311. title = "";
  312. Title = title;
  313. }
  314. int GetMenuBarItemLength (ustring title)
  315. {
  316. int len = 0;
  317. foreach (var ch in title) {
  318. if (ch == MenuBar.HotKeySpecifier)
  319. continue;
  320. len++;
  321. }
  322. return len;
  323. }
  324. ///// <summary>
  325. ///// Gets or sets the title to display.
  326. ///// </summary>
  327. ///// <value>The title.</value>
  328. //public ustring Title { get; set; }
  329. /// <summary>
  330. /// Gets or sets an array of <see cref="MenuItem"/> objects that are the children of this <see cref="MenuBarItem"/>
  331. /// </summary>
  332. /// <value>The children.</value>
  333. public MenuItem [] Children { get; set; }
  334. internal int TitleLength => GetMenuBarItemLength (Title);
  335. internal bool IsTopLevel { get => Parent == null && (Children == null || Children.Length == 0) && Action != null; }
  336. }
  337. class Menu : View {
  338. internal MenuBarItem barItems;
  339. internal MenuBar host;
  340. internal int current;
  341. internal View previousSubFocused;
  342. internal static Rect MakeFrame (int x, int y, MenuItem [] items)
  343. {
  344. if (items == null || items.Length == 0) {
  345. return new Rect ();
  346. }
  347. int maxW = items.Max (z => z?.Width) ?? 0;
  348. return new Rect (x, y, maxW + 2, items.Length + 2);
  349. }
  350. public Menu (MenuBar host, int x, int y, MenuBarItem barItems) : base (MakeFrame (x, y, barItems.Children))
  351. {
  352. this.barItems = barItems;
  353. this.host = host;
  354. if (barItems.IsTopLevel) {
  355. // This is a standalone MenuItem on a MenuBar
  356. ColorScheme = host.ColorScheme;
  357. CanFocus = true;
  358. } else {
  359. current = -1;
  360. for (int i = 0; i < barItems.Children?.Length; i++) {
  361. if (barItems.Children [i] != null) {
  362. current = i;
  363. break;
  364. }
  365. }
  366. ColorScheme = host.ColorScheme;
  367. CanFocus = true;
  368. WantMousePositionReports = host.WantMousePositionReports;
  369. }
  370. // Things this view knows how to do
  371. AddCommand (Command.LineUp, () => MoveUp ());
  372. AddCommand (Command.LineDown, () => MoveDown ());
  373. AddCommand (Command.Left, () => { this.host.PreviousMenu (true); return true; });
  374. AddCommand (Command.Right, () => {
  375. this.host.NextMenu (this.barItems.IsTopLevel || (this.barItems.Children != null
  376. && current > -1 && current < this.barItems.Children.Length && this.barItems.Children [current].IsFromSubMenu),
  377. current > -1 && host.UseSubMenusSingleFrame && this.barItems.SubMenu (this.barItems.Children [current]) != null);
  378. return true;
  379. });
  380. AddCommand (Command.Cancel, () => { CloseAllMenus (); return true; });
  381. AddCommand (Command.Accept, () => { RunSelected (); return true; });
  382. // Default keybindings for this view
  383. AddKeyBinding (Key.CursorUp, Command.LineUp);
  384. AddKeyBinding (Key.CursorDown, Command.LineDown);
  385. AddKeyBinding (Key.CursorLeft, Command.Left);
  386. AddKeyBinding (Key.CursorRight, Command.Right);
  387. AddKeyBinding (Key.Esc, Command.Cancel);
  388. AddKeyBinding (Key.Enter, Command.Accept);
  389. }
  390. internal Attribute DetermineColorSchemeFor (MenuItem item, int index)
  391. {
  392. if (item != null) {
  393. if (index == current) return ColorScheme.Focus;
  394. if (!item.IsEnabled ()) return ColorScheme.Disabled;
  395. }
  396. return GetNormalColor ();
  397. }
  398. public override void Redraw (Rect bounds)
  399. {
  400. Driver.SetAttribute (GetNormalColor ());
  401. DrawFrame (Bounds, padding: 0, fill: true);
  402. for (int i = Bounds.Y; i < barItems.Children.Length; i++) {
  403. if (i < 0)
  404. continue;
  405. var item = barItems.Children [i];
  406. Driver.SetAttribute (item == null ? GetNormalColor ()
  407. : i == current ? ColorScheme.Focus : GetNormalColor ());
  408. if (item == null) {
  409. Move (0, i + 1);
  410. Driver.AddRune (Driver.LeftTee);
  411. } else if (Frame.X + 1 < Driver.Cols)
  412. Move (1, i + 1);
  413. Driver.SetAttribute (DetermineColorSchemeFor (item, i));
  414. for (int p = Bounds.X; p < Frame.Width - 2; p++) {
  415. if (p < 0)
  416. continue;
  417. if (item == null)
  418. Driver.AddRune (Driver.HLine);
  419. else if (i == 0 && p == 0 && host.UseSubMenusSingleFrame && item.Parent.Parent != null)
  420. Driver.AddRune (Driver.LeftArrow);
  421. else if (p == Frame.Width - 3 && barItems.SubMenu (barItems.Children [i]) != null)
  422. Driver.AddRune (Driver.RightArrow);
  423. else
  424. Driver.AddRune (' ');
  425. }
  426. if (item == null) {
  427. if (SuperView?.Frame.Right - Frame.X > Frame.Width - 1) {
  428. Move (Frame.Width - 1, i + 1);
  429. Driver.AddRune (Driver.RightTee);
  430. }
  431. continue;
  432. }
  433. ustring textToDraw;
  434. var checkChar = Driver.Selected;
  435. var uncheckedChar = Driver.UnSelected;
  436. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked)) {
  437. checkChar = Driver.Checked;
  438. uncheckedChar = Driver.UnChecked;
  439. }
  440. // Support Checked even though CheckType wasn't set
  441. if (item.Checked) {
  442. textToDraw = ustring.Make (new Rune [] { checkChar, ' ' }) + item.Title;
  443. } else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio)) {
  444. textToDraw = ustring.Make (new Rune [] { uncheckedChar, ' ' }) + item.Title;
  445. } else {
  446. textToDraw = item.Title;
  447. }
  448. ViewToScreen (2, i + 1, out int vtsCol, out _, false);
  449. if (vtsCol < Driver.Cols) {
  450. Move (2, i + 1);
  451. if (!item.IsEnabled ()) {
  452. DrawHotString (textToDraw, ColorScheme.Disabled, ColorScheme.Disabled);
  453. } else if (i == 0 && host.UseSubMenusSingleFrame && item.Parent.Parent != null) {
  454. var tf = new TextFormatter () {
  455. Alignment = TextAlignment.Centered,
  456. HotKeySpecifier = MenuBar.HotKeySpecifier,
  457. Text = textToDraw
  458. };
  459. tf.Draw (ViewToScreen (new Rect (2, i + 1, Frame.Width - 3, 1)),
  460. i == current ? ColorScheme.Focus : GetNormalColor (),
  461. i == current ? ColorScheme.HotFocus : ColorScheme.HotNormal,
  462. 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.RuneCount == 0 ? item.Help.RuneCount : item.Help.RuneCount + item.ShortcutTag.RuneCount + 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.RuneCount;
  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.Length > 0 ? menu.Help.Length + 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 if (IsMenuOpen) {
  931. pos += 1 + Menus [i].TitleLength + (Menus [i].Help.Length > 0 ? Menus [i].Help.Length + 2 : 0) + 2;
  932. } else {
  933. pos += 2 + Menus [i].TitleLength + (Menus [i].Help.Length > 0 ? Menus [i].Help.Length + 2 : 0) + 1;
  934. }
  935. }
  936. }
  937. void Selected (MenuItem item)
  938. {
  939. var action = item.Action;
  940. if (action == null)
  941. return;
  942. Application.UngrabMouse ();
  943. CloseAllMenus ();
  944. Application.Refresh ();
  945. Application.MainLoop.AddIdle (() => {
  946. action ();
  947. return false;
  948. });
  949. }
  950. /// <summary>
  951. /// Raised as a menu is opening.
  952. /// </summary>
  953. public event Action<MenuOpeningEventArgs> MenuOpening;
  954. /// <summary>
  955. /// Raised when a menu is opened.
  956. /// </summary>
  957. public event Action<MenuItem> MenuOpened;
  958. /// <summary>
  959. /// Raised when a menu is closing passing <see cref="MenuClosingEventArgs"/>.
  960. /// </summary>
  961. public event Action<MenuClosingEventArgs> MenuClosing;
  962. /// <summary>
  963. /// Raised when all the menu are closed.
  964. /// </summary>
  965. public event Action MenuAllClosed;
  966. internal Menu openMenu;
  967. Menu ocm;
  968. internal Menu openCurrentMenu {
  969. get => ocm;
  970. set {
  971. if (ocm != value) {
  972. ocm = value;
  973. if (ocm.current > -1) {
  974. OnMenuOpened ();
  975. }
  976. }
  977. }
  978. }
  979. internal List<Menu> openSubMenu;
  980. View previousFocused;
  981. internal bool isMenuOpening;
  982. internal bool isMenuClosing;
  983. /// <summary>
  984. /// True if the menu is open; otherwise false.
  985. /// </summary>
  986. public bool IsMenuOpen { get; protected set; }
  987. /// <summary>
  988. /// Virtual method that will invoke the <see cref="MenuOpening"/> event if it's defined.
  989. /// </summary>
  990. /// <param name="currentMenu">The current menu to be replaced.</param>
  991. /// <returns>Returns the <see cref="MenuOpeningEventArgs"/></returns>
  992. public virtual MenuOpeningEventArgs OnMenuOpening (MenuBarItem currentMenu)
  993. {
  994. var ev = new MenuOpeningEventArgs (currentMenu);
  995. MenuOpening?.Invoke (ev);
  996. return ev;
  997. }
  998. /// <summary>
  999. /// Virtual method that will invoke the <see cref="MenuOpened"/> event if it's defined.
  1000. /// </summary>
  1001. public virtual void OnMenuOpened ()
  1002. {
  1003. MenuItem mi = null;
  1004. if (openCurrentMenu.barItems.Children != null) {
  1005. mi = openCurrentMenu.barItems.Children [openCurrentMenu.current];
  1006. }
  1007. MenuOpened?.Invoke (mi);
  1008. }
  1009. /// <summary>
  1010. /// Virtual method that will invoke the <see cref="MenuClosing"/>
  1011. /// </summary>
  1012. /// <param name="currentMenu">The current menu to be closed.</param>
  1013. /// <param name="reopen">Whether the current menu will be reopen.</param>
  1014. /// <param name="isSubMenu">Whether is a sub-menu or not.</param>
  1015. public virtual MenuClosingEventArgs OnMenuClosing (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  1016. {
  1017. var ev = new MenuClosingEventArgs (currentMenu, reopen, isSubMenu);
  1018. MenuClosing?.Invoke (ev);
  1019. return ev;
  1020. }
  1021. /// <summary>
  1022. /// Virtual method that will invoke the <see cref="MenuAllClosed"/>
  1023. /// </summary>
  1024. public virtual void OnMenuAllClosed ()
  1025. {
  1026. MenuAllClosed?.Invoke ();
  1027. }
  1028. View lastFocused;
  1029. /// <summary>
  1030. /// Get the lasted focused view before open the menu.
  1031. /// </summary>
  1032. public View LastFocused { get; private set; }
  1033. internal void OpenMenu (int index, int sIndex = -1, MenuBarItem subMenu = null)
  1034. {
  1035. isMenuOpening = true;
  1036. var newMenu = OnMenuOpening (Menus [index]);
  1037. if (newMenu.Cancel) {
  1038. isMenuOpening = false;
  1039. return;
  1040. }
  1041. if (newMenu.NewMenuBarItem != null) {
  1042. Menus [index] = newMenu.NewMenuBarItem;
  1043. }
  1044. int pos = 0;
  1045. switch (subMenu) {
  1046. case null:
  1047. lastFocused = lastFocused ?? (SuperView == null ? Application.Current.MostFocused : SuperView.MostFocused);
  1048. if (openSubMenu != null && !CloseMenu (false, true))
  1049. return;
  1050. if (openMenu != null) {
  1051. if (SuperView == null) {
  1052. Application.Current.Remove (openMenu);
  1053. } else {
  1054. SuperView.Remove (openMenu);
  1055. }
  1056. openMenu.Dispose ();
  1057. }
  1058. for (int i = 0; i < index; i++)
  1059. pos += Menus [i].Title.RuneCount + (Menus [i].Help.RuneCount > 0 ? Menus [i].Help.RuneCount + 2 : 0) + 2;
  1060. openMenu = new Menu (this, Frame.X + pos, Frame.Y + 1, Menus [index]);
  1061. openCurrentMenu = openMenu;
  1062. openCurrentMenu.previousSubFocused = openMenu;
  1063. if (SuperView == null) {
  1064. Application.Current.Add (openMenu);
  1065. } else {
  1066. SuperView.Add (openMenu);
  1067. }
  1068. openMenu.SetFocus ();
  1069. break;
  1070. default:
  1071. if (openSubMenu == null)
  1072. openSubMenu = new List<Menu> ();
  1073. if (sIndex > -1) {
  1074. RemoveSubMenu (sIndex);
  1075. } else {
  1076. var last = openSubMenu.Count > 0 ? openSubMenu.Last () : openMenu;
  1077. if (!UseSubMenusSingleFrame) {
  1078. openCurrentMenu = new Menu (this, last.Frame.Left + last.Frame.Width, last.Frame.Top + 1 + last.current, subMenu);
  1079. } else {
  1080. var first = openSubMenu.Count > 0 ? openSubMenu.First () : openMenu;
  1081. var mbi = new MenuItem [2 + subMenu.Children.Length];
  1082. mbi [0] = new MenuItem () { Title = subMenu.Title, Parent = subMenu };
  1083. mbi [1] = null;
  1084. for (int j = 0; j < subMenu.Children.Length; j++) {
  1085. mbi [j + 2] = subMenu.Children [j];
  1086. }
  1087. var newSubMenu = new MenuBarItem (mbi);
  1088. openCurrentMenu = new Menu (this, first.Frame.Left, first.Frame.Top, newSubMenu);
  1089. last.Visible = false;
  1090. Application.GrabMouse (openCurrentMenu);
  1091. }
  1092. openCurrentMenu.previousSubFocused = last.previousSubFocused;
  1093. openSubMenu.Add (openCurrentMenu);
  1094. if (SuperView == null) {
  1095. Application.Current.Add (openCurrentMenu);
  1096. } else {
  1097. SuperView.Add (openCurrentMenu);
  1098. }
  1099. }
  1100. selectedSub = openSubMenu.Count - 1;
  1101. if (selectedSub > -1 && SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1102. openCurrentMenu.SetFocus ();
  1103. }
  1104. break;
  1105. }
  1106. isMenuOpening = false;
  1107. IsMenuOpen = true;
  1108. }
  1109. /// <summary>
  1110. /// Opens the current Menu programatically.
  1111. /// </summary>
  1112. public void OpenMenu ()
  1113. {
  1114. var mbar = GetMouseGrabViewInstance (this);
  1115. if (mbar != null) {
  1116. mbar.CleanUp ();
  1117. }
  1118. if (openMenu != null)
  1119. return;
  1120. selected = 0;
  1121. SetNeedsDisplay ();
  1122. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1123. OpenMenu (selected);
  1124. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current) && !CloseMenu (false)) {
  1125. return;
  1126. }
  1127. if (!openCurrentMenu.CheckSubMenu ())
  1128. return;
  1129. Application.GrabMouse (this);
  1130. }
  1131. // Activates the menu, handles either first focus, or activating an entry when it was already active
  1132. // For mouse events.
  1133. internal void Activate (int idx, int sIdx = -1, MenuBarItem subMenu = null)
  1134. {
  1135. selected = idx;
  1136. selectedSub = sIdx;
  1137. if (openMenu == null)
  1138. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1139. OpenMenu (idx, sIdx, subMenu);
  1140. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)
  1141. && subMenu == null && !CloseMenu (false)) {
  1142. return;
  1143. }
  1144. SetNeedsDisplay ();
  1145. }
  1146. internal bool SelectEnabledItem (IEnumerable<MenuItem> chldren, int current, out int newCurrent, bool forward = true)
  1147. {
  1148. if (chldren == null) {
  1149. newCurrent = -1;
  1150. return true;
  1151. }
  1152. IEnumerable<MenuItem> childrens;
  1153. if (forward) {
  1154. childrens = chldren;
  1155. } else {
  1156. childrens = chldren.Reverse ();
  1157. }
  1158. int count;
  1159. if (forward) {
  1160. count = -1;
  1161. } else {
  1162. count = childrens.Count ();
  1163. }
  1164. foreach (var child in childrens) {
  1165. if (forward) {
  1166. if (++count < current) {
  1167. continue;
  1168. }
  1169. } else {
  1170. if (--count > current) {
  1171. continue;
  1172. }
  1173. }
  1174. if (child == null || !child.IsEnabled ()) {
  1175. if (forward) {
  1176. current++;
  1177. } else {
  1178. current--;
  1179. }
  1180. } else {
  1181. newCurrent = current;
  1182. return true;
  1183. }
  1184. }
  1185. newCurrent = -1;
  1186. return false;
  1187. }
  1188. /// <summary>
  1189. /// Closes the current Menu programatically, if open and not canceled.
  1190. /// </summary>
  1191. public bool CloseMenu (bool ignoreUseSubMenusSingleFrame = false)
  1192. {
  1193. return CloseMenu (false, false, ignoreUseSubMenusSingleFrame);
  1194. }
  1195. bool reopen;
  1196. internal bool CloseMenu (bool reopen = false, bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1197. {
  1198. var mbi = isSubMenu ? openCurrentMenu.barItems : openMenu?.barItems;
  1199. if (UseSubMenusSingleFrame && mbi != null &&
  1200. !ignoreUseSubMenusSingleFrame && mbi.Parent != null) {
  1201. return false;
  1202. }
  1203. isMenuClosing = true;
  1204. this.reopen = reopen;
  1205. var args = OnMenuClosing (mbi, reopen, isSubMenu);
  1206. if (args.Cancel) {
  1207. isMenuClosing = false;
  1208. if (args.CurrentMenu.Parent != null)
  1209. openMenu.current = ((MenuBarItem)args.CurrentMenu.Parent).Children.IndexOf (args.CurrentMenu);
  1210. return false;
  1211. }
  1212. switch (isSubMenu) {
  1213. case false:
  1214. if (openMenu != null) {
  1215. if (SuperView == null) {
  1216. Application.Current.Remove (openMenu);
  1217. } else {
  1218. SuperView?.Remove (openMenu);
  1219. }
  1220. }
  1221. SetNeedsDisplay ();
  1222. if (previousFocused != null && previousFocused is Menu && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
  1223. previousFocused.SetFocus ();
  1224. openMenu?.Dispose ();
  1225. openMenu = null;
  1226. if (lastFocused is Menu || lastFocused is MenuBar) {
  1227. lastFocused = null;
  1228. }
  1229. LastFocused = lastFocused;
  1230. lastFocused = null;
  1231. if (LastFocused != null && LastFocused.CanFocus) {
  1232. if (!reopen) {
  1233. selected = -1;
  1234. }
  1235. LastFocused.SetFocus ();
  1236. } else {
  1237. SetFocus ();
  1238. PositionCursor ();
  1239. }
  1240. IsMenuOpen = false;
  1241. break;
  1242. case true:
  1243. selectedSub = -1;
  1244. SetNeedsDisplay ();
  1245. RemoveAllOpensSubMenus ();
  1246. openCurrentMenu.previousSubFocused.SetFocus ();
  1247. openSubMenu = null;
  1248. IsMenuOpen = true;
  1249. break;
  1250. }
  1251. this.reopen = false;
  1252. isMenuClosing = false;
  1253. return true;
  1254. }
  1255. void RemoveSubMenu (int index, bool ignoreUseSubMenusSingleFrame = false)
  1256. {
  1257. if (openSubMenu == null || (UseSubMenusSingleFrame && !ignoreUseSubMenusSingleFrame && openSubMenu.Count > 0))
  1258. return;
  1259. for (int i = openSubMenu.Count - 1; i > index; i--) {
  1260. isMenuClosing = true;
  1261. Menu menu;
  1262. if (openSubMenu.Count - 1 > 0)
  1263. menu = openSubMenu [i - 1];
  1264. else
  1265. menu = openMenu;
  1266. if (!menu.Visible)
  1267. menu.Visible = true;
  1268. openCurrentMenu = menu;
  1269. openCurrentMenu.SetFocus ();
  1270. if (openSubMenu != null) {
  1271. menu = openSubMenu [i];
  1272. if (SuperView == null) {
  1273. Application.Current.Remove (menu);
  1274. } else {
  1275. SuperView.Remove (menu);
  1276. }
  1277. openSubMenu.Remove (menu);
  1278. menu.Dispose ();
  1279. }
  1280. RemoveSubMenu (i, ignoreUseSubMenusSingleFrame);
  1281. }
  1282. if (openSubMenu.Count > 0)
  1283. openCurrentMenu = openSubMenu.Last ();
  1284. //if (openMenu.Subviews.Count == 0)
  1285. // return;
  1286. //if (index == 0) {
  1287. // //SuperView.SetFocus (previousSubFocused);
  1288. // FocusPrev ();
  1289. // return;
  1290. //}
  1291. //for (int i = openMenu.Subviews.Count - 1; i > index; i--) {
  1292. // isMenuClosing = true;
  1293. // if (openMenu.Subviews.Count - 1 > 0)
  1294. // SuperView.SetFocus (openMenu.Subviews [i - 1]);
  1295. // else
  1296. // SuperView.SetFocus (openMenu);
  1297. // if (openMenu != null) {
  1298. // Remove (openMenu.Subviews [i]);
  1299. // openMenu.Remove (openMenu.Subviews [i]);
  1300. // }
  1301. // RemoveSubMenu (i);
  1302. //}
  1303. isMenuClosing = false;
  1304. }
  1305. internal void RemoveAllOpensSubMenus ()
  1306. {
  1307. if (openSubMenu != null) {
  1308. foreach (var item in openSubMenu) {
  1309. if (SuperView == null) {
  1310. Application.Current.Remove (item);
  1311. } else {
  1312. SuperView.Remove (item);
  1313. }
  1314. item.Dispose ();
  1315. }
  1316. }
  1317. }
  1318. internal void CloseAllMenus ()
  1319. {
  1320. if (!isMenuOpening && !isMenuClosing) {
  1321. if (openSubMenu != null && !CloseMenu (false, true))
  1322. return;
  1323. if (!CloseMenu (false))
  1324. return;
  1325. if (LastFocused != null && LastFocused != this)
  1326. selected = -1;
  1327. }
  1328. IsMenuOpen = false;
  1329. openedByHotKey = false;
  1330. openedByAltKey = false;
  1331. OnMenuAllClosed ();
  1332. }
  1333. View FindDeepestMenu (View view, ref int count)
  1334. {
  1335. count = count > 0 ? count : 0;
  1336. foreach (var menu in view.Subviews) {
  1337. if (menu is Menu) {
  1338. count++;
  1339. return FindDeepestMenu ((Menu)menu, ref count);
  1340. }
  1341. }
  1342. return view;
  1343. }
  1344. internal void PreviousMenu (bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1345. {
  1346. switch (isSubMenu) {
  1347. case false:
  1348. if (selected <= 0)
  1349. selected = Menus.Length - 1;
  1350. else
  1351. selected--;
  1352. if (selected > -1 && !CloseMenu (true, false, ignoreUseSubMenusSingleFrame))
  1353. return;
  1354. OpenMenu (selected);
  1355. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current, false)) {
  1356. openCurrentMenu.current = 0;
  1357. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1358. CloseMenu (ignoreUseSubMenusSingleFrame);
  1359. }
  1360. }
  1361. break;
  1362. case true:
  1363. if (selectedSub > -1) {
  1364. selectedSub--;
  1365. RemoveSubMenu (selectedSub, ignoreUseSubMenusSingleFrame);
  1366. SetNeedsDisplay ();
  1367. } else
  1368. PreviousMenu ();
  1369. break;
  1370. }
  1371. }
  1372. internal void NextMenu (bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1373. {
  1374. switch (isSubMenu) {
  1375. case false:
  1376. if (selected == -1)
  1377. selected = 0;
  1378. else if (selected + 1 == Menus.Length)
  1379. selected = 0;
  1380. else
  1381. selected++;
  1382. if (selected > -1 && !CloseMenu (true, ignoreUseSubMenusSingleFrame))
  1383. return;
  1384. OpenMenu (selected);
  1385. SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current);
  1386. break;
  1387. case true:
  1388. if (UseKeysUpDownAsKeysLeftRight) {
  1389. if (CloseMenu (false, true, ignoreUseSubMenusSingleFrame)) {
  1390. NextMenu (false, ignoreUseSubMenusSingleFrame);
  1391. }
  1392. } else {
  1393. var subMenu = openCurrentMenu.barItems.SubMenu (openCurrentMenu.barItems.Children [openCurrentMenu.current]);
  1394. if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && subMenu == null) {
  1395. if (openSubMenu != null && !CloseMenu (false, true))
  1396. return;
  1397. NextMenu (false, ignoreUseSubMenusSingleFrame);
  1398. } else if (subMenu != null ||
  1399. !openCurrentMenu.barItems.Children [openCurrentMenu.current].IsFromSubMenu)
  1400. selectedSub++;
  1401. else
  1402. return;
  1403. SetNeedsDisplay ();
  1404. openCurrentMenu.CheckSubMenu ();
  1405. }
  1406. break;
  1407. }
  1408. }
  1409. bool openedByHotKey;
  1410. internal bool FindAndOpenMenuByHotkey (KeyEvent kb)
  1411. {
  1412. //int pos = 0;
  1413. var c = ((uint)kb.Key & (uint)Key.CharMask);
  1414. for (int i = 0; i < Menus.Length; i++) {
  1415. // TODO: this code is duplicated, hotkey should be part of the MenuBarItem
  1416. var mi = Menus [i];
  1417. int p = mi.Title.IndexOf (MenuBar.HotKeySpecifier);
  1418. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1419. if (Char.ToUpperInvariant ((char)mi.Title [p + 1]) == c) {
  1420. ProcessMenu (i, mi);
  1421. return true;
  1422. }
  1423. }
  1424. }
  1425. return false;
  1426. }
  1427. internal bool FindAndOpenMenuByShortcut (KeyEvent kb, MenuItem [] children = null)
  1428. {
  1429. if (children == null) {
  1430. children = Menus;
  1431. }
  1432. var key = kb.KeyValue;
  1433. var keys = ShortcutHelper.GetModifiersKey (kb);
  1434. key |= (int)keys;
  1435. for (int i = 0; i < children.Length; i++) {
  1436. var mi = children [i];
  1437. if (mi == null) {
  1438. continue;
  1439. }
  1440. if ((!(mi is MenuBarItem mbiTopLevel) || mbiTopLevel.IsTopLevel) && mi.Shortcut != Key.Null && mi.Shortcut == (Key)key) {
  1441. var action = mi.Action;
  1442. if (action != null) {
  1443. Application.MainLoop.AddIdle (() => {
  1444. action ();
  1445. return false;
  1446. });
  1447. }
  1448. return true;
  1449. }
  1450. if (mi is MenuBarItem menuBarItem && !menuBarItem.IsTopLevel && FindAndOpenMenuByShortcut (kb, menuBarItem.Children)) {
  1451. return true;
  1452. }
  1453. }
  1454. return false;
  1455. }
  1456. private void ProcessMenu (int i, MenuBarItem mi)
  1457. {
  1458. if (selected < 0 && IsMenuOpen) {
  1459. return;
  1460. }
  1461. if (mi.IsTopLevel) {
  1462. var menu = new Menu (this, i, 0, mi);
  1463. menu.Run (mi.Action);
  1464. menu.Dispose ();
  1465. } else {
  1466. openedByHotKey = true;
  1467. Application.GrabMouse (this);
  1468. selected = i;
  1469. OpenMenu (i);
  1470. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current) && !CloseMenu (false)) {
  1471. return;
  1472. }
  1473. if (!openCurrentMenu.CheckSubMenu ())
  1474. return;
  1475. }
  1476. SetNeedsDisplay ();
  1477. }
  1478. ///<inheritdoc/>
  1479. public override bool ProcessHotKey (KeyEvent kb)
  1480. {
  1481. if (kb.Key == Key.F9) {
  1482. if (!IsMenuOpen)
  1483. OpenMenu ();
  1484. else
  1485. CloseAllMenus ();
  1486. return true;
  1487. }
  1488. // To ncurses simulate a AltMask key pressing Alt+Space because
  1489. // it can't detect an alone special key down was pressed.
  1490. if (kb.IsAlt && kb.Key == Key.AltMask && openMenu == null) {
  1491. OnKeyDown (kb);
  1492. OnKeyUp (kb);
  1493. return true;
  1494. } else if (kb.IsAlt && !kb.IsCtrl && !kb.IsShift) {
  1495. if (FindAndOpenMenuByHotkey (kb)) return true;
  1496. }
  1497. //var kc = kb.KeyValue;
  1498. return base.ProcessHotKey (kb);
  1499. }
  1500. ///<inheritdoc/>
  1501. public override bool ProcessKey (KeyEvent kb)
  1502. {
  1503. if (InvokeKeybindings (kb) == true)
  1504. return true;
  1505. var key = kb.KeyValue;
  1506. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  1507. char c = Char.ToUpper ((char)key);
  1508. if (selected == -1 || Menus [selected].IsTopLevel)
  1509. return false;
  1510. foreach (var mi in Menus [selected].Children) {
  1511. if (mi == null)
  1512. continue;
  1513. int p = mi.Title.IndexOf (MenuBar.HotKeySpecifier);
  1514. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1515. if (mi.Title [p + 1] == c) {
  1516. Selected (mi);
  1517. return true;
  1518. }
  1519. }
  1520. }
  1521. }
  1522. return false;
  1523. }
  1524. void CloseMenuBar ()
  1525. {
  1526. if (!CloseMenu (false))
  1527. return;
  1528. if (openedByAltKey) {
  1529. openedByAltKey = false;
  1530. LastFocused?.SetFocus ();
  1531. }
  1532. SetNeedsDisplay ();
  1533. }
  1534. void MoveRight ()
  1535. {
  1536. selected = (selected + 1) % Menus.Length;
  1537. OpenMenu (selected);
  1538. SetNeedsDisplay ();
  1539. }
  1540. void MoveLeft ()
  1541. {
  1542. selected--;
  1543. if (selected < 0)
  1544. selected = Menus.Length - 1;
  1545. OpenMenu (selected);
  1546. SetNeedsDisplay ();
  1547. }
  1548. ///<inheritdoc/>
  1549. public override bool ProcessColdKey (KeyEvent kb)
  1550. {
  1551. return FindAndOpenMenuByShortcut (kb);
  1552. }
  1553. ///<inheritdoc/>
  1554. public override bool MouseEvent (MouseEvent me)
  1555. {
  1556. if (!handled && !HandleGrabView (me, this)) {
  1557. return false;
  1558. }
  1559. handled = false;
  1560. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.Button1Clicked ||
  1561. (me.Flags == MouseFlags.ReportMousePosition && selected > -1) ||
  1562. (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && selected > -1)) {
  1563. int pos = 1;
  1564. int cx = me.X;
  1565. for (int i = 0; i < Menus.Length; i++) {
  1566. if (cx >= pos && cx < pos + 1 + Menus [i].TitleLength + Menus [i].Help.RuneCount + 2) {
  1567. if (me.Flags == MouseFlags.Button1Clicked) {
  1568. if (Menus [i].IsTopLevel) {
  1569. var menu = new Menu (this, i, 0, Menus [i]);
  1570. menu.Run (Menus [i].Action);
  1571. menu.Dispose ();
  1572. } else if (!IsMenuOpen) {
  1573. Activate (i);
  1574. }
  1575. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked) {
  1576. if (IsMenuOpen && !Menus [i].IsTopLevel) {
  1577. CloseAllMenus ();
  1578. } else if (!Menus [i].IsTopLevel) {
  1579. Activate (i);
  1580. }
  1581. } else if (selected != i && selected > -1 && (me.Flags == MouseFlags.ReportMousePosition ||
  1582. me.Flags == MouseFlags.Button1Pressed && me.Flags == MouseFlags.ReportMousePosition)) {
  1583. if (IsMenuOpen) {
  1584. if (!CloseMenu (true, false)) {
  1585. return true;
  1586. }
  1587. Activate (i);
  1588. }
  1589. } else {
  1590. if (IsMenuOpen)
  1591. Activate (i);
  1592. }
  1593. return true;
  1594. }
  1595. pos += 1 + Menus [i].TitleLength + 2;
  1596. }
  1597. }
  1598. return false;
  1599. }
  1600. internal bool handled;
  1601. internal bool isContextMenuLoading;
  1602. internal bool HandleGrabView (MouseEvent me, View current)
  1603. {
  1604. if (Application.mouseGrabView != null) {
  1605. if (me.View is MenuBar || me.View is Menu) {
  1606. var mbar = GetMouseGrabViewInstance (me.View);
  1607. if (mbar != null) {
  1608. if (me.Flags == MouseFlags.Button1Clicked) {
  1609. mbar.CleanUp ();
  1610. Application.GrabMouse (me.View);
  1611. } else {
  1612. handled = false;
  1613. return false;
  1614. }
  1615. }
  1616. if (me.View != current) {
  1617. Application.UngrabMouse ();
  1618. var v = me.View;
  1619. Application.GrabMouse (v);
  1620. MouseEvent nme;
  1621. if (me.Y > -1) {
  1622. var newxy = v.ScreenToView (me.X, me.Y);
  1623. nme = new MouseEvent () {
  1624. X = newxy.X,
  1625. Y = newxy.Y,
  1626. Flags = me.Flags,
  1627. OfX = me.X - newxy.X,
  1628. OfY = me.Y - newxy.Y,
  1629. View = v
  1630. };
  1631. } else {
  1632. nme = new MouseEvent () {
  1633. X = me.X + current.Frame.X,
  1634. Y = 0,
  1635. Flags = me.Flags,
  1636. View = v
  1637. };
  1638. }
  1639. v.MouseEvent (nme);
  1640. return false;
  1641. }
  1642. } else if (!isContextMenuLoading && !(me.View is MenuBar || me.View is Menu)
  1643. && me.Flags != MouseFlags.ReportMousePosition && me.Flags != 0) {
  1644. Application.UngrabMouse ();
  1645. if (IsMenuOpen)
  1646. CloseAllMenus ();
  1647. handled = false;
  1648. return false;
  1649. } else {
  1650. handled = false;
  1651. isContextMenuLoading = false;
  1652. return false;
  1653. }
  1654. } else if (!IsMenuOpen && (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked
  1655. || me.Flags == MouseFlags.Button1TripleClicked || me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  1656. Application.GrabMouse (current);
  1657. } else if (IsMenuOpen && (me.View is MenuBar || me.View is Menu)) {
  1658. Application.GrabMouse (me.View);
  1659. } else {
  1660. handled = false;
  1661. return false;
  1662. }
  1663. //if (me.View != this && me.Flags != MouseFlags.Button1Pressed)
  1664. // return true;
  1665. //else if (me.View != this && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1666. // Application.UngrabMouse ();
  1667. // host.CloseAllMenus ();
  1668. // return true;
  1669. //}
  1670. //if (!(me.View is MenuBar) && !(me.View is Menu) && me.Flags != MouseFlags.Button1Pressed))
  1671. // return false;
  1672. //if (Application.mouseGrabView != null) {
  1673. // if (me.View is MenuBar || me.View is Menu) {
  1674. // me.X -= me.OfX;
  1675. // me.Y -= me.OfY;
  1676. // me.View.MouseEvent (me);
  1677. // return true;
  1678. // } else if (!(me.View is MenuBar || me.View is Menu) && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1679. // Application.UngrabMouse ();
  1680. // CloseAllMenus ();
  1681. // }
  1682. //} else if (!isMenuClosed && selected == -1 && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1683. // Application.GrabMouse (this);
  1684. // return true;
  1685. //}
  1686. //if (Application.mouseGrabView != null) {
  1687. // if (Application.mouseGrabView == me.View && me.View == current) {
  1688. // me.X -= me.OfX;
  1689. // me.Y -= me.OfY;
  1690. // } else if (me.View != current && me.View is MenuBar && me.View is Menu) {
  1691. // Application.UngrabMouse ();
  1692. // Application.GrabMouse (me.View);
  1693. // } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1694. // Application.UngrabMouse ();
  1695. // CloseMenu ();
  1696. // }
  1697. //} else if ((!isMenuClosed && selected > -1)) {
  1698. // Application.GrabMouse (current);
  1699. //}
  1700. handled = true;
  1701. return true;
  1702. }
  1703. MenuBar GetMouseGrabViewInstance (View view)
  1704. {
  1705. if (view == null || Application.mouseGrabView == null) {
  1706. return null;
  1707. }
  1708. MenuBar hostView = null;
  1709. if (view is MenuBar) {
  1710. hostView = (MenuBar)view;
  1711. } else if (view is Menu) {
  1712. hostView = ((Menu)view).host;
  1713. }
  1714. var grabView = Application.mouseGrabView;
  1715. MenuBar hostGrabView = null;
  1716. if (grabView is MenuBar) {
  1717. hostGrabView = (MenuBar)grabView;
  1718. } else if (grabView is Menu) {
  1719. hostGrabView = ((Menu)grabView).host;
  1720. }
  1721. return hostView != hostGrabView ? hostGrabView : null;
  1722. }
  1723. ///<inheritdoc/>
  1724. public override bool OnEnter (View view)
  1725. {
  1726. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  1727. return base.OnEnter (view);
  1728. }
  1729. }
  1730. /// <summary>
  1731. /// An <see cref="EventArgs"/> which allows passing a cancelable menu opening event or replacing with a new <see cref="MenuBarItem"/>.
  1732. /// </summary>
  1733. public class MenuOpeningEventArgs : EventArgs {
  1734. /// <summary>
  1735. /// The current <see cref="MenuBarItem"/> parent.
  1736. /// </summary>
  1737. public MenuBarItem CurrentMenu { get; }
  1738. /// <summary>
  1739. /// The new <see cref="MenuBarItem"/> to be replaced.
  1740. /// </summary>
  1741. public MenuBarItem NewMenuBarItem { get; set; }
  1742. /// <summary>
  1743. /// Flag that allows you to cancel the opening of the menu.
  1744. /// </summary>
  1745. public bool Cancel { get; set; }
  1746. /// <summary>
  1747. /// Initializes a new instance of <see cref="MenuOpeningEventArgs"/>
  1748. /// </summary>
  1749. /// <param name="currentMenu">The current <see cref="MenuBarItem"/> parent.</param>
  1750. public MenuOpeningEventArgs (MenuBarItem currentMenu)
  1751. {
  1752. CurrentMenu = currentMenu;
  1753. }
  1754. }
  1755. /// <summary>
  1756. /// An <see cref="EventArgs"/> which allows passing a cancelable menu closing event.
  1757. /// </summary>
  1758. public class MenuClosingEventArgs : EventArgs {
  1759. /// <summary>
  1760. /// The current <see cref="MenuBarItem"/> parent.
  1761. /// </summary>
  1762. public MenuBarItem CurrentMenu { get; }
  1763. /// <summary>
  1764. /// Indicates whether the current menu will be reopen.
  1765. /// </summary>
  1766. public bool Reopen { get; }
  1767. /// <summary>
  1768. /// Indicates whether the current menu is a sub-menu.
  1769. /// </summary>
  1770. public bool IsSubMenu { get; }
  1771. /// <summary>
  1772. /// Flag that allows you to cancel the opening of the menu.
  1773. /// </summary>
  1774. public bool Cancel { get; set; }
  1775. /// <summary>
  1776. /// Initializes a new instance of <see cref="MenuClosingEventArgs"/>
  1777. /// </summary>
  1778. /// <param name="currentMenu">The current <see cref="MenuBarItem"/> parent.</param>
  1779. /// <param name="reopen">Whether the current menu will be reopen.</param>
  1780. /// <param name="isSubMenu">Indicates whether it is a sub-menu.</param>
  1781. public MenuClosingEventArgs (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  1782. {
  1783. CurrentMenu = currentMenu;
  1784. Reopen = reopen;
  1785. IsSubMenu = isSubMenu;
  1786. }
  1787. }
  1788. }