Menu.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. namespace Terminal.Gui {
  13. /// <summary>
  14. /// A menu item has a title, an associated help text, and an action to execute on activation.
  15. /// </summary>
  16. public class MenuItem {
  17. /// <summary>
  18. /// Initializes a new <see cref="T:Terminal.Gui.MenuItem"/>.
  19. /// </summary>
  20. /// <param name="title">Title for the menu item.</param>
  21. /// <param name="help">Help text to display.</param>
  22. /// <param name="action">Action to invoke when the menu item is activated.</param>
  23. public MenuItem (ustring title, string help, Action action)
  24. {
  25. Title = title ?? "";
  26. Help = help ?? "";
  27. Action = action;
  28. bool nextIsHot = false;
  29. foreach (var x in Title) {
  30. if (x == '_')
  31. nextIsHot = true;
  32. else {
  33. if (nextIsHot) {
  34. HotKey = x;
  35. break;
  36. }
  37. nextIsHot = false;
  38. }
  39. }
  40. }
  41. //
  42. //
  43. /// <summary>
  44. /// The hotkey is used when the menu is active, the shortcut can be triggered when the menu is not active.
  45. /// For example HotKey would be "N" when the File Menu is open (assuming there is a "_New" entry
  46. /// if the ShortCut is set to "Control-N", this would be a global hotkey that would trigger as well
  47. /// </summary>
  48. public Rune HotKey;
  49. /// <summary>
  50. /// This is the global setting that can be used as a global shortcut to invoke the action on the menu.
  51. /// </summary>
  52. public Key ShortCut;
  53. /// <summary>
  54. /// Gets or sets the title.
  55. /// </summary>
  56. /// <value>The title.</value>
  57. public ustring Title { get; set; }
  58. /// <summary>
  59. /// Gets or sets the help text for the menu item.
  60. /// </summary>
  61. /// <value>The help text.</value>
  62. public ustring Help { get; set; }
  63. /// <summary>
  64. /// Gets or sets the action to be invoked when the menu is triggered
  65. /// </summary>
  66. /// <value>Method to invoke.</value>
  67. public Action Action { get; set; }
  68. internal int Width => Title.Length + Help.Length + 1 + 2;
  69. }
  70. /// <summary>
  71. /// A menu bar item contains other menu items.
  72. /// </summary>
  73. public class MenuBarItem {
  74. public MenuBarItem (ustring title, MenuItem [] children)
  75. {
  76. SetTitle (title ?? "");
  77. Children = children;
  78. }
  79. void SetTitle (ustring title)
  80. {
  81. if (title == null)
  82. title = "";
  83. Title = title;
  84. int len = 0;
  85. foreach (var ch in Title) {
  86. if (ch == '_')
  87. continue;
  88. len++;
  89. }
  90. TitleLength = len;
  91. }
  92. /// <summary>
  93. /// Gets or sets the title to display.
  94. /// </summary>
  95. /// <value>The title.</value>
  96. public ustring Title { get; set; }
  97. /// <summary>
  98. /// Gets or sets the children for this MenuBarItem
  99. /// </summary>
  100. /// <value>The children.</value>
  101. public MenuItem [] Children { get; set; }
  102. internal int TitleLength { get; private set; }
  103. }
  104. class Menu : View {
  105. MenuBarItem barItems;
  106. MenuBar host;
  107. int current;
  108. static Rect MakeFrame (int x, int y, MenuItem [] items)
  109. {
  110. int maxW = 0;
  111. foreach (var item in items) {
  112. var l = item.Width;
  113. maxW = Math.Max (l, maxW);
  114. }
  115. return new Rect (x, y, maxW + 2, items.Length + 2);
  116. }
  117. public Menu (MenuBar host, int x, int y, MenuBarItem barItems) : base (MakeFrame (x, y, barItems.Children))
  118. {
  119. this.barItems = barItems;
  120. this.host = host;
  121. ColorScheme = Colors.Menu;
  122. CanFocus = true;
  123. }
  124. public override void Redraw (Rect region)
  125. {
  126. Driver.SetAttribute (ColorScheme.Normal);
  127. DrawFrame (region, padding: 0, fill: true);
  128. for (int i = 0; i < barItems.Children.Length; i++){
  129. var item = barItems.Children [i];
  130. Move (1, i+1);
  131. Driver.SetAttribute (item == null ? Colors.Base.Focus : i == current ? ColorScheme.Focus : ColorScheme.Normal);
  132. for (int p = 0; p < Frame.Width-2; p++)
  133. if (item == null)
  134. Driver.AddRune (Driver.HLine);
  135. else
  136. Driver.AddRune (' ');
  137. if (item == null)
  138. continue;
  139. Move (2, i + 1);
  140. DrawHotString (item.Title,
  141. i == current? ColorScheme.HotFocus : ColorScheme.HotNormal,
  142. i == current ? ColorScheme.Focus : ColorScheme.Normal);
  143. // The help string
  144. var l = item.Help.Length;
  145. Move (Frame.Width - l - 2, 1 + i);
  146. Driver.AddStr (item.Help);
  147. }
  148. }
  149. public override void PositionCursor ()
  150. {
  151. Move (2, 1 + current);
  152. }
  153. void Run (Action action)
  154. {
  155. if (action == null)
  156. return;
  157. Application.MainLoop.AddIdle (() => {
  158. action ();
  159. return false;
  160. });
  161. }
  162. public override bool ProcessKey (KeyEvent kb)
  163. {
  164. switch (kb.Key) {
  165. case Key.CursorUp:
  166. current--;
  167. if (current < 0)
  168. current = barItems.Children.Length - 1;
  169. SetNeedsDisplay ();
  170. break;
  171. case Key.CursorDown:
  172. current++;
  173. if (current== barItems.Children.Length)
  174. current = 0;
  175. SetNeedsDisplay ();
  176. break;
  177. case Key.CursorLeft:
  178. host.PreviousMenu ();
  179. break;
  180. case Key.CursorRight:
  181. host.NextMenu ();
  182. break;
  183. case Key.Esc:
  184. host.CloseMenu ();
  185. break;
  186. case Key.Enter:
  187. host.CloseMenu ();
  188. Run (barItems.Children [current].Action);
  189. break;
  190. default:
  191. // TODO: rune-ify
  192. if (Char.IsLetterOrDigit ((char)kb.KeyValue)) {
  193. var x = Char.ToUpper ((char)kb.KeyValue);
  194. foreach (var item in barItems.Children) {
  195. if (item.HotKey == x) {
  196. host.CloseMenu ();
  197. Run (item.Action);
  198. return true;
  199. }
  200. }
  201. }
  202. break;
  203. }
  204. return true;
  205. }
  206. public override bool MouseEvent(MouseEvent me)
  207. {
  208. if (me.Flags == MouseFlags.Button1Clicked || me.Flags == MouseFlags.Button1Released) {
  209. if (me.Y < 1)
  210. return true;
  211. var item = me.Y - 1;
  212. if (item >= barItems.Children.Length)
  213. return true;
  214. host.CloseMenu ();
  215. Run (barItems.Children [item].Action);
  216. return true;
  217. }
  218. if (me.Flags == MouseFlags.Button1Pressed) {
  219. if (me.Y < 1)
  220. return true;
  221. if (me.Y - 1 >= barItems.Children.Length)
  222. return true;
  223. current = me.Y - 1;
  224. SetNeedsDisplay ();
  225. return true;
  226. }
  227. return false;
  228. }
  229. }
  230. /// <summary>
  231. /// A menu bar for your application.
  232. /// </summary>
  233. public class MenuBar : View {
  234. /// <summary>
  235. /// The menus that were defined when the menubar was created. This can be updated if the menu is not currently visible.
  236. /// </summary>
  237. /// <value>The menu array.</value>
  238. public MenuBarItem [] Menus { get; set; }
  239. int selected;
  240. Action action;
  241. /// <summary>
  242. /// Initializes a new instance of the <see cref="T:Terminal.Gui.MenuBar"/> class with the specified set of toplevel menu items.
  243. /// </summary>
  244. /// <param name="menus">Menus.</param>
  245. public MenuBar (MenuBarItem [] menus) : base ()
  246. {
  247. X = 0;
  248. Y = 0;
  249. Width = Dim.Fill ();
  250. Height = 1;
  251. Menus = menus;
  252. CanFocus = false;
  253. selected = -1;
  254. ColorScheme = Colors.Menu;
  255. }
  256. public override void Redraw (Rect region)
  257. {
  258. Move (0, 0);
  259. Driver.SetAttribute (Colors.Base.Focus);
  260. for (int i = 0; i < Frame.Width; i++)
  261. Driver.AddRune (' ');
  262. Move (1, 0);
  263. int pos = 1;
  264. for (int i = 0; i < Menus.Length; i++) {
  265. var menu = Menus [i];
  266. Move (pos, 0);
  267. Attribute hotColor, normalColor;
  268. if (i == selected){
  269. hotColor = i == selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  270. normalColor = i == selected ? ColorScheme.Focus : ColorScheme.Normal;
  271. } else {
  272. hotColor = Colors.Base.Focus;
  273. normalColor = Colors.Base.Focus;
  274. }
  275. DrawHotString (" " + menu.Title + " " + " ", hotColor, normalColor);
  276. pos += menu.TitleLength+ 3;
  277. }
  278. PositionCursor ();
  279. }
  280. public override void PositionCursor ()
  281. {
  282. int pos = 0;
  283. for (int i = 0; i < Menus.Length; i++) {
  284. if (i == selected) {
  285. pos++;
  286. Move (pos, 0);
  287. return;
  288. } else {
  289. pos += Menus [i].TitleLength + 4;
  290. }
  291. }
  292. Move (0, 0);
  293. }
  294. void Selected (MenuItem item)
  295. {
  296. // TODO: Running = false;
  297. action = item.Action;
  298. }
  299. Menu openMenu;
  300. View previousFocused;
  301. void OpenMenu (int index)
  302. {
  303. if (openMenu != null)
  304. SuperView.Remove (openMenu);
  305. int pos = 0;
  306. for (int i = 0; i < index; i++)
  307. pos += Menus [i].Title.Length + 3;
  308. openMenu = new Menu (this, pos, 1, Menus [index]);
  309. SuperView.Add (openMenu);
  310. SuperView.SetFocus (openMenu);
  311. }
  312. // Starts the menu from a hotkey
  313. void StartMenu ()
  314. {
  315. if (openMenu != null)
  316. return;
  317. selected = 0;
  318. SetNeedsDisplay ();
  319. previousFocused = SuperView.Focused;
  320. OpenMenu (selected);
  321. }
  322. // Activates the menu, handles either first focus, or activating an entry when it was already active
  323. // For mouse events.
  324. void Activate (int idx)
  325. {
  326. selected = idx;
  327. if (openMenu == null)
  328. previousFocused = SuperView.Focused;
  329. OpenMenu (idx);
  330. SetNeedsDisplay ();
  331. }
  332. internal void CloseMenu ()
  333. {
  334. selected = -1;
  335. SetNeedsDisplay ();
  336. SuperView.Remove (openMenu);
  337. previousFocused.SuperView.SetFocus (previousFocused);
  338. openMenu = null;
  339. }
  340. internal void PreviousMenu ()
  341. {
  342. if (selected <= 0)
  343. selected = Menus.Length - 1;
  344. else
  345. selected--;
  346. OpenMenu (selected);
  347. }
  348. internal void NextMenu ()
  349. {
  350. if (selected == -1)
  351. selected = 0;
  352. else if (selected + 1 == Menus.Length)
  353. selected = 0;
  354. else
  355. selected++;
  356. OpenMenu (selected);
  357. }
  358. public override bool ProcessHotKey (KeyEvent kb)
  359. {
  360. if (kb.Key == Key.F9) {
  361. StartMenu ();
  362. return true;
  363. }
  364. var kc = kb.KeyValue;
  365. return base.ProcessHotKey (kb);
  366. }
  367. public override bool ProcessKey (KeyEvent kb)
  368. {
  369. switch (kb.Key) {
  370. case Key.CursorLeft:
  371. selected--;
  372. if (selected < 0)
  373. selected = Menus.Length - 1;
  374. break;
  375. case Key.CursorRight:
  376. selected = (selected + 1) % Menus.Length;
  377. break;
  378. case Key.Esc:
  379. case Key.ControlC:
  380. //TODO: Running = false;
  381. break;
  382. default:
  383. var key = kb.KeyValue;
  384. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  385. char c = Char.ToUpper ((char)key);
  386. if (Menus [selected].Children == null)
  387. return false;
  388. foreach (var mi in Menus [selected].Children) {
  389. int p = mi.Title.IndexOf ('_');
  390. if (p != -1 && p + 1 < mi.Title.Length) {
  391. if (mi.Title [p + 1] == c) {
  392. Selected (mi);
  393. return true;
  394. }
  395. }
  396. }
  397. }
  398. return false;
  399. }
  400. SetNeedsDisplay ();
  401. return true;
  402. }
  403. public override bool MouseEvent(MouseEvent me)
  404. {
  405. if (me.Flags == MouseFlags.Button1Clicked) {
  406. int pos = 1;
  407. int cx = me.X;
  408. for (int i = 0; i < Menus.Length; i++) {
  409. if (cx > pos && me.X < pos + 1 + Menus [i].TitleLength) {
  410. Activate (i);
  411. return true;
  412. }
  413. pos += 2 + Menus [i].TitleLength + 1;
  414. }
  415. }
  416. return false;
  417. }
  418. }
  419. }