BordersOnFrameView.cs 11 KB

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