gecko/layout/xul/base/src/nsTitleBarFrame.cpp

332 lines
11 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla 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/MPL/
*
* 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 the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Original Author: Eric J. Burley (ericb@neoplanet.com)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsCOMPtr.h"
#include "nsTitleBarFrame.h"
#include "nsIContent.h"
#include "nsIDocument.h"
#include "nsIDOMXULDocument.h"
#include "nsIDOMNodeList.h"
#include "nsGkAtoms.h"
#include "nsIWidget.h"
#include "nsMenuPopupFrame.h"
#include "nsPresContext.h"
#include "nsIDocShellTreeItem.h"
#include "nsPIDOMWindow.h"
#include "nsIViewManager.h"
#include "nsGUIEvent.h"
#include "nsEventDispatcher.h"
#include "nsDisplayList.h"
//
// NS_NewTitleBarFrame
//
// Creates a new TitleBar frame and returns it
//
nsIFrame*
NS_NewTitleBarFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
{
return new (aPresShell) nsTitleBarFrame(aPresShell, aContext);
} // NS_NewTitleBarFrame
nsTitleBarFrame::nsTitleBarFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
:nsBoxFrame(aPresShell, aContext, PR_FALSE)
{
mTrackingMouseMove = PR_FALSE;
}
NS_IMETHODIMP
nsTitleBarFrame::Init(nsIContent* aContent,
nsIFrame* aParent,
nsIFrame* asPrevInFlow)
{
nsresult rv = nsBoxFrame::Init(aContent, aParent, asPrevInFlow);
CreateViewForFrame(PresContext(), this, GetStyleContext(), PR_TRUE);
return rv;
}
class nsDisplayTitleBarRedirector : public nsDisplayWrapList {
public:
nsDisplayTitleBarRedirector(nsIFrame* aFrame, nsDisplayItem* aItem,
nsIFrame* aTargetFrame)
: nsDisplayWrapList(aFrame, aItem), mTargetFrame(aTargetFrame) {}
nsDisplayTitleBarRedirector(nsIFrame* aFrame, nsDisplayList* aList,
nsIFrame* aTargetFrame)
: nsDisplayWrapList(aFrame, aList), mTargetFrame(aTargetFrame) {}
virtual nsIFrame* HitTest(nsDisplayListBuilder* aBuilder, nsPoint aPt,
HitTestState* aState);
NS_DISPLAY_DECL_NAME("XULTitleBarRedirector")
private:
nsIFrame* mTargetFrame;
};
nsIFrame* nsDisplayTitleBarRedirector::HitTest(nsDisplayListBuilder* aBuilder,
nsPoint aPt, HitTestState* aState)
{
nsIFrame* frame = mList.HitTest(aBuilder, aPt, aState);
if (!frame)
return nsnull;
// We want a drag over a background-like child of the titlebar to move the
// window, which means that the event must be retargetted at the titlebar.
// However, other elements such as controls should receive events normally.
// Here, we look for tags that usually represent empty space. A loop that
// iterates up the parent chain is used so that these elements can still
// appear inside a non-background element, for instance an hbox that is
// inside a button should still be targetted as normal.
nsIFrame* foundframe = frame;
while (foundframe && foundframe != mTargetFrame) {
nsIContent* content = foundframe->GetContent();
if (content) {
// not a XUL element so target normally
if (content->GetNameSpaceID() != kNameSpaceID_XUL)
return frame;
nsIAtom* tag = content->Tag();
// if this is not a background-like element, use the original frame that
// was targetted, so that the event is processed as normal
if (tag != nsGkAtoms::box &&
tag != nsGkAtoms::hbox &&
tag != nsGkAtoms::vbox &&
tag != nsGkAtoms::spacer &&
tag != nsGkAtoms::toolbar &&
tag != nsGkAtoms::toolbaritem &&
tag != nsGkAtoms::toolbarspacer &&
tag != nsGkAtoms::toolbarspring &&
tag != nsGkAtoms::toolbarseparator)
return frame;
}
foundframe = foundframe->GetParent();
}
// retarget to the titlebar
return mTargetFrame;
}
class nsXULTitleBarWrapper : public nsDisplayWrapper
{
public:
nsXULTitleBarWrapper(nsIFrame* aTargetFrame)
: mTargetFrame(aTargetFrame) {}
virtual nsDisplayItem* WrapList(nsDisplayListBuilder* aBuilder,
nsIFrame* aFrame, nsDisplayList* aList) {
return new (aBuilder) nsDisplayTitleBarRedirector(aFrame, aList, mTargetFrame);
}
virtual nsDisplayItem* WrapItem(nsDisplayListBuilder* aBuilder,
nsDisplayItem* aItem) {
return new (aBuilder) nsDisplayTitleBarRedirector(aItem->GetUnderlyingFrame(),
aItem, mTargetFrame);
}
private:
nsIFrame* mTargetFrame;
};
NS_IMETHODIMP
nsTitleBarFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists)
{
// override, since we don't want children to get events
if (aBuilder->IsForEventDelivery()) {
if (mTrackingMouseMove ||
!mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::allowevents,
nsGkAtoms::_true, eCaseMatters))
return NS_OK;
nsDisplayListCollection set;
nsresult rv = nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, set);
NS_ENSURE_SUCCESS(rv, rv);
nsXULTitleBarWrapper wrapper(this);
return wrapper.WrapLists(aBuilder, this, set, aLists);
}
return nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, aLists);
}
NS_IMETHODIMP
nsTitleBarFrame::HandleEvent(nsPresContext* aPresContext,
nsGUIEvent* aEvent,
nsEventStatus* aEventStatus)
{
PRBool doDefault = PR_TRUE;
switch (aEvent->message) {
case NS_MOUSE_BUTTON_DOWN: {
if (aEvent->eventStructType == NS_MOUSE_EVENT &&
static_cast<nsMouseEvent*>(aEvent)->button ==
nsMouseEvent::eLeftButton)
{
// titlebar has no effect in non-chrome shells
nsCOMPtr<nsISupports> cont = aPresContext->GetContainer();
nsCOMPtr<nsIDocShellTreeItem> dsti = do_QueryInterface(cont);
if (dsti) {
PRInt32 type = -1;
if (NS_SUCCEEDED(dsti->GetItemType(&type)) &&
type == nsIDocShellTreeItem::typeChrome) {
// we're tracking.
mTrackingMouseMove = PR_TRUE;
// start capture.
CaptureMouseEvents(aPresContext,PR_TRUE);
// remember current mouse coordinates.
mLastPoint = aEvent->refPoint;
}
}
*aEventStatus = nsEventStatus_eConsumeNoDefault;
doDefault = PR_FALSE;
}
}
break;
case NS_MOUSE_BUTTON_UP: {
if(mTrackingMouseMove && aEvent->eventStructType == NS_MOUSE_EVENT &&
static_cast<nsMouseEvent*>(aEvent)->button ==
nsMouseEvent::eLeftButton)
{
// we're done tracking.
mTrackingMouseMove = PR_FALSE;
// end capture
CaptureMouseEvents(aPresContext,PR_FALSE);
*aEventStatus = nsEventStatus_eConsumeNoDefault;
doDefault = PR_FALSE;
}
}
break;
case NS_MOUSE_MOVE: {
if(mTrackingMouseMove)
{
nsPoint nsMoveBy = aEvent->refPoint - mLastPoint;
nsIFrame* parent = GetParent();
while (parent && parent->GetType() != nsGkAtoms::menuPopupFrame)
parent = parent->GetParent();
// if the titlebar is in a popup, move the popup's widget, otherwise
// move the widget associated with the window
if (parent) {
nsCOMPtr<nsIWidget> widget;
(static_cast<nsMenuPopupFrame*>(parent))->
GetWidget(getter_AddRefs(widget));
nsRect bounds;
widget->GetScreenBounds(bounds);
widget->Move(bounds.x + nsMoveBy.x, bounds.y + nsMoveBy.y);
}
else {
nsIPresShell* presShell = aPresContext->PresShell();
nsPIDOMWindow *window = presShell->GetDocument()->GetWindow();
window->MoveBy(nsMoveBy.x, nsMoveBy.y);
}
*aEventStatus = nsEventStatus_eConsumeNoDefault;
doDefault = PR_FALSE;
}
}
break;
case NS_MOUSE_CLICK:
if (NS_IS_MOUSE_LEFT_CLICK(aEvent))
{
MouseClicked(aPresContext, aEvent);
}
break;
}
if ( doDefault )
return nsBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
else
return NS_OK;
}
NS_IMETHODIMP
nsTitleBarFrame::CaptureMouseEvents(nsPresContext* aPresContext,PRBool aGrabMouseEvents)
{
// get its view
nsIView* view = GetView();
PRBool result;
if (view) {
nsIViewManager* viewMan = view->GetViewManager();
if (viewMan) {
// nsIWidget* widget = view->GetWidget();
if (aGrabMouseEvents) {
viewMan->GrabMouseEvents(view,result);
//mIsCapturingMouseEvents = PR_TRUE;
//widget->CaptureMouse(PR_TRUE);
} else {
viewMan->GrabMouseEvents(nsnull,result);
//mIsCapturingMouseEvents = PR_FALSE;
//widget->CaptureMouse(PR_FALSE);
}
}
}
return NS_OK;
}
void
nsTitleBarFrame::MouseClicked(nsPresContext* aPresContext, nsGUIEvent* aEvent)
{
// Execute the oncommand event handler.
nsEventStatus status = nsEventStatus_eIgnore;
nsXULCommandEvent event(aEvent ? NS_IS_TRUSTED_EVENT(aEvent) : PR_FALSE,
NS_XUL_COMMAND, nsnull);
nsEventDispatcher::Dispatch(mContent, aPresContext, &event, nsnull, &status);
}