Form.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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-2005 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 form_border_style;
  45. private bool autoscale;
  46. private bool clientsize_set;
  47. private Size autoscale_base_size;
  48. private bool allow_transparency;
  49. private static Icon default_icon;
  50. internal bool is_modal;
  51. private FormWindowState window_state;
  52. private bool control_box;
  53. private bool minimize_box;
  54. private bool maximize_box;
  55. private bool help_button;
  56. private bool show_in_taskbar;
  57. private bool topmost;
  58. private IButtonControl accept_button;
  59. private IButtonControl cancel_button;
  60. private DialogResult dialog_result;
  61. private FormStartPosition start_position;
  62. private Form owner;
  63. private Form.ControlCollection owned_forms;
  64. private MdiClient mdi_container;
  65. private InternalWindowManager window_manager;
  66. private Form mdi_parent;
  67. private bool key_preview;
  68. private MainMenu menu;
  69. private Icon icon;
  70. private Size maximum_size;
  71. private Size minimum_size;
  72. private SizeGripStyle size_grip_style;
  73. private Rectangle maximized_bounds;
  74. private Rectangle default_maximized_bounds;
  75. private double opacity;
  76. internal ApplicationContext context;
  77. Color transparency_key;
  78. internal MenuTracker active_tracker;
  79. #endregion // Local Variables
  80. #region Private & Internal Methods
  81. static Form ()
  82. {
  83. default_icon = (Icon)Locale.GetResource("mono.ico");
  84. }
  85. #endregion // Private & Internal Methods
  86. #region Public Classes
  87. public new class ControlCollection : Control.ControlCollection {
  88. Form form_owner;
  89. public ControlCollection(Form owner) : base(owner) {
  90. this.form_owner = owner;
  91. }
  92. public override void Add(Control value) {
  93. if (Contains (value))
  94. return;
  95. AddToList (value);
  96. ((Form)value).owner=(Form)owner;
  97. }
  98. public override void Remove(Control value) {
  99. ((Form)value).owner = null;
  100. base.Remove (value);
  101. }
  102. }
  103. #endregion // Public Classes
  104. #region Public Constructor & Destructor
  105. public Form ()
  106. {
  107. SizeF current_scale = GetAutoScaleSize (DeviceContext, Font);
  108. autoscale = true;
  109. autoscale_base_size = new Size ((int)current_scale.Width, (int) current_scale.Height);
  110. allow_transparency = false;
  111. closing = false;
  112. is_modal = false;
  113. dialog_result = DialogResult.None;
  114. start_position = FormStartPosition.WindowsDefaultLocation;
  115. form_border_style = FormBorderStyle.Sizable;
  116. window_state = FormWindowState.Normal;
  117. key_preview = false;
  118. opacity = 1D;
  119. menu = null;
  120. icon = default_icon;
  121. minimum_size = Size.Empty;
  122. maximum_size = Size.Empty;
  123. control_box = true;
  124. minimize_box = true;
  125. maximize_box = true;
  126. help_button = false;
  127. show_in_taskbar = true;
  128. ime_mode = ImeMode.NoControl;
  129. is_visible = false;
  130. is_toplevel = true;
  131. size_grip_style = SizeGripStyle.Auto;
  132. maximized_bounds = Rectangle.Empty;
  133. default_maximized_bounds = Rectangle.Empty;
  134. owned_forms = new Form.ControlCollection(this);
  135. transparency_key = Color.Empty;
  136. }
  137. #endregion // Public Constructor & Destructor
  138. #region Public Static Properties
  139. public static Form ActiveForm {
  140. get {
  141. Control active;
  142. active = FromHandle(XplatUI.GetActive());
  143. if (active != null) {
  144. if ( !(active is Form)) {
  145. Control parent;
  146. parent = active.Parent;
  147. while (parent != null) {
  148. if (parent is Form) {
  149. return (Form)parent;
  150. }
  151. parent = parent.Parent;
  152. }
  153. } else {
  154. return (Form)active;
  155. }
  156. }
  157. return null;
  158. }
  159. }
  160. #endregion // Public Static Properties
  161. #region Public Instance Properties
  162. [DefaultValue(null)]
  163. public IButtonControl AcceptButton {
  164. get {
  165. return accept_button;
  166. }
  167. set {
  168. accept_button = value;
  169. }
  170. }
  171. [Browsable(false)]
  172. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  173. public bool AllowTransparency {
  174. get {
  175. return allow_transparency;
  176. }
  177. set {
  178. if (XplatUI.SupportsTransparency()) {
  179. allow_transparency = value;
  180. if (value) {
  181. XplatUI.SetWindowTransparency(Handle, Opacity, TransparencyKey);
  182. } else {
  183. UpdateStyles(); // Remove the WS_EX_LAYERED style
  184. }
  185. }
  186. }
  187. }
  188. [DefaultValue(true)]
  189. public bool AutoScale {
  190. get {
  191. return autoscale;
  192. }
  193. set {
  194. autoscale = value;
  195. }
  196. }
  197. [Localizable(true)]
  198. [Browsable(false)]
  199. [EditorBrowsable(EditorBrowsableState.Advanced)]
  200. public virtual Size AutoScaleBaseSize {
  201. get {
  202. return autoscale_base_size;
  203. }
  204. set {
  205. autoscale_base_size = value;
  206. }
  207. }
  208. [Localizable(true)]
  209. public override bool AutoScroll {
  210. get {
  211. return base.AutoScroll;
  212. }
  213. set {
  214. base.AutoScroll = value;
  215. }
  216. }
  217. public override Color BackColor {
  218. get {
  219. return base.BackColor;
  220. }
  221. set {
  222. base.BackColor = value;
  223. }
  224. }
  225. [DefaultValue(null)]
  226. public IButtonControl CancelButton {
  227. get {
  228. return cancel_button;
  229. }
  230. set {
  231. cancel_button = value;
  232. }
  233. }
  234. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  235. [Localizable(true)]
  236. public Size ClientSize {
  237. get {
  238. return base.ClientSize;
  239. }
  240. set {
  241. base.ClientSize = value;
  242. }
  243. }
  244. [DefaultValue(true)]
  245. public bool ControlBox {
  246. get {
  247. return control_box;
  248. }
  249. set {
  250. if (control_box != value) {
  251. control_box = value;
  252. UpdateStyles();
  253. }
  254. }
  255. }
  256. [Browsable(false)]
  257. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  258. public Rectangle DesktopBounds {
  259. get {
  260. return new Rectangle(Location, Size);
  261. }
  262. set {
  263. Bounds = value;
  264. }
  265. }
  266. [Browsable(false)]
  267. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  268. public Point DesktopLocation {
  269. get {
  270. return Location;
  271. }
  272. set {
  273. Location = value;
  274. }
  275. }
  276. [Browsable(false)]
  277. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  278. public DialogResult DialogResult {
  279. get {
  280. return dialog_result;
  281. }
  282. set {
  283. dialog_result = value;
  284. if (is_modal) {
  285. closing = true;
  286. }
  287. }
  288. }
  289. [DefaultValue(FormBorderStyle.Sizable)]
  290. [DispId(-504)]
  291. public FormBorderStyle FormBorderStyle {
  292. get {
  293. return form_border_style;
  294. }
  295. set {
  296. form_border_style = value;
  297. if (window_manager == null) {
  298. if (IsHandleCreated) {
  299. XplatUI.SetBorderStyle(window.Handle, form_border_style);
  300. }
  301. if (value == FormBorderStyle.FixedToolWindow ||
  302. value == FormBorderStyle.SizableToolWindow)
  303. window_manager = new InternalWindowManager (this);
  304. } else {
  305. window_manager.UpdateBorderStyle (value);
  306. }
  307. UpdateStyles();
  308. }
  309. }
  310. [DefaultValue(false)]
  311. public bool HelpButton {
  312. get {
  313. return help_button;
  314. }
  315. set {
  316. if (help_button != value) {
  317. help_button = value;
  318. UpdateStyles();
  319. }
  320. }
  321. }
  322. [Localizable(true)]
  323. [AmbientValue(null)]
  324. public Icon Icon {
  325. get {
  326. return icon;
  327. }
  328. set {
  329. if (icon != value) {
  330. icon = value;
  331. if (IsHandleCreated) {
  332. XplatUI.SetIcon(Handle, icon == null ? default_icon : icon);
  333. }
  334. }
  335. }
  336. }
  337. [Browsable(false)]
  338. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  339. public bool IsMdiChild {
  340. get {
  341. return mdi_parent != null;
  342. }
  343. }
  344. [DefaultValue(false)]
  345. public bool IsMdiContainer {
  346. get {
  347. return mdi_container != null;
  348. }
  349. set {
  350. if (value && mdi_container == null) {
  351. mdi_container = new MdiClient();
  352. Controls.Add(mdi_container);
  353. } else if (!value && mdi_container != null) {
  354. Controls.Remove(mdi_container);
  355. mdi_container.Dispose();
  356. }
  357. }
  358. }
  359. [Browsable(false)]
  360. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  361. public Form ActiveMdiChild {
  362. get {
  363. if (!IsMdiContainer)
  364. return null;
  365. return (Form) mdi_container.ActiveMdiChild;
  366. }
  367. }
  368. [Browsable(false)]
  369. [EditorBrowsable(EditorBrowsableState.Advanced)]
  370. public bool IsRestrictedWindow {
  371. get {
  372. return false;
  373. }
  374. }
  375. [DefaultValue(false)]
  376. public bool KeyPreview {
  377. get {
  378. return key_preview;
  379. }
  380. set {
  381. key_preview = value;
  382. }
  383. }
  384. [DefaultValue(true)]
  385. public bool MaximizeBox {
  386. get {
  387. return maximize_box;
  388. }
  389. set {
  390. if (maximize_box != value) {
  391. maximize_box = value;
  392. if (IsHandleCreated) {
  393. RecreateHandle();
  394. }
  395. UpdateStyles();
  396. }
  397. }
  398. }
  399. [DefaultValue("{Width=0, Height=0}")]
  400. [Localizable(true)]
  401. [RefreshProperties(RefreshProperties.Repaint)]
  402. public Size MaximumSize {
  403. get {
  404. return maximum_size;
  405. }
  406. set {
  407. if (maximum_size != value) {
  408. maximum_size = value;
  409. OnMaximumSizeChanged(EventArgs.Empty);
  410. if (IsHandleCreated) {
  411. XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
  412. }
  413. }
  414. }
  415. }
  416. [Browsable(false)]
  417. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  418. public Form[] MdiChildren {
  419. get {
  420. if (mdi_container != null) {
  421. Form[] form_list;
  422. form_list = new Form[mdi_container.Controls.Count];
  423. for (int i = 0; i < mdi_container.Controls.Count; i++) {
  424. form_list[i] = (Form)mdi_container.Controls[i];
  425. }
  426. return form_list;
  427. } else {
  428. return new Form[0];
  429. }
  430. }
  431. }
  432. [MonoTODO("Finish setter")]
  433. [Browsable(false)]
  434. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  435. public Form MdiParent {
  436. get {
  437. return mdi_parent;
  438. }
  439. set {
  440. SuspendLayout ();
  441. // TopLevel = true;
  442. if (!value.IsMdiContainer)
  443. throw new ArgumentException ();
  444. if (mdi_parent != null) {
  445. mdi_parent.MdiContainer.Controls.Remove (this);
  446. }
  447. mdi_parent = value;
  448. if (mdi_parent != null) {
  449. window_manager = new MdiWindowManager (this,
  450. mdi_parent.MdiContainer);
  451. mdi_parent.MdiContainer.Controls.Add (this);
  452. }
  453. ResumeLayout ();
  454. }
  455. }
  456. internal MenuTracker ActiveTracker {
  457. get { return active_tracker; }
  458. set {
  459. if (value == active_tracker)
  460. return;
  461. Capture = value != null;
  462. active_tracker = value;
  463. }
  464. }
  465. internal MdiClient MdiContainer {
  466. get { return mdi_container; }
  467. }
  468. internal InternalWindowManager WindowManager {
  469. get { return window_manager; }
  470. }
  471. [DefaultValue(null)]
  472. public MainMenu Menu {
  473. get {
  474. return menu;
  475. }
  476. set {
  477. if (menu != value) {
  478. menu = value;
  479. if (menu != null && !IsMdiChild) {
  480. menu.SetForm (this);
  481. if (IsHandleCreated) {
  482. XplatUI.SetMenu (window.Handle, menu);
  483. }
  484. if (clientsize_set) {
  485. SetClientSizeCore(client_size.Width, client_size.Height);
  486. } else {
  487. UpdateBounds (bounds.X, bounds.Y, bounds.Width, bounds.Height, ClientSize.Width, ClientSize.Height -
  488. ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width));
  489. }
  490. } else
  491. UpdateBounds ();
  492. }
  493. }
  494. }
  495. public MainMenu MergedMenu {
  496. get {
  497. if (!IsMdiChild || window_manager == null)
  498. return null;
  499. return ((MdiWindowManager) window_manager).MergedMenu;
  500. }
  501. }
  502. // This is the menu in display and being used because of merging this can
  503. // be different then the menu that is actually assosciated with the form
  504. internal MainMenu ActiveMenu {
  505. get {
  506. if (IsMdiChild)
  507. return null;
  508. Form amc = ActiveMdiChild;
  509. if (amc == null || amc.Menu == null)
  510. return menu;
  511. return amc.MergedMenu;
  512. }
  513. }
  514. [DefaultValue(true)]
  515. public bool MinimizeBox {
  516. get {
  517. return minimize_box;
  518. }
  519. set {
  520. if (minimize_box != value) {
  521. minimize_box = value;
  522. if (IsHandleCreated) {
  523. RecreateHandle();
  524. }
  525. UpdateStyles();
  526. }
  527. }
  528. }
  529. [DefaultValue("{Width=0, Height=0}")]
  530. [Localizable(true)]
  531. [RefreshProperties(RefreshProperties.Repaint)]
  532. public Size MinimumSize {
  533. get {
  534. return minimum_size;
  535. }
  536. set {
  537. if (minimum_size != value) {
  538. minimum_size = value;
  539. OnMinimumSizeChanged(EventArgs.Empty);
  540. if (IsHandleCreated) {
  541. XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
  542. }
  543. }
  544. }
  545. }
  546. [Browsable(false)]
  547. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  548. public bool Modal {
  549. get {
  550. return is_modal;
  551. }
  552. }
  553. [DefaultValue(1D)]
  554. [TypeConverter(typeof(OpacityConverter))]
  555. public double Opacity {
  556. get {
  557. return opacity;
  558. }
  559. set {
  560. opacity = value;
  561. AllowTransparency = true;
  562. UpdateStyles();
  563. XplatUI.SetWindowTransparency(Handle, opacity, TransparencyKey);
  564. }
  565. }
  566. [Browsable(false)]
  567. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  568. public Form[] OwnedForms {
  569. get {
  570. Form[] form_list;
  571. form_list = new Form[owned_forms.Count];
  572. for (int i=0; i<owned_forms.Count; i++) {
  573. form_list[i] = (Form)owned_forms[i];
  574. }
  575. return form_list;
  576. }
  577. }
  578. [Browsable(false)]
  579. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  580. public Form Owner {
  581. get {
  582. return owner;
  583. }
  584. set {
  585. if (owner != value) {
  586. if (owner != null) {
  587. owner.RemoveOwnedForm(this);
  588. }
  589. owner = value;
  590. owner.AddOwnedForm(this);
  591. if (owner != null) {
  592. XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
  593. } else {
  594. XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
  595. }
  596. }
  597. }
  598. }
  599. [DefaultValue(true)]
  600. public bool ShowInTaskbar {
  601. get {
  602. return show_in_taskbar;
  603. }
  604. set {
  605. if (show_in_taskbar != value) {
  606. show_in_taskbar = value;
  607. if (IsHandleCreated) {
  608. RecreateHandle();
  609. }
  610. UpdateStyles();
  611. }
  612. }
  613. }
  614. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  615. [Localizable(false)]
  616. public Size Size {
  617. get {
  618. return base.Size;
  619. }
  620. set {
  621. base.Size = value;
  622. }
  623. }
  624. [MonoTODO("Trigger something when GripStyle is set")]
  625. [DefaultValue(SizeGripStyle.Auto)]
  626. public SizeGripStyle SizeGripStyle {
  627. get {
  628. return size_grip_style;
  629. }
  630. set {
  631. size_grip_style = value;
  632. }
  633. }
  634. [DefaultValue(FormStartPosition.WindowsDefaultLocation)]
  635. [Localizable(true)]
  636. public FormStartPosition StartPosition {
  637. get {
  638. return start_position;
  639. }
  640. set {
  641. if (start_position == FormStartPosition.WindowsDefaultLocation) { // Only do this if it's not set yet
  642. start_position = value;
  643. if (IsHandleCreated) {
  644. switch(start_position) {
  645. case FormStartPosition.CenterParent: {
  646. CenterToParent();
  647. break;
  648. }
  649. case FormStartPosition.CenterScreen: {
  650. CenterToScreen();
  651. break;
  652. }
  653. default: {
  654. break;
  655. }
  656. }
  657. }
  658. }
  659. }
  660. }
  661. [Browsable(false)]
  662. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  663. [EditorBrowsable(EditorBrowsableState.Never)]
  664. public int TabIndex {
  665. get {
  666. return base.TabIndex;
  667. }
  668. set {
  669. base.TabIndex = value;
  670. }
  671. }
  672. [Browsable(false)]
  673. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  674. [EditorBrowsable(EditorBrowsableState.Advanced)]
  675. public bool TopLevel {
  676. get {
  677. return GetTopLevel();
  678. }
  679. set {
  680. if (!value && IsMdiContainer)
  681. throw new ArgumentException ("MDI Container forms must be top level.");
  682. SetTopLevel(value);
  683. }
  684. }
  685. [DefaultValue(false)]
  686. public bool TopMost {
  687. get {
  688. return topmost;
  689. }
  690. set {
  691. if (topmost != value) {
  692. topmost = value;
  693. if (IsHandleCreated)
  694. XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
  695. }
  696. }
  697. }
  698. public Color TransparencyKey {
  699. get {
  700. return transparency_key;
  701. }
  702. set {
  703. transparency_key = value;
  704. AllowTransparency = true;
  705. UpdateStyles();
  706. XplatUI.SetWindowTransparency(Handle, Opacity, transparency_key);
  707. }
  708. }
  709. [DefaultValue(FormWindowState.Normal)]
  710. public FormWindowState WindowState {
  711. get {
  712. if (IsHandleCreated) {
  713. try {
  714. window_state = XplatUI.GetWindowState(Handle);
  715. }
  716. catch(NotSupportedException) {
  717. }
  718. }
  719. return window_state;
  720. }
  721. set {
  722. window_state = value;
  723. if (IsHandleCreated) {
  724. try {
  725. XplatUI.SetWindowState(Handle, value);
  726. }
  727. catch(NotSupportedException) {
  728. }
  729. }
  730. }
  731. }
  732. #endregion // Public Instance Properties
  733. #region Protected Instance Properties
  734. [MonoTODO("Need to set start position properly")]
  735. protected override CreateParams CreateParams {
  736. get {
  737. CreateParams cp = new CreateParams ();
  738. cp.Caption = Text;
  739. cp.ClassName = XplatUI.DefaultClassName;
  740. cp.ClassStyle = 0;
  741. cp.Style = 0;
  742. cp.ExStyle = 0;
  743. cp.Param = 0;
  744. cp.Parent = IntPtr.Zero;
  745. // if (start_position == FormStartPosition.WindowsDefaultLocation) {
  746. cp.X = unchecked((int)0x80000000);
  747. cp.Y = unchecked((int)0x80000000);
  748. // } else {
  749. // cp.X = Left;
  750. // cp.Y = Top;
  751. // }
  752. cp.Width = Width;
  753. cp.Height = Height;
  754. cp.Style = (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS);
  755. if (IsMdiChild) {
  756. cp.Style |= (int)(WindowStyles.WS_CHILD | WindowStyles.WS_CAPTION);
  757. cp.Parent = Parent.Handle;
  758. cp.ExStyle |= (int) (WindowStyles.WS_EX_WINDOWEDGE | WindowStyles.WS_EX_MDICHILD);
  759. switch (FormBorderStyle) {
  760. case FormBorderStyle.None:
  761. break;
  762. case FormBorderStyle.FixedToolWindow:
  763. case FormBorderStyle.SizableToolWindow:
  764. cp.ExStyle |= (int) WindowStyles.WS_EX_TOOLWINDOW;
  765. goto default;
  766. default:
  767. cp.Style |= (int) WindowStyles.WS_OVERLAPPEDWINDOW;
  768. break;
  769. }
  770. } else {
  771. switch (FormBorderStyle) {
  772. case FormBorderStyle.Fixed3D: {
  773. cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
  774. cp.ExStyle |= (int)WindowStyles.WS_EX_CLIENTEDGE;
  775. break;
  776. }
  777. case FormBorderStyle.FixedDialog: {
  778. cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
  779. cp.ExStyle |= (int)(WindowStyles.WS_EX_DLGMODALFRAME);
  780. break;
  781. }
  782. case FormBorderStyle.FixedSingle: {
  783. cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
  784. break;
  785. }
  786. case FormBorderStyle.FixedToolWindow: {
  787. cp.Style |= (int)(WindowStyles.WS_CAPTION | WindowStyles.WS_BORDER);
  788. cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW);
  789. break;
  790. }
  791. case FormBorderStyle.Sizable: {
  792. cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
  793. break;
  794. }
  795. case FormBorderStyle.SizableToolWindow: {
  796. cp.Style |= (int)(WindowStyles.WS_BORDER | WindowStyles.WS_THICKFRAME | WindowStyles.WS_CAPTION);
  797. cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW);
  798. break;
  799. }
  800. case FormBorderStyle.None: {
  801. break;
  802. }
  803. }
  804. }
  805. switch(window_state) {
  806. case FormWindowState.Maximized: {
  807. cp.Style |= (int)WindowStyles.WS_MAXIMIZE;
  808. break;
  809. }
  810. case FormWindowState.Minimized: {
  811. cp.Style |= (int)WindowStyles.WS_MINIMIZE;
  812. break;
  813. }
  814. }
  815. if (TopMost) {
  816. cp.ExStyle |= (int) WindowStyles.WS_EX_TOPMOST;
  817. }
  818. if (ShowInTaskbar) {
  819. cp.ExStyle |= (int)WindowStyles.WS_EX_APPWINDOW;
  820. }
  821. if (MaximizeBox) {
  822. cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
  823. }
  824. if (MinimizeBox) {
  825. cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
  826. }
  827. if (ControlBox) {
  828. cp.Style |= (int)WindowStyles.WS_SYSMENU;
  829. }
  830. if (HelpButton && !MaximizeBox && !MinimizeBox) {
  831. cp.ExStyle |= (int)WindowStyles.WS_EX_CONTEXTHELP;
  832. }
  833. if (Visible)
  834. cp.Style |= (int)WindowStyles.WS_VISIBLE;
  835. if (Opacity < 1.0 || TransparencyKey != Color.Empty) {
  836. cp.ExStyle |= (int)WindowStyles.WS_EX_LAYERED;
  837. }
  838. if (!is_enabled && context == null) {
  839. cp.Style |= (int)(WindowStyles.WS_DISABLED);
  840. }
  841. return cp;
  842. }
  843. }
  844. protected override ImeMode DefaultImeMode {
  845. get {
  846. return ImeMode.NoControl;
  847. }
  848. }
  849. protected override Size DefaultSize {
  850. get {
  851. return new Size (250, 250);
  852. }
  853. }
  854. protected Rectangle MaximizedBounds {
  855. get {
  856. if (maximized_bounds != Rectangle.Empty) {
  857. return maximized_bounds;
  858. }
  859. return default_maximized_bounds;
  860. }
  861. set {
  862. maximized_bounds = value;
  863. OnMaximizedBoundsChanged(EventArgs.Empty);
  864. if (IsHandleCreated) {
  865. XplatUI.SetWindowMinMax(Handle, maximized_bounds, minimum_size, maximum_size);
  866. }
  867. }
  868. }
  869. #endregion // Protected Instance Properties
  870. #region Public Static Methods
  871. [EditorBrowsable(EditorBrowsableState.Advanced)]
  872. public static SizeF GetAutoScaleSize (Font font)
  873. {
  874. return XplatUI.GetAutoScaleSize(font);
  875. }
  876. #endregion // Public Static Methods
  877. #region Public Instance Methods
  878. internal SizeF GetAutoScaleSize (Graphics g, Font font)
  879. {
  880. //
  881. // The following constants come from the dotnet mailing list
  882. // discussion: http://discuss.develop.com/archives/wa.exe?A2=ind0203A&L=DOTNET&P=R3655
  883. //
  884. // The magic number is "Its almost the length
  885. // of the string with a smattering added in
  886. // for compat with earlier code".
  887. //
  888. string magic_string = "The quick brown fox jumped over the lazy dog.";
  889. double magic_number = 44.549996948242189;
  890. float width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
  891. return new SizeF (width, font.Height);
  892. }
  893. public void Activate() {
  894. Form active;
  895. // The docs say activate only activates if our app is already active
  896. active = ActiveForm;
  897. if ((active != null) && (this != active)) {
  898. XplatUI.Activate(window.Handle);
  899. }
  900. }
  901. public void AddOwnedForm(Form ownedForm) {
  902. owned_forms.Add(ownedForm);
  903. }
  904. public void Close () {
  905. if (!IsDisposed) {
  906. XplatUI.SendMessage(this.Handle, Msg.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
  907. }
  908. }
  909. public void LayoutMdi(MdiLayout value) {
  910. if (mdi_container != null) {
  911. mdi_container.LayoutMdi(value);
  912. }
  913. }
  914. public void RemoveOwnedForm(Form ownedForm) {
  915. owned_forms.Remove(ownedForm);
  916. }
  917. public void SetDesktopBounds(int x, int y, int width, int height) {
  918. DesktopBounds = new Rectangle(x, y, width, height);
  919. }
  920. public void SetDesktopLocation(int x, int y) {
  921. DesktopLocation = new Point(x, y);
  922. }
  923. public DialogResult ShowDialog() {
  924. return ShowDialog(null);
  925. }
  926. public DialogResult ShowDialog(IWin32Window ownerWin32) {
  927. Form previous;
  928. if (ownerWin32 != null) {
  929. owner = (Form)Control.FromHandle(ownerWin32.Handle);
  930. }
  931. if (owner == this) {
  932. throw new InvalidOperationException("The 'ownerWin32' cannot be the form being shown.");
  933. }
  934. if (is_modal) {
  935. throw new InvalidOperationException("The form is already displayed as a modal dialog.");
  936. }
  937. if (Visible) {
  938. throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
  939. }
  940. if (!Enabled) {
  941. throw new InvalidOperationException("Cannot display a disabled form as modal dialog.");
  942. }
  943. if (TopLevelControl != this) {
  944. throw new InvalidOperationException("Can only display TopLevel forms as modal dialog.");
  945. }
  946. #if broken
  947. // Can't do this, will screw us in the modal loop
  948. form_parent_window.Parent = this.owner;
  949. #endif
  950. previous = Form.ActiveForm;
  951. if (!IsHandleCreated) {
  952. CreateControl();
  953. }
  954. Application.RunLoop(true, new ApplicationContext(this));
  955. if (previous != null) {
  956. // Cannot use Activate(), it has a check for the current active window...
  957. XplatUI.Activate(previous.window.Handle);
  958. }
  959. return DialogResult;
  960. }
  961. public override string ToString() {
  962. return GetType().FullName.ToString() + ", Text: " + Text;
  963. }
  964. #endregion // Public Instance Methods
  965. #region Protected Instance Methods
  966. [MonoTODO("Finish when MDI is more complete")]
  967. protected void ActivateMdiChild(Form form) {
  968. OnMdiChildActivate(EventArgs.Empty);
  969. throw new NotImplementedException();
  970. }
  971. [EditorBrowsable(EditorBrowsableState.Advanced)]
  972. protected override void AdjustFormScrollbars(bool displayScrollbars) {
  973. base.AdjustFormScrollbars (displayScrollbars);
  974. }
  975. [EditorBrowsable(EditorBrowsableState.Advanced)]
  976. protected void ApplyAutoScaling()
  977. {
  978. SizeF current_size_f = GetAutoScaleSize (DeviceContext, Font);
  979. Size current_size = new Size ((int) current_size_f.Width, (int) current_size_f.Height);
  980. if (current_size == autoscale_base_size)
  981. return;
  982. if (Environment.GetEnvironmentVariable ("MONO_MWF_SCALING") == "disable"){
  983. Console.WriteLine ("Not scaling");
  984. return;
  985. }
  986. //
  987. // I tried applying the Fudge height factor from:
  988. // http://blogs.msdn.com/mharsh/archive/2004/01/25/62621.aspx
  989. // but it makes things larger without looking better.
  990. //
  991. Scale (current_size_f.Width / AutoScaleBaseSize.Width + 0.08f,
  992. current_size_f.Height / AutoScaleBaseSize.Height + 0.08f);
  993. AutoScaleBaseSize = current_size;
  994. }
  995. protected void CenterToParent() {
  996. Control ctl;
  997. int w;
  998. int h;
  999. if (Width > 0) {
  1000. w = Width;
  1001. } else {
  1002. w = DefaultSize.Width;
  1003. }
  1004. if (Height > 0) {
  1005. h = Height;
  1006. } else {
  1007. h = DefaultSize.Height;
  1008. }
  1009. ctl = null;
  1010. if (parent != null) {
  1011. ctl = parent;
  1012. } else if (owner != null) {
  1013. ctl = owner;
  1014. }
  1015. if (owner != null) {
  1016. this.Location = new Point(ctl.Left + ctl.Width / 2 - w /2, ctl.Top + ctl.Height / 2 - h / 2);
  1017. }
  1018. }
  1019. protected void CenterToScreen() {
  1020. Size DisplaySize;
  1021. int w;
  1022. int h;
  1023. if (Width > 0) {
  1024. w = Width;
  1025. } else {
  1026. w = DefaultSize.Width;
  1027. }
  1028. if (Height > 0) {
  1029. h = Height;
  1030. } else {
  1031. h = DefaultSize.Height;
  1032. }
  1033. XplatUI.GetDisplaySize(out DisplaySize);
  1034. this.Location = new Point(DisplaySize.Width / 2 - w / 2, DisplaySize.Height / 2 - h / 2);
  1035. }
  1036. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1037. protected override Control.ControlCollection CreateControlsInstance() {
  1038. return base.CreateControlsInstance ();
  1039. }
  1040. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1041. protected override void CreateHandle() {
  1042. base.CreateHandle ();
  1043. XplatUI.SetWindowMinMax(window.Handle, maximized_bounds, minimum_size, maximum_size);
  1044. if (icon != null) {
  1045. XplatUI.SetIcon(window.Handle, icon);
  1046. }
  1047. }
  1048. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1049. protected override void DefWndProc(ref Message m) {
  1050. base.DefWndProc (ref m);
  1051. }
  1052. protected override void Dispose(bool disposing) {
  1053. base.Dispose (disposing);
  1054. }
  1055. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1056. protected virtual void OnActivated(EventArgs e) {
  1057. if (Activated != null) {
  1058. Activated(this, e);
  1059. }
  1060. }
  1061. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1062. protected virtual void OnClosed(EventArgs e) {
  1063. if (Closed != null) {
  1064. Closed(this, e);
  1065. }
  1066. }
  1067. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1068. protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
  1069. if (Closing != null) {
  1070. Closing(this, e);
  1071. }
  1072. }
  1073. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1074. protected override void OnCreateControl() {
  1075. base.OnCreateControl ();
  1076. if (this.ActiveControl == null) {
  1077. bool visible;
  1078. // This visible hack is to work around CanSelect always being false if one of the parents
  1079. // is not visible; and we by default create Form invisible...
  1080. visible = this.is_visible;
  1081. this.is_visible = true;
  1082. if (SelectNextControl(this, true, true, true, true) == false) {
  1083. Select(this);
  1084. }
  1085. this.is_visible = visible;
  1086. }
  1087. if (!IsMdiChild) {
  1088. switch (StartPosition) {
  1089. case FormStartPosition.CenterScreen:
  1090. this.CenterToScreen();
  1091. break;
  1092. case FormStartPosition.CenterParent:
  1093. this.CenterToParent ();
  1094. break;
  1095. }
  1096. } else {
  1097. Left = 25 * MdiParent.MdiContainer.ChildrenCreated + 1;
  1098. Top = 25 * MdiParent.MdiContainer.ChildrenCreated + 1;
  1099. MdiParent.MdiContainer.ChildrenCreated++;
  1100. }
  1101. if (menu != null) {
  1102. XplatUI.SetMenu(window.Handle, menu);
  1103. }
  1104. OnLoad(EventArgs.Empty);
  1105. // Send initial location
  1106. OnLocationChanged(EventArgs.Empty);
  1107. }
  1108. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1109. protected virtual void OnDeactivate(EventArgs e) {
  1110. if (Deactivate != null) {
  1111. Deactivate(this, e);
  1112. }
  1113. }
  1114. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1115. protected override void OnFontChanged(EventArgs e) {
  1116. base.OnFontChanged (e);
  1117. }
  1118. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1119. protected override void OnHandleCreated(EventArgs e) {
  1120. XplatUI.SetBorderStyle(window.Handle, form_border_style);
  1121. base.OnHandleCreated (e);
  1122. }
  1123. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1124. protected override void OnHandleDestroyed(EventArgs e) {
  1125. base.OnHandleDestroyed (e);
  1126. }
  1127. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1128. protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
  1129. if (InputLanguageChanged!=null) {
  1130. InputLanguageChanged(this, e);
  1131. }
  1132. }
  1133. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1134. protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
  1135. if (InputLanguageChanging!=null) {
  1136. InputLanguageChanging(this, e);
  1137. }
  1138. }
  1139. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1140. protected virtual void OnLoad(EventArgs e) {
  1141. if (Load != null) {
  1142. Load(this, e);
  1143. }
  1144. if (AutoScale){
  1145. ApplyAutoScaling ();
  1146. AutoScale = false;
  1147. }
  1148. }
  1149. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1150. protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
  1151. if (MaximizedBoundsChanged != null) {
  1152. MaximizedBoundsChanged(this, e);
  1153. }
  1154. }
  1155. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1156. protected virtual void OnMaximumSizeChanged(EventArgs e) {
  1157. if (MaximumSizeChanged != null) {
  1158. MaximumSizeChanged(this, e);
  1159. }
  1160. }
  1161. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1162. protected virtual void OnMdiChildActivate(EventArgs e) {
  1163. if (MdiChildActivate != null) {
  1164. MdiChildActivate(this, e);
  1165. }
  1166. }
  1167. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1168. protected virtual void OnMenuComplete(EventArgs e) {
  1169. if (MenuComplete != null) {
  1170. MenuComplete(this, e);
  1171. }
  1172. }
  1173. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1174. protected virtual void OnMenuStart(EventArgs e) {
  1175. if (MenuStart != null) {
  1176. MenuStart(this, e);
  1177. }
  1178. }
  1179. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1180. protected virtual void OnMinimumSizeChanged(EventArgs e) {
  1181. if (MinimumSizeChanged != null) {
  1182. MinimumSizeChanged(this, e);
  1183. }
  1184. }
  1185. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1186. protected override void OnPaint (PaintEventArgs pevent) {
  1187. base.OnPaint (pevent);
  1188. }
  1189. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1190. protected override void OnResize(EventArgs e) {
  1191. base.OnResize(e);
  1192. }
  1193. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1194. protected override void OnStyleChanged(EventArgs e) {
  1195. base.OnStyleChanged (e);
  1196. }
  1197. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1198. protected override void OnTextChanged(EventArgs e) {
  1199. base.OnTextChanged (e);
  1200. }
  1201. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1202. protected override void OnVisibleChanged(EventArgs e) {
  1203. base.OnVisibleChanged (e);
  1204. }
  1205. protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
  1206. if (base.ProcessCmdKey (ref msg, keyData)) {
  1207. return true;
  1208. }
  1209. // Give our menu a shot
  1210. if (ActiveMenu != null) {
  1211. return ActiveMenu.ProcessCmdKey(ref msg, keyData);
  1212. }
  1213. return false;
  1214. }
  1215. // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
  1216. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1217. protected override bool ProcessDialogChar(char charCode) {
  1218. return base.ProcessDialogChar (charCode);
  1219. }
  1220. protected override bool ProcessDialogKey(Keys keyData) {
  1221. if ((keyData & Keys.Modifiers) == 0) {
  1222. if (keyData == Keys.Enter && accept_button != null) {
  1223. accept_button.PerformClick();
  1224. return true;
  1225. } else if (keyData == Keys.Escape && cancel_button != null) {
  1226. cancel_button.PerformClick();
  1227. return true;
  1228. }
  1229. }
  1230. return base.ProcessDialogKey(keyData);
  1231. }
  1232. protected override bool ProcessKeyPreview(ref Message msg) {
  1233. if (key_preview) {
  1234. if (ProcessKeyEventArgs(ref msg)) {
  1235. return true;
  1236. }
  1237. }
  1238. return base.ProcessKeyPreview (ref msg);
  1239. }
  1240. protected override bool ProcessTabKey(bool forward) {
  1241. return SelectNextControl(ActiveControl, forward, true, true, true);
  1242. }
  1243. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1244. protected override void ScaleCore(float dx, float dy) {
  1245. base.ScaleCore (dx, dy);
  1246. }
  1247. protected override void Select(bool directed, bool forward) {
  1248. Form parent;
  1249. if (directed) {
  1250. base.SelectNextControl(null, forward, true, true, true);
  1251. }
  1252. parent = this.ParentForm;
  1253. if (parent != null) {
  1254. parent.ActiveControl = this;
  1255. }
  1256. Activate();
  1257. }
  1258. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1259. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  1260. base.SetBoundsCore (x, y, width, height, specified);
  1261. }
  1262. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1263. protected override void SetClientSizeCore(int x, int y) {
  1264. if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
  1265. x = minimum_size.Width;
  1266. } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
  1267. x = maximum_size.Width;
  1268. }
  1269. if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
  1270. y = minimum_size.Height;
  1271. } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
  1272. y = maximum_size.Height;
  1273. }
  1274. Rectangle ClientRect = new Rectangle(0, 0, x, y);
  1275. Rectangle WindowRect;
  1276. CreateParams cp = this.CreateParams;
  1277. clientsize_set = true;
  1278. if (XplatUI.CalculateWindowRect(Handle, ref ClientRect, cp.Style, cp.ExStyle, ActiveMenu, out WindowRect)) {
  1279. SetBoundsCore(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
  1280. }
  1281. }
  1282. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1283. protected override void SetVisibleCore(bool value) {
  1284. base.SetVisibleCore (value);
  1285. }
  1286. protected override void UpdateDefaultButton() {
  1287. base.UpdateDefaultButton ();
  1288. }
  1289. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1290. protected override void WndProc(ref Message m) {
  1291. if (window_manager != null && window_manager.HandleMessage (ref m)) {
  1292. return;
  1293. }
  1294. switch((Msg)m.Msg) {
  1295. case Msg.WM_DESTROY: {
  1296. base.WndProc(ref m);
  1297. if (!is_recreating) {
  1298. this.closing = true;
  1299. }
  1300. return;
  1301. }
  1302. case Msg.WM_CLOSE_INTERNAL: {
  1303. DestroyHandle();
  1304. break;
  1305. }
  1306. case Msg.WM_CLOSE: {
  1307. if (!is_modal) {
  1308. CancelEventArgs args = new CancelEventArgs ();
  1309. OnClosing (args);
  1310. if (!args.Cancel) {
  1311. OnClosed (EventArgs.Empty);
  1312. DestroyHandle();
  1313. }
  1314. return;
  1315. } else {
  1316. closing = true;
  1317. }
  1318. return;
  1319. }
  1320. case Msg.WM_WINDOWPOSCHANGED: {
  1321. if (WindowState != FormWindowState.Minimized) {
  1322. base.WndProc(ref m);
  1323. }
  1324. return;
  1325. }
  1326. case Msg.WM_ACTIVATE: {
  1327. if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
  1328. OnActivated(EventArgs.Empty);
  1329. } else {
  1330. OnDeactivate(EventArgs.Empty);
  1331. }
  1332. return;
  1333. }
  1334. case Msg.WM_KILLFOCUS: {
  1335. base.WndProc(ref m);
  1336. return;
  1337. }
  1338. case Msg.WM_SETFOCUS: {
  1339. if (ActiveControl != null && ActiveControl != this) {
  1340. ActiveControl.Focus();
  1341. return; // FIXME - do we need to run base.WndProc, even though we just changed focus?
  1342. }
  1343. base.WndProc(ref m);
  1344. return;
  1345. }
  1346. // Menu drawing
  1347. case Msg.WM_NCLBUTTONDOWN: {
  1348. if (ActiveMenu != null) {
  1349. ActiveMenu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
  1350. }
  1351. base.WndProc(ref m);
  1352. return;
  1353. }
  1354. case Msg.WM_NCMOUSEMOVE: {
  1355. if (ActiveMenu != null) {
  1356. ActiveMenu.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
  1357. }
  1358. base.WndProc(ref m);
  1359. return;
  1360. }
  1361. case Msg.WM_NCPAINT: {
  1362. if (ActiveMenu != null) {
  1363. PaintEventArgs pe;
  1364. Point pnt;
  1365. pe = XplatUI.PaintEventStart(Handle, false);
  1366. pnt = XplatUI.GetMenuOrigin(window.Handle);
  1367. ActiveMenu.Draw (pe, new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0));
  1368. XplatUI.PaintEventEnd(Handle, false);
  1369. }
  1370. base.WndProc(ref m);
  1371. return;
  1372. }
  1373. case Msg.WM_NCCALCSIZE: {
  1374. XplatUIWin32.NCCALCSIZE_PARAMS ncp;
  1375. if ((ActiveMenu != null) && (m.WParam == (IntPtr)1)) {
  1376. ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
  1377. // Adjust for menu
  1378. ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu, ClientSize.Width);
  1379. Marshal.StructureToPtr(ncp, m.LParam, true);
  1380. }
  1381. DefWndProc(ref m);
  1382. break;
  1383. }
  1384. case Msg.WM_MOUSEMOVE: {
  1385. if (active_tracker != null) {
  1386. MouseEventArgs args;
  1387. args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  1388. mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
  1389. active_tracker.OnMotion(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
  1390. break;
  1391. }
  1392. base.WndProc(ref m);
  1393. break;
  1394. }
  1395. case Msg.WM_LBUTTONDOWN:
  1396. case Msg.WM_MBUTTONDOWN:
  1397. case Msg.WM_RBUTTONDOWN: {
  1398. if (active_tracker != null) {
  1399. MouseEventArgs args;
  1400. args = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  1401. mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0);
  1402. active_tracker.OnClick(new MouseEventArgs (args.Button, args.Clicks, Control.MousePosition.X, Control.MousePosition.Y, args.Delta));
  1403. return;
  1404. }
  1405. base.WndProc(ref m);
  1406. return;
  1407. }
  1408. case Msg.WM_GETMINMAXINFO: {
  1409. MINMAXINFO mmi;
  1410. if (m.LParam != IntPtr.Zero) {
  1411. mmi = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
  1412. default_maximized_bounds = new Rectangle(mmi.ptMaxPosition.x, mmi.ptMaxPosition.y, mmi.ptMaxSize.x, mmi.ptMaxSize.y);
  1413. if (maximized_bounds != Rectangle.Empty) {
  1414. mmi.ptMaxPosition.x = maximized_bounds.Left;
  1415. mmi.ptMaxPosition.y = maximized_bounds.Top;
  1416. mmi.ptMaxSize.x = maximized_bounds.Width;
  1417. mmi.ptMaxSize.y = maximized_bounds.Height;
  1418. }
  1419. if (minimum_size != Size.Empty) {
  1420. mmi.ptMinTrackSize.x = minimum_size.Width;
  1421. mmi.ptMinTrackSize.y = minimum_size.Height;
  1422. }
  1423. if (minimum_size != Size.Empty) {
  1424. mmi.ptMaxTrackSize.x = maximum_size.Width;
  1425. mmi.ptMaxTrackSize.y = maximum_size.Height;
  1426. }
  1427. Marshal.StructureToPtr(mmi, m.LParam, false);
  1428. }
  1429. break;
  1430. }
  1431. default: {
  1432. base.WndProc (ref m);
  1433. break;
  1434. }
  1435. }
  1436. }
  1437. #endregion // Protected Instance Methods
  1438. internal void RemoveWindowManager ()
  1439. {
  1440. window_manager = null;
  1441. }
  1442. #region Events
  1443. public event EventHandler Activated;
  1444. public event EventHandler Closed;
  1445. public event CancelEventHandler Closing;
  1446. public event EventHandler Deactivate;
  1447. public event InputLanguageChangedEventHandler InputLanguageChanged;
  1448. public event InputLanguageChangingEventHandler InputLanguageChanging;
  1449. public event EventHandler Load;
  1450. public event EventHandler MaximizedBoundsChanged;
  1451. public event EventHandler MaximumSizeChanged;
  1452. public event EventHandler MdiChildActivate;
  1453. public event EventHandler MenuComplete;
  1454. public event EventHandler MenuStart;
  1455. public event EventHandler MinimumSizeChanged;
  1456. [Browsable(false)]
  1457. [EditorBrowsable(EditorBrowsableState.Never)]
  1458. public new event EventHandler TabIndexChanged;
  1459. #endregion // Events
  1460. }
  1461. }