Form.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. // NOT COMPLETE
  26. using System;
  27. using System.Drawing;
  28. using System.ComponentModel;
  29. using System.Collections;
  30. using System.Runtime.InteropServices;
  31. using System.Threading;
  32. namespace System.Windows.Forms {
  33. public class Form : ContainerControl {
  34. #region Local Variables
  35. internal static Form active_form;
  36. internal bool closing;
  37. FormBorderStyle formBorderStyle;
  38. private static bool autoscale;
  39. private static Size autoscale_base_size;
  40. internal bool is_modal;
  41. internal bool end_modal; // This var is being monitored by the application modal loop
  42. private bool control_box;
  43. private bool minimize_box;
  44. private bool maximize_box;
  45. private bool help_button;
  46. private bool show_in_taskbar;
  47. private bool topmost;
  48. private IButtonControl accept_button;
  49. private IButtonControl cancel_button;
  50. private DialogResult dialog_result;
  51. private FormStartPosition start_position;
  52. private Form owner;
  53. private Form.ControlCollection owned_forms;
  54. private bool key_preview;
  55. private MainMenu menu;
  56. internal FormParentWindow form_parent_window;
  57. private Icon icon;
  58. private Size maximum_size;
  59. private Size minimum_size;
  60. #endregion // Local Variables
  61. #region Private Classes
  62. // This class will take over for the client area
  63. internal class FormParentWindow : Control {
  64. #region FormParentWindow Class Local Variables
  65. internal Form owner;
  66. #endregion // FormParentWindow Class Local Variables
  67. #region FormParentWindow Class Constructor
  68. internal FormParentWindow(Form owner) : base() {
  69. this.owner = owner;
  70. this.Width = 250;
  71. this.Height = 250;
  72. BackColor = owner.BackColor;
  73. Text = "FormParent";
  74. this.Location = new Point(0, 0);
  75. this.Dock = DockStyle.Fill;
  76. this.is_visible = false;
  77. // We must set this via the internal var, the SetTopLevel method will too much stuff
  78. is_toplevel = true;
  79. MouseDown += new MouseEventHandler (OnMouseDownForm);
  80. MouseMove += new MouseEventHandler (OnMouseMoveForm);
  81. owner.TextChanged += new EventHandler(OnFormTextChanged);
  82. }
  83. #endregion // FormParentWindow Class Constructor
  84. #region FormParentWindow Class Protected Instance Methods
  85. protected override CreateParams CreateParams {
  86. get {
  87. CreateParams cp;
  88. cp = base.CreateParams;
  89. cp.Style = (int)(WindowStyles.WS_OVERLAPPEDWINDOW |
  90. WindowStyles.WS_CLIPSIBLINGS |
  91. WindowStyles.WS_CLIPCHILDREN);
  92. cp.Width = 250;
  93. cp.Height = 250;
  94. if (owner != null) {
  95. if (owner.ShowInTaskbar) {
  96. cp.ExStyle |= (int)WindowStyles.WS_EX_APPWINDOW;
  97. }
  98. if (owner.MaximizeBox) {
  99. cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
  100. }
  101. if (owner.MinimizeBox) {
  102. cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
  103. }
  104. if (owner.ControlBox) {
  105. cp.Style |= (int)WindowStyles.WS_SYSMENU;
  106. }
  107. if (owner.HelpButton) {
  108. cp.ExStyle |= (int)WindowStyles.WS_EX_CONTEXTHELP;
  109. }
  110. } else {
  111. // Defaults
  112. cp.Style |= (int)(WindowStyles.WS_SYSMENU | WindowStyles.WS_MINIMIZEBOX | WindowStyles.WS_MAXIMIZEBOX);
  113. cp.ExStyle |= (int)WindowStyles.WS_EX_APPWINDOW;
  114. }
  115. return cp;
  116. }
  117. }
  118. protected override void OnResize(EventArgs e) {
  119. base.OnResize(e);
  120. //owner.SetBoundsCore(owner.Bounds.X, owner.Bounds.Y, ClientSize.Width, ClientSize.Height, BoundsSpecified.All);
  121. if (owner.menu == null) {
  122. owner.SetBoundsCore(0, 0, ClientSize.Width, ClientSize.Height, BoundsSpecified.All);
  123. } else {
  124. int menu_height;
  125. menu_height = MenuAPI.MenuBarCalcSize(DeviceContext, owner.Menu.menu_handle, ClientSize.Width);
  126. Invalidate (new Rectangle (0, 0, ClientSize.Width, menu_height));
  127. owner.SetBoundsCore(0, menu_height, ClientSize.Width, ClientSize.Height-menu_height, BoundsSpecified.All);
  128. }
  129. }
  130. protected override void OnPaint(PaintEventArgs pevent) {
  131. OnDrawMenu (pevent.Graphics);
  132. }
  133. protected override void WndProc(ref Message m) {
  134. switch((Msg)m.Msg) {
  135. case Msg.WM_CLOSE: {
  136. CancelEventArgs args = new CancelEventArgs();
  137. owner.OnClosing(args);
  138. if (!args.Cancel) {
  139. owner.OnClosed(EventArgs.Empty);
  140. owner.closing = true;
  141. base.WndProc(ref m);
  142. break;
  143. }
  144. break;
  145. }
  146. case Msg.WM_ACTIVATE: {
  147. if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
  148. owner.OnActivated(EventArgs.Empty);
  149. } else {
  150. owner.OnDeactivate(EventArgs.Empty);
  151. }
  152. break;
  153. }
  154. #if topmost_workaround
  155. case Msg.WM_ACTIVATE: {
  156. if (this.OwnedForms.Length>0) {
  157. XplatUI.SetZOrder(this.OwnedForms[0].window.Handle, this.window.Handle, false, false);
  158. }
  159. break;
  160. }
  161. #endif
  162. default: {
  163. base.WndProc (ref m);
  164. break;
  165. }
  166. }
  167. }
  168. #endregion // FormParentWindow Class Protected Instance Methods
  169. #region FormParentWindow Class Private Methods
  170. private void OnMouseDownForm (object sender, MouseEventArgs e) {
  171. if (owner.menu != null)
  172. owner.menu.OnMouseDown (owner, e);
  173. }
  174. private void OnMouseMoveForm (object sender, MouseEventArgs e) {
  175. if (owner.menu != null)
  176. owner.menu.OnMouseMove (owner, e);
  177. }
  178. private void OnDrawMenu (Graphics dc) {
  179. if (owner.menu != null) {
  180. Rectangle rect = new Rectangle (0,0, Width, 0);
  181. MenuAPI.DrawMenuBar (dc, owner.menu.Handle, rect);
  182. }
  183. }
  184. private void OnFormTextChanged(object sender, EventArgs e) {
  185. this.Text = ((Control)sender).Text;
  186. }
  187. #endregion // FormParentWindow Class Private Methods
  188. }
  189. #endregion // Private Classes
  190. #region Public Classes
  191. public new class ControlCollection : Control.ControlCollection {
  192. Form form_owner;
  193. public ControlCollection(Form owner) : base(owner) {
  194. this.form_owner = owner;
  195. }
  196. public override void Add(Control value) {
  197. for (int i=0; i<list.Count; i++) {
  198. if (list[i]==value) {
  199. // Do we need to do anything here?
  200. return;
  201. }
  202. }
  203. list.Add(value);
  204. ((Form)value).owner=(Form)owner;
  205. }
  206. public override void Remove(Control value) {
  207. ((Form)value).owner = null;
  208. base.Remove (value);
  209. }
  210. }
  211. #endregion // Public Classes
  212. #region Public Constructor & Destructor
  213. public Form() {
  214. closing = false;
  215. is_modal = false;
  216. end_modal = false;
  217. dialog_result = DialogResult.None;
  218. start_position = FormStartPosition.WindowsDefaultLocation;
  219. formBorderStyle = FormBorderStyle.Sizable;
  220. key_preview = false;
  221. menu = null;
  222. icon = null;
  223. minimum_size = new Size(0, 0);
  224. maximum_size = new Size(0, 0);
  225. control_box = true;
  226. minimize_box = true;
  227. maximize_box = true;
  228. help_button = false;
  229. show_in_taskbar = true;
  230. owned_forms = new Form.ControlCollection(this);
  231. }
  232. #endregion // Public Constructor & Destructor
  233. #region Public Static Properties
  234. public static Form ActiveForm {
  235. get {
  236. Control active;
  237. active = FromHandle(XplatUI.GetActive());
  238. if (active != null) {
  239. if ( !(active is Form)) {
  240. if (active is FormParentWindow) {
  241. return ((FormParentWindow)active).owner;
  242. } else {
  243. Control parent;
  244. parent = active.Parent;
  245. while (parent != null) {
  246. if (parent is Form) {
  247. return (Form)parent;
  248. }
  249. parent = parent.Parent;
  250. }
  251. }
  252. } else {
  253. return (Form)active;
  254. }
  255. }
  256. return null;
  257. }
  258. }
  259. #endregion // Public Static Properties
  260. #region Public Instance Properties
  261. public IButtonControl AcceptButton {
  262. get {
  263. return accept_button;
  264. }
  265. set {
  266. accept_button = value;
  267. }
  268. }
  269. public bool AutoScale {
  270. get {
  271. return autoscale;
  272. }
  273. set {
  274. autoscale=value;
  275. }
  276. }
  277. public virtual Size AutoScaleBaseSize {
  278. get {
  279. return autoscale_base_size;
  280. }
  281. set {
  282. autoscale_base_size=value;
  283. }
  284. }
  285. public IButtonControl CancelButton {
  286. get {
  287. return cancel_button;
  288. }
  289. set {
  290. cancel_button = value;
  291. }
  292. }
  293. public Size ClientSize {
  294. get {
  295. return base.ClientSize;
  296. }
  297. set {
  298. form_parent_window.ClientSize = value;
  299. }
  300. }
  301. public bool ControlBox {
  302. get {
  303. return control_box;
  304. }
  305. set {
  306. if (control_box != value) {
  307. control_box = value;
  308. }
  309. }
  310. }
  311. public Rectangle DesktopBounds {
  312. get {
  313. return new Rectangle(form_parent_window.Location, form_parent_window.Size);
  314. }
  315. set {
  316. this.form_parent_window.Bounds = value;
  317. }
  318. }
  319. public Point DesktopLocation {
  320. get {
  321. return form_parent_window.Location;
  322. }
  323. set {
  324. form_parent_window.Location = value;
  325. }
  326. }
  327. public DialogResult DialogResult {
  328. get {
  329. return dialog_result;
  330. }
  331. set {
  332. dialog_result = value;
  333. if (is_modal && (dialog_result != DialogResult.None)) {
  334. end_modal = true;
  335. }
  336. }
  337. }
  338. public FormBorderStyle FormBorderStyle {
  339. get {
  340. return formBorderStyle;
  341. }
  342. set {
  343. formBorderStyle = value;
  344. Invalidate ();
  345. }
  346. }
  347. public bool HelpButton {
  348. get {
  349. return help_button;
  350. }
  351. set {
  352. if (help_button != value) {
  353. help_button = value;
  354. }
  355. }
  356. }
  357. public Icon Icon {
  358. get {
  359. return icon;
  360. }
  361. set {
  362. if (icon != value) {
  363. icon = value;
  364. }
  365. }
  366. }
  367. public bool IsRestrictedWindow {
  368. get {
  369. return false;
  370. }
  371. }
  372. public bool KeyPreview {
  373. get {
  374. return key_preview;
  375. }
  376. set {
  377. key_preview = value;
  378. }
  379. }
  380. public bool MaximizeBox {
  381. get {
  382. return maximize_box;
  383. }
  384. set {
  385. if (maximize_box != value) {
  386. maximize_box = value;
  387. }
  388. }
  389. }
  390. public Size MaximumSize {
  391. get {
  392. return maximum_size;
  393. }
  394. set {
  395. if (maximum_size != value) {
  396. maximum_size = value;
  397. }
  398. }
  399. }
  400. public MainMenu Menu {
  401. get {
  402. return menu;
  403. }
  404. set {
  405. if (menu != value) {
  406. if (value == null) {
  407. form_parent_window.Width = form_parent_window.Width; // Trigger a resize
  408. }
  409. menu = value;
  410. // To simulate the non-client are for menus we create a
  411. // new control as the 'client area' of our form. This
  412. // way, the origin stays 0,0 and we don't have to fiddle with
  413. // coordinates. The menu area is part of the original container
  414. if (menu != null) {
  415. form_parent_window.Width = form_parent_window.Width; // Trigger a resize
  416. }
  417. menu.SetForm (this);
  418. MenuAPI.SetMenuBarWindow (menu.Handle, form_parent_window);
  419. }
  420. }
  421. }
  422. public bool MinimizeBox {
  423. get {
  424. return minimize_box;
  425. }
  426. set {
  427. if (minimize_box != value) {
  428. minimize_box = value;
  429. }
  430. }
  431. }
  432. public Size MinimumSize {
  433. get {
  434. return minimum_size;
  435. }
  436. set {
  437. if (minimum_size != value) {
  438. minimum_size = value;
  439. }
  440. }
  441. }
  442. public bool Modal {
  443. get {
  444. return is_modal;
  445. }
  446. }
  447. public Form[] OwnedForms {
  448. get {
  449. Form[] form_list;
  450. form_list = new Form[owned_forms.Count];
  451. for (int i=0; i<owned_forms.Count; i++) {
  452. form_list[i] = (Form)owned_forms[i];
  453. }
  454. return form_list;
  455. }
  456. }
  457. public Form Owner {
  458. get {
  459. return owner;
  460. }
  461. set {
  462. if (owner != value) {
  463. if (owner != null) {
  464. owner.RemoveOwnedForm(this);
  465. }
  466. owner = value;
  467. owner.AddOwnedForm(this);
  468. if (owner != null) {
  469. XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
  470. } else {
  471. XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
  472. }
  473. }
  474. }
  475. }
  476. public bool ShowInTaskbar {
  477. get {
  478. return show_in_taskbar;
  479. }
  480. set {
  481. if (show_in_taskbar != value) {
  482. show_in_taskbar = value;
  483. }
  484. }
  485. }
  486. public Size Size {
  487. get {
  488. return form_parent_window.Size;
  489. }
  490. set {
  491. form_parent_window.Size = value;
  492. }
  493. }
  494. public FormStartPosition StartPosition {
  495. get {
  496. return start_position;
  497. }
  498. set {
  499. if (start_position == FormStartPosition.WindowsDefaultLocation) { // Only do this if it's not set yet
  500. start_position = value;
  501. if (form_parent_window.IsHandleCreated) {
  502. switch(start_position) {
  503. case FormStartPosition.CenterParent: {
  504. if (Parent!=null && Width>0 && Height>0) {
  505. this.Location = new Point(Parent.Size.Width/2-Width/2, Parent.Size.Height/2-Height/2);
  506. }
  507. break;
  508. }
  509. case FormStartPosition.CenterScreen: {
  510. if (Width>0 && Height>0) {
  511. Size DisplaySize;
  512. XplatUI.GetDisplaySize(out DisplaySize);
  513. this.Location = new Point(DisplaySize.Width/2-Width/2, DisplaySize.Height/2-Height/2);
  514. }
  515. break;
  516. }
  517. default: {
  518. break;
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }
  525. public bool TopLevel {
  526. get {
  527. return GetTopLevel();
  528. }
  529. set {
  530. SetTopLevel(value);
  531. }
  532. }
  533. public bool TopMost {
  534. get {
  535. return topmost;
  536. }
  537. set {
  538. if (topmost != value) {
  539. topmost = value;
  540. XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
  541. }
  542. }
  543. }
  544. public FormWindowState WindowState {
  545. get {
  546. return XplatUI.GetWindowState(form_parent_window.window.Handle);;
  547. }
  548. set {
  549. XplatUI.SetWindowState(form_parent_window.window.Handle, value);
  550. }
  551. }
  552. #endregion // Public Instance Properties
  553. #region Protected Instance Properties
  554. [MonoTODO("Need to add MDI support")]
  555. protected override CreateParams CreateParams {
  556. get {
  557. CreateParams cp = new CreateParams();
  558. if (this.form_parent_window == null) {
  559. form_parent_window = new FormParentWindow(this);
  560. }
  561. cp.Caption = "ClientArea";
  562. cp.ClassName=XplatUI.DefaultClassName;
  563. cp.ClassStyle = 0;
  564. cp.ExStyle=0;
  565. cp.Param=0;
  566. cp.Parent = this.form_parent_window.window.Handle;
  567. cp.X = Left;
  568. cp.Y = Top;
  569. cp.Width = Width;
  570. cp.Height = Height;
  571. cp.Style = (int)WindowStyles.WS_CHILD;
  572. cp.Style |= (int)WindowStyles.WS_VISIBLE;
  573. cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
  574. cp.Style |= (int)WindowStyles.WS_CLIPCHILDREN;
  575. return cp;
  576. }
  577. }
  578. protected override Size DefaultSize {
  579. get {
  580. return new Size (250, 250);
  581. }
  582. }
  583. protected override void OnPaint (PaintEventArgs pevent)
  584. {
  585. base.OnPaint (pevent);
  586. }
  587. #endregion // Protected Instance Properties
  588. #region Public Static Methods
  589. #endregion // Public Static Methods
  590. #region Public Instance Methods
  591. public void Activate() {
  592. Form active;
  593. // The docs say activate only activates if our app is already active
  594. active = ActiveForm;
  595. if ((active != null) && (this != active)) {
  596. XplatUI.Activate(form_parent_window.window.Handle);
  597. }
  598. }
  599. public void AddOwnedForm(Form ownedForm) {
  600. owned_forms.Add(ownedForm);
  601. }
  602. public void RemoveOwnedForm(Form ownedForm) {
  603. owned_forms.Remove(ownedForm);
  604. }
  605. public void SetDesktopBounds(int x, int y, int width, int height) {
  606. DesktopBounds = new Rectangle(x, y, width, height);
  607. }
  608. public void SetDesktopLocation(int x, int y) {
  609. DesktopLocation = new Point(x, y);
  610. }
  611. public DialogResult ShowDialog() {
  612. return ShowDialog(null);
  613. }
  614. public DialogResult ShowDialog(IWin32Window ownerWin32) {
  615. Form previous;
  616. #if broken
  617. Control owner = null;
  618. if (ownerWin32 != null) {
  619. owner = Control.FromHandle(ownerWin32.Handle);
  620. }
  621. #endif
  622. if (is_modal) {
  623. return DialogResult.None;
  624. }
  625. if (Visible) {
  626. throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
  627. }
  628. #if broken
  629. // Can't do this, will screw us in the modal loop
  630. form_parent_window.Parent = owner;
  631. #endif
  632. previous = Form.ActiveForm;
  633. if (!IsHandleCreated) {
  634. CreateControl();
  635. }
  636. XplatUI.SetModal(form_parent_window.window.Handle, true);
  637. Show();
  638. is_modal = true;
  639. Application.ModalRun(this);
  640. is_modal = false;
  641. Hide();
  642. XplatUI.SetModal(form_parent_window.window.Handle, false);
  643. if (previous != null) {
  644. // Cannot use Activate(), it has a check for the current active window...
  645. XplatUI.Activate(previous.form_parent_window.window.Handle);
  646. }
  647. return DialogResult;
  648. }
  649. public void Close ()
  650. {
  651. CancelEventArgs args = new CancelEventArgs ();
  652. OnClosing (args);
  653. if (!args.Cancel) {
  654. OnClosed (EventArgs.Empty);
  655. closing = true;
  656. return;
  657. }
  658. }
  659. #endregion // Public Instance Methods
  660. #region Protected Instance Methods
  661. protected override void CreateHandle() {
  662. base.CreateHandle ();
  663. }
  664. protected override void OnCreateControl() {
  665. base.OnCreateControl ();
  666. OnLoad(EventArgs.Empty);
  667. }
  668. protected override void OnHandleCreated(EventArgs e) {
  669. base.OnHandleCreated (e);
  670. }
  671. protected override void OnHandleDestroyed(EventArgs e) {
  672. base.OnHandleDestroyed (e);
  673. }
  674. protected override void OnResize(EventArgs e) {
  675. base.OnResize(e);
  676. }
  677. protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
  678. if (base.ProcessCmdKey (ref msg, keyData)) {
  679. return true;
  680. }
  681. // Give our menu a shot
  682. if (menu != null) {
  683. return menu.ProcessCmdKey(ref msg, keyData);
  684. }
  685. return false;
  686. }
  687. protected override bool ProcessDialogKey(Keys keyData) {
  688. if (keyData == Keys.Enter && accept_button != null) {
  689. accept_button.PerformClick();
  690. return true;
  691. } else if (keyData == Keys.Enter && cancel_button != null) {
  692. cancel_button.PerformClick();
  693. return true;
  694. }
  695. return base.ProcessDialogKey(keyData);
  696. }
  697. protected override bool ProcessKeyPreview(ref Message msg) {
  698. if (key_preview) {
  699. if (ProcessKeyEventArgs(ref msg)) {
  700. return true;
  701. }
  702. }
  703. return base.ProcessKeyPreview (ref msg);
  704. }
  705. protected override void SetClientSizeCore(int x, int y) {
  706. if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
  707. x = minimum_size.Width;
  708. } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
  709. x = maximum_size.Width;
  710. }
  711. if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
  712. y = minimum_size.Height;
  713. } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
  714. y = maximum_size.Height;
  715. }
  716. base.SetClientSizeCore (x, y);
  717. }
  718. protected override void WndProc(ref Message m) {
  719. switch((Msg)m.Msg) {
  720. case Msg.WM_CLOSE: {
  721. CancelEventArgs args = new CancelEventArgs();
  722. OnClosing(args);
  723. if (!args.Cancel) {
  724. OnClosed(EventArgs.Empty);
  725. closing = true;
  726. base.WndProc(ref m);
  727. break;
  728. }
  729. break;
  730. }
  731. default: {
  732. base.WndProc (ref m);
  733. break;
  734. }
  735. }
  736. }
  737. #endregion // Protected Instance Methods
  738. #region Events
  739. protected virtual void OnActivated(EventArgs e) {
  740. if (Activated != null) {
  741. Activated(this, e);
  742. }
  743. }
  744. protected virtual void OnClosed(EventArgs e) {
  745. if (Closed != null) {
  746. Closed(this, e);
  747. }
  748. }
  749. protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
  750. if (Closing != null) {
  751. Closing(this, e);
  752. }
  753. }
  754. protected virtual void OnDeactivate(EventArgs e) {
  755. if (Deactivate != null) {
  756. Deactivate(this, e);
  757. }
  758. }
  759. protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
  760. if (InputLanguageChanged!=null) {
  761. InputLanguageChanged(this, e);
  762. }
  763. }
  764. protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
  765. if (InputLanguageChanging!=null) {
  766. InputLanguageChanging(this, e);
  767. }
  768. }
  769. protected virtual void OnLoad(EventArgs e) {
  770. if (Load != null) {
  771. Load(this, e);
  772. }
  773. }
  774. protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
  775. if (MaximizedBoundsChanged != null) {
  776. MaximizedBoundsChanged(this, e);
  777. }
  778. }
  779. protected virtual void OnMaximumSizeChanged(EventArgs e) {
  780. if (MaximumSizeChanged != null) {
  781. MaximumSizeChanged(this, e);
  782. }
  783. }
  784. protected virtual void OnMdiChildActivate(EventArgs e) {
  785. if (MdiChildActivate != null) {
  786. MdiChildActivate(this, e);
  787. }
  788. }
  789. protected virtual void OnMenuComplete(EventArgs e) {
  790. if (MenuComplete != null) {
  791. MenuComplete(this, e);
  792. }
  793. }
  794. protected virtual void OnMenuStart(EventArgs e) {
  795. if (MenuStart != null) {
  796. MenuStart(this, e);
  797. }
  798. }
  799. protected virtual void OnMinimumSizeChanged(EventArgs e) {
  800. if (MinimumSizeChanged != null) {
  801. MinimumSizeChanged(this, e);
  802. }
  803. }
  804. public event EventHandler Activated;
  805. public event EventHandler Closed;
  806. public event CancelEventHandler Closing;
  807. public event EventHandler Deactivate;
  808. public event InputLanguageChangedEventHandler InputLanguageChanged;
  809. public event InputLanguageChangingEventHandler InputLanguageChanging;
  810. public event EventHandler Load;
  811. public event EventHandler MaximizedBoundsChanged;
  812. public event EventHandler MaximumSizeChanged;
  813. public event EventHandler MdiChildActivate;
  814. public event EventHandler MenuComplete;
  815. public event EventHandler MenuStart;
  816. public event EventHandler MinimumSizeChanged;
  817. #endregion // Events
  818. }
  819. }