Form.cs 43 KB

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