1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Steve Clark (buster@netscape.com)
24 * Ilya Konstantinov (mozilla-code@future.shiny.co.il)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsDOMKeyboardEvent.h"
41 #include "nsContentUtils.h"
43 nsDOMKeyboardEvent::nsDOMKeyboardEvent(nsPresContext
* aPresContext
,
45 : nsDOMUIEvent(aPresContext
, aEvent
? aEvent
:
46 new nsKeyEvent(PR_FALSE
, 0, nsnull
))
48 NS_ASSERTION(mEvent
->eventStructType
== NS_KEY_EVENT
, "event type mismatch");
51 mEventIsInternal
= PR_FALSE
;
54 mEventIsInternal
= PR_TRUE
;
55 mEvent
->time
= PR_Now();
59 nsDOMKeyboardEvent::~nsDOMKeyboardEvent()
61 if (mEventIsInternal
) {
62 delete static_cast<nsKeyEvent
*>(mEvent
);
67 NS_IMPL_ADDREF_INHERITED(nsDOMKeyboardEvent
, nsDOMUIEvent
)
68 NS_IMPL_RELEASE_INHERITED(nsDOMKeyboardEvent
, nsDOMUIEvent
)
70 NS_INTERFACE_MAP_BEGIN(nsDOMKeyboardEvent
)
71 NS_INTERFACE_MAP_ENTRY(nsIDOMKeyEvent
)
72 NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(KeyboardEvent
)
73 NS_INTERFACE_MAP_END_INHERITING(nsDOMUIEvent
)
76 nsDOMKeyboardEvent::GetAltKey(PRBool
* aIsDown
)
78 NS_ENSURE_ARG_POINTER(aIsDown
);
79 *aIsDown
= ((nsInputEvent
*)mEvent
)->isAlt
;
84 nsDOMKeyboardEvent::GetCtrlKey(PRBool
* aIsDown
)
86 NS_ENSURE_ARG_POINTER(aIsDown
);
87 *aIsDown
= ((nsInputEvent
*)mEvent
)->isControl
;
92 nsDOMKeyboardEvent::GetShiftKey(PRBool
* aIsDown
)
94 NS_ENSURE_ARG_POINTER(aIsDown
);
95 *aIsDown
= ((nsInputEvent
*)mEvent
)->isShift
;
100 nsDOMKeyboardEvent::GetMetaKey(PRBool
* aIsDown
)
102 NS_ENSURE_ARG_POINTER(aIsDown
);
103 *aIsDown
= ((nsInputEvent
*)mEvent
)->isMeta
;
108 nsDOMKeyboardEvent::GetCharCode(PRUint32
* aCharCode
)
110 NS_ENSURE_ARG_POINTER(aCharCode
);
112 switch (mEvent
->message
) {
115 ReportWrongPropertyAccessWarning("charCode");
119 *aCharCode
= ((nsKeyEvent
*)mEvent
)->charCode
;
122 ReportWrongPropertyAccessWarning("charCode");
129 nsDOMKeyboardEvent::GetKeyCode(PRUint32
* aKeyCode
)
131 NS_ENSURE_ARG_POINTER(aKeyCode
);
133 switch (mEvent
->message
) {
137 *aKeyCode
= ((nsKeyEvent
*)mEvent
)->keyCode
;
140 ReportWrongPropertyAccessWarning("keyCode");
149 nsDOMKeyboardEvent::GetWhich(PRUint32
* aWhich
)
151 NS_ENSURE_ARG_POINTER(aWhich
);
153 switch (mEvent
->message
) {
156 return GetKeyCode(aWhich
);
158 //Special case for 4xp bug 62878. Try to make value of which
159 //more closely mirror the values that 4.x gave for RETURN and BACKSPACE
161 PRUint32 keyCode
= ((nsKeyEvent
*)mEvent
)->keyCode
;
162 if (keyCode
== NS_VK_RETURN
|| keyCode
== NS_VK_BACK
) {
166 return GetCharCode(aWhich
);
170 ReportWrongPropertyAccessWarning("which");
179 nsDOMKeyboardEvent::InitKeyEvent(const nsAString
& aType
, PRBool aCanBubble
, PRBool aCancelable
,
180 nsIDOMAbstractView
* aView
, PRBool aCtrlKey
, PRBool aAltKey
,
181 PRBool aShiftKey
, PRBool aMetaKey
,
182 PRUint32 aKeyCode
, PRUint32 aCharCode
)
184 nsresult rv
= nsDOMUIEvent::InitUIEvent(aType
, aCanBubble
, aCancelable
, aView
, 0);
185 NS_ENSURE_SUCCESS(rv
, rv
);
187 nsKeyEvent
* keyEvent
= static_cast<nsKeyEvent
*>(mEvent
);
188 keyEvent
->isControl
= aCtrlKey
;
189 keyEvent
->isAlt
= aAltKey
;
190 keyEvent
->isShift
= aShiftKey
;
191 keyEvent
->isMeta
= aMetaKey
;
192 keyEvent
->keyCode
= aKeyCode
;
193 keyEvent
->charCode
= aCharCode
;
198 nsresult
NS_NewDOMKeyboardEvent(nsIDOMEvent
** aInstancePtrResult
,
199 nsPresContext
* aPresContext
,
202 nsDOMKeyboardEvent
* it
= new nsDOMKeyboardEvent(aPresContext
, aEvent
);
204 return NS_ERROR_OUT_OF_MEMORY
;
207 return CallQueryInterface(it
, aInstancePtrResult
);