nsComboboxControlFrame.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. /* -*- Mode: C++; tab-width: 2; 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 "nsComboboxControlFrame.h"
  6. #include "gfxUtils.h"
  7. #include "mozilla/gfx/2D.h"
  8. #include "mozilla/gfx/PathHelpers.h"
  9. #include "nsCOMPtr.h"
  10. #include "nsFocusManager.h"
  11. #include "nsFormControlFrame.h"
  12. #include "nsGkAtoms.h"
  13. #include "nsCSSAnonBoxes.h"
  14. #include "nsHTMLParts.h"
  15. #include "nsIFormControl.h"
  16. #include "nsNameSpaceManager.h"
  17. #include "nsIListControlFrame.h"
  18. #include "nsPIDOMWindow.h"
  19. #include "nsIPresShell.h"
  20. #include "nsPresState.h"
  21. #include "nsContentList.h"
  22. #include "nsView.h"
  23. #include "nsViewManager.h"
  24. #include "nsIDOMEventListener.h"
  25. #include "nsIDOMNode.h"
  26. #include "nsISelectControlFrame.h"
  27. #include "nsContentUtils.h"
  28. #include "nsIDocument.h"
  29. #include "nsIScrollableFrame.h"
  30. #include "nsListControlFrame.h"
  31. #include "mozilla/StyleSetHandle.h"
  32. #include "mozilla/StyleSetHandleInlines.h"
  33. #include "nsNodeInfoManager.h"
  34. #include "nsContentCreatorFunctions.h"
  35. #include "nsLayoutUtils.h"
  36. #include "nsDisplayList.h"
  37. #include "nsITheme.h"
  38. #include "nsThemeConstants.h"
  39. #include "nsRenderingContext.h"
  40. #include "mozilla/Likely.h"
  41. #include <algorithm>
  42. #include "nsTextNode.h"
  43. #include "mozilla/AsyncEventDispatcher.h"
  44. #include "mozilla/EventStates.h"
  45. #include "mozilla/LookAndFeel.h"
  46. #include "mozilla/MouseEvents.h"
  47. #include "mozilla/Unused.h"
  48. #include "gfx2DGlue.h"
  49. #ifdef XP_WIN
  50. #define COMBOBOX_ROLLUP_CONSUME_EVENT 0
  51. #else
  52. #define COMBOBOX_ROLLUP_CONSUME_EVENT 1
  53. #endif
  54. using namespace mozilla;
  55. using namespace mozilla::gfx;
  56. NS_IMETHODIMP
  57. nsComboboxControlFrame::RedisplayTextEvent::Run()
  58. {
  59. if (mControlFrame)
  60. mControlFrame->HandleRedisplayTextEvent();
  61. return NS_OK;
  62. }
  63. class nsPresState;
  64. #define FIX_FOR_BUG_53259
  65. // Drop down list event management.
  66. // The combo box uses the following strategy for managing the drop-down list.
  67. // If the combo box or its arrow button is clicked on the drop-down list is displayed
  68. // If mouse exits the combo box with the drop-down list displayed the drop-down list
  69. // is asked to capture events
  70. // The drop-down list will capture all events including mouse down and up and will always
  71. // return with ListWasSelected method call regardless of whether an item in the list was
  72. // actually selected.
  73. // The ListWasSelected code will turn off mouse-capture for the drop-down list.
  74. // The drop-down list does not explicitly set capture when it is in the drop-down mode.
  75. /**
  76. * Helper class that listens to the combo boxes button. If the button is pressed the
  77. * combo box is toggled to open or close. this is used by Accessibility which presses
  78. * that button Programmatically.
  79. */
  80. class nsComboButtonListener : public nsIDOMEventListener
  81. {
  82. private:
  83. virtual ~nsComboButtonListener() {}
  84. public:
  85. NS_DECL_ISUPPORTS
  86. NS_IMETHOD HandleEvent(nsIDOMEvent*) override
  87. {
  88. mComboBox->ShowDropDown(!mComboBox->IsDroppedDown());
  89. return NS_OK;
  90. }
  91. explicit nsComboButtonListener(nsComboboxControlFrame* aCombobox)
  92. {
  93. mComboBox = aCombobox;
  94. }
  95. nsComboboxControlFrame* mComboBox;
  96. };
  97. NS_IMPL_ISUPPORTS(nsComboButtonListener,
  98. nsIDOMEventListener)
  99. // static class data member for Bug 32920
  100. nsComboboxControlFrame* nsComboboxControlFrame::sFocused = nullptr;
  101. nsComboboxControlFrame*
  102. NS_NewComboboxControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext, nsFrameState aStateFlags)
  103. {
  104. nsComboboxControlFrame* it = new (aPresShell) nsComboboxControlFrame(aContext);
  105. if (it) {
  106. // set the state flags (if any are provided)
  107. it->AddStateBits(aStateFlags);
  108. }
  109. return it;
  110. }
  111. NS_IMPL_FRAMEARENA_HELPERS(nsComboboxControlFrame)
  112. //-----------------------------------------------------------
  113. // Reflow Debugging Macros
  114. // These let us "see" how many reflow counts are happening
  115. //-----------------------------------------------------------
  116. #ifdef DO_REFLOW_COUNTER
  117. #define MAX_REFLOW_CNT 1024
  118. static int32_t gTotalReqs = 0;;
  119. static int32_t gTotalReflows = 0;;
  120. static int32_t gReflowControlCntRQ[MAX_REFLOW_CNT];
  121. static int32_t gReflowControlCnt[MAX_REFLOW_CNT];
  122. static int32_t gReflowInx = -1;
  123. #define REFLOW_COUNTER() \
  124. if (mReflowId > -1) \
  125. gReflowControlCnt[mReflowId]++;
  126. #define REFLOW_COUNTER_REQUEST() \
  127. if (mReflowId > -1) \
  128. gReflowControlCntRQ[mReflowId]++;
  129. #define REFLOW_COUNTER_DUMP(__desc) \
  130. if (mReflowId > -1) {\
  131. gTotalReqs += gReflowControlCntRQ[mReflowId];\
  132. gTotalReflows += gReflowControlCnt[mReflowId];\
  133. printf("** Id:%5d %s RF: %d RQ: %d %d/%d %5.2f\n", \
  134. mReflowId, (__desc), \
  135. gReflowControlCnt[mReflowId], \
  136. gReflowControlCntRQ[mReflowId],\
  137. gTotalReflows, gTotalReqs, float(gTotalReflows)/float(gTotalReqs)*100.0f);\
  138. }
  139. #define REFLOW_COUNTER_INIT() \
  140. if (gReflowInx < MAX_REFLOW_CNT) { \
  141. gReflowInx++; \
  142. mReflowId = gReflowInx; \
  143. gReflowControlCnt[mReflowId] = 0; \
  144. gReflowControlCntRQ[mReflowId] = 0; \
  145. } else { \
  146. mReflowId = -1; \
  147. }
  148. // reflow messages
  149. #define REFLOW_DEBUG_MSG(_msg1) printf((_msg1))
  150. #define REFLOW_DEBUG_MSG2(_msg1, _msg2) printf((_msg1), (_msg2))
  151. #define REFLOW_DEBUG_MSG3(_msg1, _msg2, _msg3) printf((_msg1), (_msg2), (_msg3))
  152. #define REFLOW_DEBUG_MSG4(_msg1, _msg2, _msg3, _msg4) printf((_msg1), (_msg2), (_msg3), (_msg4))
  153. #else //-------------
  154. #define REFLOW_COUNTER_REQUEST()
  155. #define REFLOW_COUNTER()
  156. #define REFLOW_COUNTER_DUMP(__desc)
  157. #define REFLOW_COUNTER_INIT()
  158. #define REFLOW_DEBUG_MSG(_msg)
  159. #define REFLOW_DEBUG_MSG2(_msg1, _msg2)
  160. #define REFLOW_DEBUG_MSG3(_msg1, _msg2, _msg3)
  161. #define REFLOW_DEBUG_MSG4(_msg1, _msg2, _msg3, _msg4)
  162. #endif
  163. //------------------------------------------
  164. // This is for being VERY noisy
  165. //------------------------------------------
  166. #ifdef DO_VERY_NOISY
  167. #define REFLOW_NOISY_MSG(_msg1) printf((_msg1))
  168. #define REFLOW_NOISY_MSG2(_msg1, _msg2) printf((_msg1), (_msg2))
  169. #define REFLOW_NOISY_MSG3(_msg1, _msg2, _msg3) printf((_msg1), (_msg2), (_msg3))
  170. #define REFLOW_NOISY_MSG4(_msg1, _msg2, _msg3, _msg4) printf((_msg1), (_msg2), (_msg3), (_msg4))
  171. #else
  172. #define REFLOW_NOISY_MSG(_msg)
  173. #define REFLOW_NOISY_MSG2(_msg1, _msg2)
  174. #define REFLOW_NOISY_MSG3(_msg1, _msg2, _msg3)
  175. #define REFLOW_NOISY_MSG4(_msg1, _msg2, _msg3, _msg4)
  176. #endif
  177. //------------------------------------------
  178. // Displays value in pixels or twips
  179. //------------------------------------------
  180. #ifdef DO_PIXELS
  181. #define PX(__v) __v / 15
  182. #else
  183. #define PX(__v) __v
  184. #endif
  185. //------------------------------------------------------
  186. //-- Done with macros
  187. //------------------------------------------------------
  188. nsComboboxControlFrame::nsComboboxControlFrame(nsStyleContext* aContext)
  189. : nsBlockFrame(aContext)
  190. , mDisplayFrame(nullptr)
  191. , mButtonFrame(nullptr)
  192. , mDropdownFrame(nullptr)
  193. , mListControlFrame(nullptr)
  194. , mDisplayISize(0)
  195. , mRecentSelectedIndex(NS_SKIP_NOTIFY_INDEX)
  196. , mDisplayedIndex(-1)
  197. , mLastDropDownBeforeScreenBCoord(nscoord_MIN)
  198. , mLastDropDownAfterScreenBCoord(nscoord_MIN)
  199. , mDroppedDown(false)
  200. , mInRedisplayText(false)
  201. , mDelayedShowDropDown(false)
  202. , mIsOpenInParentProcess(false)
  203. {
  204. REFLOW_COUNTER_INIT()
  205. }
  206. //--------------------------------------------------------------
  207. nsComboboxControlFrame::~nsComboboxControlFrame()
  208. {
  209. REFLOW_COUNTER_DUMP("nsCCF");
  210. }
  211. //--------------------------------------------------------------
  212. NS_QUERYFRAME_HEAD(nsComboboxControlFrame)
  213. NS_QUERYFRAME_ENTRY(nsComboboxControlFrame)
  214. NS_QUERYFRAME_ENTRY(nsIComboboxControlFrame)
  215. NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
  216. NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
  217. NS_QUERYFRAME_ENTRY(nsISelectControlFrame)
  218. NS_QUERYFRAME_ENTRY(nsIStatefulFrame)
  219. NS_QUERYFRAME_TAIL_INHERITING(nsBlockFrame)
  220. #ifdef ACCESSIBILITY
  221. a11y::AccType
  222. nsComboboxControlFrame::AccessibleType()
  223. {
  224. return a11y::eHTMLComboboxType;
  225. }
  226. #endif
  227. void
  228. nsComboboxControlFrame::SetFocus(bool aOn, bool aRepaint)
  229. {
  230. nsWeakFrame weakFrame(this);
  231. if (aOn) {
  232. nsListControlFrame::ComboboxFocusSet();
  233. sFocused = this;
  234. if (mDelayedShowDropDown) {
  235. ShowDropDown(true); // might destroy us
  236. if (!weakFrame.IsAlive()) {
  237. return;
  238. }
  239. }
  240. } else {
  241. sFocused = nullptr;
  242. mDelayedShowDropDown = false;
  243. if (mDroppedDown) {
  244. mListControlFrame->ComboboxFinish(mDisplayedIndex); // might destroy us
  245. if (!weakFrame.IsAlive()) {
  246. return;
  247. }
  248. }
  249. // May delete |this|.
  250. mListControlFrame->FireOnInputAndOnChange();
  251. }
  252. if (!weakFrame.IsAlive()) {
  253. return;
  254. }
  255. // This is needed on a temporary basis. It causes the focus
  256. // rect to be drawn. This is much faster than ReResolvingStyle
  257. // Bug 32920
  258. InvalidateFrame();
  259. }
  260. void
  261. nsComboboxControlFrame::ShowPopup(bool aShowPopup)
  262. {
  263. nsView* view = mDropdownFrame->GetView();
  264. nsViewManager* viewManager = view->GetViewManager();
  265. if (aShowPopup) {
  266. nsRect rect = mDropdownFrame->GetRect();
  267. rect.x = rect.y = 0;
  268. viewManager->ResizeView(view, rect);
  269. viewManager->SetViewVisibility(view, nsViewVisibility_kShow);
  270. } else {
  271. viewManager->SetViewVisibility(view, nsViewVisibility_kHide);
  272. nsRect emptyRect(0, 0, 0, 0);
  273. viewManager->ResizeView(view, emptyRect);
  274. }
  275. // fire a popup dom event if it is safe to do so
  276. nsCOMPtr<nsIPresShell> shell = PresContext()->GetPresShell();
  277. if (shell && nsContentUtils::IsSafeToRunScript()) {
  278. nsEventStatus status = nsEventStatus_eIgnore;
  279. WidgetMouseEvent event(true, aShowPopup ? eXULPopupShowing : eXULPopupHiding,
  280. nullptr, WidgetMouseEvent::eReal);
  281. shell->HandleDOMEventWithTarget(mContent, &event, &status);
  282. }
  283. }
  284. bool
  285. nsComboboxControlFrame::ShowList(bool aShowList)
  286. {
  287. nsView* view = mDropdownFrame->GetView();
  288. if (aShowList) {
  289. NS_ASSERTION(!view->HasWidget(),
  290. "We shouldn't have a widget before we need to display the popup");
  291. // Create the widget for the drop-down list
  292. view->GetViewManager()->SetViewFloating(view, true);
  293. nsWidgetInitData widgetData;
  294. widgetData.mWindowType = eWindowType_popup;
  295. widgetData.mBorderStyle = eBorderStyle_default;
  296. view->CreateWidgetForPopup(&widgetData);
  297. } else {
  298. nsIWidget* widget = view->GetWidget();
  299. if (widget) {
  300. // We must do this before ShowPopup in case it destroys us (bug 813442).
  301. widget->CaptureRollupEvents(this, false);
  302. }
  303. }
  304. nsWeakFrame weakFrame(this);
  305. ShowPopup(aShowList); // might destroy us
  306. if (!weakFrame.IsAlive()) {
  307. return false;
  308. }
  309. mDroppedDown = aShowList;
  310. nsIWidget* widget = view->GetWidget();
  311. if (mDroppedDown) {
  312. // The listcontrol frame will call back to the nsComboboxControlFrame's
  313. // ListWasSelected which will stop the capture.
  314. mListControlFrame->AboutToDropDown();
  315. mListControlFrame->CaptureMouseEvents(true);
  316. if (widget) {
  317. widget->CaptureRollupEvents(this, true);
  318. }
  319. } else {
  320. if (widget) {
  321. view->DestroyWidget();
  322. }
  323. }
  324. return weakFrame.IsAlive();
  325. }
  326. class nsResizeDropdownAtFinalPosition final
  327. : public nsIReflowCallback, public Runnable
  328. {
  329. public:
  330. explicit nsResizeDropdownAtFinalPosition(nsComboboxControlFrame* aFrame)
  331. : mFrame(aFrame)
  332. {
  333. MOZ_COUNT_CTOR(nsResizeDropdownAtFinalPosition);
  334. }
  335. protected:
  336. ~nsResizeDropdownAtFinalPosition()
  337. {
  338. MOZ_COUNT_DTOR(nsResizeDropdownAtFinalPosition);
  339. }
  340. public:
  341. virtual bool ReflowFinished() override
  342. {
  343. Run();
  344. NS_RELEASE_THIS();
  345. return false;
  346. }
  347. virtual void ReflowCallbackCanceled() override
  348. {
  349. NS_RELEASE_THIS();
  350. }
  351. NS_IMETHOD Run() override
  352. {
  353. if (mFrame.IsAlive()) {
  354. static_cast<nsComboboxControlFrame*>(mFrame.GetFrame())->
  355. AbsolutelyPositionDropDown();
  356. }
  357. return NS_OK;
  358. }
  359. nsWeakFrame mFrame;
  360. };
  361. void
  362. nsComboboxControlFrame::ReflowDropdown(nsPresContext* aPresContext,
  363. const ReflowInput& aReflowInput)
  364. {
  365. // All we want out of it later on, really, is the block size of a row, so we
  366. // don't even need to cache mDropdownFrame's ascent or anything. If we don't
  367. // need to reflow it, just bail out here.
  368. if (!aReflowInput.ShouldReflowAllKids() &&
  369. !NS_SUBTREE_DIRTY(mDropdownFrame)) {
  370. return;
  371. }
  372. // XXXbz this will, for small-block-size dropdowns, have extra space
  373. // on the appropriate edge for the scrollbar we don't show... but
  374. // that's the best we can do here for now.
  375. WritingMode wm = mDropdownFrame->GetWritingMode();
  376. LogicalSize availSize = aReflowInput.AvailableSize(wm);
  377. availSize.BSize(wm) = NS_UNCONSTRAINEDSIZE;
  378. ReflowInput kidReflowInput(aPresContext, aReflowInput, mDropdownFrame,
  379. availSize);
  380. // If the dropdown's intrinsic inline size is narrower than our
  381. // specified inline size, then expand it out. We want our border-box
  382. // inline size to end up the same as the dropdown's so account for
  383. // both sets of mComputedBorderPadding.
  384. nscoord forcedISize = aReflowInput.ComputedISize() +
  385. aReflowInput.ComputedLogicalBorderPadding().IStartEnd(wm) -
  386. kidReflowInput.ComputedLogicalBorderPadding().IStartEnd(wm);
  387. kidReflowInput.SetComputedISize(std::max(kidReflowInput.ComputedISize(),
  388. forcedISize));
  389. // ensure we start off hidden
  390. if (!mDroppedDown && GetStateBits() & NS_FRAME_FIRST_REFLOW) {
  391. nsView* view = mDropdownFrame->GetView();
  392. nsViewManager* viewManager = view->GetViewManager();
  393. viewManager->SetViewVisibility(view, nsViewVisibility_kHide);
  394. nsRect emptyRect(0, 0, 0, 0);
  395. viewManager->ResizeView(view, emptyRect);
  396. }
  397. // Allow the child to move/size/change-visibility its view if it's currently
  398. // dropped down
  399. int32_t flags = mDroppedDown ? 0
  400. : NS_FRAME_NO_MOVE_FRAME |
  401. NS_FRAME_NO_VISIBILITY |
  402. NS_FRAME_NO_SIZE_VIEW;
  403. //XXX Can this be different from the dropdown's writing mode?
  404. // That would be odd!
  405. // Note that we don't need to pass the true frame position or container size
  406. // to ReflowChild or FinishReflowChild here; it will be positioned as needed
  407. // by AbsolutelyPositionDropDown().
  408. WritingMode outerWM = GetWritingMode();
  409. const nsSize dummyContainerSize;
  410. ReflowOutput desiredSize(aReflowInput);
  411. nsReflowStatus ignoredStatus;
  412. ReflowChild(mDropdownFrame, aPresContext, desiredSize,
  413. kidReflowInput, outerWM, LogicalPoint(outerWM),
  414. dummyContainerSize, flags, ignoredStatus);
  415. // Set the child's width and height to its desired size
  416. FinishReflowChild(mDropdownFrame, aPresContext, desiredSize, &kidReflowInput,
  417. outerWM, LogicalPoint(outerWM), dummyContainerSize, flags);
  418. }
  419. nsPoint
  420. nsComboboxControlFrame::GetCSSTransformTranslation()
  421. {
  422. nsIFrame* frame = this;
  423. bool is3DTransform = false;
  424. Matrix transform;
  425. while (frame) {
  426. nsIFrame* parent;
  427. Matrix4x4 ctm = frame->GetTransformMatrix(nullptr, &parent);
  428. Matrix matrix;
  429. if (ctm.Is2D(&matrix)) {
  430. transform = transform * matrix;
  431. } else {
  432. is3DTransform = true;
  433. break;
  434. }
  435. frame = parent;
  436. }
  437. nsPoint translation;
  438. if (!is3DTransform && !transform.HasNonTranslation()) {
  439. nsPresContext* pc = PresContext();
  440. // To get the translation introduced only by transforms we subtract the
  441. // regular non-transform translation.
  442. nsRootPresContext* rootPC = pc->GetRootPresContext();
  443. if (rootPC) {
  444. int32_t apd = pc->AppUnitsPerDevPixel();
  445. translation.x = NSFloatPixelsToAppUnits(transform._31, apd);
  446. translation.y = NSFloatPixelsToAppUnits(transform._32, apd);
  447. translation -= GetOffsetToCrossDoc(rootPC->PresShell()->GetRootFrame());
  448. }
  449. }
  450. return translation;
  451. }
  452. class nsAsyncRollup : public Runnable
  453. {
  454. public:
  455. explicit nsAsyncRollup(nsComboboxControlFrame* aFrame) : mFrame(aFrame) {}
  456. NS_IMETHOD Run() override
  457. {
  458. if (mFrame.IsAlive()) {
  459. static_cast<nsComboboxControlFrame*>(mFrame.GetFrame())
  460. ->RollupFromList();
  461. }
  462. return NS_OK;
  463. }
  464. nsWeakFrame mFrame;
  465. };
  466. class nsAsyncResize : public Runnable
  467. {
  468. public:
  469. explicit nsAsyncResize(nsComboboxControlFrame* aFrame) : mFrame(aFrame) {}
  470. NS_IMETHOD Run() override
  471. {
  472. if (mFrame.IsAlive()) {
  473. nsComboboxControlFrame* combo =
  474. static_cast<nsComboboxControlFrame*>(mFrame.GetFrame());
  475. static_cast<nsListControlFrame*>(combo->mDropdownFrame)->
  476. SetSuppressScrollbarUpdate(true);
  477. nsCOMPtr<nsIPresShell> shell = mFrame->PresContext()->PresShell();
  478. shell->FrameNeedsReflow(combo->mDropdownFrame, nsIPresShell::eResize,
  479. NS_FRAME_IS_DIRTY);
  480. shell->FlushPendingNotifications(Flush_Layout);
  481. if (mFrame.IsAlive()) {
  482. combo = static_cast<nsComboboxControlFrame*>(mFrame.GetFrame());
  483. static_cast<nsListControlFrame*>(combo->mDropdownFrame)->
  484. SetSuppressScrollbarUpdate(false);
  485. if (combo->mDelayedShowDropDown) {
  486. combo->ShowDropDown(true);
  487. }
  488. }
  489. }
  490. return NS_OK;
  491. }
  492. nsWeakFrame mFrame;
  493. };
  494. void
  495. nsComboboxControlFrame::GetAvailableDropdownSpace(WritingMode aWM,
  496. nscoord* aBefore,
  497. nscoord* aAfter,
  498. LogicalPoint* aTranslation)
  499. {
  500. MOZ_ASSERT(!XRE_IsContentProcess());
  501. // Note: At first glance, it appears that you could simply get the
  502. // absolute bounding box for the dropdown list by first getting its
  503. // view, then getting the view's nsIWidget, then asking the nsIWidget
  504. // for its AbsoluteBounds.
  505. // The problem with this approach, is that the dropdown list's bcoord
  506. // location can change based on whether the dropdown is placed after
  507. // or before the display frame. The approach taken here is to get the
  508. // absolute position of the display frame and use its location to
  509. // determine if the dropdown will go offscreen.
  510. // Normal frame geometry (eg GetOffsetTo, mRect) doesn't include transforms.
  511. // In the special case that our transform is only a 2D translation we
  512. // introduce this hack so that the dropdown will show up in the right place.
  513. // Use null container size when converting a vector from logical to physical.
  514. const nsSize nullContainerSize;
  515. *aTranslation = LogicalPoint(aWM, GetCSSTransformTranslation(),
  516. nullContainerSize);
  517. *aBefore = 0;
  518. *aAfter = 0;
  519. nsRect screen = nsFormControlFrame::GetUsableScreenRect(PresContext());
  520. nsSize containerSize = screen.Size();
  521. LogicalRect logicalScreen(aWM, screen, containerSize);
  522. if (mLastDropDownAfterScreenBCoord == nscoord_MIN) {
  523. LogicalRect thisScreenRect(aWM, GetScreenRectInAppUnits(),
  524. containerSize);
  525. mLastDropDownAfterScreenBCoord = thisScreenRect.BEnd(aWM) +
  526. aTranslation->B(aWM);
  527. mLastDropDownBeforeScreenBCoord = thisScreenRect.BStart(aWM) +
  528. aTranslation->B(aWM);
  529. }
  530. nscoord minBCoord;
  531. nsPresContext* pc = PresContext()->GetToplevelContentDocumentPresContext();
  532. nsIFrame* root = pc ? pc->PresShell()->GetRootFrame() : nullptr;
  533. if (root) {
  534. minBCoord = LogicalRect(aWM,
  535. root->GetScreenRectInAppUnits(),
  536. containerSize).BStart(aWM);
  537. if (mLastDropDownAfterScreenBCoord < minBCoord) {
  538. // Don't allow the drop-down to be placed before the content area.
  539. return;
  540. }
  541. } else {
  542. minBCoord = logicalScreen.BStart(aWM);
  543. }
  544. nscoord after = logicalScreen.BEnd(aWM) - mLastDropDownAfterScreenBCoord;
  545. nscoord before = mLastDropDownBeforeScreenBCoord - minBCoord;
  546. // If the difference between the space before and after is less
  547. // than a row-block-size, then we favor the space after.
  548. if (before >= after) {
  549. nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
  550. nscoord rowBSize = lcf->GetBSizeOfARow();
  551. if (before < after + rowBSize) {
  552. before -= rowBSize;
  553. }
  554. }
  555. *aAfter = after;
  556. *aBefore = before;
  557. }
  558. nsComboboxControlFrame::DropDownPositionState
  559. nsComboboxControlFrame::AbsolutelyPositionDropDown()
  560. {
  561. if (XRE_IsContentProcess()) {
  562. return eDropDownPositionSuppressed;
  563. }
  564. WritingMode wm = GetWritingMode();
  565. LogicalPoint translation(wm);
  566. nscoord before, after;
  567. mLastDropDownAfterScreenBCoord = nscoord_MIN;
  568. GetAvailableDropdownSpace(wm, &before, &after, &translation);
  569. if (before <= 0 && after <= 0) {
  570. if (IsDroppedDown()) {
  571. // Hide the view immediately to minimize flicker.
  572. nsView* view = mDropdownFrame->GetView();
  573. view->GetViewManager()->SetViewVisibility(view, nsViewVisibility_kHide);
  574. NS_DispatchToCurrentThread(new nsAsyncRollup(this));
  575. }
  576. return eDropDownPositionSuppressed;
  577. }
  578. LogicalSize dropdownSize = mDropdownFrame->GetLogicalSize(wm);
  579. nscoord bSize = std::max(before, after);
  580. nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
  581. if (bSize < dropdownSize.BSize(wm)) {
  582. if (lcf->GetNumDisplayRows() > 1) {
  583. // The drop-down doesn't fit and currently shows more than 1 row -
  584. // schedule a resize to show fewer rows.
  585. NS_DispatchToCurrentThread(new nsAsyncResize(this));
  586. return eDropDownPositionPendingResize;
  587. }
  588. } else if (bSize > (dropdownSize.BSize(wm) + lcf->GetBSizeOfARow() * 1.5) &&
  589. lcf->GetDropdownCanGrow()) {
  590. // The drop-down fits but there is room for at least 1.5 more rows -
  591. // schedule a resize to show more rows if it has more rows to show.
  592. // (1.5 rows for good measure to avoid any rounding issues that would
  593. // lead to a loop of reflow requests)
  594. NS_DispatchToCurrentThread(new nsAsyncResize(this));
  595. return eDropDownPositionPendingResize;
  596. }
  597. // Position the drop-down after if there is room, otherwise place it before
  598. // if there is room. If there is no room for it on either side then place
  599. // it after (to avoid overlapping UI like the URL bar).
  600. bool b = dropdownSize.BSize(wm)<= after || dropdownSize.BSize(wm) > before;
  601. LogicalPoint dropdownPosition(wm, 0, b ? BSize(wm) : -dropdownSize.BSize(wm));
  602. // Don't position the view unless the position changed since it might cause
  603. // a call to NotifyGeometryChange() and an infinite loop here.
  604. nsSize containerSize = GetSize();
  605. const LogicalPoint currentPos =
  606. mDropdownFrame->GetLogicalPosition(containerSize);
  607. const LogicalPoint newPos = dropdownPosition + translation;
  608. if (currentPos != newPos) {
  609. mDropdownFrame->SetPosition(wm, newPos, containerSize);
  610. nsContainerFrame::PositionFrameView(mDropdownFrame);
  611. }
  612. return eDropDownPositionFinal;
  613. }
  614. void
  615. nsComboboxControlFrame::NotifyGeometryChange()
  616. {
  617. if (XRE_IsContentProcess()) {
  618. return;
  619. }
  620. // We don't need to resize if we're not dropped down since ShowDropDown
  621. // does that, or if we're dirty then the reflow callback does it,
  622. // or if we have a delayed ShowDropDown pending.
  623. if (IsDroppedDown() &&
  624. !(GetStateBits() & NS_FRAME_IS_DIRTY) &&
  625. !mDelayedShowDropDown) {
  626. // Async because we're likely in a middle of a scroll here so
  627. // frame/view positions are in flux.
  628. RefPtr<nsResizeDropdownAtFinalPosition> resize =
  629. new nsResizeDropdownAtFinalPosition(this);
  630. NS_DispatchToCurrentThread(resize);
  631. }
  632. }
  633. //----------------------------------------------------------
  634. //
  635. //----------------------------------------------------------
  636. #ifdef DO_REFLOW_DEBUG
  637. static int myCounter = 0;
  638. static void printSize(char * aDesc, nscoord aSize)
  639. {
  640. printf(" %s: ", aDesc);
  641. if (aSize == NS_UNCONSTRAINEDSIZE) {
  642. printf("UC");
  643. } else {
  644. printf("%d", PX(aSize));
  645. }
  646. }
  647. #endif
  648. //-------------------------------------------------------------------
  649. //-- Main Reflow for the Combobox
  650. //-------------------------------------------------------------------
  651. nscoord
  652. nsComboboxControlFrame::GetIntrinsicISize(nsRenderingContext* aRenderingContext,
  653. nsLayoutUtils::IntrinsicISizeType aType)
  654. {
  655. // get the scrollbar width, we'll use this later
  656. nscoord scrollbarWidth = 0;
  657. nsPresContext* presContext = PresContext();
  658. if (mListControlFrame) {
  659. nsIScrollableFrame* scrollable = do_QueryFrame(mListControlFrame);
  660. NS_ASSERTION(scrollable, "List must be a scrollable frame");
  661. scrollbarWidth = scrollable->GetNondisappearingScrollbarWidth(
  662. presContext, aRenderingContext, GetWritingMode());
  663. }
  664. nscoord displayISize = 0;
  665. if (MOZ_LIKELY(mDisplayFrame)) {
  666. displayISize = nsLayoutUtils::IntrinsicForContainer(aRenderingContext,
  667. mDisplayFrame,
  668. aType);
  669. }
  670. if (mDropdownFrame) {
  671. nscoord dropdownContentISize;
  672. bool isUsingOverlayScrollbars =
  673. LookAndFeel::GetInt(LookAndFeel::eIntID_UseOverlayScrollbars) != 0;
  674. if (aType == nsLayoutUtils::MIN_ISIZE) {
  675. dropdownContentISize = mDropdownFrame->GetMinISize(aRenderingContext);
  676. if (isUsingOverlayScrollbars) {
  677. dropdownContentISize += scrollbarWidth;
  678. }
  679. } else {
  680. NS_ASSERTION(aType == nsLayoutUtils::PREF_ISIZE, "Unexpected type");
  681. dropdownContentISize = mDropdownFrame->GetPrefISize(aRenderingContext);
  682. if (isUsingOverlayScrollbars) {
  683. dropdownContentISize += scrollbarWidth;
  684. }
  685. }
  686. dropdownContentISize = NSCoordSaturatingSubtract(dropdownContentISize,
  687. scrollbarWidth,
  688. nscoord_MAX);
  689. displayISize = std::max(dropdownContentISize, displayISize);
  690. }
  691. // add room for the dropmarker button if there is one
  692. if ((!IsThemed() ||
  693. presContext->GetTheme()->ThemeNeedsComboboxDropmarker()) &&
  694. StyleDisplay()->mAppearance != NS_THEME_NONE) {
  695. displayISize += scrollbarWidth;
  696. }
  697. return displayISize;
  698. }
  699. nscoord
  700. nsComboboxControlFrame::GetMinISize(nsRenderingContext *aRenderingContext)
  701. {
  702. nscoord minISize;
  703. DISPLAY_MIN_WIDTH(this, minISize);
  704. minISize = GetIntrinsicISize(aRenderingContext, nsLayoutUtils::MIN_ISIZE);
  705. return minISize;
  706. }
  707. nscoord
  708. nsComboboxControlFrame::GetPrefISize(nsRenderingContext *aRenderingContext)
  709. {
  710. nscoord prefISize;
  711. DISPLAY_PREF_WIDTH(this, prefISize);
  712. prefISize = GetIntrinsicISize(aRenderingContext, nsLayoutUtils::PREF_ISIZE);
  713. return prefISize;
  714. }
  715. void
  716. nsComboboxControlFrame::Reflow(nsPresContext* aPresContext,
  717. ReflowOutput& aDesiredSize,
  718. const ReflowInput& aReflowInput,
  719. nsReflowStatus& aStatus)
  720. {
  721. MarkInReflow();
  722. // Constraints we try to satisfy:
  723. // 1) Default inline size of button is the vertical scrollbar size
  724. // 2) If the inline size of button is bigger than our inline size, set
  725. // inline size of button to 0.
  726. // 3) Default block size of button is block size of display area
  727. // 4) Inline size of display area is whatever is left over from our
  728. // inline size after allocating inline size for the button.
  729. // 5) Block Size of display area is GetBSizeOfARow() on the
  730. // mListControlFrame.
  731. if (!mDisplayFrame || !mButtonFrame || !mDropdownFrame) {
  732. NS_ERROR("Why did the frame constructor allow this to happen? Fix it!!");
  733. return;
  734. }
  735. // Make sure the displayed text is the same as the selected option, bug 297389.
  736. int32_t selectedIndex;
  737. nsAutoString selectedOptionText;
  738. if (!mDroppedDown) {
  739. selectedIndex = mListControlFrame->GetSelectedIndex();
  740. }
  741. else {
  742. // In dropped down mode the "selected index" is the hovered menu item,
  743. // we want the last selected item which is |mDisplayedIndex| in this case.
  744. selectedIndex = mDisplayedIndex;
  745. }
  746. if (selectedIndex != -1) {
  747. mListControlFrame->GetOptionText(selectedIndex, selectedOptionText);
  748. }
  749. if (mDisplayedOptionText != selectedOptionText) {
  750. RedisplayText(selectedIndex);
  751. }
  752. // First reflow our dropdown so that we know how tall we should be.
  753. ReflowDropdown(aPresContext, aReflowInput);
  754. RefPtr<nsResizeDropdownAtFinalPosition> resize =
  755. new nsResizeDropdownAtFinalPosition(this);
  756. if (NS_SUCCEEDED(aPresContext->PresShell()->PostReflowCallback(resize))) {
  757. // The reflow callback queue doesn't AddRef so we keep it alive until
  758. // it's released in its ReflowFinished / ReflowCallbackCanceled.
  759. Unused << resize.forget();
  760. }
  761. // Get the width of the vertical scrollbar. That will be the inline
  762. // size of the dropdown button.
  763. WritingMode wm = aReflowInput.GetWritingMode();
  764. nscoord buttonISize;
  765. const nsStyleDisplay *disp = StyleDisplay();
  766. if ((IsThemed(disp) && !aPresContext->GetTheme()->ThemeNeedsComboboxDropmarker()) ||
  767. StyleDisplay()->mAppearance == NS_THEME_NONE) {
  768. buttonISize = 0;
  769. }
  770. else {
  771. nsIScrollableFrame* scrollable = do_QueryFrame(mListControlFrame);
  772. NS_ASSERTION(scrollable, "List must be a scrollable frame");
  773. buttonISize = scrollable->GetNondisappearingScrollbarWidth(
  774. PresContext(), aReflowInput.mRenderingContext, wm);
  775. if (buttonISize > aReflowInput.ComputedISize()) {
  776. buttonISize = 0;
  777. }
  778. }
  779. mDisplayISize = aReflowInput.ComputedISize() - buttonISize;
  780. nsBlockFrame::Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
  781. // The button should occupy the same space as a scrollbar
  782. nsSize containerSize = aDesiredSize.PhysicalSize();
  783. LogicalRect buttonRect = mButtonFrame->GetLogicalRect(containerSize);
  784. buttonRect.IStart(wm) =
  785. aReflowInput.ComputedLogicalBorderPadding().IStartEnd(wm) +
  786. mDisplayISize -
  787. (aReflowInput.ComputedLogicalBorderPadding().IEnd(wm) -
  788. aReflowInput.ComputedLogicalPadding().IEnd(wm));
  789. buttonRect.ISize(wm) = buttonISize;
  790. buttonRect.BStart(wm) = this->GetLogicalUsedBorder(wm).BStart(wm);
  791. buttonRect.BSize(wm) = mDisplayFrame->BSize(wm) +
  792. this->GetLogicalUsedPadding(wm).BStartEnd(wm);
  793. mButtonFrame->SetRect(buttonRect, containerSize);
  794. if (!NS_INLINE_IS_BREAK_BEFORE(aStatus) &&
  795. !NS_FRAME_IS_FULLY_COMPLETE(aStatus)) {
  796. // This frame didn't fit inside a fragmentation container. Splitting
  797. // a nsComboboxControlFrame makes no sense, so we override the status here.
  798. aStatus = NS_FRAME_COMPLETE;
  799. }
  800. }
  801. //--------------------------------------------------------------
  802. nsIAtom*
  803. nsComboboxControlFrame::GetType() const
  804. {
  805. return nsGkAtoms::comboboxControlFrame;
  806. }
  807. #ifdef DEBUG_FRAME_DUMP
  808. nsresult
  809. nsComboboxControlFrame::GetFrameName(nsAString& aResult) const
  810. {
  811. return MakeFrameName(NS_LITERAL_STRING("ComboboxControl"), aResult);
  812. }
  813. #endif
  814. //----------------------------------------------------------------------
  815. // nsIComboboxControlFrame
  816. //----------------------------------------------------------------------
  817. void
  818. nsComboboxControlFrame::ShowDropDown(bool aDoDropDown)
  819. {
  820. MOZ_ASSERT(!XRE_IsContentProcess());
  821. mDelayedShowDropDown = false;
  822. EventStates eventStates = mContent->AsElement()->State();
  823. if (aDoDropDown && eventStates.HasState(NS_EVENT_STATE_DISABLED)) {
  824. return;
  825. }
  826. if (!mDroppedDown && aDoDropDown) {
  827. nsFocusManager* fm = nsFocusManager::GetFocusManager();
  828. if (!fm || fm->GetFocusedContent() == GetContent()) {
  829. DropDownPositionState state = AbsolutelyPositionDropDown();
  830. if (state == eDropDownPositionFinal) {
  831. ShowList(aDoDropDown); // might destroy us
  832. } else if (state == eDropDownPositionPendingResize) {
  833. // Delay until after the resize reflow, see nsAsyncResize.
  834. mDelayedShowDropDown = true;
  835. }
  836. } else {
  837. // Delay until we get focus, see SetFocus().
  838. mDelayedShowDropDown = true;
  839. }
  840. } else if (mDroppedDown && !aDoDropDown) {
  841. ShowList(aDoDropDown); // might destroy us
  842. }
  843. }
  844. void
  845. nsComboboxControlFrame::SetDropDown(nsIFrame* aDropDownFrame)
  846. {
  847. mDropdownFrame = aDropDownFrame;
  848. mListControlFrame = do_QueryFrame(mDropdownFrame);
  849. if (!sFocused && nsContentUtils::IsFocusedContent(GetContent())) {
  850. sFocused = this;
  851. nsListControlFrame::ComboboxFocusSet();
  852. }
  853. }
  854. nsIFrame*
  855. nsComboboxControlFrame::GetDropDown()
  856. {
  857. return mDropdownFrame;
  858. }
  859. ///////////////////////////////////////////////////////////////
  860. NS_IMETHODIMP
  861. nsComboboxControlFrame::RedisplaySelectedText()
  862. {
  863. nsAutoScriptBlocker scriptBlocker;
  864. return RedisplayText(mListControlFrame->GetSelectedIndex());
  865. }
  866. nsresult
  867. nsComboboxControlFrame::RedisplayText(int32_t aIndex)
  868. {
  869. // Get the text to display
  870. if (aIndex != -1) {
  871. mListControlFrame->GetOptionText(aIndex, mDisplayedOptionText);
  872. } else {
  873. mDisplayedOptionText.Truncate();
  874. }
  875. mDisplayedIndex = aIndex;
  876. REFLOW_DEBUG_MSG2("RedisplayText \"%s\"\n",
  877. NS_LossyConvertUTF16toASCII(mDisplayedOptionText).get());
  878. // Send reflow command because the new text maybe larger
  879. nsresult rv = NS_OK;
  880. if (mDisplayContent) {
  881. // Don't call ActuallyDisplayText(true) directly here since that
  882. // could cause recursive frame construction. See bug 283117 and the comment in
  883. // HandleRedisplayTextEvent() below.
  884. // Revoke outstanding events to avoid out-of-order events which could mean
  885. // displaying the wrong text.
  886. mRedisplayTextEvent.Revoke();
  887. NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
  888. "If we happen to run our redisplay event now, we might kill "
  889. "ourselves!");
  890. RefPtr<RedisplayTextEvent> event = new RedisplayTextEvent(this);
  891. mRedisplayTextEvent = event;
  892. nsContentUtils::AddScriptRunner(event);
  893. }
  894. return rv;
  895. }
  896. void
  897. nsComboboxControlFrame::HandleRedisplayTextEvent()
  898. {
  899. // First, make sure that the content model is up to date and we've
  900. // constructed the frames for all our content in the right places.
  901. // Otherwise they'll end up under the wrong insertion frame when we
  902. // ActuallyDisplayText, since that flushes out the content sink by
  903. // calling SetText on a DOM node with aNotify set to true. See bug
  904. // 289730.
  905. nsWeakFrame weakThis(this);
  906. PresContext()->Document()->
  907. FlushPendingNotifications(Flush_ContentAndNotify);
  908. if (!weakThis.IsAlive())
  909. return;
  910. // Redirect frame insertions during this method (see GetContentInsertionFrame())
  911. // so that any reframing that the frame constructor forces upon us is inserted
  912. // into the correct parent (mDisplayFrame). See bug 282607.
  913. NS_PRECONDITION(!mInRedisplayText, "Nested RedisplayText");
  914. mInRedisplayText = true;
  915. mRedisplayTextEvent.Forget();
  916. ActuallyDisplayText(true);
  917. if (!weakThis.IsAlive())
  918. return;
  919. // XXXbz This should perhaps be eResize. Check.
  920. PresContext()->PresShell()->FrameNeedsReflow(mDisplayFrame,
  921. nsIPresShell::eStyleChange,
  922. NS_FRAME_IS_DIRTY);
  923. mInRedisplayText = false;
  924. }
  925. void
  926. nsComboboxControlFrame::ActuallyDisplayText(bool aNotify)
  927. {
  928. if (mDisplayedOptionText.IsEmpty()) {
  929. // Have to use a non-breaking space for line-block-size calculations
  930. // to be right
  931. static const char16_t space = 0xA0;
  932. mDisplayContent->SetText(&space, 1, aNotify);
  933. } else {
  934. mDisplayContent->SetText(mDisplayedOptionText, aNotify);
  935. }
  936. }
  937. int32_t
  938. nsComboboxControlFrame::GetIndexOfDisplayArea()
  939. {
  940. return mDisplayedIndex;
  941. }
  942. //----------------------------------------------------------------------
  943. // nsISelectControlFrame
  944. //----------------------------------------------------------------------
  945. NS_IMETHODIMP
  946. nsComboboxControlFrame::DoneAddingChildren(bool aIsDone)
  947. {
  948. nsISelectControlFrame* listFrame = do_QueryFrame(mDropdownFrame);
  949. if (!listFrame)
  950. return NS_ERROR_FAILURE;
  951. return listFrame->DoneAddingChildren(aIsDone);
  952. }
  953. NS_IMETHODIMP
  954. nsComboboxControlFrame::AddOption(int32_t aIndex)
  955. {
  956. if (aIndex <= mDisplayedIndex) {
  957. ++mDisplayedIndex;
  958. }
  959. nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
  960. return lcf->AddOption(aIndex);
  961. }
  962. NS_IMETHODIMP
  963. nsComboboxControlFrame::RemoveOption(int32_t aIndex)
  964. {
  965. nsWeakFrame weakThis(this);
  966. if (mListControlFrame->GetNumberOfOptions() > 0) {
  967. if (aIndex < mDisplayedIndex) {
  968. --mDisplayedIndex;
  969. } else if (aIndex == mDisplayedIndex) {
  970. mDisplayedIndex = 0; // IE6 compat
  971. RedisplayText(mDisplayedIndex);
  972. }
  973. }
  974. else {
  975. // If we removed the last option, we need to blank things out
  976. RedisplayText(-1);
  977. }
  978. if (!weakThis.IsAlive())
  979. return NS_OK;
  980. nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
  981. return lcf->RemoveOption(aIndex);
  982. }
  983. NS_IMETHODIMP
  984. nsComboboxControlFrame::OnSetSelectedIndex(int32_t aOldIndex, int32_t aNewIndex)
  985. {
  986. nsAutoScriptBlocker scriptBlocker;
  987. RedisplayText(aNewIndex);
  988. NS_ASSERTION(mDropdownFrame, "No dropdown frame!");
  989. nsISelectControlFrame* listFrame = do_QueryFrame(mDropdownFrame);
  990. NS_ASSERTION(listFrame, "No list frame!");
  991. return listFrame->OnSetSelectedIndex(aOldIndex, aNewIndex);
  992. }
  993. // End nsISelectControlFrame
  994. //----------------------------------------------------------------------
  995. nsresult
  996. nsComboboxControlFrame::HandleEvent(nsPresContext* aPresContext,
  997. WidgetGUIEvent* aEvent,
  998. nsEventStatus* aEventStatus)
  999. {
  1000. NS_ENSURE_ARG_POINTER(aEventStatus);
  1001. if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
  1002. return NS_OK;
  1003. }
  1004. EventStates eventStates = mContent->AsElement()->State();
  1005. if (eventStates.HasState(NS_EVENT_STATE_DISABLED)) {
  1006. return NS_OK;
  1007. }
  1008. #if COMBOBOX_ROLLUP_CONSUME_EVENT == 0
  1009. if (aEvent->mMessage == eMouseDown) {
  1010. nsIWidget* widget = GetNearestWidget();
  1011. if (widget && GetContent() == widget->GetLastRollup()) {
  1012. // This event did a Rollup on this control - prevent it from opening
  1013. // the dropdown again!
  1014. *aEventStatus = nsEventStatus_eConsumeNoDefault;
  1015. return NS_OK;
  1016. }
  1017. }
  1018. #endif
  1019. // If we have style that affects how we are selected, feed event down to
  1020. // nsFrame::HandleEvent so that selection takes place when appropriate.
  1021. const nsStyleUserInterface* uiStyle = StyleUserInterface();
  1022. if (uiStyle->mUserInput == StyleUserInput::None ||
  1023. uiStyle->mUserInput == StyleUserInput::Disabled) {
  1024. return nsBlockFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
  1025. }
  1026. return NS_OK;
  1027. }
  1028. nsresult
  1029. nsComboboxControlFrame::SetFormProperty(nsIAtom* aName, const nsAString& aValue)
  1030. {
  1031. nsIFormControlFrame* fcFrame = do_QueryFrame(mDropdownFrame);
  1032. if (!fcFrame) {
  1033. return NS_NOINTERFACE;
  1034. }
  1035. return fcFrame->SetFormProperty(aName, aValue);
  1036. }
  1037. nsContainerFrame*
  1038. nsComboboxControlFrame::GetContentInsertionFrame() {
  1039. return mInRedisplayText ? mDisplayFrame : mDropdownFrame->GetContentInsertionFrame();
  1040. }
  1041. nsresult
  1042. nsComboboxControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
  1043. {
  1044. // The frames used to display the combo box and the button used to popup the dropdown list
  1045. // are created through anonymous content. The dropdown list is not created through anonymous
  1046. // content because its frame is initialized specifically for the drop-down case and it is placed
  1047. // a special list referenced through NS_COMBO_FRAME_POPUP_LIST_INDEX to keep separate from the
  1048. // layout of the display and button.
  1049. //
  1050. // Note: The value attribute of the display content is set when an item is selected in the dropdown list.
  1051. // If the content specified below does not honor the value attribute than nothing will be displayed.
  1052. // For now the content that is created corresponds to two input buttons. It would be better to create the
  1053. // tag as something other than input, but then there isn't any way to create a button frame since it
  1054. // isn't possible to set the display type in CSS2 to create a button frame.
  1055. // create content used for display
  1056. //nsIAtom* tag = NS_Atomize("mozcombodisplay");
  1057. // Add a child text content node for the label
  1058. nsNodeInfoManager *nimgr = mContent->NodeInfo()->NodeInfoManager();
  1059. mDisplayContent = new nsTextNode(nimgr);
  1060. // set the value of the text node
  1061. mDisplayedIndex = mListControlFrame->GetSelectedIndex();
  1062. if (mDisplayedIndex != -1) {
  1063. mListControlFrame->GetOptionText(mDisplayedIndex, mDisplayedOptionText);
  1064. }
  1065. ActuallyDisplayText(false);
  1066. if (!aElements.AppendElement(mDisplayContent))
  1067. return NS_ERROR_OUT_OF_MEMORY;
  1068. mButtonContent = mContent->OwnerDoc()->CreateHTMLElement(nsGkAtoms::button);
  1069. if (!mButtonContent)
  1070. return NS_ERROR_OUT_OF_MEMORY;
  1071. // make someone to listen to the button. If its pressed by someone like Accessibility
  1072. // then open or close the combo box.
  1073. mButtonListener = new nsComboButtonListener(this);
  1074. mButtonContent->AddEventListener(NS_LITERAL_STRING("click"), mButtonListener,
  1075. false, false);
  1076. mButtonContent->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
  1077. NS_LITERAL_STRING("button"), false);
  1078. // Set tabindex="-1" so that the button is not tabbable
  1079. mButtonContent->SetAttr(kNameSpaceID_None, nsGkAtoms::tabindex,
  1080. NS_LITERAL_STRING("-1"), false);
  1081. WritingMode wm = GetWritingMode();
  1082. if (wm.IsVertical()) {
  1083. mButtonContent->SetAttr(kNameSpaceID_None, nsGkAtoms::orientation,
  1084. wm.IsVerticalRL() ? NS_LITERAL_STRING("left")
  1085. : NS_LITERAL_STRING("right"),
  1086. false);
  1087. }
  1088. if (!aElements.AppendElement(mButtonContent))
  1089. return NS_ERROR_OUT_OF_MEMORY;
  1090. return NS_OK;
  1091. }
  1092. void
  1093. nsComboboxControlFrame::AppendAnonymousContentTo(nsTArray<nsIContent*>& aElements,
  1094. uint32_t aFilter)
  1095. {
  1096. if (mDisplayContent) {
  1097. aElements.AppendElement(mDisplayContent);
  1098. }
  1099. if (mButtonContent) {
  1100. aElements.AppendElement(mButtonContent);
  1101. }
  1102. }
  1103. // XXXbz this is a for-now hack. Now that display:inline-block works,
  1104. // need to revisit this.
  1105. class nsComboboxDisplayFrame : public nsBlockFrame {
  1106. public:
  1107. NS_DECL_FRAMEARENA_HELPERS
  1108. nsComboboxDisplayFrame (nsStyleContext* aContext,
  1109. nsComboboxControlFrame* aComboBox)
  1110. : nsBlockFrame(aContext),
  1111. mComboBox(aComboBox)
  1112. {}
  1113. // Need this so that line layout knows that this block's inline size
  1114. // depends on the available inline size.
  1115. virtual nsIAtom* GetType() const override;
  1116. virtual bool IsFrameOfType(uint32_t aFlags) const override
  1117. {
  1118. return nsBlockFrame::IsFrameOfType(aFlags &
  1119. ~(nsIFrame::eReplacedContainsBlock));
  1120. }
  1121. virtual void Reflow(nsPresContext* aPresContext,
  1122. ReflowOutput& aDesiredSize,
  1123. const ReflowInput& aReflowInput,
  1124. nsReflowStatus& aStatus) override;
  1125. virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
  1126. const nsDisplayListSet& aLists) override;
  1127. protected:
  1128. nsComboboxControlFrame* mComboBox;
  1129. };
  1130. NS_IMPL_FRAMEARENA_HELPERS(nsComboboxDisplayFrame)
  1131. nsIAtom*
  1132. nsComboboxDisplayFrame::GetType() const
  1133. {
  1134. return nsGkAtoms::comboboxDisplayFrame;
  1135. }
  1136. void
  1137. nsComboboxDisplayFrame::Reflow(nsPresContext* aPresContext,
  1138. ReflowOutput& aDesiredSize,
  1139. const ReflowInput& aReflowInput,
  1140. nsReflowStatus& aStatus)
  1141. {
  1142. ReflowInput state(aReflowInput);
  1143. if (state.ComputedBSize() == NS_INTRINSICSIZE) {
  1144. // Note that the only way we can have a computed block size here is
  1145. // if the combobox had a specified block size. If it didn't, size
  1146. // based on what our rows look like, for lack of anything better.
  1147. state.SetComputedBSize(mComboBox->mListControlFrame->GetBSizeOfARow());
  1148. }
  1149. WritingMode wm = aReflowInput.GetWritingMode();
  1150. nscoord computedISize = mComboBox->mDisplayISize -
  1151. state.ComputedLogicalBorderPadding().IStartEnd(wm);
  1152. if (computedISize < 0) {
  1153. computedISize = 0;
  1154. }
  1155. state.SetComputedISize(computedISize);
  1156. nsBlockFrame::Reflow(aPresContext, aDesiredSize, state, aStatus);
  1157. aStatus = NS_FRAME_COMPLETE; // this type of frame can't be split
  1158. }
  1159. void
  1160. nsComboboxDisplayFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
  1161. const nsDisplayListSet& aLists)
  1162. {
  1163. nsDisplayListCollection set(aBuilder);
  1164. nsBlockFrame::BuildDisplayList(aBuilder, set);
  1165. // remove background items if parent frame is themed
  1166. if (mComboBox->IsThemed()) {
  1167. set.BorderBackground()->DeleteAll();
  1168. }
  1169. set.MoveTo(aLists);
  1170. }
  1171. nsIFrame*
  1172. nsComboboxControlFrame::CreateFrameForDisplayNode()
  1173. {
  1174. MOZ_ASSERT(mDisplayContent);
  1175. // Get PresShell
  1176. nsIPresShell *shell = PresContext()->PresShell();
  1177. StyleSetHandle styleSet = shell->StyleSet();
  1178. // create the style contexts for the anonymous block frame and text frame
  1179. RefPtr<nsStyleContext> styleContext;
  1180. styleContext = styleSet->
  1181. ResolveAnonymousBoxStyle(nsCSSAnonBoxes::mozDisplayComboboxControlFrame,
  1182. mStyleContext,
  1183. nsStyleSet::eSkipParentDisplayBasedStyleFixup);
  1184. RefPtr<nsStyleContext> textStyleContext;
  1185. textStyleContext =
  1186. styleSet->ResolveStyleForText(mDisplayContent, mStyleContext);
  1187. // Start by creating our anonymous block frame
  1188. mDisplayFrame = new (shell) nsComboboxDisplayFrame(styleContext, this);
  1189. mDisplayFrame->Init(mContent, this, nullptr);
  1190. // Create a text frame and put it inside the block frame
  1191. nsIFrame* textFrame = NS_NewTextFrame(shell, textStyleContext);
  1192. // initialize the text frame
  1193. textFrame->Init(mDisplayContent, mDisplayFrame, nullptr);
  1194. mDisplayContent->SetPrimaryFrame(textFrame);
  1195. nsFrameList textList(textFrame, textFrame);
  1196. mDisplayFrame->SetInitialChildList(kPrincipalList, textList);
  1197. return mDisplayFrame;
  1198. }
  1199. void
  1200. nsComboboxControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
  1201. {
  1202. if (sFocused == this) {
  1203. sFocused = nullptr;
  1204. }
  1205. // Revoke any pending RedisplayTextEvent
  1206. mRedisplayTextEvent.Revoke();
  1207. nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
  1208. if (mDroppedDown) {
  1209. MOZ_ASSERT(mDropdownFrame, "mDroppedDown without frame");
  1210. nsView* view = mDropdownFrame->GetView();
  1211. MOZ_ASSERT(view);
  1212. nsIWidget* widget = view->GetWidget();
  1213. if (widget) {
  1214. widget->CaptureRollupEvents(this, false);
  1215. }
  1216. }
  1217. // Cleanup frames in popup child list
  1218. mPopupFrames.DestroyFramesFrom(aDestructRoot);
  1219. nsContentUtils::DestroyAnonymousContent(&mDisplayContent);
  1220. nsContentUtils::DestroyAnonymousContent(&mButtonContent);
  1221. nsBlockFrame::DestroyFrom(aDestructRoot);
  1222. }
  1223. const nsFrameList&
  1224. nsComboboxControlFrame::GetChildList(ChildListID aListID) const
  1225. {
  1226. if (kSelectPopupList == aListID) {
  1227. return mPopupFrames;
  1228. }
  1229. return nsBlockFrame::GetChildList(aListID);
  1230. }
  1231. void
  1232. nsComboboxControlFrame::GetChildLists(nsTArray<ChildList>* aLists) const
  1233. {
  1234. nsBlockFrame::GetChildLists(aLists);
  1235. mPopupFrames.AppendIfNonempty(aLists, kSelectPopupList);
  1236. }
  1237. void
  1238. nsComboboxControlFrame::SetInitialChildList(ChildListID aListID,
  1239. nsFrameList& aChildList)
  1240. {
  1241. if (kSelectPopupList == aListID) {
  1242. mPopupFrames.SetFrames(aChildList);
  1243. } else {
  1244. for (nsFrameList::Enumerator e(aChildList); !e.AtEnd(); e.Next()) {
  1245. nsCOMPtr<nsIFormControl> formControl =
  1246. do_QueryInterface(e.get()->GetContent());
  1247. if (formControl && formControl->GetType() == NS_FORM_BUTTON_BUTTON) {
  1248. mButtonFrame = e.get();
  1249. break;
  1250. }
  1251. }
  1252. NS_ASSERTION(mButtonFrame, "missing button frame in initial child list");
  1253. nsBlockFrame::SetInitialChildList(aListID, aChildList);
  1254. }
  1255. }
  1256. //----------------------------------------------------------------------
  1257. //nsIRollupListener
  1258. //----------------------------------------------------------------------
  1259. bool
  1260. nsComboboxControlFrame::Rollup(uint32_t aCount, bool aFlush,
  1261. const nsIntPoint* pos, nsIContent** aLastRolledUp)
  1262. {
  1263. if (!mDroppedDown) {
  1264. return false;
  1265. }
  1266. bool consume = !!COMBOBOX_ROLLUP_CONSUME_EVENT;
  1267. nsWeakFrame weakFrame(this);
  1268. mListControlFrame->AboutToRollup(); // might destroy us
  1269. if (!weakFrame.IsAlive()) {
  1270. return consume;
  1271. }
  1272. ShowDropDown(false); // might destroy us
  1273. if (weakFrame.IsAlive()) {
  1274. mListControlFrame->CaptureMouseEvents(false);
  1275. }
  1276. if (aFlush && weakFrame.IsAlive()) {
  1277. // The popup's visibility doesn't update until the minimize animation has
  1278. // finished, so call UpdateWidgetGeometry to update it right away.
  1279. nsViewManager* viewManager = mDropdownFrame->GetView()->GetViewManager();
  1280. viewManager->UpdateWidgetGeometry(); // might destroy us
  1281. }
  1282. if (!weakFrame.IsAlive()) {
  1283. return consume;
  1284. }
  1285. if (aLastRolledUp) {
  1286. *aLastRolledUp = GetContent();
  1287. }
  1288. return consume;
  1289. }
  1290. nsIWidget*
  1291. nsComboboxControlFrame::GetRollupWidget()
  1292. {
  1293. nsView* view = mDropdownFrame->GetView();
  1294. MOZ_ASSERT(view);
  1295. return view->GetWidget();
  1296. }
  1297. void
  1298. nsComboboxControlFrame::RollupFromList()
  1299. {
  1300. if (ShowList(false))
  1301. mListControlFrame->CaptureMouseEvents(false);
  1302. }
  1303. int32_t
  1304. nsComboboxControlFrame::UpdateRecentIndex(int32_t aIndex)
  1305. {
  1306. int32_t index = mRecentSelectedIndex;
  1307. if (mRecentSelectedIndex == NS_SKIP_NOTIFY_INDEX || aIndex == NS_SKIP_NOTIFY_INDEX)
  1308. mRecentSelectedIndex = aIndex;
  1309. return index;
  1310. }
  1311. class nsDisplayComboboxFocus : public nsDisplayItem {
  1312. public:
  1313. nsDisplayComboboxFocus(nsDisplayListBuilder* aBuilder,
  1314. nsComboboxControlFrame* aFrame)
  1315. : nsDisplayItem(aBuilder, aFrame) {
  1316. MOZ_COUNT_CTOR(nsDisplayComboboxFocus);
  1317. }
  1318. #ifdef NS_BUILD_REFCNT_LOGGING
  1319. virtual ~nsDisplayComboboxFocus() {
  1320. MOZ_COUNT_DTOR(nsDisplayComboboxFocus);
  1321. }
  1322. #endif
  1323. virtual void Paint(nsDisplayListBuilder* aBuilder,
  1324. nsRenderingContext* aCtx) override;
  1325. NS_DISPLAY_DECL_NAME("ComboboxFocus", TYPE_COMBOBOX_FOCUS)
  1326. };
  1327. void nsDisplayComboboxFocus::Paint(nsDisplayListBuilder* aBuilder,
  1328. nsRenderingContext* aCtx)
  1329. {
  1330. static_cast<nsComboboxControlFrame*>(mFrame)
  1331. ->PaintFocus(*aCtx->GetDrawTarget(), ToReferenceFrame());
  1332. }
  1333. void
  1334. nsComboboxControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
  1335. const nsDisplayListSet& aLists)
  1336. {
  1337. if (aBuilder->IsForEventDelivery()) {
  1338. // Don't allow children to receive events.
  1339. // REVIEW: following old GetFrameForPoint
  1340. DisplayBorderBackgroundOutline(aBuilder, aLists);
  1341. } else {
  1342. // REVIEW: Our in-flow child frames are inline-level so they will paint in our
  1343. // content list, so we don't need to mess with layers.
  1344. nsBlockFrame::BuildDisplayList(aBuilder, aLists);
  1345. }
  1346. // draw a focus indicator only when focus rings should be drawn
  1347. nsIDocument* doc = mContent->GetComposedDoc();
  1348. if (doc) {
  1349. nsPIDOMWindowOuter* window = doc->GetWindow();
  1350. if (window && window->ShouldShowFocusRing()) {
  1351. nsPresContext *presContext = PresContext();
  1352. const nsStyleDisplay *disp = StyleDisplay();
  1353. if ((!IsThemed(disp) ||
  1354. !presContext->GetTheme()->ThemeDrawsFocusForWidget(disp->mAppearance)) &&
  1355. mDisplayFrame && IsVisibleForPainting(aBuilder)) {
  1356. aLists.Content()->AppendNewToTop(
  1357. new (aBuilder) nsDisplayComboboxFocus(aBuilder, this));
  1358. }
  1359. }
  1360. }
  1361. DisplaySelectionOverlay(aBuilder, aLists.Content());
  1362. }
  1363. void nsComboboxControlFrame::PaintFocus(DrawTarget& aDrawTarget, nsPoint aPt)
  1364. {
  1365. /* Do we need to do anything? */
  1366. EventStates eventStates = mContent->AsElement()->State();
  1367. if (eventStates.HasState(NS_EVENT_STATE_DISABLED) || sFocused != this)
  1368. return;
  1369. int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
  1370. nsRect clipRect = mDisplayFrame->GetRect() + aPt;
  1371. aDrawTarget.PushClipRect(NSRectToSnappedRect(clipRect,
  1372. appUnitsPerDevPixel,
  1373. aDrawTarget));
  1374. // REVIEW: Why does the old code paint mDisplayFrame again? We've
  1375. // already painted it in the children above. So clipping it here won't do
  1376. // us much good.
  1377. /////////////////////
  1378. // draw focus
  1379. StrokeOptions strokeOptions;
  1380. nsLayoutUtils::InitDashPattern(strokeOptions, NS_STYLE_BORDER_STYLE_DOTTED);
  1381. ColorPattern color(ToDeviceColor(StyleColor()->mColor));
  1382. nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
  1383. clipRect.width -= onePixel;
  1384. clipRect.height -= onePixel;
  1385. Rect r = ToRect(nsLayoutUtils::RectToGfxRect(clipRect, appUnitsPerDevPixel));
  1386. StrokeSnappedEdgesOfRect(r, aDrawTarget, color, strokeOptions);
  1387. aDrawTarget.PopClip();
  1388. }
  1389. //---------------------------------------------------------
  1390. // gets the content (an option) by index and then set it as
  1391. // being selected or not selected
  1392. //---------------------------------------------------------
  1393. NS_IMETHODIMP
  1394. nsComboboxControlFrame::OnOptionSelected(int32_t aIndex, bool aSelected)
  1395. {
  1396. if (mDroppedDown) {
  1397. nsISelectControlFrame *selectFrame = do_QueryFrame(mListControlFrame);
  1398. if (selectFrame) {
  1399. selectFrame->OnOptionSelected(aIndex, aSelected);
  1400. }
  1401. } else {
  1402. if (aSelected) {
  1403. nsAutoScriptBlocker blocker;
  1404. RedisplayText(aIndex);
  1405. } else {
  1406. nsWeakFrame weakFrame(this);
  1407. RedisplaySelectedText();
  1408. if (weakFrame.IsAlive()) {
  1409. FireValueChangeEvent(); // Fire after old option is unselected
  1410. }
  1411. }
  1412. }
  1413. return NS_OK;
  1414. }
  1415. void nsComboboxControlFrame::FireValueChangeEvent()
  1416. {
  1417. // Fire ValueChange event to indicate data value of combo box has changed
  1418. nsContentUtils::AddScriptRunner(
  1419. new AsyncEventDispatcher(mContent, NS_LITERAL_STRING("ValueChange"), true,
  1420. false));
  1421. }
  1422. void
  1423. nsComboboxControlFrame::OnContentReset()
  1424. {
  1425. if (mListControlFrame) {
  1426. mListControlFrame->OnContentReset();
  1427. }
  1428. }
  1429. //--------------------------------------------------------
  1430. // nsIStatefulFrame
  1431. //--------------------------------------------------------
  1432. NS_IMETHODIMP
  1433. nsComboboxControlFrame::SaveState(nsPresState** aState)
  1434. {
  1435. MOZ_ASSERT(!(*aState));
  1436. (*aState) = new nsPresState();
  1437. (*aState)->SetDroppedDown(mDroppedDown);
  1438. return NS_OK;
  1439. }
  1440. NS_IMETHODIMP
  1441. nsComboboxControlFrame::RestoreState(nsPresState* aState)
  1442. {
  1443. if (!aState) {
  1444. return NS_ERROR_FAILURE;
  1445. }
  1446. ShowList(aState->GetDroppedDown()); // might destroy us
  1447. return NS_OK;
  1448. }
  1449. // Append a suffix so that the state key for the combobox is different
  1450. // from the state key the list control uses to sometimes save the scroll
  1451. // position for the same Element
  1452. NS_IMETHODIMP
  1453. nsComboboxControlFrame::GenerateStateKey(nsIContent* aContent,
  1454. nsIDocument* aDocument,
  1455. nsACString& aKey)
  1456. {
  1457. nsresult rv = nsContentUtils::GenerateStateKey(aContent, aDocument, aKey);
  1458. if (NS_FAILED(rv) || aKey.IsEmpty()) {
  1459. return rv;
  1460. }
  1461. aKey.Append("CCF");
  1462. return NS_OK;
  1463. }
  1464. // Fennec uses a custom combobox built-in widget.
  1465. //
  1466. /* static */
  1467. bool
  1468. nsComboboxControlFrame::ToolkitHasNativePopup()
  1469. {
  1470. #ifdef MOZ_USE_NATIVE_POPUP_WINDOWS
  1471. return true;
  1472. #else
  1473. return false;
  1474. #endif /* MOZ_USE_NATIVE_POPUP_WINDOWS */
  1475. }