gecko-dev/layout/xul/base/src/nsPopupSetFrame.cpp

537 lines
15 KiB
C++
Raw Normal View History

/* -*- 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 "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsXULAtoms.h"
#include "nsHTMLAtoms.h"
#include "nsPopupSetFrame.h"
#include "nsBoxFrame.h"
#include "nsIContent.h"
#include "prtypes.h"
#include "nsIAtom.h"
#include "nsIPresContext.h"
#include "nsIStyleContext.h"
#include "nsIReflowCommand.h"
#include "nsCSSRendering.h"
#include "nsINameSpaceManager.h"
#include "nsLayoutAtoms.h"
#include "nsMenuPopupFrame.h"
#include "nsMenuBarFrame.h"
#include "nsIView.h"
#include "nsIWidget.h"
#include "nsIDocument.h"
#include "nsIDOMNSDocument.h"
#include "nsIDOMDocument.h"
#include "nsIDOMXULDocument.h"
#include "nsIDOMElement.h"
#include "nsISupportsArray.h"
#include "nsIDOMText.h"
#define NS_MENU_POPUP_LIST_INDEX (NS_AREA_FRAME_ABSOLUTE_LIST_INDEX + 1)
//
// NS_NewPopupSetFrame
//
// Wrapper for creating a new menu popup container
//
nsresult
NS_NewPopupSetFrame(nsIFrame** aNewFrame)
{
NS_PRECONDITION(aNewFrame, "null OUT ptr");
if (nsnull == aNewFrame) {
return NS_ERROR_NULL_POINTER;
}
nsPopupSetFrame* it = new nsPopupSetFrame;
if ( !it )
return NS_ERROR_OUT_OF_MEMORY;
*aNewFrame = it;
return NS_OK;
}
NS_IMETHODIMP_(nsrefcnt)
nsPopupSetFrame::AddRef(void)
{
return NS_OK;
}
NS_IMETHODIMP_(nsrefcnt)
nsPopupSetFrame::Release(void)
{
return NS_OK;
}
NS_IMETHODIMP nsPopupSetFrame::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
if (NULL == aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
*aInstancePtr = NULL;
if (aIID.Equals(nsIPopupSetFrame::GetIID())) {
*aInstancePtr = (void*)(nsIPopupSetFrame*) this;
NS_ADDREF_THIS();
return NS_OK;
}
return nsBoxFrame::QueryInterface(aIID, aInstancePtr);
}
//
// nsPopupSetFrame cntr
//
nsPopupSetFrame::nsPopupSetFrame()
1999-09-10 08:47:12 +00:00
:mPresContext(nsnull), mElementFrame(nsnull)
{
} // cntr
1999-09-10 08:47:12 +00:00
nsIFrame*
nsPopupSetFrame::GetActiveChild()
{
return mPopupFrames.FirstChild();
}
NS_IMETHODIMP
nsPopupSetFrame::Init(nsIPresContext& aPresContext,
nsIContent* aContent,
nsIFrame* aParent,
nsIStyleContext* aContext,
nsIFrame* aPrevInFlow)
{
mPresContext = &aPresContext; // Don't addref it. Our lifetime is shorter.
nsresult rv = nsBoxFrame::Init(aPresContext, aContent, aParent, aContext, aPrevInFlow);
return rv;
}
// The following methods are all overridden to ensure that the menupopup frames
// are placed in the appropriate list.
NS_IMETHODIMP
nsPopupSetFrame::FirstChild(nsIAtom* aListName,
nsIFrame** aFirstChild) const
{
if (nsLayoutAtoms::popupList == aListName) {
*aFirstChild = mPopupFrames.FirstChild();
} else {
nsBoxFrame::FirstChild(aListName, aFirstChild);
}
return NS_OK;
}
NS_IMETHODIMP
nsPopupSetFrame::SetInitialChildList(nsIPresContext& aPresContext,
nsIAtom* aListName,
nsIFrame* aChildList)
{
nsresult rv = NS_OK;
if (nsLayoutAtoms::popupList == aListName) {
mPopupFrames.SetFrames(aChildList);
} else {
nsFrameList frames(aChildList);
// We may have menupopups in here. Get them out, and move them into
// the popup frame list.
nsIFrame* frame = frames.FirstChild();
while (frame) {
nsCOMPtr<nsIContent> content;
frame->GetContent(getter_AddRefs(content));
nsCOMPtr<nsIAtom> tag;
content->GetTag(*getter_AddRefs(tag));
1999-09-10 08:47:12 +00:00
if (tag.get() == nsXULAtoms::popup) {
// Remove this frame from the list and place it in the other list.
frames.RemoveFrame(frame);
mPopupFrames.AppendFrame(this, frame);
nsIFrame* first = frames.FirstChild();
}
frame->GetNextSibling(&frame);
}
// Didn't find it.
rv = nsBoxFrame::SetInitialChildList(aPresContext, aListName, aChildList);
}
return rv;
}
NS_IMETHODIMP
nsPopupSetFrame::GetAdditionalChildListName(PRInt32 aIndex,
nsIAtom** aListName) const
{
// Maintain a separate child list for the menu contents.
// This is necessary because we don't want the menu contents to be included in the layout
// of the menu's single item because it would take up space, when it is supposed to
// be floating above the display.
/*NS_PRECONDITION(nsnull != aListName, "null OUT parameter pointer");
*aListName = nsnull;
if (NS_MENU_POPUP_LIST_INDEX == aIndex) {
*aListName = nsLayoutAtoms::popupList;
NS_ADDREF(*aListName);
return NS_OK;
}*/
return nsBoxFrame::GetAdditionalChildListName(aIndex, aListName);
}
NS_IMETHODIMP
nsPopupSetFrame::Destroy(nsIPresContext& aPresContext)
{
// Cleanup frames in popup child list
mPopupFrames.DestroyFrames(aPresContext);
return nsBoxFrame::Destroy(aPresContext);
}
NS_IMETHODIMP
nsPopupSetFrame::Reflow(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
{
nsresult rv = nsBoxFrame::Reflow(aPresContext, aDesiredSize, aReflowState, aStatus);
1999-09-10 08:47:12 +00:00
nsIFrame* frame = GetActiveChild();
if (!frame || (rv != NS_OK))
return rv;
// Constrain the child's width and height to aAvailableWidth and aAvailableHeight
nsSize availSize(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
nsHTMLReflowState kidReflowState(aPresContext, aReflowState, frame,
availSize);
kidReflowState.mComputedWidth = NS_UNCONSTRAINEDSIZE;
kidReflowState.mComputedHeight = NS_UNCONSTRAINEDSIZE;
// Reflow child
nscoord w = aDesiredSize.width;
nscoord h = aDesiredSize.height;
rv = ReflowChild(frame, aPresContext, aDesiredSize, kidReflowState, aStatus);
// Set the child's width and height to its desired size
nsRect rect;
frame->GetRect(rect);
rect.width = aDesiredSize.width;
rect.height = aDesiredSize.height;
frame->SetRect(rect);
// Don't let it affect our size.
aDesiredSize.width = w;
aDesiredSize.height = h;
return rv;
}
NS_IMETHODIMP
nsPopupSetFrame::DidReflow(nsIPresContext& aPresContext,
nsDidReflowStatus aStatus)
{
// Sync up the view.
1999-09-10 08:47:12 +00:00
nsIFrame* activeChild = GetActiveChild();
if (activeChild) {
((nsMenuPopupFrame*)activeChild)->SyncViewWithFrame(aPresContext, PR_TRUE, mElementFrame, mXPos, mYPos);
}
return nsBoxFrame::DidReflow(aPresContext, aStatus);
}
// Overridden Box method.
NS_IMETHODIMP
nsPopupSetFrame::Dirty(const nsHTMLReflowState& aReflowState, nsIFrame*& incrementalChild)
{
incrementalChild = nsnull;
nsresult rv = NS_OK;
// Dirty any children that need it.
nsIFrame* frame;
aReflowState.reflowCommand->GetNext(frame, PR_FALSE);
if (frame == nsnull) {
incrementalChild = this;
return rv;
}
// Now call our original box frame method
rv = nsBoxFrame::Dirty(aReflowState, incrementalChild);
if (rv != NS_OK || incrementalChild)
return rv;
1999-09-10 08:47:12 +00:00
nsIFrame* popup = GetActiveChild();
if (popup && (frame == popup)) {
// An incremental reflow command is targeting something inside our
// hidden popup view. We can't actually return the child, since it
// won't ever be found by box. Instead return ourselves, so that box
// will later send us an incremental reflow command.
incrementalChild = this;
// In order for the child box to know what it needs to reflow, we need
// to call its Dirty method...
nsIFrame* ignore;
nsIBox* ibox;
if (NS_SUCCEEDED(popup->QueryInterface(nsIBox::GetIID(), (void**)&ibox)) && ibox)
ibox->Dirty(aReflowState, ignore);
}
return rv;
}
NS_IMETHODIMP
nsPopupSetFrame::RemoveFrame(nsIPresContext& aPresContext,
nsIPresShell& aPresShell,
nsIAtom* aListName,
nsIFrame* aOldFrame)
{
// need to rebuild all the springs.
for (int i=0; i < mSpringCount; i++)
mSprings[i].clear();
1999-08-19 03:51:25 +00:00
nsresult rv;
if (mPopupFrames.ContainsFrame(aOldFrame)) {
// Go ahead and remove this frame.
mPopupFrames.DestroyFrame(aPresContext, aOldFrame);
1999-08-19 03:51:25 +00:00
rv = GenerateDirtyReflowCommand(aPresContext, aPresShell);
} else {
rv = nsBoxFrame::RemoveFrame(aPresContext, aPresShell, aListName, aOldFrame);
}
1999-08-19 03:51:25 +00:00
return rv;
}
NS_IMETHODIMP
nsPopupSetFrame::InsertFrames(nsIPresContext& aPresContext,
nsIPresShell& aPresShell,
nsIAtom* aListName,
nsIFrame* aPrevFrame,
nsIFrame* aFrameList)
{
// need to rebuild all the springs.
for (int i=0; i < mSpringCount; i++)
mSprings[i].clear();
nsCOMPtr<nsIContent> frameChild;
aFrameList->GetContent(getter_AddRefs(frameChild));
nsCOMPtr<nsIAtom> tag;
1999-08-19 03:51:25 +00:00
nsresult rv;
frameChild->GetTag(*getter_AddRefs(tag));
1999-09-10 08:47:12 +00:00
if (tag && tag.get() == nsXULAtoms::popup) {
mPopupFrames.InsertFrames(nsnull, nsnull, aFrameList);
1999-08-19 03:51:25 +00:00
rv = GenerateDirtyReflowCommand(aPresContext, aPresShell);
} else {
rv = nsBoxFrame::InsertFrames(aPresContext, aPresShell, aListName, aPrevFrame, aFrameList);
}
1999-08-19 03:51:25 +00:00
return rv;
}
NS_IMETHODIMP
nsPopupSetFrame::AppendFrames(nsIPresContext& aPresContext,
nsIPresShell& aPresShell,
nsIAtom* aListName,
nsIFrame* aFrameList)
{
if (!aFrameList)
return NS_OK;
// need to rebuild all the springs.
for (int i=0; i < mSpringCount; i++)
mSprings[i].clear();
nsCOMPtr<nsIContent> frameChild;
aFrameList->GetContent(getter_AddRefs(frameChild));
nsCOMPtr<nsIAtom> tag;
1999-08-19 03:51:25 +00:00
nsresult rv;
frameChild->GetTag(*getter_AddRefs(tag));
1999-09-10 08:47:12 +00:00
if (tag && tag.get() == nsXULAtoms::popup) {
mPopupFrames.AppendFrames(nsnull, aFrameList);
1999-08-19 03:51:25 +00:00
rv = GenerateDirtyReflowCommand(aPresContext, aPresShell);
} else {
rv = nsBoxFrame::AppendFrames(aPresContext, aPresShell, aListName, aFrameList);
}
1999-08-19 03:51:25 +00:00
return rv;
}
NS_IMETHODIMP
nsPopupSetFrame::CreatePopup(nsIFrame* aElementFrame, nsIContent* aPopupContent,
PRInt32 aXPos, PRInt32 aYPos,
const nsString& aPopupType, const nsString& anAnchorAlignment,
const nsString& aPopupAlignment)
{
1999-09-10 08:47:12 +00:00
// Cache the element frame.
mElementFrame = aElementFrame;
// Show the popup at the specified position.
mXPos = aXPos;
mYPos = aYPos;
1999-09-10 08:47:12 +00:00
printf("X Pos: %d\n", mXPos);
printf("Y Pos: %d\n", mYPos);
if (!OnCreate(aPopupContent))
return NS_OK;
// Generate the popup.
MarkAsGenerated(aPopupContent);
// Now we'll have it in our child frame list.
// Now open the popup.
OpenPopup(PR_TRUE);
return NS_OK;
}
1999-09-21 01:03:00 +00:00
NS_IMETHODIMP
nsPopupSetFrame::HidePopup()
{
ActivatePopup(PR_FALSE);
return NS_OK;
}
NS_IMETHODIMP
nsPopupSetFrame::DestroyPopup()
{
OpenPopup(PR_FALSE);
return NS_OK;
}
void
nsPopupSetFrame::MarkAsGenerated(nsIContent* aPopupContent)
{
// Ungenerate all other popups in the set. No more than one can exist
// at any point in time.
PRInt32 childCount;
mContent->ChildCount(childCount);
for (PRInt32 i = 0; i < childCount; i++) {
nsCOMPtr<nsIContent> childContent;
mContent->ChildAt(i, *getter_AddRefs(childContent));
// Retrieve the menugenerated attribute.
nsAutoString value;
childContent->GetAttribute(kNameSpaceID_None, nsXULAtoms::menugenerated,
value);
if (value == "true") {
// Ungenerate this element.
childContent->UnsetAttribute(kNameSpaceID_None, nsXULAtoms::menugenerated,
PR_TRUE);
}
}
// Set our attribute, but only if we aren't already generated.
// Retrieve the menugenerated attribute.
nsAutoString value;
aPopupContent->GetAttribute(kNameSpaceID_None, nsXULAtoms::menugenerated,
value);
if (value != "true") {
1999-09-10 08:47:12 +00:00
// Generate this element.
aPopupContent->SetAttribute(kNameSpaceID_None, nsXULAtoms::menugenerated, "true",
PR_TRUE);
}
}
void
1999-09-10 08:47:12 +00:00
nsPopupSetFrame::OpenPopup(PRBool aActivateFlag)
{
1999-09-10 08:47:12 +00:00
if (aActivateFlag) {
ActivatePopup(PR_TRUE);
nsIFrame* activeChild = GetActiveChild();
}
else {
// Close the menu.
nsIFrame* frame = GetActiveChild();
nsMenuPopupFrame* menuPopup = (nsMenuPopupFrame*)frame;
// Make sure we clear out our own items.
if (menuPopup)
menuPopup->SetCurrentMenuItem(nsnull);
ActivatePopup(PR_FALSE);
// Set the focus back to our view's widget.
nsIView* view;
mElementFrame->GetView(&view);
if (!view) {
nsPoint offset;
mElementFrame->GetOffsetFromView(offset, &view);
}
1999-09-10 08:47:12 +00:00
nsCOMPtr<nsIWidget> widget;
view->GetWidget(*getter_AddRefs(widget));
if (widget)
widget->SetFocus();
}
}
void
1999-09-10 08:47:12 +00:00
nsPopupSetFrame::ActivatePopup(PRBool aActivateFlag)
{
1999-09-10 08:47:12 +00:00
nsCOMPtr<nsIContent> content;
GetActiveChildElement(getter_AddRefs(content));
if (content) {
if (aActivateFlag)
content->SetAttribute(kNameSpaceID_None, nsXULAtoms::menuactive, "true", PR_TRUE);
else content->UnsetAttribute(kNameSpaceID_None, nsXULAtoms::menuactive, PR_TRUE);
}
}
1999-09-10 08:47:12 +00:00
PRBool
nsPopupSetFrame::OnCreate(nsIContent* aPopupContent)
{
nsEventStatus status = nsEventStatus_eIgnore;
nsMouseEvent event;
event.eventStructType = NS_EVENT;
event.message = NS_MENU_CREATE;
if (aPopupContent) {
nsresult rv = aPopupContent->HandleDOMEvent(*mPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, status);
if ( NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault )
return PR_FALSE;
}
1999-09-10 08:47:12 +00:00
return PR_TRUE;
}
1999-09-10 08:47:12 +00:00
PRBool
nsPopupSetFrame::OnDestroy()
{
nsEventStatus status = nsEventStatus_eIgnore;
nsMouseEvent event;
event.eventStructType = NS_EVENT;
event.message = NS_MENU_DESTROY;
nsCOMPtr<nsIContent> content;
GetActiveChildElement(getter_AddRefs(content));
if (content) {
nsresult rv = content->HandleDOMEvent(*mPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, status);
if ( NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault )
return PR_FALSE;
}
return PR_TRUE;
}
void
nsPopupSetFrame::GetActiveChildElement(nsIContent** aResult)
{
*aResult = nsnull;
nsIFrame* child = GetActiveChild();
if (child) {
child->GetContent(aResult);
}
1999-09-11 00:46:07 +00:00
}