if (value == null)
return;
- bool owner_permits_toplevels = (owner is MdiClient) || (owner is Form && ((Form)owner).IsMdiContainer);
- bool child_is_toplevel = ((Control)value).GetTopLevel();
- bool child_is_mdichild = (value is Form && ((Form)value).IsMdiChild);
+ Form form_value = value as Form;
+ Form form_owner = owner as Form;
+ bool owner_permits_toplevels = (owner is MdiClient) || (form_owner != null && form_owner.IsMdiContainer);
+ bool child_is_toplevel = value.GetTopLevel();
+ bool child_is_mdichild = form_value != null && form_value.IsMdiChild;
if (child_is_toplevel && !(owner_permits_toplevels && child_is_mdichild))
throw new ArgumentException("Cannot add a top level control to a control.", "value");
+ if (child_is_mdichild && form_value.MdiParent != null && form_value.MdiParent != owner && form_value.MdiParent != owner.Parent) {
+ throw new ArgumentException ("Form cannot be added to the Controls collection that has a valid MDI parent.", "value");
+ }
+
if (Contains (value)) {
owner.PerformLayout();
return;
control.InitLayout ();
if (owner.Visible)
owner.UpdateChildrenZOrder ();
- owner.PerformLayout (control, "Parent");
- owner.OnControlAdded (new ControlEventArgs (control));
+
+ // If we are adding a new control that isn't
+ // visible, don't trigger a layout
+ if (control.VisibleInternal)
+ owner.PerformLayout (control, "Parent");
}
#if NET_2_0
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
}
if (this.InvokeRequired) {
- if (Application.MessageLoop) {
- this.BeginInvokeInternal(new MethodInvoker(DestroyHandle), null, true);
+ if (Application.MessageLoop && IsHandleCreated) {
+ this.BeginInvokeInternal(new MethodInvoker(DestroyHandle), null);
}
} else {
DestroyHandle();
}
#endif
- internal IAsyncResult BeginInvokeInternal (Delegate method, object [] args, bool disposing) {
+ internal IAsyncResult BeginInvokeInternal (Delegate method, object [] args) {
+ return BeginInvokeInternal (method, args, FindControlToInvokeOn ());
+ }
+
+ internal IAsyncResult BeginInvokeInternal (Delegate method, object [] args, Control control) {
AsyncMethodResult result;
AsyncMethodData data;
- if (!disposing) {
- Control p = this;
- do {
- if (!p.IsHandleCreated)
- throw new InvalidOperationException("Cannot call Invoke or BeginInvoke on a control until the window handle is created");
- p = p.parent;
- } while (p != null);
- }
-
result = new AsyncMethodResult ();
data = new AsyncMethodData ();
- data.Handle = window.Handle;
+ data.Handle = control.Handle;
data.Method = method;
data.Args = args;
data.Result = result;
return bmp_g;
}
}
+
+ private Control FindControlToInvokeOn ()
+ {
+ Control p = this;
+ do {
+ if (p.IsHandleCreated)
+ break;
+ p = p.parent;
+ } while (p != null);
+
+ if (p == null || !p.IsHandleCreated)
+ throw new InvalidOperationException ("Cannot call Invoke or BeginInvoke on a control until the window handle is created");
+
+ return p;
+ }
private void InvalidateBackBuffer () {
if (backbuffer != null)
// set.
if (attrs != null && attrs.Length > 0)
a = (AssemblyProductAttribute) attrs [0];
+ if (a == null) {
+ return GetType ().Namespace;
+ }
return a.Product;
}
}
object [] prms = null;
if (method is EventHandler)
prms = new object [] { this, EventArgs.Empty };
- return BeginInvokeInternal(method, prms, false);
+ return BeginInvokeInternal(method, prms);
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
public IAsyncResult BeginInvoke (Delegate method, object[] args)
#endif
{
- return BeginInvokeInternal (method, args, false);
+ return BeginInvokeInternal (method, args);
}
public void BringToFront() {
return;
}
+ if (!is_visible) {
+ return;
+ }
+
if (!IsHandleCreated) {
CreateHandle();
}
if (!is_created) {
is_created = true;
- }
- if (binding_context == null) { // seem to be sent whenever it's null?
- OnBindingContextChanged(EventArgs.Empty);
- }
+ if (binding_context == null) { // seem to be sent whenever it's null?
+ OnBindingContextChanged(EventArgs.Empty);
+ }
- OnCreateControl();
+ OnCreateControl();
+ }
}
public Graphics CreateGraphics() {
[EditorBrowsable (EditorBrowsableState.Advanced)]
#endif
public bool Focus() {
- if (CanFocus && IsHandleCreated && !has_focus && !is_focusing) {
+ return FocusInternal (false);
+ }
+
+ internal virtual bool FocusInternal (bool skip_check) {
+ if (skip_check || (CanFocus && IsHandleCreated && !has_focus && !is_focusing)) {
is_focusing = true;
Select(this);
is_focusing = false;
return has_focus;
}
- internal void FocusInternal () {
- is_focusing = true;
- Select(this);
- is_focusing = false;
- }
-
public Control GetChildAtPoint(Point pt) {
// MS's version causes the handle to be created. The stack trace shows that get_Handle is called here, but
// we'll just call CreateHandle instead.
return Invoke(method, prms);
}
-
+#if NET_2_0
+ public object Invoke (Delegate method, params object [] args) {
+#else
public object Invoke (Delegate method, object[] args) {
- Control p = this;
- do {
- if (!p.IsHandleCreated)
- throw new InvalidOperationException("Cannot call Invoke or BeginInvoke on a control until the window handle is created");
- p = p.parent;
- } while (p != null);
+#endif
+ Control control = FindControlToInvokeOn ();
if (!this.InvokeRequired) {
return method.DynamicInvoke(args);
}
- IAsyncResult result = BeginInvoke (method, args);
+ IAsyncResult result = BeginInvokeInternal (method, args, control);
return EndInvoke(result);
}
return;
}
- window.CreateHandle(CreateParams);
+ CreateParams create_params = CreateParams;
+ window.CreateHandle(create_params);
if (window.Handle != IntPtr.Zero) {
creator_thread = Thread.CurrentThread;
throw new ArgumentException ("Cannot change toplevel style of a parented control.");
}
- // XXX MS.NET causes handle to be created here
- CreateHandle ();
-
if (this is Form) {
- if (value == true) {
- if (!Visible) {
- Visible = true;
- }
- } else {
- if (Visible) {
- Visible = false;
- }
+ if (IsHandleCreated && value != Visible) {
+ Visible = value;
}
+ } else {
+ // XXX MS.NET causes handle to be created here
+ CreateHandle ();
}
is_toplevel = value;
}
private void WmPaint (ref Message m) {
PaintEventArgs paint_event;
- paint_event = XplatUI.PaintEventStart(Handle, true);
+ IntPtr handle = Handle;
+
+ paint_event = XplatUI.PaintEventStart (handle, true);
- if (paint_event == null) {
+ if (paint_event == null)
return;
- }
+
DoubleBuffer current_buffer = null;
if (UseDoubleBuffering) {
current_buffer = GetBackBuffer ();
if (!current_buffer.InvalidRegion.IsVisible (paint_event.ClipRectangle)) {
// Just blit the previous image
current_buffer.Blit (paint_event);
- XplatUI.PaintEventEnd (Handle, true);
+ XplatUI.PaintEventEnd (handle, true);
return;
}
current_buffer.Start (paint_event);
}
if (!GetStyle(ControlStyles.Opaque)) {
- OnPaintBackground(paint_event);
+ OnPaintBackground (paint_event);
}
// Button-derived controls choose to ignore their Opaque style, give them a chance to draw their background anyways
- OnPaintBackgroundInternal(paint_event);
+ OnPaintBackgroundInternal (paint_event);
OnPaintInternal(paint_event);
if (!paint_event.Handled) {
- OnPaint(paint_event);
+ OnPaint (paint_event);
}
if (current_buffer != null) {
}
- XplatUI.PaintEventEnd(Handle, true);
-
+ XplatUI.PaintEventEnd (handle, true);
}
private void WmEraseBackground (ref Message m) {
private void WmShowWindow (ref Message m) {
if (m.WParam.ToInt32() != 0) {
+ CreateControl ();
/* if we're being shown, make sure our child controls all have their handles created */
Control [] controls = child_controls.GetAllControls ();
for (int i=0; i<controls.Length; i++) {
}
}
- if (is_toplevel) /* XXX make sure this works for mdi forms */
+ if (is_toplevel || (this is Form && ((Form) this).IsMdiChild)) /* XXX make sure this works for mdi forms */
OnVisibleChanged(EventArgs.Empty);
}