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"
22 WidgetUtils::Shutdown()
24 WidgetKeyboardEvent::Shutdown();
28 already_AddRefed
<nsIWidget
>
29 WidgetUtils::DOMWindowToWidget(nsPIDOMWindowOuter
* aDOMWindow
)
31 nsCOMPtr
<nsIWidget
> widget
;
32 nsCOMPtr
<nsPIDOMWindowOuter
> window
= aDOMWindow
;
35 nsCOMPtr
<nsIBaseWindow
> baseWin(do_QueryInterface(window
->GetDocShell()));
37 while (!widget
&& baseWin
) {
38 baseWin
->GetParentWidget(getter_AddRefs(widget
));
40 nsCOMPtr
<nsIDocShellTreeItem
> docShellAsItem(do_QueryInterface(baseWin
));
44 nsCOMPtr
<nsIDocShellTreeItem
> parent
;
45 docShellAsItem
->GetParent(getter_AddRefs(parent
));
47 window
= do_GetInterface(parent
);
51 baseWin
= do_QueryInterface(window
->GetDocShell());
56 return widget
.forget();
61 WidgetUtils::ComputeKeyCodeFromChar(uint32_t aCharCode
)
63 if (aCharCode
>= 'A' && aCharCode
<= 'Z') {
64 return aCharCode
- 'A' + NS_VK_A
;
66 if (aCharCode
>= 'a' && aCharCode
<= 'z') {
67 return aCharCode
- 'a' + NS_VK_A
;
69 if (aCharCode
>= '0' && aCharCode
<= '9') {
70 return aCharCode
- '0' + NS_VK_0
;
73 case ' ': return NS_VK_SPACE
;
74 case '\t': return NS_VK_TAB
;
75 case ':': return NS_VK_COLON
;
76 case ';': return NS_VK_SEMICOLON
;
77 case '<': return NS_VK_LESS_THAN
;
78 case '=': return NS_VK_EQUALS
;
79 case '>': return NS_VK_GREATER_THAN
;
80 case '?': return NS_VK_QUESTION_MARK
;
81 case '@': return NS_VK_AT
;
82 case '^': return NS_VK_CIRCUMFLEX
;
83 case '!': return NS_VK_EXCLAMATION
;
84 case '"': return NS_VK_DOUBLE_QUOTE
;
85 case '#': return NS_VK_HASH
;
86 case '$': return NS_VK_DOLLAR
;
87 case '%': return NS_VK_PERCENT
;
88 case '&': return NS_VK_AMPERSAND
;
89 case '_': return NS_VK_UNDERSCORE
;
90 case '(': return NS_VK_OPEN_PAREN
;
91 case ')': return NS_VK_CLOSE_PAREN
;
92 case '*': return NS_VK_ASTERISK
;
93 case '+': return NS_VK_PLUS
;
94 case '|': return NS_VK_PIPE
;
95 case '-': return NS_VK_HYPHEN_MINUS
;
96 case '{': return NS_VK_OPEN_CURLY_BRACKET
;
97 case '}': return NS_VK_CLOSE_CURLY_BRACKET
;
98 case '~': return NS_VK_TILDE
;
99 case ',': return NS_VK_COMMA
;
100 case '.': return NS_VK_PERIOD
;
101 case '/': return NS_VK_SLASH
;
102 case '`': return NS_VK_BACK_QUOTE
;
103 case '[': return NS_VK_OPEN_BRACKET
;
104 case '\\': return NS_VK_BACK_SLASH
;
105 case ']': return NS_VK_CLOSE_BRACKET
;
106 case '\'': return NS_VK_QUOTE
;
113 WidgetUtils::GetLatinCharCodeForKeyCode(uint32_t aKeyCode
,
115 uint32_t* aUnshiftedCharCode
,
116 uint32_t* aShiftedCharCode
)
118 MOZ_ASSERT(aUnshiftedCharCode
&& aShiftedCharCode
,
119 "aUnshiftedCharCode and aShiftedCharCode must not be NULL");
121 if (aKeyCode
>= NS_VK_A
&& aKeyCode
<= NS_VK_Z
) {
122 *aUnshiftedCharCode
= *aShiftedCharCode
= aKeyCode
;
124 *aShiftedCharCode
+= 0x20;
126 *aUnshiftedCharCode
+= 0x20;
131 // aShiftedCharCode must be zero for non-alphabet keys.
132 *aShiftedCharCode
= 0;
134 if (aKeyCode
>= NS_VK_0
&& aKeyCode
<= NS_VK_9
) {
135 *aUnshiftedCharCode
= aKeyCode
;
140 case NS_VK_SPACE
: *aUnshiftedCharCode
= ' '; break;
141 case NS_VK_COLON
: *aUnshiftedCharCode
= ':'; break;
142 case NS_VK_SEMICOLON
: *aUnshiftedCharCode
= ';'; break;
143 case NS_VK_LESS_THAN
: *aUnshiftedCharCode
= '<'; break;
144 case NS_VK_EQUALS
: *aUnshiftedCharCode
= '='; break;
145 case NS_VK_GREATER_THAN
: *aUnshiftedCharCode
= '>'; break;
146 case NS_VK_QUESTION_MARK
: *aUnshiftedCharCode
= '?'; break;
147 case NS_VK_AT
: *aUnshiftedCharCode
= '@'; break;
148 case NS_VK_CIRCUMFLEX
: *aUnshiftedCharCode
= '^'; break;
149 case NS_VK_EXCLAMATION
: *aUnshiftedCharCode
= '!'; break;
150 case NS_VK_DOUBLE_QUOTE
: *aUnshiftedCharCode
= '"'; break;
151 case NS_VK_HASH
: *aUnshiftedCharCode
= '#'; break;
152 case NS_VK_DOLLAR
: *aUnshiftedCharCode
= '$'; break;
153 case NS_VK_PERCENT
: *aUnshiftedCharCode
= '%'; break;
154 case NS_VK_AMPERSAND
: *aUnshiftedCharCode
= '&'; break;
155 case NS_VK_UNDERSCORE
: *aUnshiftedCharCode
= '_'; break;
156 case NS_VK_OPEN_PAREN
: *aUnshiftedCharCode
= '('; break;
157 case NS_VK_CLOSE_PAREN
: *aUnshiftedCharCode
= ')'; break;
158 case NS_VK_ASTERISK
: *aUnshiftedCharCode
= '*'; break;
159 case NS_VK_PLUS
: *aUnshiftedCharCode
= '+'; break;
160 case NS_VK_PIPE
: *aUnshiftedCharCode
= '|'; break;
161 case NS_VK_HYPHEN_MINUS
: *aUnshiftedCharCode
= '-'; break;
162 case NS_VK_OPEN_CURLY_BRACKET
: *aUnshiftedCharCode
= '{'; break;
163 case NS_VK_CLOSE_CURLY_BRACKET
: *aUnshiftedCharCode
= '}'; break;
164 case NS_VK_TILDE
: *aUnshiftedCharCode
= '~'; break;
165 case NS_VK_COMMA
: *aUnshiftedCharCode
= ','; break;
166 case NS_VK_PERIOD
: *aUnshiftedCharCode
= '.'; break;
167 case NS_VK_SLASH
: *aUnshiftedCharCode
= '/'; break;
168 case NS_VK_BACK_QUOTE
: *aUnshiftedCharCode
= '`'; break;
169 case NS_VK_OPEN_BRACKET
: *aUnshiftedCharCode
= '['; break;
170 case NS_VK_BACK_SLASH
: *aUnshiftedCharCode
= '\\'; break;
171 case NS_VK_CLOSE_BRACKET
: *aUnshiftedCharCode
= ']'; break;
172 case NS_VK_QUOTE
: *aUnshiftedCharCode
= '\''; break;
173 default: *aUnshiftedCharCode
= 0; break;
177 } // namespace widget
178 } // namespace mozilla