Control.cs 52 KB

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