diff options
author | oshima@google.com <oshima@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-22 22:45:03 +0000 |
---|---|---|
committer | oshima@google.com <oshima@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-22 22:45:03 +0000 |
commit | 8c7f65db00d5994cc430be11934ec564a894ec76 (patch) | |
tree | fac501ea2588816be1665529bcda1d7093dddccf /ui | |
parent | 9b867976a757b80ed566a49736d1c3591e7cc3e4 (diff) | |
download | chromium_src-8c7f65db00d5994cc430be11934ec564a894ec76.zip chromium_src-8c7f65db00d5994cc430be11934ec564a894ec76.tar.gz chromium_src-8c7f65db00d5994cc430be11934ec564a894ec76.tar.bz2 |
Move screen.h to ui/gfx because Screen doesn't depend on views, and they're useful outside views.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/7483014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93724 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gfx/screen.h | 41 | ||||
-rw-r--r-- | ui/gfx/screen_gtk.cc | 107 | ||||
-rw-r--r-- | ui/gfx/screen_win.cc | 62 | ||||
-rw-r--r-- | ui/ui.gyp | 3 |
4 files changed, 213 insertions, 0 deletions
diff --git a/ui/gfx/screen.h b/ui/gfx/screen.h new file mode 100644 index 0000000..60b9b7d --- /dev/null +++ b/ui/gfx/screen.h @@ -0,0 +1,41 @@ +// 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. + +#ifndef UI_GFX_SCREEN_H_ +#define UI_GFX_SCREEN_H_ +#pragma once + +#include "ui/gfx/native_widget_types.h" +#include "ui/gfx/point.h" +#include "ui/gfx/rect.h" + +namespace gfx { + +// A utility class for getting various info about screen size, monitors, +// cursor position, etc. +// TODO(erikkay) add more of those methods here +class Screen { + public: + static gfx::Point GetCursorScreenPoint(); + + // Returns the work area of the monitor nearest the specified window. + static gfx::Rect GetMonitorWorkAreaNearestWindow(gfx::NativeView view); + + // Returns the bounds of the monitor nearest the specified window. + static gfx::Rect GetMonitorAreaNearestWindow(gfx::NativeView view); + + // Returns the work area of the monitor nearest the specified point. + static gfx::Rect GetMonitorWorkAreaNearestPoint(const gfx::Point& point); + + // Returns the monitor area (not the work area, but the complete bounds) of + // the monitor nearest the specified point. + static gfx::Rect GetMonitorAreaNearestPoint(const gfx::Point& point); + + // Returns the window under the cursor. + static gfx::NativeWindow GetWindowAtCursorScreenPoint(); +}; + +} // namespace gfx + +#endif // VIEWS_SCREEN_H_ diff --git a/ui/gfx/screen_gtk.cc b/ui/gfx/screen_gtk.cc new file mode 100644 index 0000000..bb71da5 --- /dev/null +++ b/ui/gfx/screen_gtk.cc @@ -0,0 +1,107 @@ +// 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 "ui/gfx/screen.h" + +#include <gdk/gdkx.h> +#include <gtk/gtk.h> + +#include "base/logging.h" + +namespace gfx { + +// static +gfx::Point Screen::GetCursorScreenPoint() { + gint x, y; + gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL); + return gfx::Point(x, y); +} + +gfx::Rect static GetPrimaryMonitorBounds() { + guchar* raw_data = NULL; + gint data_len = 0; + gboolean success = gdk_property_get(gdk_get_default_root_window(), + gdk_atom_intern("_NET_WORKAREA", FALSE), + gdk_atom_intern("CARDINAL", FALSE), + 0, 0xFF, false, NULL, NULL, &data_len, + &raw_data); + int top_left_x = 0; + int top_left_y = 0; + int width = 0; + int height = 0; + + if (success) { + glong* data = reinterpret_cast<glong*>(raw_data); + top_left_x = data[0]; + top_left_y = data[1]; + width = data[2]; + height = data[3]; + g_free(raw_data); + } else { + // If there's no window manager, we can ask X for Monitor info directly. + XWindowAttributes attributes; + Status status = XGetWindowAttributes(gdk_x11_get_default_xdisplay(), + gdk_x11_get_default_root_xwindow(), + &attributes); + if (status) { + top_left_x = attributes.x; + top_left_y = attributes.y; + width = attributes.width; + height = attributes.height; + success = true; + } + } + DCHECK(success); + return gfx::Rect(top_left_x, top_left_y, width, height); +} + +// static +gfx::Rect Screen::GetMonitorWorkAreaNearestWindow(gfx::NativeView view) { + // TODO(beng): use |view|. + return GetPrimaryMonitorBounds(); +} + +// static +gfx::Rect Screen::GetMonitorAreaNearestWindow(gfx::NativeView view) { + GtkWidget* top_level = gtk_widget_get_toplevel(view); + DCHECK(GTK_IS_WINDOW(top_level)); + GtkWindow* window = GTK_WINDOW(top_level); + GdkScreen* screen = gtk_window_get_screen(window); + gint monitor_num = gdk_screen_get_monitor_at_window(screen, + top_level->window); + GdkRectangle bounds; + gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds); + return gfx::Rect(bounds); +} + +// static +gfx::Rect Screen::GetMonitorWorkAreaNearestPoint(const gfx::Point& point) { + // TODO(jamiewalch): Restrict this to the work area of the monitor. + return GetMonitorAreaNearestPoint(point); +} + +// static +gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) { + GdkScreen* screen = gdk_screen_get_default(); + gint monitor = gdk_screen_get_monitor_at_point(screen, point.x(), point.y()); + GdkRectangle bounds; + gdk_screen_get_monitor_geometry(screen, monitor, &bounds); + return gfx::Rect(bounds); +} + +gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { + GdkWindow* window = gdk_window_at_pointer(NULL, NULL); + if (!window) + return NULL; + + gpointer data = NULL; + gdk_window_get_user_data(window, &data); + GtkWidget* widget = reinterpret_cast<GtkWidget*>(data); + if (!widget) + return NULL; + widget = gtk_widget_get_toplevel(widget); + return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL; +} + +} // namespace gfx diff --git a/ui/gfx/screen_win.cc b/ui/gfx/screen_win.cc new file mode 100644 index 0000000..530cebe --- /dev/null +++ b/ui/gfx/screen_win.cc @@ -0,0 +1,62 @@ +// 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 "ui/gfx/screen.h" + +#include <windows.h> + +namespace gfx { + +// static +gfx::Point Screen::GetCursorScreenPoint() { + POINT pt; + GetCursorPos(&pt); + return gfx::Point(pt); +} + +// static +gfx::Rect Screen::GetMonitorWorkAreaNearestWindow(gfx::NativeWindow window) { + MONITORINFO monitor_info; + monitor_info.cbSize = sizeof(monitor_info); + GetMonitorInfo(MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST), + &monitor_info); + return gfx::Rect(monitor_info.rcWork); +} + +// static +gfx::Rect Screen::GetMonitorAreaNearestWindow(gfx::NativeWindow window) { + MONITORINFO monitor_info; + monitor_info.cbSize = sizeof(monitor_info); + GetMonitorInfo(MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST), + &monitor_info); + return gfx::Rect(monitor_info.rcMonitor); +} + +static gfx::Rect GetMonitorAreaOrWorkAreaNearestPoint(const gfx::Point& point, + bool work_area) { + POINT initial_loc = { point.x(), point.y() }; + HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); + MONITORINFO mi = {0}; + mi.cbSize = sizeof(mi); + if (monitor && GetMonitorInfo(monitor, &mi)) + return gfx::Rect(work_area ? mi.rcWork : mi.rcMonitor); + return gfx::Rect(); +} + +// static +gfx::Rect Screen::GetMonitorWorkAreaNearestPoint(const gfx::Point& point) { + return GetMonitorAreaOrWorkAreaNearestPoint(point, true); +} + +// static +gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) { + return GetMonitorAreaOrWorkAreaNearestPoint(point, false); +} + +gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { + POINT location; + return GetCursorPos(&location) ? WindowFromPoint(location) : NULL; +} + +} // namespace gfx @@ -249,6 +249,9 @@ 'gfx/point.h', 'gfx/rect.cc', 'gfx/rect.h', + 'gfx/screen.h', + 'gfx/screen_gtk.cc', + 'gfx/screen_win.cc', 'gfx/scoped_cg_context_save_gstate_mac.h', 'gfx/scoped_ns_graphics_context_save_gstate_mac.h', 'gfx/scoped_ns_graphics_context_save_gstate_mac.mm', |