Bumping manifests a=b2g-bump
[gecko.git] / widget / SharedWidgetUtils.cpp
blob2ef7b89bb0c416e46b0f2a4b71c39232f7ccb021
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"
16 namespace mozilla {
17 namespace widget {
19 //static
20 already_AddRefed<nsIWidget>
21 WidgetUtils::DOMWindowToWidget(nsIDOMWindow *aDOMWindow)
23 nsCOMPtr<nsIWidget> widget;
25 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aDOMWindow);
26 if (window) {
27 nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(window->GetDocShell()));
29 while (!widget && baseWin) {
30 baseWin->GetParentWidget(getter_AddRefs(widget));
31 if (!widget) {
32 nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(do_QueryInterface(baseWin));
33 if (!docShellAsItem)
34 return nullptr;
36 nsCOMPtr<nsIDocShellTreeItem> parent;
37 docShellAsItem->GetParent(getter_AddRefs(parent));
39 window = do_GetInterface(parent);
40 if (!window)
41 return nullptr;
43 baseWin = do_QueryInterface(window->GetDocShell());
48 return widget.forget();
51 // static
52 uint32_t
53 WidgetUtils::ComputeKeyCodeFromChar(uint32_t aCharCode)
55 if (aCharCode >= 'A' && aCharCode <= 'Z') {
56 return aCharCode - 'A' + NS_VK_A;
58 if (aCharCode >= 'a' && aCharCode <= 'z') {
59 return aCharCode - 'a' + NS_VK_A;
61 if (aCharCode >= '0' && aCharCode <= '9') {
62 return aCharCode - '0' + NS_VK_0;
64 switch (aCharCode) {
65 case ' ': return NS_VK_SPACE;
66 case '\t': return NS_VK_TAB;
67 case ':': return NS_VK_COLON;
68 case ';': return NS_VK_SEMICOLON;
69 case '<': return NS_VK_LESS_THAN;
70 case '=': return NS_VK_EQUALS;
71 case '>': return NS_VK_GREATER_THAN;
72 case '?': return NS_VK_QUESTION_MARK;
73 case '@': return NS_VK_AT;
74 case '^': return NS_VK_CIRCUMFLEX;
75 case '!': return NS_VK_EXCLAMATION;
76 case '"': return NS_VK_DOUBLE_QUOTE;
77 case '#': return NS_VK_HASH;
78 case '$': return NS_VK_DOLLAR;
79 case '%': return NS_VK_PERCENT;
80 case '&': return NS_VK_AMPERSAND;
81 case '_': return NS_VK_UNDERSCORE;
82 case '(': return NS_VK_OPEN_PAREN;
83 case ')': return NS_VK_CLOSE_PAREN;
84 case '*': return NS_VK_ASTERISK;
85 case '+': return NS_VK_PLUS;
86 case '|': return NS_VK_PIPE;
87 case '-': return NS_VK_HYPHEN_MINUS;
88 case '{': return NS_VK_OPEN_CURLY_BRACKET;
89 case '}': return NS_VK_CLOSE_CURLY_BRACKET;
90 case '~': return NS_VK_TILDE;
91 case ',': return NS_VK_COMMA;
92 case '.': return NS_VK_PERIOD;
93 case '/': return NS_VK_SLASH;
94 case '`': return NS_VK_BACK_QUOTE;
95 case '[': return NS_VK_OPEN_BRACKET;
96 case '\\': return NS_VK_BACK_SLASH;
97 case ']': return NS_VK_CLOSE_BRACKET;
98 case '\'': return NS_VK_QUOTE;
100 return 0;
103 // static
104 void
105 WidgetUtils::GetLatinCharCodeForKeyCode(uint32_t aKeyCode,
106 bool aIsCapsLock,
107 uint32_t* aUnshiftedCharCode,
108 uint32_t* aShiftedCharCode)
110 MOZ_ASSERT(aUnshiftedCharCode && aShiftedCharCode,
111 "aUnshiftedCharCode and aShiftedCharCode must not be NULL");
113 if (aKeyCode >= NS_VK_A && aKeyCode <= NS_VK_Z) {
114 *aUnshiftedCharCode = *aShiftedCharCode = aKeyCode;
115 if (aIsCapsLock) {
116 *aShiftedCharCode += 0x20;
117 } else {
118 *aUnshiftedCharCode += 0x20;
120 return;
123 // aShiftedCharCode must be zero for non-alphabet keys.
124 *aShiftedCharCode = 0;
126 if (aKeyCode >= NS_VK_0 && aKeyCode <= NS_VK_9) {
127 *aUnshiftedCharCode = aKeyCode;
128 return;
131 switch (aKeyCode) {
132 case NS_VK_SPACE: *aUnshiftedCharCode = ' '; break;
133 case NS_VK_COLON: *aUnshiftedCharCode = ':'; break;
134 case NS_VK_SEMICOLON: *aUnshiftedCharCode = ';'; break;
135 case NS_VK_LESS_THAN: *aUnshiftedCharCode = '<'; break;
136 case NS_VK_EQUALS: *aUnshiftedCharCode = '='; break;
137 case NS_VK_GREATER_THAN: *aUnshiftedCharCode = '>'; break;
138 case NS_VK_QUESTION_MARK: *aUnshiftedCharCode = '?'; break;
139 case NS_VK_AT: *aUnshiftedCharCode = '@'; break;
140 case NS_VK_CIRCUMFLEX: *aUnshiftedCharCode = '^'; break;
141 case NS_VK_EXCLAMATION: *aUnshiftedCharCode = '!'; break;
142 case NS_VK_DOUBLE_QUOTE: *aUnshiftedCharCode = '"'; break;
143 case NS_VK_HASH: *aUnshiftedCharCode = '#'; break;
144 case NS_VK_DOLLAR: *aUnshiftedCharCode = '$'; break;
145 case NS_VK_PERCENT: *aUnshiftedCharCode = '%'; break;
146 case NS_VK_AMPERSAND: *aUnshiftedCharCode = '&'; break;
147 case NS_VK_UNDERSCORE: *aUnshiftedCharCode = '_'; break;
148 case NS_VK_OPEN_PAREN: *aUnshiftedCharCode = '('; break;
149 case NS_VK_CLOSE_PAREN: *aUnshiftedCharCode = ')'; break;
150 case NS_VK_ASTERISK: *aUnshiftedCharCode = '*'; break;
151 case NS_VK_PLUS: *aUnshiftedCharCode = '+'; break;
152 case NS_VK_PIPE: *aUnshiftedCharCode = '|'; break;
153 case NS_VK_HYPHEN_MINUS: *aUnshiftedCharCode = '-'; break;
154 case NS_VK_OPEN_CURLY_BRACKET: *aUnshiftedCharCode = '{'; break;
155 case NS_VK_CLOSE_CURLY_BRACKET: *aUnshiftedCharCode = '}'; break;
156 case NS_VK_TILDE: *aUnshiftedCharCode = '~'; break;
157 case NS_VK_COMMA: *aUnshiftedCharCode = ','; break;
158 case NS_VK_PERIOD: *aUnshiftedCharCode = '.'; break;
159 case NS_VK_SLASH: *aUnshiftedCharCode = '/'; break;
160 case NS_VK_BACK_QUOTE: *aUnshiftedCharCode = '`'; break;
161 case NS_VK_OPEN_BRACKET: *aUnshiftedCharCode = '['; break;
162 case NS_VK_BACK_SLASH: *aUnshiftedCharCode = '\\'; break;
163 case NS_VK_CLOSE_BRACKET: *aUnshiftedCharCode = ']'; break;
164 case NS_VK_QUOTE: *aUnshiftedCharCode = '\''; break;
165 default: *aUnshiftedCharCode = 0; break;
169 } // namespace widget
170 } // namespace mozilla