Menu.cs 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  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. static int leftPadding = 1;
  918. static int rightPadding = 1;
  919. ///<inheritdoc/>
  920. public override void Redraw (Rect bounds)
  921. {
  922. Move (0, 0);
  923. Driver.SetAttribute (GetNormalColor ());
  924. for (int i = 0; i < Frame.Width; i++)
  925. Driver.AddRune (' ');
  926. Move (1, 0);
  927. int pos = 0;
  928. for (int i = 0; i < Menus.Length; i++) {
  929. var menu = Menus [i];
  930. Move (pos, 0);
  931. Attribute hotColor, normalColor;
  932. if (i == selected && IsMenuOpen) {
  933. hotColor = i == selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  934. normalColor = i == selected ? ColorScheme.Focus :
  935. GetNormalColor ();
  936. } else if (openedByAltKey) {
  937. hotColor = ColorScheme.HotNormal;
  938. normalColor = GetNormalColor ();
  939. } else {
  940. hotColor = GetNormalColor ();
  941. normalColor = GetNormalColor ();
  942. }
  943. DrawHotString (menu.Help.IsEmpty ? $" {menu.Title} " : $" {menu.Title} ({menu.Help}) ", hotColor, normalColor);
  944. pos += leftPadding + menu.TitleLength + (menu.Help.ConsoleWidth > 0 ? menu.Help.ConsoleWidth + 3 : 0) + rightPadding;
  945. }
  946. PositionCursor ();
  947. }
  948. ///<inheritdoc/>
  949. public override void PositionCursor ()
  950. {
  951. if (selected == -1 && HasFocus && Menus.Length > 0) {
  952. selected = 0;
  953. }
  954. int pos = 0;
  955. for (int i = 0; i < Menus.Length; i++) {
  956. if (i == selected) {
  957. pos++;
  958. if (IsMenuOpen)
  959. Move (pos + 1, 0);
  960. else {
  961. Move (pos + 1, 0);
  962. }
  963. return;
  964. } else {
  965. pos += leftPadding + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + 3 : 0) + rightPadding;
  966. }
  967. }
  968. }
  969. void Selected (MenuItem item)
  970. {
  971. var action = item.Action;
  972. if (action == null)
  973. return;
  974. Application.UngrabMouse ();
  975. CloseAllMenus ();
  976. Application.Refresh ();
  977. Application.MainLoop.AddIdle (() => {
  978. action ();
  979. return false;
  980. });
  981. }
  982. /// <summary>
  983. /// Raised as a menu is opening.
  984. /// </summary>
  985. public event Action<MenuOpeningEventArgs> MenuOpening;
  986. /// <summary>
  987. /// Raised when a menu is opened.
  988. /// </summary>
  989. public event Action<MenuItem> MenuOpened;
  990. /// <summary>
  991. /// Raised when a menu is closing passing <see cref="MenuClosingEventArgs"/>.
  992. /// </summary>
  993. public event Action<MenuClosingEventArgs> MenuClosing;
  994. /// <summary>
  995. /// Raised when all the menu are closed.
  996. /// </summary>
  997. public event Action MenuAllClosed;
  998. internal Menu openMenu;
  999. Menu ocm;
  1000. internal Menu openCurrentMenu {
  1001. get => ocm;
  1002. set {
  1003. if (ocm != value) {
  1004. ocm = value;
  1005. if (ocm.current > -1) {
  1006. OnMenuOpened ();
  1007. }
  1008. }
  1009. }
  1010. }
  1011. internal List<Menu> openSubMenu;
  1012. View previousFocused;
  1013. internal bool isMenuOpening;
  1014. internal bool isMenuClosing;
  1015. /// <summary>
  1016. /// True if the menu is open; otherwise false.
  1017. /// </summary>
  1018. public bool IsMenuOpen { get; protected set; }
  1019. /// <summary>
  1020. /// Virtual method that will invoke the <see cref="MenuOpening"/> event if it's defined.
  1021. /// </summary>
  1022. /// <param name="currentMenu">The current menu to be replaced.</param>
  1023. /// <returns>Returns the <see cref="MenuOpeningEventArgs"/></returns>
  1024. public virtual MenuOpeningEventArgs OnMenuOpening (MenuBarItem currentMenu)
  1025. {
  1026. var ev = new MenuOpeningEventArgs (currentMenu);
  1027. MenuOpening?.Invoke (ev);
  1028. return ev;
  1029. }
  1030. /// <summary>
  1031. /// Virtual method that will invoke the <see cref="MenuOpened"/> event if it's defined.
  1032. /// </summary>
  1033. public virtual void OnMenuOpened ()
  1034. {
  1035. MenuItem mi = null;
  1036. if (openCurrentMenu.barItems.Children != null && openCurrentMenu?.current > -1) {
  1037. mi = openCurrentMenu.barItems.Children [openCurrentMenu.current];
  1038. } else if (openCurrentMenu.barItems.IsTopLevel) {
  1039. mi = openCurrentMenu.barItems;
  1040. } else {
  1041. mi = openMenu.barItems.Children [openMenu.current];
  1042. }
  1043. MenuOpened?.Invoke (mi);
  1044. }
  1045. /// <summary>
  1046. /// Virtual method that will invoke the <see cref="MenuClosing"/>
  1047. /// </summary>
  1048. /// <param name="currentMenu">The current menu to be closed.</param>
  1049. /// <param name="reopen">Whether the current menu will be reopen.</param>
  1050. /// <param name="isSubMenu">Whether is a sub-menu or not.</param>
  1051. public virtual MenuClosingEventArgs OnMenuClosing (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  1052. {
  1053. var ev = new MenuClosingEventArgs (currentMenu, reopen, isSubMenu);
  1054. MenuClosing?.Invoke (ev);
  1055. return ev;
  1056. }
  1057. /// <summary>
  1058. /// Virtual method that will invoke the <see cref="MenuAllClosed"/>
  1059. /// </summary>
  1060. public virtual void OnMenuAllClosed ()
  1061. {
  1062. MenuAllClosed?.Invoke ();
  1063. }
  1064. View lastFocused;
  1065. /// <summary>
  1066. /// Get the lasted focused view before open the menu.
  1067. /// </summary>
  1068. public View LastFocused { get; private set; }
  1069. internal void OpenMenu (int index, int sIndex = -1, MenuBarItem subMenu = null)
  1070. {
  1071. isMenuOpening = true;
  1072. var newMenu = OnMenuOpening (Menus [index]);
  1073. if (newMenu.Cancel) {
  1074. isMenuOpening = false;
  1075. return;
  1076. }
  1077. if (newMenu.NewMenuBarItem != null) {
  1078. Menus [index] = newMenu.NewMenuBarItem;
  1079. }
  1080. int pos = 0;
  1081. switch (subMenu) {
  1082. case null:
  1083. lastFocused = lastFocused ?? (SuperView == null ? Application.Current.MostFocused : SuperView.MostFocused);
  1084. if (openSubMenu != null && !CloseMenu (false, true))
  1085. return;
  1086. if (openMenu != null) {
  1087. if (SuperView == null) {
  1088. Application.Current.Remove (openMenu);
  1089. } else {
  1090. SuperView.Remove (openMenu);
  1091. }
  1092. openMenu.Dispose ();
  1093. }
  1094. for (int i = 0; i < index; i++)
  1095. pos += 1 + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + 2 : 0) + 2;
  1096. openMenu = new Menu (this, Frame.X + pos, Frame.Y + 1, Menus [index]);
  1097. openCurrentMenu = openMenu;
  1098. openCurrentMenu.previousSubFocused = openMenu;
  1099. if (SuperView == null) {
  1100. Application.Current.Add (openMenu);
  1101. } else {
  1102. SuperView.Add (openMenu);
  1103. }
  1104. openMenu.SetFocus ();
  1105. break;
  1106. default:
  1107. if (openSubMenu == null)
  1108. openSubMenu = new List<Menu> ();
  1109. if (sIndex > -1) {
  1110. RemoveSubMenu (sIndex);
  1111. } else {
  1112. var last = openSubMenu.Count > 0 ? openSubMenu.Last () : openMenu;
  1113. if (!UseSubMenusSingleFrame) {
  1114. openCurrentMenu = new Menu (this, last.Frame.Left + last.Frame.Width, last.Frame.Top + 1 + last.current, subMenu, last);
  1115. } else {
  1116. var first = openSubMenu.Count > 0 ? openSubMenu.First () : openMenu;
  1117. var mbi = new MenuItem [2 + subMenu.Children.Length];
  1118. mbi [0] = new MenuItem () { Title = subMenu.Title, Parent = subMenu };
  1119. mbi [1] = null;
  1120. for (int j = 0; j < subMenu.Children.Length; j++) {
  1121. mbi [j + 2] = subMenu.Children [j];
  1122. }
  1123. var newSubMenu = new MenuBarItem (mbi);
  1124. openCurrentMenu = new Menu (this, first.Frame.Left, first.Frame.Top, newSubMenu);
  1125. last.Visible = false;
  1126. Application.GrabMouse (openCurrentMenu);
  1127. }
  1128. openCurrentMenu.previousSubFocused = last.previousSubFocused;
  1129. openSubMenu.Add (openCurrentMenu);
  1130. if (SuperView == null) {
  1131. Application.Current.Add (openCurrentMenu);
  1132. } else {
  1133. SuperView.Add (openCurrentMenu);
  1134. }
  1135. }
  1136. selectedSub = openSubMenu.Count - 1;
  1137. if (selectedSub > -1 && SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1138. openCurrentMenu.SetFocus ();
  1139. }
  1140. break;
  1141. }
  1142. isMenuOpening = false;
  1143. IsMenuOpen = true;
  1144. }
  1145. /// <summary>
  1146. /// Opens the current Menu programatically.
  1147. /// </summary>
  1148. public void OpenMenu ()
  1149. {
  1150. var mbar = GetMouseGrabViewInstance (this);
  1151. if (mbar != null) {
  1152. mbar.CleanUp ();
  1153. }
  1154. if (openMenu != null)
  1155. return;
  1156. selected = 0;
  1157. SetNeedsDisplay ();
  1158. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1159. OpenMenu (selected);
  1160. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current) && !CloseMenu (false)) {
  1161. return;
  1162. }
  1163. if (!openCurrentMenu.CheckSubMenu ())
  1164. return;
  1165. Application.GrabMouse (this);
  1166. }
  1167. // Activates the menu, handles either first focus, or activating an entry when it was already active
  1168. // For mouse events.
  1169. internal void Activate (int idx, int sIdx = -1, MenuBarItem subMenu = null)
  1170. {
  1171. selected = idx;
  1172. selectedSub = sIdx;
  1173. if (openMenu == null)
  1174. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1175. OpenMenu (idx, sIdx, subMenu);
  1176. SetNeedsDisplay ();
  1177. }
  1178. internal bool SelectEnabledItem (IEnumerable<MenuItem> chldren, int current, out int newCurrent, bool forward = true)
  1179. {
  1180. if (chldren == null) {
  1181. newCurrent = -1;
  1182. return true;
  1183. }
  1184. IEnumerable<MenuItem> childrens;
  1185. if (forward) {
  1186. childrens = chldren;
  1187. } else {
  1188. childrens = chldren.Reverse ();
  1189. }
  1190. int count;
  1191. if (forward) {
  1192. count = -1;
  1193. } else {
  1194. count = childrens.Count ();
  1195. }
  1196. foreach (var child in childrens) {
  1197. if (forward) {
  1198. if (++count < current) {
  1199. continue;
  1200. }
  1201. } else {
  1202. if (--count > current) {
  1203. continue;
  1204. }
  1205. }
  1206. if (child == null || !child.IsEnabled ()) {
  1207. if (forward) {
  1208. current++;
  1209. } else {
  1210. current--;
  1211. }
  1212. } else {
  1213. newCurrent = current;
  1214. return true;
  1215. }
  1216. }
  1217. newCurrent = -1;
  1218. return false;
  1219. }
  1220. /// <summary>
  1221. /// Closes the current Menu programatically, if open and not canceled.
  1222. /// </summary>
  1223. public bool CloseMenu (bool ignoreUseSubMenusSingleFrame = false)
  1224. {
  1225. return CloseMenu (false, false, ignoreUseSubMenusSingleFrame);
  1226. }
  1227. bool reopen;
  1228. internal bool CloseMenu (bool reopen = false, bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1229. {
  1230. var mbi = isSubMenu ? openCurrentMenu.barItems : openMenu?.barItems;
  1231. if (UseSubMenusSingleFrame && mbi != null &&
  1232. !ignoreUseSubMenusSingleFrame && mbi.Parent != null) {
  1233. return false;
  1234. }
  1235. isMenuClosing = true;
  1236. this.reopen = reopen;
  1237. var args = OnMenuClosing (mbi, reopen, isSubMenu);
  1238. if (args.Cancel) {
  1239. isMenuClosing = false;
  1240. if (args.CurrentMenu.Parent != null)
  1241. openMenu.current = ((MenuBarItem)args.CurrentMenu.Parent).Children.IndexOf (args.CurrentMenu);
  1242. return false;
  1243. }
  1244. switch (isSubMenu) {
  1245. case false:
  1246. if (openMenu != null) {
  1247. if (SuperView == null) {
  1248. Application.Current.Remove (openMenu);
  1249. } else {
  1250. SuperView?.Remove (openMenu);
  1251. }
  1252. }
  1253. SetNeedsDisplay ();
  1254. if (previousFocused != null && previousFocused is Menu && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
  1255. previousFocused.SetFocus ();
  1256. openMenu?.Dispose ();
  1257. openMenu = null;
  1258. if (lastFocused is Menu || lastFocused is MenuBar) {
  1259. lastFocused = null;
  1260. }
  1261. LastFocused = lastFocused;
  1262. lastFocused = null;
  1263. if (LastFocused != null && LastFocused.CanFocus) {
  1264. if (!reopen) {
  1265. selected = -1;
  1266. }
  1267. LastFocused.SetFocus ();
  1268. } else {
  1269. SetFocus ();
  1270. PositionCursor ();
  1271. }
  1272. IsMenuOpen = false;
  1273. break;
  1274. case true:
  1275. selectedSub = -1;
  1276. SetNeedsDisplay ();
  1277. RemoveAllOpensSubMenus ();
  1278. openCurrentMenu.previousSubFocused.SetFocus ();
  1279. openSubMenu = null;
  1280. IsMenuOpen = true;
  1281. break;
  1282. }
  1283. this.reopen = false;
  1284. isMenuClosing = false;
  1285. return true;
  1286. }
  1287. void RemoveSubMenu (int index, bool ignoreUseSubMenusSingleFrame = false)
  1288. {
  1289. if (openSubMenu == null || (UseSubMenusSingleFrame
  1290. && !ignoreUseSubMenusSingleFrame && openSubMenu.Count == 0))
  1291. return;
  1292. for (int i = openSubMenu.Count - 1; i > index; i--) {
  1293. isMenuClosing = true;
  1294. Menu menu;
  1295. if (openSubMenu.Count - 1 > 0)
  1296. menu = openSubMenu [i - 1];
  1297. else
  1298. menu = openMenu;
  1299. if (!menu.Visible)
  1300. menu.Visible = true;
  1301. openCurrentMenu = menu;
  1302. openCurrentMenu.SetFocus ();
  1303. if (openSubMenu != null) {
  1304. menu = openSubMenu [i];
  1305. if (SuperView == null) {
  1306. Application.Current.Remove (menu);
  1307. } else {
  1308. SuperView.Remove (menu);
  1309. }
  1310. openSubMenu.Remove (menu);
  1311. menu.Dispose ();
  1312. }
  1313. RemoveSubMenu (i, ignoreUseSubMenusSingleFrame);
  1314. }
  1315. if (openSubMenu.Count > 0)
  1316. openCurrentMenu = openSubMenu.Last ();
  1317. //if (openMenu.Subviews.Count == 0)
  1318. // return;
  1319. //if (index == 0) {
  1320. // //SuperView.SetFocus (previousSubFocused);
  1321. // FocusPrev ();
  1322. // return;
  1323. //}
  1324. //for (int i = openMenu.Subviews.Count - 1; i > index; i--) {
  1325. // isMenuClosing = true;
  1326. // if (openMenu.Subviews.Count - 1 > 0)
  1327. // SuperView.SetFocus (openMenu.Subviews [i - 1]);
  1328. // else
  1329. // SuperView.SetFocus (openMenu);
  1330. // if (openMenu != null) {
  1331. // Remove (openMenu.Subviews [i]);
  1332. // openMenu.Remove (openMenu.Subviews [i]);
  1333. // }
  1334. // RemoveSubMenu (i);
  1335. //}
  1336. isMenuClosing = false;
  1337. }
  1338. internal void RemoveAllOpensSubMenus ()
  1339. {
  1340. if (openSubMenu != null) {
  1341. foreach (var item in openSubMenu) {
  1342. if (SuperView == null) {
  1343. Application.Current.Remove (item);
  1344. } else {
  1345. SuperView.Remove (item);
  1346. }
  1347. item.Dispose ();
  1348. }
  1349. }
  1350. }
  1351. internal void CloseAllMenus ()
  1352. {
  1353. if (!isMenuOpening && !isMenuClosing) {
  1354. if (openSubMenu != null && !CloseMenu (false, true))
  1355. return;
  1356. if (!CloseMenu (false))
  1357. return;
  1358. if (LastFocused != null && LastFocused != this)
  1359. selected = -1;
  1360. Application.UngrabMouse ();
  1361. }
  1362. IsMenuOpen = false;
  1363. openedByHotKey = false;
  1364. openedByAltKey = false;
  1365. OnMenuAllClosed ();
  1366. }
  1367. View FindDeepestMenu (View view, ref int count)
  1368. {
  1369. count = count > 0 ? count : 0;
  1370. foreach (var menu in view.Subviews) {
  1371. if (menu is Menu) {
  1372. count++;
  1373. return FindDeepestMenu ((Menu)menu, ref count);
  1374. }
  1375. }
  1376. return view;
  1377. }
  1378. internal void PreviousMenu (bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1379. {
  1380. switch (isSubMenu) {
  1381. case false:
  1382. if (selected <= 0)
  1383. selected = Menus.Length - 1;
  1384. else
  1385. selected--;
  1386. if (selected > -1 && !CloseMenu (true, false, ignoreUseSubMenusSingleFrame))
  1387. return;
  1388. OpenMenu (selected);
  1389. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current, false)) {
  1390. openCurrentMenu.current = 0;
  1391. }
  1392. break;
  1393. case true:
  1394. if (selectedSub > -1) {
  1395. selectedSub--;
  1396. RemoveSubMenu (selectedSub, ignoreUseSubMenusSingleFrame);
  1397. SetNeedsDisplay ();
  1398. } else
  1399. PreviousMenu ();
  1400. break;
  1401. }
  1402. }
  1403. internal void NextMenu (bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  1404. {
  1405. switch (isSubMenu) {
  1406. case false:
  1407. if (selected == -1)
  1408. selected = 0;
  1409. else if (selected + 1 == Menus.Length)
  1410. selected = 0;
  1411. else
  1412. selected++;
  1413. if (selected > -1 && !CloseMenu (true, ignoreUseSubMenusSingleFrame))
  1414. return;
  1415. OpenMenu (selected);
  1416. SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current);
  1417. break;
  1418. case true:
  1419. if (UseKeysUpDownAsKeysLeftRight) {
  1420. if (CloseMenu (false, true, ignoreUseSubMenusSingleFrame)) {
  1421. NextMenu (false, ignoreUseSubMenusSingleFrame);
  1422. }
  1423. } else {
  1424. var subMenu = openCurrentMenu.current > -1 && openCurrentMenu.barItems.Children.Length > 0
  1425. ? openCurrentMenu.barItems.SubMenu (openCurrentMenu.barItems.Children [openCurrentMenu.current])
  1426. : null;
  1427. if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && subMenu == null) {
  1428. if (openSubMenu != null && !CloseMenu (false, true))
  1429. return;
  1430. NextMenu (false, ignoreUseSubMenusSingleFrame);
  1431. } else if (subMenu != null || (openCurrentMenu.current > -1
  1432. && !openCurrentMenu.barItems.Children [openCurrentMenu.current].IsFromSubMenu)) {
  1433. selectedSub++;
  1434. openCurrentMenu.CheckSubMenu ();
  1435. } else {
  1436. if (CloseMenu (false, true, ignoreUseSubMenusSingleFrame)) {
  1437. NextMenu (false, ignoreUseSubMenusSingleFrame);
  1438. }
  1439. return;
  1440. }
  1441. SetNeedsDisplay ();
  1442. if (UseKeysUpDownAsKeysLeftRight)
  1443. openCurrentMenu.CheckSubMenu ();
  1444. }
  1445. break;
  1446. }
  1447. }
  1448. bool openedByHotKey;
  1449. internal bool FindAndOpenMenuByHotkey (KeyEvent kb)
  1450. {
  1451. //int pos = 0;
  1452. var c = ((uint)kb.Key & (uint)Key.CharMask);
  1453. for (int i = 0; i < Menus.Length; i++) {
  1454. // TODO: this code is duplicated, hotkey should be part of the MenuBarItem
  1455. var mi = Menus [i];
  1456. int p = mi.Title.IndexOf (MenuBar.HotKeySpecifier);
  1457. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1458. if (Char.ToUpperInvariant ((char)mi.Title [p + 1]) == c) {
  1459. ProcessMenu (i, mi);
  1460. return true;
  1461. }
  1462. }
  1463. }
  1464. return false;
  1465. }
  1466. internal bool FindAndOpenMenuByShortcut (KeyEvent kb, MenuItem [] children = null)
  1467. {
  1468. if (children == null) {
  1469. children = Menus;
  1470. }
  1471. var key = kb.KeyValue;
  1472. var keys = ShortcutHelper.GetModifiersKey (kb);
  1473. key |= (int)keys;
  1474. for (int i = 0; i < children.Length; i++) {
  1475. var mi = children [i];
  1476. if (mi == null) {
  1477. continue;
  1478. }
  1479. if ((!(mi is MenuBarItem mbiTopLevel) || mbiTopLevel.IsTopLevel) && mi.Shortcut != Key.Null && mi.Shortcut == (Key)key) {
  1480. var action = mi.Action;
  1481. if (action != null) {
  1482. Application.MainLoop.AddIdle (() => {
  1483. action ();
  1484. return false;
  1485. });
  1486. }
  1487. return true;
  1488. }
  1489. if (mi is MenuBarItem menuBarItem && !menuBarItem.IsTopLevel && FindAndOpenMenuByShortcut (kb, menuBarItem.Children)) {
  1490. return true;
  1491. }
  1492. }
  1493. return false;
  1494. }
  1495. private void ProcessMenu (int i, MenuBarItem mi)
  1496. {
  1497. if (selected < 0 && IsMenuOpen) {
  1498. return;
  1499. }
  1500. if (mi.IsTopLevel) {
  1501. var menu = new Menu (this, i, 0, mi);
  1502. menu.Run (mi.Action);
  1503. menu.Dispose ();
  1504. } else {
  1505. openedByHotKey = true;
  1506. Application.GrabMouse (this);
  1507. selected = i;
  1508. OpenMenu (i);
  1509. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current) && !CloseMenu (false)) {
  1510. return;
  1511. }
  1512. if (!openCurrentMenu.CheckSubMenu ())
  1513. return;
  1514. }
  1515. SetNeedsDisplay ();
  1516. }
  1517. ///<inheritdoc/>
  1518. public override bool ProcessHotKey (KeyEvent kb)
  1519. {
  1520. if (kb.Key == Key.F9) {
  1521. if (!IsMenuOpen)
  1522. OpenMenu ();
  1523. else
  1524. CloseAllMenus ();
  1525. return true;
  1526. }
  1527. // To ncurses simulate a AltMask key pressing Alt+Space because
  1528. // it can't detect an alone special key down was pressed.
  1529. if (kb.IsAlt && kb.Key == Key.AltMask && openMenu == null) {
  1530. OnKeyDown (kb);
  1531. OnKeyUp (kb);
  1532. return true;
  1533. } else if (kb.IsAlt && !kb.IsCtrl && !kb.IsShift) {
  1534. if (FindAndOpenMenuByHotkey (kb)) return true;
  1535. }
  1536. //var kc = kb.KeyValue;
  1537. return base.ProcessHotKey (kb);
  1538. }
  1539. ///<inheritdoc/>
  1540. public override bool ProcessKey (KeyEvent kb)
  1541. {
  1542. if (InvokeKeybindings (kb) == true)
  1543. return true;
  1544. var key = kb.KeyValue;
  1545. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  1546. char c = Char.ToUpper ((char)key);
  1547. if (selected == -1 || Menus [selected].IsTopLevel)
  1548. return false;
  1549. foreach (var mi in Menus [selected].Children) {
  1550. if (mi == null)
  1551. continue;
  1552. int p = mi.Title.IndexOf (MenuBar.HotKeySpecifier);
  1553. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1554. if (mi.Title [p + 1] == c) {
  1555. Selected (mi);
  1556. return true;
  1557. }
  1558. }
  1559. }
  1560. }
  1561. return false;
  1562. }
  1563. void CloseMenuBar ()
  1564. {
  1565. if (!CloseMenu (false))
  1566. return;
  1567. if (openedByAltKey) {
  1568. openedByAltKey = false;
  1569. LastFocused?.SetFocus ();
  1570. }
  1571. SetNeedsDisplay ();
  1572. }
  1573. void MoveRight ()
  1574. {
  1575. selected = (selected + 1) % Menus.Length;
  1576. OpenMenu (selected);
  1577. SetNeedsDisplay ();
  1578. }
  1579. void MoveLeft ()
  1580. {
  1581. selected--;
  1582. if (selected < 0)
  1583. selected = Menus.Length - 1;
  1584. OpenMenu (selected);
  1585. SetNeedsDisplay ();
  1586. }
  1587. ///<inheritdoc/>
  1588. public override bool ProcessColdKey (KeyEvent kb)
  1589. {
  1590. return FindAndOpenMenuByShortcut (kb);
  1591. }
  1592. ///<inheritdoc/>
  1593. public override bool MouseEvent (MouseEvent me)
  1594. {
  1595. if (!handled && !HandleGrabView (me, this)) {
  1596. return false;
  1597. }
  1598. handled = false;
  1599. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.Button1Clicked ||
  1600. (me.Flags == MouseFlags.ReportMousePosition && selected > -1) ||
  1601. (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && selected > -1)) {
  1602. int pos = 1;
  1603. int cx = me.X;
  1604. for (int i = 0; i < Menus.Length; i++) {
  1605. if (cx >= pos && cx < pos + 1 + Menus [i].TitleLength + Menus [i].Help.ConsoleWidth + 2) {
  1606. if (me.Flags == MouseFlags.Button1Clicked) {
  1607. if (Menus [i].IsTopLevel) {
  1608. var menu = new Menu (this, i, 0, Menus [i]);
  1609. menu.Run (Menus [i].Action);
  1610. menu.Dispose ();
  1611. } else if (!IsMenuOpen) {
  1612. Activate (i);
  1613. }
  1614. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked) {
  1615. if (IsMenuOpen && !Menus [i].IsTopLevel) {
  1616. CloseAllMenus ();
  1617. } else if (!Menus [i].IsTopLevel) {
  1618. Activate (i);
  1619. }
  1620. } else if (selected != i && selected > -1 && (me.Flags == MouseFlags.ReportMousePosition ||
  1621. me.Flags == MouseFlags.Button1Pressed && me.Flags == MouseFlags.ReportMousePosition)) {
  1622. if (IsMenuOpen) {
  1623. if (!CloseMenu (true, false)) {
  1624. return true;
  1625. }
  1626. Activate (i);
  1627. }
  1628. } else {
  1629. if (IsMenuOpen)
  1630. Activate (i);
  1631. }
  1632. return true;
  1633. }
  1634. pos += 1 + Menus [i].TitleLength + 2;
  1635. }
  1636. }
  1637. return false;
  1638. }
  1639. internal bool handled;
  1640. internal bool isContextMenuLoading;
  1641. internal bool HandleGrabView (MouseEvent me, View current)
  1642. {
  1643. if (Application.MouseGrabView != null) {
  1644. if (me.View is MenuBar || me.View is Menu) {
  1645. var mbar = GetMouseGrabViewInstance (me.View);
  1646. if (mbar != null) {
  1647. if (me.Flags == MouseFlags.Button1Clicked) {
  1648. mbar.CleanUp ();
  1649. Application.GrabMouse (me.View);
  1650. } else {
  1651. handled = false;
  1652. return false;
  1653. }
  1654. }
  1655. if (me.View != current) {
  1656. Application.UngrabMouse ();
  1657. var v = me.View;
  1658. Application.GrabMouse (v);
  1659. MouseEvent nme;
  1660. if (me.Y > -1) {
  1661. var newxy = v.ScreenToView (me.X, me.Y);
  1662. nme = new MouseEvent () {
  1663. X = newxy.X,
  1664. Y = newxy.Y,
  1665. Flags = me.Flags,
  1666. OfX = me.X - newxy.X,
  1667. OfY = me.Y - newxy.Y,
  1668. View = v
  1669. };
  1670. } else {
  1671. nme = new MouseEvent () {
  1672. X = me.X + current.Frame.X,
  1673. Y = 0,
  1674. Flags = me.Flags,
  1675. View = v
  1676. };
  1677. }
  1678. v.MouseEvent (nme);
  1679. return false;
  1680. }
  1681. } else if (!isContextMenuLoading && !(me.View is MenuBar || me.View is Menu)
  1682. && me.Flags != MouseFlags.ReportMousePosition && me.Flags != 0) {
  1683. Application.UngrabMouse ();
  1684. if (IsMenuOpen)
  1685. CloseAllMenus ();
  1686. handled = false;
  1687. return false;
  1688. } else {
  1689. handled = false;
  1690. isContextMenuLoading = false;
  1691. return false;
  1692. }
  1693. } else if (!IsMenuOpen && (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked
  1694. || me.Flags == MouseFlags.Button1TripleClicked || me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  1695. Application.GrabMouse (current);
  1696. } else if (IsMenuOpen && (me.View is MenuBar || me.View is Menu)) {
  1697. Application.GrabMouse (me.View);
  1698. } else {
  1699. handled = false;
  1700. return false;
  1701. }
  1702. //if (me.View != this && me.Flags != MouseFlags.Button1Pressed)
  1703. // return true;
  1704. //else if (me.View != this && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1705. // Application.UngrabMouse ();
  1706. // host.CloseAllMenus ();
  1707. // return true;
  1708. //}
  1709. //if (!(me.View is MenuBar) && !(me.View is Menu) && me.Flags != MouseFlags.Button1Pressed))
  1710. // return false;
  1711. //if (Application.MouseGrabView != null) {
  1712. // if (me.View is MenuBar || me.View is Menu) {
  1713. // me.X -= me.OfX;
  1714. // me.Y -= me.OfY;
  1715. // me.View.MouseEvent (me);
  1716. // return true;
  1717. // } else if (!(me.View is MenuBar || me.View is Menu) && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1718. // Application.UngrabMouse ();
  1719. // CloseAllMenus ();
  1720. // }
  1721. //} else if (!isMenuClosed && selected == -1 && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1722. // Application.GrabMouse (this);
  1723. // return true;
  1724. //}
  1725. //if (Application.MouseGrabView != null) {
  1726. // if (Application.MouseGrabView == me.View && me.View == current) {
  1727. // me.X -= me.OfX;
  1728. // me.Y -= me.OfY;
  1729. // } else if (me.View != current && me.View is MenuBar && me.View is Menu) {
  1730. // Application.UngrabMouse ();
  1731. // Application.GrabMouse (me.View);
  1732. // } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1733. // Application.UngrabMouse ();
  1734. // CloseMenu ();
  1735. // }
  1736. //} else if ((!isMenuClosed && selected > -1)) {
  1737. // Application.GrabMouse (current);
  1738. //}
  1739. handled = true;
  1740. return true;
  1741. }
  1742. MenuBar GetMouseGrabViewInstance (View view)
  1743. {
  1744. if (view == null || Application.MouseGrabView == null) {
  1745. return null;
  1746. }
  1747. MenuBar hostView = null;
  1748. if (view is MenuBar) {
  1749. hostView = (MenuBar)view;
  1750. } else if (view is Menu) {
  1751. hostView = ((Menu)view).host;
  1752. }
  1753. var grabView = Application.MouseGrabView;
  1754. MenuBar hostGrabView = null;
  1755. if (grabView is MenuBar) {
  1756. hostGrabView = (MenuBar)grabView;
  1757. } else if (grabView is Menu) {
  1758. hostGrabView = ((Menu)grabView).host;
  1759. }
  1760. return hostView != hostGrabView ? hostGrabView : null;
  1761. }
  1762. ///<inheritdoc/>
  1763. public override bool OnEnter (View view)
  1764. {
  1765. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  1766. return base.OnEnter (view);
  1767. }
  1768. }
  1769. /// <summary>
  1770. /// An <see cref="EventArgs"/> which allows passing a cancelable menu opening event or replacing with a new <see cref="MenuBarItem"/>.
  1771. /// </summary>
  1772. public class MenuOpeningEventArgs : EventArgs {
  1773. /// <summary>
  1774. /// The current <see cref="MenuBarItem"/> parent.
  1775. /// </summary>
  1776. public MenuBarItem CurrentMenu { get; }
  1777. /// <summary>
  1778. /// The new <see cref="MenuBarItem"/> to be replaced.
  1779. /// </summary>
  1780. public MenuBarItem NewMenuBarItem { get; set; }
  1781. /// <summary>
  1782. /// Flag that allows you to cancel the opening of the menu.
  1783. /// </summary>
  1784. public bool Cancel { get; set; }
  1785. /// <summary>
  1786. /// Initializes a new instance of <see cref="MenuOpeningEventArgs"/>
  1787. /// </summary>
  1788. /// <param name="currentMenu">The current <see cref="MenuBarItem"/> parent.</param>
  1789. public MenuOpeningEventArgs (MenuBarItem currentMenu)
  1790. {
  1791. CurrentMenu = currentMenu;
  1792. }
  1793. }
  1794. /// <summary>
  1795. /// An <see cref="EventArgs"/> which allows passing a cancelable menu closing event.
  1796. /// </summary>
  1797. public class MenuClosingEventArgs : EventArgs {
  1798. /// <summary>
  1799. /// The current <see cref="MenuBarItem"/> parent.
  1800. /// </summary>
  1801. public MenuBarItem CurrentMenu { get; }
  1802. /// <summary>
  1803. /// Indicates whether the current menu will be reopen.
  1804. /// </summary>
  1805. public bool Reopen { get; }
  1806. /// <summary>
  1807. /// Indicates whether the current menu is a sub-menu.
  1808. /// </summary>
  1809. public bool IsSubMenu { get; }
  1810. /// <summary>
  1811. /// Flag that allows you to cancel the opening of the menu.
  1812. /// </summary>
  1813. public bool Cancel { get; set; }
  1814. /// <summary>
  1815. /// Initializes a new instance of <see cref="MenuClosingEventArgs"/>
  1816. /// </summary>
  1817. /// <param name="currentMenu">The current <see cref="MenuBarItem"/> parent.</param>
  1818. /// <param name="reopen">Whether the current menu will be reopen.</param>
  1819. /// <param name="isSubMenu">Indicates whether it is a sub-menu.</param>
  1820. public MenuClosingEventArgs (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  1821. {
  1822. CurrentMenu = currentMenu;
  1823. Reopen = reopen;
  1824. IsSubMenu = isSubMenu;
  1825. }
  1826. }
  1827. }