BordersOnWindow.cs 11 KB

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