widget/qt/nsScreenManagerQt.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/qt/nsScreenManagerQt.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <QGuiApplication>
     1.9 +#include <QScreen>
    1.10 +
    1.11 +#include "nsScreenManagerQt.h"
    1.12 +#include "nsScreenQt.h"
    1.13 +
    1.14 +nsScreenManagerQt::nsScreenManagerQt()
    1.15 +{
    1.16 +    mInitialized = false;
    1.17 +    desktop = 0;
    1.18 +    screens = 0;
    1.19 +}
    1.20 +
    1.21 +nsScreenManagerQt::~nsScreenManagerQt()
    1.22 +{
    1.23 +    delete [] screens;
    1.24 +}
    1.25 +
    1.26 +// addref, release, QI
    1.27 +NS_IMPL_ISUPPORTS(nsScreenManagerQt, nsIScreenManager)
    1.28 +
    1.29 +void nsScreenManagerQt::init()
    1.30 +{
    1.31 +    if (mInitialized)
    1.32 +        return;
    1.33 +
    1.34 +    nScreens = QGuiApplication::screens().size();
    1.35 +    screens = new nsCOMPtr<nsIScreen>[nScreens];
    1.36 +
    1.37 +    for (int i = 0; i < nScreens; ++i)
    1.38 +        screens[i] = new nsScreenQt(i);
    1.39 +    mInitialized = true;
    1.40 +}
    1.41 +
    1.42 +//
    1.43 +// ScreenForRect
    1.44 +//
    1.45 +// Returns the screen that contains the rectangle. If the rect overlaps
    1.46 +// multiple screens, it picks the screen with the greatest area of intersection.
    1.47 +//
    1.48 +// The coordinates are in pixels (not twips) and in screen coordinates.
    1.49 +//
    1.50 +NS_IMETHODIMP
    1.51 +nsScreenManagerQt::ScreenForRect(int32_t inLeft, int32_t inTop,
    1.52 +                                 int32_t inWidth, int32_t inHeight,
    1.53 +                                 nsIScreen **outScreen)
    1.54 +{
    1.55 +    if (!mInitialized)
    1.56 +        init();
    1.57 +
    1.58 +    QRect r(inLeft, inTop, inWidth, inHeight);
    1.59 +    int best = 0;
    1.60 +    int area = 0;
    1.61 +    for (int i = 0; i < nScreens; ++i) {
    1.62 +        const QRect& rect = QGuiApplication::screens()[i]->geometry();
    1.63 +        QRect intersection = r&rect;
    1.64 +        int a = intersection.width()*intersection.height();
    1.65 +        if (a > area) {
    1.66 +            best = i;
    1.67 +            area = a;
    1.68 +        }
    1.69 +    }
    1.70 +
    1.71 +    NS_IF_ADDREF(*outScreen = screens[best]);
    1.72 +    return NS_OK;
    1.73 +}
    1.74 +
    1.75 +//
    1.76 +// GetPrimaryScreen
    1.77 +//
    1.78 +// The screen with the menubar/taskbar. This shouldn't be needed very
    1.79 +// often.
    1.80 +//
    1.81 +NS_IMETHODIMP
    1.82 +nsScreenManagerQt::GetPrimaryScreen(nsIScreen **aPrimaryScreen)
    1.83 +{
    1.84 +    if (!desktop)
    1.85 +        init();
    1.86 +
    1.87 +    NS_IF_ADDREF(*aPrimaryScreen = screens[0]);
    1.88 +    return NS_OK;
    1.89 +}
    1.90 +
    1.91 +//
    1.92 +// GetNumberOfScreens
    1.93 +//
    1.94 +// Returns how many physical screens are available.
    1.95 +//
    1.96 +NS_IMETHODIMP
    1.97 +nsScreenManagerQt::GetNumberOfScreens(uint32_t *aNumberOfScreens)
    1.98 +{
    1.99 +    if (!desktop)
   1.100 +        init();
   1.101 +
   1.102 +    *aNumberOfScreens = nScreens;
   1.103 +    return NS_OK;
   1.104 +}
   1.105 +
   1.106 +NS_IMETHODIMP
   1.107 +nsScreenManagerQt::GetSystemDefaultScale(float *aDefaultScale)
   1.108 +{
   1.109 +    *aDefaultScale = 1.0f;
   1.110 +    return NS_OK;
   1.111 +}
   1.112 +
   1.113 +NS_IMETHODIMP
   1.114 +nsScreenManagerQt::ScreenForNativeWidget(void *aWidget, nsIScreen **outScreen)
   1.115 +{
   1.116 +    // I don't know how to go from GtkWindow to nsIScreen, especially
   1.117 +    // given xinerama and stuff, so let's just do this
   1.118 +    QRect rect(0, 0, 1, 1);
   1.119 +    return ScreenForRect(rect.x(), rect.y(), rect.width(), rect.height(), outScreen);
   1.120 +}

mercurial