BordersOnToplevel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios {
  6. [ScenarioMetadata (Name: "Borders on Toplevel", Description: "Demonstrates Toplevel borders manipulation.")]
  7. [ScenarioCategory ("Layout")]
  8. [ScenarioCategory ("Borders")]
  9. public class BordersOnToplevel : Scenario {
  10. public override void Setup ()
  11. {
  12. var borderStyle = BorderStyle.Double;
  13. var drawMarginFrame = false;
  14. var borderThickness = new Thickness (1, 2, 3, 4);
  15. var borderBrush = Colors.Base.HotFocus.Foreground;
  16. var padding = new Thickness (1, 2, 3, 4);
  17. var background = Colors.Base.HotNormal.Foreground;
  18. var effect3D = true;
  19. var smartView = new Border.ToplevelContainer () {
  20. X = Pos.Center (),
  21. Y = Pos.Center () - 7,
  22. Width = 40,
  23. Height = 20,
  24. Border = new Border () {
  25. BorderStyle = borderStyle,
  26. DrawMarginFrame = drawMarginFrame,
  27. BorderThickness = borderThickness,
  28. BorderBrush = borderBrush,
  29. Padding = padding,
  30. Background = background,
  31. Effect3D = effect3D,
  32. Title = "Toplevel"
  33. },
  34. ColorScheme = Colors.TopLevel
  35. };
  36. var tf1 = new TextField ("1234567890") { Width = 10 };
  37. var button = new Button ("Press me!") {
  38. X = Pos.Center (),
  39. Y = Pos.Center (),
  40. };
  41. button.Clicked += () => MessageBox.Query (20, 7, "Hi", "I'm a Toplevel?", "Yes", "No");
  42. var label = new Label ("I'm a Toplevel") {
  43. X = Pos.Center (),
  44. Y = Pos.Center () - 3,
  45. };
  46. var tf2 = new TextField ("1234567890") {
  47. X = Pos.AnchorEnd (10),
  48. Y = Pos.AnchorEnd (1),
  49. Width = 10
  50. };
  51. var tv = new TextView () {
  52. Y = Pos.AnchorEnd (2),
  53. Width = 10,
  54. Height = Dim.Fill (),
  55. Text = "1234567890"
  56. };
  57. smartView.Add (tf1, button, label, tf2, tv);
  58. Win.Add (new Label ("Padding:") {
  59. X = Pos.Center () - 23,
  60. });
  61. var paddingTopEdit = new TextField ("") {
  62. X = Pos.Center () - 22,
  63. Y = 1,
  64. Width = 5
  65. };
  66. paddingTopEdit.TextChanging += (e) => {
  67. try {
  68. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Left,
  69. int.Parse (e.NewText.ToString ()), smartView.Border.Padding.Right,
  70. smartView.Border.Padding.Bottom);
  71. } catch {
  72. if (!e.NewText.IsEmpty) {
  73. e.Cancel = true;
  74. }
  75. }
  76. };
  77. paddingTopEdit.Text = $"{smartView.Border.Padding.Top}";
  78. Win.Add (paddingTopEdit);
  79. var paddingLeftEdit = new TextField ("") {
  80. X = Pos.Center () - 30,
  81. Y = 2,
  82. Width = 5
  83. };
  84. paddingLeftEdit.TextChanging += (e) => {
  85. try {
  86. smartView.Border.Padding = new Thickness (int.Parse (e.NewText.ToString ()),
  87. smartView.Border.Padding.Top, smartView.Border.Padding.Right,
  88. smartView.Border.Padding.Bottom);
  89. } catch {
  90. if (!e.NewText.IsEmpty) {
  91. e.Cancel = true;
  92. }
  93. }
  94. };
  95. paddingLeftEdit.Text = $"{smartView.Border.Padding.Left}";
  96. Win.Add (paddingLeftEdit);
  97. var paddingRightEdit = new TextField ("") {
  98. X = Pos.Center () - 15,
  99. Y = 2,
  100. Width = 5
  101. };
  102. paddingRightEdit.TextChanging += (e) => {
  103. try {
  104. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Left,
  105. smartView.Border.Padding.Top, int.Parse (e.NewText.ToString ()),
  106. smartView.Border.Padding.Bottom);
  107. } catch {
  108. if (!e.NewText.IsEmpty) {
  109. e.Cancel = true;
  110. }
  111. }
  112. };
  113. paddingRightEdit.Text = $"{smartView.Border.Padding.Right}";
  114. Win.Add (paddingRightEdit);
  115. var paddingBottomEdit = new TextField ("") {
  116. X = Pos.Center () - 22,
  117. Y = 3,
  118. Width = 5
  119. };
  120. paddingBottomEdit.TextChanging += (e) => {
  121. try {
  122. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Left,
  123. smartView.Border.Padding.Top, smartView.Border.Padding.Right,
  124. int.Parse (e.NewText.ToString ()));
  125. } catch {
  126. if (!e.NewText.IsEmpty) {
  127. e.Cancel = true;
  128. }
  129. }
  130. };
  131. paddingBottomEdit.Text = $"{smartView.Border.Padding.Bottom}";
  132. Win.Add (paddingBottomEdit);
  133. var replacePadding = new Button ("Replace all based on top") {
  134. X = Pos.Center () - 35,
  135. Y = 5
  136. };
  137. replacePadding.Clicked += () => {
  138. smartView.Border.Padding = new Thickness (smartView.Border.Padding.Top);
  139. if (paddingTopEdit.Text.IsEmpty) {
  140. paddingTopEdit.Text = "0";
  141. }
  142. paddingBottomEdit.Text = paddingLeftEdit.Text = paddingRightEdit.Text = paddingTopEdit.Text;
  143. };
  144. Win.Add (replacePadding);
  145. Win.Add (new Label ("Border:") {
  146. X = Pos.Center () + 11,
  147. });
  148. var borderTopEdit = new TextField ("") {
  149. X = Pos.Center () + 12,
  150. Y = 1,
  151. Width = 5
  152. };
  153. borderTopEdit.TextChanging += (e) => {
  154. try {
  155. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Left,
  156. int.Parse (e.NewText.ToString ()), smartView.Border.BorderThickness.Right,
  157. smartView.Border.BorderThickness.Bottom);
  158. } catch {
  159. if (!e.NewText.IsEmpty) {
  160. e.Cancel = true;
  161. }
  162. }
  163. };
  164. borderTopEdit.Text = $"{smartView.Border.BorderThickness.Top}";
  165. Win.Add (borderTopEdit);
  166. var borderLeftEdit = new TextField ("") {
  167. X = Pos.Center () + 5,
  168. Y = 2,
  169. Width = 5
  170. };
  171. borderLeftEdit.TextChanging += (e) => {
  172. try {
  173. smartView.Border.BorderThickness = new Thickness (int.Parse (e.NewText.ToString ()),
  174. smartView.Border.BorderThickness.Top, smartView.Border.BorderThickness.Right,
  175. smartView.Border.BorderThickness.Bottom);
  176. } catch {
  177. if (!e.NewText.IsEmpty) {
  178. e.Cancel = true;
  179. }
  180. }
  181. };
  182. borderLeftEdit.Text = $"{smartView.Border.BorderThickness.Left}";
  183. Win.Add (borderLeftEdit);
  184. var borderRightEdit = new TextField ("") {
  185. X = Pos.Center () + 19,
  186. Y = 2,
  187. Width = 5
  188. };
  189. borderRightEdit.TextChanging += (e) => {
  190. try {
  191. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Left,
  192. smartView.Border.BorderThickness.Top, int.Parse (e.NewText.ToString ()),
  193. smartView.Border.BorderThickness.Bottom);
  194. } catch {
  195. if (!e.NewText.IsEmpty) {
  196. e.Cancel = true;
  197. }
  198. }
  199. };
  200. borderRightEdit.Text = $"{smartView.Border.BorderThickness.Right}";
  201. Win.Add (borderRightEdit);
  202. var borderBottomEdit = new TextField ("") {
  203. X = Pos.Center () + 12,
  204. Y = 3,
  205. Width = 5
  206. };
  207. borderBottomEdit.TextChanging += (e) => {
  208. try {
  209. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Left,
  210. smartView.Border.BorderThickness.Top, smartView.Border.BorderThickness.Right,
  211. int.Parse (e.NewText.ToString ()));
  212. } catch {
  213. if (!e.NewText.IsEmpty) {
  214. e.Cancel = true;
  215. }
  216. }
  217. };
  218. borderBottomEdit.Text = $"{smartView.Border.BorderThickness.Bottom}";
  219. Win.Add (borderBottomEdit);
  220. var replaceBorder = new Button ("Replace all based on top") {
  221. X = Pos.Center () + 1,
  222. Y = 5
  223. };
  224. replaceBorder.Clicked += () => {
  225. smartView.Border.BorderThickness = new Thickness (smartView.Border.BorderThickness.Top);
  226. if (borderTopEdit.Text.IsEmpty) {
  227. borderTopEdit.Text = "0";
  228. }
  229. borderBottomEdit.Text = borderLeftEdit.Text = borderRightEdit.Text = borderTopEdit.Text;
  230. };
  231. Win.Add (replaceBorder);
  232. Win.Add (new Label ("BorderStyle:"));
  233. var borderStyleEnum = Enum.GetValues (typeof (BorderStyle)).Cast<BorderStyle> ().ToList ();
  234. var rbBorderStyle = new RadioGroup (borderStyleEnum.Select (
  235. e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  236. X = 2,
  237. Y = 1,
  238. SelectedItem = (int)smartView.Border.BorderStyle
  239. };
  240. Win.Add (rbBorderStyle);
  241. var cbDrawMarginFrame = new CheckBox ("Draw Margin Frame", smartView.Border.DrawMarginFrame) {
  242. X = Pos.AnchorEnd (20),
  243. Y = 0,
  244. Width = 5
  245. };
  246. cbDrawMarginFrame.Toggled += (e) => {
  247. try {
  248. smartView.Border.DrawMarginFrame = cbDrawMarginFrame.Checked;
  249. if (cbDrawMarginFrame.Checked != smartView.Border.DrawMarginFrame) {
  250. cbDrawMarginFrame.Checked = smartView.Border.DrawMarginFrame;
  251. }
  252. } catch { }
  253. };
  254. Win.Add (cbDrawMarginFrame);
  255. rbBorderStyle.SelectedItemChanged += (e) => {
  256. smartView.Border.BorderStyle = (BorderStyle)e.SelectedItem;
  257. smartView.SetNeedsDisplay ();
  258. if (cbDrawMarginFrame.Checked != smartView.Border.DrawMarginFrame) {
  259. cbDrawMarginFrame.Checked = smartView.Border.DrawMarginFrame;
  260. }
  261. };
  262. var cbEffect3D = new CheckBox ("Draw 3D effects", smartView.Border.Effect3D) {
  263. X = Pos.AnchorEnd (20),
  264. Y = 1,
  265. Width = 5
  266. };
  267. Win.Add (cbEffect3D);
  268. Win.Add (new Label ("Effect3D Offset:") {
  269. X = Pos.AnchorEnd (20),
  270. Y = 2
  271. });
  272. Win.Add (new Label ("X:") {
  273. X = Pos.AnchorEnd (19),
  274. Y = 3
  275. });
  276. var effect3DOffsetX = new TextField ("") {
  277. X = Pos.AnchorEnd (16),
  278. Y = 3,
  279. Width = 5
  280. };
  281. effect3DOffsetX.TextChanging += (e) => {
  282. try {
  283. smartView.Border.Effect3DOffset = new Point (int.Parse (e.NewText.ToString ()),
  284. smartView.Border.Effect3DOffset.Y);
  285. } catch {
  286. if (!e.NewText.IsEmpty && e.NewText != CultureInfo.CurrentCulture.NumberFormat.NegativeSign) {
  287. e.Cancel = true;
  288. }
  289. }
  290. };
  291. effect3DOffsetX.Text = $"{smartView.Border.Effect3DOffset.X}";
  292. Win.Add (effect3DOffsetX);
  293. Win.Add (new Label ("Y:") {
  294. X = Pos.AnchorEnd (10),
  295. Y = 3
  296. });
  297. var effect3DOffsetY = new TextField ("") {
  298. X = Pos.AnchorEnd (7),
  299. Y = 3,
  300. Width = 5
  301. };
  302. effect3DOffsetY.TextChanging += (e) => {
  303. try {
  304. smartView.Border.Effect3DOffset = new Point (smartView.Border.Effect3DOffset.X,
  305. int.Parse (e.NewText.ToString ()));
  306. } catch {
  307. if (!e.NewText.IsEmpty && e.NewText != CultureInfo.CurrentCulture.NumberFormat.NegativeSign) {
  308. e.Cancel = true;
  309. }
  310. }
  311. };
  312. effect3DOffsetY.Text = $"{smartView.Border.Effect3DOffset.Y}";
  313. Win.Add (effect3DOffsetY);
  314. cbEffect3D.Toggled += (e) => {
  315. try {
  316. smartView.Border.Effect3D = effect3DOffsetX.Enabled =
  317. effect3DOffsetY.Enabled = cbEffect3D.Checked;
  318. } catch { }
  319. };
  320. Win.Add (new Label ("Background:") {
  321. Y = 5
  322. });
  323. var colorEnum = Enum.GetValues (typeof (Color)).Cast<Color> ().ToList ();
  324. var rbBackground = new RadioGroup (colorEnum.Select (
  325. e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  326. X = 2,
  327. Y = 6,
  328. SelectedItem = (int)smartView.Border.Background
  329. };
  330. rbBackground.SelectedItemChanged += (e) => {
  331. smartView.Border.Background = (Color)e.SelectedItem;
  332. };
  333. Win.Add (rbBackground);
  334. Win.Add (new Label ("BorderBrush:") {
  335. X = Pos.AnchorEnd (20),
  336. Y = 5
  337. });
  338. var rbBorderBrush = new RadioGroup (colorEnum.Select (
  339. e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  340. X = Pos.AnchorEnd (18),
  341. Y = 6,
  342. SelectedItem = (int)smartView.Border.BorderBrush
  343. };
  344. rbBorderBrush.SelectedItemChanged += (e) => {
  345. smartView.Border.BorderBrush = (Color)e.SelectedItem;
  346. };
  347. Win.Add (rbBorderBrush);
  348. Win.Add (smartView);
  349. }
  350. }
  351. }