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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Steve Clark (buster@netscape.com)
24 : * Ilya Konstantinov (mozilla-code@future.shiny.co.il)
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsDOMKeyboardEvent.h"
41 : #include "nsContentUtils.h"
42 :
43 0 : nsDOMKeyboardEvent::nsDOMKeyboardEvent(nsPresContext* aPresContext,
44 : nsKeyEvent* aEvent)
45 : : nsDOMUIEvent(aPresContext, aEvent ? aEvent :
46 0 : new nsKeyEvent(false, 0, nsnull))
47 : {
48 0 : NS_ASSERTION(mEvent->eventStructType == NS_KEY_EVENT, "event type mismatch");
49 :
50 0 : if (aEvent) {
51 0 : mEventIsInternal = false;
52 : }
53 : else {
54 0 : mEventIsInternal = true;
55 0 : mEvent->time = PR_Now();
56 : }
57 0 : }
58 :
59 0 : nsDOMKeyboardEvent::~nsDOMKeyboardEvent()
60 : {
61 0 : if (mEventIsInternal) {
62 0 : delete static_cast<nsKeyEvent*>(mEvent);
63 0 : mEvent = nsnull;
64 : }
65 0 : }
66 :
67 0 : NS_IMPL_ADDREF_INHERITED(nsDOMKeyboardEvent, nsDOMUIEvent)
68 0 : NS_IMPL_RELEASE_INHERITED(nsDOMKeyboardEvent, nsDOMUIEvent)
69 :
70 : DOMCI_DATA(KeyboardEvent, nsDOMKeyboardEvent)
71 :
72 0 : NS_INTERFACE_MAP_BEGIN(nsDOMKeyboardEvent)
73 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMKeyEvent)
74 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(KeyboardEvent)
75 0 : NS_INTERFACE_MAP_END_INHERITING(nsDOMUIEvent)
76 :
77 : NS_IMETHODIMP
78 0 : nsDOMKeyboardEvent::GetAltKey(bool* aIsDown)
79 : {
80 0 : NS_ENSURE_ARG_POINTER(aIsDown);
81 0 : *aIsDown = ((nsInputEvent*)mEvent)->isAlt;
82 0 : return NS_OK;
83 : }
84 :
85 : NS_IMETHODIMP
86 0 : nsDOMKeyboardEvent::GetCtrlKey(bool* aIsDown)
87 : {
88 0 : NS_ENSURE_ARG_POINTER(aIsDown);
89 0 : *aIsDown = ((nsInputEvent*)mEvent)->isControl;
90 0 : return NS_OK;
91 : }
92 :
93 : NS_IMETHODIMP
94 0 : nsDOMKeyboardEvent::GetShiftKey(bool* aIsDown)
95 : {
96 0 : NS_ENSURE_ARG_POINTER(aIsDown);
97 0 : *aIsDown = ((nsInputEvent*)mEvent)->isShift;
98 0 : return NS_OK;
99 : }
100 :
101 : NS_IMETHODIMP
102 0 : nsDOMKeyboardEvent::GetMetaKey(bool* aIsDown)
103 : {
104 0 : NS_ENSURE_ARG_POINTER(aIsDown);
105 0 : *aIsDown = ((nsInputEvent*)mEvent)->isMeta;
106 0 : return NS_OK;
107 : }
108 :
109 : NS_IMETHODIMP
110 0 : nsDOMKeyboardEvent::GetCharCode(PRUint32* aCharCode)
111 : {
112 0 : NS_ENSURE_ARG_POINTER(aCharCode);
113 :
114 0 : switch (mEvent->message) {
115 : case NS_KEY_UP:
116 : case NS_KEY_DOWN:
117 0 : *aCharCode = 0;
118 0 : break;
119 : case NS_KEY_PRESS:
120 0 : *aCharCode = ((nsKeyEvent*)mEvent)->charCode;
121 0 : break;
122 : default:
123 0 : break;
124 : }
125 0 : return NS_OK;
126 : }
127 :
128 : NS_IMETHODIMP
129 0 : nsDOMKeyboardEvent::GetKeyCode(PRUint32* aKeyCode)
130 : {
131 0 : NS_ENSURE_ARG_POINTER(aKeyCode);
132 :
133 0 : switch (mEvent->message) {
134 : case NS_KEY_UP:
135 : case NS_KEY_PRESS:
136 : case NS_KEY_DOWN:
137 0 : *aKeyCode = ((nsKeyEvent*)mEvent)->keyCode;
138 0 : break;
139 : default:
140 0 : *aKeyCode = 0;
141 0 : break;
142 : }
143 :
144 0 : return NS_OK;
145 : }
146 :
147 : /* virtual */
148 : nsresult
149 0 : nsDOMKeyboardEvent::Which(PRUint32* aWhich)
150 : {
151 0 : NS_ENSURE_ARG_POINTER(aWhich);
152 :
153 0 : switch (mEvent->message) {
154 : case NS_KEY_UP:
155 : case NS_KEY_DOWN:
156 0 : 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
160 : {
161 0 : PRUint32 keyCode = ((nsKeyEvent*)mEvent)->keyCode;
162 0 : if (keyCode == NS_VK_RETURN || keyCode == NS_VK_BACK) {
163 0 : *aWhich = keyCode;
164 0 : return NS_OK;
165 : }
166 0 : return GetCharCode(aWhich);
167 : }
168 : break;
169 : default:
170 0 : *aWhich = 0;
171 : break;
172 : }
173 :
174 0 : return NS_OK;
175 : }
176 :
177 : NS_IMETHODIMP
178 0 : nsDOMKeyboardEvent::InitKeyEvent(const nsAString& aType, bool aCanBubble, bool aCancelable,
179 : nsIDOMWindow* aView, bool aCtrlKey, bool aAltKey,
180 : bool aShiftKey, bool aMetaKey,
181 : PRUint32 aKeyCode, PRUint32 aCharCode)
182 : {
183 0 : nsresult rv = nsDOMUIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
184 0 : NS_ENSURE_SUCCESS(rv, rv);
185 :
186 0 : nsKeyEvent* keyEvent = static_cast<nsKeyEvent*>(mEvent);
187 0 : keyEvent->isControl = aCtrlKey;
188 0 : keyEvent->isAlt = aAltKey;
189 0 : keyEvent->isShift = aShiftKey;
190 0 : keyEvent->isMeta = aMetaKey;
191 0 : keyEvent->keyCode = aKeyCode;
192 0 : keyEvent->charCode = aCharCode;
193 :
194 0 : return NS_OK;
195 : }
196 :
197 0 : nsresult NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
198 : nsPresContext* aPresContext,
199 : nsKeyEvent *aEvent)
200 : {
201 0 : nsDOMKeyboardEvent* it = new nsDOMKeyboardEvent(aPresContext, aEvent);
202 0 : return CallQueryInterface(it, aInstancePtrResult);
203 : }
|