CommonDialog.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. // NOT COMPLETE
  27. namespace System.Windows.Forms {
  28. public abstract class CommonDialog : System.ComponentModel.Component {
  29. #region DialogForm
  30. internal class DialogForm : Form {
  31. #region FormParentWindow Override
  32. internal new class FormParentWindow : Form.FormParentWindow {
  33. internal FormParentWindow(Form owner) : base(owner) {
  34. }
  35. protected override CreateParams CreateParams {
  36. get {
  37. CreateParams cp;
  38. if (owner != null) {
  39. owner.ControlBox = true;
  40. owner.MinimizeBox = false;
  41. owner.MaximizeBox = false;
  42. }
  43. cp = base.CreateParams;
  44. // FIXME - I need to rethink this a bit, we're loosing any cp.Style set in base; might not matter though
  45. cp.Style = (int)(WindowStyles.WS_POPUP | WindowStyles.WS_CAPTION | WindowStyles.WS_SYSMENU);
  46. return cp;
  47. }
  48. }
  49. }
  50. #endregion
  51. #region DialogForm Local Variables
  52. protected CommonDialog owner;
  53. #endregion DialogForm Local Variables
  54. #region DialogForm Constructors
  55. internal DialogForm(CommonDialog owner) {
  56. this.owner = owner;
  57. }
  58. #endregion DialogForm Constructors
  59. #region Protected Instance Properties
  60. protected override CreateParams CreateParams {
  61. get {
  62. if (this.form_parent_window == null) {
  63. form_parent_window = new FormParentWindow(this);
  64. }
  65. return base.CreateParams;
  66. }
  67. }
  68. protected override void WndProc(ref Message m) {
  69. base.WndProc (ref m);
  70. }
  71. #endregion // Protected Instance Properties
  72. #region Internal Methods
  73. internal DialogResult RunDialog () {
  74. this.StartPosition = FormStartPosition.CenterScreen;
  75. owner.InitFormsSize (this);
  76. this.ShowDialog ();
  77. return this.DialogResult;
  78. }
  79. #endregion Internal Methods
  80. }
  81. #endregion DialogForm
  82. #region Local Variables
  83. internal DialogForm form;
  84. #endregion Local Variables
  85. #region Public Constructors
  86. public CommonDialog() {
  87. form = new DialogForm(this);
  88. }
  89. #endregion Public Constructors
  90. #region Internal Methods
  91. internal virtual void InitFormsSize(Form form) {
  92. // Override this to set a default size for the form
  93. form.Width = 200;
  94. form.Height = 200;
  95. }
  96. #endregion Internal Methods
  97. #region Public Instance Methods
  98. public abstract void Reset();
  99. public DialogResult ShowDialog() {
  100. return ShowDialog(null);
  101. }
  102. public DialogResult ShowDialog(IWin32Window ownerWin32) {
  103. #if broken
  104. Control owner = null;
  105. if (ownerWin32 != null) {
  106. owner = Control.FromHandle(ownerWin32.Handle);
  107. }
  108. #endif
  109. RunDialog(form.Handle);
  110. if (form.Visible) {
  111. throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
  112. }
  113. if (!form.IsHandleCreated) {
  114. form.CreateControl();
  115. }
  116. #if broken
  117. form.form_parent_window.Parent = owner;
  118. #endif
  119. XplatUI.SetModal(form.form_parent_window.Handle, true);
  120. form.Show();
  121. form.is_modal = true;
  122. Application.ModalRun(form);
  123. form.is_modal = false;
  124. form.Hide();
  125. XplatUI.SetModal(form.form_parent_window.Handle, false);
  126. return form.DialogResult;
  127. }
  128. #endregion // Public Instance Methods
  129. #region Protected Instance Methods
  130. protected virtual IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam) {
  131. return IntPtr.Zero;
  132. }
  133. protected virtual void OnHelpRequest(EventArgs e) {
  134. if (HelpRequest != null) {
  135. HelpRequest(this, e);
  136. }
  137. }
  138. protected virtual IntPtr OwnerWndProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam) {
  139. return IntPtr.Zero;
  140. }
  141. protected abstract bool RunDialog(IntPtr hwndOwner);
  142. #endregion // Protected Instance Methods
  143. #region Events
  144. public event EventHandler HelpRequest;
  145. #endregion // Events
  146. }
  147. }