2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 12:12:37 +01: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 10:39:21 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2000-03-31 07:02:06 +00:00
|
|
|
#include "nsButtonBoxFrame.h"
|
2000-03-11 10:39:21 +00:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
2003-07-14 09:45:54 +00:00
|
|
|
#include "nsIDOMXULButtonElement.h"
|
2006-12-26 17:47:52 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2014-02-27 20:04:46 -03:00
|
|
|
#include "nsNameSpaceManager.h"
|
2004-07-31 23:15:21 +00:00
|
|
|
#include "nsPresContext.h"
|
2000-06-29 02:02:43 +00:00
|
|
|
#include "nsIPresShell.h"
|
2001-03-21 09:01:34 +00:00
|
|
|
#include "nsIDOMElement.h"
|
2006-01-26 02:29:17 +00:00
|
|
|
#include "nsDisplayList.h"
|
2009-06-30 10:56:40 +03:00
|
|
|
#include "nsContentUtils.h"
|
2011-07-20 12:18:54 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-04-01 13:09:23 +09:00
|
|
|
#include "mozilla/EventStateManager.h"
|
2014-04-03 13:18:36 +09:00
|
|
|
#include "mozilla/EventStates.h"
|
2013-10-28 18:03:19 +09:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2013-09-25 20:21:19 +09:00
|
|
|
#include "mozilla/TextEvents.h"
|
2011-07-20 12:18:54 -07:00
|
|
|
|
2013-10-01 16:22:58 +09:00
|
|
|
using namespace mozilla;
|
2000-03-11 10:39:21 +00:00
|
|
|
|
2015-03-29 18:05:00 -05: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 10:39:21 +00:00
|
|
|
//
|
|
|
|
// NS_NewXULButtonFrame
|
|
|
|
//
|
2005-10-26 21:46:39 +00:00
|
|
|
// Creates a new Button frame and returns it
|
2000-03-11 10:39:21 +00:00
|
|
|
//
|
2005-10-26 21:46:39 +00:00
|
|
|
nsIFrame*
|
2006-03-26 21:30:36 +00:00
|
|
|
NS_NewButtonBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
|
2000-03-11 10:39:21 +00:00
|
|
|
{
|
2015-01-06 09:27:56 +00:00
|
|
|
return new (aPresShell) nsButtonBoxFrame(aContext);
|
2009-09-12 17:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsButtonBoxFrame)
|
2000-03-11 10:39:21 +00:00
|
|
|
|
2015-03-29 18:05:00 -05: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-15 00:12:27 +13:00
|
|
|
void
|
2006-01-26 02:29:17 +00:00
|
|
|
nsButtonBoxFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
2000-03-11 10:39:21 +00:00
|
|
|
{
|
2000-03-22 02:43:08 +00:00
|
|
|
// override, since we don't want children to get events
|
2006-01-26 02:29:17 +00:00
|
|
|
if (aBuilder->IsForEventDelivery())
|
2013-02-15 00:12:27 +13:00
|
|
|
return;
|
|
|
|
nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, aLists);
|
2000-03-11 10:39:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 23:47:48 -08:00
|
|
|
nsresult
|
2004-07-31 23:15:21 +00:00
|
|
|
nsButtonBoxFrame::HandleEvent(nsPresContext* aPresContext,
|
2013-10-02 12:46:03 +09:00
|
|
|
WidgetGUIEvent* aEvent,
|
2001-01-09 02:15:55 +00:00
|
|
|
nsEventStatus* aEventStatus)
|
2000-03-14 11:09:46 +00:00
|
|
|
{
|
2009-02-27 10:48:25 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aEventStatus);
|
|
|
|
if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-14 11:09:46 +00:00
|
|
|
switch (aEvent->message) {
|
2013-10-18 15:10:24 +09:00
|
|
|
case NS_KEY_DOWN: {
|
|
|
|
WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent();
|
|
|
|
if (!keyEvent) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (NS_VK_SPACE == keyEvent->keyCode) {
|
2014-04-01 13:09:23 +09:00
|
|
|
EventStateManager* esm = aPresContext->EventStateManager();
|
2013-10-18 15:10:24 +09:00
|
|
|
// :hover:active state
|
|
|
|
esm->SetContentState(mContent, NS_EVENT_STATE_HOVER);
|
|
|
|
esm->SetContentState(mContent, NS_EVENT_STATE_ACTIVE);
|
2015-03-29 18:05:00 -05:00
|
|
|
mIsHandlingKeyEvent = true;
|
2001-01-09 02:15:55 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-10-18 15:10:24 +09:00
|
|
|
}
|
2001-01-09 02:15:55 +00:00
|
|
|
|
2013-10-18 15:10:24 +09:00
|
|
|
// On mac, Return fires the default button, not the focused one.
|
2005-06-29 14:20:57 +00:00
|
|
|
#ifndef XP_MACOSX
|
2013-10-18 15:10:24 +09:00
|
|
|
case NS_KEY_PRESS: {
|
|
|
|
WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent();
|
|
|
|
if (!keyEvent) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (NS_VK_RETURN == keyEvent->keyCode) {
|
|
|
|
nsCOMPtr<nsIDOMXULButtonElement> buttonEl(do_QueryInterface(mContent));
|
|
|
|
if (buttonEl) {
|
|
|
|
MouseClicked(aPresContext, aEvent);
|
|
|
|
*aEventStatus = nsEventStatus_eConsumeNoDefault;
|
2001-01-09 02:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-10-18 15:10:24 +09:00
|
|
|
}
|
2005-06-29 14:20:57 +00:00
|
|
|
#endif
|
2001-01-09 02:15:55 +00:00
|
|
|
|
2013-10-18 15:10:24 +09:00
|
|
|
case NS_KEY_UP: {
|
|
|
|
WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent();
|
|
|
|
if (!keyEvent) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (NS_VK_SPACE == keyEvent->keyCode) {
|
2015-03-29 18:05:00 -05:00
|
|
|
mIsHandlingKeyEvent = false;
|
2013-10-18 15:10:24 +09: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 13:18:36 +09:00
|
|
|
EventStates buttonState = mContent->AsElement()->State();
|
2013-10-18 15:10:24 +09:00
|
|
|
if (buttonState.HasAllStates(NS_EVENT_STATE_ACTIVE |
|
|
|
|
NS_EVENT_STATE_HOVER)) {
|
|
|
|
// return to normal state
|
2014-04-01 13:09:23 +09:00
|
|
|
EventStateManager* esm = aPresContext->EventStateManager();
|
2013-10-18 15:10:24 +09:00
|
|
|
esm->SetContentState(nullptr, NS_EVENT_STATE_ACTIVE);
|
|
|
|
esm->SetContentState(nullptr, NS_EVENT_STATE_HOVER);
|
|
|
|
MouseClicked(aPresContext, aEvent);
|
2000-03-14 11:09:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-10-18 15:10:24 +09:00
|
|
|
}
|
2000-03-14 11:09:46 +00:00
|
|
|
|
2013-10-28 18:03:19 +09:00
|
|
|
case NS_MOUSE_CLICK: {
|
|
|
|
WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
|
|
|
|
if (mouseEvent->IsLeftClickEvent()) {
|
|
|
|
MouseClicked(aPresContext, mouseEvent);
|
2006-11-16 21:35:39 +00:00
|
|
|
}
|
2000-03-14 11:09:46 +00:00
|
|
|
break;
|
2013-10-28 18:03:19 +09:00
|
|
|
}
|
2000-03-14 11:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
|
|
|
|
}
|
|
|
|
|
2015-03-29 18:05:00 -05: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 12:46:03 +09:00
|
|
|
nsButtonBoxFrame::DoMouseClick(WidgetGUIEvent* aEvent, bool aTrustEvent)
|
2000-03-14 11:09:46 +00:00
|
|
|
{
|
2000-07-30 07:19:58 +00:00
|
|
|
// Don't execute if we're disabled.
|
2006-12-26 17:47:52 +00:00
|
|
|
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_true, eCaseMatters))
|
2000-07-30 07:19:58 +00:00
|
|
|
return;
|
|
|
|
|
2000-03-14 11:09:46 +00:00
|
|
|
// Execute the oncommand event handler.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isShift = false;
|
|
|
|
bool isControl = false;
|
|
|
|
bool isAlt = false;
|
|
|
|
bool isMeta = false;
|
2000-08-24 21:28:22 +00:00
|
|
|
if(aEvent) {
|
2013-10-18 15:10:26 +09:00
|
|
|
WidgetInputEvent* inputEvent = aEvent->AsInputEvent();
|
|
|
|
isShift = inputEvent->IsShift();
|
|
|
|
isControl = inputEvent->IsControl();
|
|
|
|
isAlt = inputEvent->IsAlt();
|
|
|
|
isMeta = inputEvent->IsMeta();
|
2000-08-24 21:28:22 +00:00
|
|
|
}
|
2000-06-29 02:02:43 +00:00
|
|
|
|
|
|
|
// Have the content handle the event, propagating it according to normal DOM rules.
|
2007-03-30 14:11:41 -07:00
|
|
|
nsCOMPtr<nsIPresShell> shell = PresContext()->GetPresShell();
|
2003-12-21 05:36:36 +00:00
|
|
|
if (shell) {
|
2009-06-30 10:56:40 +03:00
|
|
|
nsContentUtils::DispatchXULCommand(mContent,
|
|
|
|
aEvent ?
|
2012-12-16 10:26:03 +09:00
|
|
|
aEvent->mFlags.mIsTrusted : aTrustEvent,
|
2012-07-30 17:20:58 +03:00
|
|
|
nullptr, shell,
|
2009-06-30 10:56:40 +03:00
|
|
|
isControl, isAlt, isShift, isMeta);
|
2000-06-29 02:02:43 +00:00
|
|
|
}
|
2000-03-14 11:09:46 +00:00
|
|
|
}
|