Menu.cs 56 KB

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