Bug 1675375 Part 7: Update expectations in helper_hittest_clippath.html. r=botond
[gecko.git] / widget / WidgetUtils.cpp
blob11ac5c40fcc0808b6cc35f554998f656b2f69d0e
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/Components.h"
11 #include "mozilla/Unused.h"
12 #include "nsContentUtils.h"
13 #include "nsIBidiKeyboard.h"
14 #include "nsIStringBundle.h"
15 #include "nsTArray.h"
16 #include "prenv.h"
17 #ifdef XP_WIN
18 # include "WinUtils.h"
19 #endif
20 #ifdef MOZ_WIDGET_GTK
21 # include "mozilla/WidgetUtilsGtk.h"
22 #endif
24 namespace mozilla {
26 gfx::Matrix ComputeTransformForRotation(const nsIntRect& aBounds,
27 ScreenRotation aRotation) {
28 gfx::Matrix transform;
29 static const gfx::Float floatPi = static_cast<gfx::Float>(M_PI);
31 switch (aRotation) {
32 case ROTATION_0:
33 break;
34 case ROTATION_90:
35 transform.PreTranslate(aBounds.Width(), 0);
36 transform.PreRotate(floatPi / 2);
37 break;
38 case ROTATION_180:
39 transform.PreTranslate(aBounds.Width(), aBounds.Height());
40 transform.PreRotate(floatPi);
41 break;
42 case ROTATION_270:
43 transform.PreTranslate(0, aBounds.Height());
44 transform.PreRotate(floatPi * 3 / 2);
45 break;
46 default:
47 MOZ_CRASH("Unknown rotation");
49 return transform;
52 gfx::Matrix ComputeTransformForUnRotation(const nsIntRect& aBounds,
53 ScreenRotation aRotation) {
54 gfx::Matrix transform;
55 static const gfx::Float floatPi = static_cast<gfx::Float>(M_PI);
57 switch (aRotation) {
58 case ROTATION_0:
59 break;
60 case ROTATION_90:
61 transform.PreTranslate(0, aBounds.Height());
62 transform.PreRotate(floatPi * 3 / 2);
63 break;
64 case ROTATION_180:
65 transform.PreTranslate(aBounds.Width(), aBounds.Height());
66 transform.PreRotate(floatPi);
67 break;
68 case ROTATION_270:
69 transform.PreTranslate(aBounds.Width(), 0);
70 transform.PreRotate(floatPi / 2);
71 break;
72 default:
73 MOZ_CRASH("Unknown rotation");
75 return transform;
78 nsIntRect RotateRect(nsIntRect aRect, const nsIntRect& aBounds,
79 ScreenRotation aRotation) {
80 switch (aRotation) {
81 case ROTATION_0:
82 return aRect;
83 case ROTATION_90:
84 return nsIntRect(aRect.Y(), aBounds.Width() - aRect.XMost(),
85 aRect.Height(), aRect.Width());
86 case ROTATION_180:
87 return nsIntRect(aBounds.Width() - aRect.XMost(),
88 aBounds.Height() - aRect.YMost(), aRect.Width(),
89 aRect.Height());
90 case ROTATION_270:
91 return nsIntRect(aBounds.Height() - aRect.YMost(), aRect.X(),
92 aRect.Height(), aRect.Width());
93 default:
94 MOZ_CRASH("Unknown rotation");
98 namespace widget {
100 uint32_t WidgetUtils::IsTouchDeviceSupportPresent() {
101 #ifdef XP_WIN
102 return WinUtils::IsTouchDeviceSupportPresent();
103 #elif defined(MOZ_WIDGET_GTK)
104 return WidgetUtilsGTK::IsTouchDeviceSupportPresent();
105 #else
106 return 0;
107 #endif
110 // static
111 void WidgetUtils::SendBidiKeyboardInfoToContent() {
112 nsCOMPtr<nsIBidiKeyboard> bidiKeyboard = nsContentUtils::GetBidiKeyboard();
113 if (!bidiKeyboard) {
114 return;
117 bool rtl;
118 if (NS_FAILED(bidiKeyboard->IsLangRTL(&rtl))) {
119 return;
121 bool bidiKeyboards = false;
122 bidiKeyboard->GetHaveBidiKeyboards(&bidiKeyboards);
124 nsTArray<dom::ContentParent*> children;
125 dom::ContentParent::GetAll(children);
126 for (uint32_t i = 0; i < children.Length(); i++) {
127 Unused << children[i]->SendBidiKeyboardNotify(rtl, bidiKeyboards);
131 // static
132 void WidgetUtils::GetBrandShortName(nsAString& aBrandName) {
133 aBrandName.Truncate();
135 nsCOMPtr<nsIStringBundleService> bundleService =
136 mozilla::components::StringBundle::Service();
138 nsCOMPtr<nsIStringBundle> bundle;
139 if (bundleService) {
140 bundleService->CreateBundle("chrome://branding/locale/brand.properties",
141 getter_AddRefs(bundle));
144 if (bundle) {
145 bundle->GetStringFromName("brandShortName", aBrandName);
149 const char* WidgetUtils::GetSnapInstanceName() {
150 char* instanceName = PR_GetEnv("SNAP_INSTANCE_NAME");
151 if (instanceName != nullptr) {
152 return instanceName;
154 // Compatibility for snapd <= 2.35:
155 return PR_GetEnv("SNAP_NAME");
158 } // namespace widget
159 } // namespace mozilla