Control.cs 51 KB

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