Form.cs 44 KB

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