Bug 1454293 [wpt PR 10484] - null is not the correct origin for createDocument()...
[gecko.git] / widget / WidgetUtils.cpp
blob9285586bb1f1cdf9f2addda214dea31fbebb3854
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/WidgetUtils.h"
9 #include "mozilla/dom/ContentParent.h"
10 #include "mozilla/Services.h"
11 #include "mozilla/Unused.h"
12 #include "nsContentUtils.h"
13 #include "nsIBidiKeyboard.h"
14 #include "nsIStringBundle.h"
15 #include "nsTArray.h"
16 #ifdef XP_WIN
17 #include "WinUtils.h"
18 #endif
19 #ifdef MOZ_WIDGET_GTK
20 #include "mozilla/WidgetUtilsGtk.h"
21 #endif
23 namespace mozilla {
25 gfx::Matrix
26 ComputeTransformForRotation(const nsIntRect& aBounds,
27 ScreenRotation aRotation)
29 gfx::Matrix transform;
30 static const gfx::Float floatPi = static_cast<gfx::Float>(M_PI);
32 switch (aRotation) {
33 case ROTATION_0:
34 break;
35 case ROTATION_90:
36 transform.PreTranslate(aBounds.Width(), 0);
37 transform.PreRotate(floatPi / 2);
38 break;
39 case ROTATION_180:
40 transform.PreTranslate(aBounds.Width(), aBounds.Height());
41 transform.PreRotate(floatPi);
42 break;
43 case ROTATION_270:
44 transform.PreTranslate(0, aBounds.Height());
45 transform.PreRotate(floatPi * 3 / 2);
46 break;
47 default:
48 MOZ_CRASH("Unknown rotation");
50 return transform;
53 gfx::Matrix
54 ComputeTransformForUnRotation(const nsIntRect& aBounds,
55 ScreenRotation aRotation)
57 gfx::Matrix transform;
58 static const gfx::Float floatPi = static_cast<gfx::Float>(M_PI);
60 switch (aRotation) {
61 case ROTATION_0:
62 break;
63 case ROTATION_90:
64 transform.PreTranslate(0, aBounds.Height());
65 transform.PreRotate(floatPi * 3 / 2);
66 break;
67 case ROTATION_180:
68 transform.PreTranslate(aBounds.Width(), aBounds.Height());
69 transform.PreRotate(floatPi);
70 break;
71 case ROTATION_270:
72 transform.PreTranslate(aBounds.Width(), 0);
73 transform.PreRotate(floatPi / 2);
74 break;
75 default:
76 MOZ_CRASH("Unknown rotation");
78 return transform;
81 nsIntRect RotateRect(nsIntRect aRect,
82 const nsIntRect& aBounds,
83 ScreenRotation aRotation)
85 switch (aRotation) {
86 case ROTATION_0:
87 return aRect;
88 case ROTATION_90:
89 return nsIntRect(aRect.Y(),
90 aBounds.Width() - aRect.XMost(),
91 aRect.Height(), aRect.Width());
92 case ROTATION_180:
93 return nsIntRect(aBounds.Width() - aRect.XMost(),
94 aBounds.Height() - aRect.YMost(),
95 aRect.Width(), aRect.Height());
96 case ROTATION_270:
97 return nsIntRect(aBounds.Height() - aRect.YMost(),
98 aRect.X(),
99 aRect.Height(), aRect.Width());
100 default:
101 MOZ_CRASH("Unknown rotation");
105 namespace widget {
107 uint32_t
108 WidgetUtils::IsTouchDeviceSupportPresent()
110 #ifdef XP_WIN
111 return WinUtils::IsTouchDeviceSupportPresent();
112 #elif defined(MOZ_WIDGET_GTK)
113 return WidgetUtilsGTK::IsTouchDeviceSupportPresent();
114 #else
115 return 0;
116 #endif
119 // static
120 void
121 WidgetUtils::SendBidiKeyboardInfoToContent()
123 nsCOMPtr<nsIBidiKeyboard> bidiKeyboard = nsContentUtils::GetBidiKeyboard();
124 if (!bidiKeyboard) {
125 return;
128 bool rtl;
129 if (NS_FAILED(bidiKeyboard->IsLangRTL(&rtl))) {
130 return;
132 bool bidiKeyboards = false;
133 bidiKeyboard->GetHaveBidiKeyboards(&bidiKeyboards);
135 nsTArray<dom::ContentParent*> children;
136 dom::ContentParent::GetAll(children);
137 for (uint32_t i = 0; i < children.Length(); i++) {
138 Unused << children[i]->SendBidiKeyboardNotify(rtl, bidiKeyboards);
142 // static
143 void
144 WidgetUtils::GetBrandShortName(nsAString& aBrandName)
146 aBrandName.Truncate();
148 nsCOMPtr<nsIStringBundleService> bundleService =
149 mozilla::services::GetStringBundleService();
151 nsCOMPtr<nsIStringBundle> bundle;
152 if (bundleService) {
153 bundleService->CreateBundle(
154 "chrome://branding/locale/brand.properties",
155 getter_AddRefs(bundle));
158 if (bundle) {
159 bundle->GetStringFromName("brandShortName", aBrandName);
163 } // namespace widget
164 } // namespace mozilla