/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Mozilla Communicator client code. * * The Initial Developer of the Original Code is Netscape Communications * Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * Pierre Phaneuf */ #include "nsCOMPtr.h" #include "nsListControlFrame.h" #include "nsFormControlFrame.h" // for COMPARE macro #include "nsFormControlHelper.h" #include "nsHTMLIIDs.h" #include "nsHTMLAtoms.h" #include "nsIHTMLContent.h" #include "nsIFormControl.h" #include "nsINameSpaceManager.h" #include "nsIDeviceContext.h" #include "nsIDOMHTMLCollection.h" #include "nsIDOMNSHTMLOptionCollection.h" #include "nsIDOMHTMLSelectElement.h" #include "nsIDOMHTMLOptionElement.h" #include "nsIComboboxControlFrame.h" #include "nsIViewManager.h" #include "nsFormFrame.h" #include "nsIScrollableView.h" #include "nsIDOMHTMLOptGroupElement.h" #include "nsWidgetsCID.h" #include "nsIReflowCommand.h" #include "nsIPresShell.h" #include "nsHTMLParts.h" #include "nsIDOMEventReceiver.h" #include "nsIEventStateManager.h" #include "nsIDOMKeyEvent.h" #include "nsIDOMMouseEvent.h" #include "nsIPrivateDOMEvent.h" #include "nsIStatefulFrame.h" #include "nsISupportsArray.h" #include "nsISupportsPrimitives.h" #include "nsIComponentManager.h" #include "nsILookAndFeel.h" #include "nsLayoutAtoms.h" #include "nsIFontMetrics.h" #include "nsVoidArray.h" #include "nsIScrollableFrame.h" #include "nsIDOMEventTarget.h" #include "nsISelectElement.h" #include "nsIPrivateDOMEvent.h" static NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); static NS_DEFINE_IID(kIDOMMouseMotionListenerIID, NS_IDOMMOUSEMOTIONLISTENER_IID); static NS_DEFINE_IID(kIDOMKeyListenerIID, NS_IDOMKEYLISTENER_IID); static NS_DEFINE_IID(kIDOMNodeIID, NS_IDOMNODE_IID); static NS_DEFINE_IID(kIFrameIID, NS_IFRAME_IID); static NS_DEFINE_IID(kIPrivateDOMEventIID, NS_IPRIVATEDOMEVENT_IID); //static NS_DEFINE_IID(kBlockFrameCID, NS_BLOCK_FRAME_CID); // Constants const nscoord kMaxDropDownRows = 20; // This matches the setting for 4.x browsers const PRInt32 kDefaultMultiselectHeight = 4; // This is compatible with 4.x browsers const PRInt32 kNothingSelected = -1; const PRInt32 kMaxZ = 0x7fffffff; //XXX: Shouldn't there be a define somewhere for MaxInt for PRInt32 const PRInt32 kNoSizeSpecified = -1; //XXX: This is temporary. It simulates psuedo states by using a attribute selector on // -moz-option-selected in the ua.css style sheet. This will not be needed when //The event state manager is functional. KMM //const char * kMozSelected = "-moz-option-selected"; // it is now using "nsLayoutAtoms::optionSelectedPseudo" //--------------------------------------------------------- nsresult NS_NewListControlFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame) { NS_PRECONDITION(aNewFrame, "null OUT ptr"); if (nsnull == aNewFrame) { return NS_ERROR_NULL_POINTER; } nsListControlFrame* it = new (aPresShell) nsListControlFrame; if (!it) { return NS_ERROR_OUT_OF_MEMORY; } #if 0 // set the state flags (if any are provided) nsFrameState state; it->GetFrameState( &state ); state |= NS_BLOCK_SPACE_MGR; it->SetFrameState( state ); #endif *aNewFrame = it; return NS_OK; } //----------------------------------------------------------- // Reflow Debugging Macros // These let us "see" how many reflow counts are happening //----------------------------------------------------------- #ifdef DO_REFLOW_COUNTER #define MAX_REFLOW_CNT 1024 static PRInt32 gTotalReqs = 0;; static PRInt32 gTotalReflows = 0;; static PRInt32 gReflowControlCntRQ[MAX_REFLOW_CNT]; static PRInt32 gReflowControlCnt[MAX_REFLOW_CNT]; static PRInt32 gReflowInx = -1; #define REFLOW_COUNTER() \ if (mReflowId > -1) \ gReflowControlCnt[mReflowId]++; #define REFLOW_COUNTER_REQUEST() \ if (mReflowId > -1) \ gReflowControlCntRQ[mReflowId]++; #define REFLOW_COUNTER_DUMP(__desc) \ if (mReflowId > -1) {\ gTotalReqs += gReflowControlCntRQ[mReflowId];\ gTotalReflows += gReflowControlCnt[mReflowId];\ PRINTF("** Id:%5d %s RF: %d RQ: %d %d/%d %5.2f\n", \ mReflowId, (__desc), \ gReflowControlCnt[mReflowId], \ gReflowControlCntRQ[mReflowId],\ gTotalReflows, gTotalReqs, float(gTotalReflows)/float(gTotalReqs)*100.0f);\ } #define REFLOW_COUNTER_INIT() \ if (gReflowInx < MAX_REFLOW_CNT) { \ gReflowInx++; \ mReflowId = gReflowInx; \ gReflowControlCnt[mReflowId] = 0; \ gReflowControlCntRQ[mReflowId] = 0; \ } else { \ mReflowId = -1; \ } // reflow messages #define REFLOW_DEBUG_MSG(_msg1) PRINTF((_msg1)) #define REFLOW_DEBUG_MSG2(_msg1, _msg2) PRINTF((_msg1), (_msg2)) #define REFLOW_DEBUG_MSG3(_msg1, _msg2, _msg3) PRINTF((_msg1), (_msg2), (_msg3)) #define REFLOW_DEBUG_MSG4(_msg1, _msg2, _msg3, _msg4) PRINTF((_msg1), (_msg2), (_msg3), (_msg4)) #else //------------- #define REFLOW_COUNTER_REQUEST() #define REFLOW_COUNTER() #define REFLOW_COUNTER_DUMP(__desc) #define REFLOW_COUNTER_INIT() #define REFLOW_DEBUG_MSG(_msg) #define REFLOW_DEBUG_MSG2(_msg1, _msg2) #define REFLOW_DEBUG_MSG3(_msg1, _msg2, _msg3) #define REFLOW_DEBUG_MSG4(_msg1, _msg2, _msg3, _msg4) #endif //------------------------------------------ // This is for being VERY noisy //------------------------------------------ #ifdef DO_VERY_NOISY #define REFLOW_NOISY_MSG(_msg1) PRINTF((_msg1)) #define REFLOW_NOISY_MSG2(_msg1, _msg2) PRINTF((_msg1), (_msg2)) #define REFLOW_NOISY_MSG3(_msg1, _msg2, _msg3) PRINTF((_msg1), (_msg2), (_msg3)) #define REFLOW_NOISY_MSG4(_msg1, _msg2, _msg3, _msg4) PRINTF((_msg1), (_msg2), (_msg3), (_msg4)) #else #define REFLOW_NOISY_MSG(_msg) #define REFLOW_NOISY_MSG2(_msg1, _msg2) #define REFLOW_NOISY_MSG3(_msg1, _msg2, _msg3) #define REFLOW_NOISY_MSG4(_msg1, _msg2, _msg3, _msg4) #endif //------------------------------------------ // Displays value in pixels or twips //------------------------------------------ #ifdef DO_PIXELS #define PX(__v) __v / 15 #else #define PX(__v) __v #endif //------------------------------------------ // Asserts if we return a desired size that // doesn't correctly match the mComputedWidth //------------------------------------------ #ifdef DO_UNCONSTRAINED_CHECK #define UNCONSTRAINED_CHECK() \ if (aReflowState.mComputedWidth != NS_UNCONSTRAINEDSIZE) { \ nscoord width = aDesiredSize.width - borderPadding.left - borderPadding.right; \ if (width != aReflowState.mComputedWidth) { \ PRINTF("aDesiredSize.width %d %d != aReflowState.mComputedWidth %d\n", aDesiredSize.width, width, aReflowState.mComputedWidth); \ } \ NS_ASSERTION(width == aReflowState.mComputedWidth, "Returning bad value when constrained!"); \ } #else #define UNCONSTRAINED_CHECK() #endif //------------------------------------------------------ //-- Done with macros //------------------------------------------------------ //--------------------------------------------------------- nsListControlFrame::nsListControlFrame() : mWeakReferent(this) { mSelectedIndex = kNothingSelected; mComboboxFrame = nsnull; mFormFrame = nsnull; mButtonDown = PR_FALSE; mMaxWidth = 0; mMaxHeight = 0; mPresContext = nsnull; mEndExtendedIndex = kNothingSelected; mStartExtendedIndex = kNothingSelected; mIsCapturingMouseEvents = PR_FALSE; mDelayedIndexSetting = kNothingSelected; mDelayedValueSetting = PR_FALSE; mSelectionCache = new nsVoidArray(); mSelectionCacheLength = 0; mIsAllContentHere = PR_FALSE; mIsAllFramesHere = PR_FALSE; mHasBeenInitialized = PR_FALSE; mCacheSize.width = -1; mCacheSize.height = -1; mCachedMaxElementSize.width = -1; mCachedMaxElementSize.height = -1; mCachedAvailableSize.width = -1; mCachedAvailableSize.height = -1; mCachedUnconstrainedSize.width = -1; mCachedUnconstrainedSize.height = -1; mOverrideReflowOpt = PR_FALSE; REFLOW_COUNTER_INIT() } //--------------------------------------------------------- nsListControlFrame::~nsListControlFrame() { REFLOW_COUNTER_DUMP("nsLCF"); mComboboxFrame = nsnull; if (mFormFrame) { mFormFrame->RemoveFormControlFrame(*this); mFormFrame = nsnull; } NS_IF_RELEASE(mPresContext); if (mSelectionCache) { delete mSelectionCache; } } // for Bug 47302 (remove this comment later) NS_IMETHODIMP nsListControlFrame::Destroy(nsIPresContext *aPresContext) { // get the reciever interface from the browser button's content node nsCOMPtr reciever(do_QueryInterface(mContent)); nsCOMPtr mouseListener = do_QueryInterface(mEventListener); if (!mouseListener) { return NS_ERROR_NO_INTERFACE; } reciever->RemoveEventListenerByIID(mouseListener, NS_GET_IID(nsIDOMMouseListener)); nsCOMPtr mouseMotionListener = do_QueryInterface(mEventListener); if (!mouseMotionListener) { return NS_ERROR_NO_INTERFACE; } reciever->RemoveEventListenerByIID(mouseMotionListener, NS_GET_IID(nsIDOMMouseMotionListener)); nsCOMPtr keyListener = do_QueryInterface(mEventListener); if (!keyListener) { return NS_ERROR_NO_INTERFACE; } reciever->RemoveEventListenerByIID(keyListener, NS_GET_IID(nsIDOMKeyListener)); if (IsInDropDownMode() == PR_FALSE) { nsFormControlFrame::RegUnRegAccessKey(aPresContext, NS_STATIC_CAST(nsIFrame*, this), PR_FALSE); } return nsScrollFrame::Destroy(aPresContext); } //--------------------------------------------------------- //NS_IMPL_ADDREF(nsListControlFrame) //NS_IMPL_RELEASE(nsListControlFrame) //--------------------------------------------------------- // Frames are not refcounted, no need to AddRef NS_IMETHODIMP nsListControlFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr) { if (NULL == aInstancePtr) { return NS_ERROR_NULL_POINTER; } if (aIID.Equals(NS_GET_IID(nsIFormControlFrame))) { *aInstancePtr = (void*) ((nsIFormControlFrame*) this); return NS_OK; } if (aIID.Equals(NS_GET_IID(nsIListControlFrame))) { *aInstancePtr = (void *)((nsIListControlFrame*)this); return NS_OK; } if (aIID.Equals(NS_GET_IID(nsISelectControlFrame))) { *aInstancePtr = (void *)((nsISelectControlFrame*)this); return NS_OK; } if (aIID.Equals(kIDOMMouseListenerIID)) { *aInstancePtr = (void*)(nsIDOMMouseListener*) this; return NS_OK; } if (aIID.Equals(kIDOMMouseMotionListenerIID)) { *aInstancePtr = (void*)(nsIDOMMouseMotionListener*) this; return NS_OK; } if (aIID.Equals(kIDOMKeyListenerIID)) { *aInstancePtr = (void*)(nsIDOMKeyListener*) this; return NS_OK; } if (aIID.Equals(NS_GET_IID(nsIStatefulFrame))) { *aInstancePtr = (void*)(nsIStatefulFrame*) this; return NS_OK; } return nsScrollFrame::QueryInterface(aIID, aInstancePtr); } //--------------------------------------------------------- // Reflow is overriden to constrain the listbox height to the number of rows and columns // specified. #ifdef DO_REFLOW_DEBUG static int myCounter = 0; static void printSize(char * aDesc, nscoord aSize) { PRINTF(" %s: ", aDesc); if (aSize == NS_UNCONSTRAINEDSIZE) { PRINTF("UNC"); } else { PRINTF("%d", aSize); } } #endif //----------------------------------------------------------------- // Main Reflow for ListBox/Dropdown //----------------------------------------------------------------- NS_IMETHODIMP nsListControlFrame::Reflow(nsIPresContext* aPresContext, nsHTMLReflowMetrics& aDesiredSize, const nsHTMLReflowState& aReflowState, nsReflowStatus& aStatus) { DO_GLOBAL_REFLOW_COUNT("nsListControlFrame", aReflowState.reason); REFLOW_COUNTER_REQUEST(); aStatus = NS_FRAME_COMPLETE; #ifdef DO_REFLOW_DEBUG PRINTF("%p ** Id: %d nsLCF::Reflow %d R: ", this, mReflowId, myCounter++); switch (aReflowState.reason) { case eReflowReason_Initial: PRINTF("Initia");break; case eReflowReason_Incremental: PRINTF("Increm");break; case eReflowReason_Resize: PRINTF("Resize");break; case eReflowReason_StyleChange: PRINTF("StyleC");break; case eReflowReason_Dirty: PRINTF("Dirty ");break; default:PRINTF("%d", aReflowState.reason);break; } printSize("AW", aReflowState.availableWidth); printSize("AH", aReflowState.availableHeight); printSize("CW", aReflowState.mComputedWidth); printSize("CH", aReflowState.mComputedHeight); PRINTF("\n"); #if 0 { const nsStyleDisplay* display; GetStyleData(eStyleStruct_Display, (const nsStyleStruct*&)display); PRINTF("+++++++++++++++++++++++++++++++++ "); switch (display->mVisible) { case NS_STYLE_VISIBILITY_COLLAPSE: PRINTF("NS_STYLE_VISIBILITY_COLLAPSE\n");break; case NS_STYLE_VISIBILITY_HIDDEN: PRINTF("NS_STYLE_VISIBILITY_HIDDEN\n");break; case NS_STYLE_VISIBILITY_VISIBLE: PRINTF("NS_STYLE_VISIBILITY_VISIBLE\n");break; } } #endif #endif // DEBUG_rodsXXX PRBool bailOnWidth; PRBool bailOnHeight; // This ifdef is for turning off the optimization // so we can check timings against the old version #if 1 nsFormControlFrame::SkipResizeReflow(mCacheSize, mCachedMaxElementSize, mCachedAvailableSize, aDesiredSize, aReflowState, aStatus, bailOnWidth, bailOnHeight); // Here we bail if both the height and the width haven't changed // also we see if we should override the optimization // // The optimization can get overridden by the combobox // sometime the combobox knows that the list MUST do a full reflow // no matter what if (!mOverrideReflowOpt && bailOnWidth && bailOnHeight) { REFLOW_DEBUG_MSG3("*** Done nsLCF - Bailing on DW: %d DH: %d ", PX(aDesiredSize.width), PX(aDesiredSize.height)); REFLOW_DEBUG_MSG3("bailOnWidth %d bailOnHeight %d\n", PX(bailOnWidth), PX(bailOnHeight)); NS_ASSERTION(aDesiredSize.width < 100000, "Width is still NS_UNCONSTRAINEDSIZE"); NS_ASSERTION(aDesiredSize.height < 100000, "Height is still NS_UNCONSTRAINEDSIZE"); return NS_OK; } else if (mOverrideReflowOpt) { mOverrideReflowOpt = PR_FALSE; } #else bailOnWidth = PR_FALSE; bailOnHeight = PR_FALSE; #endif #ifdef DEBUG_rodsXXX // Lists out all the options { nsresult rv = NS_ERROR_FAILURE; nsCOMPtr options = getter_AddRefs(GetOptions(mContent)); if (options) { PRUint32 numOptions; options->GetLength(&numOptions); PRINTF("--- Num of Items %d ---\n", numOptions); for (PRUint32 i=0;i optionElement = getter_AddRefs(GetOption(*options, i)); if (optionElement) { nsAutoString text; rv = optionElement->GetLabel(text); if (NS_CONTENT_ATTR_HAS_VALUE != rv || 0 == text.Length()) { if (NS_OK != optionElement->GetText(text)) { text = "No Value"; } } else { text = "No Value"; } PRINTF("[%d] - %s\n", i, text.ToNewCString()); } } } } #endif // DEBUG_rodsXXX if (mDelayedIndexSetting != kNothingSelected) { SetOptionSelected(mDelayedIndexSetting, mDelayedValueSetting); mDelayedIndexSetting = kNothingSelected; } // If all the content and frames are here // then initialize it before reflow if (mIsAllContentHere && !mHasBeenInitialized) { if (PR_FALSE == mIsAllFramesHere) { CheckIfAllFramesHere(); } if (mIsAllFramesHere && !mHasBeenInitialized) { mHasBeenInitialized = PR_TRUE; Reset(aPresContext); } } if (eReflowReason_Incremental == aReflowState.reason) { nsIFrame* targetFrame; aReflowState.reflowCommand->GetTarget(targetFrame); if (targetFrame == this) { // XXX So this may do it too often // the side effect of this is if the user has scrolled to some other place in the list and // an incremental reflow comes through the list gets scrolled to the first selected item // I haven't been able to make it do it, but it will do it // basically the real solution is to know when all the reframes are there. nsCOMPtr content = getter_AddRefs(GetOptionContent(mSelectedIndex)); if (content) { ScrollToFrame(content); } } } // Strategy: Let the inherited reflow happen as though the width and height of the // ScrollFrame are big enough to allow the listbox to // shrink to fit the longest option element line in the list. // The desired width and height returned by the inherited reflow is returned, // unless one of the following has been specified. // 1. A css width has been specified. // 2. The size has been specified. // 3. The css height has been specified, but the number of rows has not. // The size attribute overrides the height setting but the height setting // should be honored if there isn't a size specified. // Determine the desired width + height for the listbox + aDesiredSize.width = 0; aDesiredSize.height = 0; // Add the list frame as a child of the form if (eReflowReason_Initial == aReflowState.reason) { if (mPresState) { RestoreState(aPresContext, mPresState); mPresState = do_QueryInterface(nsnull); } if (IsInDropDownMode() == PR_FALSE && !mFormFrame) { nsFormControlFrame::RegUnRegAccessKey(aPresContext, NS_STATIC_CAST(nsIFrame*, this), PR_TRUE); nsFormFrame::AddFormControlFrame(aPresContext, *NS_STATIC_CAST(nsIFrame*, this)); } } //--Calculate a width just big enough for the scrollframe to shrink around the //longest element in the list nsHTMLReflowState secondPassState(aReflowState); nsHTMLReflowState firstPassState(aReflowState); //nsHTMLReflowState firstPassState(aPresContext, nsnull, // this, aDesiredSize); // Get the size of option elements inside the listbox // Compute the width based on the longest line in the listbox. firstPassState.mComputedWidth = NS_UNCONSTRAINEDSIZE; firstPassState.mComputedHeight = NS_UNCONSTRAINEDSIZE; firstPassState.availableWidth = NS_UNCONSTRAINEDSIZE; firstPassState.availableHeight = NS_UNCONSTRAINEDSIZE; nsSize scrolledAreaSize(0,0); nsHTMLReflowMetrics scrolledAreaDesiredSize(&scrolledAreaSize); if (eReflowReason_Incremental == aReflowState.reason) { nsIFrame* targetFrame; firstPassState.reflowCommand->GetTarget(targetFrame); if (this == targetFrame) { nsIReflowCommand::ReflowType type; aReflowState.reflowCommand->GetType(type); firstPassState.reason = eReflowReason_StyleChange; firstPassState.reflowCommand = nsnull; } else { nsresult res = nsScrollFrame::Reflow(aPresContext, scrolledAreaDesiredSize, aReflowState, aStatus); if (NS_FAILED(res)) { NS_ASSERTION(aDesiredSize.width < 100000, "Width is still NS_UNCONSTRAINEDSIZE"); NS_ASSERTION(aDesiredSize.height < 100000, "Height is still NS_UNCONSTRAINEDSIZE"); return res; } nsIReflowCommand::ReflowType type; aReflowState.reflowCommand->GetType(type); firstPassState.reason = eReflowReason_StyleChange; firstPassState.reflowCommand = nsnull; } } nsresult res = nsScrollFrame::Reflow(aPresContext, scrolledAreaDesiredSize, firstPassState, aStatus); if (NS_FAILED(res)) { NS_ASSERTION(aDesiredSize.width < 100000, "Width is still NS_UNCONSTRAINEDSIZE"); NS_ASSERTION(aDesiredSize.height < 100000, "Height is still NS_UNCONSTRAINEDSIZE"); return res; } nsIScrollableFrame * scrollableFrame = nsnull; nsRect scrolledRect; if (NS_SUCCEEDED(QueryInterface(NS_GET_IID(nsIScrollableFrame), (void**)&scrollableFrame))) { nsIFrame * scrolledFrame; scrollableFrame->GetScrolledFrame(aPresContext, scrolledFrame); NS_ASSERTION(scrolledFrame != nsnull, "Must have scrollable frame"); scrolledFrame->GetRect(scrolledRect); } else { NS_ASSERTION(scrollableFrame != nsnull, "Must have scrollableFrame frame"); } // Compute the bounding box of the contents of the list using the area // calculated by the first reflow as a starting point. // // The nsScrollFrame::REflow adds in the scrollbar width and border dimensions // to the maxElementSize, so these need to be subtracted nscoord scrolledAreaWidth = scrolledAreaDesiredSize.maxElementSize->width; nscoord scrolledAreaHeight = scrolledAreaDesiredSize.height; // Keep the oringal values mMaxWidth = scrolledAreaWidth; mMaxHeight = scrolledAreaDesiredSize.maxElementSize->height; // The first reflow produces a box with the scrollbar width and borders // added in so we need to subtract them out. // Retrieve the scrollbar's width and height float sbWidth = 0.0; float sbHeight = 0.0;; nsCOMPtr dc; aPresContext->GetDeviceContext(getter_AddRefs(dc)); dc->GetScrollBarDimensions(sbWidth, sbHeight); // Convert to nscoord's by rounding nscoord scrollbarWidth = NSToCoordRound(sbWidth); //nscoord scrollbarHeight = NSToCoordRound(sbHeight); // Subtract out the borders nsMargin border; if (!aReflowState.mStyleSpacing->GetBorder(border)) { NS_NOTYETIMPLEMENTED("percentage border"); border.SizeTo(0, 0, 0, 0); } nsMargin padding; if (!aReflowState.mStyleSpacing->GetPadding(padding)) { NS_NOTYETIMPLEMENTED("percentage padding"); padding.SizeTo(0, 0, 0, 0); } mMaxWidth -= (border.left + border.right + padding.left + padding.right); mMaxHeight -= (border.top + border.bottom + padding.top + padding.bottom); // Now the scrolledAreaWidth and scrolledAreaHeight are exactly // wide and high enough to enclose their contents PRBool isInDropDownMode = IsInDropDownMode(); scrolledAreaWidth -= (border.left + border.right + padding.left + padding.right); scrolledAreaHeight -= (border.top + border.bottom + padding.top + padding.bottom); nscoord visibleWidth = 0; if (isInDropDownMode) { if (NS_UNCONSTRAINEDSIZE == aReflowState.mComputedWidth) { visibleWidth = scrolledAreaWidth; } else { visibleWidth = aReflowState.mComputedWidth; visibleWidth -= (border.left + border.right + padding.left + padding.right); //scrolledAreaHeight -= (border.top + border.bottom + padding.top + padding.bottom); } } else { if (NS_UNCONSTRAINEDSIZE == aReflowState.mComputedWidth) { visibleWidth = scrolledAreaWidth; } else { visibleWidth = aReflowState.mComputedWidth;// - scrollbarWidth; // XXX rods - this hould not be subtracted in //visibleWidth -= (border.left + border.right + padding.left + padding.right); } } // Determine if a scrollbar will be needed, If so we need to add // enough the width to allow for the scrollbar. // The scrollbar will be needed under two conditions: // (size * heightOfaRow) < scrolledAreaHeight or // the height set through Style < scrolledAreaHeight. // Calculate the height of a single row in the listbox or dropdown list // Note: It is calculated based on what layout returns for the maxElement // size, rather than trying to take the scrolledAreaHeight and dividing by the number // of option elements. The reason is that their may be option groups in addition to // option elements. Either of which may be visible or invisible. PRInt32 heightOfARow = scrolledAreaDesiredSize.maxElementSize->height; heightOfARow -= (border.top + border.bottom + padding.top + padding.bottom); // Check to see if we have zero items PRInt32 length = 0; GetNumberOfOptions(&length); // If there is only one option and that option's content is empty // then heightOfARow is zero, so we need to go measure // the height of the option as if it had some text. if (heightOfARow == 0 && length > 0) { nsIContent * option = GetOptionContent(0); if (option != nsnull) { nsIFrame * optFrame; nsCOMPtr presShell; mPresContext->GetShell(getter_AddRefs(presShell)); nsresult result = presShell->GetPrimaryFrameFor(option, &optFrame); if (NS_SUCCEEDED(result) && optFrame != nsnull) { nsCOMPtr optStyle; optFrame->GetStyleContext(getter_AddRefs(optStyle)); if (optStyle) { const nsStyleFont* styleFont = (const nsStyleFont*)optStyle->GetStyleData(eStyleStruct_Font); nsCOMPtr deviceContext; aPresContext->GetDeviceContext(getter_AddRefs(deviceContext)); NS_ASSERTION(deviceContext, "Couldn't get the device context"); nsIFontMetrics * fontMet; result = deviceContext->GetMetricsFor(styleFont->mFont, fontMet); if (NS_SUCCEEDED(result) && fontMet != nsnull) { if (fontMet) { fontMet->GetHeight(heightOfARow); mMaxHeight = heightOfARow; } NS_RELEASE(fontMet); } } } NS_RELEASE(option); } } // Check to see if we have no width and height // The following code measures the width and height // of a bogus string so the list actually displays nscoord visibleHeight = 0; if (isInDropDownMode) { // Compute the visible height of the drop-down list // The dropdown list height is the smaller of it's height setting or the height // of the smallest box that can drawn around it's contents. visibleHeight = scrolledAreaHeight; mNumDisplayRows = kMaxDropDownRows; if (visibleHeight > (mNumDisplayRows * heightOfARow)) { visibleHeight = (mNumDisplayRows * heightOfARow); // This is an adaptive algorithm for figuring out how many rows // should be displayed in the drop down. The standard size is 20 rows, // but on 640x480 it is typically too big. // This takes the height of the screen divides it by two and then subtracts off // an estimated height of the combobox. I estimate it by taking the max element size // of the drop down and multiplying it by 2 (this is arbitrary) then subtract off // the border and padding of the drop down (again rather arbitrary) // This all breaks down if the font of the combobox is a lot larger then the option items // or CSS style has set the height of the combobox to be rather large. // We can fix these cases later if they actually happen. if (isInDropDownMode) { nscoord screenHeightInPixels = 0; if (NS_SUCCEEDED(nsFormControlFrame::GetScreenHeight(aPresContext, screenHeightInPixels))) { float p2t; aPresContext->GetPixelsToTwips(&p2t); nscoord screenHeight = NSIntPixelsToTwips(screenHeightInPixels, p2t); nscoord availDropHgt = (screenHeight / 2) - (heightOfARow*2); // approx half screen minus combo size availDropHgt -= (border.top + border.bottom + padding.top + padding.bottom); nscoord hgt = visibleHeight + border.top + border.bottom + padding.top + padding.bottom; if (heightOfARow > 0) { if (hgt > availDropHgt) { visibleHeight = (availDropHgt / heightOfARow) * heightOfARow; } mNumDisplayRows = visibleHeight / heightOfARow; } else { // Hmmm, not sure what to do here. Punt, and make both of them one visibleHeight = 1; mNumDisplayRows = 1; } } } } } else { // Calculate the visible height of the listbox if (NS_UNCONSTRAINEDSIZE != aReflowState.mComputedHeight) { visibleHeight = aReflowState.mComputedHeight; visibleHeight -= (border.top + border.bottom + padding.top + padding.bottom); } else { mNumDisplayRows = 1; GetSizeAttribute(&mNumDisplayRows); // because we are not a drop down // we will always have 2 or more rows if (mNumDisplayRows >= 1) { visibleHeight = mNumDisplayRows * heightOfARow; } else { PRBool multipleSelections = PR_FALSE; GetMultiple(&multipleSelections); if (multipleSelections) { visibleHeight = PR_MIN(length, kMaxDropDownRows) * heightOfARow; } else { visibleHeight = 2 * heightOfARow; } } } } // There are no items in the list // but we want to include space for the scrollbars // So fake like we will need scrollbars also if (!isInDropDownMode && 0 == length) { scrolledAreaHeight = visibleHeight+1; } PRBool needsVerticalScrollbar = PR_FALSE; if (visibleHeight < scrolledAreaHeight) { needsVerticalScrollbar = PR_TRUE; } if (needsVerticalScrollbar) { mIsScrollbarVisible = PR_TRUE; // XXX temp code } else { mIsScrollbarVisible = PR_FALSE; // XXX temp code } // The visible height is zero, this could be a select with no options // or a select with a single option that has no content or no label // // So this may not be the best solution, but we get the height of the font // for the list frame and use that as the max/minimum size for the contents if (visibleHeight == 0) { nsCOMPtr fontMet; nsresult rvv = nsFormControlHelper::GetFrameFontFM(aPresContext, this, getter_AddRefs(fontMet)); if (NS_SUCCEEDED(rvv) && fontMet) { aReflowState.rendContext->SetFont(fontMet); fontMet->GetHeight(visibleHeight); mMaxHeight = visibleHeight; } } if (NS_UNCONSTRAINEDSIZE == aReflowState.mComputedWidth) { visibleWidth += (border.left + border.right + padding.left + padding.right); } // Do a second reflow with the adjusted width and height settings // This sets up all of the frames with the correct width and height. secondPassState.mComputedWidth = visibleWidth; secondPassState.mComputedHeight = visibleHeight; secondPassState.reason = eReflowReason_Resize; nsScrollFrame::Reflow(aPresContext, aDesiredSize, secondPassState, aStatus); // Set the max element size to be the same as the desired element size. if (nsnull != aDesiredSize.maxElementSize) { aDesiredSize.maxElementSize->width = aDesiredSize.width; aDesiredSize.maxElementSize->height = aDesiredSize.height; } aStatus = NS_FRAME_COMPLETE; #ifdef DEBUG_rods if (!isInDropDownMode) { PRInt32 numRows = 1; GetSizeAttribute(&numRows); PRINTF("%d ", numRows); if (numRows == 2) { COMPARE_QUIRK_SIZE("nsListControlFrame", 56, 38) } else if (numRows == 3) { COMPARE_QUIRK_SIZE("nsListControlFrame", 56, 54) } else if (numRows == 4) { COMPARE_QUIRK_SIZE("nsListControlFrame", 56, 70) } else { COMPARE_QUIRK_SIZE("nsListControlFrame", 127, 118) } } #endif if (aReflowState.availableWidth != NS_UNCONSTRAINEDSIZE) { mCachedAvailableSize.width = aDesiredSize.width - (border.left + border.right + padding.left + padding.right); REFLOW_DEBUG_MSG2("** nsLCF Caching AW: %d\n", PX(mCachedAvailableSize.width)); } if (aReflowState.availableHeight != NS_UNCONSTRAINEDSIZE) { mCachedAvailableSize.height = aDesiredSize.height - (border.top + border.bottom + padding.top + padding.bottom); REFLOW_DEBUG_MSG2("** nsLCF Caching AH: %d\n", PX(mCachedAvailableSize.height)); } //REFLOW_DEBUG_MSG3("** nsLCF Caching AW: %d AH: %d\n", PX(mCachedAvailableSize.width), PX(mCachedAvailableSize.height)); nsFormControlFrame::SetupCachedSizes(mCacheSize, mCachedMaxElementSize, aDesiredSize); REFLOW_DEBUG_MSG3("** Done nsLCF DW: %d DH: %d\n\n", PX(aDesiredSize.width), PX(aDesiredSize.height)); REFLOW_COUNTER(); #ifdef DO_REFLOW_COUNTER if (gReflowControlCnt[mReflowId] > 50) { REFLOW_DEBUG_MSG3("** Id: %d Cnt: %d ", mReflowId, gReflowControlCnt[mReflowId]); REFLOW_DEBUG_MSG3("Done nsLCF DW: %d DH: %d\n", PX(aDesiredSize.width), PX(aDesiredSize.height)); } #endif NS_ASSERTION(aDesiredSize.width < 100000, "Width is still NS_UNCONSTRAINEDSIZE"); NS_ASSERTION(aDesiredSize.height < 100000, "Height is still NS_UNCONSTRAINEDSIZE"); return NS_OK; } //--------------------------------------------------------- NS_IMETHODIMP nsListControlFrame::GetFormContent(nsIContent*& aContent) const { nsIContent* content; nsresult rv; rv = GetContent(&content); aContent = content; return rv; } //--------------------------------------------------------- NS_IMETHODIMP nsListControlFrame::GetFont(nsIPresContext* aPresContext, const nsFont*& aFont) { return nsFormControlHelper::GetFont(this, aPresContext, mStyleContext, aFont); } //--------------------------------------------------------- PRBool nsListControlFrame::IsOptionElement(nsIContent* aContent) { PRBool result = PR_FALSE; nsCOMPtr optElem; if (NS_SUCCEEDED(aContent->QueryInterface(NS_GET_IID(nsIDOMHTMLOptionElement),(void**) getter_AddRefs(optElem)))) { if (optElem != nsnull) { result = PR_TRUE; } } return result; } //--------------------------------------------------------- PRBool nsListControlFrame::IsOptionElementFrame(nsIFrame *aFrame) { PRBool result = PR_FALSE; nsCOMPtr content; aFrame->GetContent(getter_AddRefs(content)); if (content) { result = IsOptionElement(content); } return result; } //--------------------------------------------------------- // Go up the frame tree looking for the first ancestor that has content // which is selectable nsIFrame * nsListControlFrame::GetSelectableFrame(nsIFrame *aFrame) { nsIFrame* selectedFrame = aFrame; while ((nsnull != selectedFrame) && (PR_FALSE ==IsOptionElementFrame(selectedFrame))) { selectedFrame->GetParent(&selectedFrame); } return(selectedFrame); } //--------------------------------------------------------- void nsListControlFrame::ForceRedraw(nsIPresContext* aPresContext) { //XXX: Hack. This should not be needed. The problem is DisplaySelected //and DisplayDeselected set and unset an attribute on generic HTML content //which does not know that it should repaint as the result of the attribute //being set. This should not be needed once the event state manager handles //selection. nsFormControlHelper::ForceDrawFrame(aPresContext, this); } //--------------------------------------------------------- // XXX: Here we introduce a new -moz-option-selected attribute so a attribute // selecitor n the ua.css can change the style when the option is selected. void nsListControlFrame::DisplaySelected(nsIContent* aContent) { //XXX: This is temporary. It simulates psuedo states by using a attribute selector on // -moz-option-selected in the ua.css style sheet. This will not be needed when // The event state manager supports selected states. KMM if (PR_TRUE == mIsAllFramesHere) { aContent->SetAttribute(kNameSpaceID_None, nsLayoutAtoms::optionSelectedPseudo, nsAutoString(), PR_TRUE); //ForceRedraw(); } else { aContent->SetAttribute(kNameSpaceID_None, nsLayoutAtoms::optionSelectedPseudo, nsAutoString(), PR_FALSE); } } //--------------------------------------------------------- void nsListControlFrame::DisplayDeselected(nsIContent* aContent) { //XXX: This is temporary. It simulates psuedo states by using a attribute selector on // -moz-option-selected in the ua.css style sheet. This will not be needed when // The event state manager is functional. KMM if (PR_TRUE == mIsAllFramesHere) { aContent->UnsetAttribute(kNameSpaceID_None, nsLayoutAtoms::optionSelectedPseudo, PR_TRUE); //ForceRedraw(); } else { aContent->UnsetAttribute(kNameSpaceID_None, nsLayoutAtoms::optionSelectedPseudo, PR_FALSE); } } //--------------------------------------------------------- // Starts at the passed in content object and walks up the // parent heierarchy looking for the nsIDOMHTMLOptionElement //--------------------------------------------------------- nsIContent * nsListControlFrame::GetOptionFromContent(nsIContent *aContent) { nsIContent * content = aContent; NS_ADDREF(content); while (nsnull != content) { if (IsOptionElement(content)) { return content; } nsIContent * node = content; node->GetParent(content); // this add refs NS_RELEASE(node); } return nsnull; } //--------------------------------------------------------- // Finds the index of the hit frame's content in the list // of option elements //--------------------------------------------------------- PRInt32 nsListControlFrame::GetSelectedIndexFromContent(nsIContent *aContent) { // Search the list of option elements looking for a match // between the hit frame's content and the content of an option element // get the collection of option items nsCOMPtr options = getter_AddRefs(GetOptions(mContent)); if (options) { PRUint32 numOptions; options->GetLength(&numOptions); PRUint32 inx; for (inx = 0; inx < numOptions; inx++) { nsIContent * option = GetOptionAsContent(options, inx); if (option != nsnull) { if (option == aContent) { NS_RELEASE(option); return inx; } NS_RELEASE(option); } } } return kNothingSelected; } //--------------------------------------------------------- // Finds the index of the hit frame's content in the list // of option elements //--------------------------------------------------------- PRInt32 nsListControlFrame::GetSelectedIndexFromFrame(nsIFrame *aHitFrame) { NS_ASSERTION(aHitFrame, "No frame for html