nsTreeBodyFrame.cpp 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/AsyncEventDispatcher.h"
  6. #include "mozilla/ContentEvents.h"
  7. #include "mozilla/DebugOnly.h"
  8. #include "mozilla/EventDispatcher.h"
  9. #include "mozilla/gfx/2D.h"
  10. #include "mozilla/gfx/PathHelpers.h"
  11. #include "mozilla/Likely.h"
  12. #include "mozilla/MathAlgorithms.h"
  13. #include "mozilla/MouseEvents.h"
  14. #include "mozilla/TextEditRules.h"
  15. #include "gfxUtils.h"
  16. #include "nsAlgorithm.h"
  17. #include "nsCOMPtr.h"
  18. #include "nsFontMetrics.h"
  19. #include "nsPresContext.h"
  20. #include "nsNameSpaceManager.h"
  21. #include "nsTreeBodyFrame.h"
  22. #include "nsTreeSelection.h"
  23. #include "nsTreeImageListener.h"
  24. #include "nsGkAtoms.h"
  25. #include "nsCSSAnonBoxes.h"
  26. #include "nsIContent.h"
  27. #include "nsStyleContext.h"
  28. #include "nsIBoxObject.h"
  29. #include "nsIDOMCustomEvent.h"
  30. #include "nsIDOMMouseEvent.h"
  31. #include "nsIDOMElement.h"
  32. #include "nsIDOMNodeList.h"
  33. #include "nsIDOMDocument.h"
  34. #include "nsIDOMXULElement.h"
  35. #include "nsIDocument.h"
  36. #include "mozilla/css/StyleRule.h"
  37. #include "nsCSSRendering.h"
  38. #include "nsIXULTemplateBuilder.h"
  39. #include "nsXPIDLString.h"
  40. #include "nsContainerFrame.h"
  41. #include "nsView.h"
  42. #include "nsViewManager.h"
  43. #include "nsVariant.h"
  44. #include "nsWidgetsCID.h"
  45. #include "nsBoxFrame.h"
  46. #include "nsIURL.h"
  47. #include "nsBoxLayoutState.h"
  48. #include "nsTreeContentView.h"
  49. #include "nsTreeUtils.h"
  50. #include "nsThemeConstants.h"
  51. #include "nsITheme.h"
  52. #include "imgIRequest.h"
  53. #include "imgIContainer.h"
  54. #include "imgILoader.h"
  55. #include "mozilla/dom/NodeInfo.h"
  56. #include "nsContentUtils.h"
  57. #include "nsLayoutUtils.h"
  58. #include "nsIScrollableFrame.h"
  59. #include "nsDisplayList.h"
  60. #include "mozilla/dom/TreeBoxObject.h"
  61. #include "nsRenderingContext.h"
  62. #include "nsIScriptableRegion.h"
  63. #include <algorithm>
  64. #include "ScrollbarActivity.h"
  65. #ifdef ACCESSIBILITY
  66. #include "nsAccessibilityService.h"
  67. #include "nsIWritablePropertyBag2.h"
  68. #endif
  69. #include "nsBidiUtils.h"
  70. using namespace mozilla;
  71. using namespace mozilla::gfx;
  72. using namespace mozilla::image;
  73. using namespace mozilla::layout;
  74. // Function that cancels all the image requests in our cache.
  75. void
  76. nsTreeBodyFrame::CancelImageRequests()
  77. {
  78. for (auto iter = mImageCache.Iter(); !iter.Done(); iter.Next()) {
  79. // If our imgIRequest object was registered with the refresh driver
  80. // then we need to deregister it.
  81. nsTreeImageCacheEntry entry = iter.UserData();
  82. nsLayoutUtils::DeregisterImageRequest(PresContext(), entry.request,
  83. nullptr);
  84. entry.request->CancelAndForgetObserver(NS_BINDING_ABORTED);
  85. }
  86. }
  87. //
  88. // NS_NewTreeFrame
  89. //
  90. // Creates a new tree frame
  91. //
  92. nsIFrame*
  93. NS_NewTreeBodyFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
  94. {
  95. return new (aPresShell) nsTreeBodyFrame(aContext);
  96. }
  97. NS_IMPL_FRAMEARENA_HELPERS(nsTreeBodyFrame)
  98. NS_QUERYFRAME_HEAD(nsTreeBodyFrame)
  99. NS_QUERYFRAME_ENTRY(nsIScrollbarMediator)
  100. NS_QUERYFRAME_ENTRY(nsTreeBodyFrame)
  101. NS_QUERYFRAME_TAIL_INHERITING(nsLeafBoxFrame)
  102. // Constructor
  103. nsTreeBodyFrame::nsTreeBodyFrame(nsStyleContext* aContext)
  104. :nsLeafBoxFrame(aContext),
  105. mSlots(nullptr),
  106. mImageCache(),
  107. mTopRowIndex(0),
  108. mPageLength(0),
  109. mHorzPosition(0),
  110. mOriginalHorzWidth(-1),
  111. mHorzWidth(0),
  112. mAdjustWidth(0),
  113. mRowHeight(0),
  114. mIndentation(0),
  115. mStringWidth(-1),
  116. mUpdateBatchNest(0),
  117. mRowCount(0),
  118. mMouseOverRow(-1),
  119. mFocused(false),
  120. mHasFixedRowCount(false),
  121. mVerticalOverflow(false),
  122. mHorizontalOverflow(false),
  123. mReflowCallbackPosted(false),
  124. mCheckingOverflow(false)
  125. {
  126. mColumns = new nsTreeColumns(this);
  127. }
  128. // Destructor
  129. nsTreeBodyFrame::~nsTreeBodyFrame()
  130. {
  131. CancelImageRequests();
  132. DetachImageListeners();
  133. delete mSlots;
  134. }
  135. static void
  136. GetBorderPadding(nsStyleContext* aContext, nsMargin& aMargin)
  137. {
  138. aMargin.SizeTo(0, 0, 0, 0);
  139. aContext->StylePadding()->GetPadding(aMargin);
  140. aMargin += aContext->StyleBorder()->GetComputedBorder();
  141. }
  142. static void
  143. AdjustForBorderPadding(nsStyleContext* aContext, nsRect& aRect)
  144. {
  145. nsMargin borderPadding(0, 0, 0, 0);
  146. GetBorderPadding(aContext, borderPadding);
  147. aRect.Deflate(borderPadding);
  148. }
  149. void
  150. nsTreeBodyFrame::Init(nsIContent* aContent,
  151. nsContainerFrame* aParent,
  152. nsIFrame* aPrevInFlow)
  153. {
  154. nsLeafBoxFrame::Init(aContent, aParent, aPrevInFlow);
  155. mIndentation = GetIndentation();
  156. mRowHeight = GetRowHeight();
  157. EnsureBoxObject();
  158. if (LookAndFeel::GetInt(LookAndFeel::eIntID_UseOverlayScrollbars) != 0) {
  159. mScrollbarActivity = new ScrollbarActivity(
  160. static_cast<nsIScrollbarMediator*>(this));
  161. }
  162. }
  163. nsSize
  164. nsTreeBodyFrame::GetXULMinSize(nsBoxLayoutState& aBoxLayoutState)
  165. {
  166. EnsureView();
  167. nsIContent* baseElement = GetBaseElement();
  168. nsSize min(0,0);
  169. int32_t desiredRows;
  170. if (MOZ_UNLIKELY(!baseElement)) {
  171. desiredRows = 0;
  172. }
  173. else if (baseElement->IsHTMLElement(nsGkAtoms::select)) {
  174. min.width = CalcMaxRowWidth();
  175. nsAutoString size;
  176. baseElement->GetAttr(kNameSpaceID_None, nsGkAtoms::size, size);
  177. if (!size.IsEmpty()) {
  178. nsresult err;
  179. desiredRows = size.ToInteger(&err);
  180. mHasFixedRowCount = true;
  181. mPageLength = desiredRows;
  182. }
  183. else {
  184. desiredRows = 1;
  185. }
  186. }
  187. else {
  188. // tree
  189. nsAutoString rows;
  190. baseElement->GetAttr(kNameSpaceID_None, nsGkAtoms::rows, rows);
  191. if (!rows.IsEmpty()) {
  192. nsresult err;
  193. desiredRows = rows.ToInteger(&err);
  194. mPageLength = desiredRows;
  195. }
  196. else {
  197. desiredRows = 0;
  198. }
  199. }
  200. min.height = mRowHeight * desiredRows;
  201. AddBorderAndPadding(min);
  202. bool widthSet, heightSet;
  203. nsIFrame::AddXULMinSize(aBoxLayoutState, this, min, widthSet, heightSet);
  204. return min;
  205. }
  206. nscoord
  207. nsTreeBodyFrame::CalcMaxRowWidth()
  208. {
  209. if (mStringWidth != -1)
  210. return mStringWidth;
  211. if (!mView)
  212. return 0;
  213. nsStyleContext* rowContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreerow);
  214. nsMargin rowMargin(0,0,0,0);
  215. GetBorderPadding(rowContext, rowMargin);
  216. nscoord rowWidth;
  217. nsTreeColumn* col;
  218. nsRenderingContext rc(
  219. PresContext()->PresShell()->CreateReferenceRenderingContext());
  220. for (int32_t row = 0; row < mRowCount; ++row) {
  221. rowWidth = 0;
  222. for (col = mColumns->GetFirstColumn(); col; col = col->GetNext()) {
  223. nscoord desiredWidth, currentWidth;
  224. nsresult rv = GetCellWidth(row, col, &rc, desiredWidth, currentWidth);
  225. if (NS_FAILED(rv)) {
  226. NS_NOTREACHED("invalid column");
  227. continue;
  228. }
  229. rowWidth += desiredWidth;
  230. }
  231. if (rowWidth > mStringWidth)
  232. mStringWidth = rowWidth;
  233. }
  234. mStringWidth += rowMargin.left + rowMargin.right;
  235. return mStringWidth;
  236. }
  237. void
  238. nsTreeBodyFrame::DestroyFrom(nsIFrame* aDestructRoot)
  239. {
  240. if (mScrollbarActivity) {
  241. mScrollbarActivity->Destroy();
  242. mScrollbarActivity = nullptr;
  243. }
  244. mScrollEvent.Revoke();
  245. // Make sure we cancel any posted callbacks.
  246. if (mReflowCallbackPosted) {
  247. PresContext()->PresShell()->CancelReflowCallback(this);
  248. mReflowCallbackPosted = false;
  249. }
  250. if (mColumns)
  251. mColumns->SetTree(nullptr);
  252. // Save off our info into the box object.
  253. nsCOMPtr<nsPIBoxObject> box(do_QueryInterface(mTreeBoxObject));
  254. if (box) {
  255. if (mTopRowIndex > 0) {
  256. nsAutoString topRowStr; topRowStr.AssignLiteral("topRow");
  257. nsAutoString topRow;
  258. topRow.AppendInt(mTopRowIndex);
  259. box->SetProperty(topRowStr.get(), topRow.get());
  260. }
  261. // Always null out the cached tree body frame.
  262. box->ClearCachedValues();
  263. mTreeBoxObject = nullptr; // Drop our ref here.
  264. }
  265. if (mView) {
  266. nsCOMPtr<nsITreeSelection> sel;
  267. mView->GetSelection(getter_AddRefs(sel));
  268. if (sel)
  269. sel->SetTree(nullptr);
  270. mView->SetTree(nullptr);
  271. mView = nullptr;
  272. }
  273. nsLeafBoxFrame::DestroyFrom(aDestructRoot);
  274. }
  275. void
  276. nsTreeBodyFrame::EnsureBoxObject()
  277. {
  278. if (!mTreeBoxObject) {
  279. nsIContent* parent = GetBaseElement();
  280. if (parent) {
  281. nsIDocument* nsDoc = parent->GetComposedDoc();
  282. if (!nsDoc) // there may be no document, if we're called from Destroy()
  283. return;
  284. ErrorResult ignored;
  285. nsCOMPtr<nsIBoxObject> box =
  286. nsDoc->GetBoxObjectFor(parent->AsElement(), ignored);
  287. // Ensure that we got a native box object.
  288. nsCOMPtr<nsPIBoxObject> pBox = do_QueryInterface(box);
  289. if (pBox) {
  290. nsCOMPtr<nsITreeBoxObject> realTreeBoxObject = do_QueryInterface(pBox);
  291. if (realTreeBoxObject) {
  292. nsTreeBodyFrame* innerTreeBoxObject =
  293. static_cast<dom::TreeBoxObject*>(realTreeBoxObject.get())
  294. ->GetCachedTreeBodyFrame();
  295. ENSURE_TRUE(!innerTreeBoxObject || innerTreeBoxObject == this);
  296. mTreeBoxObject = realTreeBoxObject;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. void
  303. nsTreeBodyFrame::EnsureView()
  304. {
  305. if (!mView) {
  306. if (PresContext()->PresShell()->IsReflowLocked()) {
  307. if (!mReflowCallbackPosted) {
  308. mReflowCallbackPosted = true;
  309. PresContext()->PresShell()->PostReflowCallback(this);
  310. }
  311. return;
  312. }
  313. nsCOMPtr<nsIBoxObject> box = do_QueryInterface(mTreeBoxObject);
  314. if (box) {
  315. nsWeakFrame weakFrame(this);
  316. nsCOMPtr<nsITreeView> treeView;
  317. mTreeBoxObject->GetView(getter_AddRefs(treeView));
  318. if (treeView && weakFrame.IsAlive()) {
  319. nsXPIDLString rowStr;
  320. box->GetProperty(u"topRow", getter_Copies(rowStr));
  321. nsAutoString rowStr2(rowStr);
  322. nsresult error;
  323. int32_t rowIndex = rowStr2.ToInteger(&error);
  324. // Set our view.
  325. SetView(treeView);
  326. ENSURE_TRUE(weakFrame.IsAlive());
  327. // Scroll to the given row.
  328. // XXX is this optimal if we haven't laid out yet?
  329. ScrollToRow(rowIndex);
  330. ENSURE_TRUE(weakFrame.IsAlive());
  331. // Clear out the property info for the top row, but we always keep the
  332. // view current.
  333. box->RemoveProperty(u"topRow");
  334. }
  335. }
  336. }
  337. }
  338. void
  339. nsTreeBodyFrame::ManageReflowCallback(const nsRect& aRect, nscoord aHorzWidth)
  340. {
  341. if (!mReflowCallbackPosted &&
  342. (!aRect.IsEqualEdges(mRect) || mHorzWidth != aHorzWidth)) {
  343. PresContext()->PresShell()->PostReflowCallback(this);
  344. mReflowCallbackPosted = true;
  345. mOriginalHorzWidth = mHorzWidth;
  346. }
  347. else if (mReflowCallbackPosted &&
  348. mHorzWidth != aHorzWidth && mOriginalHorzWidth == aHorzWidth) {
  349. PresContext()->PresShell()->CancelReflowCallback(this);
  350. mReflowCallbackPosted = false;
  351. mOriginalHorzWidth = -1;
  352. }
  353. }
  354. void
  355. nsTreeBodyFrame::SetXULBounds(nsBoxLayoutState& aBoxLayoutState, const nsRect& aRect,
  356. bool aRemoveOverflowArea)
  357. {
  358. nscoord horzWidth = CalcHorzWidth(GetScrollParts());
  359. ManageReflowCallback(aRect, horzWidth);
  360. mHorzWidth = horzWidth;
  361. nsLeafBoxFrame::SetXULBounds(aBoxLayoutState, aRect, aRemoveOverflowArea);
  362. }
  363. bool
  364. nsTreeBodyFrame::ReflowFinished()
  365. {
  366. if (!mView) {
  367. nsWeakFrame weakFrame(this);
  368. EnsureView();
  369. NS_ENSURE_TRUE(weakFrame.IsAlive(), false);
  370. }
  371. if (mView) {
  372. CalcInnerBox();
  373. ScrollParts parts = GetScrollParts();
  374. mHorzWidth = CalcHorzWidth(parts);
  375. if (!mHasFixedRowCount) {
  376. mPageLength = mInnerBox.height / mRowHeight;
  377. }
  378. int32_t lastPageTopRow = std::max(0, mRowCount - mPageLength);
  379. if (mTopRowIndex > lastPageTopRow)
  380. ScrollToRowInternal(parts, lastPageTopRow);
  381. nsIContent *treeContent = GetBaseElement();
  382. if (treeContent &&
  383. treeContent->AttrValueIs(kNameSpaceID_None,
  384. nsGkAtoms::keepcurrentinview,
  385. nsGkAtoms::_true, eCaseMatters)) {
  386. // make sure that the current selected item is still
  387. // visible after the tree changes size.
  388. nsCOMPtr<nsITreeSelection> sel;
  389. mView->GetSelection(getter_AddRefs(sel));
  390. if (sel) {
  391. int32_t currentIndex;
  392. sel->GetCurrentIndex(&currentIndex);
  393. if (currentIndex != -1)
  394. EnsureRowIsVisibleInternal(parts, currentIndex);
  395. }
  396. }
  397. if (!FullScrollbarsUpdate(false)) {
  398. return false;
  399. }
  400. }
  401. mReflowCallbackPosted = false;
  402. return false;
  403. }
  404. void
  405. nsTreeBodyFrame::ReflowCallbackCanceled()
  406. {
  407. mReflowCallbackPosted = false;
  408. }
  409. nsresult
  410. nsTreeBodyFrame::GetView(nsITreeView * *aView)
  411. {
  412. *aView = nullptr;
  413. nsWeakFrame weakFrame(this);
  414. EnsureView();
  415. NS_ENSURE_STATE(weakFrame.IsAlive());
  416. NS_IF_ADDREF(*aView = mView);
  417. return NS_OK;
  418. }
  419. nsresult
  420. nsTreeBodyFrame::SetView(nsITreeView * aView)
  421. {
  422. // First clear out the old view.
  423. if (mView) {
  424. nsCOMPtr<nsITreeSelection> sel;
  425. mView->GetSelection(getter_AddRefs(sel));
  426. if (sel)
  427. sel->SetTree(nullptr);
  428. mView->SetTree(nullptr);
  429. // Only reset the top row index and delete the columns if we had an old non-null view.
  430. mTopRowIndex = 0;
  431. }
  432. // Tree, meet the view.
  433. mView = aView;
  434. // Changing the view causes us to refetch our data. This will
  435. // necessarily entail a full invalidation of the tree.
  436. Invalidate();
  437. nsIContent *treeContent = GetBaseElement();
  438. if (treeContent) {
  439. #ifdef ACCESSIBILITY
  440. nsAccessibilityService* accService = nsIPresShell::AccService();
  441. if (accService)
  442. accService->TreeViewChanged(PresContext()->GetPresShell(), treeContent, mView);
  443. #endif
  444. FireDOMEvent(NS_LITERAL_STRING("TreeViewChanged"), treeContent);
  445. }
  446. if (mView) {
  447. // Give the view a new empty selection object to play with, but only if it
  448. // doesn't have one already.
  449. nsCOMPtr<nsITreeSelection> sel;
  450. mView->GetSelection(getter_AddRefs(sel));
  451. if (sel) {
  452. sel->SetTree(mTreeBoxObject);
  453. }
  454. else {
  455. NS_NewTreeSelection(mTreeBoxObject, getter_AddRefs(sel));
  456. mView->SetSelection(sel);
  457. }
  458. // View, meet the tree.
  459. nsWeakFrame weakFrame(this);
  460. mView->SetTree(mTreeBoxObject);
  461. NS_ENSURE_STATE(weakFrame.IsAlive());
  462. mView->GetRowCount(&mRowCount);
  463. if (!PresContext()->PresShell()->IsReflowLocked()) {
  464. // The scrollbar will need to be updated.
  465. FullScrollbarsUpdate(false);
  466. } else if (!mReflowCallbackPosted) {
  467. mReflowCallbackPosted = true;
  468. PresContext()->PresShell()->PostReflowCallback(this);
  469. }
  470. }
  471. return NS_OK;
  472. }
  473. nsresult
  474. nsTreeBodyFrame::SetFocused(bool aFocused)
  475. {
  476. if (mFocused != aFocused) {
  477. mFocused = aFocused;
  478. if (mView) {
  479. nsCOMPtr<nsITreeSelection> sel;
  480. mView->GetSelection(getter_AddRefs(sel));
  481. if (sel)
  482. sel->InvalidateSelection();
  483. }
  484. }
  485. return NS_OK;
  486. }
  487. nsresult
  488. nsTreeBodyFrame::GetTreeBody(nsIDOMElement** aElement)
  489. {
  490. //NS_ASSERTION(mContent, "no content, see bug #104878");
  491. if (!mContent)
  492. return NS_ERROR_NULL_POINTER;
  493. return mContent->QueryInterface(NS_GET_IID(nsIDOMElement), (void**)aElement);
  494. }
  495. int32_t
  496. nsTreeBodyFrame::RowHeight() const
  497. {
  498. return nsPresContext::AppUnitsToIntCSSPixels(mRowHeight);
  499. }
  500. int32_t
  501. nsTreeBodyFrame::RowWidth()
  502. {
  503. return nsPresContext::AppUnitsToIntCSSPixels(CalcHorzWidth(GetScrollParts()));
  504. }
  505. int32_t
  506. nsTreeBodyFrame::GetHorizontalPosition() const
  507. {
  508. return nsPresContext::AppUnitsToIntCSSPixels(mHorzPosition);
  509. }
  510. nsresult
  511. nsTreeBodyFrame::GetSelectionRegion(nsIScriptableRegion **aRegion)
  512. {
  513. *aRegion = nullptr;
  514. nsCOMPtr<nsITreeSelection> selection;
  515. mView->GetSelection(getter_AddRefs(selection));
  516. NS_ENSURE_TRUE(selection, NS_OK);
  517. nsCOMPtr<nsIScriptableRegion> region = do_CreateInstance("@mozilla.org/gfx/region;1");
  518. NS_ENSURE_TRUE(region, NS_ERROR_FAILURE);
  519. region->Init();
  520. RefPtr<nsPresContext> presContext = PresContext();
  521. nsIntRect rect = mRect.ToOutsidePixels(presContext->AppUnitsPerCSSPixel());
  522. nsIFrame* rootFrame = presContext->PresShell()->GetRootFrame();
  523. nsPoint origin = GetOffsetTo(rootFrame);
  524. // iterate through the visible rows and add the selected ones to the
  525. // drag region
  526. int32_t x = nsPresContext::AppUnitsToIntCSSPixels(origin.x);
  527. int32_t y = nsPresContext::AppUnitsToIntCSSPixels(origin.y);
  528. int32_t top = y;
  529. int32_t end = LastVisibleRow();
  530. int32_t rowHeight = nsPresContext::AppUnitsToIntCSSPixels(mRowHeight);
  531. for (int32_t i = mTopRowIndex; i <= end; i++) {
  532. bool isSelected;
  533. selection->IsSelected(i, &isSelected);
  534. if (isSelected)
  535. region->UnionRect(x, y, rect.width, rowHeight);
  536. y += rowHeight;
  537. }
  538. // clip to the tree boundary in case one row extends past it
  539. region->IntersectRect(x, top, rect.width, rect.height);
  540. region.forget(aRegion);
  541. return NS_OK;
  542. }
  543. nsresult
  544. nsTreeBodyFrame::Invalidate()
  545. {
  546. if (mUpdateBatchNest)
  547. return NS_OK;
  548. InvalidateFrame();
  549. return NS_OK;
  550. }
  551. nsresult
  552. nsTreeBodyFrame::InvalidateColumn(nsITreeColumn* aCol)
  553. {
  554. if (mUpdateBatchNest)
  555. return NS_OK;
  556. RefPtr<nsTreeColumn> col = GetColumnImpl(aCol);
  557. if (!col)
  558. return NS_ERROR_INVALID_ARG;
  559. #ifdef ACCESSIBILITY
  560. if (nsIPresShell::IsAccessibilityActive())
  561. FireInvalidateEvent(-1, -1, aCol, aCol);
  562. #endif
  563. nsRect columnRect;
  564. nsresult rv = col->GetRect(this, mInnerBox.y, mInnerBox.height, &columnRect);
  565. NS_ENSURE_SUCCESS(rv, rv);
  566. // When false then column is out of view
  567. if (OffsetForHorzScroll(columnRect, true))
  568. InvalidateFrameWithRect(columnRect);
  569. return NS_OK;
  570. }
  571. nsresult
  572. nsTreeBodyFrame::InvalidateRow(int32_t aIndex)
  573. {
  574. if (mUpdateBatchNest)
  575. return NS_OK;
  576. #ifdef ACCESSIBILITY
  577. if (nsIPresShell::IsAccessibilityActive())
  578. FireInvalidateEvent(aIndex, aIndex, nullptr, nullptr);
  579. #endif
  580. aIndex -= mTopRowIndex;
  581. if (aIndex < 0 || aIndex > mPageLength)
  582. return NS_OK;
  583. nsRect rowRect(mInnerBox.x, mInnerBox.y+mRowHeight*aIndex, mInnerBox.width, mRowHeight);
  584. InvalidateFrameWithRect(rowRect);
  585. return NS_OK;
  586. }
  587. nsresult
  588. nsTreeBodyFrame::InvalidateCell(int32_t aIndex, nsITreeColumn* aCol)
  589. {
  590. if (mUpdateBatchNest)
  591. return NS_OK;
  592. #ifdef ACCESSIBILITY
  593. if (nsIPresShell::IsAccessibilityActive())
  594. FireInvalidateEvent(aIndex, aIndex, aCol, aCol);
  595. #endif
  596. aIndex -= mTopRowIndex;
  597. if (aIndex < 0 || aIndex > mPageLength)
  598. return NS_OK;
  599. RefPtr<nsTreeColumn> col = GetColumnImpl(aCol);
  600. if (!col)
  601. return NS_ERROR_INVALID_ARG;
  602. nsRect cellRect;
  603. nsresult rv = col->GetRect(this, mInnerBox.y+mRowHeight*aIndex, mRowHeight,
  604. &cellRect);
  605. NS_ENSURE_SUCCESS(rv, rv);
  606. if (OffsetForHorzScroll(cellRect, true))
  607. InvalidateFrameWithRect(cellRect);
  608. return NS_OK;
  609. }
  610. nsresult
  611. nsTreeBodyFrame::InvalidateRange(int32_t aStart, int32_t aEnd)
  612. {
  613. if (mUpdateBatchNest)
  614. return NS_OK;
  615. if (aStart == aEnd)
  616. return InvalidateRow(aStart);
  617. int32_t last = LastVisibleRow();
  618. if (aStart > aEnd || aEnd < mTopRowIndex || aStart > last)
  619. return NS_OK;
  620. if (aStart < mTopRowIndex)
  621. aStart = mTopRowIndex;
  622. if (aEnd > last)
  623. aEnd = last;
  624. #ifdef ACCESSIBILITY
  625. if (nsIPresShell::IsAccessibilityActive()) {
  626. int32_t end =
  627. mRowCount > 0 ? ((mRowCount <= aEnd) ? mRowCount - 1 : aEnd) : 0;
  628. FireInvalidateEvent(aStart, end, nullptr, nullptr);
  629. }
  630. #endif
  631. nsRect rangeRect(mInnerBox.x, mInnerBox.y+mRowHeight*(aStart-mTopRowIndex), mInnerBox.width, mRowHeight*(aEnd-aStart+1));
  632. InvalidateFrameWithRect(rangeRect);
  633. return NS_OK;
  634. }
  635. nsresult
  636. nsTreeBodyFrame::InvalidateColumnRange(int32_t aStart, int32_t aEnd, nsITreeColumn* aCol)
  637. {
  638. if (mUpdateBatchNest)
  639. return NS_OK;
  640. RefPtr<nsTreeColumn> col = GetColumnImpl(aCol);
  641. if (!col)
  642. return NS_ERROR_INVALID_ARG;
  643. if (aStart == aEnd)
  644. return InvalidateCell(aStart, col);
  645. int32_t last = LastVisibleRow();
  646. if (aStart > aEnd || aEnd < mTopRowIndex || aStart > last)
  647. return NS_OK;
  648. if (aStart < mTopRowIndex)
  649. aStart = mTopRowIndex;
  650. if (aEnd > last)
  651. aEnd = last;
  652. #ifdef ACCESSIBILITY
  653. if (nsIPresShell::IsAccessibilityActive()) {
  654. int32_t end =
  655. mRowCount > 0 ? ((mRowCount <= aEnd) ? mRowCount - 1 : aEnd) : 0;
  656. FireInvalidateEvent(aStart, end, aCol, aCol);
  657. }
  658. #endif
  659. nsRect rangeRect;
  660. nsresult rv = col->GetRect(this,
  661. mInnerBox.y+mRowHeight*(aStart-mTopRowIndex),
  662. mRowHeight*(aEnd-aStart+1),
  663. &rangeRect);
  664. NS_ENSURE_SUCCESS(rv, rv);
  665. InvalidateFrameWithRect(rangeRect);
  666. return NS_OK;
  667. }
  668. static void
  669. FindScrollParts(nsIFrame* aCurrFrame, nsTreeBodyFrame::ScrollParts* aResult)
  670. {
  671. if (!aResult->mColumnsScrollFrame) {
  672. nsIScrollableFrame* f = do_QueryFrame(aCurrFrame);
  673. if (f) {
  674. aResult->mColumnsFrame = aCurrFrame;
  675. aResult->mColumnsScrollFrame = f;
  676. }
  677. }
  678. nsScrollbarFrame *sf = do_QueryFrame(aCurrFrame);
  679. if (sf) {
  680. if (!aCurrFrame->IsXULHorizontal()) {
  681. if (!aResult->mVScrollbar) {
  682. aResult->mVScrollbar = sf;
  683. }
  684. } else {
  685. if (!aResult->mHScrollbar) {
  686. aResult->mHScrollbar = sf;
  687. }
  688. }
  689. // don't bother searching inside a scrollbar
  690. return;
  691. }
  692. nsIFrame* child = aCurrFrame->PrincipalChildList().FirstChild();
  693. while (child &&
  694. !child->GetContent()->IsRootOfNativeAnonymousSubtree() &&
  695. (!aResult->mVScrollbar || !aResult->mHScrollbar ||
  696. !aResult->mColumnsScrollFrame)) {
  697. FindScrollParts(child, aResult);
  698. child = child->GetNextSibling();
  699. }
  700. }
  701. nsTreeBodyFrame::ScrollParts nsTreeBodyFrame::GetScrollParts()
  702. {
  703. ScrollParts result = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  704. nsIContent* baseElement = GetBaseElement();
  705. nsIFrame* treeFrame =
  706. baseElement ? baseElement->GetPrimaryFrame() : nullptr;
  707. if (treeFrame) {
  708. // The way we do this, searching through the entire frame subtree, is pretty
  709. // dumb! We should know where these frames are.
  710. FindScrollParts(treeFrame, &result);
  711. if (result.mHScrollbar) {
  712. result.mHScrollbar->SetScrollbarMediatorContent(GetContent());
  713. nsIFrame* f = do_QueryFrame(result.mHScrollbar);
  714. result.mHScrollbarContent = f->GetContent();
  715. }
  716. if (result.mVScrollbar) {
  717. result.mVScrollbar->SetScrollbarMediatorContent(GetContent());
  718. nsIFrame* f = do_QueryFrame(result.mVScrollbar);
  719. result.mVScrollbarContent = f->GetContent();
  720. }
  721. }
  722. return result;
  723. }
  724. void
  725. nsTreeBodyFrame::UpdateScrollbars(const ScrollParts& aParts)
  726. {
  727. nscoord rowHeightAsPixels = nsPresContext::AppUnitsToIntCSSPixels(mRowHeight);
  728. nsWeakFrame weakFrame(this);
  729. if (aParts.mVScrollbar) {
  730. nsAutoString curPos;
  731. curPos.AppendInt(mTopRowIndex*rowHeightAsPixels);
  732. aParts.mVScrollbarContent->
  733. SetAttr(kNameSpaceID_None, nsGkAtoms::curpos, curPos, true);
  734. // 'this' might be deleted here
  735. }
  736. if (weakFrame.IsAlive() && aParts.mHScrollbar) {
  737. nsAutoString curPos;
  738. curPos.AppendInt(mHorzPosition);
  739. aParts.mHScrollbarContent->
  740. SetAttr(kNameSpaceID_None, nsGkAtoms::curpos, curPos, true);
  741. // 'this' might be deleted here
  742. }
  743. if (weakFrame.IsAlive() && mScrollbarActivity) {
  744. mScrollbarActivity->ActivityOccurred();
  745. }
  746. }
  747. void
  748. nsTreeBodyFrame::CheckOverflow(const ScrollParts& aParts)
  749. {
  750. bool verticalOverflowChanged = false;
  751. bool horizontalOverflowChanged = false;
  752. if (!mVerticalOverflow && mRowCount > mPageLength) {
  753. mVerticalOverflow = true;
  754. verticalOverflowChanged = true;
  755. }
  756. else if (mVerticalOverflow && mRowCount <= mPageLength) {
  757. mVerticalOverflow = false;
  758. verticalOverflowChanged = true;
  759. }
  760. if (aParts.mColumnsFrame) {
  761. nsRect bounds = aParts.mColumnsFrame->GetRect();
  762. if (bounds.width != 0) {
  763. /* Ignore overflows that are less than half a pixel. Yes these happen
  764. all over the place when flex boxes are compressed real small.
  765. Probably a result of a rounding errors somewhere in the layout code. */
  766. bounds.width += nsPresContext::CSSPixelsToAppUnits(0.5f);
  767. if (!mHorizontalOverflow && bounds.width < mHorzWidth) {
  768. mHorizontalOverflow = true;
  769. horizontalOverflowChanged = true;
  770. } else if (mHorizontalOverflow && bounds.width >= mHorzWidth) {
  771. mHorizontalOverflow = false;
  772. horizontalOverflowChanged = true;
  773. }
  774. }
  775. }
  776. nsWeakFrame weakFrame(this);
  777. RefPtr<nsPresContext> presContext = PresContext();
  778. nsCOMPtr<nsIPresShell> presShell = presContext->GetPresShell();
  779. nsCOMPtr<nsIContent> content = mContent;
  780. if (verticalOverflowChanged) {
  781. InternalScrollPortEvent event(true,
  782. mVerticalOverflow ? eScrollPortOverflow : eScrollPortUnderflow,
  783. nullptr);
  784. event.mOrient = InternalScrollPortEvent::eVertical;
  785. EventDispatcher::Dispatch(content, presContext, &event);
  786. }
  787. if (horizontalOverflowChanged) {
  788. InternalScrollPortEvent event(true,
  789. mHorizontalOverflow ? eScrollPortOverflow : eScrollPortUnderflow,
  790. nullptr);
  791. event.mOrient = InternalScrollPortEvent::eHorizontal;
  792. EventDispatcher::Dispatch(content, presContext, &event);
  793. }
  794. // The synchronous event dispatch above can trigger reflow notifications.
  795. // Flush those explicitly now, so that we can guard against potential infinite
  796. // recursion. See bug 905909.
  797. if (!weakFrame.IsAlive()) {
  798. return;
  799. }
  800. NS_ASSERTION(!mCheckingOverflow, "mCheckingOverflow should not already be set");
  801. // Don't use AutoRestore since we want to not touch mCheckingOverflow if we fail
  802. // the weakFrame.IsAlive() check below
  803. mCheckingOverflow = true;
  804. presShell->FlushPendingNotifications(Flush_Layout);
  805. if (!weakFrame.IsAlive()) {
  806. return;
  807. }
  808. mCheckingOverflow = false;
  809. }
  810. void
  811. nsTreeBodyFrame::InvalidateScrollbars(const ScrollParts& aParts, nsWeakFrame& aWeakColumnsFrame)
  812. {
  813. if (mUpdateBatchNest || !mView)
  814. return;
  815. nsWeakFrame weakFrame(this);
  816. if (aParts.mVScrollbar) {
  817. // Do Vertical Scrollbar
  818. nsAutoString maxposStr;
  819. nscoord rowHeightAsPixels = nsPresContext::AppUnitsToIntCSSPixels(mRowHeight);
  820. int32_t size = rowHeightAsPixels * (mRowCount > mPageLength ? mRowCount - mPageLength : 0);
  821. maxposStr.AppendInt(size);
  822. aParts.mVScrollbarContent->
  823. SetAttr(kNameSpaceID_None, nsGkAtoms::maxpos, maxposStr, true);
  824. ENSURE_TRUE(weakFrame.IsAlive());
  825. // Also set our page increment and decrement.
  826. nscoord pageincrement = mPageLength*rowHeightAsPixels;
  827. nsAutoString pageStr;
  828. pageStr.AppendInt(pageincrement);
  829. aParts.mVScrollbarContent->
  830. SetAttr(kNameSpaceID_None, nsGkAtoms::pageincrement, pageStr, true);
  831. ENSURE_TRUE(weakFrame.IsAlive());
  832. }
  833. if (aParts.mHScrollbar && aParts.mColumnsFrame && aWeakColumnsFrame.IsAlive()) {
  834. // And now Horizontal scrollbar
  835. nsRect bounds = aParts.mColumnsFrame->GetRect();
  836. nsAutoString maxposStr;
  837. maxposStr.AppendInt(mHorzWidth > bounds.width ? mHorzWidth - bounds.width : 0);
  838. aParts.mHScrollbarContent->
  839. SetAttr(kNameSpaceID_None, nsGkAtoms::maxpos, maxposStr, true);
  840. ENSURE_TRUE(weakFrame.IsAlive());
  841. nsAutoString pageStr;
  842. pageStr.AppendInt(bounds.width);
  843. aParts.mHScrollbarContent->
  844. SetAttr(kNameSpaceID_None, nsGkAtoms::pageincrement, pageStr, true);
  845. ENSURE_TRUE(weakFrame.IsAlive());
  846. pageStr.Truncate();
  847. pageStr.AppendInt(nsPresContext::CSSPixelsToAppUnits(16));
  848. aParts.mHScrollbarContent->
  849. SetAttr(kNameSpaceID_None, nsGkAtoms::increment, pageStr, true);
  850. }
  851. if (weakFrame.IsAlive() && mScrollbarActivity) {
  852. mScrollbarActivity->ActivityOccurred();
  853. }
  854. }
  855. // Takes client x/y in pixels, converts them to appunits, and converts into
  856. // values relative to this nsTreeBodyFrame frame.
  857. nsPoint
  858. nsTreeBodyFrame::AdjustClientCoordsToBoxCoordSpace(int32_t aX, int32_t aY)
  859. {
  860. nsPoint point(nsPresContext::CSSPixelsToAppUnits(aX),
  861. nsPresContext::CSSPixelsToAppUnits(aY));
  862. nsPresContext* presContext = PresContext();
  863. point -= GetOffsetTo(presContext->GetPresShell()->GetRootFrame());
  864. // Adjust by the inner box coords, so that we're in the inner box's
  865. // coordinate space.
  866. point -= mInnerBox.TopLeft();
  867. return point;
  868. } // AdjustClientCoordsToBoxCoordSpace
  869. nsresult
  870. nsTreeBodyFrame::GetRowAt(int32_t aX, int32_t aY, int32_t* _retval)
  871. {
  872. if (!mView)
  873. return NS_OK;
  874. nsPoint point = AdjustClientCoordsToBoxCoordSpace(aX, aY);
  875. // Check if the coordinates are above our visible space.
  876. if (point.y < 0) {
  877. *_retval = -1;
  878. return NS_OK;
  879. }
  880. *_retval = GetRowAt(point.x, point.y);
  881. return NS_OK;
  882. }
  883. nsresult
  884. nsTreeBodyFrame::GetCellAt(int32_t aX, int32_t aY, int32_t* aRow, nsITreeColumn** aCol,
  885. nsACString& aChildElt)
  886. {
  887. if (!mView)
  888. return NS_OK;
  889. nsPoint point = AdjustClientCoordsToBoxCoordSpace(aX, aY);
  890. // Check if the coordinates are above our visible space.
  891. if (point.y < 0) {
  892. *aRow = -1;
  893. return NS_OK;
  894. }
  895. nsTreeColumn* col;
  896. nsIAtom* child;
  897. GetCellAt(point.x, point.y, aRow, &col, &child);
  898. if (col) {
  899. NS_ADDREF(*aCol = col);
  900. if (child == nsCSSAnonBoxes::moztreecell)
  901. aChildElt.AssignLiteral("cell");
  902. else if (child == nsCSSAnonBoxes::moztreetwisty)
  903. aChildElt.AssignLiteral("twisty");
  904. else if (child == nsCSSAnonBoxes::moztreeimage)
  905. aChildElt.AssignLiteral("image");
  906. else if (child == nsCSSAnonBoxes::moztreecelltext)
  907. aChildElt.AssignLiteral("text");
  908. }
  909. return NS_OK;
  910. }
  911. //
  912. // GetCoordsForCellItem
  913. //
  914. // Find the x/y location and width/height (all in PIXELS) of the given object
  915. // in the given column.
  916. //
  917. // XXX IMPORTANT XXX:
  918. // Hyatt says in the bug for this, that the following needs to be done:
  919. // (1) You need to deal with overflow when computing cell rects. See other column
  920. // iteration examples... if you don't deal with this, you'll mistakenly extend the
  921. // cell into the scrollbar's rect.
  922. //
  923. // (2) You are adjusting the cell rect by the *row" border padding. That's
  924. // wrong. You need to first adjust a row rect by its border/padding, and then the
  925. // cell rect fits inside the adjusted row rect. It also can have border/padding
  926. // as well as margins. The vertical direction isn't that important, but you need
  927. // to get the horizontal direction right.
  928. //
  929. // (3) GetImageSize() does not include margins (but it does include border/padding).
  930. // You need to make sure to add in the image's margins as well.
  931. //
  932. nsresult
  933. nsTreeBodyFrame::GetCoordsForCellItem(int32_t aRow, nsITreeColumn* aCol, const nsACString& aElement,
  934. int32_t *aX, int32_t *aY, int32_t *aWidth, int32_t *aHeight)
  935. {
  936. *aX = 0;
  937. *aY = 0;
  938. *aWidth = 0;
  939. *aHeight = 0;
  940. bool isRTL = StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
  941. nscoord currX = mInnerBox.x - mHorzPosition;
  942. // The Rect for the requested item.
  943. nsRect theRect;
  944. nsPresContext* presContext = PresContext();
  945. for (nsTreeColumn* currCol = mColumns->GetFirstColumn(); currCol; currCol = currCol->GetNext()) {
  946. // The Rect for the current cell.
  947. nscoord colWidth;
  948. #ifdef DEBUG
  949. nsresult rv =
  950. #endif
  951. currCol->GetWidthInTwips(this, &colWidth);
  952. NS_ASSERTION(NS_SUCCEEDED(rv), "invalid column");
  953. nsRect cellRect(currX, mInnerBox.y + mRowHeight * (aRow - mTopRowIndex),
  954. colWidth, mRowHeight);
  955. // Check the ID of the current column to see if it matches. If it doesn't
  956. // increment the current X value and continue to the next column.
  957. if (currCol != aCol) {
  958. currX += cellRect.width;
  959. continue;
  960. }
  961. // Now obtain the properties for our cell.
  962. PrefillPropertyArray(aRow, currCol);
  963. nsAutoString properties;
  964. mView->GetCellProperties(aRow, currCol, properties);
  965. nsTreeUtils::TokenizeProperties(properties, mScratchArray);
  966. nsStyleContext* rowContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreerow);
  967. // We don't want to consider any of the decorations that may be present
  968. // on the current row, so we have to deflate the rect by the border and
  969. // padding and offset its left and top coordinates appropriately.
  970. AdjustForBorderPadding(rowContext, cellRect);
  971. nsStyleContext* cellContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecell);
  972. NS_NAMED_LITERAL_CSTRING(cell, "cell");
  973. if (currCol->IsCycler() || cell.Equals(aElement)) {
  974. // If the current Column is a Cycler, then the Rect is just the cell - the margins.
  975. // Similarly, if we're just being asked for the cell rect, provide it.
  976. theRect = cellRect;
  977. nsMargin cellMargin;
  978. cellContext->StyleMargin()->GetMargin(cellMargin);
  979. theRect.Deflate(cellMargin);
  980. break;
  981. }
  982. // Since we're not looking for the cell, and since the cell isn't a cycler,
  983. // we're looking for some subcomponent, and now we need to subtract the
  984. // borders and padding of the cell from cellRect so this does not
  985. // interfere with our computations.
  986. AdjustForBorderPadding(cellContext, cellRect);
  987. nsRenderingContext rc(
  988. presContext->PresShell()->CreateReferenceRenderingContext());
  989. // Now we'll start making our way across the cell, starting at the edge of
  990. // the cell and proceeding until we hit the right edge. |cellX| is the
  991. // working X value that we will increment as we crawl from left to right.
  992. nscoord cellX = cellRect.x;
  993. nscoord remainWidth = cellRect.width;
  994. if (currCol->IsPrimary()) {
  995. // If the current Column is a Primary, then we need to take into account the indentation
  996. // and possibly a twisty.
  997. // The amount of indentation is the indentation width (|mIndentation|) by the level.
  998. int32_t level;
  999. mView->GetLevel(aRow, &level);
  1000. if (!isRTL)
  1001. cellX += mIndentation * level;
  1002. remainWidth -= mIndentation * level;
  1003. // Find the twisty rect by computing its size.
  1004. nsRect imageRect;
  1005. nsRect twistyRect(cellRect);
  1006. nsStyleContext* twistyContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreetwisty);
  1007. GetTwistyRect(aRow, currCol, imageRect, twistyRect, presContext,
  1008. twistyContext);
  1009. if (NS_LITERAL_CSTRING("twisty").Equals(aElement)) {
  1010. // If we're looking for the twisty Rect, just return the size
  1011. theRect = twistyRect;
  1012. break;
  1013. }
  1014. // Now we need to add in the margins of the twisty element, so that we
  1015. // can find the offset of the next element in the cell.
  1016. nsMargin twistyMargin;
  1017. twistyContext->StyleMargin()->GetMargin(twistyMargin);
  1018. twistyRect.Inflate(twistyMargin);
  1019. // Adjust our working X value with the twisty width (image size, margins,
  1020. // borders, padding.
  1021. if (!isRTL)
  1022. cellX += twistyRect.width;
  1023. }
  1024. // Cell Image
  1025. nsStyleContext* imageContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeimage);
  1026. nsRect imageSize = GetImageSize(aRow, currCol, false, imageContext);
  1027. if (NS_LITERAL_CSTRING("image").Equals(aElement)) {
  1028. theRect = imageSize;
  1029. theRect.x = cellX;
  1030. theRect.y = cellRect.y;
  1031. break;
  1032. }
  1033. // Add in the margins of the cell image.
  1034. nsMargin imageMargin;
  1035. imageContext->StyleMargin()->GetMargin(imageMargin);
  1036. imageSize.Inflate(imageMargin);
  1037. // Increment cellX by the image width
  1038. if (!isRTL)
  1039. cellX += imageSize.width;
  1040. // Cell Text
  1041. nsAutoString cellText;
  1042. mView->GetCellText(aRow, currCol, cellText);
  1043. // We're going to measure this text so we need to ensure bidi is enabled if
  1044. // necessary
  1045. CheckTextForBidi(cellText);
  1046. // Create a scratch rect to represent the text rectangle, with the current
  1047. // X and Y coords, and a guess at the width and height. The width is the
  1048. // remaining width we have left to traverse in the cell, which will be the
  1049. // widest possible value for the text rect, and the row height.
  1050. nsRect textRect(cellX, cellRect.y, remainWidth, cellRect.height);
  1051. // Measure the width of the text. If the width of the text is greater than
  1052. // the remaining width available, then we just assume that the text has
  1053. // been cropped and use the remaining rect as the text Rect. Otherwise,
  1054. // we add in borders and padding to the text dimension and give that back.
  1055. nsStyleContext* textContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecelltext);
  1056. RefPtr<nsFontMetrics> fm =
  1057. nsLayoutUtils::GetFontMetricsForStyleContext(textContext);
  1058. nscoord height = fm->MaxHeight();
  1059. nsMargin textMargin;
  1060. textContext->StyleMargin()->GetMargin(textMargin);
  1061. textRect.Deflate(textMargin);
  1062. // Center the text. XXX Obey vertical-align style prop?
  1063. if (height < textRect.height) {
  1064. textRect.y += (textRect.height - height) / 2;
  1065. textRect.height = height;
  1066. }
  1067. nsMargin bp(0,0,0,0);
  1068. GetBorderPadding(textContext, bp);
  1069. textRect.height += bp.top + bp.bottom;
  1070. AdjustForCellText(cellText, aRow, currCol, rc, *fm, textRect);
  1071. theRect = textRect;
  1072. }
  1073. if (isRTL)
  1074. theRect.x = mInnerBox.width - theRect.x - theRect.width;
  1075. *aX = nsPresContext::AppUnitsToIntCSSPixels(theRect.x);
  1076. *aY = nsPresContext::AppUnitsToIntCSSPixels(theRect.y);
  1077. *aWidth = nsPresContext::AppUnitsToIntCSSPixels(theRect.width);
  1078. *aHeight = nsPresContext::AppUnitsToIntCSSPixels(theRect.height);
  1079. return NS_OK;
  1080. }
  1081. int32_t
  1082. nsTreeBodyFrame::GetRowAt(int32_t aX, int32_t aY)
  1083. {
  1084. // Now just mod by our total inner box height and add to our top row index.
  1085. int32_t row = (aY/mRowHeight)+mTopRowIndex;
  1086. // Check if the coordinates are below our visible space (or within our visible
  1087. // space but below any row).
  1088. if (row > mTopRowIndex + mPageLength || row >= mRowCount)
  1089. return -1;
  1090. return row;
  1091. }
  1092. void
  1093. nsTreeBodyFrame::CheckTextForBidi(nsAutoString& aText)
  1094. {
  1095. // We could check to see whether the prescontext already has bidi enabled,
  1096. // but usually it won't, so it's probably faster to avoid the call to
  1097. // GetPresContext() when it's not needed.
  1098. if (HasRTLChars(aText)) {
  1099. PresContext()->SetBidiEnabled();
  1100. }
  1101. }
  1102. void
  1103. nsTreeBodyFrame::AdjustForCellText(nsAutoString& aText,
  1104. int32_t aRowIndex, nsTreeColumn* aColumn,
  1105. nsRenderingContext& aRenderingContext,
  1106. nsFontMetrics& aFontMetrics,
  1107. nsRect& aTextRect)
  1108. {
  1109. NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
  1110. DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
  1111. nscoord maxWidth = aTextRect.width;
  1112. bool widthIsGreater = nsLayoutUtils::StringWidthIsGreaterThan(aText,
  1113. aFontMetrics,
  1114. drawTarget,
  1115. maxWidth);
  1116. if (aColumn->Overflow()) {
  1117. DebugOnly<nsresult> rv;
  1118. nsTreeColumn* nextColumn = aColumn->GetNext();
  1119. while (nextColumn && widthIsGreater) {
  1120. while (nextColumn) {
  1121. nscoord width;
  1122. rv = nextColumn->GetWidthInTwips(this, &width);
  1123. NS_ASSERTION(NS_SUCCEEDED(rv), "nextColumn is invalid");
  1124. if (width != 0)
  1125. break;
  1126. nextColumn = nextColumn->GetNext();
  1127. }
  1128. if (nextColumn) {
  1129. nsAutoString nextText;
  1130. mView->GetCellText(aRowIndex, nextColumn, nextText);
  1131. // We don't measure or draw this text so no need to check it for
  1132. // bidi-ness
  1133. if (nextText.Length() == 0) {
  1134. nscoord width;
  1135. rv = nextColumn->GetWidthInTwips(this, &width);
  1136. NS_ASSERTION(NS_SUCCEEDED(rv), "nextColumn is invalid");
  1137. maxWidth += width;
  1138. widthIsGreater = nsLayoutUtils::StringWidthIsGreaterThan(aText,
  1139. aFontMetrics,
  1140. drawTarget,
  1141. maxWidth);
  1142. nextColumn = nextColumn->GetNext();
  1143. }
  1144. else {
  1145. nextColumn = nullptr;
  1146. }
  1147. }
  1148. }
  1149. }
  1150. nscoord width;
  1151. if (widthIsGreater) {
  1152. // See if the width is even smaller than the ellipsis
  1153. // If so, clear the text completely.
  1154. const nsDependentString& kEllipsis = nsContentUtils::GetLocalizedEllipsis();
  1155. aFontMetrics.SetTextRunRTL(false);
  1156. nscoord ellipsisWidth =
  1157. nsLayoutUtils::AppUnitWidthOfString(kEllipsis, aFontMetrics, drawTarget);
  1158. width = maxWidth;
  1159. if (ellipsisWidth > width)
  1160. aText.SetLength(0);
  1161. else if (ellipsisWidth == width)
  1162. aText.Assign(kEllipsis);
  1163. else {
  1164. // We will be drawing an ellipsis, thank you very much.
  1165. // Subtract out the required width of the ellipsis.
  1166. // This is the total remaining width we have to play with.
  1167. width -= ellipsisWidth;
  1168. // Now we crop.
  1169. switch (aColumn->GetCropStyle()) {
  1170. default:
  1171. case 0: {
  1172. // Crop right.
  1173. nscoord cwidth;
  1174. nscoord twidth = 0;
  1175. uint32_t length = aText.Length();
  1176. uint32_t i;
  1177. for (i = 0; i < length; ++i) {
  1178. char16_t ch = aText[i];
  1179. // XXX this is horrible and doesn't handle clusters
  1180. cwidth = nsLayoutUtils::AppUnitWidthOfString(ch, aFontMetrics,
  1181. drawTarget);
  1182. if (twidth + cwidth > width)
  1183. break;
  1184. twidth += cwidth;
  1185. }
  1186. aText.Truncate(i);
  1187. aText.Append(kEllipsis);
  1188. }
  1189. break;
  1190. case 2: {
  1191. // Crop left.
  1192. nscoord cwidth;
  1193. nscoord twidth = 0;
  1194. int32_t length = aText.Length();
  1195. int32_t i;
  1196. for (i=length-1; i >= 0; --i) {
  1197. char16_t ch = aText[i];
  1198. cwidth = nsLayoutUtils::AppUnitWidthOfString(ch, aFontMetrics,
  1199. drawTarget);
  1200. if (twidth + cwidth > width)
  1201. break;
  1202. twidth += cwidth;
  1203. }
  1204. nsAutoString copy;
  1205. aText.Right(copy, length-1-i);
  1206. aText.Assign(kEllipsis);
  1207. aText += copy;
  1208. }
  1209. break;
  1210. case 1:
  1211. {
  1212. // Crop center.
  1213. nsAutoString leftStr, rightStr;
  1214. nscoord cwidth, twidth = 0;
  1215. int32_t length = aText.Length();
  1216. int32_t rightPos = length - 1;
  1217. for (int32_t leftPos = 0; leftPos < rightPos; ++leftPos) {
  1218. char16_t ch = aText[leftPos];
  1219. cwidth = nsLayoutUtils::AppUnitWidthOfString(ch, aFontMetrics,
  1220. drawTarget);
  1221. twidth += cwidth;
  1222. if (twidth > width)
  1223. break;
  1224. leftStr.Append(ch);
  1225. ch = aText[rightPos];
  1226. cwidth = nsLayoutUtils::AppUnitWidthOfString(ch, aFontMetrics,
  1227. drawTarget);
  1228. twidth += cwidth;
  1229. if (twidth > width)
  1230. break;
  1231. rightStr.Insert(ch, 0);
  1232. --rightPos;
  1233. }
  1234. aText = leftStr;
  1235. aText.Append(kEllipsis);
  1236. aText += rightStr;
  1237. }
  1238. break;
  1239. }
  1240. }
  1241. }
  1242. width = nsLayoutUtils::AppUnitWidthOfStringBidi(aText, this, aFontMetrics,
  1243. aRenderingContext);
  1244. switch (aColumn->GetTextAlignment()) {
  1245. case NS_STYLE_TEXT_ALIGN_RIGHT: {
  1246. aTextRect.x += aTextRect.width - width;
  1247. }
  1248. break;
  1249. case NS_STYLE_TEXT_ALIGN_CENTER: {
  1250. aTextRect.x += (aTextRect.width - width) / 2;
  1251. }
  1252. break;
  1253. }
  1254. aTextRect.width = width;
  1255. }
  1256. nsIAtom*
  1257. nsTreeBodyFrame::GetItemWithinCellAt(nscoord aX, const nsRect& aCellRect,
  1258. int32_t aRowIndex,
  1259. nsTreeColumn* aColumn)
  1260. {
  1261. NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
  1262. // Obtain the properties for our cell.
  1263. PrefillPropertyArray(aRowIndex, aColumn);
  1264. nsAutoString properties;
  1265. mView->GetCellProperties(aRowIndex, aColumn, properties);
  1266. nsTreeUtils::TokenizeProperties(properties, mScratchArray);
  1267. // Resolve style for the cell.
  1268. nsStyleContext* cellContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecell);
  1269. // Obtain the margins for the cell and then deflate our rect by that
  1270. // amount. The cell is assumed to be contained within the deflated rect.
  1271. nsRect cellRect(aCellRect);
  1272. nsMargin cellMargin;
  1273. cellContext->StyleMargin()->GetMargin(cellMargin);
  1274. cellRect.Deflate(cellMargin);
  1275. // Adjust the rect for its border and padding.
  1276. AdjustForBorderPadding(cellContext, cellRect);
  1277. if (aX < cellRect.x || aX >= cellRect.x + cellRect.width) {
  1278. // The user clicked within the cell's margins/borders/padding. This constitutes a click on the cell.
  1279. return nsCSSAnonBoxes::moztreecell;
  1280. }
  1281. nscoord currX = cellRect.x;
  1282. nscoord remainingWidth = cellRect.width;
  1283. // Handle right alignment hit testing.
  1284. bool isRTL = StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
  1285. nsPresContext* presContext = PresContext();
  1286. nsRenderingContext rc(
  1287. presContext->PresShell()->CreateReferenceRenderingContext());
  1288. if (aColumn->IsPrimary()) {
  1289. // If we're the primary column, we have indentation and a twisty.
  1290. int32_t level;
  1291. mView->GetLevel(aRowIndex, &level);
  1292. if (!isRTL)
  1293. currX += mIndentation*level;
  1294. remainingWidth -= mIndentation*level;
  1295. if ((isRTL && aX > currX + remainingWidth) ||
  1296. (!isRTL && aX < currX)) {
  1297. // The user clicked within the indentation.
  1298. return nsCSSAnonBoxes::moztreecell;
  1299. }
  1300. // Always leave space for the twisty.
  1301. nsRect twistyRect(currX, cellRect.y, remainingWidth, cellRect.height);
  1302. bool hasTwisty = false;
  1303. bool isContainer = false;
  1304. mView->IsContainer(aRowIndex, &isContainer);
  1305. if (isContainer) {
  1306. bool isContainerEmpty = false;
  1307. mView->IsContainerEmpty(aRowIndex, &isContainerEmpty);
  1308. if (!isContainerEmpty)
  1309. hasTwisty = true;
  1310. }
  1311. // Resolve style for the twisty.
  1312. nsStyleContext* twistyContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreetwisty);
  1313. nsRect imageSize;
  1314. GetTwistyRect(aRowIndex, aColumn, imageSize, twistyRect, presContext,
  1315. twistyContext);
  1316. // We will treat a click as hitting the twisty if it happens on the margins, borders, padding,
  1317. // or content of the twisty object. By allowing a "slop" into the margin, we make it a little
  1318. // bit easier for a user to hit the twisty. (We don't want to be too picky here.)
  1319. nsMargin twistyMargin;
  1320. twistyContext->StyleMargin()->GetMargin(twistyMargin);
  1321. twistyRect.Inflate(twistyMargin);
  1322. if (isRTL)
  1323. twistyRect.x = currX + remainingWidth - twistyRect.width;
  1324. // Now we test to see if aX is actually within the twistyRect. If it is, and if the item should
  1325. // have a twisty, then we return "twisty". If it is within the rect but we shouldn't have a twisty,
  1326. // then we return "cell".
  1327. if (aX >= twistyRect.x && aX < twistyRect.x + twistyRect.width) {
  1328. if (hasTwisty)
  1329. return nsCSSAnonBoxes::moztreetwisty;
  1330. else
  1331. return nsCSSAnonBoxes::moztreecell;
  1332. }
  1333. if (!isRTL)
  1334. currX += twistyRect.width;
  1335. remainingWidth -= twistyRect.width;
  1336. }
  1337. // Now test to see if the user hit the icon for the cell.
  1338. nsRect iconRect(currX, cellRect.y, remainingWidth, cellRect.height);
  1339. // Resolve style for the image.
  1340. nsStyleContext* imageContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeimage);
  1341. nsRect iconSize = GetImageSize(aRowIndex, aColumn, false, imageContext);
  1342. nsMargin imageMargin;
  1343. imageContext->StyleMargin()->GetMargin(imageMargin);
  1344. iconSize.Inflate(imageMargin);
  1345. iconRect.width = iconSize.width;
  1346. if (isRTL)
  1347. iconRect.x = currX + remainingWidth - iconRect.width;
  1348. if (aX >= iconRect.x && aX < iconRect.x + iconRect.width) {
  1349. // The user clicked on the image.
  1350. return nsCSSAnonBoxes::moztreeimage;
  1351. }
  1352. if (!isRTL)
  1353. currX += iconRect.width;
  1354. remainingWidth -= iconRect.width;
  1355. nsAutoString cellText;
  1356. mView->GetCellText(aRowIndex, aColumn, cellText);
  1357. // We're going to measure this text so we need to ensure bidi is enabled if
  1358. // necessary
  1359. CheckTextForBidi(cellText);
  1360. nsRect textRect(currX, cellRect.y, remainingWidth, cellRect.height);
  1361. nsStyleContext* textContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecelltext);
  1362. nsMargin textMargin;
  1363. textContext->StyleMargin()->GetMargin(textMargin);
  1364. textRect.Deflate(textMargin);
  1365. AdjustForBorderPadding(textContext, textRect);
  1366. RefPtr<nsFontMetrics> fm =
  1367. nsLayoutUtils::GetFontMetricsForStyleContext(textContext);
  1368. AdjustForCellText(cellText, aRowIndex, aColumn, rc, *fm, textRect);
  1369. if (aX >= textRect.x && aX < textRect.x + textRect.width)
  1370. return nsCSSAnonBoxes::moztreecelltext;
  1371. else
  1372. return nsCSSAnonBoxes::moztreecell;
  1373. }
  1374. void
  1375. nsTreeBodyFrame::GetCellAt(nscoord aX, nscoord aY, int32_t* aRow,
  1376. nsTreeColumn** aCol, nsIAtom** aChildElt)
  1377. {
  1378. *aCol = nullptr;
  1379. *aChildElt = nullptr;
  1380. *aRow = GetRowAt(aX, aY);
  1381. if (*aRow < 0)
  1382. return;
  1383. // Determine the column hit.
  1384. for (nsTreeColumn* currCol = mColumns->GetFirstColumn(); currCol;
  1385. currCol = currCol->GetNext()) {
  1386. nsRect cellRect;
  1387. nsresult rv = currCol->GetRect(this,
  1388. mInnerBox.y +
  1389. mRowHeight * (*aRow - mTopRowIndex),
  1390. mRowHeight,
  1391. &cellRect);
  1392. if (NS_FAILED(rv)) {
  1393. NS_NOTREACHED("column has no frame");
  1394. continue;
  1395. }
  1396. if (!OffsetForHorzScroll(cellRect, false))
  1397. continue;
  1398. if (aX >= cellRect.x && aX < cellRect.x + cellRect.width) {
  1399. // We know the column hit now.
  1400. *aCol = currCol;
  1401. if (currCol->IsCycler())
  1402. // Cyclers contain only images. Fill this in immediately and return.
  1403. *aChildElt = nsCSSAnonBoxes::moztreeimage;
  1404. else
  1405. *aChildElt = GetItemWithinCellAt(aX, cellRect, *aRow, currCol);
  1406. break;
  1407. }
  1408. }
  1409. }
  1410. nsresult
  1411. nsTreeBodyFrame::GetCellWidth(int32_t aRow, nsTreeColumn* aCol,
  1412. nsRenderingContext* aRenderingContext,
  1413. nscoord& aDesiredSize, nscoord& aCurrentSize)
  1414. {
  1415. NS_PRECONDITION(aCol, "aCol must not be null");
  1416. NS_PRECONDITION(aRenderingContext, "aRenderingContext must not be null");
  1417. // The rect for the current cell.
  1418. nscoord colWidth;
  1419. nsresult rv = aCol->GetWidthInTwips(this, &colWidth);
  1420. NS_ENSURE_SUCCESS(rv, rv);
  1421. nsRect cellRect(0, 0, colWidth, mRowHeight);
  1422. int32_t overflow = cellRect.x+cellRect.width-(mInnerBox.x+mInnerBox.width);
  1423. if (overflow > 0)
  1424. cellRect.width -= overflow;
  1425. // Adjust borders and padding for the cell.
  1426. nsStyleContext* cellContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecell);
  1427. nsMargin bp(0,0,0,0);
  1428. GetBorderPadding(cellContext, bp);
  1429. aCurrentSize = cellRect.width;
  1430. aDesiredSize = bp.left + bp.right;
  1431. if (aCol->IsPrimary()) {
  1432. // If the current Column is a Primary, then we need to take into account
  1433. // the indentation and possibly a twisty.
  1434. // The amount of indentation is the indentation width (|mIndentation|) by the level.
  1435. int32_t level;
  1436. mView->GetLevel(aRow, &level);
  1437. aDesiredSize += mIndentation * level;
  1438. // Find the twisty rect by computing its size.
  1439. nsStyleContext* twistyContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreetwisty);
  1440. nsRect imageSize;
  1441. nsRect twistyRect(cellRect);
  1442. GetTwistyRect(aRow, aCol, imageSize, twistyRect, PresContext(),
  1443. twistyContext);
  1444. // Add in the margins of the twisty element.
  1445. nsMargin twistyMargin;
  1446. twistyContext->StyleMargin()->GetMargin(twistyMargin);
  1447. twistyRect.Inflate(twistyMargin);
  1448. aDesiredSize += twistyRect.width;
  1449. }
  1450. nsStyleContext* imageContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeimage);
  1451. // Account for the width of the cell image.
  1452. nsRect imageSize = GetImageSize(aRow, aCol, false, imageContext);
  1453. // Add in the margins of the cell image.
  1454. nsMargin imageMargin;
  1455. imageContext->StyleMargin()->GetMargin(imageMargin);
  1456. imageSize.Inflate(imageMargin);
  1457. aDesiredSize += imageSize.width;
  1458. // Get the cell text.
  1459. nsAutoString cellText;
  1460. mView->GetCellText(aRow, aCol, cellText);
  1461. // We're going to measure this text so we need to ensure bidi is enabled if
  1462. // necessary
  1463. CheckTextForBidi(cellText);
  1464. nsStyleContext* textContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecelltext);
  1465. // Get the borders and padding for the text.
  1466. GetBorderPadding(textContext, bp);
  1467. RefPtr<nsFontMetrics> fm =
  1468. nsLayoutUtils::GetFontMetricsForStyleContext(textContext);
  1469. // Get the width of the text itself
  1470. nscoord width = nsLayoutUtils::AppUnitWidthOfStringBidi(cellText, this, *fm,
  1471. *aRenderingContext);
  1472. nscoord totalTextWidth = width + bp.left + bp.right;
  1473. aDesiredSize += totalTextWidth;
  1474. return NS_OK;
  1475. }
  1476. nsresult
  1477. nsTreeBodyFrame::IsCellCropped(int32_t aRow, nsITreeColumn* aCol, bool *_retval)
  1478. {
  1479. nscoord currentSize, desiredSize;
  1480. nsresult rv;
  1481. RefPtr<nsTreeColumn> col = GetColumnImpl(aCol);
  1482. if (!col)
  1483. return NS_ERROR_INVALID_ARG;
  1484. nsRenderingContext rc(
  1485. PresContext()->PresShell()->CreateReferenceRenderingContext());
  1486. rv = GetCellWidth(aRow, col, &rc, desiredSize, currentSize);
  1487. NS_ENSURE_SUCCESS(rv, rv);
  1488. *_retval = desiredSize > currentSize;
  1489. return NS_OK;
  1490. }
  1491. void
  1492. nsTreeBodyFrame::MarkDirtyIfSelect()
  1493. {
  1494. nsIContent* baseElement = GetBaseElement();
  1495. if (baseElement && baseElement->IsHTMLElement(nsGkAtoms::select)) {
  1496. // If we are an intrinsically sized select widget, we may need to
  1497. // resize, if the widest item was removed or a new item was added.
  1498. // XXX optimize this more
  1499. mStringWidth = -1;
  1500. PresContext()->PresShell()->FrameNeedsReflow(this,
  1501. nsIPresShell::eTreeChange,
  1502. NS_FRAME_IS_DIRTY);
  1503. }
  1504. }
  1505. nsresult
  1506. nsTreeBodyFrame::CreateTimer(const LookAndFeel::IntID aID,
  1507. nsTimerCallbackFunc aFunc, int32_t aType,
  1508. nsITimer** aTimer)
  1509. {
  1510. // Get the delay from the look and feel service.
  1511. int32_t delay = LookAndFeel::GetInt(aID, 0);
  1512. nsCOMPtr<nsITimer> timer;
  1513. // Create a new timer only if the delay is greater than zero.
  1514. // Zero value means that this feature is completely disabled.
  1515. if (delay > 0) {
  1516. timer = do_CreateInstance("@mozilla.org/timer;1");
  1517. if (timer)
  1518. timer->InitWithFuncCallback(aFunc, this, delay, aType);
  1519. }
  1520. NS_IF_ADDREF(*aTimer = timer);
  1521. return NS_OK;
  1522. }
  1523. nsresult
  1524. nsTreeBodyFrame::RowCountChanged(int32_t aIndex, int32_t aCount)
  1525. {
  1526. if (aCount == 0 || !mView)
  1527. return NS_OK; // Nothing to do.
  1528. #ifdef ACCESSIBILITY
  1529. if (nsIPresShell::IsAccessibilityActive())
  1530. FireRowCountChangedEvent(aIndex, aCount);
  1531. #endif
  1532. nsWeakFrame weakFrame(this);
  1533. // Adjust our selection.
  1534. nsCOMPtr<nsITreeView> view = mView;
  1535. nsCOMPtr<nsITreeSelection> sel;
  1536. view->GetSelection(getter_AddRefs(sel));
  1537. if (sel)
  1538. sel->AdjustSelection(aIndex, aCount);
  1539. NS_ENSURE_STATE (weakFrame.IsAlive());
  1540. if (mUpdateBatchNest)
  1541. return NS_OK;
  1542. mRowCount += aCount;
  1543. #ifdef DEBUG
  1544. int32_t rowCount = mRowCount;
  1545. mView->GetRowCount(&rowCount);
  1546. NS_ASSERTION(rowCount == mRowCount, "row count did not change by the amount suggested, check caller");
  1547. #endif
  1548. int32_t count = Abs(aCount);
  1549. int32_t last = LastVisibleRow();
  1550. if (aIndex >= mTopRowIndex && aIndex <= last)
  1551. InvalidateRange(aIndex, last);
  1552. ScrollParts parts = GetScrollParts();
  1553. if (mTopRowIndex == 0) {
  1554. // Just update the scrollbar and return.
  1555. if (FullScrollbarsUpdate(false)) {
  1556. MarkDirtyIfSelect();
  1557. }
  1558. return NS_OK;
  1559. }
  1560. bool needsInvalidation = false;
  1561. // Adjust our top row index.
  1562. if (aCount > 0) {
  1563. if (mTopRowIndex > aIndex) {
  1564. // Rows came in above us. Augment the top row index.
  1565. mTopRowIndex += aCount;
  1566. }
  1567. }
  1568. else if (aCount < 0) {
  1569. if (mTopRowIndex > aIndex+count-1) {
  1570. // No need to invalidate. The remove happened
  1571. // completely above us (offscreen).
  1572. mTopRowIndex -= count;
  1573. }
  1574. else if (mTopRowIndex >= aIndex) {
  1575. // This is a full-blown invalidate.
  1576. if (mTopRowIndex + mPageLength > mRowCount - 1) {
  1577. mTopRowIndex = std::max(0, mRowCount - 1 - mPageLength);
  1578. }
  1579. needsInvalidation = true;
  1580. }
  1581. }
  1582. if (FullScrollbarsUpdate(needsInvalidation)) {
  1583. MarkDirtyIfSelect();
  1584. }
  1585. return NS_OK;
  1586. }
  1587. nsresult
  1588. nsTreeBodyFrame::BeginUpdateBatch()
  1589. {
  1590. ++mUpdateBatchNest;
  1591. return NS_OK;
  1592. }
  1593. nsresult
  1594. nsTreeBodyFrame::EndUpdateBatch()
  1595. {
  1596. NS_ASSERTION(mUpdateBatchNest > 0, "badly nested update batch");
  1597. if (--mUpdateBatchNest == 0) {
  1598. if (mView) {
  1599. Invalidate();
  1600. int32_t countBeforeUpdate = mRowCount;
  1601. mView->GetRowCount(&mRowCount);
  1602. if (countBeforeUpdate != mRowCount) {
  1603. if (mTopRowIndex + mPageLength > mRowCount - 1) {
  1604. mTopRowIndex = std::max(0, mRowCount - 1 - mPageLength);
  1605. }
  1606. FullScrollbarsUpdate(false);
  1607. }
  1608. }
  1609. }
  1610. return NS_OK;
  1611. }
  1612. void
  1613. nsTreeBodyFrame::PrefillPropertyArray(int32_t aRowIndex, nsTreeColumn* aCol)
  1614. {
  1615. NS_PRECONDITION(!aCol || aCol->GetFrame(), "invalid column passed");
  1616. mScratchArray.Clear();
  1617. // focus
  1618. if (mFocused)
  1619. mScratchArray.AppendElement(nsGkAtoms::focus);
  1620. // sort
  1621. bool sorted = false;
  1622. mView->IsSorted(&sorted);
  1623. if (sorted)
  1624. mScratchArray.AppendElement(nsGkAtoms::sorted);
  1625. // drag session
  1626. if (mSlots && mSlots->mIsDragging)
  1627. mScratchArray.AppendElement(nsGkAtoms::dragSession);
  1628. if (aRowIndex != -1) {
  1629. if (aRowIndex == mMouseOverRow)
  1630. mScratchArray.AppendElement(nsGkAtoms::hover);
  1631. nsCOMPtr<nsITreeSelection> selection;
  1632. mView->GetSelection(getter_AddRefs(selection));
  1633. if (selection) {
  1634. // selected
  1635. bool isSelected;
  1636. selection->IsSelected(aRowIndex, &isSelected);
  1637. if (isSelected)
  1638. mScratchArray.AppendElement(nsGkAtoms::selected);
  1639. // current
  1640. int32_t currentIndex;
  1641. selection->GetCurrentIndex(&currentIndex);
  1642. if (aRowIndex == currentIndex)
  1643. mScratchArray.AppendElement(nsGkAtoms::current);
  1644. // active
  1645. if (aCol) {
  1646. nsCOMPtr<nsITreeColumn> currentColumn;
  1647. selection->GetCurrentColumn(getter_AddRefs(currentColumn));
  1648. if (aCol == currentColumn)
  1649. mScratchArray.AppendElement(nsGkAtoms::active);
  1650. }
  1651. }
  1652. // container or leaf
  1653. bool isContainer = false;
  1654. mView->IsContainer(aRowIndex, &isContainer);
  1655. if (isContainer) {
  1656. mScratchArray.AppendElement(nsGkAtoms::container);
  1657. // open or closed
  1658. bool isOpen = false;
  1659. mView->IsContainerOpen(aRowIndex, &isOpen);
  1660. if (isOpen)
  1661. mScratchArray.AppendElement(nsGkAtoms::open);
  1662. else
  1663. mScratchArray.AppendElement(nsGkAtoms::closed);
  1664. }
  1665. else {
  1666. mScratchArray.AppendElement(nsGkAtoms::leaf);
  1667. }
  1668. // drop orientation
  1669. if (mSlots && mSlots->mDropAllowed && mSlots->mDropRow == aRowIndex) {
  1670. if (mSlots->mDropOrient == nsITreeView::DROP_BEFORE)
  1671. mScratchArray.AppendElement(nsGkAtoms::dropBefore);
  1672. else if (mSlots->mDropOrient == nsITreeView::DROP_ON)
  1673. mScratchArray.AppendElement(nsGkAtoms::dropOn);
  1674. else if (mSlots->mDropOrient == nsITreeView::DROP_AFTER)
  1675. mScratchArray.AppendElement(nsGkAtoms::dropAfter);
  1676. }
  1677. // odd or even
  1678. if (aRowIndex % 2)
  1679. mScratchArray.AppendElement(nsGkAtoms::odd);
  1680. else
  1681. mScratchArray.AppendElement(nsGkAtoms::even);
  1682. nsIContent* baseContent = GetBaseElement();
  1683. if (baseContent && baseContent->HasAttr(kNameSpaceID_None, nsGkAtoms::editing))
  1684. mScratchArray.AppendElement(nsGkAtoms::editing);
  1685. // multiple columns
  1686. if (mColumns->GetColumnAt(1))
  1687. mScratchArray.AppendElement(nsGkAtoms::multicol);
  1688. }
  1689. if (aCol) {
  1690. mScratchArray.AppendElement(aCol->GetAtom());
  1691. if (aCol->IsPrimary())
  1692. mScratchArray.AppendElement(nsGkAtoms::primary);
  1693. if (aCol->GetType() == nsITreeColumn::TYPE_CHECKBOX) {
  1694. mScratchArray.AppendElement(nsGkAtoms::checkbox);
  1695. if (aRowIndex != -1) {
  1696. nsAutoString value;
  1697. mView->GetCellValue(aRowIndex, aCol, value);
  1698. if (value.EqualsLiteral("true"))
  1699. mScratchArray.AppendElement(nsGkAtoms::checked);
  1700. }
  1701. }
  1702. else if (aCol->GetType() == nsITreeColumn::TYPE_PROGRESSMETER) {
  1703. mScratchArray.AppendElement(nsGkAtoms::progressmeter);
  1704. if (aRowIndex != -1) {
  1705. int32_t state;
  1706. mView->GetProgressMode(aRowIndex, aCol, &state);
  1707. if (state == nsITreeView::PROGRESS_NORMAL)
  1708. mScratchArray.AppendElement(nsGkAtoms::progressNormal);
  1709. else if (state == nsITreeView::PROGRESS_UNDETERMINED)
  1710. mScratchArray.AppendElement(nsGkAtoms::progressUndetermined);
  1711. }
  1712. }
  1713. // Read special properties from attributes on the column content node
  1714. if (aCol->mContent->AttrValueIs(kNameSpaceID_None,
  1715. nsGkAtoms::insertbefore,
  1716. nsGkAtoms::_true, eCaseMatters))
  1717. mScratchArray.AppendElement(nsGkAtoms::insertbefore);
  1718. if (aCol->mContent->AttrValueIs(kNameSpaceID_None,
  1719. nsGkAtoms::insertafter,
  1720. nsGkAtoms::_true, eCaseMatters))
  1721. mScratchArray.AppendElement(nsGkAtoms::insertafter);
  1722. }
  1723. }
  1724. nsITheme*
  1725. nsTreeBodyFrame::GetTwistyRect(int32_t aRowIndex,
  1726. nsTreeColumn* aColumn,
  1727. nsRect& aImageRect,
  1728. nsRect& aTwistyRect,
  1729. nsPresContext* aPresContext,
  1730. nsStyleContext* aTwistyContext)
  1731. {
  1732. // The twisty rect extends all the way to the end of the cell. This is incorrect. We need to
  1733. // determine the twisty rect's true width. This is done by examining the style context for
  1734. // a width first. If it has one, we use that. If it doesn't, we use the image's natural width.
  1735. // If the image hasn't loaded and if no width is specified, then we just bail. If there is
  1736. // a -moz-appearance involved, adjust the rect by the minimum widget size provided by
  1737. // the theme implementation.
  1738. aImageRect = GetImageSize(aRowIndex, aColumn, true, aTwistyContext);
  1739. if (aImageRect.height > aTwistyRect.height)
  1740. aImageRect.height = aTwistyRect.height;
  1741. if (aImageRect.width > aTwistyRect.width)
  1742. aImageRect.width = aTwistyRect.width;
  1743. else
  1744. aTwistyRect.width = aImageRect.width;
  1745. bool useTheme = false;
  1746. nsITheme *theme = nullptr;
  1747. const nsStyleDisplay* twistyDisplayData = aTwistyContext->StyleDisplay();
  1748. if (twistyDisplayData->mAppearance) {
  1749. theme = aPresContext->GetTheme();
  1750. if (theme && theme->ThemeSupportsWidget(aPresContext, nullptr, twistyDisplayData->mAppearance))
  1751. useTheme = true;
  1752. }
  1753. if (useTheme) {
  1754. LayoutDeviceIntSize minTwistySizePx;
  1755. bool canOverride = true;
  1756. theme->GetMinimumWidgetSize(aPresContext, this, twistyDisplayData->mAppearance,
  1757. &minTwistySizePx, &canOverride);
  1758. // GMWS() returns size in pixels, we need to convert it back to app units
  1759. nsSize minTwistySize;
  1760. minTwistySize.width = aPresContext->DevPixelsToAppUnits(minTwistySizePx.width);
  1761. minTwistySize.height = aPresContext->DevPixelsToAppUnits(minTwistySizePx.height);
  1762. if (aTwistyRect.width < minTwistySize.width || !canOverride)
  1763. aTwistyRect.width = minTwistySize.width;
  1764. }
  1765. return useTheme ? theme : nullptr;
  1766. }
  1767. nsresult
  1768. nsTreeBodyFrame::GetImage(int32_t aRowIndex, nsTreeColumn* aCol, bool aUseContext,
  1769. nsStyleContext* aStyleContext, bool& aAllowImageRegions, imgIContainer** aResult)
  1770. {
  1771. *aResult = nullptr;
  1772. nsAutoString imageSrc;
  1773. mView->GetImageSrc(aRowIndex, aCol, imageSrc);
  1774. RefPtr<imgRequestProxy> styleRequest;
  1775. if (!aUseContext && !imageSrc.IsEmpty()) {
  1776. aAllowImageRegions = false;
  1777. }
  1778. else {
  1779. // Obtain the URL from the style context.
  1780. aAllowImageRegions = true;
  1781. styleRequest = aStyleContext->StyleList()->GetListStyleImage();
  1782. if (!styleRequest)
  1783. return NS_OK;
  1784. nsCOMPtr<nsIURI> uri;
  1785. styleRequest->GetURI(getter_AddRefs(uri));
  1786. nsAutoCString spec;
  1787. nsresult rv = uri->GetSpec(spec);
  1788. NS_ENSURE_SUCCESS(rv, rv);
  1789. CopyUTF8toUTF16(spec, imageSrc);
  1790. }
  1791. // Look the image up in our cache.
  1792. nsTreeImageCacheEntry entry;
  1793. if (mImageCache.Get(imageSrc, &entry)) {
  1794. // Find out if the image has loaded.
  1795. uint32_t status;
  1796. imgIRequest *imgReq = entry.request;
  1797. imgReq->GetImageStatus(&status);
  1798. imgReq->GetImage(aResult); // We hand back the image here. The GetImage call addrefs *aResult.
  1799. bool animated = true; // Assuming animated is the safe option
  1800. // We can only call GetAnimated if we're decoded
  1801. if (*aResult && (status & imgIRequest::STATUS_DECODE_COMPLETE))
  1802. (*aResult)->GetAnimated(&animated);
  1803. if ((!(status & imgIRequest::STATUS_LOAD_COMPLETE)) || animated) {
  1804. // We either aren't done loading, or we're animating. Add our row as a listener for invalidations.
  1805. nsCOMPtr<imgINotificationObserver> obs;
  1806. imgReq->GetNotificationObserver(getter_AddRefs(obs));
  1807. if (obs) {
  1808. static_cast<nsTreeImageListener*> (obs.get())->AddCell(aRowIndex, aCol);
  1809. }
  1810. return NS_OK;
  1811. }
  1812. }
  1813. if (!*aResult) {
  1814. // Create a new nsTreeImageListener object and pass it our row and column
  1815. // information.
  1816. nsTreeImageListener* listener = new nsTreeImageListener(this);
  1817. if (!listener)
  1818. return NS_ERROR_OUT_OF_MEMORY;
  1819. if (!mCreatedListeners.PutEntry(listener)) {
  1820. return NS_ERROR_FAILURE;
  1821. }
  1822. listener->AddCell(aRowIndex, aCol);
  1823. nsCOMPtr<imgINotificationObserver> imgNotificationObserver = listener;
  1824. RefPtr<imgRequestProxy> imageRequest;
  1825. if (styleRequest) {
  1826. styleRequest->Clone(imgNotificationObserver, getter_AddRefs(imageRequest));
  1827. } else {
  1828. nsIDocument* doc = mContent->GetComposedDoc();
  1829. if (!doc)
  1830. // The page is currently being torn down. Why bother.
  1831. return NS_ERROR_FAILURE;
  1832. nsCOMPtr<nsIURI> baseURI = mContent->GetBaseURI();
  1833. nsCOMPtr<nsIURI> srcURI;
  1834. nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(srcURI),
  1835. imageSrc,
  1836. doc,
  1837. baseURI);
  1838. if (!srcURI)
  1839. return NS_ERROR_FAILURE;
  1840. // XXXbz what's the origin principal for this stuff that comes from our
  1841. // view? I guess we should assume that it's the node's principal...
  1842. nsresult rv = nsContentUtils::LoadImage(srcURI,
  1843. mContent,
  1844. doc,
  1845. mContent->NodePrincipal(),
  1846. doc->GetDocumentURI(),
  1847. doc->GetReferrerPolicy(),
  1848. imgNotificationObserver,
  1849. nsIRequest::LOAD_NORMAL,
  1850. EmptyString(),
  1851. getter_AddRefs(imageRequest));
  1852. NS_ENSURE_SUCCESS(rv, rv);
  1853. }
  1854. listener->UnsuppressInvalidation();
  1855. if (!imageRequest)
  1856. return NS_ERROR_FAILURE;
  1857. // We don't want discarding/decode-on-draw for xul images
  1858. imageRequest->StartDecoding();
  1859. imageRequest->LockImage();
  1860. // In a case it was already cached.
  1861. imageRequest->GetImage(aResult);
  1862. nsTreeImageCacheEntry cacheEntry(imageRequest, imgNotificationObserver);
  1863. mImageCache.Put(imageSrc, cacheEntry);
  1864. }
  1865. return NS_OK;
  1866. }
  1867. nsRect nsTreeBodyFrame::GetImageSize(int32_t aRowIndex, nsTreeColumn* aCol, bool aUseContext,
  1868. nsStyleContext* aStyleContext)
  1869. {
  1870. // XXX We should respond to visibility rules for collapsed vs. hidden.
  1871. // This method returns the width of the twisty INCLUDING borders and padding.
  1872. // It first checks the style context for a width. If none is found, it tries to
  1873. // use the default image width for the twisty. If no image is found, it defaults
  1874. // to border+padding.
  1875. nsRect r(0,0,0,0);
  1876. nsMargin bp(0,0,0,0);
  1877. GetBorderPadding(aStyleContext, bp);
  1878. r.Inflate(bp);
  1879. // Now r contains our border+padding info. We now need to get our width and
  1880. // height.
  1881. bool needWidth = false;
  1882. bool needHeight = false;
  1883. // We have to load image even though we already have a size.
  1884. // Don't change this, otherwise things start to go crazy.
  1885. bool useImageRegion = true;
  1886. nsCOMPtr<imgIContainer> image;
  1887. GetImage(aRowIndex, aCol, aUseContext, aStyleContext, useImageRegion, getter_AddRefs(image));
  1888. const nsStylePosition* myPosition = aStyleContext->StylePosition();
  1889. const nsStyleList* myList = aStyleContext->StyleList();
  1890. if (useImageRegion) {
  1891. r.x += myList->mImageRegion.x;
  1892. r.y += myList->mImageRegion.y;
  1893. }
  1894. if (myPosition->mWidth.GetUnit() == eStyleUnit_Coord) {
  1895. int32_t val = myPosition->mWidth.GetCoordValue();
  1896. r.width += val;
  1897. }
  1898. else if (useImageRegion && myList->mImageRegion.width > 0)
  1899. r.width += myList->mImageRegion.width;
  1900. else
  1901. needWidth = true;
  1902. if (myPosition->mHeight.GetUnit() == eStyleUnit_Coord) {
  1903. int32_t val = myPosition->mHeight.GetCoordValue();
  1904. r.height += val;
  1905. }
  1906. else if (useImageRegion && myList->mImageRegion.height > 0)
  1907. r.height += myList->mImageRegion.height;
  1908. else
  1909. needHeight = true;
  1910. if (image) {
  1911. if (needWidth || needHeight) {
  1912. // Get the natural image size.
  1913. if (needWidth) {
  1914. // Get the size from the image.
  1915. nscoord width;
  1916. image->GetWidth(&width);
  1917. r.width += nsPresContext::CSSPixelsToAppUnits(width);
  1918. }
  1919. if (needHeight) {
  1920. nscoord height;
  1921. image->GetHeight(&height);
  1922. r.height += nsPresContext::CSSPixelsToAppUnits(height);
  1923. }
  1924. }
  1925. }
  1926. return r;
  1927. }
  1928. // GetImageDestSize returns the destination size of the image.
  1929. // The width and height do not include borders and padding.
  1930. // The width and height have not been adjusted to fit in the row height
  1931. // or cell width.
  1932. // The width and height reflect the destination size specified in CSS,
  1933. // or the image region specified in CSS, or the natural size of the
  1934. // image.
  1935. // If only the destination width has been specified in CSS, the height is
  1936. // calculated to maintain the aspect ratio of the image.
  1937. // If only the destination height has been specified in CSS, the width is
  1938. // calculated to maintain the aspect ratio of the image.
  1939. nsSize
  1940. nsTreeBodyFrame::GetImageDestSize(nsStyleContext* aStyleContext,
  1941. bool useImageRegion,
  1942. imgIContainer* image)
  1943. {
  1944. nsSize size(0,0);
  1945. // We need to get the width and height.
  1946. bool needWidth = false;
  1947. bool needHeight = false;
  1948. // Get the style position to see if the CSS has specified the
  1949. // destination width/height.
  1950. const nsStylePosition* myPosition = aStyleContext->StylePosition();
  1951. if (myPosition->mWidth.GetUnit() == eStyleUnit_Coord) {
  1952. // CSS has specified the destination width.
  1953. size.width = myPosition->mWidth.GetCoordValue();
  1954. }
  1955. else {
  1956. // We'll need to get the width of the image/region.
  1957. needWidth = true;
  1958. }
  1959. if (myPosition->mHeight.GetUnit() == eStyleUnit_Coord) {
  1960. // CSS has specified the destination height.
  1961. size.height = myPosition->mHeight.GetCoordValue();
  1962. }
  1963. else {
  1964. // We'll need to get the height of the image/region.
  1965. needHeight = true;
  1966. }
  1967. if (needWidth || needHeight) {
  1968. // We need to get the size of the image/region.
  1969. nsSize imageSize(0,0);
  1970. const nsStyleList* myList = aStyleContext->StyleList();
  1971. if (useImageRegion && myList->mImageRegion.width > 0) {
  1972. // CSS has specified an image region.
  1973. // Use the width of the image region.
  1974. imageSize.width = myList->mImageRegion.width;
  1975. }
  1976. else if (image) {
  1977. nscoord width;
  1978. image->GetWidth(&width);
  1979. imageSize.width = nsPresContext::CSSPixelsToAppUnits(width);
  1980. }
  1981. if (useImageRegion && myList->mImageRegion.height > 0) {
  1982. // CSS has specified an image region.
  1983. // Use the height of the image region.
  1984. imageSize.height = myList->mImageRegion.height;
  1985. }
  1986. else if (image) {
  1987. nscoord height;
  1988. image->GetHeight(&height);
  1989. imageSize.height = nsPresContext::CSSPixelsToAppUnits(height);
  1990. }
  1991. if (needWidth) {
  1992. if (!needHeight && imageSize.height != 0) {
  1993. // The CSS specified the destination height, but not the destination
  1994. // width. We need to calculate the width so that we maintain the
  1995. // image's aspect ratio.
  1996. size.width = imageSize.width * size.height / imageSize.height;
  1997. }
  1998. else {
  1999. size.width = imageSize.width;
  2000. }
  2001. }
  2002. if (needHeight) {
  2003. if (!needWidth && imageSize.width != 0) {
  2004. // The CSS specified the destination width, but not the destination
  2005. // height. We need to calculate the height so that we maintain the
  2006. // image's aspect ratio.
  2007. size.height = imageSize.height * size.width / imageSize.width;
  2008. }
  2009. else {
  2010. size.height = imageSize.height;
  2011. }
  2012. }
  2013. }
  2014. return size;
  2015. }
  2016. // GetImageSourceRect returns the source rectangle of the image to be
  2017. // displayed.
  2018. // The width and height reflect the image region specified in CSS, or
  2019. // the natural size of the image.
  2020. // The width and height do not include borders and padding.
  2021. // The width and height do not reflect the destination size specified
  2022. // in CSS.
  2023. nsRect
  2024. nsTreeBodyFrame::GetImageSourceRect(nsStyleContext* aStyleContext,
  2025. bool useImageRegion,
  2026. imgIContainer* image)
  2027. {
  2028. nsRect r(0,0,0,0);
  2029. const nsStyleList* myList = aStyleContext->StyleList();
  2030. if (useImageRegion &&
  2031. (myList->mImageRegion.width > 0 || myList->mImageRegion.height > 0)) {
  2032. // CSS has specified an image region.
  2033. r = myList->mImageRegion;
  2034. }
  2035. else if (image) {
  2036. // Use the actual image size.
  2037. nscoord coord;
  2038. image->GetWidth(&coord);
  2039. r.width = nsPresContext::CSSPixelsToAppUnits(coord);
  2040. image->GetHeight(&coord);
  2041. r.height = nsPresContext::CSSPixelsToAppUnits(coord);
  2042. }
  2043. return r;
  2044. }
  2045. int32_t nsTreeBodyFrame::GetRowHeight()
  2046. {
  2047. // Look up the correct height. It is equal to the specified height
  2048. // + the specified margins.
  2049. mScratchArray.Clear();
  2050. nsStyleContext* rowContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreerow);
  2051. if (rowContext) {
  2052. const nsStylePosition* myPosition = rowContext->StylePosition();
  2053. nscoord minHeight = 0;
  2054. if (myPosition->mMinHeight.GetUnit() == eStyleUnit_Coord)
  2055. minHeight = myPosition->mMinHeight.GetCoordValue();
  2056. nscoord height = 0;
  2057. if (myPosition->mHeight.GetUnit() == eStyleUnit_Coord)
  2058. height = myPosition->mHeight.GetCoordValue();
  2059. if (height < minHeight)
  2060. height = minHeight;
  2061. if (height > 0) {
  2062. height = nsPresContext::AppUnitsToIntCSSPixels(height);
  2063. height += height % 2;
  2064. height = nsPresContext::CSSPixelsToAppUnits(height);
  2065. // XXX Check box-sizing to determine if border/padding should augment the height
  2066. // Inflate the height by our margins.
  2067. nsRect rowRect(0,0,0,height);
  2068. nsMargin rowMargin;
  2069. rowContext->StyleMargin()->GetMargin(rowMargin);
  2070. rowRect.Inflate(rowMargin);
  2071. height = rowRect.height;
  2072. return height;
  2073. }
  2074. }
  2075. return nsPresContext::CSSPixelsToAppUnits(18); // As good a default as any.
  2076. }
  2077. int32_t nsTreeBodyFrame::GetIndentation()
  2078. {
  2079. // Look up the correct indentation. It is equal to the specified indentation width.
  2080. mScratchArray.Clear();
  2081. nsStyleContext* indentContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeindentation);
  2082. if (indentContext) {
  2083. const nsStylePosition* myPosition = indentContext->StylePosition();
  2084. if (myPosition->mWidth.GetUnit() == eStyleUnit_Coord) {
  2085. nscoord val = myPosition->mWidth.GetCoordValue();
  2086. return val;
  2087. }
  2088. }
  2089. return nsPresContext::CSSPixelsToAppUnits(16); // As good a default as any.
  2090. }
  2091. void nsTreeBodyFrame::CalcInnerBox()
  2092. {
  2093. mInnerBox.SetRect(0, 0, mRect.width, mRect.height);
  2094. AdjustForBorderPadding(mStyleContext, mInnerBox);
  2095. }
  2096. nscoord
  2097. nsTreeBodyFrame::CalcHorzWidth(const ScrollParts& aParts)
  2098. {
  2099. // Compute the adjustment to the last column. This varies depending on the
  2100. // visibility of the columnpicker and the scrollbar.
  2101. if (aParts.mColumnsFrame)
  2102. mAdjustWidth = mRect.width - aParts.mColumnsFrame->GetRect().width;
  2103. else
  2104. mAdjustWidth = 0;
  2105. nscoord width = 0;
  2106. // We calculate this from the scrollable frame, so that it
  2107. // properly covers all contingencies of what could be
  2108. // scrollable (columns, body, etc...)
  2109. if (aParts.mColumnsScrollFrame) {
  2110. width = aParts.mColumnsScrollFrame->GetScrollRange().width +
  2111. aParts.mColumnsScrollFrame->GetScrollPortRect().width;
  2112. }
  2113. // If no horz scrolling periphery is present, then just return our width
  2114. if (width == 0)
  2115. width = mRect.width;
  2116. return width;
  2117. }
  2118. nsresult
  2119. nsTreeBodyFrame::GetCursor(const nsPoint& aPoint,
  2120. nsIFrame::Cursor& aCursor)
  2121. {
  2122. // Check the GetScriptHandlingObject so we don't end up running code when
  2123. // the document is a zombie.
  2124. bool dummy;
  2125. if (mView && GetContent()->GetComposedDoc()->GetScriptHandlingObject(dummy)) {
  2126. int32_t row;
  2127. nsTreeColumn* col;
  2128. nsIAtom* child;
  2129. GetCellAt(aPoint.x, aPoint.y, &row, &col, &child);
  2130. if (child) {
  2131. // Our scratch array is already prefilled.
  2132. nsStyleContext* childContext = GetPseudoStyleContext(child);
  2133. FillCursorInformationFromStyle(childContext->StyleUserInterface(),
  2134. aCursor);
  2135. if (aCursor.mCursor == NS_STYLE_CURSOR_AUTO)
  2136. aCursor.mCursor = NS_STYLE_CURSOR_DEFAULT;
  2137. return NS_OK;
  2138. }
  2139. }
  2140. return nsLeafBoxFrame::GetCursor(aPoint, aCursor);
  2141. }
  2142. static uint32_t GetDropEffect(WidgetGUIEvent* aEvent)
  2143. {
  2144. NS_ASSERTION(aEvent->mClass == eDragEventClass, "wrong event type");
  2145. WidgetDragEvent* dragEvent = aEvent->AsDragEvent();
  2146. nsContentUtils::SetDataTransferInEvent(dragEvent);
  2147. uint32_t action = 0;
  2148. if (dragEvent->mDataTransfer) {
  2149. dragEvent->mDataTransfer->GetDropEffectInt(&action);
  2150. }
  2151. return action;
  2152. }
  2153. nsresult
  2154. nsTreeBodyFrame::HandleEvent(nsPresContext* aPresContext,
  2155. WidgetGUIEvent* aEvent,
  2156. nsEventStatus* aEventStatus)
  2157. {
  2158. if (aEvent->mMessage == eMouseOver || aEvent->mMessage == eMouseMove) {
  2159. nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, this);
  2160. int32_t xTwips = pt.x - mInnerBox.x;
  2161. int32_t yTwips = pt.y - mInnerBox.y;
  2162. int32_t newrow = GetRowAt(xTwips, yTwips);
  2163. if (mMouseOverRow != newrow) {
  2164. // redraw the old and the new row
  2165. if (mMouseOverRow != -1)
  2166. InvalidateRow(mMouseOverRow);
  2167. mMouseOverRow = newrow;
  2168. if (mMouseOverRow != -1)
  2169. InvalidateRow(mMouseOverRow);
  2170. }
  2171. } else if (aEvent->mMessage == eMouseOut) {
  2172. if (mMouseOverRow != -1) {
  2173. InvalidateRow(mMouseOverRow);
  2174. mMouseOverRow = -1;
  2175. }
  2176. } else if (aEvent->mMessage == eDragEnter) {
  2177. if (!mSlots)
  2178. mSlots = new Slots();
  2179. // Cache several things we'll need throughout the course of our work. These
  2180. // will all get released on a drag exit.
  2181. if (mSlots->mTimer) {
  2182. mSlots->mTimer->Cancel();
  2183. mSlots->mTimer = nullptr;
  2184. }
  2185. // Cache the drag session.
  2186. mSlots->mIsDragging = true;
  2187. mSlots->mDropRow = -1;
  2188. mSlots->mDropOrient = -1;
  2189. mSlots->mDragAction = GetDropEffect(aEvent);
  2190. } else if (aEvent->mMessage == eDragOver) {
  2191. // The mouse is hovering over this tree. If we determine things are
  2192. // different from the last time, invalidate the drop feedback at the old
  2193. // position, query the view to see if the current location is droppable,
  2194. // and then invalidate the drop feedback at the new location if it is.
  2195. // The mouse may or may not have changed position from the last time
  2196. // we were called, so optimize out a lot of the extra notifications by
  2197. // checking if anything changed first. For drop feedback we use drop,
  2198. // dropBefore and dropAfter property.
  2199. if (!mView || !mSlots)
  2200. return NS_OK;
  2201. // Save last values, we will need them.
  2202. int32_t lastDropRow = mSlots->mDropRow;
  2203. int16_t lastDropOrient = mSlots->mDropOrient;
  2204. int16_t lastScrollLines = mSlots->mScrollLines;
  2205. // Find out the current drag action
  2206. uint32_t lastDragAction = mSlots->mDragAction;
  2207. mSlots->mDragAction = GetDropEffect(aEvent);
  2208. // Compute the row mouse is over and the above/below/on state.
  2209. // Below we'll use this to see if anything changed.
  2210. // Also check if we want to auto-scroll.
  2211. ComputeDropPosition(aEvent, &mSlots->mDropRow, &mSlots->mDropOrient, &mSlots->mScrollLines);
  2212. // While we're here, handle tracking of scrolling during a drag.
  2213. if (mSlots->mScrollLines) {
  2214. if (mSlots->mDropAllowed) {
  2215. // Invalidate primary cell at old location.
  2216. mSlots->mDropAllowed = false;
  2217. InvalidateDropFeedback(lastDropRow, lastDropOrient);
  2218. }
  2219. if (!lastScrollLines) {
  2220. // Cancel any previously initialized timer.
  2221. if (mSlots->mTimer) {
  2222. mSlots->mTimer->Cancel();
  2223. mSlots->mTimer = nullptr;
  2224. }
  2225. // Set a timer to trigger the tree scrolling.
  2226. CreateTimer(LookAndFeel::eIntID_TreeLazyScrollDelay,
  2227. LazyScrollCallback, nsITimer::TYPE_ONE_SHOT,
  2228. getter_AddRefs(mSlots->mTimer));
  2229. }
  2230. // Bail out to prevent spring loaded timer and feedback line settings.
  2231. return NS_OK;
  2232. }
  2233. // If changed from last time, invalidate primary cell at the old location and if allowed,
  2234. // invalidate primary cell at the new location. If nothing changed, just bail.
  2235. if (mSlots->mDropRow != lastDropRow ||
  2236. mSlots->mDropOrient != lastDropOrient ||
  2237. mSlots->mDragAction != lastDragAction) {
  2238. // Invalidate row at the old location.
  2239. if (mSlots->mDropAllowed) {
  2240. mSlots->mDropAllowed = false;
  2241. InvalidateDropFeedback(lastDropRow, lastDropOrient);
  2242. }
  2243. if (mSlots->mTimer) {
  2244. // Timer is active but for a different row than the current one, kill it.
  2245. mSlots->mTimer->Cancel();
  2246. mSlots->mTimer = nullptr;
  2247. }
  2248. if (mSlots->mDropRow >= 0) {
  2249. if (!mSlots->mTimer && mSlots->mDropOrient == nsITreeView::DROP_ON) {
  2250. // Either there wasn't a timer running or it was just killed above.
  2251. // If over a folder, start up a timer to open the folder.
  2252. bool isContainer = false;
  2253. mView->IsContainer(mSlots->mDropRow, &isContainer);
  2254. if (isContainer) {
  2255. bool isOpen = false;
  2256. mView->IsContainerOpen(mSlots->mDropRow, &isOpen);
  2257. if (!isOpen) {
  2258. // This node isn't expanded, set a timer to expand it.
  2259. CreateTimer(LookAndFeel::eIntID_TreeOpenDelay,
  2260. OpenCallback, nsITimer::TYPE_ONE_SHOT,
  2261. getter_AddRefs(mSlots->mTimer));
  2262. }
  2263. }
  2264. }
  2265. // The dataTransfer was initialized by the call to GetDropEffect above.
  2266. bool canDropAtNewLocation = false;
  2267. mView->CanDrop(mSlots->mDropRow, mSlots->mDropOrient,
  2268. aEvent->AsDragEvent()->mDataTransfer,
  2269. &canDropAtNewLocation);
  2270. if (canDropAtNewLocation) {
  2271. // Invalidate row at the new location.
  2272. mSlots->mDropAllowed = canDropAtNewLocation;
  2273. InvalidateDropFeedback(mSlots->mDropRow, mSlots->mDropOrient);
  2274. }
  2275. }
  2276. }
  2277. // Indicate that the drop is allowed by preventing the default behaviour.
  2278. if (mSlots->mDropAllowed)
  2279. *aEventStatus = nsEventStatus_eConsumeNoDefault;
  2280. } else if (aEvent->mMessage == eDrop) {
  2281. // this event was meant for another frame, so ignore it
  2282. if (!mSlots)
  2283. return NS_OK;
  2284. // Tell the view where the drop happened.
  2285. // Remove the drop folder and all its parents from the array.
  2286. int32_t parentIndex;
  2287. nsresult rv = mView->GetParentIndex(mSlots->mDropRow, &parentIndex);
  2288. while (NS_SUCCEEDED(rv) && parentIndex >= 0) {
  2289. mSlots->mArray.RemoveElement(parentIndex);
  2290. rv = mView->GetParentIndex(parentIndex, &parentIndex);
  2291. }
  2292. NS_ASSERTION(aEvent->mClass == eDragEventClass, "wrong event type");
  2293. WidgetDragEvent* dragEvent = aEvent->AsDragEvent();
  2294. nsContentUtils::SetDataTransferInEvent(dragEvent);
  2295. mView->Drop(mSlots->mDropRow, mSlots->mDropOrient,
  2296. dragEvent->mDataTransfer);
  2297. mSlots->mDropRow = -1;
  2298. mSlots->mDropOrient = -1;
  2299. mSlots->mIsDragging = false;
  2300. *aEventStatus = nsEventStatus_eConsumeNoDefault; // already handled the drop
  2301. } else if (aEvent->mMessage == eDragExit) {
  2302. // this event was meant for another frame, so ignore it
  2303. if (!mSlots)
  2304. return NS_OK;
  2305. // Clear out all our tracking vars.
  2306. if (mSlots->mDropAllowed) {
  2307. mSlots->mDropAllowed = false;
  2308. InvalidateDropFeedback(mSlots->mDropRow, mSlots->mDropOrient);
  2309. }
  2310. else
  2311. mSlots->mDropAllowed = false;
  2312. mSlots->mIsDragging = false;
  2313. mSlots->mScrollLines = 0;
  2314. // If a drop is occuring, the exit event will fire just before the drop
  2315. // event, so don't reset mDropRow or mDropOrient as these fields are used
  2316. // by the drop event.
  2317. if (mSlots->mTimer) {
  2318. mSlots->mTimer->Cancel();
  2319. mSlots->mTimer = nullptr;
  2320. }
  2321. if (!mSlots->mArray.IsEmpty()) {
  2322. // Close all spring loaded folders except the drop folder.
  2323. CreateTimer(LookAndFeel::eIntID_TreeCloseDelay,
  2324. CloseCallback, nsITimer::TYPE_ONE_SHOT,
  2325. getter_AddRefs(mSlots->mTimer));
  2326. }
  2327. }
  2328. return NS_OK;
  2329. }
  2330. class nsDisplayTreeBody final : public nsDisplayItem {
  2331. public:
  2332. nsDisplayTreeBody(nsDisplayListBuilder* aBuilder, nsFrame* aFrame) :
  2333. nsDisplayItem(aBuilder, aFrame),
  2334. mDisableSubpixelAA(false) {
  2335. MOZ_COUNT_CTOR(nsDisplayTreeBody);
  2336. }
  2337. #ifdef NS_BUILD_REFCNT_LOGGING
  2338. virtual ~nsDisplayTreeBody() {
  2339. MOZ_COUNT_DTOR(nsDisplayTreeBody);
  2340. }
  2341. #endif
  2342. nsDisplayItemGeometry* AllocateGeometry(nsDisplayListBuilder* aBuilder) override
  2343. {
  2344. return new nsDisplayItemGenericImageGeometry(this, aBuilder);
  2345. }
  2346. void ComputeInvalidationRegion(nsDisplayListBuilder* aBuilder,
  2347. const nsDisplayItemGeometry* aGeometry,
  2348. nsRegion *aInvalidRegion) override
  2349. {
  2350. auto geometry =
  2351. static_cast<const nsDisplayItemGenericImageGeometry*>(aGeometry);
  2352. if (aBuilder->ShouldSyncDecodeImages() &&
  2353. geometry->ShouldInvalidateToSyncDecodeImages()) {
  2354. bool snap;
  2355. aInvalidRegion->Or(*aInvalidRegion, GetBounds(aBuilder, &snap));
  2356. }
  2357. nsDisplayItem::ComputeInvalidationRegion(aBuilder, aGeometry, aInvalidRegion);
  2358. }
  2359. virtual void Paint(nsDisplayListBuilder* aBuilder,
  2360. nsRenderingContext* aCtx) override
  2361. {
  2362. DrawTargetAutoDisableSubpixelAntialiasing disable(aCtx->GetDrawTarget(),
  2363. mDisableSubpixelAA);
  2364. DrawResult result = static_cast<nsTreeBodyFrame*>(mFrame)
  2365. ->PaintTreeBody(*aCtx, mVisibleRect, ToReferenceFrame());
  2366. nsDisplayItemGenericImageGeometry::UpdateDrawResult(this, result);
  2367. }
  2368. NS_DISPLAY_DECL_NAME("XULTreeBody", TYPE_XUL_TREE_BODY)
  2369. virtual nsRect GetComponentAlphaBounds(nsDisplayListBuilder* aBuilder) override
  2370. {
  2371. bool snap;
  2372. return GetBounds(aBuilder, &snap);
  2373. }
  2374. virtual void DisableComponentAlpha() override {
  2375. mDisableSubpixelAA = true;
  2376. }
  2377. bool mDisableSubpixelAA;
  2378. };
  2379. // Painting routines
  2380. void
  2381. nsTreeBodyFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
  2382. const nsDisplayListSet& aLists)
  2383. {
  2384. // REVIEW: why did we paint if we were collapsed? that makes no sense!
  2385. if (!IsVisibleForPainting(aBuilder))
  2386. return; // We're invisible. Don't paint.
  2387. // Handles painting our background, border, and outline.
  2388. nsLeafBoxFrame::BuildDisplayList(aBuilder, aLists);
  2389. // Bail out now if there's no view or we can't run script because the
  2390. // document is a zombie
  2391. if (!mView || !GetContent ()->GetComposedDoc()->GetWindow())
  2392. return;
  2393. aLists.Content()->AppendNewToTop(new (aBuilder)
  2394. nsDisplayTreeBody(aBuilder, this));
  2395. }
  2396. DrawResult
  2397. nsTreeBodyFrame::PaintTreeBody(nsRenderingContext& aRenderingContext,
  2398. const nsRect& aDirtyRect, nsPoint aPt)
  2399. {
  2400. // Update our available height and our page count.
  2401. CalcInnerBox();
  2402. DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
  2403. gfxContext* gfx = aRenderingContext.ThebesContext();
  2404. gfx->Save();
  2405. gfx->Clip(NSRectToSnappedRect(mInnerBox + aPt,
  2406. PresContext()->AppUnitsPerDevPixel(),
  2407. *drawTarget));
  2408. int32_t oldPageCount = mPageLength;
  2409. if (!mHasFixedRowCount)
  2410. mPageLength = mInnerBox.height/mRowHeight;
  2411. if (oldPageCount != mPageLength || mHorzWidth != CalcHorzWidth(GetScrollParts())) {
  2412. // Schedule a ResizeReflow that will update our info properly.
  2413. PresContext()->PresShell()->
  2414. FrameNeedsReflow(this, nsIPresShell::eResize, NS_FRAME_IS_DIRTY);
  2415. }
  2416. #ifdef DEBUG
  2417. int32_t rowCount = mRowCount;
  2418. mView->GetRowCount(&rowCount);
  2419. NS_WARNING_ASSERTION(mRowCount == rowCount, "row count changed unexpectedly");
  2420. #endif
  2421. DrawResult result = DrawResult::SUCCESS;
  2422. // Loop through our columns and paint them (e.g., for sorting). This is only
  2423. // relevant when painting backgrounds, since columns contain no content. Content
  2424. // is contained in the rows.
  2425. for (nsTreeColumn* currCol = mColumns->GetFirstColumn(); currCol;
  2426. currCol = currCol->GetNext()) {
  2427. nsRect colRect;
  2428. nsresult rv = currCol->GetRect(this, mInnerBox.y, mInnerBox.height,
  2429. &colRect);
  2430. // Don't paint hidden columns.
  2431. if (NS_FAILED(rv) || colRect.width == 0) continue;
  2432. if (OffsetForHorzScroll(colRect, false)) {
  2433. nsRect dirtyRect;
  2434. colRect += aPt;
  2435. if (dirtyRect.IntersectRect(aDirtyRect, colRect)) {
  2436. result &=
  2437. PaintColumn(currCol, colRect, PresContext(), aRenderingContext, aDirtyRect);
  2438. }
  2439. }
  2440. }
  2441. // Loop through our on-screen rows.
  2442. for (int32_t i = mTopRowIndex; i < mRowCount && i <= mTopRowIndex+mPageLength; i++) {
  2443. nsRect rowRect(mInnerBox.x, mInnerBox.y+mRowHeight*(i-mTopRowIndex), mInnerBox.width, mRowHeight);
  2444. nsRect dirtyRect;
  2445. if (dirtyRect.IntersectRect(aDirtyRect, rowRect + aPt) &&
  2446. rowRect.y < (mInnerBox.y+mInnerBox.height)) {
  2447. result &=
  2448. PaintRow(i, rowRect + aPt, PresContext(), aRenderingContext, aDirtyRect, aPt);
  2449. }
  2450. }
  2451. if (mSlots && mSlots->mDropAllowed && (mSlots->mDropOrient == nsITreeView::DROP_BEFORE ||
  2452. mSlots->mDropOrient == nsITreeView::DROP_AFTER)) {
  2453. nscoord yPos = mInnerBox.y + mRowHeight * (mSlots->mDropRow - mTopRowIndex) - mRowHeight / 2;
  2454. nsRect feedbackRect(mInnerBox.x, yPos, mInnerBox.width, mRowHeight);
  2455. if (mSlots->mDropOrient == nsITreeView::DROP_AFTER)
  2456. feedbackRect.y += mRowHeight;
  2457. nsRect dirtyRect;
  2458. feedbackRect += aPt;
  2459. if (dirtyRect.IntersectRect(aDirtyRect, feedbackRect)) {
  2460. result &=
  2461. PaintDropFeedback(feedbackRect, PresContext(), aRenderingContext,
  2462. aDirtyRect, aPt);
  2463. }
  2464. }
  2465. gfx->Restore();
  2466. return result;
  2467. }
  2468. DrawResult
  2469. nsTreeBodyFrame::PaintColumn(nsTreeColumn* aColumn,
  2470. const nsRect& aColumnRect,
  2471. nsPresContext* aPresContext,
  2472. nsRenderingContext& aRenderingContext,
  2473. const nsRect& aDirtyRect)
  2474. {
  2475. NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
  2476. // Now obtain the properties for our cell.
  2477. PrefillPropertyArray(-1, aColumn);
  2478. nsAutoString properties;
  2479. mView->GetColumnProperties(aColumn, properties);
  2480. nsTreeUtils::TokenizeProperties(properties, mScratchArray);
  2481. // Resolve style for the column. It contains all the info we need to lay ourselves
  2482. // out and to paint.
  2483. nsStyleContext* colContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecolumn);
  2484. // Obtain the margins for the cell and then deflate our rect by that
  2485. // amount. The cell is assumed to be contained within the deflated rect.
  2486. nsRect colRect(aColumnRect);
  2487. nsMargin colMargin;
  2488. colContext->StyleMargin()->GetMargin(colMargin);
  2489. colRect.Deflate(colMargin);
  2490. return PaintBackgroundLayer(colContext, aPresContext, aRenderingContext,
  2491. colRect, aDirtyRect);
  2492. }
  2493. DrawResult
  2494. nsTreeBodyFrame::PaintRow(int32_t aRowIndex,
  2495. const nsRect& aRowRect,
  2496. nsPresContext* aPresContext,
  2497. nsRenderingContext& aRenderingContext,
  2498. const nsRect& aDirtyRect,
  2499. nsPoint aPt)
  2500. {
  2501. // We have been given a rect for our row. We treat this row like a full-blown
  2502. // frame, meaning that it can have borders, margins, padding, and a background.
  2503. // Without a view, we have no data. Check for this up front.
  2504. if (!mView) {
  2505. return DrawResult::SUCCESS;
  2506. }
  2507. nsresult rv;
  2508. // Now obtain the properties for our row.
  2509. // XXX Automatically fill in the following props: open, closed, container, leaf, selected, focused
  2510. PrefillPropertyArray(aRowIndex, nullptr);
  2511. nsAutoString properties;
  2512. mView->GetRowProperties(aRowIndex, properties);
  2513. nsTreeUtils::TokenizeProperties(properties, mScratchArray);
  2514. // Resolve style for the row. It contains all the info we need to lay ourselves
  2515. // out and to paint.
  2516. nsStyleContext* rowContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreerow);
  2517. // Obtain the margins for the row and then deflate our rect by that
  2518. // amount. The row is assumed to be contained within the deflated rect.
  2519. nsRect rowRect(aRowRect);
  2520. nsMargin rowMargin;
  2521. rowContext->StyleMargin()->GetMargin(rowMargin);
  2522. rowRect.Deflate(rowMargin);
  2523. DrawResult result = DrawResult::SUCCESS;
  2524. // Paint our borders and background for our row rect.
  2525. nsITheme* theme = nullptr;
  2526. auto appearance = rowContext->StyleDisplay()->mAppearance;
  2527. if (appearance) {
  2528. theme = aPresContext->GetTheme();
  2529. }
  2530. gfxContext* ctx = aRenderingContext.ThebesContext();
  2531. // Save the current font smoothing background color in case we change it.
  2532. Color originalColor(ctx->GetFontSmoothingBackgroundColor());
  2533. if (theme && theme->ThemeSupportsWidget(aPresContext, nullptr, appearance)) {
  2534. nscolor color;
  2535. if (theme->WidgetProvidesFontSmoothingBackgroundColor(this, appearance,
  2536. &color)) {
  2537. // Set the font smoothing background color provided by the widget.
  2538. ctx->SetFontSmoothingBackgroundColor(ToDeviceColor(color));
  2539. }
  2540. nsRect dirty;
  2541. dirty.IntersectRect(rowRect, aDirtyRect);
  2542. theme->DrawWidgetBackground(&aRenderingContext, this, appearance, rowRect,
  2543. dirty);
  2544. } else {
  2545. result &= PaintBackgroundLayer(rowContext, aPresContext, aRenderingContext,
  2546. rowRect, aDirtyRect);
  2547. }
  2548. // Adjust the rect for its border and padding.
  2549. nsRect originalRowRect = rowRect;
  2550. AdjustForBorderPadding(rowContext, rowRect);
  2551. bool isSeparator = false;
  2552. mView->IsSeparator(aRowIndex, &isSeparator);
  2553. if (isSeparator) {
  2554. // The row is a separator.
  2555. nscoord primaryX = rowRect.x;
  2556. nsTreeColumn* primaryCol = mColumns->GetPrimaryColumn();
  2557. if (primaryCol) {
  2558. // Paint the primary cell.
  2559. nsRect cellRect;
  2560. rv = primaryCol->GetRect(this, rowRect.y, rowRect.height, &cellRect);
  2561. if (NS_FAILED(rv)) {
  2562. NS_NOTREACHED("primary column is invalid");
  2563. return result;
  2564. }
  2565. if (OffsetForHorzScroll(cellRect, false)) {
  2566. cellRect.x += aPt.x;
  2567. nsRect dirtyRect;
  2568. nsRect checkRect(cellRect.x, originalRowRect.y,
  2569. cellRect.width, originalRowRect.height);
  2570. if (dirtyRect.IntersectRect(aDirtyRect, checkRect)) {
  2571. result &= PaintCell(aRowIndex, primaryCol, cellRect, aPresContext,
  2572. aRenderingContext, aDirtyRect, primaryX, aPt);
  2573. }
  2574. }
  2575. // Paint the left side of the separator.
  2576. nscoord currX;
  2577. nsTreeColumn* previousCol = primaryCol->GetPrevious();
  2578. if (previousCol) {
  2579. nsRect prevColRect;
  2580. rv = previousCol->GetRect(this, 0, 0, &prevColRect);
  2581. if (NS_SUCCEEDED(rv)) {
  2582. currX = (prevColRect.x - mHorzPosition) + prevColRect.width + aPt.x;
  2583. } else {
  2584. NS_NOTREACHED("The column before the primary column is invalid");
  2585. currX = rowRect.x;
  2586. }
  2587. } else {
  2588. currX = rowRect.x;
  2589. }
  2590. int32_t level;
  2591. mView->GetLevel(aRowIndex, &level);
  2592. if (level == 0)
  2593. currX += mIndentation;
  2594. if (currX > rowRect.x) {
  2595. nsRect separatorRect(rowRect);
  2596. separatorRect.width -= rowRect.x + rowRect.width - currX;
  2597. result &= PaintSeparator(aRowIndex, separatorRect, aPresContext,
  2598. aRenderingContext, aDirtyRect);
  2599. }
  2600. }
  2601. // Paint the right side (whole) separator.
  2602. nsRect separatorRect(rowRect);
  2603. if (primaryX > rowRect.x) {
  2604. separatorRect.width -= primaryX - rowRect.x;
  2605. separatorRect.x += primaryX - rowRect.x;
  2606. }
  2607. result &= PaintSeparator(aRowIndex, separatorRect, aPresContext,
  2608. aRenderingContext, aDirtyRect);
  2609. }
  2610. else {
  2611. // Now loop over our cells. Only paint a cell if it intersects with our dirty rect.
  2612. for (nsTreeColumn* currCol = mColumns->GetFirstColumn(); currCol;
  2613. currCol = currCol->GetNext()) {
  2614. nsRect cellRect;
  2615. rv = currCol->GetRect(this, rowRect.y, rowRect.height, &cellRect);
  2616. // Don't paint cells in hidden columns.
  2617. if (NS_FAILED(rv) || cellRect.width == 0)
  2618. continue;
  2619. if (OffsetForHorzScroll(cellRect, false)) {
  2620. cellRect.x += aPt.x;
  2621. // for primary columns, use the row's vertical size so that the
  2622. // lines get drawn properly
  2623. nsRect checkRect = cellRect;
  2624. if (currCol->IsPrimary())
  2625. checkRect = nsRect(cellRect.x, originalRowRect.y,
  2626. cellRect.width, originalRowRect.height);
  2627. nsRect dirtyRect;
  2628. nscoord dummy;
  2629. if (dirtyRect.IntersectRect(aDirtyRect, checkRect))
  2630. result &= PaintCell(aRowIndex, currCol, cellRect, aPresContext,
  2631. aRenderingContext, aDirtyRect, dummy, aPt);
  2632. }
  2633. }
  2634. }
  2635. // If we've changed the font smoothing background color for this row, restore
  2636. // the color to the original one.
  2637. if (originalColor != ctx->GetFontSmoothingBackgroundColor()) {
  2638. ctx->SetFontSmoothingBackgroundColor(originalColor);
  2639. }
  2640. return result;
  2641. }
  2642. DrawResult
  2643. nsTreeBodyFrame::PaintSeparator(int32_t aRowIndex,
  2644. const nsRect& aSeparatorRect,
  2645. nsPresContext* aPresContext,
  2646. nsRenderingContext& aRenderingContext,
  2647. const nsRect& aDirtyRect)
  2648. {
  2649. // Resolve style for the separator.
  2650. nsStyleContext* separatorContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeseparator);
  2651. bool useTheme = false;
  2652. nsITheme *theme = nullptr;
  2653. const nsStyleDisplay* displayData = separatorContext->StyleDisplay();
  2654. if ( displayData->mAppearance ) {
  2655. theme = aPresContext->GetTheme();
  2656. if (theme && theme->ThemeSupportsWidget(aPresContext, nullptr, displayData->mAppearance))
  2657. useTheme = true;
  2658. }
  2659. DrawResult result = DrawResult::SUCCESS;
  2660. // use -moz-appearance if provided.
  2661. if (useTheme) {
  2662. nsRect dirty;
  2663. dirty.IntersectRect(aSeparatorRect, aDirtyRect);
  2664. theme->DrawWidgetBackground(&aRenderingContext, this,
  2665. displayData->mAppearance, aSeparatorRect, dirty);
  2666. }
  2667. else {
  2668. const nsStylePosition* stylePosition = separatorContext->StylePosition();
  2669. // Obtain the height for the separator or use the default value.
  2670. nscoord height;
  2671. if (stylePosition->mHeight.GetUnit() == eStyleUnit_Coord)
  2672. height = stylePosition->mHeight.GetCoordValue();
  2673. else {
  2674. // Use default height 2px.
  2675. height = nsPresContext::CSSPixelsToAppUnits(2);
  2676. }
  2677. // Obtain the margins for the separator and then deflate our rect by that
  2678. // amount. The separator is assumed to be contained within the deflated rect.
  2679. nsRect separatorRect(aSeparatorRect.x, aSeparatorRect.y, aSeparatorRect.width, height);
  2680. nsMargin separatorMargin;
  2681. separatorContext->StyleMargin()->GetMargin(separatorMargin);
  2682. separatorRect.Deflate(separatorMargin);
  2683. // Center the separator.
  2684. separatorRect.y += (aSeparatorRect.height - height) / 2;
  2685. result &= PaintBackgroundLayer(separatorContext, aPresContext,
  2686. aRenderingContext, separatorRect,
  2687. aDirtyRect);
  2688. }
  2689. return result;
  2690. }
  2691. DrawResult
  2692. nsTreeBodyFrame::PaintCell(int32_t aRowIndex,
  2693. nsTreeColumn* aColumn,
  2694. const nsRect& aCellRect,
  2695. nsPresContext* aPresContext,
  2696. nsRenderingContext& aRenderingContext,
  2697. const nsRect& aDirtyRect,
  2698. nscoord& aCurrX,
  2699. nsPoint aPt)
  2700. {
  2701. NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
  2702. // Now obtain the properties for our cell.
  2703. // XXX Automatically fill in the following props: open, closed, container, leaf, selected, focused, and the col ID.
  2704. PrefillPropertyArray(aRowIndex, aColumn);
  2705. nsAutoString properties;
  2706. mView->GetCellProperties(aRowIndex, aColumn, properties);
  2707. nsTreeUtils::TokenizeProperties(properties, mScratchArray);
  2708. // Resolve style for the cell. It contains all the info we need to lay ourselves
  2709. // out and to paint.
  2710. nsStyleContext* cellContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecell);
  2711. bool isRTL = StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
  2712. // Obtain the margins for the cell and then deflate our rect by that
  2713. // amount. The cell is assumed to be contained within the deflated rect.
  2714. nsRect cellRect(aCellRect);
  2715. nsMargin cellMargin;
  2716. cellContext->StyleMargin()->GetMargin(cellMargin);
  2717. cellRect.Deflate(cellMargin);
  2718. // Paint our borders and background for our row rect.
  2719. DrawResult result = PaintBackgroundLayer(cellContext, aPresContext,
  2720. aRenderingContext, cellRect,
  2721. aDirtyRect);
  2722. // Adjust the rect for its border and padding.
  2723. AdjustForBorderPadding(cellContext, cellRect);
  2724. nscoord currX = cellRect.x;
  2725. nscoord remainingWidth = cellRect.width;
  2726. // Now we paint the contents of the cells.
  2727. // Directionality of the tree determines the order in which we paint.
  2728. // NS_STYLE_DIRECTION_LTR means paint from left to right.
  2729. // NS_STYLE_DIRECTION_RTL means paint from right to left.
  2730. if (aColumn->IsPrimary()) {
  2731. // If we're the primary column, we need to indent and paint the twisty and any connecting lines
  2732. // between siblings.
  2733. int32_t level;
  2734. mView->GetLevel(aRowIndex, &level);
  2735. if (!isRTL)
  2736. currX += mIndentation * level;
  2737. remainingWidth -= mIndentation * level;
  2738. // Resolve the style to use for the connecting lines.
  2739. nsStyleContext* lineContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeline);
  2740. if (mIndentation && level &&
  2741. lineContext->StyleVisibility()->IsVisibleOrCollapsed()) {
  2742. // Paint the thread lines.
  2743. // Get the size of the twisty. We don't want to paint the twisty
  2744. // before painting of connecting lines since it would paint lines over
  2745. // the twisty. But we need to leave a place for it.
  2746. nsStyleContext* twistyContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreetwisty);
  2747. nsRect imageSize;
  2748. nsRect twistyRect(aCellRect);
  2749. GetTwistyRect(aRowIndex, aColumn, imageSize, twistyRect, aPresContext,
  2750. twistyContext);
  2751. nsMargin twistyMargin;
  2752. twistyContext->StyleMargin()->GetMargin(twistyMargin);
  2753. twistyRect.Inflate(twistyMargin);
  2754. aRenderingContext.ThebesContext()->Save();
  2755. const nsStyleBorder* borderStyle = lineContext->StyleBorder();
  2756. // Resolve currentcolor values against the treeline context
  2757. nscolor color = lineContext->StyleColor()->
  2758. CalcComplexColor(borderStyle->mBorderLeftColor);
  2759. ColorPattern colorPatt(ToDeviceColor(color));
  2760. uint8_t style = borderStyle->GetBorderStyle(NS_SIDE_LEFT);
  2761. StrokeOptions strokeOptions;
  2762. nsLayoutUtils::InitDashPattern(strokeOptions, style);
  2763. nscoord srcX = currX + twistyRect.width - mIndentation / 2;
  2764. nscoord lineY = (aRowIndex - mTopRowIndex) * mRowHeight + aPt.y;
  2765. DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
  2766. nsPresContext* pc = PresContext();
  2767. // Don't paint off our cell.
  2768. if (srcX <= cellRect.x + cellRect.width) {
  2769. nscoord destX = currX + twistyRect.width;
  2770. if (destX > cellRect.x + cellRect.width)
  2771. destX = cellRect.x + cellRect.width;
  2772. if (isRTL) {
  2773. srcX = currX + remainingWidth - (srcX - cellRect.x);
  2774. destX = currX + remainingWidth - (destX - cellRect.x);
  2775. }
  2776. Point p1(pc->AppUnitsToGfxUnits(srcX),
  2777. pc->AppUnitsToGfxUnits(lineY + mRowHeight / 2));
  2778. Point p2(pc->AppUnitsToGfxUnits(destX),
  2779. pc->AppUnitsToGfxUnits(lineY + mRowHeight / 2));
  2780. SnapLineToDevicePixelsForStroking(p1, p2, *drawTarget,
  2781. strokeOptions.mLineWidth);
  2782. drawTarget->StrokeLine(p1, p2, colorPatt, strokeOptions);
  2783. }
  2784. int32_t currentParent = aRowIndex;
  2785. for (int32_t i = level; i > 0; i--) {
  2786. if (srcX <= cellRect.x + cellRect.width) {
  2787. // Paint full vertical line only if we have next sibling.
  2788. bool hasNextSibling;
  2789. mView->HasNextSibling(currentParent, aRowIndex, &hasNextSibling);
  2790. if (hasNextSibling || i == level) {
  2791. Point p1(pc->AppUnitsToGfxUnits(srcX),
  2792. pc->AppUnitsToGfxUnits(lineY));
  2793. Point p2;
  2794. p2.x = pc->AppUnitsToGfxUnits(srcX);
  2795. if (hasNextSibling)
  2796. p2.y = pc->AppUnitsToGfxUnits(lineY + mRowHeight);
  2797. else if (i == level)
  2798. p2.y = pc->AppUnitsToGfxUnits(lineY + mRowHeight / 2);
  2799. SnapLineToDevicePixelsForStroking(p1, p2, *drawTarget,
  2800. strokeOptions.mLineWidth);
  2801. drawTarget->StrokeLine(p1, p2, colorPatt, strokeOptions);
  2802. }
  2803. }
  2804. int32_t parent;
  2805. if (NS_FAILED(mView->GetParentIndex(currentParent, &parent)) || parent < 0)
  2806. break;
  2807. currentParent = parent;
  2808. srcX -= mIndentation;
  2809. }
  2810. aRenderingContext.ThebesContext()->Restore();
  2811. }
  2812. // Always leave space for the twisty.
  2813. nsRect twistyRect(currX, cellRect.y, remainingWidth, cellRect.height);
  2814. result &= PaintTwisty(aRowIndex, aColumn, twistyRect, aPresContext,
  2815. aRenderingContext, aDirtyRect, remainingWidth,
  2816. currX);
  2817. }
  2818. // Now paint the icon for our cell.
  2819. nsRect iconRect(currX, cellRect.y, remainingWidth, cellRect.height);
  2820. nsRect dirtyRect;
  2821. if (dirtyRect.IntersectRect(aDirtyRect, iconRect)) {
  2822. result &= PaintImage(aRowIndex, aColumn, iconRect, aPresContext,
  2823. aRenderingContext, aDirtyRect, remainingWidth,
  2824. currX);
  2825. }
  2826. // Now paint our element, but only if we aren't a cycler column.
  2827. // XXX until we have the ability to load images, allow the view to
  2828. // insert text into cycler columns...
  2829. if (!aColumn->IsCycler()) {
  2830. nsRect elementRect(currX, cellRect.y, remainingWidth, cellRect.height);
  2831. nsRect dirtyRect;
  2832. if (dirtyRect.IntersectRect(aDirtyRect, elementRect)) {
  2833. switch (aColumn->GetType()) {
  2834. case nsITreeColumn::TYPE_TEXT:
  2835. case nsITreeColumn::TYPE_PASSWORD:
  2836. result &= PaintText(aRowIndex, aColumn, elementRect, aPresContext,
  2837. aRenderingContext, aDirtyRect, currX);
  2838. break;
  2839. case nsITreeColumn::TYPE_CHECKBOX:
  2840. result &= PaintCheckbox(aRowIndex, aColumn, elementRect, aPresContext,
  2841. aRenderingContext, aDirtyRect);
  2842. break;
  2843. case nsITreeColumn::TYPE_PROGRESSMETER:
  2844. int32_t state;
  2845. mView->GetProgressMode(aRowIndex, aColumn, &state);
  2846. switch (state) {
  2847. case nsITreeView::PROGRESS_NORMAL:
  2848. case nsITreeView::PROGRESS_UNDETERMINED:
  2849. result &= PaintProgressMeter(aRowIndex, aColumn, elementRect,
  2850. aPresContext, aRenderingContext,
  2851. aDirtyRect);
  2852. break;
  2853. case nsITreeView::PROGRESS_NONE:
  2854. default:
  2855. result &= PaintText(aRowIndex, aColumn, elementRect, aPresContext,
  2856. aRenderingContext, aDirtyRect, currX);
  2857. break;
  2858. }
  2859. break;
  2860. }
  2861. }
  2862. }
  2863. aCurrX = currX;
  2864. return result;
  2865. }
  2866. DrawResult
  2867. nsTreeBodyFrame::PaintTwisty(int32_t aRowIndex,
  2868. nsTreeColumn* aColumn,
  2869. const nsRect& aTwistyRect,
  2870. nsPresContext* aPresContext,
  2871. nsRenderingContext& aRenderingContext,
  2872. const nsRect& aDirtyRect,
  2873. nscoord& aRemainingWidth,
  2874. nscoord& aCurrX)
  2875. {
  2876. NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
  2877. bool isRTL = StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
  2878. nscoord rightEdge = aCurrX + aRemainingWidth;
  2879. // Paint the twisty, but only if we are a non-empty container.
  2880. bool shouldPaint = false;
  2881. bool isContainer = false;
  2882. mView->IsContainer(aRowIndex, &isContainer);
  2883. if (isContainer) {
  2884. bool isContainerEmpty = false;
  2885. mView->IsContainerEmpty(aRowIndex, &isContainerEmpty);
  2886. if (!isContainerEmpty)
  2887. shouldPaint = true;
  2888. }
  2889. // Resolve style for the twisty.
  2890. nsStyleContext* twistyContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreetwisty);
  2891. // Obtain the margins for the twisty and then deflate our rect by that
  2892. // amount. The twisty is assumed to be contained within the deflated rect.
  2893. nsRect twistyRect(aTwistyRect);
  2894. nsMargin twistyMargin;
  2895. twistyContext->StyleMargin()->GetMargin(twistyMargin);
  2896. twistyRect.Deflate(twistyMargin);
  2897. nsRect imageSize;
  2898. nsITheme* theme = GetTwistyRect(aRowIndex, aColumn, imageSize, twistyRect,
  2899. aPresContext, twistyContext);
  2900. // Subtract out the remaining width. This is done even when we don't actually paint a twisty in
  2901. // this cell, so that cells in different rows still line up.
  2902. nsRect copyRect(twistyRect);
  2903. copyRect.Inflate(twistyMargin);
  2904. aRemainingWidth -= copyRect.width;
  2905. if (!isRTL)
  2906. aCurrX += copyRect.width;
  2907. DrawResult result = DrawResult::SUCCESS;
  2908. if (shouldPaint) {
  2909. // Paint our borders and background for our image rect.
  2910. result &= PaintBackgroundLayer(twistyContext, aPresContext,
  2911. aRenderingContext, twistyRect,
  2912. aDirtyRect);
  2913. if (theme) {
  2914. if (isRTL)
  2915. twistyRect.x = rightEdge - twistyRect.width;
  2916. // yeah, I know it says we're drawing a background, but a twisty is really a fg
  2917. // object since it doesn't have anything that gecko would want to draw over it. Besides,
  2918. // we have to prevent imagelib from drawing it.
  2919. nsRect dirty;
  2920. dirty.IntersectRect(twistyRect, aDirtyRect);
  2921. theme->DrawWidgetBackground(&aRenderingContext, this,
  2922. twistyContext->StyleDisplay()->mAppearance, twistyRect, dirty);
  2923. }
  2924. else {
  2925. // Time to paint the twisty.
  2926. // Adjust the rect for its border and padding.
  2927. nsMargin bp(0,0,0,0);
  2928. GetBorderPadding(twistyContext, bp);
  2929. twistyRect.Deflate(bp);
  2930. if (isRTL)
  2931. twistyRect.x = rightEdge - twistyRect.width;
  2932. imageSize.Deflate(bp);
  2933. // Get the image for drawing.
  2934. nsCOMPtr<imgIContainer> image;
  2935. bool useImageRegion = true;
  2936. GetImage(aRowIndex, aColumn, true, twistyContext, useImageRegion, getter_AddRefs(image));
  2937. if (image) {
  2938. nsPoint pt = twistyRect.TopLeft();
  2939. // Center the image. XXX Obey vertical-align style prop?
  2940. if (imageSize.height < twistyRect.height) {
  2941. pt.y += (twistyRect.height - imageSize.height)/2;
  2942. }
  2943. // Paint the image.
  2944. result &=
  2945. nsLayoutUtils::DrawSingleUnscaledImage(
  2946. *aRenderingContext.ThebesContext(), aPresContext, image,
  2947. SamplingFilter::POINT, pt, &aDirtyRect,
  2948. imgIContainer::FLAG_NONE, &imageSize);
  2949. }
  2950. }
  2951. }
  2952. return result;
  2953. }
  2954. DrawResult
  2955. nsTreeBodyFrame::PaintImage(int32_t aRowIndex,
  2956. nsTreeColumn* aColumn,
  2957. const nsRect& aImageRect,
  2958. nsPresContext* aPresContext,
  2959. nsRenderingContext& aRenderingContext,
  2960. const nsRect& aDirtyRect,
  2961. nscoord& aRemainingWidth,
  2962. nscoord& aCurrX)
  2963. {
  2964. NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
  2965. bool isRTL = StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
  2966. nscoord rightEdge = aCurrX + aRemainingWidth;
  2967. // Resolve style for the image.
  2968. nsStyleContext* imageContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeimage);
  2969. // Obtain opacity value for the image.
  2970. float opacity = imageContext->StyleEffects()->mOpacity;
  2971. // Obtain the margins for the image and then deflate our rect by that
  2972. // amount. The image is assumed to be contained within the deflated rect.
  2973. nsRect imageRect(aImageRect);
  2974. nsMargin imageMargin;
  2975. imageContext->StyleMargin()->GetMargin(imageMargin);
  2976. imageRect.Deflate(imageMargin);
  2977. // Get the image.
  2978. bool useImageRegion = true;
  2979. nsCOMPtr<imgIContainer> image;
  2980. GetImage(aRowIndex, aColumn, false, imageContext, useImageRegion, getter_AddRefs(image));
  2981. // Get the image destination size.
  2982. nsSize imageDestSize = GetImageDestSize(imageContext, useImageRegion, image);
  2983. if (!imageDestSize.width || !imageDestSize.height) {
  2984. return DrawResult::SUCCESS;
  2985. }
  2986. // Get the borders and padding.
  2987. nsMargin bp(0,0,0,0);
  2988. GetBorderPadding(imageContext, bp);
  2989. // destRect will be passed as the aDestRect argument in the DrawImage method.
  2990. // Start with the imageDestSize width and height.
  2991. nsRect destRect(0, 0, imageDestSize.width, imageDestSize.height);
  2992. // Inflate destRect for borders and padding so that we can compare/adjust
  2993. // with respect to imageRect.
  2994. destRect.Inflate(bp);
  2995. // The destRect width and height have not been adjusted to fit within the
  2996. // cell width and height.
  2997. // We must adjust the width even if image is null, because the width is used
  2998. // to update the aRemainingWidth and aCurrX values.
  2999. // Since the height isn't used unless the image is not null, we will adjust
  3000. // the height inside the if (image) block below.
  3001. if (destRect.width > imageRect.width) {
  3002. // The destRect is too wide to fit within the cell width.
  3003. // Adjust destRect width to fit within the cell width.
  3004. destRect.width = imageRect.width;
  3005. }
  3006. else {
  3007. // The cell is wider than the destRect.
  3008. // In a cycler column, the image is centered horizontally.
  3009. if (!aColumn->IsCycler()) {
  3010. // If this column is not a cycler, we won't center the image horizontally.
  3011. // We adjust the imageRect width so that the image is placed at the start
  3012. // of the cell.
  3013. imageRect.width = destRect.width;
  3014. }
  3015. }
  3016. DrawResult result = DrawResult::SUCCESS;
  3017. if (image) {
  3018. if (isRTL)
  3019. imageRect.x = rightEdge - imageRect.width;
  3020. // Paint our borders and background for our image rect
  3021. result &= PaintBackgroundLayer(imageContext, aPresContext,
  3022. aRenderingContext, imageRect,
  3023. aDirtyRect);
  3024. // The destRect x and y have not been set yet. Let's do that now.
  3025. // Initially, we use the imageRect x and y.
  3026. destRect.x = imageRect.x;
  3027. destRect.y = imageRect.y;
  3028. if (destRect.width < imageRect.width) {
  3029. // The destRect width is smaller than the cell width.
  3030. // Center the image horizontally in the cell.
  3031. // Adjust the destRect x accordingly.
  3032. destRect.x += (imageRect.width - destRect.width)/2;
  3033. }
  3034. // Now it's time to adjust the destRect height to fit within the cell height.
  3035. if (destRect.height > imageRect.height) {
  3036. // The destRect height is larger than the cell height.
  3037. // Adjust destRect height to fit within the cell height.
  3038. destRect.height = imageRect.height;
  3039. }
  3040. else if (destRect.height < imageRect.height) {
  3041. // The destRect height is smaller than the cell height.
  3042. // Center the image vertically in the cell.
  3043. // Adjust the destRect y accordingly.
  3044. destRect.y += (imageRect.height - destRect.height)/2;
  3045. }
  3046. // It's almost time to paint the image.
  3047. // Deflate destRect for the border and padding.
  3048. destRect.Deflate(bp);
  3049. // Compute the area where our whole image would be mapped, to get the
  3050. // desired subregion onto our actual destRect:
  3051. nsRect wholeImageDest;
  3052. CSSIntSize rawImageCSSIntSize;
  3053. if (NS_SUCCEEDED(image->GetWidth(&rawImageCSSIntSize.width)) &&
  3054. NS_SUCCEEDED(image->GetHeight(&rawImageCSSIntSize.height))) {
  3055. // Get the image source rectangle - the rectangle containing the part of
  3056. // the image that we are going to display. sourceRect will be passed as
  3057. // the aSrcRect argument in the DrawImage method.
  3058. nsRect sourceRect = GetImageSourceRect(imageContext, useImageRegion, image);
  3059. // Let's say that the image is 100 pixels tall and that the CSS has
  3060. // specified that the destination height should be 50 pixels tall. Let's
  3061. // say that the cell height is only 20 pixels. So, in those 20 visible
  3062. // pixels, we want to see the top 20/50ths of the image. So, the
  3063. // sourceRect.height should be 100 * 20 / 50, which is 40 pixels.
  3064. // Essentially, we are scaling the image as dictated by the CSS
  3065. // destination height and width, and we are then clipping the scaled
  3066. // image by the cell width and height.
  3067. nsSize rawImageSize(CSSPixel::ToAppUnits(rawImageCSSIntSize));
  3068. wholeImageDest =
  3069. nsLayoutUtils::GetWholeImageDestination(rawImageSize, sourceRect,
  3070. nsRect(destRect.TopLeft(),
  3071. imageDestSize));
  3072. } else {
  3073. // GetWidth/GetHeight failed, so we can't easily map a subregion of the
  3074. // source image onto the destination area.
  3075. // * If this happens with a RasterImage, it probably means the image is
  3076. // in an error state, and we shouldn't draw anything. Hence, we leave
  3077. // wholeImageDest as an empty rect (its initial state).
  3078. // * If this happens with a VectorImage, it probably means the image has
  3079. // no explicit width or height attribute -- but we can still proceed and
  3080. // just treat the destination area as our whole SVG image area. Hence, we
  3081. // set wholeImageDest to the full destRect.
  3082. if (image->GetType() == imgIContainer::TYPE_VECTOR) {
  3083. wholeImageDest = destRect;
  3084. }
  3085. }
  3086. gfxContext* ctx = aRenderingContext.ThebesContext();
  3087. if (opacity != 1.0f) {
  3088. ctx->PushGroupForBlendBack(gfxContentType::COLOR_ALPHA, opacity);
  3089. }
  3090. result &=
  3091. nsLayoutUtils::DrawImage(*ctx, aPresContext, image,
  3092. nsLayoutUtils::GetSamplingFilterForFrame(this),
  3093. wholeImageDest, destRect, destRect.TopLeft(), aDirtyRect,
  3094. imgIContainer::FLAG_NONE);
  3095. if (opacity != 1.0f) {
  3096. ctx->PopGroupAndBlend();
  3097. }
  3098. }
  3099. // Update the aRemainingWidth and aCurrX values.
  3100. imageRect.Inflate(imageMargin);
  3101. aRemainingWidth -= imageRect.width;
  3102. if (!isRTL) {
  3103. aCurrX += imageRect.width;
  3104. }
  3105. return result;
  3106. }
  3107. // Disable PGO for PaintText because MSVC 2015 seems to have decided
  3108. // that it can null out the alreadyAddRefed<nsFontMetrics> used to
  3109. // initialize fontMet after storing fontMet on the stack in the same
  3110. // space, overwriting fontMet's stack storage with null.
  3111. #ifdef _MSC_VER
  3112. # pragma optimize("g", off)
  3113. #endif
  3114. DrawResult
  3115. nsTreeBodyFrame::PaintText(int32_t aRowIndex,
  3116. nsTreeColumn* aColumn,
  3117. const nsRect& aTextRect,
  3118. nsPresContext* aPresContext,
  3119. nsRenderingContext& aRenderingContext,
  3120. const nsRect& aDirtyRect,
  3121. nscoord& aCurrX)
  3122. {
  3123. NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
  3124. bool isRTL = StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
  3125. // Now obtain the text for our cell.
  3126. nsAutoString text;
  3127. mView->GetCellText(aRowIndex, aColumn, text);
  3128. if (aColumn->Type() == nsITreeColumn::TYPE_PASSWORD) {
  3129. TextEditRules::FillBufWithPWChars(&text, text.Length());
  3130. }
  3131. // We're going to paint this text so we need to ensure bidi is enabled if
  3132. // necessary
  3133. CheckTextForBidi(text);
  3134. DrawResult result = DrawResult::SUCCESS;
  3135. if (text.Length() == 0) {
  3136. // Don't paint an empty string. XXX What about background/borders? Still paint?
  3137. return result;
  3138. }
  3139. int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
  3140. DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
  3141. // Resolve style for the text. It contains all the info we need to lay ourselves
  3142. // out and to paint.
  3143. nsStyleContext* textContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecelltext);
  3144. // Obtain opacity value for the image.
  3145. float opacity = textContext->StyleEffects()->mOpacity;
  3146. // Obtain the margins for the text and then deflate our rect by that
  3147. // amount. The text is assumed to be contained within the deflated rect.
  3148. nsRect textRect(aTextRect);
  3149. nsMargin textMargin;
  3150. textContext->StyleMargin()->GetMargin(textMargin);
  3151. textRect.Deflate(textMargin);
  3152. // Adjust the rect for its border and padding.
  3153. nsMargin bp(0,0,0,0);
  3154. GetBorderPadding(textContext, bp);
  3155. textRect.Deflate(bp);
  3156. // Compute our text size.
  3157. RefPtr<nsFontMetrics> fontMet =
  3158. nsLayoutUtils::GetFontMetricsForStyleContext(textContext);
  3159. nscoord height = fontMet->MaxHeight();
  3160. nscoord baseline = fontMet->MaxAscent();
  3161. // Center the text. XXX Obey vertical-align style prop?
  3162. if (height < textRect.height) {
  3163. textRect.y += (textRect.height - height)/2;
  3164. textRect.height = height;
  3165. }
  3166. // Set our font.
  3167. AdjustForCellText(text, aRowIndex, aColumn, aRenderingContext, *fontMet, textRect);
  3168. textRect.Inflate(bp);
  3169. // Subtract out the remaining width.
  3170. if (!isRTL)
  3171. aCurrX += textRect.width + textMargin.LeftRight();
  3172. result &= PaintBackgroundLayer(textContext, aPresContext, aRenderingContext,
  3173. textRect, aDirtyRect);
  3174. // Time to paint our text.
  3175. textRect.Deflate(bp);
  3176. // Set our color.
  3177. ColorPattern color(ToDeviceColor(textContext->StyleColor()->mColor));
  3178. // Draw decorations.
  3179. uint8_t decorations = textContext->StyleTextReset()->mTextDecorationLine;
  3180. nscoord offset;
  3181. nscoord size;
  3182. if (decorations & (NS_STYLE_TEXT_DECORATION_LINE_OVERLINE |
  3183. NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE)) {
  3184. fontMet->GetUnderline(offset, size);
  3185. if (decorations & NS_STYLE_TEXT_DECORATION_LINE_OVERLINE) {
  3186. nsRect r(textRect.x, textRect.y, textRect.width, size);
  3187. Rect devPxRect =
  3188. NSRectToSnappedRect(r, appUnitsPerDevPixel, *drawTarget);
  3189. drawTarget->FillRect(devPxRect, color);
  3190. }
  3191. if (decorations & NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE) {
  3192. nsRect r(textRect.x, textRect.y + baseline - offset,
  3193. textRect.width, size);
  3194. Rect devPxRect =
  3195. NSRectToSnappedRect(r, appUnitsPerDevPixel, *drawTarget);
  3196. drawTarget->FillRect(devPxRect, color);
  3197. }
  3198. }
  3199. if (decorations & NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH) {
  3200. fontMet->GetStrikeout(offset, size);
  3201. nsRect r(textRect.x, textRect.y + baseline - offset, textRect.width, size);
  3202. Rect devPxRect =
  3203. NSRectToSnappedRect(r, appUnitsPerDevPixel, *drawTarget);
  3204. drawTarget->FillRect(devPxRect, color);
  3205. }
  3206. nsStyleContext* cellContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecell);
  3207. gfxContext* ctx = aRenderingContext.ThebesContext();
  3208. if (opacity != 1.0f) {
  3209. ctx->PushGroupForBlendBack(gfxContentType::COLOR_ALPHA, opacity);
  3210. }
  3211. ctx->SetColor(Color::FromABGR(textContext->StyleColor()->mColor));
  3212. nsLayoutUtils::DrawString(this, *fontMet, &aRenderingContext, text.get(),
  3213. text.Length(),
  3214. textRect.TopLeft() + nsPoint(0, baseline),
  3215. cellContext);
  3216. if (opacity != 1.0f) {
  3217. ctx->PopGroupAndBlend();
  3218. }
  3219. return result;
  3220. }
  3221. #ifdef _MSC_VER
  3222. # pragma optimize("", on)
  3223. #endif
  3224. DrawResult
  3225. nsTreeBodyFrame::PaintCheckbox(int32_t aRowIndex,
  3226. nsTreeColumn* aColumn,
  3227. const nsRect& aCheckboxRect,
  3228. nsPresContext* aPresContext,
  3229. nsRenderingContext& aRenderingContext,
  3230. const nsRect& aDirtyRect)
  3231. {
  3232. NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
  3233. // Resolve style for the checkbox.
  3234. nsStyleContext* checkboxContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecheckbox);
  3235. nscoord rightEdge = aCheckboxRect.XMost();
  3236. // Obtain the margins for the checkbox and then deflate our rect by that
  3237. // amount. The checkbox is assumed to be contained within the deflated rect.
  3238. nsRect checkboxRect(aCheckboxRect);
  3239. nsMargin checkboxMargin;
  3240. checkboxContext->StyleMargin()->GetMargin(checkboxMargin);
  3241. checkboxRect.Deflate(checkboxMargin);
  3242. nsRect imageSize = GetImageSize(aRowIndex, aColumn, true, checkboxContext);
  3243. if (imageSize.height > checkboxRect.height)
  3244. imageSize.height = checkboxRect.height;
  3245. if (imageSize.width > checkboxRect.width)
  3246. imageSize.width = checkboxRect.width;
  3247. if (StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL)
  3248. checkboxRect.x = rightEdge - checkboxRect.width;
  3249. // Paint our borders and background for our image rect.
  3250. DrawResult result = PaintBackgroundLayer(checkboxContext, aPresContext,
  3251. aRenderingContext, checkboxRect,
  3252. aDirtyRect);
  3253. // Time to paint the checkbox.
  3254. // Adjust the rect for its border and padding.
  3255. nsMargin bp(0,0,0,0);
  3256. GetBorderPadding(checkboxContext, bp);
  3257. checkboxRect.Deflate(bp);
  3258. // Get the image for drawing.
  3259. nsCOMPtr<imgIContainer> image;
  3260. bool useImageRegion = true;
  3261. GetImage(aRowIndex, aColumn, true, checkboxContext, useImageRegion, getter_AddRefs(image));
  3262. if (image) {
  3263. nsPoint pt = checkboxRect.TopLeft();
  3264. if (imageSize.height < checkboxRect.height) {
  3265. pt.y += (checkboxRect.height - imageSize.height)/2;
  3266. }
  3267. if (imageSize.width < checkboxRect.width) {
  3268. pt.x += (checkboxRect.width - imageSize.width)/2;
  3269. }
  3270. // Paint the image.
  3271. result &=
  3272. nsLayoutUtils::DrawSingleUnscaledImage(*aRenderingContext.ThebesContext(),
  3273. aPresContext,
  3274. image, SamplingFilter::POINT, pt, &aDirtyRect,
  3275. imgIContainer::FLAG_NONE, &imageSize);
  3276. }
  3277. return result;
  3278. }
  3279. DrawResult
  3280. nsTreeBodyFrame::PaintProgressMeter(int32_t aRowIndex,
  3281. nsTreeColumn* aColumn,
  3282. const nsRect& aProgressMeterRect,
  3283. nsPresContext* aPresContext,
  3284. nsRenderingContext& aRenderingContext,
  3285. const nsRect& aDirtyRect)
  3286. {
  3287. NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
  3288. // Resolve style for the progress meter. It contains all the info we need
  3289. // to lay ourselves out and to paint.
  3290. nsStyleContext* meterContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeprogressmeter);
  3291. // Obtain the margins for the progress meter and then deflate our rect by that
  3292. // amount. The progress meter is assumed to be contained within the deflated
  3293. // rect.
  3294. nsRect meterRect(aProgressMeterRect);
  3295. nsMargin meterMargin;
  3296. meterContext->StyleMargin()->GetMargin(meterMargin);
  3297. meterRect.Deflate(meterMargin);
  3298. // Paint our borders and background for our progress meter rect.
  3299. DrawResult result = PaintBackgroundLayer(meterContext, aPresContext,
  3300. aRenderingContext, meterRect,
  3301. aDirtyRect);
  3302. // Time to paint our progress.
  3303. int32_t state;
  3304. mView->GetProgressMode(aRowIndex, aColumn, &state);
  3305. if (state == nsITreeView::PROGRESS_NORMAL) {
  3306. // Adjust the rect for its border and padding.
  3307. AdjustForBorderPadding(meterContext, meterRect);
  3308. // Now obtain the value for our cell.
  3309. nsAutoString value;
  3310. mView->GetCellValue(aRowIndex, aColumn, value);
  3311. nsresult rv;
  3312. int32_t intValue = value.ToInteger(&rv);
  3313. if (intValue < 0)
  3314. intValue = 0;
  3315. else if (intValue > 100)
  3316. intValue = 100;
  3317. nscoord meterWidth = NSToCoordRound((float)intValue / 100 * meterRect.width);
  3318. if (StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL)
  3319. meterRect.x += meterRect.width - meterWidth; // right align
  3320. meterRect.width = meterWidth;
  3321. bool useImageRegion = true;
  3322. nsCOMPtr<imgIContainer> image;
  3323. GetImage(aRowIndex, aColumn, true, meterContext, useImageRegion, getter_AddRefs(image));
  3324. if (image) {
  3325. int32_t width, height;
  3326. image->GetWidth(&width);
  3327. image->GetHeight(&height);
  3328. nsSize size(width*nsDeviceContext::AppUnitsPerCSSPixel(),
  3329. height*nsDeviceContext::AppUnitsPerCSSPixel());
  3330. result &=
  3331. nsLayoutUtils::DrawImage(*aRenderingContext.ThebesContext(),
  3332. aPresContext, image,
  3333. nsLayoutUtils::GetSamplingFilterForFrame(this),
  3334. nsRect(meterRect.TopLeft(), size), meterRect, meterRect.TopLeft(),
  3335. aDirtyRect, imgIContainer::FLAG_NONE);
  3336. } else {
  3337. DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
  3338. int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
  3339. Rect rect =
  3340. NSRectToSnappedRect(meterRect, appUnitsPerDevPixel, *drawTarget);
  3341. ColorPattern color(ToDeviceColor(meterContext->StyleColor()->mColor));
  3342. drawTarget->FillRect(rect, color);
  3343. }
  3344. }
  3345. else if (state == nsITreeView::PROGRESS_UNDETERMINED) {
  3346. // Adjust the rect for its border and padding.
  3347. AdjustForBorderPadding(meterContext, meterRect);
  3348. bool useImageRegion = true;
  3349. nsCOMPtr<imgIContainer> image;
  3350. GetImage(aRowIndex, aColumn, true, meterContext, useImageRegion, getter_AddRefs(image));
  3351. if (image) {
  3352. int32_t width, height;
  3353. image->GetWidth(&width);
  3354. image->GetHeight(&height);
  3355. nsSize size(width*nsDeviceContext::AppUnitsPerCSSPixel(),
  3356. height*nsDeviceContext::AppUnitsPerCSSPixel());
  3357. result &=
  3358. nsLayoutUtils::DrawImage(*aRenderingContext.ThebesContext(),
  3359. aPresContext, image,
  3360. nsLayoutUtils::GetSamplingFilterForFrame(this),
  3361. nsRect(meterRect.TopLeft(), size), meterRect, meterRect.TopLeft(),
  3362. aDirtyRect, imgIContainer::FLAG_NONE);
  3363. }
  3364. }
  3365. return result;
  3366. }
  3367. DrawResult
  3368. nsTreeBodyFrame::PaintDropFeedback(const nsRect& aDropFeedbackRect,
  3369. nsPresContext* aPresContext,
  3370. nsRenderingContext& aRenderingContext,
  3371. const nsRect& aDirtyRect,
  3372. nsPoint aPt)
  3373. {
  3374. // Paint the drop feedback in between rows.
  3375. nscoord currX;
  3376. // Adjust for the primary cell.
  3377. nsTreeColumn* primaryCol = mColumns->GetPrimaryColumn();
  3378. if (primaryCol) {
  3379. #ifdef DEBUG
  3380. nsresult rv =
  3381. #endif
  3382. primaryCol->GetXInTwips(this, &currX);
  3383. NS_ASSERTION(NS_SUCCEEDED(rv), "primary column is invalid?");
  3384. currX += aPt.x - mHorzPosition;
  3385. } else {
  3386. currX = aDropFeedbackRect.x;
  3387. }
  3388. PrefillPropertyArray(mSlots->mDropRow, primaryCol);
  3389. // Resolve the style to use for the drop feedback.
  3390. nsStyleContext* feedbackContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreedropfeedback);
  3391. DrawResult result = DrawResult::SUCCESS;
  3392. // Paint only if it is visible.
  3393. if (feedbackContext->StyleVisibility()->IsVisibleOrCollapsed()) {
  3394. int32_t level;
  3395. mView->GetLevel(mSlots->mDropRow, &level);
  3396. // If our previous or next row has greater level use that for
  3397. // correct visual indentation.
  3398. if (mSlots->mDropOrient == nsITreeView::DROP_BEFORE) {
  3399. if (mSlots->mDropRow > 0) {
  3400. int32_t previousLevel;
  3401. mView->GetLevel(mSlots->mDropRow - 1, &previousLevel);
  3402. if (previousLevel > level)
  3403. level = previousLevel;
  3404. }
  3405. }
  3406. else {
  3407. if (mSlots->mDropRow < mRowCount - 1) {
  3408. int32_t nextLevel;
  3409. mView->GetLevel(mSlots->mDropRow + 1, &nextLevel);
  3410. if (nextLevel > level)
  3411. level = nextLevel;
  3412. }
  3413. }
  3414. currX += mIndentation * level;
  3415. if (primaryCol){
  3416. nsStyleContext* twistyContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreetwisty);
  3417. nsRect imageSize;
  3418. nsRect twistyRect;
  3419. GetTwistyRect(mSlots->mDropRow, primaryCol, imageSize, twistyRect,
  3420. aPresContext, twistyContext);
  3421. nsMargin twistyMargin;
  3422. twistyContext->StyleMargin()->GetMargin(twistyMargin);
  3423. twistyRect.Inflate(twistyMargin);
  3424. currX += twistyRect.width;
  3425. }
  3426. const nsStylePosition* stylePosition = feedbackContext->StylePosition();
  3427. // Obtain the width for the drop feedback or use default value.
  3428. nscoord width;
  3429. if (stylePosition->mWidth.GetUnit() == eStyleUnit_Coord)
  3430. width = stylePosition->mWidth.GetCoordValue();
  3431. else {
  3432. // Use default width 50px.
  3433. width = nsPresContext::CSSPixelsToAppUnits(50);
  3434. }
  3435. // Obtain the height for the drop feedback or use default value.
  3436. nscoord height;
  3437. if (stylePosition->mHeight.GetUnit() == eStyleUnit_Coord)
  3438. height = stylePosition->mHeight.GetCoordValue();
  3439. else {
  3440. // Use default height 2px.
  3441. height = nsPresContext::CSSPixelsToAppUnits(2);
  3442. }
  3443. // Obtain the margins for the drop feedback and then deflate our rect
  3444. // by that amount.
  3445. nsRect feedbackRect(currX, aDropFeedbackRect.y, width, height);
  3446. nsMargin margin;
  3447. feedbackContext->StyleMargin()->GetMargin(margin);
  3448. feedbackRect.Deflate(margin);
  3449. feedbackRect.y += (aDropFeedbackRect.height - height) / 2;
  3450. // Finally paint the drop feedback.
  3451. result &= PaintBackgroundLayer(feedbackContext, aPresContext,
  3452. aRenderingContext, feedbackRect,
  3453. aDirtyRect);
  3454. }
  3455. return result;
  3456. }
  3457. DrawResult
  3458. nsTreeBodyFrame::PaintBackgroundLayer(nsStyleContext* aStyleContext,
  3459. nsPresContext* aPresContext,
  3460. nsRenderingContext& aRenderingContext,
  3461. const nsRect& aRect,
  3462. const nsRect& aDirtyRect)
  3463. {
  3464. const nsStyleBorder* myBorder = aStyleContext->StyleBorder();
  3465. nsCSSRendering::PaintBGParams params =
  3466. nsCSSRendering::PaintBGParams::ForAllLayers(*aPresContext, aRenderingContext,
  3467. aDirtyRect, aRect, this,
  3468. nsCSSRendering::PAINTBG_SYNC_DECODE_IMAGES);
  3469. DrawResult result =
  3470. nsCSSRendering::PaintBackgroundWithSC(params, aStyleContext, *myBorder);
  3471. result &=
  3472. nsCSSRendering::PaintBorderWithStyleBorder(aPresContext, aRenderingContext,
  3473. this, aDirtyRect, aRect,
  3474. *myBorder, mStyleContext,
  3475. PaintBorderFlags::SYNC_DECODE_IMAGES);
  3476. nsCSSRendering::PaintOutline(aPresContext, aRenderingContext, this,
  3477. aDirtyRect, aRect, aStyleContext);
  3478. return result;
  3479. }
  3480. // Scrolling
  3481. nsresult
  3482. nsTreeBodyFrame::EnsureRowIsVisible(int32_t aRow)
  3483. {
  3484. ScrollParts parts = GetScrollParts();
  3485. nsresult rv = EnsureRowIsVisibleInternal(parts, aRow);
  3486. NS_ENSURE_SUCCESS(rv, rv);
  3487. UpdateScrollbars(parts);
  3488. return rv;
  3489. }
  3490. nsresult nsTreeBodyFrame::EnsureRowIsVisibleInternal(const ScrollParts& aParts, int32_t aRow)
  3491. {
  3492. if (!mView || !mPageLength)
  3493. return NS_OK;
  3494. if (mTopRowIndex <= aRow && mTopRowIndex+mPageLength > aRow)
  3495. return NS_OK;
  3496. if (aRow < mTopRowIndex)
  3497. ScrollToRowInternal(aParts, aRow);
  3498. else {
  3499. // Bring it just on-screen.
  3500. int32_t distance = aRow - (mTopRowIndex+mPageLength)+1;
  3501. ScrollToRowInternal(aParts, mTopRowIndex+distance);
  3502. }
  3503. return NS_OK;
  3504. }
  3505. nsresult
  3506. nsTreeBodyFrame::EnsureCellIsVisible(int32_t aRow, nsITreeColumn* aCol)
  3507. {
  3508. RefPtr<nsTreeColumn> col = GetColumnImpl(aCol);
  3509. if (!col)
  3510. return NS_ERROR_INVALID_ARG;
  3511. ScrollParts parts = GetScrollParts();
  3512. nscoord result = -1;
  3513. nsresult rv;
  3514. nscoord columnPos;
  3515. rv = col->GetXInTwips(this, &columnPos);
  3516. if(NS_FAILED(rv)) return rv;
  3517. nscoord columnWidth;
  3518. rv = col->GetWidthInTwips(this, &columnWidth);
  3519. if(NS_FAILED(rv)) return rv;
  3520. // If the start of the column is before the
  3521. // start of the horizontal view, then scroll
  3522. if (columnPos < mHorzPosition)
  3523. result = columnPos;
  3524. // If the end of the column is past the end of
  3525. // the horizontal view, then scroll
  3526. else if ((columnPos + columnWidth) > (mHorzPosition + mInnerBox.width))
  3527. result = ((columnPos + columnWidth) - (mHorzPosition + mInnerBox.width)) + mHorzPosition;
  3528. if (result != -1) {
  3529. rv = ScrollHorzInternal(parts, result);
  3530. if(NS_FAILED(rv)) return rv;
  3531. }
  3532. rv = EnsureRowIsVisibleInternal(parts, aRow);
  3533. NS_ENSURE_SUCCESS(rv, rv);
  3534. UpdateScrollbars(parts);
  3535. return rv;
  3536. }
  3537. nsresult
  3538. nsTreeBodyFrame::ScrollToCell(int32_t aRow, nsITreeColumn* aCol)
  3539. {
  3540. ScrollParts parts = GetScrollParts();
  3541. nsresult rv = ScrollToRowInternal(parts, aRow);
  3542. NS_ENSURE_SUCCESS(rv, rv);
  3543. rv = ScrollToColumnInternal(parts, aCol);
  3544. NS_ENSURE_SUCCESS(rv, rv);
  3545. UpdateScrollbars(parts);
  3546. return rv;
  3547. }
  3548. nsresult
  3549. nsTreeBodyFrame::ScrollToColumn(nsITreeColumn* aCol)
  3550. {
  3551. ScrollParts parts = GetScrollParts();
  3552. nsresult rv = ScrollToColumnInternal(parts, aCol);
  3553. NS_ENSURE_SUCCESS(rv, rv);
  3554. UpdateScrollbars(parts);
  3555. return rv;
  3556. }
  3557. nsresult nsTreeBodyFrame::ScrollToColumnInternal(const ScrollParts& aParts,
  3558. nsITreeColumn* aCol)
  3559. {
  3560. RefPtr<nsTreeColumn> col = GetColumnImpl(aCol);
  3561. if (!col)
  3562. return NS_ERROR_INVALID_ARG;
  3563. nscoord x;
  3564. nsresult rv = col->GetXInTwips(this, &x);
  3565. if (NS_FAILED(rv))
  3566. return rv;
  3567. return ScrollHorzInternal(aParts, x);
  3568. }
  3569. nsresult
  3570. nsTreeBodyFrame::ScrollToHorizontalPosition(int32_t aHorizontalPosition)
  3571. {
  3572. ScrollParts parts = GetScrollParts();
  3573. int32_t position = nsPresContext::CSSPixelsToAppUnits(aHorizontalPosition);
  3574. nsresult rv = ScrollHorzInternal(parts, position);
  3575. NS_ENSURE_SUCCESS(rv, rv);
  3576. UpdateScrollbars(parts);
  3577. return rv;
  3578. }
  3579. nsresult
  3580. nsTreeBodyFrame::ScrollToRow(int32_t aRow)
  3581. {
  3582. ScrollParts parts = GetScrollParts();
  3583. ScrollToRowInternal(parts, aRow);
  3584. UpdateScrollbars(parts);
  3585. return NS_OK;
  3586. }
  3587. nsresult nsTreeBodyFrame::ScrollToRowInternal(const ScrollParts& aParts, int32_t aRow)
  3588. {
  3589. ScrollInternal(aParts, aRow);
  3590. return NS_OK;
  3591. }
  3592. nsresult
  3593. nsTreeBodyFrame::ScrollByLines(int32_t aNumLines)
  3594. {
  3595. if (!mView) {
  3596. return NS_OK;
  3597. }
  3598. int32_t newIndex = mTopRowIndex + aNumLines;
  3599. ScrollToRow(newIndex);
  3600. return NS_OK;
  3601. }
  3602. nsresult
  3603. nsTreeBodyFrame::ScrollByPages(int32_t aNumPages)
  3604. {
  3605. if (!mView) {
  3606. return NS_OK;
  3607. }
  3608. int32_t newIndex = mTopRowIndex + aNumPages * mPageLength;
  3609. ScrollToRow(newIndex);
  3610. return NS_OK;
  3611. }
  3612. nsresult
  3613. nsTreeBodyFrame::ScrollInternal(const ScrollParts& aParts, int32_t aRow)
  3614. {
  3615. if (!mView) {
  3616. return NS_OK;
  3617. }
  3618. // Note that we may be "over scrolled" at this point; that is the
  3619. // current mTopRowIndex may be larger than mRowCount - mPageLength.
  3620. // This can happen when items are removed for example. (bug 1085050)
  3621. int32_t maxTopRowIndex = std::max(0, mRowCount - mPageLength);
  3622. aRow = mozilla::clamped(aRow, 0, maxTopRowIndex);
  3623. if (aRow == mTopRowIndex) {
  3624. return NS_OK;
  3625. }
  3626. mTopRowIndex = aRow;
  3627. Invalidate();
  3628. PostScrollEvent();
  3629. return NS_OK;
  3630. }
  3631. nsresult
  3632. nsTreeBodyFrame::ScrollHorzInternal(const ScrollParts& aParts, int32_t aPosition)
  3633. {
  3634. if (!mView || !aParts.mColumnsScrollFrame || !aParts.mHScrollbar)
  3635. return NS_OK;
  3636. if (aPosition == mHorzPosition)
  3637. return NS_OK;
  3638. if (aPosition < 0 || aPosition > mHorzWidth)
  3639. return NS_OK;
  3640. nsRect bounds = aParts.mColumnsFrame->GetRect();
  3641. if (aPosition > (mHorzWidth - bounds.width))
  3642. aPosition = mHorzWidth - bounds.width;
  3643. mHorzPosition = aPosition;
  3644. Invalidate();
  3645. // Update the column scroll view
  3646. nsWeakFrame weakFrame(this);
  3647. aParts.mColumnsScrollFrame->ScrollTo(nsPoint(mHorzPosition, 0),
  3648. nsIScrollableFrame::INSTANT);
  3649. if (!weakFrame.IsAlive()) {
  3650. return NS_ERROR_FAILURE;
  3651. }
  3652. // And fire off an event about it all
  3653. PostScrollEvent();
  3654. return NS_OK;
  3655. }
  3656. void
  3657. nsTreeBodyFrame::ScrollByPage(nsScrollbarFrame* aScrollbar, int32_t aDirection,
  3658. nsIScrollbarMediator::ScrollSnapMode aSnap)
  3659. {
  3660. // CSS Scroll Snapping is not enabled for XUL, aSnap is ignored
  3661. MOZ_ASSERT(aScrollbar != nullptr);
  3662. ScrollByPages(aDirection);
  3663. }
  3664. void
  3665. nsTreeBodyFrame::ScrollByWhole(nsScrollbarFrame* aScrollbar, int32_t aDirection,
  3666. nsIScrollbarMediator::ScrollSnapMode aSnap)
  3667. {
  3668. // CSS Scroll Snapping is not enabled for XUL, aSnap is ignored
  3669. MOZ_ASSERT(aScrollbar != nullptr);
  3670. int32_t newIndex = aDirection < 0 ? 0 : mTopRowIndex;
  3671. ScrollToRow(newIndex);
  3672. }
  3673. void
  3674. nsTreeBodyFrame::ScrollByLine(nsScrollbarFrame* aScrollbar, int32_t aDirection,
  3675. nsIScrollbarMediator::ScrollSnapMode aSnap)
  3676. {
  3677. // CSS Scroll Snapping is not enabled for XUL, aSnap is ignored
  3678. MOZ_ASSERT(aScrollbar != nullptr);
  3679. ScrollByLines(aDirection);
  3680. }
  3681. void
  3682. nsTreeBodyFrame::RepeatButtonScroll(nsScrollbarFrame* aScrollbar)
  3683. {
  3684. ScrollParts parts = GetScrollParts();
  3685. int32_t increment = aScrollbar->GetIncrement();
  3686. int32_t direction = 0;
  3687. if (increment < 0) {
  3688. direction = -1;
  3689. } else if (increment > 0) {
  3690. direction = 1;
  3691. }
  3692. bool isHorizontal = aScrollbar->IsXULHorizontal();
  3693. nsWeakFrame weakFrame(this);
  3694. if (isHorizontal) {
  3695. int32_t curpos = aScrollbar->MoveToNewPosition();
  3696. if (weakFrame.IsAlive()) {
  3697. ScrollHorzInternal(parts, curpos);
  3698. }
  3699. } else {
  3700. ScrollToRowInternal(parts, mTopRowIndex+direction);
  3701. }
  3702. if (weakFrame.IsAlive() && mScrollbarActivity) {
  3703. mScrollbarActivity->ActivityOccurred();
  3704. }
  3705. if (weakFrame.IsAlive()) {
  3706. UpdateScrollbars(parts);
  3707. }
  3708. }
  3709. void
  3710. nsTreeBodyFrame::ThumbMoved(nsScrollbarFrame* aScrollbar,
  3711. nscoord aOldPos,
  3712. nscoord aNewPos)
  3713. {
  3714. ScrollParts parts = GetScrollParts();
  3715. if (aOldPos == aNewPos)
  3716. return;
  3717. nsWeakFrame weakFrame(this);
  3718. // Vertical Scrollbar
  3719. if (parts.mVScrollbar == aScrollbar) {
  3720. nscoord rh = nsPresContext::AppUnitsToIntCSSPixels(mRowHeight);
  3721. nscoord newIndex = nsPresContext::AppUnitsToIntCSSPixels(aNewPos);
  3722. nscoord newrow = newIndex/rh;
  3723. ScrollInternal(parts, newrow);
  3724. // Horizontal Scrollbar
  3725. } else if (parts.mHScrollbar == aScrollbar) {
  3726. int32_t newIndex = nsPresContext::AppUnitsToIntCSSPixels(aNewPos);
  3727. ScrollHorzInternal(parts, newIndex);
  3728. }
  3729. if (weakFrame.IsAlive()) {
  3730. UpdateScrollbars(parts);
  3731. }
  3732. }
  3733. // The style cache.
  3734. nsStyleContext*
  3735. nsTreeBodyFrame::GetPseudoStyleContext(nsIAtom* aPseudoElement)
  3736. {
  3737. return mStyleCache.GetStyleContext(this, PresContext(), mContent,
  3738. mStyleContext, aPseudoElement,
  3739. mScratchArray);
  3740. }
  3741. // Our comparator for resolving our complex pseudos
  3742. bool
  3743. nsTreeBodyFrame::PseudoMatches(nsCSSSelector* aSelector)
  3744. {
  3745. // Iterate the class list. For each item in the list, see if
  3746. // it is contained in our scratch array. If we have a miss, then
  3747. // we aren't a match. If all items in the class list are
  3748. // present in the scratch array, then we have a match.
  3749. nsAtomList* curr = aSelector->mClassList;
  3750. while (curr) {
  3751. if (!mScratchArray.Contains(curr->mAtom))
  3752. return false;
  3753. curr = curr->mNext;
  3754. }
  3755. return true;
  3756. }
  3757. nsIContent*
  3758. nsTreeBodyFrame::GetBaseElement()
  3759. {
  3760. nsIFrame* parent = GetParent();
  3761. while (parent) {
  3762. nsIContent* content = parent->GetContent();
  3763. if (content) {
  3764. dom::NodeInfo* ni = content->NodeInfo();
  3765. if (ni->Equals(nsGkAtoms::tree, kNameSpaceID_XUL) ||
  3766. (ni->Equals(nsGkAtoms::select) &&
  3767. content->IsHTMLElement()))
  3768. return content;
  3769. }
  3770. parent = parent->GetParent();
  3771. }
  3772. return nullptr;
  3773. }
  3774. nsresult
  3775. nsTreeBodyFrame::ClearStyleAndImageCaches()
  3776. {
  3777. mStyleCache.Clear();
  3778. CancelImageRequests();
  3779. mImageCache.Clear();
  3780. return NS_OK;
  3781. }
  3782. /* virtual */ void
  3783. nsTreeBodyFrame::DidSetStyleContext(nsStyleContext* aOldStyleContext)
  3784. {
  3785. nsLeafBoxFrame::DidSetStyleContext(aOldStyleContext);
  3786. // Clear the style cache; the pointers are no longer even valid
  3787. mStyleCache.Clear();
  3788. // XXX The following is hacky, but it's not incorrect,
  3789. // and appears to fix a few bugs with style changes, like text zoom and
  3790. // dpi changes
  3791. mIndentation = GetIndentation();
  3792. mRowHeight = GetRowHeight();
  3793. mStringWidth = -1;
  3794. }
  3795. bool
  3796. nsTreeBodyFrame::OffsetForHorzScroll(nsRect& rect, bool clip)
  3797. {
  3798. rect.x -= mHorzPosition;
  3799. // Scrolled out before
  3800. if (rect.XMost() <= mInnerBox.x)
  3801. return false;
  3802. // Scrolled out after
  3803. if (rect.x > mInnerBox.XMost())
  3804. return false;
  3805. if (clip) {
  3806. nscoord leftEdge = std::max(rect.x, mInnerBox.x);
  3807. nscoord rightEdge = std::min(rect.XMost(), mInnerBox.XMost());
  3808. rect.x = leftEdge;
  3809. rect.width = rightEdge - leftEdge;
  3810. // Should have returned false above
  3811. NS_ASSERTION(rect.width >= 0, "horz scroll code out of sync");
  3812. }
  3813. return true;
  3814. }
  3815. bool
  3816. nsTreeBodyFrame::CanAutoScroll(int32_t aRowIndex)
  3817. {
  3818. // Check first for partially visible last row.
  3819. if (aRowIndex == mRowCount - 1) {
  3820. nscoord y = mInnerBox.y + (aRowIndex - mTopRowIndex) * mRowHeight;
  3821. if (y < mInnerBox.height && y + mRowHeight > mInnerBox.height)
  3822. return true;
  3823. }
  3824. if (aRowIndex > 0 && aRowIndex < mRowCount - 1)
  3825. return true;
  3826. return false;
  3827. }
  3828. // Given a dom event, figure out which row in the tree the mouse is over,
  3829. // if we should drop before/after/on that row or we should auto-scroll.
  3830. // Doesn't query the content about if the drag is allowable, that's done elsewhere.
  3831. //
  3832. // For containers, we break up the vertical space of the row as follows: if in
  3833. // the topmost 25%, the drop is _before_ the row the mouse is over; if in the
  3834. // last 25%, _after_; in the middle 50%, we consider it a drop _on_ the container.
  3835. //
  3836. // For non-containers, if the mouse is in the top 50% of the row, the drop is
  3837. // _before_ and the bottom 50% _after_
  3838. void
  3839. nsTreeBodyFrame::ComputeDropPosition(WidgetGUIEvent* aEvent,
  3840. int32_t* aRow,
  3841. int16_t* aOrient,
  3842. int16_t* aScrollLines)
  3843. {
  3844. *aOrient = -1;
  3845. *aScrollLines = 0;
  3846. // Convert the event's point to our coordinates. We want it in
  3847. // the coordinates of our inner box's coordinates.
  3848. nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, this);
  3849. int32_t xTwips = pt.x - mInnerBox.x;
  3850. int32_t yTwips = pt.y - mInnerBox.y;
  3851. *aRow = GetRowAt(xTwips, yTwips);
  3852. if (*aRow >=0) {
  3853. // Compute the top/bottom of the row in question.
  3854. int32_t yOffset = yTwips - mRowHeight * (*aRow - mTopRowIndex);
  3855. bool isContainer = false;
  3856. mView->IsContainer (*aRow, &isContainer);
  3857. if (isContainer) {
  3858. // for a container, use a 25%/50%/25% breakdown
  3859. if (yOffset < mRowHeight / 4)
  3860. *aOrient = nsITreeView::DROP_BEFORE;
  3861. else if (yOffset > mRowHeight - (mRowHeight / 4))
  3862. *aOrient = nsITreeView::DROP_AFTER;
  3863. else
  3864. *aOrient = nsITreeView::DROP_ON;
  3865. }
  3866. else {
  3867. // for a non-container use a 50%/50% breakdown
  3868. if (yOffset < mRowHeight / 2)
  3869. *aOrient = nsITreeView::DROP_BEFORE;
  3870. else
  3871. *aOrient = nsITreeView::DROP_AFTER;
  3872. }
  3873. }
  3874. if (CanAutoScroll(*aRow)) {
  3875. // Get the max value from the look and feel service.
  3876. int32_t scrollLinesMax =
  3877. LookAndFeel::GetInt(LookAndFeel::eIntID_TreeScrollLinesMax, 0);
  3878. scrollLinesMax--;
  3879. if (scrollLinesMax < 0)
  3880. scrollLinesMax = 0;
  3881. // Determine if we're w/in a margin of the top/bottom of the tree during a drag.
  3882. // This will ultimately cause us to scroll, but that's done elsewhere.
  3883. nscoord height = (3 * mRowHeight) / 4;
  3884. if (yTwips < height) {
  3885. // scroll up
  3886. *aScrollLines = NSToIntRound(-scrollLinesMax * (1 - (float)yTwips / height) - 1);
  3887. }
  3888. else if (yTwips > mRect.height - height) {
  3889. // scroll down
  3890. *aScrollLines = NSToIntRound(scrollLinesMax * (1 - (float)(mRect.height - yTwips) / height) + 1);
  3891. }
  3892. }
  3893. } // ComputeDropPosition
  3894. void
  3895. nsTreeBodyFrame::OpenCallback(nsITimer *aTimer, void *aClosure)
  3896. {
  3897. nsTreeBodyFrame* self = static_cast<nsTreeBodyFrame*>(aClosure);
  3898. if (self) {
  3899. aTimer->Cancel();
  3900. self->mSlots->mTimer = nullptr;
  3901. if (self->mSlots->mDropRow >= 0) {
  3902. self->mSlots->mArray.AppendElement(self->mSlots->mDropRow);
  3903. self->mView->ToggleOpenState(self->mSlots->mDropRow);
  3904. }
  3905. }
  3906. }
  3907. void
  3908. nsTreeBodyFrame::CloseCallback(nsITimer *aTimer, void *aClosure)
  3909. {
  3910. nsTreeBodyFrame* self = static_cast<nsTreeBodyFrame*>(aClosure);
  3911. if (self) {
  3912. aTimer->Cancel();
  3913. self->mSlots->mTimer = nullptr;
  3914. for (uint32_t i = self->mSlots->mArray.Length(); i--; ) {
  3915. if (self->mView)
  3916. self->mView->ToggleOpenState(self->mSlots->mArray[i]);
  3917. }
  3918. self->mSlots->mArray.Clear();
  3919. }
  3920. }
  3921. void
  3922. nsTreeBodyFrame::LazyScrollCallback(nsITimer *aTimer, void *aClosure)
  3923. {
  3924. nsTreeBodyFrame* self = static_cast<nsTreeBodyFrame*>(aClosure);
  3925. if (self) {
  3926. aTimer->Cancel();
  3927. self->mSlots->mTimer = nullptr;
  3928. if (self->mView) {
  3929. // Set a new timer to scroll the tree repeatedly.
  3930. self->CreateTimer(LookAndFeel::eIntID_TreeScrollDelay,
  3931. ScrollCallback, nsITimer::TYPE_REPEATING_SLACK,
  3932. getter_AddRefs(self->mSlots->mTimer));
  3933. self->ScrollByLines(self->mSlots->mScrollLines);
  3934. // ScrollByLines may have deleted |self|.
  3935. }
  3936. }
  3937. }
  3938. void
  3939. nsTreeBodyFrame::ScrollCallback(nsITimer *aTimer, void *aClosure)
  3940. {
  3941. nsTreeBodyFrame* self = static_cast<nsTreeBodyFrame*>(aClosure);
  3942. if (self) {
  3943. // Don't scroll if we are already at the top or bottom of the view.
  3944. if (self->mView && self->CanAutoScroll(self->mSlots->mDropRow)) {
  3945. self->ScrollByLines(self->mSlots->mScrollLines);
  3946. }
  3947. else {
  3948. aTimer->Cancel();
  3949. self->mSlots->mTimer = nullptr;
  3950. }
  3951. }
  3952. }
  3953. NS_IMETHODIMP
  3954. nsTreeBodyFrame::ScrollEvent::Run()
  3955. {
  3956. if (mInner) {
  3957. mInner->FireScrollEvent();
  3958. }
  3959. return NS_OK;
  3960. }
  3961. void
  3962. nsTreeBodyFrame::FireScrollEvent()
  3963. {
  3964. mScrollEvent.Forget();
  3965. WidgetGUIEvent event(true, eScroll, nullptr);
  3966. // scroll events fired at elements don't bubble
  3967. event.mFlags.mBubbles = false;
  3968. EventDispatcher::Dispatch(GetContent(), PresContext(), &event);
  3969. }
  3970. void
  3971. nsTreeBodyFrame::PostScrollEvent()
  3972. {
  3973. if (mScrollEvent.IsPending())
  3974. return;
  3975. RefPtr<ScrollEvent> ev = new ScrollEvent(this);
  3976. if (NS_FAILED(NS_DispatchToCurrentThread(ev))) {
  3977. NS_WARNING("failed to dispatch ScrollEvent");
  3978. } else {
  3979. mScrollEvent = ev;
  3980. }
  3981. }
  3982. void
  3983. nsTreeBodyFrame::ScrollbarActivityStarted() const
  3984. {
  3985. if (mScrollbarActivity) {
  3986. mScrollbarActivity->ActivityStarted();
  3987. }
  3988. }
  3989. void
  3990. nsTreeBodyFrame::ScrollbarActivityStopped() const
  3991. {
  3992. if (mScrollbarActivity) {
  3993. mScrollbarActivity->ActivityStopped();
  3994. }
  3995. }
  3996. void
  3997. nsTreeBodyFrame::DetachImageListeners()
  3998. {
  3999. mCreatedListeners.Clear();
  4000. }
  4001. void
  4002. nsTreeBodyFrame::RemoveTreeImageListener(nsTreeImageListener* aListener)
  4003. {
  4004. if (aListener) {
  4005. mCreatedListeners.RemoveEntry(aListener);
  4006. }
  4007. }
  4008. #ifdef ACCESSIBILITY
  4009. void
  4010. nsTreeBodyFrame::FireRowCountChangedEvent(int32_t aIndex, int32_t aCount)
  4011. {
  4012. nsCOMPtr<nsIContent> content(GetBaseElement());
  4013. if (!content)
  4014. return;
  4015. nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(content->OwnerDoc());
  4016. if (!domDoc)
  4017. return;
  4018. nsCOMPtr<nsIDOMEvent> event;
  4019. domDoc->CreateEvent(NS_LITERAL_STRING("customevent"),
  4020. getter_AddRefs(event));
  4021. nsCOMPtr<nsIDOMCustomEvent> treeEvent(do_QueryInterface(event));
  4022. if (!treeEvent)
  4023. return;
  4024. nsCOMPtr<nsIWritablePropertyBag2> propBag(
  4025. do_CreateInstance("@mozilla.org/hash-property-bag;1"));
  4026. if (!propBag)
  4027. return;
  4028. // Set 'index' data - the row index rows are changed from.
  4029. propBag->SetPropertyAsInt32(NS_LITERAL_STRING("index"), aIndex);
  4030. // Set 'count' data - the number of changed rows.
  4031. propBag->SetPropertyAsInt32(NS_LITERAL_STRING("count"), aCount);
  4032. RefPtr<nsVariant> detailVariant(new nsVariant());
  4033. detailVariant->SetAsISupports(propBag);
  4034. treeEvent->InitCustomEvent(NS_LITERAL_STRING("TreeRowCountChanged"),
  4035. true, false, detailVariant);
  4036. event->SetTrusted(true);
  4037. RefPtr<AsyncEventDispatcher> asyncDispatcher =
  4038. new AsyncEventDispatcher(content, event);
  4039. asyncDispatcher->PostDOMEvent();
  4040. }
  4041. void
  4042. nsTreeBodyFrame::FireInvalidateEvent(int32_t aStartRowIdx, int32_t aEndRowIdx,
  4043. nsITreeColumn *aStartCol,
  4044. nsITreeColumn *aEndCol)
  4045. {
  4046. nsCOMPtr<nsIContent> content(GetBaseElement());
  4047. if (!content)
  4048. return;
  4049. nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(content->OwnerDoc());
  4050. if (!domDoc)
  4051. return;
  4052. nsCOMPtr<nsIDOMEvent> event;
  4053. domDoc->CreateEvent(NS_LITERAL_STRING("customevent"),
  4054. getter_AddRefs(event));
  4055. nsCOMPtr<nsIDOMCustomEvent> treeEvent(do_QueryInterface(event));
  4056. if (!treeEvent)
  4057. return;
  4058. nsCOMPtr<nsIWritablePropertyBag2> propBag(
  4059. do_CreateInstance("@mozilla.org/hash-property-bag;1"));
  4060. if (!propBag)
  4061. return;
  4062. if (aStartRowIdx != -1 && aEndRowIdx != -1) {
  4063. // Set 'startrow' data - the start index of invalidated rows.
  4064. propBag->SetPropertyAsInt32(NS_LITERAL_STRING("startrow"),
  4065. aStartRowIdx);
  4066. // Set 'endrow' data - the end index of invalidated rows.
  4067. propBag->SetPropertyAsInt32(NS_LITERAL_STRING("endrow"),
  4068. aEndRowIdx);
  4069. }
  4070. if (aStartCol && aEndCol) {
  4071. // Set 'startcolumn' data - the start index of invalidated rows.
  4072. int32_t startColIdx = 0;
  4073. nsresult rv = aStartCol->GetIndex(&startColIdx);
  4074. if (NS_FAILED(rv))
  4075. return;
  4076. propBag->SetPropertyAsInt32(NS_LITERAL_STRING("startcolumn"),
  4077. startColIdx);
  4078. // Set 'endcolumn' data - the start index of invalidated rows.
  4079. int32_t endColIdx = 0;
  4080. rv = aEndCol->GetIndex(&endColIdx);
  4081. if (NS_FAILED(rv))
  4082. return;
  4083. propBag->SetPropertyAsInt32(NS_LITERAL_STRING("endcolumn"),
  4084. endColIdx);
  4085. }
  4086. RefPtr<nsVariant> detailVariant(new nsVariant());
  4087. detailVariant->SetAsISupports(propBag);
  4088. treeEvent->InitCustomEvent(NS_LITERAL_STRING("TreeInvalidated"),
  4089. true, false, detailVariant);
  4090. event->SetTrusted(true);
  4091. RefPtr<AsyncEventDispatcher> asyncDispatcher =
  4092. new AsyncEventDispatcher(content, event);
  4093. asyncDispatcher->PostDOMEvent();
  4094. }
  4095. #endif
  4096. class nsOverflowChecker : public Runnable
  4097. {
  4098. public:
  4099. explicit nsOverflowChecker(nsTreeBodyFrame* aFrame) : mFrame(aFrame) {}
  4100. NS_IMETHOD Run() override
  4101. {
  4102. if (mFrame.IsAlive()) {
  4103. nsTreeBodyFrame* tree = static_cast<nsTreeBodyFrame*>(mFrame.GetFrame());
  4104. nsTreeBodyFrame::ScrollParts parts = tree->GetScrollParts();
  4105. tree->CheckOverflow(parts);
  4106. }
  4107. return NS_OK;
  4108. }
  4109. private:
  4110. nsWeakFrame mFrame;
  4111. };
  4112. bool
  4113. nsTreeBodyFrame::FullScrollbarsUpdate(bool aNeedsFullInvalidation)
  4114. {
  4115. ScrollParts parts = GetScrollParts();
  4116. nsWeakFrame weakFrame(this);
  4117. nsWeakFrame weakColumnsFrame(parts.mColumnsFrame);
  4118. UpdateScrollbars(parts);
  4119. NS_ENSURE_TRUE(weakFrame.IsAlive(), false);
  4120. if (aNeedsFullInvalidation) {
  4121. Invalidate();
  4122. }
  4123. InvalidateScrollbars(parts, weakColumnsFrame);
  4124. NS_ENSURE_TRUE(weakFrame.IsAlive(), false);
  4125. // Overflow checking dispatches synchronous events, which can cause infinite
  4126. // recursion during reflow. Do the first overflow check synchronously, but
  4127. // force any nested checks to round-trip through the event loop. See bug
  4128. // 905909.
  4129. RefPtr<nsOverflowChecker> checker = new nsOverflowChecker(this);
  4130. if (!mCheckingOverflow) {
  4131. nsContentUtils::AddScriptRunner(checker);
  4132. } else {
  4133. NS_DispatchToCurrentThread(checker);
  4134. }
  4135. return weakFrame.IsAlive();
  4136. }
  4137. nsresult
  4138. nsTreeBodyFrame::OnImageIsAnimated(imgIRequest* aRequest)
  4139. {
  4140. nsLayoutUtils::RegisterImageRequest(PresContext(),
  4141. aRequest, nullptr);
  4142. return NS_OK;
  4143. }