summaryrefslogtreecommitdiffstats
path: root/ui/gfx
diff options
context:
space:
mode:
authorderat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-25 00:39:35 +0000
committerderat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-25 00:39:35 +0000
commit6b7d954ff37811b45fac57e34418ff1c169b6ef8 (patch)
treef9b48972f6343d85508e1277bc9c524d7cb222a2 /ui/gfx
parented5c8219cf7da9a89d3da219c09f777ac3998ba9 (diff)
downloadchromium_src-6b7d954ff37811b45fac57e34418ff1c169b6ef8.zip
chromium_src-6b7d954ff37811b45fac57e34418ff1c169b6ef8.tar.gz
chromium_src-6b7d954ff37811b45fac57e34418ff1c169b6ef8.tar.bz2
Move DisplayUtils methods into gfx::Screen.
These methods are currently just used by metrics. I moved them from base into the browser a while back, but I think it makes the most sense for them to live in gfx::Screen. I'm also relocating the tests to ui_unittest and making them work on Aura. BUG=99711,100341 TEST=ran ui_unittest Review URL: http://codereview.chromium.org/8382019 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@107027 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r--ui/gfx/screen.h12
-rw-r--r--ui/gfx/screen_aura.cc16
-rw-r--r--ui/gfx/screen_gtk.cc15
-rw-r--r--ui/gfx/screen_mac.mm43
-rw-r--r--ui/gfx/screen_unittest.cc45
-rw-r--r--ui/gfx/screen_win.cc12
6 files changed, 137 insertions, 6 deletions
diff --git a/ui/gfx/screen.h b/ui/gfx/screen.h
index 0c712d47..45a15ad 100644
--- a/ui/gfx/screen.h
+++ b/ui/gfx/screen.h
@@ -9,6 +9,7 @@
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
+#include "ui/gfx/size.h"
namespace gfx {
@@ -26,6 +27,7 @@ class UI_EXPORT Screen {
static void SetInstance(Screen* screen);
#endif
+ // Returns the current absolute position of the mouse pointer.
static gfx::Point GetCursorScreenPoint();
// Returns the work area of the monitor nearest the specified window.
@@ -44,6 +46,14 @@ class UI_EXPORT Screen {
// Returns the window under the cursor.
static gfx::NativeWindow GetWindowAtCursorScreenPoint();
+ // Returns the dimensions of the primary monitor in pixels.
+ static gfx::Size GetPrimaryMonitorSize();
+
+ // Returns the number of monitors.
+ // Mirrored displays are excluded; this method is intended to return the
+ // number of distinct, usable displays.
+ static int GetNumMonitors();
+
protected:
virtual gfx::Point GetCursorScreenPointImpl() = 0;
virtual gfx::Rect GetMonitorWorkAreaNearestWindowImpl(
@@ -54,6 +64,8 @@ class UI_EXPORT Screen {
const gfx::Point& point) = 0;
virtual gfx::Rect GetMonitorAreaNearestPointImpl(const gfx::Point& point) = 0;
virtual gfx::NativeWindow GetWindowAtCursorScreenPointImpl() = 0;
+ virtual gfx::Size GetPrimaryMonitorSizeImpl() = 0;
+ virtual int GetNumMonitorsImpl() = 0;
private:
#if defined(USE_AURA)
diff --git a/ui/gfx/screen_aura.cc b/ui/gfx/screen_aura.cc
index 097fe8c..0093af6 100644
--- a/ui/gfx/screen_aura.cc
+++ b/ui/gfx/screen_aura.cc
@@ -24,38 +24,42 @@ void Screen::SetInstance(Screen* screen) {
// static
gfx::Point Screen::GetCursorScreenPoint() {
- DCHECK(instance_);
return instance_->GetCursorScreenPointImpl();
}
// static
gfx::Rect Screen::GetMonitorWorkAreaNearestWindow(gfx::NativeWindow window) {
- DCHECK(instance_);
return instance_->GetMonitorWorkAreaNearestWindowImpl(window);
}
// static
gfx::Rect Screen::GetMonitorAreaNearestWindow(gfx::NativeWindow window) {
- DCHECK(instance_);
return instance_->GetMonitorAreaNearestWindowImpl(window);
}
// static
gfx::Rect Screen::GetMonitorWorkAreaNearestPoint(const gfx::Point& point) {
- DCHECK(instance_);
return instance_->GetMonitorWorkAreaNearestPointImpl(point);
}
// static
gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) {
- DCHECK(instance_);
return instance_->GetMonitorAreaNearestPointImpl(point);
}
// static
gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() {
- DCHECK(instance_);
return instance_->GetWindowAtCursorScreenPointImpl();
}
+// static
+gfx::Size Screen::GetPrimaryMonitorSize() {
+ return instance_->GetPrimaryMonitorSizeImpl();
+}
+
+// static
+int Screen::GetNumMonitors() {
+ return instance_->GetNumMonitorsImpl();
+}
+
} // namespace gfx
diff --git a/ui/gfx/screen_gtk.cc b/ui/gfx/screen_gtk.cc
index 81904e1..42799b7 100644
--- a/ui/gfx/screen_gtk.cc
+++ b/ui/gfx/screen_gtk.cc
@@ -63,6 +63,7 @@ gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) {
return gfx::Rect(bounds);
}
+// static
gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() {
GdkWindow* window = gdk_window_at_pointer(NULL, NULL);
if (!window)
@@ -77,4 +78,18 @@ gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() {
return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL;
}
+// static
+gfx::Size Screen::GetPrimaryMonitorSize() {
+ GdkScreen* screen = gdk_screen_get_default();
+ return gfx::Size(gdk_screen_get_width(screen), gdk_screen_get_height(screen));
+}
+
+// static
+int Screen::GetNumMonitors() {
+ // This query is kinda bogus for Linux -- do we want number of X screens?
+ // The number of monitors Xinerama has? We'll just use whatever GDK uses.
+ GdkScreen* screen = gdk_screen_get_default();
+ return gdk_screen_get_n_monitors(screen);
+}
+
} // namespace gfx
diff --git a/ui/gfx/screen_mac.mm b/ui/gfx/screen_mac.mm
index b38344a..6bf0992 100644
--- a/ui/gfx/screen_mac.mm
+++ b/ui/gfx/screen_mac.mm
@@ -4,6 +4,7 @@
#include "ui/gfx/screen.h"
+#import <ApplicationServices/ApplicationServices.h>
#import <Cocoa/Cocoa.h>
namespace gfx {
@@ -17,4 +18,46 @@ gfx::Point Screen::GetCursorScreenPoint() {
return gfx::Point(mouseLocation.x, mouseLocation.y);
}
+// static
+gfx::Size Screen::GetPrimaryMonitorSize() {
+ CGDirectDisplayID main_display = CGMainDisplayID();
+ return gfx::Size(CGDisplayPixelsWide(main_display),
+ CGDisplayPixelsHigh(main_display));
+}
+
+// static
+int Screen::GetNumMonitors() {
+ // Don't just return the number of online displays. It includes displays
+ // that mirror other displays, which are not desired in the count. It's
+ // tempting to use the count returned by CGGetActiveDisplayList, but active
+ // displays exclude sleeping displays, and those are desired in the count.
+
+ // It would be ridiculous to have this many displays connected, but
+ // CGDirectDisplayID is just an integer, so supporting up to this many
+ // doesn't hurt.
+ CGDirectDisplayID online_displays[128];
+ CGDisplayCount online_display_count = 0;
+ if (CGGetOnlineDisplayList(arraysize(online_displays),
+ online_displays,
+ &online_display_count) != kCGErrorSuccess) {
+ // 1 is a reasonable assumption.
+ return 1;
+ }
+
+ int display_count = 0;
+ for (CGDisplayCount online_display_index = 0;
+ online_display_index < online_display_count;
+ ++online_display_index) {
+ CGDirectDisplayID online_display = online_displays[online_display_index];
+ if (CGDisplayMirrorsDisplay(online_display) == kCGNullDirectDisplay) {
+ // If this display doesn't mirror any other, include it in the count.
+ // The primary display in a mirrored set will be counted, but those that
+ // mirror it will not be.
+ ++display_count;
+ }
+ }
+
+ return display_count;
+}
+
} // namespace gfx
diff --git a/ui/gfx/screen_unittest.cc b/ui/gfx/screen_unittest.cc
new file mode 100644
index 0000000..4b1a572
--- /dev/null
+++ b/ui/gfx/screen_unittest.cc
@@ -0,0 +1,45 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/gfx/screen.h"
+
+#if defined(USE_AURA)
+#include "base/message_loop.h"
+#include "ui/aura/desktop.h"
+#endif
+
+namespace {
+
+#if defined(USE_AURA)
+class ScreenTest : public testing::Test {
+ public:
+ ScreenTest() {
+ aura::Desktop::GetInstance()->ShowDesktop();
+ }
+
+ virtual ~ScreenTest() {
+ aura::Desktop::GetInstance()->DeleteInstanceForTesting();
+ }
+
+ private:
+ MessageLoopForUI message_loop_;
+};
+#else
+typedef testing::Test ScreenTest;
+#endif
+
+TEST_F(ScreenTest, GetPrimaryMonitorSize) {
+ // We aren't actually testing that it's correct, just that it's sane.
+ const gfx::Size size = gfx::Screen::GetPrimaryMonitorSize();
+ EXPECT_GE(size.width(), 1);
+ EXPECT_GE(size.height(), 1);
+}
+
+TEST_F(ScreenTest, GetNumMonitors) {
+ // We aren't actually testing that it's correct, just that it's sane.
+ EXPECT_GE(gfx::Screen::GetNumMonitors(), 1);
+}
+
+} // namespace
diff --git a/ui/gfx/screen_win.cc b/ui/gfx/screen_win.cc
index 530cebe..2873b72 100644
--- a/ui/gfx/screen_win.cc
+++ b/ui/gfx/screen_win.cc
@@ -54,9 +54,21 @@ gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) {
return GetMonitorAreaOrWorkAreaNearestPoint(point, false);
}
+// static
gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() {
POINT location;
return GetCursorPos(&location) ? WindowFromPoint(location) : NULL;
}
+// static
+gfx::Size Screen::GetPrimaryMonitorSize() {
+ return gfx::Size(GetSystemMetrics(SM_CXSCREEN),
+ GetSystemMetrics(SM_CYSCREEN));
+}
+
+// static
+int Screen::GetNumMonitors() {
+ return GetSystemMetrics(SM_CMONITORS);
+}
+
} // namespace gfx