Menu.cs 57 KB

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