Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsScreenManagerCocoa.h" |
michael@0 | 7 | #include "nsObjCExceptions.h" |
michael@0 | 8 | #include "nsCOMPtr.h" |
michael@0 | 9 | #include "nsCocoaUtils.h" |
michael@0 | 10 | |
michael@0 | 11 | NS_IMPL_ISUPPORTS(nsScreenManagerCocoa, nsIScreenManager) |
michael@0 | 12 | |
michael@0 | 13 | nsScreenManagerCocoa::nsScreenManagerCocoa() |
michael@0 | 14 | { |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | nsScreenManagerCocoa::~nsScreenManagerCocoa() |
michael@0 | 18 | { |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | nsScreenCocoa* |
michael@0 | 22 | nsScreenManagerCocoa::ScreenForCocoaScreen(NSScreen *screen) |
michael@0 | 23 | { |
michael@0 | 24 | for (uint32_t i = 0; i < mScreenList.Length(); ++i) { |
michael@0 | 25 | nsScreenCocoa* sc = mScreenList[i]; |
michael@0 | 26 | if (sc->CocoaScreen() == screen) { |
michael@0 | 27 | // doesn't addref |
michael@0 | 28 | return sc; |
michael@0 | 29 | } |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | // didn't find it; create and insert |
michael@0 | 33 | nsRefPtr<nsScreenCocoa> sc = new nsScreenCocoa(screen); |
michael@0 | 34 | mScreenList.AppendElement(sc); |
michael@0 | 35 | return sc.get(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | NS_IMETHODIMP |
michael@0 | 39 | nsScreenManagerCocoa::ScreenForRect (int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight, |
michael@0 | 40 | nsIScreen **outScreen) |
michael@0 | 41 | { |
michael@0 | 42 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 43 | |
michael@0 | 44 | NSEnumerator *screenEnum = [[NSScreen screens] objectEnumerator]; |
michael@0 | 45 | NSRect inRect = nsCocoaUtils::GeckoRectToCocoaRect(nsIntRect(aX, aY, aWidth, aHeight)); |
michael@0 | 46 | NSScreen *screenWindowIsOn = [NSScreen mainScreen]; |
michael@0 | 47 | float greatestArea = 0; |
michael@0 | 48 | |
michael@0 | 49 | while (NSScreen *screen = [screenEnum nextObject]) { |
michael@0 | 50 | NSDictionary *desc = [screen deviceDescription]; |
michael@0 | 51 | if ([desc objectForKey:NSDeviceIsScreen] == nil) |
michael@0 | 52 | continue; |
michael@0 | 53 | |
michael@0 | 54 | NSRect r = NSIntersectionRect([screen frame], inRect); |
michael@0 | 55 | float area = r.size.width * r.size.height; |
michael@0 | 56 | if (area > greatestArea) { |
michael@0 | 57 | greatestArea = area; |
michael@0 | 58 | screenWindowIsOn = screen; |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | *outScreen = ScreenForCocoaScreen(screenWindowIsOn); |
michael@0 | 63 | NS_ADDREF(*outScreen); |
michael@0 | 64 | return NS_OK; |
michael@0 | 65 | |
michael@0 | 66 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | NS_IMETHODIMP |
michael@0 | 70 | nsScreenManagerCocoa::GetPrimaryScreen (nsIScreen **outScreen) |
michael@0 | 71 | { |
michael@0 | 72 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 73 | |
michael@0 | 74 | // the mainScreen is the screen with the "key window" (focus, I assume?) |
michael@0 | 75 | NSScreen *sc = [[NSScreen screens] objectAtIndex:0]; |
michael@0 | 76 | |
michael@0 | 77 | *outScreen = ScreenForCocoaScreen(sc); |
michael@0 | 78 | NS_ADDREF(*outScreen); |
michael@0 | 79 | |
michael@0 | 80 | return NS_OK; |
michael@0 | 81 | |
michael@0 | 82 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | NS_IMETHODIMP |
michael@0 | 86 | nsScreenManagerCocoa::GetNumberOfScreens (uint32_t *aNumberOfScreens) |
michael@0 | 87 | { |
michael@0 | 88 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 89 | |
michael@0 | 90 | NSArray *ss = [NSScreen screens]; |
michael@0 | 91 | |
michael@0 | 92 | *aNumberOfScreens = [ss count]; |
michael@0 | 93 | |
michael@0 | 94 | return NS_OK; |
michael@0 | 95 | |
michael@0 | 96 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | NS_IMETHODIMP |
michael@0 | 100 | nsScreenManagerCocoa::GetSystemDefaultScale(float *aDefaultScale) |
michael@0 | 101 | { |
michael@0 | 102 | *aDefaultScale = 1.0f; |
michael@0 | 103 | return NS_OK; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | NS_IMETHODIMP |
michael@0 | 107 | nsScreenManagerCocoa::ScreenForNativeWidget (void *nativeWidget, nsIScreen **outScreen) |
michael@0 | 108 | { |
michael@0 | 109 | NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 110 | |
michael@0 | 111 | NSWindow *window = static_cast<NSWindow*>(nativeWidget); |
michael@0 | 112 | if (window) { |
michael@0 | 113 | nsIScreen *screen = ScreenForCocoaScreen([window screen]); |
michael@0 | 114 | *outScreen = screen; |
michael@0 | 115 | NS_ADDREF(*outScreen); |
michael@0 | 116 | return NS_OK; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | *outScreen = nullptr; |
michael@0 | 120 | return NS_OK; |
michael@0 | 121 | |
michael@0 | 122 | NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
michael@0 | 123 | } |