1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6 
7 #include "WidgetUtils.h"
8 
9 #include "mozilla/TextEvents.h"
10 
11 #include "nsIBaseWindow.h"
12 #include "nsIDocShellTreeItem.h"
13 #include "nsIDocShell.h"
14 #include "nsIInterfaceRequestorUtils.h"
15 #include "nsPIDOMWindow.h"
16 
17 namespace mozilla {
18 namespace widget {
19 
20 // static
Shutdown()21 void WidgetUtils::Shutdown() {
22   WidgetKeyboardEvent::Shutdown();
23   InternalEditorInputEvent::Shutdown();
24 }
25 
26 // static
DOMWindowToWidget(nsPIDOMWindowOuter * aDOMWindow)27 already_AddRefed<nsIWidget> WidgetUtils::DOMWindowToWidget(
28     nsPIDOMWindowOuter* aDOMWindow) {
29   nsCOMPtr<nsIWidget> widget;
30   nsCOMPtr<nsPIDOMWindowOuter> window = aDOMWindow;
31 
32   if (window) {
33     nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(window->GetDocShell()));
34 
35     while (!widget && baseWin) {
36       baseWin->GetParentWidget(getter_AddRefs(widget));
37       if (!widget) {
38         nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(
39             do_QueryInterface(baseWin));
40         if (!docShellAsItem) return nullptr;
41 
42         nsCOMPtr<nsIDocShellTreeItem> parent;
43         docShellAsItem->GetInProcessParent(getter_AddRefs(parent));
44 
45         window = do_GetInterface(parent);
46         if (!window) return nullptr;
47 
48         baseWin = do_QueryInterface(window->GetDocShell());
49       }
50     }
51   }
52 
53   return widget.forget();
54 }
55 
56 // static
ComputeKeyCodeFromChar(uint32_t aCharCode)57 uint32_t WidgetUtils::ComputeKeyCodeFromChar(uint32_t aCharCode) {
58   if (aCharCode >= 'A' && aCharCode <= 'Z') {
59     return aCharCode - 'A' + NS_VK_A;
60   }
61   if (aCharCode >= 'a' && aCharCode <= 'z') {
62     return aCharCode - 'a' + NS_VK_A;
63   }
64   if (aCharCode >= '0' && aCharCode <= '9') {
65     return aCharCode - '0' + NS_VK_0;
66   }
67   switch (aCharCode) {
68     case ' ':
69       return NS_VK_SPACE;
70     case '\t':
71       return NS_VK_TAB;
72     case ':':
73       return NS_VK_COLON;
74     case ';':
75       return NS_VK_SEMICOLON;
76     case '<':
77       return NS_VK_LESS_THAN;
78     case '=':
79       return NS_VK_EQUALS;
80     case '>':
81       return NS_VK_GREATER_THAN;
82     case '?':
83       return NS_VK_QUESTION_MARK;
84     case '@':
85       return NS_VK_AT;
86     case '^':
87       return NS_VK_CIRCUMFLEX;
88     case '!':
89       return NS_VK_EXCLAMATION;
90     case '"':
91       return NS_VK_DOUBLE_QUOTE;
92     case '#':
93       return NS_VK_HASH;
94     case '$':
95       return NS_VK_DOLLAR;
96     case '%':
97       return NS_VK_PERCENT;
98     case '&':
99       return NS_VK_AMPERSAND;
100     case '_':
101       return NS_VK_UNDERSCORE;
102     case '(':
103       return NS_VK_OPEN_PAREN;
104     case ')':
105       return NS_VK_CLOSE_PAREN;
106     case '*':
107       return NS_VK_ASTERISK;
108     case '+':
109       return NS_VK_PLUS;
110     case '|':
111       return NS_VK_PIPE;
112     case '-':
113       return NS_VK_HYPHEN_MINUS;
114     case '{':
115       return NS_VK_OPEN_CURLY_BRACKET;
116     case '}':
117       return NS_VK_CLOSE_CURLY_BRACKET;
118     case '~':
119       return NS_VK_TILDE;
120     case ',':
121       return NS_VK_COMMA;
122     case '.':
123       return NS_VK_PERIOD;
124     case '/':
125       return NS_VK_SLASH;
126     case '`':
127       return NS_VK_BACK_QUOTE;
128     case '[':
129       return NS_VK_OPEN_BRACKET;
130     case '\\':
131       return NS_VK_BACK_SLASH;
132     case ']':
133       return NS_VK_CLOSE_BRACKET;
134     case '\'':
135       return NS_VK_QUOTE;
136   }
137   return 0;
138 }
139 
140 // static
GetLatinCharCodeForKeyCode(uint32_t aKeyCode,bool aIsCapsLock,uint32_t * aUnshiftedCharCode,uint32_t * aShiftedCharCode)141 void WidgetUtils::GetLatinCharCodeForKeyCode(uint32_t aKeyCode,
142                                              bool aIsCapsLock,
143                                              uint32_t* aUnshiftedCharCode,
144                                              uint32_t* aShiftedCharCode) {
145   MOZ_ASSERT(aUnshiftedCharCode && aShiftedCharCode,
146              "aUnshiftedCharCode and aShiftedCharCode must not be NULL");
147 
148   if (aKeyCode >= NS_VK_A && aKeyCode <= NS_VK_Z) {
149     *aUnshiftedCharCode = *aShiftedCharCode = aKeyCode;
150     if (aIsCapsLock) {
151       *aShiftedCharCode += 0x20;
152     } else {
153       *aUnshiftedCharCode += 0x20;
154     }
155     return;
156   }
157 
158   // aShiftedCharCode must be zero for non-alphabet keys.
159   *aShiftedCharCode = 0;
160 
161   if (aKeyCode >= NS_VK_0 && aKeyCode <= NS_VK_9) {
162     *aUnshiftedCharCode = aKeyCode;
163     return;
164   }
165 
166   switch (aKeyCode) {
167     case NS_VK_SPACE:
168       *aUnshiftedCharCode = ' ';
169       break;
170     case NS_VK_COLON:
171       *aUnshiftedCharCode = ':';
172       break;
173     case NS_VK_SEMICOLON:
174       *aUnshiftedCharCode = ';';
175       break;
176     case NS_VK_LESS_THAN:
177       *aUnshiftedCharCode = '<';
178       break;
179     case NS_VK_EQUALS:
180       *aUnshiftedCharCode = '=';
181       break;
182     case NS_VK_GREATER_THAN:
183       *aUnshiftedCharCode = '>';
184       break;
185     case NS_VK_QUESTION_MARK:
186       *aUnshiftedCharCode = '?';
187       break;
188     case NS_VK_AT:
189       *aUnshiftedCharCode = '@';
190       break;
191     case NS_VK_CIRCUMFLEX:
192       *aUnshiftedCharCode = '^';
193       break;
194     case NS_VK_EXCLAMATION:
195       *aUnshiftedCharCode = '!';
196       break;
197     case NS_VK_DOUBLE_QUOTE:
198       *aUnshiftedCharCode = '"';
199       break;
200     case NS_VK_HASH:
201       *aUnshiftedCharCode = '#';
202       break;
203     case NS_VK_DOLLAR:
204       *aUnshiftedCharCode = '$';
205       break;
206     case NS_VK_PERCENT:
207       *aUnshiftedCharCode = '%';
208       break;
209     case NS_VK_AMPERSAND:
210       *aUnshiftedCharCode = '&';
211       break;
212     case NS_VK_UNDERSCORE:
213       *aUnshiftedCharCode = '_';
214       break;
215     case NS_VK_OPEN_PAREN:
216       *aUnshiftedCharCode = '(';
217       break;
218     case NS_VK_CLOSE_PAREN:
219       *aUnshiftedCharCode = ')';
220       break;
221     case NS_VK_ASTERISK:
222       *aUnshiftedCharCode = '*';
223       break;
224     case NS_VK_PLUS:
225       *aUnshiftedCharCode = '+';
226       break;
227     case NS_VK_PIPE:
228       *aUnshiftedCharCode = '|';
229       break;
230     case NS_VK_HYPHEN_MINUS:
231       *aUnshiftedCharCode = '-';
232       break;
233     case NS_VK_OPEN_CURLY_BRACKET:
234       *aUnshiftedCharCode = '{';
235       break;
236     case NS_VK_CLOSE_CURLY_BRACKET:
237       *aUnshiftedCharCode = '}';
238       break;
239     case NS_VK_TILDE:
240       *aUnshiftedCharCode = '~';
241       break;
242     case NS_VK_COMMA:
243       *aUnshiftedCharCode = ',';
244       break;
245     case NS_VK_PERIOD:
246       *aUnshiftedCharCode = '.';
247       break;
248     case NS_VK_SLASH:
249       *aUnshiftedCharCode = '/';
250       break;
251     case NS_VK_BACK_QUOTE:
252       *aUnshiftedCharCode = '`';
253       break;
254     case NS_VK_OPEN_BRACKET:
255       *aUnshiftedCharCode = '[';
256       break;
257     case NS_VK_BACK_SLASH:
258       *aUnshiftedCharCode = '\\';
259       break;
260     case NS_VK_CLOSE_BRACKET:
261       *aUnshiftedCharCode = ']';
262       break;
263     case NS_VK_QUOTE:
264       *aUnshiftedCharCode = '\'';
265       break;
266     default:
267       *aUnshiftedCharCode = 0;
268       break;
269   }
270 }
271 
272 }  // namespace widget
273 }  // namespace mozilla
274