dom/events/KeyboardEvent.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/events/KeyboardEvent.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,295 @@
     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 +
     1.9 +#include "mozilla/dom/KeyboardEvent.h"
    1.10 +#include "mozilla/TextEvents.h"
    1.11 +#include "prtime.h"
    1.12 +
    1.13 +namespace mozilla {
    1.14 +namespace dom {
    1.15 +
    1.16 +KeyboardEvent::KeyboardEvent(EventTarget* aOwner,
    1.17 +                             nsPresContext* aPresContext,
    1.18 +                             WidgetKeyboardEvent* aEvent)
    1.19 +  : UIEvent(aOwner, aPresContext,
    1.20 +            aEvent ? aEvent : new WidgetKeyboardEvent(false, 0, nullptr))
    1.21 +  , mInitializedByCtor(false)
    1.22 +  , mInitialzedWhichValue(0)
    1.23 +{
    1.24 +  NS_ASSERTION(mEvent->eventStructType == NS_KEY_EVENT, "event type mismatch");
    1.25 +
    1.26 +  if (aEvent) {
    1.27 +    mEventIsInternal = false;
    1.28 +  }
    1.29 +  else {
    1.30 +    mEventIsInternal = true;
    1.31 +    mEvent->time = PR_Now();
    1.32 +    mEvent->AsKeyboardEvent()->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
    1.33 +  }
    1.34 +}
    1.35 +
    1.36 +NS_IMPL_ADDREF_INHERITED(KeyboardEvent, UIEvent)
    1.37 +NS_IMPL_RELEASE_INHERITED(KeyboardEvent, UIEvent)
    1.38 +
    1.39 +NS_INTERFACE_MAP_BEGIN(KeyboardEvent)
    1.40 +  NS_INTERFACE_MAP_ENTRY(nsIDOMKeyEvent)
    1.41 +NS_INTERFACE_MAP_END_INHERITING(UIEvent)
    1.42 +
    1.43 +bool
    1.44 +KeyboardEvent::AltKey()
    1.45 +{
    1.46 +  return mEvent->AsKeyboardEvent()->IsAlt();
    1.47 +}
    1.48 +
    1.49 +NS_IMETHODIMP
    1.50 +KeyboardEvent::GetAltKey(bool* aIsDown)
    1.51 +{
    1.52 +  NS_ENSURE_ARG_POINTER(aIsDown);
    1.53 +  *aIsDown = AltKey();
    1.54 +  return NS_OK;
    1.55 +}
    1.56 +
    1.57 +bool
    1.58 +KeyboardEvent::CtrlKey()
    1.59 +{
    1.60 +  return mEvent->AsKeyboardEvent()->IsControl();
    1.61 +}
    1.62 +
    1.63 +NS_IMETHODIMP
    1.64 +KeyboardEvent::GetCtrlKey(bool* aIsDown)
    1.65 +{
    1.66 +  NS_ENSURE_ARG_POINTER(aIsDown);
    1.67 +  *aIsDown = CtrlKey();
    1.68 +  return NS_OK;
    1.69 +}
    1.70 +
    1.71 +bool
    1.72 +KeyboardEvent::ShiftKey()
    1.73 +{
    1.74 +  return mEvent->AsKeyboardEvent()->IsShift();
    1.75 +}
    1.76 +
    1.77 +NS_IMETHODIMP
    1.78 +KeyboardEvent::GetShiftKey(bool* aIsDown)
    1.79 +{
    1.80 +  NS_ENSURE_ARG_POINTER(aIsDown);
    1.81 +  *aIsDown = ShiftKey();
    1.82 +  return NS_OK;
    1.83 +}
    1.84 +
    1.85 +bool
    1.86 +KeyboardEvent::MetaKey()
    1.87 +{
    1.88 +  return mEvent->AsKeyboardEvent()->IsMeta();
    1.89 +}
    1.90 +
    1.91 +NS_IMETHODIMP
    1.92 +KeyboardEvent::GetMetaKey(bool* aIsDown)
    1.93 +{
    1.94 +  NS_ENSURE_ARG_POINTER(aIsDown);
    1.95 +  *aIsDown = MetaKey();
    1.96 +  return NS_OK;
    1.97 +}
    1.98 +
    1.99 +bool
   1.100 +KeyboardEvent::Repeat()
   1.101 +{
   1.102 +  return mEvent->AsKeyboardEvent()->mIsRepeat;
   1.103 +}
   1.104 +
   1.105 +NS_IMETHODIMP
   1.106 +KeyboardEvent::GetRepeat(bool* aIsRepeat)
   1.107 +{
   1.108 +  NS_ENSURE_ARG_POINTER(aIsRepeat);
   1.109 +  *aIsRepeat = Repeat();
   1.110 +  return NS_OK;
   1.111 +}
   1.112 +
   1.113 +bool
   1.114 +KeyboardEvent::IsComposing()
   1.115 +{
   1.116 +  return mEvent->AsKeyboardEvent()->mIsComposing;
   1.117 +}
   1.118 +
   1.119 +NS_IMETHODIMP
   1.120 +KeyboardEvent::GetModifierState(const nsAString& aKey,
   1.121 +                                bool* aState)
   1.122 +{
   1.123 +  NS_ENSURE_ARG_POINTER(aState);
   1.124 +
   1.125 +  *aState = GetModifierState(aKey);
   1.126 +  return NS_OK;
   1.127 +}
   1.128 +
   1.129 +NS_IMETHODIMP
   1.130 +KeyboardEvent::GetKey(nsAString& aKeyName)
   1.131 +{
   1.132 +  mEvent->AsKeyboardEvent()->GetDOMKeyName(aKeyName);
   1.133 +  return NS_OK;
   1.134 +}
   1.135 +
   1.136 +NS_IMETHODIMP
   1.137 +KeyboardEvent::GetCharCode(uint32_t* aCharCode)
   1.138 +{
   1.139 +  NS_ENSURE_ARG_POINTER(aCharCode);
   1.140 +  *aCharCode = CharCode();
   1.141 +  return NS_OK;
   1.142 +}
   1.143 +
   1.144 +uint32_t
   1.145 +KeyboardEvent::CharCode()
   1.146 +{
   1.147 +  // If this event is initialized with ctor, we shouldn't check event type.
   1.148 +  if (mInitializedByCtor) {
   1.149 +    return mEvent->AsKeyboardEvent()->charCode;
   1.150 +  }
   1.151 +
   1.152 +  switch (mEvent->message) {
   1.153 +  case NS_KEY_UP:
   1.154 +  case NS_KEY_DOWN:
   1.155 +    return 0;
   1.156 +  case NS_KEY_PRESS:
   1.157 +    return mEvent->AsKeyboardEvent()->charCode;
   1.158 +  }
   1.159 +  return 0;
   1.160 +}
   1.161 +
   1.162 +NS_IMETHODIMP
   1.163 +KeyboardEvent::GetKeyCode(uint32_t* aKeyCode)
   1.164 +{
   1.165 +  NS_ENSURE_ARG_POINTER(aKeyCode);
   1.166 +  *aKeyCode = KeyCode();
   1.167 +  return NS_OK;
   1.168 +}
   1.169 +
   1.170 +uint32_t
   1.171 +KeyboardEvent::KeyCode()
   1.172 +{
   1.173 +  // If this event is initialized with ctor, we shouldn't check event type.
   1.174 +  if (mInitializedByCtor) {
   1.175 +    return mEvent->AsKeyboardEvent()->keyCode;
   1.176 +  }
   1.177 +
   1.178 +  switch (mEvent->message) {
   1.179 +  case NS_KEY_UP:
   1.180 +  case NS_KEY_PRESS:
   1.181 +  case NS_KEY_DOWN:
   1.182 +    return mEvent->AsKeyboardEvent()->keyCode;
   1.183 +  }
   1.184 +  return 0;
   1.185 +}
   1.186 +
   1.187 +uint32_t
   1.188 +KeyboardEvent::Which()
   1.189 +{
   1.190 +  // If this event is initialized with ctor, which can have independent value.
   1.191 +  if (mInitializedByCtor) {
   1.192 +    return mInitialzedWhichValue;
   1.193 +  }
   1.194 +
   1.195 +  switch (mEvent->message) {
   1.196 +    case NS_KEY_UP:
   1.197 +    case NS_KEY_DOWN:
   1.198 +      return KeyCode();
   1.199 +    case NS_KEY_PRESS:
   1.200 +      //Special case for 4xp bug 62878.  Try to make value of which
   1.201 +      //more closely mirror the values that 4.x gave for RETURN and BACKSPACE
   1.202 +      {
   1.203 +        uint32_t keyCode = mEvent->AsKeyboardEvent()->keyCode;
   1.204 +        if (keyCode == NS_VK_RETURN || keyCode == NS_VK_BACK) {
   1.205 +          return keyCode;
   1.206 +        }
   1.207 +        return CharCode();
   1.208 +      }
   1.209 +  }
   1.210 +
   1.211 +  return 0;
   1.212 +}
   1.213 +
   1.214 +NS_IMETHODIMP
   1.215 +KeyboardEvent::GetLocation(uint32_t* aLocation)
   1.216 +{
   1.217 +  NS_ENSURE_ARG_POINTER(aLocation);
   1.218 +
   1.219 +  *aLocation = Location();
   1.220 +  return NS_OK;
   1.221 +}
   1.222 +
   1.223 +uint32_t
   1.224 +KeyboardEvent::Location()
   1.225 +{
   1.226 +  return mEvent->AsKeyboardEvent()->location;
   1.227 +}
   1.228 +
   1.229 +// static
   1.230 +already_AddRefed<KeyboardEvent>
   1.231 +KeyboardEvent::Constructor(const GlobalObject& aGlobal,
   1.232 +                           const nsAString& aType,
   1.233 +                           const KeyboardEventInit& aParam,
   1.234 +                           ErrorResult& aRv)
   1.235 +{
   1.236 +  nsCOMPtr<EventTarget> target = do_QueryInterface(aGlobal.GetAsSupports());
   1.237 +  nsRefPtr<KeyboardEvent> newEvent =
   1.238 +    new KeyboardEvent(target, nullptr, nullptr);
   1.239 +  bool trusted = newEvent->Init(target);
   1.240 +  aRv = newEvent->InitKeyEvent(aType, aParam.mBubbles, aParam.mCancelable,
   1.241 +                               aParam.mView, aParam.mCtrlKey, aParam.mAltKey,
   1.242 +                               aParam.mShiftKey, aParam.mMetaKey,
   1.243 +                               aParam.mKeyCode, aParam.mCharCode);
   1.244 +  newEvent->SetTrusted(trusted);
   1.245 +  newEvent->mDetail = aParam.mDetail;
   1.246 +  newEvent->mInitializedByCtor = true;
   1.247 +  newEvent->mInitialzedWhichValue = aParam.mWhich;
   1.248 +
   1.249 +  WidgetKeyboardEvent* internalEvent = newEvent->mEvent->AsKeyboardEvent();
   1.250 +  internalEvent->location = aParam.mLocation;
   1.251 +  internalEvent->mIsRepeat = aParam.mRepeat;
   1.252 +  internalEvent->mIsComposing = aParam.mIsComposing;
   1.253 +  internalEvent->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
   1.254 +  internalEvent->mKeyValue = aParam.mKey;
   1.255 +
   1.256 +  return newEvent.forget();
   1.257 +}
   1.258 +
   1.259 +NS_IMETHODIMP
   1.260 +KeyboardEvent::InitKeyEvent(const nsAString& aType,
   1.261 +                            bool aCanBubble,
   1.262 +                            bool aCancelable,
   1.263 +                            nsIDOMWindow* aView,
   1.264 +                            bool aCtrlKey,
   1.265 +                            bool aAltKey,
   1.266 +                            bool aShiftKey,
   1.267 +                            bool aMetaKey,
   1.268 +                            uint32_t aKeyCode,
   1.269 +                            uint32_t aCharCode)
   1.270 +{
   1.271 +  nsresult rv = UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
   1.272 +  NS_ENSURE_SUCCESS(rv, rv);
   1.273 +
   1.274 +  WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent();
   1.275 +  keyEvent->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, aMetaKey);
   1.276 +  keyEvent->keyCode = aKeyCode;
   1.277 +  keyEvent->charCode = aCharCode;
   1.278 +
   1.279 +  return NS_OK;
   1.280 +}
   1.281 +
   1.282 +} // namespace dom
   1.283 +} // namespace mozilla
   1.284 +
   1.285 +using namespace mozilla;
   1.286 +using namespace mozilla::dom;
   1.287 +
   1.288 +nsresult
   1.289 +NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
   1.290 +                       EventTarget* aOwner,
   1.291 +                       nsPresContext* aPresContext,
   1.292 +                       WidgetKeyboardEvent* aEvent)
   1.293 +{
   1.294 +  KeyboardEvent* it = new KeyboardEvent(aOwner, aPresContext, aEvent);
   1.295 +  NS_ADDREF(it);
   1.296 +  *aInstancePtrResult = static_cast<Event*>(it);
   1.297 +  return NS_OK;
   1.298 +}

mercurial