CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / layout / forms / nsComboboxControlFrame.cpp
blob3830193f4a99cd8e61c81487b50f5ae8c8655d75
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
24 * Mats Palmgren <mats.palmgren@bredband.net>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
39 #include "nsCOMPtr.h"
40 #include "nsReadableUtils.h"
41 #include "nsComboboxControlFrame.h"
42 #include "nsIDOMEventTarget.h"
43 #include "nsFrameManager.h"
44 #include "nsFormControlFrame.h"
45 #include "nsGfxButtonControlFrame.h"
46 #include "nsGkAtoms.h"
47 #include "nsCSSAnonBoxes.h"
48 #include "nsHTMLParts.h"
49 #include "nsIFormControl.h"
50 #include "nsINameSpaceManager.h"
51 #include "nsIDOMElement.h"
52 #include "nsIListControlFrame.h"
53 #include "nsIDOMHTMLCollection.h"
54 #include "nsIDOMHTMLSelectElement.h"
55 #include "nsIDOMHTMLOptionElement.h"
56 #include "nsIDOMNSHTMLOptionCollectn.h"
57 #include "nsPIDOMWindow.h"
58 #include "nsIPresShell.h"
59 #include "nsIDeviceContext.h"
60 #include "nsIView.h"
61 #include "nsIViewManager.h"
62 #include "nsEventDispatcher.h"
63 #include "nsIEventStateManager.h"
64 #include "nsIEventListenerManager.h"
65 #include "nsIDOMNode.h"
66 #include "nsIPrivateDOMEvent.h"
67 #include "nsISelectControlFrame.h"
68 #include "nsXPCOM.h"
69 #include "nsISupportsPrimitives.h"
70 #include "nsIComponentManager.h"
71 #include "nsContentUtils.h"
72 #include "nsTextFragment.h"
73 #include "nsCSSFrameConstructor.h"
74 #include "nsIDocument.h"
75 #include "nsINodeInfo.h"
76 #include "nsIScrollableFrame.h"
77 #include "nsListControlFrame.h"
78 #include "nsContentCID.h"
79 #ifdef ACCESSIBILITY
80 #include "nsAccessibilityService.h"
81 #endif
82 #include "nsIServiceManager.h"
83 #include "nsIDOMNode.h"
84 #include "nsGUIEvent.h"
85 #include "nsAutoPtr.h"
86 #include "nsStyleSet.h"
87 #include "nsNodeInfoManager.h"
88 #include "nsContentCreatorFunctions.h"
89 #include "nsLayoutUtils.h"
90 #include "nsDisplayList.h"
91 #include "nsITheme.h"
92 #include "nsThemeConstants.h"
93 #include "nsPLDOMEvent.h"
95 namespace dom = mozilla::dom;
97 NS_IMETHODIMP
98 nsComboboxControlFrame::RedisplayTextEvent::Run()
100 if (mControlFrame)
101 mControlFrame->HandleRedisplayTextEvent();
102 return NS_OK;
105 class nsPresState;
107 #define FIX_FOR_BUG_53259
109 // Drop down list event management.
110 // The combo box uses the following strategy for managing the drop-down list.
111 // If the combo box or it's arrow button is clicked on the drop-down list is displayed
112 // If mouse exit's the combo box with the drop-down list displayed the drop-down list
113 // is asked to capture events
114 // The drop-down list will capture all events including mouse down and up and will always
115 // return with ListWasSelected method call regardless of whether an item in the list was
116 // actually selected.
117 // The ListWasSelected code will turn off mouse-capture for the drop-down list.
118 // The drop-down list does not explicitly set capture when it is in the drop-down mode.
121 //XXX: This is temporary. It simulates pseudo states by using a attribute selector on
123 const PRInt32 kSizeNotSet = -1;
126 * Helper class that listens to the combo boxes button. If the button is pressed the
127 * combo box is toggled to open or close. this is used by Accessibility which presses
128 * that button Programmatically.
130 class nsComboButtonListener: public nsIDOMMouseListener
132 public:
134 NS_DECL_ISUPPORTS
135 NS_IMETHOD HandleEvent(nsIDOMEvent* anEvent) { return PR_FALSE; }
136 NS_IMETHOD MouseDown(nsIDOMEvent* aMouseEvent) { return PR_FALSE; }
137 NS_IMETHOD MouseUp(nsIDOMEvent* aMouseEvent) { return PR_FALSE; }
138 NS_IMETHOD MouseDblClick(nsIDOMEvent* aMouseEvent) { return PR_FALSE; }
139 NS_IMETHOD MouseOver(nsIDOMEvent* aMouseEvent) { return PR_FALSE; }
140 NS_IMETHOD MouseOut(nsIDOMEvent* aMouseEvent) { return PR_FALSE; }
142 NS_IMETHOD MouseClick(nsIDOMEvent* aMouseEvent)
144 mComboBox->ShowDropDown(!mComboBox->IsDroppedDown());
145 return NS_OK;
148 nsComboButtonListener(nsComboboxControlFrame* aCombobox)
150 mComboBox = aCombobox;
153 virtual ~nsComboButtonListener() {}
155 nsComboboxControlFrame* mComboBox;
158 NS_IMPL_ISUPPORTS2(nsComboButtonListener,
159 nsIDOMMouseListener,
160 nsIDOMEventListener)
162 // static class data member for Bug 32920
163 nsComboboxControlFrame * nsComboboxControlFrame::mFocused = nsnull;
165 nsIFrame*
166 NS_NewComboboxControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext, PRUint32 aStateFlags)
168 nsComboboxControlFrame* it = new (aPresShell) nsComboboxControlFrame(aContext);
170 if (it) {
171 // set the state flags (if any are provided)
172 it->AddStateBits(aStateFlags);
175 return it;
178 NS_IMPL_FRAMEARENA_HELPERS(nsComboboxControlFrame)
180 //-----------------------------------------------------------
181 // Reflow Debugging Macros
182 // These let us "see" how many reflow counts are happening
183 //-----------------------------------------------------------
184 #ifdef DO_REFLOW_COUNTER
186 #define MAX_REFLOW_CNT 1024
187 static PRInt32 gTotalReqs = 0;;
188 static PRInt32 gTotalReflows = 0;;
189 static PRInt32 gReflowControlCntRQ[MAX_REFLOW_CNT];
190 static PRInt32 gReflowControlCnt[MAX_REFLOW_CNT];
191 static PRInt32 gReflowInx = -1;
193 #define REFLOW_COUNTER() \
194 if (mReflowId > -1) \
195 gReflowControlCnt[mReflowId]++;
197 #define REFLOW_COUNTER_REQUEST() \
198 if (mReflowId > -1) \
199 gReflowControlCntRQ[mReflowId]++;
201 #define REFLOW_COUNTER_DUMP(__desc) \
202 if (mReflowId > -1) {\
203 gTotalReqs += gReflowControlCntRQ[mReflowId];\
204 gTotalReflows += gReflowControlCnt[mReflowId];\
205 printf("** Id:%5d %s RF: %d RQ: %d %d/%d %5.2f\n", \
206 mReflowId, (__desc), \
207 gReflowControlCnt[mReflowId], \
208 gReflowControlCntRQ[mReflowId],\
209 gTotalReflows, gTotalReqs, float(gTotalReflows)/float(gTotalReqs)*100.0f);\
212 #define REFLOW_COUNTER_INIT() \
213 if (gReflowInx < MAX_REFLOW_CNT) { \
214 gReflowInx++; \
215 mReflowId = gReflowInx; \
216 gReflowControlCnt[mReflowId] = 0; \
217 gReflowControlCntRQ[mReflowId] = 0; \
218 } else { \
219 mReflowId = -1; \
222 // reflow messages
223 #define REFLOW_DEBUG_MSG(_msg1) printf((_msg1))
224 #define REFLOW_DEBUG_MSG2(_msg1, _msg2) printf((_msg1), (_msg2))
225 #define REFLOW_DEBUG_MSG3(_msg1, _msg2, _msg3) printf((_msg1), (_msg2), (_msg3))
226 #define REFLOW_DEBUG_MSG4(_msg1, _msg2, _msg3, _msg4) printf((_msg1), (_msg2), (_msg3), (_msg4))
228 #else //-------------
230 #define REFLOW_COUNTER_REQUEST()
231 #define REFLOW_COUNTER()
232 #define REFLOW_COUNTER_DUMP(__desc)
233 #define REFLOW_COUNTER_INIT()
235 #define REFLOW_DEBUG_MSG(_msg)
236 #define REFLOW_DEBUG_MSG2(_msg1, _msg2)
237 #define REFLOW_DEBUG_MSG3(_msg1, _msg2, _msg3)
238 #define REFLOW_DEBUG_MSG4(_msg1, _msg2, _msg3, _msg4)
241 #endif
243 //------------------------------------------
244 // This is for being VERY noisy
245 //------------------------------------------
246 #ifdef DO_VERY_NOISY
247 #define REFLOW_NOISY_MSG(_msg1) printf((_msg1))
248 #define REFLOW_NOISY_MSG2(_msg1, _msg2) printf((_msg1), (_msg2))
249 #define REFLOW_NOISY_MSG3(_msg1, _msg2, _msg3) printf((_msg1), (_msg2), (_msg3))
250 #define REFLOW_NOISY_MSG4(_msg1, _msg2, _msg3, _msg4) printf((_msg1), (_msg2), (_msg3), (_msg4))
251 #else
252 #define REFLOW_NOISY_MSG(_msg)
253 #define REFLOW_NOISY_MSG2(_msg1, _msg2)
254 #define REFLOW_NOISY_MSG3(_msg1, _msg2, _msg3)
255 #define REFLOW_NOISY_MSG4(_msg1, _msg2, _msg3, _msg4)
256 #endif
258 //------------------------------------------
259 // Displays value in pixels or twips
260 //------------------------------------------
261 #ifdef DO_PIXELS
262 #define PX(__v) __v / 15
263 #else
264 #define PX(__v) __v
265 #endif
267 //------------------------------------------------------
268 //-- Done with macros
269 //------------------------------------------------------
271 nsComboboxControlFrame::nsComboboxControlFrame(nsStyleContext* aContext)
272 : nsBlockFrame(aContext),
273 mDisplayWidth(0)
275 mListControlFrame = nsnull;
276 mDroppedDown = PR_FALSE;
277 mDisplayFrame = nsnull;
278 mButtonFrame = nsnull;
279 mDropdownFrame = nsnull;
281 mInRedisplayText = PR_FALSE;
283 mRecentSelectedIndex = NS_SKIP_NOTIFY_INDEX;
285 REFLOW_COUNTER_INIT()
288 //--------------------------------------------------------------
289 nsComboboxControlFrame::~nsComboboxControlFrame()
291 REFLOW_COUNTER_DUMP("nsCCF");
294 //--------------------------------------------------------------
296 NS_QUERYFRAME_HEAD(nsComboboxControlFrame)
297 NS_QUERYFRAME_ENTRY(nsIComboboxControlFrame)
298 NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
299 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
300 NS_QUERYFRAME_ENTRY(nsISelectControlFrame)
301 NS_QUERYFRAME_ENTRY(nsIStatefulFrame)
302 NS_QUERYFRAME_TAIL_INHERITING(nsBlockFrame)
304 #ifdef ACCESSIBILITY
305 already_AddRefed<nsAccessible>
306 nsComboboxControlFrame::CreateAccessible()
308 nsAccessibilityService* accService = nsIPresShell::AccService();
309 if (accService) {
310 return accService->CreateHTMLComboboxAccessible(mContent,
311 PresContext()->PresShell());
314 return nsnull;
316 #endif
318 void
319 nsComboboxControlFrame::SetFocus(PRBool aOn, PRBool aRepaint)
321 nsWeakFrame weakFrame(this);
322 if (aOn) {
323 nsListControlFrame::ComboboxFocusSet();
324 mFocused = this;
325 } else {
326 mFocused = nsnull;
327 if (mDroppedDown) {
328 mListControlFrame->ComboboxFinish(mDisplayedIndex); // might destroy us
329 if (!weakFrame.IsAlive()) {
330 return;
333 // May delete |this|.
334 mListControlFrame->FireOnChange();
337 if (!weakFrame.IsAlive()) {
338 return;
341 // This is needed on a temporary basis. It causes the focus
342 // rect to be drawn. This is much faster than ReResolvingStyle
343 // Bug 32920
344 Invalidate(nsRect(0,0,mRect.width,mRect.height));
346 // Make sure the content area gets updated for where the dropdown was
347 // This is only needed for embedding, the focus may go to
348 // the chrome that is not part of the Gecko system (Bug 83493)
349 // XXX this is rather inefficient
350 nsIViewManager* vm = PresContext()->GetPresShell()->GetViewManager();
351 if (vm) {
352 vm->UpdateAllViews(NS_VMREFRESH_NO_SYNC);
356 void
357 nsComboboxControlFrame::ShowPopup(PRBool aShowPopup)
359 nsIView* view = mDropdownFrame->GetView();
360 nsIViewManager* viewManager = view->GetViewManager();
362 if (aShowPopup) {
363 nsRect rect = mDropdownFrame->GetRect();
364 rect.x = rect.y = 0;
365 viewManager->ResizeView(view, rect);
366 viewManager->SetViewVisibility(view, nsViewVisibility_kShow);
367 } else {
368 viewManager->SetViewVisibility(view, nsViewVisibility_kHide);
369 nsRect emptyRect(0, 0, 0, 0);
370 viewManager->ResizeView(view, emptyRect);
373 // fire a popup dom event
374 nsEventStatus status = nsEventStatus_eIgnore;
375 nsMouseEvent event(PR_TRUE, aShowPopup ?
376 NS_XUL_POPUP_SHOWING : NS_XUL_POPUP_HIDING, nsnull,
377 nsMouseEvent::eReal);
379 nsCOMPtr<nsIPresShell> shell = PresContext()->GetPresShell();
380 if (shell)
381 shell->HandleDOMEventWithTarget(mContent, &event, &status);
384 PRBool
385 nsComboboxControlFrame::ShowList(PRBool aShowList)
387 nsCOMPtr<nsIPresShell> shell = PresContext()->GetPresShell();
389 nsWeakFrame weakFrame(this);
390 ShowPopup(aShowList); // might destroy us
391 if (!weakFrame.IsAlive()) {
392 return PR_FALSE;
395 mDroppedDown = aShowList;
396 if (mDroppedDown) {
397 // The listcontrol frame will call back to the nsComboboxControlFrame's
398 // ListWasSelected which will stop the capture.
399 mListControlFrame->AboutToDropDown();
400 mListControlFrame->CaptureMouseEvents(PR_TRUE);
403 // XXXbz so why do we need to flush here, exactly?
404 shell->GetDocument()->FlushPendingNotifications(Flush_Layout);
405 if (!weakFrame.IsAlive()) {
406 return PR_FALSE;
409 nsIFrame* listFrame = do_QueryFrame(mListControlFrame);
410 if (listFrame) {
411 nsIView* view = listFrame->GetView();
412 NS_ASSERTION(view, "nsComboboxControlFrame view is null");
413 if (view) {
414 nsIWidget* widget = view->GetWidget();
415 if (widget)
416 widget->CaptureRollupEvents(this, nsnull, mDroppedDown, mDroppedDown);
420 return weakFrame.IsAlive();
423 nsresult
424 nsComboboxControlFrame::ReflowDropdown(nsPresContext* aPresContext,
425 const nsHTMLReflowState& aReflowState)
427 // All we want out of it later on, really, is the height of a row, so we
428 // don't even need to cache mDropdownFrame's ascent or anything. If we don't
429 // need to reflow it, just bail out here.
430 if (!aReflowState.ShouldReflowAllKids() &&
431 !NS_SUBTREE_DIRTY(mDropdownFrame)) {
432 return NS_OK;
435 // XXXbz this will, for small-height dropdowns, have extra space on the right
436 // edge for the scrollbar we don't show... but that's the best we can do here
437 // for now.
438 nsSize availSize(aReflowState.availableWidth, NS_UNCONSTRAINEDSIZE);
439 nsHTMLReflowState kidReflowState(aPresContext, aReflowState, mDropdownFrame,
440 availSize);
442 // If the dropdown's intrinsic width is narrower than our specified width,
443 // then expand it out. We want our border-box width to end up the same as
444 // the dropdown's so account for both sets of mComputedBorderPadding.
445 nscoord forcedWidth = aReflowState.ComputedWidth() +
446 aReflowState.mComputedBorderPadding.LeftRight() -
447 kidReflowState.mComputedBorderPadding.LeftRight();
448 kidReflowState.SetComputedWidth(NS_MAX(kidReflowState.ComputedWidth(),
449 forcedWidth));
451 // ensure we start off hidden
452 if (GetStateBits() & NS_FRAME_FIRST_REFLOW) {
453 nsIView* view = mDropdownFrame->GetView();
454 nsIViewManager* viewManager = view->GetViewManager();
455 viewManager->SetViewVisibility(view, nsViewVisibility_kHide);
456 nsRect emptyRect(0, 0, 0, 0);
457 viewManager->ResizeView(view, emptyRect);
460 // Allow the child to move/size/change-visibility its view if it's currently
461 // dropped down
462 PRInt32 flags = NS_FRAME_NO_MOVE_FRAME | NS_FRAME_NO_VISIBILITY | NS_FRAME_NO_SIZE_VIEW;
463 if (mDroppedDown) {
464 flags = 0;
466 nsRect rect = mDropdownFrame->GetRect();
467 nsHTMLReflowMetrics desiredSize;
468 nsReflowStatus ignoredStatus;
469 nsresult rv = ReflowChild(mDropdownFrame, aPresContext, desiredSize,
470 kidReflowState, rect.x, rect.y, flags,
471 ignoredStatus);
473 // Set the child's width and height to it's desired size
474 FinishReflowChild(mDropdownFrame, aPresContext, &kidReflowState,
475 desiredSize, rect.x, rect.y, flags);
476 return rv;
479 void
480 nsComboboxControlFrame::AbsolutelyPositionDropDown()
482 // Position the dropdown list. It is positioned below the display frame if there is enough
483 // room on the screen to display the entire list. Otherwise it is placed above the display
484 // frame.
486 // Note: As first glance, it appears that you could simply get the absolute bounding box for the
487 // dropdown list by first getting its view, then getting the view's nsIWidget, then asking the nsIWidget
488 // for it's AbsoluteBounds. The problem with this approach, is that the dropdown lists y location can
489 // change based on whether the dropdown is placed below or above the display frame.
490 // The approach, taken here is to get use the absolute position of the display frame and use it's location
491 // to determine if the dropdown will go offscreen.
493 // Use the height calculated for the area frame so it includes both
494 // the display and button heights.
495 nscoord dropdownYOffset = GetRect().height;
496 nsSize dropdownSize = mDropdownFrame->GetSize();
498 nsRect screen = nsFormControlFrame::GetUsableScreenRect(PresContext());
500 // Check to see if the drop-down list will go offscreen
501 if (GetScreenRectInAppUnits().YMost() + dropdownSize.height > screen.YMost()) {
502 // move the dropdown list up
503 dropdownYOffset = - (dropdownSize.height);
506 nsPoint dropdownPosition;
507 const nsStyleVisibility* vis = GetStyleVisibility();
508 if (vis->mDirection == NS_STYLE_DIRECTION_RTL) {
509 // Align the right edge of the drop-down with the right edge of the control.
510 dropdownPosition.x = GetRect().width - dropdownSize.width;
511 } else {
512 dropdownPosition.x = 0;
514 dropdownPosition.y = dropdownYOffset;
516 mDropdownFrame->SetPosition(dropdownPosition);
519 //----------------------------------------------------------
521 //----------------------------------------------------------
522 #ifdef DO_REFLOW_DEBUG
523 static int myCounter = 0;
525 static void printSize(char * aDesc, nscoord aSize)
527 printf(" %s: ", aDesc);
528 if (aSize == NS_UNCONSTRAINEDSIZE) {
529 printf("UC");
530 } else {
531 printf("%d", PX(aSize));
534 #endif
536 //-------------------------------------------------------------------
537 //-- Main Reflow for the Combobox
538 //-------------------------------------------------------------------
540 nscoord
541 nsComboboxControlFrame::GetIntrinsicWidth(nsIRenderingContext* aRenderingContext,
542 nsLayoutUtils::IntrinsicWidthType aType)
544 // get the scrollbar width, we'll use this later
545 nscoord scrollbarWidth = 0;
546 nsPresContext* presContext = PresContext();
547 if (mListControlFrame) {
548 nsIScrollableFrame* scrollable = do_QueryFrame(mListControlFrame);
549 NS_ASSERTION(scrollable, "List must be a scrollable frame");
550 scrollbarWidth =
551 scrollable->GetDesiredScrollbarSizes(presContext, aRenderingContext).LeftRight();
554 nscoord displayWidth = 0;
555 if (NS_LIKELY(mDisplayFrame)) {
556 displayWidth = nsLayoutUtils::IntrinsicForContainer(aRenderingContext,
557 mDisplayFrame,
558 aType);
561 if (mDropdownFrame) {
562 nscoord dropdownContentWidth;
563 if (aType == nsLayoutUtils::MIN_WIDTH) {
564 dropdownContentWidth = mDropdownFrame->GetMinWidth(aRenderingContext);
565 } else {
566 NS_ASSERTION(aType == nsLayoutUtils::PREF_WIDTH, "Unexpected type");
567 dropdownContentWidth = mDropdownFrame->GetPrefWidth(aRenderingContext);
569 dropdownContentWidth = NSCoordSaturatingSubtract(dropdownContentWidth,
570 scrollbarWidth,
571 nscoord_MAX);
573 displayWidth = NS_MAX(dropdownContentWidth, displayWidth);
576 // add room for the dropmarker button if there is one
577 if (!IsThemed() || presContext->GetTheme()->ThemeNeedsComboboxDropmarker())
578 displayWidth += scrollbarWidth;
580 return displayWidth;
584 nscoord
585 nsComboboxControlFrame::GetMinWidth(nsIRenderingContext *aRenderingContext)
587 nscoord minWidth;
588 DISPLAY_MIN_WIDTH(this, minWidth);
589 minWidth = GetIntrinsicWidth(aRenderingContext, nsLayoutUtils::MIN_WIDTH);
590 return minWidth;
593 nscoord
594 nsComboboxControlFrame::GetPrefWidth(nsIRenderingContext *aRenderingContext)
596 nscoord prefWidth;
597 DISPLAY_PREF_WIDTH(this, prefWidth);
598 prefWidth = GetIntrinsicWidth(aRenderingContext, nsLayoutUtils::PREF_WIDTH);
599 return prefWidth;
602 NS_IMETHODIMP
603 nsComboboxControlFrame::Reflow(nsPresContext* aPresContext,
604 nsHTMLReflowMetrics& aDesiredSize,
605 const nsHTMLReflowState& aReflowState,
606 nsReflowStatus& aStatus)
608 // Constraints we try to satisfy:
610 // 1) Default width of button is the vertical scrollbar size
611 // 2) If the width of button is bigger than our width, set width of
612 // button to 0.
613 // 3) Default height of button is height of display area
614 // 4) Width of display area is whatever is left over from our width after
615 // allocating width for the button.
616 // 5) Height of display area is GetHeightOfARow() on the
617 // mListControlFrame.
619 if (!mDisplayFrame || !mButtonFrame || !mDropdownFrame) {
620 NS_ERROR("Why did the frame constructor allow this to happen? Fix it!!");
621 return NS_ERROR_UNEXPECTED;
624 // Make sure the displayed text is the same as the selected option, bug 297389.
625 PRInt32 selectedIndex;
626 nsAutoString selectedOptionText;
627 if (!mDroppedDown) {
628 selectedIndex = mListControlFrame->GetSelectedIndex();
630 else {
631 // In dropped down mode the "selected index" is the hovered menu item,
632 // we want the last selected item which is |mDisplayedIndex| in this case.
633 selectedIndex = mDisplayedIndex;
635 if (selectedIndex != -1) {
636 mListControlFrame->GetOptionText(selectedIndex, selectedOptionText);
638 if (mDisplayedOptionText != selectedOptionText) {
639 RedisplayText(selectedIndex);
642 // First reflow our dropdown so that we know how tall we should be.
643 ReflowDropdown(aPresContext, aReflowState);
645 // Get the width of the vertical scrollbar. That will be the width of the
646 // dropdown button.
647 nscoord buttonWidth;
648 const nsStyleDisplay *disp = GetStyleDisplay();
649 if (IsThemed(disp) && !aPresContext->GetTheme()->ThemeNeedsComboboxDropmarker()) {
650 buttonWidth = 0;
652 else {
653 nsIScrollableFrame* scrollable = do_QueryFrame(mListControlFrame);
654 NS_ASSERTION(scrollable, "List must be a scrollable frame");
655 buttonWidth =
656 scrollable->GetDesiredScrollbarSizes(PresContext(),
657 aReflowState.rendContext).LeftRight();
658 if (buttonWidth > aReflowState.ComputedWidth()) {
659 buttonWidth = 0;
663 mDisplayWidth = aReflowState.ComputedWidth() - buttonWidth;
665 nsresult rv = nsBlockFrame::Reflow(aPresContext, aDesiredSize, aReflowState,
666 aStatus);
667 NS_ENSURE_SUCCESS(rv, rv);
669 // Now set the correct width and height on our button. The width we need to
670 // set always, the height only if we had an auto height.
671 nsRect buttonRect = mButtonFrame->GetRect();
672 // If we have a non-intrinsic computed height, our kids should have sized
673 // themselves properly on their own.
674 if (aReflowState.ComputedHeight() == NS_INTRINSICSIZE) {
675 // The display frame is going to be the right height and width at this
676 // point. Use its height as the button height.
677 nsRect displayRect = mDisplayFrame->GetRect();
678 buttonRect.height = displayRect.height;
679 buttonRect.y = displayRect.y;
681 #ifdef DEBUG
682 else {
683 nscoord buttonHeight = mButtonFrame->GetSize().height;
684 nscoord displayHeight = mDisplayFrame->GetSize().height;
686 // The button and display area should be equal heights, unless the computed
687 // height on the combobox is too small to fit their borders and padding.
688 NS_ASSERTION(buttonHeight == displayHeight ||
689 (aReflowState.ComputedHeight() < buttonHeight &&
690 buttonHeight ==
691 mButtonFrame->GetUsedBorderAndPadding().TopBottom()) ||
692 (aReflowState.ComputedHeight() < displayHeight &&
693 displayHeight ==
694 mDisplayFrame->GetUsedBorderAndPadding().TopBottom()),
695 "Different heights?");
697 #endif
699 if (GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL) {
700 // Make sure the right edge of the button frame stays where it is now
701 buttonRect.x -= buttonWidth - buttonRect.width;
703 buttonRect.width = buttonWidth;
704 mButtonFrame->SetRect(buttonRect);
706 return rv;
709 //--------------------------------------------------------------
711 nsIAtom*
712 nsComboboxControlFrame::GetType() const
714 return nsGkAtoms::comboboxControlFrame;
717 #ifdef NS_DEBUG
718 NS_IMETHODIMP
719 nsComboboxControlFrame::GetFrameName(nsAString& aResult) const
721 return MakeFrameName(NS_LITERAL_STRING("ComboboxControl"), aResult);
723 #endif
726 //----------------------------------------------------------------------
727 // nsIComboboxControlFrame
728 //----------------------------------------------------------------------
729 void
730 nsComboboxControlFrame::ShowDropDown(PRBool aDoDropDown)
732 nsEventStates eventStates = mContent->IntrinsicState();
733 if (eventStates.HasState(NS_EVENT_STATE_DISABLED)) {
734 return;
737 if (!mDroppedDown && aDoDropDown) {
738 if (mListControlFrame) {
739 mListControlFrame->SyncViewWithFrame();
741 ShowList(aDoDropDown); // might destroy us
742 } else if (mDroppedDown && !aDoDropDown) {
743 ShowList(aDoDropDown); // might destroy us
747 void
748 nsComboboxControlFrame::SetDropDown(nsIFrame* aDropDownFrame)
750 mDropdownFrame = aDropDownFrame;
751 mListControlFrame = do_QueryFrame(mDropdownFrame);
754 nsIFrame*
755 nsComboboxControlFrame::GetDropDown()
757 return mDropdownFrame;
760 ///////////////////////////////////////////////////////////////
762 NS_IMETHODIMP
763 nsComboboxControlFrame::RedisplaySelectedText()
765 nsAutoScriptBlocker scriptBlocker;
766 return RedisplayText(mListControlFrame->GetSelectedIndex());
769 nsresult
770 nsComboboxControlFrame::RedisplayText(PRInt32 aIndex)
772 // Get the text to display
773 if (aIndex != -1) {
774 mListControlFrame->GetOptionText(aIndex, mDisplayedOptionText);
775 } else {
776 mDisplayedOptionText.Truncate();
778 mDisplayedIndex = aIndex;
780 REFLOW_DEBUG_MSG2("RedisplayText \"%s\"\n",
781 NS_LossyConvertUTF16toASCII(mDisplayedOptionText).get());
783 // Send reflow command because the new text maybe larger
784 nsresult rv = NS_OK;
785 if (mDisplayContent) {
786 // Don't call ActuallyDisplayText(PR_TRUE) directly here since that
787 // could cause recursive frame construction. See bug 283117 and the comment in
788 // HandleRedisplayTextEvent() below.
790 // Revoke outstanding events to avoid out-of-order events which could mean
791 // displaying the wrong text.
792 mRedisplayTextEvent.Revoke();
794 NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
795 "If we happen to run our redisplay event now, we might kill "
796 "ourselves!");
798 nsRefPtr<RedisplayTextEvent> event = new RedisplayTextEvent(this);
799 mRedisplayTextEvent = event;
800 if (!nsContentUtils::AddScriptRunner(event))
801 mRedisplayTextEvent.Forget();
803 return rv;
806 void
807 nsComboboxControlFrame::HandleRedisplayTextEvent()
809 // First, make sure that the content model is up to date and we've
810 // constructed the frames for all our content in the right places.
811 // Otherwise they'll end up under the wrong insertion frame when we
812 // ActuallyDisplayText, since that flushes out the content sink by
813 // calling SetText on a DOM node with aNotify set to true. See bug
814 // 289730.
815 nsWeakFrame weakThis(this);
816 PresContext()->Document()->
817 FlushPendingNotifications(Flush_ContentAndNotify);
818 if (!weakThis.IsAlive())
819 return;
821 // Redirect frame insertions during this method (see GetContentInsertionFrame())
822 // so that any reframing that the frame constructor forces upon us is inserted
823 // into the correct parent (mDisplayFrame). See bug 282607.
824 NS_PRECONDITION(!mInRedisplayText, "Nested RedisplayText");
825 mInRedisplayText = PR_TRUE;
826 mRedisplayTextEvent.Forget();
828 ActuallyDisplayText(PR_TRUE);
829 // XXXbz This should perhaps be eResize. Check.
830 PresContext()->PresShell()->FrameNeedsReflow(mDisplayFrame,
831 nsIPresShell::eStyleChange,
832 NS_FRAME_IS_DIRTY);
834 mInRedisplayText = PR_FALSE;
837 void
838 nsComboboxControlFrame::ActuallyDisplayText(PRBool aNotify)
840 if (mDisplayedOptionText.IsEmpty()) {
841 // Have to use a non-breaking space for line-height calculations
842 // to be right
843 static const PRUnichar space = 0xA0;
844 mDisplayContent->SetText(&space, 1, aNotify);
845 } else {
846 mDisplayContent->SetText(mDisplayedOptionText, aNotify);
850 PRInt32
851 nsComboboxControlFrame::GetIndexOfDisplayArea()
853 return mDisplayedIndex;
856 //----------------------------------------------------------------------
857 // nsISelectControlFrame
858 //----------------------------------------------------------------------
859 NS_IMETHODIMP
860 nsComboboxControlFrame::DoneAddingChildren(PRBool aIsDone)
862 nsISelectControlFrame* listFrame = do_QueryFrame(mDropdownFrame);
863 if (!listFrame)
864 return NS_ERROR_FAILURE;
866 return listFrame->DoneAddingChildren(aIsDone);
869 NS_IMETHODIMP
870 nsComboboxControlFrame::AddOption(PRInt32 aIndex)
872 if (aIndex <= mDisplayedIndex) {
873 ++mDisplayedIndex;
876 nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
877 return lcf->AddOption(aIndex);
881 NS_IMETHODIMP
882 nsComboboxControlFrame::RemoveOption(PRInt32 aIndex)
884 nsWeakFrame weakThis(this);
885 if (mListControlFrame->GetNumberOfOptions() > 0) {
886 if (aIndex < mDisplayedIndex) {
887 --mDisplayedIndex;
888 } else if (aIndex == mDisplayedIndex) {
889 mDisplayedIndex = 0; // IE6 compat
890 RedisplayText(mDisplayedIndex);
893 else {
894 // If we removed the last option, we need to blank things out
895 RedisplayText(-1);
898 if (!weakThis.IsAlive())
899 return NS_OK;
901 nsListControlFrame* lcf = static_cast<nsListControlFrame*>(mDropdownFrame);
902 return lcf->RemoveOption(aIndex);
905 NS_IMETHODIMP
906 nsComboboxControlFrame::OnSetSelectedIndex(PRInt32 aOldIndex, PRInt32 aNewIndex)
908 nsAutoScriptBlocker scriptBlocker;
909 RedisplayText(aNewIndex);
910 NS_ASSERTION(mDropdownFrame, "No dropdown frame!");
912 nsISelectControlFrame* listFrame = do_QueryFrame(mDropdownFrame);
913 NS_ASSERTION(listFrame, "No list frame!");
915 return listFrame->OnSetSelectedIndex(aOldIndex, aNewIndex);
918 // End nsISelectControlFrame
919 //----------------------------------------------------------------------
921 NS_IMETHODIMP
922 nsComboboxControlFrame::HandleEvent(nsPresContext* aPresContext,
923 nsGUIEvent* aEvent,
924 nsEventStatus* aEventStatus)
926 NS_ENSURE_ARG_POINTER(aEventStatus);
928 if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
929 return NS_OK;
932 nsEventStates eventStates = mContent->IntrinsicState();
933 if (eventStates.HasState(NS_EVENT_STATE_DISABLED)) {
934 return NS_OK;
937 // If we have style that affects how we are selected, feed event down to
938 // nsFrame::HandleEvent so that selection takes place when appropriate.
939 const nsStyleUserInterface* uiStyle = GetStyleUserInterface();
940 if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE || uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED)
941 return nsBlockFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
943 return NS_OK;
947 nsresult
948 nsComboboxControlFrame::SetFormProperty(nsIAtom* aName, const nsAString& aValue)
950 nsIFormControlFrame* fcFrame = do_QueryFrame(mDropdownFrame);
951 if (!fcFrame) {
952 return NS_NOINTERFACE;
955 return fcFrame->SetFormProperty(aName, aValue);
958 nsresult
959 nsComboboxControlFrame::GetFormProperty(nsIAtom* aName, nsAString& aValue) const
961 nsIFormControlFrame* fcFrame = do_QueryFrame(mDropdownFrame);
962 if (!fcFrame) {
963 return NS_ERROR_FAILURE;
966 return fcFrame->GetFormProperty(aName, aValue);
969 nsIFrame*
970 nsComboboxControlFrame::GetContentInsertionFrame() {
971 return mInRedisplayText ? mDisplayFrame : mDropdownFrame->GetContentInsertionFrame();
974 nsresult
975 nsComboboxControlFrame::CreateAnonymousContent(nsTArray<nsIContent*>& aElements)
977 // The frames used to display the combo box and the button used to popup the dropdown list
978 // are created through anonymous content. The dropdown list is not created through anonymous
979 // content because it's frame is initialized specifically for the drop-down case and it is placed
980 // a special list referenced through NS_COMBO_FRAME_POPUP_LIST_INDEX to keep separate from the
981 // layout of the display and button.
983 // Note: The value attribute of the display content is set when an item is selected in the dropdown list.
984 // If the content specified below does not honor the value attribute than nothing will be displayed.
986 // For now the content that is created corresponds to two input buttons. It would be better to create the
987 // tag as something other than input, but then there isn't any way to create a button frame since it
988 // isn't possible to set the display type in CSS2 to create a button frame.
990 // create content used for display
991 //nsIAtom* tag = NS_NewAtom("mozcombodisplay");
993 // Add a child text content node for the label
995 nsNodeInfoManager *nimgr = mContent->NodeInfo()->NodeInfoManager();
997 NS_NewTextNode(getter_AddRefs(mDisplayContent), nimgr);
998 if (!mDisplayContent)
999 return NS_ERROR_OUT_OF_MEMORY;
1001 // set the value of the text node
1002 mDisplayedIndex = mListControlFrame->GetSelectedIndex();
1003 if (mDisplayedIndex != -1) {
1004 mListControlFrame->GetOptionText(mDisplayedIndex, mDisplayedOptionText);
1006 ActuallyDisplayText(PR_FALSE);
1008 if (!aElements.AppendElement(mDisplayContent))
1009 return NS_ERROR_OUT_OF_MEMORY;
1011 nsCOMPtr<nsINodeInfo> nodeInfo;
1012 nodeInfo = nimgr->GetNodeInfo(nsGkAtoms::input, nsnull, kNameSpaceID_XHTML);
1014 // create button which drops the list down
1015 NS_NewHTMLElement(getter_AddRefs(mButtonContent), nodeInfo.forget(),
1016 dom::NOT_FROM_PARSER);
1017 if (!mButtonContent)
1018 return NS_ERROR_OUT_OF_MEMORY;
1020 // make someone to listen to the button. If its pressed by someone like Accessibility
1021 // then open or close the combo box.
1022 mButtonListener = new nsComboButtonListener(this);
1023 mButtonContent->AddEventListenerByIID(mButtonListener,
1024 NS_GET_IID(nsIDOMMouseListener));
1026 mButtonContent->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
1027 NS_LITERAL_STRING("button"), PR_FALSE);
1028 // Set tabindex="-1" so that the button is not tabbable
1029 mButtonContent->SetAttr(kNameSpaceID_None, nsGkAtoms::tabindex,
1030 NS_LITERAL_STRING("-1"), PR_FALSE);
1032 if (!aElements.AppendElement(mButtonContent))
1033 return NS_ERROR_OUT_OF_MEMORY;
1035 return NS_OK;
1038 void
1039 nsComboboxControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
1040 PRUint32 aFilter)
1042 aElements.MaybeAppendElement(mDisplayContent);
1043 aElements.MaybeAppendElement(mButtonContent);
1046 // XXXbz this is a for-now hack. Now that display:inline-block works,
1047 // need to revisit this.
1048 class nsComboboxDisplayFrame : public nsBlockFrame {
1049 public:
1050 NS_DECL_FRAMEARENA_HELPERS
1052 nsComboboxDisplayFrame (nsStyleContext* aContext,
1053 nsComboboxControlFrame* aComboBox)
1054 : nsBlockFrame(aContext),
1055 mComboBox(aComboBox)
1058 // Need this so that line layout knows that this block's width
1059 // depends on the available width.
1060 virtual nsIAtom* GetType() const;
1062 virtual PRBool IsFrameOfType(PRUint32 aFlags) const
1064 return nsBlockFrame::IsFrameOfType(aFlags &
1065 ~(nsIFrame::eReplacedContainsBlock));
1068 NS_IMETHOD Reflow(nsPresContext* aPresContext,
1069 nsHTMLReflowMetrics& aDesiredSize,
1070 const nsHTMLReflowState& aReflowState,
1071 nsReflowStatus& aStatus);
1073 NS_IMETHOD BuildDisplayList(nsDisplayListBuilder* aBuilder,
1074 const nsRect& aDirtyRect,
1075 const nsDisplayListSet& aLists);
1077 protected:
1078 nsComboboxControlFrame* mComboBox;
1081 NS_IMPL_FRAMEARENA_HELPERS(nsComboboxDisplayFrame)
1083 nsIAtom*
1084 nsComboboxDisplayFrame::GetType() const
1086 return nsGkAtoms::comboboxDisplayFrame;
1089 NS_IMETHODIMP
1090 nsComboboxDisplayFrame::Reflow(nsPresContext* aPresContext,
1091 nsHTMLReflowMetrics& aDesiredSize,
1092 const nsHTMLReflowState& aReflowState,
1093 nsReflowStatus& aStatus)
1095 nsHTMLReflowState state(aReflowState);
1096 if (state.ComputedHeight() == NS_INTRINSICSIZE) {
1097 // Note that the only way we can have a computed height here is if the
1098 // combobox had a specified height. If it didn't, size based on what our
1099 // rows look like, for lack of anything better.
1100 state.SetComputedHeight(mComboBox->mListControlFrame->GetHeightOfARow());
1102 nscoord computedWidth = mComboBox->mDisplayWidth -
1103 state.mComputedBorderPadding.LeftRight();
1104 if (computedWidth < 0) {
1105 computedWidth = 0;
1107 state.SetComputedWidth(computedWidth);
1109 return nsBlockFrame::Reflow(aPresContext, aDesiredSize, state, aStatus);
1112 NS_IMETHODIMP
1113 nsComboboxDisplayFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
1114 const nsRect& aDirtyRect,
1115 const nsDisplayListSet& aLists)
1117 nsDisplayListCollection set;
1118 nsresult rv = nsBlockFrame::BuildDisplayList(aBuilder, aDirtyRect, set);
1119 if (NS_FAILED(rv))
1120 return rv;
1122 // remove background items if parent frame is themed
1123 if (mComboBox->IsThemed()) {
1124 set.BorderBackground()->DeleteAll();
1127 set.MoveTo(aLists);
1129 return NS_OK;
1132 nsIFrame*
1133 nsComboboxControlFrame::CreateFrameFor(nsIContent* aContent)
1135 NS_PRECONDITION(nsnull != aContent, "null ptr");
1137 NS_ASSERTION(mDisplayContent, "mDisplayContent can't be null!");
1139 if (mDisplayContent != aContent) {
1140 // We only handle the frames for mDisplayContent here
1141 return nsnull;
1144 // Get PresShell
1145 nsIPresShell *shell = PresContext()->PresShell();
1146 nsStyleSet *styleSet = shell->StyleSet();
1148 // create the style contexts for the anonymous block frame and text frame
1149 nsRefPtr<nsStyleContext> styleContext;
1150 styleContext = styleSet->
1151 ResolveAnonymousBoxStyle(nsCSSAnonBoxes::mozDisplayComboboxControlFrame,
1152 mStyleContext);
1153 if (NS_UNLIKELY(!styleContext)) {
1154 return nsnull;
1157 nsRefPtr<nsStyleContext> textStyleContext;
1158 textStyleContext = styleSet->ResolveStyleForNonElement(mStyleContext);
1159 if (NS_UNLIKELY(!textStyleContext)) {
1160 return nsnull;
1163 // Start by by creating our anonymous block frame
1164 mDisplayFrame = new (shell) nsComboboxDisplayFrame(styleContext, this);
1165 if (NS_UNLIKELY(!mDisplayFrame)) {
1166 return nsnull;
1169 nsresult rv = mDisplayFrame->Init(mContent, this, nsnull);
1170 if (NS_FAILED(rv)) {
1171 mDisplayFrame->Destroy();
1172 mDisplayFrame = nsnull;
1173 return nsnull;
1176 // Create a text frame and put it inside the block frame
1177 nsIFrame* textFrame = NS_NewTextFrame(shell, textStyleContext);
1178 if (NS_UNLIKELY(!textFrame)) {
1179 return nsnull;
1182 // initialize the text frame
1183 rv = textFrame->Init(aContent, mDisplayFrame, nsnull);
1184 if (NS_FAILED(rv)) {
1185 mDisplayFrame->Destroy();
1186 mDisplayFrame = nsnull;
1187 textFrame->Destroy();
1188 textFrame = nsnull;
1189 return nsnull;
1191 mDisplayContent->SetPrimaryFrame(textFrame);
1193 nsFrameList textList(textFrame, textFrame);
1194 mDisplayFrame->SetInitialChildList(nsnull, textList);
1195 return mDisplayFrame;
1198 void
1199 nsComboboxControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
1201 // Revoke any pending RedisplayTextEvent
1202 mRedisplayTextEvent.Revoke();
1204 nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), PR_FALSE);
1206 if (mDroppedDown) {
1207 // Get parent view
1208 nsIFrame * listFrame = do_QueryFrame(mListControlFrame);
1209 if (listFrame) {
1210 nsIView* view = listFrame->GetView();
1211 NS_ASSERTION(view, "nsComboboxControlFrame view is null");
1212 if (view) {
1213 nsIWidget* widget = view->GetWidget();
1214 if (widget)
1215 widget->CaptureRollupEvents(this, nsnull, PR_FALSE, PR_TRUE);
1220 // Cleanup frames in popup child list
1221 mPopupFrames.DestroyFramesFrom(aDestructRoot);
1222 nsContentUtils::DestroyAnonymousContent(&mDisplayContent);
1223 nsContentUtils::DestroyAnonymousContent(&mButtonContent);
1224 nsBlockFrame::DestroyFrom(aDestructRoot);
1228 nsFrameList
1229 nsComboboxControlFrame::GetChildList(nsIAtom* aListName) const
1231 if (nsGkAtoms::selectPopupList == aListName) {
1232 return mPopupFrames;
1234 return nsBlockFrame::GetChildList(aListName);
1237 NS_IMETHODIMP
1238 nsComboboxControlFrame::SetInitialChildList(nsIAtom* aListName,
1239 nsFrameList& aChildList)
1241 nsresult rv = NS_OK;
1242 if (nsGkAtoms::selectPopupList == aListName) {
1243 mPopupFrames.SetFrames(aChildList);
1244 } else {
1245 for (nsFrameList::Enumerator e(aChildList); !e.AtEnd(); e.Next()) {
1246 nsCOMPtr<nsIFormControl> formControl =
1247 do_QueryInterface(e.get()->GetContent());
1248 if (formControl && formControl->GetType() == NS_FORM_INPUT_BUTTON) {
1249 mButtonFrame = e.get();
1250 break;
1253 NS_ASSERTION(mButtonFrame, "missing button frame in initial child list");
1254 rv = nsBlockFrame::SetInitialChildList(aListName, aChildList);
1256 return rv;
1259 #define NS_COMBO_FRAME_POPUP_LIST_INDEX (NS_BLOCK_LIST_COUNT)
1261 nsIAtom*
1262 nsComboboxControlFrame::GetAdditionalChildListName(PRInt32 aIndex) const
1264 // Maintain a separate child list for the dropdown list (i.e. popup listbox)
1265 // This is necessary because we don't want the listbox to be included in the layout
1266 // of the combox's children because it would take up space, when it is suppose to
1267 // be floating above the display.
1268 if (aIndex < NS_BLOCK_LIST_COUNT) {
1269 return nsBlockFrame::GetAdditionalChildListName(aIndex);
1272 if (NS_COMBO_FRAME_POPUP_LIST_INDEX == aIndex) {
1273 return nsGkAtoms::selectPopupList;
1275 return nsnull;
1278 //----------------------------------------------------------------------
1279 //nsIRollupListener
1280 //----------------------------------------------------------------------
1281 NS_IMETHODIMP
1282 nsComboboxControlFrame::Rollup(PRUint32 aCount,
1283 nsIContent** aLastRolledUp)
1285 if (aLastRolledUp)
1286 *aLastRolledUp = nsnull;
1288 if (mDroppedDown) {
1289 nsWeakFrame weakFrame(this);
1290 mListControlFrame->AboutToRollup(); // might destroy us
1291 if (!weakFrame.IsAlive())
1292 return NS_OK;
1293 ShowDropDown(PR_FALSE); // might destroy us
1294 if (!weakFrame.IsAlive())
1295 return NS_OK;
1296 mListControlFrame->CaptureMouseEvents(PR_FALSE);
1298 return NS_OK;
1301 void
1302 nsComboboxControlFrame::RollupFromList()
1304 if (ShowList(PR_FALSE))
1305 mListControlFrame->CaptureMouseEvents(PR_FALSE);
1308 PRInt32
1309 nsComboboxControlFrame::UpdateRecentIndex(PRInt32 aIndex)
1311 PRInt32 index = mRecentSelectedIndex;
1312 if (mRecentSelectedIndex == NS_SKIP_NOTIFY_INDEX || aIndex == NS_SKIP_NOTIFY_INDEX)
1313 mRecentSelectedIndex = aIndex;
1314 return index;
1317 class nsDisplayComboboxFocus : public nsDisplayItem {
1318 public:
1319 nsDisplayComboboxFocus(nsDisplayListBuilder* aBuilder,
1320 nsComboboxControlFrame* aFrame)
1321 : nsDisplayItem(aBuilder, aFrame) {
1322 MOZ_COUNT_CTOR(nsDisplayComboboxFocus);
1324 #ifdef NS_BUILD_REFCNT_LOGGING
1325 virtual ~nsDisplayComboboxFocus() {
1326 MOZ_COUNT_DTOR(nsDisplayComboboxFocus);
1328 #endif
1330 virtual void Paint(nsDisplayListBuilder* aBuilder,
1331 nsIRenderingContext* aCtx);
1332 NS_DISPLAY_DECL_NAME("ComboboxFocus", TYPE_COMBOBOX_FOCUS)
1335 void nsDisplayComboboxFocus::Paint(nsDisplayListBuilder* aBuilder,
1336 nsIRenderingContext* aCtx)
1338 static_cast<nsComboboxControlFrame*>(mFrame)
1339 ->PaintFocus(*aCtx, ToReferenceFrame());
1342 NS_IMETHODIMP
1343 nsComboboxControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
1344 const nsRect& aDirtyRect,
1345 const nsDisplayListSet& aLists)
1347 #ifdef NOISY
1348 printf("%p paint at (%d, %d, %d, %d)\n", this,
1349 aDirtyRect.x, aDirtyRect.y, aDirtyRect.width, aDirtyRect.height);
1350 #endif
1352 if (aBuilder->IsForEventDelivery()) {
1353 // Don't allow children to receive events.
1354 // REVIEW: following old GetFrameForPoint
1355 nsresult rv = DisplayBorderBackgroundOutline(aBuilder, aLists);
1356 NS_ENSURE_SUCCESS(rv, rv);
1357 } else {
1358 // REVIEW: Our in-flow child frames are inline-level so they will paint in our
1359 // content list, so we don't need to mess with layers.
1360 nsresult rv = nsBlockFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
1361 NS_ENSURE_SUCCESS(rv, rv);
1364 // draw a focus indicator only when focus rings should be drawn
1365 nsIDocument* doc = mContent->GetCurrentDoc();
1366 if (doc) {
1367 nsPIDOMWindow* window = doc->GetWindow();
1368 if (window && window->ShouldShowFocusRing()) {
1369 nsPresContext *presContext = PresContext();
1370 const nsStyleDisplay *disp = GetStyleDisplay();
1371 if ((!IsThemed(disp) ||
1372 !presContext->GetTheme()->ThemeDrawsFocusForWidget(presContext, this, disp->mAppearance)) &&
1373 mDisplayFrame && IsVisibleForPainting(aBuilder)) {
1374 nsresult rv = aLists.Content()->AppendNewToTop(
1375 new (aBuilder) nsDisplayComboboxFocus(aBuilder, this));
1376 NS_ENSURE_SUCCESS(rv, rv);
1381 return DisplaySelectionOverlay(aBuilder, aLists.Content());
1384 void nsComboboxControlFrame::PaintFocus(nsIRenderingContext& aRenderingContext,
1385 nsPoint aPt)
1387 /* Do we need to do anything? */
1388 nsEventStates eventStates = mContent->IntrinsicState();
1389 if (eventStates.HasState(NS_EVENT_STATE_DISABLED) || mFocused != this)
1390 return;
1392 aRenderingContext.PushState();
1393 nsRect clipRect = mDisplayFrame->GetRect() + aPt;
1394 aRenderingContext.SetClipRect(clipRect, nsClipCombine_kIntersect);
1396 // REVIEW: Why does the old code paint mDisplayFrame again? We've
1397 // already painted it in the children above. So clipping it here won't do
1398 // us much good.
1400 /////////////////////
1401 // draw focus
1403 aRenderingContext.SetLineStyle(nsLineStyle_kDotted);
1404 aRenderingContext.SetColor(GetStyleColor()->mColor);
1406 //aRenderingContext.DrawRect(clipRect);
1408 nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
1409 clipRect.width -= onePixel;
1410 clipRect.height -= onePixel;
1411 aRenderingContext.DrawLine(clipRect.x, clipRect.y,
1412 clipRect.x+clipRect.width, clipRect.y);
1413 aRenderingContext.DrawLine(clipRect.x+clipRect.width, clipRect.y,
1414 clipRect.x+clipRect.width, clipRect.y+clipRect.height);
1415 aRenderingContext.DrawLine(clipRect.x+clipRect.width, clipRect.y+clipRect.height,
1416 clipRect.x, clipRect.y+clipRect.height);
1417 aRenderingContext.DrawLine(clipRect.x, clipRect.y+clipRect.height,
1418 clipRect.x, clipRect.y);
1419 aRenderingContext.DrawLine(clipRect.x, clipRect.y+clipRect.height,
1420 clipRect.x, clipRect.y);
1422 aRenderingContext.PopState();
1425 //---------------------------------------------------------
1426 // gets the content (an option) by index and then set it as
1427 // being selected or not selected
1428 //---------------------------------------------------------
1429 NS_IMETHODIMP
1430 nsComboboxControlFrame::OnOptionSelected(PRInt32 aIndex, PRBool aSelected)
1432 if (mDroppedDown) {
1433 nsISelectControlFrame *selectFrame = do_QueryFrame(mListControlFrame);
1434 if (selectFrame) {
1435 selectFrame->OnOptionSelected(aIndex, aSelected);
1437 } else {
1438 if (aSelected) {
1439 nsAutoScriptBlocker blocker;
1440 RedisplayText(aIndex);
1441 } else {
1442 nsWeakFrame weakFrame(this);
1443 RedisplaySelectedText();
1444 if (weakFrame.IsAlive()) {
1445 FireValueChangeEvent(); // Fire after old option is unselected
1450 return NS_OK;
1453 void nsComboboxControlFrame::FireValueChangeEvent()
1455 // Fire ValueChange event to indicate data value of combo box has changed
1456 nsContentUtils::AddScriptRunner(
1457 new nsPLDOMEvent(mContent, NS_LITERAL_STRING("ValueChange"), PR_TRUE,
1458 PR_FALSE));
1461 void
1462 nsComboboxControlFrame::OnContentReset()
1464 if (mListControlFrame) {
1465 mListControlFrame->OnContentReset();
1470 //--------------------------------------------------------
1471 // nsIStatefulFrame
1472 //--------------------------------------------------------
1473 NS_IMETHODIMP
1474 nsComboboxControlFrame::SaveState(SpecialStateID aStateID,
1475 nsPresState** aState)
1477 if (!mListControlFrame)
1478 return NS_ERROR_FAILURE;
1480 nsIStatefulFrame* stateful = do_QueryFrame(mListControlFrame);
1481 return stateful->SaveState(aStateID, aState);
1484 NS_IMETHODIMP
1485 nsComboboxControlFrame::RestoreState(nsPresState* aState)
1487 if (!mListControlFrame)
1488 return NS_ERROR_FAILURE;
1490 nsIStatefulFrame* stateful = do_QueryFrame(mListControlFrame);
1491 NS_ASSERTION(stateful, "Must implement nsIStatefulFrame");
1492 return stateful->RestoreState(aState);
1497 // Camino uses a native widget for the combobox
1498 // popup, which affects drawing and event
1499 // handling here and in nsListControlFrame.
1501 // Also, Fennec use a custom combobox built-in widget
1504 /* static */
1505 PRBool
1506 nsComboboxControlFrame::ToolkitHasNativePopup()
1508 return nsContentUtils::GetBoolPref("ui.use_native_popup_windows");