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/. */
|
2000-03-11 13:39:21 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2000-03-31 11:02:06 +04:00
|
|
|
#include "nsButtonBoxFrame.h"
|
2000-03-11 13:39:21 +03:00
|
|
|
#include "nsIContent.h"
|
2003-07-14 13:45:54 +04:00
|
|
|
#include "nsIDOMXULButtonElement.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2004-08-01 03:15:21 +04:00
|
|
|
#include "nsPresContext.h"
|
2006-01-26 05:29:17 +03:00
|
|
|
#include "nsDisplayList.h"
|
2009-06-30 11:56:40 +04:00
|
|
|
#include "nsContentUtils.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2018-04-20 07:49:29 +03:00
|
|
|
#include "mozilla/dom/Event.h"
|
2018-03-20 07:16:06 +03:00
|
|
|
#include "mozilla/dom/MouseEventBinding.h"
|
2014-04-01 08:09:23 +04:00
|
|
|
#include "mozilla/EventStateManager.h"
|
2014-04-03 08:18:36 +04:00
|
|
|
#include "mozilla/EventStates.h"
|
2013-10-28 13:03:19 +04:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2019-04-03 15:40:26 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2013-09-25 15:21:19 +04:00
|
|
|
#include "mozilla/TextEvents.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
|
2013-10-01 11:22:58 +04:00
|
|
|
using namespace mozilla;
|
2018-11-13 01:20:52 +03:00
|
|
|
using namespace mozilla::dom;
|
2015-03-30 02:05:00 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsButtonBoxFrame::nsButtonBoxListener, nsIDOMEventListener)
|
|
|
|
|
2018-04-20 07:49:29 +03:00
|
|
|
nsresult nsButtonBoxFrame::nsButtonBoxListener::HandleEvent(
|
|
|
|
dom::Event* aEvent) {
|
2015-03-30 02:05:00 +03:00
|
|
|
if (!mButtonBoxFrame) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString eventType;
|
|
|
|
aEvent->GetType(eventType);
|
|
|
|
|
|
|
|
if (eventType.EqualsLiteral("blur")) {
|
|
|
|
mButtonBoxFrame->Blurred();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:52:56 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unexpected eventType");
|
2015-03-30 02:05:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-11 13:39:21 +03:00
|
|
|
//
|
|
|
|
// NS_NewXULButtonFrame
|
|
|
|
//
|
2005-10-27 01:46:39 +04:00
|
|
|
// Creates a new Button frame and returns it
|
2000-03-11 13:39:21 +03:00
|
|
|
//
|
2019-04-16 10:24:49 +03:00
|
|
|
nsIFrame* NS_NewButtonBoxFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
|
2019-02-05 19:45:54 +03:00
|
|
|
return new (aPresShell)
|
|
|
|
nsButtonBoxFrame(aStyle, aPresShell->GetPresContext());
|
2009-09-12 20:49:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsButtonBoxFrame)
|
2000-03-11 13:39:21 +03:00
|
|
|
|
2019-02-05 19:45:54 +03:00
|
|
|
nsButtonBoxFrame::nsButtonBoxFrame(ComputedStyle* aStyle,
|
|
|
|
nsPresContext* aPresContext, ClassID aID)
|
|
|
|
: nsBoxFrame(aStyle, aPresContext, aID, false),
|
2015-03-30 02:05:00 +03:00
|
|
|
mButtonBoxListener(nullptr),
|
2019-11-22 16:15:28 +03:00
|
|
|
mIsHandlingKeyEvent(false) {}
|
2015-03-30 02:05:00 +03:00
|
|
|
|
|
|
|
void nsButtonBoxFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow) {
|
|
|
|
nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
|
|
|
|
|
|
|
|
mButtonBoxListener = new nsButtonBoxListener(this);
|
|
|
|
|
|
|
|
mContent->AddSystemEventListener(u"blur"_ns, mButtonBoxListener, false);
|
|
|
|
}
|
|
|
|
|
2017-11-07 03:20:33 +03:00
|
|
|
void nsButtonBoxFrame::DestroyFrom(nsIFrame* aDestructRoot,
|
|
|
|
PostDestroyData& aPostDestroyData) {
|
2015-03-30 02:05:00 +03:00
|
|
|
mContent->RemoveSystemEventListener(u"blur"_ns, mButtonBoxListener, false);
|
|
|
|
|
|
|
|
mButtonBoxListener->mButtonBoxFrame = nullptr;
|
|
|
|
mButtonBoxListener = nullptr;
|
|
|
|
|
2017-11-07 03:20:33 +03:00
|
|
|
nsBoxFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
|
2015-03-30 02:05:00 +03:00
|
|
|
}
|
|
|
|
|
2006-01-26 05:29:17 +03:00
|
|
|
void nsButtonBoxFrame::BuildDisplayListForChildren(
|
|
|
|
nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists) {
|
2000-03-22 05:43:08 +03:00
|
|
|
// override, since we don't want children to get events
|
2006-01-26 05:29:17 +03:00
|
|
|
if (aBuilder->IsForEventDelivery()) return;
|
2017-08-07 05:23:35 +03:00
|
|
|
nsBoxFrame::BuildDisplayListForChildren(aBuilder, aLists);
|
2000-03-11 13:39:21 +03:00
|
|
|
}
|
|
|
|
|
2004-08-01 03:15:21 +04:00
|
|
|
nsresult nsButtonBoxFrame::HandleEvent(nsPresContext* aPresContext,
|
2013-10-02 07:46:03 +04:00
|
|
|
WidgetGUIEvent* aEvent,
|
2001-01-09 05:15:55 +03:00
|
|
|
nsEventStatus* aEventStatus) {
|
2009-02-27 13:48:25 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aEventStatus);
|
|
|
|
if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-22 04:34:51 +03:00
|
|
|
switch (aEvent->mMessage) {
|
2015-08-29 02:58:27 +03:00
|
|
|
case eKeyDown: {
|
2013-10-18 10:10:24 +04:00
|
|
|
WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent();
|
|
|
|
if (!keyEvent) {
|
|
|
|
break;
|
|
|
|
}
|
2016-05-12 11:13:49 +03:00
|
|
|
if (NS_VK_SPACE == keyEvent->mKeyCode) {
|
2014-04-01 08:09:23 +04:00
|
|
|
EventStateManager* esm = aPresContext->EventStateManager();
|
2013-10-18 10:10:24 +04:00
|
|
|
// :hover:active state
|
|
|
|
esm->SetContentState(mContent, NS_EVENT_STATE_HOVER);
|
|
|
|
esm->SetContentState(mContent, NS_EVENT_STATE_ACTIVE);
|
2015-03-30 02:05:00 +03:00
|
|
|
mIsHandlingKeyEvent = true;
|
2001-01-09 05:15:55 +03:00
|
|
|
}
|
|
|
|
break;
|
2013-10-18 10:10:24 +04:00
|
|
|
}
|
2001-01-09 05:15:55 +03:00
|
|
|
|
2013-10-18 10:10:24 +04:00
|
|
|
// On mac, Return fires the default button, not the focused one.
|
2005-06-29 18:20:57 +04:00
|
|
|
#ifndef XP_MACOSX
|
2015-08-29 02:58:27 +03:00
|
|
|
case eKeyPress: {
|
2013-10-18 10:10:24 +04:00
|
|
|
WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent();
|
|
|
|
if (!keyEvent) {
|
|
|
|
break;
|
|
|
|
}
|
2016-05-12 11:13:49 +03:00
|
|
|
if (NS_VK_RETURN == keyEvent->mKeyCode) {
|
2018-12-04 19:33:05 +03:00
|
|
|
RefPtr<nsIDOMXULButtonElement> button =
|
|
|
|
mContent->AsElement()->AsXULButton();
|
|
|
|
if (button) {
|
2015-12-08 02:30:05 +03:00
|
|
|
MouseClicked(aEvent);
|
2013-10-18 10:10:24 +04:00
|
|
|
*aEventStatus = nsEventStatus_eConsumeNoDefault;
|
2001-01-09 05:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-10-18 10:10:24 +04:00
|
|
|
}
|
2005-06-29 18:20:57 +04:00
|
|
|
#endif
|
2001-01-09 05:15:55 +03:00
|
|
|
|
2015-08-29 02:58:27 +03:00
|
|
|
case eKeyUp: {
|
2013-10-18 10:10:24 +04:00
|
|
|
WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent();
|
|
|
|
if (!keyEvent) {
|
|
|
|
break;
|
|
|
|
}
|
2016-05-12 11:13:49 +03:00
|
|
|
if (NS_VK_SPACE == keyEvent->mKeyCode) {
|
2015-03-30 02:05:00 +03:00
|
|
|
mIsHandlingKeyEvent = false;
|
2013-10-18 10:10:24 +04:00
|
|
|
// only activate on keyup if we're already in the :hover:active state
|
|
|
|
NS_ASSERTION(mContent->IsElement(), "How do we have a non-element?");
|
2014-04-03 08:18:36 +04:00
|
|
|
EventStates buttonState = mContent->AsElement()->State();
|
2013-10-18 10:10:24 +04:00
|
|
|
if (buttonState.HasAllStates(NS_EVENT_STATE_ACTIVE |
|
|
|
|
NS_EVENT_STATE_HOVER)) {
|
|
|
|
// return to normal state
|
2014-04-01 08:09:23 +04:00
|
|
|
EventStateManager* esm = aPresContext->EventStateManager();
|
2013-10-18 10:10:24 +04:00
|
|
|
esm->SetContentState(nullptr, NS_EVENT_STATE_ACTIVE);
|
|
|
|
esm->SetContentState(nullptr, NS_EVENT_STATE_HOVER);
|
2015-12-08 02:30:05 +03:00
|
|
|
MouseClicked(aEvent);
|
2000-03-14 14:09:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-10-18 10:10:24 +04:00
|
|
|
}
|
2000-03-14 14:09:46 +03:00
|
|
|
|
2015-08-29 02:58:32 +03:00
|
|
|
case eMouseClick: {
|
2013-10-28 13:03:19 +04:00
|
|
|
WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
|
2020-04-07 02:59:53 +03:00
|
|
|
|
|
|
|
if (mouseEvent->IsLeftClickEvent()
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// On Mac, ctrl-click will send a context menu event from the widget,
|
|
|
|
// so we don't want to dispatch widget command if it is redispatched
|
|
|
|
// from the mouse event with ctrl key is pressed.
|
|
|
|
&& !mouseEvent->IsControl()
|
|
|
|
#endif
|
|
|
|
) {
|
2015-12-08 02:30:05 +03:00
|
|
|
MouseClicked(mouseEvent);
|
2006-11-17 00:35:39 +03:00
|
|
|
}
|
2000-03-14 14:09:46 +03:00
|
|
|
break;
|
2013-10-28 13:03:19 +04:00
|
|
|
}
|
2015-08-26 15:56:59 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2000-03-14 14:09:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
|
|
|
|
}
|
|
|
|
|
2015-03-30 02:05:00 +03:00
|
|
|
void nsButtonBoxFrame::Blurred() {
|
|
|
|
NS_ASSERTION(mContent->IsElement(), "How do we have a non-element?");
|
|
|
|
EventStates buttonState = mContent->AsElement()->State();
|
|
|
|
if (mIsHandlingKeyEvent &&
|
|
|
|
buttonState.HasAllStates(NS_EVENT_STATE_ACTIVE | NS_EVENT_STATE_HOVER)) {
|
|
|
|
// return to normal state
|
|
|
|
EventStateManager* esm = PresContext()->EventStateManager();
|
|
|
|
esm->SetContentState(nullptr, NS_EVENT_STATE_ACTIVE);
|
|
|
|
esm->SetContentState(nullptr, NS_EVENT_STATE_HOVER);
|
|
|
|
}
|
|
|
|
mIsHandlingKeyEvent = false;
|
|
|
|
}
|
|
|
|
|
2018-06-01 13:15:56 +03:00
|
|
|
void nsButtonBoxFrame::MouseClicked(WidgetGUIEvent* aEvent) {
|
2000-07-30 11:19:58 +04:00
|
|
|
// Don't execute if we're disabled.
|
2017-12-07 21:13:50 +03:00
|
|
|
if (mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_true, eCaseMatters))
|
2000-07-30 11:19:58 +04:00
|
|
|
return;
|
|
|
|
|
2000-06-29 06:02:43 +04:00
|
|
|
// Have the content handle the event, propagating it according to normal DOM
|
|
|
|
// rules.
|
2019-04-03 15:40:26 +03:00
|
|
|
RefPtr<mozilla::PresShell> presShell = PresContext()->GetPresShell();
|
|
|
|
if (!presShell) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-01 13:15:56 +03:00
|
|
|
|
|
|
|
// Execute the oncommand event handler.
|
2019-04-30 04:35:30 +03:00
|
|
|
nsCOMPtr<nsIContent> content = mContent;
|
2018-06-01 13:15:56 +03:00
|
|
|
WidgetInputEvent* inputEvent = aEvent->AsInputEvent();
|
|
|
|
WidgetMouseEventBase* mouseEvent = aEvent->AsMouseEventBase();
|
|
|
|
nsContentUtils::DispatchXULCommand(
|
2019-04-30 04:35:30 +03:00
|
|
|
content, aEvent->IsTrusted(), nullptr, presShell, inputEvent->IsControl(),
|
|
|
|
inputEvent->IsAlt(), inputEvent->IsShift(), inputEvent->IsMeta(),
|
2019-04-21 21:19:43 +03:00
|
|
|
mouseEvent ? mouseEvent->mInputSource
|
2018-06-26 00:20:54 +03:00
|
|
|
: MouseEvent_Binding::MOZ_SOURCE_UNKNOWN);
|
2000-03-14 14:09:46 +03:00
|
|
|
}
|