Form.cs 49 KB

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