Buttons.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Text;
  5. using JetBrains.Annotations;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("Buttons", "Demonstrates all sorts of Buttons.")]
  9. [ScenarioCategory ("Controls")]
  10. [ScenarioCategory ("Layout")]
  11. public class Buttons : Scenario
  12. {
  13. public override void Main ()
  14. {
  15. Application.Init ();
  16. Window main = new ()
  17. {
  18. Title = GetQuitKeyAndName ()
  19. };
  20. // Add a label & text field so we can demo IsDefault
  21. var editLabel = new Label { X = 0, Y = 0, Text = "TextField (to demo IsDefault):" };
  22. main.Add (editLabel);
  23. // Add a TextField using Absolute layout.
  24. var edit = new TextField { X = 31, Width = 15, HotKey = Key.Y.WithAlt };
  25. main.Add (edit);
  26. // This is the default button (IsDefault = true); if user presses ENTER in the TextField
  27. // the scenario will quit
  28. var defaultButton = new Button { X = Pos.Center (), Y = Pos.AnchorEnd (), IsDefault = true, Text = "_Quit" };
  29. main.Add (defaultButton);
  30. // Note we handle Accept on main, not defaultButton
  31. main.Accepting += (s, e) => Application.RequestStop ();
  32. var swapButton = new Button
  33. {
  34. X = 50,
  35. Width = 45,
  36. Height = 3,
  37. Text = "S_wap Default (Size = 45, 3)",
  38. ColorScheme = Colors.ColorSchemes ["Error"]
  39. };
  40. swapButton.Accepting += (s, e) =>
  41. {
  42. e.Cancel = !swapButton.IsDefault;
  43. defaultButton.IsDefault = !defaultButton.IsDefault;
  44. swapButton.IsDefault = !swapButton.IsDefault;
  45. };
  46. defaultButton.Accepting += (s, e) =>
  47. {
  48. e.Cancel = !defaultButton.IsDefault;
  49. if (e.Cancel)
  50. {
  51. MessageBox.ErrorQuery ("Error", "This button is no longer the Quit button; the Swap Default button is.", "_Ok");
  52. }
  53. };
  54. main.Add (swapButton);
  55. static void DoMessage (Button button, string txt)
  56. {
  57. button.Accepting += (s, e) =>
  58. {
  59. string btnText = button.Text;
  60. MessageBox.Query ("Message", $"Did you click {txt}?", "Yes", "No");
  61. e.Cancel = true;
  62. };
  63. }
  64. var colorButtonsLabel = new Label { X = 0, Y = Pos.Bottom (swapButton) + 1, Text = "Color Buttons: " };
  65. main.Add (colorButtonsLabel);
  66. View prev = colorButtonsLabel;
  67. foreach (KeyValuePair<string, ColorScheme> colorScheme in Colors.ColorSchemes)
  68. {
  69. var colorButton = new Button
  70. {
  71. X = Pos.Right (prev),
  72. Y = Pos.Y (colorButtonsLabel),
  73. Text = $"_{colorScheme.Key}",
  74. ColorScheme = colorScheme.Value,
  75. };
  76. DoMessage (colorButton, colorButton.Text);
  77. main.Add (colorButton);
  78. prev = colorButton;
  79. }
  80. Button button;
  81. main.Add (
  82. button = new ()
  83. {
  84. X = 2,
  85. Y = Pos.Bottom (colorButtonsLabel) + 1,
  86. Text =
  87. "A super l_öng Button that will probably expose a bug in clipping or wrapping of text. Will it?"
  88. }
  89. );
  90. DoMessage (button, button.Text);
  91. // Note the 'N' in 'Newline' will be the hotkey
  92. main.Add (
  93. button = new () { X = 2, Y = Pos.Bottom (button) + 1, Height = 2, Text = "a Newline\nin the button" }
  94. );
  95. button.Accepting += (s, e) =>
  96. {
  97. MessageBox.Query ("Message", "Question?", "Yes", "No");
  98. e.Cancel = true;
  99. };
  100. var textChanger = new Button { X = 2, Y = Pos.Bottom (button) + 1, Text = "Te_xt Changer" };
  101. main.Add (textChanger);
  102. textChanger.Accepting += (s, e) =>
  103. {
  104. textChanger.Text += "!";
  105. e.Cancel = true;
  106. };
  107. main.Add (
  108. button = new ()
  109. {
  110. X = Pos.Right (textChanger) + 2,
  111. Y = Pos.Y (textChanger),
  112. Text = "Lets see if this will move as \"Text Changer\" grows"
  113. }
  114. );
  115. button.Accepting += (sender, args) => { args.Cancel = true; };
  116. var removeButton = new Button
  117. {
  118. X = 2, Y = Pos.Bottom (button) + 1,
  119. ColorScheme = Colors.ColorSchemes ["Error"], Text = "Remove this button"
  120. };
  121. main.Add (removeButton);
  122. // This in interesting test case because `moveBtn` and below are laid out relative to this one!
  123. removeButton.Accepting += (s, e) =>
  124. {
  125. removeButton.Visible = false;
  126. e.Cancel = true;
  127. };
  128. var computedFrame = new FrameView
  129. {
  130. X = 0,
  131. Y = Pos.Bottom (removeButton) + 1,
  132. Width = Dim.Percent (50),
  133. Height = 6,
  134. Title = "Computed Layout"
  135. };
  136. main.Add (computedFrame);
  137. // Demonstrates how changing the View.Frame property can move Views
  138. var moveBtn = new Button
  139. {
  140. X = 0,
  141. Y = Pos.Center () - 1,
  142. Width = 30,
  143. ColorScheme = Colors.ColorSchemes ["Error"],
  144. Text = "Move This \u263b Button v_ia Pos"
  145. };
  146. moveBtn.Accepting += (s, e) =>
  147. {
  148. moveBtn.X = moveBtn.Frame.X + 5;
  149. e.Cancel = true;
  150. };
  151. computedFrame.Add (moveBtn);
  152. // Demonstrates how changing the View.Frame property can SIZE Views (#583)
  153. var sizeBtn = new Button
  154. {
  155. Y = Pos.Center () + 1,
  156. X = 0,
  157. Width = 30,
  158. Text = "Grow This \u263a Button _via Pos",
  159. ColorScheme = Colors.ColorSchemes ["Error"],
  160. };
  161. sizeBtn.Accepting += (s, e) =>
  162. {
  163. sizeBtn.Width = sizeBtn.Frame.Width + 5;
  164. e.Cancel = true;
  165. };
  166. computedFrame.Add (sizeBtn);
  167. var absoluteFrame = new FrameView
  168. {
  169. X = Pos.Right (computedFrame),
  170. Y = Pos.Bottom (removeButton) + 1,
  171. Width = Dim.Fill (),
  172. Height = 6,
  173. Title = "Absolute Layout"
  174. };
  175. main.Add (absoluteFrame);
  176. // Demonstrates how changing the View.Frame property can move Views
  177. var moveBtnA = new Button { ColorScheme = Colors.ColorSchemes ["Error"], Text = "Move This Button via Frame" };
  178. moveBtnA.Accepting += (s, e) =>
  179. {
  180. moveBtnA.Frame = new (
  181. moveBtnA.Frame.X + 5,
  182. moveBtnA.Frame.Y,
  183. moveBtnA.Frame.Width,
  184. moveBtnA.Frame.Height
  185. );
  186. e.Cancel = true;
  187. };
  188. absoluteFrame.Add (moveBtnA);
  189. // Demonstrates how changing the View.Frame property can SIZE Views (#583)
  190. var sizeBtnA = new Button
  191. {
  192. Y = 2, ColorScheme = Colors.ColorSchemes ["Error"], Text = " ~  s  gui.cs   master ↑_10 = Сохранить"
  193. };
  194. sizeBtnA.Accepting += (s, e) =>
  195. {
  196. sizeBtnA.Frame = new (
  197. sizeBtnA.Frame.X,
  198. sizeBtnA.Frame.Y,
  199. sizeBtnA.Frame.Width + 5,
  200. sizeBtnA.Frame.Height
  201. );
  202. e.Cancel = true;
  203. };
  204. absoluteFrame.Add (sizeBtnA);
  205. var label = new Label
  206. {
  207. X = 2, Y = Pos.Bottom (computedFrame) + 1,
  208. Text = "Text Ali_gnment (changes the four buttons above): "
  209. };
  210. main.Add (label);
  211. var radioGroup = new RadioGroup
  212. {
  213. X = 4,
  214. Y = Pos.Bottom (label) + 1,
  215. SelectedItem = 2,
  216. RadioLabels = new [] { "_Start", "_End", "_Center", "_Fill" },
  217. Title = "_9 RadioGroup",
  218. BorderStyle = LineStyle.Dotted,
  219. // CanFocus = false
  220. };
  221. main.Add (radioGroup);
  222. // Demo changing hotkey
  223. string MoveHotkey (string txt)
  224. {
  225. // Remove the '_'
  226. List<Rune> runes = txt.ToRuneList ();
  227. int i = runes.IndexOf ((Rune)'_');
  228. var start = "";
  229. if (i > -1)
  230. {
  231. start = StringExtensions.ToString (runes.GetRange (0, i));
  232. }
  233. txt = start + StringExtensions.ToString (runes.GetRange (i + 1, runes.Count - (i + 1)));
  234. runes = txt.ToRuneList ();
  235. // Move over one or go to start
  236. i++;
  237. if (i >= runes.Count)
  238. {
  239. i = 0;
  240. }
  241. // Slip in the '_'
  242. start = StringExtensions.ToString (runes.GetRange (0, i));
  243. return start + '_' + StringExtensions.ToString (runes.GetRange (i, runes.Count - i));
  244. }
  245. var mhkb = "Click to Change th_is Button's Hotkey";
  246. var moveHotKeyBtn = new Button
  247. {
  248. X = 2,
  249. Y = Pos.Bottom (radioGroup) + 1,
  250. Width = Dim.Width (computedFrame) - 2,
  251. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  252. Text = mhkb
  253. };
  254. moveHotKeyBtn.Accepting += (s, e) =>
  255. {
  256. moveHotKeyBtn.Text = MoveHotkey (moveHotKeyBtn.Text);
  257. e.Cancel = true;
  258. };
  259. main.Add (moveHotKeyBtn);
  260. var muhkb = " ~  s  gui.cs   master ↑10 = Сохранить";
  261. var moveUnicodeHotKeyBtn = new Button
  262. {
  263. X = Pos.Left (absoluteFrame) + 1,
  264. Y = Pos.Bottom (radioGroup) + 1,
  265. Width = Dim.Width (absoluteFrame) - 2,
  266. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  267. Text = muhkb
  268. };
  269. moveUnicodeHotKeyBtn.Accepting += (s, e) =>
  270. {
  271. moveUnicodeHotKeyBtn.Text = MoveHotkey (moveUnicodeHotKeyBtn.Text);
  272. e.Cancel = true;
  273. };
  274. main.Add (moveUnicodeHotKeyBtn);
  275. radioGroup.SelectedItemChanged += (s, args) =>
  276. {
  277. switch (args.SelectedItem)
  278. {
  279. case 0:
  280. moveBtn.TextAlignment = Alignment.Start;
  281. sizeBtn.TextAlignment = Alignment.Start;
  282. moveBtnA.TextAlignment = Alignment.Start;
  283. sizeBtnA.TextAlignment = Alignment.Start;
  284. moveHotKeyBtn.TextAlignment = Alignment.Start;
  285. moveUnicodeHotKeyBtn.TextAlignment = Alignment.Start;
  286. break;
  287. case 1:
  288. moveBtn.TextAlignment = Alignment.End;
  289. sizeBtn.TextAlignment = Alignment.End;
  290. moveBtnA.TextAlignment = Alignment.End;
  291. sizeBtnA.TextAlignment = Alignment.End;
  292. moveHotKeyBtn.TextAlignment = Alignment.End;
  293. moveUnicodeHotKeyBtn.TextAlignment = Alignment.End;
  294. break;
  295. case 2:
  296. moveBtn.TextAlignment = Alignment.Center;
  297. sizeBtn.TextAlignment = Alignment.Center;
  298. moveBtnA.TextAlignment = Alignment.Center;
  299. sizeBtnA.TextAlignment = Alignment.Center;
  300. moveHotKeyBtn.TextAlignment = Alignment.Center;
  301. moveUnicodeHotKeyBtn.TextAlignment = Alignment.Center;
  302. break;
  303. case 3:
  304. moveBtn.TextAlignment = Alignment.Fill;
  305. sizeBtn.TextAlignment = Alignment.Fill;
  306. moveBtnA.TextAlignment = Alignment.Fill;
  307. sizeBtnA.TextAlignment = Alignment.Fill;
  308. moveHotKeyBtn.TextAlignment = Alignment.Fill;
  309. moveUnicodeHotKeyBtn.TextAlignment = Alignment.Fill;
  310. break;
  311. }
  312. };
  313. label = new ()
  314. {
  315. X = 0,
  316. Y = Pos.Bottom (moveUnicodeHotKeyBtn) + 1,
  317. Title = "_Numeric Up/Down (press-and-hold):",
  318. };
  319. var numericUpDown = new NumericUpDown<int>
  320. {
  321. Value = 69,
  322. X = Pos.Right (label) + 1,
  323. Y = Pos.Top (label),
  324. };
  325. numericUpDown.ValueChanged += NumericUpDown_ValueChanged;
  326. void NumericUpDown_ValueChanged (object sender, EventArgs<int> e) { }
  327. main.Add (label, numericUpDown);
  328. label = new ()
  329. {
  330. X = 0,
  331. Y = Pos.Bottom (numericUpDown) + 1,
  332. Title = "_No Repeat:"
  333. };
  334. var noRepeatAcceptCount = 0;
  335. var noRepeatButton = new Button
  336. {
  337. X = Pos.Right (label) + 1,
  338. Y = Pos.Top (label),
  339. Title = $"Accept Cou_nt: {noRepeatAcceptCount}",
  340. WantContinuousButtonPressed = false
  341. };
  342. noRepeatButton.Accepting += (s, e) =>
  343. {
  344. noRepeatButton.Title = $"Accept Cou_nt: {++noRepeatAcceptCount}";
  345. e.Cancel = true;
  346. };
  347. main.Add (label, noRepeatButton);
  348. label = new ()
  349. {
  350. X = 0,
  351. Y = Pos.Bottom (label) + 1,
  352. Title = "_Repeat (press-and-hold):"
  353. };
  354. var acceptCount = 0;
  355. var repeatButton = new Button
  356. {
  357. X = Pos.Right (label) + 1,
  358. Y = Pos.Top (label),
  359. Title = $"Accept Co_unt: {acceptCount}",
  360. WantContinuousButtonPressed = true
  361. };
  362. repeatButton.Accepting += (s, e) =>
  363. {
  364. repeatButton.Title = $"Accept Co_unt: {++acceptCount}";
  365. e.Cancel = true;
  366. };
  367. var enableCB = new CheckBox
  368. {
  369. X = Pos.Right (repeatButton) + 1,
  370. Y = Pos.Top (repeatButton),
  371. Title = "Enabled",
  372. CheckedState = CheckState.Checked
  373. };
  374. enableCB.CheckedStateChanging += (s, e) => { repeatButton.Enabled = !repeatButton.Enabled; };
  375. main.Add (label, repeatButton, enableCB);
  376. var decNumericUpDown = new NumericUpDown<int>
  377. {
  378. Value = 911,
  379. Increment = 1,
  380. Format = "{0:X}",
  381. X = 0,
  382. Y = Pos.Bottom (enableCB) + 1,
  383. };
  384. main.Add (decNumericUpDown);
  385. Application.Run (main);
  386. main.Dispose ();
  387. Application.Shutdown ();
  388. }
  389. }