bug 477627 - avoiding deadlocks in ClaimTitle. r=brendan
[mozilla-central.git] / content / events / src / nsDOMKeyboardEvent.cpp
blobe2c417755b0300c3747bb4fdef7b6e8ddca3287d
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
13 * License.
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.
22 * Contributor(s):
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,
44 nsKeyEvent* aEvent)
45 : nsDOMUIEvent(aPresContext, aEvent ? aEvent :
46 new nsKeyEvent(PR_FALSE, 0, nsnull))
48 NS_ASSERTION(mEvent->eventStructType == NS_KEY_EVENT, "event type mismatch");
50 if (aEvent) {
51 mEventIsInternal = PR_FALSE;
53 else {
54 mEventIsInternal = PR_TRUE;
55 mEvent->time = PR_Now();
59 nsDOMKeyboardEvent::~nsDOMKeyboardEvent()
61 if (mEventIsInternal) {
62 delete static_cast<nsKeyEvent*>(mEvent);
63 mEvent = nsnull;
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)
75 NS_IMETHODIMP
76 nsDOMKeyboardEvent::GetAltKey(PRBool* aIsDown)
78 NS_ENSURE_ARG_POINTER(aIsDown);
79 *aIsDown = ((nsInputEvent*)mEvent)->isAlt;
80 return NS_OK;
83 NS_IMETHODIMP
84 nsDOMKeyboardEvent::GetCtrlKey(PRBool* aIsDown)
86 NS_ENSURE_ARG_POINTER(aIsDown);
87 *aIsDown = ((nsInputEvent*)mEvent)->isControl;
88 return NS_OK;
91 NS_IMETHODIMP
92 nsDOMKeyboardEvent::GetShiftKey(PRBool* aIsDown)
94 NS_ENSURE_ARG_POINTER(aIsDown);
95 *aIsDown = ((nsInputEvent*)mEvent)->isShift;
96 return NS_OK;
99 NS_IMETHODIMP
100 nsDOMKeyboardEvent::GetMetaKey(PRBool* aIsDown)
102 NS_ENSURE_ARG_POINTER(aIsDown);
103 *aIsDown = ((nsInputEvent*)mEvent)->isMeta;
104 return NS_OK;
107 NS_IMETHODIMP
108 nsDOMKeyboardEvent::GetCharCode(PRUint32* aCharCode)
110 NS_ENSURE_ARG_POINTER(aCharCode);
112 switch (mEvent->message) {
113 case NS_KEY_UP:
114 case NS_KEY_DOWN:
115 ReportWrongPropertyAccessWarning("charCode");
116 *aCharCode = 0;
117 break;
118 case NS_KEY_PRESS:
119 *aCharCode = ((nsKeyEvent*)mEvent)->charCode;
120 break;
121 default:
122 ReportWrongPropertyAccessWarning("charCode");
123 break;
125 return NS_OK;
128 NS_IMETHODIMP
129 nsDOMKeyboardEvent::GetKeyCode(PRUint32* aKeyCode)
131 NS_ENSURE_ARG_POINTER(aKeyCode);
133 switch (mEvent->message) {
134 case NS_KEY_UP:
135 case NS_KEY_PRESS:
136 case NS_KEY_DOWN:
137 *aKeyCode = ((nsKeyEvent*)mEvent)->keyCode;
138 break;
139 default:
140 ReportWrongPropertyAccessWarning("keyCode");
141 *aKeyCode = 0;
142 break;
145 return NS_OK;
148 NS_IMETHODIMP
149 nsDOMKeyboardEvent::GetWhich(PRUint32* aWhich)
151 NS_ENSURE_ARG_POINTER(aWhich);
153 switch (mEvent->message) {
154 case NS_KEY_UP:
155 case NS_KEY_DOWN:
156 return GetKeyCode(aWhich);
157 case NS_KEY_PRESS:
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) {
163 *aWhich = keyCode;
164 return NS_OK;
166 return GetCharCode(aWhich);
168 break;
169 default:
170 ReportWrongPropertyAccessWarning("which");
171 *aWhich = 0;
172 break;
175 return NS_OK;
178 NS_IMETHODIMP
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;
195 return NS_OK;
198 nsresult NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
199 nsPresContext* aPresContext,
200 nsKeyEvent *aEvent)
202 nsDOMKeyboardEvent* it = new nsDOMKeyboardEvent(aPresContext, aEvent);
203 if (nsnull == it) {
204 return NS_ERROR_OUT_OF_MEMORY;
207 return CallQueryInterface(it, aInstancePtrResult);