Control.cs 69 KB

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