Control.cs 50 KB

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