public DialogResult ShowDialog(IWin32Window owner) {
Rectangle area;
bool confined;
IntPtr capture_window;
Form owner_to_be = null;
if ((owner == null) && (Application.MWFThread.Current.Context != null)) {
IntPtr active = XplatUI.GetActive ();
if (active != IntPtr.Zero) {
owner = Control.FromHandle (active) as Form;
}
}
if (owner != null) {
Control c = Control.FromHandle (owner.Handle);
if (c != null)
owner_to_be = c.TopLevelControl as Form;
}
if (owner_to_be == this) {
throw new ArgumentException ("Forms cannot own themselves or their owners.", "owner");
}
if (is_modal) {
throw new InvalidOperationException ("The form is already displayed as a modal dialog.");
}
if (Visible) {
throw new InvalidOperationException ("Forms that are already "
+ " visible cannot be displayed as a modal dialog. Set the"
+ " form's visible property to false before calling"
+ " ShowDialog.");
}
if (!Enabled) {
throw new InvalidOperationException ("Forms that are not enabled"
+ " cannot be displayed as a modal dialog. Set the form's"
+ " enabled property to true before calling ShowDialog.");
}
if (TopLevelControl != this) {
throw new InvalidOperationException ("Forms that are not top level"
+ " forms cannot be displayed as a modal dialog. Remove the"
+ " form from any parent form before calling ShowDialog.");
}
if (owner_to_be != null)
this.owner = owner_to_be;
// If our owner is topmost, we better be too, or else we'll show up under our owner
if (this.owner != null && this.owner.TopMost)
this.TopMost = true;
#if broken
// Can't do this, will screw us in the modal loop
form_parent_window.Parent = this.owner;
#endif
// Release any captures
XplatUI.GrabInfo(out capture_window, out confined, out area);
if (capture_window != IntPtr.Zero) {
XplatUI.UngrabWindow(capture_window);
}
foreach (Form form in Application.OpenForms)
{
if (form.Enabled == true)
{
disabled_by_showdialog.Add(form);
form.Enabled = false;
}
}
modal_dialogs.Add(this);
#if not
// Commented out; we instead let the Visible=true inside the runloop create the control
// otherwise setting DialogResult inside any of the events that are triggered by the
// create will not actually cause the form to not be displayed.
// Leaving this comment here in case there was an actual purpose to creating the control
// in here.
if (!IsHandleCreated) {
CreateControl();
}
#endif
Application.RunLoop(true, new ApplicationContext(this));
if (this.owner != null) {
// Cannot use Activate(), it has a check for the current active window...
XplatUI.Activate(this.owner.window.Handle);
}
if (IsHandleCreated) {
DestroyHandle ();
}
if (DialogResult == DialogResult.None) {
DialogResult = DialogResult.Cancel;
}
return DialogResult;
}