Control.cs 52 KB

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