Form.cs 49 KB

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