Form.cs 49 KB

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