Menu.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. // Add mouse support
  10. // Allow menus inside menus
  11. using System;
  12. namespace Terminal {
  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.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 (string 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 char 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 string 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 string 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 (string title, MenuItem [] children)
  75. {
  76. SetTitle (title ?? "");
  77. Children = children;
  78. }
  79. void SetTitle (string 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. public string Title { get; set; }
  93. public MenuItem [] Children { get; set; }
  94. internal int TitleLength { get; private set; }
  95. }
  96. class Menu : View {
  97. MenuBarItem barItems;
  98. MenuBar host;
  99. int current;
  100. static Rect MakeFrame (int x, int y, MenuItem [] items)
  101. {
  102. int maxW = 0;
  103. foreach (var item in items) {
  104. var l = item.Width;
  105. maxW = Math.Max (l, maxW);
  106. }
  107. return new Rect (x, y, maxW + 2, items.Length + 2);
  108. }
  109. public Menu (MenuBar host, int x, int y, MenuBarItem barItems) : base (MakeFrame (x, y, barItems.Children))
  110. {
  111. this.barItems = barItems;
  112. this.host = host;
  113. CanFocus = true;
  114. }
  115. public override void Redraw (Rect region)
  116. {
  117. Driver.SetAttribute (Colors.Menu.Normal);
  118. DrawFrame (region, true);
  119. for (int i = 0; i < barItems.Children.Length; i++){
  120. var item = barItems.Children [i];
  121. Move (1, i+1);
  122. Driver.SetAttribute (item == null ? Colors.Base.Focus : i == current ? Colors.Menu.Focus : Colors.Menu.Normal);
  123. for (int p = 0; p < Frame.Width-2; p++)
  124. if (item == null)
  125. Driver.AddSpecial (SpecialChar.HLine);
  126. else
  127. Driver.AddCh (' ');
  128. if (item == null)
  129. continue;
  130. Move (2, i + 1);
  131. DrawHotString (item.Title,
  132. i == current? Colors.Menu.HotFocus : Colors.Menu.HotNormal,
  133. i == current ? Colors.Menu.Focus : Colors.Menu.Normal);
  134. // The help string
  135. var l = item.Help.Length;
  136. Move (Frame.Width - l - 2, 1 + i);
  137. Driver.AddStr (item.Help);
  138. }
  139. }
  140. public override void PositionCursor ()
  141. {
  142. Move (2, 1 + current);
  143. }
  144. void Run (Action action)
  145. {
  146. if (action == null)
  147. return;
  148. Application.MainLoop.AddIdle (() => {
  149. action ();
  150. return false;
  151. });
  152. }
  153. public override bool ProcessKey (KeyEvent kb)
  154. {
  155. switch (kb.Key) {
  156. case Key.CursorUp:
  157. current--;
  158. if (current < 0)
  159. current = barItems.Children.Length - 1;
  160. SetNeedsDisplay ();
  161. break;
  162. case Key.CursorDown:
  163. current++;
  164. if (current== barItems.Children.Length)
  165. current = 0;
  166. SetNeedsDisplay ();
  167. break;
  168. case Key.CursorLeft:
  169. host.PreviousMenu ();
  170. break;
  171. case Key.CursorRight:
  172. host.NextMenu ();
  173. break;
  174. case Key.Esc:
  175. host.CloseMenu ();
  176. break;
  177. case Key.Enter:
  178. host.CloseMenu ();
  179. Run (barItems.Children [current].Action);
  180. break;
  181. default:
  182. // TODO: rune-ify
  183. if (Char.IsLetterOrDigit ((char)kb.KeyValue)) {
  184. var x = Char.ToUpper ((char)kb.KeyValue);
  185. foreach (var item in barItems.Children) {
  186. if (item.HotKey == x) {
  187. host.CloseMenu ();
  188. Run (item.Action);
  189. return true;
  190. }
  191. }
  192. }
  193. break;
  194. }
  195. return base.ProcessKey (kb);
  196. }
  197. public override bool MouseEvent(MouseEvent me)
  198. {
  199. if (me.Flags == MouseFlags.Button1Clicked || me.Flags == MouseFlags.Button1Released) {
  200. if (me.Y < 1)
  201. return true;
  202. var item = me.Y - 1;
  203. if (item >= barItems.Children.Length)
  204. return true;
  205. host.CloseMenu ();
  206. Run (barItems.Children [item].Action);
  207. return true;
  208. }
  209. if (me.Flags == MouseFlags.Button1Pressed) {
  210. if (me.Y < 1)
  211. return true;
  212. if (me.Y - 1 >= barItems.Children.Length)
  213. return true;
  214. current = me.Y - 1;
  215. SetNeedsDisplay ();
  216. return true;
  217. }
  218. return false;
  219. }
  220. }
  221. /// <summary>
  222. /// A menu bar for your application.
  223. /// </summary>
  224. public class MenuBar : View {
  225. /// <summary>
  226. /// The menus that were defined when the menubar was created. This can be updated if the menu is not currently visible.
  227. /// </summary>
  228. /// <value>The menu array.</value>
  229. public MenuBarItem [] Menus { get; set; }
  230. int selected;
  231. Action action;
  232. /// <summary>
  233. /// Initializes a new instance of the <see cref="T:Terminal.MenuBar"/> class with the specified set of toplevel menu items.
  234. /// </summary>
  235. /// <param name="menus">Menus.</param>
  236. public MenuBar (MenuBarItem [] menus) : base (new Rect (0, 0, Application.Driver.Cols, 1))
  237. {
  238. Menus = menus;
  239. CanFocus = false;
  240. selected = -1;
  241. }
  242. public override void Redraw (Rect region)
  243. {
  244. Move (0, 0);
  245. Driver.SetAttribute (Colors.Base.Focus);
  246. for (int i = 0; i < Frame.Width; i++)
  247. Driver.AddCh (' ');
  248. Move (1, 0);
  249. int pos = 1;
  250. for (int i = 0; i < Menus.Length; i++) {
  251. var menu = Menus [i];
  252. Move (pos, 0);
  253. Attribute hotColor, normalColor;
  254. if (i == selected){
  255. hotColor = i == selected ? Colors.Menu.HotFocus : Colors.Menu.HotNormal;
  256. normalColor = i == selected ? Colors.Menu.Focus : Colors.Menu.Normal;
  257. } else {
  258. hotColor = Colors.Base.Focus;
  259. normalColor = Colors.Base.Focus;
  260. }
  261. DrawHotString (" " + menu.Title + " " + " ", hotColor, normalColor);
  262. pos += menu.TitleLength+ 3;
  263. }
  264. PositionCursor ();
  265. }
  266. public override void PositionCursor ()
  267. {
  268. int pos = 0;
  269. for (int i = 0; i < Menus.Length; i++) {
  270. if (i == selected) {
  271. pos++;
  272. Move (pos, 0);
  273. return;
  274. } else {
  275. pos += Menus [i].TitleLength + 4;
  276. }
  277. }
  278. Move (0, 0);
  279. }
  280. void Selected (MenuItem item)
  281. {
  282. // TODO: Running = false;
  283. action = item.Action;
  284. }
  285. Menu openMenu;
  286. View previousFocused;
  287. void OpenMenu (int index)
  288. {
  289. if (openMenu != null)
  290. SuperView.Remove (openMenu);
  291. int pos = 0;
  292. for (int i = 0; i < index; i++)
  293. pos += Menus [i].Title.Length + 3;
  294. openMenu = new Menu (this, pos, 1, Menus [index]);
  295. SuperView.Add (openMenu);
  296. SuperView.SetFocus (openMenu);
  297. }
  298. // Starts the menu from a hotkey
  299. void StartMenu ()
  300. {
  301. if (openMenu != null)
  302. return;
  303. selected = 0;
  304. SetNeedsDisplay ();
  305. previousFocused = SuperView.Focused;
  306. OpenMenu (selected);
  307. }
  308. // Activates the menu, handles either first focus, or activating an entry when it was already active
  309. // For mouse events.
  310. void Activate (int idx)
  311. {
  312. selected = idx;
  313. if (openMenu == null)
  314. previousFocused = SuperView.Focused;
  315. OpenMenu (idx);
  316. SetNeedsDisplay ();
  317. }
  318. internal void CloseMenu ()
  319. {
  320. selected = -1;
  321. SetNeedsDisplay ();
  322. SuperView.Remove (openMenu);
  323. previousFocused.SuperView.SetFocus (previousFocused);
  324. openMenu = null;
  325. }
  326. internal void PreviousMenu ()
  327. {
  328. if (selected <= 0)
  329. selected = Menus.Length - 1;
  330. else
  331. selected--;
  332. OpenMenu (selected);
  333. }
  334. internal void NextMenu ()
  335. {
  336. if (selected == -1)
  337. selected = 0;
  338. else if (selected + 1 == Menus.Length)
  339. selected = 0;
  340. else
  341. selected++;
  342. OpenMenu (selected);
  343. }
  344. public override bool ProcessHotKey (KeyEvent kb)
  345. {
  346. if (kb.Key == Key.F9) {
  347. StartMenu ();
  348. return true;
  349. }
  350. var kc = kb.KeyValue;
  351. return base.ProcessHotKey (kb);
  352. }
  353. public override bool ProcessKey (KeyEvent kb)
  354. {
  355. switch (kb.Key) {
  356. case Key.CursorLeft:
  357. selected--;
  358. if (selected < 0)
  359. selected = Menus.Length - 1;
  360. break;
  361. case Key.CursorRight:
  362. selected = (selected + 1) % Menus.Length;
  363. break;
  364. case Key.Esc:
  365. case Key.ControlC:
  366. //TODO: Running = false;
  367. break;
  368. default:
  369. var key = kb.KeyValue;
  370. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  371. char c = Char.ToUpper ((char)key);
  372. if (Menus [selected].Children == null)
  373. return false;
  374. foreach (var mi in Menus [selected].Children) {
  375. int p = mi.Title.IndexOf ('_');
  376. if (p != -1 && p + 1 < mi.Title.Length) {
  377. if (mi.Title [p + 1] == c) {
  378. Selected (mi);
  379. return true;
  380. }
  381. }
  382. }
  383. }
  384. return false;
  385. }
  386. SetNeedsDisplay ();
  387. return true;
  388. }
  389. public override bool MouseEvent(MouseEvent me)
  390. {
  391. if (me.Flags == MouseFlags.Button1Clicked) {
  392. int pos = 1;
  393. int cx = me.X;
  394. for (int i = 0; i < Menus.Length; i++) {
  395. if (cx > pos && me.X < pos + 1 + Menus [i].TitleLength) {
  396. Activate (i);
  397. return true;
  398. }
  399. pos += 2 + Menus [i].TitleLength + 1;
  400. }
  401. }
  402. return false;
  403. }
  404. }
  405. }