Переглянути джерело

* Button.cs: only perform click when button is Selectable (so as
not to activate default buttons when they're disabled)

* Control.cs: Rewrite of the SelectNextControl and related
methods. HandleClick now selects next control if the current one
is being disabled.

* Form.cs: OnActivated selects next active control only if Load
has already occurred. If Load hasn't run, there's no point in
selecting here, Load might change the state of controls.

* FocusTest.cs: Tests marked as working again for these fixes

2006-11-10 Andreia Gaita <[email protected]>

svn path=/trunk/mcs/; revision=67668

Andreia Gaita 19 роки тому
батько
коміт
e0440a3e00

+ 2 - 1
mcs/class/Managed.Windows.Forms/System.Windows.Forms/Button.cs

@@ -75,7 +75,8 @@ namespace System.Windows.Forms {
 		}
 
 		public void PerformClick() {			// IButtonControl
-			OnClick(EventArgs.Empty);
+			if (CanSelect)
+				OnClick(EventArgs.Empty);
 		}
 
 		public override string ToString() {

+ 15 - 0
mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog

@@ -1,3 +1,18 @@
+2006-11-10  Andreia Gaita  <[email protected]>
+
+	* Button.cs: only perform click when button is Selectable (so as 
+	not to activate default buttons when they're disabled)
+	
+	* Control.cs: Rewrite of the SelectNextControl and related 
+	methods. HandleClick now selects next control if the current one
+	is being disabled.
+	
+	* Form.cs: OnActivated selects next active control only if Load 
+	has already occurred. If Load hasn't run, there's no point in 
+	selecting here, Load might change the state of controls.
+	
+	* FocusTest.cs: Tests marked as working again for these fixes
+
 2006-11-10  Chris Toshok  <[email protected]>
 
 	* XplatUIX11.cs: a couple of fixes.

+ 82 - 51
mcs/class/Managed.Windows.Forms/System.Windows.Forms/Control.cs

@@ -33,6 +33,7 @@
 // COMPLETE 
 
 #undef DebugRecreate
+#undef DebugFocus
 
 using System;
 using System.ComponentModel;
@@ -1197,30 +1198,27 @@ namespace System.Windows.Forms
 
 		private static Control FindControlForward(Control container, Control start) {
 			Control found;
-			Control	p;
 
 			found = null;
 
-			if (start != null) {
-				if (start.child_controls != null && start.child_controls.Count > 0 &&
-					!((start is IContainerControl) && start.GetStyle(ControlStyles.ContainerControl))) {
-					found = FindControlForward(start, null);
-					if (found != null) {
-						return found;
-					}
-				}
+			if (start == null) {
+				return FindFlatForward(container, start);
+			}
 
-				p = start.parent;
-				while (p != container) {
-					found = FindFlatForward(p, start);
+			if (start.child_controls != null && start.child_controls.Count > 0 && 
+				(start == container || !((start is IContainerControl) &&  start.GetStyle(ControlStyles.ContainerControl)))) {
+				return FindControlForward(start, null);
+			}
+			else {
+				while (start != container) {
+					found = FindFlatForward(start.parent, start);
 					if (found != null) {
 						return found;
 					}
-					start = p;
-					p = p.parent;
+					start = start.parent;
 				}
 			}
-			return FindFlatForward(container, start);
+			return null;
 		}
 
 		private static Control FindFlatBackward(Control container, Control start) {
@@ -1244,27 +1242,55 @@ namespace System.Windows.Forms
 				index++;
 			}
 
-			for (int i = end-1, pos = -1; i >= 0; i--) {
+			bool hit = false;
+					
+			for (int i = end - 1; i >= 0; i--) {
 				if (start == container.child_controls[i]) {
-					pos = i;
+					hit = true;
 					continue;
 				}
 
-				if (found == null) {
-					if (container.child_controls[i].tab_index < index || 
-						(pos > -1 && pos > i && container.child_controls[i].tab_index == index)) {
-						found = container.child_controls[i];
-					}
-				} else if (found.tab_index < container.child_controls[i].tab_index) {
-					if (container.child_controls[i].tab_index < index) {
+				if (found == null || found.tab_index < container.child_controls[i].tab_index) {
+					if (container.child_controls[i].tab_index < index || (hit && container.child_controls[i].tab_index == index))
 						found = container.child_controls[i];
-					}
+
 				}
 			}
 			return found;
 		}
 
 		private static Control FindControlBackward(Control container, Control start) {
+
+			Control found = null;
+
+			if (start == null) {
+				found = FindFlatBackward(container, start);
+			}
+			else if (start != container) {
+				if (start.parent != null) {
+					found = FindFlatBackward(start.parent, start);
+
+					if (found == null) {
+						if (start.parent != container)
+							return start.parent;
+						return null;
+					}
+				}
+			}
+		
+			if (found == null || start.parent == null)
+				found = start;
+
+			while (found != null && (found == container || (!((found is IContainerControl) && found.GetStyle(ControlStyles.ContainerControl))) &&
+				found.child_controls != null && found.child_controls.Count > 0)) {
+//				while (ctl.child_controls != null && ctl.child_controls.Count > 0 && 
+//					(ctl == this || (!((ctl is IContainerControl) && ctl.GetStyle(ControlStyles.ContainerControl))))) {
+				found = FindFlatBackward(found, null);
+			}
+
+			return found;
+
+/*
 			Control found;
 
 			found = null;
@@ -1289,8 +1315,8 @@ namespace System.Windows.Forms
 					}
 				}
 			}
-
 			return found;
+*/			
 		}
 
 		internal virtual void HandleClick(int clicks, MouseEventArgs me) {
@@ -1959,14 +1985,12 @@ namespace System.Windows.Forms
 			}
 
 			set {
-				if (Enabled == value) {
-					is_enabled = value;
-					return;
-				}
 
-				is_enabled = value;
+				bool old_value = is_enabled;
 
-				// FIXME - we need to switch focus to next control if we're disabling the focused control
+				is_enabled = value;
+				if (old_value != value && !value && this.has_focus)
+					SelectNextControl(this, true, true, true, true);
 
 				OnEnabledChanged (EventArgs.Empty);				
 			}
@@ -2817,30 +2841,21 @@ namespace System.Windows.Forms
 
 		public Control GetNextControl(Control ctl, bool forward) {
 
-			if (ctl != null && this != ctl) {
-				if (!ctl.CanSelect || 
-					((parent == null) && (ctl is IContainerControl) && ctl.GetStyle(ControlStyles.ContainerControl))
-				   ) {
-					if (forward) {
-						return FindFlatForward(this, ctl);
-					} else {
-						return FindFlatBackward(this, ctl);
-					}
-				}
-			}
-
-			// If ctl is not contained by this, we start at the first child of this
 			if (!this.Contains(ctl)) {
-				ctl = null;
+				ctl = this;
 			}
 
-			// Search through our controls, starting at ctl, stepping into children as we encounter them
-			// try to find the control with the tabindex closest to our own, or, if we're looking into
-			// child controls, the one with the smallest tabindex
 			if (forward) {
-				return FindControlForward(this, ctl);
+				ctl = FindControlForward(this, ctl);
 			}
-			return FindControlBackward(this, ctl);
+			else {
+				ctl = FindControlBackward(this, ctl);
+			}
+
+			if (ctl != this) {
+				return ctl;
+			}
+			return null;
 		}
 
 #if NET_2_0
@@ -3226,9 +3241,25 @@ namespace System.Windows.Forms
 			Select(false, false);
 		}
 
+#if DebugFocus
+		private void printTree(Control c, string t) {
+			foreach(Control i in c.child_controls) {
+				Console.WriteLine("{2}{0}.TabIndex={1}", i, i.tab_index, t);
+				printTree(i, t+"\t");
+			}
+		}
+#endif
 		public bool SelectNextControl(Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) {
 			Control c;
 
+#if DebugFocus
+			Console.WriteLine("{0}", this.FindForm());
+			printTree(this, "\t");
+#endif
+
+			if (!this.Contains(ctl) || (!nested && (ctl.parent != this))) {
+				ctl = null;
+			}
 			c = ctl;
 			do {
 				c = GetNextControl(c, forward);

+ 4 - 1
mcs/class/Managed.Windows.Forms/System.Windows.Forms/Form.cs

@@ -78,6 +78,7 @@ namespace System.Windows.Forms {
 		internal ApplicationContext	context;
 		Color				transparency_key;
 		internal MenuTracker		active_tracker;
+		private bool			is_loaded;
 
 		#endregion	// Local Variables
 
@@ -1424,7 +1425,8 @@ namespace System.Windows.Forms {
 		[EditorBrowsable(EditorBrowsableState.Advanced)]
 		protected virtual void OnActivated(EventArgs e)
 		{
-			SelectActiveControl ();
+			if (is_loaded)
+				SelectActiveControl ();
 
 			if (Activated != null) {
 				Activated(this, e);
@@ -1527,6 +1529,7 @@ namespace System.Windows.Forms {
 						break;
 				}
 			}
+			is_loaded = true;
 		}
 
 		[EditorBrowsable(EditorBrowsableState.Advanced)]

+ 0 - 4
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/FocusTest.cs

@@ -601,7 +601,6 @@ namespace MonoTests.System.Windows.Forms {
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		public void GetNextGroupBoxControlFlat ()
 		{
 			Form form = new Form ();
@@ -640,7 +639,6 @@ namespace MonoTests.System.Windows.Forms {
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		public void GetNextControlFromTabControl ()
 		{
 			Form form = new Form ();
@@ -670,7 +668,6 @@ namespace MonoTests.System.Windows.Forms {
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		public void GetNextControlFromTabControl2 () {
 			Form form = new Form ();
 			form.ShowInTaskbar = false;
@@ -816,7 +813,6 @@ namespace MonoTests.System.Windows.Forms {
 		}
 
 		[Test]
-		[Category ("NotWorking")]
 		public void FocusSetsActive ()
 		{
 			Form form = new Form ();