Menu.cs 10 KB

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