Control.cs 50 KB

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