Control.cs 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851
  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-2006 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. // Partially 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. // COMPLETE
  33. #undef DebugRecreate
  34. #undef DebugFocus
  35. using System;
  36. using System.ComponentModel;
  37. using System.ComponentModel.Design;
  38. using System.ComponentModel.Design.Serialization;
  39. using System.Collections;
  40. using System.Diagnostics;
  41. using System.Drawing;
  42. using System.Drawing.Drawing2D;
  43. using System.Reflection;
  44. using System.Runtime.InteropServices;
  45. using System.Security;
  46. using System.Threading;
  47. namespace System.Windows.Forms
  48. {
  49. [Designer("System.Windows.Forms.Design.ControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  50. [DefaultProperty("Text")]
  51. [DefaultEvent("Click")]
  52. [DesignerSerializer("System.Windows.Forms.Design.ControlCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
  53. [ToolboxItemFilter("System.Windows.Forms")]
  54. public class Control : Component, ISynchronizeInvoke, IWin32Window
  55. {
  56. #region Local Variables
  57. // Basic
  58. internal Rectangle bounds; // bounding rectangle for control (client area + decorations)
  59. internal object creator_thread; // thread that created the control
  60. internal ControlNativeWindow window; // object for native window handle
  61. internal string name; // for object naming
  62. // State
  63. internal bool is_created; // true if OnCreateControl has been sent
  64. internal bool has_focus; // true if control has focus
  65. internal bool is_visible; // true if control is visible
  66. internal bool is_entered; // is the mouse inside the control?
  67. internal bool is_enabled; // true if control is enabled (usable/not grayed out)
  68. internal bool is_accessible; // true if the control is visible to accessibility applications
  69. internal bool is_captured; // tracks if the control has captured the mouse
  70. internal bool is_toplevel; // tracks if the control is a toplevel window
  71. internal bool is_recreating; // tracks if the handle for the control is being recreated
  72. internal bool causes_validation; // tracks if validation is executed on changes
  73. internal bool is_focusing; // tracks if Focus has been called on the control and has not yet finished
  74. internal int tab_index; // position in tab order of siblings
  75. internal bool tab_stop = true; // is the control a tab stop?
  76. internal bool is_disposed; // has the window already been disposed?
  77. internal Size client_size; // size of the client area (window excluding decorations)
  78. internal Rectangle client_rect; // rectangle with the client area (window excluding decorations)
  79. internal ControlStyles control_style; // rather win32-specific, style bits for control
  80. internal ImeMode ime_mode = ImeMode.Inherit;
  81. internal bool layout_pending; // true if our parent needs to re-layout us
  82. internal object control_tag; // object that contains data about our control
  83. internal int mouse_clicks; // Counter for mouse clicks
  84. internal Cursor cursor; // Cursor for the window
  85. internal bool allow_drop; // true if the control accepts droping objects on it
  86. internal Region clip_region; // User-specified clip region for the window
  87. // Visuals
  88. internal Color foreground_color; // foreground color for control
  89. internal Color background_color; // background color for control
  90. internal Image background_image; // background image for control
  91. internal Font font; // font for control
  92. internal string text; // window/title text for control
  93. internal BorderStyle border_style; // Border style of control
  94. // Layout
  95. internal AnchorStyles anchor_style; // anchoring requirements for our control
  96. internal DockStyle dock_style; // docking requirements for our control (supercedes anchoring)
  97. internal int dist_left; // distance to the left border of the parent
  98. internal int dist_top; // distance to the top border of the parent
  99. internal int dist_right; // distance to the right border of the parent
  100. internal int dist_bottom; // distance to the bottom border of the parent
  101. // to be categorized...
  102. static internal ArrayList controls = new ArrayList(); // All of the application's controls, in a flat list
  103. internal ControlCollection child_controls; // our children
  104. internal Control parent; // our parent control
  105. internal AccessibleObject accessibility_object; // object that contains accessibility information about our control
  106. internal BindingContext binding_context; // TODO
  107. internal RightToLeft right_to_left; // drawing direction for control
  108. internal int layout_suspended;
  109. internal ContextMenu context_menu; // Context menu associated with the control
  110. private Graphics dc_mem; // Graphics context for double buffering
  111. private Bitmap bmp_mem; // Bitmap for double buffering control
  112. private Region invalid_region;
  113. private ControlBindingsCollection data_bindings;
  114. #if NET_2_0
  115. internal bool use_compatible_text_rendering;
  116. static internal bool verify_thread_handle;
  117. private Padding padding;
  118. private Size maximum_size;
  119. private Size minimum_size;
  120. private Size preferred_size;
  121. private Padding margin;
  122. internal Layout.LayoutEngine layout_engine;
  123. #endif
  124. #endregion // Local Variables
  125. #region Private Classes
  126. // This helper class allows us to dispatch messages to Control.WndProc
  127. internal class ControlNativeWindow : NativeWindow {
  128. private Control owner;
  129. public ControlNativeWindow(Control control) : base() {
  130. this.owner=control;
  131. }
  132. public Control Owner {
  133. get {
  134. return owner;
  135. }
  136. }
  137. static internal Control ControlFromHandle(IntPtr hWnd) {
  138. ControlNativeWindow window;
  139. window = (ControlNativeWindow)window_collection[hWnd];
  140. if (window != null) {
  141. return window.owner;
  142. }
  143. return null;
  144. }
  145. static internal Control ControlFromChildHandle (IntPtr handle) {
  146. ControlNativeWindow window;
  147. Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
  148. while (hwnd != null) {
  149. window = (ControlNativeWindow)window_collection[hwnd.Handle];
  150. if (window != null) {
  151. return window.owner;
  152. }
  153. hwnd = hwnd.Parent;
  154. }
  155. return null;
  156. }
  157. protected override void WndProc(ref Message m) {
  158. owner.WndProc(ref m);
  159. }
  160. }
  161. #endregion
  162. #region Public Classes
  163. [ComVisible(true)]
  164. public class ControlAccessibleObject : AccessibleObject {
  165. #region ControlAccessibleObject Constructors
  166. public ControlAccessibleObject(Control ownerControl) {
  167. this.owner = ownerControl;
  168. }
  169. #endregion // ControlAccessibleObject Constructors
  170. #region ControlAccessibleObject Public Instance Properties
  171. public override string DefaultAction {
  172. get {
  173. return base.DefaultAction;
  174. }
  175. }
  176. public override string Description {
  177. get {
  178. return base.Description;
  179. }
  180. }
  181. public IntPtr Handle {
  182. get {
  183. return owner.Handle;
  184. }
  185. set {
  186. // We don't want to let them set it
  187. }
  188. }
  189. public override string Help {
  190. get {
  191. return base.Help;
  192. }
  193. }
  194. public override string KeyboardShortcut {
  195. get {
  196. return base.KeyboardShortcut;
  197. }
  198. }
  199. public override string Name {
  200. get {
  201. return base.Name;
  202. }
  203. set {
  204. base.Name = value;
  205. }
  206. }
  207. public Control Owner {
  208. get {
  209. return owner;
  210. }
  211. }
  212. public override AccessibleObject Parent {
  213. get {
  214. return base.Parent;
  215. }
  216. }
  217. public override AccessibleRole Role {
  218. get {
  219. return base.Role;
  220. }
  221. }
  222. #endregion // ControlAccessibleObject Public Instance Properties
  223. #region ControlAccessibleObject Public Instance Methods
  224. public override int GetHelpTopic(out string FileName) {
  225. return base.GetHelpTopic (out FileName);
  226. }
  227. [MonoTODO("Implement this and tie it into Control.AccessibilityNotifyClients")]
  228. public void NotifyClients(AccessibleEvents accEvent) {
  229. throw new NotImplementedException();
  230. }
  231. [MonoTODO("Implement this and tie it into Control.AccessibilityNotifyClients")]
  232. public void NotifyClients(AccessibleEvents accEvent, int childID) {
  233. throw new NotImplementedException();
  234. }
  235. public override string ToString() {
  236. return "ControlAccessibleObject: Owner = " + owner.ToString() + ", Text: " + owner.text;
  237. }
  238. #endregion // ControlAccessibleObject Public Instance Methods
  239. }
  240. [DesignerSerializer("System.Windows.Forms.Design.ControlCollectionCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
  241. [ListBindable(false)]
  242. public class ControlCollection : IList, ICollection, ICloneable, IEnumerable {
  243. #region ControlCollection Local Variables
  244. private ArrayList list;
  245. internal ArrayList impl_list;
  246. private Control [] all_controls;
  247. internal Control owner;
  248. #endregion // ControlCollection Local Variables
  249. #region ControlCollection Public Constructor
  250. public ControlCollection(Control owner) {
  251. this.owner=owner;
  252. this.list=new ArrayList();
  253. }
  254. #endregion
  255. #region ControlCollection Public Instance Properties
  256. public int Count {
  257. get {
  258. return list.Count;
  259. }
  260. }
  261. public bool IsReadOnly {
  262. get {
  263. return list.IsReadOnly;
  264. }
  265. }
  266. public virtual Control this[int index] {
  267. get {
  268. if (index < 0 || index >= list.Count) {
  269. throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
  270. }
  271. return (Control)list[index];
  272. }
  273. }
  274. #endregion // ControlCollection Public Instance Properties
  275. #region ControlCollection Private Instance Methods
  276. public virtual void Add (Control value)
  277. {
  278. if (value == null)
  279. return;
  280. if (Contains (value)) {
  281. owner.PerformLayout();
  282. return;
  283. }
  284. if (value.tab_index == -1) {
  285. int end;
  286. int index;
  287. int use;
  288. use = 0;
  289. end = owner.child_controls.Count;
  290. for (int i = 0; i < end; i++) {
  291. index = owner.child_controls[i].tab_index;
  292. if (index >= use) {
  293. use = index + 1;
  294. }
  295. }
  296. value.tab_index = use;
  297. }
  298. if (value.parent != null) {
  299. value.parent.Controls.Remove(value);
  300. }
  301. all_controls = null;
  302. list.Add (value);
  303. value.ChangeParent(owner);
  304. value.InitLayout();
  305. owner.UpdateChildrenZOrder();
  306. owner.PerformLayout(value, "Parent");
  307. owner.OnControlAdded(new ControlEventArgs(value));
  308. }
  309. internal void AddToList (Control c)
  310. {
  311. all_controls = null;
  312. list.Add (c);
  313. }
  314. internal virtual void AddImplicit (Control control)
  315. {
  316. if (impl_list == null)
  317. impl_list = new ArrayList ();
  318. if (AllContains (control))
  319. return;
  320. all_controls = null;
  321. impl_list.Add (control);
  322. control.ChangeParent (owner);
  323. control.InitLayout ();
  324. owner.UpdateChildrenZOrder ();
  325. owner.PerformLayout (control, "Parent");
  326. owner.OnControlAdded (new ControlEventArgs (control));
  327. }
  328. public virtual void AddRange (Control[] controls)
  329. {
  330. if (controls == null)
  331. throw new ArgumentNullException ("controls");
  332. owner.SuspendLayout ();
  333. try {
  334. for (int i = 0; i < controls.Length; i++)
  335. Add (controls[i]);
  336. } finally {
  337. owner.ResumeLayout ();
  338. }
  339. }
  340. internal virtual void AddRangeImplicit (Control [] controls)
  341. {
  342. if (controls == null)
  343. throw new ArgumentNullException ("controls");
  344. owner.SuspendLayout ();
  345. try {
  346. for (int i = 0; i < controls.Length; i++)
  347. AddImplicit (controls [i]);
  348. } finally {
  349. owner.ResumeLayout ();
  350. }
  351. }
  352. public virtual void Clear ()
  353. {
  354. all_controls = null;
  355. // MS sends remove events in reverse order
  356. while (list.Count > 0) {
  357. Remove((Control)list[list.Count - 1]);
  358. }
  359. }
  360. internal virtual void ClearImplicit ()
  361. {
  362. if (impl_list == null)
  363. return;
  364. all_controls = null;
  365. impl_list.Clear ();
  366. }
  367. public bool Contains (Control value)
  368. {
  369. for (int i = list.Count; i > 0; ) {
  370. i--;
  371. if (list [i] == value) {
  372. // Do we need to do anything here?
  373. return true;
  374. }
  375. }
  376. return false;
  377. }
  378. internal bool ImplicitContains (Control value)
  379. {
  380. if (impl_list == null)
  381. return false;
  382. for (int i = impl_list.Count; i > 0; ) {
  383. i--;
  384. if (impl_list [i] == value) {
  385. // Do we need to do anything here?
  386. return true;
  387. }
  388. }
  389. return false;
  390. }
  391. internal bool AllContains (Control value)
  392. {
  393. return Contains (value) || ImplicitContains (value);
  394. }
  395. public void CopyTo (Array array, int index)
  396. {
  397. list.CopyTo(array, index);
  398. }
  399. public override bool Equals(object other) {
  400. if (other is ControlCollection && (((ControlCollection)other).owner==this.owner)) {
  401. return(true);
  402. } else {
  403. return(false);
  404. }
  405. }
  406. public int GetChildIndex(Control child) {
  407. return GetChildIndex(child, false);
  408. }
  409. public int GetChildIndex(Control child, bool throwException) {
  410. int index;
  411. index=list.IndexOf(child);
  412. if (index==-1 && throwException) {
  413. throw new ArgumentException("Not a child control", "child");
  414. }
  415. return index;
  416. }
  417. public IEnumerator GetEnumerator() {
  418. return list.GetEnumerator();
  419. }
  420. internal IEnumerator GetAllEnumerator ()
  421. {
  422. Control [] res = GetAllControls ();
  423. return res.GetEnumerator ();
  424. }
  425. internal Control [] GetAllControls ()
  426. {
  427. if (all_controls != null)
  428. return all_controls;
  429. if (impl_list == null) {
  430. all_controls = (Control []) list.ToArray (typeof (Control));
  431. return all_controls;
  432. }
  433. all_controls = new Control [list.Count + impl_list.Count];
  434. impl_list.CopyTo (all_controls);
  435. list.CopyTo (all_controls, impl_list.Count);
  436. return all_controls;
  437. }
  438. public override int GetHashCode() {
  439. return base.GetHashCode();
  440. }
  441. public int IndexOf(Control control) {
  442. return list.IndexOf(control);
  443. }
  444. public virtual void Remove(Control value) {
  445. owner.PerformLayout(value, "Parent");
  446. owner.OnControlRemoved(new ControlEventArgs(value));
  447. all_controls = null;
  448. list.Remove(value);
  449. value.ChangeParent(null);
  450. owner.UpdateChildrenZOrder();
  451. }
  452. internal virtual void RemoveImplicit (Control control)
  453. {
  454. if (impl_list != null) {
  455. all_controls = null;
  456. owner.PerformLayout (control, "Parent");
  457. owner.OnControlRemoved (new ControlEventArgs (control));
  458. impl_list.Remove (control);
  459. }
  460. control.ChangeParent (null);
  461. owner.UpdateChildrenZOrder ();
  462. }
  463. public void RemoveAt(int index) {
  464. if (index < 0 || index >= list.Count) {
  465. throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
  466. }
  467. Remove ((Control)list[index]);
  468. }
  469. public void SetChildIndex(Control child, int newIndex) {
  470. int old_index;
  471. old_index=list.IndexOf(child);
  472. if (old_index==-1) {
  473. throw new ArgumentException("Not a child control", "child");
  474. }
  475. if (old_index==newIndex) {
  476. return;
  477. }
  478. all_controls = null;
  479. list.RemoveAt(old_index);
  480. if (newIndex>list.Count) {
  481. list.Add(child);
  482. } else {
  483. list.Insert(newIndex, child);
  484. }
  485. child.UpdateZOrder();
  486. owner.PerformLayout();
  487. }
  488. #endregion // ControlCollection Private Instance Methods
  489. #region ControlCollection Interface Properties
  490. object IList.this[int index] {
  491. get {
  492. if (index<0 || index>=list.Count) {
  493. throw new ArgumentOutOfRangeException("index", index, "ControlCollection does not have that many controls");
  494. }
  495. return this[index];
  496. }
  497. set {
  498. if (!(value is Control)) {
  499. throw new ArgumentException("Object of type Control required", "value");
  500. }
  501. all_controls = null;
  502. Control ctrl = (Control) value;
  503. list[index]= ctrl;
  504. ctrl.ChangeParent(owner);
  505. ctrl.InitLayout();
  506. owner.UpdateChildrenZOrder();
  507. owner.PerformLayout(ctrl, "Parent");
  508. }
  509. }
  510. bool IList.IsFixedSize {
  511. get {
  512. return false;
  513. }
  514. }
  515. bool ICollection.IsSynchronized {
  516. get {
  517. return list.IsSynchronized;
  518. }
  519. }
  520. object ICollection.SyncRoot {
  521. get {
  522. return list.SyncRoot;
  523. }
  524. }
  525. #endregion // ControlCollection Interface Properties
  526. #region ControlCollection Interface Methods
  527. int IList.Add(object value) {
  528. if (value == null) {
  529. throw new ArgumentNullException("value", "Cannot add null controls");
  530. }
  531. if (!(value is Control)) {
  532. throw new ArgumentException("Object of type Control required", "value");
  533. }
  534. return list.Add(value);
  535. }
  536. bool IList.Contains(object value) {
  537. if (!(value is Control)) {
  538. throw new ArgumentException("Object of type Control required", "value");
  539. }
  540. return this.Contains((Control) value);
  541. }
  542. int IList.IndexOf(object value) {
  543. if (!(value is Control)) {
  544. throw new ArgumentException("Object of type Control required", "value");
  545. }
  546. return this.IndexOf((Control) value);
  547. }
  548. void IList.Insert(int index, object value) {
  549. if (!(value is Control)) {
  550. throw new ArgumentException("Object of type Control required", "value");
  551. }
  552. all_controls = null;
  553. list.Insert(index, value);
  554. }
  555. void IList.Remove(object value) {
  556. if (!(value is Control)) {
  557. throw new ArgumentException("Object of type Control required", "value");
  558. }
  559. all_controls = null;
  560. list.Remove(value);
  561. }
  562. Object ICloneable.Clone() {
  563. ControlCollection clone = new ControlCollection(this.owner);
  564. clone.list=(ArrayList)list.Clone(); // FIXME: Do we need this?
  565. return clone;
  566. }
  567. #endregion // ControlCollection Interface Methods
  568. }
  569. #endregion // ControlCollection Class
  570. #region Public Constructors
  571. public Control() {
  572. anchor_style = AnchorStyles.Top | AnchorStyles.Left;
  573. is_created = false;
  574. is_visible = true;
  575. is_captured = false;
  576. is_disposed = false;
  577. is_enabled = true;
  578. is_entered = false;
  579. layout_pending = false;
  580. is_toplevel = false;
  581. causes_validation = true;
  582. has_focus = false;
  583. layout_suspended = 0;
  584. mouse_clicks = 1;
  585. tab_index = -1;
  586. cursor = null;
  587. right_to_left = RightToLeft.Inherit;
  588. border_style = BorderStyle.None;
  589. background_color = Color.Empty;
  590. dist_left = 0;
  591. dist_right = 0;
  592. dist_top = 0;
  593. dist_bottom = 0;
  594. #if NET_2_0
  595. use_compatible_text_rendering = Application.use_compatible_text_rendering;
  596. padding = new Padding(0);
  597. maximum_size = new Size();
  598. minimum_size = new Size();
  599. preferred_size = new Size();
  600. margin = this.DefaultMargin;
  601. layout_engine = new Layout.DefaultLayout();
  602. #endif
  603. control_style = ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |
  604. ControlStyles.Selectable | ControlStyles.StandardClick |
  605. ControlStyles.StandardDoubleClick;
  606. parent = null;
  607. background_image = null;
  608. text = string.Empty;
  609. name = string.Empty;
  610. window = new ControlNativeWindow(this);
  611. child_controls = CreateControlsInstance();
  612. client_size = new Size(DefaultSize.Width, DefaultSize.Height);
  613. client_rect = new Rectangle(0, 0, DefaultSize.Width, DefaultSize.Height);
  614. XplatUI.CalculateWindowRect(ref client_rect, CreateParams.Style, CreateParams.ExStyle, null, out bounds);
  615. if ((CreateParams.Style & (int)WindowStyles.WS_CHILD) == 0) {
  616. bounds.X=-1;
  617. bounds.Y=-1;
  618. }
  619. }
  620. public Control(Control parent, string text) : this() {
  621. Text=text;
  622. Parent=parent;
  623. }
  624. public Control(Control parent, string text, int left, int top, int width, int height) : this() {
  625. Parent=parent;
  626. bounds.X=left;
  627. bounds.Y=top;
  628. bounds.Width=width;
  629. bounds.Height=height;
  630. SetBounds(left, top, width, height, BoundsSpecified.All);
  631. Text=text;
  632. }
  633. public Control(string text) : this() {
  634. Text=text;
  635. }
  636. public Control(string text, int left, int top, int width, int height) : this() {
  637. bounds.X=left;
  638. bounds.Y=top;
  639. bounds.Width=width;
  640. bounds.Height=height;
  641. SetBounds(left, top, width, height, BoundsSpecified.All);
  642. Text=text;
  643. }
  644. private delegate void RemoveDelegate(object c);
  645. protected override void Dispose(bool disposing) {
  646. if (!is_disposed && disposing) {
  647. Capture = false;
  648. if (dc_mem!=null) {
  649. dc_mem.Dispose();
  650. dc_mem=null;
  651. }
  652. if (bmp_mem!=null) {
  653. bmp_mem.Dispose();
  654. bmp_mem=null;
  655. }
  656. if (invalid_region!=null) {
  657. invalid_region.Dispose();
  658. invalid_region=null;
  659. }
  660. if (this.InvokeRequired) {
  661. if (Application.MessageLoop) {
  662. this.BeginInvokeInternal(new MethodInvoker(DestroyHandle), null, true);
  663. this.BeginInvokeInternal(new RemoveDelegate(controls.Remove), new object[] {this}, true);
  664. }
  665. } else {
  666. DestroyHandle();
  667. lock (Control.controls) {
  668. Control.controls.Remove(this);
  669. }
  670. }
  671. if (parent != null) {
  672. parent.Controls.Remove(this);
  673. }
  674. Control [] children = child_controls.GetAllControls ();
  675. for (int i=0; i<children.Length; i++) {
  676. children[i].parent = null; // Need to set to null or our child will try and remove from ourselves and crash
  677. children[i].Dispose();
  678. }
  679. }
  680. is_disposed = true;
  681. is_visible = false;
  682. base.Dispose(disposing);
  683. }
  684. #endregion // Public Constructors
  685. #region Internal Properties
  686. internal BorderStyle InternalBorderStyle {
  687. get {
  688. return border_style;
  689. }
  690. set {
  691. if (!Enum.IsDefined (typeof (BorderStyle), value))
  692. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for BorderStyle", value));
  693. if (border_style != value) {
  694. border_style = value;
  695. if (IsHandleCreated) {
  696. XplatUI.SetBorderStyle(window.Handle, (FormBorderStyle)border_style);
  697. Refresh();
  698. }
  699. }
  700. }
  701. }
  702. #endregion // Internal Properties
  703. #region Private & Internal Methods
  704. internal IAsyncResult BeginInvokeInternal (Delegate method, object [] args, bool disposing) {
  705. AsyncMethodResult result;
  706. AsyncMethodData data;
  707. if (!disposing) {
  708. Control p;
  709. p = this;
  710. do {
  711. if (!p.IsHandleCreated) {
  712. throw new InvalidOperationException("Cannot call Invoke or InvokeAsync on a control until the window handle is created");
  713. }
  714. p = p.parent;
  715. } while (p != null);
  716. }
  717. result = new AsyncMethodResult ();
  718. data = new AsyncMethodData ();
  719. data.Handle = window.Handle;
  720. data.Method = method;
  721. data.Args = args;
  722. data.Result = result;
  723. #if NET_2_0
  724. if (!ExecutionContext.IsFlowSuppressed ()) {
  725. data.Context = ExecutionContext.Capture ();
  726. }
  727. #else
  728. #if !MWF_ON_MSRUNTIME
  729. if (SecurityManager.SecurityEnabled) {
  730. data.Stack = CompressedStack.GetCompressedStack ();
  731. }
  732. #endif
  733. #endif
  734. XplatUI.SendAsyncMethod (data);
  735. return result;
  736. }
  737. internal void PointToClient (ref int x, ref int y)
  738. {
  739. XplatUI.ScreenToClient (Handle, ref x, ref y);
  740. }
  741. internal void PointToScreen (ref int x, ref int y)
  742. {
  743. XplatUI.ClientToScreen (Handle, ref x, ref y);
  744. }
  745. internal bool IsRecreating {
  746. get {
  747. return is_recreating;
  748. }
  749. }
  750. internal Graphics DeviceContext {
  751. get {
  752. if (dc_mem == null) {
  753. CreateBuffers(this.Width, this.Height);
  754. }
  755. return dc_mem;
  756. }
  757. }
  758. private void ImageBufferNeedsRedraw () {
  759. if (invalid_region != null)
  760. invalid_region.Dispose();
  761. invalid_region = new Region (ClientRectangle);
  762. }
  763. private Bitmap ImageBuffer {
  764. get {
  765. if (bmp_mem==null) {
  766. CreateBuffers(this.Width, this.Height);
  767. }
  768. return bmp_mem;
  769. }
  770. }
  771. internal void CreateBuffers (int width, int height) {
  772. if (width < 1) {
  773. width = 1;
  774. }
  775. if (height < 1) {
  776. height = 1;
  777. }
  778. bmp_mem = new Bitmap (width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  779. dc_mem = Graphics.FromImage (bmp_mem);
  780. ImageBufferNeedsRedraw ();
  781. }
  782. internal void InvalidateBuffers ()
  783. {
  784. if (dc_mem != null) {
  785. dc_mem.Dispose ();
  786. }
  787. dc_mem = null;
  788. bmp_mem = null;
  789. ImageBufferNeedsRedraw ();
  790. }
  791. internal static void SetChildColor(Control parent) {
  792. Control child;
  793. for (int i=0; i < parent.child_controls.Count; i++) {
  794. child=parent.child_controls[i];
  795. if (child.child_controls.Count>0) {
  796. SetChildColor(child);
  797. }
  798. }
  799. }
  800. internal bool Select(Control control) {
  801. IContainerControl container;
  802. if (control == null) {
  803. return false;
  804. }
  805. container = GetContainerControl();
  806. if (container != null) {
  807. container.ActiveControl = control;
  808. }
  809. if (control.IsHandleCreated) {
  810. XplatUI.SetFocus(control.window.Handle);
  811. }
  812. return true;
  813. }
  814. internal void SelectChild (Control control)
  815. {
  816. if (control.IsHandleCreated)
  817. XplatUI.SetFocus (control.window.Handle);
  818. }
  819. internal virtual void DoDefaultAction() {
  820. // Only here to be overriden by our actual controls; this is needed by the accessibility class
  821. }
  822. internal static int LowOrder (int param) {
  823. return ((int)(short)(param & 0xffff));
  824. }
  825. internal static int HighOrder (int param) {
  826. return ((int)(short)(param >> 16));
  827. }
  828. // This method exists so controls overriding OnPaintBackground can have default background painting done
  829. internal virtual void PaintControlBackground (PaintEventArgs pevent)
  830. {
  831. if (GetStyle(ControlStyles.SupportsTransparentBackColor) && (BackColor.A != 0xff)) {
  832. if (parent != null) {
  833. PaintEventArgs parent_pe;
  834. GraphicsState state;
  835. parent_pe = new PaintEventArgs(pevent.Graphics, new Rectangle(pevent.ClipRectangle.X + Left, pevent.ClipRectangle.Y + Top, pevent.ClipRectangle.Width, pevent.ClipRectangle.Height));
  836. state = parent_pe.Graphics.Save();
  837. parent_pe.Graphics.TranslateTransform(-Left, -Top);
  838. parent.OnPaintBackground(parent_pe);
  839. parent_pe.Graphics.Restore(state);
  840. state = parent_pe.Graphics.Save();
  841. parent_pe.Graphics.TranslateTransform(-Left, -Top);
  842. parent.OnPaint(parent_pe);
  843. parent_pe.Graphics.Restore(state);
  844. parent_pe.SetGraphics(null);
  845. }
  846. }
  847. if ((clip_region != null) && (XplatUI.UserClipWontExposeParent)) {
  848. if (parent != null) {
  849. PaintEventArgs parent_pe;
  850. Region region;
  851. GraphicsState state;
  852. Hwnd hwnd;
  853. hwnd = Hwnd.ObjectFromHandle(Handle);
  854. if (hwnd != null) {
  855. parent_pe = new PaintEventArgs(pevent.Graphics, new Rectangle(pevent.ClipRectangle.X + Left, pevent.ClipRectangle.Y + Top, pevent.ClipRectangle.Width, pevent.ClipRectangle.Height));
  856. region = new Region ();
  857. region.MakeEmpty();
  858. region.Union(ClientRectangle);
  859. foreach (Rectangle r in hwnd.ClipRectangles) {
  860. region.Union (r);
  861. }
  862. state = parent_pe.Graphics.Save();
  863. parent_pe.Graphics.Clip = region;
  864. parent_pe.Graphics.TranslateTransform(-Left, -Top);
  865. parent.OnPaintBackground(parent_pe);
  866. parent_pe.Graphics.Restore(state);
  867. state = parent_pe.Graphics.Save();
  868. parent_pe.Graphics.Clip = region;
  869. parent_pe.Graphics.TranslateTransform(-Left, -Top);
  870. parent.OnPaint(parent_pe);
  871. parent_pe.Graphics.Restore(state);
  872. parent_pe.SetGraphics(null);
  873. region.Intersect(clip_region);
  874. pevent.Graphics.Clip = region;
  875. }
  876. }
  877. }
  878. if (background_image == null) {
  879. pevent.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(BackColor), new Rectangle(pevent.ClipRectangle.X - 1, pevent.ClipRectangle.Y - 1, pevent.ClipRectangle.Width + 2, pevent.ClipRectangle.Height + 2));
  880. return;
  881. }
  882. DrawBackgroundImage (pevent.Graphics);
  883. }
  884. void DrawBackgroundImage (Graphics g)
  885. {
  886. using (TextureBrush b = new TextureBrush (background_image, WrapMode.Tile)) {
  887. g.FillRectangle (b, ClientRectangle);
  888. }
  889. }
  890. internal virtual void DndEnter (DragEventArgs e)
  891. {
  892. try {
  893. OnDragEnter (e);
  894. } catch { }
  895. }
  896. internal virtual void DndOver (DragEventArgs e)
  897. {
  898. try {
  899. OnDragOver (e);
  900. } catch { }
  901. }
  902. internal virtual void DndDrop (DragEventArgs e)
  903. {
  904. try {
  905. OnDragDrop (e);
  906. } catch (Exception exc) {
  907. Console.Error.WriteLine ("MWF: Exception while dropping:");
  908. Console.Error.WriteLine (exc);
  909. }
  910. }
  911. internal virtual void DndLeave (EventArgs e)
  912. {
  913. try {
  914. OnDragLeave (e);
  915. } catch { }
  916. }
  917. internal virtual void DndFeedback(GiveFeedbackEventArgs e)
  918. {
  919. try {
  920. OnGiveFeedback(e);
  921. } catch { }
  922. }
  923. internal virtual void DndContinueDrag(QueryContinueDragEventArgs e)
  924. {
  925. try {
  926. OnQueryContinueDrag(e);
  927. } catch { }
  928. }
  929. internal static MouseButtons FromParamToMouseButtons (int param) {
  930. MouseButtons buttons = MouseButtons.None;
  931. if ((param & (int) MsgButtons.MK_LBUTTON) != 0)
  932. buttons |= MouseButtons.Left;
  933. if ((param & (int) MsgButtons.MK_MBUTTON) != 0)
  934. buttons |= MouseButtons.Middle;
  935. if ((param & (int) MsgButtons.MK_RBUTTON) != 0)
  936. buttons |= MouseButtons.Right;
  937. return buttons;
  938. }
  939. internal void FireEnter ()
  940. {
  941. OnEnter (EventArgs.Empty);
  942. }
  943. internal void FireLeave ()
  944. {
  945. OnLeave (EventArgs.Empty);
  946. }
  947. internal void FireValidating (CancelEventArgs ce)
  948. {
  949. OnValidating (ce);
  950. }
  951. internal void FireValidated ()
  952. {
  953. OnValidated (EventArgs.Empty);
  954. }
  955. internal virtual bool ProcessControlMnemonic(char charCode) {
  956. return ProcessMnemonic(charCode);
  957. }
  958. private static Control FindFlatForward(Control container, Control start) {
  959. Control found;
  960. int index;
  961. int end;
  962. found = null;
  963. end = container.child_controls.Count;
  964. if (start != null) {
  965. index = start.tab_index;
  966. } else {
  967. index = -1;
  968. }
  969. for (int i = 0, pos = -1; i < end; i++) {
  970. if (start == container.child_controls[i]) {
  971. pos = i;
  972. continue;
  973. }
  974. if (found == null) {
  975. if (container.child_controls[i].tab_index > index || (pos > -1 && pos < i && container.child_controls[i].tab_index == index)) {
  976. found = container.child_controls[i];
  977. }
  978. } else if (found.tab_index > container.child_controls[i].tab_index) {
  979. if (container.child_controls[i].tab_index > index) {
  980. found = container.child_controls[i];
  981. }
  982. }
  983. }
  984. return found;
  985. }
  986. private static Control FindControlForward(Control container, Control start) {
  987. Control found;
  988. found = null;
  989. if (start == null) {
  990. return FindFlatForward(container, start);
  991. }
  992. if (start.child_controls != null && start.child_controls.Count > 0 &&
  993. (start == container || !((start is IContainerControl) && start.GetStyle(ControlStyles.ContainerControl)))) {
  994. return FindControlForward(start, null);
  995. }
  996. else {
  997. while (start != container) {
  998. found = FindFlatForward(start.parent, start);
  999. if (found != null) {
  1000. return found;
  1001. }
  1002. start = start.parent;
  1003. }
  1004. }
  1005. return null;
  1006. }
  1007. private static Control FindFlatBackward(Control container, Control start) {
  1008. Control found;
  1009. int index;
  1010. int end;
  1011. found = null;
  1012. end = container.child_controls.Count;
  1013. if (start != null) {
  1014. index = start.tab_index;
  1015. } else {
  1016. // FIXME: Possible speed-up: Keep the highest taborder index in the container
  1017. index = -1;
  1018. for (int i = 0; i < end; i++) {
  1019. if (container.child_controls[i].tab_index > index) {
  1020. index = container.child_controls[i].tab_index;
  1021. }
  1022. }
  1023. index++;
  1024. }
  1025. bool hit = false;
  1026. for (int i = end - 1; i >= 0; i--) {
  1027. if (start == container.child_controls[i]) {
  1028. hit = true;
  1029. continue;
  1030. }
  1031. if (found == null || found.tab_index < container.child_controls[i].tab_index) {
  1032. if (container.child_controls[i].tab_index < index || (hit && container.child_controls[i].tab_index == index))
  1033. found = container.child_controls[i];
  1034. }
  1035. }
  1036. return found;
  1037. }
  1038. private static Control FindControlBackward(Control container, Control start) {
  1039. Control found = null;
  1040. if (start == null) {
  1041. found = FindFlatBackward(container, start);
  1042. }
  1043. else if (start != container) {
  1044. if (start.parent != null) {
  1045. found = FindFlatBackward(start.parent, start);
  1046. if (found == null) {
  1047. if (start.parent != container)
  1048. return start.parent;
  1049. return null;
  1050. }
  1051. }
  1052. }
  1053. if (found == null || start.parent == null)
  1054. found = start;
  1055. while (found != null && (found == container || (!((found is IContainerControl) && found.GetStyle(ControlStyles.ContainerControl))) &&
  1056. found.child_controls != null && found.child_controls.Count > 0)) {
  1057. // while (ctl.child_controls != null && ctl.child_controls.Count > 0 &&
  1058. // (ctl == this || (!((ctl is IContainerControl) && ctl.GetStyle(ControlStyles.ContainerControl))))) {
  1059. found = FindFlatBackward(found, null);
  1060. }
  1061. return found;
  1062. /*
  1063. Control found;
  1064. found = null;
  1065. if (start != null) {
  1066. found = FindFlatBackward(start.parent, start);
  1067. if (found == null) {
  1068. if (start.parent != container) {
  1069. return start.parent;
  1070. }
  1071. }
  1072. }
  1073. if (found == null) {
  1074. found = FindFlatBackward(container, start);
  1075. }
  1076. if (container != start) {
  1077. while ((found != null) && (!found.Contains(start)) && found.child_controls != null && found.child_controls.Count > 0 && !(found is IContainerControl)) {// || found.GetStyle(ControlStyles.ContainerControl))) {
  1078. found = FindControlBackward(found, null);
  1079. if (found != null) {
  1080. return found;
  1081. }
  1082. }
  1083. }
  1084. return found;
  1085. */
  1086. }
  1087. internal virtual void HandleClick(int clicks, MouseEventArgs me) {
  1088. if (GetStyle(ControlStyles.StandardClick)) {
  1089. if ((clicks > 1) && GetStyle(ControlStyles.StandardDoubleClick)) {
  1090. #if !NET_2_0
  1091. OnDoubleClick(EventArgs.Empty);
  1092. } else {
  1093. OnClick(EventArgs.Empty);
  1094. #else
  1095. OnDoubleClick(me);
  1096. } else {
  1097. OnClick(me);
  1098. #endif
  1099. }
  1100. }
  1101. }
  1102. private void CheckDataBindings ()
  1103. {
  1104. if (data_bindings == null)
  1105. return;
  1106. BindingContext binding_context = BindingContext;
  1107. foreach (Binding binding in data_bindings) {
  1108. binding.Check (binding_context);
  1109. }
  1110. }
  1111. private void ChangeParent(Control new_parent) {
  1112. bool pre_enabled;
  1113. bool pre_visible;
  1114. Font pre_font;
  1115. Color pre_fore_color;
  1116. Color pre_back_color;
  1117. RightToLeft pre_rtl;
  1118. // These properties are inherited from our parent
  1119. // Get them pre parent-change and then send events
  1120. // if they are changed after we have our new parent
  1121. pre_enabled = Enabled;
  1122. pre_visible = Visible;
  1123. pre_font = Font;
  1124. pre_fore_color = ForeColor;
  1125. pre_back_color = BackColor;
  1126. pre_rtl = RightToLeft;
  1127. // MS doesn't seem to send a CursorChangedEvent
  1128. parent = new_parent;
  1129. if (IsHandleCreated)
  1130. XplatUI.SetParent(Handle,
  1131. (new_parent == null || !new_parent.IsHandleCreated) ? IntPtr.Zero : new_parent.Handle);
  1132. OnParentChanged(EventArgs.Empty);
  1133. if (pre_enabled != Enabled) {
  1134. OnEnabledChanged(EventArgs.Empty);
  1135. }
  1136. if (pre_visible != Visible) {
  1137. OnVisibleChanged(EventArgs.Empty);
  1138. }
  1139. if (pre_font != Font) {
  1140. OnFontChanged(EventArgs.Empty);
  1141. }
  1142. if (pre_fore_color != ForeColor) {
  1143. OnForeColorChanged(EventArgs.Empty);
  1144. }
  1145. if (pre_back_color != BackColor) {
  1146. OnBackColorChanged(EventArgs.Empty);
  1147. }
  1148. if (pre_rtl != RightToLeft) {
  1149. // MS sneaks a OnCreateControl and OnHandleCreated in here, I guess
  1150. // because when RTL changes they have to recreate the win32 control
  1151. // We don't really need that (until someone runs into compatibility issues)
  1152. OnRightToLeftChanged(EventArgs.Empty);
  1153. }
  1154. if ((new_parent != null) && new_parent.Created && !Created) {
  1155. CreateControl();
  1156. }
  1157. if ((binding_context == null) && Created) {
  1158. OnBindingContextChanged(EventArgs.Empty);
  1159. }
  1160. }
  1161. private void UpdateDistances() {
  1162. if ((parent != null) && (parent.layout_suspended == 0)) {
  1163. dist_left = bounds.X;
  1164. dist_top = bounds.Y;
  1165. dist_right = parent.ClientSize.Width - bounds.X - bounds.Width;
  1166. dist_bottom = parent.ClientSize.Height - bounds.Y - bounds.Height;
  1167. }
  1168. }
  1169. #endregion // Private & Internal Methods
  1170. #region Public Static Properties
  1171. public static Color DefaultBackColor {
  1172. get {
  1173. return ThemeEngine.Current.DefaultControlBackColor;
  1174. }
  1175. }
  1176. public static Font DefaultFont {
  1177. get {
  1178. return ThemeEngine.Current.DefaultFont;
  1179. }
  1180. }
  1181. public static Color DefaultForeColor {
  1182. get {
  1183. return ThemeEngine.Current.DefaultControlForeColor;
  1184. }
  1185. }
  1186. public static Keys ModifierKeys {
  1187. get {
  1188. return XplatUI.State.ModifierKeys;
  1189. }
  1190. }
  1191. public static MouseButtons MouseButtons {
  1192. get {
  1193. return XplatUI.State.MouseButtons;
  1194. }
  1195. }
  1196. public static Point MousePosition {
  1197. get {
  1198. return Cursor.Position;
  1199. }
  1200. }
  1201. #if NET_2_0
  1202. [MonoTODO]
  1203. public static bool CheckForIllegalCrossThreadCalls
  1204. {
  1205. get {
  1206. return verify_thread_handle;
  1207. }
  1208. set {
  1209. verify_thread_handle = value;
  1210. }
  1211. }
  1212. #endif
  1213. #endregion // Public Static Properties
  1214. #region Public Instance Properties
  1215. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1216. [Browsable(false)]
  1217. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1218. public AccessibleObject AccessibilityObject {
  1219. get {
  1220. if (accessibility_object==null) {
  1221. accessibility_object=CreateAccessibilityInstance();
  1222. }
  1223. return accessibility_object;
  1224. }
  1225. }
  1226. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1227. [Browsable(false)]
  1228. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1229. public string AccessibleDefaultActionDescription {
  1230. get {
  1231. return AccessibilityObject.default_action;
  1232. }
  1233. set {
  1234. AccessibilityObject.default_action=value;
  1235. }
  1236. }
  1237. [Localizable(true)]
  1238. [DefaultValue(null)]
  1239. [MWFCategory("Accessibility")]
  1240. public string AccessibleDescription {
  1241. get {
  1242. return AccessibilityObject.description;
  1243. }
  1244. set {
  1245. AccessibilityObject.description=value;
  1246. }
  1247. }
  1248. [Localizable(true)]
  1249. [DefaultValue(null)]
  1250. [MWFCategory("Accessibility")]
  1251. public string AccessibleName {
  1252. get {
  1253. return AccessibilityObject.Name;
  1254. }
  1255. set {
  1256. AccessibilityObject.Name=value;
  1257. }
  1258. }
  1259. [DefaultValue(AccessibleRole.Default)]
  1260. [MWFDescription("Role of the control"), MWFCategory("Accessibility")]
  1261. public AccessibleRole AccessibleRole {
  1262. get {
  1263. return AccessibilityObject.role;
  1264. }
  1265. set {
  1266. AccessibilityObject.role=value;
  1267. }
  1268. }
  1269. [DefaultValue(false)]
  1270. [MWFCategory("Behavior")]
  1271. public virtual bool AllowDrop {
  1272. get {
  1273. return allow_drop;
  1274. }
  1275. set {
  1276. if (allow_drop == value)
  1277. return;
  1278. allow_drop = value;
  1279. if (IsHandleCreated) {
  1280. UpdateStyles();
  1281. XplatUI.SetAllowDrop (Handle, value);
  1282. }
  1283. }
  1284. }
  1285. [Localizable(true)]
  1286. [RefreshProperties(RefreshProperties.Repaint)]
  1287. [DefaultValue(AnchorStyles.Top | AnchorStyles.Left)]
  1288. [MWFCategory("Layout")]
  1289. public virtual AnchorStyles Anchor {
  1290. get {
  1291. return anchor_style;
  1292. }
  1293. set {
  1294. anchor_style=value;
  1295. if (parent != null) {
  1296. parent.PerformLayout(this, "Parent");
  1297. }
  1298. }
  1299. }
  1300. #if NET_2_0
  1301. // XXX: Implement me!
  1302. bool auto_size;
  1303. public virtual bool AutoSize {
  1304. get {
  1305. //Console.Error.WriteLine("Unimplemented: Control::get_AutoSize()");
  1306. return auto_size;
  1307. }
  1308. set {
  1309. Console.Error.WriteLine("Unimplemented: Control::set_AutoSize(bool)");
  1310. auto_size = value;
  1311. }
  1312. }
  1313. public virtual Size MaximumSize {
  1314. get {
  1315. return maximum_size;
  1316. }
  1317. set {
  1318. maximum_size = value;
  1319. }
  1320. }
  1321. public virtual Size MinimumSize {
  1322. get {
  1323. return minimum_size;
  1324. }
  1325. set {
  1326. minimum_size = value;
  1327. }
  1328. }
  1329. #endif // NET_2_0
  1330. [DispId(-501)]
  1331. [MWFCategory("Appearance")]
  1332. public virtual Color BackColor {
  1333. get {
  1334. if (background_color.IsEmpty) {
  1335. if (parent!=null) {
  1336. Color pcolor = parent.BackColor;
  1337. if (pcolor.A == 0xff || GetStyle(ControlStyles.SupportsTransparentBackColor))
  1338. return pcolor;
  1339. }
  1340. return DefaultBackColor;
  1341. }
  1342. return background_color;
  1343. }
  1344. set {
  1345. if (!value.IsEmpty && (value.A != 0xff) && !GetStyle(ControlStyles.SupportsTransparentBackColor)) {
  1346. throw new ArgumentException("Transparent background colors are not supported on this control");
  1347. }
  1348. if (background_color != value) {
  1349. background_color=value;
  1350. SetChildColor(this);
  1351. OnBackColorChanged(EventArgs.Empty);
  1352. Invalidate();
  1353. }
  1354. }
  1355. }
  1356. [Localizable(true)]
  1357. [DefaultValue(null)]
  1358. [MWFCategory("Appearance")]
  1359. public virtual Image BackgroundImage {
  1360. get {
  1361. return background_image;
  1362. }
  1363. set {
  1364. if (background_image!=value) {
  1365. background_image=value;
  1366. OnBackgroundImageChanged(EventArgs.Empty);
  1367. }
  1368. }
  1369. }
  1370. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1371. [Browsable(false)]
  1372. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1373. public virtual BindingContext BindingContext {
  1374. get {
  1375. if (binding_context != null)
  1376. return binding_context;
  1377. if (Parent == null)
  1378. return null;
  1379. binding_context = Parent.BindingContext;
  1380. return binding_context;
  1381. }
  1382. set {
  1383. if (binding_context != value) {
  1384. binding_context = value;
  1385. OnBindingContextChanged(EventArgs.Empty);
  1386. }
  1387. }
  1388. }
  1389. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1390. [Browsable(false)]
  1391. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1392. public int Bottom {
  1393. get {
  1394. return bounds.Y+bounds.Height;
  1395. }
  1396. }
  1397. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1398. [Browsable(false)]
  1399. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1400. public Rectangle Bounds {
  1401. get {
  1402. return this.bounds;
  1403. }
  1404. set {
  1405. SetBounds(value.Left, value.Top, value.Width, value.Height, BoundsSpecified.All);
  1406. }
  1407. }
  1408. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1409. [Browsable(false)]
  1410. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1411. public bool CanFocus {
  1412. get {
  1413. if (IsHandleCreated && Visible && Enabled) {
  1414. return true;
  1415. }
  1416. return false;
  1417. }
  1418. }
  1419. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1420. [Browsable(false)]
  1421. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1422. public bool CanSelect {
  1423. get {
  1424. Control parent;
  1425. if (!GetStyle(ControlStyles.Selectable)) {
  1426. return false;
  1427. }
  1428. parent = this;
  1429. while (parent != null) {
  1430. if (!parent.is_visible || !parent.is_enabled) {
  1431. return false;
  1432. }
  1433. parent = parent.parent;
  1434. }
  1435. return true;
  1436. }
  1437. }
  1438. internal virtual bool InternalCapture {
  1439. get {
  1440. return Capture;
  1441. }
  1442. set {
  1443. Capture = value;
  1444. }
  1445. }
  1446. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1447. [Browsable(false)]
  1448. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1449. public bool Capture {
  1450. get {
  1451. return this.is_captured;
  1452. }
  1453. set {
  1454. if (this.IsHandleCreated && value != is_captured) {
  1455. if (value) {
  1456. is_captured = true;
  1457. XplatUI.GrabWindow(this.window.Handle, IntPtr.Zero);
  1458. } else {
  1459. XplatUI.UngrabWindow(this.window.Handle);
  1460. is_captured = false;
  1461. }
  1462. }
  1463. }
  1464. }
  1465. [DefaultValue(true)]
  1466. [MWFCategory("Focus")]
  1467. public bool CausesValidation {
  1468. get {
  1469. return this.causes_validation;
  1470. }
  1471. set {
  1472. if (this.causes_validation != value) {
  1473. causes_validation = value;
  1474. OnCausesValidationChanged(EventArgs.Empty);
  1475. }
  1476. }
  1477. }
  1478. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1479. [Browsable(false)]
  1480. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1481. public Rectangle ClientRectangle {
  1482. get {
  1483. client_rect.Width = client_size.Width;
  1484. client_rect.Height = client_size.Height;
  1485. return client_rect;
  1486. }
  1487. }
  1488. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1489. [Browsable(false)]
  1490. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1491. public Size ClientSize {
  1492. get {
  1493. #if notneeded
  1494. if ((this is Form) && (((Form)this).form_parent_window != null)) {
  1495. return ((Form)this).form_parent_window.ClientSize;
  1496. }
  1497. #endif
  1498. return client_size;
  1499. }
  1500. set {
  1501. this.SetClientSizeCore(value.Width, value.Height);
  1502. }
  1503. }
  1504. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1505. [Browsable(false)]
  1506. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1507. [DescriptionAttribute("ControlCompanyNameDescr")]
  1508. public String CompanyName {
  1509. get {
  1510. return "Mono Project, Novell, Inc.";
  1511. }
  1512. }
  1513. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1514. [Browsable(false)]
  1515. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1516. public bool ContainsFocus {
  1517. get {
  1518. IntPtr focused_window;
  1519. focused_window = XplatUI.GetFocus();
  1520. if (IsHandleCreated) {
  1521. if (focused_window == Handle) {
  1522. return true;
  1523. }
  1524. for (int i=0; i < child_controls.Count; i++) {
  1525. if (child_controls[i].ContainsFocus) {
  1526. return true;
  1527. }
  1528. }
  1529. }
  1530. return false;
  1531. }
  1532. }
  1533. [DefaultValue(null)]
  1534. [MWFCategory("Behavior")]
  1535. public virtual ContextMenu ContextMenu {
  1536. get {
  1537. return context_menu;
  1538. }
  1539. set {
  1540. if (context_menu != value) {
  1541. context_menu = value;
  1542. OnContextMenuChanged(EventArgs.Empty);
  1543. }
  1544. }
  1545. }
  1546. [Browsable(false)]
  1547. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  1548. public ControlCollection Controls {
  1549. get {
  1550. return this.child_controls;
  1551. }
  1552. }
  1553. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1554. [Browsable(false)]
  1555. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1556. public bool Created {
  1557. get {
  1558. return (!is_disposed && is_created);
  1559. }
  1560. }
  1561. [AmbientValue(null)]
  1562. [MWFCategory("Appearance")]
  1563. public virtual Cursor Cursor {
  1564. get {
  1565. if (cursor != null) {
  1566. return cursor;
  1567. }
  1568. if (parent != null) {
  1569. return parent.Cursor;
  1570. }
  1571. return Cursors.Default;
  1572. }
  1573. set {
  1574. if (cursor != value) {
  1575. Point pt;
  1576. cursor = value;
  1577. if (IsHandleCreated) {
  1578. pt = Cursor.Position;
  1579. if (bounds.Contains(pt) || Capture) {
  1580. if (GetChildAtPoint(pt) == null) {
  1581. if (cursor != null) {
  1582. XplatUI.SetCursor(window.Handle, cursor.handle);
  1583. } else {
  1584. if (parent != null) {
  1585. XplatUI.SetCursor(window.Handle, parent.Cursor.handle);
  1586. } else {
  1587. XplatUI.SetCursor(window.Handle, Cursors.Default.handle);
  1588. }
  1589. }
  1590. }
  1591. }
  1592. }
  1593. OnCursorChanged(EventArgs.Empty);
  1594. }
  1595. }
  1596. }
  1597. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  1598. [ParenthesizePropertyName(true)]
  1599. [RefreshProperties(RefreshProperties.All)]
  1600. [MWFCategory("Data")]
  1601. public ControlBindingsCollection DataBindings {
  1602. get {
  1603. if (data_bindings == null)
  1604. data_bindings = new ControlBindingsCollection (this);
  1605. return data_bindings;
  1606. }
  1607. }
  1608. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1609. [Browsable(false)]
  1610. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1611. public virtual Rectangle DisplayRectangle {
  1612. get {
  1613. return ClientRectangle;
  1614. }
  1615. }
  1616. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1617. [Browsable(false)]
  1618. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1619. public bool Disposing {
  1620. get {
  1621. return is_disposed;
  1622. }
  1623. }
  1624. [Localizable(true)]
  1625. [RefreshProperties(RefreshProperties.Repaint)]
  1626. [DefaultValue(DockStyle.None)]
  1627. [MWFCategory("Layout")]
  1628. public virtual DockStyle Dock {
  1629. get {
  1630. return dock_style;
  1631. }
  1632. set {
  1633. if (dock_style == value) {
  1634. return;
  1635. }
  1636. dock_style = value;
  1637. if (parent != null) {
  1638. parent.PerformLayout(this, "Parent");
  1639. }
  1640. OnDockChanged(EventArgs.Empty);
  1641. }
  1642. }
  1643. [DispId(-514)]
  1644. [Localizable(true)]
  1645. [MWFCategory("Behavior")]
  1646. public bool Enabled {
  1647. get {
  1648. if (!is_enabled) {
  1649. return false;
  1650. }
  1651. if (parent != null) {
  1652. return parent.Enabled;
  1653. }
  1654. return true;
  1655. }
  1656. set {
  1657. bool old_value = is_enabled;
  1658. is_enabled = value;
  1659. if (old_value != value && !value && this.has_focus)
  1660. SelectNextControl(this, true, true, true, true);
  1661. OnEnabledChanged (EventArgs.Empty);
  1662. }
  1663. }
  1664. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1665. [Browsable(false)]
  1666. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1667. public virtual bool Focused {
  1668. get {
  1669. return this.has_focus;
  1670. }
  1671. }
  1672. [DispId(-512)]
  1673. [AmbientValue(null)]
  1674. [Localizable(true)]
  1675. [MWFCategory("Appearance")]
  1676. public virtual Font Font {
  1677. get {
  1678. if (font != null) {
  1679. return font;
  1680. }
  1681. if (Parent != null && Parent.Font != null) {
  1682. return Parent.Font;
  1683. }
  1684. return DefaultFont;
  1685. }
  1686. [param:MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Font))]
  1687. set {
  1688. if (font != null && font.Equals (value)) {
  1689. return;
  1690. }
  1691. font = value;
  1692. Invalidate();
  1693. OnFontChanged (EventArgs.Empty);
  1694. }
  1695. }
  1696. [DispId(-513)]
  1697. [MWFCategory("Appearance")]
  1698. public virtual Color ForeColor {
  1699. get {
  1700. if (foreground_color.IsEmpty) {
  1701. if (parent!=null) {
  1702. return parent.ForeColor;
  1703. }
  1704. return DefaultForeColor;
  1705. }
  1706. return foreground_color;
  1707. }
  1708. set {
  1709. if (foreground_color != value) {
  1710. foreground_color=value;
  1711. Invalidate();
  1712. OnForeColorChanged(EventArgs.Empty);
  1713. }
  1714. }
  1715. }
  1716. [DispId(-515)]
  1717. [Browsable(false)]
  1718. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1719. public IntPtr Handle { // IWin32Window
  1720. get {
  1721. #if NET_2_0
  1722. if (verify_thread_handle) {
  1723. if (this.InvokeRequired) {
  1724. throw new InvalidOperationException("Cross-thread access of handle detected. Handle access only valid on thread that created the control");
  1725. }
  1726. }
  1727. #endif
  1728. if (!IsHandleCreated) {
  1729. CreateHandle();
  1730. }
  1731. return window.Handle;
  1732. }
  1733. }
  1734. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1735. [Browsable(false)]
  1736. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1737. public bool HasChildren {
  1738. get {
  1739. if (this.child_controls.Count>0) {
  1740. return true;
  1741. }
  1742. return false;
  1743. }
  1744. }
  1745. [EditorBrowsable(EditorBrowsableState.Always)]
  1746. [Browsable(false)]
  1747. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1748. public int Height {
  1749. get {
  1750. return this.bounds.Height;
  1751. }
  1752. set {
  1753. SetBounds(bounds.X, bounds.Y, bounds.Width, value, BoundsSpecified.Height);
  1754. }
  1755. }
  1756. [AmbientValue(ImeMode.Inherit)]
  1757. [Localizable(true)]
  1758. [MWFCategory("Behavior")]
  1759. public ImeMode ImeMode {
  1760. get {
  1761. if (ime_mode == DefaultImeMode) {
  1762. if (parent != null)
  1763. return parent.ImeMode;
  1764. else
  1765. return ImeMode.NoControl; // default value
  1766. }
  1767. return ime_mode;
  1768. }
  1769. set {
  1770. if (ime_mode != value) {
  1771. ime_mode = value;
  1772. OnImeModeChanged(EventArgs.Empty);
  1773. }
  1774. }
  1775. }
  1776. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1777. [Browsable(false)]
  1778. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1779. public bool InvokeRequired { // ISynchronizeInvoke
  1780. get {
  1781. if (creator_thread != null && creator_thread!=Thread.CurrentThread) {
  1782. return true;
  1783. }
  1784. return false;
  1785. }
  1786. }
  1787. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1788. [Browsable(false)]
  1789. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1790. public bool IsAccessible {
  1791. get {
  1792. return is_accessible;
  1793. }
  1794. set {
  1795. is_accessible = value;
  1796. }
  1797. }
  1798. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1799. [Browsable(false)]
  1800. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1801. public bool IsDisposed {
  1802. get {
  1803. return this.is_disposed;
  1804. }
  1805. }
  1806. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1807. [Browsable(false)]
  1808. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1809. public bool IsHandleCreated {
  1810. get {
  1811. if ((window != null) && (window.Handle != IntPtr.Zero)) {
  1812. Hwnd hwnd = Hwnd.ObjectFromHandle (window.Handle);
  1813. if (hwnd != null && !hwnd.zombie)
  1814. return true;
  1815. }
  1816. return false;
  1817. }
  1818. }
  1819. #if NET_2_0
  1820. public virtual Layout.LayoutEngine LayoutEngine {
  1821. get { return this.layout_engine; }
  1822. }
  1823. #endif
  1824. [EditorBrowsable(EditorBrowsableState.Always)]
  1825. [Browsable(false)]
  1826. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1827. public int Left {
  1828. get {
  1829. return this.bounds.X;
  1830. }
  1831. set {
  1832. SetBounds(value, bounds.Y, bounds.Width, bounds.Height, BoundsSpecified.X);
  1833. }
  1834. }
  1835. [Localizable(true)]
  1836. [MWFCategory("Layout")]
  1837. public Point Location {
  1838. get {
  1839. return new Point(bounds.X, bounds.Y);
  1840. }
  1841. set {
  1842. SetBounds(value.X, value.Y, bounds.Width, bounds.Height, BoundsSpecified.Location);
  1843. }
  1844. }
  1845. #if NET_2_0
  1846. [Localizable (true)]
  1847. public Padding Margin {
  1848. get { return this.margin; }
  1849. set { this.margin = value; }
  1850. }
  1851. #endif
  1852. [Browsable(false)]
  1853. public string Name {
  1854. get {
  1855. return name;
  1856. }
  1857. set {
  1858. name = value;
  1859. }
  1860. }
  1861. #if NET_2_0
  1862. [Localizable(true)]
  1863. public Padding Padding {
  1864. get {
  1865. return padding;
  1866. }
  1867. set {
  1868. padding = value;
  1869. }
  1870. }
  1871. #endif
  1872. [Browsable(false)]
  1873. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1874. public Control Parent {
  1875. get {
  1876. return this.parent;
  1877. }
  1878. set {
  1879. if (value == this) {
  1880. throw new ArgumentException("A circular control reference has been made. A control cannot be owned or parented to itself.");
  1881. }
  1882. if (parent!=value) {
  1883. if (value==null) {
  1884. parent.Controls.Remove(this);
  1885. parent = null;
  1886. return;
  1887. }
  1888. value.Controls.Add(this);
  1889. }
  1890. }
  1891. }
  1892. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1893. [Browsable(false)]
  1894. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1895. public string ProductName {
  1896. get {
  1897. Type t = typeof (AssemblyProductAttribute);
  1898. Assembly assembly = GetType().Module.Assembly;
  1899. object [] attrs = assembly.GetCustomAttributes (t, false);
  1900. AssemblyProductAttribute a = null;
  1901. // On MS we get a NullRefException if product attribute is not
  1902. // set.
  1903. if (attrs != null && attrs.Length > 0)
  1904. a = (AssemblyProductAttribute) attrs [0];
  1905. return a.Product;
  1906. }
  1907. }
  1908. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1909. [Browsable(false)]
  1910. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1911. public string ProductVersion {
  1912. get {
  1913. Type t = typeof (AssemblyVersionAttribute);
  1914. Assembly assembly = GetType().Module.Assembly;
  1915. object [] attrs = assembly.GetCustomAttributes (t, false);
  1916. if (attrs == null || attrs.Length < 1)
  1917. return "1.0.0.0";
  1918. return ((AssemblyVersionAttribute)attrs [0]).Version;
  1919. }
  1920. }
  1921. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1922. [Browsable(false)]
  1923. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1924. public bool RecreatingHandle {
  1925. get {
  1926. return is_recreating;
  1927. }
  1928. }
  1929. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1930. [Browsable(false)]
  1931. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1932. public Region Region {
  1933. get {
  1934. return clip_region;
  1935. }
  1936. set {
  1937. if (IsHandleCreated) {
  1938. XplatUI.SetClipRegion(Handle, value);
  1939. }
  1940. clip_region = value;
  1941. }
  1942. }
  1943. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1944. [Browsable(false)]
  1945. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1946. public int Right {
  1947. get {
  1948. return this.bounds.X+this.bounds.Width;
  1949. }
  1950. }
  1951. [AmbientValue(RightToLeft.Inherit)]
  1952. [Localizable(true)]
  1953. [MWFCategory("Appearance")]
  1954. public virtual RightToLeft RightToLeft {
  1955. get {
  1956. if (right_to_left == RightToLeft.Inherit) {
  1957. if (parent != null)
  1958. return parent.RightToLeft;
  1959. else
  1960. return RightToLeft.No; // default value
  1961. }
  1962. return right_to_left;
  1963. }
  1964. set {
  1965. if (value != right_to_left) {
  1966. right_to_left = value;
  1967. OnRightToLeftChanged(EventArgs.Empty);
  1968. }
  1969. }
  1970. }
  1971. [EditorBrowsable(EditorBrowsableState.Advanced)]
  1972. public override ISite Site {
  1973. get {
  1974. return base.Site;
  1975. }
  1976. set {
  1977. base.Site = value;
  1978. AmbientProperties ap = (AmbientProperties) value.GetService (typeof (AmbientProperties));
  1979. if (ap != null) {
  1980. BackColor = ap.BackColor;
  1981. ForeColor = ap.ForeColor;
  1982. Cursor = ap.Cursor;
  1983. Font = ap.Font;
  1984. }
  1985. }
  1986. }
  1987. [Localizable(true)]
  1988. [MWFCategory("Layout")]
  1989. public Size Size {
  1990. get {
  1991. return new Size(Width, Height);
  1992. }
  1993. set {
  1994. SetBounds(bounds.X, bounds.Y, value.Width, value.Height, BoundsSpecified.Size);
  1995. }
  1996. }
  1997. [Localizable(true)]
  1998. [MergableProperty(false)]
  1999. [MWFCategory("Behavior")]
  2000. public int TabIndex {
  2001. get {
  2002. if (tab_index != -1) {
  2003. return tab_index;
  2004. }
  2005. return 0;
  2006. }
  2007. set {
  2008. if (tab_index != value) {
  2009. tab_index = value;
  2010. OnTabIndexChanged(EventArgs.Empty);
  2011. }
  2012. }
  2013. }
  2014. [DispId(-516)]
  2015. [DefaultValue(true)]
  2016. [MWFCategory("Behavior")]
  2017. public bool TabStop {
  2018. get {
  2019. return tab_stop;
  2020. }
  2021. set {
  2022. if (tab_stop != value) {
  2023. tab_stop = value;
  2024. OnTabStopChanged(EventArgs.Empty);
  2025. }
  2026. }
  2027. }
  2028. [Localizable(false)]
  2029. [Bindable(true)]
  2030. [TypeConverter(typeof(StringConverter))]
  2031. [DefaultValue(null)]
  2032. [MWFCategory("Data")]
  2033. public object Tag {
  2034. get {
  2035. return control_tag;
  2036. }
  2037. set {
  2038. control_tag = value;
  2039. }
  2040. }
  2041. [DispId(-517)]
  2042. [Localizable(true)]
  2043. [BindableAttribute(true)]
  2044. [MWFCategory("Appearance")]
  2045. public virtual string Text {
  2046. get {
  2047. // Our implementation ignores ControlStyles.CacheText - we always cache
  2048. return this.text;
  2049. }
  2050. set {
  2051. if (value == null) {
  2052. value = String.Empty;
  2053. }
  2054. if (text!=value) {
  2055. text=value;
  2056. if (IsHandleCreated) {
  2057. /* we need to call .SetWindowStyle here instead of just .Text
  2058. because the presence/absence of Text (== "" or not) can cause
  2059. other window style things to appear/disappear */
  2060. XplatUI.SetWindowStyle(window.Handle, CreateParams);
  2061. XplatUI.Text(Handle, text);
  2062. }
  2063. OnTextChanged (EventArgs.Empty);
  2064. }
  2065. }
  2066. }
  2067. [EditorBrowsable(EditorBrowsableState.Always)]
  2068. [Browsable(false)]
  2069. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2070. public int Top {
  2071. get {
  2072. return this.bounds.Y;
  2073. }
  2074. set {
  2075. SetBounds(bounds.X, value, bounds.Width, bounds.Height, BoundsSpecified.Y);
  2076. }
  2077. }
  2078. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2079. [Browsable(false)]
  2080. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2081. public Control TopLevelControl {
  2082. get {
  2083. Control p = this;
  2084. while (p.parent != null) {
  2085. p = p.parent;
  2086. }
  2087. return p is Form ? p : null;
  2088. }
  2089. }
  2090. [Localizable(true)]
  2091. [MWFCategory("Behavior")]
  2092. public bool Visible {
  2093. get {
  2094. if (!is_visible) {
  2095. return false;
  2096. } else if (parent != null) {
  2097. return parent.Visible;
  2098. }
  2099. return true;
  2100. }
  2101. set {
  2102. SetVisibleCore(value);
  2103. }
  2104. }
  2105. [EditorBrowsable(EditorBrowsableState.Always)]
  2106. [Browsable(false)]
  2107. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2108. public int Width {
  2109. get {
  2110. return this.bounds.Width;
  2111. }
  2112. set {
  2113. SetBounds(bounds.X, bounds.Y, value, bounds.Height, BoundsSpecified.Width);
  2114. }
  2115. }
  2116. [EditorBrowsable(EditorBrowsableState.Never)]
  2117. [Browsable(false)]
  2118. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2119. public IWindowTarget WindowTarget {
  2120. get {
  2121. return null;
  2122. }
  2123. set {
  2124. ; // MS Internal
  2125. }
  2126. }
  2127. #endregion // Public Instance Properties
  2128. #region Protected Instance Properties
  2129. protected virtual CreateParams CreateParams {
  2130. get {
  2131. CreateParams create_params = new CreateParams();
  2132. try {
  2133. create_params.Caption = Text;
  2134. }
  2135. catch {
  2136. create_params.Caption = text;
  2137. }
  2138. try {
  2139. create_params.X = Left;
  2140. }
  2141. catch {
  2142. create_params.X = this.bounds.X;
  2143. }
  2144. try {
  2145. create_params.Y = Top;
  2146. }
  2147. catch {
  2148. create_params.Y = this.bounds.Y;
  2149. }
  2150. try {
  2151. create_params.Width = Width;
  2152. }
  2153. catch {
  2154. create_params.Width = this.bounds.Width;
  2155. }
  2156. try {
  2157. create_params.Height = Height;
  2158. }
  2159. catch {
  2160. create_params.Height = this.bounds.Height;
  2161. }
  2162. create_params.ClassName = XplatUI.DefaultClassName;
  2163. create_params.ClassStyle = 0;
  2164. create_params.ExStyle = 0;
  2165. create_params.Param = 0;
  2166. if (allow_drop) {
  2167. create_params.ExStyle |= (int)WindowExStyles.WS_EX_ACCEPTFILES;
  2168. }
  2169. if ((parent!=null) && (parent.IsHandleCreated)) {
  2170. create_params.Parent = parent.Handle;
  2171. }
  2172. create_params.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
  2173. if (is_visible) {
  2174. create_params.Style |= (int)WindowStyles.WS_VISIBLE;
  2175. }
  2176. if (!is_enabled) {
  2177. create_params.Style |= (int)WindowStyles.WS_DISABLED;
  2178. }
  2179. switch (border_style) {
  2180. case BorderStyle.FixedSingle:
  2181. create_params.Style |= (int) WindowStyles.WS_BORDER;
  2182. break;
  2183. case BorderStyle.Fixed3D:
  2184. create_params.ExStyle |= (int) WindowExStyles.WS_EX_CLIENTEDGE;
  2185. break;
  2186. }
  2187. return create_params;
  2188. }
  2189. }
  2190. protected virtual ImeMode DefaultImeMode {
  2191. get {
  2192. return ImeMode.Inherit;
  2193. }
  2194. }
  2195. #if NET_2_0
  2196. protected virtual Padding DefaultMargin {
  2197. get { return new Padding (3); }
  2198. }
  2199. #endif
  2200. protected virtual Size DefaultSize {
  2201. get {
  2202. return new Size(0, 0);
  2203. }
  2204. }
  2205. protected int FontHeight {
  2206. get {
  2207. return Font.Height;
  2208. }
  2209. set {
  2210. ;; // Nothing to do
  2211. }
  2212. }
  2213. protected bool RenderRightToLeft {
  2214. get {
  2215. return (this.right_to_left == RightToLeft.Yes);
  2216. }
  2217. }
  2218. protected bool ResizeRedraw {
  2219. get {
  2220. return GetStyle(ControlStyles.ResizeRedraw);
  2221. }
  2222. set {
  2223. SetStyle(ControlStyles.ResizeRedraw, value);
  2224. }
  2225. }
  2226. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2227. [Browsable(false)]
  2228. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2229. protected virtual bool ShowFocusCues {
  2230. get {
  2231. return true;
  2232. }
  2233. }
  2234. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2235. [Browsable(false)]
  2236. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2237. protected bool ShowKeyboardCues {
  2238. get {
  2239. return true;
  2240. }
  2241. }
  2242. #endregion // Protected Instance Properties
  2243. #region Public Static Methods
  2244. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2245. public static Control FromChildHandle(IntPtr handle) {
  2246. return Control.ControlNativeWindow.ControlFromChildHandle (handle);
  2247. }
  2248. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2249. public static Control FromHandle(IntPtr handle) {
  2250. return Control.ControlNativeWindow.ControlFromHandle(handle);
  2251. }
  2252. public static bool IsMnemonic(char charCode, string text) {
  2253. int amp;
  2254. amp = text.IndexOf('&');
  2255. if (amp != -1) {
  2256. if (amp + 1 < text.Length) {
  2257. if (text[amp + 1] != '&') {
  2258. if (Char.ToUpper(charCode) == Char.ToUpper(text.ToCharArray(amp + 1, 1)[0])) {
  2259. return true;
  2260. }
  2261. }
  2262. }
  2263. }
  2264. return false;
  2265. }
  2266. #endregion
  2267. #region Protected Static Methods
  2268. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2269. protected static bool ReflectMessage(IntPtr hWnd, ref Message m) {
  2270. Control c;
  2271. c = Control.FromHandle(hWnd);
  2272. if (c != null) {
  2273. c.WndProc(ref m);
  2274. return true;
  2275. }
  2276. return false;
  2277. }
  2278. #endregion
  2279. #region Public Instance Methods
  2280. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2281. public IAsyncResult BeginInvoke(Delegate method) {
  2282. object [] prms = null;
  2283. if (method is EventHandler)
  2284. prms = new object [] { this, EventArgs.Empty };
  2285. return BeginInvokeInternal(method, prms, false);
  2286. }
  2287. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2288. public IAsyncResult BeginInvoke (Delegate method, object[] args) {
  2289. return BeginInvokeInternal (method, args, false);
  2290. }
  2291. public void BringToFront() {
  2292. if (parent != null) {
  2293. parent.child_controls.SetChildIndex(this, 0);
  2294. parent.Refresh();
  2295. } else {
  2296. XplatUI.SetZOrder(Handle, IntPtr.Zero, false, false);
  2297. }
  2298. }
  2299. public bool Contains(Control ctl) {
  2300. while (ctl != null) {
  2301. ctl = ctl.parent;
  2302. if (ctl == this) {
  2303. return true;
  2304. }
  2305. }
  2306. return false;
  2307. }
  2308. public void CreateControl() {
  2309. if (is_disposed) {
  2310. throw new ObjectDisposedException(GetType().FullName.ToString());
  2311. }
  2312. if (is_created) {
  2313. return;
  2314. }
  2315. if (!IsHandleCreated) {
  2316. CreateHandle();
  2317. }
  2318. if (!is_created) {
  2319. is_created = true;
  2320. }
  2321. Control [] controls = child_controls.GetAllControls ();
  2322. for (int i=0; i<controls.Length; i++) {
  2323. controls [i].CreateControl ();
  2324. }
  2325. UpdateChildrenZOrder();
  2326. if (binding_context == null) { // seem to be sent whenever it's null?
  2327. OnBindingContextChanged(EventArgs.Empty);
  2328. }
  2329. OnCreateControl();
  2330. }
  2331. public Graphics CreateGraphics() {
  2332. if (!IsHandleCreated) {
  2333. this.CreateHandle();
  2334. }
  2335. return Graphics.FromHwnd(this.window.Handle);
  2336. }
  2337. public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects) {
  2338. return XplatUI.StartDrag(this.window.Handle, data, allowedEffects);
  2339. }
  2340. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2341. public object EndInvoke (IAsyncResult async_result) {
  2342. AsyncMethodResult result = (AsyncMethodResult) async_result;
  2343. return result.EndInvoke ();
  2344. }
  2345. public Form FindForm() {
  2346. Control c;
  2347. c = this;
  2348. while (c != null) {
  2349. if (c is Form) {
  2350. return (Form)c;
  2351. }
  2352. c = c.Parent;
  2353. }
  2354. return null;
  2355. }
  2356. public bool Focus() {
  2357. if (CanFocus && IsHandleCreated && !has_focus && !is_focusing) {
  2358. is_focusing = true;
  2359. Select(this);
  2360. is_focusing = false;
  2361. }
  2362. return has_focus;
  2363. }
  2364. public Control GetChildAtPoint(Point pt) {
  2365. // Microsoft's version of this function doesn't seem to work, so I can't check
  2366. // if we only consider children or also grandchildren, etc.
  2367. // I'm gonna say 'children only'
  2368. for (int i=0; i<child_controls.Count; i++) {
  2369. if (child_controls[i].Bounds.Contains(pt)) {
  2370. return child_controls[i];
  2371. }
  2372. }
  2373. return null;
  2374. }
  2375. public IContainerControl GetContainerControl() {
  2376. Control current = this;
  2377. while (current!=null) {
  2378. if ((current is IContainerControl) && ((current.control_style & ControlStyles.ContainerControl)!=0)) {
  2379. return (IContainerControl)current;
  2380. }
  2381. current = current.parent;
  2382. }
  2383. return null;
  2384. }
  2385. public Control GetNextControl(Control ctl, bool forward) {
  2386. if (!this.Contains(ctl)) {
  2387. ctl = this;
  2388. }
  2389. if (forward) {
  2390. ctl = FindControlForward(this, ctl);
  2391. }
  2392. else {
  2393. ctl = FindControlBackward(this, ctl);
  2394. }
  2395. if (ctl != this) {
  2396. return ctl;
  2397. }
  2398. return null;
  2399. }
  2400. #if NET_2_0
  2401. public virtual Size GetPreferredSize (Size proposedSize) {
  2402. return preferred_size;
  2403. }
  2404. #endif
  2405. public void Hide() {
  2406. this.Visible = false;
  2407. }
  2408. public void Invalidate() {
  2409. Invalidate(ClientRectangle, false);
  2410. }
  2411. public void Invalidate(bool invalidateChildren) {
  2412. Invalidate(ClientRectangle, invalidateChildren);
  2413. }
  2414. public void Invalidate(System.Drawing.Rectangle rc) {
  2415. Invalidate(rc, false);
  2416. }
  2417. public void Invalidate(System.Drawing.Rectangle rc, bool invalidateChildren) {
  2418. if (!IsHandleCreated || !Visible || rc.Width == 0 || rc.Height == 0) {
  2419. return;
  2420. }
  2421. NotifyInvalidate(rc);
  2422. XplatUI.Invalidate(Handle, rc, false);
  2423. if (invalidateChildren) {
  2424. Control [] controls = child_controls.GetAllControls ();
  2425. for (int i=0; i<controls.Length; i++)
  2426. controls [i].Invalidate ();
  2427. }
  2428. OnInvalidated(new InvalidateEventArgs(rc));
  2429. }
  2430. public void Invalidate(System.Drawing.Region region) {
  2431. Invalidate(region, false);
  2432. }
  2433. public void Invalidate(System.Drawing.Region region, bool invalidateChildren) {
  2434. RectangleF bounds = region.GetBounds (CreateGraphics ());
  2435. Invalidate (new Rectangle ((int) bounds.X, (int) bounds.Y, (int) bounds.Width, (int) bounds.Height),
  2436. invalidateChildren);
  2437. }
  2438. public object Invoke (Delegate method) {
  2439. object [] prms = null;
  2440. if (method is EventHandler)
  2441. prms = new object [] { this, EventArgs.Empty };
  2442. return Invoke(method, prms);
  2443. }
  2444. public object Invoke (Delegate method, object[] args) {
  2445. if (!this.InvokeRequired) {
  2446. return method.DynamicInvoke(args);
  2447. }
  2448. IAsyncResult result = BeginInvoke (method, args);
  2449. return EndInvoke(result);
  2450. }
  2451. internal object InvokeInternal (Delegate method, bool disposing) {
  2452. return InvokeInternal(method, null, disposing);
  2453. }
  2454. internal object InvokeInternal (Delegate method, object[] args, bool disposing) {
  2455. if (!this.InvokeRequired) {
  2456. return method.DynamicInvoke(args);
  2457. }
  2458. IAsyncResult result = BeginInvokeInternal (method, args, disposing);
  2459. return EndInvoke(result);
  2460. }
  2461. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2462. public void PerformLayout() {
  2463. PerformLayout(null, null);
  2464. }
  2465. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2466. public void PerformLayout(Control affectedControl, string affectedProperty) {
  2467. LayoutEventArgs levent = new LayoutEventArgs(affectedControl, affectedProperty);
  2468. if (layout_suspended > 0) {
  2469. layout_pending = true;
  2470. return;
  2471. }
  2472. layout_pending = false;
  2473. // Prevent us from getting messed up
  2474. layout_suspended++;
  2475. // Perform all Dock and Anchor calculations
  2476. try {
  2477. #if NET_2_0
  2478. this.layout_engine.Layout(this, levent);
  2479. #else
  2480. // This has been moved to Layout/DefaultLayout.cs for 2.0, please duplicate any changes/fixes there.
  2481. Control child;
  2482. AnchorStyles anchor;
  2483. Rectangle space;
  2484. space = DisplayRectangle;
  2485. // Deal with docking; go through in reverse, MS docs say that lowest Z-order is closest to edge
  2486. Control [] controls = child_controls.GetAllControls ();
  2487. for (int i = controls.Length - 1; i >= 0; i--) {
  2488. child = controls [i];
  2489. if (!child.Visible) {
  2490. continue;
  2491. }
  2492. switch (child.Dock) {
  2493. case DockStyle.None: {
  2494. // Do nothing
  2495. break;
  2496. }
  2497. case DockStyle.Left: {
  2498. child.SetBounds(space.Left, space.Y, child.Width, space.Height);
  2499. space.X+=child.Width;
  2500. space.Width-=child.Width;
  2501. break;
  2502. }
  2503. case DockStyle.Top: {
  2504. child.SetBounds(space.Left, space.Y, space.Width, child.Height);
  2505. space.Y+=child.Height;
  2506. space.Height-=child.Height;
  2507. break;
  2508. }
  2509. case DockStyle.Right: {
  2510. child.SetBounds(space.Right-child.Width, space.Y, child.Width, space.Height);
  2511. space.Width-=child.Width;
  2512. break;
  2513. }
  2514. case DockStyle.Bottom: {
  2515. child.SetBounds(space.Left, space.Bottom-child.Height, space.Width, child.Height);
  2516. space.Height-=child.Height;
  2517. break;
  2518. }
  2519. }
  2520. }
  2521. for (int i = controls.Length - 1; i >= 0; i--) {
  2522. child=controls[i];
  2523. //if (child.Visible && (child.Dock == DockStyle.Fill)) {
  2524. if (child.Dock == DockStyle.Fill) {
  2525. child.SetBounds(space.Left, space.Top, space.Width, space.Height);
  2526. }
  2527. }
  2528. space = DisplayRectangle;
  2529. for (int i=0; i < controls.Length; i++) {
  2530. int left;
  2531. int top;
  2532. int width;
  2533. int height;
  2534. child = controls[i];
  2535. // If the control is docked we don't need to do anything
  2536. if (child.Dock != DockStyle.None) {
  2537. continue;
  2538. }
  2539. anchor = child.Anchor;
  2540. left = child.Left;
  2541. top = child.Top;
  2542. width = child.Width;
  2543. height = child.Height;
  2544. if ((anchor & AnchorStyles.Left) !=0 ) {
  2545. if ((anchor & AnchorStyles.Right) != 0) {
  2546. width = space.Width - child.dist_right - left;
  2547. } else {
  2548. ; // Left anchored only, nothing to be done
  2549. }
  2550. } else if ((anchor & AnchorStyles.Right) != 0) {
  2551. left = space.Width - child.dist_right - width;
  2552. } else {
  2553. // left+=diff_width/2 will introduce rounding errors (diff_width removed from svn after r51780)
  2554. // This calculates from scratch every time:
  2555. left = child.dist_left + (space.Width - (child.dist_left + width + child.dist_right)) / 2;
  2556. }
  2557. if ((anchor & AnchorStyles.Top) !=0 ) {
  2558. if ((anchor & AnchorStyles.Bottom) != 0) {
  2559. height = space.Height - child.dist_bottom - top;
  2560. } else {
  2561. ; // Top anchored only, nothing to be done
  2562. }
  2563. } else if ((anchor & AnchorStyles.Bottom) != 0) {
  2564. top = space.Height - child.dist_bottom - height;
  2565. } else {
  2566. // top += diff_height/2 will introduce rounding errors (diff_height removed from after r51780)
  2567. // This calculates from scratch every time:
  2568. top = child.dist_top + (space.Height - (child.dist_top + height + child.dist_bottom)) / 2;
  2569. }
  2570. // Sanity
  2571. if (width < 0) {
  2572. width=0;
  2573. }
  2574. if (height < 0) {
  2575. height=0;
  2576. }
  2577. child.SetBounds(left, top, width, height);
  2578. }
  2579. #endif
  2580. // Let everyone know
  2581. OnLayout(levent);
  2582. }
  2583. // Need to make sure we decremend layout_suspended
  2584. finally {
  2585. layout_suspended--;
  2586. }
  2587. }
  2588. public Point PointToClient (Point p) {
  2589. int x = p.X;
  2590. int y = p.Y;
  2591. XplatUI.ScreenToClient (Handle, ref x, ref y);
  2592. return new Point (x, y);
  2593. }
  2594. public Point PointToScreen(Point p) {
  2595. int x = p.X;
  2596. int y = p.Y;
  2597. XplatUI.ClientToScreen(Handle, ref x, ref y);
  2598. return new Point(x, y);
  2599. }
  2600. public virtual bool PreProcessMessage(ref Message msg) {
  2601. return InternalPreProcessMessage (ref msg);
  2602. }
  2603. internal virtual bool InternalPreProcessMessage (ref Message msg) {
  2604. Keys key_data;
  2605. if ((msg.Msg == (int)Msg.WM_KEYDOWN) || (msg.Msg == (int)Msg.WM_SYSKEYDOWN)) {
  2606. key_data = (Keys)msg.WParam.ToInt32() | XplatUI.State.ModifierKeys;
  2607. if (!ProcessCmdKey(ref msg, key_data)) {
  2608. if (IsInputKey(key_data)) {
  2609. return false;
  2610. }
  2611. return ProcessDialogKey(key_data);
  2612. }
  2613. return true;
  2614. } else if (msg.Msg == (int)Msg.WM_CHAR) {
  2615. if (IsInputChar((char)msg.WParam)) {
  2616. return false;
  2617. }
  2618. return ProcessDialogChar((char)msg.WParam);
  2619. } else if (msg.Msg == (int)Msg.WM_SYSCHAR) {
  2620. return ProcessDialogChar((char)msg.WParam);
  2621. }
  2622. return false;
  2623. }
  2624. public Rectangle RectangleToClient(Rectangle r) {
  2625. return new Rectangle(PointToClient(r.Location), r.Size);
  2626. }
  2627. public Rectangle RectangleToScreen(Rectangle r) {
  2628. return new Rectangle(PointToScreen(r.Location), r.Size);
  2629. }
  2630. public virtual void Refresh() {
  2631. if (IsHandleCreated == true) {
  2632. Invalidate();
  2633. XplatUI.UpdateWindow(window.Handle);
  2634. Control [] controls = child_controls.GetAllControls ();
  2635. for (int i=0; i < controls.Length; i++) {
  2636. controls[i].Refresh();
  2637. }
  2638. }
  2639. }
  2640. [EditorBrowsable(EditorBrowsableState.Never)]
  2641. public virtual void ResetBackColor() {
  2642. BackColor = Color.Empty;
  2643. }
  2644. [EditorBrowsable(EditorBrowsableState.Never)]
  2645. public void ResetBindings() {
  2646. data_bindings.Clear();
  2647. }
  2648. [EditorBrowsable(EditorBrowsableState.Never)]
  2649. public virtual void ResetCursor() {
  2650. Cursor = null;
  2651. }
  2652. [EditorBrowsable(EditorBrowsableState.Never)]
  2653. public virtual void ResetFont() {
  2654. font = null;
  2655. }
  2656. [EditorBrowsable(EditorBrowsableState.Never)]
  2657. public virtual void ResetForeColor() {
  2658. foreground_color = Color.Empty;
  2659. }
  2660. [EditorBrowsable(EditorBrowsableState.Never)]
  2661. public void ResetImeMode() {
  2662. ime_mode = DefaultImeMode;
  2663. }
  2664. [EditorBrowsable(EditorBrowsableState.Never)]
  2665. public virtual void ResetRightToLeft() {
  2666. right_to_left = RightToLeft.Inherit;
  2667. }
  2668. public virtual void ResetText() {
  2669. text = String.Empty;
  2670. }
  2671. public void ResumeLayout() {
  2672. ResumeLayout (true);
  2673. }
  2674. public void ResumeLayout(bool performLayout) {
  2675. if (layout_suspended > 0) {
  2676. layout_suspended--;
  2677. }
  2678. if (layout_suspended == 0) {
  2679. Control [] controls = child_controls.GetAllControls ();
  2680. for (int i=0; i<controls.Length; i++) {
  2681. controls [i].UpdateDistances ();
  2682. }
  2683. if (performLayout && layout_pending) {
  2684. PerformLayout();
  2685. }
  2686. }
  2687. }
  2688. public void Scale(float ratio) {
  2689. ScaleCore(ratio, ratio);
  2690. }
  2691. public void Scale(float dx, float dy) {
  2692. ScaleCore(dx, dy);
  2693. }
  2694. #if NET_2_0
  2695. public void Scale(SizeF factor) {
  2696. ScaleCore(factor.Width, factor.Height);
  2697. }
  2698. #endif
  2699. public void Select() {
  2700. Select(false, false);
  2701. }
  2702. #if DebugFocus
  2703. private void printTree(Control c, string t) {
  2704. foreach(Control i in c.child_controls) {
  2705. Console.WriteLine("{2}{0}.TabIndex={1}", i, i.tab_index, t);
  2706. printTree(i, t+"\t");
  2707. }
  2708. }
  2709. #endif
  2710. public bool SelectNextControl(Control ctl, bool forward, bool tabStopOnly, bool nested, bool wrap) {
  2711. Control c;
  2712. #if DebugFocus
  2713. Console.WriteLine("{0}", this.FindForm());
  2714. printTree(this, "\t");
  2715. #endif
  2716. if (!this.Contains(ctl) || (!nested && (ctl.parent != this))) {
  2717. ctl = null;
  2718. }
  2719. c = ctl;
  2720. do {
  2721. c = GetNextControl(c, forward);
  2722. if (c == null) {
  2723. if (wrap) {
  2724. wrap = false;
  2725. continue;
  2726. }
  2727. break;
  2728. }
  2729. if (c.CanSelect && ((c.parent == this) || nested) && (c.tab_stop || !tabStopOnly)) {
  2730. c.Select (true, true);
  2731. return true;
  2732. }
  2733. } while (c != ctl); // If we wrap back to ourselves we stop
  2734. return false;
  2735. }
  2736. public void SendToBack() {
  2737. if (parent != null) {
  2738. parent.child_controls.SetChildIndex(this, parent.child_controls.Count);
  2739. }
  2740. }
  2741. public void SetBounds(int x, int y, int width, int height) {
  2742. SetBounds(x, y, width, height, BoundsSpecified.All);
  2743. }
  2744. public void SetBounds(int x, int y, int width, int height, BoundsSpecified specified) {
  2745. if ((specified & BoundsSpecified.X) != BoundsSpecified.X) {
  2746. x = Left;
  2747. }
  2748. if ((specified & BoundsSpecified.Y) != BoundsSpecified.Y) {
  2749. y = Top;
  2750. }
  2751. if ((specified & BoundsSpecified.Width) != BoundsSpecified.Width) {
  2752. width = Width;
  2753. }
  2754. if ((specified & BoundsSpecified.Height) != BoundsSpecified.Height) {
  2755. height = Height;
  2756. }
  2757. SetBoundsCore(x, y, width, height, specified);
  2758. if (parent != null) {
  2759. parent.PerformLayout(this, "Bounds");
  2760. }
  2761. }
  2762. public void Show() {
  2763. if (!is_created) {
  2764. this.CreateControl();
  2765. }
  2766. this.Visible=true;
  2767. }
  2768. public void SuspendLayout() {
  2769. layout_suspended++;
  2770. }
  2771. public void Update() {
  2772. if (IsHandleCreated) {
  2773. XplatUI.UpdateWindow(window.Handle);
  2774. }
  2775. }
  2776. #endregion // Public Instance Methods
  2777. #region Protected Instance Methods
  2778. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2779. [MonoTODO("Implement this and tie it into Control.ControlAccessibleObject.NotifyClients")]
  2780. protected void AccessibilityNotifyClients(AccessibleEvents accEvent, int childID) {
  2781. throw new NotImplementedException();
  2782. }
  2783. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2784. protected virtual AccessibleObject CreateAccessibilityInstance() {
  2785. return new Control.ControlAccessibleObject(this);
  2786. }
  2787. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2788. protected virtual ControlCollection CreateControlsInstance() {
  2789. return new ControlCollection(this);
  2790. }
  2791. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2792. protected virtual void CreateHandle() {
  2793. if (IsDisposed) {
  2794. throw new ObjectDisposedException(GetType().FullName.ToString());
  2795. }
  2796. if (IsHandleCreated && !is_recreating) {
  2797. return;
  2798. }
  2799. window.CreateHandle(CreateParams);
  2800. if (window.Handle != IntPtr.Zero) {
  2801. lock (Control.controls) {
  2802. if (!Control.controls.Contains(window.Handle)) {
  2803. Control.controls.Add(this);
  2804. }
  2805. }
  2806. creator_thread = Thread.CurrentThread;
  2807. XplatUI.EnableWindow(window.Handle, is_enabled);
  2808. XplatUI.SetVisible(window.Handle, is_visible, true);
  2809. if (clip_region != null) {
  2810. XplatUI.SetClipRegion(Handle, clip_region);
  2811. }
  2812. // Set our handle with our parent
  2813. if ((parent != null) && (parent.IsHandleCreated)) {
  2814. XplatUI.SetParent(window.Handle, parent.Handle);
  2815. }
  2816. // Set our handle as parent for our children
  2817. Control [] children;
  2818. children = child_controls.GetAllControls ();
  2819. for (int i = 0; i < children.Length; i++ ) {
  2820. if (!children[i].RecreatingHandle)
  2821. XplatUI.SetParent(children[i].Handle, window.Handle);
  2822. }
  2823. UpdateStyles();
  2824. XplatUI.SetAllowDrop (Handle, allow_drop);
  2825. // Find out where the window manager placed us
  2826. if ((CreateParams.Style & (int)WindowStyles.WS_CHILD) != 0) {
  2827. XplatUI.SetBorderStyle(window.Handle, (FormBorderStyle)border_style);
  2828. }
  2829. UpdateBounds();
  2830. OnHandleCreated(EventArgs.Empty);
  2831. }
  2832. }
  2833. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2834. protected virtual void DefWndProc(ref Message m) {
  2835. window.DefWndProc(ref m);
  2836. }
  2837. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2838. protected virtual void DestroyHandle() {
  2839. if (IsHandleCreated) {
  2840. if (window != null) {
  2841. window.DestroyHandle();
  2842. }
  2843. }
  2844. }
  2845. protected bool GetStyle(ControlStyles flag) {
  2846. return (control_style & flag) != 0;
  2847. }
  2848. protected bool GetTopLevel() {
  2849. return is_toplevel;
  2850. }
  2851. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2852. protected virtual void InitLayout() {
  2853. UpdateDistances();
  2854. }
  2855. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2856. protected void InvokeGotFocus(Control toInvoke, EventArgs e) {
  2857. toInvoke.OnGotFocus(e);
  2858. }
  2859. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2860. protected void InvokeLostFocus(Control toInvoke, EventArgs e) {
  2861. toInvoke.OnLostFocus(e);
  2862. }
  2863. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2864. protected void InvokeOnClick(Control toInvoke, EventArgs e) {
  2865. toInvoke.OnClick(e);
  2866. }
  2867. protected void InvokePaint(Control toInvoke, PaintEventArgs e) {
  2868. toInvoke.OnPaint(e);
  2869. }
  2870. protected void InvokePaintBackground(Control toInvoke, PaintEventArgs e) {
  2871. toInvoke.OnPaintBackground(e);
  2872. }
  2873. protected virtual bool IsInputChar (char charCode) {
  2874. return true;
  2875. }
  2876. protected virtual bool IsInputKey (Keys keyData) {
  2877. // Doc says this one calls IsInputChar; not sure what to do with that
  2878. return false;
  2879. }
  2880. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2881. protected virtual void NotifyInvalidate(Rectangle invalidatedArea) {
  2882. // override me?
  2883. }
  2884. protected virtual bool ProcessCmdKey(ref Message msg, Keys keyData) {
  2885. if ((context_menu != null) && context_menu.ProcessCmdKey(ref msg, keyData)) {
  2886. return true;
  2887. }
  2888. if (parent != null) {
  2889. return parent.ProcessCmdKey(ref msg, keyData);
  2890. }
  2891. return false;
  2892. }
  2893. protected virtual bool ProcessDialogChar(char charCode) {
  2894. if (parent != null) {
  2895. return parent.ProcessDialogChar (charCode);
  2896. }
  2897. return false;
  2898. }
  2899. protected virtual bool ProcessDialogKey (Keys keyData) {
  2900. if (parent != null) {
  2901. return parent.ProcessDialogKey (keyData);
  2902. }
  2903. return false;
  2904. }
  2905. protected virtual bool ProcessKeyEventArgs (ref Message msg)
  2906. {
  2907. KeyEventArgs key_event;
  2908. switch (msg.Msg) {
  2909. case (int)Msg.WM_SYSKEYDOWN:
  2910. case (int)Msg.WM_KEYDOWN: {
  2911. key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
  2912. OnKeyDown (key_event);
  2913. return key_event.Handled;
  2914. }
  2915. case (int)Msg.WM_SYSKEYUP:
  2916. case (int)Msg.WM_KEYUP: {
  2917. key_event = new KeyEventArgs ((Keys)msg.WParam.ToInt32 ());
  2918. OnKeyUp (key_event);
  2919. return key_event.Handled;
  2920. }
  2921. case (int)Msg.WM_SYSCHAR:
  2922. case (int)Msg.WM_CHAR: {
  2923. KeyPressEventArgs key_press_event;
  2924. key_press_event = new KeyPressEventArgs((char)msg.WParam);
  2925. OnKeyPress(key_press_event);
  2926. #if NET_2_0
  2927. msg.WParam = (IntPtr)key_press_event.KeyChar;
  2928. #endif
  2929. return key_press_event.Handled;
  2930. }
  2931. default: {
  2932. break;
  2933. }
  2934. }
  2935. return false;
  2936. }
  2937. protected internal virtual bool ProcessKeyMessage(ref Message msg) {
  2938. if (parent != null) {
  2939. if (parent.ProcessKeyPreview(ref msg)) {
  2940. return true;
  2941. }
  2942. }
  2943. return ProcessKeyEventArgs(ref msg);
  2944. }
  2945. protected virtual bool ProcessKeyPreview(ref Message msg) {
  2946. if (parent != null) {
  2947. return parent.ProcessKeyPreview(ref msg);
  2948. }
  2949. return false;
  2950. }
  2951. protected virtual bool ProcessMnemonic(char charCode) {
  2952. // override me
  2953. return false;
  2954. }
  2955. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2956. protected void RaiseDragEvent(object key, DragEventArgs e) {
  2957. // MS Internal
  2958. }
  2959. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2960. protected void RaiseKeyEvent(object key, KeyEventArgs e) {
  2961. // MS Internal
  2962. }
  2963. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2964. protected void RaiseMouseEvent(object key, MouseEventArgs e) {
  2965. // MS Internal
  2966. }
  2967. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2968. protected void RaisePaintEvent(object key, PaintEventArgs e) {
  2969. // MS Internal
  2970. }
  2971. private void SetIsRecreating ()
  2972. {
  2973. is_recreating=true;
  2974. foreach (Control c in Controls.GetAllControls()) {
  2975. c.SetIsRecreating ();
  2976. }
  2977. }
  2978. [EditorBrowsable(EditorBrowsableState.Advanced)]
  2979. protected void RecreateHandle() {
  2980. #if DebugRecreate
  2981. Console.WriteLine("Recreating control {0}", XplatUI.Window(window.Handle));
  2982. #endif
  2983. SetIsRecreating ();
  2984. if (IsHandleCreated) {
  2985. #if DebugRecreate
  2986. Console.WriteLine(" + handle is created, destroying it.");
  2987. #endif
  2988. DestroyHandle();
  2989. // WM_DESTROY will CreateHandle for us
  2990. } else {
  2991. #if DebugRecreate
  2992. Console.WriteLine(" + handle is not created, creating it.");
  2993. #endif
  2994. if (!is_created) {
  2995. CreateControl();
  2996. } else {
  2997. CreateHandle();
  2998. }
  2999. is_recreating = false;
  3000. #if DebugRecreate
  3001. Console.WriteLine (" + new handle = {0:X}", Handle.ToInt32());
  3002. #endif
  3003. }
  3004. }
  3005. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3006. protected void ResetMouseEventArgs() {
  3007. // MS Internal
  3008. }
  3009. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3010. protected ContentAlignment RtlTranslateAlignment(ContentAlignment align) {
  3011. if (right_to_left == RightToLeft.No) {
  3012. return align;
  3013. }
  3014. switch (align) {
  3015. case ContentAlignment.TopLeft: {
  3016. return ContentAlignment.TopRight;
  3017. }
  3018. case ContentAlignment.TopRight: {
  3019. return ContentAlignment.TopLeft;
  3020. }
  3021. case ContentAlignment.MiddleLeft: {
  3022. return ContentAlignment.MiddleRight;
  3023. }
  3024. case ContentAlignment.MiddleRight: {
  3025. return ContentAlignment.MiddleLeft;
  3026. }
  3027. case ContentAlignment.BottomLeft: {
  3028. return ContentAlignment.BottomRight;
  3029. }
  3030. case ContentAlignment.BottomRight: {
  3031. return ContentAlignment.BottomLeft;
  3032. }
  3033. default: {
  3034. // if it's center it doesn't change
  3035. return align;
  3036. }
  3037. }
  3038. }
  3039. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3040. protected HorizontalAlignment RtlTranslateAlignment(HorizontalAlignment align) {
  3041. if ((right_to_left == RightToLeft.No) || (align == HorizontalAlignment.Center)) {
  3042. return align;
  3043. }
  3044. if (align == HorizontalAlignment.Left) {
  3045. return HorizontalAlignment.Right;
  3046. }
  3047. // align must be HorizontalAlignment.Right
  3048. return HorizontalAlignment.Left;
  3049. }
  3050. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3051. protected LeftRightAlignment RtlTranslateAlignment(LeftRightAlignment align) {
  3052. if (right_to_left == RightToLeft.No) {
  3053. return align;
  3054. }
  3055. if (align == LeftRightAlignment.Left) {
  3056. return LeftRightAlignment.Right;
  3057. }
  3058. // align must be LeftRightAlignment.Right;
  3059. return LeftRightAlignment.Left;
  3060. }
  3061. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3062. protected ContentAlignment RtlTranslateContent(ContentAlignment align) {
  3063. return RtlTranslateAlignment(align);
  3064. }
  3065. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3066. protected HorizontalAlignment RtlTranslateHorizontal(HorizontalAlignment align) {
  3067. return RtlTranslateAlignment(align);
  3068. }
  3069. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3070. protected LeftRightAlignment RtlTranslateLeftRight(LeftRightAlignment align) {
  3071. return RtlTranslateAlignment(align);
  3072. }
  3073. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3074. protected virtual void ScaleCore(float dx, float dy) {
  3075. Point location;
  3076. Size size;
  3077. SuspendLayout();
  3078. location = new Point((int)(Left * dx), (int)(Top * dy));
  3079. size = this.ClientSize;
  3080. if (!GetStyle(ControlStyles.FixedWidth)) {
  3081. size.Width = (int)(size.Width * dx);
  3082. }
  3083. if (!GetStyle(ControlStyles.FixedHeight)) {
  3084. size.Height = (int)(size.Height * dy);
  3085. }
  3086. SetBounds(location.X, location.Y, size.Width, size.Height, BoundsSpecified.All);
  3087. /* Now scale our children */
  3088. Control [] controls = child_controls.GetAllControls ();
  3089. for (int i=0; i < controls.Length; i++) {
  3090. controls[i].Scale(dx, dy);
  3091. }
  3092. ResumeLayout();
  3093. }
  3094. protected virtual void Select(bool directed, bool forward) {
  3095. IContainerControl container;
  3096. container = GetContainerControl();
  3097. if (container != null)
  3098. container.ActiveControl = this;
  3099. }
  3100. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3101. protected virtual void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  3102. // SetBoundsCore updates the Win32 control itself. UpdateBounds updates the controls variables and fires events, I'm guessing - pdb
  3103. if (IsHandleCreated) {
  3104. XplatUI.SetWindowPos(Handle, x, y, width, height);
  3105. }
  3106. UpdateBounds(x, y, width, height);
  3107. UpdateDistances();
  3108. }
  3109. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3110. protected virtual void SetClientSizeCore(int x, int y) {
  3111. // Calculate the actual window size from the client size (it usually stays the same or grows)
  3112. Rectangle ClientRect;
  3113. Rectangle WindowRect;
  3114. CreateParams cp;
  3115. ClientRect = new Rectangle(0, 0, x, y);
  3116. cp = this.CreateParams;
  3117. if (XplatUI.CalculateWindowRect(ref ClientRect, cp.Style, cp.ExStyle, null, out WindowRect)==false) {
  3118. return;
  3119. }
  3120. SetBounds(bounds.X, bounds.Y, WindowRect.Width, WindowRect.Height, BoundsSpecified.Size);
  3121. }
  3122. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3123. protected void SetStyle(ControlStyles flag, bool value) {
  3124. if (value) {
  3125. control_style |= flag;
  3126. } else {
  3127. control_style &= ~flag;
  3128. }
  3129. }
  3130. protected void SetTopLevel(bool value) {
  3131. if ((GetTopLevel() != value) && (parent != null)) {
  3132. throw new Exception();
  3133. }
  3134. if (this is Form) {
  3135. if (value == true) {
  3136. if (!Visible) {
  3137. Visible = true;
  3138. }
  3139. } else {
  3140. if (Visible) {
  3141. Visible = false;
  3142. }
  3143. }
  3144. }
  3145. is_toplevel = value;
  3146. }
  3147. protected virtual void SetVisibleCore(bool value) {
  3148. if (value!=is_visible) {
  3149. if (value && (window.Handle == IntPtr.Zero) || !is_created) {
  3150. CreateControl();
  3151. }
  3152. is_visible=value;
  3153. if (IsHandleCreated) {
  3154. XplatUI.SetVisible(Handle, value, true);
  3155. // Explicitly move Toplevel windows to where we want them;
  3156. // apparently moving unmapped toplevel windows doesn't work
  3157. if (is_visible && (this is Form)) {
  3158. XplatUI.SetWindowPos(window.Handle, bounds.X, bounds.Y, bounds.Width, bounds.Height);
  3159. }
  3160. }
  3161. OnVisibleChanged(EventArgs.Empty);
  3162. if (value == false && parent != null && Focused) {
  3163. Control container;
  3164. // Need to start at parent, GetContainerControl might return ourselves if we're a container
  3165. container = (Control)parent.GetContainerControl();
  3166. if (container != null) {
  3167. container.SelectNextControl(this, true, true, true, true);
  3168. }
  3169. }
  3170. if (parent != null) {
  3171. parent.PerformLayout(this, "visible");
  3172. } else {
  3173. PerformLayout(this, "visible");
  3174. }
  3175. }
  3176. }
  3177. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3178. protected void UpdateBounds() {
  3179. int x;
  3180. int y;
  3181. int width;
  3182. int height;
  3183. int client_width;
  3184. int client_height;
  3185. if (!IsHandleCreated) {
  3186. CreateHandle();
  3187. }
  3188. XplatUI.GetWindowPos(this.Handle, this is Form, out x, out y, out width, out height, out client_width, out client_height);
  3189. UpdateBounds(x, y, width, height, client_width, client_height);
  3190. }
  3191. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3192. protected void UpdateBounds(int x, int y, int width, int height) {
  3193. CreateParams cp;
  3194. Rectangle rect;
  3195. // Calculate client rectangle
  3196. rect = new Rectangle(0, 0, 0, 0);
  3197. cp = CreateParams;
  3198. XplatUI.CalculateWindowRect(ref rect, cp.Style, cp.ExStyle, cp.menu, out rect);
  3199. UpdateBounds(x, y, width, height, width - (rect.Right - rect.Left), height - (rect.Bottom - rect.Top));
  3200. }
  3201. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3202. protected void UpdateBounds(int x, int y, int width, int height, int clientWidth, int clientHeight) {
  3203. // UpdateBounds only seems to set our sizes and fire events but not update the GUI window to match
  3204. bool moved = false;
  3205. bool resized = false;
  3206. // Needed to generate required notifications
  3207. if ((this.bounds.X!=x) || (this.bounds.Y!=y)) {
  3208. moved=true;
  3209. }
  3210. if ((this.Bounds.Width!=width) || (this.Bounds.Height!=height)) {
  3211. resized=true;
  3212. }
  3213. bounds.X=x;
  3214. bounds.Y=y;
  3215. bounds.Width=width;
  3216. bounds.Height=height;
  3217. client_size.Width=clientWidth;
  3218. client_size.Height=clientHeight;
  3219. if (moved) {
  3220. OnLocationChanged(EventArgs.Empty);
  3221. }
  3222. if (resized) {
  3223. OnSizeChanged(EventArgs.Empty);
  3224. }
  3225. }
  3226. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3227. protected void UpdateStyles() {
  3228. if (!IsHandleCreated) {
  3229. return;
  3230. }
  3231. XplatUI.SetWindowStyle(window.Handle, CreateParams);
  3232. OnStyleChanged(EventArgs.Empty);
  3233. }
  3234. private void UpdateZOrderOfChild(Control child) {
  3235. if (IsHandleCreated && child.IsHandleCreated && (child.parent == this)) {
  3236. int index;
  3237. index = child_controls.IndexOf(child);
  3238. if (index > 0) {
  3239. XplatUI.SetZOrder(child.Handle, child_controls[index - 1].Handle, false, false);
  3240. } else {
  3241. XplatUI.SetZOrder(child.Handle, IntPtr.Zero, true, false);
  3242. }
  3243. }
  3244. }
  3245. private void UpdateChildrenZOrder() {
  3246. Control [] controls;
  3247. if (!IsHandleCreated) {
  3248. return;
  3249. }
  3250. controls = child_controls.GetAllControls ();
  3251. for (int i = 1; i < controls.Length; i++ ) {
  3252. XplatUI.SetZOrder(controls[i].Handle, controls[i-1].Handle, false, false);
  3253. }
  3254. }
  3255. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3256. protected void UpdateZOrder() {
  3257. if (parent != null) {
  3258. parent.UpdateZOrderOfChild(this);
  3259. }
  3260. }
  3261. protected virtual void WndProc(ref Message m) {
  3262. #if debug
  3263. Console.WriteLine("Control {0} received message {1}", window.Handle == IntPtr.Zero ? this.Text : XplatUI.Window(window.Handle), (Msg)m.Msg);
  3264. #endif
  3265. if ((this.control_style & ControlStyles.EnableNotifyMessage) != 0) {
  3266. OnNotifyMessage(m);
  3267. }
  3268. switch((Msg)m.Msg) {
  3269. case Msg.WM_DESTROY: {
  3270. OnHandleDestroyed(EventArgs.Empty);
  3271. #if DebugRecreate
  3272. IntPtr handle = window.Handle;
  3273. #endif
  3274. window.InvalidateHandle();
  3275. if (is_recreating) {
  3276. #if DebugRecreate
  3277. Console.WriteLine ("Creating handle for {0:X}", handle.ToInt32());
  3278. #endif
  3279. CreateHandle();
  3280. #if DebugRecreate
  3281. Console.WriteLine (" + new handle = {0:X}", Handle.ToInt32());
  3282. #endif
  3283. is_recreating = false;
  3284. }
  3285. return;
  3286. }
  3287. case Msg.WM_WINDOWPOSCHANGED: {
  3288. if (Visible) {
  3289. UpdateBounds();
  3290. if (GetStyle(ControlStyles.ResizeRedraw)) {
  3291. Invalidate();
  3292. }
  3293. }
  3294. return;
  3295. }
  3296. // Nice description of what should happen when handling WM_PAINT
  3297. // can be found here: http://pluralsight.com/wiki/default.aspx/Craig/FlickerFreeControlDrawing.html
  3298. // and here http://msdn.microsoft.com/msdnmag/issues/06/03/WindowsFormsPerformance/
  3299. case Msg.WM_PAINT: {
  3300. PaintEventArgs paint_event;
  3301. paint_event = XplatUI.PaintEventStart(Handle, true);
  3302. if (paint_event == null) {
  3303. return;
  3304. }
  3305. if (invalid_region != null && !invalid_region.IsVisible (paint_event.ClipRectangle)) {
  3306. // Just blit the previous image
  3307. paint_event.Graphics.DrawImage (ImageBuffer, paint_event.ClipRectangle, paint_event.ClipRectangle, GraphicsUnit.Pixel);
  3308. XplatUI.PaintEventEnd(Handle, true);
  3309. return;
  3310. }
  3311. Graphics dc = null;
  3312. Graphics back_dc = null;
  3313. Bitmap backbuffer = null;
  3314. if (ThemeEngine.Current.DoubleBufferingSupported) {
  3315. if ((control_style & ControlStyles.DoubleBuffer) != 0) {
  3316. backbuffer = ImageBuffer;
  3317. back_dc = Graphics.FromImage (backbuffer);
  3318. dc = paint_event.SetGraphics (back_dc);
  3319. }
  3320. }
  3321. if (!GetStyle(ControlStyles.Opaque)) {
  3322. OnPaintBackground(paint_event);
  3323. }
  3324. // Button-derived controls choose to ignore their Opaque style, give them a chance to draw their background anyways
  3325. OnPaintBackgroundInternal(paint_event);
  3326. OnPaintInternal(paint_event);
  3327. if (!paint_event.Handled) {
  3328. OnPaint(paint_event);
  3329. }
  3330. if (ThemeEngine.Current.DoubleBufferingSupported)
  3331. if ((control_style & ControlStyles.DoubleBuffer) != 0) {
  3332. dc.DrawImage (ImageBuffer, paint_event.ClipRectangle, paint_event.ClipRectangle, GraphicsUnit.Pixel);
  3333. paint_event.SetGraphics (dc);
  3334. invalid_region.Exclude (paint_event.ClipRectangle);
  3335. back_dc.Dispose ();
  3336. if (backbuffer != bmp_mem)
  3337. backbuffer.Dispose();
  3338. }
  3339. XplatUI.PaintEventEnd(Handle, true);
  3340. return;
  3341. }
  3342. case Msg.WM_ERASEBKGND: {
  3343. // The DefWndProc will never have to handle this, we always paint the background in managed code
  3344. // In theory this code would look at ControlStyles.AllPaintingInWmPaint and and call OnPaintBackground
  3345. // here but it just makes things more complicated...
  3346. m.Result = (IntPtr)1;
  3347. return;
  3348. }
  3349. case Msg.WM_LBUTTONUP: {
  3350. MouseEventArgs me;
  3351. me = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Left,
  3352. mouse_clicks,
  3353. LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3354. 0);
  3355. HandleClick(mouse_clicks, me);
  3356. OnMouseUp (me);
  3357. if (InternalCapture) {
  3358. InternalCapture = false;
  3359. }
  3360. if (mouse_clicks > 1) {
  3361. mouse_clicks = 1;
  3362. }
  3363. return;
  3364. }
  3365. case Msg.WM_LBUTTONDOWN: {
  3366. if (CanSelect) {
  3367. Select (true, true);
  3368. }
  3369. InternalCapture = true;
  3370. OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  3371. mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3372. 0));
  3373. return;
  3374. }
  3375. case Msg.WM_LBUTTONDBLCLK: {
  3376. InternalCapture = true;
  3377. mouse_clicks++;
  3378. OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  3379. mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3380. 0));
  3381. return;
  3382. }
  3383. case Msg.WM_MBUTTONUP: {
  3384. MouseEventArgs me;
  3385. me = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Middle,
  3386. mouse_clicks,
  3387. LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3388. 0);
  3389. HandleClick(mouse_clicks, me);
  3390. OnMouseUp (me);
  3391. if (InternalCapture) {
  3392. InternalCapture = false;
  3393. }
  3394. if (mouse_clicks > 1) {
  3395. mouse_clicks = 1;
  3396. }
  3397. return;
  3398. }
  3399. case Msg.WM_MBUTTONDOWN: {
  3400. InternalCapture = true;
  3401. OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  3402. mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3403. 0));
  3404. return;
  3405. }
  3406. case Msg.WM_MBUTTONDBLCLK: {
  3407. InternalCapture = true;
  3408. mouse_clicks++;
  3409. OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  3410. mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3411. 0));
  3412. return;
  3413. }
  3414. case Msg.WM_RBUTTONUP: {
  3415. MouseEventArgs me;
  3416. Point pt;
  3417. pt = new Point(LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()));
  3418. pt = PointToScreen(pt);
  3419. XplatUI.SendMessage(m.HWnd, Msg.WM_CONTEXTMENU, m.HWnd, (IntPtr)(pt.X + (pt.Y << 16)));
  3420. me = new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()) | MouseButtons.Right,
  3421. mouse_clicks,
  3422. LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3423. 0);
  3424. HandleClick(mouse_clicks, me);
  3425. OnMouseUp (me);
  3426. if (InternalCapture) {
  3427. InternalCapture = false;
  3428. }
  3429. if (mouse_clicks > 1) {
  3430. mouse_clicks = 1;
  3431. }
  3432. return;
  3433. }
  3434. case Msg.WM_RBUTTONDOWN: {
  3435. InternalCapture = true;
  3436. OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  3437. mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3438. 0));
  3439. return;
  3440. }
  3441. case Msg.WM_RBUTTONDBLCLK: {
  3442. InternalCapture = true;
  3443. mouse_clicks++;
  3444. OnMouseDown (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  3445. mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3446. 0));
  3447. return;
  3448. }
  3449. case Msg.WM_CONTEXTMENU: {
  3450. if (context_menu != null) {
  3451. Point pt;
  3452. pt = new Point(LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()));
  3453. context_menu.Show(this, PointToClient(pt));
  3454. return;
  3455. }
  3456. DefWndProc(ref m);
  3457. return;
  3458. }
  3459. case Msg.WM_MOUSEWHEEL: {
  3460. DefWndProc(ref m);
  3461. OnMouseWheel (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  3462. mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3463. HighOrder(m.WParam.ToInt32())));
  3464. return;
  3465. }
  3466. case Msg.WM_MOUSEMOVE: {
  3467. OnMouseMove (new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()),
  3468. mouse_clicks,
  3469. LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()),
  3470. 0));
  3471. return;
  3472. }
  3473. case Msg.WM_MOUSE_ENTER: {
  3474. if (is_entered) {
  3475. return;
  3476. }
  3477. is_entered = true;
  3478. OnMouseEnter(EventArgs.Empty);
  3479. return;
  3480. }
  3481. case Msg.WM_MOUSE_LEAVE: {
  3482. is_entered=false;
  3483. OnMouseLeave(EventArgs.Empty);
  3484. return;
  3485. }
  3486. case Msg.WM_MOUSEHOVER: {
  3487. OnMouseHover(EventArgs.Empty);
  3488. return;
  3489. }
  3490. case Msg.WM_SYSKEYUP: {
  3491. if (ProcessKeyMessage(ref m)) {
  3492. m.Result = IntPtr.Zero;
  3493. return;
  3494. }
  3495. if ((m.WParam.ToInt32() & (int)Keys.KeyCode) == (int)Keys.Menu) {
  3496. Form form;
  3497. form = FindForm();
  3498. if (form != null && form.ActiveMenu != null) {
  3499. form.ActiveMenu.ProcessCmdKey(ref m, (Keys)m.WParam.ToInt32());
  3500. }
  3501. }
  3502. DefWndProc (ref m);
  3503. return;
  3504. }
  3505. case Msg.WM_SYSKEYDOWN:
  3506. case Msg.WM_KEYDOWN:
  3507. case Msg.WM_KEYUP:
  3508. case Msg.WM_SYSCHAR:
  3509. case Msg.WM_CHAR: {
  3510. if (ProcessKeyMessage(ref m)) {
  3511. m.Result = IntPtr.Zero;
  3512. return;
  3513. }
  3514. DefWndProc (ref m);
  3515. return;
  3516. }
  3517. case Msg.WM_HELP: {
  3518. Point mouse_pos;
  3519. if (m.LParam != IntPtr.Zero) {
  3520. HELPINFO hi;
  3521. hi = new HELPINFO();
  3522. hi = (HELPINFO) Marshal.PtrToStructure (m.LParam, typeof (HELPINFO));
  3523. mouse_pos = new Point(hi.MousePos.x, hi.MousePos.y);
  3524. } else {
  3525. mouse_pos = Control.MousePosition;
  3526. }
  3527. OnHelpRequested(new HelpEventArgs(mouse_pos));
  3528. m.Result = (IntPtr)1;
  3529. return;
  3530. }
  3531. case Msg.WM_KILLFOCUS: {
  3532. this.has_focus = false;
  3533. OnLostFocusInternal (EventArgs.Empty);
  3534. return;
  3535. }
  3536. case Msg.WM_SETFOCUS: {
  3537. if (!has_focus) {
  3538. this.has_focus = true;
  3539. OnGotFocusInternal (EventArgs.Empty);
  3540. }
  3541. return;
  3542. }
  3543. case Msg.WM_SYSCOLORCHANGE: {
  3544. ThemeEngine.Current.ResetDefaults();
  3545. OnSystemColorsChanged(EventArgs.Empty);
  3546. return;
  3547. }
  3548. case Msg.WM_SETCURSOR: {
  3549. if ((cursor == null) || ((HitTest)(m.LParam.ToInt32() & 0xffff) != HitTest.HTCLIENT)) {
  3550. DefWndProc(ref m);
  3551. return;
  3552. }
  3553. XplatUI.SetCursor(window.Handle, cursor.handle);
  3554. m.Result = (IntPtr)1;
  3555. return;
  3556. }
  3557. default: {
  3558. DefWndProc(ref m);
  3559. return;
  3560. }
  3561. }
  3562. }
  3563. #endregion // Public Instance Methods
  3564. #region OnXXX methods
  3565. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3566. protected virtual void OnBackColorChanged(EventArgs e) {
  3567. if (BackColorChanged!=null) BackColorChanged(this, e);
  3568. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackColorChanged(e);
  3569. }
  3570. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3571. protected virtual void OnBackgroundImageChanged(EventArgs e) {
  3572. if (BackgroundImageChanged!=null) BackgroundImageChanged(this, e);
  3573. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBackgroundImageChanged(e);
  3574. }
  3575. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3576. protected virtual void OnBindingContextChanged(EventArgs e) {
  3577. CheckDataBindings ();
  3578. if (BindingContextChanged!=null) {
  3579. BindingContextChanged(this, e);
  3580. }
  3581. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentBindingContextChanged(e);
  3582. }
  3583. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3584. protected virtual void OnCausesValidationChanged(EventArgs e) {
  3585. if (CausesValidationChanged!=null) CausesValidationChanged(this, e);
  3586. }
  3587. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3588. protected virtual void OnChangeUICues(UICuesEventArgs e) {
  3589. if (ChangeUICues!=null) ChangeUICues(this, e);
  3590. }
  3591. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3592. protected virtual void OnClick(EventArgs e) {
  3593. if (Click!=null) Click(this, e);
  3594. }
  3595. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3596. protected virtual void OnContextMenuChanged(EventArgs e) {
  3597. if (ContextMenuChanged!=null) ContextMenuChanged(this, e);
  3598. }
  3599. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3600. protected virtual void OnControlAdded(ControlEventArgs e) {
  3601. if (ControlAdded!=null) ControlAdded(this, e);
  3602. }
  3603. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3604. protected virtual void OnControlRemoved(ControlEventArgs e) {
  3605. if (ControlRemoved!=null) ControlRemoved(this, e);
  3606. }
  3607. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3608. protected virtual void OnCreateControl() {
  3609. // Override me!
  3610. }
  3611. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3612. protected virtual void OnCursorChanged(EventArgs e) {
  3613. if (CursorChanged!=null) CursorChanged(this, e);
  3614. }
  3615. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3616. protected virtual void OnDockChanged(EventArgs e) {
  3617. if (DockChanged!=null) DockChanged(this, e);
  3618. }
  3619. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3620. protected virtual void OnDoubleClick(EventArgs e) {
  3621. if (DoubleClick!=null) DoubleClick(this, e);
  3622. }
  3623. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3624. protected virtual void OnDragDrop(DragEventArgs drgevent) {
  3625. if (DragDrop!=null) DragDrop(this, drgevent);
  3626. }
  3627. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3628. protected virtual void OnDragEnter(DragEventArgs drgevent) {
  3629. if (DragEnter!=null) DragEnter(this, drgevent);
  3630. }
  3631. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3632. protected virtual void OnDragLeave(EventArgs e) {
  3633. if (DragLeave!=null) DragLeave(this, e);
  3634. }
  3635. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3636. protected virtual void OnDragOver(DragEventArgs drgevent) {
  3637. if (DragOver!=null) DragOver(this, drgevent);
  3638. }
  3639. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3640. protected virtual void OnEnabledChanged(EventArgs e) {
  3641. if (IsHandleCreated) {
  3642. if (this is Form) {
  3643. if (((Form)this).context == null) {
  3644. XplatUI.EnableWindow(window.Handle, Enabled);
  3645. }
  3646. } else {
  3647. XplatUI.EnableWindow(window.Handle, Enabled);
  3648. }
  3649. Refresh();
  3650. }
  3651. if (EnabledChanged != null) {
  3652. EnabledChanged(this, e);
  3653. }
  3654. for (int i=0; i<child_controls.Count; i++) {
  3655. child_controls[i].OnParentEnabledChanged(e);
  3656. }
  3657. }
  3658. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3659. protected virtual void OnEnter(EventArgs e) {
  3660. if (Enter!=null) Enter(this, e);
  3661. }
  3662. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3663. protected virtual void OnFontChanged(EventArgs e) {
  3664. if (FontChanged!=null) FontChanged(this, e);
  3665. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentFontChanged(e);
  3666. }
  3667. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3668. protected virtual void OnForeColorChanged(EventArgs e) {
  3669. if (ForeColorChanged!=null) ForeColorChanged(this, e);
  3670. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentForeColorChanged(e);
  3671. }
  3672. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3673. protected virtual void OnGiveFeedback(GiveFeedbackEventArgs gfbevent) {
  3674. if (GiveFeedback!=null) GiveFeedback(this, gfbevent);
  3675. }
  3676. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3677. protected virtual void OnGotFocus(EventArgs e) {
  3678. if (GotFocus!=null) GotFocus(this, e);
  3679. }
  3680. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3681. protected virtual void OnHandleCreated(EventArgs e) {
  3682. if (HandleCreated!=null) HandleCreated(this, e);
  3683. }
  3684. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3685. protected virtual void OnHandleDestroyed(EventArgs e) {
  3686. if (HandleDestroyed!=null) HandleDestroyed(this, e);
  3687. }
  3688. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3689. protected virtual void OnHelpRequested(HelpEventArgs hevent) {
  3690. if (HelpRequested!=null) HelpRequested(this, hevent);
  3691. }
  3692. protected virtual void OnImeModeChanged(EventArgs e) {
  3693. if (ImeModeChanged!=null) ImeModeChanged(this, e);
  3694. }
  3695. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3696. protected virtual void OnInvalidated(InvalidateEventArgs e) {
  3697. if (ThemeEngine.Current.DoubleBufferingSupported)
  3698. if ((control_style & ControlStyles.DoubleBuffer) != 0) {
  3699. // should this block be here? seems like it
  3700. // would be more at home in
  3701. // NotifyInvalidated..
  3702. if (e.InvalidRect == ClientRectangle) {
  3703. ImageBufferNeedsRedraw ();
  3704. }
  3705. else {
  3706. // we need this Inflate call here so
  3707. // that the border of the rectangle is
  3708. // considered Visible (the
  3709. // invalid_region.IsVisible call) in
  3710. // the WM_PAINT handling below.
  3711. Rectangle r = Rectangle.Inflate(e.InvalidRect, 1,1);
  3712. if (invalid_region == null)
  3713. invalid_region = new Region (r);
  3714. else
  3715. invalid_region.Union (r);
  3716. }
  3717. }
  3718. if (Invalidated!=null) Invalidated(this, e);
  3719. }
  3720. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3721. protected virtual void OnKeyDown(KeyEventArgs e) {
  3722. if (KeyDown!=null) KeyDown(this, e);
  3723. }
  3724. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3725. protected virtual void OnKeyPress(KeyPressEventArgs e) {
  3726. if (KeyPress!=null) KeyPress(this, e);
  3727. }
  3728. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3729. protected virtual void OnKeyUp(KeyEventArgs e) {
  3730. if (KeyUp!=null) KeyUp(this, e);
  3731. }
  3732. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3733. protected virtual void OnLayout(LayoutEventArgs levent) {
  3734. if (Layout!=null) Layout(this, levent);
  3735. }
  3736. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3737. protected virtual void OnLeave(EventArgs e) {
  3738. if (Leave!=null) Leave(this, e);
  3739. }
  3740. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3741. protected virtual void OnLocationChanged(EventArgs e) {
  3742. OnMove(e);
  3743. if (LocationChanged!=null) LocationChanged(this, e);
  3744. }
  3745. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3746. protected virtual void OnLostFocus(EventArgs e) {
  3747. if (LostFocus!=null) LostFocus(this, e);
  3748. }
  3749. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3750. protected virtual void OnMouseDown(MouseEventArgs e) {
  3751. if (MouseDown!=null) MouseDown(this, e);
  3752. }
  3753. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3754. protected virtual void OnMouseEnter(EventArgs e) {
  3755. if (MouseEnter!=null) MouseEnter(this, e);
  3756. }
  3757. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3758. protected virtual void OnMouseHover(EventArgs e) {
  3759. if (MouseHover!=null) MouseHover(this, e);
  3760. }
  3761. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3762. protected virtual void OnMouseLeave(EventArgs e) {
  3763. if (MouseLeave!=null) MouseLeave(this, e);
  3764. }
  3765. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3766. protected virtual void OnMouseMove(MouseEventArgs e) {
  3767. if (MouseMove!=null) MouseMove(this, e);
  3768. }
  3769. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3770. protected virtual void OnMouseUp(MouseEventArgs e) {
  3771. if (MouseUp!=null) MouseUp(this, e);
  3772. }
  3773. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3774. protected virtual void OnMouseWheel(MouseEventArgs e) {
  3775. if (MouseWheel!=null) MouseWheel(this, e);
  3776. }
  3777. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3778. protected virtual void OnMove(EventArgs e) {
  3779. if (Move!=null) Move(this, e);
  3780. }
  3781. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3782. protected virtual void OnNotifyMessage(Message m) {
  3783. // Override me!
  3784. }
  3785. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3786. protected virtual void OnPaint(PaintEventArgs e) {
  3787. if (Paint!=null) Paint(this, e);
  3788. }
  3789. internal virtual void OnPaintBackgroundInternal(PaintEventArgs e) {
  3790. // Override me
  3791. }
  3792. internal virtual void OnPaintInternal(PaintEventArgs e) {
  3793. // Override me
  3794. }
  3795. internal virtual void OnGotFocusInternal (EventArgs e)
  3796. {
  3797. OnGotFocus (e);
  3798. }
  3799. internal virtual void OnLostFocusInternal (EventArgs e)
  3800. {
  3801. OnLostFocus (e);
  3802. }
  3803. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3804. protected virtual void OnPaintBackground(PaintEventArgs pevent) {
  3805. PaintControlBackground (pevent);
  3806. }
  3807. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3808. protected virtual void OnParentBackColorChanged(EventArgs e) {
  3809. if (background_color.IsEmpty && background_image==null) {
  3810. Invalidate();
  3811. OnBackColorChanged(e);
  3812. }
  3813. }
  3814. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3815. protected virtual void OnParentBackgroundImageChanged(EventArgs e) {
  3816. if (background_color.IsEmpty && background_image==null) {
  3817. Invalidate();
  3818. OnBackgroundImageChanged(e);
  3819. }
  3820. }
  3821. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3822. protected virtual void OnParentBindingContextChanged(EventArgs e) {
  3823. if (binding_context==null) {
  3824. binding_context=Parent.binding_context;
  3825. OnBindingContextChanged(e);
  3826. }
  3827. }
  3828. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3829. protected virtual void OnParentChanged(EventArgs e) {
  3830. if (ParentChanged!=null) ParentChanged(this, e);
  3831. }
  3832. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3833. protected virtual void OnParentEnabledChanged(EventArgs e) {
  3834. if (is_enabled) {
  3835. OnEnabledChanged(e);
  3836. }
  3837. }
  3838. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3839. protected virtual void OnParentFontChanged(EventArgs e) {
  3840. if (font==null) {
  3841. Invalidate();
  3842. OnFontChanged(e);
  3843. }
  3844. }
  3845. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3846. protected virtual void OnParentForeColorChanged(EventArgs e) {
  3847. if (foreground_color.IsEmpty) {
  3848. Invalidate();
  3849. OnForeColorChanged(e);
  3850. }
  3851. }
  3852. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3853. protected virtual void OnParentRightToLeftChanged(EventArgs e) {
  3854. if (right_to_left==RightToLeft.Inherit) {
  3855. Invalidate();
  3856. OnRightToLeftChanged(e);
  3857. }
  3858. }
  3859. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3860. protected virtual void OnParentVisibleChanged(EventArgs e) {
  3861. if (is_visible) {
  3862. OnVisibleChanged(e);
  3863. }
  3864. }
  3865. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3866. protected virtual void OnQueryContinueDrag(QueryContinueDragEventArgs e) {
  3867. if (QueryContinueDrag!=null) QueryContinueDrag(this, e);
  3868. }
  3869. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3870. protected virtual void OnResize(EventArgs e) {
  3871. if (Resize!=null) Resize(this, e);
  3872. PerformLayout(this, "bounds");
  3873. if (parent != null) {
  3874. parent.PerformLayout();
  3875. }
  3876. }
  3877. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3878. protected virtual void OnRightToLeftChanged(EventArgs e) {
  3879. if (RightToLeftChanged!=null) RightToLeftChanged(this, e);
  3880. for (int i=0; i<child_controls.Count; i++) child_controls[i].OnParentRightToLeftChanged(e);
  3881. }
  3882. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3883. protected virtual void OnSizeChanged(EventArgs e) {
  3884. InvalidateBuffers ();
  3885. OnResize(e);
  3886. if (SizeChanged!=null) SizeChanged(this, e);
  3887. }
  3888. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3889. protected virtual void OnStyleChanged(EventArgs e) {
  3890. if (StyleChanged!=null) StyleChanged(this, e);
  3891. }
  3892. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3893. protected virtual void OnSystemColorsChanged(EventArgs e) {
  3894. if (SystemColorsChanged!=null) SystemColorsChanged(this, e);
  3895. }
  3896. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3897. protected virtual void OnTabIndexChanged(EventArgs e) {
  3898. if (TabIndexChanged!=null) TabIndexChanged(this, e);
  3899. }
  3900. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3901. protected virtual void OnTabStopChanged(EventArgs e) {
  3902. if (TabStopChanged!=null) TabStopChanged(this, e);
  3903. }
  3904. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3905. protected virtual void OnTextChanged(EventArgs e) {
  3906. if (TextChanged!=null) TextChanged(this, e);
  3907. }
  3908. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3909. protected virtual void OnValidated(EventArgs e) {
  3910. if (Validated!=null) Validated(this, e);
  3911. }
  3912. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3913. protected virtual void OnValidating(System.ComponentModel.CancelEventArgs e) {
  3914. if (Validating!=null) Validating(this, e);
  3915. }
  3916. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3917. protected virtual void OnVisibleChanged(EventArgs e) {
  3918. if ((parent != null) && !Created && Visible) {
  3919. if (!is_disposed) {
  3920. CreateControl();
  3921. PerformLayout();
  3922. }
  3923. }
  3924. if (VisibleChanged!=null) VisibleChanged(this, e);
  3925. // We need to tell our kids
  3926. for (int i=0; i<child_controls.Count; i++) {
  3927. if (child_controls[i].Visible) {
  3928. child_controls[i].OnParentVisibleChanged(e);
  3929. }
  3930. }
  3931. }
  3932. #endregion // OnXXX methods
  3933. #region Events
  3934. public event EventHandler BackColorChanged;
  3935. public event EventHandler BackgroundImageChanged;
  3936. public event EventHandler BindingContextChanged;
  3937. public event EventHandler CausesValidationChanged;
  3938. public event UICuesEventHandler ChangeUICues;
  3939. public event EventHandler Click;
  3940. public event EventHandler ContextMenuChanged;
  3941. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3942. [Browsable(false)]
  3943. public event ControlEventHandler ControlAdded;
  3944. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3945. [Browsable(false)]
  3946. public event ControlEventHandler ControlRemoved;
  3947. [MWFDescription("Fired when the cursor for the control has been changed"), MWFCategory("PropertyChanged")]
  3948. public event EventHandler CursorChanged;
  3949. public event EventHandler DockChanged;
  3950. public event EventHandler DoubleClick;
  3951. public event DragEventHandler DragDrop;
  3952. public event DragEventHandler DragEnter;
  3953. public event EventHandler DragLeave;
  3954. public event DragEventHandler DragOver;
  3955. public event EventHandler EnabledChanged;
  3956. public event EventHandler Enter;
  3957. public event EventHandler FontChanged;
  3958. public event EventHandler ForeColorChanged;
  3959. public event GiveFeedbackEventHandler GiveFeedback;
  3960. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3961. [Browsable(false)]
  3962. public event EventHandler GotFocus;
  3963. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3964. [Browsable(false)]
  3965. public event EventHandler HandleCreated;
  3966. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3967. [Browsable(false)]
  3968. public event EventHandler HandleDestroyed;
  3969. public event HelpEventHandler HelpRequested;
  3970. public event EventHandler ImeModeChanged;
  3971. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3972. [Browsable(false)]
  3973. public event InvalidateEventHandler Invalidated;
  3974. public event KeyEventHandler KeyDown;
  3975. public event KeyPressEventHandler KeyPress;
  3976. public event KeyEventHandler KeyUp;
  3977. public event LayoutEventHandler Layout;
  3978. public event EventHandler Leave;
  3979. public event EventHandler LocationChanged;
  3980. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3981. [Browsable(false)]
  3982. public event EventHandler LostFocus;
  3983. public event MouseEventHandler MouseDown;
  3984. public event EventHandler MouseEnter;
  3985. public event EventHandler MouseHover;
  3986. public event EventHandler MouseLeave;
  3987. public event MouseEventHandler MouseMove;
  3988. public event MouseEventHandler MouseUp;
  3989. [EditorBrowsable(EditorBrowsableState.Advanced)]
  3990. [Browsable(false)]
  3991. public event MouseEventHandler MouseWheel;
  3992. public event EventHandler Move;
  3993. public event PaintEventHandler Paint;
  3994. public event EventHandler ParentChanged;
  3995. public event QueryAccessibilityHelpEventHandler QueryAccessibilityHelp;
  3996. public event QueryContinueDragEventHandler QueryContinueDrag;
  3997. public event EventHandler Resize;
  3998. public event EventHandler RightToLeftChanged;
  3999. public event EventHandler SizeChanged;
  4000. public event EventHandler StyleChanged;
  4001. public event EventHandler SystemColorsChanged;
  4002. public event EventHandler TabIndexChanged;
  4003. public event EventHandler TabStopChanged;
  4004. public event EventHandler TextChanged;
  4005. public event EventHandler Validated;
  4006. public event CancelEventHandler Validating;
  4007. public event EventHandler VisibleChanged;
  4008. #endregion // Events
  4009. }
  4010. }