1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/xul/nsButtonBoxFrame.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,155 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 +#include "nsCOMPtr.h" 1.9 +#include "nsButtonBoxFrame.h" 1.10 +#include "nsIContent.h" 1.11 +#include "nsIDOMNodeList.h" 1.12 +#include "nsIDOMXULButtonElement.h" 1.13 +#include "nsGkAtoms.h" 1.14 +#include "nsNameSpaceManager.h" 1.15 +#include "nsPresContext.h" 1.16 +#include "nsIPresShell.h" 1.17 +#include "nsIDOMElement.h" 1.18 +#include "nsDisplayList.h" 1.19 +#include "nsContentUtils.h" 1.20 +#include "mozilla/dom/Element.h" 1.21 +#include "mozilla/EventStateManager.h" 1.22 +#include "mozilla/EventStates.h" 1.23 +#include "mozilla/MouseEvents.h" 1.24 +#include "mozilla/TextEvents.h" 1.25 + 1.26 +using namespace mozilla; 1.27 + 1.28 +// 1.29 +// NS_NewXULButtonFrame 1.30 +// 1.31 +// Creates a new Button frame and returns it 1.32 +// 1.33 +nsIFrame* 1.34 +NS_NewButtonBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext) 1.35 +{ 1.36 + return new (aPresShell) nsButtonBoxFrame(aPresShell, aContext); 1.37 +} 1.38 + 1.39 +NS_IMPL_FRAMEARENA_HELPERS(nsButtonBoxFrame) 1.40 + 1.41 +void 1.42 +nsButtonBoxFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder, 1.43 + const nsRect& aDirtyRect, 1.44 + const nsDisplayListSet& aLists) 1.45 +{ 1.46 + // override, since we don't want children to get events 1.47 + if (aBuilder->IsForEventDelivery()) 1.48 + return; 1.49 + nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, aLists); 1.50 +} 1.51 + 1.52 +nsresult 1.53 +nsButtonBoxFrame::HandleEvent(nsPresContext* aPresContext, 1.54 + WidgetGUIEvent* aEvent, 1.55 + nsEventStatus* aEventStatus) 1.56 +{ 1.57 + NS_ENSURE_ARG_POINTER(aEventStatus); 1.58 + if (nsEventStatus_eConsumeNoDefault == *aEventStatus) { 1.59 + return NS_OK; 1.60 + } 1.61 + 1.62 + switch (aEvent->message) { 1.63 + case NS_KEY_DOWN: { 1.64 + WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent(); 1.65 + if (!keyEvent) { 1.66 + break; 1.67 + } 1.68 + if (NS_VK_SPACE == keyEvent->keyCode) { 1.69 + EventStateManager* esm = aPresContext->EventStateManager(); 1.70 + // :hover:active state 1.71 + esm->SetContentState(mContent, NS_EVENT_STATE_HOVER); 1.72 + esm->SetContentState(mContent, NS_EVENT_STATE_ACTIVE); 1.73 + } 1.74 + break; 1.75 + } 1.76 + 1.77 +// On mac, Return fires the default button, not the focused one. 1.78 +#ifndef XP_MACOSX 1.79 + case NS_KEY_PRESS: { 1.80 + WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent(); 1.81 + if (!keyEvent) { 1.82 + break; 1.83 + } 1.84 + if (NS_VK_RETURN == keyEvent->keyCode) { 1.85 + nsCOMPtr<nsIDOMXULButtonElement> buttonEl(do_QueryInterface(mContent)); 1.86 + if (buttonEl) { 1.87 + MouseClicked(aPresContext, aEvent); 1.88 + *aEventStatus = nsEventStatus_eConsumeNoDefault; 1.89 + } 1.90 + } 1.91 + break; 1.92 + } 1.93 +#endif 1.94 + 1.95 + case NS_KEY_UP: { 1.96 + WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent(); 1.97 + if (!keyEvent) { 1.98 + break; 1.99 + } 1.100 + if (NS_VK_SPACE == keyEvent->keyCode) { 1.101 + // only activate on keyup if we're already in the :hover:active state 1.102 + NS_ASSERTION(mContent->IsElement(), "How do we have a non-element?"); 1.103 + EventStates buttonState = mContent->AsElement()->State(); 1.104 + if (buttonState.HasAllStates(NS_EVENT_STATE_ACTIVE | 1.105 + NS_EVENT_STATE_HOVER)) { 1.106 + // return to normal state 1.107 + EventStateManager* esm = aPresContext->EventStateManager(); 1.108 + esm->SetContentState(nullptr, NS_EVENT_STATE_ACTIVE); 1.109 + esm->SetContentState(nullptr, NS_EVENT_STATE_HOVER); 1.110 + MouseClicked(aPresContext, aEvent); 1.111 + } 1.112 + } 1.113 + break; 1.114 + } 1.115 + 1.116 + case NS_MOUSE_CLICK: { 1.117 + WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent(); 1.118 + if (mouseEvent->IsLeftClickEvent()) { 1.119 + MouseClicked(aPresContext, mouseEvent); 1.120 + } 1.121 + break; 1.122 + } 1.123 + } 1.124 + 1.125 + return nsBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus); 1.126 +} 1.127 + 1.128 +void 1.129 +nsButtonBoxFrame::DoMouseClick(WidgetGUIEvent* aEvent, bool aTrustEvent) 1.130 +{ 1.131 + // Don't execute if we're disabled. 1.132 + if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled, 1.133 + nsGkAtoms::_true, eCaseMatters)) 1.134 + return; 1.135 + 1.136 + // Execute the oncommand event handler. 1.137 + bool isShift = false; 1.138 + bool isControl = false; 1.139 + bool isAlt = false; 1.140 + bool isMeta = false; 1.141 + if(aEvent) { 1.142 + WidgetInputEvent* inputEvent = aEvent->AsInputEvent(); 1.143 + isShift = inputEvent->IsShift(); 1.144 + isControl = inputEvent->IsControl(); 1.145 + isAlt = inputEvent->IsAlt(); 1.146 + isMeta = inputEvent->IsMeta(); 1.147 + } 1.148 + 1.149 + // Have the content handle the event, propagating it according to normal DOM rules. 1.150 + nsCOMPtr<nsIPresShell> shell = PresContext()->GetPresShell(); 1.151 + if (shell) { 1.152 + nsContentUtils::DispatchXULCommand(mContent, 1.153 + aEvent ? 1.154 + aEvent->mFlags.mIsTrusted : aTrustEvent, 1.155 + nullptr, shell, 1.156 + isControl, isAlt, isShift, isMeta); 1.157 + } 1.158 +}