Menu.cs 10 KB

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