|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include <QGuiApplication> |
|
6 #include <QScreen> |
|
7 |
|
8 #include "nsScreenManagerQt.h" |
|
9 #include "nsScreenQt.h" |
|
10 |
|
11 nsScreenManagerQt::nsScreenManagerQt() |
|
12 { |
|
13 mInitialized = false; |
|
14 desktop = 0; |
|
15 screens = 0; |
|
16 } |
|
17 |
|
18 nsScreenManagerQt::~nsScreenManagerQt() |
|
19 { |
|
20 delete [] screens; |
|
21 } |
|
22 |
|
23 // addref, release, QI |
|
24 NS_IMPL_ISUPPORTS(nsScreenManagerQt, nsIScreenManager) |
|
25 |
|
26 void nsScreenManagerQt::init() |
|
27 { |
|
28 if (mInitialized) |
|
29 return; |
|
30 |
|
31 nScreens = QGuiApplication::screens().size(); |
|
32 screens = new nsCOMPtr<nsIScreen>[nScreens]; |
|
33 |
|
34 for (int i = 0; i < nScreens; ++i) |
|
35 screens[i] = new nsScreenQt(i); |
|
36 mInitialized = true; |
|
37 } |
|
38 |
|
39 // |
|
40 // ScreenForRect |
|
41 // |
|
42 // Returns the screen that contains the rectangle. If the rect overlaps |
|
43 // multiple screens, it picks the screen with the greatest area of intersection. |
|
44 // |
|
45 // The coordinates are in pixels (not twips) and in screen coordinates. |
|
46 // |
|
47 NS_IMETHODIMP |
|
48 nsScreenManagerQt::ScreenForRect(int32_t inLeft, int32_t inTop, |
|
49 int32_t inWidth, int32_t inHeight, |
|
50 nsIScreen **outScreen) |
|
51 { |
|
52 if (!mInitialized) |
|
53 init(); |
|
54 |
|
55 QRect r(inLeft, inTop, inWidth, inHeight); |
|
56 int best = 0; |
|
57 int area = 0; |
|
58 for (int i = 0; i < nScreens; ++i) { |
|
59 const QRect& rect = QGuiApplication::screens()[i]->geometry(); |
|
60 QRect intersection = r▭ |
|
61 int a = intersection.width()*intersection.height(); |
|
62 if (a > area) { |
|
63 best = i; |
|
64 area = a; |
|
65 } |
|
66 } |
|
67 |
|
68 NS_IF_ADDREF(*outScreen = screens[best]); |
|
69 return NS_OK; |
|
70 } |
|
71 |
|
72 // |
|
73 // GetPrimaryScreen |
|
74 // |
|
75 // The screen with the menubar/taskbar. This shouldn't be needed very |
|
76 // often. |
|
77 // |
|
78 NS_IMETHODIMP |
|
79 nsScreenManagerQt::GetPrimaryScreen(nsIScreen **aPrimaryScreen) |
|
80 { |
|
81 if (!desktop) |
|
82 init(); |
|
83 |
|
84 NS_IF_ADDREF(*aPrimaryScreen = screens[0]); |
|
85 return NS_OK; |
|
86 } |
|
87 |
|
88 // |
|
89 // GetNumberOfScreens |
|
90 // |
|
91 // Returns how many physical screens are available. |
|
92 // |
|
93 NS_IMETHODIMP |
|
94 nsScreenManagerQt::GetNumberOfScreens(uint32_t *aNumberOfScreens) |
|
95 { |
|
96 if (!desktop) |
|
97 init(); |
|
98 |
|
99 *aNumberOfScreens = nScreens; |
|
100 return NS_OK; |
|
101 } |
|
102 |
|
103 NS_IMETHODIMP |
|
104 nsScreenManagerQt::GetSystemDefaultScale(float *aDefaultScale) |
|
105 { |
|
106 *aDefaultScale = 1.0f; |
|
107 return NS_OK; |
|
108 } |
|
109 |
|
110 NS_IMETHODIMP |
|
111 nsScreenManagerQt::ScreenForNativeWidget(void *aWidget, nsIScreen **outScreen) |
|
112 { |
|
113 // I don't know how to go from GtkWindow to nsIScreen, especially |
|
114 // given xinerama and stuff, so let's just do this |
|
115 QRect rect(0, 0, 1, 1); |
|
116 return ScreenForRect(rect.x(), rect.y(), rect.width(), rect.height(), outScreen); |
|
117 } |