Control.cs 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982
  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.64 $
  33. // $Modtime: $
  34. // $Log: Control.cs,v $
  35. // Revision 1.64 2004/09/16 23:44:19 pbartok
  36. // - Added SendToBack and BringToFront methods
  37. //
  38. // Revision 1.63 2004/09/11 03:50:00 pbartok
  39. // - Added DoDragDrop() [incomplete]
  40. // - Properly implemented 'Visible' handling
  41. // - Added SetVisibleCore()
  42. // - Implemented FindChildAtPoint()
  43. // - Implemented GetContainerControl()
  44. // - Implemented Hide()
  45. //
  46. // Revision 1.62 2004/09/11 01:28:11 pbartok
  47. // - Moved methods into their appropriate #regions
  48. // - Reordered methods within regions alphabetically
  49. //
  50. // Revision 1.61 2004/09/11 00:56:33 pbartok
  51. // - Moved some internal functions into the internal region
  52. // - Implemented FontHeight
  53. // - Implemented RenderRightToLeft
  54. // - Implemented ResizeRedraw
  55. // - Implemented ShowFocusCues
  56. // - Implemented ShowKeyboardCues
  57. // - Implemented FromChildHandle
  58. // - Implemented FromHandle
  59. // - Implemented IsMnemonic
  60. // - Implemented ReflectMessage
  61. // - All public and protected Static Methods are now complete
  62. //
  63. // Revision 1.60 2004/09/10 22:54:52 pbartok
  64. // - Implemented remaining missing public instance properties
  65. // - Alphabetized some out of order properties
  66. //
  67. // Revision 1.59 2004/09/06 12:55:07 jordi
  68. // Caches ClientRectangle rectangle value
  69. //
  70. // Revision 1.58 2004/09/02 22:24:50 pbartok
  71. // - Added sanity check to creation of double buffer bitmap
  72. //
  73. // Revision 1.57 2004/09/02 20:26:21 pbartok
  74. // - Added missing Control.ControlAccessibleObject class
  75. // - Started to implement Select()ion mechanisms, still very incomplete
  76. //
  77. // Revision 1.56 2004/09/01 13:19:19 jordi
  78. // Init string variables with an empty object
  79. //
  80. // Revision 1.55 2004/09/01 10:20:57 jordi
  81. // fires OnFontChanged event
  82. //
  83. // Revision 1.54 2004/09/01 01:41:31 pbartok
  84. // - Added firing of BackColorChanged event
  85. // - Added TopLevelControl property
  86. // - Fixed handling of WM_ERASEBKGRND message
  87. //
  88. // Revision 1.53 2004/08/27 20:17:25 pbartok
  89. // - Removed unneeded stack vars
  90. // - First attempt to fix sizing issues when layout is suspended
  91. //
  92. // Revision 1.52 2004/08/25 19:20:47 pbartok
  93. // - Control now properly passes the ambient background color to child
  94. // controls
  95. //
  96. // Revision 1.51 2004/08/25 18:32:15 pbartok
  97. // - Fixed generation of MouseUp message
  98. //
  99. // Revision 1.50 2004/08/24 18:24:25 jordi
  100. // fire OnEnabledChanged event
  101. //
  102. // Revision 1.49 2004/08/23 21:22:53 pbartok
  103. // - Added InitLayout() method
  104. // - Added code to properly perform layout when Anchor or Dock property is
  105. // changed
  106. // - Changed 'interpretation' of ResumeLayout. MS seems to have a LAMESPEC,
  107. // tried to do it in a way that makes sense
  108. //
  109. // Revision 1.48 2004/08/23 19:55:08 pbartok
  110. // - Properly fixed Jordi's last fix
  111. // - Now uses Cursor's Position property instead of calling XplatUI directly
  112. //
  113. // Revision 1.47 2004/08/23 19:16:23 jordi
  114. // avoids null exception
  115. //
  116. // Revision 1.46 2004/08/22 21:10:30 pbartok
  117. // - Removed OverlappedWindow style from Control, instead it's default
  118. // now is child
  119. // - Made form windows OverlappedWindow by default
  120. //
  121. // Revision 1.45 2004/08/21 22:19:30 pbartok
  122. // - Signature fixes
  123. //
  124. // Revision 1.44 2004/08/21 20:53:13 pbartok
  125. // - Updated to match new GrabWindow signature
  126. //
  127. // Revision 1.43 2004/08/21 19:32:15 pbartok
  128. // - Implemented Created property
  129. //
  130. // Revision 1.42 2004/08/21 19:28:22 pbartok
  131. // - Implemented ContainsFocus
  132. //
  133. // Revision 1.41 2004/08/21 19:26:24 pbartok
  134. // - Implemented CausesValidation
  135. //
  136. // Revision 1.40 2004/08/21 19:21:50 pbartok
  137. // - Implemented CanFocus
  138. // - Implemented CanSelect
  139. // - Implemented Capture
  140. //
  141. // Revision 1.39 2004/08/21 16:54:11 jackson
  142. // Implement EndInvoke
  143. //
  144. // Revision 1.38 2004/08/20 23:38:54 ravindra
  145. // Made RightToLeft property virtual and removed a Console.WriteLine.
  146. //
  147. // Revision 1.37 2004/08/20 20:09:24 pbartok
  148. // - Added call to set window background color
  149. //
  150. // Revision 1.36 2004/08/20 19:18:30 jackson
  151. // Implement Begininvoke
  152. //
  153. // Revision 1.35 2004/08/20 01:17:24 pbartok
  154. // - Added handling of WM_MOUSEHOVER
  155. // - Worked around 'bug' in Win32 WM_MOUSE_ENTER/WM_MOUSE_LEAVE driver code
  156. //
  157. // Revision 1.34 2004/08/19 23:09:22 pbartok
  158. // - Added Right property
  159. // - Added RightToLeft property
  160. //
  161. // Revision 1.33 2004/08/19 22:25:31 jordi
  162. // theme enhancaments
  163. //
  164. // Revision 1.32 2004/08/17 21:25:12 pbartok
  165. // - Drawing improvement; don't call UpdateBounds if we are not visible (or
  166. // have been minimized)
  167. //
  168. // Revision 1.31 2004/08/17 20:25:28 pbartok
  169. // - Fixed broken handling of default window sizes
  170. //
  171. // Revision 1.30 2004/08/16 21:47:11 pbartok
  172. // - Added handling of WM_MOUSE_ENTER & WM_MOUSE_LEAVE to
  173. // support OnMouseEnter/Leave()
  174. // - Added WS_CLIPSIBLINGS and WS_CLIPCHILDREN window styles to improve
  175. // exposure handling
  176. //
  177. // Revision 1.29 2004/08/13 22:15:46 pbartok
  178. // - Fixed Anchor default
  179. //
  180. // Revision 1.28 2004/08/13 21:43:39 pbartok
  181. // - Changed GetCursorPos signature
  182. //
  183. // Revision 1.27 2004/08/13 19:00:15 jordi
  184. // implements PointToClient (ScreenToClient)
  185. //
  186. // Revision 1.26 2004/08/13 18:52:07 pbartok
  187. // - Added Dispose() and destructor
  188. // - Fixed resizing and bounds calculation
  189. // - Fixed Layout
  190. // - Added memory savings for invisible windows
  191. //
  192. // Revision 1.25 2004/08/12 19:31:19 pbartok
  193. // - Fixed Anchoring bugs
  194. //
  195. // Revision 1.24 2004/08/12 16:10:42 jackson
  196. // Add missing properties
  197. //
  198. // Revision 1.23 2004/08/11 22:20:59 pbartok
  199. // - Signature fixes
  200. //
  201. // Revision 1.22 2004/08/11 19:19:44 pbartok
  202. // - We had SetWindowPos and MoveWindow to set window positions and size,
  203. // removed MoveWindow. We have GetWindowPos, so it made sense to keep
  204. // SetWindowPos as matching counterpart
  205. // - Added some X11 sanity checking
  206. //
  207. // Revision 1.21 2004/08/11 18:59:45 pbartok
  208. // - Major cleanup of my SetBounds/SetBoundsCore/UpdateBounds mess
  209. // (It seems that SetBounds is just a front for SetBoundsCore and
  210. // SetBoundsCore updates the underlying window system and UpdateBounds is
  211. // responsible for updating the variables associated with the Control and
  212. // sending the events)
  213. // - Major cleanup of Size handling; we now have two sizes, client_size and
  214. // bounds. Bounds defines the window with decorations, client_size without
  215. // them.
  216. //
  217. // Revision 1.20 2004/08/11 15:13:32 pbartok
  218. // - Now properly reparents windows
  219. //
  220. // Revision 1.19 2004/08/11 14:37:11 pbartok
  221. // - Duh!
  222. //
  223. // Revision 1.18 2004/08/11 13:47:22 pbartok
  224. // - Rewrote the collection stuff. Might not be as fast now, not keeping
  225. // the number of children around and accessible directly, but it's more
  226. // straightforward
  227. //
  228. // Revision 1.17 2004/08/10 18:32:10 jordi
  229. // throw ontextchange event
  230. //
  231. // Revision 1.16 2004/08/10 17:43:04 pbartok
  232. // - Added more to the still unfinished Dock/Anchor layout code
  233. //
  234. // Revision 1.15 2004/08/10 15:08:05 jackson
  235. // Control will now handle the buffering code, so each control does not have to implement this.
  236. //
  237. // Revision 1.14 2004/08/09 22:11:25 pbartok
  238. // - Added incomplete dock layout code
  239. // - Added support for mouse wheel
  240. //
  241. // Revision 1.13 2004/08/09 17:25:56 jackson
  242. // Use new color names
  243. //
  244. // Revision 1.12 2004/08/09 15:54:51 jackson
  245. // Get default properties from the theme.
  246. //
  247. // Revision 1.11 2004/08/06 21:30:56 pbartok
  248. // - Fixed recursive loop when resizing
  249. // - Improved/fixed redrawing on expose messages
  250. //
  251. // Revision 1.10 2004/08/06 15:53:39 jordi
  252. // X11 keyboard navigation
  253. //
  254. // Revision 1.9 2004/08/04 21:14:26 pbartok
  255. // - Fixed Invalidation bug (calculated wrong client area)
  256. // - Added ClientSize setter
  257. //
  258. // Revision 1.8 2004/08/04 20:11:24 pbartok
  259. // - Added Invalidate handling
  260. //
  261. // Revision 1.7 2004/07/27 10:38:17 jordi
  262. // changes to be able to run winforms samples
  263. //
  264. // Revision 1.6 2004/07/19 19:09:42 jordi
  265. // label control re-written: added missing functionlity, events, and properties
  266. //
  267. // Revision 1.5 2004/07/19 16:49:23 jordi
  268. // fixes SetBounds logic
  269. //
  270. // Revision 1.4 2004/07/19 07:29:35 jordi
  271. // Call RefreshWindow only if the window has created
  272. //
  273. // Revision 1.3 2004/07/15 17:03:35 jordi
  274. // added basic mouse handeling events
  275. //
  276. // Revision 1.2 2004/07/13 15:31:45 jordi
  277. // commit: new properties and fixes form size problems
  278. //
  279. // Revision 1.1 2004/07/09 05:21:25 pbartok
  280. // - Initial check-in
  281. //
  282. //
  283. // NOT COMPLETE
  284. using System;
  285. using System.Drawing;
  286. using System.ComponentModel;
  287. using System.Collections;
  288. using System.Diagnostics;
  289. using System.Threading;
  290. using System.Runtime.InteropServices;
  291. namespace System.Windows.Forms
  292. {
  293. public class Control : Component, ISynchronizeInvoke, IWin32Window
  294. {
  295. #region Local Variables
  296. // Basic
  297. internal Rectangle bounds; // bounding rectangle for control (client area + decorations)
  298. internal object creator_thread; // thread that created the control
  299. internal ControlNativeWindow window; // object for native window handle
  300. internal string name; // for object naming
  301. // State
  302. internal bool has_focus; // true if control has focus
  303. internal bool is_visible; // true if control is visible
  304. internal bool is_entered; // is the mouse inside the control?
  305. internal bool is_enabled; // true if control is enabled (usable/not grayed out)
  306. internal bool is_selected; // true if control is selected
  307. internal bool is_accessible; // true if the control is visible to accessibility applications
  308. internal bool is_captured; // tracks if the control has captured the mouse
  309. internal bool is_recreating; // tracks if the handle for the control is being recreated
  310. internal bool causes_validation; // tracks if validation is executed on changes
  311. internal int tab_index; // position in tab order of siblings
  312. internal bool tab_stop = true; // is the control a tab stop?
  313. internal bool is_disposed; // has the window already been disposed?
  314. internal Size client_size; // size of the client area (window excluding decorations)
  315. internal Rectangle client_rect; // rectangle with the client area (window excluding decorations)
  316. internal ControlStyles control_style; // rather win32-specific, style bits for control
  317. internal ImeMode ime_mode = ImeMode.Inherit;
  318. internal bool layout_pending; // true if our parent needs to re-layout us
  319. internal object control_tag; // object that contains data about our control
  320. // Visuals
  321. internal Color foreground_color; // foreground color for control
  322. internal Color background_color; // background color for control
  323. internal Image background_image; // background image for control
  324. internal Font font; // font for control
  325. internal string text; // window/title text for control
  326. // Layout
  327. internal AnchorStyles anchor_style; // anchoring requirements for our control
  328. internal DockStyle dock_style; // docking requirements for our control (supercedes anchoring)
  329. internal int prev_width; // previous width of the control; required for anchoring
  330. internal int prev_height; // previous height of our control; required for anchoring
  331. internal Size prev_size;
  332. // to be categorized...
  333. static internal Hashtable controls; // All of the applications controls, in a flat list
  334. internal ControlCollection child_controls; // our children
  335. internal Control parent; // our parent control
  336. internal AccessibleObject accessibility_object; // object that contains accessibility information about our control
  337. internal BindingContext binding_context; // TODO
  338. internal RightToLeft right_to_left; // drawing direction for control
  339. internal int layout_suspended;
  340. internal bool double_buffering;
  341. private Graphics dc_mem; // Graphics context for double buffering
  342. private Bitmap bmp_mem; // Bitmap for double buffering control
  343. #endregion // Local Variables
  344. #region Private Classes
  345. // This helper class allows us to dispatch messages to Control.WndProc
  346. internal class ControlNativeWindow : NativeWindow {
  347. private Control control;
  348. public ControlNativeWindow(Control control) : base() {
  349. this.control=control;
  350. }
  351. protected override void WndProc(ref Message m) {
  352. control.WndProc(ref m);
  353. }
  354. }
  355. #endregion
  356. #region Public Classes
  357. public class ControlAccessibleObject : AccessibleObject {
  358. #region ControlAccessibleObject Local Variables
  359. private Control owner;
  360. #endregion // ControlAccessibleObject Local Variables
  361. #region ControlAccessibleObject Constructors
  362. public ControlAccessibleObject(Control ownerControl) {
  363. this.owner = ownerControl;
  364. }
  365. #endregion // ControlAccessibleObject Constructors
  366. #region ControlAccessibleObject Public Instance Properties
  367. public override string DefaultAction {
  368. get {
  369. return base.DefaultAction;
  370. }
  371. }
  372. public override string Description {
  373. get {
  374. return base.Description;
  375. }
  376. }
  377. public IntPtr Handle {
  378. get {
  379. return owner.Handle;
  380. }
  381. set {
  382. // We don't want to let them set it
  383. }
  384. }
  385. public override string Help {
  386. get {
  387. return base.Help;
  388. }
  389. }
  390. public override string KeyboardShortcut {
  391. get {
  392. return base.KeyboardShortcut;
  393. }
  394. }
  395. public override string Name {
  396. get {
  397. return base.Name;
  398. }
  399. set {
  400. base.Name = value;
  401. }
  402. }
  403. public Control Owner {
  404. get {
  405. return owner;
  406. }
  407. }
  408. public override AccessibleRole Role {
  409. get {
  410. return base.Role;
  411. }
  412. }
  413. #endregion // ControlAccessibleObject Public Instance Properties
  414. #region ControlAccessibleObject Public Instance Methods
  415. public override int GetHelpTopic(out string FileName) {
  416. return base.GetHelpTopic (out FileName);
  417. }
  418. #endregion // ControlAccessibleObject Public Instance Methods
  419. }
  420. public class ControlCollection : IList, ICollection, ICloneable, IEnumerable {
  421. #region ControlCollection Local Variables
  422. private ArrayList list;
  423. private Control owner;
  424. #endregion // ControlCollection Local Variables
  425. #region ControlCollection Public Constructor
  426. public ControlCollection(Control owner) {
  427. this.owner=owner;
  428. this.list=new ArrayList();
  429. }
  430. #endregion
  431. #region ControlCollection Public Instance Properties
  432. public int Count {
  433. get {
  434. return list.Count;
  435. }
  436. }
  437. public bool IsReadOnly {
  438. get {
  439. return list.IsReadOnly;
  440. }
  441. }
  442. public virtual Control this[int index] {
  443. get {
  444. if (index < 0 || index >= list.Count) {
  445. throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
  446. }
  447. return (Control)list[index];
  448. }
  449. }
  450. #endregion // ControlCollection Public Instance Properties
  451. #region ControlCollection Private Instance Methods
  452. public virtual void Add(Control value) {
  453. for (int i=0; i<list.Count; i++) {
  454. if (list[i]==value) {
  455. // Do we need to do anything here?
  456. return;
  457. }
  458. }
  459. list.Add(value);
  460. value.Parent=owner;
  461. }
  462. public virtual void AddRange(Control[] controls) {
  463. for (int i=0; i<controls.Length; i++) {
  464. Add(controls[i]);
  465. }
  466. }
  467. public virtual void Clear() {
  468. owner.SuspendLayout();
  469. list.Clear();
  470. owner.ResumeLayout();
  471. }
  472. public virtual bool Contains(Control value) {
  473. return list.Contains(value);
  474. }
  475. public void CopyTo(Array array, int index) {
  476. list.CopyTo(array, index);
  477. }
  478. public override bool Equals(object other) {
  479. if (other is ControlCollection && (((ControlCollection)other).owner==this.owner)) {
  480. return(true);
  481. } else {
  482. return(false);
  483. }
  484. }
  485. public int GetChildIndex(Control child) {
  486. return GetChildIndex(child, false);
  487. }
  488. public int GetChildIndex(Control child, bool throwException) {
  489. int index;
  490. index=list.IndexOf(child);
  491. if (index==-1 && throwException) {
  492. throw new ArgumentException("Not a child control", "child");
  493. }
  494. return index;
  495. }
  496. public IEnumerator GetEnumerator() {
  497. return list.GetEnumerator();
  498. }
  499. public override int GetHashCode() {
  500. return base.GetHashCode();
  501. }
  502. public int IndexOf(Control control) {
  503. return list.IndexOf(control);
  504. }
  505. public virtual void Remove(Control value) {
  506. list.Remove(value);
  507. }
  508. public void RemoveAt(int index) {
  509. if (index<0 || index>=list.Count) {
  510. throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
  511. }
  512. list.RemoveAt(index);
  513. }
  514. public void SetChildIndex(Control child, int newIndex) {
  515. int old_index;
  516. old_index=list.IndexOf(child);
  517. if (old_index==-1) {
  518. throw new ArgumentException("Not a child control", "child");
  519. }
  520. if (old_index==newIndex) {
  521. return;
  522. }
  523. RemoveAt(old_index);
  524. if (newIndex>list.Count) {
  525. list.Add(child);
  526. } else {
  527. list.Insert(newIndex, child);
  528. }
  529. }
  530. #endregion // ControlCollection Private Instance Methods
  531. #region ControlCollection Interface Properties
  532. object IList.this[int index] {
  533. get {
  534. if (index<0 || index>=list.Count) {
  535. throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
  536. }
  537. return this[index];
  538. }
  539. set {
  540. if (!(value is Control)) {
  541. throw new ArgumentException("Object of type Control required", "value");
  542. }
  543. list[index]=(Control)value;
  544. }
  545. }
  546. bool IList.IsFixedSize {
  547. get {
  548. return false;
  549. }
  550. }
  551. bool IList.IsReadOnly {
  552. get {
  553. return list.IsReadOnly;
  554. }
  555. }
  556. bool ICollection.IsSynchronized {
  557. get {
  558. return list.IsSynchronized;
  559. }
  560. }
  561. object ICollection.SyncRoot {
  562. get {
  563. return list.SyncRoot;
  564. }
  565. }
  566. #endregion // ControlCollection Interface Properties
  567. #region ControlCollection Interface Methods
  568. int IList.Add(object value) {
  569. if (value == null) {
  570. throw new ArgumentNullException("value", "Cannot add null controls");
  571. }
  572. if (!(value is Control)) {
  573. throw new ArgumentException("Object of type Control required", "value");
  574. }
  575. return list.Add(value);
  576. }
  577. bool IList.Contains(object value) {
  578. if (!(value is Control)) {
  579. throw new ArgumentException("Object of type Control required", "value");
  580. }
  581. return this.Contains((Control) value);
  582. }
  583. int IList.IndexOf(object value) {
  584. if (!(value is Control)) {
  585. throw new ArgumentException("Object of type Control required", "value");
  586. }
  587. return this.IndexOf((Control) value);
  588. }
  589. void IList.Insert(int index, object value) {
  590. if (!(value is Control)) {
  591. throw new ArgumentException("Object of type Control required", "value");
  592. }
  593. list.Insert(index, value);
  594. }
  595. void IList.Remove(object value) {
  596. if (!(value is Control)) {
  597. throw new ArgumentException("Object of type Control required", "value");
  598. }
  599. list.Remove(value);
  600. }
  601. void ICollection.CopyTo(Array array, int index) {
  602. if (list.Count>0) {
  603. list.CopyTo(array, index);
  604. }
  605. }
  606. Object ICloneable.Clone() {
  607. ControlCollection clone = new ControlCollection(this.owner);
  608. clone.list=(ArrayList)list.Clone(); // FIXME: Do we need this?
  609. return clone;
  610. }
  611. #endregion // ControlCollection Interface Methods
  612. }
  613. #endregion // ControlCollection Class
  614. #region Public Constructors
  615. public Control() {
  616. creator_thread = Thread.CurrentThread;
  617. controls = new Hashtable();
  618. child_controls = CreateControlsInstance();
  619. client_size = new Size(DefaultSize.Width, DefaultSize.Height);
  620. client_rect = new Rectangle(0, 0, DefaultSize.Width, DefaultSize.Height);
  621. XplatUI.CalculateWindowRect(IntPtr.Zero, ref client_rect, CreateParams.Style, false, out bounds);
  622. if ((CreateParams.Style & (int)WindowStyles.WS_CHILD) == 0) {
  623. bounds.X=-1;
  624. bounds.Y=-1;
  625. }
  626. prev_size = Size.Empty;
  627. anchor_style = AnchorStyles.Top | AnchorStyles.Left;
  628. is_visible = true;
  629. is_captured = false;
  630. is_disposed = false;
  631. is_enabled = true;
  632. is_entered = false;
  633. layout_pending = false;
  634. causes_validation = true;
  635. has_focus = false;
  636. layout_suspended = 0;
  637. double_buffering = true;
  638. parent = null;
  639. background_image = null;
  640. text = string.Empty;
  641. name = string.Empty;
  642. }
  643. public Control(Control parent, string text) : this() {
  644. Text=text;
  645. Parent=parent;
  646. }
  647. public Control(Control parent, string text, int left, int top, int width, int height) : this() {
  648. Parent=parent;
  649. bounds.X=left;
  650. bounds.Y=top;
  651. bounds.Width=width;
  652. bounds.Height=height;
  653. SetBoundsCore(left, top, width, height, BoundsSpecified.All);
  654. Text=text;
  655. }
  656. public Control(string text) : this() {
  657. Text=text;
  658. }
  659. public Control(string text, int left, int top, int width, int height) : this() {
  660. bounds.X=left;
  661. bounds.Y=top;
  662. bounds.Width=width;
  663. bounds.Height=height;
  664. SetBoundsCore(left, top, width, height, BoundsSpecified.All);
  665. Text=text;
  666. }
  667. protected override void Dispose(bool disposing) {
  668. is_disposed = true;
  669. if (dc_mem!=null) {
  670. dc_mem.Dispose();
  671. dc_mem=null;
  672. }
  673. if (bmp_mem!=null) {
  674. bmp_mem.Dispose();
  675. bmp_mem=null;
  676. }
  677. DestroyHandle();
  678. }
  679. #endregion // Public Constructors
  680. #region Internal Properties
  681. #endregion // Internal Properties
  682. #region Private & Internal Methods
  683. internal static IAsyncResult BeginInvokeInternal (Delegate method, object [] args) {
  684. AsyncMethodResult result = new AsyncMethodResult ();
  685. AsyncMethodData data = new AsyncMethodData ();
  686. data.Method = method;
  687. data.Args = args;
  688. data.Result = new WeakReference (result);
  689. XplatUI.SendAsyncMethod (data);
  690. return result;
  691. }
  692. internal Graphics DeviceContext {
  693. get {
  694. if (dc_mem==null) {
  695. CreateBuffers(this.Width, this.Height);
  696. }
  697. return dc_mem;
  698. }
  699. }
  700. internal Bitmap ImageBuffer {
  701. get {
  702. if (bmp_mem==null) {
  703. CreateBuffers(this.Width, this.Height);
  704. }
  705. return bmp_mem;
  706. }
  707. }
  708. internal void CreateBuffers (int width, int height) {
  709. if (double_buffering == false)
  710. return;
  711. if (dc_mem != null)
  712. dc_mem.Dispose ();
  713. if (bmp_mem != null)
  714. bmp_mem.Dispose ();
  715. if (width < 1) {
  716. width = 1;
  717. }
  718. if (height < 1) {
  719. height = 1;
  720. }
  721. bmp_mem = new Bitmap (width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  722. dc_mem = Graphics.FromImage (bmp_mem);
  723. }
  724. internal static void SetChildColor(Control parent) {
  725. Control child;
  726. for (int i=0; i < parent.child_controls.Count; i++) {
  727. child=parent.child_controls[i];
  728. if (child.IsHandleCreated) {
  729. XplatUI.SetWindowBackground(child.window.Handle, child.BackColor);
  730. }
  731. if (child.child_controls.Count>0) {
  732. SetChildColor(child);
  733. }
  734. }
  735. }
  736. private bool Select(Control control) {
  737. Control parent;
  738. if (control == null) {
  739. return false;
  740. }
  741. parent = control.parent;
  742. if (((control.control_style & ControlStyles.Selectable) !=0) && (parent != null)) {
  743. while (parent != null) {
  744. if (!parent.is_visible || !parent.is_enabled) {
  745. return false;
  746. }
  747. }
  748. }
  749. control.is_selected = true;
  750. return true;
  751. }
  752. private Control FindTabStop(Control control, bool forward) {
  753. if (control == null) {
  754. return null;
  755. }
  756. return null;
  757. }
  758. internal virtual void DoDefaultAction() {
  759. // Only here to be overriden by our actual controls; this is needed by the accessibility class
  760. }
  761. internal static int LowOrder (int param) {
  762. return (param & 0xffff);
  763. }
  764. internal static int HighOrder (int param) {
  765. return (param >> 16);
  766. }
  767. internal static MouseButtons FromParamToMouseButtons (int param) {
  768. MouseButtons buttons = MouseButtons.None;
  769. if ((param & (int) MsgButtons.MK_LBUTTON) != 0)
  770. buttons |= MouseButtons.Left;
  771. if ((param & (int) MsgButtons.MK_MBUTTON) != 0)
  772. buttons |= MouseButtons.Middle;
  773. if ((param & (int) MsgButtons.MK_RBUTTON) != 0)
  774. buttons |= MouseButtons.Right;
  775. return buttons;
  776. }
  777. #endregion // Private & Internal Methods
  778. #region Public Static Properties
  779. public static Color DefaultBackColor {
  780. get {
  781. return ThemeEngine.Current.DefaultControlBackColor;
  782. }
  783. }
  784. public static Font DefaultFont {
  785. get {
  786. return ThemeEngine.Current.DefaultFont;
  787. }
  788. }
  789. public static Color DefaultForeColor {
  790. get {
  791. return ThemeEngine.Current.DefaultControlForeColor;
  792. }
  793. }
  794. public static Keys ModifierKeys {
  795. get {
  796. return XplatUI.State.ModifierKeys;
  797. }
  798. }
  799. public static MouseButtons MouseButtons {
  800. get {
  801. return XplatUI.State.MouseButtons;
  802. }
  803. }
  804. public static Point MousePosition {
  805. get {
  806. return Cursor.Position;
  807. }
  808. }
  809. #endregion // Public Static Properties
  810. #region Public Instance Properties
  811. public AccessibleObject AccessibilityObject {
  812. get {
  813. if (accessibility_object==null) {
  814. accessibility_object=CreateAccessibilityInstance();
  815. }
  816. return accessibility_object;
  817. }
  818. }
  819. public string AccessibleDefaultActionDescription {
  820. get {
  821. return AccessibilityObject.default_action;
  822. }
  823. set {
  824. AccessibilityObject.default_action=value;
  825. }
  826. }
  827. public string AccessibleDescription {
  828. get {
  829. return AccessibilityObject.description;
  830. }
  831. set {
  832. AccessibilityObject.description=value;
  833. }
  834. }
  835. public string AccessibleName {
  836. get {
  837. return AccessibilityObject.Name;
  838. }
  839. set {
  840. AccessibilityObject.Name=value;
  841. }
  842. }
  843. public AccessibleRole AccessibleRole {
  844. get {
  845. return AccessibilityObject.role;
  846. }
  847. set {
  848. AccessibilityObject.role=value;
  849. }
  850. }
  851. public virtual bool AllowDrop {
  852. get {
  853. return XplatUI.State.DropTarget;
  854. }
  855. set {
  856. XplatUI.State.DropTarget=value;
  857. }
  858. }
  859. public virtual AnchorStyles Anchor {
  860. get {
  861. return anchor_style;
  862. }
  863. set {
  864. anchor_style=value;
  865. if (parent != null) {
  866. parent.PerformLayout(this, "Parent");
  867. }
  868. }
  869. }
  870. public virtual Color BackColor {
  871. get {
  872. if (background_color.IsEmpty) {
  873. if (parent!=null) {
  874. return parent.BackColor;
  875. }
  876. return DefaultBackColor;
  877. }
  878. return background_color;
  879. }
  880. set {
  881. background_color=value;
  882. if (this.IsHandleCreated) {
  883. XplatUI.SetWindowBackground(this.window.Handle, value);
  884. }
  885. SetChildColor(this);
  886. OnBackColorChanged(EventArgs.Empty);
  887. Refresh();
  888. }
  889. }
  890. public virtual Image BackgroundImage {
  891. get {
  892. return background_image;
  893. }
  894. set {
  895. if (background_image!=value) {
  896. background_image=value;
  897. OnBackgroundImageChanged(EventArgs.Empty);
  898. }
  899. }
  900. }
  901. public virtual BindingContext BindingContext {
  902. get {
  903. throw new NotImplementedException();
  904. }
  905. set {
  906. throw new NotImplementedException();
  907. }
  908. }
  909. public int Bottom {
  910. get {
  911. return bounds.Y+bounds.Height;
  912. }
  913. }
  914. public Rectangle Bounds {
  915. get {
  916. return this.bounds;
  917. }
  918. set {
  919. SetBoundsCore(value.Left, value.Top, value.Width, value.Height, BoundsSpecified.All);
  920. }
  921. }
  922. public bool CanFocus {
  923. get {
  924. if (is_visible && is_enabled && GetStyle(ControlStyles.Selectable)) {
  925. return true;
  926. }
  927. return false;
  928. }
  929. }
  930. public bool CanSelect {
  931. get {
  932. Control parent;
  933. if (!GetStyle(ControlStyles.Selectable) || this.parent == null) {
  934. return false;
  935. }
  936. parent = this.parent;
  937. while (parent != null) {
  938. if (!parent.is_visible || !parent.is_enabled) {
  939. return false;
  940. }
  941. parent = parent.parent;
  942. }
  943. return true;
  944. }
  945. }
  946. public bool Capture {
  947. get {
  948. return this.is_captured;
  949. }
  950. set {
  951. if (this.IsHandleCreated) {
  952. if (value && !is_captured) {
  953. is_captured = true;
  954. XplatUI.GrabWindow(this.window.Handle, IntPtr.Zero);
  955. } else if (!value && is_captured) {
  956. XplatUI.ReleaseWindow(this.window.Handle);
  957. is_captured = false;
  958. }
  959. }
  960. }
  961. }
  962. public bool CausesValidation {
  963. get {
  964. return this.causes_validation;
  965. }
  966. set {
  967. if (this.causes_validation != value) {
  968. causes_validation = value;
  969. OnCausesValidationChanged(EventArgs.Empty);
  970. }
  971. }
  972. }
  973. public Rectangle ClientRectangle {
  974. get {
  975. client_rect.Width = client_size.Width;
  976. client_rect.Height = client_size.Height;
  977. return client_rect;
  978. }
  979. }
  980. public Size ClientSize {
  981. get {
  982. return client_size;
  983. }
  984. set {
  985. this.SetClientSizeCore(value.Width, value.Height);
  986. }
  987. }
  988. public String CompanyName {
  989. get {
  990. return "Mono Project, Novell, Inc.";
  991. }
  992. }
  993. public bool ContainsFocus {
  994. get {
  995. if (this.Focused) {
  996. return true;
  997. }
  998. for (int i=0; i < child_controls.Count; i++) {
  999. if (child_controls[i].Focused) {
  1000. return true;
  1001. }
  1002. }
  1003. return false;
  1004. }
  1005. }
  1006. #if notdef
  1007. public virtual ContextMenu ContextMenu {
  1008. get {
  1009. throw new NotImplementedException();
  1010. }
  1011. set {
  1012. throw new NotImplementedException();
  1013. }
  1014. }
  1015. #endif
  1016. public ControlCollection Controls {
  1017. get {
  1018. return this.child_controls;
  1019. }
  1020. }
  1021. public bool Created {
  1022. get {
  1023. if (!this.is_disposed && (this.window.Handle != IntPtr.Zero)) {
  1024. return true;
  1025. }
  1026. return false;
  1027. }
  1028. }
  1029. #if notdef
  1030. public virtual Cursor Cursor {
  1031. get {
  1032. throw new NotImplementedException();
  1033. }
  1034. set {
  1035. throw new NotImplementedException();
  1036. }
  1037. }
  1038. public ControlBidingsCollection DataBindings {
  1039. get {
  1040. throw new NotImplementedException();
  1041. }
  1042. }
  1043. #endif
  1044. public virtual Rectangle DisplayRectangle {
  1045. get {
  1046. return ClientRectangle;
  1047. }
  1048. }
  1049. public bool Disposing {
  1050. get {
  1051. return is_disposed;
  1052. }
  1053. }
  1054. public virtual DockStyle Dock {
  1055. get {
  1056. return dock_style;
  1057. }
  1058. set {
  1059. if (dock_style == value) {
  1060. return;
  1061. }
  1062. dock_style = value;
  1063. if (parent != null) {
  1064. parent.PerformLayout(this, "Parent");
  1065. }
  1066. OnDockChanged(EventArgs.Empty);
  1067. }
  1068. }
  1069. public bool Enabled {
  1070. get {
  1071. return is_enabled;
  1072. }
  1073. set {
  1074. if (is_enabled == value) {
  1075. return;
  1076. }
  1077. is_enabled = value;
  1078. Refresh();
  1079. OnEnabledChanged (EventArgs.Empty);
  1080. }
  1081. }
  1082. public virtual bool Focused {
  1083. get {
  1084. return this.has_focus;
  1085. }
  1086. }
  1087. public virtual Font Font {
  1088. get {
  1089. if (font != null) {
  1090. return font;
  1091. }
  1092. if (Parent != null && Parent.Font != null) {
  1093. return Parent.Font;
  1094. }
  1095. return DefaultFont;
  1096. }
  1097. set {
  1098. if (font == value) {
  1099. return;
  1100. }
  1101. font = value;
  1102. Refresh();
  1103. OnFontChanged (EventArgs.Empty);
  1104. }
  1105. }
  1106. public virtual Color ForeColor {
  1107. get {
  1108. if (foreground_color.IsEmpty) {
  1109. if (parent!=null) {
  1110. return parent.ForeColor;
  1111. }
  1112. return DefaultForeColor;
  1113. }
  1114. return foreground_color;
  1115. }
  1116. set {
  1117. foreground_color=value;
  1118. Refresh();
  1119. }
  1120. }
  1121. public IntPtr Handle { // IWin32Window
  1122. get {
  1123. if (!IsHandleCreated) {
  1124. CreateHandle();
  1125. }
  1126. return window.Handle;
  1127. }
  1128. }
  1129. public bool HasChildren {
  1130. get {
  1131. if (this.child_controls.Count>0) {
  1132. return true;
  1133. }
  1134. return false;
  1135. }
  1136. }
  1137. public int Height {
  1138. get {
  1139. return this.bounds.Height;
  1140. }
  1141. set {
  1142. SetBoundsCore(bounds.X, bounds.Y, bounds.Width, value, BoundsSpecified.Height);
  1143. }
  1144. }
  1145. public ImeMode ImeMode {
  1146. get {
  1147. return ime_mode;
  1148. }
  1149. set {
  1150. ime_mode = value;
  1151. }
  1152. }
  1153. public bool InvokeRequired { // ISynchronizeInvoke
  1154. get {
  1155. if (creator_thread!=Thread.CurrentThread) {
  1156. return true;
  1157. }
  1158. return false;
  1159. }
  1160. }
  1161. public bool IsAccessible {
  1162. get {
  1163. return is_accessible;
  1164. }
  1165. set {
  1166. is_accessible = value;
  1167. }
  1168. }
  1169. public bool IsDisposed {
  1170. get {
  1171. return this.is_disposed;
  1172. }
  1173. }
  1174. public bool IsHandleCreated {
  1175. get {
  1176. if ((window!=null) && (window.Handle!=IntPtr.Zero)) {
  1177. return true;
  1178. }
  1179. return false;
  1180. }
  1181. }
  1182. public int Left {
  1183. get {
  1184. return this.bounds.X;
  1185. }
  1186. set {
  1187. SetBoundsCore(value, bounds.Y, bounds.Width, bounds.Height, BoundsSpecified.X);
  1188. }
  1189. }
  1190. public Point Location {
  1191. get {
  1192. return new Point(bounds.X, bounds.Y);
  1193. }
  1194. set {
  1195. SetBoundsCore(value.X, value.Y, bounds.Width, bounds.Height, BoundsSpecified.Location);
  1196. }
  1197. }
  1198. public string Name {
  1199. get {
  1200. return this.name;
  1201. }
  1202. set {
  1203. this.name=value;
  1204. }
  1205. }
  1206. public Control Parent {
  1207. get {
  1208. return this.parent;
  1209. }
  1210. set {
  1211. if (parent!=value) {
  1212. if (parent!=null) {
  1213. parent.Controls.Remove(this);
  1214. }
  1215. parent=value;
  1216. if (!parent.Controls.Contains(this)) {
  1217. parent.Controls.Add(this);
  1218. }
  1219. XplatUI.SetParent(Handle, value.Handle);
  1220. InitLayout();
  1221. }
  1222. }
  1223. }
  1224. public string ProductName {
  1225. get {
  1226. return "Novell Mono .NET Framework";
  1227. }
  1228. }
  1229. public string ProductVersion {
  1230. get {
  1231. return "1.1.4322.573";
  1232. }
  1233. }
  1234. public bool RecreatingHandle {
  1235. get {
  1236. return is_recreating;
  1237. }
  1238. }
  1239. public Region Region {
  1240. get {
  1241. return new Region(this.bounds);
  1242. }
  1243. set {
  1244. Graphics g;
  1245. RectangleF r;
  1246. g = Graphics.FromHwnd(this.window.Handle);
  1247. r = value.GetBounds(g);
  1248. SetBounds((int)r.X, (int)r.Y, (int)r.Width, (int)r.Height);
  1249. g.Dispose();
  1250. }
  1251. }
  1252. public int Right {
  1253. get {
  1254. return this.bounds.X+this.bounds.Width;
  1255. }
  1256. }
  1257. public virtual RightToLeft RightToLeft {
  1258. get {
  1259. return right_to_left;
  1260. }
  1261. set {
  1262. if (value != right_to_left) {
  1263. right_to_left = value;
  1264. OnRightToLeftChanged(EventArgs.Empty);
  1265. }
  1266. }
  1267. }
  1268. public override ISite Site {
  1269. get {
  1270. return base.Site;
  1271. }
  1272. set {
  1273. base.Site = value;
  1274. }
  1275. }
  1276. public Size Size {
  1277. get {
  1278. return new Size(Width, Height);
  1279. }
  1280. set {
  1281. SetBoundsCore(bounds.X, bounds.Y, value.Width, value.Height, BoundsSpecified.Size);
  1282. }
  1283. }
  1284. public int TabIndex {
  1285. get {
  1286. return tab_index;
  1287. }
  1288. set {
  1289. tab_index = value;
  1290. }
  1291. }
  1292. public bool TabStop {
  1293. get {
  1294. return tab_stop;
  1295. }
  1296. set {
  1297. tab_stop = value;
  1298. }
  1299. }
  1300. public object Tag {
  1301. get {
  1302. return control_tag;
  1303. }
  1304. set {
  1305. control_tag = value;
  1306. }
  1307. }
  1308. public virtual string Text {
  1309. get {
  1310. return this.text;
  1311. }
  1312. set {
  1313. if (text!=value) {
  1314. text=value;
  1315. XplatUI.Text(Handle, text);
  1316. // FIXME: Do we need a Refresh() here?
  1317. OnTextChanged (EventArgs.Empty);
  1318. }
  1319. }
  1320. }
  1321. public int Top {
  1322. get {
  1323. return this.bounds.Y;
  1324. }
  1325. set {
  1326. SetBoundsCore(bounds.X, value, bounds.Width, bounds.Height, BoundsSpecified.Y);
  1327. }
  1328. }
  1329. public Control TopLevelControl {
  1330. get {
  1331. Control p = this;
  1332. while (p.parent != null) {
  1333. p = p.parent;
  1334. }
  1335. return p;
  1336. }
  1337. }
  1338. public bool Visible {
  1339. get {
  1340. if (!is_visible) {
  1341. return false;
  1342. }
  1343. if (parent != null) {
  1344. return parent.Visible;
  1345. }
  1346. return true;
  1347. }
  1348. set {
  1349. SetVisibleCore(value);
  1350. }
  1351. }
  1352. public int Width {
  1353. get {
  1354. return this.bounds.Width;
  1355. }
  1356. set {
  1357. SetBoundsCore(bounds.X, bounds.Y, value, bounds.Height, BoundsSpecified.Width);
  1358. }
  1359. }
  1360. public IWindowTarget WindowTarget {
  1361. get {
  1362. return null;
  1363. }
  1364. set {
  1365. ;
  1366. }
  1367. }
  1368. #endregion // Public Instance Properties
  1369. #region Protected Instance Properties
  1370. protected virtual CreateParams CreateParams {
  1371. get {
  1372. CreateParams create_params = new CreateParams();
  1373. create_params.Caption = Text;
  1374. create_params.X = Left;
  1375. create_params.Y = Top;
  1376. create_params.Width = Width;
  1377. create_params.Height = Height;
  1378. create_params.ClassName = XplatUI.DefaultClassName;
  1379. create_params.ClassStyle = 0;
  1380. create_params.ExStyle = 0;
  1381. create_params.Param = 0;
  1382. if (parent!=null) {
  1383. create_params.Parent = parent.Handle;
  1384. }
  1385. create_params.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
  1386. if (is_visible) {
  1387. create_params.Style |= (int)WindowStyles.WS_VISIBLE;
  1388. }
  1389. return create_params;
  1390. }
  1391. }
  1392. protected virtual ImeMode DefaultImeMode {
  1393. get {
  1394. return ImeMode.Inherit;
  1395. }
  1396. }
  1397. protected virtual Size DefaultSize {
  1398. get {
  1399. return new Size(100, 23);
  1400. }
  1401. }
  1402. protected int FontHeight {
  1403. get {
  1404. return Font.Height;
  1405. }
  1406. set {
  1407. ;; // Nothing to do
  1408. }
  1409. }
  1410. protected bool RenderRightToLeft {
  1411. get {
  1412. return (this.right_to_left == RightToLeft.Yes);
  1413. }
  1414. set {
  1415. ;; // Nothing to do?
  1416. }
  1417. }
  1418. protected bool ResizeRedraw {
  1419. get {
  1420. return GetStyle(ControlStyles.ResizeRedraw);
  1421. }
  1422. set {
  1423. SetStyle(ControlStyles.ResizeRedraw, value);
  1424. }
  1425. }
  1426. protected virtual bool ShowFocusCues {
  1427. get {
  1428. return true;
  1429. }
  1430. }
  1431. protected bool ShowKeyboardCues {
  1432. get {
  1433. return true;
  1434. }
  1435. }
  1436. #endregion // Protected Instance Properties
  1437. #region Public Static Methods
  1438. public static Control FromChildHandle(IntPtr handle) {
  1439. IEnumerator control = Control.controls.GetEnumerator();
  1440. while (control.MoveNext()) {
  1441. if (((Control)control.Current).window.Handle == handle) {
  1442. // Found it
  1443. if (((Control)control.Current).Parent != null) {
  1444. return ((Control)control.Current).Parent;
  1445. }
  1446. }
  1447. }
  1448. return null;
  1449. }
  1450. public static Control FromHandle(IntPtr handle) {
  1451. IEnumerator control = Control.controls.GetEnumerator();
  1452. while (control.MoveNext()) {
  1453. if (((Control)control.Current).window.Handle == handle) {
  1454. // Found it
  1455. return ((Control)control.Current);
  1456. }
  1457. }
  1458. return null;
  1459. }
  1460. public static bool IsMnemonic(char charCode, string text) {
  1461. int amp;
  1462. amp = text.IndexOf('&');
  1463. if (amp != -1) {
  1464. if (charCode == text.ToCharArray(amp, 1)[0]) {
  1465. return true;
  1466. }
  1467. }
  1468. return false;
  1469. }
  1470. #endregion
  1471. #region Protected Static Methods
  1472. protected static bool ReflectMessage(IntPtr hWnd, ref Message m) {
  1473. Control c;
  1474. c = Control.FromHandle(hWnd);
  1475. if (c != null) {
  1476. c.WndProc(ref m);
  1477. return true;
  1478. }
  1479. return false;
  1480. }
  1481. #endregion
  1482. #region Public Instance Methods
  1483. public IAsyncResult BeginInvoke(Delegate method) {
  1484. return BeginInvokeInternal(method, null);
  1485. }
  1486. public IAsyncResult BeginInvoke (Delegate method, object[] args) {
  1487. return BeginInvokeInternal (method, args);
  1488. }
  1489. public void BringToFront() {
  1490. // Need to update the child list of our parent
  1491. if ((parent != null) && (parent.child_controls[0]!=this)) {
  1492. if (parent.child_controls.Contains(this)) {
  1493. parent.child_controls.SetChildIndex(this, 0);
  1494. }
  1495. }
  1496. XplatUI.SetZOrder(this.window.Handle, IntPtr.Zero, true, false);
  1497. if (parent != null) {
  1498. parent.Refresh();
  1499. }
  1500. }
  1501. public void SendToBack() {
  1502. // Need to update the child list of our parent
  1503. if ((parent != null) && (parent.child_controls[parent.child_controls.Count-1]!=this)) {
  1504. if (parent.child_controls.Contains(this)) {
  1505. parent.child_controls.SetChildIndex(this, parent.child_controls.Count);
  1506. }
  1507. }
  1508. XplatUI.SetZOrder(this.window.Handle, IntPtr.Zero, false, true);
  1509. if (parent != null) {
  1510. parent.Refresh();
  1511. }
  1512. }
  1513. public bool Contains(Control ctl) {
  1514. Control current;
  1515. current=ctl;
  1516. while (current!=null) {
  1517. if (current==ctl) {
  1518. return true;
  1519. }
  1520. current=current.parent;
  1521. }
  1522. return false;
  1523. }
  1524. public void CreateControl() {
  1525. Control child;
  1526. CreateHandle();
  1527. for (int i=0; i<child_controls.Count; i++) {
  1528. child_controls[i].CreateControl();
  1529. }
  1530. OnCreateControl();
  1531. }
  1532. public Graphics CreateGraphics() {
  1533. return Graphics.FromHwnd(this.window.Handle);
  1534. }
  1535. [MonoTODO("Come up with cross platform drag-drop driver interface")]
  1536. public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects) {
  1537. return DragDropEffects.None;
  1538. }
  1539. public object EndInvoke (IAsyncResult async_result) {
  1540. AsyncMethodResult result = (AsyncMethodResult) async_result;
  1541. return result.EndInvoke ();
  1542. }
  1543. public Control GetChildAtPoint(Point pt) {
  1544. // Microsoft's version of this function doesn't seem to work, so I can't check
  1545. // if we only consider children or also grandchildren, etc.
  1546. // I'm gonna say 'children only'
  1547. for (int i=0; i<child_controls.Count; i++) {
  1548. if (child_controls[i].Bounds.Contains(pt)) {
  1549. return child_controls[i];
  1550. }
  1551. }
  1552. return null;
  1553. }
  1554. public IContainerControl GetContainerControl() {
  1555. Control current = this;
  1556. while (current!=null) {
  1557. if ((current.control_style & ControlStyles.ContainerControl)!=0) {
  1558. return (IContainerControl)current;
  1559. }
  1560. current = current.parent;
  1561. }
  1562. return null;
  1563. }
  1564. public void Hide() {
  1565. this.Visible = false;
  1566. }
  1567. public void Invalidate() {
  1568. Invalidate(ClientRectangle, false);
  1569. }
  1570. public void Invalidate(bool invalidateChildren) {
  1571. Invalidate(ClientRectangle, invalidateChildren);
  1572. }
  1573. public void Invalidate(System.Drawing.Rectangle rc) {
  1574. Invalidate(rc, false);
  1575. }
  1576. public void Invalidate(System.Drawing.Rectangle rc, bool invalidateChildren) {
  1577. if (!IsHandleCreated || !is_visible) {
  1578. return;
  1579. }
  1580. XplatUI.Invalidate(Handle, rc, false);
  1581. if (invalidateChildren) {
  1582. for (int i=0; i<child_controls.Count; i++) child_controls[i].Invalidate();
  1583. }
  1584. }
  1585. public void Invalidate(System.Drawing.Region region) {
  1586. Invalidate(region, false);
  1587. }
  1588. [MonoTODO("Figure out if GetRegionScans is usable")]
  1589. public void Invalidate(System.Drawing.Region region, bool invalidateChildren) {
  1590. throw new NotImplementedException();
  1591. // FIXME - should use the GetRegionScans function of the region to invalidate each area
  1592. if (invalidateChildren) {
  1593. for (int i=0; i<child_controls.Count; i++) child_controls[i].Invalidate();
  1594. }
  1595. }
  1596. public object Invoke (Delegate method) {
  1597. return Invoke(method, null);
  1598. }
  1599. public object Invoke (Delegate method, object[] args) {
  1600. IAsyncResult result = BeginInvoke (method, args);
  1601. return EndInvoke(result);
  1602. }
  1603. public void PerformLayout() {
  1604. PerformLayout(null, null);
  1605. }
  1606. public void PerformLayout(Control affectedControl, string affectedProperty) {
  1607. LayoutEventArgs levent = new LayoutEventArgs(affectedControl, affectedProperty);
  1608. if (layout_suspended>0) {
  1609. layout_pending = true;
  1610. return;
  1611. }
  1612. layout_pending = false;
  1613. // Prevent us from getting messed up
  1614. layout_suspended++;
  1615. // Perform all Dock and Anchor calculations
  1616. try {
  1617. Control child;
  1618. AnchorStyles anchor;
  1619. Rectangle space;
  1620. int diff_width;
  1621. int diff_height;
  1622. space=this.DisplayRectangle;
  1623. if (prev_size != Size.Empty) {
  1624. diff_width = space.Width - prev_size.Width;
  1625. diff_height = space.Height - prev_size.Height;
  1626. } else {
  1627. diff_width = 0;
  1628. diff_height = 0;
  1629. }
  1630. // Deal with docking
  1631. for (int i=0; i < child_controls.Count; i++) {
  1632. child=child_controls[i];
  1633. switch (child.Dock) {
  1634. case DockStyle.None: {
  1635. // Do nothing
  1636. break;
  1637. }
  1638. case DockStyle.Left: {
  1639. child.SetBounds(space.Left, space.Y, child.Width, space.Height);
  1640. space.X+=child.Width;
  1641. space.Width-=child.Width;
  1642. break;
  1643. }
  1644. case DockStyle.Top: {
  1645. child.SetBounds(space.Left, space.Y, space.Width, child.Height);
  1646. space.Y+=child.Height;
  1647. space.Height-=child.Height;
  1648. break;
  1649. }
  1650. case DockStyle.Right: {
  1651. child.SetBounds(space.Right-child.Width, space.Y, child.Width, space.Height);
  1652. space.Width-=child.Width;
  1653. break;
  1654. }
  1655. case DockStyle.Bottom: {
  1656. child.SetBounds(space.Left, space.Bottom-child.Height, space.Width, child.Height);
  1657. space.Height-=child.Height;
  1658. break;
  1659. }
  1660. case DockStyle.Fill: {
  1661. child.SetBounds(space.Left, space.Top, space.Width, space.Height);
  1662. space.Width=0;
  1663. space.Height=0;
  1664. break;
  1665. }
  1666. }
  1667. }
  1668. space=this.DisplayRectangle;
  1669. // Deal with anchoring
  1670. for (int i=0; i < child_controls.Count; i++) {
  1671. int left;
  1672. int top;
  1673. int width;
  1674. int height;
  1675. child=child_controls[i];
  1676. anchor=child.Anchor;
  1677. left=child.Left;
  1678. top=child.Top;
  1679. width=child.Width;
  1680. height=child.Height;
  1681. // If the control is docked we don't need to do anything
  1682. if (child.Dock != DockStyle.None) {
  1683. continue;
  1684. }
  1685. if ((anchor & AnchorStyles.Left) !=0 ) {
  1686. if ((anchor & AnchorStyles.Right) != 0) {
  1687. // Anchoring to left and right
  1688. width=width+diff_width;
  1689. } else {
  1690. ; // nothing to do
  1691. }
  1692. } else if ((anchor & AnchorStyles.Right) != 0) {
  1693. left+=diff_width;
  1694. } else {
  1695. left+=diff_width/2;
  1696. }
  1697. if ((anchor & AnchorStyles.Top) !=0 ) {
  1698. if ((anchor & AnchorStyles.Bottom) != 0) {
  1699. height+=diff_height;
  1700. } else {
  1701. ; // nothing to do
  1702. }
  1703. } else if ((anchor & AnchorStyles.Bottom) != 0) {
  1704. top+=diff_height;
  1705. } else {
  1706. top+=diff_height/2;
  1707. }
  1708. // Sanity
  1709. if (width < 0) {
  1710. width=0;
  1711. }
  1712. if (height < 0) {
  1713. height=0;
  1714. }
  1715. child.SetBounds(left, top, width, height);
  1716. }
  1717. // Let everyone know
  1718. OnLayout(levent);
  1719. }
  1720. // Need to make sure we decremend layout_suspended
  1721. finally {
  1722. layout_suspended--;
  1723. }
  1724. }
  1725. public Point PointToClient (Point p) {
  1726. int x = p.X;
  1727. int y = p.Y;
  1728. XplatUI.ScreenToClient (Handle, ref x, ref y);
  1729. return new Point (x, y);
  1730. }
  1731. public virtual void Refresh() {
  1732. if (IsHandleCreated == true) {
  1733. XplatUI.RefreshWindow(window.Handle);
  1734. }
  1735. }
  1736. public void ResumeLayout() {
  1737. ResumeLayout (true);
  1738. }
  1739. public void ResumeLayout(bool performLayout) {
  1740. layout_suspended--;
  1741. if (layout_suspended > 0) {
  1742. return;
  1743. }
  1744. if (performLayout || layout_pending) {
  1745. PerformLayout();
  1746. }
  1747. }
  1748. public void Select() {
  1749. Select(false, false);
  1750. }
  1751. [MonoTODO("Finish")]
  1752. public bool SelectNextControl(Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) {
  1753. return false;
  1754. }
  1755. public void SetBounds(int x, int y, int width, int height) {
  1756. SetBoundsCore(x, y, width, height, BoundsSpecified.All);
  1757. }
  1758. public void SetBounds(int x, int y, int width, int height, BoundsSpecified specified) {
  1759. SetBoundsCore(x, y, width, height, specified);
  1760. }
  1761. public void Show() {
  1762. if (!IsHandleCreated) {
  1763. this.CreateHandle();
  1764. }
  1765. this.Visible=true;
  1766. }
  1767. public void SuspendLayout() {
  1768. layout_suspended++;
  1769. }
  1770. #endregion // Public Instance Methods
  1771. #region Protected Instance Methods
  1772. protected virtual AccessibleObject CreateAccessibilityInstance() {
  1773. return new AccessibleObject(this);
  1774. }
  1775. protected virtual ControlCollection CreateControlsInstance() {
  1776. return new ControlCollection(this);
  1777. }
  1778. protected virtual void CreateHandle() {
  1779. if (IsDisposed) {
  1780. throw new ObjectDisposedException(Name);
  1781. }
  1782. if (IsHandleCreated) {
  1783. return;
  1784. }
  1785. if (window==null) {
  1786. window = new ControlNativeWindow(this);
  1787. window.CreateHandle(CreateParams);
  1788. }
  1789. if (window.Handle!=IntPtr.Zero) {
  1790. if (!controls.Contains(window.Handle)) {
  1791. controls.Add(window.Handle, this);
  1792. }
  1793. creator_thread = Thread.CurrentThread;
  1794. XplatUI.SetWindowBackground(window.Handle, this.BackColor);
  1795. OnHandleCreated(EventArgs.Empty);
  1796. }
  1797. }
  1798. protected virtual void DefWndProc(ref Message m) {
  1799. window.DefWndProc(ref m);
  1800. }
  1801. protected virtual void DestroyHandle() {
  1802. if (IsHandleCreated) {
  1803. if (Handle != IntPtr.Zero) {
  1804. controls.Remove(Handle);
  1805. }
  1806. if (window != null) {
  1807. window.DestroyHandle();
  1808. }
  1809. }
  1810. }
  1811. protected virtual void InitLayout() {
  1812. if (parent != null) {
  1813. parent.PerformLayout(this, "parent");
  1814. }
  1815. }
  1816. protected virtual bool ProcessKeyEventArgs (ref Message msg)
  1817. {
  1818. KeyEventArgs key_event;
  1819. switch (msg.Msg) {
  1820. case (int)Msg.WM_KEYDOWN: {
  1821. key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
  1822. OnKeyDown (key_event);
  1823. return key_event.Handled;
  1824. }
  1825. case (int)Msg.WM_KEYUP: {
  1826. key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
  1827. OnKeyUp (key_event);
  1828. return key_event.Handled;
  1829. }
  1830. default:
  1831. break;
  1832. }
  1833. return false;
  1834. }
  1835. protected virtual bool IsInputKey (Keys keyData)
  1836. {
  1837. return false;
  1838. }
  1839. protected virtual bool ProcessDialogKey (Keys keyData)
  1840. {
  1841. if (parent != null)
  1842. return Parent.ProcessDialogKey (keyData);
  1843. return false;
  1844. }
  1845. protected bool GetStyle(ControlStyles flag) {
  1846. return (control_style & flag) != 0;
  1847. }
  1848. protected virtual bool ProcessDialogChar(char charCode) {
  1849. throw new NotImplementedException();
  1850. }
  1851. protected virtual bool ProcessMnemonic(char charCode) {
  1852. throw new NotImplementedException();
  1853. }
  1854. protected void RecreateHandle() {
  1855. IEnumerator child = child_controls.GetEnumerator();
  1856. is_recreating=true;
  1857. if (IsHandleCreated) {
  1858. DestroyHandle();
  1859. CreateHandle();
  1860. // FIXME ZOrder?
  1861. while (child.MoveNext()) {
  1862. ((Control)child.Current).RecreateHandle();
  1863. }
  1864. } else {
  1865. CreateHandle();
  1866. }
  1867. is_recreating = false;
  1868. }
  1869. protected virtual void ScaleCore(float dx, float dy) {
  1870. throw new NotImplementedException();
  1871. }
  1872. protected virtual void Select(bool directed, bool forward) {
  1873. int index;
  1874. bool result;
  1875. if (!directed) {
  1876. // Select this control
  1877. Select(this);
  1878. }
  1879. if (parent == null) {
  1880. return;
  1881. }
  1882. index = parent.child_controls.IndexOf(this);
  1883. result = false;
  1884. do {
  1885. if (forward) {
  1886. if ((index+1) < parent.child_controls.Count) {
  1887. index++;
  1888. } else {
  1889. index = 0;
  1890. }
  1891. } else {
  1892. if (index>0) {
  1893. index++;
  1894. } else {
  1895. index = parent.child_controls.Count-1;
  1896. }
  1897. }
  1898. result = Select(parent.child_controls[index]);
  1899. } while (!result && parent.child_controls[index] != this);
  1900. }
  1901. protected virtual void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  1902. // SetBoundsCore updates the Win32 control itself. UpdateBounds updates the controls variables and fires events, I'm guessing - pdb
  1903. if ((specified & BoundsSpecified.X) != BoundsSpecified.X) {
  1904. x = Left;
  1905. }
  1906. if ((specified & BoundsSpecified.Y) != BoundsSpecified.Y) {
  1907. y = Top;
  1908. }
  1909. if ((specified & BoundsSpecified.Width)!= BoundsSpecified.Width) {
  1910. width = Width;
  1911. }
  1912. if ((specified & BoundsSpecified.Height) != BoundsSpecified.Height) {
  1913. height = Height;
  1914. }
  1915. if (IsHandleCreated) {
  1916. XplatUI.SetWindowPos(Handle, x, y, width, height);
  1917. }
  1918. UpdateBounds(x, y, width, height);
  1919. }
  1920. protected virtual void SetClientSizeCore(int x, int y) {
  1921. // Calculate the actual window size from the client size (it usually stays the same or grows)
  1922. Rectangle ClientRect;
  1923. Rectangle WindowRect;
  1924. CreateParams cp;
  1925. ClientRect = new Rectangle(0, 0, x, y);
  1926. cp = this.CreateParams;
  1927. if (XplatUI.CalculateWindowRect(Handle, ref ClientRect, cp.Style, false, out WindowRect)==false) {
  1928. return;
  1929. }
  1930. this.client_size = new Size(x, y);
  1931. SetBoundsCore(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
  1932. }
  1933. protected void SetStyle(ControlStyles flag, bool value) {
  1934. if (value) {
  1935. control_style |= flag;
  1936. } else {
  1937. control_style &= ~flag;
  1938. }
  1939. }
  1940. protected virtual void SetVisibleCore(bool value) {
  1941. if (value!=is_visible) {
  1942. is_visible=value;
  1943. XplatUI.SetVisible(Handle, value);
  1944. OnVisibleChanged(EventArgs.Empty);
  1945. if (!is_visible) {
  1946. if (dc_mem != null) {
  1947. dc_mem.Dispose();
  1948. dc_mem = null;
  1949. }
  1950. if (bmp_mem != null) {
  1951. bmp_mem.Dispose();
  1952. bmp_mem = null;
  1953. }
  1954. } else {
  1955. this.CreateBuffers(bounds.Width, bounds.Height);
  1956. }
  1957. // FIXME - deal with focus
  1958. if (parent != null) {
  1959. parent.PerformLayout(this, "visible");
  1960. } else {
  1961. PerformLayout(this, "visible");
  1962. }
  1963. }
  1964. }
  1965. protected void UpdateBounds() {
  1966. int x;
  1967. int y;
  1968. int width;
  1969. int height;
  1970. int client_width;
  1971. int client_height;
  1972. if (!IsHandleCreated) {
  1973. CreateHandle();
  1974. }
  1975. XplatUI.GetWindowPos(this.Handle, out x, out y, out width, out height, out client_width, out client_height);
  1976. UpdateBounds(x, y, width, height, client_width, client_height);
  1977. }
  1978. protected void UpdateBounds(int x, int y, int width, int height) {
  1979. // UpdateBounds only seems to set our sizes and fire events but not update the GUI window to match
  1980. bool moved = false;
  1981. bool resized = false;
  1982. int client_x_diff = this.bounds.Width-this.client_size.Width;
  1983. int client_y_diff = this.bounds.Height-this.client_size.Height;
  1984. // Needed to generate required notifications
  1985. if ((this.bounds.X!=x) || (this.bounds.Y!=y)) {
  1986. moved=true;
  1987. }
  1988. if ((this.Bounds.Width!=width) || (this.Bounds.Height!=height)) {
  1989. resized=true;
  1990. }
  1991. bounds.X=x;
  1992. bounds.Y=y;
  1993. bounds.Width=width;
  1994. bounds.Height=height;
  1995. // Update client rectangle as well
  1996. if (this.layout_suspended==0) {
  1997. prev_size.Width=client_size.Width;
  1998. prev_size.Height=client_size.Height;
  1999. }
  2000. client_size.Width=width-client_x_diff;
  2001. client_size.Height=height-client_y_diff;
  2002. if (moved) {
  2003. OnLocationChanged(EventArgs.Empty);
  2004. }
  2005. if (resized) {
  2006. OnSizeChanged(EventArgs.Empty);
  2007. }
  2008. }
  2009. protected void UpdateBounds(int x, int y, int width, int height, int clientWidth, int clientHeight) {
  2010. UpdateBounds(x, y, width, height);
  2011. this.client_size.Width=clientWidth;
  2012. this.client_size.Height=clientHeight;
  2013. }
  2014. [MonoTODO]
  2015. protected virtual void WndProc(ref Message m) {
  2016. EventArgs e = new EventArgs();
  2017. #if debug
  2018. Console.WriteLine("Received message {0}", m);
  2019. #endif
  2020. switch((Msg)m.Msg) {
  2021. #if notyet
  2022. // Mouse handling
  2023. case Msg.WM_LBUTTONDBLCLK: throw new NotImplementedException(); break;
  2024. case Msg.WM_RBUTTONDOWN: throw new NotImplementedException(); break;
  2025. case Msg.WM_RBUTTONUP: throw new NotImplementedException(); break;
  2026. case Msg.WM_RBUTTONDBLCLK: throw new NotImplementedException(); break;
  2027. case Msg.WM_MOUSEHOVER: throw new NotImplementedException(); break;
  2028. case Msg.WM_MOUSELEAVE: throw new NotImplementedException(); break;
  2029. // Keyboard handling
  2030. case Msg.WM_CHAR: throw new NotImplementedException(); break;
  2031. case Msg.WM_KEYDOWN: throw new NotImplementedException(); break;
  2032. case Msg.WM_KEYUP: throw new NotImplementedException(); break;
  2033. #endif
  2034. // Window management
  2035. case Msg.WM_WINDOWPOSCHANGED: {
  2036. if (XplatUI.IsVisible(m.HWnd)) {
  2037. UpdateBounds();
  2038. }
  2039. DefWndProc(ref m);
  2040. break;
  2041. }
  2042. case Msg.WM_PAINT: {
  2043. Rectangle rect;
  2044. PaintEventArgs paint_event;
  2045. paint_event = XplatUI.PaintEventStart(Handle);
  2046. OnPaint(paint_event);
  2047. XplatUI.PaintEventEnd(Handle);
  2048. DefWndProc(ref m);
  2049. break;
  2050. }
  2051. case Msg.WM_ERASEBKGND:{
  2052. if (GetStyle (ControlStyles.UserPaint)){
  2053. if (!GetStyle(ControlStyles.AllPaintingInWmPaint)) {
  2054. PaintEventArgs eraseEventArgs = new PaintEventArgs (Graphics.FromHdc (m.WParam), new Rectangle (new Point (0,0),Size));
  2055. OnPaintBackground (eraseEventArgs);
  2056. }
  2057. m.Result = (IntPtr)1;
  2058. }
  2059. else {
  2060. #if notdef
  2061. SolidBrush sb = new SolidBrush(this.BackColor);
  2062. this.DeviceContext.FillRectangle(sb, this.ClientRectangle);
  2063. sb.Dispose();
  2064. #endif
  2065. m.Result = IntPtr.Zero;
  2066. DefWndProc (ref m);
  2067. }
  2068. break;
  2069. }
  2070. case Msg.WM_LBUTTONUP: {
  2071. int clicks = 1;
  2072. OnMouseUp (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Left,
  2073. clicks,
  2074. LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  2075. 0));
  2076. break;
  2077. }
  2078. case Msg.WM_LBUTTONDOWN: {
  2079. int clicks = 1;
  2080. OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  2081. clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  2082. 0));
  2083. break;
  2084. }
  2085. case Msg.WM_MOUSEWHEEL: {
  2086. int clicks = 1;
  2087. OnMouseWheel (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  2088. clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  2089. HighOrder(m.WParam.ToInt32())));
  2090. break;
  2091. }
  2092. case Msg.WM_MOUSEMOVE: {
  2093. int clicks = 1;
  2094. OnMouseMove (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  2095. clicks,
  2096. LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  2097. 0));
  2098. break;
  2099. }
  2100. case Msg.WM_MOUSE_ENTER: {
  2101. if (is_entered) {
  2102. return;
  2103. }
  2104. is_entered = true;
  2105. OnMouseEnter(EventArgs.Empty);
  2106. break;
  2107. }
  2108. case Msg.WM_MOUSE_LEAVE: {
  2109. is_entered=false;
  2110. OnMouseLeave(EventArgs.Empty);
  2111. break;
  2112. }
  2113. case Msg.WM_MOUSEHOVER: {
  2114. OnMouseHover(EventArgs.Empty);
  2115. break;
  2116. }
  2117. case Msg.WM_SIZE: {
  2118. if (GetStyle(ControlStyles.ResizeRedraw)) {
  2119. Invalidate();
  2120. }
  2121. DefWndProc(ref m);
  2122. break;
  2123. }
  2124. case Msg.WM_KEYDOWN: {
  2125. if (!ProcessKeyEventArgs (ref m))
  2126. DefWndProc (ref m);
  2127. break;
  2128. }
  2129. case Msg.WM_KEYUP: {
  2130. if (!ProcessKeyEventArgs (ref m))
  2131. DefWndProc (ref m);
  2132. break;
  2133. }
  2134. #if notyet
  2135. case Msg.WM_WINDOWPOSCHANGED: throw new NotImplementedException(); break;
  2136. case Msg.WM_SYSCOLORCHANGE: throw new NotImplementedException(); break;
  2137. #endif
  2138. default:
  2139. DefWndProc(ref m);
  2140. break;
  2141. }
  2142. }
  2143. #endregion // Public Instance Methods
  2144. #region OnXXX methods
  2145. protected virtual void OnBackColorChanged(EventArgs e) {
  2146. if (BackColorChanged!=null) BackColorChanged(this, e);
  2147. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackColorChanged(e);
  2148. }
  2149. protected virtual void OnBackgroundImageChanged(EventArgs e) {
  2150. if (BackgroundImageChanged!=null) BackgroundImageChanged(this, e);
  2151. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackgroundImageChanged(e);
  2152. }
  2153. protected virtual void OnBindingContextChanged(EventArgs e) {
  2154. if (BindingContextChanged!=null) BindingContextChanged(this, e);
  2155. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBindingContextChanged(e);
  2156. }
  2157. protected virtual void OnCausesValidationChanged(EventArgs e) {
  2158. if (CausesValidationChanged!=null) CausesValidationChanged(this, e);
  2159. }
  2160. protected virtual void OnChangeUICues(UICuesEventArgs e) {
  2161. if (CausesValidationChanged!=null) CausesValidationChanged(this, e);
  2162. }
  2163. protected virtual void OnClick(EventArgs e) {
  2164. if (Click!=null) Click(this, e);
  2165. }
  2166. protected virtual void OnContextMenuChanged(EventArgs e) {
  2167. if (ContextMenuChanged!=null) ContextMenuChanged(this, e);
  2168. }
  2169. protected virtual void OnControlAdded(ControlEventArgs e) {
  2170. if (ControlAdded!=null) ControlAdded(this, e);
  2171. }
  2172. protected virtual void OnControlRemoved(ControlEventArgs e) {
  2173. if (ControlRemoved!=null) ControlRemoved(this, e);
  2174. }
  2175. protected virtual void OnCreateControl() {
  2176. // Override me!
  2177. }
  2178. protected virtual void OnCursorChanged(EventArgs e) {
  2179. if (CursorChanged!=null) CursorChanged(this, e);
  2180. }
  2181. protected virtual void OnDockChanged(EventArgs e) {
  2182. if (DockChanged!=null) DockChanged(this, e);
  2183. }
  2184. protected virtual void OnDoubleClick(EventArgs e) {
  2185. if (DoubleClick!=null) DoubleClick(this, e);
  2186. }
  2187. protected virtual void OnDragDrop(DragEventArgs drgevent) {
  2188. if (DragDrop!=null) DragDrop(this, drgevent);
  2189. }
  2190. protected virtual void OnDragEnter(DragEventArgs drgevent) {
  2191. if (DragEnter!=null) DragEnter(this, drgevent);
  2192. }
  2193. protected virtual void OnDragLeave(EventArgs e) {
  2194. if (DragLeave!=null) DragLeave(this, e);
  2195. }
  2196. protected virtual void OnDragOver(DragEventArgs drgevent) {
  2197. if (DragOver!=null) DragOver(this, drgevent);
  2198. }
  2199. protected virtual void OnEnabledChanged(EventArgs e) {
  2200. if (EnabledChanged!=null) EnabledChanged(this, e);
  2201. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentEnabledChanged(e);
  2202. }
  2203. protected virtual void OnEnter(EventArgs e) {
  2204. if (Enter!=null) Enter(this, e);
  2205. }
  2206. protected virtual void OnFontChanged(EventArgs e) {
  2207. if (FontChanged!=null) FontChanged(this, e);
  2208. }
  2209. protected virtual void OnForeColorChanged(EventArgs e) {
  2210. if (ForeColorChanged!=null) ForeColorChanged(this, e);
  2211. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentForeColorChanged(e);
  2212. }
  2213. protected virtual void OnGiveFeedback(GiveFeedbackEventArgs gfbevent) {
  2214. if (GiveFeedback!=null) GiveFeedback(this, gfbevent);
  2215. }
  2216. protected virtual void OnGotFocus(EventArgs e) {
  2217. if (GotFocus!=null) GotFocus(this, e);
  2218. }
  2219. protected virtual void OnHandleCreated(EventArgs e) {
  2220. if (HandleCreated!=null) HandleCreated(this, e);
  2221. }
  2222. protected virtual void OnHandleDestroyed(EventArgs e) {
  2223. if (HandleDestroyed!=null) HandleDestroyed(this, e);
  2224. }
  2225. protected virtual void OnHelpRequested(HelpEventArgs hevent) {
  2226. if (HelpRequested!=null) HelpRequested(this, hevent);
  2227. }
  2228. protected virtual void OnImeModeChanged(EventArgs e) {
  2229. if (ImeModeChanged!=null) ImeModeChanged(this, e);
  2230. }
  2231. protected virtual void OnInvalidated(InvalidateEventArgs e) {
  2232. if (Invalidated!=null) Invalidated(this, e);
  2233. }
  2234. protected virtual void OnKeyDown(KeyEventArgs e) {
  2235. if (KeyDown!=null) KeyDown(this, e);
  2236. }
  2237. protected virtual void OnKeyUp(KeyEventArgs e) {
  2238. if (KeyUp!=null) KeyUp(this, e);
  2239. }
  2240. protected virtual void OnLayout(LayoutEventArgs levent) {
  2241. if (Layout!=null) Layout(this, levent);
  2242. }
  2243. protected virtual void OnLeave(EventArgs e) {
  2244. if (Leave!=null) Leave(this, e);
  2245. }
  2246. protected virtual void OnLocationChanged(EventArgs e) {
  2247. if (LocationChanged!=null) LocationChanged(this, e);
  2248. }
  2249. protected virtual void OnLostFocus(EventArgs e) {
  2250. if (LostFocus!=null) LostFocus(this, e);
  2251. }
  2252. protected virtual void OnMouseDown(MouseEventArgs e) {
  2253. if (MouseDown!=null) MouseDown(this, e);
  2254. }
  2255. protected virtual void OnMouseEnter(EventArgs e) {
  2256. if (MouseEnter!=null) MouseEnter(this, e);
  2257. }
  2258. protected virtual void OnMouseHover(EventArgs e) {
  2259. if (MouseHover!=null) MouseHover(this, e);
  2260. }
  2261. protected virtual void OnMouseLeave(EventArgs e) {
  2262. if (MouseLeave!=null) MouseLeave(this, e);
  2263. }
  2264. protected virtual void OnMouseMove(MouseEventArgs e) {
  2265. if (MouseMove!=null) MouseMove(this, e);
  2266. }
  2267. protected virtual void OnMouseUp(MouseEventArgs e) {
  2268. if (MouseUp!=null) MouseUp(this, e);
  2269. }
  2270. protected virtual void OnMouseWheel(MouseEventArgs e) {
  2271. if (MouseWheel!=null) MouseWheel(this, e);
  2272. }
  2273. protected virtual void OnMove(EventArgs e) {
  2274. if (Move!=null) Move(this, e);
  2275. }
  2276. protected virtual void OnNotifyMessage(Message m) {
  2277. // Override me!
  2278. }
  2279. protected virtual void OnPaint(PaintEventArgs e) {
  2280. if (Paint!=null) Paint(this, e);
  2281. }
  2282. protected virtual void OnPaintBackground(PaintEventArgs pevent) {
  2283. // Override me!
  2284. }
  2285. protected virtual void OnParentBackColorChanged(EventArgs e) {
  2286. if (background_color.IsEmpty && background_image==null) {
  2287. Invalidate();
  2288. OnBackColorChanged(e);
  2289. }
  2290. }
  2291. protected virtual void OnParentBackgroundImageChanged(EventArgs e) {
  2292. if (background_color.IsEmpty && background_image==null) {
  2293. Invalidate();
  2294. OnBackgroundImageChanged(e);
  2295. }
  2296. }
  2297. protected virtual void OnParentBindingContextChanged(EventArgs e) {
  2298. if (binding_context==null) {
  2299. binding_context=Parent.binding_context;
  2300. OnBindingContextChanged(e);
  2301. }
  2302. }
  2303. protected virtual void OnParentChanged(EventArgs e) {
  2304. if (ParentChanged!=null) ParentChanged(this, e);
  2305. }
  2306. protected virtual void OnParentEnabledChanged(EventArgs e) {
  2307. if ((is_enabled && !Parent.is_enabled) || (!is_enabled && Parent.is_enabled)) {
  2308. is_enabled=false;
  2309. Invalidate();
  2310. EnabledChanged(this, e);
  2311. }
  2312. }
  2313. protected virtual void OnParentFontChanged(EventArgs e) {
  2314. if (font==null) {
  2315. Invalidate();
  2316. OnFontChanged(e);
  2317. }
  2318. }
  2319. protected virtual void OnParentForeColorChanged(EventArgs e) {
  2320. if (foreground_color.IsEmpty) {
  2321. Invalidate();
  2322. OnForeColorChanged(e);
  2323. }
  2324. }
  2325. protected virtual void OnParentRightToLeftChanged(EventArgs e) {
  2326. if (right_to_left==RightToLeft.Inherit) {
  2327. Invalidate();
  2328. OnRightToLeftChanged(e);
  2329. }
  2330. }
  2331. protected virtual void OnParentVisibleChanged(EventArgs e) {
  2332. if (is_visible!=Parent.is_visible) {
  2333. is_visible=false;
  2334. Invalidate();
  2335. OnVisibleChanged(e);
  2336. }
  2337. }
  2338. protected virtual void OnQueryContinueDrag(QueryContinueDragEventArgs e) {
  2339. if (QueryContinueDrag!=null) QueryContinueDrag(this, e);
  2340. }
  2341. protected virtual void OnResize(EventArgs e) {
  2342. if (Resize!=null) Resize(this, e);
  2343. PerformLayout(this, "bounds");
  2344. if (parent != null) {
  2345. parent.PerformLayout();
  2346. }
  2347. }
  2348. protected virtual void OnRightToLeftChanged(EventArgs e) {
  2349. if (RightToLeftChanged!=null) RightToLeftChanged(this, e);
  2350. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentRightToLeftChanged(e);
  2351. }
  2352. protected virtual void OnSizeChanged(EventArgs e) {
  2353. OnResize(e);
  2354. if (SizeChanged!=null) SizeChanged(this, e);
  2355. }
  2356. protected virtual void OnStyleChanged(EventArgs e) {
  2357. if (StyleChanged!=null) StyleChanged(this, e);
  2358. }
  2359. protected virtual void OnSystemColorsChanged(EventArgs e) {
  2360. if (SystemColorsChanged!=null) SystemColorsChanged(this, e);
  2361. }
  2362. protected virtual void OnTabIndexChanged(EventArgs e) {
  2363. if (TabIndexChanged!=null) TabIndexChanged(this, e);
  2364. }
  2365. protected virtual void OnTabStopChanged(EventArgs e) {
  2366. if (TabStopChanged!=null) TabStopChanged(this, e);
  2367. }
  2368. protected virtual void OnTextChanged(EventArgs e) {
  2369. if (TextChanged!=null) TextChanged(this, e);
  2370. }
  2371. protected virtual void OnValidated(EventArgs e) {
  2372. if (Validated!=null) Validated(this, e);
  2373. }
  2374. protected virtual void OnValidating(System.ComponentModel.CancelEventArgs e) {
  2375. if (Validating!=null) Validating(this, e);
  2376. }
  2377. protected virtual void OnVisibleChanged(EventArgs e) {
  2378. if (!is_visible) {
  2379. if (dc_mem!=null) {
  2380. dc_mem.Dispose ();
  2381. bmp_mem=null;
  2382. }
  2383. if (bmp_mem!=null) {
  2384. bmp_mem.Dispose();
  2385. bmp_mem=null;
  2386. }
  2387. } else {
  2388. if (!is_disposed) {
  2389. if (!this.IsHandleCreated) {
  2390. this.CreateHandle();
  2391. }
  2392. PerformLayout();
  2393. }
  2394. }
  2395. if (VisibleChanged!=null) VisibleChanged(this, e);
  2396. // We need to tell our kids
  2397. for (int i=0; i<child_controls.Count; i++) {
  2398. child_controls[i].OnParentVisibleChanged(e);
  2399. }
  2400. }
  2401. #endregion // OnXXX methods
  2402. #region Events
  2403. public event EventHandler BackColorChanged;
  2404. public event EventHandler BackgroundImageChanged;
  2405. public event EventHandler BindingContextChanged;
  2406. public event EventHandler CausesValidationChanged;
  2407. public event UICuesEventHandler ChangeUICues;
  2408. public event EventHandler Click;
  2409. public event EventHandler ContextMenuChanged;
  2410. public event ControlEventHandler ControlAdded;
  2411. public event ControlEventHandler ControlRemoved;
  2412. public event EventHandler CursorChanged;
  2413. public event EventHandler DockChanged;
  2414. public event EventHandler DoubleClick;
  2415. public event DragEventHandler DragDrop;
  2416. public event DragEventHandler DragEnter;
  2417. public event EventHandler DragLeave;
  2418. public event DragEventHandler DragOver;
  2419. public event EventHandler EnabledChanged;
  2420. public event EventHandler Enter;
  2421. public event EventHandler FontChanged;
  2422. public event EventHandler ForeColorChanged;
  2423. public event GiveFeedbackEventHandler GiveFeedback;
  2424. public event EventHandler GotFocus;
  2425. public event EventHandler HandleCreated;
  2426. public event EventHandler HandleDestroyed;
  2427. public event HelpEventHandler HelpRequested;
  2428. public event EventHandler ImeModeChanged;
  2429. public event InvalidateEventHandler Invalidated;
  2430. public event KeyEventHandler KeyDown;
  2431. public event KeyPressEventHandler KeyPress;
  2432. public event KeyEventHandler KeyUp;
  2433. public event LayoutEventHandler Layout;
  2434. public event EventHandler Leave;
  2435. public event EventHandler LocationChanged;
  2436. public event EventHandler LostFocus;
  2437. public event MouseEventHandler MouseDown;
  2438. public event EventHandler MouseEnter;
  2439. public event EventHandler MouseHover;
  2440. public event EventHandler MouseLeave;
  2441. public event MouseEventHandler MouseMove;
  2442. public event MouseEventHandler MouseUp;
  2443. public event MouseEventHandler MouseWheel;
  2444. public event EventHandler Move;
  2445. public event PaintEventHandler Paint;
  2446. public event EventHandler ParentChanged;
  2447. public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp;
  2448. public event QueryContinueDragEventHandler QueryContinueDrag;
  2449. public event EventHandler Resize;
  2450. public event EventHandler RightToLeftChanged;
  2451. public event EventHandler SizeChanged;
  2452. public event EventHandler StyleChanged;
  2453. public event EventHandler SystemColorsChanged;
  2454. public event EventHandler TabIndexChanged;
  2455. public event EventHandler TabStopChanged;
  2456. public event EventHandler TextChanged;
  2457. public event EventHandler Validated;
  2458. public event CancelEventHandler Validating;
  2459. public event EventHandler VisibleChanged;
  2460. #endregion // Events
  2461. }
  2462. }