2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 14:30:37 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2000-07-11 23:37:00 +00:00
|
|
|
*
|
2004-04-18 14:30:37 +00:00
|
|
|
* 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/
|
2000-07-11 23:37:00 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* 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.
|
2000-07-11 23:37:00 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
2004-04-18 14:30:37 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-28 20:14:13 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2000-07-11 23:37:00 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
2004-04-18 14:30:37 +00:00
|
|
|
* Original Author: Eric J. Burley (ericb@neoplanet.com)
|
2000-07-11 23:37:00 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 14:30:37 +00:00
|
|
|
* 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"),
|
2001-09-28 20:14:13 +00:00
|
|
|
* 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
|
2004-04-18 14:30:37 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-28 20:14:13 +00:00
|
|
|
* 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
|
2004-04-18 14:30:37 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2000-07-11 23:37:00 +00:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsTitleBarFrame.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMXULDocument.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
2006-12-26 17:47:52 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2000-07-11 23:37:00 +00:00
|
|
|
#include "nsIWidget.h"
|
2007-04-20 10:56:49 +00:00
|
|
|
#include "nsMenuPopupFrame.h"
|
2004-07-31 23:15:21 +00:00
|
|
|
#include "nsPresContext.h"
|
2007-04-20 10:56:49 +00:00
|
|
|
#include "nsIDocShellTreeItem.h"
|
2005-11-28 23:56:44 +00:00
|
|
|
#include "nsPIDOMWindow.h"
|
2000-07-11 23:37:00 +00:00
|
|
|
#include "nsIViewManager.h"
|
2001-07-16 02:40:48 +00:00
|
|
|
#include "nsGUIEvent.h"
|
2006-03-07 17:08:51 +00:00
|
|
|
#include "nsEventDispatcher.h"
|
2006-01-26 02:29:17 +00:00
|
|
|
#include "nsDisplayList.h"
|
2000-07-11 23:37:00 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// NS_NewTitleBarFrame
|
|
|
|
//
|
2005-10-26 21:46:39 +00:00
|
|
|
// Creates a new TitleBar frame and returns it
|
2000-07-11 23:37:00 +00:00
|
|
|
//
|
2005-10-26 21:46:39 +00:00
|
|
|
nsIFrame*
|
2006-03-26 21:30:36 +00:00
|
|
|
NS_NewTitleBarFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
2000-07-11 23:37:00 +00:00
|
|
|
{
|
2006-03-26 21:30:36 +00:00
|
|
|
return new (aPresShell) nsTitleBarFrame(aPresShell, aContext);
|
2000-07-11 23:37:00 +00:00
|
|
|
} // NS_NewTitleBarFrame
|
|
|
|
|
2006-03-26 21:30:36 +00:00
|
|
|
nsTitleBarFrame::nsTitleBarFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
|
|
|
:nsBoxFrame(aPresShell, aContext, PR_FALSE)
|
2000-07-11 23:37:00 +00:00
|
|
|
{
|
2005-11-28 23:56:44 +00:00
|
|
|
mTrackingMouseMove = PR_FALSE;
|
2000-07-11 23:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-03-09 18:55:21 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTitleBarFrame::Init(nsIContent* aContent,
|
|
|
|
nsIFrame* aParent,
|
|
|
|
nsIFrame* asPrevInFlow)
|
2000-07-11 23:37:00 +00:00
|
|
|
{
|
2006-03-26 21:30:36 +00:00
|
|
|
nsresult rv = nsBoxFrame::Init(aContent, aParent, asPrevInFlow);
|
2000-07-11 23:37:00 +00:00
|
|
|
|
2007-03-30 21:11:41 +00:00
|
|
|
CreateViewForFrame(PresContext(), this, GetStyleContext(), PR_TRUE);
|
2000-07-11 23:37:00 +00:00
|
|
|
|
2005-11-28 23:56:44 +00:00
|
|
|
return rv;
|
2000-07-11 23:37:00 +00:00
|
|
|
}
|
|
|
|
|
2008-04-16 15:29:54 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2006-01-26 02:29:17 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTitleBarFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
2000-07-11 23:37:00 +00:00
|
|
|
{
|
|
|
|
// override, since we don't want children to get events
|
2006-11-21 21:04:49 +00:00
|
|
|
if (aBuilder->IsForEventDelivery()) {
|
2008-04-16 15:29:54 +00:00
|
|
|
if (mTrackingMouseMove ||
|
|
|
|
!mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::allowevents,
|
2006-12-26 17:47:52 +00:00
|
|
|
nsGkAtoms::_true, eCaseMatters))
|
2006-11-21 21:04:49 +00:00
|
|
|
return NS_OK;
|
2008-04-16 15:29:54 +00:00
|
|
|
|
|
|
|
nsDisplayListCollection set;
|
|
|
|
nsresult rv = nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, set);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsXULTitleBarWrapper wrapper(this);
|
|
|
|
return wrapper.WrapLists(aBuilder, this, set, aLists);
|
2006-11-21 21:04:49 +00:00
|
|
|
}
|
2006-01-26 02:29:17 +00:00
|
|
|
return nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, aLists);
|
2000-07-11 23:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2005-11-28 23:56:44 +00:00
|
|
|
nsTitleBarFrame::HandleEvent(nsPresContext* aPresContext,
|
2000-07-11 23:37:00 +00:00
|
|
|
nsGUIEvent* aEvent,
|
|
|
|
nsEventStatus* aEventStatus)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
PRBool doDefault = PR_TRUE;
|
|
|
|
|
|
|
|
switch (aEvent->message) {
|
|
|
|
|
2006-11-16 21:35:39 +00:00
|
|
|
case NS_MOUSE_BUTTON_DOWN: {
|
|
|
|
if (aEvent->eventStructType == NS_MOUSE_EVENT &&
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<nsMouseEvent*>(aEvent)->button ==
|
2006-11-16 21:35:39 +00:00
|
|
|
nsMouseEvent::eLeftButton)
|
|
|
|
{
|
2007-04-20 10:56:49 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2005-11-28 23:56:44 +00:00
|
|
|
|
2006-11-16 21:35:39 +00:00
|
|
|
*aEventStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
doDefault = PR_FALSE;
|
|
|
|
}
|
2005-11-28 23:56:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2006-11-16 21:35:39 +00:00
|
|
|
case NS_MOUSE_BUTTON_UP: {
|
|
|
|
if(mTrackingMouseMove && aEvent->eventStructType == NS_MOUSE_EVENT &&
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<nsMouseEvent*>(aEvent)->button ==
|
2006-11-16 21:35:39 +00:00
|
|
|
nsMouseEvent::eLeftButton)
|
2005-11-28 23:56:44 +00:00
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
2007-04-20 10:56:49 +00:00
|
|
|
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;
|
2007-07-08 07:08:04 +00:00
|
|
|
(static_cast<nsMenuPopupFrame*>(parent))->
|
2007-04-20 10:56:49 +00:00
|
|
|
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);
|
2005-05-04 20:22:32 +00:00
|
|
|
}
|
2005-11-28 23:56:44 +00:00
|
|
|
|
|
|
|
*aEventStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
|
|
|
|
doDefault = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2000-07-11 23:37:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-11-16 21:35:39 +00:00
|
|
|
case NS_MOUSE_CLICK:
|
|
|
|
if (NS_IS_MOUSE_LEFT_CLICK(aEvent))
|
|
|
|
{
|
|
|
|
MouseClicked(aPresContext, aEvent);
|
|
|
|
}
|
2000-07-11 23:37:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-11-28 23:56:44 +00:00
|
|
|
|
|
|
|
if ( doDefault )
|
|
|
|
return nsBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
|
2000-07-11 23:37:00 +00:00
|
|
|
else
|
2005-11-28 23:56:44 +00:00
|
|
|
return NS_OK;
|
2000-07-11 23:37:00 +00:00
|
|
|
}
|
|
|
|
|
2005-11-28 23:56:44 +00:00
|
|
|
NS_IMETHODIMP
|
2004-07-31 23:15:21 +00:00
|
|
|
nsTitleBarFrame::CaptureMouseEvents(nsPresContext* aPresContext,PRBool aGrabMouseEvents)
|
2000-07-11 23:37:00 +00:00
|
|
|
{
|
2005-11-28 23:56:44 +00:00
|
|
|
// get its view
|
2003-08-04 12:39:51 +00:00
|
|
|
nsIView* view = GetView();
|
2000-07-11 23:37:00 +00:00
|
|
|
PRBool result;
|
|
|
|
|
|
|
|
if (view) {
|
2003-08-04 12:39:51 +00:00
|
|
|
nsIViewManager* viewMan = view->GetViewManager();
|
2000-07-11 23:37:00 +00:00
|
|
|
if (viewMan) {
|
2003-08-04 12:39:51 +00:00
|
|
|
// nsIWidget* widget = view->GetWidget();
|
2000-07-11 23:37:00 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-11-28 23:56:44 +00:00
|
|
|
void
|
|
|
|
nsTitleBarFrame::MouseClicked(nsPresContext* aPresContext, nsGUIEvent* aEvent)
|
2000-07-11 23:37:00 +00:00
|
|
|
{
|
|
|
|
// Execute the oncommand event handler.
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2005-04-28 23:48:28 +00:00
|
|
|
|
2006-05-22 20:37:32 +00:00
|
|
|
nsXULCommandEvent event(aEvent ? NS_IS_TRUSTED_EVENT(aEvent) : PR_FALSE,
|
|
|
|
NS_XUL_COMMAND, nsnull);
|
2005-04-28 23:48:28 +00:00
|
|
|
|
2006-03-07 17:08:51 +00:00
|
|
|
nsEventDispatcher::Dispatch(mContent, aPresContext, &event, nsnull, &status);
|
2000-07-11 23:37:00 +00:00
|
|
|
}
|