/* -*- 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.0 (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. */ #include "nsCOMPtr.h" #include "nsListControlFrame.h" #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 "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 "nsIDOMUIEvent.h" #include "nsIPrivateDOMEvent.h" #include "nsIStatefulFrame.h" #include "nsISupportsArray.h" #include "nsISupportsPrimitives.h" #include "nsIComponentManager.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); // 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"; //--------------------------------------------------------- nsresult NS_NewListControlFrame(nsIFrame** aNewFrame) { NS_PRECONDITION(aNewFrame, "null OUT ptr"); if (nsnull == aNewFrame) { return NS_ERROR_NULL_POINTER; } nsListControlFrame* it = new nsListControlFrame; if (!it) { return NS_ERROR_OUT_OF_MEMORY; } *aNewFrame = it; return NS_OK; } //--------------------------------------------------------- nsListControlFrame::nsListControlFrame() { mHitFrame = nsnull; mSelectedIndex = kNothingSelected; mComboboxFrame = nsnull; mFormFrame = nsnull; mDisplayed = PR_FALSE; mButtonDown = PR_FALSE; mLastFrame = nsnull; mMaxWidth = 0; mMaxHeight = 0; mPresContext = nsnull; mEndExtendedIndex = kNothingSelected; mStartExtendedIndex = kNothingSelected; mIgnoreMouseUp = PR_FALSE; mIsCapturingMouseEvents = PR_FALSE; mSelectionCache = nsnull; mSelectionCacheLength = -1; } //--------------------------------------------------------- nsListControlFrame::~nsListControlFrame() { nsCOMPtr reciever(do_QueryInterface(mContent)); // we shouldn't have to unregister this listener because when // our frame goes away all these content node go away as well // because our frame is the only one who references them. reciever->RemoveEventListenerByIID((nsIDOMMouseListener *)this, kIDOMMouseListenerIID); reciever->RemoveEventListenerByIID((nsIDOMMouseMotionListener *)this, kIDOMMouseMotionListenerIID); reciever->RemoveEventListenerByIID((nsIDOMKeyListener *)this, kIDOMKeyListenerIID); mComboboxFrame = nsnull; mFormFrame = nsnull; NS_IF_RELEASE(mPresContext); if (mSelectionCache) { delete[] mSelectionCache; } } //--------------------------------------------------------- //NS_IMPL_ADDREF(nsListControlFrame) //NS_IMPL_RELEASE(nsListControlFrame) //--------------------------------------------------------- NS_IMETHODIMP nsListControlFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr) { if (NULL == aInstancePtr) { return NS_ERROR_NULL_POINTER; } if (aIID.Equals(nsCOMTypeInfo::GetIID())) { *aInstancePtr = (void*) ((nsIFormControlFrame*) this); return NS_OK; } if (aIID.Equals(nsCOMTypeInfo::GetIID())) { *aInstancePtr = (void *)((nsIListControlFrame*)this); return NS_OK; } if (aIID.Equals(nsCOMTypeInfo::GetIID())) { *aInstancePtr = (void *)((nsISelectControlFrame*)this); return NS_OK; } if (aIID.Equals(kIDOMMouseListenerIID)) { *aInstancePtr = (void*)(nsIDOMMouseListener*) this; NS_ADDREF_THIS(); return NS_OK; } if (aIID.Equals(kIDOMMouseMotionListenerIID)) { *aInstancePtr = (void*)(nsIDOMMouseMotionListener*) this; NS_ADDREF_THIS(); return NS_OK; } if (aIID.Equals(kIDOMKeyListenerIID)) { *aInstancePtr = (void*)(nsIDOMKeyListener*) this; NS_ADDREF_THIS(); return NS_OK; } if (aIID.Equals(NS_GET_IID(nsIStatefulFrame))) { *aInstancePtr = (void*)(nsIStatefulFrame*) this; NS_ADDREF_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. NS_IMETHODIMP nsListControlFrame::Reflow(nsIPresContext& aPresContext, nsHTMLReflowMetrics& aDesiredSize, const nsHTMLReflowState& aReflowState, nsReflowStatus& aStatus) { #ifdef DEBUG_rodsXXX printf("nsListControlFrame::Reflow Reason: "); switch (aReflowState.reason) { case eReflowReason_Initial:printf("eReflowReason_Initial\n");break; case eReflowReason_Incremental:printf("eReflowReason_Incremental\n");break; case eReflowReason_Resize:printf("eReflowReason_Resize\n");break; case eReflowReason_StyleChange:printf("eReflowReason_StyleChange\n");break; } #endif // DEBUG_rods // 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 + /*printf("nsListControlFrame::Reflow Reason: "); switch (aReflowState.reason) { case eReflowReason_Initial:printf("eReflowReason_Initial\n");break; case eReflowReason_Incremental:printf("eReflowReason_Incremental\n");break; case eReflowReason_Resize:printf("eReflowReason_Resize\n");break; case eReflowReason_StyleChange:printf("eReflowReason_StyleChange\n");break; }*/ aDesiredSize.width = 0; aDesiredSize.height = 0; // Add the list frame as a child of the form if (IsInDropDownMode() == PR_FALSE && !mFormFrame && (eReflowReason_Initial == aReflowState.reason)) { nsFormFrame::AddFormControlFrame(aPresContext, *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 == firstPassState.reason) { nsIFrame* targetFrame; firstPassState.reflowCommand->GetTarget(targetFrame); if (this == targetFrame) { nsIReflowCommand::ReflowType type; aReflowState.reflowCommand->GetType(type); firstPassState.reason = eReflowReason_StyleChange; firstPassState.reflowCommand = nsnull; } } nsScrollFrame::Reflow(aPresContext, scrolledAreaDesiredSize, firstPassState, aStatus); // Compute the bounding box of the contents of the list using the area // calculated by the first reflow as a starting point. nscoord scrolledAreaWidth = scrolledAreaDesiredSize.maxElementSize->width; nscoord scrolledAreaHeight = scrolledAreaDesiredSize.height; 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 scrollbar width scrolledAreaWidth -= scrollbarWidth; // 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); } mBorderOffsetY = border.top; scrolledAreaWidth -= (border.left + border.right); scrolledAreaHeight -= (border.top + border.bottom); // Now the scrolledAreaWidth and scrolledAreaHeight are exactly // wide and high enough to enclose their contents PRBool isInDropDownMode = IsInDropDownMode(); nscoord visibleWidth = 0; if (isInDropDownMode) { // Calculate visible width for dropdown if (NS_UNCONSTRAINEDSIZE == aReflowState.mComputedWidth) { visibleWidth = scrolledAreaWidth; } else { visibleWidth = aReflowState.mComputedWidth - (border.left + border.right); } } else { if (NS_UNCONSTRAINEDSIZE == aReflowState.mComputedWidth) { visibleWidth = scrolledAreaWidth; } else { visibleWidth = aReflowState.mComputedWidth; } } // 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); // Check to see if we have zero item and // whether we have no width and height // The following code measures the width and height // of a bogus string so the list actually displays PRInt32 length = 0; GetNumberOfOptions(&length); /*if (!isInDropDownMode && (0 == length || (0 == visibleWidth && 0 == heightOfARow))) { nsCOMPtr presShell; nsresult rv = aPresContext.GetShell(getter_AddRefs(presShell)); if (NS_SUCCEEDED(rv) && presShell) { nsCOMPtr renderContext; rv = presShell->CreateRenderingContext(this, getter_AddRefs(renderContext)); if (NS_SUCCEEDED(rv) && renderContext) { nsSize size; rv = presShell->CreateRenderingContext(this, getter_AddRefs(renderContext)); const nsStyleFont* fontStyle = (const nsStyleFont*)mStyleContext->GetStyleData(eStyleStruct_Font); renderContext->SetFont(fontStyle->mFont); nsFormControlHelper::GetTextSize(aPresContext, this, nsAutoString("XX"), size, renderContext); visibleWidth = size.width; heightOfARow = size.height; } } }*/ 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; if (visibleHeight > (kMaxDropDownRows * heightOfARow)) { visibleHeight = (kMaxDropDownRows * heightOfARow); } } else { // Calculate the visible height of the listbox if (NS_UNCONSTRAINEDSIZE != aReflowState.mComputedHeight) { visibleHeight = aReflowState.mComputedHeight; } else { PRInt32 numRows = 1; GetSizeAttribute(&numRows); if (numRows == kNoSizeSpecified) { visibleHeight = aReflowState.mComputedHeight; } else { visibleHeight = numRows * 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 && !isInDropDownMode) { visibleWidth += scrollbarWidth; mIsScrollbarVisible = PR_TRUE; // XXX temp code } else { mIsScrollbarVisible = PR_FALSE; // XXX temp code } // options that are null end up with no height // so we need to make sure the list box size is at some small minimum // because we could have a select with a single // which means we size the select do to almost nothing. // We also have to make sure we set the mMaxWidth & mMaxHeight // because these are used to calculate the size of the select // get the css size //nsSize txtSize; //nsFormControlHelper::GetTextSize(aPresContext, this,nsAutoString("."), txtSize, aReflowState.rendContext); /*if (visibleWidth < scrollbarWidth) { visibleWidth = scrollbarWidth; mMaxWidth = scrollbarWidth; }*/ if (visibleHeight < scrollbarHeight) { visibleHeight = scrollbarHeight; mMaxHeight = scrollbarHeight; } // 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; mDisplayed = PR_TRUE; 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, nsFont& aFont) { nsFormControlHelper::GetFont(this, aPresContext, mStyleContext, aFont); return NS_OK; } //--------------------------------------------------------- PRBool nsListControlFrame::IsOptionElement(nsIContent* aContent) { PRBool result = PR_FALSE; nsCOMPtr optElem; if (NS_SUCCEEDED(aContent->QueryInterface(nsCOMTypeInfo::GetIID(),(void**) getter_AddRefs(optElem)))) { if (optElem != nsnull) { result = PR_TRUE; } } return result; } //--------------------------------------------------------- PRBool nsListControlFrame::IsOptionElementFrame(nsIFrame *aFrame) { nsIContent *content = nsnull; aFrame->GetContent(&content); PRBool result = PR_FALSE; if (nsnull != content) { result = IsOptionElement(content); NS_RELEASE(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() { //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(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 nsIAtom * selectedAtom = NS_NewAtom(kMozSelected); if (PR_TRUE == mDisplayed) { aContent->SetAttribute(kNameSpaceID_None, selectedAtom, "", PR_TRUE); //ForceRedraw(); } else { aContent->SetAttribute(kNameSpaceID_None, selectedAtom, "", PR_FALSE); } NS_RELEASE(selectedAtom); } //--------------------------------------------------------- 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 nsIAtom * selectedAtom = NS_NewAtom(kMozSelected); if (PR_TRUE == mDisplayed) { aContent->UnsetAttribute(kNameSpaceID_None, selectedAtom, PR_TRUE); //ForceRedraw(); } else { aContent->UnsetAttribute(kNameSpaceID_None, selectedAtom, PR_FALSE); } NS_RELEASE(selectedAtom); } //--------------------------------------------------------- // 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 nsIDOMHTMLCollection* options = GetOptions(mContent); if (nsnull != options) { PRUint32 numOptions; options->GetLength(&numOptions); PRUint32 inx; for (inx = 0; inx < numOptions; inx++) { nsIContent* option = nsnull; option = GetOptionAsContent(options, inx); if (nsnull != option) { if (option == aContent) { NS_RELEASE(option); NS_RELEASE(options); return inx; } NS_RELEASE(option); } } NS_RELEASE(options); } return kNothingSelected; } //--------------------------------------------------------- // Finds the index of the hit frame's content in the list // of option elements //--------------------------------------------------------- PRInt32 nsListControlFrame::GetSelectedIndexFromFrame(nsIFrame *aHitFrame) { PRInt32 indx = kNothingSelected; // Get the content of the frame that was selected nsIContent* selectedContent = nsnull; NS_ASSERTION(aHitFrame, "No frame for html