2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2014-10-15 00:15:21 +04:00
|
|
|
|
2001-08-18 05:04:47 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIContent.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2001-09-10 11:34:54 +04:00
|
|
|
#include "nsMenuPopupFrame.h"
|
2013-06-11 01:00:00 +04:00
|
|
|
#include "nsView.h"
|
2013-09-23 15:55:35 +04:00
|
|
|
#include "mozilla/AppUnits.h"
|
2013-09-20 14:21:03 +04:00
|
|
|
#include "mozilla/dom/DOMRect.h"
|
2014-10-15 00:15:21 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/Event.h"
|
2018-04-27 18:04:38 +03:00
|
|
|
#include "mozilla/dom/XULPopupElement.h"
|
|
|
|
#include "mozilla/dom/XULPopupElementBinding.h"
|
2014-10-15 00:15:21 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-09-20 14:21:03 +04:00
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
nsXULElement* NS_NewXULPopupElement(
|
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) {
|
2020-03-17 17:53:33 +03:00
|
|
|
RefPtr<mozilla::dom::NodeInfo> nodeInfo(aNodeInfo);
|
|
|
|
auto* nim = nodeInfo->NodeInfoManager();
|
|
|
|
return new (nim) XULPopupElement(nodeInfo.forget());
|
2014-10-15 00:15:21 +04:00
|
|
|
}
|
2001-08-18 05:04:47 +04:00
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
JSObject* XULPopupElement::WrapNode(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return XULPopupElement_Binding::Wrap(aCx, this, aGivenProto);
|
2014-10-15 00:15:21 +04:00
|
|
|
}
|
2001-09-10 11:34:54 +04:00
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
void XULPopupElement::OpenPopup(Element* aAnchorElement,
|
|
|
|
const StringOrOpenPopupOptions& aOptions,
|
|
|
|
int32_t aXPos, int32_t aYPos,
|
|
|
|
bool aIsContextMenu, bool aAttributesOverride,
|
|
|
|
Event* aTriggerEvent) {
|
2018-04-27 18:04:37 +03:00
|
|
|
nsAutoString position;
|
|
|
|
if (aOptions.IsOpenPopupOptions()) {
|
|
|
|
const OpenPopupOptions& options = aOptions.GetAsOpenPopupOptions();
|
|
|
|
position = options.mPosition;
|
|
|
|
aXPos = options.mX;
|
|
|
|
aYPos = options.mY;
|
|
|
|
aIsContextMenu = options.mIsContextMenu;
|
|
|
|
aAttributesOverride = options.mAttributesOverride;
|
|
|
|
aTriggerEvent = options.mTriggerEvent;
|
|
|
|
} else {
|
|
|
|
position = aOptions.GetAsString();
|
|
|
|
}
|
|
|
|
|
2007-07-04 19:49:38 +04:00
|
|
|
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
|
2018-04-27 18:04:38 +03:00
|
|
|
if (pm) {
|
2018-04-27 18:04:37 +03:00
|
|
|
// As a special case for popups that are menus when no anchor or position
|
|
|
|
// are specified, open the popup with ShowMenu instead of ShowPopup so that
|
|
|
|
// the popup is aligned with the menu.
|
2018-04-27 18:04:38 +03:00
|
|
|
if (!aAnchorElement && position.IsEmpty() && GetPrimaryFrame()) {
|
|
|
|
nsMenuFrame* menu = do_QueryFrame(GetPrimaryFrame()->GetParent());
|
2018-04-27 18:04:37 +03:00
|
|
|
if (menu) {
|
|
|
|
pm->ShowMenu(menu->GetContent(), false, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
pm->ShowPopup(this, aAnchorElement, position, aXPos, aYPos, aIsContextMenu,
|
2011-10-17 18:59:28 +04:00
|
|
|
aAttributesOverride, false, aTriggerEvent);
|
2001-09-10 11:34:54 +04:00
|
|
|
}
|
2007-07-04 19:49:38 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
void XULPopupElement::OpenPopupAtScreen(int32_t aXPos, int32_t aYPos,
|
|
|
|
bool aIsContextMenu,
|
|
|
|
Event* aTriggerEvent) {
|
2007-07-04 19:49:38 +04:00
|
|
|
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
|
2018-04-27 18:04:38 +03:00
|
|
|
if (pm) {
|
|
|
|
pm->ShowPopupAtScreen(this, aXPos, aYPos, aIsContextMenu, aTriggerEvent);
|
|
|
|
}
|
2001-09-10 11:34:54 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
void XULPopupElement::OpenPopupAtScreenRect(const nsAString& aPosition,
|
|
|
|
int32_t aXPos, int32_t aYPos,
|
|
|
|
int32_t aWidth, int32_t aHeight,
|
|
|
|
bool aIsContextMenu,
|
|
|
|
bool aAttributesOverride,
|
|
|
|
Event* aTriggerEvent) {
|
2015-05-08 21:49:54 +03:00
|
|
|
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
|
2018-04-27 18:04:38 +03:00
|
|
|
if (pm) {
|
|
|
|
pm->ShowPopupAtScreenRect(
|
2015-05-08 21:49:54 +03:00
|
|
|
this, aPosition, nsIntRect(aXPos, aYPos, aWidth, aHeight),
|
|
|
|
aIsContextMenu, aAttributesOverride, aTriggerEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
void XULPopupElement::HidePopup(bool aCancel) {
|
|
|
|
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
|
|
|
|
if (pm) {
|
|
|
|
pm->HidePopup(this, false, true, false, aCancel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XULPopupElement::MoveTo(int32_t aLeft, int32_t aTop) {
|
|
|
|
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
|
2006-01-01 02:34:46 +03:00
|
|
|
if (menuPopupFrame) {
|
2015-09-29 03:17:34 +03:00
|
|
|
menuPopupFrame->MoveTo(CSSIntPoint(aLeft, aTop), true);
|
2006-01-01 02:34:46 +03:00
|
|
|
}
|
2001-09-10 11:34:54 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
void XULPopupElement::MoveToAnchor(Element* aAnchorElement,
|
|
|
|
const nsAString& aPosition, int32_t aXPos,
|
|
|
|
int32_t aYPos, bool aAttributesOverride) {
|
|
|
|
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
|
|
|
|
if (menuPopupFrame && menuPopupFrame->IsVisible()) {
|
|
|
|
menuPopupFrame->MoveToAnchor(aAnchorElement, aPosition, aXPos, aYPos,
|
|
|
|
aAttributesOverride);
|
2012-10-23 16:11:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
void XULPopupElement::SizeTo(int32_t aWidth, int32_t aHeight) {
|
2001-09-10 11:34:54 +04:00
|
|
|
nsAutoString width, height;
|
|
|
|
width.AppendInt(aWidth);
|
|
|
|
height.AppendInt(aHeight);
|
2006-09-04 00:25:58 +04:00
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
nsCOMPtr<nsIContent> kungFuDeathGrip = this; // keep a reference
|
2012-11-07 04:58:01 +04:00
|
|
|
|
|
|
|
// We only want to pass aNotify=true to SetAttr once, but must make sure
|
|
|
|
// we pass it when a value is being changed. Thus, we check if the height
|
2012-11-17 06:04:39 +04:00
|
|
|
// is the same and if so, pass true when setting the width.
|
2018-04-27 18:04:38 +03:00
|
|
|
bool heightSame =
|
|
|
|
AttrValueIs(kNameSpaceID_None, nsGkAtoms::height, height, eCaseMatters);
|
2012-11-07 04:58:01 +04:00
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
SetAttr(kNameSpaceID_None, nsGkAtoms::width, width, heightSame);
|
|
|
|
SetAttr(kNameSpaceID_None, nsGkAtoms::height, height, true);
|
2018-04-27 18:04:37 +03:00
|
|
|
|
|
|
|
// If the popup is open, force a reposition of the popup after resizing it
|
|
|
|
// with notifications set to true so that the popuppositioned event is fired.
|
2018-04-27 18:04:38 +03:00
|
|
|
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
|
2018-04-27 18:04:37 +03:00
|
|
|
if (menuPopupFrame && menuPopupFrame->PopupState() == ePopupShown) {
|
2020-10-06 19:28:33 +03:00
|
|
|
menuPopupFrame->SetPopupPosition(nullptr, false, false);
|
2018-04-27 18:04:37 +03:00
|
|
|
}
|
2001-09-10 11:34:54 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
bool XULPopupElement::AutoPosition() {
|
|
|
|
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
|
2006-01-01 02:34:46 +03:00
|
|
|
if (menuPopupFrame) {
|
2014-10-15 00:15:21 +04:00
|
|
|
return menuPopupFrame->GetAutoPosition();
|
2006-01-01 02:34:46 +03:00
|
|
|
}
|
2014-10-15 00:15:21 +04:00
|
|
|
return true;
|
2001-09-10 11:34:54 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
void XULPopupElement::SetAutoPosition(bool aShouldAutoPosition) {
|
|
|
|
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
|
2006-01-01 02:34:46 +03:00
|
|
|
if (menuPopupFrame) {
|
|
|
|
menuPopupFrame->SetAutoPosition(aShouldAutoPosition);
|
|
|
|
}
|
2001-09-10 11:34:54 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
void XULPopupElement::GetState(nsString& aState) {
|
2009-08-26 20:19:38 +04:00
|
|
|
// set this here in case there's no frame for the popup
|
|
|
|
aState.AssignLiteral("closed");
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
|
2007-08-16 03:52:47 +04:00
|
|
|
if (menuPopupFrame) {
|
|
|
|
switch (menuPopupFrame->PopupState()) {
|
2014-07-14 21:39:04 +04:00
|
|
|
case ePopupShown:
|
|
|
|
aState.AssignLiteral("open");
|
|
|
|
break;
|
2007-08-16 03:52:47 +04:00
|
|
|
case ePopupShowing:
|
2016-08-17 01:33:05 +03:00
|
|
|
case ePopupPositioning:
|
2014-07-14 21:39:04 +04:00
|
|
|
case ePopupOpening:
|
|
|
|
case ePopupVisible:
|
2007-08-16 03:52:47 +04:00
|
|
|
aState.AssignLiteral("showing");
|
|
|
|
break;
|
|
|
|
case ePopupHiding:
|
|
|
|
case ePopupInvisible:
|
|
|
|
aState.AssignLiteral("hiding");
|
|
|
|
break;
|
2008-09-16 15:25:35 +04:00
|
|
|
case ePopupClosed:
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Bad popup state");
|
2008-09-16 15:25:35 +04:00
|
|
|
break;
|
2007-08-16 03:52:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
nsINode* XULPopupElement::GetTriggerNode() const {
|
|
|
|
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
|
2014-10-15 00:15:21 +04:00
|
|
|
return nsMenuPopupFrame::GetTriggerContent(menuPopupFrame);
|
2010-08-09 20:17:19 +04:00
|
|
|
}
|
2007-08-16 03:52:47 +04:00
|
|
|
|
2020-08-10 12:47:38 +03:00
|
|
|
bool XULPopupElement::IsAnchored() const {
|
|
|
|
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
|
|
|
|
if (!menuPopupFrame) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return menuPopupFrame->IsAnchored();
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:56:24 +03:00
|
|
|
// FIXME(emilio): should probably be renamed to GetAnchorElement?
|
2018-04-27 18:04:38 +03:00
|
|
|
Element* XULPopupElement::GetAnchorNode() const {
|
2018-05-30 17:56:24 +03:00
|
|
|
nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetPrimaryFrame());
|
2014-10-15 00:15:21 +04:00
|
|
|
if (!menuPopupFrame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-09-21 21:53:01 +04:00
|
|
|
|
2018-05-30 17:56:24 +03:00
|
|
|
return Element::FromNodeOrNull(menuPopupFrame->GetAnchor());
|
2010-09-21 21:53:01 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
already_AddRefed<DOMRect> XULPopupElement::GetOuterScreenRect() {
|
|
|
|
RefPtr<DOMRect> rect = new DOMRect(ToSupports(this));
|
2011-06-03 23:38:23 +04:00
|
|
|
|
|
|
|
// Return an empty rectangle if the popup is not open.
|
2018-10-18 15:40:10 +03:00
|
|
|
nsMenuPopupFrame* menuPopupFrame =
|
|
|
|
do_QueryFrame(GetPrimaryFrame(FlushType::Frames));
|
2014-10-15 00:15:21 +04:00
|
|
|
if (!menuPopupFrame || !menuPopupFrame->IsOpen()) {
|
|
|
|
return rect.forget();
|
|
|
|
}
|
2011-06-03 23:38:23 +04:00
|
|
|
|
2013-01-03 17:23:11 +04:00
|
|
|
nsView* view = menuPopupFrame->GetView();
|
2011-06-03 23:38:23 +04:00
|
|
|
if (view) {
|
|
|
|
nsIWidget* widget = view->GetWidget();
|
|
|
|
if (widget) {
|
2016-08-19 02:03:04 +03:00
|
|
|
LayoutDeviceIntRect screenRect = widget->GetScreenBounds();
|
2011-06-03 23:38:23 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t pp = menuPopupFrame->PresContext()->AppUnitsPerDevPixel();
|
2015-11-26 07:55:34 +03:00
|
|
|
rect->SetLayoutRect(LayoutDeviceIntRect::ToAppUnits(screenRect, pp));
|
2011-06-03 23:38:23 +04:00
|
|
|
}
|
|
|
|
}
|
2014-10-15 00:15:21 +04:00
|
|
|
return rect.forget();
|
2011-06-03 23:38:23 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 18:04:38 +03:00
|
|
|
void XULPopupElement::SetConstraintRect(dom::DOMRectReadOnly& aRect) {
|
2018-10-18 15:40:10 +03:00
|
|
|
nsMenuPopupFrame* menuPopupFrame =
|
|
|
|
do_QueryFrame(GetPrimaryFrame(FlushType::Frames));
|
2016-06-09 18:34:12 +03:00
|
|
|
if (menuPopupFrame) {
|
|
|
|
menuPopupFrame->SetOverrideConstraintRect(LayoutDeviceIntRect::Truncate(
|
2016-09-16 18:49:39 +03:00
|
|
|
aRect.Left(), aRect.Top(), aRect.Width(), aRect.Height()));
|
2016-06-09 18:34:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|