2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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"
|
|
|
|
#include "nsIDOMNodeList.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"
|
2000-06-29 06:02:43 +04:00
|
|
|
#include "nsIPresShell.h"
|
2001-03-21 12:01:34 +03:00
|
|
|
#include "nsIDOMElement.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"
|
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"
|
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;
|
2000-03-11 13:39:21 +03:00
|
|
|
|
2015-03-30 02:05:00 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsButtonBoxFrame::nsButtonBoxListener, nsIDOMEventListener)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsButtonBoxFrame::nsButtonBoxListener::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
if (!mButtonBoxFrame) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString eventType;
|
|
|
|
aEvent->GetType(eventType);
|
|
|
|
|
|
|
|
if (eventType.EqualsLiteral("blur")) {
|
|
|
|
mButtonBoxFrame->Blurred();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ABORT();
|
|
|
|
|
|
|
|
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
|
|
|
//
|
2005-10-27 01:46:39 +04:00
|
|
|
nsIFrame*
|
2006-03-27 01:30:36 +04:00
|
|
|
NS_NewButtonBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
|
2000-03-11 13:39:21 +03:00
|
|
|
{
|
2015-01-06 12:27:56 +03:00
|
|
|
return new (aPresShell) nsButtonBoxFrame(aContext);
|
2009-09-12 20:49:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsButtonBoxFrame)
|
2000-03-11 13:39:21 +03:00
|
|
|
|
2015-03-30 02:05:00 +03:00
|
|
|
nsButtonBoxFrame::nsButtonBoxFrame(nsStyleContext* aContext) :
|
|
|
|
nsBoxFrame(aContext, false),
|
|
|
|
mButtonBoxListener(nullptr),
|
|
|
|
mIsHandlingKeyEvent(false)
|
|
|
|
{
|
|
|
|
UpdateMouseThrough();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsButtonBoxFrame::Init(nsIContent* aContent,
|
|
|
|
nsContainerFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow)
|
|
|
|
{
|
|
|
|
nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
|
|
|
|
|
|
|
|
mButtonBoxListener = new nsButtonBoxListener(this);
|
|
|
|
|
|
|
|
mContent->AddSystemEventListener(NS_LITERAL_STRING("blur"), mButtonBoxListener, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsButtonBoxFrame::DestroyFrom(nsIFrame* aDestructRoot)
|
|
|
|
{
|
|
|
|
mContent->RemoveSystemEventListener(NS_LITERAL_STRING("blur"), mButtonBoxListener, false);
|
|
|
|
|
|
|
|
mButtonBoxListener->mButtonBoxFrame = nullptr;
|
|
|
|
mButtonBoxListener = nullptr;
|
|
|
|
|
|
|
|
nsBoxFrame::DestroyFrom(aDestructRoot);
|
|
|
|
}
|
|
|
|
|
2013-02-14 15:12:27 +04:00
|
|
|
void
|
2006-01-26 05:29:17 +03:00
|
|
|
nsButtonBoxFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
2000-03-11 13:39:21 +03:00
|
|
|
{
|
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())
|
2013-02-14 15:12:27 +04:00
|
|
|
return;
|
|
|
|
nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, aLists);
|
2000-03-11 13:39:21 +03:00
|
|
|
}
|
|
|
|
|
2014-02-18 11:47:48 +04:00
|
|
|
nsresult
|
2004-08-01 03:15:21 +04:00
|
|
|
nsButtonBoxFrame::HandleEvent(nsPresContext* aPresContext,
|
2013-10-02 07:46:03 +04:00
|
|
|
WidgetGUIEvent* aEvent,
|
2001-01-09 05:15:55 +03:00
|
|
|
nsEventStatus* aEventStatus)
|
2000-03-14 14:09:46 +03:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
if (NS_VK_SPACE == keyEvent->keyCode) {
|
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;
|
|
|
|
}
|
|
|
|
if (NS_VK_RETURN == keyEvent->keyCode) {
|
|
|
|
nsCOMPtr<nsIDOMXULButtonElement> buttonEl(do_QueryInterface(mContent));
|
|
|
|
if (buttonEl) {
|
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;
|
|
|
|
}
|
|
|
|
if (NS_VK_SPACE == keyEvent->keyCode) {
|
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();
|
|
|
|
if (mouseEvent->IsLeftClickEvent()) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-02 07:46:03 +04:00
|
|
|
nsButtonBoxFrame::DoMouseClick(WidgetGUIEvent* aEvent, bool aTrustEvent)
|
2000-03-14 14:09:46 +03:00
|
|
|
{
|
2000-07-30 11:19:58 +04:00
|
|
|
// Don't execute if we're disabled.
|
2006-12-26 20:47:52 +03:00
|
|
|
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_true, eCaseMatters))
|
2000-07-30 11:19:58 +04:00
|
|
|
return;
|
|
|
|
|
2000-03-14 14:09:46 +03:00
|
|
|
// Execute the oncommand event handler.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isShift = false;
|
|
|
|
bool isControl = false;
|
|
|
|
bool isAlt = false;
|
|
|
|
bool isMeta = false;
|
2000-08-25 01:28:22 +04:00
|
|
|
if(aEvent) {
|
2013-10-18 10:10:26 +04:00
|
|
|
WidgetInputEvent* inputEvent = aEvent->AsInputEvent();
|
|
|
|
isShift = inputEvent->IsShift();
|
|
|
|
isControl = inputEvent->IsControl();
|
|
|
|
isAlt = inputEvent->IsAlt();
|
|
|
|
isMeta = inputEvent->IsMeta();
|
2000-08-25 01:28:22 +04:00
|
|
|
}
|
2000-06-29 06:02:43 +04:00
|
|
|
|
|
|
|
// Have the content handle the event, propagating it according to normal DOM rules.
|
2007-03-31 01:11:41 +04:00
|
|
|
nsCOMPtr<nsIPresShell> shell = PresContext()->GetPresShell();
|
2003-12-21 08:36:36 +03:00
|
|
|
if (shell) {
|
2009-06-30 11:56:40 +04:00
|
|
|
nsContentUtils::DispatchXULCommand(mContent,
|
|
|
|
aEvent ?
|
2012-12-16 05:26:03 +04:00
|
|
|
aEvent->mFlags.mIsTrusted : aTrustEvent,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, shell,
|
2009-06-30 11:56:40 +04:00
|
|
|
isControl, isAlt, isShift, isMeta);
|
2000-06-29 06:02:43 +04:00
|
|
|
}
|
2000-03-14 14:09:46 +03:00
|
|
|
}
|