Răsfoiți Sursa

Fixes #990. Pos and Dim only can be properly used when all the views are totally initialized.

BDisp 4 ani în urmă
părinte
comite
4bbe50c506

+ 2 - 2
Terminal.Gui/Core/View.cs

@@ -656,10 +656,10 @@ namespace Terminal.Gui {
 		/// </para>
 		/// </remarks>
 		/// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
-		public View (ustring text) : base ()
+		public View (ustring text)
 		{
 			textFormatter = new TextFormatter ();
-			this.Text = text;
+			Text = text;
 
 			shortcutHelper = new ShortcutHelper ();
 

+ 5 - 4
Terminal.Gui/Views/Button.cs

@@ -55,7 +55,7 @@ namespace Terminal.Gui {
 		///   If <c>true</c>, a special decoration is used, and the user pressing the enter key 
 		///   in a <see cref="Dialog"/> will implicitly activate this button.
 		/// </param>
-		public Button (ustring text, bool is_default = false) : base ()
+		public Button (ustring text, bool is_default = false) : base (text)
 		{
 			Init (text, is_default);
 		}
@@ -87,7 +87,7 @@ namespace Terminal.Gui {
 		///   in a <see cref="Dialog"/> will implicitly activate this button.
 		/// </param>
 		public Button (int x, int y, ustring text, bool is_default)
-		    : base (new Rect (x, y, text.RuneCount + 4 + (is_default ? 2 : 0), 1))
+		    : base (new Rect (x, y, text.RuneCount + 4 + (is_default ? 2 : 0), 1), text)
 		{
 			Init (text, is_default);
 		}
@@ -107,8 +107,9 @@ namespace Terminal.Gui {
 			_rightDefault = new Rune (Driver != null ? Driver.RightDefaultIndicator : '>');
 
 			CanFocus = true;
-			this.IsDefault = is_default;
-			Text = text ?? string.Empty;
+			this.is_default = is_default;
+			this.text = text ?? string.Empty;
+			Update ();
 		}
 
 		/// <summary>

+ 1 - 2
Terminal.Gui/Views/TextField.cs

@@ -59,8 +59,7 @@ namespace Terminal.Gui {
 		/// <param name="text">Initial text contents.</param>
 		public TextField (ustring text) : base (text)
 		{
-			Initialize (text, 0);
-			Width = text.RuneCount + 1;
+			Initialize (text, text.RuneCount + 1);
 		}
 
 		/// <summary>

+ 2 - 1
Terminal.Gui/Windows/Dialog.cs

@@ -104,6 +104,7 @@ namespace Terminal.Gui {
 
 			buttons.Add (button);
 			Add (button);
+			SetNeedsDisplay ();
 			LayoutSubviews ();
 		}
 
@@ -112,7 +113,7 @@ namespace Terminal.Gui {
 			if (buttons.Count == 0) {
 				return 0;
 			}
-			return buttons.Select (b => b.Bounds.Width).Sum () + buttons.Count() - 1;
+			return buttons.Select (b => b.Bounds.Width).Sum () + buttons.Count - 1;
 		}
 
 		void LayoutStartedHandler ()

+ 60 - 0
UICatalog/NumberToWords.cs

@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace UICatalog {
+	public static class NumberToWords {
+		private static String [] units = { "Zero", "One", "Two", "Three",
+		    "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
+		    "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
+		    "Seventeen", "Eighteen", "Nineteen" };
+		private static String [] tens = { "", "", "Twenty", "Thirty", "Forty",
+		    "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
+
+		public static String ConvertAmount (double amount)
+		{
+			try {
+				Int64 amount_int = (Int64)amount;
+				Int64 amount_dec = (Int64)Math.Round ((amount - (double)(amount_int)) * 100);
+				if (amount_dec == 0) {
+					return Convert (amount_int) + " Only.";
+				} else {
+					return Convert (amount_int) + " Point " + Convert (amount_dec) + " Only.";
+				}
+			} catch (Exception e) {
+				// TODO: handle exception  
+			}
+			return "";
+		}
+
+		public static String Convert (Int64 i)
+		{
+			if (i < 20) {
+				return units [i];
+			}
+			if (i < 100) {
+				return tens [i / 10] + ((i % 10 > 0) ? " " + Convert (i % 10) : "");
+			}
+			if (i < 1000) {
+				return units [i / 100] + " Hundred"
+					+ ((i % 100 > 0) ? " And " + Convert (i % 100) : "");
+			}
+			if (i < 100000) {
+				return Convert (i / 1000) + " Thousand "
+				+ ((i % 1000 > 0) ? " " + Convert (i % 1000) : "");
+			}
+			if (i < 10000000) {
+				return Convert (i / 100000) + " Lakh "
+					+ ((i % 100000 > 0) ? " " + Convert (i % 100000) : "");
+			}
+			if (i < 1000000000) {
+				return Convert (i / 10000000) + " Crore "
+					+ ((i % 10000000 > 0) ? " " + Convert (i % 10000000) : "");
+			}
+			return Convert (i / 1000000000) + " Arab "
+				+ ((i % 1000000000 > 0) ? " " + Convert (i % 1000000000) : "");
+		}
+	}
+}

+ 16 - 12
UICatalog/Scenarios/Dialogs.cs

@@ -39,7 +39,7 @@ namespace UICatalog {
 			label = new Label ("height:") {
 				X = 0,
 				Y = Pos.Bottom (label),
-				Width = Dim.Width(label),
+				Width = Dim.Width (label),
 				Height = 1,
 				TextAlignment = Terminal.Gui.TextAlignment.Right,
 			};
@@ -93,28 +93,28 @@ namespace UICatalog {
 			};
 			frame.Add (numButtonsEdit);
 
-			frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) 
-				+ Dim.Height(numButtonsEdit) + 2;
+			Top.Ready += () => frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit)
+				+ Dim.Height (numButtonsEdit) + 2;
 
 			label = new Label ("Button Pressed:") {
 				X = Pos.Center (),
-				Y = Pos.Bottom (frame) + 2,
+				Y = Pos.Bottom (frame) + 4,
 				Height = 1,
 				TextAlignment = Terminal.Gui.TextAlignment.Right,
 			};
 			Win.Add (label);
-			var buttonPressedLabel = new Label ("") {
+			var buttonPressedLabel = new Label (" ") {
 				X = Pos.Center (),
-				Y = Pos.Bottom (frame) + 4,
+				Y = Pos.Bottom (frame) + 5,
 				Width = 25,
 				Height = 1,
 				ColorScheme = Colors.Error,
 			};
 
-			var btnText = new [] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
+			//var btnText = new [] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
 			var showDialogButton = new Button ("Show Dialog") {
 				X = Pos.Center(),
-				Y = Pos.Bottom (frame) + 2			,
+				Y = Pos.Bottom (frame) + 2,
 				IsDefault = true,
 			};
 			showDialogButton.Clicked += () => {
@@ -127,7 +127,9 @@ namespace UICatalog {
 					var clicked = -1;
 					for (int i = 0; i < numButtons; i++) {
 						var buttonId = i;
-						var button = new Button (btnText [buttonId % 10],
+						//var button = new Button (btnText [buttonId % 10],
+						//	is_default: buttonId == 0);
+						var button = new Button (NumberToWords.Convert(buttonId),
 							is_default: buttonId == 0);
 						button.Clicked += () => {
 							clicked = buttonId;
@@ -142,12 +144,13 @@ namespace UICatalog {
 						buttons.ToArray ());
 					var add = new Button ("Add a button") {
 						X = Pos.Center (),
-						Y = Pos.Center (),
-
+						Y = Pos.Center ()
 					};
 					add.Clicked += () => {
 						var buttonId = buttons.Count;
-						var button = new Button (btnText [buttonId % 10],
+						//var button = new Button (btnText [buttonId % 10],
+						//	is_default: buttonId == 0);
+						var button = new Button (NumberToWords.Convert (buttonId),
 							is_default: buttonId == 0);
 						button.Clicked += () => {
 							clicked = buttonId;
@@ -155,6 +158,7 @@ namespace UICatalog {
 						};
 						buttons.Add (button);
 						dialog.AddButton (button);
+						button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
 					};
 					dialog.Add (add);
 

+ 5 - 6
UICatalog/Scenarios/MessageBoxes.cs

@@ -38,7 +38,7 @@ namespace UICatalog {
 			label = new Label ("height:") {
 				X = 0,
 				Y = Pos.Bottom (label),
-				Width = Dim.Width(label),
+				Width = Dim.Width (label),
 				Height = 1,
 				TextAlignment = Terminal.Gui.TextAlignment.Right,
 			};
@@ -124,9 +124,7 @@ namespace UICatalog {
 			};
 			frame.Add (styleRadioGroup);
 
-			frame.LayoutSubviews ();
-
-			frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (messageEdit)
+			Top.Ready += () => frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (messageEdit)
 				+ Dim.Height (numButtonsEdit) + Dim.Height (styleRadioGroup) + 2;
 
 			label = new Label ("Button Pressed:") {
@@ -144,7 +142,7 @@ namespace UICatalog {
 				ColorScheme = Colors.Error,
 			};
 
-			var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };
+			//var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };
 
 			var showMessageBoxButton = new Button ("Show MessageBox") {
 				X = Pos.Center(),
@@ -159,7 +157,8 @@ namespace UICatalog {
 
 					var btns = new List<ustring> ();
 					for (int i = 0; i < numButtons; i++) {
-						btns.Add(btnText[i % 10]);
+						//btns.Add(btnText[i % 10]);
+						btns.Add (NumberToWords.Convert (i));
 					}
 					if (styleRadioGroup.SelectedItem == 0) {
 						buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), btns.ToArray ())}";