1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "WidgetUtils.h"
9 #include "mozilla/TextEvents.h"
11 #include "nsIBaseWindow.h"
12 #include "nsIDocShellTreeItem.h"
13 #include "nsIDocShell.h"
14 #include "nsIInterfaceRequestorUtils.h"
15 #include "nsPIDOMWindow.h"
21 void WidgetUtils::Shutdown() {
22 WidgetKeyboardEvent::Shutdown();
23 InternalEditorInputEvent::Shutdown();
27 already_AddRefed
<nsIWidget
> WidgetUtils::DOMWindowToWidget(
28 nsPIDOMWindowOuter
* aDOMWindow
) {
29 nsCOMPtr
<nsIWidget
> widget
;
30 nsCOMPtr
<nsPIDOMWindowOuter
> window
= aDOMWindow
;
33 nsCOMPtr
<nsIBaseWindow
> baseWin(do_QueryInterface(window
->GetDocShell()));
35 while (!widget
&& baseWin
) {
36 baseWin
->GetParentWidget(getter_AddRefs(widget
));
38 nsCOMPtr
<nsIDocShellTreeItem
> docShellAsItem(
39 do_QueryInterface(baseWin
));
40 if (!docShellAsItem
) return nullptr;
42 nsCOMPtr
<nsIDocShellTreeItem
> parent
;
43 docShellAsItem
->GetInProcessParent(getter_AddRefs(parent
));
45 window
= do_GetInterface(parent
);
46 if (!window
) return nullptr;
48 baseWin
= do_QueryInterface(window
->GetDocShell());
53 return widget
.forget();
57 uint32_t WidgetUtils::ComputeKeyCodeFromChar(uint32_t aCharCode
) {
58 if (aCharCode
>= 'A' && aCharCode
<= 'Z') {
59 return aCharCode
- 'A' + NS_VK_A
;
61 if (aCharCode
>= 'a' && aCharCode
<= 'z') {
62 return aCharCode
- 'a' + NS_VK_A
;
64 if (aCharCode
>= '0' && aCharCode
<= '9') {
65 return aCharCode
- '0' + NS_VK_0
;
75 return NS_VK_SEMICOLON
;
77 return NS_VK_LESS_THAN
;
81 return NS_VK_GREATER_THAN
;
83 return NS_VK_QUESTION_MARK
;
87 return NS_VK_CIRCUMFLEX
;
89 return NS_VK_EXCLAMATION
;
91 return NS_VK_DOUBLE_QUOTE
;
99 return NS_VK_AMPERSAND
;
101 return NS_VK_UNDERSCORE
;
103 return NS_VK_OPEN_PAREN
;
105 return NS_VK_CLOSE_PAREN
;
107 return NS_VK_ASTERISK
;
113 return NS_VK_HYPHEN_MINUS
;
115 return NS_VK_OPEN_CURLY_BRACKET
;
117 return NS_VK_CLOSE_CURLY_BRACKET
;
127 return NS_VK_BACK_QUOTE
;
129 return NS_VK_OPEN_BRACKET
;
131 return NS_VK_BACK_SLASH
;
133 return NS_VK_CLOSE_BRACKET
;
141 void WidgetUtils::GetLatinCharCodeForKeyCode(uint32_t aKeyCode
,
143 uint32_t* aUnshiftedCharCode
,
144 uint32_t* aShiftedCharCode
) {
145 MOZ_ASSERT(aUnshiftedCharCode
&& aShiftedCharCode
,
146 "aUnshiftedCharCode and aShiftedCharCode must not be NULL");
148 if (aKeyCode
>= NS_VK_A
&& aKeyCode
<= NS_VK_Z
) {
149 *aUnshiftedCharCode
= *aShiftedCharCode
= aKeyCode
;
151 *aShiftedCharCode
+= 0x20;
153 *aUnshiftedCharCode
+= 0x20;
158 // aShiftedCharCode must be zero for non-alphabet keys.
159 *aShiftedCharCode
= 0;
161 if (aKeyCode
>= NS_VK_0
&& aKeyCode
<= NS_VK_9
) {
162 *aUnshiftedCharCode
= aKeyCode
;
168 *aUnshiftedCharCode
= ' ';
171 *aUnshiftedCharCode
= ':';
173 case NS_VK_SEMICOLON
:
174 *aUnshiftedCharCode
= ';';
176 case NS_VK_LESS_THAN
:
177 *aUnshiftedCharCode
= '<';
180 *aUnshiftedCharCode
= '=';
182 case NS_VK_GREATER_THAN
:
183 *aUnshiftedCharCode
= '>';
185 case NS_VK_QUESTION_MARK
:
186 *aUnshiftedCharCode
= '?';
189 *aUnshiftedCharCode
= '@';
191 case NS_VK_CIRCUMFLEX
:
192 *aUnshiftedCharCode
= '^';
194 case NS_VK_EXCLAMATION
:
195 *aUnshiftedCharCode
= '!';
197 case NS_VK_DOUBLE_QUOTE
:
198 *aUnshiftedCharCode
= '"';
201 *aUnshiftedCharCode
= '#';
204 *aUnshiftedCharCode
= '$';
207 *aUnshiftedCharCode
= '%';
209 case NS_VK_AMPERSAND
:
210 *aUnshiftedCharCode
= '&';
212 case NS_VK_UNDERSCORE
:
213 *aUnshiftedCharCode
= '_';
215 case NS_VK_OPEN_PAREN
:
216 *aUnshiftedCharCode
= '(';
218 case NS_VK_CLOSE_PAREN
:
219 *aUnshiftedCharCode
= ')';
222 *aUnshiftedCharCode
= '*';
225 *aUnshiftedCharCode
= '+';
228 *aUnshiftedCharCode
= '|';
230 case NS_VK_HYPHEN_MINUS
:
231 *aUnshiftedCharCode
= '-';
233 case NS_VK_OPEN_CURLY_BRACKET
:
234 *aUnshiftedCharCode
= '{';
236 case NS_VK_CLOSE_CURLY_BRACKET
:
237 *aUnshiftedCharCode
= '}';
240 *aUnshiftedCharCode
= '~';
243 *aUnshiftedCharCode
= ',';
246 *aUnshiftedCharCode
= '.';
249 *aUnshiftedCharCode
= '/';
251 case NS_VK_BACK_QUOTE
:
252 *aUnshiftedCharCode
= '`';
254 case NS_VK_OPEN_BRACKET
:
255 *aUnshiftedCharCode
= '[';
257 case NS_VK_BACK_SLASH
:
258 *aUnshiftedCharCode
= '\\';
260 case NS_VK_CLOSE_BRACKET
:
261 *aUnshiftedCharCode
= ']';
264 *aUnshiftedCharCode
= '\'';
267 *aUnshiftedCharCode
= 0;
272 } // namespace widget
273 } // namespace mozilla