Form.cs 23 KB

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