Control.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109
  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 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. // Based on work by:
  26. // Aleksey Ryabchuk [email protected]
  27. // Alexandre Pigolkine [email protected]
  28. // Dennis Hayes [email protected]
  29. // Jaak Simm [email protected]
  30. // John Sohn [email protected]
  31. //
  32. // $Revision: 1.23 $
  33. // $Modtime: $
  34. // $Log: Control.cs,v $
  35. // Revision 1.23 2004/08/11 22:20:59 pbartok
  36. // - Signature fixes
  37. //
  38. // Revision 1.22 2004/08/11 19:19:44 pbartok
  39. // - We had SetWindowPos and MoveWindow to set window positions and size,
  40. // removed MoveWindow. We have GetWindowPos, so it made sense to keep
  41. // SetWindowPos as matching counterpart
  42. // - Added some X11 sanity checking
  43. //
  44. // Revision 1.21 2004/08/11 18:59:45 pbartok
  45. // - Major cleanup of my SetBounds/SetBoundsCore/UpdateBounds mess
  46. // (It seems that SetBounds is just a front for SetBoundsCore and
  47. // SetBoundsCore updates the underlying window system and UpdateBounds is
  48. // responsible for updating the variables associated with the Control and
  49. // sending the events)
  50. // - Major cleanup of Size handling; we now have two sizes, client_size and
  51. // bounds. Bounds defines the window with decorations, client_size without
  52. // them.
  53. //
  54. // Revision 1.20 2004/08/11 15:13:32 pbartok
  55. // - Now properly reparents windows
  56. //
  57. // Revision 1.19 2004/08/11 14:37:11 pbartok
  58. // - Duh!
  59. //
  60. // Revision 1.18 2004/08/11 13:47:22 pbartok
  61. // - Rewrote the collection stuff. Might not be as fast now, not keeping
  62. // the number of children around and accessible directly, but it's more
  63. // straightforward
  64. //
  65. // Revision 1.17 2004/08/10 18:32:10 jordi
  66. // throw ontextchange event
  67. //
  68. // Revision 1.16 2004/08/10 17:43:04 pbartok
  69. // - Added more to the still unfinished Dock/Anchor layout code
  70. //
  71. // Revision 1.15 2004/08/10 15:08:05 jackson
  72. // Control will now handle the buffering code, so each control does not have to implement this.
  73. //
  74. // Revision 1.14 2004/08/09 22:11:25 pbartok
  75. // - Added incomplete dock layout code
  76. // - Added support for mouse wheel
  77. //
  78. // Revision 1.13 2004/08/09 17:25:56 jackson
  79. // Use new color names
  80. //
  81. // Revision 1.12 2004/08/09 15:54:51 jackson
  82. // Get default properties from the theme.
  83. //
  84. // Revision 1.11 2004/08/06 21:30:56 pbartok
  85. // - Fixed recursive loop when resizing
  86. // - Improved/fixed redrawing on expose messages
  87. //
  88. // Revision 1.10 2004/08/06 15:53:39 jordi
  89. // X11 keyboard navigation
  90. //
  91. // Revision 1.9 2004/08/04 21:14:26 pbartok
  92. // - Fixed Invalidation bug (calculated wrong client area)
  93. // - Added ClientSize setter
  94. //
  95. // Revision 1.8 2004/08/04 20:11:24 pbartok
  96. // - Added Invalidate handling
  97. //
  98. // Revision 1.7 2004/07/27 10:38:17 jordi
  99. // changes to be able to run winforms samples
  100. //
  101. // Revision 1.6 2004/07/19 19:09:42 jordi
  102. // label control re-written: added missing functionlity, events, and properties
  103. //
  104. // Revision 1.5 2004/07/19 16:49:23 jordi
  105. // fixes SetBounds logic
  106. //
  107. // Revision 1.4 2004/07/19 07:29:35 jordi
  108. // Call RefreshWindow only if the window has created
  109. //
  110. // Revision 1.3 2004/07/15 17:03:35 jordi
  111. // added basic mouse handeling events
  112. //
  113. // Revision 1.2 2004/07/13 15:31:45 jordi
  114. // commit: new properties and fixes form size problems
  115. //
  116. // Revision 1.1 2004/07/09 05:21:25 pbartok
  117. // - Initial check-in
  118. //
  119. //
  120. // NOT COMPLETE
  121. using System;
  122. using System.Drawing;
  123. using System.ComponentModel;
  124. using System.Collections;
  125. using System.Diagnostics;
  126. using System.Threading;
  127. using System.Runtime.InteropServices;
  128. namespace System.Windows.Forms
  129. {
  130. public class Control : Component, ISynchronizeInvoke, IWin32Window
  131. {
  132. #region Local Variables
  133. // Basic
  134. internal Rectangle bounds; // bounding rectangle for control (client area + decorations)
  135. internal object creator_thread; // thread that created the control
  136. internal ControlNativeWindow window; // object for native window handle
  137. internal string name; // for object naming
  138. // State
  139. internal bool has_focus; // true if control has focus
  140. internal bool is_visible; // true if control is visible
  141. internal bool is_enabled; // true if control is enabled (usable/not grayed out)
  142. internal int tab_index; // position in tab order of siblings
  143. internal bool is_disposed; // has the window already been disposed?
  144. internal Size client_size; // size of the client area (window excluding decorations)
  145. internal ControlStyles control_style; // rather win32-specific, style bits for control
  146. // Visuals
  147. internal Color foreground_color; // foreground color for control
  148. internal Color background_color; // background color for control
  149. internal Image background_image; // background image for control
  150. internal Font font; // font for control
  151. internal string text; // window/title text for control
  152. // Layout
  153. internal AnchorStyles anchor_style; // anchoring requirements for our control
  154. internal DockStyle dock_style; // docking requirements for our control (supercedes anchoring)
  155. internal int prev_width; // previous width of the control; required for anchoring
  156. internal int prev_height; // previous height of our control; required for anchoring
  157. internal Size prev_size;
  158. // to be categorized...
  159. static internal Hashtable controls; // All of the applications controls, in a flat list
  160. internal ControlCollection child_controls; // our children
  161. internal Control parent; // our parent control
  162. internal AccessibleObject accessibility_object; // object that contains accessibility information about our control
  163. internal BindingContext binding_context; // TODO
  164. internal RightToLeft right_to_left; // drawing direction for control
  165. internal int layout_suspended;
  166. private Graphics dc_mem;
  167. private Bitmap bmp_mem;
  168. #endregion // Local Variables
  169. #region Private Classes
  170. // This helper class allows us to dispatch messages to Control.WndProc
  171. internal class ControlNativeWindow : NativeWindow {
  172. private Control control;
  173. public ControlNativeWindow(Control control) : base() {
  174. this.control=control;
  175. }
  176. protected override void WndProc(ref Message m) {
  177. control.WndProc(ref m);
  178. }
  179. }
  180. #endregion
  181. #region Public Classes
  182. public class ControlCollection : IList, ICollection, ICloneable, IEnumerable {
  183. #region ControlCollection Local Variables
  184. private ArrayList list;
  185. private Control owner;
  186. #endregion // ControlCollection Local Variables
  187. #region ControlCollection Public Constructor
  188. public ControlCollection(Control owner) {
  189. this.owner=owner;
  190. this.list=new ArrayList();
  191. }
  192. #endregion
  193. #region ControlCollection Public Instance Properties
  194. public int Count {
  195. get {
  196. return list.Count;
  197. }
  198. }
  199. public bool IsReadOnly {
  200. get {
  201. return list.IsReadOnly;
  202. }
  203. }
  204. public virtual Control this[int index] {
  205. get {
  206. if (index < 0 || index >= list.Count) {
  207. throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
  208. }
  209. return (Control)list[index];
  210. }
  211. }
  212. #endregion // ControlCollection Public Instance Properties
  213. #region ControlCollection Private Instance Methods
  214. public virtual void Add(Control value) {
  215. for (int i=0; i<list.Count; i++) {
  216. if (list[i]==value) {
  217. // Do we need to do anything here?
  218. return;
  219. }
  220. }
  221. list.Add(value);
  222. value.Parent=owner;
  223. }
  224. public virtual void AddRange(Control[] controls) {
  225. for (int i=0; i<controls.Length; i++) {
  226. Add(controls[i]);
  227. }
  228. }
  229. public virtual void Clear() {
  230. owner.SuspendLayout();
  231. list.Clear();
  232. owner.ResumeLayout();
  233. }
  234. public virtual bool Contains(Control value) {
  235. return list.Contains(value);
  236. }
  237. public void CopyTo(Array array, int index) {
  238. list.CopyTo(array, index);
  239. }
  240. public override bool Equals(object other) {
  241. if (other is ControlCollection && (((ControlCollection)other).owner==this.owner)) {
  242. return(true);
  243. } else {
  244. return(false);
  245. }
  246. }
  247. public int GetChildIndex(Control child) {
  248. return GetChildIndex(child, false);
  249. }
  250. public int GetChildIndex(Control child, bool throwException) {
  251. int index;
  252. index=list.IndexOf(child);
  253. if (index==-1 && throwException) {
  254. throw new ArgumentException("Not a child control", "child");
  255. }
  256. return index;
  257. }
  258. public IEnumerator GetEnumerator() {
  259. return list.GetEnumerator();
  260. }
  261. public override int GetHashCode() {
  262. return base.GetHashCode();
  263. }
  264. public int IndexOf(Control control) {
  265. return list.IndexOf(control);
  266. }
  267. public virtual void Remove(Control value) {
  268. list.Remove(value);
  269. }
  270. public void RemoveAt(int index) {
  271. if (index<0 || index>=list.Count) {
  272. throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
  273. }
  274. list.RemoveAt(index);
  275. }
  276. public void SetChildIndex(Control child, int newIndex) {
  277. int old_index;
  278. old_index=list.IndexOf(child);
  279. if (old_index==-1) {
  280. throw new ArgumentException("Not a child control", "child");
  281. }
  282. if (old_index==newIndex) {
  283. return;
  284. }
  285. RemoveAt(old_index);
  286. if (newIndex>list.Count) {
  287. list.Add(child);
  288. } else {
  289. list.Insert(newIndex, child);
  290. }
  291. }
  292. #endregion // ControlCollection Private Instance Methods
  293. #region ControlCollection Interface Properties
  294. object IList.this[int index] {
  295. get {
  296. if (index<0 || index>=list.Count) {
  297. throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
  298. }
  299. return this[index];
  300. }
  301. set {
  302. if (!(value is Control)) {
  303. throw new ArgumentException("Object of type Control required", "value");
  304. }
  305. list[index]=(Control)value;
  306. }
  307. }
  308. bool IList.IsFixedSize {
  309. get {
  310. return false;
  311. }
  312. }
  313. bool IList.IsReadOnly {
  314. get {
  315. return list.IsReadOnly;
  316. }
  317. }
  318. bool ICollection.IsSynchronized {
  319. get {
  320. return list.IsSynchronized;
  321. }
  322. }
  323. object ICollection.SyncRoot {
  324. get {
  325. return list.SyncRoot;
  326. }
  327. }
  328. #endregion // ControlCollection Interface Properties
  329. #region ControlCollection Interface Methods
  330. int IList.Add(object value) {
  331. if (value == null) {
  332. throw new ArgumentNullException("value", "Cannot add null controls");
  333. }
  334. if (!(value is Control)) {
  335. throw new ArgumentException("Object of type Control required", "value");
  336. }
  337. return list.Add(value);
  338. }
  339. bool IList.Contains(object value) {
  340. if (!(value is Control)) {
  341. throw new ArgumentException("Object of type Control required", "value");
  342. }
  343. return this.Contains((Control) value);
  344. }
  345. int IList.IndexOf(object value) {
  346. if (!(value is Control)) {
  347. throw new ArgumentException("Object of type Control required", "value");
  348. }
  349. return this.IndexOf((Control) value);
  350. }
  351. void IList.Insert(int index, object value) {
  352. if (!(value is Control)) {
  353. throw new ArgumentException("Object of type Control required", "value");
  354. }
  355. list.Insert(index, value);
  356. }
  357. void IList.Remove(object value) {
  358. if (!(value is Control)) {
  359. throw new ArgumentException("Object of type Control required", "value");
  360. }
  361. list.Remove(value);
  362. }
  363. void ICollection.CopyTo(Array array, int index) {
  364. if (list.Count>0) {
  365. list.CopyTo(array, index);
  366. }
  367. }
  368. Object ICloneable.Clone() {
  369. ControlCollection clone = new ControlCollection(this.owner);
  370. clone.list=(ArrayList)list.Clone(); // FIXME: Do we need this?
  371. return clone;
  372. }
  373. #endregion // ControlCollection Interface Methods
  374. }
  375. #endregion // ControlCollection Class
  376. #region Public Constructors
  377. public Control() {
  378. creator_thread = Thread.CurrentThread;
  379. controls = new Hashtable();
  380. child_controls = CreateControlsInstance();
  381. bounds = new Rectangle(0, 0, DefaultSize.Width, DefaultSize.Height);
  382. client_size = new Size(DefaultSize.Width, DefaultSize.Height);
  383. prev_size = client_size;
  384. is_visible = true;
  385. is_disposed = false;
  386. is_enabled = true;
  387. has_focus = false;
  388. layout_suspended = 0;
  389. parent = null;
  390. background_image = null;
  391. }
  392. public Control(Control parent, string text) : this() {
  393. Text=text;
  394. Parent=parent;
  395. }
  396. public Control(Control parent, string text, int left, int top, int width, int height) : this() {
  397. Parent=parent;
  398. Left=left;
  399. Top=top;
  400. Width=width;
  401. Height=height;
  402. Text=text;
  403. }
  404. public Control(string text) : this() {
  405. Text=text;
  406. }
  407. public Control(string text, int left, int top, int width, int height) : this() {
  408. Left=left;
  409. Top=top;
  410. Width=width;
  411. Height=height;
  412. Text=text;
  413. }
  414. #endregion // Public Constructors
  415. #region Public Static Properties
  416. public static Color DefaultBackColor {
  417. get {
  418. return ThemeEngine.Current.DefaultControlBackColor;
  419. }
  420. }
  421. public static Font DefaultFont {
  422. get {
  423. return ThemeEngine.Current.DefaultFont;
  424. }
  425. }
  426. public static Color DefaultForeColor {
  427. get {
  428. return ThemeEngine.Current.DefaultControlForeColor;
  429. }
  430. }
  431. public static Keys ModifierKeys {
  432. get {
  433. return XplatUI.State.ModifierKeys;
  434. }
  435. }
  436. public static MouseButtons MouseButtons {
  437. get {
  438. return XplatUI.State.MouseButtons;
  439. }
  440. }
  441. public static Point MousePosition {
  442. get {
  443. return XplatUI.State.MousePosition;
  444. }
  445. }
  446. #endregion // Public Static Properties
  447. #region Public Instance Properties
  448. public AccessibleObject AccessibilityObject {
  449. get {
  450. if (accessibility_object==null) {
  451. accessibility_object=CreateAccessibilityInstance();
  452. }
  453. return accessibility_object;
  454. }
  455. }
  456. public string AccessibleDefaultActionDescription {
  457. get {
  458. return AccessibilityObject.default_action;
  459. }
  460. set {
  461. AccessibilityObject.default_action=value;
  462. }
  463. }
  464. public string AccessibleDescription {
  465. get {
  466. return AccessibilityObject.description;
  467. }
  468. set {
  469. AccessibilityObject.description=value;
  470. }
  471. }
  472. public string AccessibleName {
  473. get {
  474. return AccessibilityObject.Name;
  475. }
  476. set {
  477. AccessibilityObject.Name=value;
  478. }
  479. }
  480. public AccessibleRole AccessibleRole {
  481. get {
  482. return AccessibilityObject.role;
  483. }
  484. set {
  485. AccessibilityObject.role=value;
  486. }
  487. }
  488. public virtual bool AllowDrop {
  489. get {
  490. return XplatUI.State.DropTarget;
  491. }
  492. set {
  493. XplatUI.State.DropTarget=value;
  494. }
  495. }
  496. public virtual AnchorStyles Anchor {
  497. get {
  498. return anchor_style;
  499. }
  500. set {
  501. anchor_style=value;
  502. }
  503. }
  504. public virtual Color BackColor {
  505. get {
  506. if (background_color.IsEmpty) {
  507. if (parent!=null) {
  508. return parent.BackColor;
  509. }
  510. return DefaultBackColor;
  511. }
  512. return background_color;
  513. }
  514. set {
  515. background_color=value;
  516. Refresh();
  517. }
  518. }
  519. public virtual Image BackgroundImage {
  520. get {
  521. return background_image;
  522. }
  523. set {
  524. if (background_image!=value) {
  525. background_image=value;
  526. OnBackgroundImageChanged(EventArgs.Empty);
  527. }
  528. }
  529. }
  530. public virtual BindingContext BindingContext {
  531. get {
  532. throw new NotImplementedException();
  533. }
  534. set {
  535. throw new NotImplementedException();
  536. }
  537. }
  538. public int Bottom {
  539. get {
  540. return bounds.Y+bounds.Height;
  541. }
  542. }
  543. public Rectangle Bounds {
  544. get {
  545. return this.bounds;
  546. }
  547. set {
  548. SetBoundsCore(value.Left, value.Top, value.Width, value.Height, BoundsSpecified.All);
  549. }
  550. }
  551. public bool CanFocus {
  552. get {
  553. throw new NotImplementedException();
  554. }
  555. }
  556. public bool CanSelect {
  557. get {
  558. throw new NotImplementedException();
  559. }
  560. }
  561. public bool Capture {
  562. get {
  563. throw new NotImplementedException();
  564. }
  565. set {
  566. throw new NotImplementedException();
  567. }
  568. }
  569. public bool CausesValidation {
  570. get {
  571. throw new NotImplementedException();
  572. }
  573. set {
  574. throw new NotImplementedException();
  575. }
  576. }
  577. public Rectangle ClientRectangle {
  578. get {
  579. return new Rectangle(0, 0, client_size.Width, client_size.Height);
  580. }
  581. }
  582. public Size ClientSize {
  583. get {
  584. return client_size;
  585. }
  586. set {
  587. this.SetClientSizeCore(value.Width, value.Height);
  588. }
  589. }
  590. public String CompanyName {
  591. get {
  592. return "Mono Project, Novell, Inc.";
  593. }
  594. }
  595. public bool ContainsFocus {
  596. get {
  597. throw new NotImplementedException();
  598. }
  599. }
  600. #if notdef
  601. public virtual ContextMenu ContextMenu {
  602. get {
  603. throw new NotImplementedException();
  604. }
  605. set {
  606. throw new NotImplementedException();
  607. }
  608. }
  609. #endif
  610. public ControlCollection Controls {
  611. get {
  612. return this.child_controls;
  613. }
  614. }
  615. public bool Created {
  616. get {
  617. throw new NotImplementedException();
  618. }
  619. }
  620. #if notdef
  621. public virtual Cursor Cursor {
  622. get {
  623. throw new NotImplementedException();
  624. }
  625. set {
  626. throw new NotImplementedException();
  627. }
  628. }
  629. public ControlBidingsCollection DataBindings {
  630. get {
  631. throw new NotImplementedException();
  632. }
  633. }
  634. #endif
  635. public virtual Rectangle DisplayRectangle {
  636. get {
  637. return ClientRectangle;
  638. }
  639. }
  640. public bool Disposing {
  641. get {
  642. throw new NotImplementedException();
  643. }
  644. }
  645. public virtual DockStyle Dock {
  646. get { return dock_style;}
  647. set {
  648. if (dock_style == value)
  649. return;
  650. dock_style = value;
  651. OnDockChanged(EventArgs.Empty);
  652. }
  653. }
  654. public bool Enabled {
  655. get {
  656. return is_enabled;
  657. }
  658. set {
  659. is_enabled = value;
  660. }
  661. }
  662. public virtual bool Focused {
  663. get {
  664. return this.has_focus;
  665. }
  666. }
  667. public virtual Font Font {
  668. get {
  669. if (font != null) {
  670. return font;
  671. }
  672. if (Parent != null && Parent.Font != null) {
  673. return Parent.Font;
  674. }
  675. return DefaultFont;
  676. }
  677. set {
  678. font=value;
  679. Refresh();
  680. }
  681. }
  682. public virtual Color ForeColor {
  683. get {
  684. if (foreground_color.IsEmpty) {
  685. if (parent!=null) {
  686. return parent.ForeColor;
  687. }
  688. return DefaultForeColor;
  689. }
  690. return foreground_color;
  691. }
  692. set {
  693. foreground_color=value;
  694. Refresh();
  695. }
  696. }
  697. public bool IsDisposed {
  698. get {
  699. return this.is_disposed;
  700. }
  701. }
  702. public bool IsHandleCreated {
  703. get {
  704. if ((window!=null) && (window.Handle!=IntPtr.Zero)) {
  705. return true;
  706. }
  707. return false;
  708. }
  709. }
  710. public string Name {
  711. get {
  712. return this.name;
  713. }
  714. set {
  715. this.name=value;
  716. }
  717. }
  718. public Control Parent {
  719. get {
  720. return this.parent;
  721. }
  722. set {
  723. if (parent!=value) {
  724. if (parent!=null) {
  725. parent.Controls.Remove(this);
  726. }
  727. parent=value;
  728. if (!parent.Controls.Contains(this)) {
  729. parent.Controls.Add(this);
  730. }
  731. XplatUI.SetParent(Handle, value.Handle);
  732. }
  733. }
  734. }
  735. public IntPtr Handle { // IWin32Window
  736. get
  737. {
  738. if (!IsHandleCreated) {
  739. CreateHandle();
  740. }
  741. return window.Handle;
  742. }
  743. }
  744. public bool InvokeRequired { // ISynchronizeInvoke
  745. get {
  746. if (creator_thread!=Thread.CurrentThread) {
  747. return true;
  748. }
  749. return false;
  750. }
  751. }
  752. public bool Visible {
  753. get {
  754. return this.is_visible;
  755. }
  756. set {
  757. if (value!=is_visible) {
  758. is_visible=value;
  759. XplatUI.SetVisible(Handle, value);
  760. }
  761. }
  762. }
  763. public virtual string Text {
  764. get {
  765. return this.text;
  766. }
  767. set {
  768. if (text!=value) {
  769. text=value;
  770. XplatUI.Text(Handle, text);
  771. OnTextChanged (EventArgs.Empty);
  772. }
  773. }
  774. }
  775. public int Left {
  776. get {
  777. return this.bounds.X;
  778. }
  779. set {
  780. SetBoundsCore(value, bounds.Y, bounds.Width, bounds.Height, BoundsSpecified.X);
  781. }
  782. }
  783. public int Top {
  784. get {
  785. return this.bounds.Y;
  786. }
  787. set {
  788. SetBoundsCore(bounds.X, value, bounds.Width, bounds.Height, BoundsSpecified.Y);
  789. }
  790. }
  791. public int Width {
  792. get {
  793. return this.bounds.Width;
  794. }
  795. set {
  796. SetBoundsCore(bounds.X, bounds.Y, value, bounds.Height, BoundsSpecified.Width);
  797. }
  798. }
  799. public int Height {
  800. get {
  801. return this.bounds.Height;
  802. }
  803. set {
  804. SetBoundsCore(bounds.X, bounds.Y, bounds.Width, value, BoundsSpecified.Height);
  805. }
  806. }
  807. public Point Location {
  808. get {
  809. return new Point(bounds.X, bounds.Y);
  810. }
  811. set {
  812. SetBoundsCore(value.X, value.Y, bounds.Width, bounds.Height, BoundsSpecified.Location);
  813. }
  814. }
  815. public Size Size {
  816. get {
  817. return new Size(Width, Height);
  818. }
  819. set {
  820. SetBoundsCore(bounds.X, bounds.Y, value.Width, value.Height, BoundsSpecified.Size);
  821. }
  822. }
  823. public int TabIndex {
  824. get {
  825. return tab_index;
  826. }
  827. set {
  828. tab_index = value;
  829. }
  830. }
  831. #endregion // Public Instance Properties
  832. internal Graphics DeviceContext {
  833. get { return dc_mem; }
  834. }
  835. internal Bitmap ImageBuffer {
  836. get { return bmp_mem; }
  837. }
  838. internal void CreateBuffers (int width, int height)
  839. {
  840. if (dc_mem != null)
  841. dc_mem.Dispose ();
  842. if (bmp_mem != null)
  843. bmp_mem.Dispose ();
  844. bmp_mem = new Bitmap (width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  845. dc_mem = Graphics.FromImage (bmp_mem);
  846. }
  847. #region Protected Instance Properties
  848. protected virtual CreateParams CreateParams {
  849. get {
  850. CreateParams create_params = new CreateParams();
  851. create_params.Caption = Text;
  852. create_params.X = Left;
  853. create_params.Y = Top;
  854. create_params.Width = Width;
  855. create_params.Height = Height;
  856. create_params.ClassName = XplatUI.DefaultClassName;
  857. create_params.ClassStyle = 0;
  858. create_params.ExStyle = 0;
  859. create_params.Param = 0;
  860. if (parent!=null) {
  861. create_params.Parent = parent.Handle;
  862. }
  863. create_params.Style = (int)WindowStyles.WS_OVERLAPPED;
  864. if (is_visible) {
  865. create_params.Style |= (int)WindowStyles.WS_VISIBLE;
  866. }
  867. return create_params;
  868. }
  869. }
  870. protected virtual ImeMode DefaultImeMode {
  871. get {
  872. return ImeMode.Inherit;
  873. }
  874. }
  875. protected virtual Size DefaultSize {
  876. get {
  877. return new Size(100, 23);
  878. }
  879. }
  880. #endregion // Protected Instance Properties
  881. #region Public Instance Methods
  882. public bool Contains(Control ctl) {
  883. Control current;
  884. current=ctl;
  885. while (current!=null) {
  886. if (current==ctl) {
  887. return true;
  888. }
  889. current=current.parent;
  890. }
  891. return false;
  892. }
  893. public void CreateControl() {
  894. Control child;
  895. CreateHandle();
  896. for (int i=0; i<child_controls.Count; i++) {
  897. child_controls[i].CreateControl();
  898. }
  899. OnCreateControl();
  900. }
  901. public virtual void Refresh() {
  902. if (IsHandleCreated == true)
  903. XplatUI.RefreshWindow(window.Handle);
  904. }
  905. public void SetBounds(int x, int y, int width, int height) {
  906. SetBoundsCore(x, y, width, height, BoundsSpecified.All);
  907. }
  908. public void SetBounds(int x, int y, int width, int height, BoundsSpecified specified) {
  909. SetBoundsCore(x, y, width, height, specified);
  910. }
  911. public void Show() {
  912. if (!IsHandleCreated) {
  913. this.CreateHandle();
  914. }
  915. this.Visible=true;
  916. }
  917. public object Invoke(Delegate method) { // ISynchronizeInvoke
  918. return Invoke(method, null);
  919. }
  920. public object Invoke(Delegate method, object[] args) { // ISynchronizeInvoke
  921. IAsyncResult result;
  922. result=BeginInvoke(method, args);
  923. return EndInvoke(result);
  924. }
  925. public IAsyncResult BeginInvoke(Delegate method) { // ISynchronizeInvoke
  926. return BeginInvoke(method, null);
  927. }
  928. protected virtual AccessibleObject CreateAccessibilityInstance() {
  929. return new AccessibleObject(this);
  930. }
  931. protected virtual ControlCollection CreateControlsInstance() {
  932. return new ControlCollection(this);
  933. }
  934. protected void UpdateBounds() {
  935. int x;
  936. int y;
  937. int width;
  938. int height;
  939. if (!IsHandleCreated) {
  940. CreateHandle();
  941. }
  942. XplatUI.GetWindowPos(this.Handle, out x, out y, out width, out height);
  943. UpdateBounds(x, y, width, height);
  944. }
  945. protected void UpdateBounds(int x, int y, int width, int height) {
  946. // UpdateBounds only seems to set our sizes and fire events but not update the GUI window to match
  947. bool moved = false;
  948. bool resized = false;
  949. int client_x_diff = this.bounds.Width-this.client_size.Width;
  950. int client_y_diff = this.bounds.Height-this.client_size.Height;
  951. // Needed to generate required notifications
  952. if ((this.bounds.X!=x) || (this.bounds.Y!=y)) {
  953. moved=true;
  954. }
  955. if ((this.Bounds.Width!=width) || (this.Bounds.Height!=height)) {
  956. resized=true;
  957. }
  958. bounds.X=x;
  959. bounds.Y=y;
  960. bounds.Width=width;
  961. bounds.Height=height;
  962. // Update client rectangle as well
  963. client_size.Width=width-client_x_diff;
  964. client_size.Height=height-client_y_diff;
  965. if (moved) {
  966. OnLocationChanged(EventArgs.Empty);
  967. }
  968. if (resized) {
  969. OnSizeChanged(EventArgs.Empty);
  970. }
  971. }
  972. protected void UpdateBounds(int x, int y, int width, int height, int clientWidth, int clientHeight) {
  973. this.client_size.Width=clientWidth;
  974. this.client_size.Height=clientHeight;
  975. UpdateBounds(x, y, width, height);
  976. }
  977. public void Invalidate() {
  978. Invalidate(new Rectangle(0, 0, bounds.Width, bounds.Height), false);
  979. }
  980. public void Invalidate(bool invalidateChildren) {
  981. Invalidate(new Rectangle(0, 0, bounds.Width, bounds.Height), invalidateChildren);
  982. }
  983. public void Invalidate(System.Drawing.Rectangle rc) {
  984. Invalidate(rc, false);
  985. }
  986. public void Invalidate(System.Drawing.Rectangle rc, bool invalidateChildren) {
  987. if (!IsHandleCreated || !is_visible) {
  988. return;
  989. }
  990. XplatUI.Invalidate(Handle, rc, false);
  991. if (invalidateChildren) {
  992. for (int i=0; i<child_controls.Count; i++) child_controls[i].Invalidate();
  993. }
  994. }
  995. public void Invalidate(System.Drawing.Region region) {
  996. Invalidate(region, false);
  997. }
  998. [MonoTODO]
  999. public void Invalidate(System.Drawing.Region region, bool invalidateChildren) {
  1000. throw new NotImplementedException();
  1001. // FIXME - should use the GetRegionScans function of the region to invalidate each area
  1002. if (invalidateChildren) {
  1003. for (int i=0; i<child_controls.Count; i++) child_controls[i].Invalidate();
  1004. }
  1005. }
  1006. [MonoTODO("BeginInvoke() : Figure out a cross-platform way to handle this")]
  1007. public IAsyncResult BeginInvoke(Delegate method, object[] args) { // ISynchronizeInvoke
  1008. IAsyncResult result = null;
  1009. return result;
  1010. }
  1011. [MonoTODO]
  1012. public object EndInvoke(IAsyncResult async_result) { // ISynchronizeInvoke
  1013. object result = null;
  1014. return result;
  1015. }
  1016. public void PerformLayout() {
  1017. PerformLayout(null, null);
  1018. }
  1019. public void PerformLayout(Control affectedControl, string affectedProperty) {
  1020. LayoutEventArgs levent = new LayoutEventArgs(affectedControl, affectedProperty);
  1021. Console.WriteLine("Performing layout on control {0}", this);
  1022. if (layout_suspended>0) {
  1023. return;
  1024. }
  1025. // Prevent us from getting messed up
  1026. layout_suspended++;
  1027. // Perform all Dock and Anchor calculations
  1028. try {
  1029. Control child;
  1030. AnchorStyles anchor;
  1031. Rectangle space;
  1032. space=this.DisplayRectangle;
  1033. // Deal with docking
  1034. for (int i=0; i < child_controls.Count; i++) {
  1035. child=child_controls[i];
  1036. switch (child.Dock) {
  1037. case DockStyle.None: {
  1038. // Do nothing
  1039. break;
  1040. }
  1041. case DockStyle.Left: {
  1042. child.SetBounds(space.Left, space.Y, child.Width, space.Height);
  1043. Console.WriteLine("DockStyle.Left: Moving control {0} to {1}:{2} {3}x{4}", this, space.Left, space.Y, child.Width, space.Height);
  1044. space.X+=child.Width;
  1045. space.Width-=child.Width;
  1046. break;
  1047. }
  1048. case DockStyle.Top: {
  1049. child.SetBounds(space.Left, space.Y, space.Width, child.Height);
  1050. Console.WriteLine("DockStyle.Top: Moving control {0} to {1}:{2} {3}x{4}", this, space.Left, space.Y, child.Width, space.Height);
  1051. space.Y+=child.Height;
  1052. space.Height-=child.Height;
  1053. break;
  1054. }
  1055. case DockStyle.Right: {
  1056. child.SetBounds(space.Right-child.Width, space.Y, child.Width, space.Height);
  1057. Console.WriteLine("DockStyle.Right: Moving control {0} to {1}:{2} {3}x{4}", this, space.Left, space.Y, child.Width, space.Height);
  1058. space.Width-=child.Width;
  1059. break;
  1060. }
  1061. case DockStyle.Bottom: {
  1062. child.SetBounds(space.Left, space.Bottom-child.Height, space.Width, child.Height);
  1063. Console.WriteLine("DockStyle.Bottom: Moving control {0} to {1}:{2} {3}x{4}", this, space.Left, space.Y, child.Width, space.Height);
  1064. space.Height-=child.Height;
  1065. break;
  1066. }
  1067. case DockStyle.Fill: {
  1068. Console.WriteLine("DockStyle.Fill: Moving control {0} to {1}:{2} {3}x{4} (prev width/height: {5}x{6}", child, space.Left, space.Y, space.Width, space.Height, child.Width, child.Height);
  1069. child.SetBounds(space.Left, space.Top, space.Width, space.Height);
  1070. space.Width=0;
  1071. space.Height=0;
  1072. break;
  1073. }
  1074. }
  1075. }
  1076. space=this.DisplayRectangle;
  1077. // Deal with anchoring
  1078. for (int i=0; i < child_controls.Count; i++) {
  1079. int left;
  1080. int top;
  1081. int width;
  1082. int height;
  1083. int diff_width;
  1084. int diff_height;
  1085. child=child_controls[i];
  1086. anchor=child.Anchor;
  1087. left=child.Left-space.Left;
  1088. top=child.Top-space.Top;
  1089. width=prev_size.Width-child.Width-child.Left;
  1090. height=prev_size.Height-child.Height-child.Top;
  1091. diff_width=space.Width-prev_size.Width;
  1092. diff_height=space.Height-prev_size.Height;
  1093. prev_size.Width=space.Width;
  1094. prev_size.Height=space.Height;
  1095. // If the control is docked we don't need to do anything
  1096. if (child.Dock != DockStyle.None) {
  1097. continue;
  1098. }
  1099. if ((anchor & AnchorStyles.Left) !=0 ) {
  1100. if ((anchor & AnchorStyles.Right) != 0) {
  1101. // Anchoring to left and right
  1102. width=width+diff_width;
  1103. } else {
  1104. ; // nothing to do
  1105. }
  1106. } else if ((anchor & AnchorStyles.Right) != 0) {
  1107. left+=diff_width;
  1108. } else {
  1109. left+=diff_width/2;
  1110. }
  1111. if ((anchor & AnchorStyles.Top) !=0 ) {
  1112. if ((anchor & AnchorStyles.Bottom) != 0) {
  1113. height+=diff_height;
  1114. } else {
  1115. ; // nothing to do
  1116. }
  1117. } else if ((anchor & AnchorStyles.Bottom) != 0) {
  1118. top+=diff_height;
  1119. } else {
  1120. top+=diff_height/2;
  1121. }
  1122. // Sanity
  1123. if (width < 0) {
  1124. width=0;
  1125. }
  1126. if (height < 0) {
  1127. height=0;
  1128. }
  1129. child.SetBounds(left, top, width, height);
  1130. }
  1131. // Let everyone know
  1132. OnLayout(levent);
  1133. }
  1134. // Need to make sure we decremend layout_suspended
  1135. finally {
  1136. layout_suspended--;
  1137. }
  1138. }
  1139. public void ResumeLayout()
  1140. {
  1141. ResumeLayout (true);
  1142. }
  1143. public void ResumeLayout(bool peformLayout)
  1144. {
  1145. layout_suspended--;
  1146. if (layout_suspended > 0 || peformLayout == false)
  1147. return;
  1148. PerformLayout();
  1149. }
  1150. public void SuspendLayout()
  1151. {
  1152. layout_suspended++;
  1153. }
  1154. [MonoTODO]
  1155. protected virtual void WndProc(ref Message m) {
  1156. EventArgs e = new EventArgs();
  1157. #if debug
  1158. Console.WriteLine("Received message {0}", m);
  1159. #endif
  1160. switch((Msg)m.Msg) {
  1161. #if notyet
  1162. // Mouse handling
  1163. case Msg.WM_LBUTTONDBLCLK: throw new NotImplementedException(); break;
  1164. case Msg.WM_RBUTTONDOWN: throw new NotImplementedException(); break;
  1165. case Msg.WM_RBUTTONUP: throw new NotImplementedException(); break;
  1166. case Msg.WM_RBUTTONDBLCLK: throw new NotImplementedException(); break;
  1167. case Msg.WM_MOUSEHOVER: throw new NotImplementedException(); break;
  1168. case Msg.WM_MOUSELEAVE: throw new NotImplementedException(); break;
  1169. // Keyboard handling
  1170. case Msg.WM_CHAR: throw new NotImplementedException(); break;
  1171. case Msg.WM_KEYDOWN: throw new NotImplementedException(); break;
  1172. case Msg.WM_KEYUP: throw new NotImplementedException(); break;
  1173. #endif
  1174. // Window management
  1175. case Msg.WM_WINDOWPOSCHANGED: {
  1176. UpdateBounds();
  1177. DefWndProc(ref m);
  1178. break;
  1179. }
  1180. case Msg.WM_PAINT: {
  1181. Rectangle rect;
  1182. PaintEventArgs paint_event;
  1183. paint_event = XplatUI.PaintEventStart(Handle);
  1184. OnPaint(paint_event);
  1185. XplatUI.PaintEventEnd(Handle);
  1186. DefWndProc(ref m);
  1187. break;
  1188. }
  1189. case Msg.WM_ERASEBKGND:{
  1190. if (GetStyle (ControlStyles.UserPaint)){
  1191. PaintEventArgs eraseEventArgs = new PaintEventArgs (Graphics.FromHdc (m.WParam), new Rectangle (new Point (0,0),Size));
  1192. OnPaintBackground (eraseEventArgs);
  1193. m.Result = (IntPtr)1;
  1194. }
  1195. else {
  1196. m.Result = (IntPtr)0;
  1197. DefWndProc (ref m);
  1198. }
  1199. break;
  1200. }
  1201. case Msg.WM_LBUTTONUP: {
  1202. int clicks = 1;
  1203. OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  1204. clicks,
  1205. LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  1206. 0));
  1207. break;
  1208. }
  1209. case Msg.WM_LBUTTONDOWN: {
  1210. int clicks = 1;
  1211. OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  1212. clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  1213. 0));
  1214. break;
  1215. }
  1216. case Msg.WM_MOUSEWHEEL: {
  1217. int clicks = 1;
  1218. OnMouseWheel (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  1219. clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  1220. HighOrder(m.WParam.ToInt32())));
  1221. break;
  1222. }
  1223. case Msg.WM_MOUSEMOVE: {
  1224. int clicks = 1;
  1225. OnMouseMove (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  1226. clicks,
  1227. LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  1228. 0));
  1229. break;
  1230. }
  1231. case Msg.WM_SIZE: {
  1232. if (GetStyle(ControlStyles.ResizeRedraw)) {
  1233. Invalidate();
  1234. }
  1235. // Call UpdateBounds, the GUI already has the sizes
  1236. UpdateBounds (bounds.X, bounds.Y, LowOrder ((int) m.LParam.ToInt32 ()),
  1237. HighOrder ((int) m.LParam.ToInt32 ()));
  1238. DefWndProc(ref m);
  1239. break;
  1240. }
  1241. case Msg.WM_KEYDOWN: {
  1242. if (!ProcessKeyEventArgs (ref m))
  1243. DefWndProc (ref m);
  1244. break;
  1245. }
  1246. case Msg.WM_KEYUP: {
  1247. if (!ProcessKeyEventArgs (ref m))
  1248. DefWndProc (ref m);
  1249. break;
  1250. }
  1251. #if notyet
  1252. case Msg.WM_WINDOWPOSCHANGED: throw new NotImplementedException(); break;
  1253. case Msg.WM_SYSCOLORCHANGE: throw new NotImplementedException(); break;
  1254. #endif
  1255. default:
  1256. DefWndProc(ref m);
  1257. break;
  1258. }
  1259. }
  1260. #endregion // Public Instance Methods
  1261. #region // Protected Instance Methods
  1262. protected virtual void CreateHandle() {
  1263. if (IsDisposed) {
  1264. throw new ObjectDisposedException(Name);
  1265. }
  1266. if (IsHandleCreated) {
  1267. return;
  1268. }
  1269. if (window==null) {
  1270. window = new ControlNativeWindow(this);
  1271. window.CreateHandle(CreateParams);
  1272. }
  1273. if (window.Handle!=IntPtr.Zero) {
  1274. if (!controls.Contains(window.Handle)) {
  1275. controls.Add(window.Handle, this);
  1276. }
  1277. creator_thread = Thread.CurrentThread;
  1278. OnHandleCreated(EventArgs.Empty);
  1279. }
  1280. }
  1281. protected virtual void DefWndProc(ref Message m) {
  1282. window.DefWndProc(ref m);
  1283. }
  1284. protected virtual void DestroyHandle() {
  1285. if (IsHandleCreated) {
  1286. if (Handle != IntPtr.Zero) {
  1287. controls.Remove(Handle);
  1288. }
  1289. if (window != null) {
  1290. window.DestroyHandle();
  1291. }
  1292. }
  1293. }
  1294. protected virtual bool ProcessKeyEventArgs (ref Message msg)
  1295. {
  1296. KeyEventArgs key_event;
  1297. switch (msg.Msg) {
  1298. case (int)Msg.WM_KEYDOWN: {
  1299. key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
  1300. OnKeyDown (key_event);
  1301. return key_event.Handled;
  1302. }
  1303. case (int)Msg.WM_KEYUP: {
  1304. key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
  1305. OnKeyUp (key_event);
  1306. return key_event.Handled;
  1307. }
  1308. default:
  1309. break;
  1310. }
  1311. return false;
  1312. }
  1313. protected virtual bool IsInputKey (Keys keyData)
  1314. {
  1315. return false;
  1316. }
  1317. protected virtual bool ProcessDialogKey (Keys keyData)
  1318. {
  1319. if (parent != null)
  1320. return Parent.ProcessDialogKey (keyData);
  1321. return false;
  1322. }
  1323. protected bool GetStyle(ControlStyles flag) {
  1324. return (control_style & flag) != 0;
  1325. }
  1326. protected virtual bool ProcessDialogChar(char charCode) {
  1327. throw new NotImplementedException();
  1328. }
  1329. protected virtual bool ProcessMnemonic(char charCode) {
  1330. throw new NotImplementedException();
  1331. }
  1332. protected void RecreateHandle() {
  1333. IEnumerator child = child_controls.GetEnumerator();
  1334. if (IsHandleCreated) {
  1335. DestroyHandle();
  1336. CreateHandle();
  1337. // FIXME ZOrder?
  1338. while (child.MoveNext()) {
  1339. ((Control)child.Current).RecreateHandle();
  1340. }
  1341. }
  1342. }
  1343. protected virtual void ScaleCore(float dx, float dy) {
  1344. throw new NotImplementedException();
  1345. }
  1346. protected virtual void Select(bool directed, bool forward) {
  1347. throw new NotImplementedException();
  1348. }
  1349. protected virtual void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  1350. // SetBoundsCore updates the Win32 control itself. UpdateBounds updates the controls variables and fires events, I'm guessing - pdb
  1351. if ((specified & BoundsSpecified.X) != BoundsSpecified.X) {
  1352. x = Left;
  1353. }
  1354. if ((specified & BoundsSpecified.Y) != BoundsSpecified.Y) {
  1355. y = Top;
  1356. }
  1357. if ((specified & BoundsSpecified.Width)!= BoundsSpecified.Width) {
  1358. width = Width;
  1359. }
  1360. if ((specified & BoundsSpecified.Height) != BoundsSpecified.Height) {
  1361. height = Height;
  1362. }
  1363. if (IsHandleCreated) {
  1364. XplatUI.SetWindowPos(Handle, x, y, width, height);
  1365. }
  1366. UpdateBounds(x, y, width, height);
  1367. }
  1368. protected virtual void SetClientSizeCore(int x, int y) {
  1369. // Calculate the actual window size from the client size (it usually stays the same or grows)
  1370. Rectangle ClientRect;
  1371. Rectangle WindowRect;
  1372. CreateParams cp;
  1373. ClientRect = new Rectangle(0, 0, x, y);
  1374. cp = this.CreateParams;
  1375. if (XplatUI.CalculateWindowRect(Handle, ref ClientRect, cp.Style, false, out WindowRect)==false) {
  1376. return;
  1377. }
  1378. this.client_size = new Size(x, y);
  1379. SetBoundsCore(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
  1380. }
  1381. protected void SetStyle(ControlStyles flag, bool value) {
  1382. if (value) {
  1383. control_style |= flag;
  1384. } else {
  1385. control_style &= ~flag;
  1386. }
  1387. }
  1388. #endregion // Public Instance Methods
  1389. #region Private Instance Methods
  1390. #endregion // Private Instance Methods
  1391. #region Private Instance Methods
  1392. internal virtual void DoDefaultAction() {
  1393. // Only here to be overriden by our actual controls; this is needed by the accessibility class
  1394. }
  1395. #endregion // Private Instance Methods
  1396. #region OnXXX methods
  1397. protected virtual void OnBackColorChanged(EventArgs e) {
  1398. if (BackColorChanged!=null) BackColorChanged(this, e);
  1399. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackColorChanged(e);
  1400. }
  1401. protected virtual void OnBackgroundImageChanged(EventArgs e) {
  1402. if (BackgroundImageChanged!=null) BackgroundImageChanged(this, e);
  1403. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackgroundImageChanged(e);
  1404. }
  1405. protected virtual void OnBindingContextChanged(EventArgs e) {
  1406. if (BindingContextChanged!=null) BindingContextChanged(this, e);
  1407. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBindingContextChanged(e);
  1408. }
  1409. protected virtual void OnCausesValidationChanged(EventArgs e) {
  1410. if (CausesValidationChanged!=null) CausesValidationChanged(this, e);
  1411. }
  1412. protected virtual void OnChangeUICues(UICuesEventArgs e) {
  1413. if (CausesValidationChanged!=null) CausesValidationChanged(this, e);
  1414. }
  1415. protected virtual void OnClick(EventArgs e) {
  1416. if (Click!=null) Click(this, e);
  1417. }
  1418. protected virtual void OnContextMenuChanged(EventArgs e) {
  1419. if (ContextMenuChanged!=null) ContextMenuChanged(this, e);
  1420. }
  1421. protected virtual void OnControlAdded(ControlEventArgs e) {
  1422. if (ControlAdded!=null) ControlAdded(this, e);
  1423. }
  1424. protected virtual void OnControlRemoved(ControlEventArgs e) {
  1425. if (ControlRemoved!=null) ControlRemoved(this, e);
  1426. }
  1427. protected virtual void OnCreateControl() {
  1428. }
  1429. protected virtual void OnCursorChanged(EventArgs e) {
  1430. if (CursorChanged!=null) CursorChanged(this, e);
  1431. }
  1432. protected virtual void OnDockChanged(EventArgs e) {
  1433. if (DockChanged!=null) DockChanged(this, e);
  1434. }
  1435. protected virtual void OnDoubleClick(EventArgs e) {
  1436. if (DoubleClick!=null) DoubleClick(this, e);
  1437. }
  1438. protected virtual void OnDragDrop(DragEventArgs drgevent) {
  1439. if (DragDrop!=null) DragDrop(this, drgevent);
  1440. }
  1441. protected virtual void OnDragEnter(DragEventArgs drgevent) {
  1442. if (DragEnter!=null) DragEnter(this, drgevent);
  1443. }
  1444. protected virtual void OnDragLeave(EventArgs e) {
  1445. if (DragLeave!=null) DragLeave(this, e);
  1446. }
  1447. protected virtual void OnDragOver(DragEventArgs drgevent) {
  1448. if (DragOver!=null) DragOver(this, drgevent);
  1449. }
  1450. protected virtual void OnEnabledChanged(EventArgs e) {
  1451. if (EnabledChanged!=null) EnabledChanged(this, e);
  1452. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentEnabledChanged(e);
  1453. }
  1454. protected virtual void OnEnter(EventArgs e) {
  1455. if (Enter!=null) Enter(this, e);
  1456. }
  1457. protected virtual void OnFontChanged(EventArgs e) {
  1458. if (FontChanged!=null) FontChanged(this, e);
  1459. }
  1460. protected virtual void OnForeColorChanged(EventArgs e) {
  1461. if (ForeColorChanged!=null) ForeColorChanged(this, e);
  1462. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentForeColorChanged(e);
  1463. }
  1464. protected virtual void OnGiveFeedback(GiveFeedbackEventArgs gfbevent) {
  1465. if (GiveFeedback!=null) GiveFeedback(this, gfbevent);
  1466. }
  1467. protected virtual void OnGotFocus(EventArgs e) {
  1468. if (GotFocus!=null) GotFocus(this, e);
  1469. }
  1470. protected virtual void OnHandleCreated(EventArgs e) {
  1471. if (HandleCreated!=null) HandleCreated(this, e);
  1472. }
  1473. protected virtual void OnHandleDestroyed(EventArgs e) {
  1474. if (HandleDestroyed!=null) HandleDestroyed(this, e);
  1475. }
  1476. protected virtual void OnHelpRequested(HelpEventArgs hevent) {
  1477. if (HelpRequested!=null) HelpRequested(this, hevent);
  1478. }
  1479. protected virtual void OnImeModeChanged(EventArgs e) {
  1480. if (ImeModeChanged!=null) ImeModeChanged(this, e);
  1481. }
  1482. protected virtual void OnInvalidated(InvalidateEventArgs e) {
  1483. if (Invalidated!=null) Invalidated(this, e);
  1484. }
  1485. protected virtual void OnKeyDown(KeyEventArgs e) {
  1486. if (KeyDown!=null) KeyDown(this, e);
  1487. }
  1488. protected virtual void OnKeyUp(KeyEventArgs e) {
  1489. if (KeyUp!=null) KeyUp(this, e);
  1490. }
  1491. protected virtual void OnLayout(LayoutEventArgs levent) {
  1492. if (Layout!=null) Layout(this, levent);
  1493. }
  1494. protected virtual void OnLeave(EventArgs e) {
  1495. if (Leave!=null) Leave(this, e);
  1496. }
  1497. protected virtual void OnLocationChanged(EventArgs e) {
  1498. if (LocationChanged!=null) LocationChanged(this, e);
  1499. }
  1500. protected virtual void OnLostFocus(EventArgs e) {
  1501. if (LostFocus!=null) LostFocus(this, e);
  1502. }
  1503. protected virtual void OnMouseDown(MouseEventArgs e) {
  1504. if (MouseDown!=null) MouseDown(this, e);
  1505. }
  1506. protected virtual void OnMouseEnter(EventArgs e) {
  1507. if (MouseEnter!=null) MouseEnter(this, e);
  1508. }
  1509. protected virtual void OnMouseHover(EventArgs e) {
  1510. if (MouseHover!=null) MouseHover(this, e);
  1511. }
  1512. protected virtual void OnMouseLeave(EventArgs e) {
  1513. if (MouseLeave!=null) MouseLeave(this, e);
  1514. }
  1515. protected virtual void OnMouseMove(MouseEventArgs e) {
  1516. if (MouseMove!=null) MouseMove(this, e);
  1517. }
  1518. protected virtual void OnMouseUp(MouseEventArgs e) {
  1519. if (MouseUp!=null) MouseUp(this, e);
  1520. }
  1521. protected virtual void OnMouseWheel(MouseEventArgs e) {
  1522. if (MouseWheel!=null) MouseWheel(this, e);
  1523. }
  1524. protected virtual void OnMove(EventArgs e) {
  1525. if (Move!=null) Move(this, e);
  1526. }
  1527. protected virtual void OnNotifyMessage(Message m) {
  1528. // Override me!
  1529. }
  1530. protected virtual void OnPaint(PaintEventArgs e) {
  1531. if (Paint!=null) Paint(this, e);
  1532. }
  1533. protected virtual void OnPaintBackground(PaintEventArgs pevent) {
  1534. // Override me!
  1535. }
  1536. protected virtual void OnParentBackColorChanged(EventArgs e) {
  1537. if (background_color.IsEmpty && background_image==null) {
  1538. Invalidate();
  1539. OnBackColorChanged(e);
  1540. }
  1541. }
  1542. protected virtual void OnParentBackgroundImageChanged(EventArgs e) {
  1543. if (background_color.IsEmpty && background_image==null) {
  1544. Invalidate();
  1545. OnBackgroundImageChanged(e);
  1546. }
  1547. }
  1548. protected virtual void OnParentBindingContextChanged(EventArgs e) {
  1549. if (binding_context==null) {
  1550. binding_context=Parent.binding_context;
  1551. OnBindingContextChanged(e);
  1552. }
  1553. }
  1554. protected virtual void OnParentChanged(EventArgs e) {
  1555. if (ParentChanged!=null) ParentChanged(this, e);
  1556. }
  1557. protected virtual void OnParentEnabledChanged(EventArgs e) {
  1558. if ((is_enabled && !Parent.is_enabled) || (!is_enabled && Parent.is_enabled)) {
  1559. is_enabled=false;
  1560. Invalidate();
  1561. EnabledChanged(this, e);
  1562. }
  1563. }
  1564. protected virtual void OnParentFontChanged(EventArgs e) {
  1565. if (font==null) {
  1566. Invalidate();
  1567. OnFontChanged(e);
  1568. }
  1569. }
  1570. protected virtual void OnParentForeColorChanged(EventArgs e) {
  1571. if (foreground_color.IsEmpty) {
  1572. Invalidate();
  1573. OnForeColorChanged(e);
  1574. }
  1575. }
  1576. protected virtual void OnParentRightToLeftChanged(EventArgs e) {
  1577. if (right_to_left==RightToLeft.Inherit) {
  1578. Invalidate();
  1579. OnRightToLeftChanged(e);
  1580. }
  1581. }
  1582. protected virtual void OnParentVisibleChanged(EventArgs e) {
  1583. if (is_visible!=Parent.is_visible) {
  1584. is_visible=false;
  1585. Invalidate();
  1586. OnVisibleChanged(e);
  1587. }
  1588. }
  1589. protected virtual void OnQueryContinueDrag(QueryContinueDragEventArgs e) {
  1590. if (QueryContinueDrag!=null) QueryContinueDrag(this, e);
  1591. }
  1592. protected virtual void OnResize(EventArgs e) {
  1593. if (Resize!=null) Resize(this, e);
  1594. PerformLayout(this, "bounds");
  1595. // Do we need to let our parent regenerate it's layout thing since we resized?
  1596. }
  1597. protected virtual void OnRightToLeftChanged(EventArgs e) {
  1598. if (RightToLeftChanged!=null) RightToLeftChanged(this, e);
  1599. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentRightToLeftChanged(e);
  1600. }
  1601. protected virtual void OnSizeChanged(EventArgs e) {
  1602. OnResize(e);
  1603. if (SizeChanged!=null) SizeChanged(this, e);
  1604. }
  1605. protected virtual void OnStyleChanged(EventArgs e) {
  1606. if (StyleChanged!=null) StyleChanged(this, e);
  1607. }
  1608. protected virtual void OnSystemColorsChanged(EventArgs e) {
  1609. if (SystemColorsChanged!=null) SystemColorsChanged(this, e);
  1610. }
  1611. protected virtual void OnTabIndexChanged(EventArgs e) {
  1612. if (TabIndexChanged!=null) TabIndexChanged(this, e);
  1613. }
  1614. protected virtual void OnTabStopChanged(EventArgs e) {
  1615. if (TabStopChanged!=null) TabStopChanged(this, e);
  1616. }
  1617. protected virtual void OnTextChanged(EventArgs e) {
  1618. if (TextChanged!=null) TextChanged(this, e);
  1619. }
  1620. protected virtual void OnValidated(EventArgs e) {
  1621. if (Validated!=null) Validated(this, e);
  1622. }
  1623. protected virtual void OnValidating(System.ComponentModel.CancelEventArgs e) {
  1624. if (Validating!=null) Validating(this, e);
  1625. }
  1626. protected virtual void OnVisibleChanged(EventArgs e) {
  1627. if (VisibleChanged!=null) VisibleChanged(this, e);
  1628. }
  1629. #endregion // OnXXX methods
  1630. #region Events
  1631. public event EventHandler BackColorChanged;
  1632. public event EventHandler BackgroundImageChanged;
  1633. public event EventHandler BindingContextChanged;
  1634. public event EventHandler CausesValidationChanged;
  1635. public event UICuesEventHandler ChangeUICues;
  1636. public event EventHandler Click;
  1637. public event EventHandler ContextMenuChanged;
  1638. public event ControlEventHandler ControlAdded;
  1639. public event ControlEventHandler ControlRemoved;
  1640. public event EventHandler CursorChanged;
  1641. public event EventHandler DockChanged;
  1642. public event EventHandler DoubleClick;
  1643. public event DragEventHandler DragDrop;
  1644. public event DragEventHandler DragEnter;
  1645. public event EventHandler DragLeave;
  1646. public event DragEventHandler DragOver;
  1647. public event EventHandler EnabledChanged;
  1648. public event EventHandler Enter;
  1649. public event EventHandler FontChanged;
  1650. public event EventHandler ForeColorChanged;
  1651. public event GiveFeedbackEventHandler GiveFeedback;
  1652. public event EventHandler GotFocus;
  1653. public event EventHandler HandleCreated;
  1654. public event EventHandler HandleDestroyed;
  1655. public event HelpEventHandler HelpRequested;
  1656. public event EventHandler ImeModeChanged;
  1657. public event InvalidateEventHandler Invalidated;
  1658. public event KeyEventHandler KeyDown;
  1659. public event KeyPressEventHandler KeyPress;
  1660. public event KeyEventHandler KeyUp;
  1661. public event LayoutEventHandler Layout;
  1662. public event EventHandler Leave;
  1663. public event EventHandler LocationChanged;
  1664. public event EventHandler LostFocus;
  1665. public event MouseEventHandler MouseDown;
  1666. public event EventHandler MouseEnter;
  1667. public event EventHandler MouseHover;
  1668. public event EventHandler MouseLeave;
  1669. public event MouseEventHandler MouseMove;
  1670. public event MouseEventHandler MouseUp;
  1671. public event MouseEventHandler MouseWheel;
  1672. public event EventHandler Move;
  1673. public event PaintEventHandler Paint;
  1674. public event EventHandler ParentChanged;
  1675. public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp;
  1676. public event QueryContinueDragEventHandler QueryContinueDrag;
  1677. public event EventHandler Resize;
  1678. public event EventHandler RightToLeftChanged;
  1679. public event EventHandler SizeChanged;
  1680. public event EventHandler StyleChanged;
  1681. public event EventHandler SystemColorsChanged;
  1682. public event EventHandler TabIndexChanged;
  1683. public event EventHandler TabStopChanged;
  1684. public event EventHandler TextChanged;
  1685. public event EventHandler Validated;
  1686. public event CancelEventHandler Validating;
  1687. public event EventHandler VisibleChanged;
  1688. #endregion // Events
  1689. #region Private Methods
  1690. internal static int LowOrder (int param)
  1691. {
  1692. return (param & 0xffff);
  1693. }
  1694. internal static int HighOrder (int param)
  1695. {
  1696. return (param >> 16);
  1697. }
  1698. internal static MouseButtons FromParamToMouseButtons (int param)
  1699. {
  1700. MouseButtons buttons = MouseButtons.None;
  1701. if ((param & (int) MsgButtons.MK_LBUTTON) != 0)
  1702. buttons |= MouseButtons.Left;
  1703. if ((param & (int) MsgButtons.MK_MBUTTON) != 0)
  1704. buttons |= MouseButtons.Middle;
  1705. if ((param & (int) MsgButtons.MK_RBUTTON) != 0)
  1706. buttons |= MouseButtons.Right;
  1707. return buttons;
  1708. }
  1709. #endregion
  1710. }
  1711. }